1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
|
unit Unit1;
{$mode objfpc}{$H+}
{$ASMMODE INTEL}
interface
uses
Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ExtCtrls,
StdCtrls, ExtDlgs, FPImage;
type
TRGBQuad = packed record
R,G,B,A : Byte
end;
PRGBQuad = ^TRGBQuad;
{ TForm1 }
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
OpenPictureDialog: TOpenPictureDialog;
Panel1: TPanel;
Panel2: TPanel;
pnlSrc: TPanel;
pnlDst: TPanel;
procedure Button1Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
procedure FormShow(Sender: TObject);
private
{ private declarations }
public
{ public declarations }
SrcBmp : TBitmap;
DstBmp : TBitmap;
procedure UpdateSrcView;
procedure UpdateDstView;
Procedure MyStretch(Const NewWidth:Integer;Const NewHeight:Integer;Const KeepRatio:Boolean=false);
end;
var
Form1: TForm1;
implementation
{$R *.lfm}
{ TForm1 }
function RGBQuadToFPColor(Const RGBA: TRGBQuad) : TFPcolor;
begin
with Result do
begin
Red :=(RGBA.R shl 8) or RGBA.R;
Green :=(RGBA.G shl 8) or RGBA.G;
Blue :=(RGBA.B shl 8) or RGBA.B;
Alpha :=255-RGBA.A;
Alpha :=(RGBA.A shl 8) or RGBA.A;
end;
end;
function Clamp(const V,Min,Max : integer) : integer; assembler; nostackframe;
asm
{$IFDEF CPUX86_64}
MOV EAX,ECX
MOV ECX,R8D
{$ENDIF}
CMP EDX,EAX
CMOVG EAX,EDX
CMP ECX,EAX
CMOVL EAX,ECX
end;
procedure KeepAspectRatio(Const SrcW,SrcH:Integer; var NewWidth, NewHeight:Integer);
begin
// KEEP RATIO
if SrcW <> SrcH then
begin
if SrcH > SrcW then
begin
// NewHeight := wHeight;
NewWidth := (NewHeight * SrcW) div SrcH;
end
else
begin
// newWidth := wWidth;
NewHeight := (NewWidth * SrcH) div SrcW;
end;
end;
end;
Procedure TForm1.MyStretch(Const NewWidth:Integer;Const NewHeight:Integer;Const KeepRatio:Boolean=false);
Var
NH,NW,SrcW,SrcH,X,Y : Integer;
//SrcWMax,SrcHMax: Integer;
destwidth,destheight : Integer;
fx,fy,fxstep,fystep :Single;
U, V, U2, V2 : Integer;
AColor : TRGBQuad;
// DstPtr:PByte; //PRGBQuad;
SrcPtr:PRGBQuad;
begin
NW:=NewWidth;
NH:=NewHeight;
SrcW:=SrcBmp.Width;
SrcH:=SrcBmp.Height;
if KeepRatio then KeepAspectRatio(SrcW,SrcH,NW,NH);
with DstBmp do
begin
SetSize(NW,NH);
end;
destwidth := NW-1;
destheight := NH-1;
fx := SrcW / NW;
fy := SrcH /NH;
fxstep := 0.99999*fx;
fystep := 0.99999*fy;
V2:=0;
//DstPtr:=DstBmp.RawImage.GetLineStart(0); //PRGBQuad(
For Y:=0 to destheight do
begin
V := V2;
V2 := round((fyStep+Y)*fy);
V:=Clamp(V,0,SrcBmp.Height-1);
U2:=0;
SrcPtr:=PRGBQuad(SrcBmp.RawImage.GetLineStart(V));
For X:=0 to destwidth do
begin
U:= U2;
U2 := round((fxStep+X)*fx);
U2:=Clamp(U2,0,SrcBmp.Width-1);
AColor:=(SrcPtr+U)^;
DstBmp.Canvas.Colors[x,y]:=RGBQuadToFPColor(AColor);
end;
end;
end;
procedure TForm1.FormShow(Sender: TObject);
begin
SrcBmp:=TBitmap.Create;
DstBmp:=TBitmap.Create;
end;
procedure TForm1.UpdateSrcView;
begin
with pnlSrc.Canvas do
begin
Lock;
Draw(0,0,SrcBmp);
Unlock;
end;
end;
procedure TForm1.UpdateDstView;
begin
with pnlDst.Canvas do
begin
Lock;
Draw(0,0,DstBmp);
Unlock;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
if OpenPictureDialog.Execute then
begin
SrcBmp.LoadFromFile(OpenPictureDialog.FileName);
UpdateSrcView;
end;
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
MyStretch(SrcBmp.Width*2, SrcBmp.Height*2,true);
UpdateDstView;
end;
procedure TForm1.FormClose(Sender: TObject; var CloseAction: TCloseAction);
begin
SrcBmp.Free;
DstBmp.Free;
end;
end. |
Partager