This topic is now archived and is closed to further replies.


Palette reading function

Recommended Posts

I've been trying to write a function that'll read the palette data from a 256 color bmp and store it in a PALETTE structure but it just doesn't work I've read that the palette is stored at offset 0x0036 and that it comes in packs of 4 bytes (1 byte for the blue component, 1 byte for the green, 1 byte for the red and 1 filler byte). Here's the function that I've written (I had to change some code so that the forum wouldn't think it was HTML): int load_palette(const char *filename, PALETTE *p) { FILE *fp = NULL; int i; fp = fopen(filename, "rb"); if(fp == NULL) return FALSE; /* Go to offset 0x0036 in the file. This is where the palette data is stored. */ fseek(fp, 0x0036, SEEK_SET); /* Read the palette. */ for(i = 0; i < 256; i++) { /* Get blue component. */ fread(&p[ i ]-b, sizeof(char), 1, fp); /* Get green component. */ fread(&p[ i ]->g, sizeof(char), 1, fp); /* Get red component. */ fread(&p[ i ]->r, sizeof(char), 1, fp); /* Read the filler byte. */ fread(&p[ i ]->filler, sizeof(char), 1, fp); } /* Close the file and return TRUE for succes. */ fclose(fp); return TRUE; } And when I try to show the bmp "gfx.bmp" the colors come out all wrong... /. Muzzafarath Edited by - Muzzafarath on 4/1/00 10:35:44 AM

Share this post

Link to post
Share on other sites
You''re in luck, I just finished off my super error checking bmp reader, it also loads palettes. If you want it I will email it to you, it''s very clear code and has very good error checking. You might also try support the Microsoft .pal palettes. That way you don''t need a whole image to get the palette. The header format is unknown to me right now (cuz I didn''t bother trying to learn it) but at offset 24 the RGBF 94-byte) color palette starts Have fun

- I kode kuz I kan!

Share this post

Link to post
Share on other sites
your making it harder than it has to be. just do this:
int Load_Palette_From_File(char *filename, LPPALETTEENTRY palette)
// this function loads a palette from disk into a palette
// structure, but does not set the pallette

FILE *fp_file; // working file

// try and open file
if ((fp_file = fopen(filename,"r"))==NULL)

// read in all 256 colors RGBF
for (int index=0; index<256; index++)
// read the next entry in
fscanf(fp_file,"%d %d %d %d",&palette[index].peRed,
} // end for index

// close the file

// return success
} // end Load_Palette_From_Disk

just use scanf! look how simple it really is!

oh!! wait!!! you wanted it from a bitmap file, not a designated palette file, oh i see!!

well, make fread into _lread, for one (_lread reads data from a file to a buffer). heres the dec:
UINT _lread(HFILE file, LPVOID lpBuffer, UINT ubytes)
that should make it easier.
with that, you don't even need a loop! just go:
_lread(file_handle, &bitmap->palette,MAX_COLORS_PALETTE*sizeof(PALETTEENTRY));
where bitmap->palette is a PALETTEENTRY and MAX_COLORS_PALETTE is 256.
after that, howeer, you have to switch the red and blue elements for each palette entry, since its just the way it is.
i know i sound confusing, so e-mail me if you are still confused

Edited by - Zipster on 4/1/00 5:18:59 PM

Share this post

Link to post
Share on other sites