ImagingComponents.pas 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396
  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 VCL/LCL TGraphic descendant which uses Imaging library
  12. for saving and loading.}
  13. unit ImagingComponents;
  14. {$I ImagingOptions.inc}
  15. interface
  16. {$IF Defined(FPC) and Defined(LCL)}
  17. {$DEFINE COMPONENT_SET_LCL}
  18. {$ELSEIF Defined(DELPHI)}
  19. {$DEFINE COMPONENT_SET_VCL}
  20. {$IFEND}
  21. {$IF not Defined(COMPONENT_SET_LCL) and not Defined(COMPONENT_SET_VCL)}
  22. // If no component sets should be used just include empty unit.
  23. implementation
  24. {$ELSE}
  25. uses
  26. {$IFDEF MSWINDOWS}
  27. Windows,
  28. {$ENDIF}
  29. SysUtils, Types, Classes,
  30. {$IFDEF COMPONENT_SET_VCL}
  31. Graphics,
  32. {$ENDIF}
  33. {$IFDEF COMPONENT_SET_LCL}
  34. GraphType,
  35. Graphics,
  36. LCLType,
  37. LCLIntf,
  38. {$ENDIF}
  39. ImagingTypes, Imaging, ImagingClasses;
  40. type
  41. { Graphic class which uses Imaging to load images.
  42. It has standard TBitmap class as ancestor and it can
  43. Assign also to/from TImageData structures and TBaseImage
  44. classes. If you want to perfectly preserve the original pixel format
  45. of the source image then these classes may not for you.
  46. This class is automatically registered to TPicture for all
  47. file extensions supported by Imaging (useful only for loading).
  48. If you just want to load images in various formats you can use this
  49. class or simply use TPicture.LoadFromXXX which will create this class
  50. automatically.
  51. For saving it always uses PNG fallback.
  52. For TGraphic classes that save in different formats look
  53. at TImagingGraphicForSave class.}
  54. TImagingGraphic = class(TBitmap)
  55. protected
  56. procedure AssignTo(Dest: TPersistent); override;
  57. { Called by TFiler when reading and writing TPicture.Data property.
  58. We need to override ReadData+WriteData otherwise inherited ones from
  59. TBitmap would be called resulting in errors.}
  60. procedure ReadData(Stream: TStream); override;
  61. procedure WriteData(Stream: TStream); override;
  62. public
  63. constructor Create; override;
  64. { Loads new image from the stream. It can load all image
  65. file formats supported by Imaging (and enabled of course)
  66. even though it is called by descendant class capable of
  67. saving only one file format.}
  68. procedure LoadFromStream(Stream: TStream); override;
  69. { Always saves as PNG.}
  70. procedure SaveToStream(Stream: TStream); override;
  71. { Copies the image contained in Source to this graphic object.
  72. Supports also TBaseImage descendants from ImagingClasses unit. }
  73. procedure Assign(Source: TPersistent); override;
  74. { Copies the image contained in TBaseImage to this graphic object.}
  75. procedure AssignFromImage(Image: TBaseImage);
  76. { Copies the current image to TBaseImage object.}
  77. procedure AssignToImage(Image: TBaseImage);
  78. { Copies the image contained in TImageData structure to this graphic object.}
  79. procedure AssignFromImageData(const ImageData: TImageData);
  80. { Copies the current image to TImageData structure.}
  81. procedure AssignToImageData(var ImageData: TImageData);
  82. {$IFDEF COMPONENT_SET_LCL}
  83. { Needed for TGraphic.LoadFromResourceName() to work.
  84. We return RT_RCDATA here. Also for TImagingBitmap since
  85. RT_BITMAP is stored differently than bitmap on disk (no BITMAPFILEHEADER).}
  86. function GetResourceType: TResourceType; override;
  87. { Used by TPicture.LoadFromStream to find the right TGraphic class for streams. }
  88. class function IsStreamFormatSupported(Stream: TStream): boolean; override;
  89. {$ENDIF}
  90. end;
  91. TImagingGraphicClass = class of TImagingGraphic;
  92. { Base (abstract) class for file format specific TGraphic classes that use
  93. Imaging for saving. Each descendant class can load all file formats
  94. supported by Imaging but save only one format (TImagingBitmap
  95. for *.bmp, TImagingJpeg for *.jpg). The image is saved in this one file
  96. format regardless of the extension you request).
  97. Format specific classes also allow easy access to Imaging options that
  98. affect saving of files (they are properties here).}
  99. TImagingGraphicForSave = class(TImagingGraphic)
  100. protected
  101. FDefaultFileExt: string;
  102. FSavingFormat: TImageFormat;
  103. procedure WriteData(Stream: TStream); override;
  104. public
  105. constructor Create; override;
  106. { Saves the current image to the stream. It is saved in the
  107. file format according to the DefaultFileExt property.
  108. So each descendant class can save some other file format.}
  109. procedure SaveToStream(Stream: TStream); override;
  110. { Returns TImageFileFormat descendant for this graphic class.}
  111. class function GetFileFormat: TImageFileFormat; virtual; abstract;
  112. {$IFDEF COMPONENT_SET_LCL}
  113. { Returns file extensions of this graphic class.}
  114. class function GetFileExtensions: string; override;
  115. { Returns default MIME type of this graphic class.}
  116. function GetMimeType: string; override;
  117. {$ENDIF}
  118. { Default (the most common) file extension of this graphic class.}
  119. property DefaultFileExt: string read FDefaultFileExt;
  120. end;
  121. TImagingGraphicForSaveClass = class of TImagingGraphicForSave;
  122. {$IFNDEF DONT_LINK_BITMAP}
  123. { TImagingGraphic descendant for loading/saving Windows bitmaps.
  124. VCL/LCL both have native support for bitmaps so you might
  125. want to disable this class (although you can save bitmaps with
  126. RLE compression with this class).}
  127. TImagingBitmap = class(TImagingGraphicForSave)
  128. protected
  129. FUseRLE: Boolean;
  130. public
  131. constructor Create; override;
  132. procedure SaveToStream(Stream: TStream); override;
  133. class function GetFileFormat: TImageFileFormat; override;
  134. { See ImagingBitmapRLE option for details.}
  135. property UseRLE: Boolean read FUseRLE write FUseRLE;
  136. end;
  137. {$ENDIF}
  138. {$IFNDEF DONT_LINK_JPEG}
  139. { TImagingGraphic descendant for loading/saving JPEG images.}
  140. TImagingJpeg = class(TImagingGraphicForSave)
  141. protected
  142. FQuality: LongInt;
  143. FProgressive: Boolean;
  144. public
  145. constructor Create; override;
  146. procedure SaveToStream(Stream: TStream); override;
  147. class function GetFileFormat: TImageFileFormat; override;
  148. {$IFDEF COMPONENT_SET_LCL}
  149. function GetMimeType: string; override;
  150. {$ENDIF}
  151. { See ImagingJpegQuality option for details.}
  152. property Quality: LongInt read FQuality write FQuality;
  153. { See ImagingJpegProgressive option for details.}
  154. property Progressive: Boolean read FProgressive write FProgressive;
  155. end;
  156. {$ENDIF}
  157. {$IFNDEF DONT_LINK_PNG}
  158. { TImagingGraphic descendant for loading/saving PNG images.}
  159. TImagingPNG = class(TImagingGraphicForSave)
  160. protected
  161. FPreFilter: LongInt;
  162. FCompressLevel: LongInt;
  163. public
  164. constructor Create; override;
  165. procedure SaveToStream(Stream: TStream); override;
  166. class function GetFileFormat: TImageFileFormat; override;
  167. { See ImagingPNGPreFilter option for details.}
  168. property PreFilter: LongInt read FPreFilter write FPreFilter;
  169. { See ImagingPNGCompressLevel option for details.}
  170. property CompressLevel: LongInt read FCompressLevel write FCompressLevel;
  171. end;
  172. {$ENDIF}
  173. {$IFNDEF DONT_LINK_GIF}
  174. { TImagingGraphic descendant for loading/saving GIF images.}
  175. TImagingGIF = class(TImagingGraphicForSave)
  176. public
  177. class function GetFileFormat: TImageFileFormat; override;
  178. end;
  179. {$ENDIF}
  180. {$IFNDEF DONT_LINK_TARGA}
  181. { TImagingGraphic descendant for loading/saving Targa images.}
  182. TImagingTarga = class(TImagingGraphicForSave)
  183. protected
  184. FUseRLE: Boolean;
  185. public
  186. constructor Create; override;
  187. procedure SaveToStream(Stream: TStream); override;
  188. class function GetFileFormat: TImageFileFormat; override;
  189. { See ImagingTargaRLE option for details.}
  190. property UseRLE: Boolean read FUseRLE write FUseRLE;
  191. end;
  192. {$ENDIF}
  193. {$IFNDEF DONT_LINK_DDS}
  194. { Compression type used when saving DDS files by TImagingDds.}
  195. TDDSCompression = (dcNone, dcDXT1, dcDXT3, dcDXT5);
  196. { TImagingGraphic descendant for loading/saving DDS images.}
  197. TImagingDDS = class(TImagingGraphicForSave)
  198. protected
  199. FCompression: TDDSCompression;
  200. public
  201. constructor Create; override;
  202. procedure SaveToStream(Stream: TStream); override;
  203. class function GetFileFormat: TImageFileFormat; override;
  204. { You can choose compression type used when saving DDS file.
  205. dcNone means that file will be saved in the current bitmaps pixel format.}
  206. property Compression: TDDSCompression read FCompression write FCompression;
  207. end;
  208. {$ENDIF}
  209. {$IFNDEF DONT_LINK_MNG}
  210. { TImagingGraphic descendant for loading/saving MNG images.}
  211. TImagingMNG = class(TImagingGraphicForSave)
  212. protected
  213. FLossyCompression: Boolean;
  214. FLossyAlpha: Boolean;
  215. FPreFilter: LongInt;
  216. FCompressLevel: LongInt;
  217. FQuality: LongInt;
  218. FProgressive: Boolean;
  219. public
  220. constructor Create; override;
  221. procedure SaveToStream(Stream: TStream); override;
  222. class function GetFileFormat: TImageFileFormat; override;
  223. {$IFDEF COMPONENT_SET_LCL}
  224. function GetMimeType: string; override;
  225. {$ENDIF}
  226. { See ImagingMNGLossyCompression option for details.}
  227. property LossyCompression: Boolean read FLossyCompression write FLossyCompression;
  228. { See ImagingMNGLossyAlpha option for details.}
  229. property LossyAlpha: Boolean read FLossyAlpha write FLossyAlpha;
  230. { See ImagingMNGPreFilter option for details.}
  231. property PreFilter: LongInt read FPreFilter write FPreFilter;
  232. { See ImagingMNGCompressLevel option for details.}
  233. property CompressLevel: LongInt read FCompressLevel write FCompressLevel;
  234. { See ImagingMNGQuality option for details.}
  235. property Quality: LongInt read FQuality write FQuality;
  236. { See ImagingMNGProgressive option for details.}
  237. property Progressive: Boolean read FProgressive write FProgressive;
  238. end;
  239. {$ENDIF}
  240. {$IFNDEF DONT_LINK_JNG}
  241. { TImagingGraphic descendant for loading/saving JNG images.}
  242. TImagingJNG = class(TImagingGraphicForSave)
  243. protected
  244. FLossyAlpha: Boolean;
  245. FAlphaPreFilter: LongInt;
  246. FAlphaCompressLevel: LongInt;
  247. FQuality: LongInt;
  248. FProgressive: Boolean;
  249. public
  250. constructor Create; override;
  251. procedure SaveToStream(Stream: TStream); override;
  252. class function GetFileFormat: TImageFileFormat; override;
  253. { See ImagingJNGLossyAlpha option for details.}
  254. property LossyAlpha: Boolean read FLossyAlpha write FLossyAlpha;
  255. { See ImagingJNGPreFilter option for details.}
  256. property AlphaPreFilter: LongInt read FAlphaPreFilter write FAlphaPreFilter;
  257. { See ImagingJNGCompressLevel option for details.}
  258. property AlphaCompressLevel: LongInt read FAlphaCompressLevel write FAlphaCompressLevel;
  259. { See ImagingJNGQuality option for details.}
  260. property Quality: LongInt read FQuality write FQuality;
  261. { See ImagingJNGProgressive option for details.}
  262. property Progressive: Boolean read FProgressive write FProgressive;
  263. end;
  264. {$ENDIF}
  265. { Returns bitmap pixel format with the closest match with given data format.}
  266. function DataFormatToPixelFormat(Format: TImageFormat): TPixelFormat;
  267. { Returns data format with closest match with given bitmap pixel format.}
  268. function PixelFormatToDataFormat(Format: TPixelFormat): TImageFormat;
  269. { Converts TImageData structure to VCL/CLX/LCL bitmap.}
  270. procedure ConvertDataToBitmap(const Data: TImageData; Bitmap: TBitmap);
  271. { Converts VCL/CLX/LCL bitmap to TImageData structure.}
  272. procedure ConvertBitmapToData(Bitmap: TBitmap; var Data: TImageData);
  273. { Converts TBaseImage instance to VCL/CLX/LCL bitmap.}
  274. procedure ConvertImageToBitmap(Image: TBaseImage; Bitmap: TBitmap);
  275. { Converts VCL/CLX/LCL bitmap to TBaseImage. Image must exist before
  276. procedure is called. It overwrites its current image data.
  277. When Image is TMultiImage only the current image level is overwritten.}
  278. procedure ConvertBitmapToImage(Bitmap: TBitmap; Image: TBaseImage);
  279. { Displays image onto TCanvas to rectangle DstRect. This procedure
  280. draws image without converting from Imaging format to TBitmap.
  281. Only [ifA8R8G8B8, ifX8R8G8B8] image formats are supported. Use this
  282. when you want displaying images that change frequently (because converting to
  283. TBitmap by ConvertImageDataToBitmap is generally slow).}
  284. procedure DisplayImageData(DstCanvas: TCanvas; const DstRect: TRect; const ImageData: TImageData); overload;
  285. { Displays image stored in TImageData structure onto TCanvas. This procedure
  286. draws image without converting from Imaging format to TBitmap.
  287. Only [ifA8R8G8B8, ifX8R8G8B8] image formats are supported. Use this
  288. when you want displaying images that change frequently (because converting to
  289. TBitmap by ConvertImageDataToBitmap is generally slow). Dest and Src
  290. rectangles represent coordinates in the form (X1, Y1, X2, Y2).}
  291. procedure DisplayImageData(DstCanvas: TCanvas; const DstRect: TRect; const ImageData: TImageData; const SrcRect: TRect); overload;
  292. { Displays image onto TCanvas at position [DstX, DstY]. This procedure
  293. draws image without converting from Imaging format to TBitmap.
  294. Only [ifA8R8G8B8, ifX8R8G8B8] image formats are supported. Use this
  295. when you want displaying images that change frequently (because converting to
  296. TBitmap by ConvertImageDataToBitmap is generally slow).}
  297. procedure DisplayImage(DstCanvas: TCanvas; DstX, DstY: LongInt; Image: TBaseImage); overload;
  298. { Displays image onto TCanvas to rectangle DstRect. This procedure
  299. draws image without converting from Imaging format to TBitmap.
  300. Only [ifA8R8G8B8, ifX8R8G8B8] image formats are supported. Use this
  301. when you want displaying images that change frequently (because converting to
  302. TBitmap by ConvertImageDataToBitmap is generally slow).}
  303. procedure DisplayImage(DstCanvas: TCanvas; const DstRect: TRect; Image: TBaseImage); overload;
  304. { Displays part of the image specified by SrcRect onto TCanvas to rectangle DstRect.
  305. This procedure draws image without converting from Imaging format to TBitmap.
  306. Only [ifA8R8G8B8, ifX8R8G8B8] image formats are supported. Use this
  307. when you want displaying images that change frequently (because converting to
  308. TBitmap by ConvertImageDataToBitmap is generally slow).}
  309. procedure DisplayImage(DstCanvas: TCanvas; const DstRect: TRect; Image: TBaseImage; const SrcRect: TRect); overload;
  310. {$IFDEF MSWINDOWS}
  311. { Displays image stored in TImageData structure onto Windows device context.
  312. Behaviour is the same as of DisplayImageData.}
  313. procedure DisplayImageDataOnDC(DC: HDC; const DstRect: TRect; const ImageData: TImageData; const SrcRect: TRect);
  314. {$ENDIF}
  315. procedure RegisterTypes;
  316. implementation
  317. uses
  318. {$IF Defined(LCL)}
  319. InterfaceBase,
  320. {$IF Defined(LCLGTK2)}
  321. GLib2, GDK2, GTK2, GTK2Def, GTK2Proc,
  322. {$ELSEIF Defined(LCLqt5)}
  323. Qt5, qtobjects,
  324. {$ELSEIF Defined(LCLcocoa)}
  325. CocoaGDIObjects, CocoaUtils,
  326. {$IFEND}
  327. {$IFEND}
  328. {$IFNDEF DONT_LINK_BITMAP}
  329. ImagingBitmap,
  330. {$ENDIF}
  331. {$IFNDEF DONT_LINK_JPEG}
  332. ImagingJpeg,
  333. {$ENDIF}
  334. {$IFNDEF DONT_LINK_GIF}
  335. ImagingGif,
  336. {$ENDIF}
  337. {$IFNDEF DONT_LINK_TARGA}
  338. ImagingTarga,
  339. {$ENDIF}
  340. {$IFNDEF DONT_LINK_DDS}
  341. ImagingDds,
  342. {$ENDIF}
  343. {$IF not Defined(DONT_LINK_PNG) or not Defined(DONT_LINK_MNG) or not Defined(DONT_LINK_JNG)}
  344. ImagingNetworkGraphics,
  345. {$IFEND}
  346. ImagingFormats, ImagingUtility;
  347. resourcestring
  348. SBadFormatDataToBitmap = 'Cannot find compatible bitmap format for image %s';
  349. SBadFormatBitmapToData = 'Cannot find compatible data format for bitmap %p';
  350. SBadFormatDisplay = 'Unsupported image format passed';
  351. SUnsupportedLCLWidgetSet = 'This function is not implemented for current LCL widget set';
  352. SImagingGraphicName = 'Imaging Graphic AllInOne';
  353. var
  354. RegisteredFormats: TList;
  355. RegisteredGraphicsClasses: Boolean = False;
  356. { Registers types to VCL/LCL.
  357. In some cases (base+ext package installed in Lazarus) RegisterTypes can be
  358. called twice so must keep track of which formats were already registered. }
  359. procedure RegisterTypes;
  360. var
  361. I: LongInt;
  362. procedure RegisterFileFormatAllInOne(Format: TImageFileFormat);
  363. var
  364. I: LongInt;
  365. begin
  366. if RegisteredFormats.IndexOf(Format) >= 0 then
  367. Exit;
  368. for I := 0 to Format.Extensions.Count - 1 do
  369. begin
  370. TPicture.RegisterFileFormat(Format.Extensions[I], SImagingGraphicName,
  371. TImagingGraphic);
  372. end;
  373. RegisteredFormats.Add(Format);
  374. end;
  375. procedure RegisterFileFormat(AClass: TImagingGraphicForSaveClass);
  376. var
  377. I: LongInt;
  378. begin
  379. for I := 0 to AClass.GetFileFormat.Extensions.Count - 1 do
  380. TPicture.RegisterFileFormat(AClass.GetFileFormat.Extensions[I],
  381. AClass.GetFileFormat.Name, AClass);
  382. end;
  383. begin
  384. for I := Imaging.GetFileFormatCount - 1 downto 0 do
  385. RegisterFileFormatAllInOne(Imaging.GetFileFormatAtIndex(I));
  386. Classes.RegisterClass(TImagingGraphic);
  387. if RegisteredGraphicsClasses then
  388. Exit;
  389. {$IFNDEF DONT_LINK_TARGA}
  390. RegisterFileFormat(TImagingTarga);
  391. Classes.RegisterClass(TImagingTarga);
  392. {$ENDIF}
  393. {$IFNDEF DONT_LINK_DDS}
  394. RegisterFileFormat(TImagingDDS);
  395. Classes.RegisterClass(TImagingDDS);
  396. {$ENDIF}
  397. {$IFNDEF DONT_LINK_JNG}
  398. RegisterFileFormat(TImagingJNG);
  399. Classes.RegisterClass(TImagingJNG);
  400. {$ENDIF}
  401. {$IFNDEF DONT_LINK_MNG}
  402. RegisterFileFormat(TImagingMNG);
  403. Classes.RegisterClass(TImagingMNG);
  404. {$ENDIF}
  405. {$IFNDEF DONT_LINK_GIF}
  406. RegisterFileFormat(TImagingGIF);
  407. Classes.RegisterClass(TImagingGIF);
  408. {$ENDIF}
  409. {$IFNDEF DONT_LINK_PNG}
  410. RegisterFileFormat(TImagingPNG);
  411. Classes.RegisterClass(TImagingPNG);
  412. {$ENDIF}
  413. {$IFNDEF DONT_LINK_JPEG}
  414. RegisterFileFormat(TImagingJpeg);
  415. Classes.RegisterClass(TImagingJpeg);
  416. {$ENDIF}
  417. {$IFNDEF DONT_LINK_BITMAP}
  418. RegisterFileFormat(TImagingBitmap);
  419. Classes.RegisterClass(TImagingBitmap);
  420. {$ENDIF}
  421. RegisteredGraphicsClasses := True;
  422. end;
  423. { Unregisters types from VCL/LCL.}
  424. procedure UnRegisterTypes;
  425. begin
  426. {$IFNDEF DONT_LINK_BITMAP}
  427. TPicture.UnregisterGraphicClass(TImagingBitmap);
  428. Classes.UnRegisterClass(TImagingBitmap);
  429. {$ENDIF}
  430. {$IFNDEF DONT_LINK_JPEG}
  431. TPicture.UnregisterGraphicClass(TImagingJpeg);
  432. Classes.UnRegisterClass(TImagingJpeg);
  433. {$ENDIF}
  434. {$IFNDEF DONT_LINK_PNG}
  435. TPicture.UnregisterGraphicClass(TImagingPNG);
  436. Classes.UnRegisterClass(TImagingPNG);
  437. {$ENDIF}
  438. {$IFNDEF DONT_LINK_GIF}
  439. TPicture.UnregisterGraphicClass(TImagingGIF);
  440. Classes.UnRegisterClass(TImagingGIF);
  441. {$ENDIF}
  442. {$IFNDEF DONT_LINK_TARGA}
  443. TPicture.UnregisterGraphicClass(TImagingTarga);
  444. Classes.UnRegisterClass(TImagingTarga);
  445. {$ENDIF}
  446. {$IFNDEF DONT_LINK_DDS}
  447. TPicture.UnregisterGraphicClass(TImagingDDS);
  448. Classes.UnRegisterClass(TImagingDDS);
  449. {$ENDIF}
  450. TPicture.UnregisterGraphicClass(TImagingGraphic);
  451. Classes.UnRegisterClass(TImagingGraphic);
  452. end;
  453. function DataFormatToPixelFormat(Format: TImageFormat): TPixelFormat;
  454. begin
  455. case Format of
  456. {$IFDEF COMPONENT_SET_VCL}
  457. ifIndex8: Result := pf8bit;
  458. ifR5G6B5: Result := pf16bit;
  459. ifR8G8B8: Result := pf24bit;
  460. {$ENDIF}
  461. ifA8R8G8B8,
  462. ifX8R8G8B8: Result := pf32bit;
  463. else
  464. Result := pfCustom;
  465. end;
  466. end;
  467. function PixelFormatToDataFormat(Format: TPixelFormat): TImageFormat;
  468. begin
  469. case Format of
  470. pf8bit: Result := ifIndex8;
  471. pf15bit: Result := ifA1R5G5B5;
  472. pf16bit: Result := ifR5G6B5;
  473. pf24bit: Result := ifR8G8B8;
  474. pf32bit: Result := ifA8R8G8B8;
  475. else
  476. Result := ifUnknown;
  477. end;
  478. end;
  479. procedure ConvertDataToBitmap(const Data: TImageData; Bitmap: TBitmap);
  480. var
  481. PF: TPixelFormat;
  482. Info: TImageFormatInfo;
  483. WorkData: TImageData;
  484. {$IFDEF COMPONENT_SET_VCL}
  485. I, LineBytes: LongInt;
  486. LogPalette: TMaxLogPalette;
  487. {$ENDIF}
  488. {$IFDEF COMPONENT_SET_LCL}
  489. RawImage: TRawImage;
  490. ImgHandle, ImgMaskHandle: HBitmap;
  491. {$ENDIF}
  492. begin
  493. PF := DataFormatToPixelFormat(Data.Format);
  494. GetImageFormatInfo(Data.Format, Info);
  495. if (PF = pf8bit) and PaletteHasAlpha(Data.Palette, Info.PaletteEntries) then
  496. begin
  497. // Some indexed images may have valid alpha data, don't lose it!
  498. // (e.g. transparent 8bit PNG or GIF images)
  499. PF := pfCustom;
  500. end;
  501. if PF = pfCustom then
  502. begin
  503. // Convert from formats not supported by Graphics unit
  504. Imaging.InitImage(WorkData);
  505. Imaging.CloneImage(Data, WorkData);
  506. if Info.IsFloatingPoint or Info.HasAlphaChannel or Info.IsSpecial then
  507. Imaging.ConvertImage(WorkData, ifA8R8G8B8)
  508. else
  509. begin
  510. {$IFDEF COMPONENT_SET_VCL}
  511. if Info.IsIndexed or Info.HasGrayChannel then
  512. Imaging.ConvertImage(WorkData, ifIndex8)
  513. else if Info.UsePixelFormat then
  514. Imaging.ConvertImage(WorkData, ifR5G6B5)
  515. else
  516. Imaging.ConvertImage(WorkData, ifR8G8B8);
  517. {$ELSE}
  518. Imaging.ConvertImage(WorkData, ifA8R8G8B8);
  519. {$ENDIF}
  520. end;
  521. PF := DataFormatToPixelFormat(WorkData.Format);
  522. GetImageFormatInfo(WorkData.Format, Info);
  523. end
  524. else
  525. WorkData := Data;
  526. if PF = pfCustom then
  527. RaiseImaging(SBadFormatDataToBitmap, [ImageToStr(WorkData)]);
  528. {$IFDEF COMPONENT_SET_VCL}
  529. Bitmap.Width := WorkData.Width;
  530. Bitmap.Height := WorkData.Height;
  531. Bitmap.PixelFormat := PF;
  532. if (PF = pf8bit) and (WorkData.Palette <> nil) then
  533. begin
  534. // Copy palette, this must be done before copying bits
  535. FillChar(LogPalette, SizeOf(LogPalette), 0);
  536. LogPalette.palVersion := $300;
  537. LogPalette.palNumEntries := Info.PaletteEntries;
  538. for I := 0 to Info.PaletteEntries - 1 do
  539. with LogPalette do
  540. begin
  541. palPalEntry[I].peRed := WorkData.Palette[I].R;
  542. palPalEntry[I].peGreen := WorkData.Palette[I].G;
  543. palPalEntry[I].peBlue := WorkData.Palette[I].B;
  544. end;
  545. Bitmap.Palette := CreatePalette(PLogPalette(@LogPalette)^);
  546. end;
  547. // Copy scanlines
  548. LineBytes := WorkData.Width * Info.BytesPerPixel;
  549. for I := 0 to WorkData.Height - 1 do
  550. Move(PByteArray(WorkData.Bits)[I * LineBytes], Bitmap.Scanline[I]^, LineBytes);
  551. // Delphi 2009 and newer support alpha transparency for TBitmap
  552. {$IF Defined(DELPHI) and (CompilerVersion >= 20.0)}
  553. if Bitmap.PixelFormat = pf32bit then
  554. Bitmap.AlphaFormat := afDefined;
  555. {$IFEND}
  556. {$ENDIF}
  557. {$IFDEF COMPONENT_SET_LCL}
  558. // Create 32bit raw image from image data
  559. FillChar(RawImage, SizeOf(RawImage), 0);
  560. with RawImage.Description do
  561. begin
  562. Width := WorkData.Width;
  563. Height := WorkData.Height;
  564. BitsPerPixel := 32;
  565. Format := ricfRGBA;
  566. LineEnd := rileDWordBoundary;
  567. BitOrder := riboBitsInOrder;
  568. ByteOrder := riboLSBFirst;
  569. LineOrder := riloTopToBottom;
  570. AlphaPrec := 8;
  571. RedPrec := 8;
  572. GreenPrec := 8;
  573. BluePrec := 8;
  574. AlphaShift := 24;
  575. RedShift := 16;
  576. GreenShift := 8;
  577. BlueShift := 0;
  578. Depth := 32; // Must be 32 for alpha blending (and for working in MacOSX Carbon)
  579. end;
  580. RawImage.Data := WorkData.Bits;
  581. RawImage.DataSize := WorkData.Size;
  582. // Create bitmap from raw image
  583. if RawImage_CreateBitmaps(RawImage, ImgHandle, ImgMaskHandle) then
  584. begin
  585. Bitmap.Handle := ImgHandle;
  586. Bitmap.MaskHandle := ImgMaskHandle;
  587. end;
  588. {$ENDIF}
  589. if WorkData.Bits <> Data.Bits then
  590. Imaging.FreeImage(WorkData);
  591. end;
  592. procedure ConvertBitmapToData(Bitmap: TBitmap; var Data: TImageData);
  593. var
  594. I, LineBytes: LongInt;
  595. Format: TImageFormat;
  596. Info: TImageFormatInfo;
  597. {$IFDEF COMPONENT_SET_VCL}
  598. Colors: Word;
  599. LogPalette: TMaxLogPalette;
  600. {$ENDIF}
  601. {$IFDEF COMPONENT_SET_LCL}
  602. RawImage: TRawImage;
  603. LineLazBytes: LongInt;
  604. {$ENDIF}
  605. begin
  606. Format := ifUnknown;
  607. {$IFDEF COMPONENT_SET_LCL}
  608. // In the current Lazarus 0.9.10 Bitmap.PixelFormat property is useless.
  609. // We cannot change bitmap's format by changing it (it will just release
  610. // old image but not convert it to new format) nor we can determine bitmaps's
  611. // current format (it is usually set to pfDevice). So bitmap's format is obtained
  612. // trough RawImage api and cannot be changed to mirror some Imaging format
  613. // (so formats with no corresponding Imaging format cannot be saved now).
  614. if RawImage_DescriptionFromBitmap(Bitmap.Handle, RawImage.Description) then
  615. case RawImage.Description.BitsPerPixel of
  616. 8: Format := ifIndex8;
  617. 16:
  618. if RawImage.Description.Depth = 15 then
  619. Format := ifA1R5G5B5
  620. else
  621. Format := ifR5G6B5;
  622. 24: Format := ifR8G8B8;
  623. 32: Format := ifA8R8G8B8;
  624. 48: Format := ifR16G16B16;
  625. 64: Format := ifA16R16G16B16;
  626. end;
  627. {$ELSE}
  628. Format := PixelFormatToDataFormat(Bitmap.PixelFormat);
  629. if Format = ifUnknown then
  630. begin
  631. // Convert from formats not supported by Imaging (1/4 bit)
  632. if Bitmap.PixelFormat < pf8bit then
  633. Bitmap.PixelFormat := pf8bit
  634. else
  635. Bitmap.PixelFormat := pf32bit;
  636. Format := PixelFormatToDataFormat(Bitmap.PixelFormat);
  637. end;
  638. {$ENDIF}
  639. if Format = ifUnknown then
  640. RaiseImaging(SBadFormatBitmapToData, []);
  641. Imaging.NewImage(Bitmap.Width, Bitmap.Height, Format, Data);
  642. GetImageFormatInfo(Data.Format, Info);
  643. LineBytes := Data.Width * Info.BytesPerPixel;
  644. {$IFDEF COMPONENT_SET_VCL}
  645. if (Format = ifIndex8) and (GetObject(Bitmap.Palette, SizeOf(Colors),
  646. @Colors) <> 0) then
  647. begin
  648. // Copy palette
  649. GetPaletteEntries(Bitmap.Palette, 0, Colors, LogPalette.palPalEntry);
  650. if Colors > Info.PaletteEntries then
  651. Colors := Info.PaletteEntries;
  652. for I := 0 to Colors - 1 do
  653. with LogPalette do
  654. begin
  655. Data.Palette[I].A := $FF;
  656. Data.Palette[I].R := palPalEntry[I].peRed;
  657. Data.Palette[I].G := palPalEntry[I].peGreen;
  658. Data.Palette[I].B := palPalEntry[I].peBlue;
  659. end;
  660. end;
  661. // Copy scanlines
  662. for I := 0 to Data.Height - 1 do
  663. Move(Bitmap.ScanLine[I]^, PByteArray(Data.Bits)[I * LineBytes], LineBytes);
  664. {$ENDIF}
  665. {$IFDEF COMPONENT_SET_LCL}
  666. // Get raw image from bitmap (mask handle must be 0 or expect violations)
  667. if RawImage_FromBitmap(RawImage, Bitmap.Handle, 0, nil) then
  668. begin
  669. LineLazBytes := GetBytesPerLine(Data.Width, RawImage.Description.BitsPerPixel,
  670. RawImage.Description.LineEnd);
  671. // Copy scanlines
  672. for I := 0 to Data.Height - 1 do
  673. begin
  674. Move(PByteArray(RawImage.Data)[I * LineLazBytes],
  675. PByteArray(Data.Bits)[I * LineBytes], LineBytes);
  676. end;
  677. // May need to swap RB order, depends on widget set
  678. if RawImage.Description.BlueShift > RawImage.Description.RedShift then
  679. SwapChannels(Data, ChannelRed, ChannelBlue);
  680. RawImage.FreeData;
  681. end;
  682. {$ENDIF}
  683. end;
  684. procedure ConvertImageToBitmap(Image: TBaseImage; Bitmap: TBitmap);
  685. begin
  686. ConvertDataToBitmap(Image.ImageDataPointer^, Bitmap);
  687. end;
  688. procedure ConvertBitmapToImage(Bitmap: TBitmap; Image: TBaseImage);
  689. begin
  690. ConvertBitmapToData(Bitmap, Image.ImageDataPointer^);
  691. end;
  692. {$IFDEF MSWINDOWS}
  693. procedure DisplayImageDataOnDC(DC: HDC; const DstRect: TRect; const ImageData: TImageData; const SrcRect: TRect);
  694. var
  695. OldMode: Integer;
  696. BitmapInfo: Windows.TBitmapInfo;
  697. Bmp: TBitmap;
  698. begin
  699. if TestImage(ImageData) then
  700. begin
  701. Assert(ImageData.Format in [ifA8R8G8B8, ifX8R8G8B8], SBadFormatDisplay);
  702. OldMode := Windows.SetStretchBltMode(DC, COLORONCOLOR);
  703. FillChar(BitmapInfo, SizeOf(BitmapInfo), 0);
  704. with BitmapInfo.bmiHeader do
  705. begin
  706. biSize := SizeOf(TBitmapInfoHeader);
  707. biPlanes := 1;
  708. biBitCount := 32;
  709. biCompression := BI_RGB;
  710. biWidth := ImageData.Width;
  711. biHeight := -ImageData.Height;
  712. biSizeImage := ImageData.Size;
  713. biXPelsPerMeter := 0;
  714. biYPelsPerMeter := 0;
  715. biClrUsed := 0;
  716. biClrImportant := 0;
  717. end;
  718. try
  719. with SrcRect, ImageData do
  720. if Windows.StretchDIBits(DC, DstRect.Left, DstRect.Top,
  721. DstRect.Right - DstRect.Left, DstRect.Bottom - DstRect.Top, Left,
  722. Top, Right - Left, Bottom - Top, Bits, BitmapInfo, DIB_RGB_COLORS, SRCCOPY) <> Height then
  723. begin
  724. // StretchDIBits may fail on some occasions (error 487, http://support.microsoft.com/kb/269585).
  725. // This fallback is slow but works every time. Thanks to Sergey Galezdinov for the fix.
  726. Bmp := TBitmap.Create;
  727. try
  728. ConvertDataToBitmap(ImageData, Bmp);
  729. StretchBlt(DC, DstRect.Left, DstRect.Top, DstRect.Right - DstRect.Left, DstRect.Bottom - DstRect.Top,
  730. Bmp.Canvas.Handle, 0, 0, Width, Height, SRCCOPY);
  731. finally
  732. Bmp.Free;
  733. end;
  734. end;
  735. finally
  736. Windows.SetStretchBltMode(DC, OldMode);
  737. end;
  738. end;
  739. end;
  740. {$ENDIF}
  741. procedure DisplayImageData(DstCanvas: TCanvas; const DstRect: TRect; const ImageData: TImageData);
  742. begin
  743. DisplayImageData(DstCanvas, DstRect, ImageData, Rect(0, 0, ImageData.Width, ImageData.Height));
  744. end;
  745. procedure DisplayImageData(DstCanvas: TCanvas; const DstRect: TRect; const ImageData: TImageData; const SrcRect: TRect);
  746. {$IF Defined(DCC) or Defined(LCLWIN32)} // Delphi or LCL Win32
  747. begin
  748. DisplayImageDataOnDC(DstCanvas.Handle, DstRect, ImageData, SrcRect);
  749. end;
  750. {$ELSEIF Defined(LCLGTK2)}
  751. procedure GDKDrawBitmap(Dest: HDC; DstX, DstY: Integer; SrcX, SrcY,
  752. SrcWidth, SrcHeight: Integer; ImageData: TImageData);
  753. var
  754. P: TPoint;
  755. begin
  756. P := TGtkDeviceContext(Dest).Offset;
  757. Inc(DstX, P.X);
  758. Inc(DstY, P.Y);
  759. if ImageData.Format = ifR8G8B8 then
  760. begin
  761. gdk_draw_rgb_image(TGtkDeviceContext(Dest).Drawable, TGtkDeviceContext(Dest).GC,
  762. DstX, DstY, SrcWidth, SrcHeight, GDK_RGB_DITHER_NONE,
  763. @PUInt32Array(ImageData.Bits)[SrcY * ImageData.Width + SrcX], ImageData.Width * 3);
  764. end
  765. else
  766. begin
  767. gdk_draw_rgb_32_image(TGtkDeviceContext(Dest).Drawable, TGtkDeviceContext(Dest).GC,
  768. DstX, DstY, SrcWidth, SrcHeight, GDK_RGB_DITHER_NONE,
  769. @PUInt32Array(ImageData.Bits)[SrcY * ImageData.Width + SrcX], ImageData.Width * 4);
  770. end;
  771. end;
  772. var
  773. DisplayImage: TImageData;
  774. NewWidth, NewHeight: Integer;
  775. SrcBounds, DstBounds, DstClip: TRect;
  776. begin
  777. if TestImage(ImageData) then
  778. begin
  779. if not (ImageData.Format in [ifR8G8B8, ifA8R8G8B8, ifX8R8G8B8]) then
  780. raise EImagingError.Create(SBadFormatDisplay);
  781. InitImage(DisplayImage);
  782. SrcBounds := RectToBounds(SrcRect);
  783. DstBounds := RectToBounds(DstRect);
  784. WidgetSet.GetClipBox(DstCanvas.Handle, @DstClip);
  785. ClipStretchBounds(SrcBounds.Left, SrcBounds.Top, SrcBounds.Right, SrcBounds.Bottom,
  786. DstBounds.Left, DstBounds.Top, DstBounds.Right, DstBounds.Bottom, ImageData.Width,
  787. ImageData.Height, DstClip);
  788. NewWidth := DstBounds.Right;
  789. NewHeight := DstBounds.Bottom;
  790. if (NewWidth > 0) and (NewHeight > 0) then
  791. begin
  792. if (SrcBounds.Right = NewWidth) and (SrcBounds.Bottom = NewHeight) then
  793. try
  794. CloneImage(ImageData, DisplayImage);
  795. // Swap R-B channels for GTK display compatibility!
  796. SwapChannels(DisplayImage, ChannelRed, ChannelBlue);
  797. GDKDrawBitmap(DstCanvas.Handle, DstBounds.Left, DstBounds.Top,
  798. SrcBounds.Left, SrcBounds.Top, NewWidth, NewHeight, DisplayImage);
  799. finally
  800. FreeImage(DisplayImage);
  801. end
  802. else
  803. try
  804. // Create new image with desired dimensions
  805. NewImage(NewWidth, NewHeight, ImageData.Format, DisplayImage);
  806. // Stretch pixels from old image to new one TResizeFilter = (rfNearest, rfBilinear, rfBicubic);
  807. StretchRect(ImageData, SrcBounds.Left, SrcBounds.Top, SrcBounds.Right,
  808. SrcBounds.Bottom, DisplayImage, 0, 0, NewWidth, NewHeight, rfNearest);
  809. // Swap R-B channels for GTK display compatibility!
  810. SwapChannels(DisplayImage, ChannelRed, ChannelBlue);
  811. GDKDrawBitmap(DstCanvas.Handle, DstBounds.Left, DstBounds.Top, 0, 0,
  812. NewWidth, NewHeight, DisplayImage);
  813. finally
  814. FreeImage(DisplayImage);
  815. end
  816. end;
  817. end;
  818. end;
  819. {$ELSEIF Defined(LCLqt5)}
  820. var
  821. QImage: TQtImage;
  822. Context: TQtDeviceContext;
  823. begin
  824. if TestImage(ImageData) then
  825. begin
  826. if not (ImageData.Format in [ifA8R8G8B8, ifX8R8G8B8]) then
  827. raise EImagingError.Create(SBadFormatDisplay);
  828. Context := TQtDeviceContext(DstCanvas.Handle);
  829. // QImage directly uses the image memory, there is no copy done
  830. QImage := TQtImage.Create(ImageData.Bits, ImageData.Width, ImageData.Height,
  831. ImageData.Width * 4, QImageFormat_ARGB32, False);
  832. try
  833. QPainter_drawImage(Context.Widget, PRect(@DstRect), QImage.Handle, @SrcRect, QtAutoColor);
  834. finally
  835. QImage.Free;
  836. end;
  837. end;
  838. end;
  839. {$ELSEIF Defined(LCLcocoa)}
  840. var
  841. CocoaBmp: TCocoaBitmap;
  842. Context: TCocoaContext;
  843. begin
  844. if TestImage(ImageData) then
  845. begin
  846. if not (ImageData.Format in [ifA8R8G8B8, ifX8R8G8B8]) then
  847. raise EImagingError.Create(SBadFormatDisplay);
  848. Context := CheckDC(DstCanvas.Handle);
  849. // We copy the data since it needs R/B swap and potentially alpha pre-multiply
  850. CocoaBmp := TCocoaBitmap.Create(ImageData.Width, ImageData.Height, 32, 32,
  851. cbaDWord, cbtBGRA, ImageData.Bits, True);
  852. try
  853. Context.DrawImageRep(RectToNSRect(DstRect), RectToNSRect(SrcRect), CocoaBmp.ImageRep);
  854. finally
  855. CocoaBmp.Free;
  856. end;
  857. end;
  858. end;
  859. {$ELSE}
  860. begin
  861. raise EImagingError.Create(SUnsupportedLCLWidgetSet);
  862. end;
  863. {$IFEND}
  864. procedure DisplayImage(DstCanvas: TCanvas; DstX, DstY: LongInt; Image: TBaseImage);
  865. begin
  866. DisplayImageData(DstCanvas, BoundsToRect(DstX, DstY, Image.Width, Image.Height),
  867. Image.ImageDataPointer^, Image.BoundsRect);
  868. end;
  869. procedure DisplayImage(DstCanvas: TCanvas; const DstRect: TRect; Image: TBaseImage);
  870. begin
  871. DisplayImageData(DstCanvas, DstRect, Image.ImageDataPointer^, Image.BoundsRect);
  872. end;
  873. procedure DisplayImage(DstCanvas: TCanvas; const DstRect: TRect; Image: TBaseImage; const SrcRect: TRect);
  874. begin
  875. DisplayImageData(DstCanvas, DstRect, Image.ImageDataPointer^, SrcRect);
  876. end;
  877. { TImagingGraphic class implementation }
  878. constructor TImagingGraphic.Create;
  879. begin
  880. inherited Create;
  881. PixelFormat := pf24Bit;
  882. end;
  883. procedure TImagingGraphic.ReadData(Stream: TStream);
  884. begin
  885. // Here we need to skip ReadData+WriteData of TBitmap (and LCL TRasterBitmap)
  886. // and go to the basics in TGraphic's ReadData+WriteData with just LoadFromStream
  887. // and SaveToStream.
  888. // Some VCL/LCL TGraphic classes also store size of the written data
  889. // before the stream contents. However, the stream passed here
  890. // from TReader.DefineBinaryProperty is already
  891. // a memory stream capped to the size of binary property data.
  892. // Picture.Data = <vaBinary><Size(TWriter)><TGraphicClassName(TPicture)><ImageBits(TImagingGraphicForSave)>
  893. LoadFromStream(Stream);
  894. end;
  895. procedure TImagingGraphic.WriteData(Stream: TStream);
  896. begin
  897. // This can happen when streaming some of the formats that don't have
  898. // TImagingGraphicForSave descendant.
  899. SaveToStream(Stream);
  900. end;
  901. procedure TImagingGraphic.LoadFromStream(Stream: TStream);
  902. var
  903. Image: TSingleImage;
  904. begin
  905. Image := TSingleImage.Create;
  906. try
  907. Image.LoadFromStream(Stream);
  908. Assign(Image);
  909. finally
  910. Image.Free;
  911. end;
  912. end;
  913. procedure TImagingGraphic.SaveToStream(Stream: TStream);
  914. var
  915. Image: TSingleImage;
  916. begin
  917. Image := TSingleImage.Create;
  918. try
  919. Image.Assign(Self);
  920. Image.SaveToStream('png', Stream);
  921. finally
  922. Image.Free;
  923. end;
  924. end;
  925. procedure TImagingGraphic.AssignTo(Dest: TPersistent);
  926. var
  927. Arr: TDynImageDataArray;
  928. begin
  929. if Dest is TSingleImage then
  930. begin
  931. AssignToImage(TSingleImage(Dest))
  932. end
  933. else if Dest is TMultiImage then
  934. begin
  935. SetLength(Arr, 1);
  936. AssignToImageData(Arr[0]);
  937. TMultiImage(Dest).CreateFromArray(Arr);
  938. Imaging.FreeImagesInArray(Arr);
  939. end
  940. else
  941. inherited AssignTo(Dest);
  942. end;
  943. {$IFDEF COMPONENT_SET_LCL}
  944. function TImagingGraphic.GetResourceType: TResourceType;
  945. begin
  946. Result := RT_RCDATA;
  947. end;
  948. class function TImagingGraphic.IsStreamFormatSupported(Stream: TStream): Boolean;
  949. begin
  950. Result := DetermineStreamFormat(Stream) <> '';
  951. end;
  952. {$ENDIF}
  953. procedure TImagingGraphic.Assign(Source: TPersistent);
  954. begin
  955. if Source is TBaseImage then
  956. AssignFromImage(TBaseImage(Source))
  957. else
  958. inherited Assign(Source);
  959. end;
  960. procedure TImagingGraphic.AssignFromImage(Image: TBaseImage);
  961. begin
  962. if (Image <> nil) and Image.Valid then
  963. AssignFromImageData(Image.ImageDataPointer^);
  964. end;
  965. procedure TImagingGraphic.AssignToImage(Image: TBaseImage);
  966. begin
  967. if (Image <> nil) and (Image.ImageDataPointer <> nil) then
  968. AssignToImageData(Image.ImageDataPointer^);
  969. end;
  970. procedure TImagingGraphic.AssignFromImageData(const ImageData: TImageData);
  971. begin
  972. if Imaging.TestImage(ImageData) then
  973. ConvertDataToBitmap(ImageData, Self);
  974. end;
  975. procedure TImagingGraphic.AssignToImageData(var ImageData: TImageData);
  976. begin
  977. Imaging.FreeImage(ImageData);
  978. ConvertBitmapToData(Self, ImageData);
  979. end;
  980. { TImagingGraphicForSave class implementation }
  981. constructor TImagingGraphicForSave.Create;
  982. begin
  983. inherited Create;
  984. FDefaultFileExt := GetFileFormat.Extensions[0];
  985. FSavingFormat := ifUnknown;
  986. GetFileFormat.CheckOptionsValidity;
  987. end;
  988. procedure TImagingGraphicForSave.WriteData(Stream: TStream);
  989. begin
  990. SaveToStream(Stream);
  991. end;
  992. procedure TImagingGraphicForSave.SaveToStream(Stream: TStream);
  993. var
  994. Image: TSingleImage;
  995. begin
  996. if FDefaultFileExt <> '' then
  997. begin
  998. Image := TSingleImage.Create;
  999. try
  1000. Image.Assign(Self);
  1001. if FSavingFormat <> ifUnknown then
  1002. Image.Format := FSavingFormat;
  1003. Image.SaveToStream(FDefaultFileExt, Stream);
  1004. finally
  1005. Image.Free;
  1006. end;
  1007. end;
  1008. end;
  1009. {$IFDEF COMPONENT_SET_LCL}
  1010. class function TImagingGraphicForSave.GetFileExtensions: string;
  1011. begin
  1012. Result := StringReplace(GetFileFormat.Extensions.CommaText, ',', ';', [rfReplaceAll]);
  1013. end;
  1014. function TImagingGraphicForSave.GetMimeType: string;
  1015. begin
  1016. Result := 'image/' + FDefaultFileExt;
  1017. end;
  1018. {$ENDIF}
  1019. {$IFNDEF DONT_LINK_BITMAP}
  1020. constructor TImagingBitmap.Create;
  1021. begin
  1022. inherited Create;
  1023. FUseRLE := (GetFileFormat as TBitmapFileFormat).UseRLE;
  1024. end;
  1025. class function TImagingBitmap.GetFileFormat: TImageFileFormat;
  1026. begin
  1027. Result := FindImageFileFormatByClass(TBitmapFileFormat);
  1028. end;
  1029. procedure TImagingBitmap.SaveToStream(Stream: TStream);
  1030. begin
  1031. Imaging.PushOptions;
  1032. Imaging.SetOption(ImagingBitmapRLE, Ord(FUseRLE));
  1033. inherited SaveToStream(Stream);
  1034. Imaging.PopOptions;
  1035. end;
  1036. {$ENDIF}
  1037. {$IFNDEF DONT_LINK_JPEG}
  1038. constructor TImagingJpeg.Create;
  1039. begin
  1040. inherited Create;
  1041. FQuality := (GetFileFormat as TJpegFileFormat).Quality;
  1042. FProgressive := (GetFileFormat as TJpegFileFormat).Progressive;
  1043. end;
  1044. class function TImagingJpeg.GetFileFormat: TImageFileFormat;
  1045. begin
  1046. Result := FindImageFileFormatByClass(TJpegFileFormat);
  1047. end;
  1048. {$IFDEF COMPONENT_SET_LCL}
  1049. function TImagingJpeg.GetMimeType: string;
  1050. begin
  1051. Result := 'image/jpeg';
  1052. end;
  1053. {$ENDIF}
  1054. procedure TImagingJpeg.SaveToStream(Stream: TStream);
  1055. begin
  1056. Imaging.PushOptions;
  1057. Imaging.SetOption(ImagingJpegQuality, FQuality);
  1058. Imaging.SetOption(ImagingJpegProgressive, Ord(FProgressive));
  1059. inherited SaveToStream(Stream);
  1060. Imaging.PopOptions;
  1061. end;
  1062. {$ENDIF}
  1063. {$IFNDEF DONT_LINK_PNG}
  1064. constructor TImagingPNG.Create;
  1065. begin
  1066. inherited Create;
  1067. FPreFilter := (GetFileFormat as TPNGFileFormat).PreFilter;
  1068. FCompressLevel := (GetFileFormat as TPNGFileFormat).CompressLevel;
  1069. end;
  1070. class function TImagingPNG.GetFileFormat: TImageFileFormat;
  1071. begin
  1072. Result := FindImageFileFormatByClass(TPNGFileFormat);
  1073. end;
  1074. procedure TImagingPNG.SaveToStream(Stream: TStream);
  1075. begin
  1076. Imaging.PushOptions;
  1077. Imaging.SetOption(ImagingPNGPreFilter, FPreFilter);
  1078. Imaging.SetOption(ImagingPNGCompressLevel, FCompressLevel);
  1079. inherited SaveToStream(Stream);
  1080. Imaging.PopOptions;
  1081. end;
  1082. {$ENDIF}
  1083. {$IFNDEF DONT_LINK_GIF}
  1084. class function TImagingGIF.GetFileFormat: TImageFileFormat;
  1085. begin
  1086. Result := FindImageFileFormatByClass(TGIFFileFormat);
  1087. end;
  1088. {$ENDIF}
  1089. {$IFNDEF DONT_LINK_TARGA}
  1090. constructor TImagingTarga.Create;
  1091. begin
  1092. inherited Create;
  1093. FUseRLE := (GetFileFormat as TTargaFileFormat).UseRLE;
  1094. end;
  1095. class function TImagingTarga.GetFileFormat: TImageFileFormat;
  1096. begin
  1097. Result := FindImageFileFormatByClass(TTargaFileFormat);
  1098. end;
  1099. procedure TImagingTarga.SaveToStream(Stream: TStream);
  1100. begin
  1101. Imaging.PushOptions;
  1102. Imaging.SetOption(ImagingTargaRLE, Ord(FUseRLE));
  1103. inherited SaveToStream(Stream);
  1104. Imaging.PopOptions;
  1105. end;
  1106. {$ENDIF}
  1107. {$IFNDEF DONT_LINK_DDS}
  1108. constructor TImagingDDS.Create;
  1109. begin
  1110. inherited Create;
  1111. FCompression := dcNone;
  1112. end;
  1113. class function TImagingDDS.GetFileFormat: TImageFileFormat;
  1114. begin
  1115. Result := FindImageFileFormatByClass(TDDSFileFormat);
  1116. end;
  1117. procedure TImagingDDS.SaveToStream(Stream: TStream);
  1118. begin
  1119. case FCompression of
  1120. dcNone: FSavingFormat := ifUnknown;
  1121. dcDXT1: FSavingFormat := ifDXT1;
  1122. dcDXT3: FSavingFormat := ifDXT3;
  1123. dcDXT5: FSavingFormat := ifDXT5;
  1124. end;
  1125. Imaging.PushOptions;
  1126. Imaging.SetOption(ImagingDDSSaveCubeMap, Ord(False));
  1127. Imaging.SetOption(ImagingDDSSaveVolume, Ord(False));
  1128. Imaging.SetOption(ImagingDDSSaveMipMapCount, 1);
  1129. Imaging.SetOption(ImagingDDSSaveDepth, 1);
  1130. inherited SaveToStream(Stream);
  1131. Imaging.PopOptions;
  1132. end;
  1133. {$ENDIF}
  1134. {$IFNDEF DONT_LINK_MNG}
  1135. constructor TImagingMNG.Create;
  1136. begin
  1137. inherited Create;
  1138. FLossyCompression := (GetFileFormat as TMNGFileFormat).LossyCompression;
  1139. FLossyAlpha := (GetFileFormat as TMNGFileFormat).LossyAlpha;
  1140. FPreFilter := (GetFileFormat as TMNGFileFormat).PreFilter;
  1141. FCompressLevel := (GetFileFormat as TMNGFileFormat).CompressLevel;
  1142. FQuality := (GetFileFormat as TMNGFileFormat).Quality;
  1143. FProgressive := (GetFileFormat as TMNGFileFormat).Progressive;
  1144. end;
  1145. class function TImagingMNG.GetFileFormat: TImageFileFormat;
  1146. begin
  1147. Result := FindImageFileFormatByClass(TMNGFileFormat);
  1148. end;
  1149. {$IFDEF COMPONENT_SET_LCL}
  1150. function TImagingMNG.GetMimeType: string;
  1151. begin
  1152. Result := 'video/mng';
  1153. end;
  1154. {$ENDIF}
  1155. procedure TImagingMNG.SaveToStream(Stream: TStream);
  1156. begin
  1157. Imaging.PushOptions;
  1158. Imaging.SetOption(ImagingMNGLossyCompression, Ord(FLossyCompression));
  1159. Imaging.SetOption(ImagingMNGLossyAlpha, Ord(FLossyAlpha));
  1160. Imaging.SetOption(ImagingMNGPreFilter, FPreFilter);
  1161. Imaging.SetOption(ImagingMNGCompressLevel, FCompressLevel);
  1162. Imaging.SetOption(ImagingMNGQuality, FQuality);
  1163. Imaging.SetOption(ImagingMNGProgressive, Ord(FProgressive));
  1164. inherited SaveToStream(Stream);
  1165. Imaging.PopOptions;
  1166. end;
  1167. {$ENDIF}
  1168. {$IFNDEF DONT_LINK_JNG}
  1169. constructor TImagingJNG.Create;
  1170. begin
  1171. inherited Create;
  1172. FLossyAlpha := (GetFileFormat as TJNGFileFormat).LossyAlpha;
  1173. FAlphaPreFilter := (GetFileFormat as TJNGFileFormat).PreFilter;
  1174. FAlphaCompressLevel := (GetFileFormat as TJNGFileFormat).CompressLevel;
  1175. FQuality := (GetFileFormat as TJNGFileFormat).Quality;
  1176. FProgressive := (GetFileFormat as TJNGFileFormat).Progressive;
  1177. end;
  1178. class function TImagingJNG.GetFileFormat: TImageFileFormat;
  1179. begin
  1180. Result := FindImageFileFormatByClass(TJNGFileFormat);
  1181. end;
  1182. procedure TImagingJNG.SaveToStream(Stream: TStream);
  1183. begin
  1184. Imaging.PushOptions;
  1185. Imaging.SetOption(ImagingJNGLossyALpha, Ord(FLossyAlpha));
  1186. Imaging.SetOption(ImagingJNGAlphaPreFilter, FAlphaPreFilter);
  1187. Imaging.SetOption(ImagingJNGAlphaCompressLevel, FAlphaCompressLevel);
  1188. Imaging.SetOption(ImagingJNGQuality, FQuality);
  1189. Imaging.SetOption(ImagingJNGProgressive, Ord(FProgressive));
  1190. inherited SaveToStream(Stream);
  1191. Imaging.PopOptions;
  1192. end;
  1193. {$ENDIF}
  1194. initialization
  1195. RegisteredFormats := TList.Create;
  1196. RegisterTypes;
  1197. finalization
  1198. UnRegisterTypes;
  1199. RegisteredFormats.Free;
  1200. {$IFEND} // {$IF not Defined(COMPONENT_SET_LCL) and not Defined(COMPONENT_SET_VCL)}
  1201. {
  1202. File Notes:
  1203. -- 0.77.1 ---------------------------------------------------
  1204. - Fixed bug in ConvertBitmapToData causing images from GTK2 bitmaps
  1205. to have swapped RB channels.
  1206. - LCL: Removed GTK1 support (deprecated).
  1207. -- 0.26.3 Changes/Bug Fixes ---------------------------------
  1208. - Transparency of 8bit images (like loaded from 8bit PNG or GIF) is
  1209. kept intact during conversion to TBitmap in ConvertDataToBitmap
  1210. (32bit bitmap is created).
  1211. -- 0.26.3 Changes/Bug Fixes ---------------------------------
  1212. - Setting AlphaFormat property of TBitmap in ConvertDataToBitmap
  1213. when using Delphi 2009+.
  1214. - Fixed garbled LCL TBitmaps created by ConvertDataToBitmap
  1215. in Mac OS X (Carbon).
  1216. -- 0.26.1 Changes/Bug Fixes ---------------------------------
  1217. - Added some more IFDEFs for Lazarus widget sets.
  1218. - Removed CLX code.
  1219. - GTK version of Unix DisplayImageData only used with LCL GTK so the
  1220. the rest of the unit can be used with Qt or other LCL interfaces.
  1221. - Fallback mechanism for DisplayImageDataOnDC, it may fail on occasions.
  1222. - Changed file format conditional compilation to reflect changes
  1223. in LINK symbols.
  1224. - Lazarus 0.9.26 compatibility changes.
  1225. -- 0.24.1 Changes/Bug Fixes ---------------------------------
  1226. - Fixed wrong IFDEF causing that Imaging wouldn't compile in Lazarus
  1227. with GTK2 target.
  1228. - Added comments with code for Lazarus rev. 11861+ regarding
  1229. RawImage interface. Replace current code with that in comments
  1230. if you use Lazarus from SVN. New RawImage interface will be used by
  1231. default after next Lazarus release.
  1232. -- 0.23 Changes/Bug Fixes -----------------------------------
  1233. - Added TImagingGIF.
  1234. -- 0.21 Changes/Bug Fixes -----------------------------------
  1235. - Uses only high level interface now (except for saving options).
  1236. - Slightly changed class hierarchy. TImagingGraphic is now only for loading
  1237. and base class for savers is new TImagingGraphicForSave. Also
  1238. TImagingGraphic is now registered with all supported file formats
  1239. by TPicture's format support.
  1240. -- 0.19 Changes/Bug Fixes -----------------------------------
  1241. - added DisplayImage procedures (thanks to Paul Michell, modified)
  1242. - removed RegisterTypes and UnRegisterTypes from interface section,
  1243. they are called automatically
  1244. - added procedures: ConvertImageToBitmap and ConvertBitmapToImage
  1245. -- 0.17 Changes/Bug Fixes -----------------------------------
  1246. - LCL data to bitmap conversion didn't work in Linux, fixed
  1247. - added MNG file format
  1248. - added JNG file format
  1249. -- 0.15 Changes/Bug Fixes -----------------------------------
  1250. - made it LCL compatible
  1251. - made it CLX compatible
  1252. - added all initial stuff
  1253. }
  1254. end.