source: trunk/Packages/Common/UPixelPointer.pas

Last change on this file was 10, checked in by chronos, 2 years ago
  • Modified: General UTest unit moved into Common package.
File size: 8.6 KB
Line 
1unit UPixelPointer;
2
3interface
4
5uses
6 Classes, SysUtils, Graphics;
7
8type
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
63implementation
64
65{ TPixel32 }
66
67function TPixel32.GetRGB: Cardinal;
68begin
69 Result := ARGB and $ffffff;
70end;
71
72procedure TPixel32.SetRGB(AValue: Cardinal);
73begin
74 R := (AValue shr 16) and $ff;
75 G := (AValue shr 8) and $ff;
76 B := (AValue shr 0) and $ff;
77end;
78
79{ TPixelPointer }
80
81procedure TPixelPointer.NextLine; inline;
82begin
83 Line := Pointer(Line) + BytesPerLine;
84 Pixel := Line;
85end;
86
87procedure TPixelPointer.PreviousLine;
88begin
89 Line := Pointer(Line) - BytesPerLine;
90 Pixel := Line;
91end;
92
93procedure TPixelPointer.NextPixel; inline;
94begin
95 Pixel := Pointer(Pixel) + BytesPerPixel;
96end;
97
98procedure TPixelPointer.PreviousPixel;
99begin
100 Pixel := Pointer(Pixel) - BytesPerPixel;
101end;
102
103procedure TPixelPointer.SetXY(X, Y: Integer); inline;
104begin
105 Line := Pointer(Base) + Y * BytesPerLine;
106 SetX(X);
107end;
108
109procedure TPixelPointer.SetX(X: Integer); inline;
110begin
111 Pixel := Pointer(Line) + X * BytesPerPixel;
112end;
113
114procedure BitmapCopyRect(DstBitmap: TRasterImage; DstRect: TRect;
115 SrcBitmap: TRasterImage; SrcPos: TPoint);
116var
117 SrcPtr, DstPtr: TPixelPointer;
118 X, Y: Integer;
119begin
120 SrcBitmap.BeginUpdate(True);
121 DstBitmap.BeginUpdate(True);
122 SrcPtr := PixelPointer(SrcBitmap, SrcPos.X, SrcPos.Y);
123 DstPtr := PixelPointer(DstBitmap, DstRect.Left, DstRect.Top);
124 for Y := 0 to DstRect.Height - 1 do begin
125 for X := 0 to DstRect.Width - 1 do begin
126 DstPtr.Pixel^.ARGB := SrcPtr.Pixel^.ARGB;
127 SrcPtr.NextPixel;
128 DstPtr.NextPixel;
129 end;
130 SrcPtr.NextLine;
131 DstPtr.NextLine;
132 end;
133 SrcBitmap.EndUpdate;
134 DstBitmap.EndUpdate;
135end;
136
137procedure BitmapStretchRect(DstBitmap: TRasterImage; DstRect: TRect;
138 SrcBitmap: TRasterImage; SrcRect: TRect);
139var
140 SrcPtr, DstPtr: TPixelPointer;
141 X, Y: Integer;
142 XX, YY: Integer;
143 R: TRect;
144 C: TColor32;
145begin
146 if (DstRect.Width = SrcRect.Width) and (DstRect.Height = SrcRect.Height) then begin
147 BitmapCopyRect(DstBitmap, DstRect, SrcBitmap, Point(SrcRect.Left, SrcRect.Top));
148 Exit;
149 end;
150 SrcBitmap.BeginUpdate(True);
151 DstBitmap.BeginUpdate(True);
152 SrcPtr := PixelPointer(SrcBitmap, SrcRect.Left, SrcRect.Top);
153 DstPtr := PixelPointer(DstBitmap, DstRect.Left, DstRect.Top);
154 for Y := 0 to DstRect.Height - 1 do begin
155 for X := 0 to DstRect.Width - 1 do begin
156 R := Rect(Trunc(X * SrcRect.Width / DstRect.Width),
157 Trunc(Y * SrcRect.Height / DstRect.Height),
158 Trunc((X + 1) * SrcRect.Width / DstRect.Width),
159 Trunc((Y + 1) * SrcRect.Height / DstRect.Height));
160 DstPtr.SetXY(X, Y);
161 SrcPtr.SetXY(R.Left, R.Top);
162 C := SrcPtr.Pixel^.ARGB;
163 DstPtr.Pixel^.ARGB := C;
164 for YY := 0 to R.Height - 1 do begin
165 for XX := 0 to R.Width - 1 do begin
166 DstPtr.Pixel^.ARGB := C;
167 DstPtr.NextPixel;
168 end;
169 DstPtr.NextLine;
170 end;
171 end;
172 end;
173 SrcBitmap.EndUpdate;
174 DstBitmap.EndUpdate;
175end;
176
177procedure BitmapFill(Bitmap: TRasterImage; Color: TColor32);
178var
179 X, Y: Integer;
180 Ptr: TPixelPointer;
181begin
182 Bitmap.BeginUpdate(True);
183 Ptr := PixelPointer(Bitmap);
184 for Y := 0 to Bitmap.Height - 1 do begin
185 for X := 0 to Bitmap.Width - 1 do begin
186 Ptr.Pixel^.ARGB := Color;
187 Ptr.NextPixel;
188 end;
189 Ptr.NextLine;
190 end;
191 Bitmap.EndUpdate;
192end;
193
194procedure BitmapFillRect(Bitmap: TRasterImage; Color: TColor32; Rect: TRect);
195var
196 X, Y: Integer;
197 Ptr: TPixelPointer;
198begin
199 Bitmap.BeginUpdate(True);
200 Ptr := PixelPointer(Bitmap, Rect.Left, Rect.Top);
201 for Y := 0 to Rect.Height - 1 do begin
202 for X := 0 to Rect.Width - 1 do begin
203 Ptr.Pixel^.ARGB := Color;
204 Ptr.NextPixel;
205 end;
206 Ptr.NextLine;
207 end;
208 Bitmap.EndUpdate;
209end;
210
211procedure BitmapSwapRedBlue(Bitmap: TRasterImage);
212var
213 X, Y: Integer;
214 Ptr: TPixelPointer;
215begin
216 Bitmap.BeginUpdate(True);
217 Ptr := PixelPointer(Bitmap);
218 for Y := 0 to Bitmap.Height - 1 do begin
219 for X := 0 to Bitmap.Width - 1 do begin
220 Ptr.Pixel^.ARGB := SwapRedBlue(Ptr.Pixel^.ARGB);
221 Ptr.NextPixel;
222 end;
223 Ptr.NextLine;
224 end;
225 Bitmap.EndUpdate;
226end;
227
228procedure BitmapInvert(Bitmap: TRasterImage);
229var
230 X, Y: Integer;
231 Ptr: TPixelPointer;
232begin
233 Bitmap.BeginUpdate(True);
234 Ptr := PixelPointer(Bitmap);
235 for Y := 0 to Bitmap.Height - 1 do begin
236 for X := 0 to Bitmap.Width - 1 do begin
237 Ptr.Pixel^.ARGB := Ptr.Pixel^.ARGB xor $ffffff;
238 Ptr.NextPixel;
239 end;
240 Ptr.NextLine;
241 end;
242 Bitmap.EndUpdate;
243end;
244
245procedure BitmapBlendColor(Bitmap: TRasterImage; Color: TColor32);
246var
247 X, Y: Integer;
248 Ptr: TPixelPointer;
249 A, R, G, B: Word;
250 Pixel: TPixel32;
251begin
252 Pixel := Color32ToPixel32(Color);
253 Bitmap.BeginUpdate(True);
254 Ptr := PixelPointer(Bitmap);
255 for Y := 0 to Bitmap.Height - 1 do begin
256 for X := 0 to Bitmap.Width - 1 do begin
257 A := Ptr.Pixel^.A; //(Ptr.Pixel^.A + Pixel.A) shr 1;
258 R := (Ptr.Pixel^.R + Pixel.R) shr 1;
259 G := (Ptr.Pixel^.G + Pixel.G) shr 1;
260 B := (Ptr.Pixel^.B + Pixel.B) shr 1;
261 Ptr.Pixel^.ARGB := Color32(A, R, G, B);
262 Ptr.NextPixel;
263 end;
264 Ptr.NextLine;
265 end;
266 Bitmap.EndUpdate;
267end;
268
269function Color32(A, R, G, B: Byte): TColor32;
270begin
271 Result := ((A and $ff) shl 24) or ((R and $ff) shl 16) or
272 ((G and $ff) shl 8) or ((B and $ff) shl 0);
273end;
274
275function Color32ToPixel32(Color: TColor32): TPixel32;
276begin
277 Result.ARGB := Color;
278end;
279
280function Pixel32ToColor32(Color: TPixel32): TColor32;
281begin
282 Result := Color.ARGB;
283end;
284
285function Color32ToColor(Color: TColor32): TColor;
286begin
287 Result := ((Color shr 16) and $ff) or (Color and $00ff00) or
288 ((Color and $ff) shl 16);
289end;
290
291function ColorToColor32(Color: TColor): TColor32;
292begin
293 Result := $ff000000 or ((Color shr 16) and $ff) or (Color and $00ff00) or
294 ((Color and $ff) shl 16);
295end;
296
297function PixelPointer(Bitmap: TRasterImage; BaseX: Integer;
298 BaseY: Integer): TPixelPointer;
299begin
300 Result.BytesPerLine := Bitmap.RawImage.Description.BytesPerLine;
301 Result.BytesPerPixel := Bitmap.RawImage.Description.BitsPerPixel shr 3;
302 Result.Base := PPixel32(Bitmap.RawImage.Data + BaseX * Result.BytesPerPixel +
303 BaseY * Result.BytesPerLine);
304 Result.SetXY(0, 0);
305end;
306
307function SwapRedBlue(Color: TColor32): TColor32;
308begin
309 Result := (Color and $ff00ff00) or ((Color and $ff) shl 16) or ((Color shr 16) and $ff);
310end;
311
312
313end.
314
Note: See TracBrowser for help on using the repository browser.