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