Changeset 14


Ignore:
Timestamp:
Sep 22, 2014, 5:40:17 PM (10 years ago)
Author:
chronos
Message:
  • Modified: Color format properties are now initialized as variables instead by executing function.
Location:
trunk
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • trunk/ColorFormats/UColorGray1.pas

    r10 r14  
    1212
    1313  TGColorFormatGray1 = class(TGColorFormat)
    14     function GetPixelSize: Integer; override;
    15     function GetBitDepth: Integer; override;
    16     function GetName: string; override;
    17     function GetChannelBitPos(Channel: TGColorChannel): Integer; override;
    18     function GetChannelBitWidth(Channel: TGColorChannel): Integer; override;
     14    constructor Create; override;
    1915  end;
    2016
     
    2319{ TGColorFormatGray1 }
    2420
    25 function TGColorFormatGray1.GetPixelSize: Integer;
     21constructor TGColorFormatGray1.Create;
    2622begin
    27   Result := 1;
    28 end;
    29 
    30 function TGColorFormatGray1.GetBitDepth: Integer;
    31 begin
    32   Result := 1;
    33 end;
    34 
    35 function TGColorFormatGray1.GetName: string;
    36 begin
    37   Result := 'Gray1';
    38 end;
    39 
    40 function TGColorFormatGray1.GetChannelBitPos(Channel: TGColorChannel): Integer;
    41 begin
    42   case Channel of
    43     ccGray: Result := 0;
    44     else Result := 0;
    45   end;
    46 end;
    47 
    48 function TGColorFormatGray1.GetChannelBitWidth(Channel: TGColorChannel
    49   ): Integer;
    50 begin
    51   if Channel = ccGray then
    52     Result := 1 else Result := 0;
     23  inherited;
     24  Name := 'Gray1';
     25  BitDepth := 1;
     26  Channels[ccGray].Position := 0;
     27  Channels[ccGray].BitWidth := 1;
    5328end;
    5429
  • trunk/ColorFormats/UColorGray4.pas

    r10 r14  
    1212
    1313  TGColorFormatGray4 = class(TGColorFormat)
    14     function GetPixelSize: Integer; override;
    15     function GetBitDepth: Integer; override;
    16     function GetName: string; override;
    17     function GetChannelBitPos(Channel: TGColorChannel): Integer; override;
    18     function GetChannelBitWidth(Channel: TGColorChannel): Integer; override;
     14    constructor Create; override;
    1915  end;
    2016
     
    2319{ TGColorFormatGray4 }
    2420
    25 function TGColorFormatGray4.GetPixelSize: Integer;
     21
     22
     23constructor TGColorFormatGray4.Create;
    2624begin
    27   Result := 4;
     25  inherited Create;
     26  inherited;
     27  Name := 'Gray4';
     28  BitDepth := 4;
     29  Channels[ccGray].Position := 0;
     30  Channels[ccGray].BitWidth := 4;
    2831end;
    29 
    30 function TGColorFormatGray4.GetBitDepth: Integer;
    31 begin
    32   Result := 4;
    33 end;
    34 
    35 function TGColorFormatGray4.GetName: string;
    36 begin
    37   Result := 'Gray4';
    38 end;
    39 
    40 function TGColorFormatGray4.GetChannelBitPos(Channel: TGColorChannel): Integer;
    41 begin
    42   case Channel of
    43     ccGray: Result := 0;
    44     else Result := 0;
    45   end;
    46 end;
    47 
    48 function TGColorFormatGray4.GetChannelBitWidth(Channel: TGColorChannel
    49   ): Integer;
    50 begin
    51   if Channel = ccGray then
    52     Result := 4 else Result := 0;
    53 end;
    54 
    5532
    5633end.
  • trunk/ColorFormats/UColorGray8.pas

    r9 r14  
    1212
    1313  TGColorFormatGray8 = class(TGColorFormat)
    14     function GetPixelSize: Integer; override;
    15     function GetBitDepth: Integer; override;
    16     function GetName: string; override;
    17     function GetChannelBitPos(Channel: TGColorChannel): Integer; override;
    18     function GetChannelBitWidth(Channel: TGColorChannel): Integer; override;
     14    constructor Create; override;
    1915  end;
    2016
     
    2319{ TGColorFormatGray8 }
    2420
    25 function TGColorFormatGray8.GetPixelSize: Integer;
     21
     22
     23constructor TGColorFormatGray8.Create;
    2624begin
    27   Result := 8;
     25  inherited Create;
     26  inherited;
     27  Name := 'Gray8';
     28  BitDepth := 8;
     29  Channels[ccGray].Position := 0;
     30  Channels[ccGray].BitWidth := 8;
    2831end;
    29 
    30 function TGColorFormatGray8.GetBitDepth: Integer;
    31 begin
    32   Result := 8;
    33 end;
    34 
    35 function TGColorFormatGray8.GetName: string;
    36 begin
    37   Result := 'Gray8';
    38 end;
    39 
    40 function TGColorFormatGray8.GetChannelBitPos(Channel: TGColorChannel): Integer;
    41 begin
    42   case Channel of
    43     ccGray: Result := 0;
    44     else Result := 0;
    45   end;
    46 end;
    47 
    48 function TGColorFormatGray8.GetChannelBitWidth(Channel: TGColorChannel
    49   ): Integer;
    50 begin
    51   if Channel = ccGray then
    52     Result := 8 else Result := 0;
    53 end;
    54 
    5532
    5633end.
  • trunk/ColorFormats/UColorRGB565.pas

    r11 r14  
    66
    77uses
    8   Classes, SysUtils, Graphics, UGraphic, UMemory;
     8  Classes, SysUtils, Graphics, UGraphic;
    99
    1010type
     
    1212
    1313  TGColorFormatRGB565 = class(TGColorFormat)
    14     function GetPixelSize: Integer; override;
    15     function GetBitDepth: Integer; override;
    16     function GetName: string; override;
    17     function GetChannelBitPos(Channel: TGColorChannel): Integer; override;
    18     function GetChannelBitWidth(Channel: TGColorChannel): Integer; override;
     14    constructor Create; override;
    1915  end;
    2016
     
    2420{ TGColorFormatRGB565 }
    2521
    26 function TGColorFormatRGB565.GetPixelSize: Integer;
     22constructor TGColorFormatRGB565.Create;
    2723begin
    28   Result := 16;
    29 end;
    30 
    31 function TGColorFormatRGB565.GetBitDepth: Integer;
    32 begin
    33   Result := 16;
    34 end;
    35 
    36 function TGColorFormatRGB565.GetName: string;
    37 begin
    38   Result := 'RGB565';
    39 end;
    40 
    41 function TGColorFormatRGB565.GetChannelBitPos(Channel: TGColorChannel): Integer;
    42 begin
    43   case Channel of
    44     ccRed: Result := 0;
    45     ccGreen: Result := 5;
    46     ccBlue: Result := 11;
    47     else Result := 0;
    48   end;
    49 end;
    50 
    51 function TGColorFormatRGB565.GetChannelBitWidth(Channel: TGColorChannel
    52   ): Integer;
    53 begin
    54   case Channel of
    55     ccRed: Result := 5;
    56     ccGreen: Result := 6;
    57     ccBlue: Result := 5;
    58     else Result := 0;
    59   end;
     24  inherited Create;
     25  BitDepth := 16;
     26  Name := 'RGB565';
     27  Channels[ccRed].Position := 0;
     28  Channels[ccRed].BitWidth := 5;
     29  Channels[ccGreen].Position := 5;
     30  Channels[ccGreen].BitWidth := 6;
     31  Channels[ccBlue].Position := 9;
     32  Channels[ccBlue].BitWidth := 6;
    6033end;
    6134
  • trunk/ColorFormats/UColorRGBA8.pas

    r9 r14  
    1212
    1313  TGColorFormatRGBA8 = class(TGColorFormat)
    14     function GetPixelSize: Integer; override;
    15     function GetBitDepth: Integer; override;
    16     function GetName: string; override;
    17     function GetChannelBitPos(Channel: TGColorChannel): Integer; override;
    18     function GetChannelBitWidth(Channel: TGColorChannel): Integer; override;
     14    constructor Create; override;
    1915  end;
    2016
     
    2420{ TGColorFormatRGBA8 }
    2521
    26 function TGColorFormatRGBA8.GetPixelSize: Integer;
     22constructor TGColorFormatRGBA8.Create;
    2723begin
    28   Result := 32;
    29 end;
    30 
    31 function TGColorFormatRGBA8.GetBitDepth: Integer;
    32 begin
    33   Result := 32;
    34 end;
    35 
    36 function TGColorFormatRGBA8.GetName: string;
    37 begin
    38   Result := 'RGBA8';
    39 end;
    40 
    41 function TGColorFormatRGBA8.GetChannelBitPos(Channel: TGColorChannel): Integer;
    42 begin
    43   case Channel of
    44     ccRed: Result := 0;
    45     ccGreen: Result := 8;
    46     ccBlue: Result := 16;
    47     ccOpacity: Result := 24;
    48     else Result := 0;
    49   end;
    50 end;
    51 
    52 function TGColorFormatRGBA8.GetChannelBitWidth(Channel: TGColorChannel
    53   ): Integer;
    54 begin
    55   if (Channel = ccBlue) or (Channel = ccRed) or (Channel = ccGreen) then
    56     Result := 8 else Result := 0;
     24  inherited Create;
     25  BitDepth := 32;
     26  Name := 'RGBA8';
     27  Channels[ccRed].Position := 0;
     28  Channels[ccRed].BitWidth := 8;
     29  Channels[ccGreen].Position := 8;
     30  Channels[ccGreen].BitWidth := 8;
     31  Channels[ccBlue].Position := 16;
     32  Channels[ccBlue].BitWidth := 8;
     33  Channels[ccOpacity].Position := 24;
     34  Channels[ccOpacity].BitWidth := 8;
    5735end;
    5836
  • trunk/Forms/UFormNew.pas

    r10 r14  
    4949  ColorFormat := ColorManager.Formats[ComboBoxColorFormat.ItemIndex];
    5050  LabelMemRequire.Caption := IntToStr(SpinEditWidth.Value * SpinEditHeight.Value *
    51     ColorFormat.GetBitDepth div 8) + ' bytes';
     51    ColorFormat.BitDepth div 8) + ' bytes';
    5252end;
    5353
     
    6060  for I := 0 to ColorManager.FormatCount - 1 do begin
    6161    ColorFormat := ColorManager.Formats[I];
    62     ComboBoxColorFormat.AddItem(ColorFormat.GetName, nil);
     62    ComboBoxColorFormat.AddItem(ColorFormat.Name, nil);
    6363  end;
    6464  if ComboBoxColorFormat.Items.Count > 0 then
  • trunk/LibrePaint.lpi

    r13 r14  
    118118        <Filename Value="ColorFormats/UColorGray8.pas"/>
    119119        <IsPartOfProject Value="True"/>
     120        <UnitName Value="UColorGray8"/>
    120121      </Unit7>
    121122      <Unit8>
  • trunk/UGraphic.pas

    r13 r14  
    1515  TGBitmap = class;
    1616
     17  TGColorFormatChannel = record
     18    BitWidth: Integer;
     19    Position: Integer;
     20  end;
     21
    1722  { TGColorFormat }
    1823
    1924  TGColorFormat = class
    20     function GetBitDepth: Integer; virtual;
    21     function GetPixelSize: Integer; virtual;
    22     function GetName: string; virtual;
    23     function GetChannelBitPos(Channel: TGColorChannel): Integer; virtual;
    24     function GetChannelBitWidth(Channel: TGColorChannel): Integer; virtual;
     25    BitDepth: Integer;
     26    Name: string;
     27    Channels: array[TGColorChannel] of TGColorFormatChannel;
     28    constructor Create; virtual;
    2529    function GetChannelStateCount(Channel: TGColorChannel): Integer; virtual;
    2630    function ChannelUsed(Channel: TGColorChannel): Boolean;
     
    233237{ TGColorFormat }
    234238
    235 function TGColorFormat.GetBitDepth: Integer;
    236 begin
    237   Result := 0;
    238 end;
    239 
    240 function TGColorFormat.GetPixelSize: Integer;
    241 begin
    242   Result := 0;
    243 end;
    244 
    245 function TGColorFormat.GetName: string;
    246 begin
    247   Result := '';
    248 end;
    249 
    250 function TGColorFormat.GetChannelBitPos(Channel: TGColorChannel): Integer;
    251 begin
    252 
    253 end;
    254 
    255 function TGColorFormat.GetChannelBitWidth(Channel: TGColorChannel): Integer;
    256 begin
    257   Result := 0;
     239constructor TGColorFormat.Create;
     240var
     241  Channel: TGColorChannel;
     242begin
     243  Name := '';
     244  BitDepth := 0;
     245  for Channel := Low(TGColorChannel) to High(TGColorChannel) do begin
     246    Channels[Channel].BitWidth := 0;
     247    Channels[Channel].Position := 0;
     248  end;
    258249end;
    259250
    260251function TGColorFormat.GetChannelStateCount(Channel: TGColorChannel): Integer;
    261252begin
    262   Result := 1 shl GetChannelBitWidth(Channel);
     253  Result := 1 shl Channels[Channel].BitWidth;
    263254end;
    264255
    265256function TGColorFormat.ChannelUsed(Channel: TGColorChannel): Boolean;
    266257begin
    267   Result := GetChannelBitWidth(Channel) > 0;
     258  Result := Channels[Channel].BitWidth > 0;
    268259end;
    269260
     
    275266  Channel := TBitMemory.Create;
    276267
    277   if GetChannelBitWidth(ccRed) > 0 then begin
    278     Channel.Size := GetChannelBitWidth(ccRed);
    279     Color.Data.ReadBlock(Channel, GetChannelBitPos(ccRed));
     268  if Channels[ccRed].BitWidth > 0 then begin
     269    Channel.Size := Channels[ccRed].BitWidth;
     270    Color.Data.ReadBlock(Channel, Channels[ccRed].Position);
    280271    Result := Result or (Trunc(Channel.GetInteger * 255 / (GetChannelStateCount(ccRed) - 1)) shl 0);
    281272  end;
    282273
    283   if GetChannelBitWidth(ccGreen) > 0 then begin
    284     Channel.Size := GetChannelBitWidth(ccGreen);
    285     Color.Data.ReadBlock(Channel, GetChannelBitPos(ccGreen));
     274  if Channels[ccGreen].BitWidth > 0 then begin
     275    Channel.Size := Channels[ccGreen].BitWidth;
     276    Color.Data.ReadBlock(Channel, Channels[ccGreen].Position);
    286277    Result := Result or (Trunc(Channel.GetInteger * 255 / (GetChannelStateCount(ccGreen) - 1)) shl 8);
    287278  end;
    288279
    289   if GetChannelBitWidth(ccBlue) > 0 then begin
    290     Channel.Size := GetChannelBitWidth(ccBlue);
    291     Color.Data.ReadBlock(Channel, GetChannelBitPos(ccBlue));
     280  if Channels[ccBlue].BitWidth > 0 then begin
     281    Channel.Size := Channels[ccBlue].BitWidth;
     282    Color.Data.ReadBlock(Channel, Channels[ccBlue].Position);
    292283    Result := Result or (Trunc(Channel.GetInteger * 255 / (GetChannelStateCount(ccBlue) - 1)) shl 16);
    293284  end;
    294285
    295   if GetChannelBitWidth(ccGray) > 0 then begin
    296     Channel.Size := GetChannelBitWidth(ccGray);
    297     Color.Data.ReadBlock(Channel, GetChannelBitPos(ccGray));
     286  if Channels[ccGray].BitWidth > 0 then begin
     287    Channel.Size := Channels[ccGray].BitWidth;
     288    Color.Data.ReadBlock(Channel, Channels[ccGray].Position);
    298289    Result := $010101 * Trunc(Channel.GetInteger * 255 / (GetChannelStateCount(ccGray) - 1));
    299290  end;
     
    309300  Channel := TBitMemory.Create;
    310301
    311   if GetChannelBitWidth(ccRed) > 0 then begin
    312     Channel.Size := GetChannelBitWidth(ccRed);
     302  if Channels[ccRed].BitWidth > 0 then begin
     303    Channel.Size := Channels[ccRed].BitWidth;
    313304    Channel.SetInteger(((Color shr 0) and $ff) * GetChannelStateCount(ccRed) div 256);
    314     GColor.Data.WriteBlock(Channel, GetChannelBitPos(ccRed));
    315   end;
    316 
    317   if GetChannelBitWidth(ccGreen) > 0 then begin
    318     Channel.Size := GetChannelBitWidth(ccGreen);
     305    GColor.Data.WriteBlock(Channel, Channels[ccRed].Position);
     306  end;
     307
     308  if Channels[ccGreen].BitWidth > 0 then begin
     309    Channel.Size := Channels[ccGreen].BitWidth;
    319310    Channel.SetInteger(((Color shr 8) and $ff) * GetChannelStateCount(ccGreen) div 256);
    320     GColor.Data.WriteBlock(Channel, GetChannelBitPos(ccGreen));
    321   end;
    322 
    323   if GetChannelBitWidth(ccBlue) > 0 then begin
    324     Channel.Size := GetChannelBitWidth(ccBlue);
     311    GColor.Data.WriteBlock(Channel, Channels[ccGreen].Position);
     312  end;
     313
     314  if Channels[ccBlue].BitWidth > 0 then begin
     315    Channel.Size := Channels[ccBlue].BitWidth;
    325316    Channel.SetInteger(((Color shr 16) and $ff) * GetChannelStateCount(ccBlue) div 256);
    326     GColor.Data.WriteBlock(Channel, GetChannelBitPos(ccBlue));
    327   end;
    328 
    329   if GetChannelBitWidth(ccGray) > 0 then begin
    330     Channel.Size := GetChannelBitWidth(ccGray);
     317    GColor.Data.WriteBlock(Channel, Channels[ccBlue].Position);
     318  end;
     319
     320  if Channels[ccGray].BitWidth > 0 then begin
     321    Channel.Size := Channels[ccGray].BitWidth;
    331322    Channel.SetInteger((((Color shr 16) and $ff) + ((Color shr 8) and $ff) + ((Color shr 0) and $ff))
    332323      * GetChannelStateCount(ccGray) div (3 * 256));
    333     GColor.Data.WriteBlock(Channel, GetChannelBitPos(ccGray));
     324    GColor.Data.WriteBlock(Channel, Channels[ccGray].Position);
    334325  end;
    335326
     
    348339  if FColorFormat = AValue then Exit;
    349340  FColorFormat := AValue;
    350   FData.Size := FColorFormat.GetPixelSize;
     341  FData.Size := FColorFormat.BitDepth;
    351342end;
    352343
     
    377368  Channel := TBitMemory.Create;
    378369
    379   if Format.GetChannelBitWidth(ccRed) > 0 then begin
    380     Channel.Size := Format.GetChannelBitWidth(ccRed);
    381     Data.ReadBlock(Channel, Format.GetChannelBitPos(ccRed));
     370  if Format.Channels[ccRed].BitWidth > 0 then begin
     371    Channel.Size := Format.Channels[ccRed].BitWidth;
     372    Data.ReadBlock(Channel, Format.Channels[ccRed].Position);
    382373    Channel.Invert;
    383     Data.WriteBlock(Channel, Format.GetChannelBitPos(ccRed));
    384   end;
    385 
    386   if Format.GetChannelBitWidth(ccGreen) > 0 then begin
    387     Channel.Size := Format.GetChannelBitWidth(ccGreen);
    388     Data.ReadBlock(Channel, Format.GetChannelBitPos(ccGreen));
     374    Data.WriteBlock(Channel, Format.Channels[ccRed].Position);
     375  end;
     376
     377  if Format.Channels[ccGreen].BitWidth > 0 then begin
     378    Channel.Size := Format.Channels[ccGreen].BitWidth;
     379    Data.ReadBlock(Channel, Format.Channels[ccGreen].Position);
    389380    Channel.Invert;
    390     Data.WriteBlock(Channel, Format.GetChannelBitPos(ccGreen));
    391   end;
    392 
    393   if Format.GetChannelBitWidth(ccBlue) > 0 then begin
    394     Channel.Size := Format.GetChannelBitWidth(ccBlue);
    395     Data.ReadBlock(Channel, Format.GetChannelBitPos(ccBlue));
     381    Data.WriteBlock(Channel, Format.Channels[ccGreen].Position);
     382  end;
     383
     384  if Format.Channels[ccBlue].BitWidth > 0 then begin
     385    Channel.Size := Format.Channels[ccBlue].BitWidth;
     386    Data.ReadBlock(Channel, Format.Channels[ccBlue].Position);
    396387    Channel.Invert;
    397     Data.WriteBlock(Channel, Format.GetChannelBitPos(ccBlue));
    398   end;
    399 
    400   if Format.GetChannelBitWidth(ccGray) > 0 then begin
    401     Channel.Size := Format.GetChannelBitWidth(ccGray);
    402     Data.ReadBlock(Channel, Format.GetChannelBitPos(ccGray));
     388    Data.WriteBlock(Channel, Format.Channels[ccBlue].Position);
     389  end;
     390
     391  if Format.Channels[ccGray].BitWidth > 0 then begin
     392    Channel.Size := Format.Channels[ccGray].BitWidth;
     393    Data.ReadBlock(Channel, Format.Channels[ccGray].Position);
    403394    Channel.Invert;
    404     Data.WriteBlock(Channel, Format.GetChannelBitPos(ccGray));
     395    Data.WriteBlock(Channel, Format.Channels[ccGray].Position);
    405396  end;
    406397
     
    462453function TGBitmap.GetPixelDataPos(X, Y: Integer): Integer;
    463454begin
    464   Result := X * FColorFormat.GetPixelSize + Y * FColorFormat.GetPixelSize * FSize.X;
     455  Result := X * FColorFormat.BitDepth + Y * FColorFormat.BitDepth * FSize.X;
    465456end;
    466457
     
    496487function TGBitmap.GetDataSize: Integer;
    497488begin
    498   Result := Size.X * Size.Y * FColorFormat.GetPixelSize;
     489  Result := Size.X * Size.Y * FColorFormat.BitDepth;
    499490end;
    500491
Note: See TracChangeset for help on using the changeset viewer.