#### Archived

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

This topic is 6714 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## 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 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 on other sites
Oh yeah, you also have to put "->b" not "-b" on the reader line.

- I kode kuz I kan!

##### Share on other sites
That was just a spelling mistake, it happened when I was clearing up the code so it could be posted It doesn''t work even if i put in ->.

But thanks for telling me.

/. Muzzafarath

##### Share on other sites
your making it harder than it has to be. just do this:
{
// 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)
return(0);

// 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,
&palette[index].peGreen,
&palette[index].peBlue,
&palette[index].peFlags);
} // end for index

// close the file
fclose(fp_file);

// return success
return(1);

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!!

UINT _lread(HFILE file, LPVOID lpBuffer, UINT ubytes)
that should make it easier.
with that, you don't even need a loop! just go:
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 on other sites
Thanks Zipster, I''ll look into it.

/. Muzzafarath

1. 1
2. 2
3. 3
JoeJ
12
4. 4
5. 5
frob
11

• 12
• 16
• 13
• 20
• 12
• ### Forum Statistics

• Total Topics
632176
• Total Posts
3004594

×