source: trunk/Packages/Common/UPixelPointer.pas

Last change on this file was 207, checked in by chronos, 3 years ago
  • Modified: Updated Common package.
  • Modified: CoolTranslator package merged into Common package.
  • Fixed: Build with Lazarus 2.0.12
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
62implementation
63
64{ TPixel32 }
65
66function TPixel32.GetRGB: Cardinal;
67begin
68 Result := ARGB and $ffffff;
69end;
70
71procedure TPixel32.SetRGB(AValue: Cardinal);
72begin
73 R := (AValue shr 16) and $ff;
74 G := (AValue shr 8) and $ff;
75 B := (AValue shr 0) and $ff;
76end;
77
78{ TPixelPointer }
79
80procedure TPixelPointer.NextLine; inline;
81begin
82 Line := Pointer(Line) + BytesPerLine;
83 Pixel := Line;
84end;
85
86procedure TPixelPointer.PreviousLine;
87begin
88 Line := Pointer(Line) - BytesPerLine;
89 Pixel := Line;
90end;
91
92procedure TPixelPointer.NextPixel; inline;
93begin
94 Pixel := Pointer(Pixel) + BytesPerPixel;
95end;
96
97procedure TPixelPointer.PreviousPixel;
98begin
99 Pixel := Pointer(Pixel) - BytesPerPixel;
100end;
101
102procedure TPixelPointer.SetXY(X, Y: Integer); inline;
103begin
104 Line := Pointer(Base) + Y * BytesPerLine;
105 SetX(X);
106end;
107
108procedure TPixelPointer.SetX(X: Integer); inline;
109begin
110 Pixel := Pointer(Line) + X * BytesPerPixel;
111end;
112
113procedure BitmapCopyRect(DstBitmap: TRasterImage; DstRect: TRect;
114 SrcBitmap: TRasterImage; SrcPos: TPoint);
115var
116 SrcPtr, DstPtr: TPixelPointer;
117 X, Y: Integer;
118begin
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;
134end;
135
136procedure BitmapStretchRect(DstBitmap: TRasterImage; DstRect: TRect;
137 SrcBitmap: TRasterImage; SrcRect: TRect);
138var
139 SrcPtr, DstPtr: TPixelPointer;
140 X, Y: Integer;
141 XX, YY: Integer;
142 R: TRect;
143 C: TColor32;
144begin
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;
174end;
175
176procedure BitmapFill(Bitmap: TRasterImage; Color: TColor32);
177var
178 X, Y: Integer;
179 Ptr: TPixelPointer;
180begin
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;
191end;
192
193procedure BitmapFillRect(Bitmap: TRasterImage; Color: TColor32; Rect: TRect);
194var
195 X, Y: Integer;
196 Ptr: TPixelPointer;
197begin
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;
208end;
209
210procedure BitmapSwapRedBlue(Bitmap: TRasterImage);
211var
212 X, Y: Integer;
213 Ptr: TPixelPointer;
214begin
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;
225end;
226
227procedure BitmapInvert(Bitmap: TRasterImage);
228var
229 X, Y: Integer;
230 Ptr: TPixelPointer;
231begin
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;
242end;
243
244procedure BitmapBlendColor(Bitmap: TRasterImage; Color: TColor32);
245var
246 X, Y: Integer;
247 Ptr: TPixelPointer;
248 A, R, G, B: Word;
249 Pixel: TPixel32;
250begin
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;
266end;
267
268function Color32(A, R, G, B: Byte): TColor32;
269begin
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);
272end;
273
274function Color32ToPixel32(Color: TColor32): TPixel32;
275begin
276 Result.ARGB := Color;
277end;
278
279function Pixel32ToColor32(Color: TPixel32): TColor32;
280begin
281 Result := Color.ARGB;
282end;
283
284function Color32ToColor(Color: TColor32): TColor;
285begin
286 Result := ((Color shr 16) and $ff) or (Color and $00ff00) or
287 ((Color and $ff) shl 16);
288end;
289
290function ColorToColor32(Color: TColor): TColor32;
291begin
292 Result := $ff000000 or ((Color shr 16) and $ff) or (Color and $00ff00) or
293 ((Color and $ff) shl 16);
294end;
295
296function PixelPointer(Bitmap: TRasterImage; BaseX: Integer;
297 BaseY: Integer): TPixelPointer;
298begin
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);
304end;
305
306function SwapRedBlue(Color: TColor32): TColor32;
307begin
308 Result := (Color and $ff00ff00) or ((Color and $ff) shl 16) or ((Color shr 16) and $ff);
309end;
310
311
312end.
313
Note: See TracBrowser for help on using the repository browser.