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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
| Unit Unit1;
Interface
Uses Windows, Messages, SysUtils, Classes, Graphics, Controls,
Forms, Dialogs, ExtCtrls, StdCtrls, ComCtrls, ExtDlgs;
Type
TForm1 = Class(TForm)
StartButton: TButton;
OpenPictureDialog1: TOpenPictureDialog;
Procedure StartButtonClick(Sender: TObject);
End;
Var
Form1: TForm1;
{----------------------------------------------------------------}
{ }Implementation{ }
{----------------------------------------------------------------}
{$R *.DFM}
Const
MaxPixelCount = 65536;
Type
// For pf24bit Scanlines
pRGBTripleArray = ^TRGBTripleArray;
TRGBTripleArray = Array[0..MaxPixelCount - 1] Of TRGBTriple;
{----------------------------------------------------------------}
{ 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, j, 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
Result := Result + 1;
Flags[i, j].Free;
End
End
End
End;
{----------------------------------------------------------------}
Procedure TForm1.StartButtonClick(Sender: TObject);
Var
bmp: TBitmap;
Begin
OpenPictureDialog1.Filter := 'Fichiers BMP (*.bmp)|*.bmp;';
If Not (OpenPictureDialog1.Execute) Then
Exit;
bmp := TBitmap.Create;
bmp.LoadFromFile(OpenPictureDialog1.FileName);
ShowMessage(IntToStr(CountColors(bmp)) + ' couleurs trouvées');
bmp.Free;
End;
{----------------------------------------------------------------}
End. |
Partager