//== CountColors ===================================================== // Count number of unique R-G-B triples in a pf24bit Bitmap. // // Use 2D array of TBits objects -- when (R,G) combination occurs // for the first time, create 256-bit array of bits in blue dimension. // So, overall this is a fairly sparse matrix for most pictures. // Tested with pictures created with a known number of colors, including // a specially constructed image with 1024*1024 = 1,048,576 colors. // // efg, October 1998. FUNCTION CountColors(CONST Bitmap: TBitmap): INTEGER; VAR Flags: ARRAY[BYTE, BYTE] OF TBits; i : INTEGER; j : INTEGER; k : INTEGER; rowIn: pRGBTripleArray; BEGIN // Be sure bitmap is 24-bits/pixel ASSERT (Bitmap.PixelFormat = pf24Bit); // Clear 2D array of TBits objects FOR j := 0 TO 255 DO FOR i := 0 TO 255 DO Flags[i,j] := NIL; // Step through each scanline of image FOR j := 0 TO Bitmap.Height-1 DO BEGIN rowIn := Bitmap.Scanline[j]; FOR i := 0 TO Bitmap.Width-1 DO BEGIN WITH rowIn[i] DO BEGIN IF NOT Assigned(Flags[rgbtRed, rgbtGreen]) THEN BEGIN // Create 3D column when needed Flags[rgbtRed, rgbtGreen] := TBits.Create; Flags[rgbtRed, rgbtGreen].Size := 256; END; // Mark this R-G-B triple Flags[rgbtRed,rgbtGreen].Bits[rgbtBlue] := TRUE END END END; RESULT := 0; // Count and Free TBits objects FOR j := 0 TO 255 DO BEGIN FOR i := 0 TO 255 DO BEGIN IF Assigned(Flags[i,j]) THEN BEGIN FOR k := 0 TO 255 DO IF Flags[i,j].Bits[k] THEN INC(RESULT); Flags[i,j].Free; END END END END {CountColors};