source: trunk/Packages/FastGraphics/UFGraphics.pas

Last change on this file was 40, checked in by chronos, 6 years ago
  • Added: 16-bit channel RGB color format.
File size: 14.5 KB
Line 
1unit UFGraphics;
2
3{$mode delphi}
4
5interface
6
7uses
8 Classes, SysUtils, Graphics, UGGraphics, UPixmapSpecialized, Fgl;
9
10type
11 TColorName = (cnBlack, cnWhite, cnBlue, cnRed, cnGreen, cnGray, cnSilver);
12
13 IBColor = interface
14 procedure SetColorName(ColorName: TColorName);
15 procedure SetColor(Color: TColor);
16 procedure SetRandom;
17 end;
18
19 { TBColor }
20
21 TBColor = class(TInterfacedObject, IBColor)
22 procedure SetColorName(ColorName: TColorName); virtual;
23 procedure SetColor(Color: TColor); virtual;
24 procedure SetRandom; virtual;
25 end;
26
27 IBColorClass = class of TBColor;
28
29 { TBPixmap }
30
31 TBPixmap = class
32 public
33 type
34 TGetColorPos = function (Position: TPoint): IBColor of object;
35 private
36 FSize: TPoint;
37 protected
38 function GetPixel(X, Y: Integer): IBColor; virtual;
39 procedure SetPixel(X, Y: Integer; AValue: IBColor); virtual;
40 procedure SetSize(AValue: TPoint); virtual;
41 public
42 procedure Mirror; virtual;
43 procedure Flip; virtual;
44 procedure Negative; virtual;
45 procedure Fill(Color: IBColor); virtual; overload;
46 procedure Fill(Func: TGetColorPos); virtual; overload;
47 procedure Line(P1, P2: TPoint; Color: IBColor); virtual;
48 procedure PaintToCanvas(Canvas: TCanvas); virtual; overload;
49 procedure PaintToCanvas(Canvas: TCanvas; Rect: TRect); virtual; overload;
50 procedure PaintToBitmap(Bitmap: TBitmap; Rect: TRect); virtual;
51 procedure LoadFromCanvas(Canvas: TCanvas); virtual;
52 procedure LoadFromBitmap(Bitmap: TBitmap); virtual;
53 function GetDataSize: Integer; virtual;
54 constructor Create; virtual;
55 property Size: TPoint read FSize write SetSize;
56 property Pixels[X, Y: Integer]: IBColor read GetPixel write SetPixel;
57 end;
58
59 TBPixmapClass = class of TBPixmap;
60
61 { TColorFormatChannel }
62
63 TColorFormatChannel = record
64 Name: string;
65 Position: Integer;
66 BitWidth: Integer;
67 end;
68
69 { TColorFormat }
70
71 TColorFormat = class
72 Name: string;
73 BitDepth: Integer;
74 Channels: array of TColorFormatChannel;
75 BackendColorClass: IBColorClass;
76 BackendPixmapClass: TBPixmapClass;
77 procedure AddChannel(Name: string; Position, BitWidth: Integer);
78 function GetBackendColor: IBColor;
79 function GetBackendImage: TBPixmap;
80 constructor Create; virtual;
81 function GetChannelStateCount(Channel: Integer): Integer;
82 end;
83
84 TColorFormats = class(TFPGObjectList<TColorFormat>)
85 end;
86
87 TColorFormatClass = class of TColorFormat;
88
89 { TColorFormatManager }
90
91 TColorFormatManager = class
92 private
93 FFormats: TColorFormats;
94 function GetFormat(Index: Integer): TColorFormat;
95 public
96 constructor Create; virtual;
97 destructor Destroy; override;
98 function FindByName(Name: string): Integer;
99 procedure RegisterFormat(Format: TColorFormatClass);
100 function FormatCount: Integer;
101 property Formats: TColorFormats read FFormats;
102 end;
103
104 IFColor = interface
105 end;
106
107 { TFColor }
108
109 TFColor = class(TInterfacedObject, IFColor)
110 private
111 FBackend: IBColor;
112 FColorFormat: TColorFormat;
113 procedure SetColorFormat(AValue: TColorFormat);
114 public
115 property Backend: IBColor read FBackend;
116 property ColorFormat: TColorFormat read FColorFormat write SetColorFormat;
117 procedure SetColorName(ColorName: TColorName);
118 procedure SetRandom;
119 procedure SetColor(Color: TColor);
120 constructor Create; overload;
121 constructor Create(ColorFormat: TColorFormat; ColorName: TColorName); overload;
122 constructor Create(ColorFormat: TColorFormat; Color: TColor); overload;
123 destructor Destroy; override;
124 end;
125
126 TFCanvas = class;
127 TFPixmap = class;
128
129 { TFBrush }
130
131 TFBrush = class
132 Color: IFColor;
133 Canvas: TFCanvas;
134 end;
135
136 { TFPen }
137
138 TFPen = class
139 Position: TPoint;
140 Color: IFColor;
141 Canvas: TFCanvas;
142 procedure MoveTo(Pos: TPoint);
143 procedure LineTo(Pos: TPoint);
144 end;
145
146 { TFCanvas }
147
148 TFCanvas = class
149 Pixmap: TFPixmap;
150 Pen: TFPen;
151 Brush: TFBrush;
152 constructor Create;
153 destructor Destroy; override;
154 end;
155
156
157 { TFPixmap }
158
159 TFPixmap = class(TComponent)
160 public
161 type
162 TGetColorPos = function (Position: TPoint; ColorFormat: TColorFormat): IFColor of object;
163 private
164 FBackend: TBPixmap;
165 FCanvas: TFCanvas;
166 FColorFormat: TColorFormat;
167 FSize: TPoint;
168 FillCallBack: TGetColorPos;
169 function FillGetColor(Position: TPoint): IBColor;
170 function GetPixel(X, Y: Integer): IFColor;
171 procedure SetColorFormat(AValue: TColorFormat);
172 procedure SetPixel(X, Y: Integer; AValue: IFColor);
173 procedure SetSize(AValue: TPoint);
174 function ColorRandom(Position: TPoint; ColorFormat: TColorFormat): IFColor;
175 public
176 procedure Clear;
177 procedure Flip;
178 procedure Mirror;
179 procedure Gradient;
180 procedure Negative;
181 procedure Random;
182 procedure Fill(Color: IFColor); overload;
183 procedure Fill(Func: TGetColorPos); overload;
184 procedure PaintToCanvas(Canvas: TCanvas); overload;
185 procedure PaintToCanvas(Canvas: TCanvas; Rect: TRect); overload;
186 procedure PaintToBitmap(Bitmap: TBitmap; Rect: TRect); overload;
187 procedure LoadFromCanvas(Canvas: TCanvas);
188 procedure LoadFromBitmap(Bitmap: TBitmap);
189 function GetDataSize: Integer;
190 constructor Create(AOwner: TComponent); override;
191 destructor Destroy; override;
192 property Canvas: TFCanvas read FCanvas;
193 property Backend: TBPixmap read FBackend;
194 property ColorFormat: TColorFormat read FColorFormat write SetColorFormat;
195 property Size: TPoint read FSize write SetSize;
196 property Pixels[X, Y: Integer]: IFColor read GetPixel write SetPixel;
197 end;
198
199procedure Register;
200
201var
202 ColorFormatManager: TColorFormatManager;
203
204
205implementation
206
207uses
208 UColorGray1, UColorGray2,UColorGray4, UColorGray8, UColorRGB8, UColorRGBA8,
209 UColorRGB565, UColorRGB16;
210
211procedure Register;
212begin
213 RegisterComponents('FastGraphics', [TFPixmap]);
214end;
215
216{ TFPen }
217
218procedure TFPen.MoveTo(Pos: TPoint);
219begin
220 Position := Pos;
221end;
222
223procedure TFPen.LineTo(Pos: TPoint);
224begin
225 Canvas.Pixmap.Backend.Line(Position, Pos, (Color as TFColor).Backend);
226 Position := Pos;
227end;
228
229{ TFCanvas }
230
231constructor TFCanvas.Create;
232begin
233 Pen := TFPen.Create;
234 Pen.Canvas := Self;
235 Brush := TFBrush.Create;
236 Brush.Canvas := Self;
237end;
238
239destructor TFCanvas.Destroy;
240begin
241 FreeAndNil(Pen);
242 FreeAndNil(Brush);
243 inherited Destroy;
244end;
245
246{ TBColor }
247
248procedure TBColor.SetColorName(ColorName: TColorName);
249begin
250end;
251
252procedure TBColor.SetColor(Color: TColor);
253begin
254end;
255
256procedure TBColor.SetRandom;
257begin
258end;
259
260{ TColorFormatManager }
261
262function TColorFormatManager.GetFormat(Index: Integer): TColorFormat;
263begin
264 Result := TColorFormat(FFormats[Index]);
265end;
266
267constructor TColorFormatManager.Create;
268begin
269 FFormats := TColorFormats.Create;
270end;
271
272destructor TColorFormatManager.Destroy;
273begin
274 FreeAndNil(FFormats);
275 inherited Destroy;
276end;
277
278function TColorFormatManager.FindByName(Name: string): Integer;
279var
280 I: Integer;
281begin
282 Result := -1;
283 for I := 0 to FFormats.Count - 1 do begin
284 if FFormats[I].Name = Name then begin
285 Result := I;
286 Break;
287 end;
288 end;
289end;
290
291procedure TColorFormatManager.RegisterFormat(Format: TColorFormatClass);
292begin
293 FFormats.Add(Format.Create);
294end;
295
296function TColorFormatManager.FormatCount: Integer;
297begin
298 Result := FFormats.Count;
299end;
300
301{ TColorFormat }
302
303procedure TColorFormat.AddChannel(Name: string; Position, BitWidth: Integer);
304begin
305 SetLength(Channels, Length(Channels) + 1);
306 Channels[Length(Channels) - 1].Name := Name;
307 Channels[Length(Channels) - 1].Position := Position;
308 Channels[Length(Channels) - 1].BitWidth := BitWidth;
309end;
310
311function TColorFormat.GetBackendColor: IBColor;
312begin
313 Result := BackendColorClass.Create;
314end;
315
316function TColorFormat.GetBackendImage: TBPixmap;
317begin
318 Result := BackendPixmapClass.Create;
319end;
320
321constructor TColorFormat.Create;
322begin
323 Name := 'None';
324 BitDepth := 0;
325 BackendColorClass := TBColor;
326 BackendPixmapClass := TBPixmap;
327end;
328
329function TColorFormat.GetChannelStateCount(Channel: Integer): Integer;
330begin
331 Result := 1 shl Channels[Channel].BitWidth;
332end;
333
334{ TFColor }
335
336procedure TFColor.SetColorFormat(AValue: TColorFormat);
337begin
338 if FColorFormat = AValue then Exit;
339 FBackend := AValue.GetBackendColor;
340 FColorFormat := AValue;
341end;
342
343procedure TFColor.SetColorName(ColorName: TColorName);
344begin
345 FBackend.SetColorName(ColorName);
346end;
347
348procedure TFColor.SetRandom;
349begin
350 FBackend.SetRandom;
351end;
352
353procedure TFColor.SetColor(Color: TColor);
354begin
355 Backend.SetColor(Color);
356end;
357
358constructor TFColor.Create;
359begin
360 ColorFormat := ColorFormatManager.GetFormat(0);
361end;
362
363constructor TFColor.Create(ColorFormat: TColorFormat; ColorName: TColorName);
364begin
365 Self.ColorFormat := ColorFormat;
366 Backend.SetColorName(ColorName);
367end;
368
369constructor TFColor.Create(ColorFormat: TColorFormat; Color: TColor);
370begin
371 Self.ColorFormat := ColorFormat;
372 Backend.SetColor(Color);
373end;
374
375destructor TFColor.Destroy;
376begin
377 FColorFormat := nil;
378 inherited Destroy;
379end;
380
381{ TBPixmap }
382
383function TBPixmap.GetPixel(X, Y: Integer): IBColor;
384begin
385 Result := TBColor.Create;
386end;
387
388procedure TBPixmap.SetPixel(X, Y: Integer; AValue: IBColor);
389begin
390end;
391
392procedure TBPixmap.SetSize(AValue: TPoint);
393begin
394 if (FSize.X = AValue.X) and (FSize.Y = AValue.Y) then Exit;
395 FSize := AValue;
396end;
397
398procedure TBPixmap.Mirror;
399begin
400end;
401
402procedure TBPixmap.Flip;
403begin
404end;
405
406procedure TBPixmap.Negative;
407begin
408end;
409
410procedure TBPixmap.Fill(Color: IBColor);
411begin
412end;
413
414procedure TBPixmap.Fill(Func: TGetColorPos);
415begin
416end;
417
418procedure TBPixmap.Line(P1, P2: TPoint; Color: IBColor);
419begin
420end;
421
422procedure TBPixmap.PaintToCanvas(Canvas: TCanvas);
423begin
424end;
425
426procedure TBPixmap.PaintToCanvas(Canvas: TCanvas; Rect: TRect);
427begin
428end;
429
430procedure TBPixmap.PaintToBitmap(Bitmap: TBitmap; Rect: TRect);
431begin
432end;
433
434procedure TBPixmap.LoadFromCanvas(Canvas: TCanvas);
435begin
436end;
437
438procedure TBPixmap.LoadFromBitmap(Bitmap: TBitmap);
439begin
440end;
441
442function TBPixmap.GetDataSize: Integer;
443begin
444 Result := 0;
445end;
446
447constructor TBPixmap.Create;
448begin
449 Size := Point(0, 0);
450end;
451
452{ TFPixmap }
453
454procedure TFPixmap.SetColorFormat(AValue: TColorFormat);
455begin
456 if FColorFormat = AValue then Exit;
457 FBackend.Free;
458 FBackend := AValue.GetBackendImage;
459 FBackend.Size := FSize;
460 FColorFormat := AValue;
461end;
462
463function TFPixmap.FillGetColor(Position: TPoint): IBColor;
464begin
465 Result := (FillCallBack(Position, ColorFormat) as TFColor).Backend;
466end;
467
468function TFPixmap.GetPixel(X, Y: Integer): IFColor;
469begin
470 Result := TFColor.Create;
471 (Result as TFColor).ColorFormat := ColorFormat;
472 (Result as TFColor).FBackend := FBackend.Pixels[X, Y];
473end;
474
475procedure TFPixmap.SetPixel(X, Y: Integer; AValue: IFColor);
476begin
477 FBackend.Pixels[X, Y] := (AValue as TFColor).FBackend;
478end;
479
480procedure TFPixmap.SetSize(AValue: TPoint);
481begin
482 if (FSize.X = AValue.X) and (FSize.Y = AValue.Y) then Exit;
483 FSize := AValue;
484 FBackend.Size := AValue;
485end;
486
487function TFPixmap.ColorRandom(Position: TPoint; ColorFormat: TColorFormat
488 ): IFColor;
489begin
490 Result := TFColor.Create;
491 (Result as TFColor).ColorFormat := ColorFormat;
492 (Result as TFColor).SetRandom;
493end;
494
495procedure TFPixmap.Clear;
496begin
497
498end;
499
500procedure TFPixmap.Flip;
501begin
502 FBackend.Flip;
503end;
504
505procedure TFPixmap.Mirror;
506begin
507 FBackend.Mirror;
508end;
509
510procedure TFPixmap.Gradient;
511var
512 X, Y: Integer;
513 Color: IFColor;
514begin
515 Color := TFColor.Create;
516 (Color as TFColor).ColorFormat := ColorFormat;
517 for Y := 0 to Size.Y div 4 - 1 do
518 for X := 0 to Size.X - 1 do begin
519 (Color as TFColor).SetColor(RGBToColor(
520 Trunc(X / Size.X * 256),
521 0,
522 0
523 ));
524 Pixels[X, Y] := Color;
525 end;
526 for Y := Size.Y div 4 to 2 * Size.Y div 4 - 1 do
527 for X := 0 to Size.X - 1 do begin
528 (Color as TFColor).SetColor(RGBToColor(
529 0,
530 Trunc(X / Size.X * 256),
531 0
532 ));
533 Pixels[X, Y] := Color;
534 end;
535 for Y := 2 * Size.Y div 4 to 3 * Size.Y div 4 - 1 do
536 for X := 0 to Size.X - 1 do begin
537 (Color as TFColor).SetColor(RGBToColor(
538 0,
539 0,
540 Trunc(X / Size.X * 256)
541 ));
542 Pixels[X, Y] := Color;
543 end;
544 for Y := 3 * Size.Y div 4 to Size.Y - 1 do
545 for X := 0 to Size.X - 1 do begin
546 (Color as TFColor).SetColor(RGBToColor(
547 Trunc(X / Size.X * 256),
548 Trunc(X / Size.X * 256),
549 Trunc(X / Size.X * 256)
550 ));
551 Pixels[X, Y] := Color;
552 end;
553end;
554
555procedure TFPixmap.Negative;
556begin
557 FBackend.Negative;
558end;
559
560procedure TFPixmap.Random;
561begin
562 Fill(ColorRandom);
563end;
564
565procedure TFPixmap.Fill(Color: IFColor);
566begin
567 FBackend.Fill((Color as TFColor).Backend);
568end;
569
570procedure TFPixmap.Fill(Func: TGetColorPos);
571begin
572 FillCallBack := Func;
573 FBackend.Fill(FillGetColor);
574end;
575
576procedure TFPixmap.PaintToCanvas(Canvas: TCanvas);
577begin
578 FBackend.PaintToCanvas(Canvas);
579end;
580
581procedure TFPixmap.PaintToCanvas(Canvas: TCanvas; Rect: TRect);
582begin
583 FBackend.PaintToCanvas(Canvas, Rect);
584end;
585
586procedure TFPixmap.PaintToBitmap(Bitmap: TBitmap; Rect: TRect);
587begin
588 FBackend.PaintToBitmap(Bitmap, Rect);
589end;
590
591procedure TFPixmap.LoadFromCanvas(Canvas: TCanvas);
592begin
593 Backend.LoadFromCanvas(Canvas);
594end;
595
596procedure TFPixmap.LoadFromBitmap(Bitmap: TBitmap);
597begin
598 Backend.LoadFromBitmap(Bitmap);
599end;
600
601function TFPixmap.GetDataSize: Integer;
602begin
603 Result := FBackend.GetDataSize;
604end;
605
606constructor TFPixmap.Create(AOwner: TComponent);
607begin
608 inherited;
609 FBackend := TBPixmap.Create;
610 FCanvas := TFCanvas.Create;
611 FCanvas.Pixmap := Self;
612 ColorFormat := ColorFormatManager.GetFormat(0);
613end;
614
615destructor TFPixmap.Destroy;
616begin
617 FreeAndNil(FCanvas);
618 FreeAndNil(FBackend);
619 inherited Destroy;
620end;
621
622
623initialization
624
625ColorFormatManager := TColorFormatManager.Create;
626with ColorFormatManager do begin
627 RegisterFormat(TColorFormat);
628 RegisterFormat(TColorFormatGray1);
629 RegisterFormat(TColorFormatGray2);
630 RegisterFormat(TColorFormatGray4);
631 RegisterFormat(TColorFormatGray8);
632 RegisterFormat(TColorFormatRGB8);
633 RegisterFormat(TColorFormatRGBA8);
634 RegisterFormat(TColorFormatRGB565);
635 RegisterFormat(TColorFormatRGB16);
636end;
637
638
639finalization
640
641FreeAndNil(ColorFormatManager);
642
643
644end.
645
Note: See TracBrowser for help on using the repository browser.