2
0

ImagingNET.pas 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372
  1. {
  2. $Id: ImagingNET.pas,v 1.8 2006/10/26 13:29:30 galfar Exp $
  3. Vampyre Imaging Library
  4. by Marek Mauder ([email protected])
  5. http://imaginglib.sourceforge.net
  6. The contents of this file are used with permission, subject to the Mozilla
  7. Public License Version 1.1 (the "License"); you may not use this file except
  8. in compliance with the License. You may obtain a copy of the License at
  9. http://www.mozilla.org/MPL/MPL-1.1.html
  10. Software distributed under the License is distributed on an "AS IS" basis,
  11. WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
  12. the specific language governing rights and limitations under the License.
  13. Alternatively, the contents of this file may be used under the terms of the
  14. GNU Lesser General Public License (the "LGPL License"), in which case the
  15. provisions of the LGPL License are applicable instead of those above.
  16. If you wish to allow use of your version of this file only under the terms
  17. of the LGPL License and not to allow others to use your version of this file
  18. under the MPL, indicate your decision by deleting the provisions above and
  19. replace them with the notice and other provisions required by the LGPL
  20. License. If you do not delete the provisions above, a recipient may use
  21. your version of this file under either the MPL or the LGPL License.
  22. For more information about the LGPL: http://www.gnu.org/copyleft/lesser.html
  23. }
  24. { This is import wrapper for Delphi.NET. You need VampyreImaging.dll
  25. located somewhere Windows can find it. You can use functions directly
  26. imported from DLL or much more dotNET like Imaging class members.}
  27. unit ImagingNET;
  28. {$MINENUMSIZE 4}
  29. interface
  30. uses
  31. System.Runtime.InteropServices,
  32. System.Security;
  33. const
  34. ImagingVersionMajor = 0;
  35. ImagingVersionMinor = 20;
  36. ImagingVersionPatch = 0;
  37. ImagingJpegQuality = 10;
  38. ImagingJpegProgressive = 11;
  39. ImagingBitmapRLE = 12;
  40. ImagingTargaRLE = 13;
  41. ImagingDDSLoadedCubeMap = 14;
  42. ImagingDDSLoadedVolume = 15;
  43. ImagingDDSLoadedMipMapCount = 16;
  44. ImagingDDSLoadedDepth = 17;
  45. ImagingDDSSaveCubeMap = 18;
  46. ImagingDDSSaveVolume = 19;
  47. ImagingDDSSaveMipMapCount = 20;
  48. ImagingDDSSaveDepth = 21;
  49. ImagingPNGPreFilter = 25;
  50. ImagingPNGCompressLevel = 26;
  51. ImagingMNGLossyCompression = 28;
  52. ImagingMNGLossyAlpha = 29;
  53. ImagingMNGPreFilter = 30;
  54. ImagingMNGCompressLevel = 31;
  55. ImagingMNGQuality = 32;
  56. ImagingMNGProgressive = 33;
  57. ImagingJNGLossyAlpha = 40;
  58. ImagingJNGAlphaPreFilter = 41;
  59. ImagingJNGAlphaCompressLevel = 42;
  60. ImagingJNGQuality = 43;
  61. ImagingJNGProgressive = 44;
  62. ImagingColorReductionMask = 128;
  63. ImagingLoadOverrideFormat = 129;
  64. ImagingSaveOverrideFormat = 130;
  65. ImagingMipMapFilter = 131;
  66. InvalidOption = -$7FFFFFFF;
  67. ChannelBlue = 0;
  68. ChannelGreen = 1;
  69. ChannelRed = 2;
  70. ChannelAlpha = 3;
  71. type
  72. TImageFormat = (
  73. ifUnknown = 0,
  74. ifDefault = 1,
  75. // indexed formats using palette
  76. ifIndex8 = 10,
  77. // grayscale formats
  78. ifGray8 = 40,
  79. ifA8Gray8 = 41,
  80. ifGray16 = 42,
  81. ifGray32 = 43,
  82. ifGray64 = 44,
  83. ifA16Gray16 = 45,
  84. // ARGB formats
  85. ifX5R1G1B1 = 80,
  86. ifR3G3B2 = 81,
  87. ifR5G6B5 = 82,
  88. ifA1R5G5B5 = 83,
  89. ifA4R4G4B4 = 84,
  90. ifX1R5G5B5 = 85,
  91. ifX4R4G4B4 = 86,
  92. ifR8G8B8 = 87,
  93. ifA8R8G8B8 = 88,
  94. ifX8R8G8B8 = 89,
  95. ifR16G16B16 = 90,
  96. ifA16R16G16B16 = 91,
  97. ifB16G16R16 = 92,
  98. ifA16B16G16R16 = 93,
  99. // floating point formats
  100. ifR32F = 170,
  101. ifA32R32G32B32F = 171,
  102. ifA32B32G32R32F = 172,
  103. ifR16F = 173,
  104. ifA16R16G16B16F = 174,
  105. ifA16B16G16R16F = 175,
  106. // special formats
  107. ifDXT1 = 220,
  108. ifDXT3 = 221,
  109. ifDXT5 = 222);
  110. TColor32 = UInt32;
  111. TColor64 = UInt64;
  112. TPalette32 = IntPtr;
  113. TImageDataList = UInt32;
  114. [StructLayout(LayoutKind.Sequential)]
  115. TImageData = packed record
  116. Width: LongInt;
  117. Height: LongInt;
  118. Format: TImageFormat;
  119. Size: LongInt;
  120. Bits: IntPtr;
  121. Palette: TPalette32;
  122. end;
  123. [StructLayout(LayoutKind.Sequential)]
  124. TImageFormatInfo = packed record
  125. Format: TImageFormat;
  126. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 16)]
  127. Name: string;
  128. BytesPerPixel: Byte;
  129. PaletteEntries: LongInt;
  130. HasGrayChannel: Boolean;
  131. HasAlphaChannel: Boolean;
  132. IsFloatingPoint: Boolean;
  133. UsePixelFormat: Boolean;
  134. IsRBSwapped: Boolean;
  135. IsIndexed: Boolean;
  136. IsSpecial: Boolean;
  137. PixelFormat: IntPtr;
  138. GetPixelsSize: IntPtr;
  139. CheckDimensions: IntPtr;
  140. GetPixel32: IntPtr;
  141. GetPixelFP: IntPtr;
  142. SetPixel32: IntPtr;
  143. SetPixelFP: IntPtr;
  144. end;
  145. TResizeFilter = (
  146. rfNearest = 0,
  147. rfBilinear = 1,
  148. rfBicubic = 2);
  149. TColor24Rec = record;
  150. TColor32Rec = record;
  151. TColor48Rec = record;
  152. TColor64Rec = record;
  153. TColorFPRec = record;
  154. TColor24Rec = packed record
  155. public
  156. B, G, R: Byte;
  157. function SetColor(Color: TColor32): TColor24Rec; overload;
  158. function SetColor(R, G, B: Byte): TColor24Rec; overload;
  159. function SetColor(ColorRec: TColor24Rec): TColor24Rec; overload;
  160. function SetColor(ColorRec: TColor32Rec): TColor24Rec; overload;
  161. function SetColor(ColorRec: TColor48Rec): TColor24Rec; overload;
  162. function SetColor(ColorRec: TColor64Rec): TColor24Rec; overload;
  163. function SetColor(ColorRec: TColorFPRec): TColor24Rec; overload;
  164. function GetColor: TColor32;
  165. class operator Equal(const Left, Right: TColor24Rec): Boolean;
  166. class operator NotEqual(const Left, Right: TColor24Rec): Boolean;
  167. end;
  168. TColor32Rec = packed record
  169. public
  170. B, G, R, A: Byte;
  171. function SetColor(Color: TColor32): TColor32Rec; overload;
  172. function SetColor(A, R, G, B: Byte): TColor32Rec; overload;
  173. function SetColor(ColorRec: TColor24Rec): TColor32Rec; overload;
  174. function SetColor(ColorRec: TColor32Rec): TColor32Rec; overload;
  175. function SetColor(ColorRec: TColor48Rec): TColor32Rec; overload;
  176. function SetColor(ColorRec: TColor64Rec): TColor32Rec; overload;
  177. function SetColor(ColorRec: TColorFPRec): TColor32Rec; overload;
  178. function GetColor: TColor32;
  179. class operator Equal(const Left, Right: TColor32Rec): Boolean;
  180. class operator NotEqual(const Left, Right: TColor32Rec): Boolean;
  181. end;
  182. TColor48Rec = packed record
  183. public
  184. B, G, R: Word;
  185. function SetColor(Color: TColor64): TColor48Rec; overload;
  186. function SetColor(R, G, B: Word): TColor48Rec; overload;
  187. function SetColor(ColorRec: TColor24Rec): TColor48Rec; overload;
  188. function SetColor(ColorRec: TColor32Rec): TColor48Rec; overload;
  189. function SetColor(ColorRec: TColor48Rec): TColor48Rec; overload;
  190. function SetColor(ColorRec: TColor64Rec): TColor48Rec; overload;
  191. function SetColor(ColorRec: TColorFPRec): TColor48Rec; overload;
  192. function GetColor: TColor64;
  193. class operator Equal(const Left, Right: TColor48Rec): Boolean;
  194. class operator NotEqual(const Left, Right: TColor48Rec): Boolean;
  195. end;
  196. TColor64Rec = packed record
  197. public
  198. B, G, R, A: Word;
  199. function SetColor(Color: TColor64): TColor64Rec; overload;
  200. function SetColor(A, R, G, B: Word): TColor64Rec; overload;
  201. function SetColor(ColorRec: TColor24Rec): TColor64Rec; overload;
  202. function SetColor(ColorRec: TColor32Rec): TColor64Rec; overload;
  203. function SetColor(ColorRec: TColor48Rec): TColor64Rec; overload;
  204. function SetColor(ColorRec: TColor64Rec): TColor64Rec; overload;
  205. function SetColor(ColorRec: TColorFPRec): TColor64Rec; overload;
  206. function GetColor: TColor64;
  207. class operator Equal(const Left, Right: TColor64Rec): Boolean;
  208. class operator NotEqual(const Left, Right: TColor64Rec): Boolean;
  209. end;
  210. TColorFPRec = packed record
  211. public
  212. B, G, R, A: Single;
  213. function SetColor(Color: TColor64): TColorFPRec; overload;
  214. function SetColor(A, R, G, B: Single): TColorFPRec; overload;
  215. function SetColor(ColorRec: TColor24Rec): TColorFPRec; overload;
  216. function SetColor(ColorRec: TColor32Rec): TColorFPRec; overload;
  217. function SetColor(ColorRec: TColor48Rec): TColorFPRec; overload;
  218. function SetColor(ColorRec: TColor64Rec): TColorFPRec; overload;
  219. function SetColor(ColorRec: TColorFPRec): TColorFPRec; overload;
  220. function GetColor: TColor64;
  221. class operator Equal(const Left, Right: TColorFPRec): Boolean;
  222. class operator NotEqual(const Left, Right: TColorFPRec): Boolean;
  223. end;
  224. TDynImageDataArray = array of TImageData;
  225. const
  226. LibraryName = 'VampyreImaging.dll';
  227. { Low Level Imported Functions }
  228. { General Functions }
  229. [SuppressUnmanagedCodeSecurity, DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
  230. procedure ImGetVersion(var Major, Minor, Patch: LongInt); external;
  231. [SuppressUnmanagedCodeSecurity, DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
  232. procedure ImInitImage(var Image: TImageData); external;
  233. [SuppressUnmanagedCodeSecurity, DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
  234. function ImNewImage(Width, Height: LongInt; Format: TImageFormat;
  235. var Image: TImageData): Boolean; external;
  236. [SuppressUnmanagedCodeSecurity, DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
  237. function ImTestImage(const Image: TImageData): Boolean; external;
  238. [SuppressUnmanagedCodeSecurity, DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
  239. function ImFreeImage(var Image: TImageData): Boolean; external;
  240. [SuppressUnmanagedCodeSecurity, DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
  241. function ImDetermineFileFormat(const FileName: string;
  242. [out, MarshalAs(UnmanagedType.LPArray)] Ext: array of Char): Boolean; external;
  243. [SuppressUnmanagedCodeSecurity, DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
  244. function ImDetermineMemoryFormat(Data: array of Byte; Size: LongInt;
  245. [out, MarshalAs(UnmanagedType.LPArray)] Ext: array of Char): Boolean; external;
  246. [SuppressUnmanagedCodeSecurity, DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
  247. function ImIsFileFormatSupported(const FileName: string): Boolean; external;
  248. { Image List Functions }
  249. [SuppressUnmanagedCodeSecurity, DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
  250. function ImInitImageList(Size: LongInt; var ImageList: TImageDataList): Boolean; external;
  251. [SuppressUnmanagedCodeSecurity, DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
  252. function ImGetImageListSize(ImageList: TImageDataList): LongInt; external;
  253. [SuppressUnmanagedCodeSecurity, DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
  254. function ImGetImageListElement(ImageList: TImageDataList; Index: LongInt;
  255. var OutImage: TImageData): Boolean; external;
  256. [SuppressUnmanagedCodeSecurity, DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
  257. function ImSetImageListSize(ImageList: TImageDataList; NewSize: LongInt): Boolean; external;
  258. [SuppressUnmanagedCodeSecurity, DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
  259. function ImSetImageListElement(ImageList: TImageDataList; Index: LongInt;
  260. var InImage: TImageData): Boolean; external;
  261. [SuppressUnmanagedCodeSecurity, DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
  262. function ImTestImagesInList(ImageList: TImageDataList): Boolean; external;
  263. [SuppressUnmanagedCodeSecurity, DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
  264. function ImFreeImageList(var ImageList: TImageDataList): Boolean; external;
  265. { Loading Functions }
  266. [SuppressUnmanagedCodeSecurity, DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
  267. function ImLoadImageFromFile(const FileName: string; var Image: TImageData): Boolean; external;
  268. [SuppressUnmanagedCodeSecurity, DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
  269. function ImLoadImageFromMemory(const Data: array of Byte; Size: LongInt;
  270. var Image: TImageData): Boolean; external;
  271. [SuppressUnmanagedCodeSecurity, DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
  272. function ImLoadMultiImageFromFile(const FileName: string; var ImageList: TImageDataList): Boolean; external;
  273. [SuppressUnmanagedCodeSecurity, DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
  274. function ImLoadMultiImageFromMemory(const Data: array of Byte; Size: LongInt;
  275. var ImageList: TImageDataList): Boolean; external;
  276. { Saving Functions }
  277. [SuppressUnmanagedCodeSecurity, DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
  278. function ImSaveImageToFile(const FileName: string; const Image: TImageData): Boolean; external;
  279. [SuppressUnmanagedCodeSecurity, DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
  280. function ImSaveImageToMemory(const Ext: string; const Data: array of Byte; var Size: LongInt;
  281. const Image: TImageData): Boolean; external;
  282. [SuppressUnmanagedCodeSecurity, DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
  283. function ImSaveMultiImageToFile(const FileName: string; ImageList: TImageDataList): Boolean; external;
  284. [SuppressUnmanagedCodeSecurity, DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
  285. function ImSaveMultiImageToMemory(const Ext: string; const Data: array of Byte; var Size: LongInt;
  286. ImageList: TImageDataList): Boolean; external;
  287. { Manipulation Functions }
  288. [SuppressUnmanagedCodeSecurity, DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
  289. function ImCloneImage(const Image: TImageData; var Clone: TImageData): Boolean; external;
  290. [SuppressUnmanagedCodeSecurity, DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
  291. function ImConvertImage(var Image: TImageData; DestFormat: TImageFormat): Boolean; external;
  292. [SuppressUnmanagedCodeSecurity, DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
  293. function ImFlipImage(var Image: TImageData): Boolean; external;
  294. [SuppressUnmanagedCodeSecurity, DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
  295. function ImMirrorImage(var Image: TImageData): Boolean; external;
  296. [SuppressUnmanagedCodeSecurity, DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
  297. function ImResizeImage(var Image: TImageData; NewWidth, NewHeight: LongInt; Filter: TResizeFilter): Boolean; external;
  298. [SuppressUnmanagedCodeSecurity, DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
  299. function ImSwapChannels(var Image: TImageData; SrcChannel, DstChannel: LongInt): Boolean; external;
  300. [SuppressUnmanagedCodeSecurity, DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
  301. function ImReduceColors(var Image: TImageData; MaxColors: LongInt): Boolean; external;
  302. [SuppressUnmanagedCodeSecurity, DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
  303. function ImGenerateMipMaps(const Image: TImageData; Levels: LongInt;
  304. var MipMaps: TImageDataList): Boolean; external;
  305. [SuppressUnmanagedCodeSecurity, DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
  306. function ImMapImageToPalette(var Image: TImageData; Pal: TPalette32;
  307. Entries: LongInt): Boolean; external;
  308. [SuppressUnmanagedCodeSecurity, DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
  309. function ImSplitImage(var Image: TImageData; var Chunks: TImageDataList;
  310. ChunkWidth, ChunkHeight: LongInt; var XChunks, YChunks: LongInt;
  311. PreserveSize: Boolean; Fill: IntPtr): Boolean; external;
  312. [SuppressUnmanagedCodeSecurity, DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
  313. function ImMakePaletteForImages(var Images: TImageDataList; Pal: TPalette32;
  314. MaxColors: LongInt; ConvertImages: Boolean): Boolean; external;
  315. [SuppressUnmanagedCodeSecurity, DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
  316. function ImRotateImage(var Image: TImageData; Angle: LongInt): Boolean; external;
  317. { Drawing/Pixel functions }
  318. [SuppressUnmanagedCodeSecurity, DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
  319. function ImCopyRect(const SrcImage: TImageData; SrcX, SrcY, Width, Height: LongInt;
  320. var DstImage: TImageData; DstX, DstY: LongInt): Boolean; external;
  321. [SuppressUnmanagedCodeSecurity, DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
  322. function ImFillRect(var Image: TImageData; X, Y, Width, Height: LongInt;
  323. Fill: IntPtr): Boolean; external;
  324. [SuppressUnmanagedCodeSecurity, DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
  325. function ImReplaceColor(var Image: TImageData; X, Y, Width, Height: LongInt;
  326. OldPixel, NewPixel: IntPtr): Boolean; external;
  327. [SuppressUnmanagedCodeSecurity, DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
  328. function ImStretchRect(const SrcImage: TImageData; SrcX, SrcY, SrcWidth,
  329. SrcHeight: LongInt; var DstImage: TImageData; DstX, DstY, DstWidth,
  330. DstHeight: LongInt; Filter: TResizeFilter): Boolean; external;
  331. [SuppressUnmanagedCodeSecurity, DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
  332. procedure ImGetPixelDirect(const Image: TImageData; X, Y: LongInt; Pixel: IntPtr); external;
  333. [SuppressUnmanagedCodeSecurity, DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
  334. procedure ImSetPixelDirect(const Image: TImageData; X, Y: LongInt; Pixel: IntPtr); external;
  335. [SuppressUnmanagedCodeSecurity, DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
  336. function ImGetPixel32(const Image: TImageData; X, Y: LongInt): TColor32Rec; external;
  337. [SuppressUnmanagedCodeSecurity, DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
  338. procedure ImSetPixel32(const Image: TImageData; X, Y: LongInt; const Color: TColor32Rec); external;
  339. [SuppressUnmanagedCodeSecurity, DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
  340. function ImGetPixelFP(const Image: TImageData; X, Y: LongInt): TColorFPRec; external;
  341. [SuppressUnmanagedCodeSecurity, DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
  342. procedure ImSetPixelFP(const Image: TImageData; X, Y: LongInt; const Color: TColorFPRec); external;
  343. { Palette Functions }
  344. [SuppressUnmanagedCodeSecurity, DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
  345. function ImNewPalette(Entries: LongInt; var Pal: TPalette32): Boolean; external;
  346. [SuppressUnmanagedCodeSecurity, DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
  347. function ImFreePalette(var Pal: TPalette32): Boolean; external;
  348. [SuppressUnmanagedCodeSecurity, DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
  349. function ImCopyPalette(SrcPal, DstPal: TPalette32; SrcIdx, DstIdx, Count: LongInt): Boolean; external;
  350. [SuppressUnmanagedCodeSecurity, DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
  351. function ImFindColor(Pal: TPalette32; Entries: LongInt; Color: TColor32): LongInt; external;
  352. [SuppressUnmanagedCodeSecurity, DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
  353. function ImFillGrayscalePalette(Pal: TPalette32; Entries: LongInt): Boolean; external;
  354. [SuppressUnmanagedCodeSecurity, DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
  355. function ImFillCustomPalette(Pal: TPalette32; Entries: LongInt; RBits, GBits,
  356. BBits: Byte; Alpha: Byte): Boolean; external;
  357. [SuppressUnmanagedCodeSecurity, DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
  358. function ImSwapChannelsOfPalette(Pal: TPalette32; Entries, SrcChannel,
  359. DstChannel: LongInt): Boolean; external;
  360. { Options Functions }
  361. [SuppressUnmanagedCodeSecurity, DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
  362. function ImSetOption(OptionId, Value: LongInt): Boolean; external;
  363. [SuppressUnmanagedCodeSecurity, DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
  364. function ImGetOption(OptionId: LongInt): LongInt; external;
  365. [SuppressUnmanagedCodeSecurity, DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
  366. function ImPushOptions: Boolean; external;
  367. [SuppressUnmanagedCodeSecurity, DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
  368. function ImPopOptions: Boolean; external;
  369. { Image Format Functions }
  370. [SuppressUnmanagedCodeSecurity, DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
  371. function ImGetPixelBytes(Format: TImageFormat): LongInt; external;
  372. [SuppressUnmanagedCodeSecurity, DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
  373. function ImGetImageFormatInfo(Format: TImageFormat; var Info: TImageFormatInfo): Boolean; external;
  374. [SuppressUnmanagedCodeSecurity, DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
  375. function ImGetPixelsSize(Format: TImageFormat; Width, Height: LongInt): LongInt; external;
  376. type
  377. { Class which encapsulates all Imaging functions without Im prefix and
  378. with dotNET friendly parameter types. There are also some dotNET only
  379. members.}
  380. Imaging = class(TObject)
  381. private
  382. class function ListToArray(List: TImageDataList; var Arr: TDynImageDataArray): Boolean; static;
  383. class function ArrayToList(const Arr: TDynImageDataArray; var List: TImageDataList): Boolean; static;
  384. public
  385. { General Functions }
  386. class procedure GetVersion(var Major, Minor, Patch: LongInt); static;
  387. class procedure InitImage(var Image: TImageData); static;
  388. class function NewImage(Width, Height: LongInt; Format: TImageFormat; var Image: TImageData): Boolean; static;
  389. class function TestImage(const Image: TImageData): Boolean; static;
  390. class function FreeImage(var Image: TImageData): Boolean; static;
  391. class function FreeImagesInArray(var Images: TDynImageDataArray): Boolean; static;
  392. class function TestImagesInArray(const Images: TDynImageDataArray): Boolean; static;
  393. class function DetermineFileFormat(const FileName: string): string; static;
  394. class function DetermineMemoryFormat(const Data: array of Byte): string; static;
  395. class function IsFileFormatSupported(const FileName: string): Boolean; static;
  396. { Loading Functions }
  397. class function LoadImageFromFile(const FileName: string; var Image: TImageData): Boolean; static;
  398. class function LoadImageFromMemory(const Data: array of Byte; var Image: TImageData): Boolean; static;
  399. class function LoadMultiImageFromFile(const FileName: string; var Images: TDynImageDataArray): Boolean; static;
  400. class function LoadMultiImageFromMemory(const Data: array of Byte; var Images: TDynImageDataArray): Boolean; static;
  401. { Saving Functions }
  402. class function SaveImageToFile(const FileName: string; const Image: TImageData): Boolean; static;
  403. class function SaveImageToMemory(const Ext: string; Data: array of Byte; var Size: LongInt; const Image: TImageData): Boolean; static;
  404. class function SaveMultiImageToFile(const FileName: string; const Images: TDynImageDataArray): Boolean; static;
  405. class function SaveMultiImageToMemory(const Ext: string; Data: array of Byte; var Size: LongInt; const Images: TDynImageDataArray): Boolean; static;
  406. { Manipulation Functions }
  407. class function CloneImage(const Image: TImageData; var Clone: TImageData): Boolean; static;
  408. class function ConvertImage(var Image: TImageData; DestFormat: TImageFormat): Boolean; static;
  409. class function FlipImage(var Image: TImageData): Boolean; static;
  410. class function MirrorImage(var Image: TImageData): Boolean; static;
  411. class function ResizeImage(var Image: TImageData; NewWidth, NewHeight: LongInt; Filter: TResizeFilter): Boolean; static;
  412. class function SwapChannels(var Image: TImageData; SrcChannel, DstChannel: LongInt): Boolean; static;
  413. class function ReduceColors(var Image: TImageData; MaxColors: LongInt): Boolean; static;
  414. class function GenerateMipMaps(const Image: TImageData; Levels: LongInt; var MipMaps: TDynImageDataArray): Boolean; static;
  415. class function MapImageToPalette(var Image: TImageData; Pal: TPalette32; Entries: LongInt): Boolean; static;
  416. class function SplitImage(var Image: TImageData; var Chunks: TDynImageDataArray; ChunkWidth, ChunkHeight: LongInt; var XChunks, YChunks: LongInt; PreserveSize: Boolean; Fill: TObject): Boolean; static;
  417. class function MakePaletteForImages(var Images: TDynImageDataArray; Pal: TPalette32; MaxColors: LongInt; ConvertImages: Boolean): Boolean; static;
  418. class function RotateImage(var Image: TImageData; Angle: LongInt): Boolean; static;
  419. { Drawing/Pixel functions }
  420. class function CopyRect(const SrcImage: TImageData; SrcX, SrcY, Width, Height: LongInt; var DstImage: TImageData; DstX, DstY: LongInt): Boolean; static;
  421. class function FillRect(var Image: TImageData; X, Y, Width, Height: LongInt; Fill: TObject): Boolean; static;
  422. class function ReplaceColor(var Image: TImageData; X, Y, Width, Height: LongInt; OldPixel, NewPixel: TObject): Boolean; static;
  423. class function StretchRect(const SrcImage: TImageData; SrcX, SrcY, SrcWidth, SrcHeight: LongInt; var DstImage: TImageData; DstX, DstY, DstWidth, DstHeight: LongInt; Filter: TResizeFilter): Boolean; static;
  424. class procedure GetPixelDirect(const Image: TImageData; X, Y: LongInt; Pixel: TObject); static;
  425. class procedure SetPixelDirect(const Image: TImageData; X, Y: LongInt; Pixel: TObject); static;
  426. class function GetPixel32(const Image: TImageData; X, Y: LongInt): TColor32Rec; static;
  427. class procedure SetPixel32(const Image: TImageData; X, Y: LongInt; const Color: TColor32Rec); static;
  428. class function GetPixelFP(const Image: TImageData; X, Y: LongInt): TColorFPRec; static;
  429. class procedure SetPixelFP(const Image: TImageData; X, Y: LongInt; const Color: TColorFPRec); static;
  430. { Palette Functions }
  431. class function NewPalette(Entries: LongInt; var Pal: TPalette32): Boolean; static;
  432. class function FreePalette(var Pal: TPalette32): Boolean; static;
  433. class function CopyPalette(SrcPal, DstPal: TPalette32; SrcIdx, DstIdx, Count: LongInt): Boolean; static;
  434. class function FindColor(Pal: TPalette32; Entries: LongInt; Color: TColor32): LongInt; static;
  435. class function FillGrayscalePalette(Pal: TPalette32; Entries: LongInt): Boolean; static;
  436. class function FillCustomPalette(Pal: TPalette32; Entries: LongInt; RBits, GBits, BBits: Byte; Alpha: Byte): Boolean; static;
  437. class function SwapChannelsOfPalette(Pal: TPalette32; Entries, SrcChannel, DstChannel: LongInt): Boolean; static;
  438. { dotNET only}
  439. class function GetPaletteColor(Pal: TPalette32; Index: LongInt): TColor32; static;
  440. class procedure SetPaletteColor(Pal: TPalette32; Index: LongInt; Color: TColor32); static;
  441. { Options Functions }
  442. class function SetOption(OptionId, Value: LongInt): Boolean; static;
  443. class function GetOption(OptionId: LongInt): LongInt; static;
  444. class function PushOptions: Boolean; static;
  445. class function PopOptions: Boolean; static;
  446. { Image Format Functions }
  447. class function GetPixelBytes(Format: TImageFormat): LongInt; static;
  448. class function GetImageFormatInfo(Format: TImageFormat; var Info: TImageFormatInfo): Boolean; static;
  449. class function GetPixelsSize(Format: TImageFormat; Width, Height: LongInt): LongInt; static;
  450. end;
  451. implementation
  452. { Imaging private methods }
  453. class function Imaging.ListToArray(List: TImageDataList; var Arr: TDynImageDataArray): Boolean;
  454. var
  455. Img: TImageData;
  456. I, Size: LongInt;
  457. begin
  458. Result := True;
  459. Size := ImGetImageListSize(List);
  460. FreeImagesInArray(Arr);
  461. SetLength(Arr, Size);
  462. for I := 0 to Size - 1 do
  463. begin
  464. Result := Result and ImGetImageListElement(List, I, Img);
  465. Arr[I] := Img;
  466. if not Result then Break;
  467. end;
  468. end;
  469. class function Imaging.ArrayToList(const Arr: TDynImageDataArray; var List: TImageDataList): Boolean;
  470. var
  471. Img: TImageData;
  472. I, Size: LongInt;
  473. begin
  474. Size := Length(Arr);
  475. Result := ImInitImageList(Size, List);
  476. for I := 0 to Size - 1 do
  477. begin
  478. Img := Arr[I];
  479. Result := Result and ImSetImageListElement(List, I, Img);
  480. if not Result then Break;
  481. end;
  482. end;
  483. { Imaging public methods }
  484. class procedure Imaging.GetVersion(var Major, Minor, Patch: LongInt);
  485. begin
  486. ImGetVersion(Major, Minor, Patch);
  487. end;
  488. class procedure Imaging.InitImage(var Image: TImageData);
  489. begin
  490. ImInitImage(Image);
  491. end;
  492. class function Imaging.IsFileFormatSupported(const FileName: string): Boolean;
  493. begin
  494. Result := IsFileFormatSupported(FileName);
  495. end;
  496. class function Imaging.NewImage(Width, Height: LongInt; Format: TImageFormat; var Image: TImageData): Boolean;
  497. begin
  498. Result := ImNewImage(Width, Height, Format, Image);
  499. end;
  500. class function Imaging.TestImage(const Image: TImageData): Boolean;
  501. begin
  502. Result := ImTestImage(Image);
  503. end;
  504. class function Imaging.FreeImage(var Image: TImageData): Boolean;
  505. begin
  506. Result := ImFreeImage(Image);
  507. end;
  508. class function Imaging.FreeImagesInArray(var Images: TDynImageDataArray): Boolean;
  509. var
  510. I: LongInt;
  511. begin
  512. Result := True;
  513. for I := 0 to Length(Images) - 1 do
  514. Result := Result and ImFreeImage(Images[I]);
  515. end;
  516. class function Imaging.TestImagesInArray(const Images: TDynImageDataArray): Boolean;
  517. var
  518. I: LongInt;
  519. begin
  520. Result := True;
  521. for I := 0 to Length(Images) - 1 do
  522. begin
  523. Result := Result and ImTestImage(Images[I]);
  524. if not Result then Break;
  525. end;
  526. end;
  527. const
  528. ExtLen = 16;
  529. class function Imaging.DetermineFileFormat(const FileName: string): string;
  530. var
  531. Arr: array of Char;
  532. I: LongInt;
  533. begin
  534. SetLength(Arr, ExtLen);
  535. if ImDetermineFileFormat(FileName, Arr) then
  536. begin
  537. SetLength(Result, ExtLen);
  538. for I := 1 to ExtLen do
  539. Result[I] := Arr[I - 1];
  540. SetLength(Result, Result.IndexOf(#0));
  541. end
  542. else
  543. Result := '';
  544. SetLength(Arr, 0);
  545. end;
  546. class function Imaging.DetermineMemoryFormat(const Data: array of Byte): string;
  547. var
  548. Arr: array of Char;
  549. I: LongInt;
  550. begin
  551. SetLength(Arr, 16);
  552. if ImDetermineMemoryFormat(Data, Length(Data), Arr) then
  553. begin
  554. SetLength(Result, ExtLen);
  555. for I := 1 to ExtLen do
  556. Result[I] := Arr[I - 1];
  557. SetLength(Result, Result.IndexOf(#0));
  558. end
  559. else
  560. Result := '';
  561. SetLength(Arr, 0);
  562. end;
  563. class function Imaging.LoadImageFromFile(const FileName: string; var Image: TImageData): Boolean;
  564. begin
  565. Result := ImLoadImageFromFile(FileName, Image);
  566. end;
  567. class function Imaging.LoadImageFromMemory(const Data: array of Byte; var Image: TImageData): Boolean;
  568. begin
  569. Result := ImLoadImageFromMemory(Data, Length(Data), Image);
  570. end;
  571. class function Imaging.LoadMultiImageFromFile(const FileName: string; var Images: TDynImageDataArray): Boolean;
  572. var
  573. List: TImageDataList;
  574. begin
  575. Result := ImLoadMultiImageFromFile(FileName, List);
  576. if Result then
  577. begin
  578. FreeImagesInArray(Images);
  579. Result := ListToArray(List, Images);
  580. ImFreeImageList(List);
  581. end;
  582. end;
  583. class function Imaging.LoadMultiImageFromMemory(const Data: array of Byte; var Images: TDynImageDataArray): Boolean;
  584. var
  585. List: TImageDataList;
  586. begin
  587. Result := ImLoadMultiImageFromMemory(Data, Length(Data), List);
  588. if Result then
  589. begin
  590. FreeImagesInArray(Images);
  591. Result := ListToArray(List, Images);
  592. ImFreeImageList(List);
  593. end;
  594. end;
  595. class function Imaging.SaveImageToFile(const FileName: string; const Image: TImageData): Boolean;
  596. begin
  597. Result := ImSaveImageToFile(FileName, Image);
  598. end;
  599. class function Imaging.SaveImageToMemory(const Ext: string; Data: array of Byte; var Size: LongInt; const Image: TImageData): Boolean;
  600. begin
  601. Size := Length(Data);
  602. Result := ImSaveImageToMemory(Ext, Data, Size, Image);
  603. end;
  604. class function Imaging.SaveMultiImageToFile(const FileName: string; const Images: TDynImageDataArray): Boolean;
  605. var
  606. List: TImageDataList;
  607. begin
  608. Result := ArrayToList(Images, List);
  609. if Result then
  610. begin
  611. Result := ImSaveMultiImageToFile(FileName, List);
  612. ImFreeImageList(List);
  613. end;
  614. end;
  615. class function Imaging.SaveMultiImageToMemory(const Ext: string; Data: array of Byte; var Size: LongInt; const Images: TDynImageDataArray): Boolean;
  616. var
  617. List: TImageDataList;
  618. begin
  619. Result := ArrayToList(Images, List);
  620. if Result then
  621. begin
  622. Size := Length(Data);
  623. Result := ImSaveMultiImageToMemory(Ext, Data, Size, List);
  624. ImFreeImageList(List);
  625. end;
  626. end;
  627. class function Imaging.CloneImage(const Image: TImageData; var Clone: TImageData): Boolean;
  628. begin
  629. Result := ImCloneImage(Image, Clone);
  630. end;
  631. class function Imaging.ConvertImage(var Image: TImageData; DestFormat: TImageFormat): Boolean;
  632. begin
  633. Result := ImConvertImage(Image, DestFormat);
  634. end;
  635. class function Imaging.FlipImage(var Image: TImageData): Boolean;
  636. begin
  637. Result := ImFlipImage(Image);
  638. end;
  639. class function Imaging.MirrorImage(var Image: TImageData): Boolean;
  640. begin
  641. Result := ImMirrorImage(Image);
  642. end;
  643. class function Imaging.ResizeImage(var Image: TImageData; NewWidth, NewHeight: LongInt; Filter: TResizeFilter): Boolean;
  644. begin
  645. Result := ImResizeImage(Image, NewWidth, NewHeight, Filter);
  646. end;
  647. class function Imaging.SwapChannels(var Image: TImageData; SrcChannel, DstChannel: LongInt): Boolean;
  648. begin
  649. Result := ImSwapChannels(Image, SrcChannel, DstChannel);
  650. end;
  651. class function Imaging.ReduceColors(var Image: TImageData; MaxColors: LongInt): Boolean;
  652. begin
  653. Result := ImReduceColors(Image, MaxColors);
  654. end;
  655. class function Imaging.GenerateMipMaps(const Image: TImageData; Levels: LongInt; var MipMaps: TDynImageDataArray): Boolean;
  656. var
  657. List: TImageDataList;
  658. begin
  659. Result := ImGenerateMipMaps(Image, Levels, List);
  660. if Result then
  661. begin
  662. FreeImagesInArray(MipMaps);
  663. Result := ListToArray(List, MipMaps);
  664. ImFreeImageList(List);
  665. end;
  666. end;
  667. class function Imaging.MapImageToPalette(var Image: TImageData; Pal: TPalette32; Entries: LongInt): Boolean;
  668. begin
  669. Result := ImMapImageToPalette(Image, Pal, Entries);
  670. end;
  671. class function Imaging.SplitImage(var Image: TImageData; var Chunks: TDynImageDataArray; ChunkWidth, ChunkHeight: LongInt; var XChunks, YChunks: LongInt; PreserveSize: Boolean; Fill: TObject): Boolean;
  672. var
  673. Ptr: IntPtr;
  674. List: TImageDataList;
  675. begin
  676. Ptr := Marshal.AllocHGlobal(Marshal.SizeOf(Fill));
  677. Marshal.StructureToPtr(Fill, Ptr, False);
  678. Result := ImSplitImage(Image, List, ChunkWidth, ChunkHeight, XChunks, YChunks,
  679. PreserveSize, Ptr);
  680. if Result then
  681. begin
  682. FreeImagesInArray(Chunks);
  683. Result := ListToArray(List, Chunks);
  684. ImFreeImageList(List);
  685. end;
  686. Marshal.FreeHGlobal(Ptr);
  687. end;
  688. class function Imaging.MakePaletteForImages(var Images: TDynImageDataArray; Pal: TPalette32; MaxColors: LongInt; ConvertImages: Boolean): Boolean;
  689. var
  690. List: TImageDataList;
  691. begin
  692. Result := ArrayToList(Images, List);
  693. if Result then
  694. begin
  695. Result := ImMakePaletteForImages(List, Pal, MaxColors, ConvertImages);
  696. Result := Result and ListToArray(List, Images);
  697. ImFreeImageList(List);
  698. end;
  699. end;
  700. class function Imaging.RotateImage(var Image: TImageData; Angle: LongInt): Boolean;
  701. begin
  702. Result := ImRotateImage(Image, Angle);
  703. end;
  704. class function Imaging.CopyRect(const SrcImage: TImageData; SrcX, SrcY, Width, Height: LongInt; var DstImage: TImageData; DstX, DstY: LongInt): Boolean;
  705. begin
  706. Result := ImCopyRect(SrcImage, SrcX, SrcY, Width, Height, DstImage, DstX, DstY);
  707. end;
  708. class function Imaging.FillRect(var Image: TImageData; X, Y, Width, Height: LongInt; Fill: TObject): Boolean;
  709. var
  710. Ptr: IntPtr;
  711. begin
  712. Ptr := Marshal.AllocHGlobal(Marshal.SizeOf(Fill));
  713. Marshal.StructureToPtr(Fill, Ptr, False);
  714. Result := ImFillRect(Image, X, Y, Width, Height, Ptr);
  715. Marshal.FreeHGlobal(Ptr);
  716. end;
  717. class function Imaging.ReplaceColor(var Image: TImageData; X, Y, Width,
  718. Height: LongInt; OldPixel, NewPixel: TObject): Boolean;
  719. var
  720. OldPtr, NewPtr: IntPtr;
  721. begin
  722. OldPtr := Marshal.AllocHGlobal(Marshal.SizeOf(OldPixel));
  723. Marshal.StructureToPtr(OldPixel, OldPtr, False);
  724. NewPtr := Marshal.AllocHGlobal(Marshal.SizeOf(NewPixel));
  725. Marshal.StructureToPtr(NewPixel, NewPtr, False);
  726. Result := ReplaceColor(Image, X, Y, Width, Height, OldPtr, NewPtr);
  727. Marshal.FreeHGlobal(OldPtr);
  728. Marshal.FreeHGlobal(NewPtr);
  729. end;
  730. class function Imaging.StretchRect(const SrcImage: TImageData; SrcX, SrcY,
  731. SrcWidth, SrcHeight: Integer; var DstImage: TImageData; DstX, DstY, DstWidth,
  732. DstHeight: Integer; Filter: TResizeFilter): Boolean;
  733. begin
  734. Result := ImStretchRect(SrcImage, SrcX, SrcY, SrcWidth, SrcHeight,
  735. DstImage, DstX, DstY, DstWidth, DstHeight, Filter);
  736. end;
  737. class procedure Imaging.GetPixelDirect(const Image: TImageData; X, Y: Integer;
  738. Pixel: TObject);
  739. var
  740. Ptr: IntPtr;
  741. begin
  742. Ptr := Marshal.AllocHGlobal(Marshal.SizeOf(Pixel));
  743. ImGetPixelDirect(Image, X, Y, Ptr);
  744. Marshal.PtrToStructure(Ptr, Pixel);
  745. Marshal.FreeHGlobal(Ptr);
  746. end;
  747. class function Imaging.GetPixel32(const Image: TImageData; X,
  748. Y: Integer): TColor32Rec;
  749. begin
  750. Result := ImGetPixel32(Image, X, Y);
  751. end;
  752. class procedure Imaging.SetPixel32(const Image: TImageData; X, Y: Integer;
  753. const Color: TColor32Rec);
  754. begin
  755. ImSetPixel32(Image, X, Y, Color);
  756. end;
  757. class function Imaging.GetPixelFP(const Image: TImageData; X,
  758. Y: Integer): TColorFPRec;
  759. begin
  760. Result := ImGetPixelFP(Image, X, Y);
  761. end;
  762. class procedure Imaging.SetPixelDirect(const Image: TImageData; X, Y: Integer;
  763. Pixel: TObject);
  764. var
  765. Ptr: IntPtr;
  766. begin
  767. Ptr := Marshal.AllocHGlobal(Marshal.SizeOf(Pixel));
  768. Marshal.StructureToPtr(Pixel, Ptr, False);
  769. ImSetPixelDirect(Image, X, Y, Ptr);
  770. Marshal.FreeHGlobal(Ptr);
  771. end;
  772. class procedure Imaging.SetPixelFP(const Image: TImageData; X, Y: Integer;
  773. const Color: TColorFPRec);
  774. begin
  775. ImSetPixelFP(Image, X, Y, Color);
  776. end;
  777. class function Imaging.NewPalette(Entries: LongInt; var Pal: TPalette32): Boolean;
  778. begin
  779. Result := ImNewPalette(Entries, Pal);
  780. end;
  781. class function Imaging.FreePalette(var Pal: TPalette32): Boolean;
  782. begin
  783. Result := ImFreePalette(Pal);
  784. end;
  785. class function Imaging.CopyPalette(SrcPal, DstPal: TPalette32; SrcIdx, DstIdx, Count: LongInt): Boolean;
  786. begin
  787. Result := ImCopyPalette(SrcPal, DstPal, SrcIdx, DstIdx, Count);
  788. end;
  789. class function Imaging.FindColor(Pal: TPalette32; Entries: LongInt; Color: TColor32): LongInt;
  790. begin
  791. Result := ImFindColor(Pal, Entries, Color);
  792. end;
  793. class function Imaging.FillGrayscalePalette(Pal: TPalette32; Entries: LongInt): Boolean;
  794. begin
  795. Result := ImFillGrayscalePalette(Pal, Entries);
  796. end;
  797. class function Imaging.FillCustomPalette(Pal: TPalette32; Entries: LongInt; RBits, GBits, BBits: Byte; Alpha: Byte): Boolean;
  798. begin
  799. Result := ImFillCustomPalette(Pal, Entries, RBits, GBits, BBits, Alpha);
  800. end;
  801. class function Imaging.SwapChannelsOfPalette(Pal: TPalette32; Entries, SrcChannel, DstChannel: LongInt): Boolean;
  802. begin
  803. Result := ImSwapChannelsOfPalette(Pal, Entries, SrcChannel, DstChannel);
  804. end;
  805. class function Imaging.GetPaletteColor(Pal: TPalette32; Index: LongInt): TColor32;
  806. begin
  807. Result := Marshal.ReadInt32(Pal, Index * SizeOf(TColor32));
  808. end;
  809. class procedure Imaging.SetPaletteColor(Pal: TPalette32; Index: LongInt; Color: TColor32);
  810. begin
  811. Marshal.WriteInt32(Pal, Index * SizeOf(TColor32), Color);
  812. end;
  813. class function Imaging.SetOption(OptionId, Value: LongInt): Boolean;
  814. begin
  815. Result := ImSetOption(OptionId, Value);
  816. end;
  817. class function Imaging.GetOption(OptionId: LongInt): LongInt;
  818. begin
  819. Result := ImGetOption(OptionId);
  820. end;
  821. class function Imaging.PushOptions: Boolean;
  822. begin
  823. Result := ImPushOptions;
  824. end;
  825. class function Imaging.PopOptions: Boolean;
  826. begin
  827. Result := ImPopOptions;
  828. end;
  829. class function Imaging.GetPixelBytes(Format: TImageFormat): LongInt;
  830. begin
  831. Result := ImGetPixelBytes(Format);
  832. end;
  833. class function Imaging.GetImageFormatInfo(Format: TImageFormat; var Info: TImageFormatInfo): Boolean;
  834. begin
  835. Result := ImGetImageFormatInfo(Format, Info);
  836. end;
  837. class function Imaging.GetPixelsSize(Format: TImageFormat; Width, Height: LongInt): LongInt;
  838. begin
  839. Result := ImGetPixelsSize(Format, Width, Height);
  840. end;
  841. { Color Records implementations }
  842. { TColor24Rec }
  843. function TColor24Rec.SetColor(Color: TColor32): TColor24Rec;
  844. begin
  845. R := (Color shr 16) and $FF;
  846. G := (Color shr 8) and $FF;
  847. B := Color and $FF;
  848. Result := Self;
  849. end;
  850. function TColor24Rec.GetColor: TColor32;
  851. begin
  852. Result := ($FF shl 24) or (R shl 16) or (G shl 8) or B;
  853. end;
  854. function TColor24Rec.SetColor(R, G, B: Byte): TColor24Rec;
  855. begin
  856. Self.R := R;
  857. Self.G := G;
  858. Self.B := B;
  859. Result := Self;
  860. end;
  861. function TColor24Rec.SetColor(ColorRec: TColorFPRec): TColor24Rec;
  862. begin
  863. Self.R := Math.Max(0, Math.Min(255, Trunc(ColorRec.R * 255)));
  864. Self.G := Math.Max(0, Math.Min(255, Trunc(ColorRec.G * 255)));
  865. Self.B := Math.Max(0, Math.Min(255, Trunc(ColorRec.B * 255)));
  866. Result := Self;
  867. end;
  868. function TColor24Rec.SetColor(ColorRec: TColor64Rec): TColor24Rec;
  869. begin
  870. Self.R := ColorRec.R shr 8;
  871. Self.G := ColorRec.G shr 8;
  872. Self.B := ColorRec.B shr 8;
  873. Result := Self;
  874. end;
  875. function TColor24Rec.SetColor(ColorRec: TColor48Rec): TColor24Rec;
  876. begin
  877. Self.R := ColorRec.R shr 8;
  878. Self.G := ColorRec.G shr 8;
  879. Self.B := ColorRec.B shr 8;
  880. Result := Self;
  881. end;
  882. function TColor24Rec.SetColor(ColorRec: TColor32Rec): TColor24Rec;
  883. begin
  884. Self.R := ColorRec.R;
  885. Self.G := ColorRec.G;
  886. Self.B := ColorRec.B;
  887. Result := Self;
  888. end;
  889. function TColor24Rec.SetColor(ColorRec: TColor24Rec): TColor24Rec;
  890. begin
  891. Self.R := ColorRec.R;
  892. Self.G := ColorRec.G;
  893. Self.B := ColorRec.B;
  894. Result := Self;
  895. end;
  896. class operator TColor24Rec.Equal(const Left, Right: TColor24Rec): Boolean;
  897. begin
  898. Result := (Left.R = Right.R) and (Left.G = Right.G) and (Left.B = Right.B);
  899. end;
  900. class operator TColor24Rec.NotEqual(const Left, Right: TColor24Rec): Boolean;
  901. begin
  902. Result := not TColor24Rec.Equals(Left, Right);
  903. end;
  904. { TColor32Rec }
  905. function TColor32Rec.SetColor(Color: TColor32): TColor32Rec;
  906. begin
  907. A := Color shr 24;
  908. R := (Color shr 16) and $FF;
  909. G := (Color shr 8) and $FF;
  910. B := Color and $FF;
  911. Result := Self;
  912. end;
  913. function TColor32Rec.GetColor: TColor32;
  914. begin
  915. Result := (A shl 24) or (R shl 16) or (G shl 8) or B;
  916. end;
  917. function TColor32Rec.SetColor(A, R, G, B: Byte): TColor32Rec;
  918. begin
  919. Self.A := A;
  920. Self.R := R;
  921. Self.G := G;
  922. Self.B := B;
  923. Result := Self;
  924. end;
  925. function TColor32Rec.SetColor(ColorRec: TColorFPRec): TColor32Rec;
  926. begin
  927. Self.A := Math.Max(0, Math.Min($FF, Trunc(ColorRec.A * $FF)));
  928. Self.R := Math.Max(0, Math.Min($FF, Trunc(ColorRec.R * $FF)));
  929. Self.G := Math.Max(0, Math.Min($FF, Trunc(ColorRec.G * $FF)));
  930. Self.B := Math.Max(0, Math.Min($FF, Trunc(ColorRec.B * $FF)));
  931. Result := Self;
  932. end;
  933. function TColor32Rec.SetColor(ColorRec: TColor64Rec): TColor32Rec;
  934. begin
  935. Self.A := ColorRec.A shr 8;
  936. Self.R := ColorRec.R shr 8;
  937. Self.G := ColorRec.G shr 8;
  938. Self.B := ColorRec.B shr 8;
  939. Result := Self;
  940. end;
  941. function TColor32Rec.SetColor(ColorRec: TColor48Rec): TColor32Rec;
  942. begin
  943. Self.A := $FF;
  944. Self.R := ColorRec.R shr 8;
  945. Self.G := ColorRec.G shr 8;
  946. Self.B := ColorRec.B shr 8;
  947. Result := Self;
  948. end;
  949. function TColor32Rec.SetColor(ColorRec: TColor32Rec): TColor32Rec;
  950. begin
  951. Self.A := ColorRec.A;
  952. Self.R := ColorRec.R;
  953. Self.G := ColorRec.G;
  954. Self.B := ColorRec.B;
  955. Result := Self;
  956. end;
  957. function TColor32Rec.SetColor(ColorRec: TColor24Rec): TColor32Rec;
  958. begin
  959. Self.A := $FF;
  960. Self.R := ColorRec.R;
  961. Self.G := ColorRec.G;
  962. Self.B := ColorRec.B;
  963. Result := Self;
  964. end;
  965. class operator TColor32Rec.Equal(const Left, Right: TColor32Rec): Boolean;
  966. begin
  967. Result := (Left.A = Right.A) and (Left.R = Right.R) and (Left.G = Right.G) and
  968. (Left.B = Right.B);
  969. end;
  970. class operator TColor32Rec.NotEqual(const Left, Right: TColor32Rec): Boolean;
  971. begin
  972. Result := not TColor32Rec.Equals(Left, Right);
  973. end;
  974. { TColor48Rec }
  975. function TColor48Rec.SetColor(Color: TColor64): TColor48Rec;
  976. begin
  977. R := (Color shr 32) and $FFFF;
  978. G := (Color shr 16) and $FFFF;
  979. B := Color and $FFFF;
  980. Result := Self;
  981. end;
  982. function TColor48Rec.GetColor: TColor64;
  983. begin
  984. Result := (UInt64($FFFF) shl 48) or (UInt64(R) shl 32) or (G shl 16) or B;
  985. end;
  986. function TColor48Rec.SetColor(R, G, B: Word): TColor48Rec;
  987. begin
  988. Self.R := R;
  989. Self.G := G;
  990. Self.B := B;
  991. Result := Self;
  992. end;
  993. function TColor48Rec.SetColor(ColorRec: TColorFPRec): TColor48Rec;
  994. begin
  995. Self.R := Math.Max(0, Math.Min($FFFF, Trunc(ColorRec.R * $FFFF)));
  996. Self.G := Math.Max(0, Math.Min($FFFF, Trunc(ColorRec.G * $FFFF)));
  997. Self.B := Math.Max(0, Math.Min($FFFF, Trunc(ColorRec.B * $FFFF)));
  998. Result := Self;
  999. end;
  1000. function TColor48Rec.SetColor(ColorRec: TColor64Rec): TColor48Rec;
  1001. begin
  1002. Self.R := ColorRec.R;
  1003. Self.G := ColorRec.G;
  1004. Self.B := ColorRec.B;
  1005. Result := Self;
  1006. end;
  1007. function TColor48Rec.SetColor(ColorRec: TColor48Rec): TColor48Rec;
  1008. begin
  1009. Self.R := ColorRec.R;
  1010. Self.G := ColorRec.G;
  1011. Self.B := ColorRec.B;
  1012. Result := Self;
  1013. end;
  1014. function TColor48Rec.SetColor(ColorRec: TColor32Rec): TColor48Rec;
  1015. begin
  1016. Self.R := ColorRec.R shl 8;
  1017. Self.G := ColorRec.G shl 8;
  1018. Self.B := ColorRec.B shl 8;
  1019. Result := Self;
  1020. end;
  1021. function TColor48Rec.SetColor(ColorRec: TColor24Rec): TColor48Rec;
  1022. begin
  1023. Self.R := ColorRec.R shl 8;
  1024. Self.G := ColorRec.G shl 8;
  1025. Self.B := ColorRec.B shl 8;
  1026. Result := Self;
  1027. end;
  1028. class operator TColor48Rec.Equal(const Left, Right: TColor48Rec): Boolean;
  1029. begin
  1030. Result := (Left.R = Right.R) and (Left.G = Right.G) and (Left.B = Right.B);
  1031. end;
  1032. class operator TColor48Rec.NotEqual(const Left, Right: TColor48Rec): Boolean;
  1033. begin
  1034. Result := not TColor48Rec.Equals(Left, Right);
  1035. end;
  1036. { TColor64Rec }
  1037. function TColor64Rec.SetColor(Color: TColor64): TColor64Rec;
  1038. begin
  1039. A := Color shr 48;
  1040. R := (Color shr 32) and $FFFF;
  1041. G := (Color shr 16) and $FFFF;
  1042. B := Color and $FFFF;
  1043. Result := Self;
  1044. end;
  1045. function TColor64Rec.GetColor: TColor64;
  1046. begin
  1047. Result := (UInt64(A) shl 48) or (UInt64(R) shl 32) or (G shl 16) or B;
  1048. end;
  1049. function TColor64Rec.SetColor(A, R, G, B: Word): TColor64Rec;
  1050. begin
  1051. Self.A := A;
  1052. Self.R := R;
  1053. Self.G := G;
  1054. Self.B := B;
  1055. Result := Self;
  1056. end;
  1057. function TColor64Rec.SetColor(ColorRec: TColorFPRec): TColor64Rec;
  1058. begin
  1059. Self.A := Math.Max(0, Math.Min($FFFF, Trunc(ColorRec.A * $FFFF)));
  1060. Self.R := Math.Max(0, Math.Min($FFFF, Trunc(ColorRec.R * $FFFF)));
  1061. Self.G := Math.Max(0, Math.Min($FFFF, Trunc(ColorRec.G * $FFFF)));
  1062. Self.B := Math.Max(0, Math.Min($FFFF, Trunc(ColorRec.B * $FFFF)));
  1063. Result := Self;
  1064. end;
  1065. function TColor64Rec.SetColor(ColorRec: TColor64Rec): TColor64Rec;
  1066. begin
  1067. Self.A := ColorRec.A;
  1068. Self.R := ColorRec.R;
  1069. Self.G := ColorRec.G;
  1070. Self.B := ColorRec.B;
  1071. Result := Self;
  1072. end;
  1073. function TColor64Rec.SetColor(ColorRec: TColor48Rec): TColor64Rec;
  1074. begin
  1075. Self.A := $FFFF;
  1076. Self.R := ColorRec.R;
  1077. Self.G := ColorRec.G;
  1078. Self.B := ColorRec.B;
  1079. Result := Self;
  1080. end;
  1081. function TColor64Rec.SetColor(ColorRec: TColor32Rec): TColor64Rec;
  1082. begin
  1083. Self.A := ColorRec.A shl 8;
  1084. Self.R := ColorRec.R shl 8;
  1085. Self.G := ColorRec.G shl 8;
  1086. Self.B := ColorRec.B shl 8;
  1087. Result := Self;
  1088. end;
  1089. function TColor64Rec.SetColor(ColorRec: TColor24Rec): TColor64Rec;
  1090. begin
  1091. Self.A := $FFFF;
  1092. Self.R := ColorRec.R shl 8;
  1093. Self.G := ColorRec.G shl 8;
  1094. Self.B := ColorRec.B shl 8;
  1095. Result := Self;
  1096. end;
  1097. class operator TColor64Rec.Equal(const Left, Right: TColor64Rec): Boolean;
  1098. begin
  1099. Result := (Left.A = Right.A) and (Left.R = Right.R) and (Left.G = Right.G) and
  1100. (Left.B = Right.B);
  1101. end;
  1102. class operator TColor64Rec.NotEqual(const Left, Right: TColor64Rec): Boolean;
  1103. begin
  1104. Result := not TColor64Rec.Equals(Left, Right);
  1105. end;
  1106. { TColorFRec }
  1107. function TColorFPRec.SetColor(Color: TColor64): TColorFPRec;
  1108. var
  1109. C64: TColor64Rec;
  1110. begin
  1111. C64.SetColor(Color);
  1112. Self.SetColor(C64);
  1113. Result := Self;
  1114. end;
  1115. function TColorFPRec.GetColor: TColor64;
  1116. var
  1117. C64: TColor64Rec;
  1118. begin
  1119. C64.SetColor(Self);
  1120. Result := C64.GetColor;
  1121. end;
  1122. function TColorFPRec.SetColor(A, R, G, B: Single): TColorFPRec;
  1123. begin
  1124. Self.A := A;
  1125. Self.R := R;
  1126. Self.G := G;
  1127. Self.B := B;
  1128. Result := Self;
  1129. end;
  1130. function TColorFPRec.SetColor(ColorRec: TColorFPRec): TColorFPRec;
  1131. begin
  1132. Self.A := ColorRec.A;
  1133. Self.R := ColorRec.R;
  1134. Self.G := ColorRec.G;
  1135. Self.B := ColorRec.B;
  1136. Result := Self;
  1137. end;
  1138. function TColorFPRec.SetColor(ColorRec: TColor64Rec): TColorFPRec;
  1139. begin
  1140. Self.A := ColorRec.A / $FFFF;
  1141. Self.R := ColorRec.R / $FFFF;
  1142. Self.G := ColorRec.G / $FFFF;
  1143. Self.B := ColorRec.B / $FFFF;
  1144. Result := Self;
  1145. end;
  1146. function TColorFPRec.SetColor(ColorRec: TColor48Rec): TColorFPRec;
  1147. begin
  1148. Self.A := 1.0;
  1149. Self.R := ColorRec.R / $FFFF;
  1150. Self.G := ColorRec.G / $FFFF;
  1151. Self.B := ColorRec.B / $FFFF;
  1152. Result := Self;
  1153. end;
  1154. function TColorFPRec.SetColor(ColorRec: TColor32Rec): TColorFPRec;
  1155. begin
  1156. Self.A := ColorRec.A / $FF;
  1157. Self.R := ColorRec.R / $FF;
  1158. Self.G := ColorRec.G / $FF;
  1159. Self.B := ColorRec.B / $FF;
  1160. Result := Self;
  1161. end;
  1162. function TColorFPRec.SetColor(ColorRec: TColor24Rec): TColorFPRec;
  1163. begin
  1164. Self.A := 1.0;
  1165. Self.R := ColorRec.R / $FF;
  1166. Self.G := ColorRec.G / $FF;
  1167. Self.B := ColorRec.B / $FF;
  1168. Result := Self;
  1169. end;
  1170. class operator TColorFPRec.Equal(const Left, Right: TColorFPRec): Boolean;
  1171. begin
  1172. Result := (Left.A = Right.A) and (Left.R = Right.R) and (Left.G = Right.G) and
  1173. (Left.B = Right.B);
  1174. end;
  1175. class operator TColorFPRec.NotEqual(const Left, Right: TColorFPRec): Boolean;
  1176. begin
  1177. Result := not TColorFPRec.Equals(Left, Right);
  1178. end;
  1179. {
  1180. Changes/Bug Fixes:
  1181. -- TODOS ----------------------------------------------------
  1182. - add typecast operators to color records rather than SetColor methods
  1183. - add create System.Drawing.Bitmap from TImageData function
  1184. -- 0.19 -----------------------------------------------------
  1185. - updated to DLL new version
  1186. -- 0.17 -----------------------------------------------------
  1187. - added new low level functions and their equivalents in Imaging class,
  1188. some old function headers updated
  1189. -- 0.15 -----------------------------------------------------
  1190. - changed headers of some functions because of changes in DLL
  1191. mainly related to TImageDataList parameters. Few other
  1192. "update" changes.
  1193. -- 0.13 -----------------------------------------------------
  1194. - Imaging class added
  1195. - various color records implemented
  1196. - several .NET only functions added
  1197. - unit created
  1198. }
  1199. end.