Home |
Class PegQuant HMI (Human-Machine Interface) Classes |
Class PegQuantPegQuant is a run-time histogram and optimal palette producer. PegQuant implements a form of Heckbert's Median Cut color reduction algorithm. This class is only required for applications that must determine dynamic optimal palettes. Most applications run with fixed palettes. PegQuant is passed to PegImageConvert derived classes to create a histogram of color usage. After all included images have been added, the PegQuant function ReduceColors is called to create an optimal palette for use with the scanned images. typedef struct { // structure for a cube in color space PEGUINT lower; // one corner's index in histogram PEGUINT upper; // another corner's index in histogram DWORD count; // cube's histogram count LONG level; // cube's level UCHAR rmin, rmax; UCHAR gmin, gmax; UCHAR bmin, bmax; } cube_t; typedef struct { UCHAR Blue; UCHAR Green; UCHAR Red; UCHAR Count; } PalEntry; class PegQuant { public: PegQuant(); void AddColor(PegPixel Pixel); PEGUINT ReduceColors(PEGINT iLimit = 254); UCHAR * GetPalette(void); PEGUINT PalSize(void); protected: PEGUINT MedianCut(void); void Shrink(cube_t * Cube); void InvMap(LONG ncubes); UCHAR GetNewIndex(LONG index); LONG LookupPalIndex(UCHAR Red, UCHAR Green, UCHAR Blue); cube_t mCubes[MAXCOLORS]; LONG longdim; PEGUINT * mpHistPtr; PEGUINT * mpHist; UCHAR ColMap[256][4]; PEGINT miMaxColors; PEGUINT miStartIndex; PEGUINT miEndIndex; UCHAR mPalette[256*3]; LONG mlPalSize; }; ConstructorsCreates a PegQuant object. Method AddColorThis function adds the specified color to the histogram being created. Method GetPaletteReturns a pointer to the optimal color palette produced from the image color sums. Method PalSizeThis function returns the current number of colors in the palette. Method ReduceColorsThis function is called after all colors for all images have been counted. This function actually does the work of creating the optimal palette for use with the images counted. ExamplesThe following example reads several graphic files and creates an optimal palette for use in displaying the files. The palette is then installed as the new system palette. #define GIF1 "c:\\graphics\\tree.gif" #define GIF2 "c:\\graphics\\house.gif" #define BMP1 "c:\\graphics\\garage.bmp" void CountColors(char * cPathName, PEGUINT wType, PegQuant * pQuant) { FILE * fSrc = fopen(cPathName, "r"); PegImageConvert * pConvert; if (wType == PIC_TYPE_GIF) pConvert = new PegGifConvert(0); else pConvert = new PegBmpConvert(0); pConvert->ReadImage(fSrc); pConvert->CountColors(pQuant); pConvert->DestroyImages(); delete pConvert; fclose(fSrc); } void CreatePalette(void) { PegQuant * pQuant = new PegQuant(); CountColors(GIF1, PIC_TYPE_GIF, pQuant); CountColors(GIF2, PIC_TYPE_GIF, pQuant); CountColors(BMP1, PIC_TYPE_BMP, pQuant); PEGUINT wPalSize = pQuant->ReduceColors(); Screen()->SetPalette(0, wPalSize, pQuant->GetPalette()); delete pQuant; }
|