Repo for the search and displace ingest module that takes odf, docx and pdf and transforms it into .md to be used with search and displace operations
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

697 lines
21 KiB

3 years ago
  1. {
  2. Deskew
  3. by Marek Mauder
  4. http://galfar.vevb.net/deskew
  5. The contents of this file are used with permission, subject to the Mozilla
  6. Public License Version 1.1 (the "License"); you may not use this file except
  7. in compliance with the License. You may obtain a copy of the License at
  8. http://www.mozilla.org/MPL/MPL-1.1.html
  9. Software distributed under the License is distributed on an "AS IS" basis,
  10. WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
  11. the specific language governing rights and limitations under the License.
  12. Alternatively, the contents of this file may be used under the terms of the
  13. GNU Lesser General Public License (the "LGPL License"), in which case the
  14. provisions of the LGPL License are applicable instead of those above.
  15. If you wish to allow use of your version of this file only under the terms
  16. of the LGPL License and not to allow others to use your version of this file
  17. under the MPL, indicate your decision by deleting the provisions above and
  18. replace them with the notice and other provisions required by the LGPL
  19. License. If you do not delete the provisions above, a recipient may use
  20. your version of this file under either the MPL or the LGPL License.
  21. For more information about the LGPL: http://www.gnu.org/copyleft/lesser.html
  22. }
  23. {
  24. Unit with various image processing functions. Some are taken from
  25. Imaging extensions.
  26. }
  27. unit ImageUtils;
  28. {$I ImagingOptions.inc}
  29. interface
  30. uses
  31. Types,
  32. Math,
  33. SysUtils,
  34. Classes,
  35. ImagingTypes,
  36. Imaging,
  37. ImagingFormats,
  38. ImagingUtility;
  39. type
  40. TResamplingFilter = (
  41. rfNearest,
  42. rfLinear,
  43. rfCubic,
  44. rfLanczos
  45. );
  46. { Thresholding using Otsu's method (which chooses the threshold
  47. to minimize the intraclass variance of the black and white pixels!).
  48. Functions returns calculated threshold level value [0..255].
  49. If BinarizeImage is True then the Image is automatically converted to binary using
  50. computed threshold level.}
  51. function OtsuThresholding(var Image: TImageData; BinarizeImage: Boolean = False): Integer;
  52. const
  53. SupportedRotationFormats: set of TImageFormat = [ifGray8, ifR8G8B8, ifA8R8G8B8];
  54. { Rotates image with a background (of outside "void" areas) of specified color. The image is resized to fit
  55. the whole rotated image. }
  56. procedure RotateImage(var Image: TImageData; Angle: Double; BackgroundColor: TColor32;
  57. ResamplingFilter: TResamplingFilter; FitRotated: Boolean);
  58. implementation
  59. function OtsuThresholding(var Image: TImageData; BinarizeImage: Boolean): Integer;
  60. var
  61. Histogram: array[Byte] of Single;
  62. Level, Max, Min, I, J, NumPixels: Integer;
  63. Pix: PByte;
  64. Mean, Variance: Single;
  65. Mu, Omega, LevelMean, LargestMu: Single;
  66. begin
  67. Assert(Image.Format = ifGray8);
  68. FillChar(Histogram, SizeOf(Histogram), 0);
  69. Min := 255;
  70. Max := 0;
  71. Level := 0;
  72. NumPixels := Image.Width * Image.Height;
  73. Pix := Image.Bits;
  74. // Compute histogram and determine min and max pixel values
  75. for I := 0 to NumPixels - 1 do
  76. begin
  77. Histogram[Pix^] := Histogram[Pix^] + 1.0;
  78. if Pix^ < Min then
  79. Min := Pix^;
  80. if Pix^ > Max then
  81. Max := Pix^;
  82. Inc(Pix);
  83. end;
  84. // Normalize histogram
  85. for I := 0 to 255 do
  86. Histogram[I] := Histogram[I] / NumPixels;
  87. // Compute image mean and variance
  88. Mean := 0.0;
  89. Variance := 0.0;
  90. for I := 0 to 255 do
  91. Mean := Mean + (I + 1) * Histogram[I];
  92. for I := 0 to 255 do
  93. Variance := Variance + Sqr(I + 1 - Mean) * Histogram[I];
  94. // Now finally compute threshold level
  95. LargestMu := 0;
  96. for I := 0 to 255 do
  97. begin
  98. Omega := 0.0;
  99. LevelMean := 0.0;
  100. for J := 0 to I - 1 do
  101. begin
  102. Omega := Omega + Histogram[J];
  103. LevelMean := LevelMean + (J + 1) * Histogram[J];
  104. end;
  105. Mu := Sqr(Mean * Omega - LevelMean);
  106. Omega := Omega * (1.0 - Omega);
  107. if Omega > 0.0 then
  108. Mu := Mu / Omega
  109. else
  110. Mu := 0;
  111. if Mu > LargestMu then
  112. begin
  113. LargestMu := Mu;
  114. Level := I;
  115. end;
  116. end;
  117. if BinarizeImage then
  118. begin
  119. // Do thresholding using computed level
  120. Pix := Image.Bits;
  121. for I := 0 to Image.Width * Image.Height - 1 do
  122. begin
  123. if Pix^ >= Level then
  124. Pix^ := 255
  125. else
  126. Pix^ := 0;
  127. Inc(Pix);
  128. end;
  129. end;
  130. Result := Level;
  131. end;
  132. procedure RotateImage(var Image: TImageData; Angle: Double; BackgroundColor: TColor32;
  133. ResamplingFilter: TResamplingFilter; FitRotated: Boolean);
  134. // Use precomputed weights for bicubic and Lanczos filters
  135. {$DEFINE USE_FILTER_TABLE}
  136. type
  137. TBufferEntry = record
  138. B, G, R, A: Single;
  139. end;
  140. const
  141. EmptyBufferEntry: TBufferEntry = (B: 0; G: 0; R: 0; A: 0);
  142. TableSize = 32;
  143. MaxTablePos = TableSize - 1;
  144. MaxKernelRadius = 3;
  145. var
  146. SrcWidth, SrcHeight: Integer;
  147. SrcWidthHalf, SrcHeightHalf, DstWidthHalf, DstHeightHalf: Single;
  148. DstWidth, DstHeight: Integer;
  149. AngleRad, ForwardSin, ForwardCos, BackwardSin, BackwardCos, SrcX, SrcY, D: Single;
  150. TopLeft, TopRight, BottomLeft, BottomRight: TFloatPoint;
  151. SrcImage, DstImage: TImageData;
  152. FormatInfo: TImageFormatInfo;
  153. X, Y, Bpp: Integer;
  154. DstPixel24: PColor24Rec;
  155. BackColor24: TColor24Rec;
  156. BackColor32, Pixel32: TColor32Rec;
  157. DstByte: PByte;
  158. Filter: TSamplingFilter;
  159. FilterFunction: TFilterFunction;
  160. FilterRadius: Single;
  161. KernelWidth: Integer;
  162. WeightTable: array[-MaxKernelRadius..MaxKernelRadius, 0..TableSize] of Single;
  163. function FastFloor(X: Single): Integer; inline;
  164. begin
  165. Result := Trunc(X + 65536.0) - 65536;
  166. end;
  167. function FastCeil(X: Single): Integer; inline;
  168. begin
  169. Result := 65536 - Trunc(65536.0 - X);
  170. end;
  171. function GetPixelColor24(X, Y: Integer): TColor24Rec; {$IFDEF FPC}inline;{$ENDIF}
  172. begin
  173. if (X >= 0) and (X < SrcWidth) and (Y >= 0) and (Y < SrcHeight) then
  174. Result := PColor24RecArray(SrcImage.Bits)[Y * SrcWidth + X]
  175. else
  176. Result := BackColor24;
  177. end;
  178. function GetPixelColor8(X, Y: Integer): Byte; {$IFDEF FPC}inline;{$ENDIF}
  179. begin
  180. if (X >= 0) and (X < SrcWidth) and (Y >= 0) and (Y < SrcHeight) then
  181. Result := PByteArray(SrcImage.Bits)[Y * SrcWidth + X]
  182. else
  183. Result := BackColor32.B;
  184. end;
  185. function GetPixelColor32(X, Y: Integer): TColor32Rec; {$IFDEF FPC}inline;{$ENDIF}
  186. begin
  187. if (X >= 0) and (X < SrcWidth) and (Y >= 0) and (Y < SrcHeight) then
  188. Result := PColor32RecArray(SrcImage.Bits)[Y * SrcWidth + X]
  189. else
  190. Result := BackColor32;
  191. end;
  192. procedure GetBilinearPixelCoords(X, Y: Single;
  193. out HorzWeight, VertWeight: Single;
  194. out TopLeftPt, BottomLeftPt, TopRightPt, BottomRightPt: TPoint); inline;
  195. begin
  196. TopLeftPt := Point(FastFloor(X), FastFloor(Y));
  197. HorzWeight := X - TopLeftPt.X;
  198. VertWeight := Y - TopLeftPt.Y;
  199. BottomLeftPt := Point(TopLeftPt.X, TopLeftPt.Y + 1);
  200. TopRightPt := Point(TopLeftPt.X + 1, TopLeftPt.Y);
  201. BottomRightPt := Point(TopLeftPt.X + 1, TopLeftPt.Y + 1);
  202. end;
  203. function InterpolateBytes(HorzWeight, VertWeight: Single; C11, C12, C21, C22: Byte): Byte; inline;
  204. begin
  205. Result := ClampToByte(Trunc(
  206. (1 - HorzWeight) * (1 - VertWeight) * C11 +
  207. (1 - HorzWeight) * VertWeight * C12 +
  208. HorzWeight * (1 - VertWeight) * C21 +
  209. HorzWeight * VertWeight * C22));
  210. end;
  211. function Bilinear24(X, Y: Single): TColor24Rec; inline;
  212. var
  213. TopLeftPt, BottomLeftPt, TopRightPt, BottomRightPt: TPoint;
  214. HorzWeight, VertWeight: Single;
  215. TopLeftColor, TopRightColor, BottomLeftColor, BottomRightColor: TColor24Rec;
  216. begin
  217. GetBilinearPixelCoords(X, Y,
  218. HorzWeight, VertWeight,
  219. TopLeftPt, BottomLeftPt, TopRightPt, BottomRightPt);
  220. TopLeftColor := GetPixelColor24(TopLeftPt.X, TopLeftPt.Y);
  221. BottomLeftColor := GetPixelColor24(BottomLeftPt.X, BottomLeftPt.Y);
  222. TopRightColor := GetPixelColor24(TopRightPt.X, TopRightPt.Y);
  223. BottomRightColor := GetPixelColor24(BottomRightPt.X, BottomRightPt.Y);
  224. Result.R := InterpolateBytes(HorzWeight, VertWeight,
  225. TopLeftColor.R, BottomLeftColor.R, TopRightColor.R, BottomRightColor.R);
  226. Result.G := InterpolateBytes(HorzWeight, VertWeight,
  227. TopLeftColor.G, BottomLeftColor.G, TopRightColor.G, BottomRightColor.G);
  228. Result.B := InterpolateBytes(HorzWeight, VertWeight,
  229. TopLeftColor.B, BottomLeftColor.B, TopRightColor.B, BottomRightColor.B);
  230. end;
  231. function Bilinear8(X, Y: Single): Byte; inline;
  232. var
  233. TopLeftPt, BottomLeftPt, TopRightPt, BottomRightPt: TPoint;
  234. HorzWeight, VertWeight: Single;
  235. TopLeftColor, TopRightColor, BottomLeftColor, BottomRightColor: Byte;
  236. begin
  237. GetBilinearPixelCoords(X, Y,
  238. HorzWeight, VertWeight,
  239. TopLeftPt, BottomLeftPt, TopRightPt, BottomRightPt);
  240. TopLeftColor := GetPixelColor8(TopLeftPt.X, TopLeftPt.Y);
  241. BottomLeftColor := GetPixelColor8(BottomLeftPt.X, BottomLeftPt.Y);
  242. TopRightColor := GetPixelColor8(TopRightPt.X, TopRightPt.Y);
  243. BottomRightColor := GetPixelColor8(BottomRightPt.X, BottomRightPt.Y);
  244. Result := InterpolateBytes(HorzWeight, VertWeight,
  245. TopLeftColor, BottomLeftColor, TopRightColor, BottomRightColor);
  246. end;
  247. function Bilinear32(X, Y: Single): TColor32Rec; inline;
  248. var
  249. TopLeftPt, BottomLeftPt, TopRightPt, BottomRightPt: TPoint;
  250. HorzWeight, VertWeight: Single;
  251. TopLeftColor, TopRightColor, BottomLeftColor, BottomRightColor: TColor32Rec;
  252. begin
  253. GetBilinearPixelCoords(X, Y,
  254. HorzWeight, VertWeight,
  255. TopLeftPt, BottomLeftPt, TopRightPt, BottomRightPt);
  256. TopLeftColor := GetPixelColor32(TopLeftPt.X, TopLeftPt.Y);
  257. BottomLeftColor := GetPixelColor32(BottomLeftPt.X, BottomLeftPt.Y);
  258. TopRightColor := GetPixelColor32(TopRightPt.X, TopRightPt.Y);
  259. BottomRightColor := GetPixelColor32(BottomRightPt.X, BottomRightPt.Y);
  260. Result.A := InterpolateBytes(HorzWeight, VertWeight,
  261. TopLeftColor.A, BottomLeftColor.A, TopRightColor.A, BottomRightColor.A);
  262. Result.R := InterpolateBytes(HorzWeight, VertWeight,
  263. TopLeftColor.R, BottomLeftColor.R, TopRightColor.R, BottomRightColor.R);
  264. Result.G := InterpolateBytes(HorzWeight, VertWeight,
  265. TopLeftColor.G, BottomLeftColor.G, TopRightColor.G, BottomRightColor.G);
  266. Result.B := InterpolateBytes(HorzWeight, VertWeight,
  267. TopLeftColor.B, BottomLeftColor.B, TopRightColor.B, BottomRightColor.B);
  268. end;
  269. {$IFDEF USE_FILTER_TABLE}
  270. procedure PrecomputeFilterWeights;
  271. var
  272. I, J: Integer;
  273. Weight: Single;
  274. Fraction: Single;
  275. begin
  276. FillMemoryByte(@WeightTable, SizeOf(WeightTable), 0);
  277. for I := 0 to TableSize do
  278. begin
  279. Fraction := I / (TableSize - 1);
  280. for J := -KernelWidth to KernelWidth do
  281. begin
  282. Weight := FilterFunction(J + Fraction);
  283. WeightTable[J, I] := Weight;
  284. end;
  285. end;
  286. end;
  287. {$ENDIF}
  288. function FilterPixel(X, Y: Single; Bpp: Integer): TColor32Rec;
  289. var
  290. HorzEntry, VertEntry: TBufferEntry;
  291. LoX, HiX, LoY, HiY: Integer;
  292. I, J: Integer;
  293. WeightHorz, WeightVert: Single;
  294. CeilX, CeilY: Integer;
  295. {$IFDEF USE_FILTER_TABLE}
  296. XFilterTablePos, YFilterTablePos: Integer;
  297. {$ELSE}
  298. FracXS, FracYS: Single;
  299. {$ENDIF}
  300. SrcPixel: PColor32Rec;
  301. ClipRect: TRect;
  302. Edge: Boolean;
  303. begin
  304. ClipRect := Rect(0, 0, SrcWidth, SrcHeight);
  305. Edge := False;
  306. CeilX := FastCeil(X);
  307. CeilY := FastCeil(Y);
  308. with ClipRect do
  309. begin
  310. if not ((CeilX < Left) or (CeilX > Right) or (CeilY < Top) or (CeilY > Bottom)) then
  311. begin
  312. Edge := False;
  313. if CeilX - KernelWidth < Left then
  314. begin
  315. LoX := Left - CeilX;
  316. Edge := True;
  317. end
  318. else
  319. LoX := -KernelWidth;
  320. if CeilX + KernelWidth >= Right then
  321. begin
  322. HiX := Right - CeilX - 1;
  323. Edge := True;
  324. end
  325. else
  326. HiX := KernelWidth;
  327. if CeilY - KernelWidth < Top then
  328. begin
  329. LoY := Top - CeilY;
  330. Edge := True;
  331. end
  332. else
  333. LoY := -KernelWidth;
  334. if CeilY + KernelWidth >= Bottom then
  335. begin
  336. HiY := Bottom - CeilY - 1;
  337. Edge := True;
  338. end
  339. else
  340. HiY := KernelWidth;
  341. end
  342. else
  343. Exit(BackColor32);
  344. end;
  345. {$IFDEF USE_FILTER_TABLE}
  346. XFilterTablePos := Round((CeilX - X) * MaxTablePos);
  347. YFilterTablePos := Round((CeilY - Y) * MaxTablePos);
  348. {$ELSE}
  349. FracXS := CeilX - X;
  350. FracYS := CeilY - Y;
  351. {$ENDIF}
  352. VertEntry := EmptyBufferEntry;
  353. for I := LoY to HiY do
  354. begin
  355. {$IFDEF USE_FILTER_TABLE}
  356. WeightVert := WeightTable[I, YFilterTablePos];
  357. {$ELSE}
  358. WeightVert := FilterFunction(I + FracYS);
  359. {$ENDIF}
  360. SrcPixel := PColor32Rec(@PByteArray(SrcImage.Bits)[(LoX + CeilX + (I + CeilY) * SrcWidth) * Bpp]);
  361. if WeightVert <> 0 then
  362. begin
  363. HorzEntry := EmptyBufferEntry;
  364. for J := LoX to HiX do
  365. begin
  366. {$IFDEF USE_FILTER_TABLE}
  367. WeightHorz := WeightTable[J, XFilterTablePos];
  368. {$ELSE}
  369. WeightHorz := FilterFunction(J + FracXS);
  370. {$ENDIF}
  371. HorzEntry.B := HorzEntry.B + SrcPixel.B * WeightHorz;
  372. if Bpp > 1 then
  373. begin
  374. HorzEntry.R := HorzEntry.R + SrcPixel.R * WeightHorz;
  375. HorzEntry.G := HorzEntry.G + SrcPixel.G * WeightHorz;
  376. if Bpp > 3 then
  377. HorzEntry.A := HorzEntry.A + SrcPixel.A * WeightHorz;
  378. end;
  379. Inc(PByte(SrcPixel), Bpp);
  380. end;
  381. VertEntry.A := VertEntry.A + HorzEntry.A * WeightVert;
  382. VertEntry.R := VertEntry.R + HorzEntry.R * WeightVert;
  383. VertEntry.G := VertEntry.G + HorzEntry.G * WeightVert;
  384. VertEntry.B := VertEntry.B + HorzEntry.B * WeightVert;
  385. end;
  386. end;
  387. if Edge then
  388. begin
  389. for I := -KernelWidth to KernelWidth do
  390. begin
  391. {$IFDEF USE_FILTER_TABLE}
  392. WeightVert := WeightTable[I, YFilterTablePos];
  393. {$ELSE}
  394. WeightVert := FilterFunction(I + FracYS);
  395. {$ENDIF}
  396. if WeightVert <> 0 then
  397. begin
  398. HorzEntry := EmptyBufferEntry;
  399. for J := -KernelWidth to KernelWidth do
  400. begin
  401. if (J < LoX) or (J > HiX) or (I < LoY) or (I > HiY) then
  402. begin
  403. {$IFDEF USE_FILTER_TABLE}
  404. WeightHorz := WeightTable[J, XFilterTablePos];
  405. {$ELSE}
  406. WeightHorz := FilterFunction(J + FracXS);
  407. {$ENDIF}
  408. HorzEntry.A := HorzEntry.A + BackColor32.A * WeightHorz;
  409. HorzEntry.R := HorzEntry.R + BackColor32.R * WeightHorz;
  410. HorzEntry.G := HorzEntry.G + BackColor32.G * WeightHorz;
  411. HorzEntry.B := HorzEntry.B + BackColor32.B * WeightHorz;
  412. end;
  413. end;
  414. VertEntry.A := VertEntry.A + HorzEntry.A * WeightVert;
  415. VertEntry.R := VertEntry.R + HorzEntry.R * WeightVert;
  416. VertEntry.G := VertEntry.G + HorzEntry.G * WeightVert;
  417. VertEntry.B := VertEntry.B + HorzEntry.B * WeightVert;
  418. end;
  419. end
  420. end;
  421. with Result do
  422. begin
  423. A := ClampToByte(Trunc(VertEntry.A + 0.5));
  424. R := ClampToByte(Trunc(VertEntry.R + 0.5));
  425. G := ClampToByte(Trunc(VertEntry.G + 0.5));
  426. B := ClampToByte(Trunc(VertEntry.B + 0.5));
  427. end;
  428. end;
  429. function RotatePoint(X, Y: Single): TFloatPoint;
  430. begin
  431. Result.X := ForwardCos * X - ForwardSin * Y;
  432. Result.Y := ForwardSin * X + ForwardCos * Y;
  433. end;
  434. function Max4(X1, X2, X3, X4: Single): Single;
  435. begin
  436. Result := Math.Max(Math.Max(X1, X2), Math.Max(X3, X4));
  437. end;
  438. function Min4(X1, X2, X3, X4: Single): Single;
  439. begin
  440. Result := Math.Min(Math.Min(X1, X2), Math.Min(X3, X4));
  441. end;
  442. procedure CalcSourceCoordinates(DstX, DstY: Integer; out SrcX, SrcY: Single); {$IFDEF FPC}inline;{$ENDIF}
  443. var
  444. SrcCoordX, SrcCoordY: Single;
  445. DstCoordX, DstCoordY: Single;
  446. begin
  447. DstCoordX := DstX - DstWidthHalf;
  448. DstCoordY := DstHeightHalf - DstY;
  449. SrcCoordX := BackwardCos * DstCoordX - BackwardSin * DstCoordY;
  450. SrcCoordY := BackwardSin * DstCoordX + BackwardCos * DstCoordY;
  451. SrcX := SrcCoordX + SrcWidthHalf;
  452. SrcY := SrcHeightHalf - SrcCoordY;
  453. end;
  454. function CropToSource(const Pt: TFloatPoint): Single;
  455. var
  456. X, Y: Single;
  457. begin
  458. X := Abs(Pt.X / SrcWidthHalf);
  459. Y := Abs(Pt.Y / SrcHeightHalf);
  460. Result := MaxFloat(X, Y);
  461. end;
  462. begin
  463. Assert(Image.Format in SupportedRotationFormats);
  464. GetImageFormatInfo(Image.Format, FormatInfo);
  465. while Angle >= 360 do
  466. Angle := Angle - 360;
  467. while Angle < 0 do
  468. Angle := Angle + 360;
  469. if (Angle = 0) or (Abs(Angle) = 360) then
  470. Exit;
  471. AngleRad := Angle * PI / 180;
  472. SinCos(AngleRad, ForwardSin, ForwardCos);
  473. SinCos(-AngleRad, BackwardSin, BackwardCos);
  474. SrcImage := Image;
  475. SrcWidth := SrcImage.Width;
  476. SrcHeight := SrcImage.Height;
  477. SrcWidthHalf := (SrcWidth - 1) / 2;
  478. SrcHeightHalf := (SrcHeight - 1) / 2;
  479. // Calculate width and height of the rotated image
  480. TopLeft := RotatePoint(-SrcWidthHalf, SrcHeightHalf);
  481. TopRight := RotatePoint(SrcWidthHalf, SrcHeightHalf);
  482. BottomLeft := RotatePoint(-SrcWidthHalf, -SrcHeightHalf);
  483. BottomRight := RotatePoint(SrcWidthHalf, -SrcHeightHalf);
  484. if FitRotated then
  485. begin
  486. // Encompass the whole area of rotate image => bounding box
  487. DstWidth := Ceil(Max4(TopLeft.X, TopRight.X, BottomLeft.X, BottomRight.X) -
  488. Min4(TopLeft.X, TopRight.X, BottomLeft.X, BottomRight.X));
  489. DstHeight := Ceil(Max4(TopLeft.Y, TopRight.Y, BottomLeft.Y, BottomRight.Y) -
  490. Min4(TopLeft.Y, TopRight.Y, BottomLeft.Y, BottomRight.Y));
  491. if ResamplingFilter <> rfNearest then
  492. begin
  493. // Account a bit for antialiased edges of the rotated image
  494. Inc(DstWidth);
  495. Inc(DstHeight);
  496. end;
  497. end
  498. else
  499. begin
  500. // Crop to largest proportional rect inside the rotated rect
  501. D := Max4(CropToSource(TopLeft), CropToSource(TopRight), CropToSource(BottomLeft), CropToSource(BottomRight));
  502. DstWidth := Ceil(SrcWidth / D);
  503. DstHeight := Ceil(SrcHeight / D);
  504. end;
  505. DstWidthHalf := (DstWidth - 1) / 2;
  506. DstHeightHalf := (DstHeight - 1) / 2;
  507. InitImage(DstImage);
  508. NewImage(DstWidth, DstHeight, SrcImage.Format, DstImage);
  509. Bpp := FormatInfo.BytesPerPixel;
  510. DstByte := DstImage.Bits;
  511. BackColor32 := TColor32Rec(BackgroundColor);
  512. if ResamplingFilter = rfNearest then
  513. begin
  514. for Y := 0 to DstHeight - 1 do
  515. for X := 0 to DstWidth - 1 do
  516. begin
  517. CalcSourceCoordinates(X, Y, SrcX, SrcY);
  518. if (SrcX >= 0) and (SrcY >= 0) and (SrcX <= SrcWidth - 1) and (SrcY <= SrcHeight - 1) then
  519. begin
  520. if Bpp = 3 then
  521. PColor24Rec(DstByte)^ := PColor24RecArray(SrcImage.Bits)[Round(SrcY) * SrcWidth + Round(SrcX)]
  522. else if Bpp = 1 then
  523. DstByte^ := PByteArray(SrcImage.Bits)[Round(SrcY) * SrcWidth + Round(SrcX)]
  524. else
  525. PColor32Rec(DstByte)^ := PColor32RecArray(SrcImage.Bits)[Round(SrcY) * SrcWidth + Round(SrcX)];
  526. end
  527. else
  528. CopyPixel(@BackColor32, DstByte, Bpp);
  529. Inc(DstByte, Bpp);
  530. end;
  531. end
  532. else if ResamplingFilter = rfLinear then
  533. begin
  534. if SrcImage.Format = ifR8G8B8 then
  535. begin
  536. DstPixel24 := DstImage.Bits;
  537. BackColor24 := TColor32Rec(BackgroundColor).Color24Rec;
  538. // RGB 24bit path
  539. for Y := 0 to DstHeight - 1 do
  540. for X := 0 to DstWidth - 1 do
  541. begin
  542. CalcSourceCoordinates(X, Y, SrcX, SrcY);
  543. if (SrcX >= -1) and (SrcY >= -1) and (SrcX <= SrcWidth) and (SrcY <= SrcHeight) then
  544. DstPixel24^ := Bilinear24(SrcX, SrcY)
  545. else
  546. DstPixel24^ := BackColor24;
  547. Inc(DstPixel24);
  548. end;
  549. end
  550. else
  551. begin
  552. // A bit more generic 8+32bit path
  553. for Y := 0 to DstHeight - 1 do
  554. for X := 0 to DstWidth - 1 do
  555. begin
  556. CalcSourceCoordinates(X, Y, SrcX, SrcY);
  557. if (SrcX >= -1) and (SrcY >= -1) and (SrcX <= SrcWidth) and (SrcY <= SrcHeight) then
  558. begin
  559. if Bpp = 1 then
  560. DstByte^ := Bilinear8(SrcX, SrcY)
  561. else
  562. PColor32Rec(DstByte)^ := Bilinear32(SrcX, SrcY)
  563. end
  564. else
  565. CopyPixel(@BackColor32, DstByte, Bpp);
  566. Inc(DstByte, Bpp);
  567. end;
  568. end;
  569. end
  570. else
  571. begin
  572. case ResamplingFilter of
  573. rfCubic: Filter := sfCatmullRom;
  574. rfLanczos: Filter := sfLanczos;
  575. else
  576. Assert(False);
  577. end;
  578. FilterFunction := ImagingFormats.SamplingFilterFunctions[Filter];
  579. FilterRadius := ImagingFormats.SamplingFilterRadii[Filter];
  580. {$IFDEF USE_FILTER_TABLE}
  581. KernelWidth := FastCeil(FilterRadius);
  582. PrecomputeFilterWeights;
  583. {$ENDIF}
  584. for Y := 0 to DstHeight - 1 do
  585. for X := 0 to DstWidth - 1 do
  586. begin
  587. CalcSourceCoordinates(X, Y, SrcX, SrcY);
  588. Pixel32 := FilterPixel(SrcX, SrcY, Bpp);
  589. CopyPixel(@Pixel32, DstByte, Bpp);
  590. Inc(DstByte, Bpp);
  591. end;
  592. end;
  593. FreeImage(SrcImage);
  594. Image := DstImage;
  595. end;
  596. end.