#### Archived

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

# Creating DDraw Palettes

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

## Recommended Posts

Hello everyone, I normally only work in 16-Bit so dealing with palettes is rather new to me. In Direct Draw you have to fill up an array of PALETTEENTRY with the colors on your palette. I imagine that doing so by hand is rather cumbersome and long. I was wondering if anyone had a means of extracting a palette from a bitmap of colors. Thanks for any help, Reaptide, Cunning Rogue of the Knights of Nih!!!

##### Share on other sites
As far as I remember, the D3DFrame code in the DirectX 7 SDK does something similar in d3dtextr.cpp in CTextureContainer::Restore().

ArgoN

##### Share on other sites
Not as difficult as it might seem, here''s a quick and dirty way to get the palette from a bitmap file:

  PALETTEENTRY *myPaletteLoader(char *filename){ PALETTEENTRY *pal; RGBQUAD *bmppal; BITMAPFILEHEADER hdr; BITMAPINFOHEADER info; FILE *fp; int i; fp = fopen(filename,"rb"); //open file if(!fp) return NULL; //couldn''t open file fread(&hdr,sizeof(BITMAPFILEHEADER),1,fp);//get file header if(hdr.bfType!=0x4D42){ //Bitmap id -- ''BM'' fclose(fp); return NULL; //not a windows bitmap } fread(&info,sizeof(BITMAPINFOHEADER),1,fp);// get info header if(info.biBitCount!=8){ /* 8 for 8 bit, 1 for monochrome, 4 for 16 colors */ fclose(fp); return NULL; //not an indexed color } fseek(fp,info.biSize+sizeof(BITMAPFILEHEADER),SEEK_SET); //seek position after header bmppal = malloc(sizeof(RGBQUAD)*256); /* get some memory you would only need 2 or 16 * sizeof(RGBQUAD) for monochrome or 16 colors, respectivly. */ if(!bmppal){ fclose(fp); return NULL; } fread(bmppal,sizeof(RGBQUAD),256,fp); //read the palette /* note the file position is right at the data segment now, you could easily load the bitmap data here */ fclose(fp); pal = malloc(sizeof(PALETTEENTRY)*256); if(!pal){ free(bmppal); return NULL; } for(i=0;i<256;++i){ //fill in PALETTEENTRY pal.peRed = bmppal[i].rgbRed; pal[i].peGreen = bmppal[i].rgbGreen; pal[i].peBlue = bmppal[i].rgbBlue; pal[i].peFlags = 0; } free(bmppal); //release bitmap palette return pal;}

----------
meh

##### Share on other sites
Thank you very much Bulbasaur. I haven''t tried out the function yet, but I was wondering what the bitmap need to look like?

An image 256 pixels wide, or high, or does anything work as long as I used 256 different colors?

Thanks for the code,

Reaptide

##### Share on other sites
256 = number of palette entries in an 8 bit bitmap.All 8 bit bitmap files have 256 palette entries, even if you don''t use all of them.In other words, this function would work as long as the file was an 8 bit bitmap, but all it does is return the palette entries from the file.

----------
meh