1 | unit UColorGray1;
|
---|
2 |
|
---|
3 | {$mode delphi}
|
---|
4 |
|
---|
5 | interface
|
---|
6 |
|
---|
7 | uses
|
---|
8 | Classes, SysUtils, Graphics, UFGraphics, UGGraphics;
|
---|
9 |
|
---|
10 | type
|
---|
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 |
|
---|
65 | implementation
|
---|
66 |
|
---|
67 | { TColorFormatGray1 }
|
---|
68 |
|
---|
69 | constructor TColorFormatGray1.Create;
|
---|
70 | begin
|
---|
71 | inherited;
|
---|
72 | Name := 'Gray 1-bit';
|
---|
73 | BitDepth := 1;
|
---|
74 | BackendColorClass := TBColorGray1;
|
---|
75 | BackendPixmapClass := TBPixmapGray1;
|
---|
76 | AddChannel('Gray', 0, 1);
|
---|
77 | end;
|
---|
78 |
|
---|
79 | { TPixmapGray1 }
|
---|
80 |
|
---|
81 | function TPixmapGray1.Gray1ToColor(Value: TColorGray1): TColor;
|
---|
82 | begin
|
---|
83 | Value := (Value and $1) * $ff;
|
---|
84 | Result := (Value shl 16) or (Value shl 8) or (Value shl 0);
|
---|
85 | end;
|
---|
86 |
|
---|
87 | function TPixmapGray1.ColorToGray1(Value: TColor): TColorGray1;
|
---|
88 | begin
|
---|
89 | Result := Trunc((((Value shr 16) and $ff) + ((Value shr 8) and $ff) + ((Value shr 0) and $ff)) / $300 * 2);
|
---|
90 | end;
|
---|
91 |
|
---|
92 | { TBColorGray1 }
|
---|
93 |
|
---|
94 | constructor TBColorGray1.Create(Color: TColorGray1);
|
---|
95 | begin
|
---|
96 | Value := Color;
|
---|
97 | end;
|
---|
98 |
|
---|
99 | procedure TBColorGray1.SetColorName(ColorName: TColorName);
|
---|
100 | begin
|
---|
101 | case ColorName of
|
---|
102 | cnBlack: Value := 0;
|
---|
103 | cnWhite: Value := 1;
|
---|
104 | else Value := 0;
|
---|
105 | end;
|
---|
106 | end;
|
---|
107 |
|
---|
108 | procedure TBColorGray1.SetColor(Color: TColor);
|
---|
109 | begin
|
---|
110 | Value := Trunc((((Color shr 16) and $ff) + ((Color shr 8) and $ff) + ((Color shr 0) and $ff)) / $300 * 2)
|
---|
111 | end;
|
---|
112 |
|
---|
113 | procedure TBColorGray1.SetRandom;
|
---|
114 | begin
|
---|
115 | Value := Random(2);
|
---|
116 | end;
|
---|
117 |
|
---|
118 | { TBPixmapGray1 }
|
---|
119 |
|
---|
120 | function TBPixmapGray1.FillGetColor(Position: TPoint): TColorGray1;
|
---|
121 | begin
|
---|
122 | Result := (FillCallBack(Position) as TBColorGray1).Value;
|
---|
123 | end;
|
---|
124 |
|
---|
125 | procedure TBPixmapGray1.SetSize(AValue: TPoint);
|
---|
126 | begin
|
---|
127 | inherited;
|
---|
128 | Pixmap.Size := AValue;
|
---|
129 | end;
|
---|
130 |
|
---|
131 | function TBPixmapGray1.GetPixel(X, Y: Integer): IBColor;
|
---|
132 | begin
|
---|
133 | Result := TBColorGray1.Create(Pixmap.Pixels[X, Y]);
|
---|
134 | end;
|
---|
135 |
|
---|
136 | procedure TBPixmapGray1.SetPixel(X, Y: Integer; AValue: IBColor);
|
---|
137 | begin
|
---|
138 | Pixmap.Pixels[X, Y] := (AValue as TBColorGray1).Value;
|
---|
139 | end;
|
---|
140 |
|
---|
141 | procedure TBPixmapGray1.Mirror;
|
---|
142 | begin
|
---|
143 | Pixmap.Mirror;
|
---|
144 | end;
|
---|
145 |
|
---|
146 | procedure TBPixmapGray1.Flip;
|
---|
147 | begin
|
---|
148 | Pixmap.Flip;
|
---|
149 | end;
|
---|
150 |
|
---|
151 | procedure TBPixmapGray1.Fill(Color: IBColor);
|
---|
152 | begin
|
---|
153 | if Color is TBColorGray1 then
|
---|
154 | Pixmap.Fill((Color as TBColorGray1).Value);
|
---|
155 | end;
|
---|
156 |
|
---|
157 | procedure TBPixmapGray1.Fill(Func: TGetColorPos);
|
---|
158 | begin
|
---|
159 | FillCallBack := Func;
|
---|
160 | Pixmap.Fill(FillGetColor);
|
---|
161 | end;
|
---|
162 |
|
---|
163 | procedure TBPixmapGray1.Line(P1, P2: TPoint; Color: IBColor);
|
---|
164 | begin
|
---|
165 | Pixmap.Canvas.Pen.Color := (Color as TBColorGray1).Value;
|
---|
166 | Pixmap.Canvas.Pen.MoveTo(P1);
|
---|
167 | Pixmap.Canvas.Pen.LineTo(P2);
|
---|
168 | end;
|
---|
169 |
|
---|
170 | procedure TBPixmapGray1.PaintToCanvas(Canvas: TCanvas);
|
---|
171 | begin
|
---|
172 | Pixmap.PaintToCanvas(Canvas, Pixmap.Gray1ToColor);
|
---|
173 | end;
|
---|
174 |
|
---|
175 | procedure TBPixmapGray1.PaintToCanvas(Canvas: TCanvas; Rect: TRect);
|
---|
176 | begin
|
---|
177 | Pixmap.PaintToCanvas(Canvas, Rect, Pixmap.Gray1ToColor);
|
---|
178 | end;
|
---|
179 |
|
---|
180 | procedure TBPixmapGray1.PaintToBitmap(Bitmap: TBitmap; Rect: TRect);
|
---|
181 | begin
|
---|
182 | Pixmap.PaintToBitmap(Bitmap, Rect, Pixmap.Gray1ToColor);
|
---|
183 | end;
|
---|
184 |
|
---|
185 | procedure TBPixmapGray1.LoadFromCanvas(Canvas: TCanvas);
|
---|
186 | begin
|
---|
187 | Pixmap.LoadFromCanvas(Canvas, Pixmap.ColorToGray1);
|
---|
188 | end;
|
---|
189 |
|
---|
190 | procedure TBPixmapGray1.LoadFromBitmap(Bitmap: TBitmap);
|
---|
191 | begin
|
---|
192 | Pixmap.LoadFromBitmap(Bitmap, Pixmap.ColorToGray1);
|
---|
193 | end;
|
---|
194 |
|
---|
195 | function TBPixmapGray1.GetDataSize: Integer;
|
---|
196 | begin
|
---|
197 | Result := Pixmap.GetDataSize;
|
---|
198 | end;
|
---|
199 |
|
---|
200 | constructor TBPixmapGray1.Create;
|
---|
201 | begin
|
---|
202 | Pixmap := TPixmapGray1.Create;
|
---|
203 | Pixmap.BitsPerPixel := 1;
|
---|
204 | end;
|
---|
205 |
|
---|
206 | destructor TBPixmapGray1.Destroy;
|
---|
207 | begin
|
---|
208 | FreeAndNil(Pixmap);
|
---|
209 | inherited;
|
---|
210 | end;
|
---|
211 |
|
---|
212 |
|
---|
213 | end.
|
---|
214 |
|
---|