GR32_VectorMaps.pas 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  1. unit GR32_VectorMaps;
  2. (* ***** BEGIN LICENSE BLOCK *****
  3. * Version: MPL 1.1 or LGPL 2.1 with linking exception
  4. *
  5. * The contents of this file are subject to the Mozilla Public License Version
  6. * 1.1 (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. * http://www.mozilla.org/MPL/
  9. *
  10. * Software distributed under the License is distributed on an "AS IS" basis,
  11. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12. * for the specific language governing rights and limitations under the
  13. * License.
  14. *
  15. * Alternatively, the contents of this file may be used under the terms of the
  16. * Free Pascal modified version of the GNU Lesser General Public License
  17. * Version 2.1 (the "FPC modified LGPL License"), in which case the provisions
  18. * of this license are applicable instead of those above.
  19. * Please see the file LICENSE.txt for additional information concerning this
  20. * license.
  21. *
  22. * The Original Code is GR32_VectorMaps
  23. *
  24. * The Initial Developer of the Original Code is
  25. * Michael Hansen <[email protected]>
  26. *
  27. * Portions created by the Initial Developer are Copyright (C) 2000-2009
  28. * the Initial Developer. All Rights Reserved.
  29. *
  30. * Contributor(s):
  31. * Mattias Andersson <[email protected]>
  32. *
  33. * ***** END LICENSE BLOCK ***** *)
  34. interface
  35. {$I GR32.inc}
  36. uses
  37. {$IFDEF FPC}
  38. {$IFDEF Windows}
  39. Windows,
  40. {$ENDIF}
  41. {$ELSE}
  42. Windows,
  43. {$ENDIF}
  44. Classes, GR32;
  45. type
  46. TFixedVector = TFixedPoint;
  47. PFixedVector = ^TFixedVector;
  48. TFloatVector = TFloatPoint;
  49. PFloatVector = ^TFloatVector;
  50. TArrayOfFixedVector = array of TFixedVector;
  51. PArrayOfFixedVector = ^TArrayOfFixedVector;
  52. TArrayOfFloatVector = array of TFloatVector;
  53. PArrayOfFloatVector = ^TArrayOfFixedVector;
  54. type
  55. TVectorCombineMode = (vcmAdd, vcmReplace, vcmCustom);
  56. TVectorCombineEvent= procedure(F, P: TFixedVector; var B: TFixedVector) of object;
  57. TVectorMap = class(TCustomMap)
  58. private
  59. FVectors: TArrayOfFixedVector;
  60. FOnVectorCombine: TVectorCombineEvent;
  61. FVectorCombineMode: TVectorCombineMode;
  62. function GetVectors: PFixedPointArray;
  63. function GetFixedVector(X,Y: Integer): TFixedVector;
  64. function GetFixedVectorS(X,Y: Integer): TFixedVector;
  65. function GetFixedVectorX(X,Y: TFixed): TFixedVector;
  66. function GetFixedVectorXS(X,Y: TFixed): TFixedVector;
  67. function GetFloatVector(X,Y: Integer): TFloatVector;
  68. function GetFloatVectorS(X,Y: Integer): TFloatVector;
  69. function GetFloatVectorF(X,Y: Single): TFloatVector;
  70. function GetFloatVectorFS(X,Y: Single): TFloatVector;
  71. procedure SetFixedVector(X,Y: Integer; const Point: TFixedVector);
  72. procedure SetFixedVectorS(X,Y: Integer; const Point: TFixedVector);
  73. procedure SetFixedVectorX(X,Y: TFixed; const Point: TFixedVector);
  74. procedure SetFixedVectorXS(X,Y: TFixed; const Point: TFixedVector);
  75. procedure SetFloatVector(X,Y: Integer; const Point: TFloatVector);
  76. procedure SetFloatVectorS(X,Y: Integer; const Point: TFloatVector);
  77. procedure SetFloatVectorF(X,Y: Single; const Point: TFloatVector);
  78. procedure SetFloatVectorFS(X,Y: Single; const Point: TFloatVector);
  79. procedure SetVectorCombineMode(const Value: TVectorCombineMode);
  80. protected
  81. procedure ChangeSize(var Width, Height: Integer; NewWidth,
  82. NewHeight: Integer); override;
  83. public
  84. destructor Destroy; override;
  85. procedure Clear;
  86. procedure Merge(DstLeft, DstTop: Integer; Src: TVectorMap; SrcRect: TRect);
  87. property Vectors: PFixedPointArray read GetVectors;
  88. function BoundsRect: TRect;
  89. function GetTrimmedBounds: TRect;
  90. function Empty: Boolean; override;
  91. procedure LoadFromFile(const FileName: string);
  92. procedure SaveToFile(const FileName: string);
  93. property FixedVector[X, Y: Integer]: TFixedVector read GetFixedVector write SetFixedVector; default;
  94. property FixedVectorS[X, Y: Integer]: TFixedVector read GetFixedVectorS write SetFixedVectorS;
  95. property FixedVectorX[X, Y: TFixed]: TFixedVector read GetFixedVectorX write SetFixedVectorX;
  96. property FixedVectorXS[X, Y: TFixed]: TFixedVector read GetFixedVectorXS write SetFixedVectorXS;
  97. property FloatVector[X, Y: Integer]: TFloatVector read GetFloatVector write SetFloatVector;
  98. property FloatVectorS[X, Y: Integer]: TFloatVector read GetFloatVectorS write SetFloatVectorS;
  99. property FloatVectorF[X, Y: Single]: TFloatVector read GetFloatVectorF write SetFloatVectorF;
  100. property FloatVectorFS[X, Y: Single]: TFloatVector read GetFloatVectorFS write SetFloatVectorFS;
  101. published
  102. property VectorCombineMode: TVectorCombineMode read FVectorCombineMode write SetVectorCombineMode;
  103. property OnVectorCombine: TVectorCombineEvent read FOnVectorCombine write FOnVectorCombine;
  104. end;
  105. implementation
  106. uses
  107. GR32_Lowlevel, GR32_Math, SysUtils;
  108. resourcestring
  109. RCStrCantAllocateVectorMap = 'Can''t allocate VectorMap!';
  110. RCStrBadFormat = 'Bad format - Photoshop .msh expected!';
  111. RCStrFileNotFound = 'File not found!';
  112. RCStrSrcIsEmpty = 'Src is empty!';
  113. RCStrBaseIsEmpty = 'Base is empty!';
  114. { TVectorMap }
  115. function CombineVectorsReg(const A, B: TFixedVector; Weight: TFixed): TFixedVector;
  116. begin
  117. Result.X := FixedCombine(Weight, B.X, A.X);
  118. Result.Y := FixedCombine(Weight, B.Y, A.Y);
  119. end;
  120. procedure CombineVectorsMem(const A: TFixedVector;var B: TFixedVector; Weight: TFixed);
  121. begin
  122. B.X := FixedCombine(Weight, B.X, A.X);
  123. B.Y := FixedCombine(Weight, B.Y, A.Y);
  124. end;
  125. function TVectorMap.BoundsRect: TRect;
  126. begin
  127. Result := Rect(0, 0, Width, Height);
  128. end;
  129. procedure TVectorMap.ChangeSize(var Width, Height: Integer;
  130. NewWidth, NewHeight: Integer);
  131. begin
  132. inherited;
  133. FVectors := nil;
  134. Width := 0;
  135. Height := 0;
  136. SetLength(FVectors, NewWidth * NewHeight);
  137. if (NewWidth > 0) and (NewHeight > 0) then
  138. begin
  139. if FVectors = nil then raise Exception.Create(RCStrCantAllocateVectorMap);
  140. FillLongword(FVectors[0], NewWidth * NewHeight * 2, 0);
  141. end;
  142. Width := NewWidth;
  143. Height := NewHeight;
  144. end;
  145. procedure TVectorMap.Clear;
  146. begin
  147. FillLongword(FVectors[0], Width * Height * 2, 0);
  148. end;
  149. destructor TVectorMap.Destroy;
  150. begin
  151. Lock;
  152. try
  153. SetSize(0, 0);
  154. finally
  155. Unlock;
  156. end;
  157. inherited;
  158. end;
  159. function TVectorMap.GetVectors: PFixedPointArray;
  160. begin
  161. Result := @FVectors[0];
  162. end;
  163. function TVectorMap.GetFloatVector(X, Y: Integer): TFloatVector;
  164. begin
  165. Result := FloatPoint(FVectors[X + Y * Width]);
  166. end;
  167. function TVectorMap.GetFloatVectorF(X, Y: Single): TFloatVector;
  168. begin
  169. Result := FloatPoint(GetFixedVectorX(Fixed(X), Fixed(Y)));
  170. end;
  171. function TVectorMap.GetFloatVectorFS(X, Y: Single): TFloatVector;
  172. begin
  173. Result := FloatPoint(GetFixedVectorXS(Fixed(X), Fixed(Y)));
  174. end;
  175. function TVectorMap.GetFloatVectorS(X, Y: Integer): TFloatVector;
  176. begin
  177. if (X >= 0) and (Y >= 0) and
  178. (X < Width) and (Y < Height) then
  179. Result := GetFloatVector(X,Y)
  180. else
  181. begin
  182. Result.X := 0;
  183. Result.Y := 0;
  184. end;
  185. end;
  186. function TVectorMap.GetFixedVector(X, Y: Integer): TFixedVector;
  187. begin
  188. Result := FVectors[X + Y * Width];
  189. end;
  190. function TVectorMap.GetFixedVectorS(X, Y: Integer): TFixedVector;
  191. begin
  192. if (X >= 0) and (Y >= 0) and
  193. (X < Width) and (Y < Height) then
  194. Result := GetFixedVector(X,Y)
  195. else
  196. begin
  197. Result.X := 0;
  198. Result.Y := 0;
  199. end;
  200. end;
  201. function TVectorMap.GetFixedVectorX(X, Y: TFixed): TFixedVector;
  202. const
  203. Next = SizeOf(TFixedVector);
  204. var
  205. WX,WY: TFixed;
  206. W, H: Integer;
  207. P: Pointer;
  208. begin
  209. WX := TFixedRec(X).Int;
  210. WY := TFixedRec(Y).Int;
  211. W := Width;
  212. H := Height;
  213. if (WX >= 0) and (WX <= W - 1) and (WY >= 0) and (WY <= H - 1) then
  214. begin
  215. P := @FVectors[WX + WY * W];
  216. if (WY = H - 1) then W := 0 else W := W * Next;
  217. if (WX = W - 1) then H := 0 else H := Next;
  218. WX := TFixedRec(X).Frac;
  219. WY := TFixedRec(Y).Frac;
  220. {$IFDEF HAS_NATIVEINT}
  221. Result := CombineVectorsReg(CombineVectorsReg(PFixedPoint(P)^,
  222. PFixedPoint(NativeUInt(P) + NativeUInt(H))^, WX), CombineVectorsReg(
  223. PFixedPoint(NativeUInt(P) + NativeUInt(W))^,
  224. PFixedPoint(NativeUInt(P) + NativeUInt(W + H))^, WX), WY);
  225. {$ELSE}
  226. Result := CombineVectorsReg(CombineVectorsReg(PFixedPoint(P)^,
  227. PFixedPoint(Cardinal(P) + Cardinal(H))^, WX), CombineVectorsReg(
  228. PFixedPoint(Cardinal(P) + Cardinal(W))^,
  229. PFixedPoint(Cardinal(P) + Cardinal(W) + Cardinal(H))^, WX), WY);
  230. {$ENDIF}
  231. end else
  232. begin
  233. Result.X := 0;
  234. Result.Y := 0;
  235. end;
  236. end;
  237. function TVectorMap.GetFixedVectorXS(X, Y: TFixed): TFixedVector;
  238. var
  239. WX,WY: TFixed;
  240. begin
  241. WX := TFixedRec(X).Frac;
  242. X := TFixedRec(X).Int;
  243. WY := TFixedRec(Y).Frac;
  244. Y := TFixedRec(Y).Int;
  245. Result := CombineVectorsReg(CombineVectorsReg(FixedVectorS[X,Y], FixedVectorS[X + 1,Y], WX),
  246. CombineVectorsReg(FixedVectorS[X,Y + 1], FixedVectorS[X + 1,Y + 1], WX), WY);
  247. end;
  248. function TVectorMap.Empty: Boolean;
  249. begin
  250. Result := false;
  251. if (Width = 0) or (Height = 0) or (FVectors = nil) then Result := True;
  252. end;
  253. const
  254. MeshIdent = 'yfqLhseM';
  255. type
  256. {TVectorMap supports the photoshop liquify mesh fileformat .msh}
  257. TPSLiquifyMeshHeader = record
  258. Pad0 : dword;
  259. Ident : array [0..7] of Char;
  260. Pad1 : dword;
  261. Width : dword;
  262. Height: dword;
  263. end;
  264. procedure TVectorMap.LoadFromFile(const FileName: string);
  265. procedure ConvertVertices;
  266. var
  267. I: Integer;
  268. begin
  269. for I := 0 to Length(FVectors) - 1 do
  270. begin
  271. //Not a mistake! Converting physical mem. directly to avoid temporary floating point buffer
  272. //Do no change to PFloat.. the type is relative to the msh format.
  273. FVectors[I].X := Fixed(PSingle(@FVectors[I].X)^);
  274. FVectors[I].Y := Fixed(PSingle(@FVectors[I].Y)^);
  275. end;
  276. end;
  277. var
  278. Header: TPSLiquifyMeshHeader;
  279. MeshFile: File;
  280. begin
  281. If FileExists(Filename) then
  282. try
  283. AssignFile(MeshFile, FileName);
  284. Reset(MeshFile, 1);
  285. BlockRead(MeshFile, Header, SizeOf(TPSLiquifyMeshHeader));
  286. if LowerCase(string(Header.Ident)) <> LowerCase(MeshIdent) then
  287. Exception.Create(RCStrBadFormat);
  288. with Header do
  289. begin
  290. SetSize(Width, Height);
  291. BlockRead(MeshFile, FVectors[0], Width * Height * SizeOf(TFixedVector));
  292. ConvertVertices;
  293. end;
  294. finally
  295. CloseFile(MeshFile);
  296. end
  297. else Exception.Create(RCStrFileNotFound);
  298. end;
  299. procedure TVectorMap.Merge(DstLeft, DstTop: Integer; Src: TVectorMap; SrcRect: TRect);
  300. var
  301. I,J,P: Integer;
  302. DstRect: TRect;
  303. Progression: TFixedVector;
  304. ProgressionX, ProgressionY: TFixed;
  305. CombineCallback: TVectorCombineEvent;
  306. DstPtr : PFixedPointArray;
  307. SrcPtr : PFixedPoint;
  308. begin
  309. if Src.Empty then Exception.Create(RCStrSrcIsEmpty);
  310. if Empty then Exception.Create(RCStrBaseIsEmpty);
  311. IntersectRect( SrcRect, Src.BoundsRect, SrcRect);
  312. DstRect.Left := DstLeft;
  313. DstRect.Top := DstTop;
  314. DstRect.Right := DstLeft + (SrcRect.Right - SrcRect.Left);
  315. DstRect.Bottom := DstTop + (SrcRect.Bottom - SrcRect.Top);
  316. IntersectRect(DstRect, BoundsRect, DstRect);
  317. if IsRectEmpty(DstRect) then Exit;
  318. P := SrcRect.Top * Src.Width;
  319. Progression.Y := - FixedOne;
  320. case Src.FVectorCombineMode of
  321. vcmAdd:
  322. begin
  323. for I := DstRect.Top to DstRect.Bottom do
  324. begin
  325. // Added ^ for FPC
  326. DstPtr := @GetVectors^[I * Width];
  327. SrcPtr := @Src.GetVectors^[SrcRect.Left + P];
  328. for J := DstRect.Left to DstRect.Right do
  329. begin
  330. Inc(SrcPtr^.X, DstPtr[J].X);
  331. Inc(SrcPtr^.Y, DstPtr[J].Y);
  332. Inc(SrcPtr);
  333. end;
  334. Inc(P, Src.Width);
  335. end;
  336. end;
  337. vcmReplace:
  338. begin
  339. for I := DstRect.Top to DstRect.Bottom do
  340. begin
  341. // Added ^ for FPC
  342. DstPtr := @GetVectors^[I * Width];
  343. SrcPtr := @Src.GetVectors^[SrcRect.Left + P];
  344. for J := DstRect.Left to DstRect.Right do
  345. begin
  346. SrcPtr^.X := DstPtr[J].X;
  347. SrcPtr^.Y := DstPtr[J].Y;
  348. Inc(SrcPtr);
  349. end;
  350. Inc(P, Src.Width);
  351. end;
  352. end;
  353. else
  354. CombineCallback := Src.FOnVectorCombine;
  355. ProgressionX := Fixed(2 / (DstRect.Right - DstRect.Left - 1));
  356. ProgressionY := Fixed(2 / (DstRect.Bottom - DstRect.Top - 1));
  357. for I := DstRect.Top to DstRect.Bottom do
  358. begin
  359. Progression.X := - FixedOne;
  360. // Added ^ for FPC
  361. DstPtr := @GetVectors^[I * Width];
  362. SrcPtr := @Src.GetVectors^[SrcRect.Left + P];
  363. for J := DstRect.Left to DstRect.Right do
  364. begin
  365. CombineCallback(SrcPtr^, Progression, DstPtr[J]);
  366. Inc(SrcPtr);
  367. Inc(Progression.X, ProgressionX);
  368. end;
  369. Inc(P, Src.Width);
  370. Inc(Progression.Y, ProgressionY);
  371. end;
  372. end;
  373. end;
  374. procedure TVectorMap.SaveToFile(const FileName: string);
  375. procedure ConvertVerticesX;
  376. var
  377. I: Integer;
  378. begin
  379. for I := 0 to Length(FVectors) - 1 do
  380. begin
  381. //Not a mistake! Converting physical mem. directly to avoid temporary floating point buffer
  382. //Do no change to PFloat.. the type is relative to the msh format.
  383. FVectors[I].X := Fixed(PSingle(@FVectors[I].X)^);
  384. FVectors[I].Y := Fixed(PSingle(@FVectors[I].Y)^);
  385. end;
  386. end;
  387. procedure ConvertVerticesF;
  388. var
  389. I: Integer;
  390. {$IFDEF COMPILERRX1}
  391. f: single;
  392. {$ENDIF}
  393. begin
  394. for I := 0 to Length(FVectors) - 1 do
  395. begin
  396. //Not a mistake! Converting physical mem. directly to avoid temporary floating point buffer
  397. //Do no change to PFloat.. the type is relative to the msh format.
  398. //Workaround for Delphi 10.1 Internal Error C6949 ...
  399. {$IFDEF COMPILERRX1}
  400. f := FVectors[I].X * FixedToFloat;
  401. FVectors[I].X := PInteger(@f)^;
  402. f := FVectors[I].Y * FixedToFloat;
  403. FVectors[I].Y := PInteger(@f)^;
  404. {$ELSE}
  405. PSingle(@FVectors[I].X)^ := FVectors[I].X * FixedToFloat;
  406. PSingle(@FVectors[I].Y)^ := FVectors[I].Y * FixedToFloat;
  407. {$ENDIF}
  408. end;
  409. end;
  410. var
  411. Header: TPSLiquifyMeshHeader;
  412. MeshFile: File;
  413. Pad: Cardinal;
  414. begin
  415. try
  416. AssignFile(MeshFile, FileName);
  417. Rewrite(MeshFile, 1);
  418. with Header do
  419. begin
  420. Pad0 := $02000000;
  421. Ident := MeshIdent;
  422. Pad1 := $00000002;
  423. Width := Self.Width;
  424. Height := Self.Height;
  425. end;
  426. BlockWrite(MeshFile, Header, SizeOf(TPSLiquifyMeshHeader));
  427. with Header do
  428. begin
  429. ConvertVerticesF;
  430. BlockWrite(MeshFile, FVectors[0], Length(FVectors) * SizeOf(TFixedVector));
  431. ConvertVerticesX;
  432. end;
  433. if Odd(Length(FVectors) * SizeOf(TFixedVector) - 1) then
  434. begin
  435. Pad := $00000000;
  436. BlockWrite(MeshFile, Pad, 4);
  437. BlockWrite(MeshFile, Pad, 4);
  438. end;
  439. finally
  440. CloseFile(MeshFile);
  441. end;
  442. end;
  443. procedure TVectorMap.SetFloatVector(X, Y: Integer; const Point: TFloatVector);
  444. begin
  445. FVectors[X + Y * Width] := FixedPoint(Point);
  446. end;
  447. procedure TVectorMap.SetFloatVectorF(X, Y: Single; const Point: TFloatVector);
  448. begin
  449. SetFixedVectorX(Fixed(X), Fixed(Y), FixedPoint(Point));
  450. end;
  451. procedure TVectorMap.SetFloatVectorFS(X, Y: Single; const Point: TFloatVector);
  452. begin
  453. SetFixedVectorXS(Fixed(X), Fixed(Y), FixedPoint(Point));
  454. end;
  455. procedure TVectorMap.SetFloatVectorS(X, Y: Integer; const Point: TFloatVector);
  456. begin
  457. if (X >= 0) and (X < Width) and
  458. (Y >= 0) and (Y < Height) then
  459. FVectors[X + Y * Width] := FixedPoint(Point);
  460. end;
  461. procedure TVectorMap.SetFixedVector(X, Y: Integer; const Point: TFixedVector);
  462. begin
  463. FVectors[X + Y * Width] := Point;
  464. end;
  465. procedure TVectorMap.SetFixedVectorS(X, Y: Integer; const Point: TFixedVector);
  466. begin
  467. if (X >= 0) and (X < Width) and
  468. (Y >= 0) and (Y < Height) then
  469. FVectors[X + Y * Width] := Point;
  470. end;
  471. procedure TVectorMap.SetFixedVectorX(X, Y: TFixed; const Point: TFixedVector);
  472. var
  473. flrx, flry, celx, cely: Integer;
  474. P: PFixedPoint;
  475. begin
  476. flrx := TFixedRec(X).Frac;
  477. celx := flrx xor $FFFF;
  478. flry := TFixedRec(Y).Frac;
  479. cely := flry xor $FFFF;
  480. P := @FVectors[TFixedRec(X).Int + TFixedRec(Y).Int * Width];
  481. CombineVectorsMem(Point, P^, FixedMul(celx, cely)); Inc(P);
  482. CombineVectorsMem(Point, P^, FixedMul(flrx, cely)); Inc(P, Width);
  483. CombineVectorsMem(Point, P^, FixedMul(flrx, flry)); Dec(P);
  484. CombineVectorsMem(Point, P^, FixedMul(celx, flry));
  485. end;
  486. procedure TVectorMap.SetFixedVectorXS(X, Y: TFixed; const Point: TFixedVector);
  487. var
  488. flrx, flry, celx, cely: Integer;
  489. P: PFixedPoint;
  490. begin
  491. if (X < -$10000) or (Y < -$10000) then Exit;
  492. flrx := TFixedRec(X).Frac;
  493. X := TFixedRec(X).Int;
  494. flry := TFixedRec(Y).Frac;
  495. Y := TFixedRec(Y).Int;
  496. if (X >= Width) or (Y >= Height) then Exit;
  497. celx := flrx xor $FFFF;
  498. cely := flry xor $FFFF;
  499. P := @FVectors[X + Y * Width];
  500. if (X >= 0) and (Y >= 0)then
  501. begin
  502. CombineVectorsMem(Point, P^, FixedMul(celx, cely) ); Inc(P);
  503. CombineVectorsMem(Point, P^, FixedMul(flrx, cely) ); Inc(P, Width);
  504. CombineVectorsMem(Point, P^, FixedMul(flrx, flry) ); Dec(P);
  505. CombineVectorsMem(Point, P^, FixedMul(celx, flry) );
  506. end
  507. else
  508. begin
  509. if (X >= 0) and (Y >= 0) then CombineVectorsMem(Point, P^, FixedMul(celx, cely)); Inc(P);
  510. if (X < Width - 1) and (Y >= 0) then CombineVectorsMem(Point, P^, FixedMul(flrx, cely)); Inc(P, Width);
  511. if (X < Width - 1) and (Y < Height - 1) then CombineVectorsMem(Point, P^, FixedMul(flrx, flry)); Dec(P);
  512. if (X >= 0) and (Y < Height - 1) then CombineVectorsMem(Point, P^, FixedMul(celx, flry));
  513. end;
  514. end;
  515. procedure TVectorMap.SetVectorCombineMode(const Value: TVectorCombineMode);
  516. begin
  517. if FVectorCombineMode <> Value then
  518. begin
  519. FVectorCombineMode := Value;
  520. Changed;
  521. end;
  522. end;
  523. function TVectorMap.GetTrimmedBounds: TRect;
  524. var
  525. J: Integer;
  526. VectorPtr : PFixedVector;
  527. label
  528. TopDone, BottomDone, LeftDone, RightDone;
  529. begin
  530. with Result do
  531. begin
  532. //Find Top
  533. Top := 0;
  534. VectorPtr := @Vectors[Top];
  535. repeat
  536. if Int64(VectorPtr^) <> 0 then goto TopDone;
  537. Inc(VectorPtr);
  538. Inc(Top);
  539. until Top = Self.Width * Self.Height;
  540. TopDone: Top := Top div Self.Width;
  541. //Find Bottom
  542. Bottom := Self.Width * Self.Height - 1;
  543. VectorPtr := @Vectors[Bottom];
  544. repeat
  545. if Int64(VectorPtr^) <> 0 then goto BottomDone;
  546. Dec(VectorPtr);
  547. Dec(Bottom);
  548. until Bottom < 0;
  549. BottomDone: Bottom := Bottom div Self.Width - 1;
  550. //Find Left
  551. Left := 0;
  552. repeat
  553. J := Top;
  554. repeat
  555. if Int64(FixedVector[Left, J]) <> 0 then goto LeftDone;
  556. Inc(J);
  557. until J >= Bottom;
  558. Inc(Left)
  559. until Left >= Self.Width;
  560. LeftDone:
  561. //Find Right
  562. Right := Self.Width - 1;
  563. repeat
  564. J := Bottom;
  565. repeat
  566. if Int64(FixedVector[Right, J]) <> 0 then goto RightDone;
  567. Dec(J);
  568. until J <= Top;
  569. Dec(Right)
  570. until Right <= Left;
  571. end;
  572. RightDone:
  573. if IsRectEmpty(Result) then
  574. Result := Rect(0, 0, 0, 0);
  575. end;
  576. end.