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-b, sizeof(char), 1, fp);
/* Get green component. */
fread(&p->g, sizeof(char), 1, fp);
/* Get red component. */
fread(&p->r, sizeof(char), 1, fp);
/* Read the filler byte. */
fread(&p->filler, sizeof(char), 1, fp);
}
/* Close the file and return TRUE for succes. */
fclose(fp);
return TRUE;
}
</code>
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
Palette reading function
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):
I'm reminded of the day my daughter came in, looked over my shoulder at some Perl 4 code, and said, "What is that, swearing?" - Larry Wall
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!
- I kode kuz I kan!
- I kode kuz I kan!
Oh yeah, you also have to put "->b" not "-b" on the reader line.
- I kode kuz I kan!
- I kode kuz I kan!
- I kode kuz I kan!
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
But thanks for telling me.
/. Muzzafarath
I'm reminded of the day my daughter came in, looked over my shoulder at some Perl 4 code, and said, "What is that, swearing?" - Larry Wall
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)
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);
} // 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
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)
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);
} // 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
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement