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 | TPixel32 = packed record
|
---|
12 | case Integer of
|
---|
13 | 0: (B, G, R, A: Byte);
|
---|
14 | 1: (ARGB: TColor32);
|
---|
15 | 2: (Planes: array[0..3] of Byte);
|
---|
16 | 3: (Components: array[TColor32Component] of Byte);
|
---|
17 | end;
|
---|
18 | PPixel32 = ^TPixel32;
|
---|
19 |
|
---|
20 | { TPixelPointer }
|
---|
21 |
|
---|
22 | TPixelPointer = record
|
---|
23 | Base: PPixel32;
|
---|
24 | Pixel: PPixel32;
|
---|
25 | Line: PPixel32;
|
---|
26 | RelLine: PPixel32;
|
---|
27 | BytesPerPixel: Integer;
|
---|
28 | BytesPerLine: Integer;
|
---|
29 | procedure NextLine; inline; // Move pointer to start of next line
|
---|
30 | procedure PreviousLine; inline; // Move pointer to start of previous line
|
---|
31 | procedure NextPixel; inline; // Move pointer to next pixel
|
---|
32 | procedure PreviousPixel; inline; // Move pointer to previous pixel
|
---|
33 | procedure SetXY(X, Y: Integer); inline; // Set pixel position relative to base
|
---|
34 | procedure SetX(X: Integer); inline; // Set horizontal pixel position relative to base
|
---|
35 | end;
|
---|
36 | PPixelPointer = ^TPixelPointer;
|
---|
37 |
|
---|
38 | function PixelPointer(Bitmap: TRasterImage; BaseX: Integer = 0; BaseY: Integer = 0): TPixelPointer; inline;
|
---|
39 |
|
---|
40 |
|
---|
41 | implementation
|
---|
42 |
|
---|
43 | { TPixelPointer }
|
---|
44 |
|
---|
45 | procedure TPixelPointer.NextLine; inline;
|
---|
46 | begin
|
---|
47 | Line := Pointer(Line) + BytesPerLine;
|
---|
48 | Pixel := Line;
|
---|
49 | end;
|
---|
50 |
|
---|
51 | procedure TPixelPointer.PreviousLine;
|
---|
52 | begin
|
---|
53 | Line := Pointer(Line) - BytesPerLine;
|
---|
54 | Pixel := Line;
|
---|
55 | end;
|
---|
56 |
|
---|
57 | procedure TPixelPointer.NextPixel; inline;
|
---|
58 | begin
|
---|
59 | Pixel := Pointer(Pixel) + BytesPerPixel;
|
---|
60 | end;
|
---|
61 |
|
---|
62 | procedure TPixelPointer.PreviousPixel;
|
---|
63 | begin
|
---|
64 | Pixel := Pointer(Pixel) - BytesPerPixel;
|
---|
65 | end;
|
---|
66 |
|
---|
67 | procedure TPixelPointer.SetXY(X, Y: Integer); inline;
|
---|
68 | begin
|
---|
69 | Line := Pointer(Base) + Y * BytesPerLine;
|
---|
70 | SetX(X);
|
---|
71 | end;
|
---|
72 |
|
---|
73 | procedure TPixelPointer.SetX(X: Integer); inline;
|
---|
74 | begin
|
---|
75 | Pixel := Pointer(Line) + X * BytesPerPixel;
|
---|
76 | end;
|
---|
77 |
|
---|
78 | function PixelPointer(Bitmap: TRasterImage; BaseX: Integer;
|
---|
79 | BaseY: Integer): TPixelPointer;
|
---|
80 | begin
|
---|
81 | Result.BytesPerLine := Bitmap.RawImage.Description.BytesPerLine;
|
---|
82 | Result.BytesPerPixel := Bitmap.RawImage.Description.BitsPerPixel shr 3;
|
---|
83 | Result.Base := PPixel32(Bitmap.RawImage.Data + BaseX * Result.BytesPerPixel +
|
---|
84 | BaseY * Result.BytesPerLine);
|
---|
85 | Result.SetXY(0, 0);
|
---|
86 | end;
|
---|
87 |
|
---|
88 |
|
---|
89 | end.
|
---|
90 |
|
---|