Jump to content

Recommended Posts

Posted (edited)

Hi. Just wondering if anyone has any idea on how to get what colors (as values)are in a gif image?

Thanks

Edited by marauder
Posted (edited)

uhmmm.. pixelgetcolor

?? :(

oh yeah, and you can use the autoit info window

if you want them all... you will have to search the entire pic.. might take a few seconds.

Edited by t0ddie

Valik Note Added 19 October 2006 - 08:38 AMAdded to warn level I just plain don't like you.

Posted

Doesn't that only work for what's on the screen? I'm actually looking for something to read in an image file and get the colors.

Thanks

Posted

Actually I've just had a good look at ImageMagick again and using the 'identify' cmd with '-verbose' gives the color table of it with the histogram which it seems shows what colors are in there, so I'm going to make a script to get those colors out. Cool!

Posted

do a search and find the GIF format, and write a function to read it out of the file. Shouldn't be too hard, I wrote on to do a similiar thing with BMPs ahwile back, using Pascal.

Posted

do a search and find the GIF format, and write a function to read it out of the file.  Shouldn't be too hard, I wrote on to do a similiar thing with BMPs ahwile back, using Pascal.

<{POST_SNAPBACK}>

OK, I just found some documents:

http://www.dcs.ed.ac.uk/home/mxr/gfx/2d/GIF87a.txt

http://www.dcs.ed.ac.uk/home/mxr/gfx/2d/GIF89a.txt

and it looks like this is how it works:

read the 5th byte of the file.

1) Check the lowest (0) bit. If it's set to 1 then a color palette is included, otherwise not.

2) Get bits 1,2,3 and make a number out of them and add one. Raise this number to the power of 2 and this will be the scale for the strength of the color channels.

3) Get the highest 3 bits and make a number out of them and add one. Raise this to the power of 2 and you will get the number of colors in the palette.

4) Starting at the 8th byte of the file, read 1 byte per channel (RGB=3 bytes) times the number of colors given (step 3) and scale each channel according to the max value as specified in step 2

Posted

...

read the 5th byte of the file.

...

4) Starting at the 8th byte of the file

...

<{POST_SNAPBACK}>

Oops, I forget the GIF header. Add 6 to those byte counts.
Posted

OK, in a fit of boredom, I wrote this function:

CODE

Func _GifGetHeader($FileName)

Dim $hndFile_GIF, $aReturn[10], $OK = -1, $FileSize

;Verify that file exists and is openable

IF $OK Then

$hndFile_Gif = FileOpen($FileName, 0)

If $hndFile_Gif < 0 Then $OK = 0

EndIf

;Verify that file is the minimum size

IF $OK Then

$FileSize = FileGetSize($FileName)

If $FileSize < 13 Then $OK = 0

EndIf

;Verify that file has GIF signature header

If $OK Then

$Data = FileRead($hndFile_Gif, 3)

If not ($Data == 'GIF') Then $OK = 0

EndIf

;Read GIF image descriptor, verify filesize

If $OK Then

$aReturn[0] = 9

$Data = FileRead($hndFile_Gif, 3)

$aReturn[1] = $Data

$Data = _FileReadBin($hndFile_Gif, 7)

$aReturn[2] = $Data[2] * 256 + $Data[1] ;Width

$aReturn[3] = $Data[4] * 256 + $Data[3] ;Height

$aReturn[4] = BitShift(BitAND($Data[5], 128), 7) ;Global Color Palette

$aReturn[5] = BitShift(BitAND($Data[5], 64 + 32 + 16), 4) + 1 ;bits of color resolution

$aReturn[6] = BitShift(BitAND($Data[5], 8), 3) ;Palette is sorted most- to least-used

$aReturn[7] = BitShift(BitAND($Data[5], 1 + 2 + 4), 0) + 1 ;bits per pixel

$aReturn[8] = $Data[6] ;background color

$aReturn[9] = $Data[7] ;marker = #0

If $aReturn[9] <> 0 then $Ok = 0

If $aReturn[4] And $FileSize < 13 + (2 ^ $aReturn[7]) * 3 Then $OK = 0

EndIf

;Read Global Color Palette

If $Ok Then

$aReturn[0] = 9 + 2 ^ $aReturn[7]

Dim $i, $r ,$g, $b

ReDim $aReturn[$aReturn[0] + 1]

For $i = 1 To 2 ^ $aReturn[7]

$Data = _FileReadBin($hndFile_Gif, 3)

;scale the color values up to 255

$r = $Data[1] * 255 / (2 ^ $aReturn[5] -1)

$g = $Data[2] * 255 / (2 ^ $aReturn[5] -1)

$b = $Data[3] * 255 / (2 ^ $aReturn[5] -1)

;combine and convert to hex

$aReturn[$i+9] = Hex(($r*256+$g)*256+$b,6)

Next

EndIf

;Close file handle

FileClose($hndFile_Gif)

;Return gathered data

If $OK Then

Return $aReturn

Else

SetError(1)

Return ''

EndIf

EndFunc

It calls my _FileReadBin() function and returns an array:

element 0=number of elements returned

element 1=GIF version

element 2=width

element 3=height

element 4=color table encluded

element 5=color resolution

element 6=Palette is sorted by color usage

element 7=bits per pixel

element 8=background color

element 9=reserved

element 10...=color in 0xRRGGBB format

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...