Repo for the search and displace ingest module that takes odf, docx and pdf and transforms it into .md to be used with search and displace operations
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1308 lines
42 KiB

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