Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Detecting color depth-16 color, 256 color, or 32 bit color
#17
(08-16-2024, 09:38 PM)TerryRitchie Wrote:
(08-16-2024, 09:26 PM)SMcNeill Wrote: Set a flag.  That checks for 16 NON-BLACK colors.

If theres a black in those first palette entries, and then 16 other colors, that's NOT a 16-color palette.

For example:

Black
Red
Blue
Green
White
Black * 250

The above would count as 4 non-black colors, but there's a valid Black in the 5-color palette.  I wouldn't call that a 4-color image.

The <= 16 check is for:
  standard 16 color palette (black + 15 others)
OR
  non-black palette (someone swapped black for midnight blue.)

But it's going to glitch with:
  Black + 16 other colors, which is actually a 17-color palette and probably a 256 color image.



Subtle glitch, but one that a library function should account for.  You never know who or where a library ends up being exposed to.  Wink
I think I'm getting dense in my old age. I've re-read this a few times and can't wrap my head around what you are getting at. Isn't the variable c already counting the valid colors?

Could you modify the function I created that shows the code you are referring to that needs to be added?

It's counting NON-BLACK colors:

IF _PALETTECOLOR(p, i) <> &HFF000000     <--- This line right here.   &HFF000000 is full alpha black.  If the palette we're using isn't a black color then... we count a color.   But, what if the color IS black?   it never gets counted.   

Try this example with the glitch.  I'll zap up a fix in a bit, but this highlights the issue for you, I think.  Wink

Code: (Select All)
Dim a(5)
a(1) = _NewImage(640, 480, 32) '  32 bit color
a(2) = _NewImage(640, 480, 256) ' 256 color
a(3) = _NewImage(640, 480, 12) '  16 color
a(4) = _NewImage(80, 25, 0) '    text (16 color)
a(5) = _NewImage(640, 480, 256) '256 colors, but with a very weird palette
For i = 17 To 255 'colors 17 to 255 are black
    _PaletteColor i, &HFF000000&&, a(5)
Next




Print IMG_ColorDepth(a(1)) ' 32 bit color return
Print IMG_ColorDepth(a(2)) ' 256 color return
Print IMG_ColorDepth(a(3)) ' 16 color return
Print IMG_ColorDepth(a(4)) ' text color return
Print IMG_ColorDepth(a(5)); " <--- Note here" ' 256 weird palette color return


'PRINT IMG_ColorDepth(-30) ' this line will cause the error described in the function's documentation
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 will fail if an image      |
    '|      handle value passed in is less than -1  |
    '|      but does not belong to an actual image.  |
    '+------------------------------------------------+
    Dim c As Integer ' color counter
    Dim p As Integer ' palette counter
    If i > -2 Then Exit Function '                                not an image, return 0
    If _PixelSize(i) = 4 Then '                                  4 bytes per pixel?
        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
End Function
Reply


Messages In This Thread
RE: Detecting color depth-16 color, 256 color, or 32 bit color - by SMcNeill - 08-16-2024, 10:21 PM



Users browsing this thread: 1 Guest(s)