| 1 |
unit Img32.Resamplers; |
| 2 |
|
| 3 |
(******************************************************************************* |
| 4 |
* Author : Angus Johnson * |
| 5 |
* Version : 4.8 * |
| 6 |
* Date : 10 January 2025 * |
| 7 |
* Website : http://www.angusj.com * |
| 8 |
* Copyright : Angus Johnson 2019-2025 * |
| 9 |
* Purpose : For image transformations (scaling, rotating etc.) * |
| 10 |
* License : http://www.boost.org/LICENSE_1_0.txt * |
| 11 |
*******************************************************************************) |
| 12 |
|
| 13 |
interface |
| 14 |
|
| 15 |
{$I Img32.inc} |
| 16 |
|
| 17 |
uses |
| 18 |
SysUtils, Classes, Math, Img32; |
| 19 |
|
| 20 |
// Premultiplies the alpha channel into the color channels from pSrc and stores |
| 21 |
// it into pDst. pSrc and pDst can be the same pointer. |
| 22 |
procedure PremultiplyAlpha(pSrc, pDst: PARGB; count: nativeint); overload; |
| 23 |
|
| 24 |
// BoxDownSampling: As the name implies, is only intended for image |
| 25 |
// down-sampling (ie shrinking images) where it performs a little better |
| 26 |
// than other resamplers which tend toward pixelation. Nevertheless, this |
| 27 |
// routine is inferior to other resamplers when performing other |
| 28 |
// types of transformations (ie when enlarging, rotating, and skewing images), |
| 29 |
// so BoxDownSampling should not be used as a general purpose resampler. |
| 30 |
procedure BoxDownSampling(Image: TImage32; scale: double); overload; |
| 31 |
procedure BoxDownSampling(Image: TImage32; scaleX, scaleY: double); overload; |
| 32 |
procedure BoxDownSampling(Image: TImage32; newWidth, newHeight: Integer); overload; |
| 33 |
procedure BoxDownSampling(Image, TargetImage: TImage32; scale: double); overload; |
| 34 |
procedure BoxDownSampling(Image, TargetImage: TImage32; scaleX, scaleY: double); overload; |
| 35 |
procedure BoxDownSampling(Image, TargetImage: TImage32; newWidth, newHeight: Integer); overload; |
| 36 |
|
| 37 |
procedure NearestNeighborResize(Image: TImage32; newWidth, newHeight: Integer); overload; |
| 38 |
procedure NearestNeighborResize(Image, TargetImage: TImage32; newWidth, newHeight: Integer); overload; |
| 39 |
procedure ResamplerResize(Image: TImage32; newWidth, newHeight: Integer); overload; |
| 40 |
procedure ResamplerResize(Image, TargetImage: TImage32; newWidth, newHeight: Integer); overload; |
| 41 |
|
| 42 |
// The following general purpose resamplers are registered below: |
| 43 |
// function NearestResampler(img: TImage32; x, y: double): TColor32; |
| 44 |
// function BilinearResample(img: TImage32; x, y: double): TColor32; |
| 45 |
// function BicubicResample (img: TImage32; x, y: double): TColor32; |
| 46 |
// function WeightedBilinear(img: TImage32; x, y: double): TColor32; |
| 47 |
|
| 48 |
implementation |
| 49 |
|
| 50 |
uses |
| 51 |
Img32.Transform; |
| 52 |
|
| 53 |
var |
| 54 |
sinWeighted: array [0..255] of Cardinal; |
| 55 |
|
| 56 |
//------------------------------------------------------------------------------ |
| 57 |
// NearestNeighbor resampler |
| 58 |
//------------------------------------------------------------------------------ |
| 59 |
|
| 60 |
function NearestResampler(img: TImage32; x, y: double): TColor32; |
| 61 |
var |
| 62 |
xi, yi: integer; |
| 63 |
begin |
| 64 |
xi := Round(x); yi := Round(y); |
| 65 |
if (xi < 0) or (yi < 0) or (xi >= img.Width) or (yi >= img.Height) then |
| 66 |
Result := clNone32 else |
| 67 |
Result := img.Pixels[xi + yi * img.Width]; |
| 68 |
end; |
| 69 |
|
| 70 |
//------------------------------------------------------------------------------ |
| 71 |
// BiLinear resampler |
| 72 |
//------------------------------------------------------------------------------ |
| 73 |
|
| 74 |
function BilinearResample(img: TImage32; x, y: double): TColor32; |
| 75 |
var |
| 76 |
iw, ih: integer; |
| 77 |
xx, yy, xR, yB: integer; |
| 78 |
weight: integer; |
| 79 |
pixels: TArrayOfColor32; |
| 80 |
weightedColor: TWeightedColor; |
| 81 |
xf, yf: double; |
| 82 |
begin |
| 83 |
iw := img.Width; |
| 84 |
ih := img.Height; |
| 85 |
pixels := img.Pixels; |
| 86 |
|
| 87 |
if (x < 0) then |
| 88 |
begin |
| 89 |
if (x < -0.5) then |
| 90 |
begin |
| 91 |
xf := -x; |
| 92 |
end else |
| 93 |
begin |
| 94 |
x := 0; |
| 95 |
xf := 0; |
| 96 |
end; |
| 97 |
xx := 0; |
| 98 |
xR := 0; |
| 99 |
end else |
| 100 |
begin |
| 101 |
xf := 1-frac(x); |
| 102 |
if x >= iw -1 then |
| 103 |
begin |
| 104 |
xx := iw -1; |
| 105 |
xR := xx; |
| 106 |
end else |
| 107 |
begin |
| 108 |
xx := Trunc(x); |
| 109 |
xR := xx +1; |
| 110 |
end; |
| 111 |
end; |
| 112 |
|
| 113 |
if (y < 0) then |
| 114 |
begin |
| 115 |
if (y < -0.5) then |
| 116 |
begin |
| 117 |
yf := -y; |
| 118 |
end else |
| 119 |
begin |
| 120 |
y := 0; |
| 121 |
yf := 0; |
| 122 |
end; |
| 123 |
yy := 0; |
| 124 |
yB := 0; |
| 125 |
end else |
| 126 |
begin |
| 127 |
yf := 1-frac(y); |
| 128 |
if y >= ih -1 then |
| 129 |
begin |
| 130 |
yy := ih -1; |
| 131 |
yB := yy; |
| 132 |
end else |
| 133 |
begin |
| 134 |
yy := Trunc(y); |
| 135 |
yB := yy +1; |
| 136 |
end; |
| 137 |
end; |
| 138 |
|
| 139 |
weightedColor.Reset; |
| 140 |
|
| 141 |
weight := Round(xf * yf * 255); //top-left |
| 142 |
if weight > 0 then |
| 143 |
begin |
| 144 |
if (x < 0) or (y < 0) then |
| 145 |
weightedColor.AddWeight(weight) else |
| 146 |
weightedColor.Add(pixels[xx + yy * iw], weight); |
| 147 |
end; |
| 148 |
|
| 149 |
weight := Round((1-xf) * yf * 255); //top-right |
| 150 |
if weight > 0 then |
| 151 |
begin |
| 152 |
if (x > iw - 0.5) or (y < 0) then |
| 153 |
weightedColor.AddWeight(weight) else |
| 154 |
weightedColor.Add(pixels[xR + yy * iw], weight); |
| 155 |
end; |
| 156 |
|
| 157 |
weight := Round(xf * (1-yf) * 255); //bottom-left |
| 158 |
if weight > 0 then |
| 159 |
begin |
| 160 |
if (x < 0) or (y > ih - 0.5) then |
| 161 |
weightedColor.AddWeight(weight) else |
| 162 |
weightedColor.Add(pixels[xx + yB * iw], weight); |
| 163 |
end; |
| 164 |
|
| 165 |
weight := Round((1-xf) * (1-yf) * 255); //bottom-right |
| 166 |
if weight > 0 then |
| 167 |
begin |
| 168 |
if (x > iw - 0.5) or (y > ih - 0.5) then |
| 169 |
weightedColor.AddWeight(weight) else |
| 170 |
weightedColor.Add(pixels[xR + yB * iw], weight); |
| 171 |
end; |
| 172 |
Result := weightedColor.Color; |
| 173 |
end; |
| 174 |
//------------------------------------------------------------------------------ |
| 175 |
|
| 176 |
// WeightedBilinearResample: A modified bilinear resampler that's |
| 177 |
// less blurry but also a little more pixelated. |
| 178 |
function WeightedBilinearResample(img: TImage32; x, y: double): TColor32; |
| 179 |
var |
| 180 |
iw, ih: integer; |
| 181 |
xx, yy, xR, yB: integer; |
| 182 |
weight: integer; |
| 183 |
pixels: TArrayOfColor32; |
| 184 |
weightedColor: TWeightedColor; |
| 185 |
xf, yf: double; |
| 186 |
begin |
| 187 |
iw := img.Width; |
| 188 |
ih := img.Height; |
| 189 |
pixels := img.Pixels; |
| 190 |
|
| 191 |
if (x < 0) then |
| 192 |
begin |
| 193 |
if (x < -0.5) then |
| 194 |
begin |
| 195 |
xf := -x; |
| 196 |
end else |
| 197 |
begin |
| 198 |
x := 0; |
| 199 |
xf := 0; |
| 200 |
end; |
| 201 |
xx := 0; |
| 202 |
xR := 0; |
| 203 |
end else |
| 204 |
begin |
| 205 |
xf := 1-frac(x); |
| 206 |
if x >= iw -1 then |
| 207 |
begin |
| 208 |
xx := iw -1; |
| 209 |
xR := xx; |
| 210 |
end else |
| 211 |
begin |
| 212 |
xx := Trunc(x); |
| 213 |
xR := xx +1; |
| 214 |
end; |
| 215 |
end; |
| 216 |
|
| 217 |
if (y < 0) then |
| 218 |
begin |
| 219 |
if (y < -0.5) then |
| 220 |
begin |
| 221 |
yf := -y; |
| 222 |
end else |
| 223 |
begin |
| 224 |
y := 0; |
| 225 |
yf := 0; |
| 226 |
end; |
| 227 |
yy := 0; |
| 228 |
yB := 0; |
| 229 |
end else |
| 230 |
begin |
| 231 |
yf := 1-frac(y); |
| 232 |
if y >= ih -1 then |
| 233 |
begin |
| 234 |
yy := ih -1; |
| 235 |
yB := yy; |
| 236 |
end else |
| 237 |
begin |
| 238 |
yy := Trunc(y); |
| 239 |
yB := yy +1; |
| 240 |
end; |
| 241 |
end; |
| 242 |
|
| 243 |
weightedColor.Reset; |
| 244 |
|
| 245 |
weight := sinWeighted[Round(xf * yf * 255)]; //top-left |
| 246 |
if weight > 0 then |
| 247 |
begin |
| 248 |
if (x < 0) or (y < 0) then |
| 249 |
weightedColor.AddWeight(weight) else |
| 250 |
weightedColor.Add(pixels[xx + yy * iw], weight); |
| 251 |
end; |
| 252 |
|
| 253 |
weight := sinWeighted[Round((1-xf) * yf * 255)]; //top-right |
| 254 |
if weight > 0 then |
| 255 |
begin |
| 256 |
if (x > iw - 0.5) or (y < 0) then |
| 257 |
weightedColor.AddWeight(weight) else |
| 258 |
weightedColor.Add(pixels[xR + yy * iw], weight); |
| 259 |
end; |
| 260 |
|
| 261 |
weight := sinWeighted[Round(xf * (1-yf) * 255)]; //bottom-left |
| 262 |
if weight > 0 then |
| 263 |
begin |
| 264 |
if (x < 0) or (y > ih - 0.5) then |
| 265 |
weightedColor.AddWeight(weight) else |
| 266 |
weightedColor.Add(pixels[xx + yB * iw], weight); |
| 267 |
end; |
| 268 |
|
| 269 |
weight := sinWeighted[Round((1-xf) * (1-yf) * 255)]; //bottom-right |
| 270 |
if weight > 0 then |
| 271 |
begin |
| 272 |
if (x > iw - 0.5) or (y > ih - 0.5) then |
| 273 |
weightedColor.AddWeight(weight) else |
| 274 |
weightedColor.Add(pixels[xR + yB * iw], weight); |
| 275 |
end; |
| 276 |
Result := weightedColor.Color; |
| 277 |
end; |
| 278 |
|
| 279 |
//------------------------------------------------------------------------------ |
| 280 |
// BiCubic resampler |
| 281 |
//------------------------------------------------------------------------------ |
| 282 |
|
| 283 |
type |
| 284 |
TBiCubicEdgeAdjust = (eaCenterFill, |
| 285 |
eaPreStart, eaStart, eaPostStart, eaEnd, eaPostEnd); |
| 286 |
|
| 287 |
var |
| 288 |
byteFrac: array [0..255] of double; |
| 289 |
byteFracSq: array [0..255] of double; |
| 290 |
byteFracCubed: array [0..255] of double; |
| 291 |
|
| 292 |
//------------------------------------------------------------------------------ |
| 293 |
|
| 294 |
function CubicInterpolate(aclr: PColor32; |
| 295 |
t: Byte; bce: TBiCubicEdgeAdjust): TColor32; |
| 296 |
var |
| 297 |
a,b,c,d: PARGB; |
| 298 |
q: TARGB; |
| 299 |
aa, bb, m0, m1: double; |
| 300 |
t1, t2, t3: double; |
| 301 |
res: TARGB absolute Result; |
| 302 |
const |
| 303 |
clTrans: TColor32 = clNone32; |
| 304 |
begin |
| 305 |
case bce of |
| 306 |
eaPreStart: |
| 307 |
begin |
| 308 |
a := @clTrans; |
| 309 |
b := @clTrans; |
| 310 |
c := PARGB(aclr); |
| 311 |
d := c; |
| 312 |
end; |
| 313 |
eaStart: |
| 314 |
begin |
| 315 |
Result := aclr^; |
| 316 |
Exit; |
| 317 |
end; |
| 318 |
eaPostStart: |
| 319 |
begin |
| 320 |
a := PARGB(aclr); |
| 321 |
b := a; |
| 322 |
Inc(aclr); |
| 323 |
c := PARGB(aclr); |
| 324 |
d := c; |
| 325 |
end; |
| 326 |
eaEnd: |
| 327 |
begin |
| 328 |
Inc(aclr); |
| 329 |
Result := aclr^; |
| 330 |
Exit; |
| 331 |
end; |
| 332 |
eaPostEnd: |
| 333 |
begin |
| 334 |
Inc(aclr); |
| 335 |
a := PARGB(aclr); |
| 336 |
b := a; |
| 337 |
c := @clTrans; |
| 338 |
d := @clTrans; |
| 339 |
end; |
| 340 |
else |
| 341 |
begin |
| 342 |
a := PARGB(aclr); |
| 343 |
Inc(aclr); |
| 344 |
b := PARGB(aclr); |
| 345 |
Inc(aclr); |
| 346 |
c := PARGB(aclr); |
| 347 |
Inc(aclr); |
| 348 |
d := PARGB(aclr); |
| 349 |
end; |
| 350 |
end; |
| 351 |
|
| 352 |
if (b.A = 0) and (c.A = 0) then |
| 353 |
begin |
| 354 |
result := clNone32; |
| 355 |
Exit; |
| 356 |
end |
| 357 |
else if (b = c) then |
| 358 |
begin |
| 359 |
result := b.Color; |
| 360 |
Exit; |
| 361 |
end |
| 362 |
else if b.A = 0 then |
| 363 |
begin |
| 364 |
// ignore differences between b & c's color channels |
| 365 |
q := c^; |
| 366 |
q.A := 0; |
| 367 |
b := @q; |
| 368 |
end; |
| 369 |
if c.A = 0 then |
| 370 |
begin |
| 371 |
// ignore differences between b & c's color channels |
| 372 |
q := b^; |
| 373 |
q.A := 0; |
| 374 |
c := @q; |
| 375 |
end; |
| 376 |
|
| 377 |
t1 := byteFrac[t]; |
| 378 |
t2 := byteFracSq[t]; |
| 379 |
t3 := byteFracCubed[t]; |
| 380 |
|
| 381 |
// find piecewise bicubic interpolation between pixel_b and pixel_c |
| 382 |
// at point 't' (as byte div 255) ... |
| 383 |
// given parametric equation aa(t^3) + bb(t^2) + cc(t)+ dd = 0 |
| 384 |
// where t(0) = pixel_b and t(1) = pixel_c |
| 385 |
// let m1 = slope at pixel_b (using slope of pixel_c - pixel_a) |
| 386 |
// let m2 = slope at pixel_c (using slope of pixel_d - pixel_b) |
| 387 |
// then t(0) = aa(0^3) + bb(0^2) + cc(0) + dd = dd |
| 388 |
// then t(1) = aa(1^3) + bb(1^2) + cc(1) + dd = aa + bb + cc + dd |
| 389 |
// differentiating parametic equation at t'(0) and t'(1) ... |
| 390 |
// t'(0) = m0 = 3*aa(0^2) + 2*bb(0) + cc = cc |
| 391 |
// t'(1) = m1 = 3*aa(1^2) + 2*bb(1) + cc = 3*aa + 2*bb + cc |
| 392 |
// t(0) = dd ::EQ1 |
| 393 |
// t(1) = aa+bb+cc+dd ::EQ2 |
| 394 |
// t'(0) = cc ::EQ3 |
| 395 |
// t'(1) = 3*aa + 2*bb + cc ::EQ4 |
| 396 |
// solving simultaneous equations |
| 397 |
// aa = 2*t(0) -2*t(1) +t'(0) +t'(1) |
| 398 |
// bb = 3*t(1) -3*t(0) -2*t'(0) -t'(1) |
| 399 |
// cc = m0 |
| 400 |
// dd = t(0) |
| 401 |
|
| 402 |
m0 {aka t'(0)} := (c.A - a.A) /2; |
| 403 |
m1 {aka t'(1)} := (d.A - b.A) /2; |
| 404 |
aa := 2*b.A - 2*c.A + m0 + m1; |
| 405 |
bb := 3*c.A -3*b.A -2*m0 - m1; |
| 406 |
Res.A := ClampByte(aa*t3 + bb*t2 + m0*t1 + b.A); |
| 407 |
|
| 408 |
m0 := (c.R - a.R) /2; |
| 409 |
m1 := (d.R - b.R) /2; |
| 410 |
aa := 2*b.R - 2*c.R + m0 + m1; |
| 411 |
bb := 3*c.R -3*b.R -2*m0 - m1; |
| 412 |
Res.R := ClampByte(aa*t3 + bb*t2 + m0*t1 + b.R); |
| 413 |
|
| 414 |
m0 := (c.G - a.G) /2; |
| 415 |
m1 := (d.G - b.G) /2; |
| 416 |
aa := 2*b.G - 2*c.G + m0 + m1; |
| 417 |
bb := 3*c.G -3*b.G -2*m0 - m1; |
| 418 |
Res.G := ClampByte(aa*t3 + bb*t2 + m0*t1 + b.G); |
| 419 |
|
| 420 |
m0 := (c.B - a.B) /2; |
| 421 |
m1 := (d.B - b.B) /2; |
| 422 |
aa := 2*b.B - 2*c.B + m0 + m1; |
| 423 |
bb := 3*c.B -3*b.B -2*m0 - m1; |
| 424 |
Res.B := ClampByte(aa*t3 + bb*t2 + m0*t1 + b.B); |
| 425 |
end; |
| 426 |
//------------------------------------------------------------------------------ |
| 427 |
|
| 428 |
function BicubicResample(img: TImage32; x, y: double): TColor32; |
| 429 |
var |
| 430 |
i, pi, iw, ih, last: Integer; |
| 431 |
c: array[0..3] of TColor32; |
| 432 |
xFrac, yFrac: byte; |
| 433 |
bceX, bceY: TBiCubicEdgeAdjust; |
| 434 |
begin |
| 435 |
iw := img.Width; |
| 436 |
ih := img.Height; |
| 437 |
last := iw * ih -1; |
| 438 |
|
| 439 |
if x < 1 then |
| 440 |
begin |
| 441 |
if x < -0.5 then |
| 442 |
begin |
| 443 |
xFrac := Round((1+x) *255); |
| 444 |
bceX := eaPreStart; |
| 445 |
end |
| 446 |
else if (x < 0) or |
| 447 |
((iw = 1) and (x < 0.5)) then |
| 448 |
begin |
| 449 |
x := 0; |
| 450 |
xFrac := 0; |
| 451 |
bceX := eaStart; |
| 452 |
end |
| 453 |
else if (iw = 1) and (x > 0.5) then |
| 454 |
begin |
| 455 |
// the following is a workaround to avoid the increment in eaPostEnd |
| 456 |
bceX := eaPreStart; // ie anti-aliase but without increment |
| 457 |
xFrac := Round((1-x) *127); // reversed because 'end' not 'start' |
| 458 |
end else |
| 459 |
begin |
| 460 |
xFrac := Round(frac(x) *255); |
| 461 |
bceX := eaPostStart; |
| 462 |
end; |
| 463 |
end else |
| 464 |
begin |
| 465 |
xFrac := Round(frac(x) *255); |
| 466 |
if x > iw - 1 then |
| 467 |
begin |
| 468 |
if x > iw - 0.5 then bceX := eaPostEnd |
| 469 |
else bceX := eaEnd |
| 470 |
end |
| 471 |
else |
| 472 |
bceX := eaCenterFill; |
| 473 |
end; |
| 474 |
|
| 475 |
if y < 1 then |
| 476 |
begin |
| 477 |
if y < -0.5 then |
| 478 |
begin |
| 479 |
yFrac := Round((1+y) *255); |
| 480 |
bceY := eaPreStart; |
| 481 |
end |
| 482 |
else if (y < 0) or |
| 483 |
((ih = 1) and (y < 0.5)) then |
| 484 |
begin |
| 485 |
y := 0; |
| 486 |
yFrac := 0; |
| 487 |
bceY := eaStart; |
| 488 |
end |
| 489 |
else if (ih = 1) and (y > 0.5) then |
| 490 |
begin |
| 491 |
// the following is a workaround to avoid the increment in eaPostEnd |
| 492 |
bceY := eaPreStart; // ie anti-aliase but without increment |
| 493 |
yFrac := Round((1-y) *127); // reversed because 'end' not 'start' |
| 494 |
end else |
| 495 |
begin |
| 496 |
yFrac := Round(frac(y) *255); |
| 497 |
bceY := eaPostStart; |
| 498 |
end; |
| 499 |
end else |
| 500 |
begin |
| 501 |
yFrac := Round(frac(y) *255); |
| 502 |
if y > ih - 1 then |
| 503 |
begin |
| 504 |
if y > ih - 0.5 then bceY := eaPostEnd |
| 505 |
else bceY := eaEnd |
| 506 |
end |
| 507 |
else |
| 508 |
bceY := eaCenterFill; |
| 509 |
end; |
| 510 |
|
| 511 |
x := Max(0, Min(iw -1, x -1)); |
| 512 |
y := Max(0, Min(ih -1, y -1)); |
| 513 |
pi := Trunc(y) * iw + Trunc(x); |
| 514 |
|
| 515 |
for i := 0 to 3 do |
| 516 |
begin |
| 517 |
c[i] := CubicInterpolate(@img.Pixels[pi], xFrac, bceX); |
| 518 |
inc(pi, iw); |
| 519 |
if pi > last then break; |
| 520 |
end; |
| 521 |
Result := CubicInterpolate(@c[0], yFrac, bceY); |
| 522 |
end; |
| 523 |
|
| 524 |
//------------------------------------------------------------------------------ |
| 525 |
//------------------------------------------------------------------------------ |
| 526 |
|
| 527 |
{$RANGECHECKS OFF} // negative index usage for Delphi 7-2007 |
| 528 |
procedure PremultiplyAlpha(pSrc, pDst: PARGB; count: nativeint); |
| 529 |
var |
| 530 |
a: byte; |
| 531 |
tab: PByteArray; |
| 532 |
c: TColor32; |
| 533 |
s, d: PColor32Array; |
| 534 |
begin |
| 535 |
if count = 0 then exit; |
| 536 |
|
| 537 |
// Use negative index trick |
| 538 |
inc(pSrc, count); |
| 539 |
inc(pDst, count); |
| 540 |
count := -count; |
| 541 |
|
| 542 |
// This function is optmized with the assumption that if a pixel has a certain |
| 543 |
// alpha channel, then the propability that the following pixels have the same |
| 544 |
// alpha channel, is very high. |
| 545 |
|
| 546 |
c := PColor32Array(pSrc)[count]; |
| 547 |
a := c shr 24; |
| 548 |
while True do |
| 549 |
begin |
| 550 |
case a of |
| 551 |
0: // Special handling for 0 => color becomes black |
| 552 |
begin |
| 553 |
// Win32: Load stack variable into CPU register |
| 554 |
s := PColor32Array(pSrc); |
| 555 |
d := PColor32Array(pDst); |
| 556 |
while True do |
| 557 |
begin |
| 558 |
d[count] := 0; |
| 559 |
inc(count); |
| 560 |
if count = 0 then exit; |
| 561 |
c := s[count]; |
| 562 |
a := c shr 24; |
| 563 |
if a <> 0 then break; |
| 564 |
end; |
| 565 |
end; |
| 566 |
|
| 567 |
255: // Special handling for 255 => no color change |
| 568 |
begin |
| 569 |
// Win32: Load stack variable into CPU register |
| 570 |
s := PColor32Array(pSrc); |
| 571 |
d := PColor32Array(pDst); |
| 572 |
if s = d then // if source=dest, we can skip writing to d |
| 573 |
begin |
| 574 |
while True do |
| 575 |
begin |
| 576 |
//d[count] := c; // skip the write |
| 577 |
inc(count); |
| 578 |
if count = 0 then exit; |
| 579 |
c := s[count]; |
| 580 |
a := c shr 24; |
| 581 |
if a <> 255 then break; |
| 582 |
end; |
| 583 |
end |
| 584 |
else |
| 585 |
begin |
| 586 |
while True do |
| 587 |
begin |
| 588 |
d[count] := c; |
| 589 |
inc(count); |
| 590 |
if count = 0 then exit; |
| 591 |
c := s[count]; |
| 592 |
a := c shr 24; |
| 593 |
if a <> 255 then break; |
| 594 |
end; |
| 595 |
end; |
| 596 |
end; |
| 597 |
|
| 598 |
else |
| 599 |
// Premultiply the alpha channel |
| 600 |
|
| 601 |
// Win32: Load stack variable into CPU register |
| 602 |
s := PColor32Array(pSrc); |
| 603 |
// Win32: This line "breaks" Delphi's register allocator |
| 604 |
//d := PColor32Array(pDst); |
| 605 |
while True do |
| 606 |
begin |
| 607 |
tab := @MulTable[a]; |
| 608 |
c := (c and $FF000000) or |
| 609 |
(tab[Byte(c shr 16)] shl 16) or |
| 610 |
(tab[Byte(c shr 8)] shl 8) or |
| 611 |
(tab[Byte(c )] ); |
| 612 |
//d[count] := c; |
| 613 |
PColor32Array(pDst)[count] := c; |
| 614 |
inc(count); |
| 615 |
if count = 0 then exit; |
| 616 |
c := s[count]; |
| 617 |
a := c shr 24; |
| 618 |
if (a = 0) or (a = 255) then break; |
| 619 |
end; |
| 620 |
end; |
| 621 |
end; |
| 622 |
end; |
| 623 |
{$IFDEF RANGECHECKS_ENABLED} |
| 624 |
{$RANGECHECKS ON} |
| 625 |
{$ENDIF RANGECHECKS_ENABLED} |
| 626 |
|
| 627 |
//------------------------------------------------------------------------------ |
| 628 |
// BoxDownSampling and related functions |
| 629 |
//------------------------------------------------------------------------------ |
| 630 |
|
| 631 |
function GetWeightedColor(const srcBits: TArrayOfColor32; |
| 632 |
x256, y256, xx256, yy256, maxX: Integer): TColor32; |
| 633 |
var |
| 634 |
i, j, xi, yi, xxi, yyi, weight: Integer; |
| 635 |
xf, yf, xxf, yyf: cardinal; |
| 636 |
color: TWeightedColor; |
| 637 |
begin |
| 638 |
//This function performs 'box sampling' and differs from GetWeightedPixel |
| 639 |
//(bilinear resampling) in one important aspect - it accommodates weighting |
| 640 |
//any number of pixels (rather than just adjacent pixels) and this produces |
| 641 |
//better image quality when significantly downsizing. |
| 642 |
|
| 643 |
//Note: there's no range checking here, so the precondition is that the |
| 644 |
//supplied boundary values are within the bounds of the srcBits array. |
| 645 |
|
| 646 |
color.Reset; |
| 647 |
|
| 648 |
xi := x256 shr 8; xf := x256 and $FF; |
| 649 |
yi := y256 shr 8; yf := y256 and $FF; |
| 650 |
xxi := xx256 shr 8; xxf := xx256 and $FF; |
| 651 |
yyi := yy256 shr 8; yyf := yy256 and $FF; |
| 652 |
|
| 653 |
//1. average the corners ... |
| 654 |
weight := (($100 - xf) * ($100 - yf)) shr 8; |
| 655 |
color.Add(srcBits[xi + yi * maxX], weight); |
| 656 |
weight := (xxf * ($100 - yf)) shr 8; |
| 657 |
if (weight <> 0) then color.Add(srcBits[xxi + yi * maxX], weight); |
| 658 |
weight := (($100 - xf) * yyf) shr 8; |
| 659 |
if (weight <> 0) then color.Add(srcBits[xi + yyi * maxX], weight); |
| 660 |
weight := (xxf * yyf) shr 8; |
| 661 |
if (weight <> 0) then color.Add(srcBits[xxi + yyi * maxX], weight); |
| 662 |
|
| 663 |
//2. average the edges |
| 664 |
if (yi +1 < yyi) then |
| 665 |
begin |
| 666 |
xf := $100 - xf; |
| 667 |
for i := yi + 1 to yyi - 1 do |
| 668 |
color.Add(srcBits[xi + i * maxX], xf); |
| 669 |
if (xxf <> 0) then |
| 670 |
for i := yi + 1 to yyi - 1 do |
| 671 |
color.Add(srcBits[xxi + i * maxX], xxf); |
| 672 |
end; |
| 673 |
if (xi + 1 < xxi) then |
| 674 |
begin |
| 675 |
yf := $100 - yf; |
| 676 |
for i := xi + 1 to xxi - 1 do |
| 677 |
color.Add(srcBits[i + yi * maxX], yf); |
| 678 |
if (yyf <> 0) then |
| 679 |
for i := xi + 1 to xxi - 1 do |
| 680 |
color.Add(srcBits[i + yyi * maxX], yyf); |
| 681 |
end; |
| 682 |
|
| 683 |
//3. average the non-fractional pixel 'internals' ... |
| 684 |
for i := xi + 1 to xxi - 1 do |
| 685 |
for j := yi + 1 to yyi - 1 do |
| 686 |
color.Add(srcBits[i + j * maxX], $100); |
| 687 |
|
| 688 |
//4. finally get the weighted color ... |
| 689 |
if color.AddCount = 0 then |
| 690 |
Result := srcBits[xi + yi * maxX] else |
| 691 |
Result := color.Color; |
| 692 |
end; |
| 693 |
//------------------------------------------------------------------------------ |
| 694 |
|
| 695 |
procedure BoxDownSampling(Image: TImage32; scaleX, scaleY: double); |
| 696 |
begin |
| 697 |
BoxDownSampling(Image, Image, scaleX, scaleY); |
| 698 |
end; |
| 699 |
//------------------------------------------------------------------------------ |
| 700 |
|
| 701 |
procedure BoxDownSampling(Image: TImage32; scale: double); |
| 702 |
begin |
| 703 |
BoxDownSampling(Image, Image, scale); |
| 704 |
end; |
| 705 |
//------------------------------------------------------------------------------ |
| 706 |
|
| 707 |
procedure BoxDownSampling(Image: TImage32; newWidth, newHeight: Integer); |
| 708 |
begin |
| 709 |
BoxDownSampling(Image, Image, newWidth, newHeight); |
| 710 |
end; |
| 711 |
//------------------------------------------------------------------------------ |
| 712 |
|
| 713 |
procedure BoxDownSampling(Image, TargetImage: TImage32; scaleX, scaleY: double); |
| 714 |
begin |
| 715 |
BoxDownSampling(Image, TargetImage, |
| 716 |
Max(1, Integer(Round(Image.Width * scaleX))), |
| 717 |
Max(1, Integer(Round(Image.Height * scaleY)))); |
| 718 |
end; |
| 719 |
//------------------------------------------------------------------------------ |
| 720 |
|
| 721 |
procedure BoxDownSampling(Image, TargetImage: TImage32; scale: double); |
| 722 |
begin |
| 723 |
BoxDownSampling(Image, TargetImage, |
| 724 |
Max(1, Integer(Round(Image.Width * scale))), |
| 725 |
Max(1, Integer(Round(Image.Height * scale)))); |
| 726 |
end; |
| 727 |
//------------------------------------------------------------------------------ |
| 728 |
|
| 729 |
procedure BoxDownSampling(Image, TargetImage: TImage32; newWidth, newHeight: Integer); |
| 730 |
var |
| 731 |
x,y, x256,y256,xx256,yy256: Integer; |
| 732 |
sx,sy: double; |
| 733 |
tmp: TArrayOfColor32; |
| 734 |
pc: PColor32; |
| 735 |
scaledX: TArrayOfInteger; |
| 736 |
begin |
| 737 |
sx := Image.Width/newWidth * 256; |
| 738 |
sy := Image.Height/newHeight * 256; |
| 739 |
NewColor32Array(tmp, newWidth * newHeight, True); |
| 740 |
|
| 741 |
NewIntegerArray(scaledX, newWidth, True); |
| 742 |
for x := 0 to newWidth -1 do |
| 743 |
scaledX[x] := Round((x+1) * sx); |
| 744 |
|
| 745 |
y256 := 0; |
| 746 |
pc := @tmp[0]; |
| 747 |
for y := 0 to newHeight - 1 do |
| 748 |
begin |
| 749 |
x256 := 0; |
| 750 |
yy256 := Round((y+1) * sy); |
| 751 |
for x := 0 to newWidth - 1 do |
| 752 |
begin |
| 753 |
xx256 := scaledX[x]; |
| 754 |
pc^ := GetWeightedColor(Image.Pixels, |
| 755 |
x256, y256, xx256, yy256, Image.Width); |
| 756 |
x256 := xx256; |
| 757 |
inc(pc); |
| 758 |
end; |
| 759 |
y256 := yy256; |
| 760 |
end; |
| 761 |
|
| 762 |
TargetImage.AssignPixelArray(tmp, newWidth, newHeight); |
| 763 |
end; |
| 764 |
//------------------------------------------------------------------------------ |
| 765 |
|
| 766 |
procedure NearestNeighborResize(Image: TImage32; newWidth, newHeight: Integer); |
| 767 |
begin |
| 768 |
NearestNeighborResize(Image, Image, newWidth, newHeight); |
| 769 |
end; |
| 770 |
//------------------------------------------------------------------------------ |
| 771 |
|
| 772 |
procedure NearestNeighborResize(Image, TargetImage: TImage32; newWidth, newHeight: Integer); |
| 773 |
var |
| 774 |
x, y, offset: Integer; |
| 775 |
scaledXi, scaledYiOffset: TArrayOfInteger; |
| 776 |
tmp: TArrayOfColor32; |
| 777 |
pc: PColor32; |
| 778 |
pixels: TArrayOfColor32; |
| 779 |
begin |
| 780 |
//this NearestNeighbor code is slightly more efficient than |
| 781 |
//the more general purpose one in Img32.Resamplers |
| 782 |
|
| 783 |
if (newWidth = Image.Width) and (newHeight = Image.Height) then |
| 784 |
begin |
| 785 |
if TargetImage <> Image then TargetImage.Assign(Image); |
| 786 |
Exit; |
| 787 |
end; |
| 788 |
NewColor32Array(tmp, newWidth * newHeight, True); |
| 789 |
|
| 790 |
//get scaled X & Y values once only (storing them in lookup arrays) ... |
| 791 |
NewIntegerArray(scaledXi, newWidth, True); |
| 792 |
for x := 0 to newWidth -1 do |
| 793 |
scaledXi[x] := (x * Image.Width) div newWidth; |
| 794 |
NewIntegerArray(scaledYiOffset, newHeight, True); |
| 795 |
SetLength(scaledYiOffset, newHeight); |
| 796 |
for y := 0 to newHeight -1 do |
| 797 |
//scaledYiOffset[y] := Round(y * Image.Height / newHeight) * Image.Width; |
| 798 |
scaledYiOffset[y] := ((y * Image.Height) div newHeight) * Image.Width; |
| 799 |
|
| 800 |
pc := @tmp[0]; |
| 801 |
pixels := Image.Pixels; |
| 802 |
for y := 0 to newHeight - 1 do |
| 803 |
begin |
| 804 |
offset := scaledYiOffset[y]; |
| 805 |
for x := 0 to newWidth - 1 do |
| 806 |
begin |
| 807 |
pc^ := pixels[scaledXi[x] + offset]; |
| 808 |
inc(pc); |
| 809 |
end; |
| 810 |
end; |
| 811 |
|
| 812 |
TargetImage.AssignPixelArray(tmp, newWidth, newHeight); |
| 813 |
end; |
| 814 |
//------------------------------------------------------------------------------ |
| 815 |
|
| 816 |
procedure ResamplerResize(Image: TImage32; newWidth, newHeight: Integer); |
| 817 |
begin |
| 818 |
ResamplerResize(Image, Image, newWidth, newHeight); |
| 819 |
end; |
| 820 |
//------------------------------------------------------------------------------ |
| 821 |
|
| 822 |
procedure ResamplerResize(Image, TargetImage: TImage32; newWidth, newHeight: Integer); |
| 823 |
var |
| 824 |
mat: TMatrixD; |
| 825 |
begin |
| 826 |
mat := IdentityMatrix; |
| 827 |
MatrixScale(mat, newWidth/Image.Width, newHeight/Image.Height); |
| 828 |
AffineTransformImage(Image, TargetImage, mat); |
| 829 |
end; |
| 830 |
|
| 831 |
//------------------------------------------------------------------------------ |
| 832 |
//------------------------------------------------------------------------------ |
| 833 |
|
| 834 |
procedure InitByteExponents; |
| 835 |
var |
| 836 |
i: integer; |
| 837 |
const |
| 838 |
inv255 : double = 1/255; |
| 839 |
inv255sqrd : double = 1/(255*255); |
| 840 |
inv255cubed: double = 1/(255*255*255); |
| 841 |
piDiv256 : double = Pi / 256; |
| 842 |
begin |
| 843 |
for i := 0 to 255 do |
| 844 |
begin |
| 845 |
byteFrac[i] := i *inv255; |
| 846 |
byteFracSq[i] := i*i *inv255sqrd; |
| 847 |
byteFracCubed[i] := i*i*i *inv255cubed; |
| 848 |
|
| 849 |
sinWeighted[i] := Round((Sin(i * piDiv256 - Pi/2) +1) /2 * 255); |
| 850 |
end; |
| 851 |
end; |
| 852 |
//------------------------------------------------------------------------------ |
| 853 |
|
| 854 |
initialization |
| 855 |
InitByteExponents; |
| 856 |
|
| 857 |
rNearestResampler := RegisterResampler(NearestResampler, 'NearestNeighbor'); |
| 858 |
rBilinearResampler := RegisterResampler(BilinearResample, 'Bilinear'); |
| 859 |
rBicubicResampler := RegisterResampler(BicubicResample, 'HermiteBicubic'); |
| 860 |
rWeightedBilinear := RegisterResampler(WeightedBilinearResample, 'WeightedBilinear'); |
| 861 |
DefaultResampler := rBilinearResampler; |
| 862 |
|
| 863 |
end. |