ImagingComponents.pas 40 KB

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