1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
|
Compter le nombre de couleurs d''une Bmp : SUPER RAPIDE
=====================================================
// 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}; |
Partager