source: trunk/Packages/FastGraphics/ColorFormats/UColorGray1.pas

Last change on this file was 39, checked in by chronos, 6 years ago
  • Added: Mirror, Flip, Gradient, Negative for 4-bit gray image.
File size: 4.8 KB
Line 
1unit UColorGray1;
2
3{$mode delphi}
4
5interface
6
7uses
8 Classes, SysUtils, Graphics, UFGraphics, UGGraphics;
9
10type
11 { TColorFormatGray1 }
12
13 TColorFormatGray1 = class(TColorFormat)
14 constructor Create; override;
15 end;
16
17 TColorGray1 = Byte;
18
19 { TPixmapGray1 }
20
21 TPixmapGray1 = class(TGPixmapBit<TColorGray1>)
22 function Gray1ToColor(Value: TColorGray1): TColor;
23 function ColorToGray1(Value: TColor): TColorGray1;
24 end;
25
26 { TBColorGray1 }
27
28 TBColorGray1 = class(TBColor)
29 Value: TColorGray1;
30 constructor Create(Color: TColorGray1);
31 procedure SetColorName(ColorName: TColorName); override;
32 procedure SetColor(Color: TColor); override;
33 procedure SetRandom; override;
34 end;
35
36 { TBPixmapGray1 }
37
38 TBPixmapGray1 = class(TBPixmap)
39 private
40 FillCallBack: TGetColorPos;
41 function FillGetColor(Position: TPoint): TColorGray1;
42 protected
43 procedure SetSize(AValue: TPoint); override;
44 function GetPixel(X, Y: Integer): IBColor; override;
45 procedure SetPixel(X, Y: Integer; AValue: IBColor); override;
46 public
47 Pixmap: TPixmapGray1;
48 procedure Mirror; override;
49 procedure Flip; override;
50 procedure Fill(Color: IBColor); override;
51 procedure Fill(Func: TGetColorPos); override;
52 procedure Line(P1, P2: TPoint; Color: IBColor); override;
53 procedure PaintToCanvas(Canvas: TCanvas); override;
54 procedure PaintToCanvas(Canvas: TCanvas; Rect: TRect); override;
55 procedure PaintToBitmap(Bitmap: TBitmap; Rect: TRect); override;
56 procedure LoadFromCanvas(Canvas: TCanvas); override;
57 procedure LoadFromBitmap(Bitmap: TBitmap); override;
58 function GetDataSize: Integer; override;
59 constructor Create; override;
60 destructor Destroy; override;
61 end;
62
63
64
65implementation
66
67{ TColorFormatGray1 }
68
69constructor TColorFormatGray1.Create;
70begin
71 inherited;
72 Name := 'Gray 1-bit';
73 BitDepth := 1;
74 BackendColorClass := TBColorGray1;
75 BackendPixmapClass := TBPixmapGray1;
76 AddChannel('Gray', 0, 1);
77end;
78
79{ TPixmapGray1 }
80
81function TPixmapGray1.Gray1ToColor(Value: TColorGray1): TColor;
82begin
83 Value := (Value and $1) * $ff;
84 Result := (Value shl 16) or (Value shl 8) or (Value shl 0);
85end;
86
87function TPixmapGray1.ColorToGray1(Value: TColor): TColorGray1;
88begin
89 Result := Trunc((((Value shr 16) and $ff) + ((Value shr 8) and $ff) + ((Value shr 0) and $ff)) / $300 * 2);
90end;
91
92{ TBColorGray1 }
93
94constructor TBColorGray1.Create(Color: TColorGray1);
95begin
96 Value := Color;
97end;
98
99procedure TBColorGray1.SetColorName(ColorName: TColorName);
100begin
101 case ColorName of
102 cnBlack: Value := 0;
103 cnWhite: Value := 1;
104 else Value := 0;
105 end;
106end;
107
108procedure TBColorGray1.SetColor(Color: TColor);
109begin
110 Value := Trunc((((Color shr 16) and $ff) + ((Color shr 8) and $ff) + ((Color shr 0) and $ff)) / $300 * 2)
111end;
112
113procedure TBColorGray1.SetRandom;
114begin
115 Value := Random(2);
116end;
117
118{ TBPixmapGray1 }
119
120function TBPixmapGray1.FillGetColor(Position: TPoint): TColorGray1;
121begin
122 Result := (FillCallBack(Position) as TBColorGray1).Value;
123end;
124
125procedure TBPixmapGray1.SetSize(AValue: TPoint);
126begin
127 inherited;
128 Pixmap.Size := AValue;
129end;
130
131function TBPixmapGray1.GetPixel(X, Y: Integer): IBColor;
132begin
133 Result := TBColorGray1.Create(Pixmap.Pixels[X, Y]);
134end;
135
136procedure TBPixmapGray1.SetPixel(X, Y: Integer; AValue: IBColor);
137begin
138 Pixmap.Pixels[X, Y] := (AValue as TBColorGray1).Value;
139end;
140
141procedure TBPixmapGray1.Mirror;
142begin
143 Pixmap.Mirror;
144end;
145
146procedure TBPixmapGray1.Flip;
147begin
148 Pixmap.Flip;
149end;
150
151procedure TBPixmapGray1.Fill(Color: IBColor);
152begin
153 if Color is TBColorGray1 then
154 Pixmap.Fill((Color as TBColorGray1).Value);
155end;
156
157procedure TBPixmapGray1.Fill(Func: TGetColorPos);
158begin
159 FillCallBack := Func;
160 Pixmap.Fill(FillGetColor);
161end;
162
163procedure TBPixmapGray1.Line(P1, P2: TPoint; Color: IBColor);
164begin
165 Pixmap.Canvas.Pen.Color := (Color as TBColorGray1).Value;
166 Pixmap.Canvas.Pen.MoveTo(P1);
167 Pixmap.Canvas.Pen.LineTo(P2);
168end;
169
170procedure TBPixmapGray1.PaintToCanvas(Canvas: TCanvas);
171begin
172 Pixmap.PaintToCanvas(Canvas, Pixmap.Gray1ToColor);
173end;
174
175procedure TBPixmapGray1.PaintToCanvas(Canvas: TCanvas; Rect: TRect);
176begin
177 Pixmap.PaintToCanvas(Canvas, Rect, Pixmap.Gray1ToColor);
178end;
179
180procedure TBPixmapGray1.PaintToBitmap(Bitmap: TBitmap; Rect: TRect);
181begin
182 Pixmap.PaintToBitmap(Bitmap, Rect, Pixmap.Gray1ToColor);
183end;
184
185procedure TBPixmapGray1.LoadFromCanvas(Canvas: TCanvas);
186begin
187 Pixmap.LoadFromCanvas(Canvas, Pixmap.ColorToGray1);
188end;
189
190procedure TBPixmapGray1.LoadFromBitmap(Bitmap: TBitmap);
191begin
192 Pixmap.LoadFromBitmap(Bitmap, Pixmap.ColorToGray1);
193end;
194
195function TBPixmapGray1.GetDataSize: Integer;
196begin
197 Result := Pixmap.GetDataSize;
198end;
199
200constructor TBPixmapGray1.Create;
201begin
202 Pixmap := TPixmapGray1.Create;
203 Pixmap.BitsPerPixel := 1;
204end;
205
206destructor TBPixmapGray1.Destroy;
207begin
208 FreeAndNil(Pixmap);
209 inherited;
210end;
211
212
213end.
214
Note: See TracBrowser for help on using the repository browser.