Changeset 8


Ignore:
Timestamp:
Sep 19, 2014, 9:47:22 AM (10 years ago)
Author:
chronos
Message:
  • Modified: To support generic custom user defined runtime color formats TGColor have to reference directly TGColorFormat class. Also it is faster as we don't need to create instance of TGColorFormat from TGColorFormatClass.

But used color formats can't be freed or their parameters modified.

Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/Forms/UFormNew.pas

    r2 r8  
    4747  ColorFormat: TGColorFormat;
    4848begin
    49   ColorFormat := ColorManager.Formats[ComboBoxColorFormat.ItemIndex].Create;
    50   LabelMemRequire.Caption := IntToStr(SpinEditWidth.Value * SpinEditHeight.Value * ColorFormat.GetBitDepth div 8) + ' bytes';
    51   ColorFormat.Free;
     49  ColorFormat := ColorManager.Formats[ComboBoxColorFormat.ItemIndex];
     50  LabelMemRequire.Caption := IntToStr(SpinEditWidth.Value * SpinEditHeight.Value *
     51    ColorFormat.GetBitDepth div 8) + ' bytes';
    5252end;
    5353
     
    5959  ComboBoxColorFormat.Clear;
    6060  for I := 0 to ColorManager.FormatCount - 1 do begin
    61     ColorFormat := ColorManager.Formats[I].Create;
     61    ColorFormat := ColorManager.Formats[I];
    6262    ComboBoxColorFormat.AddItem(ColorFormat.GetName, nil);
    63     ColorFormat.Free;
    6463  end;
    6564  if ComboBoxColorFormat.Items.Count > 0 then
  • trunk/LibrePaint.lpi

    r7 r8  
    9999        <HasResources Value="True"/>
    100100        <ResourceBaseClass Value="Form"/>
     101        <UnitName Value="UFormNew"/>
    101102      </Unit4>
    102103      <Unit5>
  • trunk/UCore.pas

    r7 r8  
    7474begin
    7575  if FormNew.ShowModal = mrOk then begin
    76     Project.Bitmap.ColorFormat := TGColorFormat;
     76    Project.Bitmap.ColorFormat := TGColorFormat.Create;
    7777
    7878    Project.FileName := 'New image';
  • trunk/UGraphic.pas

    r7 r8  
    3636  TGColor = class
    3737  private
    38     FColorFormat: TGColorFormatClass;
     38    FColorFormat: TGColorFormat;
    3939    FData: PByte;
    4040    function GetChannel(Channel: TGColorChannel): TGColor;
    41     procedure SetColorFormat(AValue: TGColorFormatClass);
     41    procedure SetColorFormat(AValue: TGColorFormat);
    4242    procedure LoadData(BitmapData: Pointer); virtual;
    4343    procedure SaveData(BitmapData: Pointer); virtual;
     
    5050    property Data: PByte read FData;
    5151  published
    52     property Format: TGColorFormatClass read FColorFormat write SetColorFormat;
     52    property Format: TGColorFormat read FColorFormat write SetColorFormat;
    5353  end;
    5454
     
    5959    FBackgroundColor: TGColor;
    6060    FCanvas: TGCanvas;
    61     FColorFormat: TGColorFormatClass;
     61    FColorFormat: TGColorFormat;
    6262    FDPI: Integer;
    6363    FSize: TPoint;
     
    6666    function GetSize: TPoint;
    6767    procedure SetBackgroundColor(AValue: TGColor);
    68     procedure SetColorFormat(AValue: TGColorFormatClass);
     68    procedure SetColorFormat(AValue: TGColorFormat);
    6969    procedure SetPixel(X, Y: Integer; AValue: TGColor);
    7070    procedure SetSize(AValue: TPoint);
     
    8181    property BackgroundColor: TGColor read FBackgroundColor write SetBackgroundColor;
    8282    property DPI: Integer read FDPI write FDPI;
    83     property ColorFormat: TGColorFormatClass read FColorFormat write SetColorFormat;
     83    property ColorFormat: TGColorFormat read FColorFormat write SetColorFormat;
    8484    property Size: TPoint read FSize write SetSize;
    8585    property Pixels[X, Y: Integer]: TGColor read GetPixel write SetPixel;
     
    9393  private
    9494    FFormats: TObjectList; // TList<TGColorFormatClass>
    95     function GetFormat(Index: Integer): TGColorFormatClass;
     95    function GetFormat(Index: Integer): TGColorFormat;
    9696  public
    9797    constructor Create; virtual;
     
    9999    procedure RegisterFormat(Format: TGColorFormatClass);
    100100    function FormatCount: Integer;
    101     property Formats[Index: Integer]: TGColorFormatClass read GetFormat;
     101    property Formats[Index: Integer]: TGColorFormat read GetFormat;
    102102  end;
    103103
     
    200200{ TGColorManager }
    201201
    202 function TGColorManager.GetFormat(Index: Integer): TGColorFormatClass;
    203 begin
    204   Result := TGColorFormatClass(FFormats[Index])
     202function TGColorManager.GetFormat(Index: Integer): TGColorFormat;
     203begin
     204  Result := TGColorFormat(FFormats[Index])
    205205end;
    206206
     
    208208begin
    209209  FFormats := TObjectList.Create;
    210   FFormats.OwnsObjects := False;
    211210end;
    212211
     
    219218procedure TGColorManager.RegisterFormat(Format: TGColorFormatClass);
    220219begin
    221   FFormats.Add(TObject(Format));
     220  FFormats.Add(Format.Create);
    222221end;
    223222
     
    281280end;
    282281
    283 procedure TGColor.SetColorFormat(AValue: TGColorFormatClass);
    284 var
    285   F: TGColorFormat;
     282procedure TGColor.SetColorFormat(AValue: TGColorFormat);
    286283begin
    287284  if FColorFormat = AValue then Exit;
    288285  FColorFormat := AValue;
    289   F := FColorFormat.Create;
    290   ReAllocMem(FData, F.GetPixelSize);
    291   F.Free;
     286  ReAllocMem(FData, FColorFormat.GetPixelSize);
    292287end;
    293288
    294289procedure TGColor.LoadData(BitmapData: Pointer);
    295 var
    296   F: TGColorFormat;
    297 begin
    298   F := FColorFormat.Create;
    299   Move(BitmapData^, FData^, F.GetPixelSize);
    300   F.Free;
     290begin
     291  Move(BitmapData^, FData^, FColorFormat.GetPixelSize);
    301292end;
    302293
    303294procedure TGColor.SaveData(BitmapData: Pointer);
    304 var
    305   F: TGColorFormat;
    306 begin
    307   F := FColorFormat.Create;
    308   Move(FData^, BitmapData^, F.GetPixelSize);
    309   F.Free;
     295begin
     296  Move(FData^, BitmapData^, FColorFormat.GetPixelSize);
    310297end;
    311298
    312299function TGColor.ToTColor: TColor;
    313 var
    314   F: TGColorFormat;
    315 begin
    316   F := FColorFormat.Create;
    317   Result := F.ColorToTColor(Self);
    318   F.Free;
     300begin
     301  Result := FColorFormat.ColorToTColor(Self);
    319302end;
    320303
    321304procedure TGColor.FromTColor(Color: TColor);
    322 var
    323   F: TGColorFormat;
    324 begin
    325   F := FColorFormat.Create;
    326   F.ColorFromTColor(Self, Color);
    327   F.Free;
     305begin
     306  FColorFormat.ColorFromTColor(Self, Color);
    328307end;
    329308
     
    334313constructor TGColor.Create;
    335314begin
    336   Format := TGColorFormat;
     315  Format := TGColorFormat.Create;
    337316end;
    338317
     
    357336end;
    358337
    359 procedure TGBitmap.SetColorFormat(AValue: TGColorFormatClass);
     338procedure TGBitmap.SetColorFormat(AValue: TGColorFormat);
    360339begin
    361340  if FColorFormat = AValue then Exit;
     
    378357
    379358function TGBitmap.GetPixelDataPos(X, Y: Integer): Pointer;
    380 var
    381   F: TGColorFormat;
    382 begin
    383   F := FColorFormat.Create;
    384   Result := FData + X * F.GetPixelSize + Y * F.GetPixelSize * FSize.X;
    385   F.Free;
     359begin
     360  Result := FData + X * FColorFormat.GetPixelSize + Y * FColorFormat.GetPixelSize * FSize.X;
    386361end;
    387362
    388363function TGBitmap.GetDataSize: Integer;
    389 var
    390   F: TGColorFormat;
    391 begin
    392   F := FColorFormat.Create;
    393   Result := Size.X * Size.Y * F.GetPixelSize;
    394   F.Free;
     364begin
     365  Result := Size.X * Size.Y * FColorFormat.GetPixelSize;
    395366end;
    396367
     
    471442  FData := GetMem(0);
    472443  FBackgroundColor := TGColor.Create;
    473   ColorFormat := TGColorFormat;
     444  ColorFormat := TGColorFormat.Create;
    474445  FBackgroundColor.Format := ColorFormat;
    475446  FBackgroundColor.FromTColor(clBlack);
Note: See TracChangeset for help on using the changeset viewer.