1 | unit UPixelPointer;
|
---|
2 |
|
---|
3 | interface
|
---|
4 |
|
---|
5 | uses
|
---|
6 | Classes, SysUtils, Graphics;
|
---|
7 |
|
---|
8 | type
|
---|
9 | TColor32 = type Cardinal;
|
---|
10 | TColor32Component = (ccBlue, ccGreen, ccRed, ccAlpha);
|
---|
11 |
|
---|
12 | { TPixel32 }
|
---|
13 |
|
---|
14 | TPixel32 = packed record
|
---|
15 | private
|
---|
16 | procedure SetRGB(AValue: Cardinal);
|
---|
17 | function GetRGB: Cardinal;
|
---|
18 | public
|
---|
19 | property RGB: Cardinal read GetRGB write SetRGB;
|
---|
20 | case Integer of
|
---|
21 | 0: (B, G, R, A: Byte);
|
---|
22 | 1: (ARGB: TColor32);
|
---|
23 | 2: (Planes: array[0..3] of Byte);
|
---|
24 | 3: (Components: array[TColor32Component] of Byte);
|
---|
25 | end;
|
---|
26 | PPixel32 = ^TPixel32;
|
---|
27 |
|
---|
28 | { TPixelPointer }
|
---|
29 |
|
---|
30 | TPixelPointer = record
|
---|
31 | Base: PPixel32;
|
---|
32 | Pixel: PPixel32;
|
---|
33 | Line: PPixel32;
|
---|
34 | RelLine: PPixel32;
|
---|
35 | BytesPerPixel: Integer;
|
---|
36 | BytesPerLine: Integer;
|
---|
37 | procedure NextLine; inline; // Move pointer to start of next line
|
---|
38 | procedure PreviousLine; inline; // Move pointer to start of previous line
|
---|
39 | procedure NextPixel; inline; // Move pointer to next pixel
|
---|
40 | procedure PreviousPixel; inline; // Move pointer to previous pixel
|
---|
41 | procedure SetXY(X, Y: Integer); inline; // Set pixel position relative to base
|
---|
42 | procedure SetX(X: Integer); inline; // Set horizontal pixel position relative to base
|
---|
43 | end;
|
---|
44 | PPixelPointer = ^TPixelPointer;
|
---|
45 |
|
---|
46 | function PixelPointer(Bitmap: TRasterImage; BaseX: Integer = 0; BaseY: Integer = 0): TPixelPointer; inline;
|
---|
47 | function SwapRedBlue(Color: TColor32): TColor32;
|
---|
48 | procedure BitmapCopyRect(DstBitmap: TRasterImage; DstRect: TRect; SrcBitmap: TRasterImage; SrcPos: TPoint);
|
---|
49 | procedure BitmapStretchRect(DstBitmap: TRasterImage; DstRect: TRect;
|
---|
50 | SrcBitmap: TRasterImage; SrcRect: TRect);
|
---|
51 | procedure BitmapFill(Bitmap: TRasterImage; Color: TColor32);
|
---|
52 | procedure BitmapFillRect(Bitmap: TRasterImage; Color: TColor32; Rect: TRect);
|
---|
53 | procedure BitmapSwapRedBlue(Bitmap:TRasterImage);
|
---|
54 | procedure BitmapInvert(Bitmap: TRasterImage);
|
---|
55 | procedure BitmapBlendColor(Bitmap: TRasterImage; Color: TColor32);
|
---|
56 | function Color32(A, R, G, B: Byte): TColor32;
|
---|
57 | function Color32ToPixel32(Color: TColor32): TPixel32;
|
---|
58 | function Pixel32ToColor32(Color: TPixel32): TColor32;
|
---|
59 | function Color32ToColor(Color: TColor32): TColor;
|
---|
60 | function ColorToColor32(Color: TColor): TColor32;
|
---|
61 |
|
---|
62 | implementation
|
---|
63 |
|
---|
64 | { TPixel32 }
|
---|
65 |
|
---|
66 | function TPixel32.GetRGB: Cardinal;
|
---|
67 | begin
|
---|
68 | Result := ARGB and $ffffff;
|
---|
69 | end;
|
---|
70 |
|
---|
71 | procedure TPixel32.SetRGB(AValue: Cardinal);
|
---|
72 | begin
|
---|
73 | R := (AValue shr 16) and $ff;
|
---|
74 | G := (AValue shr 8) and $ff;
|
---|
75 | B := (AValue shr 0) and $ff;
|
---|
76 | end;
|
---|
77 |
|
---|
78 | { TPixelPointer }
|
---|
79 |
|
---|
80 | procedure TPixelPointer.NextLine; inline;
|
---|
81 | begin
|
---|
82 | Line := Pointer(Line) + BytesPerLine;
|
---|
83 | Pixel := Line;
|
---|
84 | end;
|
---|
85 |
|
---|
86 | procedure TPixelPointer.PreviousLine;
|
---|
87 | begin
|
---|
88 | Line := Pointer(Line) - BytesPerLine;
|
---|
89 | Pixel := Line;
|
---|
90 | end;
|
---|
91 |
|
---|
92 | procedure TPixelPointer.NextPixel; inline;
|
---|
93 | begin
|
---|
94 | Pixel := Pointer(Pixel) + BytesPerPixel;
|
---|
95 | end;
|
---|
96 |
|
---|
97 | procedure TPixelPointer.PreviousPixel;
|
---|
98 | begin
|
---|
99 | Pixel := Pointer(Pixel) - BytesPerPixel;
|
---|
100 | end;
|
---|
101 |
|
---|
102 | procedure TPixelPointer.SetXY(X, Y: Integer); inline;
|
---|
103 | begin
|
---|
104 | Line := Pointer(Base) + Y * BytesPerLine;
|
---|
105 | SetX(X);
|
---|
106 | end;
|
---|
107 |
|
---|
108 | procedure TPixelPointer.SetX(X: Integer); inline;
|
---|
109 | begin
|
---|
110 | Pixel := Pointer(Line) + X * BytesPerPixel;
|
---|
111 | end;
|
---|
112 |
|
---|
113 | procedure BitmapCopyRect(DstBitmap: TRasterImage; DstRect: TRect;
|
---|
114 | SrcBitmap: TRasterImage; SrcPos: TPoint);
|
---|
115 | var
|
---|
116 | SrcPtr, DstPtr: TPixelPointer;
|
---|
117 | X, Y: Integer;
|
---|
118 | begin
|
---|
119 | SrcBitmap.BeginUpdate(True);
|
---|
120 | DstBitmap.BeginUpdate(True);
|
---|
121 | SrcPtr := PixelPointer(SrcBitmap, SrcPos.X, SrcPos.Y);
|
---|
122 | DstPtr := PixelPointer(DstBitmap, DstRect.Left, DstRect.Top);
|
---|
123 | for Y := 0 to DstRect.Height - 1 do begin
|
---|
124 | for X := 0 to DstRect.Width - 1 do begin
|
---|
125 | DstPtr.Pixel^.ARGB := SrcPtr.Pixel^.ARGB;
|
---|
126 | SrcPtr.NextPixel;
|
---|
127 | DstPtr.NextPixel;
|
---|
128 | end;
|
---|
129 | SrcPtr.NextLine;
|
---|
130 | DstPtr.NextLine;
|
---|
131 | end;
|
---|
132 | SrcBitmap.EndUpdate;
|
---|
133 | DstBitmap.EndUpdate;
|
---|
134 | end;
|
---|
135 |
|
---|
136 | procedure BitmapStretchRect(DstBitmap: TRasterImage; DstRect: TRect;
|
---|
137 | SrcBitmap: TRasterImage; SrcRect: TRect);
|
---|
138 | var
|
---|
139 | SrcPtr, DstPtr: TPixelPointer;
|
---|
140 | X, Y: Integer;
|
---|
141 | XX, YY: Integer;
|
---|
142 | R: TRect;
|
---|
143 | C: TColor32;
|
---|
144 | begin
|
---|
145 | if (DstRect.Width = SrcRect.Width) and (DstRect.Height = SrcRect.Height) then begin
|
---|
146 | BitmapCopyRect(DstBitmap, DstRect, SrcBitmap, Point(SrcRect.Left, SrcRect.Top));
|
---|
147 | Exit;
|
---|
148 | end;
|
---|
149 | SrcBitmap.BeginUpdate(True);
|
---|
150 | DstBitmap.BeginUpdate(True);
|
---|
151 | SrcPtr := PixelPointer(SrcBitmap, SrcRect.Left, SrcRect.Top);
|
---|
152 | DstPtr := PixelPointer(DstBitmap, DstRect.Left, DstRect.Top);
|
---|
153 | for Y := 0 to DstRect.Height - 1 do begin
|
---|
154 | for X := 0 to DstRect.Width - 1 do begin
|
---|
155 | R := Rect(Trunc(X * SrcRect.Width / DstRect.Width),
|
---|
156 | Trunc(Y * SrcRect.Height / DstRect.Height),
|
---|
157 | Trunc((X + 1) * SrcRect.Width / DstRect.Width),
|
---|
158 | Trunc((Y + 1) * SrcRect.Height / DstRect.Height));
|
---|
159 | DstPtr.SetXY(X, Y);
|
---|
160 | SrcPtr.SetXY(R.Left, R.Top);
|
---|
161 | C := SrcPtr.Pixel^.ARGB;
|
---|
162 | DstPtr.Pixel^.ARGB := C;
|
---|
163 | for YY := 0 to R.Height - 1 do begin
|
---|
164 | for XX := 0 to R.Width - 1 do begin
|
---|
165 | DstPtr.Pixel^.ARGB := C;
|
---|
166 | DstPtr.NextPixel;
|
---|
167 | end;
|
---|
168 | DstPtr.NextLine;
|
---|
169 | end;
|
---|
170 | end;
|
---|
171 | end;
|
---|
172 | SrcBitmap.EndUpdate;
|
---|
173 | DstBitmap.EndUpdate;
|
---|
174 | end;
|
---|
175 |
|
---|
176 | procedure BitmapFill(Bitmap: TRasterImage; Color: TColor32);
|
---|
177 | var
|
---|
178 | X, Y: Integer;
|
---|
179 | Ptr: TPixelPointer;
|
---|
180 | begin
|
---|
181 | Bitmap.BeginUpdate(True);
|
---|
182 | Ptr := PixelPointer(Bitmap);
|
---|
183 | for Y := 0 to Bitmap.Height - 1 do begin
|
---|
184 | for X := 0 to Bitmap.Width - 1 do begin
|
---|
185 | Ptr.Pixel^.ARGB := Color;
|
---|
186 | Ptr.NextPixel;
|
---|
187 | end;
|
---|
188 | Ptr.NextLine;
|
---|
189 | end;
|
---|
190 | Bitmap.EndUpdate;
|
---|
191 | end;
|
---|
192 |
|
---|
193 | procedure BitmapFillRect(Bitmap: TRasterImage; Color: TColor32; Rect: TRect);
|
---|
194 | var
|
---|
195 | X, Y: Integer;
|
---|
196 | Ptr: TPixelPointer;
|
---|
197 | begin
|
---|
198 | Bitmap.BeginUpdate(True);
|
---|
199 | Ptr := PixelPointer(Bitmap, Rect.Left, Rect.Top);
|
---|
200 | for Y := 0 to Rect.Height - 1 do begin
|
---|
201 | for X := 0 to Rect.Width - 1 do begin
|
---|
202 | Ptr.Pixel^.ARGB := Color;
|
---|
203 | Ptr.NextPixel;
|
---|
204 | end;
|
---|
205 | Ptr.NextLine;
|
---|
206 | end;
|
---|
207 | Bitmap.EndUpdate;
|
---|
208 | end;
|
---|
209 |
|
---|
210 | procedure BitmapSwapRedBlue(Bitmap: TRasterImage);
|
---|
211 | var
|
---|
212 | X, Y: Integer;
|
---|
213 | Ptr: TPixelPointer;
|
---|
214 | begin
|
---|
215 | Bitmap.BeginUpdate(True);
|
---|
216 | Ptr := PixelPointer(Bitmap);
|
---|
217 | for Y := 0 to Bitmap.Height - 1 do begin
|
---|
218 | for X := 0 to Bitmap.Width - 1 do begin
|
---|
219 | Ptr.Pixel^.ARGB := SwapRedBlue(Ptr.Pixel^.ARGB);
|
---|
220 | Ptr.NextPixel;
|
---|
221 | end;
|
---|
222 | Ptr.NextLine;
|
---|
223 | end;
|
---|
224 | Bitmap.EndUpdate;
|
---|
225 | end;
|
---|
226 |
|
---|
227 | procedure BitmapInvert(Bitmap: TRasterImage);
|
---|
228 | var
|
---|
229 | X, Y: Integer;
|
---|
230 | Ptr: TPixelPointer;
|
---|
231 | begin
|
---|
232 | Bitmap.BeginUpdate(True);
|
---|
233 | Ptr := PixelPointer(Bitmap);
|
---|
234 | for Y := 0 to Bitmap.Height - 1 do begin
|
---|
235 | for X := 0 to Bitmap.Width - 1 do begin
|
---|
236 | Ptr.Pixel^.ARGB := Ptr.Pixel^.ARGB xor $ffffff;
|
---|
237 | Ptr.NextPixel;
|
---|
238 | end;
|
---|
239 | Ptr.NextLine;
|
---|
240 | end;
|
---|
241 | Bitmap.EndUpdate;
|
---|
242 | end;
|
---|
243 |
|
---|
244 | procedure BitmapBlendColor(Bitmap: TRasterImage; Color: TColor32);
|
---|
245 | var
|
---|
246 | X, Y: Integer;
|
---|
247 | Ptr: TPixelPointer;
|
---|
248 | A, R, G, B: Word;
|
---|
249 | Pixel: TPixel32;
|
---|
250 | begin
|
---|
251 | Pixel := Color32ToPixel32(Color);
|
---|
252 | Bitmap.BeginUpdate(True);
|
---|
253 | Ptr := PixelPointer(Bitmap);
|
---|
254 | for Y := 0 to Bitmap.Height - 1 do begin
|
---|
255 | for X := 0 to Bitmap.Width - 1 do begin
|
---|
256 | A := Ptr.Pixel^.A; //(Ptr.Pixel^.A + Pixel.A) shr 1;
|
---|
257 | R := (Ptr.Pixel^.R + Pixel.R) shr 1;
|
---|
258 | G := (Ptr.Pixel^.G + Pixel.G) shr 1;
|
---|
259 | B := (Ptr.Pixel^.B + Pixel.B) shr 1;
|
---|
260 | Ptr.Pixel^.ARGB := Color32(A, R, G, B);
|
---|
261 | Ptr.NextPixel;
|
---|
262 | end;
|
---|
263 | Ptr.NextLine;
|
---|
264 | end;
|
---|
265 | Bitmap.EndUpdate;
|
---|
266 | end;
|
---|
267 |
|
---|
268 | function Color32(A, R, G, B: Byte): TColor32;
|
---|
269 | begin
|
---|
270 | Result := ((A and $ff) shl 24) or ((R and $ff) shl 16) or
|
---|
271 | ((G and $ff) shl 8) or ((B and $ff) shl 0);
|
---|
272 | end;
|
---|
273 |
|
---|
274 | function Color32ToPixel32(Color: TColor32): TPixel32;
|
---|
275 | begin
|
---|
276 | Result.ARGB := Color;
|
---|
277 | end;
|
---|
278 |
|
---|
279 | function Pixel32ToColor32(Color: TPixel32): TColor32;
|
---|
280 | begin
|
---|
281 | Result := Color.ARGB;
|
---|
282 | end;
|
---|
283 |
|
---|
284 | function Color32ToColor(Color: TColor32): TColor;
|
---|
285 | begin
|
---|
286 | Result := ((Color shr 16) and $ff) or (Color and $00ff00) or
|
---|
287 | ((Color and $ff) shl 16);
|
---|
288 | end;
|
---|
289 |
|
---|
290 | function ColorToColor32(Color: TColor): TColor32;
|
---|
291 | begin
|
---|
292 | Result := $ff000000 or ((Color shr 16) and $ff) or (Color and $00ff00) or
|
---|
293 | ((Color and $ff) shl 16);
|
---|
294 | end;
|
---|
295 |
|
---|
296 | function PixelPointer(Bitmap: TRasterImage; BaseX: Integer;
|
---|
297 | BaseY: Integer): TPixelPointer;
|
---|
298 | begin
|
---|
299 | Result.BytesPerLine := Bitmap.RawImage.Description.BytesPerLine;
|
---|
300 | Result.BytesPerPixel := Bitmap.RawImage.Description.BitsPerPixel shr 3;
|
---|
301 | Result.Base := PPixel32(Bitmap.RawImage.Data + BaseX * Result.BytesPerPixel +
|
---|
302 | BaseY * Result.BytesPerLine);
|
---|
303 | Result.SetXY(0, 0);
|
---|
304 | end;
|
---|
305 |
|
---|
306 | function SwapRedBlue(Color: TColor32): TColor32;
|
---|
307 | begin
|
---|
308 | Result := (Color and $ff00ff00) or ((Color and $ff) shl 16) or ((Color shr 16) and $ff);
|
---|
309 | end;
|
---|
310 |
|
---|
311 |
|
---|
312 | end.
|
---|
313 |
|
---|