source: branches/gbitmap/Packages/FastGraphics/UFGraphics.pas

Last change on this file was 25, checked in by chronos, 8 years ago
  • Fixed: Memory leaks introduced by TFColor. TFColor now use reference counting as descendant of IFColor.
  • Added: Line drawing for TFPixmap class.
File size: 10.3 KB
Line 
1unit UFGraphics;
2
3{$mode delphi}
4
5interface
6
7uses
8 Classes, SysUtils, Graphics, UGGraphics, UPixmapSpecialized, Contnrs;
9
10type
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
173procedure Register;
174
175var
176 ColorFormatManager: TColorFormatManager;
177
178
179implementation
180
181uses
182 UColorGray1, UColorGray2,UColorGray4, UColorGray8, UColorRGB8, UColorRGBA8,
183 UColorRGB565;
184
185procedure Register;
186begin
187 RegisterComponents('FastGraphics', [TFPixmap]);
188end;
189
190{ TFPen }
191
192procedure TFPen.MoveTo(Pos: TPoint);
193begin
194 Position := Pos;
195end;
196
197procedure TFPen.LineTo(Pos: TPoint);
198begin
199 Canvas.Pixmap.Backend.Line(Position, Pos, (Color as TFColor).Backend);
200 Position := Pos;
201end;
202
203{ TFCanvas }
204
205constructor TFCanvas.Create;
206begin
207 Pen := TFPen.Create;
208 Pen.Canvas := Self;
209 Brush := TFBrush.Create;
210 Brush.Canvas := Self;
211end;
212
213destructor TFCanvas.Destroy;
214begin
215 FreeAndNil(Pen);
216 FreeAndNil(Brush);
217 inherited Destroy;
218end;
219
220{ TBColor }
221
222procedure TBColor.SetColorName(ColorName: TColorName);
223begin
224end;
225
226procedure TBColor.SetRandom;
227begin
228end;
229
230{ TColorFormatManager }
231
232function TColorFormatManager.GetFormat(Index: Integer): TColorFormat;
233begin
234 Result := TColorFormat(FFormats[Index]);
235end;
236
237constructor TColorFormatManager.Create;
238begin
239 FFormats := TObjectList.Create;
240end;
241
242destructor TColorFormatManager.Destroy;
243begin
244 FreeAndNil(FFormats);
245 inherited Destroy;
246end;
247
248procedure TColorFormatManager.RegisterFormat(Format: TColorFormatClass);
249begin
250 FFormats.Add(Format.Create);
251end;
252
253function TColorFormatManager.FormatCount: Integer;
254begin
255 Result := FFormats.Count;
256end;
257
258{ TColorFormat }
259
260procedure TColorFormat.AddChannel(Name: string; Position, BitWidth: Integer);
261begin
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;
266end;
267
268function TColorFormat.GetBackendColor: IBColor;
269begin
270 Result := BackendColorClass.Create;
271end;
272
273function TColorFormat.GetBackendImage: TBImage;
274begin
275 Result := BackendImageClass.Create;
276end;
277
278constructor TColorFormat.Create;
279begin
280 Name := 'None';
281 BitDepth := 0;
282 BackendColorClass := TBColor;
283 BackendImageClass := TBImage;
284end;
285
286function TColorFormat.GetChannelStateCount(Channel: Integer): Integer;
287begin
288 Result := 1 shl Channels[Channel].BitWidth;
289end;
290
291{ TFColor }
292
293procedure TFColor.SetColorFormat(AValue: TColorFormat);
294begin
295 if FColorFormat = AValue then Exit;
296 FBackend := AValue.GetBackendColor;
297 FColorFormat := AValue;
298end;
299
300procedure TFColor.SetColorName(ColorName: TColorName);
301begin
302 FBackend.SetColorName(ColorName);
303end;
304
305procedure TFColor.SetRandom;
306begin
307 FBackend.SetRandom;
308end;
309
310constructor TFColor.Create;
311begin
312 ColorFormat := ColorFormatManager.GetFormat(0);
313end;
314
315constructor TFColor.Create(ColorFormat: TColorFormat; ColorName: TColorName);
316begin
317 Self.ColorFormat := ColorFormat;
318 Backend.SetColorName(ColorName);
319end;
320
321destructor TFColor.Destroy;
322begin
323 FColorFormat := nil;
324 inherited Destroy;
325end;
326
327{ TBImage }
328
329function TBImage.GetPixel(X, Y: Integer): IBColor;
330begin
331 Result := TBColor.Create;
332end;
333
334procedure TBImage.SetPixel(X, Y: Integer; AValue: IBColor);
335begin
336end;
337
338procedure TBImage.SetSize(AValue: TPoint);
339begin
340 if (FSize.X = AValue.X) and (FSize.Y = AValue.Y) then Exit;
341 FSize := AValue;
342end;
343
344procedure TBImage.Fill(Color: IBColor);
345begin
346end;
347
348procedure TBImage.Fill(Func: TGetColorPos);
349begin
350end;
351
352procedure TBImage.Line(P1, P2: TPoint; Color: IBColor);
353begin
354end;
355
356procedure TBImage.PaintToCanvas(Canvas: TCanvas);
357begin
358end;
359
360function TBImage.GetDataSize: Integer;
361begin
362 Result := 0;
363end;
364
365constructor TBImage.Create;
366begin
367 Size := Point(0, 0);
368end;
369
370{ TFPixmap }
371
372procedure TFPixmap.SetColorFormat(AValue: TColorFormat);
373begin
374 if FColorFormat = AValue then Exit;
375 FBackend.Free;
376 FBackend := AValue.GetBackendImage;
377 FBackend.Size := FSize;
378 FColorFormat := AValue;
379end;
380
381function TFPixmap.FillGetColor(Position: TPoint): IBColor;
382begin
383 Result := (FillCallBack(Position, ColorFormat) as TFColor).Backend;
384end;
385
386function TFPixmap.GetPixel(X, Y: Integer): IFColor;
387begin
388 Result := TFColor.Create;
389 (Result as TFColor).ColorFormat := ColorFormat;
390 (Result as TFColor).FBackend := FBackend.Pixels[X, Y];
391end;
392
393procedure TFPixmap.SetPixel(X, Y: Integer; AValue: IFColor);
394begin
395 FBackend.Pixels[X, Y] := (AValue as TFColor).FBackend;
396end;
397
398procedure TFPixmap.SetSize(AValue: TPoint);
399begin
400 if (FSize.X = AValue.X) and (FSize.Y = AValue.Y) then Exit;
401 FSize := AValue;
402 FBackend.Size := AValue;
403end;
404
405procedure TFPixmap.Fill(Color: IFColor);
406begin
407 FBackend.Fill((Color as TFColor).Backend);
408end;
409
410procedure TFPixmap.Fill(Func: TGetColorPos);
411begin
412 FillCallBack := Func;
413 FBackend.Fill(FillGetColor);
414end;
415
416procedure TFPixmap.PaintToCanvas(Canvas: TCanvas);
417begin
418 FBackend.PaintToCanvas(Canvas);
419end;
420
421function TFPixmap.GetDataSize: Integer;
422begin
423 Result := FBackend.GetDataSize;
424end;
425
426constructor TFPixmap.Create(AOwner: TComponent);
427begin
428 inherited;
429 FBackend := TBImage.Create;
430 FCanvas := TFCanvas.Create;
431 FCanvas.Pixmap := Self;
432 ColorFormat := ColorFormatManager.GetFormat(0);
433end;
434
435destructor TFPixmap.Destroy;
436begin
437 FreeAndNil(FCanvas);
438 FreeAndNil(FBackend);
439 inherited Destroy;
440end;
441
442
443initialization
444
445ColorFormatManager := TColorFormatManager.Create;
446with 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);
455end;
456
457
458finalization
459
460FreeAndNil(ColorFormatManager);
461
462
463end.
464
Note: See TracBrowser for help on using the repository browser.