ImagingComponents.pas 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260
  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. {$ENDIF}
  521. {$IFDEF COMPONENT_SET_LCL}
  522. // Create 32bit raw image from image data
  523. FillChar(RawImage, SizeOf(RawImage), 0);
  524. with RawImage.Description do
  525. begin
  526. Width := WorkData.Width;
  527. Height := WorkData.Height;
  528. BitsPerPixel := Info.BytesPerPixel * 8;
  529. Format := ricfRGBA;
  530. LineEnd := rileByteBoundary;
  531. BitOrder := riboBitsInOrder;
  532. ByteOrder := riboLSBFirst;
  533. LineOrder := riloTopToBottom;
  534. AlphaPrec := 8;
  535. RedPrec := 8;
  536. GreenPrec := 8;
  537. BluePrec := 8;
  538. AlphaShift := 24;
  539. RedShift := 16;
  540. GreenShift := 8;
  541. BlueShift := 0;
  542. Depth := 24;
  543. end;
  544. RawImage.Data := WorkData.Bits;
  545. RawImage.DataSize := WorkData.Size;
  546. // Create bitmap from raw image
  547. { If you get complitation error here upgrade to Lazarus 0.9.24+ }
  548. if RawImage_CreateBitmaps(RawImage, ImgHandle, ImgMaskHandle, False) then
  549. begin
  550. Bitmap.Handle := ImgHandle;
  551. Bitmap.MaskHandle := ImgMaskHandle;
  552. end;
  553. {$ENDIF}
  554. if WorkData.Bits <> Data.Bits then
  555. Imaging.FreeImage(WorkData);
  556. end;
  557. procedure ConvertBitmapToData(Bitmap: TBitmap; var Data: TImageData);
  558. var
  559. I, LineBytes: LongInt;
  560. Format: TImageFormat;
  561. Info: TImageFormatInfo;
  562. {$IFDEF COMPONENT_SET_VCL}
  563. Colors: Word;
  564. LogPalette: TMaxLogPalette;
  565. {$ENDIF}
  566. {$IFDEF COMPONENT_SET_LCL}
  567. RawImage: TRawImage;
  568. LineLazBytes: LongInt;
  569. {$ENDIF}
  570. begin
  571. {$IFDEF COMPONENT_SET_LCL}
  572. // In the current Lazarus 0.9.10 Bitmap.PixelFormat property is useless.
  573. // We cannot change bitmap's format by changing it (it will just release
  574. // old image but not convert it to new format) nor we can determine bitmaps's
  575. // current format (it is usually set to pfDevice). So bitmap's format is obtained
  576. // trough RawImage api and cannot be changed to mirror some Imaging format
  577. // (so formats with no coresponding Imaging format cannot be saved now).
  578. if RawImage_DescriptionFromBitmap(Bitmap.Handle, RawImage.Description) then
  579. case RawImage.Description.BitsPerPixel of
  580. 8: Format := ifIndex8;
  581. 16:
  582. if RawImage.Description.Depth = 15 then
  583. Format := ifA1R5G5B5
  584. else
  585. Format := ifR5G6B5;
  586. 24: Format := ifR8G8B8;
  587. 32: Format := ifA8R8G8B8;
  588. 48: Format := ifR16G16B16;
  589. 64: Format := ifA16R16G16B16;
  590. else
  591. Format := ifUnknown;
  592. end;
  593. {$ELSE}
  594. Format := PixelFormatToDataFormat(Bitmap.PixelFormat);
  595. if Format = ifUnknown then
  596. begin
  597. // Convert from formats not supported by Imaging (1/4 bit)
  598. if Bitmap.PixelFormat < pf8bit then
  599. Bitmap.PixelFormat := pf8bit
  600. else
  601. Bitmap.PixelFormat := pf32bit;
  602. Format := PixelFormatToDataFormat(Bitmap.PixelFormat);
  603. end;
  604. {$ENDIF}
  605. if Format = ifUnknown then
  606. RaiseImaging(SBadFormatBitmapToData, []);
  607. Imaging.NewImage(Bitmap.Width, Bitmap.Height, Format, Data);
  608. GetImageFormatInfo(Data.Format, Info);
  609. LineBytes := Data.Width * Info.BytesPerPixel;
  610. {$IFDEF COMPONENT_SET_VCL}
  611. if (Format = ifIndex8) and (GetObject(Bitmap.Palette, SizeOf(Colors),
  612. @Colors) <> 0) then
  613. begin
  614. // Copy palette
  615. GetPaletteEntries(Bitmap.Palette, 0, Colors, LogPalette.palPalEntry);
  616. if Colors > Info.PaletteEntries then
  617. Colors := Info.PaletteEntries;
  618. for I := 0 to Colors - 1 do
  619. with LogPalette do
  620. begin
  621. Data.Palette[I].A := $FF;
  622. Data.Palette[I].R := palPalEntry[I].peRed;
  623. Data.Palette[I].G := palPalEntry[I].peGreen;
  624. Data.Palette[I].B := palPalEntry[I].peBlue;
  625. end;
  626. end;
  627. // Copy scanlines
  628. for I := 0 to Data.Height - 1 do
  629. Move(Bitmap.ScanLine[I]^, PByteArray(Data.Bits)[I * LineBytes], LineBytes);
  630. {$ENDIF}
  631. {$IFDEF COMPONENT_SET_LCL}
  632. // Get raw image from bitmap (mask handle must be 0 or expect violations)
  633. if RawImage_FromBitmap(RawImage, Bitmap.Handle, 0, nil) then
  634. begin
  635. LineLazBytes := GetBytesPerLine(Data.Width, RawImage.Description.BitsPerPixel,
  636. RawImage.Description.LineEnd);
  637. // Copy scanlines
  638. for I := 0 to Data.Height - 1 do
  639. Move(PByteArray(RawImage.Data)[I * LineLazBytes],
  640. PByteArray(Data.Bits)[I * LineBytes], LineBytes);
  641. { If you get complitation error here upgrade to Lazarus 0.9.24+ }
  642. RawImage.FreeData;
  643. end;
  644. {$ENDIF}
  645. end;
  646. procedure ConvertImageToBitmap(Image: TBaseImage; Bitmap: TBitmap);
  647. begin
  648. ConvertDataToBitmap(Image.ImageDataPointer^, Bitmap);
  649. end;
  650. procedure ConvertBitmapToImage(Bitmap: TBitmap; Image: TBaseImage);
  651. begin
  652. ConvertBitmapToData(Bitmap, Image.ImageDataPointer^);
  653. end;
  654. {$IFDEF MSWINDOWS}
  655. procedure DisplayImageDataOnDC(DC: HDC; const DstRect: TRect; const ImageData: TImageData; const SrcRect: TRect);
  656. var
  657. OldMode: Integer;
  658. BitmapInfo: Windows.TBitmapInfo;
  659. Bmp: TBitmap;
  660. begin
  661. if TestImage(ImageData) then
  662. begin
  663. Assert(ImageData.Format in [ifA8R8G8B8, ifX8R8G8B8], SBadFormatDisplay);
  664. OldMode := Windows.SetStretchBltMode(DC, COLORONCOLOR);
  665. FillChar(BitmapInfo, SizeOf(BitmapInfo), 0);
  666. with BitmapInfo.bmiHeader do
  667. begin
  668. biSize := SizeOf(TBitmapInfoHeader);
  669. biPlanes := 1;
  670. biBitCount := 32;
  671. biCompression := BI_RGB;
  672. biWidth := ImageData.Width;
  673. biHeight := -ImageData.Height;
  674. biSizeImage := ImageData.Size;
  675. biXPelsPerMeter := 0;
  676. biYPelsPerMeter := 0;
  677. biClrUsed := 0;
  678. biClrImportant := 0;
  679. end;
  680. try
  681. with SrcRect, ImageData do
  682. if Windows.StretchDIBits(DC, DstRect.Left, DstRect.Top,
  683. DstRect.Right - DstRect.Left, DstRect.Bottom - DstRect.Top, Left,
  684. Top, Right - Left, Bottom - Top, Bits, BitmapInfo, DIB_RGB_COLORS, SRCCOPY) <> Height then
  685. begin
  686. // StretchDIBits may fail on some ocassions (error 487, http://support.microsoft.com/kb/269585).
  687. // This fallback is slow but works every time. Thanks to Sergey Galezdinov for the fix.
  688. Bmp := TBitmap.Create;
  689. try
  690. ConvertDataToBitmap(ImageData, Bmp);
  691. StretchBlt(DC, DstRect.Left, DstRect.Top, DstRect.Right - DstRect.Left, DstRect.Bottom - DstRect.Top,
  692. Bmp.Canvas.Handle, 0, 0, Width, Height, SRCCOPY);
  693. finally
  694. Bmp.Free;
  695. end;
  696. end;
  697. finally
  698. Windows.SetStretchBltMode(DC, OldMode);
  699. end;
  700. end;
  701. end;
  702. {$ENDIF}
  703. procedure DisplayImageData(DstCanvas: TCanvas; const DstRect: TRect; const ImageData: TImageData; const SrcRect: TRect);
  704. {$IF Defined(DCC) or Defined(LCLWIN32)} // Delphi or LCL Win32
  705. begin
  706. DisplayImageDataOnDC(DstCanvas.Handle, DstRect, ImageData, SrcRect);
  707. end;
  708. {$ELSEIF Defined(LCLGTK) or Defined(LCLGTK2)}
  709. procedure GDKDrawBitmap(Dest: HDC; DstX, DstY: Integer; SrcX, SrcY,
  710. SrcWidth, SrcHeight: Integer; ImageData: TImageData);
  711. var
  712. P: TPoint;
  713. begin
  714. P := TGtkDeviceContext(Dest).Offset;
  715. Inc(DstX, P.X);
  716. Inc(DstY, P.Y);
  717. gdk_draw_rgb_32_image(TGtkDeviceContext(Dest).Drawable, TGtkDeviceContext(Dest).GC,
  718. DstX, DstY, SrcWidth, SrcHeight, GDK_RGB_DITHER_NONE,
  719. @PLongWordArray(ImageData.Bits)[SrcY * ImageData.Width + SrcX], ImageData.Width * 4);
  720. end;
  721. var
  722. DisplayImage: TImageData;
  723. NewWidth, NewHeight: Integer;
  724. SrcBounds, DstBounds, DstClip: TRect;
  725. begin
  726. if TestImage(ImageData) then
  727. begin
  728. Assert(ImageData.Format in [ifA8R8G8B8, ifX8R8G8B8], SBadFormatDisplay);
  729. InitImage(DisplayImage);
  730. SrcBounds := RectToBounds(SrcRect);
  731. DstBounds := RectToBounds(DstRect);
  732. WidgetSet.GetClipBox(DstCanvas.Handle, @DstClip);
  733. ClipStretchBounds(SrcBounds.Left, SrcBounds.Top, SrcBounds.Right, SrcBounds.Bottom,
  734. DstBounds.Left, DstBounds.Top, DstBounds.Right, DstBounds.Bottom, ImageData.Width,
  735. ImageData.Height, DstClip);
  736. NewWidth := DstBounds.Right;
  737. NewHeight := DstBounds.Bottom;
  738. if (NewWidth > 0) and (NewHeight > 0) then
  739. begin
  740. if (SrcBounds.Right = NewWidth) and (SrcBounds.Bottom = NewHeight) then
  741. try
  742. CloneImage(ImageData, DisplayImage);
  743. // Swap R-B channels for GTK display compatability!
  744. SwapChannels(DisplayImage, ChannelRed, ChannelBlue);
  745. GDKDrawBitmap(DstCanvas.Handle, DstBounds.Left, DstBounds.Top,
  746. SrcBounds.Left, SrcBounds.Top, NewWidth, NewHeight, DisplayImage);
  747. finally
  748. FreeImage(DisplayImage);
  749. end
  750. else
  751. try
  752. // Create new image with desired dimensions
  753. NewImage(NewWidth, NewHeight, ImageData.Format, DisplayImage);
  754. // Stretch pixels from old image to new one TResizeFilter = (rfNearest, rfBilinear, rfBicubic);
  755. StretchRect(ImageData, SrcBounds.Left, SrcBounds.Top, SrcBounds.Right,
  756. SrcBounds.Bottom, DisplayImage, 0, 0, NewWidth, NewHeight, rfNearest);
  757. // Swap R-B channels for GTK display compatability!
  758. SwapChannels(DisplayImage, ChannelRed, ChannelBlue);
  759. GDKDrawBitmap(DstCanvas.Handle, DstBounds.Left, DstBounds.Top, 0, 0,
  760. NewWidth, NewHeight, DisplayImage);
  761. finally
  762. FreeImage(DisplayImage);
  763. end
  764. end;
  765. end;
  766. end;
  767. {$ELSE}
  768. begin
  769. raise Exception.Create(SUnsupportedLCLWidgetSet);
  770. end;
  771. {$IFEND}
  772. procedure DisplayImage(DstCanvas: TCanvas; DstX, DstY: LongInt; Image: TBaseImage);
  773. begin
  774. DisplayImageData(DstCanvas, BoundsToRect(DstX, DstY, Image.Width, Image.Height),
  775. Image.ImageDataPointer^, Image.BoundsRect);
  776. end;
  777. procedure DisplayImage(DstCanvas: TCanvas; const DstRect: TRect; Image: TBaseImage);
  778. begin
  779. DisplayImageData(DstCanvas, DstRect, Image.ImageDataPointer^, Image.BoundsRect);
  780. end;
  781. procedure DisplayImage(DstCanvas: TCanvas; const DstRect: TRect; Image: TBaseImage; const SrcRect: TRect);
  782. begin
  783. DisplayImageData(DstCanvas, DstRect, Image.ImageDataPointer^, SrcRect);
  784. end;
  785. { TImagingGraphic class implementation }
  786. constructor TImagingGraphic.Create;
  787. begin
  788. inherited Create;
  789. PixelFormat := pf24Bit;
  790. end;
  791. procedure TImagingGraphic.LoadFromStream(Stream: TStream);
  792. begin
  793. ReadDataFromStream(Stream);
  794. end;
  795. procedure TImagingGraphic.ReadDataFromStream(Stream: TStream);
  796. var
  797. Image: TSingleImage;
  798. begin
  799. Image := TSingleImage.Create;
  800. try
  801. Image.LoadFromStream(Stream);
  802. Assign(Image);
  803. finally
  804. Image.Free;
  805. end;
  806. end;
  807. procedure TImagingGraphic.AssignTo(Dest: TPersistent);
  808. var
  809. Arr: TDynImageDataArray;
  810. begin
  811. if Dest is TSingleImage then
  812. begin
  813. AssignToImage(TSingleImage(Dest))
  814. end
  815. else if Dest is TMultiImage then
  816. begin
  817. SetLength(Arr, 1);
  818. AssignToImageData(Arr[0]);
  819. TMultiImage(Dest).CreateFromArray(Arr);
  820. Imaging.FreeImagesInArray(Arr);
  821. end
  822. else
  823. inherited AssignTo(Dest);
  824. end;
  825. procedure TImagingGraphic.Assign(Source: TPersistent);
  826. begin
  827. if Source is TBaseImage then
  828. AssignFromImage(TBaseImage(Source))
  829. else
  830. inherited Assign(Source);
  831. end;
  832. procedure TImagingGraphic.AssignFromImage(Image: TBaseImage);
  833. begin
  834. if (Image <> nil) and Image.Valid then
  835. AssignFromImageData(Image.ImageDataPointer^);
  836. end;
  837. procedure TImagingGraphic.AssignToImage(Image: TBaseImage);
  838. begin
  839. if (Image <> nil) and (Image.ImageDataPointer <> nil) then
  840. AssignToImageData(Image.ImageDataPointer^);
  841. end;
  842. procedure TImagingGraphic.AssignFromImageData(const ImageData: TImageData);
  843. begin
  844. if Imaging.TestImage(ImageData) then
  845. ConvertDataToBitmap(ImageData, Self);
  846. end;
  847. procedure TImagingGraphic.AssignToImageData(var ImageData: TImageData);
  848. begin
  849. Imaging.FreeImage(ImageData);
  850. ConvertBitmapToData(Self, ImageData);
  851. end;
  852. { TImagingGraphicForSave class implementation }
  853. constructor TImagingGraphicForSave.Create;
  854. begin
  855. inherited Create;
  856. FDefaultFileExt := GetFileFormat.Extensions[0];
  857. FSavingFormat := ifUnknown;
  858. GetFileFormat.CheckOptionsValidity;
  859. end;
  860. procedure TImagingGraphicForSave.WriteDataToStream(Stream: TStream);
  861. var
  862. Image: TSingleImage;
  863. begin
  864. if FDefaultFileExt <> '' then
  865. begin
  866. Image := TSingleImage.Create;
  867. try
  868. Image.Assign(Self);
  869. if FSavingFormat <> ifUnknown then
  870. Image.Format := FSavingFormat;
  871. Image.SaveToStream(FDefaultFileExt, Stream);
  872. finally
  873. Image.Free;
  874. end;
  875. end;
  876. end;
  877. procedure TImagingGraphicForSave.SaveToStream(Stream: TStream);
  878. begin
  879. WriteDataToStream(Stream);
  880. end;
  881. {$IFDEF COMPONENT_SET_LCL}
  882. class function TImagingGraphicForSave.GetFileExtensions: string;
  883. begin
  884. Result := StringReplace(GetFileFormat.Extensions.CommaText, ',', ';', [rfReplaceAll]);
  885. end;
  886. function TImagingGraphicForSave.GetMimeType: string;
  887. begin
  888. Result := 'image/' + FDefaultFileExt;
  889. end;
  890. {$ENDIF}
  891. {$IFNDEF DONT_LINK_BITMAP}
  892. { TImagingBitmap class implementation }
  893. constructor TImagingBitmap.Create;
  894. begin
  895. inherited Create;
  896. FUseRLE := (GetFileFormat as TBitmapFileFormat).UseRLE;
  897. end;
  898. class function TImagingBitmap.GetFileFormat: TImageFileFormat;
  899. begin
  900. Result := FindImageFileFormatByClass(TBitmapFileFormat);
  901. end;
  902. procedure TImagingBitmap.SaveToStream(Stream: TStream);
  903. begin
  904. Imaging.PushOptions;
  905. Imaging.SetOption(ImagingBitmapRLE, Ord(FUseRLE));
  906. inherited SaveToStream(Stream);
  907. Imaging.PopOptions;
  908. end;
  909. {$ENDIF}
  910. {$IFNDEF DONT_LINK_JPEG}
  911. { TImagingJpeg class implementation }
  912. constructor TImagingJpeg.Create;
  913. begin
  914. inherited Create;
  915. FQuality := (GetFileFormat as TJpegFileFormat).Quality;
  916. FProgressive := (GetFileFormat as TJpegFileFormat).Progressive;
  917. end;
  918. class function TImagingJpeg.GetFileFormat: TImageFileFormat;
  919. begin
  920. Result := FindImageFileFormatByClass(TJpegFileFormat);
  921. end;
  922. {$IFDEF COMPONENT_SET_LCL}
  923. function TImagingJpeg.GetMimeType: string;
  924. begin
  925. Result := 'image/jpeg';
  926. end;
  927. {$ENDIF}
  928. procedure TImagingJpeg.SaveToStream(Stream: TStream);
  929. begin
  930. Imaging.PushOptions;
  931. Imaging.SetOption(ImagingJpegQuality, FQuality);
  932. Imaging.SetOption(ImagingJpegProgressive, Ord(FProgressive));
  933. inherited SaveToStream(Stream);
  934. Imaging.PopOptions;
  935. end;
  936. {$ENDIF}
  937. {$IFNDEF DONT_LINK_PNG}
  938. { TImagingPNG class implementation }
  939. constructor TImagingPNG.Create;
  940. begin
  941. inherited Create;
  942. FPreFilter := (GetFileFormat as TPNGFileFormat).PreFilter;
  943. FCompressLevel := (GetFileFormat as TPNGFileFormat).CompressLevel;
  944. end;
  945. class function TImagingPNG.GetFileFormat: TImageFileFormat;
  946. begin
  947. Result := FindImageFileFormatByClass(TPNGFileFormat);
  948. end;
  949. procedure TImagingPNG.SaveToStream(Stream: TStream);
  950. begin
  951. Imaging.PushOptions;
  952. Imaging.SetOption(ImagingPNGPreFilter, FPreFilter);
  953. Imaging.SetOption(ImagingPNGCompressLevel, FCompressLevel);
  954. inherited SaveToStream(Stream);
  955. Imaging.PopOptions;
  956. end;
  957. {$ENDIF}
  958. {$IFNDEF DONT_LINK_GIF}
  959. { TImagingGIF class implementation}
  960. class function TImagingGIF.GetFileFormat: TImageFileFormat;
  961. begin
  962. Result := FindImageFileFormatByClass(TGIFFileFormat);
  963. end;
  964. {$ENDIF}
  965. {$IFNDEF DONT_LINK_TARGA}
  966. { TImagingTarga class implementation }
  967. constructor TImagingTarga.Create;
  968. begin
  969. inherited Create;
  970. FUseRLE := (GetFileFormat as TTargaFileFormat).UseRLE;
  971. end;
  972. class function TImagingTarga.GetFileFormat: TImageFileFormat;
  973. begin
  974. Result := FindImageFileFormatByClass(TTargaFileFormat);
  975. end;
  976. procedure TImagingTarga.SaveToStream(Stream: TStream);
  977. begin
  978. Imaging.PushOptions;
  979. Imaging.SetOption(ImagingTargaRLE, Ord(FUseRLE));
  980. inherited SaveToStream(Stream);
  981. Imaging.PopOptions;
  982. end;
  983. {$ENDIF}
  984. {$IFNDEF DONT_LINK_DDS}
  985. { TImagingDDS class implementation }
  986. constructor TImagingDDS.Create;
  987. begin
  988. inherited Create;
  989. FCompression := dcNone;
  990. end;
  991. class function TImagingDDS.GetFileFormat: TImageFileFormat;
  992. begin
  993. Result := FindImageFileFormatByClass(TDDSFileFormat);
  994. end;
  995. procedure TImagingDDS.SaveToStream(Stream: TStream);
  996. begin
  997. case FCompression of
  998. dcNone: FSavingFormat := ifUnknown;
  999. dcDXT1: FSavingFormat := ifDXT1;
  1000. dcDXT3: FSavingFormat := ifDXT3;
  1001. dcDXT5: FSavingFormat := ifDXT5;
  1002. end;
  1003. Imaging.PushOptions;
  1004. Imaging.SetOption(ImagingDDSSaveCubeMap, Ord(False));
  1005. Imaging.SetOption(ImagingDDSSaveVolume, Ord(False));
  1006. Imaging.SetOption(ImagingDDSSaveMipMapCount, 1);
  1007. Imaging.SetOption(ImagingDDSSaveDepth, 1);
  1008. inherited SaveToStream(Stream);
  1009. Imaging.PopOptions;
  1010. end;
  1011. {$ENDIF}
  1012. {$IFNDEF DONT_LINK_MNG}
  1013. { TImagingMNG class implementation }
  1014. constructor TImagingMNG.Create;
  1015. begin
  1016. inherited Create;
  1017. FLossyCompression := (GetFileFormat as TMNGFileFormat).LossyCompression;
  1018. FLossyAlpha := (GetFileFormat as TMNGFileFormat).LossyAlpha;
  1019. FPreFilter := (GetFileFormat as TMNGFileFormat).PreFilter;
  1020. FCompressLevel := (GetFileFormat as TMNGFileFormat).CompressLevel;
  1021. FQuality := (GetFileFormat as TMNGFileFormat).Quality;
  1022. FProgressive := (GetFileFormat as TMNGFileFormat).Progressive;
  1023. end;
  1024. class function TImagingMNG.GetFileFormat: TImageFileFormat;
  1025. begin
  1026. Result := FindImageFileFormatByClass(TMNGFileFormat);
  1027. end;
  1028. {$IFDEF COMPONENT_SET_LCL}
  1029. function TImagingMNG.GetMimeType: string;
  1030. begin
  1031. Result := 'video/mng';
  1032. end;
  1033. {$ENDIF}
  1034. procedure TImagingMNG.SaveToStream(Stream: TStream);
  1035. begin
  1036. Imaging.PushOptions;
  1037. Imaging.SetOption(ImagingMNGLossyCompression, Ord(FLossyCompression));
  1038. Imaging.SetOption(ImagingMNGLossyAlpha, Ord(FLossyAlpha));
  1039. Imaging.SetOption(ImagingMNGPreFilter, FPreFilter);
  1040. Imaging.SetOption(ImagingMNGCompressLevel, FCompressLevel);
  1041. Imaging.SetOption(ImagingMNGQuality, FQuality);
  1042. Imaging.SetOption(ImagingMNGProgressive, Ord(FProgressive));
  1043. inherited SaveToStream(Stream);
  1044. Imaging.PopOptions;
  1045. end;
  1046. {$ENDIF}
  1047. {$IFNDEF DONT_LINK_JNG}
  1048. { TImagingJNG class implementation }
  1049. constructor TImagingJNG.Create;
  1050. begin
  1051. inherited Create;
  1052. FLossyAlpha := (GetFileFormat as TJNGFileFormat).LossyAlpha;
  1053. FAlphaPreFilter := (GetFileFormat as TJNGFileFormat).PreFilter;
  1054. FAlphaCompressLevel := (GetFileFormat as TJNGFileFormat).CompressLevel;
  1055. FQuality := (GetFileFormat as TJNGFileFormat).Quality;
  1056. FProgressive := (GetFileFormat as TJNGFileFormat).Progressive;
  1057. end;
  1058. class function TImagingJNG.GetFileFormat: TImageFileFormat;
  1059. begin
  1060. Result := FindImageFileFormatByClass(TJNGFileFormat);
  1061. end;
  1062. procedure TImagingJNG.SaveToStream(Stream: TStream);
  1063. begin
  1064. Imaging.PushOptions;
  1065. Imaging.SetOption(ImagingJNGLossyALpha, Ord(FLossyAlpha));
  1066. Imaging.SetOption(ImagingJNGAlphaPreFilter, FAlphaPreFilter);
  1067. Imaging.SetOption(ImagingJNGAlphaCompressLevel, FAlphaCompressLevel);
  1068. Imaging.SetOption(ImagingJNGQuality, FQuality);
  1069. Imaging.SetOption(ImagingJNGProgressive, Ord(FProgressive));
  1070. inherited SaveToStream(Stream);
  1071. Imaging.PopOptions;
  1072. end;
  1073. {$ENDIF}
  1074. initialization
  1075. RegisterTypes;
  1076. finalization
  1077. UnRegisterTypes;
  1078. {$IFEND} // {$IF not Defined(COMPONENT_SET_LCL) and not Defined(COMPONENT_SET_VCL)}
  1079. {
  1080. File Notes:
  1081. -- TODOS ----------------------------------------------------
  1082. - nothing now
  1083. -- 0.26.1 Changes/Bug Fixes ---------------------------------
  1084. - Added some more IFDEFs for Lazarus widget sets.
  1085. - Removed CLX code.
  1086. - GTK version of Unix DisplayImageData only used with LCL GTK so the
  1087. the rest of the unit can be used with Qt or other LCL interfaces.
  1088. - Fallback mechanism for DisplayImageDataOnDC, it may fail on occasions.
  1089. - Changed file format conditional compilation to reflect changes
  1090. in LINK symbols.
  1091. - Lazarus 0.9.26 compatibility changes.
  1092. -- 0.24.1 Changes/Bug Fixes ---------------------------------
  1093. - Fixed wrong IFDEF causing that Imaging wouldn't compile in Lazarus
  1094. with GTK2 target.
  1095. - Added commnets with code for Lazarus rev. 11861+ regarding
  1096. RawImage interface. Replace current code with that in comments
  1097. if you use Lazarus from SVN. New RawImage interface will be used by
  1098. default after next Lazarus release.
  1099. -- 0.23 Changes/Bug Fixes -----------------------------------
  1100. - Added TImagingGIF.
  1101. -- 0.21 Changes/Bug Fixes -----------------------------------
  1102. - Uses only high level interface now (except for saving options).
  1103. - Slightly changed class hierarchy. TImagingGraphic is now only for loading
  1104. and base class for savers is new TImagingGraphicForSave. Also
  1105. TImagingGraphic is now registered with all supported file formats
  1106. by TPicture's format support.
  1107. -- 0.19 Changes/Bug Fixes -----------------------------------
  1108. - added DisplayImage procedures (thanks to Paul Michell, modified)
  1109. - removed RegisterTypes and UnRegisterTypes from interface section,
  1110. they are called automatically
  1111. - added procedures: ConvertImageToBitmap and ConvertBitmapToImage
  1112. -- 0.17 Changes/Bug Fixes -----------------------------------
  1113. - LCL data to bitmap conversion didn´t work in Linux, fixed
  1114. - added MNG file format
  1115. - added JNG file format
  1116. -- 0.15 Changes/Bug Fixes -----------------------------------
  1117. - made it LCL compatible
  1118. - made it CLX compatible
  1119. - added all initial stuff
  1120. }
  1121. end.