ImagingClasses.pas 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095
  1. {
  2. Vampyre Imaging Library
  3. by Marek Mauder
  4. https://github.com/galfar/imaginglib
  5. https://imaginglib.sourceforge.io
  6. - - - - -
  7. This Source Code Form is subject to the terms of the Mozilla Public
  8. License, v. 2.0. If a copy of the MPL was not distributed with this
  9. file, You can obtain one at https://mozilla.org/MPL/2.0.
  10. }
  11. { This unit contains class based wrapper to Imaging library.}
  12. unit ImagingClasses;
  13. {$I ImagingOptions.inc}
  14. interface
  15. uses
  16. Types, Classes, ImagingTypes, Imaging, ImagingFormats, ImagingUtility;
  17. type
  18. { Base abstract high level class wrapper to low level Imaging structures and
  19. functions.}
  20. TBaseImage = class(TPersistent)
  21. private
  22. function GetEmpty: Boolean;
  23. protected
  24. FPData: PImageData;
  25. FOnDataSizeChanged: TNotifyEvent;
  26. FOnPixelsChanged: TNotifyEvent;
  27. function GetFormat: TImageFormat; {$IFDEF USE_INLINE}inline;{$ENDIF}
  28. function GetHeight: Integer; {$IFDEF USE_INLINE}inline;{$ENDIF}
  29. function GetSize: Int64; {$IFDEF USE_INLINE}inline;{$ENDIF}
  30. function GetWidth: Integer; {$IFDEF USE_INLINE}inline;{$ENDIF}
  31. function GetBits: Pointer; {$IFDEF USE_INLINE}inline;{$ENDIF}
  32. function GetPalette: PPalette32; {$IFDEF USE_INLINE}inline;{$ENDIF}
  33. function GetPaletteEntries: Integer; {$IFDEF USE_INLINE}inline;{$ENDIF}
  34. function GetScanline(Index: Integer): Pointer;
  35. function GetPixelPointer(X, Y: Integer): Pointer; {$IFDEF USE_INLINE}inline;{$ENDIF}
  36. function GetScanlineSize: Integer; {$IFDEF USE_INLINE}inline;{$ENDIF}
  37. function GetFormatInfo: TImageFormatInfo; {$IFDEF USE_INLINE}inline;{$ENDIF}
  38. function GetValid: Boolean; {$IFDEF USE_INLINE}inline;{$ENDIF}
  39. function GetBoundsRect: TRect;
  40. procedure SetFormat(const Value: TImageFormat); {$IFDEF USE_INLINE}inline;{$ENDIF}
  41. procedure SetHeight(const Value: Integer); {$IFDEF USE_INLINE}inline;{$ENDIF}
  42. procedure SetWidth(const Value: Integer); {$IFDEF USE_INLINE}inline;{$ENDIF}
  43. procedure SetPointer; virtual; abstract;
  44. procedure DoDataSizeChanged; virtual;
  45. procedure DoPixelsChanged; virtual;
  46. public
  47. constructor Create; virtual;
  48. constructor CreateFromImage(AImage: TBaseImage);
  49. destructor Destroy; override;
  50. { Returns info about current image.}
  51. function ToString: string; {$IF (Defined(DCC) and (CompilerVersion >= 20.0)) or Defined(FPC)}override;{$IFEND}
  52. { Creates a new image data with the given size and format. Old image
  53. data is lost. Works only for the current image of TMultiImage.}
  54. procedure RecreateImageData(AWidth, AHeight: Integer; AFormat: TImageFormat);
  55. { Maps underlying image data to given TImageData record. Both TBaseImage and
  56. TImageData now share some image memory (bits). So don't call FreeImage
  57. on TImageData afterwards since this TBaseImage would get really broken.}
  58. procedure MapImageData(const ImageData: TImageData);
  59. { Deletes current image.}
  60. procedure Clear;
  61. { Resizes current image with optional resampling.}
  62. procedure Resize(NewWidth, NewHeight: Integer; Filter: TResizeFilter);
  63. { Resizes current image proportionally to fit the given width and height. }
  64. procedure ResizeToFit(FitWidth, FitHeight: Integer; Filter: TResizeFilter; DstImage: TBaseImage);
  65. { Flips current image. Reverses the image along its horizontal axis the top
  66. becomes the bottom and vice versa.}
  67. procedure Flip;
  68. { Mirrors current image. Reverses the image along its vertical axis the left
  69. side becomes the right and vice versa.}
  70. procedure Mirror;
  71. { Rotates image by Angle degrees counterclockwise.}
  72. procedure Rotate(Angle: Single);
  73. { Copies rectangular part of SrcImage to DstImage. No blending is performed -
  74. alpha is simply copied to destination image. Operates also with
  75. negative X and Y coordinates.
  76. Note that copying is fastest for images in the same data format
  77. (and slowest for images in special formats).}
  78. procedure CopyTo(SrcX, SrcY, Width, Height: Integer; DstImage: TBaseImage; DstX, DstY: Integer); overload;
  79. { Copies whole image to DstImage. No blending is performed -
  80. alpha is simply copied to destination image. Operates also with
  81. negative X and Y coordinates.
  82. Note that copying is fastest for images in the same data format
  83. (and slowest for images in special formats).}
  84. procedure CopyTo(DstImage: TBaseImage; DstX, DstY: Integer); overload;
  85. { Stretches the contents of the source rectangle to the destination rectangle
  86. with optional resampling. No blending is performed - alpha is
  87. simply copied/resampled to destination image. Note that stretching is
  88. fastest for images in the same data format (and slowest for
  89. images in special formats).}
  90. procedure StretchTo(SrcX, SrcY, SrcWidth, SrcHeight: Integer; DstImage: TBaseImage; DstX, DstY, DstWidth, DstHeight: Integer; Filter: TResizeFilter);
  91. { Replaces pixels with OldPixel in the given rectangle by NewPixel.
  92. OldPixel and NewPixel should point to the pixels in the same format
  93. as the given image is in.}
  94. procedure ReplaceColor(X, Y, Width, Height: Integer; OldColor, NewColor: Pointer);
  95. { Swaps SrcChannel and DstChannel color or alpha channels of image.
  96. Use ChannelRed, ChannelBlue, ChannelGreen, ChannelAlpha constants to
  97. identify channels.}
  98. procedure SwapChannels(SrcChannel, DstChannel: Integer);
  99. { Loads current image data from file.}
  100. procedure LoadFromFile(const FileName: string); virtual;
  101. { Loads current image data from stream.}
  102. procedure LoadFromStream(Stream: TStream); virtual;
  103. { Saves current image data to file.}
  104. function SaveToFile(const FileName: string): Boolean;
  105. { Saves current image data to stream. Ext identifies desired image file
  106. format (jpg, png, dds, ...).}
  107. function SaveToStream(const Ext: string; Stream: TStream): Boolean;
  108. { Width of current image in pixels.}
  109. property Width: Integer read GetWidth write SetWidth;
  110. { Height of current image in pixels.}
  111. property Height: Integer read GetHeight write SetHeight;
  112. { Image data format of current image.}
  113. property Format: TImageFormat read GetFormat write SetFormat;
  114. { Size in bytes of current image's data.}
  115. property Size: Int64 read GetSize;
  116. { Pointer to memory containing image bits.}
  117. property Bits: Pointer read GetBits;
  118. { Pointer to palette for indexed format images. It is nil for others.
  119. Max palette entry is at index [PaletteEntries - 1].}
  120. property Palette: PPalette32 read GetPalette;
  121. { Number of entries in image's palette}
  122. property PaletteEntries: Integer read GetPaletteEntries;
  123. { Provides indexed access to each line of pixels. Does not work with special
  124. format images (like DXT).}
  125. property Scanline[Index: Integer]: Pointer read GetScanline;
  126. { Returns pointer to image pixel at [X, Y] coordinates.}
  127. property PixelPointer[X, Y: Integer]: Pointer read GetPixelPointer;
  128. { Size/length of one image scanline in bytes.}
  129. property ScanlineSize: Integer read GetScanlineSize;
  130. { Extended image format information.}
  131. property FormatInfo: TImageFormatInfo read GetFormatInfo;
  132. { This gives complete access to underlying TImageData record.
  133. It can be used in functions that take TImageData as parameter
  134. (for example: ReduceColors(SingleImageInstance.ImageData^, 64)).}
  135. property ImageDataPointer: PImageData read FPData;
  136. { Indicates whether the current image is valid (proper format,
  137. allowed dimensions, right size, ...).}
  138. property Valid: Boolean read GetValid;
  139. { Indicates whether image contains any data (size in bytes > 0).}
  140. property Empty: Boolean read GetEmpty;
  141. { Specifies the bounding rectangle of the image.}
  142. property BoundsRect: TRect read GetBoundsRect;
  143. { This event occurs when the image data size has just changed. That means
  144. image width, height, or format has been changed.}
  145. property OnDataSizeChanged: TNotifyEvent read FOnDataSizeChanged write FOnDataSizeChanged;
  146. { This event occurs when some pixels of the image have just changed.}
  147. property OnPixelsChanged: TNotifyEvent read FOnPixelsChanged write FOnPixelsChanged;
  148. end;
  149. { Extension of TBaseImage which uses single TImageData record to
  150. store image. All methods inherited from TBaseImage work with this record.}
  151. TSingleImage = class(TBaseImage)
  152. protected
  153. FImageData: TImageData;
  154. procedure SetPointer; override;
  155. public
  156. constructor Create; override;
  157. constructor CreateFromParams(AWidth, AHeight: Integer; AFormat: TImageFormat = ifDefault);
  158. constructor CreateFromData(const AData: TImageData);
  159. constructor CreateFromFile(const FileName: string);
  160. constructor CreateFromStream(Stream: TStream);
  161. destructor Destroy; override;
  162. { Assigns single image from another single image or multi image.}
  163. procedure Assign(Source: TPersistent); override;
  164. { Assigns single image from image data record.}
  165. procedure AssignFromImageData(const AImageData: TImageData);
  166. end;
  167. { Extension of TBaseImage which uses array of TImageData records to
  168. store multiple images. Images are independent on each other and they don't
  169. share any common characteristic. Each can have different size, format, and
  170. palette. All methods inherited from TBaseImage work only with
  171. active image (it could represent mipmap level, animation frame, or whatever).
  172. Methods whose names contain word 'Multi' work with all images in array
  173. (as well as other methods with obvious names).}
  174. TMultiImage = class(TBaseImage)
  175. protected
  176. FDataArray: TDynImageDataArray;
  177. FActiveImage: Integer;
  178. procedure SetActiveImage(Value: Integer); {$IFDEF USE_INLINE}inline;{$ENDIF}
  179. function GetImageCount: Integer; {$IFDEF USE_INLINE}inline;{$ENDIF}
  180. procedure SetImageCount(Value: Integer);
  181. function GetAllImagesValid: Boolean; {$IFDEF USE_INLINE}inline;{$ENDIF}
  182. function GetImage(Index: Integer): TImageData; {$IFDEF USE_INLINE}inline;{$ENDIF}
  183. procedure SetImage(Index: Integer; Value: TImageData); {$IFDEF USE_INLINE}inline;{$ENDIF}
  184. procedure SetPointer; override;
  185. function PrepareInsert(Index, InsertCount: Integer): Boolean;
  186. procedure DoInsertImages(Index: Integer; const Images: TDynImageDataArray);
  187. procedure DoInsertNew(Index: Integer; AWidth, AHeight: Integer; AFormat: TImageFormat);
  188. public
  189. constructor Create; override;
  190. constructor CreateFromParams(AWidth, AHeight: Integer; AFormat: TImageFormat; ImageCount: Integer);
  191. constructor CreateFromArray(const ADataArray: TDynImageDataArray);
  192. constructor CreateFromFile(const FileName: string);
  193. constructor CreateFromStream(Stream: TStream);
  194. destructor Destroy; override;
  195. { Assigns multi image from another multi image or single image.}
  196. procedure Assign(Source: TPersistent); override;
  197. { Assigns multi image from array of image data records.}
  198. procedure AssignFromArray(const ADataArray: TDynImageDataArray);
  199. { Adds new image at the end of the image array. Returns index of the added image.}
  200. function AddImage(AWidth, AHeight: Integer; AFormat: TImageFormat = ifDefault): Integer; overload;
  201. { Adds existing image at the end of the image array. Returns index of the added image.}
  202. function AddImage(const Image: TImageData): Integer; overload;
  203. { Adds existing image (or active image of a TMultiImage)
  204. at the end of the image array. Returns index of the added image.}
  205. function AddImage(Image: TBaseImage): Integer; overload;
  206. { Adds existing image array (all images of a multi image)
  207. at the end of the image array.}
  208. procedure AddImages(const Images: TDynImageDataArray); overload;
  209. { Adds existing MultiImage images at the end of the image array.}
  210. procedure AddImages(Images: TMultiImage); overload;
  211. { Inserts new image image at the given position in the image array. }
  212. procedure InsertImage(Index, AWidth, AHeight: Integer; AFormat: TImageFormat = ifDefault); overload;
  213. { Inserts existing image at the given position in the image array. }
  214. procedure InsertImage(Index: Integer; const Image: TImageData); overload;
  215. { Inserts existing image (Active image of a TMultiImage)
  216. at the given position in the image array. }
  217. procedure InsertImage(Index: Integer; Image: TBaseImage); overload;
  218. { Inserts existing image at the given position in the image array. }
  219. procedure InsertImages(Index: Integer; const Images: TDynImageDataArray); overload;
  220. { Inserts existing images (all images of a TMultiImage) at
  221. the given position in the image array. }
  222. procedure InsertImages(Index: Integer; Images: TMultiImage); overload;
  223. { Exchanges two images at the given positions in the image array. }
  224. procedure ExchangeImages(Index1, Index2: Integer);
  225. { Deletes image at the given position in the image array.}
  226. procedure DeleteImage(Index: Integer);
  227. { Rearranges images so that the first image will become last and vice versa.}
  228. procedure ReverseImages;
  229. { Deletes all images.}
  230. procedure ClearAll;
  231. { Converts all images to another image data format.}
  232. procedure ConvertImages(Format: TImageFormat);
  233. { Resizes all images.}
  234. procedure ResizeImages(NewWidth, NewHeight: Integer; Filter: TResizeFilter);
  235. { Overloaded loading method that will add new image to multi-image if
  236. image array is empty before loading. If it's not empty the active image is replaced.}
  237. procedure LoadFromFile(const FileName: string); override;
  238. { Overloaded loading method that will add new image to multi-image if
  239. image array is empty before loading. If it's not empty the active image is replaced.}
  240. procedure LoadFromStream(Stream: TStream); override;
  241. { Loads whole multi image from file.}
  242. procedure LoadMultiFromFile(const FileName: string);
  243. { Loads whole multi image from stream.}
  244. procedure LoadMultiFromStream(Stream: TStream);
  245. { Saves whole multi image to file.}
  246. function SaveMultiToFile(const FileName: string): Boolean;
  247. { Saves whole multi image to stream. Ext identifies desired
  248. image file format (jpg, png, dds, ...).}
  249. function SaveMultiToStream(const Ext: string; Stream: TStream): Boolean;
  250. { Indicates active image of this multi image. All methods inherited
  251. from TBaseImage operate on this image only.}
  252. property ActiveImage: Integer read FActiveImage write SetActiveImage;
  253. { Number of images of this multi image.}
  254. property ImageCount: Integer read GetImageCount write SetImageCount;
  255. { This value is True if all images of this TMultiImage are valid.}
  256. property AllImagesValid: Boolean read GetAllImagesValid;
  257. { This gives complete access to underlying TDynImageDataArray.
  258. It can be used in functions that take TDynImageDataArray
  259. as parameter.}
  260. property DataArray: TDynImageDataArray read FDataArray;
  261. { Array property for accessing individual images of TMultiImage. When you
  262. set image at given index the old image is freed and the source is cloned.}
  263. property Images[Index: Integer]: TImageData read GetImage write SetImage; default;
  264. end;
  265. implementation
  266. const
  267. DefaultWidth = 16;
  268. DefaultHeight = 16;
  269. function GetArrayFromImageData(const ImageData: TImageData): TDynImageDataArray;
  270. begin
  271. SetLength(Result, 1);
  272. Result[0] := ImageData;
  273. end;
  274. { TBaseImage class implementation }
  275. constructor TBaseImage.Create;
  276. begin
  277. SetPointer;
  278. end;
  279. constructor TBaseImage.CreateFromImage(AImage: TBaseImage);
  280. begin
  281. Create;
  282. Assign(AImage);
  283. end;
  284. destructor TBaseImage.Destroy;
  285. begin
  286. inherited Destroy;
  287. end;
  288. function TBaseImage.GetWidth: Integer;
  289. begin
  290. if Valid then
  291. Result := FPData.Width
  292. else
  293. Result := 0;
  294. end;
  295. function TBaseImage.GetHeight: Integer;
  296. begin
  297. if Valid then
  298. Result := FPData.Height
  299. else
  300. Result := 0;
  301. end;
  302. function TBaseImage.GetFormat: TImageFormat;
  303. begin
  304. if Valid then
  305. Result := FPData.Format
  306. else
  307. Result := ifUnknown;
  308. end;
  309. function TBaseImage.GetScanline(Index: Integer): Pointer;
  310. var
  311. Info: TImageFormatInfo;
  312. begin
  313. if Valid then
  314. begin
  315. Info := GetFormatInfo;
  316. if not Info.IsSpecial then
  317. Result := ImagingFormats.GetScanLine(FPData.Bits, Info, FPData.Width, Index)
  318. else
  319. Result := FPData.Bits;
  320. end
  321. else
  322. Result := nil;
  323. end;
  324. function TBaseImage.GetScanlineSize: Integer;
  325. begin
  326. if Valid then
  327. Result := FormatInfo.GetPixelsSize(Format, Width, 1)
  328. else
  329. Result := 0;
  330. end;
  331. function TBaseImage.GetPixelPointer(X, Y: Integer): Pointer;
  332. begin
  333. if Valid then
  334. Result := @PByteArray(FPData.Bits)[(Y * FPData.Width + X) * GetFormatInfo.BytesPerPixel]
  335. else
  336. Result := nil;
  337. end;
  338. function TBaseImage.GetSize: Int64;
  339. begin
  340. if Valid then
  341. Result := FPData.Size
  342. else
  343. Result := 0;
  344. end;
  345. function TBaseImage.GetBits: Pointer;
  346. begin
  347. if Valid then
  348. Result := FPData.Bits
  349. else
  350. Result := nil;
  351. end;
  352. function TBaseImage.GetPalette: PPalette32;
  353. begin
  354. if Valid then
  355. Result := FPData.Palette
  356. else
  357. Result := nil;
  358. end;
  359. function TBaseImage.GetPaletteEntries: Integer;
  360. begin
  361. Result := GetFormatInfo.PaletteEntries;
  362. end;
  363. function TBaseImage.GetFormatInfo: TImageFormatInfo;
  364. begin
  365. if Valid then
  366. Imaging.GetImageFormatInfo(FPData.Format, Result)
  367. else
  368. FillChar(Result, SizeOf(Result), 0);
  369. end;
  370. function TBaseImage.GetValid: Boolean;
  371. begin
  372. Result := Assigned(FPData) and Imaging.TestImage(FPData^);
  373. end;
  374. function TBaseImage.GetBoundsRect: TRect;
  375. begin
  376. Result := Rect(0, 0, GetWidth, GetHeight);
  377. end;
  378. function TBaseImage.GetEmpty: Boolean;
  379. begin
  380. Result := FPData.Size = 0;
  381. end;
  382. procedure TBaseImage.SetWidth(const Value: Integer);
  383. begin
  384. Resize(Value, GetHeight, rfNearest);
  385. end;
  386. procedure TBaseImage.SetHeight(const Value: Integer);
  387. begin
  388. Resize(GetWidth, Value, rfNearest);
  389. end;
  390. procedure TBaseImage.SetFormat(const Value: TImageFormat);
  391. begin
  392. if Valid and Imaging.ConvertImage(FPData^, Value) then
  393. DoDataSizeChanged;
  394. end;
  395. procedure TBaseImage.DoDataSizeChanged;
  396. begin
  397. if Assigned(FOnDataSizeChanged) then
  398. FOnDataSizeChanged(Self);
  399. DoPixelsChanged;
  400. end;
  401. procedure TBaseImage.DoPixelsChanged;
  402. begin
  403. if Assigned(FOnPixelsChanged) then
  404. FOnPixelsChanged(Self);
  405. end;
  406. procedure TBaseImage.RecreateImageData(AWidth, AHeight: Integer; AFormat: TImageFormat);
  407. begin
  408. if Assigned(FPData) and Imaging.NewImage(AWidth, AHeight, AFormat, FPData^) then
  409. DoDataSizeChanged;
  410. end;
  411. procedure TBaseImage.MapImageData(const ImageData: TImageData);
  412. begin
  413. Clear;
  414. FPData.Width := ImageData.Width;
  415. FPData.Height := ImageData.Height;
  416. FPData.Format := ImageData.Format;
  417. FPData.Size := ImageData.Size;
  418. FPData.Bits := ImageData.Bits;
  419. FPData.Palette := ImageData.Palette;
  420. end;
  421. procedure TBaseImage.Clear;
  422. begin
  423. FreeImage(FPData^);
  424. end;
  425. procedure TBaseImage.Resize(NewWidth, NewHeight: Integer; Filter: TResizeFilter);
  426. begin
  427. if Valid and Imaging.ResizeImage(FPData^, NewWidth, NewHeight, Filter) then
  428. DoDataSizeChanged;
  429. end;
  430. procedure TBaseImage.ResizeToFit(FitWidth, FitHeight: Integer;
  431. Filter: TResizeFilter; DstImage: TBaseImage);
  432. begin
  433. if Valid and Assigned(DstImage) then
  434. begin
  435. Imaging.ResizeImageToFit(FPData^, FitWidth, FitHeight, Filter,
  436. DstImage.FPData^);
  437. DstImage.DoDataSizeChanged;
  438. end;
  439. end;
  440. procedure TBaseImage.Flip;
  441. begin
  442. if Valid and Imaging.FlipImage(FPData^) then
  443. DoPixelsChanged;
  444. end;
  445. procedure TBaseImage.Mirror;
  446. begin
  447. if Valid and Imaging.MirrorImage(FPData^) then
  448. DoPixelsChanged;
  449. end;
  450. procedure TBaseImage.Rotate(Angle: Single);
  451. begin
  452. if Valid then
  453. begin
  454. Imaging.RotateImage(FPData^, Angle);
  455. DoPixelsChanged;
  456. end;
  457. end;
  458. procedure TBaseImage.CopyTo(SrcX, SrcY, Width, Height: Integer;
  459. DstImage: TBaseImage; DstX, DstY: Integer);
  460. begin
  461. if Valid and Assigned(DstImage) and DstImage.Valid then
  462. begin
  463. Imaging.CopyRect(FPData^, SrcX, SrcY, Width, Height, DstImage.FPData^, DstX, DstY);
  464. DstImage.DoPixelsChanged;
  465. end;
  466. end;
  467. procedure TBaseImage.CopyTo(DstImage: TBaseImage; DstX, DstY: Integer);
  468. begin
  469. if Valid and Assigned(DstImage) and DstImage.Valid then
  470. begin
  471. Imaging.CopyRect(FPData^, 0, 0, Width, Height, DstImage.FPData^, DstX, DstY);
  472. DstImage.DoPixelsChanged;
  473. end;
  474. end;
  475. procedure TBaseImage.StretchTo(SrcX, SrcY, SrcWidth, SrcHeight: Integer;
  476. DstImage: TBaseImage; DstX, DstY, DstWidth, DstHeight: Integer; Filter: TResizeFilter);
  477. begin
  478. if Valid and Assigned(DstImage) and DstImage.Valid then
  479. begin
  480. Imaging.StretchRect(FPData^, SrcX, SrcY, SrcWidth, SrcHeight,
  481. DstImage.FPData^, DstX, DstY, DstWidth, DstHeight, Filter);
  482. DstImage.DoPixelsChanged;
  483. end;
  484. end;
  485. procedure TBaseImage.ReplaceColor(X, Y, Width, Height: Integer; OldColor,
  486. NewColor: Pointer);
  487. begin
  488. if Valid then
  489. begin
  490. Imaging.ReplaceColor(FPData^, X, Y, Width, Height, OldColor, NewColor);
  491. DoPixelsChanged;
  492. end;
  493. end;
  494. procedure TBaseImage.SwapChannels(SrcChannel, DstChannel: Integer);
  495. begin
  496. if Valid then
  497. begin
  498. Imaging.SwapChannels(FPData^, SrcChannel, DstChannel);
  499. DoPixelsChanged;
  500. end;
  501. end;
  502. function TBaseImage.ToString: string;
  503. begin
  504. Result := Iff(Valid, Imaging.ImageToStr(FPData^), 'empty image');
  505. end;
  506. procedure TBaseImage.LoadFromFile(const FileName: string);
  507. begin
  508. if Assigned(FPData) and Imaging.LoadImageFromFile(FileName, FPData^) then
  509. DoDataSizeChanged;
  510. end;
  511. procedure TBaseImage.LoadFromStream(Stream: TStream);
  512. begin
  513. if Assigned(FPData) and Imaging.LoadImageFromStream(Stream, FPData^) then
  514. DoDataSizeChanged;
  515. end;
  516. function TBaseImage.SaveToFile(const FileName: string): Boolean;
  517. begin
  518. if Valid then
  519. Result := Imaging.SaveImageToFile(FileName, FPData^)
  520. else
  521. Result := False;
  522. end;
  523. function TBaseImage.SaveToStream(const Ext: string; Stream: TStream): Boolean;
  524. begin
  525. if Valid then
  526. Result := Imaging.SaveImageToStream(Ext, Stream, FPData^)
  527. else
  528. Result := False;
  529. end;
  530. { TSingleImage class implementation }
  531. constructor TSingleImage.Create;
  532. begin
  533. inherited Create;
  534. Clear;
  535. end;
  536. constructor TSingleImage.CreateFromParams(AWidth, AHeight: Integer; AFormat: TImageFormat);
  537. begin
  538. inherited Create;
  539. RecreateImageData(AWidth, AHeight, AFormat);
  540. end;
  541. constructor TSingleImage.CreateFromData(const AData: TImageData);
  542. begin
  543. inherited Create;
  544. AssignFromImageData(AData);
  545. end;
  546. constructor TSingleImage.CreateFromFile(const FileName: string);
  547. begin
  548. inherited Create;
  549. LoadFromFile(FileName);
  550. end;
  551. constructor TSingleImage.CreateFromStream(Stream: TStream);
  552. begin
  553. inherited Create;
  554. LoadFromStream(Stream);
  555. end;
  556. destructor TSingleImage.Destroy;
  557. begin
  558. Imaging.FreeImage(FImageData);
  559. inherited Destroy;
  560. end;
  561. procedure TSingleImage.SetPointer;
  562. begin
  563. FPData := @FImageData;
  564. end;
  565. procedure TSingleImage.Assign(Source: TPersistent);
  566. begin
  567. if Source = nil then
  568. begin
  569. Clear;
  570. end
  571. else if Source is TSingleImage then
  572. begin
  573. AssignFromImageData(TSingleImage(Source).FImageData);
  574. end
  575. else if Source is TMultiImage then
  576. begin
  577. if TMultiImage(Source).Valid then
  578. AssignFromImageData(TMultiImage(Source).FPData^)
  579. else
  580. Clear;
  581. end
  582. else
  583. inherited Assign(Source);
  584. end;
  585. procedure TSingleImage.AssignFromImageData(const AImageData: TImageData);
  586. begin
  587. if Imaging.TestImage(AImageData) then
  588. begin
  589. Imaging.CloneImage(AImageData, FImageData);
  590. DoDataSizeChanged;
  591. end
  592. else
  593. Clear;
  594. end;
  595. { TMultiImage class implementation }
  596. constructor TMultiImage.Create;
  597. begin
  598. inherited Create;
  599. end;
  600. constructor TMultiImage.CreateFromParams(AWidth, AHeight: Integer;
  601. AFormat: TImageFormat; ImageCount: Integer);
  602. var
  603. I: Integer;
  604. begin
  605. Imaging.FreeImagesInArray(FDataArray);
  606. SetLength(FDataArray, ImageCount);
  607. for I := 0 to GetImageCount - 1 do
  608. Imaging.NewImage(AWidth, AHeight, AFormat, FDataArray[I]);
  609. if GetImageCount > 0 then
  610. SetActiveImage(0);
  611. end;
  612. constructor TMultiImage.CreateFromArray(const ADataArray: TDynImageDataArray);
  613. begin
  614. AssignFromArray(ADataArray);
  615. end;
  616. constructor TMultiImage.CreateFromFile(const FileName: string);
  617. begin
  618. LoadMultiFromFile(FileName);
  619. end;
  620. constructor TMultiImage.CreateFromStream(Stream: TStream);
  621. begin
  622. LoadMultiFromStream(Stream);
  623. end;
  624. destructor TMultiImage.Destroy;
  625. begin
  626. Imaging.FreeImagesInArray(FDataArray);
  627. inherited Destroy;
  628. end;
  629. procedure TMultiImage.SetActiveImage(Value: Integer);
  630. begin
  631. FActiveImage := Value;
  632. SetPointer;
  633. end;
  634. function TMultiImage.GetImageCount: Integer;
  635. begin
  636. Result := Length(FDataArray);
  637. end;
  638. procedure TMultiImage.SetImageCount(Value: Integer);
  639. var
  640. I, OldCount: Integer;
  641. begin
  642. if Value > GetImageCount then
  643. begin
  644. // Create new empty images if array will be enlarged
  645. OldCount := GetImageCount;
  646. SetLength(FDataArray, Value);
  647. for I := OldCount to Value - 1 do
  648. Imaging.NewImage(DefaultWidth, DefaultHeight, ifDefault, FDataArray[I]);
  649. end
  650. else
  651. begin
  652. // Free images that exceed desired count and shrink array
  653. for I := Value to GetImageCount - 1 do
  654. Imaging.FreeImage(FDataArray[I]);
  655. SetLength(FDataArray, Value);
  656. end;
  657. SetPointer;
  658. end;
  659. function TMultiImage.GetAllImagesValid: Boolean;
  660. begin
  661. Result := (GetImageCount > 0) and TestImagesInArray(FDataArray);
  662. end;
  663. function TMultiImage.GetImage(Index: Integer): TImageData;
  664. begin
  665. if (Index >= 0) and (Index < GetImageCount) then
  666. Result := FDataArray[Index];
  667. end;
  668. procedure TMultiImage.SetImage(Index: Integer; Value: TImageData);
  669. begin
  670. if (Index >= 0) and (Index < GetImageCount) then
  671. Imaging.CloneImage(Value, FDataArray[Index]);
  672. end;
  673. procedure TMultiImage.SetPointer;
  674. begin
  675. if GetImageCount > 0 then
  676. begin
  677. FActiveImage := ClampInt(FActiveImage, 0, GetImageCount - 1);
  678. FPData := @FDataArray[FActiveImage];
  679. end
  680. else
  681. begin
  682. FActiveImage := -1;
  683. FPData := nil
  684. end;
  685. end;
  686. function TMultiImage.PrepareInsert(Index, InsertCount: Integer): Boolean;
  687. var
  688. I: Integer;
  689. OldImageCount, MoveCount: Integer;
  690. begin
  691. OldImageCount := GetImageCount;
  692. // Inserting to empty image will add image at index 0
  693. if OldImageCount = 0 then
  694. Index := 0;
  695. if (Index >= 0) and (Index <= OldImageCount) and (InsertCount > 0) then
  696. begin
  697. SetLength(FDataArray, OldImageCount + InsertCount);
  698. if Index < OldImageCount then
  699. begin
  700. // Move images to new position
  701. MoveCount := OldImageCount - Index;
  702. System.Move(FDataArray[Index], FDataArray[Index + InsertCount], MoveCount * SizeOf(TImageData));
  703. // Null old images, not free them!
  704. for I := Index to Index + InsertCount - 1 do
  705. InitImage(FDataArray[I]);
  706. end;
  707. Result := True;
  708. end
  709. else
  710. Result := False;
  711. end;
  712. procedure TMultiImage.DoInsertImages(Index: Integer; const Images: TDynImageDataArray);
  713. var
  714. I, Len: Integer;
  715. begin
  716. Len := Length(Images);
  717. if PrepareInsert(Index, Len) then
  718. begin
  719. for I := 0 to Len - 1 do
  720. Imaging.CloneImage(Images[I], FDataArray[Index + I]);
  721. end;
  722. end;
  723. procedure TMultiImage.DoInsertNew(Index, AWidth, AHeight: Integer;
  724. AFormat: TImageFormat);
  725. begin
  726. if PrepareInsert(Index, 1) then
  727. Imaging.NewImage(AWidth, AHeight, AFormat, FDataArray[Index]);
  728. end;
  729. procedure TMultiImage.Assign(Source: TPersistent);
  730. var
  731. Arr: TDynImageDataArray;
  732. begin
  733. if Source = nil then
  734. begin
  735. ClearAll;
  736. end
  737. else if Source is TMultiImage then
  738. begin
  739. AssignFromArray(TMultiImage(Source).FDataArray);
  740. SetActiveImage(TMultiImage(Source).ActiveImage);
  741. end
  742. else if Source is TSingleImage then
  743. begin
  744. SetLength(Arr, 1);
  745. Arr[0] := TSingleImage(Source).FImageData;
  746. AssignFromArray(Arr);
  747. end
  748. else
  749. inherited Assign(Source);
  750. end;
  751. procedure TMultiImage.AssignFromArray(const ADataArray: TDynImageDataArray);
  752. var
  753. I: Integer;
  754. begin
  755. Imaging.FreeImagesInArray(FDataArray);
  756. SetLength(FDataArray, Length(ADataArray));
  757. for I := 0 to GetImageCount - 1 do
  758. begin
  759. // Clone only valid images
  760. if Imaging.TestImage(ADataArray[I]) then
  761. Imaging.CloneImage(ADataArray[I], FDataArray[I])
  762. else
  763. Imaging.NewImage(DefaultWidth, DefaultHeight, ifDefault, FDataArray[I]);
  764. end;
  765. if GetImageCount > 0 then
  766. SetActiveImage(0);
  767. end;
  768. function TMultiImage.AddImage(AWidth, AHeight: Integer; AFormat: TImageFormat): Integer;
  769. begin
  770. Result := GetImageCount;
  771. DoInsertNew(Result, AWidth, AHeight, AFormat);
  772. end;
  773. function TMultiImage.AddImage(const Image: TImageData): Integer;
  774. begin
  775. Result := GetImageCount;
  776. DoInsertImages(Result, GetArrayFromImageData(Image));
  777. end;
  778. function TMultiImage.AddImage(Image: TBaseImage): Integer;
  779. begin
  780. if Assigned(Image) and Image.Valid then
  781. begin
  782. Result := GetImageCount;
  783. DoInsertImages(Result, GetArrayFromImageData(Image.FPData^));
  784. end
  785. else
  786. Result := -1;
  787. end;
  788. procedure TMultiImage.AddImages(const Images: TDynImageDataArray);
  789. begin
  790. DoInsertImages(GetImageCount, Images);
  791. end;
  792. procedure TMultiImage.AddImages(Images: TMultiImage);
  793. begin
  794. DoInsertImages(GetImageCount, Images.FDataArray);
  795. end;
  796. procedure TMultiImage.InsertImage(Index, AWidth, AHeight: Integer;
  797. AFormat: TImageFormat);
  798. begin
  799. DoInsertNew(Index, AWidth, AHeight, AFormat);
  800. end;
  801. procedure TMultiImage.InsertImage(Index: Integer; const Image: TImageData);
  802. begin
  803. DoInsertImages(Index, GetArrayFromImageData(Image));
  804. end;
  805. procedure TMultiImage.InsertImage(Index: Integer; Image: TBaseImage);
  806. begin
  807. if Assigned(Image) and Image.Valid then
  808. DoInsertImages(Index, GetArrayFromImageData(Image.FPData^));
  809. end;
  810. procedure TMultiImage.InsertImages(Index: Integer;
  811. const Images: TDynImageDataArray);
  812. begin
  813. DoInsertImages(Index, Images);
  814. end;
  815. procedure TMultiImage.InsertImages(Index: Integer; Images: TMultiImage);
  816. begin
  817. DoInsertImages(Index, Images.FDataArray);
  818. end;
  819. procedure TMultiImage.ExchangeImages(Index1, Index2: Integer);
  820. var
  821. TempData: TImageData;
  822. begin
  823. if (Index1 >= 0) and (Index1 < GetImageCount) and
  824. (Index2 >= 0) and (Index2 < GetImageCount) then
  825. begin
  826. TempData := FDataArray[Index1];
  827. FDataArray[Index1] := FDataArray[Index2];
  828. FDataArray[Index2] := TempData;
  829. end;
  830. end;
  831. procedure TMultiImage.DeleteImage(Index: Integer);
  832. var
  833. I: Integer;
  834. begin
  835. if (Index >= 0) and (Index < GetImageCount) then
  836. begin
  837. // Free image at index to be deleted
  838. Imaging.FreeImage(FDataArray[Index]);
  839. if Index < GetImageCount - 1 then
  840. begin
  841. // Move images to new indices if necessary
  842. for I := Index to GetImageCount - 2 do
  843. FDataArray[I] := FDataArray[I + 1];
  844. end;
  845. // Set new array length and update pointer to active image
  846. SetLength(FDataArray, GetImageCount - 1);
  847. SetPointer;
  848. end;
  849. end;
  850. procedure TMultiImage.ClearAll;
  851. begin
  852. ImageCount := 0;
  853. end;
  854. procedure TMultiImage.ConvertImages(Format: TImageFormat);
  855. var
  856. I: Integer;
  857. begin
  858. for I := 0 to GetImageCount - 1 do
  859. Imaging.ConvertImage(FDataArray[I], Format);
  860. end;
  861. procedure TMultiImage.ResizeImages(NewWidth, NewHeight: Integer;
  862. Filter: TResizeFilter);
  863. var
  864. I: Integer;
  865. begin
  866. for I := 0 to GetImageCount - 1 do
  867. Imaging.ResizeImage(FDataArray[I], NewWidth, NewHeight, Filter);
  868. end;
  869. procedure TMultiImage.ReverseImages;
  870. var
  871. I: Integer;
  872. begin
  873. for I := 0 to GetImageCount div 2 do
  874. ExchangeImages(I, GetImageCount - 1 - I);
  875. end;
  876. procedure TMultiImage.LoadFromFile(const FileName: string);
  877. begin
  878. if GetImageCount = 0 then
  879. ImageCount := 1;
  880. inherited LoadFromFile(FileName);
  881. end;
  882. procedure TMultiImage.LoadFromStream(Stream: TStream);
  883. begin
  884. if GetImageCount = 0 then
  885. ImageCount := 1;
  886. inherited LoadFromStream(Stream);
  887. end;
  888. procedure TMultiImage.LoadMultiFromFile(const FileName: string);
  889. begin
  890. Imaging.LoadMultiImageFromFile(FileName, FDataArray);
  891. SetActiveImage(0);
  892. end;
  893. procedure TMultiImage.LoadMultiFromStream(Stream: TStream);
  894. begin
  895. Imaging.LoadMultiImageFromStream(Stream, FDataArray);
  896. SetActiveImage(0);
  897. end;
  898. function TMultiImage.SaveMultiToFile(const FileName: string): Boolean;
  899. begin
  900. Result := Imaging.SaveMultiImageToFile(FileName, FDataArray);
  901. end;
  902. function TMultiImage.SaveMultiToStream(const Ext: string; Stream: TStream): Boolean;
  903. begin
  904. Result := Imaging.SaveMultiImageToStream(Ext, Stream, FDataArray);
  905. end;
  906. {
  907. File Notes (obsolete):
  908. -- 0.77.1 ---------------------------------------------------
  909. - Added TSingleImage.AssignFromData and TMultiImage.AssignFromArray
  910. as a replacement for constructors used as methods (that is
  911. compiler error in Delphi XE3).
  912. - Added TBaseImage.ResizeToFit method.
  913. - Changed TMultiImage to have default state with no images.
  914. - TMultiImage.AddImage now returns index of newly added image.
  915. - Fixed img index bug in TMultiImage.ResizeImages
  916. -- 0.26.5 Changes/Bug Fixes ---------------------------------
  917. - Added MapImageData method to TBaseImage
  918. - Added Empty property to TBaseImage.
  919. - Added Clear method to TBaseImage.
  920. - Added ScanlineSize property to TBaseImage.
  921. -- 0.24.3 Changes/Bug Fixes ---------------------------------
  922. - Added TMultiImage.ReverseImages method.
  923. -- 0.23 Changes/Bug Fixes -----------------------------------
  924. - Added SwapChannels method to TBaseImage.
  925. - Added ReplaceColor method to TBaseImage.
  926. - Added ToString method to TBaseImage.
  927. -- 0.21 Changes/Bug Fixes -----------------------------------
  928. - Inserting images to empty MultiImage will act as Add method.
  929. - MultiImages with empty arrays will now create one image when
  930. LoadFromFile or LoadFromStream is called.
  931. - Fixed bug that caused AVs when getting props like Width, Height, asn Size
  932. and when inlining was off. There was call to Iff but with inlining disabled
  933. params like FPData.Size were evaluated and when FPData was nil => AV.
  934. - Added many FPData validity checks to many methods. There were AVs
  935. when calling most methods on empty TMultiImage.
  936. - Added AllImagesValid property to TMultiImage.
  937. - Fixed memory leak in TMultiImage.CreateFromParams.
  938. -- 0.19 Changes/Bug Fixes -----------------------------------
  939. - added ResizeImages method to TMultiImage
  940. - removed Ext parameter from various LoadFromStream methods, no
  941. longer needed
  942. - fixed various issues concerning ActiveImage of TMultiImage
  943. (it pointed to invalid location after some operations)
  944. - most of property set/get methods are now inline
  945. - added PixelPointers property to TBaseImage
  946. - added Images default array property to TMultiImage
  947. - renamed methods in TMultiImage to contain 'Image' instead of 'Level'
  948. - added canvas support
  949. - added OnDataSizeChanged and OnPixelsChanged event to TBaseImage
  950. - renamed TSingleImage.NewImage to RecreateImageData, made public, and
  951. moved to TBaseImage
  952. -- 0.17 Changes/Bug Fixes -----------------------------------
  953. - added props PaletteEntries and ScanLine to TBaseImage
  954. - added new constructor to TBaseImage that take TBaseImage source
  955. - TMultiImage levels adding and inserting rewritten internally
  956. - added some new functions to TMultiImage: AddLevels, InsertLevels
  957. - added some new functions to TBaseImage: Flip, Mirror, Rotate,
  958. CopyRect, StretchRect
  959. - TBasicImage.Resize has now filter parameter
  960. - new stuff added to TMultiImage (DataArray prop, ConvertLevels)
  961. -- 0.13 Changes/Bug Fixes -----------------------------------
  962. - added AddLevel, InsertLevel, ExchangeLevels and DeleteLevel
  963. methods to TMultiImage
  964. - added TBaseImage, TSingleImage and TMultiImage with initial
  965. members
  966. }
  967. end.