Ignore:
Timestamp:
Mar 31, 2021, 7:21:45 PM (3 years ago)
Author:
chronos
Message:
  • Modified: Image resize tool scale up using xbrzscale tool.
  • Modified: Various improvements of generation of scaled up tiles.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • tools/Image resize/UPixelPointer.pas

    r280 r333  
    99  TColor32 = type Cardinal;
    1010  TColor32Component = (ccBlue, ccGreen, ccRed, ccAlpha);
     11
     12  { TPixel32 }
     13
    1114  TPixel32 = packed record
     15  private
     16    procedure SetRGB(AValue: Cardinal);
     17  public
     18    function GetRGB: Cardinal;
     19    property RGB: Cardinal read GetRGB write SetRGB;
    1220    case Integer of
    1321      0: (B, G, R, A: Byte);
     
    3745
    3846  function PixelPointer(Bitmap: TRasterImage; BaseX: Integer = 0; BaseY: Integer = 0): TPixelPointer; inline;
    39 
     47  function SwapRedBlue(Color: TColor32): TColor32;
     48  procedure BitmapCopyRect(DstBitmap: TRasterImage; DstRect: TRect; SrcBitmap: TRasterImage; SrcPos: TPoint);
     49  procedure BitmapStretchRect(DstBitmap: TRasterImage; DstRect: TRect;
     50    SrcBitmap: TRasterImage; SrcRect: TRect);
     51  procedure BitmapFill(Bitmap:TRasterImage; Color: TColor32);
     52  procedure BitmapFillRect(Bitmap:TRasterImage; Color: TColor32; Rect: TRect);
     53  procedure BitmapSwapRedBlue(Bitmap:TRasterImage);
    4054
    4155implementation
     56
     57{ TPixel32 }
     58
     59function TPixel32.GetRGB: Cardinal;
     60begin
     61  Result := ARGB and $ffffff;
     62end;
     63
     64procedure TPixel32.SetRGB(AValue: Cardinal);
     65begin
     66  R := (AValue shr 16) and $ff;
     67  G := (AValue shr 8) and $ff;
     68  B := (AValue shr 0) and $ff;
     69end;
    4270
    4371{ TPixelPointer }
     
    74102begin
    75103  Pixel := Pointer(Line) + X * BytesPerPixel;
     104end;
     105
     106procedure BitmapCopyRect(DstBitmap: TRasterImage; DstRect: TRect;
     107  SrcBitmap: TRasterImage; SrcPos: TPoint);
     108var
     109  SrcPtr, DstPtr: TPixelPointer;
     110  X, Y: Integer;
     111begin
     112  SrcBitmap.BeginUpdate(True);
     113  DstBitmap.BeginUpdate(True);
     114  SrcPtr := PixelPointer(SrcBitmap, SrcPos.X, SrcPos.Y);
     115  DstPtr := PixelPointer(DstBitmap, DstRect.Left, DstRect.Top);
     116  for Y := 0 to DstRect.Height - 1 do begin
     117    for X := 0 to DstRect.Width - 1 do begin
     118      DstPtr.Pixel^.ARGB := SrcPtr.Pixel^.ARGB;
     119      SrcPtr.NextPixel;
     120      DstPtr.NextPixel;
     121    end;
     122    SrcPtr.NextLine;
     123    DstPtr.NextLine;
     124  end;
     125  SrcBitmap.EndUpdate;
     126  DstBitmap.EndUpdate;
     127end;
     128
     129procedure BitmapStretchRect(DstBitmap: TRasterImage; DstRect: TRect;
     130  SrcBitmap: TRasterImage; SrcRect: TRect);
     131var
     132  SrcPtr, DstPtr: TPixelPointer;
     133  SubPtr: TPixelPointer;
     134  X, Y: Integer;
     135  XX, YY: Integer;
     136  R: TRect;
     137  C: TColor32;
     138begin
     139  if (DstRect.Width = SrcRect.Width) and (DstRect.Height = SrcRect.Height) then begin
     140    BitmapCopyRect(DstBitmap, DstRect, SrcBitmap, Point(SrcRect.Left, SrcRect.Top));
     141    Exit;
     142  end;
     143  SrcBitmap.BeginUpdate(True);
     144  DstBitmap.BeginUpdate(True);
     145  SrcPtr := PixelPointer(SrcBitmap, SrcRect.Left, SrcRect.Top);
     146  DstPtr := PixelPointer(DstBitmap, DstRect.Left, DstRect.Top);
     147  for Y := 0 to DstRect.Height - 1 do begin
     148    for X := 0 to DstRect.Width - 1 do begin
     149      R := Rect(Trunc(X * SrcRect.Width / DstRect.Width),
     150        Trunc(Y * SrcRect.Height / DstRect.Height),
     151        Trunc((X + 1) * SrcRect.Width / DstRect.Width),
     152        Trunc((Y + 1) * SrcRect.Height / DstRect.Height));
     153      DstPtr.SetXY(X, Y);
     154      SrcPtr.SetXY(R.Left, R.Top);
     155      C := SrcPtr.Pixel^.ARGB;
     156      DstPtr.Pixel^.ARGB := C;
     157      for YY := 0 to R.Height - 1 do begin
     158        for XX := 0 to R.Width - 1 do begin
     159          DstPtr.Pixel^.ARGB := C;
     160          DstPtr.NextPixel;
     161        end;
     162        DstPtr.NextLine;
     163      end;
     164    end;
     165  end;
     166  SrcBitmap.EndUpdate;
     167  DstBitmap.EndUpdate;
     168end;
     169
     170procedure BitmapFill(Bitmap: TRasterImage; Color: TColor32);
     171var
     172  X, Y: Integer;
     173  Ptr: TPixelPointer;
     174begin
     175  Bitmap.BeginUpdate(True);
     176  Ptr := PixelPointer(Bitmap);
     177  for Y := 0 to Bitmap.Height - 1 do begin
     178    for X := 0 to Bitmap.Width - 1 do begin
     179      Ptr.Pixel^.ARGB := Color;
     180      Ptr.NextPixel;
     181    end;
     182    Ptr.NextLine;
     183  end;
     184  Bitmap.EndUpdate;
     185end;
     186
     187procedure BitmapFillRect(Bitmap: TRasterImage; Color: TColor32; Rect: TRect);
     188var
     189  X, Y: Integer;
     190  Ptr: TPixelPointer;
     191begin
     192  Bitmap.BeginUpdate(True);
     193  Ptr := PixelPointer(Bitmap, Rect.Left, Rect.Top);
     194  for Y := 0 to Rect.Height - 1 do begin
     195    for X := 0 to Rect.Width - 1 do begin
     196      Ptr.Pixel^.ARGB := Color;
     197      Ptr.NextPixel;
     198    end;
     199    Ptr.NextLine;
     200  end;
     201  Bitmap.EndUpdate;
     202end;
     203
     204procedure BitmapSwapRedBlue(Bitmap: TRasterImage);
     205var
     206  X, Y: Integer;
     207  Ptr: TPixelPointer;
     208begin
     209  Bitmap.BeginUpdate(True);
     210  Ptr := PixelPointer(Bitmap);
     211  for Y := 0 to Bitmap.Height - 1 do begin
     212    for X := 0 to Bitmap.Width - 1 do begin
     213      Ptr.Pixel^.ARGB := SwapRedBlue(Ptr.Pixel^.ARGB);
     214      Ptr.NextPixel;
     215    end;
     216    Ptr.NextLine;
     217  end;
     218  Bitmap.EndUpdate;
    76219end;
    77220
     
    86229end;
    87230
     231function SwapRedBlue(Color: TColor32): TColor32;
     232begin
     233  Result := (Color and $ff00ff00) or ((Color and $ff) shl 16) or ((Color shr 16) and $ff);
     234end;
     235
    88236
    89237end.
Note: See TracChangeset for help on using the changeset viewer.