Changeset 10 for trunk/UProject.pas


Ignore:
Timestamp:
Sep 22, 2014, 3:07:02 AM (10 years ago)
Author:
chronos
Message:
  • Added: Zooming and moving image.
  • Added: Color format 4-bit gray.
  • Added: Image operation for generating test gradient image.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/UProject.pas

    r6 r10  
    1010type
    1111
    12   { TViewPort }
     12  { TView }
    1313
    14   TViewPort = class
     14  TView = class
     15  private
     16    FSrcRect: TRect;
     17    FDestRect: TRect;
     18    FZoom: Double;
     19    procedure SetDestRect(AValue: TRect);
     20    procedure SetSrcRect(AValue: TRect);
     21    procedure SetZoom(AValue: Double);
    1522  public
    16     Pos: TPoint;
    17     Zoom: Double;
    18     ViewSize: TPoint;
     23    procedure Center(Rect: TRect);
     24    constructor Create;
     25    function DestToSrcPos(Pos: TPoint): TPoint;
     26    function SrcToDestPos(Pos: TPoint): TPoint;
     27    property DestRect: TRect read FDestRect write SetDestRect;
     28    property Zoom: Double read FZoom write SetZoom;
     29    property SrcRect: TRect read FSrcRect write SetSrcRect;
    1930  end;
    2031
     
    2435    FileName: string;
    2536    Bitmap: TGBitmap;
    26     ViewPort: TViewPort;
     37    View: TView;
    2738    constructor Create;
    2839    destructor Destroy; override;
     
    3041
    3142implementation
     43
     44function RectEquals(A, B: TRect): Boolean;
     45begin
     46  Result := (A.Left = B.Left) and (A.Top = B.Top) and
     47    (A.Right = B.Right) and (A.Bottom = B.Bottom);
     48end;
     49
     50
     51{ TView }
     52
     53procedure TView.SetDestRect(AValue: TRect);
     54var
     55  Diff: TPoint;
     56begin
     57  if RectEquals(FDestRect, AValue) then Exit;
     58  Diff := Point(Trunc((FDestRect.Right - FDestRect.Left) / FZoom -
     59    (AValue.Right - AValue.Left) / FZoom) div 2,
     60    Trunc((FDestRect.Bottom - FDestRect.Top) / FZoom -
     61    (AValue.Bottom - AValue.Top) / FZoom) div 2);
     62  FDestRect := AValue;
     63  SrcRect := Bounds(FSrcRect.Left + Diff.X, FSrcRect.Top + Diff.Y,
     64    Trunc((FDestRect.Right - FDestRect.Left) / FZoom),
     65    Trunc((FDestRect.Bottom - FDestRect.Top) / FZoom));
     66end;
     67
     68procedure TView.SetSrcRect(AValue: TRect);
     69begin
     70  if RectEquals(FSrcRect, AValue) then Exit;
     71  FSrcRect := AValue;
     72end;
     73
     74procedure TView.SetZoom(AValue: Double);
     75const
     76  ZoomMin = 1/10000;
     77  ZoomMax = 10000;
     78begin
     79  if FZoom = AValue then Exit;
     80  if (FZoom > ZoomMax) or (FZoom < ZoomMin) then Exit;
     81  FZoom := AValue;
     82  FSrcRect := Bounds(Trunc(FSrcRect.Left + (FSrcRect.Right - FSrcRect.Left) div 2 -
     83    (DestRect.Right - DestRect.Left) / FZoom / 2),
     84    Trunc(FSrcRect.Top + (FSrcRect.Bottom - FSrcRect.Top) div 2 -
     85    (DestRect.Bottom - DestRect.Top) / FZoom / 2),
     86    Trunc((DestRect.Right - DestRect.Left) / FZoom),
     87    Trunc((DestRect.Bottom - DestRect.Top) / FZoom));
     88end;
     89
     90procedure TView.Center(Rect: TRect);
     91begin
     92  FSrcRect := Bounds(Rect.Left + (Rect.Right - Rect.Left) div 2 - (FSrcRect.Right - FSrcRect.Left) div 2,
     93    Rect.Top + (Rect.Bottom - Rect.Top) div 2 - (FSrcRect.Bottom - FSrcRect.Top) div 2,
     94    FSrcRect.Right - FSrcRect.Left,
     95    FSrcRect.Bottom - FSrcRect.Top);
     96end;
     97
     98constructor TView.Create;
     99begin
     100  FZoom := 1;
     101end;
     102
     103function TView.DestToSrcPos(Pos: TPoint): TPoint;
     104begin
     105  Result := Point(Trunc(Pos.X / FZoom + FSrcRect.Left),
     106    Trunc(Pos.Y / FZoom + FSrcRect.Top));
     107end;
     108
     109function TView.SrcToDestPos(Pos: TPoint): TPoint;
     110begin
     111  Result := Point(Trunc((Pos.X - FSrcRect.Left) * FZoom),
     112    Trunc((Pos.Y - FSrcRect.Top) * FZoom));
     113end;
    32114
    33115
     
    37119begin
    38120  Bitmap := TGBitmap.Create;
    39   ViewPort := TViewPort.Create;
     121  View := TView.Create;
    40122end;
    41123
    42124destructor TProject.Destroy;
    43125begin
    44   ViewPort.Free;
     126  View.Free;
    45127  Bitmap.Free;
    46128  inherited Destroy;
Note: See TracChangeset for help on using the changeset viewer.