Programming Tips - C/C++: Generate the Netscape palette before creating a GIF

Date: 1998jun1 Update: 2025jul11 Language: C/C++ Keywords: make, classic Q. C/C++: Generate the Netscape palette before creating a GIF A.
#include <cstddef> #include "gif_lib.h" // The original GIF library /* Defined in gif_lib.h typedef unsigned char GifByteType; struct GifColorType { GifByteType Red, Green, Blue; } GifColorType; struct ColorMapObject { int ColorCount; int BitsPerPixel; GifColorType *Colors; } ColorMapObject; ColorMapObject *MakeMapObject(const int ColorCount, GifColorType *ColorMap); */ const int N_NETSCAPE_PALETTE = 216; void MakeNetscapePalette(GifColorType *colors, const int nColors) { const GifByteType a[6] = { 0, 0x33, 0x66, 0x99, 0xcc, 0xff }; int red, green, blue; int i = 0; for (red = 0; red < 6; red++) { for (green = 0; green < 6; green++) { for (blue = 0; blue < 6; blue++) { colors[i].Red = a[red]; colors[i].Green = a[green]; colors[i].Blue = a[blue]; i++; } } } // Fill the remainder for (; i < nColors; i++) { colors[i].Red = 0; colors[i].Green = 0; colors[i].Blue = 0; } } void exampleUse() { const int nColors = 256; ColorMapObject *pColorMap = MakeMapObject(nColors, NULL); if (pColorMap == NULL) { return; } MakeNetscapePalette(pColorMap->Colors, nColors); }