source: GraphicTest/FastBitmap.pas

Last change on this file was 573, checked in by chronos, 5 months ago
  • Modified: Build with Lazarus 3.4.
  • Modified: Removed U prefix from unit names.
File size: 5.8 KB
Line 
1unit FastBitmap;
2
3interface
4
5uses
6 Classes, SysUtils, Dialogs;
7
8type
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
22const
23 FastPixelSize = SizeOf(TFastBitmapPixel);
24
25type
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
70function SwapBRComponent(Value: Cardinal): Cardinal; inline;
71function NoSwapBRComponent(Value: Cardinal): Cardinal; inline;
72
73
74implementation
75
76function SwapBRComponent(Value: Cardinal): Cardinal;
77begin
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;
82end;
83
84function NoSwapBRComponent(Value: Cardinal): Cardinal;
85begin
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;
90end;
91
92{ TFastBitmap3 }
93
94procedure TFastBitmap3.SetSize(const AValue: TPoint);
95begin
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));
99end;
100
101constructor TFastBitmap3.Create;
102begin
103 inherited;
104 Size := Point(0, 0);
105end;
106
107destructor TFastBitmap3.Destroy;
108begin
109 FreeMem(FPixelsData);
110 inherited;
111end;
112
113procedure TFastBitmap3.RandomImage;
114var
115 I, X, Y: Integer;
116 PRow: PFastBitmapPixel;
117 PPixel: PFastBitmapPixel;
118begin
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;
140end;
141
142function TFastBitmap3.GetPixelAddress(X, Y: Integer): PFastBitmapPixel;
143begin
144 Result := PFastBitmapPixel(FPixelsData + Y * FSize.X + X);
145end;
146
147function TFastBitmap3.GetPixelSize: Integer;
148begin
149 Result := SizeOf(TFastBitmapPixel);
150end;
151
152{ TFastBitmap2 }
153
154function TFastBitmap2.GetSize: TPoint;
155begin
156 Result.X := Length(Pixels);
157 if Result.X > 0 then Result.Y := Length(Pixels[0])
158 else Result.Y := 0;
159end;
160
161procedure TFastBitmap2.SetSize(const AValue: TPoint);
162begin
163 SetLength(Pixels, AValue.X, AValue.Y);
164end;
165
166procedure TFastBitmap2.RandomImage;
167var
168 X, Y: Integer;
169begin
170 for Y := 0 to Size.Y - 1 do
171 for X := 0 to Size.X - 1 do
172 Pixels[X, Y] := Random(256);
173end;
174
175{ TFastBitmap }
176
177function TFastBitmap.GetPixel(X, Y: Integer): TFastBitmapPixel;
178begin
179 Result := PFastBitmapPixel(FPixelsData + (Y * FSize.X + X) * FastPixelSize)^;
180end;
181
182procedure TFastBitmap.SetPixel(X, Y: Integer; const AValue: TFastBitmapPixel);
183begin
184 PFastBitmapPixel(FPixelsData + (Y * FSize.X + X) * FastPixelSize)^ := AValue;
185end;
186
187procedure TFastBitmap.SetSize(const AValue: TPoint);
188begin
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);
192end;
193
194constructor TFastBitmap.Create;
195begin
196 Size := Point(0, 0);
197end;
198
199destructor TFastBitmap.Destroy;
200begin
201 FreeMem(FPixelsData);
202 inherited;
203end;
204
205procedure TFastBitmap.RandomImage(Index, Count: Integer);
206var
207 I, X, Y: Integer;
208begin
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;
226end;
227
228end.
229
Note: See TracBrowser for help on using the repository browser.