08-16-2024, 10:18 PM
On another thread we were discussing the use of ON ERROR to capture when a non image negative value was sent. There's now an associated .BI file that needs to go along with the function.
Here is some example code (ColorDepthTest.BAS):
Here is LIB_IMG_ColorDepth.BI:
And here is LIB_IMG_ColorDepth.BM:
Here is some example code (ColorDepthTest.BAS):
Code: (Select All)
'$INCLUDE:'lib_img_colordepth.bi' IMG_ColorDepth function
Image& = _NEWIMAGE(640, 480, 32) ' valid image
PRINT IMG_ColorDepth(Image&) ' valid image
PRINT IMG_ColorDepth(-30) ' invalid image
'$INCLUDE:'lib_img_colordepth.bm' IMG_ColorDepth function
Here is LIB_IMG_ColorDepth.BI:
Code: (Select All)
'LIB_IMG_ColorDepth.BI
DIM SHARED ColorDepthError AS INTEGER
COLORDEPTH_BIFILE:
IF ERR THEN
ColorDepthError = ERR
RESUME NEXT
END IF
And here is LIB_IMG_ColorDepth.BM:
Code: (Select All)
'LIB_IMG_ColorDepth.BM
FUNCTION IMG_ColorDepth% (i AS LONG)
'+----------------------------------------------------------------------------+
'| Returns the color depth of an image passed in. |
'| |
'| i - the image handle |
'| |
'| Returns: 0 : not an image |
'| 32 : 32 bit color image |
'| 256 : 256 color image |
'| 16 : 16 color image (or text only) |
'| |
'| Note: this function was created from example code provided by SMcNeill at: |
'| https://qb64phoenix.com/forum/showthread.php?tid=2950 |
'+----------------------------------------------------------------------------+
DIM c AS INTEGER ' color counter
DIM p AS INTEGER ' palette counter
IF i > -2 THEN EXIT FUNCTION ' not an image, return 0
ON ERROR GOTO COLORDEPTH_BIFILE ' use error handler in .BI file
IF _PIXELSIZE(i) = 4 THEN ' 4 bytes per pixel?
IF ColorDepthError THEN ' did _PIXELSIZE generate an error?
ColorDepthError = 0 ' yes, reset error flag
IMG_ColorDepth% = 0 ' return 0 (not an image)
ON ERROR GOTO 0 ' remove ON ERROR redirection
EXIT FUNCTION ' leave function
END IF
IMG_ColorDepth% = 32 ' yes, 32 bit color image
ELSE ' no, 1 byte per pixel
p = 0 ' reset palette counter
c = 0 ' reset color counter
DO ' begin palette search
IF _PALETTECOLOR(p, i) <> &HFF000000 THEN c = c + 1 ' increment color counter if color found
p = p + 1 ' increment palette counter
LOOP UNTIL p = 256 ' leave when entire palette searched
IF c <= 16 THEN ' 16 colors or less?
IMG_ColorDepth% = 16 ' yes, 16 color image
ELSE ' no, greater than 16
IMG_ColorDepth% = 256 ' 256 color image
END IF
END IF
ON ERROR GOTO 0 ' remove ON ERROR redirection
END FUNCTION