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
|
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;
//------------------------------------------------------------------------------
// Redimensionne le bitmap vers un autre bitmap sans lissage
//------------------------------------------------------------------------------
Procedure TBZBitmap.Stretch(DestBmp:TBZBitmap;Const NewWidth:Integer;Const NewHeight:Integer;Const KeepRatio:Boolean=false);
Var
TmpBmp : TBZBitmap;
NH,NW,SrcW,SrcH,X,Y : Integer;
//SrcWMax,SrcHMax: Integer;
destwidth,destheight : Integer;
fx,fy,fxstep,fystep :Single;
U, V, U2, V2 : Integer;
AColor : TBZColor;
SrcPtr, DstPtr: TBZColorPointer;
begin
SrcW:= Width;
SrcH:= Height;
// SrcWMax:= FMaxPixelWidth; //SrcW-1;
// SrcHMax:= FMaxPixelHeight; //SrcH-1;
NW:=NewWidth;
NH:=NewHeight;
TmpBmp:=TBZBitmap.Create(SrcW,SrcH);
TmpBmp.Assign(Self);
if KeepRatio then KeepAspectRatio(SrcW,SrcH,NW,NH);
with DestBmp do
begin
SetSize(NW,NH);
Clear(ccolBlack);
end;
destwidth := NW-1;
destheight := NH-1;
fx := SrcW / NW;
fy := SrcH /NH;
fxstep := 0.99999*fx;
fystep := 0.99999*fy;
V2:=0;
DstPtr:=DestBmp.ScanLine(0);
For Y:=0 to destheight do
begin
V := V2;
V2 := round((fyStep+Y)*fy);
V:=Clamp(V,0,FMaxPixelHeight);
U2:=0;
SrcPtr:=TmpBmp.ScanLine(V);
For X:=0 to destwidth do
begin
U:= U2;
U2 := round((fxStep+X)*fx);
U2:=Clamp(U2,0,FMaxPixelWidth);
AColor:=TBZColorPointer(SrcPtr+U)^;
DstPtr^:=AColor;
Inc(DstPtr);
end;
end;
TmpBmp.Free;
end;
//------------------------------------------------------------------------------
// Redimensionne le bitmap vers un autre bitmap avec lissage
//------------------------------------------------------------------------------
Procedure TBZBitmap.StretchSmooth(DestBmp:TBZBitmap;Const NewWidth:Integer;Const NewHeight:Integer;Const KeepRatio:Boolean=false);
Var
TmpBmp : TBZBitmap;
NW,NH,SrcW,SrcH,X,Y : Integer;
//, SrcWMax,SrcHMax: Integer;
destwidth,destheight : Integer;
fx,fy : single; //factors
fxstep,fystep : single;
istart,iend, jstart, jend : Integer;
U, V, U2, V2 : Single;
I,J, C: Integer;
R,G,B,A :Integer;
sR,sG,sB,sA : byte;
AColor : TBZColor;
SrcPtr, DstPtr: TBZColorPointer;
begin
SrcW:= Width;
SrcH:= Height;
// SrcWMax:= SrcW-1;
// SrcHMax:= SrcH-1;
NW:=NewWidth;
NH:=NewHeight;
TmpBmp:=TBZBitmap.Create(SrcW,SrcH);
TmpBmp.Assign(self);
if KeepRatio then KeepAspectRatio(SrcW,SrcH,NW,NH);
with DestBmp do
begin
SetSize(NW,NH);
Clear(ccolBlack);
end;
destwidth := NW-1;
destheight := NH-1;
fx := SrcW / NW;
fy := SrcH /NH;
fxstep := 0.99999*fx;
fystep := 0.99999*fy;
V2:=0;
DstPtr:=DestBmp.ScanLine(0);
For Y:=0 to destheight do
begin
V := V2;
jstart:=round(V);
jstart:=Clamp(jstart,0,FMaxPixelHeight);
V2 := ((fyStep+Y)*fy);
jend:=round(V2);
jend:=Clamp(jend,0,FMaxPixelHeight);
U2:=0;
For X:=0 to destwidth do
begin
U:= U2;
istart:=round(U);
istart:=Clamp(istart,0,FMaxPixelWidth);
U2 := ((fxStep+X)*fx);
iend:=round(U2);
iend:=Clamp(iend,0,FMaxPixelWidth);
R:=0;
G:=0;
B:=0;
A:=0;
C:=0;
// Lissage depuis les données du fichier sources
SrcPtr:=TmpBmp.ScanLine(jstart-1);
for j := jstart to jend do //vertical
begin
for i := istart to iend do //horizontal
begin
AColor:=TBZColorPointer(SrcPtr+I)^;
R:=R+AColor.Red;
G:=G+AColor.Green;
B:=B+AColor.Blue;
A:=A+AColor.Alpha;
Inc(C);
end;
Inc(SrcPtr,SrcW);
end;
if C=0 then C:=1;
sR := ClampByte(round(R/C));;
sG := ClampByte(round(G/C));
sB := ClampByte(round(B/C));
sA := ClampByte(round(A/C));
DstPtr^:= BZColor(sR,sG,sB,sA);
Inc(DstPtr);
end;
end;
TmpBmp.Free;
end; |
Partager