source: trunk/Packages/CevoComponents/UPixelPointer.pas@ 347

Last change on this file since 347 was 347, checked in by chronos, 8 months ago
File size: 3.3 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 function GetRGB: Cardinal;
17 procedure SetRGB(AValue: 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 function Color32ToColor(Color: TColor32): TColor;
49 function ColorToColor32(Color: TColor): TColor32;
50
51implementation
52
53{ TPixel32 }
54
55function TPixel32.GetRGB: Cardinal;
56begin
57 Result := ARGB and $ffffff;
58end;
59
60procedure TPixel32.SetRGB(AValue: Cardinal);
61begin
62 R := (AValue shr 16) and $ff;
63 G := (AValue shr 8) and $ff;
64 B := (AValue shr 0) and $ff;
65end;
66
67{ TPixelPointer }
68
69procedure TPixelPointer.NextLine; inline;
70begin
71 Line := Pointer(Line) + BytesPerLine;
72 Pixel := Line;
73end;
74
75procedure TPixelPointer.PreviousLine;
76begin
77 Line := Pointer(Line) - BytesPerLine;
78 Pixel := Line;
79end;
80
81procedure TPixelPointer.NextPixel; inline;
82begin
83 Pixel := Pointer(Pixel) + BytesPerPixel;
84end;
85
86procedure TPixelPointer.PreviousPixel;
87begin
88 Pixel := Pointer(Pixel) - BytesPerPixel;
89end;
90
91procedure TPixelPointer.SetXY(X, Y: Integer); inline;
92begin
93 Line := Pointer(Base) + Y * BytesPerLine;
94 SetX(X);
95end;
96
97procedure TPixelPointer.SetX(X: Integer); inline;
98begin
99 Pixel := Pointer(Line) + X * BytesPerPixel;
100end;
101
102function PixelPointer(Bitmap: TRasterImage; BaseX: Integer;
103 BaseY: Integer): TPixelPointer;
104begin
105 Result.BytesPerLine := Bitmap.RawImage.Description.BytesPerLine;
106 Result.BytesPerPixel := Bitmap.RawImage.Description.BitsPerPixel shr 3;
107 Result.Base := PPixel32(Bitmap.RawImage.Data + BaseX * Result.BytesPerPixel +
108 BaseY * Result.BytesPerLine);
109 Result.SetXY(0, 0);
110end;
111
112function SwapRedBlue(Color: TColor32): TColor32;
113begin
114 Result := (Color and $ff00ff00) or ((Color and $ff) shl 16) or ((Color shr 16) and $ff);
115end;
116
117function Color32ToColor(Color: TColor32): TColor;
118begin
119 Result := ((Color shr 16) and $ff) or (Color and $00ff00) or
120 ((Color and $ff) shl 16);
121end;
122
123function ColorToColor32(Color: TColor): TColor32;
124begin
125 Result := $ff000000 or ((Color shr 16) and $ff) or (Color and $00ff00) or
126 ((Color and $ff) shl 16);
127end;
128
129end.
130
Note: See TracBrowser for help on using the repository browser.