08-17-2024, 02:15 AM
Ok, I now see what you were getting at. I incorporated the changes and here are the final results.
ColorDepthTest.BAS
LIB_IMG_ColorDepth.BI
LIB_IMG_ColorDepth.BM
ColorDepthTest.BAS
Code: (Select All)
'$INCLUDE:'lib_img_colordepth.bi' IMG_ColorDepth function
Image1& = _NEWIMAGE(640, 480, 32) ' valid 32 bit color image
Image2& = _NEWIMAGE(640, 480, 256) ' valid 256 color image with altered palette
Image3& = _NEWIMAGE(640, 480, 256) ' valid 256 color image with unaltered palette
Image4& = _NEWIMAGE(640, 480, 12) ' valid 16 color image (Qbasic SCREEN 12)
' Alter Image2&'s palette
FOR i = 18 TO 255 ' colors 18 to 255 are black
_PALETTECOLOR i, &HFF000000&&, Image2&
NEXT
PRINT IMG_ColorDepth(Image1&) ' valid 32 bit image image
PRINT IMG_ColorDepth(Image2&) ' valid 256 color image with altered palette
PRINT IMG_ColorDepth(Image3&) ' valid 256 color image with unaltered palette
PRINT IMG_ColorDepth(Image4&) ' valid 16 color image (Qbasic SCREEN 12)
PRINT IMG_ColorDepth(-30) ' invalid image
'$INCLUDE:'lib_img_colordepth.bm' IMG_ColorDepth function
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
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
DIM fb AS INTEGER ' first black seen
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
' ----------------------------------------
'| 4 bytes per pixel means a 32 bit image |
' ----------------------------------------
IMG_ColorDepth% = 32 ' this is a 32 bit color image
ELSE ' no, 1 byte per pixel
p = 0 ' reset palette counter
c = 0 ' reset color counter
fb = 0 ' reset first black seen
DO ' begin palette search
IF _PALETTECOLOR(p, i) <> &HFF000000 THEN ' color here?
c = c + 1 ' yes, increment color counter
ELSE ' no, black found
IF fb = 0 THEN fb = p ' record first instance of black seen
END IF
p = p + 1 ' increment palette counter
LOOP UNTIL p = 256 ' leave when entire palette searched
IF c < 16 THEN ' less than 16 colors?
' ---------------------------
'| Less than 16 colors found |
' ---------------------------
IMG_ColorDepth% = 16 ' yes, 16 color image
ELSEIF c = 16 THEN ' 16 colors found?
' -------------------------
'| Exactly 16 colors found |
' -------------------------
IF fb <= 16 THEN ' yes, is black within this range?
' ----------------------------------
'| With black in the 16 color range |
' ----------------------------------
IMG_ColorDepth% = 256 ' yes, then at least 17 colors exist
ELSE ' no, black is beyond the initial 16 colors
' ------------------------------------------
'| With black outside of the 16 color range |
' ------------------------------------------
IMG_ColorDepth% = 16 ' this is a 16 color image
END IF
ELSE ' no, greater than 16
' ------------------------------
'| Greater than 16 colors found |
' ------------------------------
IMG_ColorDepth% = 256 ' this is a 256 color image
END IF
END IF
ON ERROR GOTO 0 ' remove ON ERROR redirection
END FUNCTION