source: tools/Image resize/UPixelPointer.pas

Last change on this file was 333, checked in by chronos, 3 years ago
  • Modified: Image resize tool scale up using xbrzscale tool.
  • Modified: Various improvements of generation of scaled up tiles.
File size: 6.5 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 public
18 function GetRGB: Cardinal;
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
55implementation
56
57{ TPixel32 }
58
59function TPixel32.GetRGB: Cardinal;
60begin
61 Result := ARGB and $ffffff;
62end;
63
64procedure TPixel32.SetRGB(AValue: Cardinal);
65begin
66 R := (AValue shr 16) and $ff;
67 G := (AValue shr 8) and $ff;
68 B := (AValue shr 0) and $ff;
69end;
70
71{ TPixelPointer }
72
73procedure TPixelPointer.NextLine; inline;
74begin
75 Line := Pointer(Line) + BytesPerLine;
76 Pixel := Line;
77end;
78
79procedure TPixelPointer.PreviousLine;
80begin
81 Line := Pointer(Line) - BytesPerLine;
82 Pixel := Line;
83end;
84
85procedure TPixelPointer.NextPixel; inline;
86begin
87 Pixel := Pointer(Pixel) + BytesPerPixel;
88end;
89
90procedure TPixelPointer.PreviousPixel;
91begin
92 Pixel := Pointer(Pixel) - BytesPerPixel;
93end;
94
95procedure TPixelPointer.SetXY(X, Y: Integer); inline;
96begin
97 Line := Pointer(Base) + Y * BytesPerLine;
98 SetX(X);
99end;
100
101procedure TPixelPointer.SetX(X: Integer); inline;
102begin
103 Pixel := Pointer(Line) + X * BytesPerPixel;
104end;
105
106procedure BitmapCopyRect(DstBitmap: TRasterImage; DstRect: TRect;
107 SrcBitmap: TRasterImage; SrcPos: TPoint);
108var
109 SrcPtr, DstPtr: TPixelPointer;
110 X, Y: Integer;
111begin
112 SrcBitmap.BeginUpdate(True);
113 DstBitmap.BeginUpdate(True);
114 SrcPtr := PixelPointer(SrcBitmap, SrcPos.X, SrcPos.Y);
115 DstPtr := PixelPointer(DstBitmap, DstRect.Left, DstRect.Top);
116 for Y := 0 to DstRect.Height - 1 do begin
117 for X := 0 to DstRect.Width - 1 do begin
118 DstPtr.Pixel^.ARGB := SrcPtr.Pixel^.ARGB;
119 SrcPtr.NextPixel;
120 DstPtr.NextPixel;
121 end;
122 SrcPtr.NextLine;
123 DstPtr.NextLine;
124 end;
125 SrcBitmap.EndUpdate;
126 DstBitmap.EndUpdate;
127end;
128
129procedure BitmapStretchRect(DstBitmap: TRasterImage; DstRect: TRect;
130 SrcBitmap: TRasterImage; SrcRect: TRect);
131var
132 SrcPtr, DstPtr: TPixelPointer;
133 SubPtr: TPixelPointer;
134 X, Y: Integer;
135 XX, YY: Integer;
136 R: TRect;
137 C: TColor32;
138begin
139 if (DstRect.Width = SrcRect.Width) and (DstRect.Height = SrcRect.Height) then begin
140 BitmapCopyRect(DstBitmap, DstRect, SrcBitmap, Point(SrcRect.Left, SrcRect.Top));
141 Exit;
142 end;
143 SrcBitmap.BeginUpdate(True);
144 DstBitmap.BeginUpdate(True);
145 SrcPtr := PixelPointer(SrcBitmap, SrcRect.Left, SrcRect.Top);
146 DstPtr := PixelPointer(DstBitmap, DstRect.Left, DstRect.Top);
147 for Y := 0 to DstRect.Height - 1 do begin
148 for X := 0 to DstRect.Width - 1 do begin
149 R := Rect(Trunc(X * SrcRect.Width / DstRect.Width),
150 Trunc(Y * SrcRect.Height / DstRect.Height),
151 Trunc((X + 1) * SrcRect.Width / DstRect.Width),
152 Trunc((Y + 1) * SrcRect.Height / DstRect.Height));
153 DstPtr.SetXY(X, Y);
154 SrcPtr.SetXY(R.Left, R.Top);
155 C := SrcPtr.Pixel^.ARGB;
156 DstPtr.Pixel^.ARGB := C;
157 for YY := 0 to R.Height - 1 do begin
158 for XX := 0 to R.Width - 1 do begin
159 DstPtr.Pixel^.ARGB := C;
160 DstPtr.NextPixel;
161 end;
162 DstPtr.NextLine;
163 end;
164 end;
165 end;
166 SrcBitmap.EndUpdate;
167 DstBitmap.EndUpdate;
168end;
169
170procedure BitmapFill(Bitmap: TRasterImage; Color: TColor32);
171var
172 X, Y: Integer;
173 Ptr: TPixelPointer;
174begin
175 Bitmap.BeginUpdate(True);
176 Ptr := PixelPointer(Bitmap);
177 for Y := 0 to Bitmap.Height - 1 do begin
178 for X := 0 to Bitmap.Width - 1 do begin
179 Ptr.Pixel^.ARGB := Color;
180 Ptr.NextPixel;
181 end;
182 Ptr.NextLine;
183 end;
184 Bitmap.EndUpdate;
185end;
186
187procedure BitmapFillRect(Bitmap: TRasterImage; Color: TColor32; Rect: TRect);
188var
189 X, Y: Integer;
190 Ptr: TPixelPointer;
191begin
192 Bitmap.BeginUpdate(True);
193 Ptr := PixelPointer(Bitmap, Rect.Left, Rect.Top);
194 for Y := 0 to Rect.Height - 1 do begin
195 for X := 0 to Rect.Width - 1 do begin
196 Ptr.Pixel^.ARGB := Color;
197 Ptr.NextPixel;
198 end;
199 Ptr.NextLine;
200 end;
201 Bitmap.EndUpdate;
202end;
203
204procedure BitmapSwapRedBlue(Bitmap: TRasterImage);
205var
206 X, Y: Integer;
207 Ptr: TPixelPointer;
208begin
209 Bitmap.BeginUpdate(True);
210 Ptr := PixelPointer(Bitmap);
211 for Y := 0 to Bitmap.Height - 1 do begin
212 for X := 0 to Bitmap.Width - 1 do begin
213 Ptr.Pixel^.ARGB := SwapRedBlue(Ptr.Pixel^.ARGB);
214 Ptr.NextPixel;
215 end;
216 Ptr.NextLine;
217 end;
218 Bitmap.EndUpdate;
219end;
220
221function PixelPointer(Bitmap: TRasterImage; BaseX: Integer;
222 BaseY: Integer): TPixelPointer;
223begin
224 Result.BytesPerLine := Bitmap.RawImage.Description.BytesPerLine;
225 Result.BytesPerPixel := Bitmap.RawImage.Description.BitsPerPixel shr 3;
226 Result.Base := PPixel32(Bitmap.RawImage.Data + BaseX * Result.BytesPerPixel +
227 BaseY * Result.BytesPerLine);
228 Result.SetXY(0, 0);
229end;
230
231function SwapRedBlue(Color: TColor32): TColor32;
232begin
233 Result := (Color and $ff00ff00) or ((Color and $ff) shl 16) or ((Color shr 16) and $ff);
234end;
235
236
237end.
238
Note: See TracBrowser for help on using the repository browser.