Changeset 14 for trunk/UGraphic.pas


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.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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.