source: trunk/UFastBitmap.pas

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