1 | unit UFastBitmap;
|
---|
2 |
|
---|
3 | {$mode objfpc}{$H+}
|
---|
4 |
|
---|
5 | interface
|
---|
6 |
|
---|
7 | uses
|
---|
8 | Classes, SysUtils, Dialogs;
|
---|
9 |
|
---|
10 | type
|
---|
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 |
|
---|
24 | const
|
---|
25 | FastPixelSize = SizeOf(TFastBitmapPixel);
|
---|
26 |
|
---|
27 | type
|
---|
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 |
|
---|
72 | function SwapBRComponent(Value: Cardinal): Cardinal; inline;
|
---|
73 | function NoSwapBRComponent(Value: Cardinal): Cardinal; inline;
|
---|
74 |
|
---|
75 | implementation
|
---|
76 |
|
---|
77 | function SwapBRComponent(Value: Cardinal): Cardinal;
|
---|
78 | begin
|
---|
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;
|
---|
83 | end;
|
---|
84 |
|
---|
85 | function NoSwapBRComponent(Value: Cardinal): Cardinal;
|
---|
86 | begin
|
---|
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;
|
---|
91 | end;
|
---|
92 |
|
---|
93 | { TFastBitmap3 }
|
---|
94 |
|
---|
95 | procedure TFastBitmap3.SetSize(const AValue: TPoint);
|
---|
96 | begin
|
---|
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));
|
---|
100 | end;
|
---|
101 |
|
---|
102 | constructor TFastBitmap3.Create;
|
---|
103 | begin
|
---|
104 | inherited;
|
---|
105 | Size := Point(0, 0);
|
---|
106 | end;
|
---|
107 |
|
---|
108 | destructor TFastBitmap3.Destroy;
|
---|
109 | begin
|
---|
110 | FreeMem(FPixelsData);
|
---|
111 | inherited Destroy;
|
---|
112 | end;
|
---|
113 |
|
---|
114 | procedure TFastBitmap3.RandomImage;
|
---|
115 | var
|
---|
116 | I, X, Y: Integer;
|
---|
117 | PRow: PFastBitmapPixel;
|
---|
118 | PPixel: PFastBitmapPixel;
|
---|
119 | begin
|
---|
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;
|
---|
141 | end;
|
---|
142 |
|
---|
143 | function TFastBitmap3.GetPixelAddress(X, Y: Integer): PFastBitmapPixel;
|
---|
144 | begin
|
---|
145 | Result := PFastBitmapPixel(FPixelsData) + Y * FSize.X + X;
|
---|
146 | end;
|
---|
147 |
|
---|
148 | function TFastBitmap3.GetPixelSize: Integer;
|
---|
149 | begin
|
---|
150 | Result := SizeOf(TFastBitmapPixel);
|
---|
151 | end;
|
---|
152 |
|
---|
153 | { TFastBitmap2 }
|
---|
154 |
|
---|
155 | function TFastBitmap2.GetSize: TPoint;
|
---|
156 | begin
|
---|
157 | Result.X := Length(Pixels);
|
---|
158 | if Result.X > 0 then Result.Y := Length(Pixels[0])
|
---|
159 | else Result.Y := 0;
|
---|
160 | end;
|
---|
161 |
|
---|
162 | procedure TFastBitmap2.SetSize(const AValue: TPoint);
|
---|
163 | begin
|
---|
164 | SetLength(Pixels, AValue.X, AValue.Y);
|
---|
165 | end;
|
---|
166 |
|
---|
167 | procedure TFastBitmap2.RandomImage;
|
---|
168 | var
|
---|
169 | X, Y: Integer;
|
---|
170 | begin
|
---|
171 | for Y := 0 to Size.Y - 1 do
|
---|
172 | for X := 0 to Size.X - 1 do
|
---|
173 | Pixels[X, Y] := Random(256);
|
---|
174 | end;
|
---|
175 |
|
---|
176 | { TFastBitmap }
|
---|
177 |
|
---|
178 | function TFastBitmap.GetPixel(X, Y: Integer): TFastBitmapPixel;
|
---|
179 | begin
|
---|
180 | Result := PFastBitmapPixel(FPixelsData + (Y * FSize.X + X) * FastPixelSize)^;
|
---|
181 | end;
|
---|
182 |
|
---|
183 | procedure TFastBitmap.SetPixel(X, Y: Integer; const AValue: TFastBitmapPixel);
|
---|
184 | begin
|
---|
185 | PFastBitmapPixel(FPixelsData + (Y * FSize.X + X) * FastPixelSize)^ := AValue;
|
---|
186 | end;
|
---|
187 |
|
---|
188 | procedure TFastBitmap.SetSize(const AValue: TPoint);
|
---|
189 | begin
|
---|
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);
|
---|
193 | end;
|
---|
194 |
|
---|
195 | constructor TFastBitmap.Create;
|
---|
196 | begin
|
---|
197 | Size := Point(0, 0);
|
---|
198 | end;
|
---|
199 |
|
---|
200 | destructor TFastBitmap.Destroy;
|
---|
201 | begin
|
---|
202 | FreeMem(FPixelsData);
|
---|
203 | inherited Destroy;
|
---|
204 | end;
|
---|
205 |
|
---|
206 | procedure TFastBitmap.RandomImage(Index, Count: Integer);
|
---|
207 | var
|
---|
208 | I, X, Y: Integer;
|
---|
209 | begin
|
---|
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;
|
---|
227 | end;
|
---|
228 |
|
---|
229 |
|
---|
230 | end.
|
---|
231 |
|
---|