ImagingComponents.pas 42 KB

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