GR32_OrdinalMaps.pas 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937
  1. unit GR32_OrdinalMaps;
  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 Graphics32
  23. *
  24. * The Initial Developer of the Original Code is
  25. * Mattias Andersson
  26. * (parts of this unit were merged from GR32_ByteMaps.pas by Alex A. Denisov)
  27. *
  28. * Portions created by the Initial Developer are Copyright (C) 2000-2009
  29. * the Initial Developer. All Rights Reserved.
  30. *
  31. * Contributor(s):
  32. * Michael Hansen
  33. *
  34. * ***** END LICENSE BLOCK ***** *)
  35. interface
  36. {$I GR32.inc}
  37. uses
  38. Classes, SysUtils, GR32;
  39. type
  40. TConversionType = (ctRed, ctGreen, ctBlue, ctAlpha, ctUniformRGB,
  41. ctWeightedRGB);
  42. TBooleanMap = class(TCustomMap)
  43. private
  44. function GetValue(X, Y: Integer): Boolean;
  45. procedure SetValue(X, Y: Integer; const Value: Boolean);
  46. protected
  47. FBits: PByteArray;
  48. procedure ChangeSize(var Width, Height: Integer; NewWidth, NewHeight: Integer); override;
  49. public
  50. constructor Create; overload; override;
  51. destructor Destroy; override;
  52. function Empty: Boolean; override;
  53. procedure Clear(FillValue: Byte);
  54. procedure ToggleBit(X, Y: Integer);
  55. property Value[X, Y: Integer]: Boolean read GetValue write SetValue; default;
  56. property Bits: PByteArray read FBits;
  57. end;
  58. TByteMap = class(TCustomMap)
  59. private
  60. function GetValue(X, Y: Integer): Byte; {$IFDEF INLININGSUPPORTED} inline; {$ENDIF}
  61. function GetValPtr(X, Y: Integer): PByte; {$IFDEF INLININGSUPPORTED} inline; {$ENDIF}
  62. procedure SetValue(X, Y: Integer; Value: Byte); {$IFDEF INLININGSUPPORTED} inline; {$ENDIF}
  63. function GetScanline(Y: Integer): PByteArray;
  64. protected
  65. FBits: PByteArray;
  66. procedure AssignTo(Dst: TPersistent); override;
  67. procedure ChangeSize(var Width, Height: Integer; NewWidth, NewHeight: Integer); override;
  68. public
  69. constructor Create; overload; override;
  70. destructor Destroy; override;
  71. procedure Assign(Source: TPersistent); override;
  72. function Empty: Boolean; override;
  73. procedure Clear(FillValue: Byte);
  74. procedure ReadFrom(Source: TCustomBitmap32; Conversion: TConversionType);
  75. procedure WriteTo(Dest: TCustomBitmap32; Conversion: TConversionType); overload;
  76. procedure WriteTo(Dest: TCustomBitmap32; const Palette: TPalette32); overload;
  77. property Bits: PByteArray read FBits;
  78. property Scanline[Y: Integer]: PByteArray read GetScanline;
  79. property ValPtr[X, Y: Integer]: PByte read GetValPtr;
  80. property Value[X, Y: Integer]: Byte read GetValue write SetValue; default;
  81. end;
  82. { TWordMap }
  83. TWordMap = class(TCustomMap)
  84. private
  85. function GetValPtr(X, Y: Integer): PWord; {$IFDEF INLININGSUPPORTED} inline; {$ENDIF}
  86. function GetValue(X, Y: Integer): Word; {$IFDEF INLININGSUPPORTED} inline; {$ENDIF}
  87. procedure SetValue(X, Y: Integer; const Value: Word); {$IFDEF INLININGSUPPORTED} inline; {$ENDIF}
  88. function GetScanline(Y: Integer): PWordArray;
  89. protected
  90. FBits: PWordArray;
  91. procedure ChangeSize(var Width, Height: Integer; NewWidth, NewHeight: Integer); override;
  92. public
  93. constructor Create; overload; override;
  94. destructor Destroy; override;
  95. procedure Assign(Source: TPersistent); override;
  96. function Empty: Boolean; override;
  97. procedure Clear(FillValue: Word);
  98. property ValPtr[X, Y: Integer]: PWord read GetValPtr;
  99. property Value[X, Y: Integer]: Word read GetValue write SetValue; default;
  100. property Bits: PWordArray read FBits;
  101. property Scanline[Y: Integer]: PWordArray read GetScanline;
  102. end;
  103. { TIntegerMap }
  104. TIntegerMap = class(TCustomMap)
  105. private
  106. function GetValPtr(X, Y: Integer): PInteger; {$IFDEF INLININGSUPPORTED} inline; {$ENDIF}
  107. function GetValue(X, Y: Integer): Integer; {$IFDEF INLININGSUPPORTED} inline; {$ENDIF}
  108. procedure SetValue(X, Y: Integer; const Value: Integer); {$IFDEF INLININGSUPPORTED} inline; {$ENDIF}
  109. function GetScanline(Y: Integer): PIntegerArray;
  110. protected
  111. FBits: PIntegerArray;
  112. procedure ChangeSize(var Width, Height: Integer; NewWidth, NewHeight: Integer); override;
  113. public
  114. constructor Create; overload; override;
  115. destructor Destroy; override;
  116. procedure Assign(Source: TPersistent); override;
  117. function Empty: Boolean; override;
  118. procedure Clear(FillValue: Integer = 0);
  119. property ValPtr[X, Y: Integer]: PInteger read GetValPtr;
  120. property Value[X, Y: Integer]: Integer read GetValue write SetValue; default;
  121. property Bits: PIntegerArray read FBits;
  122. property Scanline[Y: Integer]: PIntegerArray read GetScanline;
  123. end;
  124. { TCardinalMap }
  125. TCardinalMap = class(TCustomMap)
  126. private
  127. function GetValPtr(X, Y: Cardinal): PCardinal; {$IFDEF INLININGSUPPORTED} inline; {$ENDIF}
  128. function GetValue(X, Y: Cardinal): Cardinal; {$IFDEF INLININGSUPPORTED} inline; {$ENDIF}
  129. procedure SetValue(X, Y: Cardinal; const Value: Cardinal); {$IFDEF INLININGSUPPORTED} inline; {$ENDIF}
  130. function GetScanline(Y: Integer): PCardinalArray;
  131. protected
  132. FBits: PCardinalArray;
  133. procedure ChangeSize(var Width, Height: Integer; NewWidth, NewHeight: Integer); override;
  134. public
  135. constructor Create; overload; override;
  136. destructor Destroy; override;
  137. procedure Assign(Source: TPersistent); override;
  138. function Empty: Boolean; override;
  139. procedure Clear(FillValue: Cardinal = 0);
  140. property ValPtr[X, Y: Cardinal]: PCardinal read GetValPtr;
  141. property Value[X, Y: Cardinal]: Cardinal read GetValue write SetValue; default;
  142. property Bits: PCardinalArray read FBits;
  143. property Scanline[Y: Integer]: PCardinalArray read GetScanline;
  144. end;
  145. { TFloatMap }
  146. TFloatMap = class(TCustomMap)
  147. private
  148. function GetValPtr(X, Y: Integer): PFloat; {$IFDEF INLININGSUPPORTED} inline; {$ENDIF}
  149. function GetValue(X, Y: Integer): TFloat; {$IFDEF INLININGSUPPORTED} inline; {$ENDIF}
  150. procedure SetValue(X, Y: Integer; const Value: TFloat); {$IFDEF INLININGSUPPORTED} inline; {$ENDIF}
  151. function GetScanline(Y: Integer): PFloatArray;
  152. protected
  153. FBits: PFloatArray;
  154. procedure ChangeSize(var Width, Height: Integer; NewWidth, NewHeight: Integer); override;
  155. public
  156. constructor Create; overload; override;
  157. destructor Destroy; override;
  158. procedure Assign(Source: TPersistent); override;
  159. function Empty: Boolean; override;
  160. procedure Clear; overload;
  161. procedure Clear(FillValue: TFloat); overload;
  162. property ValPtr[X, Y: Integer]: PFloat read GetValPtr;
  163. property Value[X, Y: Integer]: TFloat read GetValue write SetValue; default;
  164. property Bits: PFloatArray read FBits;
  165. property Scanline[Y: Integer]: PFloatArray read GetScanline;
  166. end;
  167. {$IFDEF COMPILER2010}
  168. { TGenericMap<T> }
  169. TGenericMap<T> = class(TCustomMap)
  170. private
  171. function GetValue(X, Y: Integer): T; {$IFDEF INLININGSUPPORTED} inline; {$ENDIF}
  172. procedure SetValue(X, Y: Integer; const Value: T); {$IFDEF INLININGSUPPORTED} inline; {$ENDIF}
  173. protected
  174. FBits: Pointer;
  175. procedure ChangeSize(var Width, Height: Integer; NewWidth, NewHeight: Integer); override;
  176. public
  177. constructor Create; overload; override;
  178. destructor Destroy; override;
  179. procedure Assign(Source: TPersistent); override;
  180. function Empty: Boolean; override;
  181. procedure Clear; overload;
  182. procedure Clear(FillValue: T); overload;
  183. property Value[X, Y: Integer]: T read GetValue write SetValue; default;
  184. property Bits: Pointer read FBits;
  185. end;
  186. {$ENDIF}
  187. implementation
  188. uses
  189. GR32_LowLevel;
  190. function Bytes(Bits: Integer): Integer;
  191. begin
  192. Result := (Bits - 1) shr 3 + 1;
  193. end;
  194. { TBooleanMap }
  195. constructor TBooleanMap.Create;
  196. begin
  197. FreeMem(FBits);
  198. inherited Create;
  199. end;
  200. procedure TBooleanMap.ChangeSize(var Width, Height: Integer; NewWidth,
  201. NewHeight: Integer);
  202. begin
  203. ReallocMem(FBits, Bytes(NewWidth * NewHeight));
  204. Width := NewWidth;
  205. Height := NewHeight;
  206. end;
  207. procedure TBooleanMap.Clear(FillValue: Byte);
  208. begin
  209. FillChar(FBits^, Bytes(Width * Height), FillValue);
  210. end;
  211. destructor TBooleanMap.Destroy;
  212. begin
  213. FBits := nil;
  214. inherited;
  215. end;
  216. function TBooleanMap.Empty: Boolean;
  217. begin
  218. Result := not Assigned(FBits);
  219. end;
  220. function TBooleanMap.GetValue(X, Y: Integer): Boolean;
  221. begin
  222. X := X + Y * Width;
  223. Result := FBits^[X shr 3] and (1 shl (X and 7)) <> 0; //Boolean(FBits^[X shr 3] and (1 shl (X and 7)));
  224. end;
  225. procedure TBooleanMap.SetValue(X, Y: Integer; const Value: Boolean);
  226. begin
  227. X := Y * Width + X;
  228. if Value then
  229. FBits^[X shr 3] := FBits^[X shr 3] or (1 shl (X and 7))
  230. else
  231. FBits^[X shr 3] := FBits^[X shr 3] and ((1 shl (X and 7)) xor $FF);
  232. end;
  233. procedure TBooleanMap.ToggleBit(X, Y: Integer);
  234. begin
  235. X := Y * Width + X;
  236. FBits^[X shr 3] := FBits^[X shr 3] xor (1 shl (X and 7));
  237. end;
  238. { TByteMap }
  239. constructor TByteMap.Create;
  240. begin
  241. FBits := nil;
  242. inherited Create;
  243. end;
  244. destructor TByteMap.Destroy;
  245. begin
  246. FreeMem(FBits);
  247. inherited;
  248. end;
  249. procedure TByteMap.Assign(Source: TPersistent);
  250. begin
  251. BeginUpdate;
  252. try
  253. if Source is TByteMap then
  254. begin
  255. inherited SetSize(TByteMap(Source).Width, TByteMap(Source).Height);
  256. Move(TByteMap(Source).Bits[0], Bits[0], Width * Height);
  257. end
  258. else if Source is TBitmap32 then
  259. ReadFrom(TBitmap32(Source), ctWeightedRGB)
  260. else
  261. inherited;
  262. finally
  263. EndUpdate;
  264. Changed;
  265. end;
  266. end;
  267. procedure TByteMap.AssignTo(Dst: TPersistent);
  268. begin
  269. if Dst is TBitmap32 then WriteTo(TBitmap32(Dst), ctUniformRGB)
  270. else inherited;
  271. end;
  272. procedure TByteMap.ChangeSize(var Width, Height: Integer; NewWidth, NewHeight: Integer);
  273. begin
  274. ReallocMem(FBits, NewWidth * NewHeight);
  275. Width := NewWidth;
  276. Height := NewHeight;
  277. end;
  278. procedure TByteMap.Clear(FillValue: Byte);
  279. begin
  280. FillChar(Bits^, Width * Height, FillValue);
  281. Changed;
  282. end;
  283. function TByteMap.Empty: Boolean;
  284. begin
  285. Result := False;
  286. if (Width = 0) or (Height = 0) or (FBits = nil) then Result := True;
  287. end;
  288. function TByteMap.GetScanline(Y: Integer): PByteArray;
  289. begin
  290. Result := @FBits^[Y * Width];
  291. end;
  292. function TByteMap.GetValPtr(X, Y: Integer): PByte;
  293. begin
  294. Result := @FBits^[X + Y * Width];
  295. end;
  296. function TByteMap.GetValue(X, Y: Integer): Byte;
  297. begin
  298. Result := FBits^[X + Y * Width];
  299. end;
  300. procedure TByteMap.ReadFrom(Source: TCustomBitmap32; Conversion: TConversionType);
  301. var
  302. W, H, I, N: Integer;
  303. SrcC: PColor32;
  304. SrcB, DstB: PByte;
  305. Value: TColor32;
  306. begin
  307. BeginUpdate;
  308. try
  309. SetSize(Source.Width, Source.Height);
  310. if Empty then Exit;
  311. W := Source.Width;
  312. H := Source.Height;
  313. N := W * H - 1;
  314. SrcC := Source.PixelPtr[0, 0];
  315. SrcB := Pointer(SrcC);
  316. DstB := @FBits^;
  317. case Conversion of
  318. ctRed:
  319. begin
  320. Inc(SrcB, 2);
  321. for I := 0 to N do
  322. begin
  323. DstB^ := SrcB^;
  324. Inc(DstB);
  325. Inc(SrcB, 4);
  326. end;
  327. end;
  328. ctGreen:
  329. begin
  330. Inc(SrcB, 1);
  331. for I := 0 to N do
  332. begin
  333. DstB^ := SrcB^;
  334. Inc(DstB);
  335. Inc(SrcB, 4);
  336. end;
  337. end;
  338. ctBlue:
  339. begin
  340. for I := 0 to N do
  341. begin
  342. DstB^ := SrcB^;
  343. Inc(DstB);
  344. Inc(SrcB, 4);
  345. end;
  346. end;
  347. ctAlpha:
  348. begin
  349. Inc(SrcB, 3);
  350. for I := 0 to N do
  351. begin
  352. DstB^ := SrcB^;
  353. Inc(DstB);
  354. Inc(SrcB, 4);
  355. end;
  356. end;
  357. ctUniformRGB:
  358. begin
  359. for I := 0 to N do
  360. begin
  361. Value := SrcC^;
  362. Value := (Value and $00FF0000) shr 16 + (Value and $0000FF00) shr 8 +
  363. (Value and $000000FF);
  364. Value := Value div 3;
  365. DstB^ := Value;
  366. Inc(DstB);
  367. Inc(SrcC);
  368. end;
  369. end;
  370. ctWeightedRGB:
  371. begin
  372. for I := 0 to N do
  373. begin
  374. DstB^ := Intensity(SrcC^);
  375. Inc(DstB);
  376. Inc(SrcC);
  377. end;
  378. end;
  379. end;
  380. finally
  381. EndUpdate;
  382. Changed;
  383. end;
  384. end;
  385. procedure TByteMap.SetValue(X, Y: Integer; Value: Byte);
  386. begin
  387. FBits^[X + Y * Width] := Value;
  388. end;
  389. procedure TByteMap.WriteTo(Dest: TCustomBitmap32; Conversion: TConversionType);
  390. var
  391. W, H, I, N: Integer;
  392. DstC: PColor32;
  393. DstB, SrcB: PByte;
  394. Resized: Boolean;
  395. begin
  396. Dest.BeginUpdate;
  397. Resized := False;
  398. try
  399. Resized := Dest.SetSize(Width, Height);
  400. if Empty then Exit;
  401. W := Width;
  402. H := Height;
  403. N := W * H - 1;
  404. DstC := Dest.PixelPtr[0, 0];
  405. DstB := Pointer(DstC);
  406. SrcB := @FBits^;
  407. case Conversion of
  408. ctRed:
  409. begin
  410. Inc(DstB, 2);
  411. for I := 0 to N do
  412. begin
  413. DstB^ := SrcB^;
  414. Inc(DstB, 4);
  415. Inc(SrcB);
  416. end;
  417. end;
  418. ctGreen:
  419. begin
  420. Inc(DstB, 1);
  421. for I := 0 to N do
  422. begin
  423. DstB^ := SrcB^;
  424. Inc(DstB, 4);
  425. Inc(SrcB);
  426. end;
  427. end;
  428. ctBlue:
  429. begin
  430. for I := 0 to N do
  431. begin
  432. DstB^ := SrcB^;
  433. Inc(DstB, 4);
  434. Inc(SrcB);
  435. end;
  436. end;
  437. ctAlpha:
  438. begin
  439. Inc(DstB, 3);
  440. for I := 0 to N do
  441. begin
  442. DstB^ := SrcB^;
  443. Inc(DstB, 4);
  444. Inc(SrcB);
  445. end;
  446. end;
  447. ctUniformRGB, ctWeightedRGB:
  448. begin
  449. for I := 0 to N do
  450. begin
  451. DstC^ := Gray32(SrcB^);
  452. Inc(DstC);
  453. Inc(SrcB);
  454. end;
  455. end;
  456. end;
  457. finally
  458. Dest.EndUpdate;
  459. Dest.Changed;
  460. if Resized then Dest.Resized;
  461. end;
  462. end;
  463. procedure TByteMap.WriteTo(Dest: TCustomBitmap32; const Palette: TPalette32);
  464. var
  465. W, H, I, N: Integer;
  466. DstC: PColor32;
  467. SrcB: PByte;
  468. begin
  469. Dest.BeginUpdate;
  470. try
  471. Dest.SetSize(Width, Height);
  472. if Empty then Exit;
  473. W := Width;
  474. H := Height;
  475. N := W * H - 1;
  476. DstC := Dest.PixelPtr[0, 0];
  477. SrcB := @FBits^;
  478. for I := 0 to N do
  479. begin
  480. DstC^ := Palette[SrcB^];
  481. Inc(DstC);
  482. Inc(SrcB);
  483. end;
  484. finally
  485. Dest.EndUpdate;
  486. Dest.Changed;
  487. end;
  488. end;
  489. { TWordMap }
  490. constructor TWordMap.Create;
  491. begin
  492. FBits := nil;
  493. inherited Create;
  494. end;
  495. destructor TWordMap.Destroy;
  496. begin
  497. FreeMem(FBits);
  498. inherited;
  499. end;
  500. procedure TWordMap.ChangeSize(var Width, Height: Integer; NewWidth,
  501. NewHeight: Integer);
  502. begin
  503. ReallocMem(FBits, NewWidth * NewHeight * SizeOf(Word));
  504. Width := NewWidth;
  505. Height := NewHeight;
  506. end;
  507. procedure TWordMap.Clear(FillValue: Word);
  508. begin
  509. FillWord(FBits^, Width * Height, FillValue);
  510. Changed;
  511. end;
  512. procedure TWordMap.Assign(Source: TPersistent);
  513. begin
  514. BeginUpdate;
  515. try
  516. if Source is TWordMap then
  517. begin
  518. inherited SetSize(TWordMap(Source).Width, TWordMap(Source).Height);
  519. Move(TWordMap(Source).Bits[0], Bits[0], Width * Height * SizeOf(Word));
  520. end
  521. //else if Source is TBitmap32 then
  522. // ReadFrom(TBitmap32(Source), ctWeightedRGB)
  523. else
  524. inherited;
  525. finally
  526. EndUpdate;
  527. Changed;
  528. end;
  529. end;
  530. function TWordMap.Empty: Boolean;
  531. begin
  532. Result := not Assigned(FBits);
  533. end;
  534. function TWordMap.GetScanline(Y: Integer): PWordArray;
  535. begin
  536. Result := @FBits^[Y * Width];
  537. end;
  538. function TWordMap.GetValPtr(X, Y: Integer): PWord;
  539. begin
  540. Result := @FBits^[X + Y * Width];
  541. end;
  542. function TWordMap.GetValue(X, Y: Integer): Word;
  543. begin
  544. Result := FBits^[X + Y * Width];
  545. end;
  546. procedure TWordMap.SetValue(X, Y: Integer; const Value: Word);
  547. begin
  548. FBits^[X + Y * Width] := Value;
  549. end;
  550. { TIntegerMap }
  551. constructor TIntegerMap.Create;
  552. begin
  553. FBits := nil;
  554. inherited Create;
  555. end;
  556. destructor TIntegerMap.Destroy;
  557. begin
  558. FreeMem(FBits);
  559. inherited;
  560. end;
  561. procedure TIntegerMap.ChangeSize(var Width, Height: Integer; NewWidth,
  562. NewHeight: Integer);
  563. begin
  564. ReallocMem(FBits, NewWidth * NewHeight * SizeOf(Integer));
  565. Width := NewWidth;
  566. Height := NewHeight;
  567. end;
  568. procedure TIntegerMap.Clear(FillValue: Integer);
  569. begin
  570. FillLongword(FBits^, Width * Height, FillValue);
  571. Changed;
  572. end;
  573. procedure TIntegerMap.Assign(Source: TPersistent);
  574. begin
  575. BeginUpdate;
  576. try
  577. if Source is TIntegerMap then
  578. begin
  579. inherited SetSize(TIntegerMap(Source).Width, TIntegerMap(Source).Height);
  580. Move(TIntegerMap(Source).Bits[0], Bits[0], Width * Height * SizeOf(Integer));
  581. end
  582. //else if Source is TBitmap32 then
  583. // ReadFrom(TBitmap32(Source), ctWeightedRGB)
  584. else
  585. inherited;
  586. finally
  587. EndUpdate;
  588. Changed;
  589. end;
  590. end;
  591. function TIntegerMap.Empty: Boolean;
  592. begin
  593. Result := not Assigned(FBits);
  594. end;
  595. function TIntegerMap.GetScanline(Y: Integer): PIntegerArray;
  596. begin
  597. Result := @FBits^[Y * Width];
  598. end;
  599. function TIntegerMap.GetValPtr(X, Y: Integer): PInteger;
  600. begin
  601. Result := @FBits^[X + Y * Width];
  602. end;
  603. function TIntegerMap.GetValue(X, Y: Integer): Integer;
  604. begin
  605. Result := FBits^[X + Y * Width];
  606. end;
  607. procedure TIntegerMap.SetValue(X, Y: Integer; const Value: Integer);
  608. begin
  609. FBits^[X + Y * Width] := Value;
  610. end;
  611. { TCardinalMap }
  612. constructor TCardinalMap.Create;
  613. begin
  614. FBits := nil;
  615. inherited Create;
  616. end;
  617. destructor TCardinalMap.Destroy;
  618. begin
  619. FreeMem(FBits);
  620. inherited;
  621. end;
  622. procedure TCardinalMap.Assign(Source: TPersistent);
  623. begin
  624. BeginUpdate;
  625. try
  626. if Source is TCardinalMap then
  627. begin
  628. inherited SetSize(TCardinalMap(Source).Width, TCardinalMap(Source).Height);
  629. Move(TCardinalMap(Source).Bits[0], Bits[0], Width * Height * SizeOf(Cardinal));
  630. end
  631. //else if Source is TBitmap32 then
  632. // ReadFrom(TBitmap32(Source), ctWeightedRGB)
  633. else
  634. inherited;
  635. finally
  636. EndUpdate;
  637. Changed;
  638. end;
  639. end;
  640. procedure TCardinalMap.ChangeSize(var Width, Height: Integer; NewWidth,
  641. NewHeight: Integer);
  642. begin
  643. ReallocMem(FBits, NewWidth * NewHeight * SizeOf(Cardinal));
  644. Width := NewWidth;
  645. Height := NewHeight;
  646. end;
  647. procedure TCardinalMap.Clear(FillValue: Cardinal);
  648. begin
  649. FillLongword(FBits^, Width * Height, FillValue);
  650. Changed;
  651. end;
  652. function TCardinalMap.Empty: Boolean;
  653. begin
  654. Result := not Assigned(FBits);
  655. end;
  656. function TCardinalMap.GetScanline(Y: Integer): PCardinalArray;
  657. begin
  658. Result := @FBits^[Y * Width];
  659. end;
  660. function TCardinalMap.GetValPtr(X, Y: Cardinal): PCardinal;
  661. begin
  662. Result := @FBits^[X + Y * Cardinal(Width)];
  663. end;
  664. function TCardinalMap.GetValue(X, Y: Cardinal): Cardinal;
  665. begin
  666. Result := FBits^[X + Y * Cardinal(Width)];
  667. end;
  668. procedure TCardinalMap.SetValue(X, Y: Cardinal; const Value: Cardinal);
  669. begin
  670. FBits^[X + Y * Cardinal(Width)] := Value;
  671. end;
  672. { TFloatMap }
  673. constructor TFloatMap.Create;
  674. begin
  675. FBits := nil;
  676. inherited Create;
  677. end;
  678. destructor TFloatMap.Destroy;
  679. begin
  680. FreeMem(FBits);
  681. inherited;
  682. end;
  683. procedure TFloatMap.Assign(Source: TPersistent);
  684. begin
  685. BeginUpdate;
  686. try
  687. if Source is TFloatMap then
  688. begin
  689. inherited SetSize(TFloatMap(Source).Width, TFloatMap(Source).Height);
  690. Move(TFloatMap(Source).Bits[0], Bits[0], Width * Height * SizeOf(TFloat));
  691. end
  692. //else if Source is TBitmap32 then
  693. // ReadFrom(TBitmap32(Source), ctWeightedRGB)
  694. else
  695. inherited;
  696. finally
  697. EndUpdate;
  698. Changed;
  699. end;
  700. end;
  701. procedure TFloatMap.ChangeSize(var Width, Height: Integer; NewWidth,
  702. NewHeight: Integer);
  703. begin
  704. ReallocMem(FBits, NewWidth * NewHeight * SizeOf(TFloat));
  705. Width := NewWidth;
  706. Height := NewHeight;
  707. end;
  708. procedure TFloatMap.Clear;
  709. begin
  710. FillChar(FBits^, Width * Height * SizeOf(TFloat), 0);
  711. Changed;
  712. end;
  713. procedure TFloatMap.Clear(FillValue: TFloat);
  714. var
  715. Index: Integer;
  716. begin
  717. for Index := 0 to Width * Height - 1 do
  718. FBits^[Index] := FillValue;
  719. Changed;
  720. end;
  721. function TFloatMap.Empty: Boolean;
  722. begin
  723. Result := not Assigned(FBits);
  724. end;
  725. function TFloatMap.GetScanline(Y: Integer): PFloatArray;
  726. begin
  727. Result := @FBits^[Y * Width];
  728. end;
  729. function TFloatMap.GetValPtr(X, Y: Integer): PFloat;
  730. begin
  731. Result := @FBits^[X + Y * Width];
  732. end;
  733. function TFloatMap.GetValue(X, Y: Integer): TFloat;
  734. begin
  735. Result := FBits^[X + Y * Width];
  736. end;
  737. procedure TFloatMap.SetValue(X, Y: Integer; const Value: TFloat);
  738. begin
  739. FBits^[X + Y * Width] := Value;
  740. end;
  741. {$IFDEF COMPILER2010}
  742. { TGenericMap<T> }
  743. constructor TGenericMap<T>.Create;
  744. begin
  745. FBits := nil;
  746. inherited Create;
  747. end;
  748. destructor TGenericMap<T>.Destroy;
  749. begin
  750. FreeMem(FBits);
  751. inherited;
  752. end;
  753. procedure TGenericMap<T>.Assign(Source: TPersistent);
  754. begin
  755. BeginUpdate;
  756. try
  757. (*
  758. if Source is TFloatMap then
  759. begin
  760. inherited SetSize(TFloatMap(Source).Width, TFloatMap(Source).Height);
  761. Move(TFloatMap(Source).Bits[0], Bits[0], Width * Height * SizeOf(TFloat));
  762. end
  763. //else if Source is TBitmap32 then
  764. // ReadFrom(TBitmap32(Source), ctWeightedRGB)
  765. else
  766. inherited;
  767. *)
  768. finally
  769. EndUpdate;
  770. Changed;
  771. end;
  772. end;
  773. procedure TGenericMap<T>.ChangeSize(var Width, Height: Integer; NewWidth,
  774. NewHeight: Integer);
  775. begin
  776. ReallocMem(FBits, NewWidth * NewHeight * SizeOf(T));
  777. Width := NewWidth;
  778. Height := NewHeight;
  779. end;
  780. procedure TGenericMap<T>.Clear(FillValue: T);
  781. var
  782. Index: Integer;
  783. begin
  784. for Index := 0 to Width * Height - 1 do
  785. Move(FillValue, PByte(FBits)[Index], SizeOf(T));
  786. Changed;
  787. end;
  788. procedure TGenericMap<T>.Clear;
  789. begin
  790. FillChar(FBits^, Width * Height * SizeOf(T), 0);
  791. Changed;
  792. end;
  793. function TGenericMap<T>.Empty: Boolean;
  794. begin
  795. Result := not Assigned(FBits);
  796. end;
  797. function TGenericMap<T>.GetValue(X, Y: Integer): T;
  798. begin
  799. Move(PByte(FBits)[(X + Y * Width) * SizeOf(T)], Result, SizeOf(T));
  800. end;
  801. procedure TGenericMap<T>.SetValue(X, Y: Integer; const Value: T);
  802. begin
  803. Move(Value, PByte(FBits)[(X + Y * Width) * SizeOf(T)], SizeOf(T));
  804. end;
  805. {$ENDIF}
  806. end.