Changeset 3 for trunk/UGraphic.pas


Ignore:
Timestamp:
Sep 15, 2014, 11:13:21 PM (10 years ago)
Author:
chronos
Message:
  • Modified: Specific TGColor implementation moved to ColorFormats directory.
  • Added: Function to clear image with background color.
  • Modified: Drawing project image to off screen bitmap to speed up drawing.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/UGraphic.pas

    r2 r3  
    3030  TGColorFormatClass = class of TGColorFormat;
    3131
    32   { TGColorFormatRGBA8 }
    33 
    34   TGColorFormatRGBA8 = class(TGColorFormat)
    35     function GetPixelSize: Integer; override;
    36     function GetBitDepth: Integer; override;
    37     function GetName: string; override;
    38     function GetChannelBytePos(Channel: TGColorChannel): Integer; override;
    39     function GetChannelBitWidth(Channel: TGColorChannel): Integer; override;
    40     function ColorToTColor(Color: TGColor): TColor; override;
    41     function GetColorClass: TGColorClass; override;
    42   end;
    43 
    4432  { TGColor }
    4533
    4634  TGColor = class
    4735  private
     36    FColorFormat: TGColorFormatClass;
    4837    FData: PByte;
    49     FColorFormat: TGColorFormatClass;
    5038    function GetChannel(Channel: TGColorChannel): TGColor;
    5139    procedure SetColorFormat(AValue: TGColorFormatClass);
     
    5644    procedure Assign(Source: TGColor); virtual;
    5745    property Channels[Channel: TGColorChannel]: TGColor read GetChannel;
     46    property Data: PByte read FData;
    5847  published
    5948    property Format: TGColorFormatClass read FColorFormat write SetColorFormat;
     
    6453  TGBitmap = class
    6554  private
     55    FBackgroundColor: TGColor;
    6656    FColorFormat: TGColorFormatClass;
    6757    FDPI: Integer;
     
    7060    function GetPixel(X, Y: Integer): TGColor;
    7161    function GetSize: TPoint;
     62    procedure SetBackgroundColor(AValue: TGColor);
    7263    procedure SetColorFormat(AValue: TGColorFormatClass);
    7364    procedure SetPixel(X, Y: Integer; AValue: TGColor);
     
    7768    function GetDataSize: Integer;
    7869    procedure PaintToCanvas(Canvas: TCanvas);
     70    procedure Clear;
    7971    constructor Create; virtual;
    8072    destructor Destroy; override;
     73    property BackgroundColor: TGColor read FBackgroundColor write SetBackgroundColor;
    8174    property DPI: Integer read FDPI write FDPI;
    8275    property ColorFormat: TGColorFormatClass read FColorFormat write SetColorFormat;
     
    134127end;
    135128
    136 { TGColorFormatRGBA8 }
    137 
    138 function TGColorFormatRGBA8.GetPixelSize: Integer;
    139 begin
    140   Result := 4;
    141 end;
    142 
    143 function TGColorFormatRGBA8.GetBitDepth: Integer;
    144 begin
    145   Result := 32;
    146 end;
    147 
    148 function TGColorFormatRGBA8.GetName: string;
    149 begin
    150   Result := 'RGBA8';
    151 end;
    152 
    153 function TGColorFormatRGBA8.GetChannelBytePos(Channel: TGColorChannel): Integer;
    154 begin
    155   case Channel of
    156     ccRed: Result := 0;
    157     ccGreen: Result := 1;
    158     ccBlue: Result := 2;
    159     ccOpacity: Result := 3;
    160     else raise Exception.Create('Unsupported color channel');
    161   end;
    162 end;
    163 
    164 function TGColorFormatRGBA8.GetChannelBitWidth(Channel: TGColorChannel
    165   ): Integer;
    166 begin
    167   if (Channel = ccBlue) or (Channel = ccRed) or (Channel = ccGreen) then
    168     Result := 8 else Result := 0;
    169 end;
    170 
    171 function TGColorFormatRGBA8.ColorToTColor(Color: TGColor): TColor;
    172 begin
    173   Result := PByte(Color.FData + GetChannelBytePos(ccRed))^ or
    174     (PByte(Color.FData + GetChannelBytePos(ccGreen))^ shl 8) or
    175     (PByte(Color.FData + GetChannelBytePos(ccBlue))^ shl 16);
    176 end;
    177 
    178 function TGColorFormatRGBA8.GetColorClass: TGColorClass;
    179 begin
    180   Result := TGColor;
    181 end;
    182 
    183129{ TGColorFormat }
    184130
     
    286232end;
    287233
     234procedure TGBitmap.SetBackgroundColor(AValue: TGColor);
     235begin
     236  if FBackgroundColor = AValue then Exit;
     237  FBackgroundColor := AValue;
     238end;
     239
    288240procedure TGBitmap.SetColorFormat(AValue: TGColorFormatClass);
    289241begin
     
    291243  FColorFormat := AValue;
    292244  ReAllocMem(FData, GetDataSize);
     245  FBackgroundColor.Format := ColorFormat;
    293246end;
    294247
     
    328281  Pixel: TGColor;
    329282begin
     283  try
     284  Canvas.Lock;
    330285  for Y := 0 to Size.Y - 1 do
    331286    for X := 0 to Size.X - 1 do begin
     
    334289      Pixel.Free;
    335290    end;
     291
     292  finally
     293    Canvas.Unlock;
     294  end;
     295end;
     296
     297procedure TGBitmap.Clear;
     298var
     299  X, Y: Integer;
     300begin
     301  for Y := 0 to Size.Y - 1 do
     302    for X := 0 to Size.X - 1 do begin
     303      Pixels[X, Y] := BackgroundColor;
     304    end;
    336305end;
    337306
     
    339308begin
    340309  FData := GetMem(0);
    341   ColorFormat := TGColorFormatRGBA8;
     310  FBackgroundColor := TGColor.Create;
     311  ColorFormat := TGColorFormat;
     312  FBackgroundColor.Format := ColorFormat;
    342313end;
    343314
Note: See TracChangeset for help on using the changeset viewer.