ImagingComponents.pas 41 KB

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