| 1 | unit UPixelPointer;
|
|---|
| 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 | 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 |
|
|---|
| 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;
|
|---|
| 70 |
|
|---|
| 71 | { TPixelPointer }
|
|---|
| 72 |
|
|---|
| 73 | procedure TPixelPointer.NextLine; inline;
|
|---|
| 74 | begin
|
|---|
| 75 | Line := Pointer(Line) + BytesPerLine;
|
|---|
| 76 | Pixel := Line;
|
|---|
| 77 | end;
|
|---|
| 78 |
|
|---|
| 79 | procedure TPixelPointer.PreviousLine;
|
|---|
| 80 | begin
|
|---|
| 81 | Line := Pointer(Line) - BytesPerLine;
|
|---|
| 82 | Pixel := Line;
|
|---|
| 83 | end;
|
|---|
| 84 |
|
|---|
| 85 | procedure TPixelPointer.NextPixel; inline;
|
|---|
| 86 | begin
|
|---|
| 87 | Pixel := Pointer(Pixel) + BytesPerPixel;
|
|---|
| 88 | end;
|
|---|
| 89 |
|
|---|
| 90 | procedure TPixelPointer.PreviousPixel;
|
|---|
| 91 | begin
|
|---|
| 92 | Pixel := Pointer(Pixel) - BytesPerPixel;
|
|---|
| 93 | end;
|
|---|
| 94 |
|
|---|
| 95 | procedure TPixelPointer.SetXY(X, Y: Integer); inline;
|
|---|
| 96 | begin
|
|---|
| 97 | Line := Pointer(Base) + Y * BytesPerLine;
|
|---|
| 98 | SetX(X);
|
|---|
| 99 | end;
|
|---|
| 100 |
|
|---|
| 101 | procedure TPixelPointer.SetX(X: Integer); inline;
|
|---|
| 102 | begin
|
|---|
| 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;
|
|---|
| 219 | end;
|
|---|
| 220 |
|
|---|
| 221 | function PixelPointer(Bitmap: TRasterImage; BaseX: Integer;
|
|---|
| 222 | BaseY: Integer): TPixelPointer;
|
|---|
| 223 | begin
|
|---|
| 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);
|
|---|
| 229 | end;
|
|---|
| 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 |
|
|---|
| 236 |
|
|---|
| 237 | end.
|
|---|
| 238 |
|
|---|