1 | unit FastBitmap;
|
---|
2 |
|
---|
3 | interface
|
---|
4 |
|
---|
5 | uses
|
---|
6 | Classes, SysUtils, Dialogs;
|
---|
7 |
|
---|
8 | type
|
---|
9 |
|
---|
10 | TFastBitmapPixel = Cardinal;
|
---|
11 | (*TFastBitmapPixel = record
|
---|
12 | Blue: Byte;
|
---|
13 | Green: Byte;
|
---|
14 | Red: Byte;
|
---|
15 | end;*)
|
---|
16 | PFastBitmapPixel = ^TFastBitmapPixel;
|
---|
17 |
|
---|
18 | TFastBitmapPixelComponents = packed record
|
---|
19 | B, G, R, A: Byte;
|
---|
20 | end;
|
---|
21 |
|
---|
22 | const
|
---|
23 | FastPixelSize = SizeOf(TFastBitmapPixel);
|
---|
24 |
|
---|
25 | type
|
---|
26 | { TFastBitmap }
|
---|
27 |
|
---|
28 | TFastBitmap = class
|
---|
29 | private
|
---|
30 | FPixelsData: PByte;
|
---|
31 | FSize: TPoint;
|
---|
32 | function GetPixel(X, Y: Integer): TFastBitmapPixel; inline;
|
---|
33 | procedure SetPixel(X, Y: Integer; const AValue: TFastBitmapPixel); inline;
|
---|
34 | procedure SetSize(const AValue: TPoint);
|
---|
35 | public
|
---|
36 | constructor Create;
|
---|
37 | destructor Destroy; override;
|
---|
38 | procedure RandomImage(Index, Count: Integer);
|
---|
39 | property Size: TPoint read FSize write SetSize;
|
---|
40 | property Pixels[X, Y: Integer]: TFastBitmapPixel read GetPixel write SetPixel;
|
---|
41 | property PixelsData: PByte read FPixelsData;
|
---|
42 | end;
|
---|
43 |
|
---|
44 | { TFastBitmap3 }
|
---|
45 |
|
---|
46 | TFastBitmap3 = class
|
---|
47 | private
|
---|
48 | FPixelsData: PByte;
|
---|
49 | FSize: TPoint;
|
---|
50 | procedure SetSize(const AValue: TPoint);
|
---|
51 | public
|
---|
52 | constructor Create;
|
---|
53 | destructor Destroy; override;
|
---|
54 | procedure RandomImage;
|
---|
55 | property Size: TPoint read FSize write SetSize;
|
---|
56 | function GetPixelAddress(X, Y: Integer): PFastBitmapPixel; inline;
|
---|
57 | function GetPixelSize: Integer; inline;
|
---|
58 | end;
|
---|
59 |
|
---|
60 | TFastBitmap2 = class
|
---|
61 | private
|
---|
62 | function GetSize: TPoint;
|
---|
63 | procedure SetSize(const AValue: TPoint);
|
---|
64 | public
|
---|
65 | Pixels: array of array of TFastBitmapPixel;
|
---|
66 | procedure RandomImage;
|
---|
67 | property Size: TPoint read GetSize write SetSize;
|
---|
68 | end;
|
---|
69 |
|
---|
70 | function SwapBRComponent(Value: Cardinal): Cardinal; inline;
|
---|
71 | function NoSwapBRComponent(Value: Cardinal): Cardinal; inline;
|
---|
72 |
|
---|
73 |
|
---|
74 | implementation
|
---|
75 |
|
---|
76 | function SwapBRComponent(Value: Cardinal): Cardinal;
|
---|
77 | begin
|
---|
78 | // Result := (Value and $00ff00) or ((Value shr 16) and $ff) or ((Value and $ff) shl 16);
|
---|
79 | Result := Value;
|
---|
80 | TFastBitmapPixelComponents(Result).R := TFastBitmapPixelComponents(Value).B;
|
---|
81 | TFastBitmapPixelComponents(Result).B := TFastBitmapPixelComponents(Value).R;
|
---|
82 | end;
|
---|
83 |
|
---|
84 | function NoSwapBRComponent(Value: Cardinal): Cardinal;
|
---|
85 | begin
|
---|
86 | // Result := (Value and $00ff00) or ((Value shr 16) and $ff) or ((Value and $ff) shl 16);
|
---|
87 | Result := Value;
|
---|
88 | TFastBitmapPixelComponents(Result).B := TFastBitmapPixelComponents(Value).B;
|
---|
89 | TFastBitmapPixelComponents(Result).R := TFastBitmapPixelComponents(Value).R;
|
---|
90 | end;
|
---|
91 |
|
---|
92 | { TFastBitmap3 }
|
---|
93 |
|
---|
94 | procedure TFastBitmap3.SetSize(const AValue: TPoint);
|
---|
95 | begin
|
---|
96 | if (FSize.X = AValue.X) and (FSize.Y = AValue.X) then Exit;
|
---|
97 | FSize := AValue;
|
---|
98 | FPixelsData := ReAllocMem(FPixelsData, FSize.X * FSize.Y * SizeOf(TFastBitmapPixel));
|
---|
99 | end;
|
---|
100 |
|
---|
101 | constructor TFastBitmap3.Create;
|
---|
102 | begin
|
---|
103 | inherited;
|
---|
104 | Size := Point(0, 0);
|
---|
105 | end;
|
---|
106 |
|
---|
107 | destructor TFastBitmap3.Destroy;
|
---|
108 | begin
|
---|
109 | FreeMem(FPixelsData);
|
---|
110 | inherited;
|
---|
111 | end;
|
---|
112 |
|
---|
113 | procedure TFastBitmap3.RandomImage;
|
---|
114 | var
|
---|
115 | I, X, Y: Integer;
|
---|
116 | PRow: PFastBitmapPixel;
|
---|
117 | PPixel: PFastBitmapPixel;
|
---|
118 | begin
|
---|
119 | for I := 0 to 2 do begin
|
---|
120 | PRow := GetPixelAddress(I * (Size.X div 3), 0);
|
---|
121 | for Y := 0 to (Size.Y div 2) - 1 do begin
|
---|
122 | PPixel := PRow;
|
---|
123 | for X := 0 to (Size.X div 3) - 1 do begin
|
---|
124 | PPixel^ := 255 shl (I * 8);
|
---|
125 | Inc(PPixel);
|
---|
126 | end;
|
---|
127 | Inc(PRow, Size.X);
|
---|
128 | end;
|
---|
129 | end;
|
---|
130 |
|
---|
131 | PRow := GetPixelAddress(0, Size.Y div 2);
|
---|
132 | for Y := (Size.Y div 2) to Size.Y - 1 do begin
|
---|
133 | PPixel := PRow;
|
---|
134 | for X := 0 to Size.X - 1 do begin
|
---|
135 | PPixel^ := Random(256) or (Random(256) shl 16) or (Random(256) shl 8);
|
---|
136 | Inc(PPixel);
|
---|
137 | end;
|
---|
138 | Inc(PRow, Size.X);
|
---|
139 | end;
|
---|
140 | end;
|
---|
141 |
|
---|
142 | function TFastBitmap3.GetPixelAddress(X, Y: Integer): PFastBitmapPixel;
|
---|
143 | begin
|
---|
144 | Result := PFastBitmapPixel(FPixelsData + Y * FSize.X + X);
|
---|
145 | end;
|
---|
146 |
|
---|
147 | function TFastBitmap3.GetPixelSize: Integer;
|
---|
148 | begin
|
---|
149 | Result := SizeOf(TFastBitmapPixel);
|
---|
150 | end;
|
---|
151 |
|
---|
152 | { TFastBitmap2 }
|
---|
153 |
|
---|
154 | function TFastBitmap2.GetSize: TPoint;
|
---|
155 | begin
|
---|
156 | Result.X := Length(Pixels);
|
---|
157 | if Result.X > 0 then Result.Y := Length(Pixels[0])
|
---|
158 | else Result.Y := 0;
|
---|
159 | end;
|
---|
160 |
|
---|
161 | procedure TFastBitmap2.SetSize(const AValue: TPoint);
|
---|
162 | begin
|
---|
163 | SetLength(Pixels, AValue.X, AValue.Y);
|
---|
164 | end;
|
---|
165 |
|
---|
166 | procedure TFastBitmap2.RandomImage;
|
---|
167 | var
|
---|
168 | X, Y: Integer;
|
---|
169 | begin
|
---|
170 | for Y := 0 to Size.Y - 1 do
|
---|
171 | for X := 0 to Size.X - 1 do
|
---|
172 | Pixels[X, Y] := Random(256);
|
---|
173 | end;
|
---|
174 |
|
---|
175 | { TFastBitmap }
|
---|
176 |
|
---|
177 | function TFastBitmap.GetPixel(X, Y: Integer): TFastBitmapPixel;
|
---|
178 | begin
|
---|
179 | Result := PFastBitmapPixel(FPixelsData + (Y * FSize.X + X) * FastPixelSize)^;
|
---|
180 | end;
|
---|
181 |
|
---|
182 | procedure TFastBitmap.SetPixel(X, Y: Integer; const AValue: TFastBitmapPixel);
|
---|
183 | begin
|
---|
184 | PFastBitmapPixel(FPixelsData + (Y * FSize.X + X) * FastPixelSize)^ := AValue;
|
---|
185 | end;
|
---|
186 |
|
---|
187 | procedure TFastBitmap.SetSize(const AValue: TPoint);
|
---|
188 | begin
|
---|
189 | if (FSize.X = AValue.X) and (FSize.Y = AValue.X) then Exit;
|
---|
190 | FSize := AValue;
|
---|
191 | FPixelsData := ReAllocMem(FPixelsData, FSize.X * FSize.Y * FastPixelSize);
|
---|
192 | end;
|
---|
193 |
|
---|
194 | constructor TFastBitmap.Create;
|
---|
195 | begin
|
---|
196 | Size := Point(0, 0);
|
---|
197 | end;
|
---|
198 |
|
---|
199 | destructor TFastBitmap.Destroy;
|
---|
200 | begin
|
---|
201 | FreeMem(FPixelsData);
|
---|
202 | inherited;
|
---|
203 | end;
|
---|
204 |
|
---|
205 | procedure TFastBitmap.RandomImage(Index, Count: Integer);
|
---|
206 | var
|
---|
207 | I, X, Y: Integer;
|
---|
208 | begin
|
---|
209 | // Main three color blocks
|
---|
210 | for I := 0 to 2 do
|
---|
211 | for Y := 0 to (Size.Y div 3) - 1 do
|
---|
212 | for X := 0 to (Size.X div 3) - 1 do
|
---|
213 | Pixels[X + (I * (Size.X div 3)), Y] := (255 shl (I * 8)) and $ffffff;
|
---|
214 |
|
---|
215 | // Random noise
|
---|
216 | for Y := (Size.Y div 3) to (Size.Y * 2 div 3) - 1 do
|
---|
217 | for X := 0 to Size.X - 1 do
|
---|
218 | Pixels[X, Y] := (Random(256) or (Random(256) shl 16) or (Random(256) shl 8)) and $ffffff;
|
---|
219 |
|
---|
220 | // Color gradient
|
---|
221 | for Y := (Size.Y * 2 div 3) to (Size.Y - 1) do begin
|
---|
222 | for X := 0 to Size.X - 1 do
|
---|
223 | Pixels[X, Y] := (Trunc(Sin((X + Trunc(Index / Count * Size.X)) mod Size.X
|
---|
224 | / Size.X * pi) * 255) * $010101) and $ffffff;
|
---|
225 | end;
|
---|
226 | end;
|
---|
227 |
|
---|
228 | end.
|
---|
229 |
|
---|