Changeset 13 for trunk/UGraphic.pas


Ignore:
Timestamp:
Sep 22, 2014, 5:25:11 PM (10 years ago)
Author:
chronos
Message:
  • Added: Image operation Negative image.
  • Added: Partialy implemented Image load from and save to file.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/UGraphic.pas

    r10 r13  
    4444    function ToTColor: TColor;
    4545    procedure FromTColor(Color: TColor);
     46    procedure Invert;
    4647    procedure Assign(Source: TGColor); virtual;
    4748    constructor Create;
     
    7172    procedure CheckLimits(X, Y: Integer);
    7273  public
     74    procedure LoadFromCanvas(Canvas: TCanvas; ASize: TPoint);
    7375    function GetDataSize: Integer;
    7476    procedure PaintToCanvas(Canvas: TCanvas; Rect: TRect);
     
    7880    procedure Flip;
    7981    procedure Mirror;
     82    procedure Negative;
    8083    constructor Create; virtual;
    8184    destructor Destroy; override;
     
    368371end;
    369372
     373procedure TGColor.Invert;
     374var
     375  Channel: TBitMemory;
     376begin
     377  Channel := TBitMemory.Create;
     378
     379  if Format.GetChannelBitWidth(ccRed) > 0 then begin
     380    Channel.Size := Format.GetChannelBitWidth(ccRed);
     381    Data.ReadBlock(Channel, Format.GetChannelBitPos(ccRed));
     382    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));
     389    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));
     396    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));
     403    Channel.Invert;
     404    Data.WriteBlock(Channel, Format.GetChannelBitPos(ccGray));
     405  end;
     406
     407  Channel.Free;
     408end;
     409
    370410procedure TGColor.Assign(Source: TGColor);
    371411begin
     
    429469  if (X < 0) or (Y < 0) or (X >= Size.X) or (Y >= Size.Y) then
    430470    raise Exception.Create('Out of range');
     471end;
     472
     473procedure TGBitmap.LoadFromCanvas(Canvas: TCanvas; ASize: TPoint);
     474var
     475  X, Y: Integer;
     476  Pixel: TGColor;
     477begin
     478  Pixel := TGColor.Create;
     479  Pixel.Format := ColorFormat;
     480  Size := ASize;
     481  try
     482  Canvas.Lock;
     483  for Y := 0 to Size.Y - 1 do
     484    for X := 0 to Size.X do
     485    if (X >= 0) and (X < Size.X) and (Y >= 0) and (Y < Size.Y) then begin
     486      Pixel.FromTColor(Canvas.Pixels[X, Y]);
     487      Pixels[X, Y] := Pixel;
     488    end;
     489
     490  finally
     491    Canvas.Unlock;
     492  end;
     493  Pixel.Free;
    431494end;
    432495
     
    536599end;
    537600
     601procedure TGBitmap.Negative;
     602var
     603  X, Y: Integer;
     604  Color: TGColor;
     605begin
     606  for Y := 0 to Size.Y - 1 do
     607    for X := 0 to Size.X - 1 do begin
     608      Color := Pixels[X, Y];
     609      Color.Invert;
     610      Pixels[X, Y] := Color;
     611      Color.Free;
     612    end;
     613end;
     614
    538615constructor TGBitmap.Create;
    539616begin
Note: See TracChangeset for help on using the changeset viewer.