Difference between revisions of "Grey Scale Bitmaps"

From QB64 Wiki
Jump to navigation Jump to search
imported>Clippy
imported>Clippy
Line 1: Line 1:
 +
Grey scale is necessary for compatibility with Qbasic's legacy [[SCREEN (statement)|SCREEN]] modes when viewing 24/32 bit bitmaps! You may also desire grey scale images for aesthetic or artistic reasons.
 +
 +
* Set each color '''attribute''''s RGB '''intensities''' to the same value while incrementing the attribute values and intensity levels.
 +
 +
* Don't exceed 63 when using [[OUT]] in legacy screen modes 12 or 13. Screens 7 and 9 use [[DAC]] color settings.
 +
 +
* If using a '''QB64''' 32 bit [[_NEWIMAGE]] Screen, use [[_RGB]] or [[RGB32]] values no higher than 255.
 +
 +
* Then set the color '''attributes''' to reflect the number of colors available in that legacy screen mode.
 +
 +
* 24/32 bit do not use color attributes, so the '''intensity levels''' must be set to an average for all three.
 +
 +
 +
<center>'''SCREEN 12 (4 BPP - 16 colors)'''</center>
 
{{CodeStart}}
 
{{CodeStart}}
 
'' ''
 
'' ''
{{Cl|SUB}} True{{Cl|COLOR}}12  'Screen 12 greyscale 24 bit 'code by Bob Seguin
+
{{Cl|SUB}} True{{Cl|COLOR}}12  'Screen 12 greyscale of 24 bit 'code by Bob Seguin
{{Cl|IF}} BMP.PWidth * 3) {{Cl|MOD}} 4) <> 0 {{Cl|THEN}}
+
{{Cl|IF}} BMP.PWidth * 3) {{Cl|MOD}} 4) {{Cl|THEN}}
 
ZeroPAD$ = {{Cl|SPACE$}}((4 - ((BMP.PWidth * 3) {{Cl|MOD}} 4)))
 
ZeroPAD$ = {{Cl|SPACE$}}((4 - ((BMP.PWidth * 3) {{Cl|MOD}} 4)))
 
{{Cl|END IF}}
 
{{Cl|END IF}}
 
n = 3
 
n = 3
{{Cl|FOR...NEXT|FOR}} Colr = 0 {{Cl|TO}} 15
+
{{Cl|FOR...NEXT|FOR}} Colr = 0 {{Cl|TO}} 15 '16 colors
 
         {{Cl|OUT}} {{Cl|&H}}3C8, Colr
 
         {{Cl|OUT}} {{Cl|&H}}3C8, Colr
 +
        {{Cl|OUT}} {{Cl|&H}}3C9, n        'color intensities are each equal in grey scale
 
         {{Cl|OUT}} {{Cl|&H}}3C9, n
 
         {{Cl|OUT}} {{Cl|&H}}3C9, n
 
         {{Cl|OUT}} {{Cl|&H}}3C9, n
 
         {{Cl|OUT}} {{Cl|&H}}3C9, n
        {{Cl|OUT}} {{Cl|&H}}3C9, n
+
         n = n + 4                     'increment intensities. maximum intensity = 63
         n = n + 4
 
 
{{Cl|NEXT}} Colr
 
{{Cl|NEXT}} Colr
y = PictureDEPTH& - 1: o$ = " "
+
y = BMP.PDepth - 1: o$ = " "
 
{{Cl|GET}} #1, BMP.Offset, o$
 
{{Cl|GET}} #1, BMP.Offset, o$
 
R$ = " ": G$ = " ": B$ = " "
 
R$ = " ": G$ = " ": B$ = " "
 
{{Cl|DO}}
 
{{Cl|DO}}
x = 0
+
x = 0                               'set image to left of screen
 
   {{Cl|DO}}
 
   {{Cl|DO}}
    {{Cl|GET}} #1, , a$
 
 
     {{Cl|GET}} #1, , B$
 
     {{Cl|GET}} #1, , B$
     {{Cl|GET}} #1, , c$
+
     {{Cl|GET}} #1, , G$
     b = {{Cl|ASC}}(a$)            'intensities from 0 to 255
+
     {{Cl|GET}} #1, , R$
     g = {{Cl|ASC}}(B$)
+
    red = {{Cl|ASC}}(R$)            'intensities from 0 to 255
     r = {{Cl|ASC}}(c$)
+
     grn = {{Cl|ASC}}(G$)
     average = (r + g + b) \ 48    'divide by 16 colors and divide by 3 for average
+
     blu = {{Cl|ASC}}(B$)
     {{Cl|PSET}} (x, y), average
+
     average = (red + grn + blu) \ 48    'divide by 16 colors and divide by 3 for average
 +
     {{Cl|PSET}} (x, y), average               '255 * 3 divided by 16 = maximum color of 15
 
     x = x + 1
 
     x = x + 1
     {{Cl|LOOP}} {{Cl|WHILE}} x < Picture{{Cl|WIDTH}}&
+
     {{Cl|LOOP}} {{Cl|WHILE}} x < BMP.Pwidth
 
+
     {{Cl|GET}} #1, , ZeroPAD$           'move up one row from bottom
     {{Cl|GET}} #1, , ZeroPAD$
 
 
     y = y - 1
 
     y = y - 1
 
 
{{Cl|LOOP}} {{Cl|UNTIL}} y = -1
 
{{Cl|LOOP}} {{Cl|UNTIL}} y = -1
 
 
{{Cl|END SUB}} '' ''
 
{{Cl|END SUB}} '' ''
  
Line 40: Line 51:
  
  
 +
<center>'''SCREEN 13 (4 or 8 BPP - 256 colors)'''</center>
 
{{CodeStart}}
 
{{CodeStart}}
 
'' ''
 
'' ''
 
{{Cl|SUB}} True{{Cl|COLOR}}13    'Screen 13 greyscale of 24 bit 'code by Bob Seguin
 
{{Cl|SUB}} True{{Cl|COLOR}}13    'Screen 13 greyscale of 24 bit 'code by Bob Seguin
 
 
{{Cl|IF}} ((BMP.PWidth * 3) {{Cl|MOD}} 4) <> 0 {{Cl|THEN}}
 
{{Cl|IF}} ((BMP.PWidth * 3) {{Cl|MOD}} 4) <> 0 {{Cl|THEN}}
 
ZeroPAD$ = {{Cl|SPACE$}}((4 - ((BMP.PWidth * 3) {{Cl|MOD}} 4)))
 
ZeroPAD$ = {{Cl|SPACE$}}((4 - ((BMP.PWidth * 3) {{Cl|MOD}} 4)))
 
{{Cl|END IF}}
 
{{Cl|END IF}}
 
+
{{Cl|FOR...NEXT|FOR}} Colr = 0 {{Cl|TO}} 255 {{Cl|STEP}} 4         '63 different intensities
{{Cl|SCREEN (statement)|SCREEN}} 13
+
     {{Cl|FOR...NEXT|FOR}} Reps = 0 {{Cl|TO}} 3             'set 3 colors at a time
 
 
{{Cl|FOR...NEXT|FOR}} Colr = 0 {{Cl|TO}} 255 {{Cl|STEP}} 4
 
     {{Cl|FOR...NEXT|FOR}} Reps = 0 {{Cl|TO}} 3
 
 
         {{Cl|OUT}} {{Cl|&H}}3C8, Colr + Reps
 
         {{Cl|OUT}} {{Cl|&H}}3C8, Colr + Reps
         {{Cl|OUT}} {{Cl|&H}}3C9, n
+
         {{Cl|OUT}} {{Cl|&H}}3C9, n     'color intensities are each equal in grey scale
 
         {{Cl|OUT}} {{Cl|&H}}3C9, n
 
         {{Cl|OUT}} {{Cl|&H}}3C9, n
 
         {{Cl|OUT}} {{Cl|&H}}3C9, n
 
         {{Cl|OUT}} {{Cl|&H}}3C9, n
 
     {{Cl|NEXT}} Reps
 
     {{Cl|NEXT}} Reps
     n = n + 1
+
     n = n + 1                         'increment intensities. maximum = 63
 
{{Cl|NEXT}} Colr
 
{{Cl|NEXT}} Colr
 +
y = BMP.PDepth - 1: o$ = " "
 +
{{Cl|GET}} #1, BMP.Offset, o$          'offset + 1 immediately after header
 +
R$ = " ": G$ = " ": B$ = " "
 +
{{Cl|DO}}
 +
x = 0                                'set image to left side of screen
 +
    {{Cl|DO}}
 +
    {{Cl|GET}} #1, , B$
 +
    {{Cl|GET}} #1, , G$
 +
    {{Cl|GET}} #1, , R$
 +
    red = {{Cl|ASC}}(R$)
 +
    grn = {{Cl|ASC}}(G$)
 +
    blu = {{Cl|ASC}}(B$)
 +
    average = (red + grn + blu) \ 3 'divide by 3 for average as colors match intensities
 +
    {{Cl|PSET}} (x, y), average            '3 * 255 divided by 3 = maximum color attribute of 255
 +
    x = x + 1
 +
    {{Cl|LOOP}} {{Cl|WHILE}} x < BMP.PWidth
 +
    {{Cl|GET}} #1, , ZeroPAD$                  'skip padder if used
 +
    y = y - 1                            'move up one row from bottom
 +
{{Cl|LOOP}} {{Cl|UNTIL}} y = -1
 +
{{Cl|END}} {{Cl|SUB}} '' ''
 +
 +
{{CodeEnd}}
 +
 +
 +
<center>'''SCREEN [[_NEWIMAGE]]([[_WIDTH (function)|_WIDTH]] h&, [[_HEIGHT]] h&, 32) (24 BPP - 16 million colors)'''</center>
 +
{{CodeStart}}
 +
 +
{{Cl|SUB}} TrueGreyScale  'convert a 24 bit bitmap image to grey scale
 +
{{Cl|IF}} ((BMP.PWidth * 3) {{Cl|MOD}} 4) <> 0 {{Cl|THEN}}
 +
ZeroPAD$ = {{Cl|SPACE$}}((4 - ((BMP.PWidth * 3) {{Cl|MOD}} 4)))
 +
{{Cl|END IF}}
 
y = BMP.PDepth - 1: o$ = " "
 
y = BMP.PDepth - 1: o$ = " "
 
{{Cl|GET}} #1, BMP.Offset, o$  'offset + 1 immediately after header
 
{{Cl|GET}} #1, BMP.Offset, o$  'offset + 1 immediately after header
 
R$ = " ": G$ = " ": B$ = " "
 
R$ = " ": G$ = " ": B$ = " "
 
{{Cl|DO}}
 
{{Cl|DO}}
x = 0                       'set image to left side of screen
+
x = 0                                 'set image to left side of screen
 
     {{Cl|DO}}
 
     {{Cl|DO}}
 
     {{Cl|GET}} #1, , B$
 
     {{Cl|GET}} #1, , B$
Line 71: Line 110:
 
     grn = {{Cl|ASC}}(G$)
 
     grn = {{Cl|ASC}}(G$)
 
     blu = {{Cl|ASC}}(B$)
 
     blu = {{Cl|ASC}}(B$)
     average = (red + grn + blu) \ 3
+
     average = (red + grn + blu) \ 3   'divide by 3 for average as colors match intensities
     {{Cl|PSET}} (x, y), average
+
     {{Cl|PSET}} (x, y), {{Cl|_RGB32}}(average, average, average)  'maximum color intensity = 255
 
     x = x + 1
 
     x = x + 1
 
     {{Cl|LOOP}} {{Cl|WHILE}} x < BMP.PWidth
 
     {{Cl|LOOP}} {{Cl|WHILE}} x < BMP.PWidth
 
     {{Cl|GET}} #1, , ZeroPAD$                  'skip padder if used
 
     {{Cl|GET}} #1, , ZeroPAD$                  'skip padder if used
     y = y - 1                             'move up one row from bottom
+
     y = y - 1                           'move up one row from bottom
 
{{Cl|LOOP}} {{Cl|UNTIL}} y = -1
 
{{Cl|LOOP}} {{Cl|UNTIL}} y = -1
 
{{Cl|END}} {{Cl|SUB}} '' ''
 
{{Cl|END}} {{Cl|SUB}} '' ''
Line 82: Line 121:
 
{{CodeEnd}}
 
{{CodeEnd}}
  
 +
 +
<center>'''Tinting Grey Scale Images'''</center>
 +
 +
: To tint a SCREEN 12 or 13 greyscale image, keep the intensity settings of 1 or 2 colors less than the others.
 +
{{CodeStart}}
 +
'' ''
 +
{{Cl|FOR...NEXT|FOR}} Colr = 0 {{Cl|TO}} 255 {{Cl|STEP}} 4        '63 different intensities
 +
    {{Cl|FOR...NEXT|FOR}} Reps = 0 {{Cl|TO}} 3              'set 3 colors at a time
 +
        {{Cl|OUT}} {{Cl|&H}}3C8, Colr + Reps
 +
        {{Cl|OUT}} {{Cl|&H}}3C9, n - tint    'decrease color intensities of red and green
 +
        {{Cl|OUT}} {{Cl|&H}}3C9, n - tint
 +
        {{Cl|OUT}} {{Cl|&H}}3C9, n          'higher blue intensity will blue the image
 +
    {{Cl|NEXT}} Reps
 +
    n = n + 1                          'increment intensities. maximum = 63
 +
    {{Cl|IF}} n > 5 {{Cl|THEN}} tint = 5          'keep red and green intensity values above 0!
 +
{{Cl|NEXT}} Colr '' ''
 +
 +
{{CodeEnd}}
 +
 +
: In 24/32 bit SCREENs, subtract some intensity from one or two of the 3 pixel settings.
 +
{{CodeStart}}
 +
'' ''
 +
{{Cl|GET}} #1, , B$
 +
    {{Cl|GET}} #1, , G$
 +
    {{Cl|GET}} #1, , R$   
 +
    red = {{Cl|ASC}}(R$) 
 +
    grn = {{Cl|ASC}}(G$)
 +
    blu = {{Cl|ASC}}(B$)
 +
    IF red > Rtint THEN red = red - Rtint 'set one or 2 tint values less than others
 +
    IF grn > Gtint THEN grn = grn - Gtint 'intensity values cannot be less than 0!
 +
    IF blu > Btint THEN blu = blu - Btint 'use small tint values to minimize darkness
 +
    average = (red + grn + blu) \ 3  'divide by 3 for average as colors match intensities
 +
    {{Cl|PSET}} (x, y), {{Cl|_RGB32}}(average, average, average)  'maximum color intensity = 255 '' ''
 +
 +
{{CodeEnd}}
  
  

Revision as of 02:50, 6 September 2010

Grey scale is necessary for compatibility with Qbasic's legacy SCREEN modes when viewing 24/32 bit bitmaps! You may also desire grey scale images for aesthetic or artistic reasons.

  • Set each color attribute's RGB intensities to the same value while incrementing the attribute values and intensity levels.
  • Don't exceed 63 when using OUT in legacy screen modes 12 or 13. Screens 7 and 9 use DAC color settings.
  • Then set the color attributes to reflect the number of colors available in that legacy screen mode.
  • 24/32 bit do not use color attributes, so the intensity levels must be set to an average for all three.


SCREEN 12 (4 BPP - 16 colors)

SUB TrueCOLOR12 'Screen 12 greyscale of 24 bit 'code by Bob Seguin IF BMP.PWidth * 3) MOD 4) THEN ZeroPAD$ = SPACE$((4 - ((BMP.PWidth * 3) MOD 4))) END IF n = 3 FOR Colr = 0 TO 15 '16 colors OUT &H3C8, Colr OUT &H3C9, n 'color intensities are each equal in grey scale OUT &H3C9, n OUT &H3C9, n n = n + 4 'increment intensities. maximum intensity = 63 NEXT Colr y = BMP.PDepth - 1: o$ = " " GET #1, BMP.Offset, o$ R$ = " ": G$ = " ": B$ = " " DO x = 0 'set image to left of screen DO GET #1, , B$ GET #1, , G$ GET #1, , R$ red = ASC(R$) 'intensities from 0 to 255 grn = ASC(G$) blu = ASC(B$) average = (red + grn + blu) \ 48 'divide by 16 colors and divide by 3 for average PSET (x, y), average '255 * 3 divided by 16 = maximum color of 15 x = x + 1 LOOP WHILE x < BMP.Pwidth GET #1, , ZeroPAD$ 'move up one row from bottom y = y - 1 LOOP UNTIL y = -1 END SUB


SCREEN 13 (4 or 8 BPP - 256 colors)

SUB TrueCOLOR13 'Screen 13 greyscale of 24 bit 'code by Bob Seguin IF ((BMP.PWidth * 3) MOD 4) <> 0 THEN ZeroPAD$ = SPACE$((4 - ((BMP.PWidth * 3) MOD 4))) END IF FOR Colr = 0 TO 255 STEP 4 '63 different intensities FOR Reps = 0 TO 3 'set 3 colors at a time OUT &H3C8, Colr + Reps OUT &H3C9, n 'color intensities are each equal in grey scale OUT &H3C9, n OUT &H3C9, n NEXT Reps n = n + 1 'increment intensities. maximum = 63 NEXT Colr y = BMP.PDepth - 1: o$ = " " GET #1, BMP.Offset, o$ 'offset + 1 immediately after header R$ = " ": G$ = " ": B$ = " " DO x = 0 'set image to left side of screen DO GET #1, , B$ GET #1, , G$ GET #1, , R$ red = ASC(R$) grn = ASC(G$) blu = ASC(B$) average = (red + grn + blu) \ 3 'divide by 3 for average as colors match intensities PSET (x, y), average '3 * 255 divided by 3 = maximum color attribute of 255 x = x + 1 LOOP WHILE x < BMP.PWidth GET #1, , ZeroPAD$ 'skip padder if used y = y - 1 'move up one row from bottom LOOP UNTIL y = -1 END SUB


SCREEN _NEWIMAGE(_WIDTH h&, _HEIGHT h&, 32) (24 BPP - 16 million colors)

SUB TrueGreyScale 'convert a 24 bit bitmap image to grey scale IF ((BMP.PWidth * 3) MOD 4) <> 0 THEN ZeroPAD$ = SPACE$((4 - ((BMP.PWidth * 3) MOD 4))) END IF y = BMP.PDepth - 1: o$ = " " GET #1, BMP.Offset, o$ 'offset + 1 immediately after header R$ = " ": G$ = " ": B$ = " " DO x = 0 'set image to left side of screen DO GET #1, , B$ GET #1, , G$ GET #1, , R$ red = ASC(R$) grn = ASC(G$) blu = ASC(B$) average = (red + grn + blu) \ 3 'divide by 3 for average as colors match intensities PSET (x, y), _RGB32(average, average, average) 'maximum color intensity = 255 x = x + 1 LOOP WHILE x < BMP.PWidth GET #1, , ZeroPAD$ 'skip padder if used y = y - 1 'move up one row from bottom LOOP UNTIL y = -1 END SUB


Tinting Grey Scale Images
To tint a SCREEN 12 or 13 greyscale image, keep the intensity settings of 1 or 2 colors less than the others.

FOR Colr = 0 TO 255 STEP 4 '63 different intensities FOR Reps = 0 TO 3 'set 3 colors at a time OUT &H3C8, Colr + Reps OUT &H3C9, n - tint 'decrease color intensities of red and green OUT &H3C9, n - tint OUT &H3C9, n 'higher blue intensity will blue the image NEXT Reps n = n + 1 'increment intensities. maximum = 63 IF n > 5 THEN tint = 5 'keep red and green intensity values above 0! NEXT Colr

In 24/32 bit SCREENs, subtract some intensity from one or two of the 3 pixel settings.

GET #1, , B$ GET #1, , G$ GET #1, , R$ red = ASC(R$) grn = ASC(G$) blu = ASC(B$) IF red > Rtint THEN red = red - Rtint 'set one or 2 tint values less than others IF grn > Gtint THEN grn = grn - Gtint 'intensity values cannot be less than 0! IF blu > Btint THEN blu = blu - Btint 'use small tint values to minimize darkness average = (red + grn + blu) \ 3 'divide by 3 for average as colors match intensities PSET (x, y), _RGB32(average, average, average) 'maximum color intensity = 255


See also:




Navigation:
Keyword Reference - Alphabetical
Keyword Reference - By Usage
Main Wiki Page