ImagingComponents.pas 39 KB

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