ImagingComponents.pas 42 KB

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