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 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
| {----------------------------------------------------------------}
{ Redimensionnement & filtre anti-aliasing - SubØ - 08/03/05 }
{ Migration pour Delphi2005 Dot.Net - sur_uix - 12/04/05 }
{----------------------------------------------------------------}
{ This function uses part of Graphics32 library }
{ Copyright © 2000-2003 Alex Denisov }
{----------------------------------------------------------------}
Unit Transforms;
Interface
Uses Windows, Math, Graphics, SysUtils, Classes,
System.Runtime.InteropServices;
Procedure Stretch32(Src, Dst: TBitmap);
Type
TFilter = (sfNearest, sfLinear, sfSpline, sfLanczos, sfMitchell);
Var
FullEdge: Boolean = True;
Filter: TFilter = sfLinear;
{----------------------------------------------------------------}
{ }Implementation{ }
{----------------------------------------------------------------}
Procedure Stretch32(Src, Dst: TBitmap);
Type
TBufferEntry = Record A, R, G, B: Integer; End;
TPointRec = Record Pos, Weight: Integer; End;
TCluster = Array Of TPointRec;
TMappingTable = Array Of TCluster;
Const
FilterWidths: Array[TFilter] Of Single = (1, 1, 2, 3, 2);
{--------------------------------------------------------------}
Function NearestFilter(Value: Single): Single;
Begin
Result := 0;
If (Value > -0.5) And (Value <= 0.5) Then
Result := 1;
End;
{--------------------------------------------------------------}
Function LinearFilter(Value: Single): Single;
Begin
Result := 0;
If (Value < -1) Then
Result := 0
Else If (Value < 0) Then
Result := 1 + Value
Else If (Value < 1) Then
Result := 1 - Value;
End;
{--------------------------------------------------------------}
Function SplineFilter(Value: Single): Single;
Var
tt: Single;
Begin
Result := 0;
Value := Abs(Value);
If (Value < 1) Then
Begin
tt := Sqr(Value);
Result := 0.5 * tt * Value - tt + 2 / 3;
End
Else If (Value < 2) Then
Begin
Value := 2 - Value;
Result := 1 / 6 * Sqr(Value) * Value;
End;
End;
{--------------------------------------------------------------}
Function LanczosFilter(Value: Single): Single;
Function Sinc(Value: Single): Single;
Begin
Result := 1;
Value := Value * Pi;
If (Value <> 0) Then
Result := Sin(Value) / Value;
End;
Begin
Result := 0;
Value := Abs(Value);
If (Value < 3) Then
Result := Sinc(Value) * Sinc(Value / 3);
End;
{--------------------------------------------------------------}
Function MitchellFilter(Value: Single): Single;
Var
tt, ttt: Single;
Begin
Result := 0;
Value := Abs(Value);
tt := Sqr(Value);
ttt := tt * Value;
If (Value < 1) Then
Result := (7 * ttt + -12 * tt + 16 / 3) / 6
Else If (Value < 2) Then
Result := (-7 / 3 * ttt + 12 * tt - 20 * Value + 32 / 3) / 6;
End;
{--------------------------------------------------------------}
Function BuildMappingTable(DstLo, DstHi, SrcLo, SrcHi: Integer;
Filter: TFilter = sfNearest): TMappingTable;
Var
SrcW, DstW: Integer;
I, J, K, Left, Right, Count, Weight: Integer;
Scale, OldScale, FilterWidth, Center: Single;
Begin
Result := Nil;
SrcW := SrcHi - SrcLo;
DstW := DstHi - DstLo;
If (SrcW <= 1) Or (DstW <= 0) Then Exit;
SetLength(Result, DstW);
If (SrcW = 1) Then
Begin
For I := 0 To DstW - 1 Do
Begin
SetLength(Result[I], 1);
Result[I][0].Pos := 0;
Result[I][0].Weight := 256;
End;
Exit;
End;
Scale := (DstW - 1) / (SrcW - 1);
If (FullEdge) Then Scale := DstW / SrcW;
FilterWidth := FilterWidths[Filter];
K := 0;
If (Scale = 0) Then
Begin
Assert(Length(Result) = 1);
SetLength(Result[0], 1);
Result[0][0].Pos := (SrcLo + SrcHi) Div 2;
Result[0][0].Weight := 256;
Exit;
End;
If (Scale < 1) Then
Begin
OldScale := Scale;
Scale := 1 / Scale;
FilterWidth := FilterWidth * Scale;
For I := 0 To DstW - 1 Do
Begin
Center := SrcLo + I * Scale;
If (FullEdge) Then Center := SrcLo - 0.5 + (I + 0.5) * Scale;
Left := Floor(Center - FilterWidth);
Right := Ceil(Center + FilterWidth);
Count := -256;
For J := Left To Right Do
Begin
Case Filter Of
sfNearest: Weight := Round(256 * NearestFilter((Center - J) * OldScale) * OldScale);
sfLinear: Weight := Round(256 * LinearFilter((Center - J) * OldScale) * OldScale);
sfSpline: Weight := Round(256 * SplineFilter((Center - J) * OldScale) * OldScale);
sfLanczos: Weight := Round(256 * LanczosFilter((Center - J) * OldScale) * OldScale);
sfMitchell: Weight := Round(256 * MitchellFilter((Center - J) * OldScale) * OldScale);
End;
If (Weight <> 0) Then
Begin
Inc(Count, Weight);
K := Length(Result[I]);
SetLength(Result[I], K + 1);
Result[I][K].Pos := Min(Max(J, SrcLo), SrcHi - 1);
Result[I][K].Weight := Weight;
End;
End;
If (Length(Result[I]) = 0) Then
Begin
SetLength(Result[I], 1);
Result[I][0].Pos := Floor(Center);
Result[I][0].Weight := 256;
End
Else If (Count <> 0) Then
Dec(Result[I][K Div 2].Weight, Count);
End;
Exit;
End;
Scale := 1 / Scale;
For I := 0 To DstW - 1 Do
Begin
Center := SrcLo + I * Scale;
If (FullEdge) Then Center := SrcLo - 0.5 + (I + 0.5) * Scale;
Left := Floor(Center - FilterWidth);
Right := Ceil(Center + FilterWidth);
Count := -256;
For J := Left To Right Do
Begin
Case Filter Of
sfNearest: Weight := Round(256 * NearestFilter(Center - J));
sfLinear: Weight := Round(256 * LinearFilter(Center - J));
sfSpline: Weight := Round(256 * SplineFilter(Center - J));
sfLanczos: Weight := Round(256 * LanczosFilter(Center - J));
sfMitchell: Weight := Round(256 * MitchellFilter(Center - J));
End;
If (Weight <> 0) Then
Begin
Inc(Count, Weight);
K := Length(Result[I]);
SetLength(Result[I], k + 1);
Result[I][K].Pos := Min(Max(j, SrcLo), SrcHi - 1);
Result[I][K].Weight := Weight;
End;
End;
If (Count <> 0) Then
Dec(Result[I][K Div 2].Weight, Count);
End;
End;
{----------------------------------------------------------------}
Var
DstRect, SrcRect: TRect;
DstW, DstH, SrcW, SrcH: Single;
MapX, MapY: TMappingTable;
ClusterX, ClusterY: TCluster;
HorzBuffer: Array Of TBufferEntry;
MapXLoPos, MapXHiPos: Integer;
I, J, X, Y, Ca, Cr, Cg, Cb,itmp: Integer;
SrcLine, DstLine: IntPtr;//PIntegerArray;
Spf: TPixelFormat;
Begin
Spf := Src.PixelFormat;
Src.PixelFormat := pf32bit;
SrcRect := Src.Canvas.ClipRect;
SrcW := SrcRect.Right - SrcRect.Left;
SrcH := SrcRect.Bottom - SrcRect.Top;
Dst.PixelFormat := pf32bit;
DstRect := Dst.Canvas.ClipRect;
DstW := DstRect.Right - DstRect.Left;
DstH := DstRect.Bottom - DstRect.Top;
ClusterX := Nil;
ClusterY := Nil;
MapX := BuildMappingTable(DstRect.Left, DstRect.Right, SrcRect.Left, SrcRect.Right, Filter);
MapY := BuildMappingTable(DstRect.Top, DstRect.Bottom, SrcRect.Top, SrcRect.Bottom, Filter);
If (MapX = Nil) Or (MapY = Nil) Then Exit;
MapXLoPos := MapX[0][0].Pos;
MapXHiPos := MapX[Round(DstW - 1)][High(MapX[Round(DstW - 1)])].Pos;
SetLength(HorzBuffer, MapXHiPos - MapXLoPos + 1);
For J := DstRect.Top To DstRect.Bottom - 1 Do
Begin
ClusterY := MapY[J - DstRect.Top];
For X := MapXLoPos To MapXHiPos Do
Begin
HorzBuffer[X - MapXLoPos].A := 0;
HorzBuffer[X - MapXLoPos].R := 0;
HorzBuffer[X - MapXLoPos].G := 0;
HorzBuffer[X - MapXLoPos].B := 0;
For Y := 0 To Length(ClusterY) - 1 Do
Begin
SrcLine := Src.ScanLine[ClusterY[Y].Pos];
itmp := Marshal.ReadInt32(SrcLine,X*4);
ca := (itmp And $FF000000) Shr 24;
cr := (itmp And $00FF0000) Shr 16;
cg := (itmp And $0000FF00) Shr 8;
cb := (itmp And $000000FF);
Inc(HorzBuffer[X - MapXLoPos].A, ClusterY[Y].Weight * ca);
Inc(HorzBuffer[X - MapXLoPos].R, ClusterY[Y].Weight * cr);
Inc(HorzBuffer[X - MapXLoPos].G, ClusterY[Y].Weight * cg);
Inc(HorzBuffer[X - MapXLoPos].B, ClusterY[Y].Weight * cb);
End;
End;
DstLine := Dst.ScanLine[J];
For I := DstRect.Left To DstRect.Right - 1 Do
Begin
ClusterX := MapX[I - DstRect.Left];
Ca := 0;
Cr := 0;
Cg := 0;
Cb := 0;
For X := 0 To Length(ClusterX) - 1 Do
Begin
Inc(Ca, ClusterX[X].Weight * HorzBuffer[ClusterX[X].Pos - MapXLoPos].A);
Inc(Cr, ClusterX[X].Weight * HorzBuffer[ClusterX[X].Pos - MapXLoPos].R);
Inc(Cg, ClusterX[X].Weight * HorzBuffer[ClusterX[X].Pos - MapXLoPos].G);
Inc(Cb, ClusterX[X].Weight * HorzBuffer[ClusterX[X].Pos - MapXLoPos].B);
End;
Ca := Max(Min(Ca, $FF0000), 0) And $FF0000;
Cr := Max(Min(Cr, $FF0000), 0) And $FF0000;
Cg := Max(Min(Cg, $FF0000), 0) And $FF0000;
Cb := Max(Min(Cb, $FF0000), 0) And $FF0000;
Marshal.WriteInt32(DstLine,I*4,((Ca Shl 8) Or (Cr) Or (Cg Shr 8) Or (Cb Shr 16)));
End;
End;
MapX := Nil;
MapY := Nil;
Src.PixelFormat := Spf;
End;
{----------------------------------------------------------------}
End. |
Partager