1 | unit UFGraphics;
|
---|
2 |
|
---|
3 | {$mode delphi}
|
---|
4 |
|
---|
5 | interface
|
---|
6 |
|
---|
7 | uses
|
---|
8 | Classes, SysUtils, Graphics, UGGraphics, UPixmapSpecialized, Contnrs;
|
---|
9 |
|
---|
10 | type
|
---|
11 | TColorName = (cnBlack, cnWhite, cnBlue, cnRed, cnGreen, cnGray, cnSilver);
|
---|
12 |
|
---|
13 | IBColor = interface
|
---|
14 | procedure SetColorName(ColorName: TColorName);
|
---|
15 | procedure SetRandom;
|
---|
16 | end;
|
---|
17 |
|
---|
18 | { TBColor }
|
---|
19 |
|
---|
20 | TBColor = class(TInterfacedObject, IBColor)
|
---|
21 | procedure SetColorName(ColorName: TColorName); virtual;
|
---|
22 | procedure SetRandom; virtual;
|
---|
23 | end;
|
---|
24 |
|
---|
25 | IBColorClass = class of TBColor;
|
---|
26 |
|
---|
27 | { TBImage }
|
---|
28 |
|
---|
29 | TBImage = class
|
---|
30 | public
|
---|
31 | type
|
---|
32 | TGetColorPos = function (Position: TPoint): IBColor of object;
|
---|
33 | private
|
---|
34 | FSize: TPoint;
|
---|
35 | protected
|
---|
36 | function GetPixel(X, Y: Integer): IBColor; virtual;
|
---|
37 | procedure SetPixel(X, Y: Integer; AValue: IBColor); virtual;
|
---|
38 | procedure SetSize(AValue: TPoint); virtual;
|
---|
39 | public
|
---|
40 | procedure Fill(Color: IBColor); virtual; overload;
|
---|
41 | procedure Fill(Func: TGetColorPos); virtual; overload;
|
---|
42 | procedure Line(P1, P2: TPoint; Color: IBColor); virtual;
|
---|
43 | procedure PaintToCanvas(Canvas: TCanvas); virtual;
|
---|
44 | function GetDataSize: Integer; virtual;
|
---|
45 | constructor Create; virtual;
|
---|
46 | property Size: TPoint read FSize write SetSize;
|
---|
47 | property Pixels[X, Y: Integer]: IBColor read GetPixel write SetPixel;
|
---|
48 | end;
|
---|
49 |
|
---|
50 | TBPixmapClass = class of TBImage;
|
---|
51 |
|
---|
52 | { TColorFormatChannel }
|
---|
53 |
|
---|
54 | TColorFormatChannel = record
|
---|
55 | Name: string;
|
---|
56 | Position: Integer;
|
---|
57 | BitWidth: Integer;
|
---|
58 | end;
|
---|
59 |
|
---|
60 | { TColorFormat }
|
---|
61 |
|
---|
62 | TColorFormat = class
|
---|
63 | Name: string;
|
---|
64 | BitDepth: Integer;
|
---|
65 | Channels: array of TColorFormatChannel;
|
---|
66 | BackendColorClass: IBColorClass;
|
---|
67 | BackendImageClass: TBPixmapClass;
|
---|
68 | procedure AddChannel(Name: string; Position, BitWidth: Integer);
|
---|
69 | function GetBackendColor: IBColor;
|
---|
70 | function GetBackendImage: TBImage;
|
---|
71 | constructor Create; virtual;
|
---|
72 | function GetChannelStateCount(Channel: Integer): Integer;
|
---|
73 | end;
|
---|
74 |
|
---|
75 | TColorFormatClass = class of TColorFormat;
|
---|
76 |
|
---|
77 | { TColorFormatManager }
|
---|
78 |
|
---|
79 | TColorFormatManager = class
|
---|
80 | private
|
---|
81 | FFormats: TObjectList; // TList<TColorFormat>
|
---|
82 | function GetFormat(Index: Integer): TColorFormat;
|
---|
83 | public
|
---|
84 | constructor Create; virtual;
|
---|
85 | destructor Destroy; override;
|
---|
86 | procedure RegisterFormat(Format: TColorFormatClass);
|
---|
87 | function FormatCount: Integer;
|
---|
88 | property Formats[Index: Integer]: TColorFormat read GetFormat;
|
---|
89 | end;
|
---|
90 |
|
---|
91 | IFColor = interface
|
---|
92 | end;
|
---|
93 |
|
---|
94 | { TFColor }
|
---|
95 |
|
---|
96 | TFColor = class(TInterfacedObject, IFColor)
|
---|
97 | private
|
---|
98 | FBackend: IBColor;
|
---|
99 | FColorFormat: TColorFormat;
|
---|
100 | procedure SetColorFormat(AValue: TColorFormat);
|
---|
101 | public
|
---|
102 | property Backend: IBColor read FBackend;
|
---|
103 | property ColorFormat: TColorFormat read FColorFormat write SetColorFormat;
|
---|
104 | procedure SetColorName(ColorName: TColorName);
|
---|
105 | procedure SetRandom;
|
---|
106 | constructor Create; overload;
|
---|
107 | constructor Create(ColorFormat: TColorFormat; ColorName: TColorName); overload;
|
---|
108 | destructor Destroy; override;
|
---|
109 | end;
|
---|
110 |
|
---|
111 | TFCanvas = class;
|
---|
112 | TFPixmap = class;
|
---|
113 |
|
---|
114 | { TFBrush }
|
---|
115 |
|
---|
116 | TFBrush = class
|
---|
117 | Color: IFColor;
|
---|
118 | Canvas: TFCanvas;
|
---|
119 | end;
|
---|
120 |
|
---|
121 | { TFPen }
|
---|
122 |
|
---|
123 | TFPen = class
|
---|
124 | Position: TPoint;
|
---|
125 | Color: IFColor;
|
---|
126 | Canvas: TFCanvas;
|
---|
127 | procedure MoveTo(Pos: TPoint);
|
---|
128 | procedure LineTo(Pos: TPoint);
|
---|
129 | end;
|
---|
130 |
|
---|
131 | { TFCanvas }
|
---|
132 |
|
---|
133 | TFCanvas = class
|
---|
134 | Pixmap: TFPixmap;
|
---|
135 | Pen: TFPen;
|
---|
136 | Brush: TFBrush;
|
---|
137 | constructor Create;
|
---|
138 | destructor Destroy; override;
|
---|
139 | end;
|
---|
140 |
|
---|
141 |
|
---|
142 | { TFPixmap }
|
---|
143 |
|
---|
144 | TFPixmap = class(TComponent)
|
---|
145 | public
|
---|
146 | type
|
---|
147 | TGetColorPos = function (Position: TPoint; ColorFormat: TColorFormat): IFColor of object;
|
---|
148 | private
|
---|
149 | FBackend: TBImage;
|
---|
150 | FCanvas: TFCanvas;
|
---|
151 | FColorFormat: TColorFormat;
|
---|
152 | FSize: TPoint;
|
---|
153 | FillCallBack: TGetColorPos;
|
---|
154 | function FillGetColor(Position: TPoint): IBColor;
|
---|
155 | function GetPixel(X, Y: Integer): IFColor;
|
---|
156 | procedure SetColorFormat(AValue: TColorFormat);
|
---|
157 | procedure SetPixel(X, Y: Integer; AValue: IFColor);
|
---|
158 | procedure SetSize(AValue: TPoint);
|
---|
159 | public
|
---|
160 | procedure Fill(Color: IFColor); overload;
|
---|
161 | procedure Fill(Func: TGetColorPos); overload;
|
---|
162 | procedure PaintToCanvas(Canvas: TCanvas);
|
---|
163 | function GetDataSize: Integer;
|
---|
164 | constructor Create(AOwner: TComponent); override;
|
---|
165 | destructor Destroy; override;
|
---|
166 | property Canvas: TFCanvas read FCanvas;
|
---|
167 | property Backend: TBImage read FBackend;
|
---|
168 | property ColorFormat: TColorFormat read FColorFormat write SetColorFormat;
|
---|
169 | property Size: TPoint read FSize write SetSize;
|
---|
170 | property Pixels[X, Y: Integer]: IFColor read GetPixel write SetPixel;
|
---|
171 | end;
|
---|
172 |
|
---|
173 | procedure Register;
|
---|
174 |
|
---|
175 | var
|
---|
176 | ColorFormatManager: TColorFormatManager;
|
---|
177 |
|
---|
178 |
|
---|
179 | implementation
|
---|
180 |
|
---|
181 | uses
|
---|
182 | UColorGray1, UColorGray2,UColorGray4, UColorGray8, UColorRGB8, UColorRGBA8,
|
---|
183 | UColorRGB565;
|
---|
184 |
|
---|
185 | procedure Register;
|
---|
186 | begin
|
---|
187 | RegisterComponents('FastGraphics', [TFPixmap]);
|
---|
188 | end;
|
---|
189 |
|
---|
190 | { TFPen }
|
---|
191 |
|
---|
192 | procedure TFPen.MoveTo(Pos: TPoint);
|
---|
193 | begin
|
---|
194 | Position := Pos;
|
---|
195 | end;
|
---|
196 |
|
---|
197 | procedure TFPen.LineTo(Pos: TPoint);
|
---|
198 | begin
|
---|
199 | Canvas.Pixmap.Backend.Line(Position, Pos, (Color as TFColor).Backend);
|
---|
200 | Position := Pos;
|
---|
201 | end;
|
---|
202 |
|
---|
203 | { TFCanvas }
|
---|
204 |
|
---|
205 | constructor TFCanvas.Create;
|
---|
206 | begin
|
---|
207 | Pen := TFPen.Create;
|
---|
208 | Pen.Canvas := Self;
|
---|
209 | Brush := TFBrush.Create;
|
---|
210 | Brush.Canvas := Self;
|
---|
211 | end;
|
---|
212 |
|
---|
213 | destructor TFCanvas.Destroy;
|
---|
214 | begin
|
---|
215 | FreeAndNil(Pen);
|
---|
216 | FreeAndNil(Brush);
|
---|
217 | inherited Destroy;
|
---|
218 | end;
|
---|
219 |
|
---|
220 | { TBColor }
|
---|
221 |
|
---|
222 | procedure TBColor.SetColorName(ColorName: TColorName);
|
---|
223 | begin
|
---|
224 | end;
|
---|
225 |
|
---|
226 | procedure TBColor.SetRandom;
|
---|
227 | begin
|
---|
228 | end;
|
---|
229 |
|
---|
230 | { TColorFormatManager }
|
---|
231 |
|
---|
232 | function TColorFormatManager.GetFormat(Index: Integer): TColorFormat;
|
---|
233 | begin
|
---|
234 | Result := TColorFormat(FFormats[Index]);
|
---|
235 | end;
|
---|
236 |
|
---|
237 | constructor TColorFormatManager.Create;
|
---|
238 | begin
|
---|
239 | FFormats := TObjectList.Create;
|
---|
240 | end;
|
---|
241 |
|
---|
242 | destructor TColorFormatManager.Destroy;
|
---|
243 | begin
|
---|
244 | FreeAndNil(FFormats);
|
---|
245 | inherited Destroy;
|
---|
246 | end;
|
---|
247 |
|
---|
248 | procedure TColorFormatManager.RegisterFormat(Format: TColorFormatClass);
|
---|
249 | begin
|
---|
250 | FFormats.Add(Format.Create);
|
---|
251 | end;
|
---|
252 |
|
---|
253 | function TColorFormatManager.FormatCount: Integer;
|
---|
254 | begin
|
---|
255 | Result := FFormats.Count;
|
---|
256 | end;
|
---|
257 |
|
---|
258 | { TColorFormat }
|
---|
259 |
|
---|
260 | procedure TColorFormat.AddChannel(Name: string; Position, BitWidth: Integer);
|
---|
261 | begin
|
---|
262 | SetLength(Channels, Length(Channels) + 1);
|
---|
263 | Channels[Length(Channels) - 1].Name := Name;
|
---|
264 | Channels[Length(Channels) - 1].Position := Position;
|
---|
265 | Channels[Length(Channels) - 1].BitWidth := BitWidth;
|
---|
266 | end;
|
---|
267 |
|
---|
268 | function TColorFormat.GetBackendColor: IBColor;
|
---|
269 | begin
|
---|
270 | Result := BackendColorClass.Create;
|
---|
271 | end;
|
---|
272 |
|
---|
273 | function TColorFormat.GetBackendImage: TBImage;
|
---|
274 | begin
|
---|
275 | Result := BackendImageClass.Create;
|
---|
276 | end;
|
---|
277 |
|
---|
278 | constructor TColorFormat.Create;
|
---|
279 | begin
|
---|
280 | Name := 'None';
|
---|
281 | BitDepth := 0;
|
---|
282 | BackendColorClass := TBColor;
|
---|
283 | BackendImageClass := TBImage;
|
---|
284 | end;
|
---|
285 |
|
---|
286 | function TColorFormat.GetChannelStateCount(Channel: Integer): Integer;
|
---|
287 | begin
|
---|
288 | Result := 1 shl Channels[Channel].BitWidth;
|
---|
289 | end;
|
---|
290 |
|
---|
291 | { TFColor }
|
---|
292 |
|
---|
293 | procedure TFColor.SetColorFormat(AValue: TColorFormat);
|
---|
294 | begin
|
---|
295 | if FColorFormat = AValue then Exit;
|
---|
296 | FBackend := AValue.GetBackendColor;
|
---|
297 | FColorFormat := AValue;
|
---|
298 | end;
|
---|
299 |
|
---|
300 | procedure TFColor.SetColorName(ColorName: TColorName);
|
---|
301 | begin
|
---|
302 | FBackend.SetColorName(ColorName);
|
---|
303 | end;
|
---|
304 |
|
---|
305 | procedure TFColor.SetRandom;
|
---|
306 | begin
|
---|
307 | FBackend.SetRandom;
|
---|
308 | end;
|
---|
309 |
|
---|
310 | constructor TFColor.Create;
|
---|
311 | begin
|
---|
312 | ColorFormat := ColorFormatManager.GetFormat(0);
|
---|
313 | end;
|
---|
314 |
|
---|
315 | constructor TFColor.Create(ColorFormat: TColorFormat; ColorName: TColorName);
|
---|
316 | begin
|
---|
317 | Self.ColorFormat := ColorFormat;
|
---|
318 | Backend.SetColorName(ColorName);
|
---|
319 | end;
|
---|
320 |
|
---|
321 | destructor TFColor.Destroy;
|
---|
322 | begin
|
---|
323 | FColorFormat := nil;
|
---|
324 | inherited Destroy;
|
---|
325 | end;
|
---|
326 |
|
---|
327 | { TBImage }
|
---|
328 |
|
---|
329 | function TBImage.GetPixel(X, Y: Integer): IBColor;
|
---|
330 | begin
|
---|
331 | Result := TBColor.Create;
|
---|
332 | end;
|
---|
333 |
|
---|
334 | procedure TBImage.SetPixel(X, Y: Integer; AValue: IBColor);
|
---|
335 | begin
|
---|
336 | end;
|
---|
337 |
|
---|
338 | procedure TBImage.SetSize(AValue: TPoint);
|
---|
339 | begin
|
---|
340 | if (FSize.X = AValue.X) and (FSize.Y = AValue.Y) then Exit;
|
---|
341 | FSize := AValue;
|
---|
342 | end;
|
---|
343 |
|
---|
344 | procedure TBImage.Fill(Color: IBColor);
|
---|
345 | begin
|
---|
346 | end;
|
---|
347 |
|
---|
348 | procedure TBImage.Fill(Func: TGetColorPos);
|
---|
349 | begin
|
---|
350 | end;
|
---|
351 |
|
---|
352 | procedure TBImage.Line(P1, P2: TPoint; Color: IBColor);
|
---|
353 | begin
|
---|
354 | end;
|
---|
355 |
|
---|
356 | procedure TBImage.PaintToCanvas(Canvas: TCanvas);
|
---|
357 | begin
|
---|
358 | end;
|
---|
359 |
|
---|
360 | function TBImage.GetDataSize: Integer;
|
---|
361 | begin
|
---|
362 | Result := 0;
|
---|
363 | end;
|
---|
364 |
|
---|
365 | constructor TBImage.Create;
|
---|
366 | begin
|
---|
367 | Size := Point(0, 0);
|
---|
368 | end;
|
---|
369 |
|
---|
370 | { TFPixmap }
|
---|
371 |
|
---|
372 | procedure TFPixmap.SetColorFormat(AValue: TColorFormat);
|
---|
373 | begin
|
---|
374 | if FColorFormat = AValue then Exit;
|
---|
375 | FBackend.Free;
|
---|
376 | FBackend := AValue.GetBackendImage;
|
---|
377 | FBackend.Size := FSize;
|
---|
378 | FColorFormat := AValue;
|
---|
379 | end;
|
---|
380 |
|
---|
381 | function TFPixmap.FillGetColor(Position: TPoint): IBColor;
|
---|
382 | begin
|
---|
383 | Result := (FillCallBack(Position, ColorFormat) as TFColor).Backend;
|
---|
384 | end;
|
---|
385 |
|
---|
386 | function TFPixmap.GetPixel(X, Y: Integer): IFColor;
|
---|
387 | begin
|
---|
388 | Result := TFColor.Create;
|
---|
389 | (Result as TFColor).ColorFormat := ColorFormat;
|
---|
390 | (Result as TFColor).FBackend := FBackend.Pixels[X, Y];
|
---|
391 | end;
|
---|
392 |
|
---|
393 | procedure TFPixmap.SetPixel(X, Y: Integer; AValue: IFColor);
|
---|
394 | begin
|
---|
395 | FBackend.Pixels[X, Y] := (AValue as TFColor).FBackend;
|
---|
396 | end;
|
---|
397 |
|
---|
398 | procedure TFPixmap.SetSize(AValue: TPoint);
|
---|
399 | begin
|
---|
400 | if (FSize.X = AValue.X) and (FSize.Y = AValue.Y) then Exit;
|
---|
401 | FSize := AValue;
|
---|
402 | FBackend.Size := AValue;
|
---|
403 | end;
|
---|
404 |
|
---|
405 | procedure TFPixmap.Fill(Color: IFColor);
|
---|
406 | begin
|
---|
407 | FBackend.Fill((Color as TFColor).Backend);
|
---|
408 | end;
|
---|
409 |
|
---|
410 | procedure TFPixmap.Fill(Func: TGetColorPos);
|
---|
411 | begin
|
---|
412 | FillCallBack := Func;
|
---|
413 | FBackend.Fill(FillGetColor);
|
---|
414 | end;
|
---|
415 |
|
---|
416 | procedure TFPixmap.PaintToCanvas(Canvas: TCanvas);
|
---|
417 | begin
|
---|
418 | FBackend.PaintToCanvas(Canvas);
|
---|
419 | end;
|
---|
420 |
|
---|
421 | function TFPixmap.GetDataSize: Integer;
|
---|
422 | begin
|
---|
423 | Result := FBackend.GetDataSize;
|
---|
424 | end;
|
---|
425 |
|
---|
426 | constructor TFPixmap.Create(AOwner: TComponent);
|
---|
427 | begin
|
---|
428 | inherited;
|
---|
429 | FBackend := TBImage.Create;
|
---|
430 | FCanvas := TFCanvas.Create;
|
---|
431 | FCanvas.Pixmap := Self;
|
---|
432 | ColorFormat := ColorFormatManager.GetFormat(0);
|
---|
433 | end;
|
---|
434 |
|
---|
435 | destructor TFPixmap.Destroy;
|
---|
436 | begin
|
---|
437 | FreeAndNil(FCanvas);
|
---|
438 | FreeAndNil(FBackend);
|
---|
439 | inherited Destroy;
|
---|
440 | end;
|
---|
441 |
|
---|
442 |
|
---|
443 | initialization
|
---|
444 |
|
---|
445 | ColorFormatManager := TColorFormatManager.Create;
|
---|
446 | with ColorFormatManager do begin
|
---|
447 | RegisterFormat(TColorFormat);
|
---|
448 | RegisterFormat(TColorFormatGray1);
|
---|
449 | RegisterFormat(TColorFormatGray2);
|
---|
450 | RegisterFormat(TColorFormatGray4);
|
---|
451 | RegisterFormat(TColorFormatGray8);
|
---|
452 | RegisterFormat(TColorFormatRGB8);
|
---|
453 | RegisterFormat(TColorFormatRGBA8);
|
---|
454 | RegisterFormat(TColorFormatRGB565);
|
---|
455 | end;
|
---|
456 |
|
---|
457 |
|
---|
458 | finalization
|
---|
459 |
|
---|
460 | FreeAndNil(ColorFormatManager);
|
---|
461 |
|
---|
462 |
|
---|
463 | end.
|
---|
464 |
|
---|