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