Changeset 333 for tools/Image resize/UPixelPointer.pas
- Timestamp:
- Mar 31, 2021, 7:21:45 PM (4 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
tools/Image resize/UPixelPointer.pas
r280 r333 9 9 TColor32 = type Cardinal; 10 10 TColor32Component = (ccBlue, ccGreen, ccRed, ccAlpha); 11 12 { TPixel32 } 13 11 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; 12 20 case Integer of 13 21 0: (B, G, R, A: Byte); … … 37 45 38 46 function PixelPointer(Bitmap: TRasterImage; BaseX: Integer = 0; BaseY: Integer = 0): TPixelPointer; inline; 39 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); 40 54 41 55 implementation 56 57 { TPixel32 } 58 59 function TPixel32.GetRGB: Cardinal; 60 begin 61 Result := ARGB and $ffffff; 62 end; 63 64 procedure TPixel32.SetRGB(AValue: Cardinal); 65 begin 66 R := (AValue shr 16) and $ff; 67 G := (AValue shr 8) and $ff; 68 B := (AValue shr 0) and $ff; 69 end; 42 70 43 71 { TPixelPointer } … … 74 102 begin 75 103 Pixel := Pointer(Line) + X * BytesPerPixel; 104 end; 105 106 procedure BitmapCopyRect(DstBitmap: TRasterImage; DstRect: TRect; 107 SrcBitmap: TRasterImage; SrcPos: TPoint); 108 var 109 SrcPtr, DstPtr: TPixelPointer; 110 X, Y: Integer; 111 begin 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; 127 end; 128 129 procedure BitmapStretchRect(DstBitmap: TRasterImage; DstRect: TRect; 130 SrcBitmap: TRasterImage; SrcRect: TRect); 131 var 132 SrcPtr, DstPtr: TPixelPointer; 133 SubPtr: TPixelPointer; 134 X, Y: Integer; 135 XX, YY: Integer; 136 R: TRect; 137 C: TColor32; 138 begin 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; 168 end; 169 170 procedure BitmapFill(Bitmap: TRasterImage; Color: TColor32); 171 var 172 X, Y: Integer; 173 Ptr: TPixelPointer; 174 begin 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; 185 end; 186 187 procedure BitmapFillRect(Bitmap: TRasterImage; Color: TColor32; Rect: TRect); 188 var 189 X, Y: Integer; 190 Ptr: TPixelPointer; 191 begin 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; 202 end; 203 204 procedure BitmapSwapRedBlue(Bitmap: TRasterImage); 205 var 206 X, Y: Integer; 207 Ptr: TPixelPointer; 208 begin 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; 76 219 end; 77 220 … … 86 229 end; 87 230 231 function SwapRedBlue(Color: TColor32): TColor32; 232 begin 233 Result := (Color and $ff00ff00) or ((Color and $ff) shl 16) or ((Color shr 16) and $ff); 234 end; 235 88 236 89 237 end.
Note:
See TracChangeset
for help on using the changeset viewer.