Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Detecting color depth-16 color, 256 color, or 32 bit color
#18
@TerryRitchie  See if this helps showcase this one weird setup, that gives false positives that we can account for:

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 = 18 To 255 'colors 18 to 255 are black
    _PaletteColor i, &HFF000000&&, a(5)
Next
'with the above, 0 is black .... it's not counted
'1 to 17 are colors.... that's 16 colors
'but we need to count that initial black to push it to being counted as a 17-color palette and not a false-16.



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
            Else
                If black = 0 Then black = p 'first instance of a black color
            End If
            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
        ElseIf c = 16 Then 'we need to test for 16 with no-black, or 16 + black
            If black <= 16 Then 'it's an early black and part of the palette.  We have 17 color palette
                IMG_ColorDepth% = 256
            Else 'that first black is beyond the initial 16 colors, it's not part of the palette
                IMG_ColorDepth% = 16
            End If
        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:34 PM



Users browsing this thread: 30 Guest(s)