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.

8594 lines
296 KiB

  1. {***********************************************************
  2. glBitmap by Steffen Xonna aka Lossy eX (2003-2008)
  3. http://www.opengl24.de/index.php?cat=header&file=glbitmap
  4. modified by Delphi OpenGL Community (http://delphigl.com/) (2013)
  5. ------------------------------------------------------------
  6. The contents of this file are used with permission, subject to
  7. the Mozilla Public License Version 1.1 (the "License"); you may
  8. not use this file except in compliance with the License. You may
  9. obtain a copy of the License at
  10. http://www.mozilla.org/MPL/MPL-1.1.html
  11. ------------------------------------------------------------
  12. Version 3.0.0 unstable
  13. ------------------------------------------------------------
  14. History
  15. 20-11-2013
  16. - refactoring of the complete library
  17. 21-03-2010
  18. - The define GLB_DELPHI dosn't check versions anymore. If you say you are using delphi
  19. then it's your problem if that isn't true. This prevents the unit for incompatibility
  20. with newer versions of Delphi.
  21. - Problems with D2009+ resolved (Thanks noeska and all i forgot)
  22. - GetPixel isn't set if you are loading textures inside the constructor (Thanks Wilson)
  23. 10-08-2008
  24. - AddAlphaFromglBitmap used the custom pointer instead the imagedatapointer (Thanks Wilson)
  25. - Additional Datapointer for functioninterface now has the name CustomData
  26. 24-07-2008
  27. - AssigneAlphaToBitmap overwrites his own palette (Thanks Wilson)
  28. - If you load an texture from an file the property Filename will be set to the name of the file
  29. - Three new properties to attach custom data to the Texture objects
  30. - CustomName (free for use string)
  31. - CustomNameW (free for use widestring)
  32. - CustomDataPointer (free for use pointer to attach other objects or complex structures)
  33. 27-05-2008
  34. - RLE TGAs loaded much faster
  35. 26-05-2008
  36. - fixed some problem with reading RLE TGAs.
  37. 21-05-2008
  38. - function clone now only copys data if it's assigned and now it also copies the ID
  39. - it seems that lazarus dont like comments in comments.
  40. 01-05-2008
  41. - It's possible to set the id of the texture
  42. - define GLB_NO_NATIVE_GL deactivated by default
  43. 27-04-2008
  44. - Now supports the following libraries
  45. - SDL and SDL_image
  46. - libPNG
  47. - libJPEG
  48. - Linux compatibillity via free pascal compatibility (delphi sources optional)
  49. - BMPs now loaded manuel
  50. - Large restructuring
  51. - Property DataPtr now has the name Data
  52. - Functions are more flexible between RGB(A) and BGR(A). RGB can be saved as Bitmap and will be saved as BGR
  53. - Unused Depth removed
  54. - Function FreeData to freeing image data added
  55. 24-10-2007
  56. - ImageID flag of TGAs was ignored. (Thanks Zwoetzen)
  57. 15-11-2006
  58. - Function SetBorderColor implemented (only used by opengl if wrap is set to GL_CLAMP_TO_BORDER)
  59. - Function AddAlphaFromValue implemented to use an fixed Value as Alphachannel
  60. - Function ReadOpenGLExtension is now only intern
  61. 29-06-2006
  62. - pngimage now disabled by default like all other versions.
  63. 26-06-2006
  64. - Setting up an anisotropic filter of 0 isnt allowed by nvidia (Thanks Ogridi)
  65. 22-06-2006
  66. - Fixed some Problem with Delphi 5
  67. - Now uses the newest version of pngimage. Makes saving pngs much easier.
  68. 22-03-2006
  69. - Property IsCompressed and Size removed. Not really supported by Spec (Thanks Ogridi)
  70. 09-03-2006
  71. - Internal Format ifDepth8 added
  72. - function GrabScreen now supports all uncompressed formats
  73. 31-01-2006
  74. - AddAlphaFromglBitmap implemented
  75. 29-12-2005
  76. - LoadFromResource and LoadFromResourceId now needs an Instance and an ResourceType (for ID)
  77. 28-12-2005
  78. - Width, Height and Depth internal changed to TglBitmapPixelPosition.
  79. property Width, Height, Depth are still existing and new property Dimension are avail
  80. 11-12-2005
  81. - Added native OpenGL Support. Breaking the dglOpenGL "barrier".
  82. 19-10-2005
  83. - Added function GrabScreen to class TglBitmap2D
  84. 18-10-2005
  85. - Added support to Save images
  86. - Added function Clone to Clone Instance
  87. 11-10-2005
  88. - Functions now works with Cardinals for each channel. Up to 32 Bits per channel.
  89. Usefull for Future
  90. - Several speed optimizations
  91. 09-10-2005
  92. - Internal structure change. Loading of TGA, PNG and DDS improved.
  93. Data, format and size will now set directly with SetDataPtr.
  94. - AddFunc now works with all Types of Images and Formats
  95. - Some Funtions moved to Baseclass TglBitmap
  96. 06-10-2005
  97. - Added Support to decompress DXT3 and DXT5 compressed Images.
  98. - Added Mapping to convert data from one format into an other.
  99. 05-10-2005
  100. - Added method ConvertTo in Class TglBitmap2D. Method allows to convert every
  101. supported Input format (supported by GetPixel) into any uncompresed Format
  102. - Added Support to decompress DXT1 compressed Images.
  103. - SwapColors replaced by ConvertTo
  104. 04-10-2005
  105. - Added Support for compressed DDSs
  106. - Added new internal formats (DXT1, DXT3, DXT5)
  107. 29-09-2005
  108. - Parameter Components renamed to InternalFormat
  109. 23-09-2005
  110. - Some AllocMem replaced with GetMem (little speed change)
  111. - better exception handling. Better protection from memory leaks.
  112. 22-09-2005
  113. - Added support for Direct Draw Surfaces (.DDS) (uncompressed images only)
  114. - Added new internal formats (RGB8, RGBA8, RGBA4, RGB5A1, RGB10A2, R5G6B5)
  115. 07-09-2005
  116. - Added support for Grayscale textures
  117. - Added internal formats (Alpha, Luminance, LuminanceAlpha, BGR8, BGRA8)
  118. 10-07-2005
  119. - Added support for GL_VERSION_2_0
  120. - Added support for GL_EXT_texture_filter_anisotropic
  121. 04-07-2005
  122. - Function FillWithColor fills the Image with one Color
  123. - Function LoadNormalMap added
  124. 30-06-2005
  125. - ToNormalMap allows to Create an NormalMap from the Alphachannel
  126. - ToNormalMap now supports Sobel (nmSobel) function.
  127. 29-06-2005
  128. - support for RLE Compressed RGB TGAs added
  129. 28-06-2005
  130. - Class TglBitmapNormalMap added to support Normalmap generation
  131. - Added function ToNormalMap in class TglBitmap2D to genereate normal maps from textures.
  132. 3 Filters are supported. (4 Samples, 3x3 and 5x5)
  133. 16-06-2005
  134. - Method LoadCubeMapClass removed
  135. - LoadCubeMap returnvalue is now the Texture paramter. Such as LoadTextures
  136. - virtual abstract method GenTexture in class TglBitmap now is protected
  137. 12-06-2005
  138. - now support DescriptionFlag in LoadTga. Allows vertical flipped images to be loaded as normal
  139. 10-06-2005
  140. - little enhancement for IsPowerOfTwo
  141. - TglBitmap1D.GenTexture now tests NPOT Textures
  142. 06-06-2005
  143. - some little name changes. All properties or function with Texture in name are
  144. now without texture in name. We have allways texture so we dosn't name it.
  145. 03-06-2005
  146. - GenTexture now tests if texture is NPOT and NPOT-Texture are supported or
  147. TextureTarget is GL_TEXTURE_RECTANGLE. Else it raised an exception.
  148. 02-06-2005
  149. - added support for GL_ARB_texture_rectangle, GL_EXT_texture_rectangle and GL_NV_texture_rectangle
  150. 25-04-2005
  151. - Function Unbind added
  152. - call of SetFilter or SetTextureWrap if TextureID exists results in setting properties to opengl texture.
  153. 21-04-2005
  154. - class TglBitmapCubeMap added (allows to Create Cubemaps)
  155. 29-03-2005
  156. - Added Support for PNG Images. (http://pngdelphi.sourceforge.net/)
  157. To Enable png's use the define pngimage
  158. 22-03-2005
  159. - New Functioninterface added
  160. - Function GetPixel added
  161. 27-11-2004
  162. - Property BuildMipMaps renamed to MipMap
  163. 21-11-2004
  164. - property Name removed.
  165. - BuildMipMaps is now a set of 3 values. None, GluBuildMipmaps and SGIS_generate_mipmap
  166. 22-05-2004
  167. - property name added. Only used in glForms!
  168. 26-11-2003
  169. - property FreeDataAfterGenTexture is now available as default (default = true)
  170. - BuildMipmaps now implemented in TglBitmap1D (i've forgotten it)
  171. - function MoveMemory replaced with function Move (little speed change)
  172. - several calculations stored in variables (little speed change)
  173. 29-09-2003
  174. - property BuildMipsMaps added (default = true)
  175. if BuildMipMaps isn't set GenTextures uses glTexImage[12]D else it use gluBuild[12]dMipmaps
  176. - property FreeDataAfterGenTexture added (default = true)
  177. if FreeDataAfterGenTexture is set the texturedata were deleted after the texture was generated.
  178. - parameter DisableOtherTextureUnits of Bind removed
  179. - parameter FreeDataAfterGeneration of GenTextures removed
  180. 12-09-2003
  181. - TglBitmap dosn't delete data if class was destroyed (fixed)
  182. 09-09-2003
  183. - Bind now enables TextureUnits (by params)
  184. - GenTextures can leave data (by param)
  185. - LoadTextures now optimal
  186. 03-09-2003
  187. - Performance optimization in AddFunc
  188. - procedure Bind moved to subclasses
  189. - Added new Class TglBitmap1D to support real OpenGL 1D Textures
  190. 19-08-2003
  191. - Texturefilter and texturewrap now also as defaults
  192. Minfilter = GL_LINEAR_MIPMAP_LINEAR
  193. Magfilter = GL_LINEAR
  194. Wrap(str) = GL_CLAMP_TO_EDGE
  195. - Added new format tfCompressed to create a compressed texture.
  196. - propertys IsCompressed, TextureSize and IsResident added
  197. IsCompressed and TextureSize only contains data from level 0
  198. 18-08-2003
  199. - Added function AddFunc to add PerPixelEffects to Image
  200. - LoadFromFunc now based on AddFunc
  201. - Invert now based on AddFunc
  202. - SwapColors now based on AddFunc
  203. 16-08-2003
  204. - Added function FlipHorz
  205. 15-08-2003
  206. - Added function LaodFromFunc to create images with function
  207. - Added function FlipVert
  208. - Added internal format RGB(A) if GL_EXT_bgra or OpenGL 1.2 isn't supported
  209. 29-07-2003
  210. - Added Alphafunctions to calculate alpha per function
  211. - Added Alpha from ColorKey using alphafunctions
  212. 28-07-2003
  213. - First full functionally Version of glBitmap
  214. - Support for 24Bit and 32Bit TGA Pictures added
  215. 25-07-2003
  216. - begin of programming
  217. ***********************************************************}
  218. unit glBitmap;
  219. // Please uncomment the defines below to configure the glBitmap to your preferences.
  220. // If you have configured the unit you can uncomment the warning above.
  221. {$MESSAGE warn 'Hey. I''m the glBitmap.pas and i need to be configured. My master tell me your preferences! ;)'}
  222. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  223. // Preferences ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  224. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  225. // activate to enable build-in OpenGL support with statically linked methods
  226. // use dglOpenGL.pas if not enabled
  227. {.$DEFINE GLB_NATIVE_OGL_STATIC}
  228. // activate to enable build-in OpenGL support with dynamically linked methods
  229. // use dglOpenGL.pas if not enabled
  230. {.$DEFINE GLB_NATIVE_OGL_DYNAMIC}
  231. // activate to enable the support for SDL_surfaces
  232. {.$DEFINE GLB_SDL}
  233. // activate to enable the support for TBitmap from Delphi (not lazarus)
  234. {.$DEFINE GLB_DELPHI}
  235. // activate to enable the support for TLazIntfImage from Lazarus
  236. {.$DEFINE GLB_LAZARUS}
  237. // activate to enable the support of SDL_image to load files. (READ ONLY)
  238. // If you enable SDL_image all other libraries will be ignored!
  239. {.$DEFINE GLB_SDL_IMAGE}
  240. // activate to enable Lazarus TPortableNetworkGraphic support
  241. // if you enable this pngImage and libPNG will be ignored
  242. {.$DEFINE GLB_LAZ_PNG}
  243. // activate to enable png support with the unit pngimage -> http://pngdelphi.sourceforge.net/
  244. // if you enable pngimage the libPNG will be ignored
  245. {.$DEFINE GLB_PNGIMAGE}
  246. // activate to use the libPNG -> http://www.libpng.org/
  247. // You will need an aditional header -> http://www.opengl24.de/index.php?cat=header&file=libpng
  248. {.$DEFINE GLB_LIB_PNG}
  249. // activate to enable Lazarus TJPEGImage support
  250. // if you enable this delphi jpegs and libJPEG will be ignored
  251. {.$DEFINE GLB_LAZ_JPEG}
  252. // if you enable delphi jpegs the libJPEG will be ignored
  253. {.$DEFINE GLB_DELPHI_JPEG}
  254. // activate to use the libJPEG -> http://www.ijg.org/
  255. // You will need an aditional header -> http://www.opengl24.de/index.php?cat=header&file=libjpeg
  256. {.$DEFINE GLB_LIB_JPEG}
  257. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  258. // PRIVATE: do not change anything! //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  259. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  260. // Delphi Versions
  261. {$IFDEF fpc}
  262. {$MODE Delphi}
  263. {$IFDEF CPUI386}
  264. {$DEFINE CPU386}
  265. {$ASMMODE INTEL}
  266. {$ENDIF}
  267. {$IFNDEF WINDOWS}
  268. {$linklib c}
  269. {$ENDIF}
  270. {$ENDIF}
  271. // Operation System
  272. {$IF DEFINED(WIN32) or DEFINED(WIN64) or DEFINED(WINDOWS)}
  273. {$DEFINE GLB_WIN}
  274. {$ELSEIF DEFINED(LINUX)}
  275. {$DEFINE GLB_LINUX}
  276. {$IFEND}
  277. // native OpenGL Support
  278. {$IF DEFINED(GLB_NATIVE_OGL_STATIC) OR DEFINED(GLB_NATIVE_OGL_DYNAMIC)}
  279. {$DEFINE GLB_NATIVE_OGL}
  280. {$IFEND}
  281. // checking define combinations
  282. //SDL Image
  283. {$IFDEF GLB_SDL_IMAGE}
  284. {$IFNDEF GLB_SDL}
  285. {$MESSAGE warn 'SDL_image won''t work without SDL. SDL will be activated.'}
  286. {$DEFINE GLB_SDL}
  287. {$ENDIF}
  288. {$IFDEF GLB_LAZ_PNG}
  289. {$MESSAGE warn 'The Lazarus TPortableNetworkGraphics will be ignored because you are using SDL_image.'}
  290. {$undef GLB_LAZ_PNG}
  291. {$ENDIF}
  292. {$IFDEF GLB_PNGIMAGE}
  293. {$MESSAGE warn 'The unit pngimage will be ignored because you are using SDL_image.'}
  294. {$undef GLB_PNGIMAGE}
  295. {$ENDIF}
  296. {$IFDEF GLB_LAZ_JPEG}
  297. {$MESSAGE warn 'The Lazarus TJPEGImage will be ignored because you are using SDL_image.'}
  298. {$undef GLB_LAZ_JPEG}
  299. {$ENDIF}
  300. {$IFDEF GLB_DELPHI_JPEG}
  301. {$MESSAGE warn 'The unit JPEG will be ignored because you are using SDL_image.'}
  302. {$undef GLB_DELPHI_JPEG}
  303. {$ENDIF}
  304. {$IFDEF GLB_LIB_PNG}
  305. {$MESSAGE warn 'The library libPNG will be ignored because you are using SDL_image.'}
  306. {$undef GLB_LIB_PNG}
  307. {$ENDIF}
  308. {$IFDEF GLB_LIB_JPEG}
  309. {$MESSAGE warn 'The library libJPEG will be ignored because you are using SDL_image.'}
  310. {$undef GLB_LIB_JPEG}
  311. {$ENDIF}
  312. {$DEFINE GLB_SUPPORT_PNG_READ}
  313. {$DEFINE GLB_SUPPORT_JPEG_READ}
  314. {$ENDIF}
  315. // Lazarus TPortableNetworkGraphic
  316. {$IFDEF GLB_LAZ_PNG}
  317. {$IFNDEF GLB_LAZARUS}
  318. {$MESSAGE warn 'Lazarus TPortableNetworkGraphic won''t work without Lazarus. Lazarus will be activated.'}
  319. {$DEFINE GLB_LAZARUS}
  320. {$ENDIF}
  321. {$IFDEF GLB_PNGIMAGE}
  322. {$MESSAGE warn 'The pngimage will be ignored if you are using Lazarus TPortableNetworkGraphic.'}
  323. {$undef GLB_PNGIMAGE}
  324. {$ENDIF}
  325. {$IFDEF GLB_LIB_PNG}
  326. {$MESSAGE warn 'The library libPNG will be ignored if you are using Lazarus TPortableNetworkGraphic.'}
  327. {$undef GLB_LIB_PNG}
  328. {$ENDIF}
  329. {$DEFINE GLB_SUPPORT_PNG_READ}
  330. {$DEFINE GLB_SUPPORT_PNG_WRITE}
  331. {$ENDIF}
  332. // PNG Image
  333. {$IFDEF GLB_PNGIMAGE}
  334. {$IFDEF GLB_LIB_PNG}
  335. {$MESSAGE warn 'The library libPNG will be ignored if you are using pngimage.'}
  336. {$undef GLB_LIB_PNG}
  337. {$ENDIF}
  338. {$DEFINE GLB_SUPPORT_PNG_READ}
  339. {$DEFINE GLB_SUPPORT_PNG_WRITE}
  340. {$ENDIF}
  341. // libPNG
  342. {$IFDEF GLB_LIB_PNG}
  343. {$DEFINE GLB_SUPPORT_PNG_READ}
  344. {$DEFINE GLB_SUPPORT_PNG_WRITE}
  345. {$ENDIF}
  346. // Lazarus TJPEGImage
  347. {$IFDEF GLB_LAZ_JPEG}
  348. {$IFNDEF GLB_LAZARUS}
  349. {$MESSAGE warn 'Lazarus TJPEGImage won''t work without Lazarus. Lazarus will be activated.'}
  350. {$DEFINE GLB_LAZARUS}
  351. {$ENDIF}
  352. {$IFDEF GLB_DELPHI_JPEG}
  353. {$MESSAGE warn 'The Delphi JPEGImage will be ignored if you are using the Lazarus TJPEGImage.'}
  354. {$undef GLB_DELPHI_JPEG}
  355. {$ENDIF}
  356. {$IFDEF GLB_LIB_JPEG}
  357. {$MESSAGE warn 'The library libJPEG will be ignored if you are using the Lazarus TJPEGImage.'}
  358. {$undef GLB_LIB_JPEG}
  359. {$ENDIF}
  360. {$DEFINE GLB_SUPPORT_JPEG_READ}
  361. {$DEFINE GLB_SUPPORT_JPEG_WRITE}
  362. {$ENDIF}
  363. // JPEG Image
  364. {$IFDEF GLB_DELPHI_JPEG}
  365. {$IFDEF GLB_LIB_JPEG}
  366. {$MESSAGE warn 'The library libJPEG will be ignored if you are using the unit JPEG.'}
  367. {$undef GLB_LIB_JPEG}
  368. {$ENDIF}
  369. {$DEFINE GLB_SUPPORT_JPEG_READ}
  370. {$DEFINE GLB_SUPPORT_JPEG_WRITE}
  371. {$ENDIF}
  372. // libJPEG
  373. {$IFDEF GLB_LIB_JPEG}
  374. {$DEFINE GLB_SUPPORT_JPEG_READ}
  375. {$DEFINE GLB_SUPPORT_JPEG_WRITE}
  376. {$ENDIF}
  377. // native OpenGL
  378. {$IF DEFINED(GLB_NATIVE_OGL_STATIC) AND DEFINED(GLB_NATIVE_OGL_DYNAMIC)}
  379. {$MESSAGE warn 'GLB_NATIVE_OGL_STATIC will be ignored because you enabled GLB_NATIVE_OGL_DYNAMIC'}
  380. {$IFEND}
  381. // general options
  382. {$EXTENDEDSYNTAX ON}
  383. {$LONGSTRINGS ON}
  384. {$ALIGN ON}
  385. {$IFNDEF FPC}
  386. {$OPTIMIZATION ON}
  387. {$ENDIF}
  388. interface
  389. uses
  390. {$IFNDEF GLB_NATIVE_OGL} dglOpenGL, {$ENDIF}
  391. {$IF DEFINED(GLB_WIN) AND
  392. (DEFINED(GLB_NATIVE_OGL) OR
  393. DEFINED(GLB_DELPHI))} windows, {$IFEND}
  394. {$IFDEF GLB_SDL} SDL, {$ENDIF}
  395. {$IFDEF GLB_LAZARUS} IntfGraphics, GraphType, Graphics, {$ENDIF}
  396. {$IFDEF GLB_DELPHI} Dialogs, Graphics, Types, {$ENDIF}
  397. {$IFDEF GLB_SDL_IMAGE} SDL_image, {$ENDIF}
  398. {$IFDEF GLB_PNGIMAGE} pngimage, {$ENDIF}
  399. {$IFDEF GLB_LIB_PNG} libPNG, {$ENDIF}
  400. {$IFDEF GLB_DELPHI_JPEG} JPEG, {$ENDIF}
  401. {$IFDEF GLB_LIB_JPEG} libJPEG, {$ENDIF}
  402. Classes, SysUtils;
  403. {$IFDEF GLB_NATIVE_OGL}
  404. const
  405. GL_TRUE = 1;
  406. GL_FALSE = 0;
  407. GL_ZERO = 0;
  408. GL_ONE = 1;
  409. GL_VERSION = $1F02;
  410. GL_EXTENSIONS = $1F03;
  411. GL_TEXTURE_1D = $0DE0;
  412. GL_TEXTURE_2D = $0DE1;
  413. GL_TEXTURE_RECTANGLE = $84F5;
  414. GL_NORMAL_MAP = $8511;
  415. GL_TEXTURE_CUBE_MAP = $8513;
  416. GL_REFLECTION_MAP = $8512;
  417. GL_TEXTURE_CUBE_MAP_POSITIVE_X = $8515;
  418. GL_TEXTURE_CUBE_MAP_NEGATIVE_X = $8516;
  419. GL_TEXTURE_CUBE_MAP_POSITIVE_Y = $8517;
  420. GL_TEXTURE_CUBE_MAP_NEGATIVE_Y = $8518;
  421. GL_TEXTURE_CUBE_MAP_POSITIVE_Z = $8519;
  422. GL_TEXTURE_CUBE_MAP_NEGATIVE_Z = $851A;
  423. GL_TEXTURE_WIDTH = $1000;
  424. GL_TEXTURE_HEIGHT = $1001;
  425. GL_TEXTURE_INTERNAL_FORMAT = $1003;
  426. GL_TEXTURE_SWIZZLE_RGBA = $8E46;
  427. GL_S = $2000;
  428. GL_T = $2001;
  429. GL_R = $2002;
  430. GL_Q = $2003;
  431. GL_TEXTURE_GEN_S = $0C60;
  432. GL_TEXTURE_GEN_T = $0C61;
  433. GL_TEXTURE_GEN_R = $0C62;
  434. GL_TEXTURE_GEN_Q = $0C63;
  435. GL_RED = $1903;
  436. GL_GREEN = $1904;
  437. GL_BLUE = $1905;
  438. GL_ALPHA = $1906;
  439. GL_ALPHA4 = $803B;
  440. GL_ALPHA8 = $803C;
  441. GL_ALPHA12 = $803D;
  442. GL_ALPHA16 = $803E;
  443. GL_LUMINANCE = $1909;
  444. GL_LUMINANCE4 = $803F;
  445. GL_LUMINANCE8 = $8040;
  446. GL_LUMINANCE12 = $8041;
  447. GL_LUMINANCE16 = $8042;
  448. GL_LUMINANCE_ALPHA = $190A;
  449. GL_LUMINANCE4_ALPHA4 = $8043;
  450. GL_LUMINANCE6_ALPHA2 = $8044;
  451. GL_LUMINANCE8_ALPHA8 = $8045;
  452. GL_LUMINANCE12_ALPHA4 = $8046;
  453. GL_LUMINANCE12_ALPHA12 = $8047;
  454. GL_LUMINANCE16_ALPHA16 = $8048;
  455. GL_RGB = $1907;
  456. GL_BGR = $80E0;
  457. GL_R3_G3_B2 = $2A10;
  458. GL_RGB4 = $804F;
  459. GL_RGB5 = $8050;
  460. GL_RGB565 = $8D62;
  461. GL_RGB8 = $8051;
  462. GL_RGB10 = $8052;
  463. GL_RGB12 = $8053;
  464. GL_RGB16 = $8054;
  465. GL_RGBA = $1908;
  466. GL_BGRA = $80E1;
  467. GL_RGBA2 = $8055;
  468. GL_RGBA4 = $8056;
  469. GL_RGB5_A1 = $8057;
  470. GL_RGBA8 = $8058;
  471. GL_RGB10_A2 = $8059;
  472. GL_RGBA12 = $805A;
  473. GL_RGBA16 = $805B;
  474. GL_DEPTH_COMPONENT = $1902;
  475. GL_DEPTH_COMPONENT16 = $81A5;
  476. GL_DEPTH_COMPONENT24 = $81A6;
  477. GL_DEPTH_COMPONENT32 = $81A7;
  478. GL_COMPRESSED_RGB = $84ED;
  479. GL_COMPRESSED_RGBA = $84EE;
  480. GL_COMPRESSED_RGB_S3TC_DXT1_EXT = $83F0;
  481. GL_COMPRESSED_RGBA_S3TC_DXT1_EXT = $83F1;
  482. GL_COMPRESSED_RGBA_S3TC_DXT3_EXT = $83F2;
  483. GL_COMPRESSED_RGBA_S3TC_DXT5_EXT = $83F3;
  484. GL_UNSIGNED_BYTE = $1401;
  485. GL_UNSIGNED_BYTE_3_3_2 = $8032;
  486. GL_UNSIGNED_BYTE_2_3_3_REV = $8362;
  487. GL_UNSIGNED_SHORT = $1403;
  488. GL_UNSIGNED_SHORT_5_6_5 = $8363;
  489. GL_UNSIGNED_SHORT_4_4_4_4 = $8033;
  490. GL_UNSIGNED_SHORT_5_5_5_1 = $8034;
  491. GL_UNSIGNED_SHORT_5_6_5_REV = $8364;
  492. GL_UNSIGNED_SHORT_4_4_4_4_REV = $8365;
  493. GL_UNSIGNED_SHORT_1_5_5_5_REV = $8366;
  494. GL_UNSIGNED_INT = $1405;
  495. GL_UNSIGNED_INT_8_8_8_8 = $8035;
  496. GL_UNSIGNED_INT_10_10_10_2 = $8036;
  497. GL_UNSIGNED_INT_8_8_8_8_REV = $8367;
  498. GL_UNSIGNED_INT_2_10_10_10_REV = $8368;
  499. { Texture Filter }
  500. GL_TEXTURE_MAG_FILTER = $2800;
  501. GL_TEXTURE_MIN_FILTER = $2801;
  502. GL_NEAREST = $2600;
  503. GL_NEAREST_MIPMAP_NEAREST = $2700;
  504. GL_NEAREST_MIPMAP_LINEAR = $2702;
  505. GL_LINEAR = $2601;
  506. GL_LINEAR_MIPMAP_NEAREST = $2701;
  507. GL_LINEAR_MIPMAP_LINEAR = $2703;
  508. { Texture Wrap }
  509. GL_TEXTURE_WRAP_S = $2802;
  510. GL_TEXTURE_WRAP_T = $2803;
  511. GL_TEXTURE_WRAP_R = $8072;
  512. GL_CLAMP = $2900;
  513. GL_REPEAT = $2901;
  514. GL_CLAMP_TO_EDGE = $812F;
  515. GL_CLAMP_TO_BORDER = $812D;
  516. GL_MIRRORED_REPEAT = $8370;
  517. { Other }
  518. GL_GENERATE_MIPMAP = $8191;
  519. GL_TEXTURE_BORDER_COLOR = $1004;
  520. GL_MAX_TEXTURE_SIZE = $0D33;
  521. GL_PACK_ALIGNMENT = $0D05;
  522. GL_UNPACK_ALIGNMENT = $0CF5;
  523. GL_TEXTURE_MAX_ANISOTROPY_EXT = $84FE;
  524. GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT = $84FF;
  525. GL_MAX_CUBE_MAP_TEXTURE_SIZE = $851C;
  526. GL_TEXTURE_GEN_MODE = $2500;
  527. {$IF DEFINED(GLB_WIN)}
  528. libglu = 'glu32.dll';
  529. libopengl = 'opengl32.dll';
  530. {$ELSEIF DEFINED(GLB_LINUX)}
  531. libglu = 'libGLU.so.1';
  532. libopengl = 'libGL.so.1';
  533. {$IFEND}
  534. type
  535. GLboolean = BYTEBOOL;
  536. GLint = Integer;
  537. GLsizei = Integer;
  538. GLuint = Cardinal;
  539. GLfloat = Single;
  540. GLenum = Cardinal;
  541. PGLvoid = Pointer;
  542. PGLboolean = ^GLboolean;
  543. PGLint = ^GLint;
  544. PGLuint = ^GLuint;
  545. PGLfloat = ^GLfloat;
  546. TglCompressedTexImage1D = procedure(target: GLenum; level: GLint; internalformat: GLenum; width: GLsizei; border: GLint; imageSize: GLsizei; const data: PGLvoid); {$IFDEF GLB_WIN}stdcall; {$ELSE}cdecl; {$ENDIF}
  547. TglCompressedTexImage2D = procedure(target: GLenum; level: GLint; internalformat: GLenum; width: GLsizei; height: GLsizei; border: GLint; imageSize: GLsizei; const data: PGLvoid); {$IFDEF GLB_WIN}stdcall; {$ELSE}cdecl; {$ENDIF}
  548. TglGetCompressedTexImage = procedure(target: GLenum; level: GLint; img: PGLvoid); {$IFDEF GLB_WIN}stdcall; {$ELSE}cdecl; {$ENDIF}
  549. {$IF DEFINED(GLB_WIN)}
  550. TwglGetProcAddress = function (ProcName: PAnsiChar): Pointer; stdcall;
  551. {$ELSEIF DEFINED(GLB_LINUX)}
  552. TglXGetProcAddress = function(ProcName: PAnsiChar): Pointer; cdecl;
  553. TglXGetProcAddressARB = function(const name: PAnsiChar): pointer; cdecl;
  554. {$IFEND}
  555. {$IF DEFINED(GLB_NATIVE_OGL_DYNAMIC)}
  556. TglEnable = procedure(cap: GLenum); {$IFDEF GLB_WIN}stdcall; {$ELSE}cdecl; {$ENDIF}
  557. TglDisable = procedure(cap: GLenum); {$IFDEF GLB_WIN}stdcall; {$ELSE}cdecl; {$ENDIF}
  558. TglGetString = function(name: GLenum): PAnsiChar; {$IFDEF GLB_WIN}stdcall; {$ELSE}cdecl; {$ENDIF}
  559. TglGetIntegerv = procedure(pname: GLenum; params: PGLint); {$IFDEF GLB_WIN}stdcall; {$ELSE}cdecl; {$ENDIF}
  560. TglTexParameteri = procedure(target: GLenum; pname: GLenum; param: GLint); {$IFDEF GLB_WIN}stdcall; {$ELSE}cdecl; {$ENDIF}
  561. TglTexParameteriv = procedure(target: GLenum; pname: GLenum; const params: PGLint); {$IFDEF GLB_WIN}stdcall; {$ELSE}cdecl; {$ENDIF}
  562. TglTexParameterfv = procedure(target: GLenum; pname: GLenum; const params: PGLfloat); {$IFDEF GLB_WIN}stdcall; {$ELSE}cdecl; {$ENDIF}
  563. TglGetTexParameteriv = procedure(target: GLenum; pname: GLenum; params: PGLint); {$IFDEF GLB_WIN}stdcall; {$ELSE}cdecl; {$ENDIF}
  564. TglGetTexParameterfv = procedure(target: GLenum; pname: GLenum; params: PGLfloat); {$IFDEF GLB_WIN}stdcall; {$ELSE}cdecl; {$ENDIF}
  565. TglGetTexLevelParameteriv = procedure(target: GLenum; level: GLint; pname: GLenum; params: PGLint); {$IFDEF GLB_WIN}stdcall; {$ELSE}cdecl; {$ENDIF}
  566. TglGetTexLevelParameterfv = procedure(target: GLenum; level: GLint; pname: GLenum; params: PGLfloat); {$IFDEF GLB_WIN}stdcall; {$ELSE}cdecl; {$ENDIF}
  567. TglTexGeni = procedure(coord: GLenum; pname: GLenum; param: GLint); {$IFDEF GLB_WIN}stdcall; {$ELSE}cdecl; {$ENDIF}
  568. TglGenTextures = procedure(n: GLsizei; textures: PGLuint); {$IFDEF GLB_WIN}stdcall; {$ELSE}cdecl; {$ENDIF}
  569. TglBindTexture = procedure(target: GLenum; texture: GLuint); {$IFDEF GLB_WIN}stdcall; {$ELSE}cdecl; {$ENDIF}
  570. TglDeleteTextures = procedure(n: GLsizei; const textures: PGLuint); {$IFDEF GLB_WIN}stdcall; {$ELSE}cdecl; {$ENDIF}
  571. TglAreTexturesResident = function(n: GLsizei; const textures: PGLuint; residences: PGLboolean): GLboolean; {$IFDEF DGL_WIN}stdcall; {$ELSE}cdecl; {$ENDIF}
  572. TglReadPixels = procedure(x: GLint; y: GLint; width: GLsizei; height: GLsizei; format: GLenum; _type: GLenum; pixels: PGLvoid); {$IFDEF GLB_WIN}stdcall; {$ELSE}cdecl; {$ENDIF}
  573. TglPixelStorei = procedure(pname: GLenum; param: GLint); {$IFDEF GLB_WIN}stdcall; {$ELSE}cdecl; {$ENDIF}
  574. TglTexImage1D = procedure(target: GLenum; level: GLint; internalformat: GLint; width: GLsizei; border: GLint; format: GLenum; _type: GLenum; const pixels: PGLvoid); {$IFDEF GLB_WIN}stdcall; {$ELSE}cdecl; {$ENDIF}
  575. TglTexImage2D = procedure(target: GLenum; level: GLint; internalformat: GLint; width: GLsizei; height: GLsizei; border: GLint; format: GLenum; _type: GLenum; const pixels: PGLvoid); {$IFDEF GLB_WIN}stdcall; {$ELSE}cdecl; {$ENDIF}
  576. TglGetTexImage = procedure(target: GLenum; level: GLint; format: GLenum; _type: GLenum; pixels: PGLvoid); {$IFDEF GLB_WIN}stdcall; {$ELSE}cdecl; {$ENDIF}
  577. TgluBuild1DMipmaps = function(target: GLEnum; components, width: GLint; format, atype: GLEnum; const data: Pointer): GLint; {$IFDEF GLB_WIN}stdcall; {$ELSE}cdecl; {$ENDIF}
  578. TgluBuild2DMipmaps = function(target: GLEnum; components, width, height: GLint; format, atype: GLEnum; const Data: Pointer): GLint; {$IFDEF GLB_WIN}stdcall; {$ELSE}cdecl; {$ENDIF}
  579. {$ELSEIF DEFINED(GLB_NATIVE_OGL_STATIC)}
  580. procedure glEnable(cap: GLenum); {$IFDEF GLB_WIN}stdcall; {$ELSE}cdecl; {$ENDIF} external libopengl;
  581. procedure glDisable(cap: GLenum); {$IFDEF GLB_WIN}stdcall; {$ELSE}cdecl; {$ENDIF} external libopengl;
  582. function glGetString(name: GLenum): PAnsiChar; {$IFDEF GLB_WIN}stdcall; {$ELSE}cdecl; {$ENDIF} external libopengl;
  583. procedure glGetIntegerv(pname: GLenum; params: PGLint); {$IFDEF GLB_WIN}stdcall; {$ELSE}cdecl; {$ENDIF} external libopengl;
  584. procedure glTexParameteri(target: GLenum; pname: GLenum; param: GLint); {$IFDEF GLB_WIN}stdcall; {$ELSE}cdecl; {$ENDIF} external libopengl;
  585. procedure glTexParameteriv(target: GLenum; pname: GLenum; const params: PGLint); {$IFDEF GLB_WIN}stdcall; {$ELSE}cdecl; {$ENDIF} external libopengl;
  586. procedure glTexParameterfv(target: GLenum; pname: GLenum; const params: PGLfloat); {$IFDEF GLB_WIN}stdcall; {$ELSE}cdecl; {$ENDIF} external libopengl;
  587. procedure glGetTexParameteriv(target: GLenum; pname: GLenum; params: PGLint); {$IFDEF GLB_WIN}stdcall; {$ELSE}cdecl; {$ENDIF} external libopengl;
  588. procedure glGetTexParameterfv(target: GLenum; pname: GLenum; params: PGLfloat); {$IFDEF GLB_WIN}stdcall; {$ELSE}cdecl; {$ENDIF} external libopengl;
  589. procedure glGetTexLevelParameteriv(target: GLenum; level: GLint; pname: GLenum; params: PGLint); {$IFDEF GLB_WIN}stdcall; {$ELSE}cdecl; {$ENDIF} external libopengl;
  590. procedure glGetTexLevelParameterfv(target: GLenum; level: GLint; pname: GLenum; params: PGLfloat); {$IFDEF GLB_WIN}stdcall; {$ELSE}cdecl; {$ENDIF} external libopengl;
  591. procedure glTexGeni(coord: GLenum; pname: GLenum; param: GLint); {$IFDEF GLB_WIN}stdcall; {$ELSE}cdecl; {$ENDIF} external libopengl;
  592. procedure glGenTextures(n: GLsizei; textures: PGLuint); {$IFDEF GLB_WIN}stdcall; {$ELSE}cdecl; {$ENDIF} external libopengl;
  593. procedure glBindTexture(target: GLenum; texture: GLuint); {$IFDEF GLB_WIN}stdcall; {$ELSE}cdecl; {$ENDIF} external libopengl;
  594. procedure glDeleteTextures(n: GLsizei; const textures: PGLuint); {$IFDEF GLB_WIN}stdcall; {$ELSE}cdecl; {$ENDIF} external libopengl;
  595. function glAreTexturesResident(n: GLsizei; const textures: PGLuint; residences: PGLboolean): GLboolean; {$IFDEF DGL_WIN}stdcall; {$ELSE}cdecl; {$ENDIF} external libopengl;
  596. procedure glReadPixels(x: GLint; y: GLint; width: GLsizei; height: GLsizei; format: GLenum; _type: GLenum; pixels: PGLvoid); {$IFDEF GLB_WIN}stdcall; {$ELSE}cdecl; {$ENDIF} external libopengl;
  597. procedure glPixelStorei(pname: GLenum; param: GLint); {$IFDEF GLB_WIN}stdcall; {$ELSE}cdecl; {$ENDIF} external libopengl;
  598. procedure glTexImage1D(target: GLenum; level: GLint; internalformat: GLint; width: GLsizei; border: GLint; format: GLenum; _type: GLenum; const pixels: PGLvoid); {$IFDEF GLB_WIN}stdcall; {$ELSE}cdecl; {$ENDIF} external libopengl;
  599. procedure glTexImage2D(target: GLenum; level: GLint; internalformat: GLint; width: GLsizei; height: GLsizei; border: GLint; format: GLenum; _type: GLenum; const pixels: PGLvoid); {$IFDEF GLB_WIN}stdcall; {$ELSE}cdecl; {$ENDIF} external libopengl;
  600. procedure glGetTexImage(target: GLenum; level: GLint; format: GLenum; _type: GLenum; pixels: PGLvoid); {$IFDEF GLB_WIN}stdcall; {$ELSE}cdecl; {$ENDIF} external libopengl;
  601. function gluBuild1DMipmaps(target: GLEnum; components, width: GLint; format, atype: GLEnum; const data: Pointer): GLint; {$IFDEF GLB_WIN}stdcall; {$ELSE}cdecl; {$ENDIF} external libglu;
  602. function gluBuild2DMipmaps(target: GLEnum; components, width, height: GLint; format, atype: GLEnum; const Data: Pointer): GLint; {$IFDEF GLB_WIN}stdcall; {$ELSE}cdecl; {$ENDIF} external libglu;
  603. {$IFEND}
  604. var
  605. GL_VERSION_1_2,
  606. GL_VERSION_1_3,
  607. GL_VERSION_1_4,
  608. GL_VERSION_2_0,
  609. GL_VERSION_3_3,
  610. GL_SGIS_generate_mipmap,
  611. GL_ARB_texture_border_clamp,
  612. GL_ARB_texture_mirrored_repeat,
  613. GL_ARB_texture_rectangle,
  614. GL_ARB_texture_non_power_of_two,
  615. GL_ARB_texture_swizzle,
  616. GL_ARB_texture_cube_map,
  617. GL_IBM_texture_mirrored_repeat,
  618. GL_NV_texture_rectangle,
  619. GL_EXT_texture_edge_clamp,
  620. GL_EXT_texture_rectangle,
  621. GL_EXT_texture_swizzle,
  622. GL_EXT_texture_cube_map,
  623. GL_EXT_texture_filter_anisotropic: Boolean;
  624. glCompressedTexImage1D: TglCompressedTexImage1D;
  625. glCompressedTexImage2D: TglCompressedTexImage2D;
  626. glGetCompressedTexImage: TglGetCompressedTexImage;
  627. {$IF DEFINED(GLB_WIN)}
  628. wglGetProcAddress: TwglGetProcAddress;
  629. {$ELSEIF DEFINED(GLB_LINUX)}
  630. glXGetProcAddress: TglXGetProcAddress;
  631. glXGetProcAddressARB: TglXGetProcAddress;
  632. {$IFEND}
  633. {$IFDEF GLB_NATIVE_OGL_DYNAMIC}
  634. glEnable: TglEnable;
  635. glDisable: TglDisable;
  636. glGetString: TglGetString;
  637. glGetIntegerv: TglGetIntegerv;
  638. glTexParameteri: TglTexParameteri;
  639. glTexParameteriv: TglTexParameteriv;
  640. glTexParameterfv: TglTexParameterfv;
  641. glGetTexParameteriv: TglGetTexParameteriv;
  642. glGetTexParameterfv: TglGetTexParameterfv;
  643. glGetTexLevelParameteriv: TglGetTexLevelParameteriv;
  644. glGetTexLevelParameterfv: TglGetTexLevelParameterfv;
  645. glTexGeni: TglTexGeni;
  646. glGenTextures: TglGenTextures;
  647. glBindTexture: TglBindTexture;
  648. glDeleteTextures: TglDeleteTextures;
  649. glAreTexturesResident: TglAreTexturesResident;
  650. glReadPixels: TglReadPixels;
  651. glPixelStorei: TglPixelStorei;
  652. glTexImage1D: TglTexImage1D;
  653. glTexImage2D: TglTexImage2D;
  654. glGetTexImage: TglGetTexImage;
  655. gluBuild1DMipmaps: TgluBuild1DMipmaps;
  656. gluBuild2DMipmaps: TgluBuild2DMipmaps;
  657. {$ENDIF}
  658. {$ENDIF}
  659. type
  660. ////////////////////////////////////////////////////////////////////////////////////////////////////
  661. TglBitmapFormat = (
  662. tfEmpty = 0, //must be smallest value!
  663. tfAlpha4,
  664. tfAlpha8,
  665. tfAlpha12,
  666. tfAlpha16,
  667. tfLuminance4,
  668. tfLuminance8,
  669. tfLuminance12,
  670. tfLuminance16,
  671. tfLuminance4Alpha4,
  672. tfLuminance6Alpha2,
  673. tfLuminance8Alpha8,
  674. tfLuminance12Alpha4,
  675. tfLuminance12Alpha12,
  676. tfLuminance16Alpha16,
  677. tfR3G3B2,
  678. tfRGB4,
  679. tfR5G6B5,
  680. tfRGB5,
  681. tfRGB8,
  682. tfRGB10,
  683. tfRGB12,
  684. tfRGB16,
  685. tfRGBA2,
  686. tfRGBA4,
  687. tfRGB5A1,
  688. tfRGBA8,
  689. tfRGB10A2,
  690. tfRGBA12,
  691. tfRGBA16,
  692. tfBGR4,
  693. tfB5G6R5,
  694. tfBGR5,
  695. tfBGR8,
  696. tfBGR10,
  697. tfBGR12,
  698. tfBGR16,
  699. tfBGRA2,
  700. tfBGRA4,
  701. tfBGR5A1,
  702. tfBGRA8,
  703. tfBGR10A2,
  704. tfBGRA12,
  705. tfBGRA16,
  706. tfDepth16,
  707. tfDepth24,
  708. tfDepth32,
  709. tfS3tcDtx1RGBA,
  710. tfS3tcDtx3RGBA,
  711. tfS3tcDtx5RGBA
  712. );
  713. TglBitmapFileType = (
  714. {$IFDEF GLB_SUPPORT_PNG_WRITE} ftPNG, {$ENDIF}
  715. {$IFDEF GLB_SUPPORT_JPEG_WRITE}ftJPEG, {$ENDIF}
  716. ftDDS,
  717. ftTGA,
  718. ftBMP);
  719. TglBitmapFileTypes = set of TglBitmapFileType;
  720. TglBitmapMipMap = (
  721. mmNone,
  722. mmMipmap,
  723. mmMipmapGlu);
  724. TglBitmapNormalMapFunc = (
  725. nm4Samples,
  726. nmSobel,
  727. nm3x3,
  728. nm5x5);
  729. ////////////////////////////////////////////////////////////////////////////////////////////////////
  730. EglBitmap = class(Exception);
  731. EglBitmapNotSupported = class(Exception);
  732. EglBitmapSizeToLarge = class(EglBitmap);
  733. EglBitmapNonPowerOfTwo = class(EglBitmap);
  734. EglBitmapUnsupportedFormat = class(EglBitmap)
  735. public
  736. constructor Create(const aFormat: TglBitmapFormat); overload;
  737. constructor Create(const aMsg: String; const aFormat: TglBitmapFormat); overload;
  738. end;
  739. ////////////////////////////////////////////////////////////////////////////////////////////////////
  740. TglBitmapColorRec = packed record
  741. case Integer of
  742. 0: (r, g, b, a: Cardinal);
  743. 1: (arr: array[0..3] of Cardinal);
  744. end;
  745. TglBitmapPixelData = packed record
  746. Data, Range: TglBitmapColorRec;
  747. Format: TglBitmapFormat;
  748. end;
  749. PglBitmapPixelData = ^TglBitmapPixelData;
  750. ////////////////////////////////////////////////////////////////////////////////////////////////////
  751. TglBitmapPixelPositionFields = set of (ffX, ffY);
  752. TglBitmapPixelPosition = record
  753. Fields : TglBitmapPixelPositionFields;
  754. X : Word;
  755. Y : Word;
  756. end;
  757. TglBitmapFormatDescriptor = class(TObject)
  758. protected
  759. function GetIsCompressed: Boolean; virtual; abstract;
  760. function GetHasAlpha: Boolean; virtual; abstract;
  761. function GetglDataFormat: GLenum; virtual; abstract;
  762. function GetglFormat: GLenum; virtual; abstract;
  763. function GetglInternalFormat: GLenum; virtual; abstract;
  764. public
  765. property IsCompressed: Boolean read GetIsCompressed;
  766. property HasAlpha: Boolean read GetHasAlpha;
  767. property glFormat: GLenum read GetglFormat;
  768. property glInternalFormat: GLenum read GetglInternalFormat;
  769. property glDataFormat: GLenum read GetglDataFormat;
  770. end;
  771. ////////////////////////////////////////////////////////////////////////////////////////////////////
  772. TglBitmap = class;
  773. TglBitmapFunctionRec = record
  774. Sender: TglBitmap;
  775. Size: TglBitmapPixelPosition;
  776. Position: TglBitmapPixelPosition;
  777. Source: TglBitmapPixelData;
  778. Dest: TglBitmapPixelData;
  779. Args: Pointer;
  780. end;
  781. TglBitmapFunction = procedure(var FuncRec: TglBitmapFunctionRec);
  782. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  783. TglBitmap = class
  784. private
  785. function GetFormatDesc: TglBitmapFormatDescriptor;
  786. protected
  787. fID: GLuint;
  788. fTarget: GLuint;
  789. fAnisotropic: Integer;
  790. fDeleteTextureOnFree: Boolean;
  791. fFreeDataOnDestroy: Boolean;
  792. fFreeDataAfterGenTexture: Boolean;
  793. fData: PByte;
  794. fIsResident: Boolean;
  795. fBorderColor: array[0..3] of Single;
  796. fDimension: TglBitmapPixelPosition;
  797. fMipMap: TglBitmapMipMap;
  798. fFormat: TglBitmapFormat;
  799. // Mapping
  800. fPixelSize: Integer;
  801. fRowSize: Integer;
  802. // Filtering
  803. fFilterMin: GLenum;
  804. fFilterMag: GLenum;
  805. // TexturWarp
  806. fWrapS: GLenum;
  807. fWrapT: GLenum;
  808. fWrapR: GLenum;
  809. //Swizzle
  810. fSwizzle: array[0..3] of GLenum;
  811. // CustomData
  812. fFilename: String;
  813. fCustomName: String;
  814. fCustomNameW: WideString;
  815. fCustomData: Pointer;
  816. //Getter
  817. function GetWidth: Integer; virtual;
  818. function GetHeight: Integer; virtual;
  819. function GetFileWidth: Integer; virtual;
  820. function GetFileHeight: Integer; virtual;
  821. //Setter
  822. procedure SetCustomData(const aValue: Pointer);
  823. procedure SetCustomName(const aValue: String);
  824. procedure SetCustomNameW(const aValue: WideString);
  825. procedure SetFreeDataOnDestroy(const aValue: Boolean);
  826. procedure SetDeleteTextureOnFree(const aValue: Boolean);
  827. procedure SetFormat(const aValue: TglBitmapFormat);
  828. procedure SetFreeDataAfterGenTexture(const aValue: Boolean);
  829. procedure SetID(const aValue: Cardinal);
  830. procedure SetMipMap(const aValue: TglBitmapMipMap);
  831. procedure SetTarget(const aValue: Cardinal);
  832. procedure SetAnisotropic(const aValue: Integer);
  833. procedure CreateID;
  834. procedure SetupParameters(out aBuildWithGlu: Boolean);
  835. procedure SetDataPointer(var aData: PByte; const aFormat: TglBitmapFormat;
  836. const aWidth: Integer = -1; const aHeight: Integer = -1); virtual; //be careful, aData could be freed by this method
  837. procedure GenTexture(const aTestTextureSize: Boolean = true); virtual; abstract;
  838. function FlipHorz: Boolean; virtual;
  839. function FlipVert: Boolean; virtual;
  840. property Width: Integer read GetWidth;
  841. property Height: Integer read GetHeight;
  842. property FileWidth: Integer read GetFileWidth;
  843. property FileHeight: Integer read GetFileHeight;
  844. public
  845. //Properties
  846. property ID: Cardinal read fID write SetID;
  847. property Target: Cardinal read fTarget write SetTarget;
  848. property Format: TglBitmapFormat read fFormat write SetFormat;
  849. property MipMap: TglBitmapMipMap read fMipMap write SetMipMap;
  850. property Anisotropic: Integer read fAnisotropic write SetAnisotropic;
  851. property FormatDesc: TglBitmapFormatDescriptor read GetFormatDesc;
  852. property Filename: String read fFilename;
  853. property CustomName: String read fCustomName write SetCustomName;
  854. property CustomNameW: WideString read fCustomNameW write SetCustomNameW;
  855. property CustomData: Pointer read fCustomData write SetCustomData;
  856. property DeleteTextureOnFree: Boolean read fDeleteTextureOnFree write SetDeleteTextureOnFree;
  857. property FreeDataOnDestroy: Boolean read fFreeDataOnDestroy write SetFreeDataOnDestroy;
  858. property FreeDataAfterGenTexture: Boolean read fFreeDataAfterGenTexture write SetFreeDataAfterGenTexture;
  859. property Dimension: TglBitmapPixelPosition read fDimension;
  860. property Data: PByte read fData;
  861. property IsResident: Boolean read fIsResident;
  862. procedure AfterConstruction; override;
  863. procedure BeforeDestruction; override;
  864. procedure PrepareResType(var aResource: String; var aResType: PChar);
  865. //Load
  866. procedure LoadFromFile(const aFilename: String);
  867. procedure LoadFromStream(const aStream: TStream); virtual;
  868. procedure LoadFromFunc(const aSize: TglBitmapPixelPosition; const aFunc: TglBitmapFunction;
  869. const aFormat: TglBitmapFormat; const aArgs: Pointer = nil);
  870. procedure LoadFromResource(const aInstance: Cardinal; aResource: String; aResType: PChar = nil);
  871. procedure LoadFromResourceID(const aInstance: Cardinal; const aResourceID: Integer; const aResType: PChar);
  872. //Save
  873. procedure SaveToFile(const aFileName: String; const aFileType: TglBitmapFileType);
  874. procedure SaveToStream(const aStream: TStream; const aFileType: TglBitmapFileType); virtual;
  875. //Convert
  876. function AddFunc(const aFunc: TglBitmapFunction; const aCreateTemp: Boolean; const aArgs: Pointer = nil): Boolean; overload;
  877. function AddFunc(const aSource: TglBitmap; const aFunc: TglBitmapFunction; aCreateTemp: Boolean;
  878. const aFormat: TglBitmapFormat; const aArgs: Pointer = nil): Boolean; overload;
  879. public
  880. //Alpha & Co
  881. {$IFDEF GLB_SDL}
  882. function AssignToSurface(out aSurface: PSDL_Surface): Boolean;
  883. function AssignFromSurface(const aSurface: PSDL_Surface): Boolean;
  884. function AssignAlphaToSurface(out aSurface: PSDL_Surface): Boolean;
  885. function AddAlphaFromSurface(const aSurface: PSDL_Surface; const aFunc: TglBitmapFunction = nil;
  886. const aArgs: Pointer = nil): Boolean;
  887. {$ENDIF}
  888. {$IFDEF GLB_DELPHI}
  889. function AssignToBitmap(const aBitmap: TBitmap): Boolean;
  890. function AssignFromBitmap(const aBitmap: TBitmap): Boolean;
  891. function AssignAlphaToBitmap(const aBitmap: TBitmap): Boolean;
  892. function AddAlphaFromBitmap(const aBitmap: TBitmap; const aFunc: TglBitmapFunction = nil;
  893. const aArgs: Pointer = nil): Boolean;
  894. {$ENDIF}
  895. {$IFDEF GLB_LAZARUS}
  896. function AssignToLazIntfImage(const aImage: TLazIntfImage): Boolean;
  897. function AssignFromLazIntfImage(const aImage: TLazIntfImage): Boolean;
  898. function AssignAlphaToLazIntfImage(const aImage: TLazIntfImage): Boolean;
  899. function AddAlphaFromLazIntfImage(const aImage: TLazIntfImage; const aFunc: TglBitmapFunction = nil;
  900. const aArgs: Pointer = nil): Boolean;
  901. {$ENDIF}
  902. function AddAlphaFromResource(const aInstance: Cardinal; aResource: String; aResType: PChar = nil;
  903. const aFunc: TglBitmapFunction = nil; const aArgs: Pointer = nil): Boolean;
  904. function AddAlphaFromResourceID(const aInstance: Cardinal; const aResourceID: Integer; const aResType: PChar;
  905. const aFunc: TglBitmapFunction = nil; const aArgs: Pointer = nil): Boolean;
  906. function AddAlphaFromFunc(const aFunc: TglBitmapFunction; const aArgs: Pointer = nil): Boolean; virtual;
  907. function AddAlphaFromFile(const aFileName: String; const aFunc: TglBitmapFunction = nil; const aArgs: Pointer = nil): Boolean;
  908. function AddAlphaFromStream(const aStream: TStream; const aFunc: TglBitmapFunction = nil; const aArgs: Pointer = nil): Boolean;
  909. function AddAlphaFromGlBitmap(const aBitmap: TglBitmap; aFunc: TglBitmapFunction = nil; const aArgs: Pointer = nil): Boolean;
  910. function AddAlphaFromColorKey(const aRed, aGreen, aBlue: Byte; const aDeviation: Byte = 0): Boolean;
  911. function AddAlphaFromColorKeyRange(const aRed, aGreen, aBlue: Cardinal; const aDeviation: Cardinal = 0): Boolean;
  912. function AddAlphaFromColorKeyFloat(const aRed, aGreen, aBlue: Single; const aDeviation: Single = 0): Boolean;
  913. function AddAlphaFromValue(const aAlpha: Byte): Boolean;
  914. function AddAlphaFromValueRange(const aAlpha: Cardinal): Boolean;
  915. function AddAlphaFromValueFloat(const aAlpha: Single): Boolean;
  916. function RemoveAlpha: Boolean; virtual;
  917. public
  918. //Common
  919. function Clone: TglBitmap;
  920. function ConvertTo(const aFormat: TglBitmapFormat): Boolean; virtual;
  921. procedure Invert(const aUseRGB: Boolean = true; const aUseAlpha: Boolean = false);
  922. procedure SetBorderColor(const aRed, aGreen, aBlue, aAlpha: Single);
  923. procedure FreeData;
  924. //ColorFill
  925. procedure FillWithColor(const aRed, aGreen, aBlue: Byte; const aAlpha: Byte = 255);
  926. procedure FillWithColorRange(const aRed, aGreen, aBlue: Cardinal; const aAlpha: Cardinal = $FFFFFFFF);
  927. procedure FillWithColorFloat(const aRed, aGreen, aBlue: Single; const aAlpha : Single = 1);
  928. //TexParameters
  929. procedure SetFilter(const aMin, aMag: GLenum);
  930. procedure SetWrap(
  931. const S: GLenum = GL_CLAMP_TO_EDGE;
  932. const T: GLenum = GL_CLAMP_TO_EDGE;
  933. const R: GLenum = GL_CLAMP_TO_EDGE);
  934. procedure SetSwizzle(const r, g, b, a: GLenum);
  935. procedure Bind(const aEnableTextureUnit: Boolean = true); virtual;
  936. procedure Unbind(const aDisableTextureUnit: Boolean = true); virtual;
  937. //Constructors
  938. constructor Create; overload;
  939. constructor Create(const aFileName: String); overload;
  940. constructor Create(const aStream: TStream); overload;
  941. constructor Create(const aSize: TglBitmapPixelPosition; const aFormat: TglBitmapFormat; aData: PByte = nil); overload;
  942. constructor Create(const aSize: TglBitmapPixelPosition; const aFormat: TglBitmapFormat; const aFunc: TglBitmapFunction; const aArgs: Pointer = nil); overload;
  943. constructor Create(const aInstance: Cardinal; const aResource: String; const aResType: PChar = nil); overload;
  944. constructor Create(const aInstance: Cardinal; const aResourceID: Integer; const aResType: PChar); overload;
  945. private
  946. {$IFDEF GLB_SUPPORT_PNG_READ} function LoadPNG(const aStream: TStream): Boolean; virtual; {$ENDIF}
  947. {$ifdef GLB_SUPPORT_PNG_WRITE} procedure SavePNG(const aStream: TStream); virtual; {$ENDIF}
  948. {$IFDEF GLB_SUPPORT_JPEG_READ} function LoadJPEG(const aStream: TStream): Boolean; virtual; {$ENDIF}
  949. {$IFDEF GLB_SUPPORT_JPEG_WRITE} procedure SaveJPEG(const aStream: TStream); virtual; {$ENDIF}
  950. function LoadBMP(const aStream: TStream): Boolean; virtual;
  951. procedure SaveBMP(const aStream: TStream); virtual;
  952. function LoadTGA(const aStream: TStream): Boolean; virtual;
  953. procedure SaveTGA(const aStream: TStream); virtual;
  954. function LoadDDS(const aStream: TStream): Boolean; virtual;
  955. procedure SaveDDS(const aStream: TStream); virtual;
  956. end;
  957. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  958. TglBitmap1D = class(TglBitmap)
  959. protected
  960. procedure SetDataPointer(var aData: PByte; const aFormat: TglBitmapFormat;
  961. const aWidth: Integer = - 1; const aHeight: Integer = - 1); override;
  962. procedure UploadData(const aBuildWithGlu: Boolean);
  963. public
  964. property Width;
  965. procedure AfterConstruction; override;
  966. function FlipHorz: Boolean; override;
  967. procedure GenTexture(const aTestTextureSize: Boolean = true); override;
  968. end;
  969. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  970. TglBitmap2D = class(TglBitmap)
  971. protected
  972. fLines: array of PByte;
  973. function GetScanline(const aIndex: Integer): Pointer;
  974. procedure SetDataPointer(var aData: PByte; const aFormat: TglBitmapFormat;
  975. const aWidth: Integer = - 1; const aHeight: Integer = - 1); override;
  976. procedure UploadData(const aTarget: GLenum; const aBuildWithGlu: Boolean);
  977. public
  978. property Width;
  979. property Height;
  980. property Scanline[const aIndex: Integer]: Pointer read GetScanline;
  981. procedure AfterConstruction; override;
  982. procedure GrabScreen(const aTop, aLeft, aRight, aBottom: Integer; const aFormat: TglBitmapFormat);
  983. procedure GetDataFromTexture;
  984. procedure GenTexture(const aTestTextureSize: Boolean = true); override;
  985. function FlipHorz: Boolean; override;
  986. function FlipVert: Boolean; override;
  987. procedure ToNormalMap(const aFunc: TglBitmapNormalMapFunc = nm3x3;
  988. const aScale: Single = 2; const aUseAlpha: Boolean = false);
  989. end;
  990. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  991. TglBitmapCubeMap = class(TglBitmap2D)
  992. protected
  993. fGenMode: Integer;
  994. procedure GenTexture(const aTestTextureSize: Boolean = true); reintroduce;
  995. public
  996. procedure AfterConstruction; override;
  997. procedure GenerateCubeMap(const aCubeTarget: Cardinal; const aTestTextureSize: Boolean = true);
  998. procedure Bind(const aEnableTexCoordsGen: Boolean = true; const aEnableTextureUnit: Boolean = true); reintroduce; virtual;
  999. procedure Unbind(const aDisableTexCoordsGen: Boolean = true; const aDisableTextureUnit: Boolean = true); reintroduce; virtual;
  1000. end;
  1001. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1002. TglBitmapNormalMap = class(TglBitmapCubeMap)
  1003. public
  1004. procedure AfterConstruction; override;
  1005. procedure GenerateNormalMap(const aSize: Integer = 32; const aTestTextureSize: Boolean = true);
  1006. end;
  1007. const
  1008. NULL_SIZE: TglBitmapPixelPosition = (Fields: []; X: 0; Y: 0);
  1009. procedure glBitmapSetDefaultDeleteTextureOnFree(const aDeleteTextureOnFree: Boolean);
  1010. procedure glBitmapSetDefaultFreeDataAfterGenTexture(const aFreeData: Boolean);
  1011. procedure glBitmapSetDefaultMipmap(const aValue: TglBitmapMipMap);
  1012. procedure glBitmapSetDefaultFormat(const aFormat: TglBitmapFormat);
  1013. procedure glBitmapSetDefaultFilter(const aMin, aMag: Integer);
  1014. procedure glBitmapSetDefaultWrap(
  1015. const S: Cardinal = GL_CLAMP_TO_EDGE;
  1016. const T: Cardinal = GL_CLAMP_TO_EDGE;
  1017. const R: Cardinal = GL_CLAMP_TO_EDGE);
  1018. function glBitmapGetDefaultDeleteTextureOnFree: Boolean;
  1019. function glBitmapGetDefaultFreeDataAfterGenTexture: Boolean;
  1020. function glBitmapGetDefaultMipmap: TglBitmapMipMap;
  1021. function glBitmapGetDefaultFormat: TglBitmapFormat;
  1022. procedure glBitmapGetDefaultFilter(var aMin, aMag: Cardinal);
  1023. procedure glBitmapGetDefaultTextureWrap(var S, T, R: Cardinal);
  1024. function glBitmapPosition(X: Integer = -1; Y: Integer = -1): TglBitmapPixelPosition;
  1025. function glBitmapColorRec(const r, g, b, a: Cardinal): TglBitmapColorRec;
  1026. function glBitmapColorRecCmp(const r1, r2: TglBitmapColorRec): Boolean;
  1027. var
  1028. glBitmapDefaultDeleteTextureOnFree: Boolean;
  1029. glBitmapDefaultFreeDataAfterGenTextures: Boolean;
  1030. glBitmapDefaultFormat: TglBitmapFormat;
  1031. glBitmapDefaultMipmap: TglBitmapMipMap;
  1032. glBitmapDefaultFilterMin: Cardinal;
  1033. glBitmapDefaultFilterMag: Cardinal;
  1034. glBitmapDefaultWrapS: Cardinal;
  1035. glBitmapDefaultWrapT: Cardinal;
  1036. glBitmapDefaultWrapR: Cardinal;
  1037. glDefaultSwizzle: array[0..3] of GLenum;
  1038. {$IFDEF GLB_DELPHI}
  1039. function CreateGrayPalette: HPALETTE;
  1040. {$ENDIF}
  1041. implementation
  1042. uses
  1043. Math, syncobjs, typinfo
  1044. {$IFDEF GLB_DELPHI}, Types{$ENDIF};
  1045. type
  1046. {$IFNDEF fpc}
  1047. QWord = System.UInt64;
  1048. PQWord = ^QWord;
  1049. PtrInt = Longint;
  1050. PtrUInt = DWord;
  1051. {$ENDIF}
  1052. ////////////////////////////////////////////////////////////////////////////////////////////////////
  1053. TShiftRec = packed record
  1054. case Integer of
  1055. 0: (r, g, b, a: Byte);
  1056. 1: (arr: array[0..3] of Byte);
  1057. end;
  1058. TFormatDescriptor = class(TglBitmapFormatDescriptor)
  1059. private
  1060. function GetRedMask: QWord;
  1061. function GetGreenMask: QWord;
  1062. function GetBlueMask: QWord;
  1063. function GetAlphaMask: QWord;
  1064. protected
  1065. fFormat: TglBitmapFormat;
  1066. fWithAlpha: TglBitmapFormat;
  1067. fWithoutAlpha: TglBitmapFormat;
  1068. fRGBInverted: TglBitmapFormat;
  1069. fUncompressed: TglBitmapFormat;
  1070. fPixelSize: Single;
  1071. fIsCompressed: Boolean;
  1072. fRange: TglBitmapColorRec;
  1073. fShift: TShiftRec;
  1074. fglFormat: GLenum;
  1075. fglInternalFormat: GLenum;
  1076. fglDataFormat: GLenum;
  1077. function GetIsCompressed: Boolean; override;
  1078. function GetHasAlpha: Boolean; override;
  1079. function GetglFormat: GLenum; override;
  1080. function GetglInternalFormat: GLenum; override;
  1081. function GetglDataFormat: GLenum; override;
  1082. function GetComponents: Integer; virtual;
  1083. public
  1084. property Format: TglBitmapFormat read fFormat;
  1085. property WithAlpha: TglBitmapFormat read fWithAlpha;
  1086. property WithoutAlpha: TglBitmapFormat read fWithoutAlpha;
  1087. property RGBInverted: TglBitmapFormat read fRGBInverted;
  1088. property Components: Integer read GetComponents;
  1089. property PixelSize: Single read fPixelSize;
  1090. property Range: TglBitmapColorRec read fRange;
  1091. property Shift: TShiftRec read fShift;
  1092. property RedMask: QWord read GetRedMask;
  1093. property GreenMask: QWord read GetGreenMask;
  1094. property BlueMask: QWord read GetBlueMask;
  1095. property AlphaMask: QWord read GetAlphaMask;
  1096. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); virtual; abstract;
  1097. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); virtual; abstract;
  1098. function GetSize(const aSize: TglBitmapPixelPosition): Integer; overload; virtual;
  1099. function GetSize(const aWidth, aHeight: Integer): Integer; overload; virtual;
  1100. function CreateMappingData: Pointer; virtual;
  1101. procedure FreeMappingData(var aMappingData: Pointer); virtual;
  1102. function IsEmpty: Boolean; virtual;
  1103. function MaskMatch(const aRedMask, aGreenMask, aBlueMask, aAlphaMask: QWord): Boolean; virtual;
  1104. procedure PreparePixel(out aPixel: TglBitmapPixelData); virtual;
  1105. constructor Create; virtual;
  1106. public
  1107. class procedure Init;
  1108. class function Get(const aFormat: TglBitmapFormat): TFormatDescriptor;
  1109. class function GetWithAlpha(const aFormat: TglBitmapFormat): TFormatDescriptor;
  1110. class procedure Clear;
  1111. class procedure Finalize;
  1112. end;
  1113. TFormatDescriptorClass = class of TFormatDescriptor;
  1114. TfdEmpty = class(TFormatDescriptor);
  1115. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1116. TfdAlpha_UB1 = class(TFormatDescriptor) //1* unsigned byte
  1117. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1118. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1119. constructor Create; override;
  1120. end;
  1121. TfdLuminance_UB1 = class(TFormatDescriptor) //1* unsigned byte
  1122. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1123. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1124. constructor Create; override;
  1125. end;
  1126. TfdUniversal_UB1 = class(TFormatDescriptor) //1* unsigned byte
  1127. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1128. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1129. constructor Create; override;
  1130. end;
  1131. TfdLuminanceAlpha_UB2 = class(TfdLuminance_UB1) //2* unsigned byte
  1132. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1133. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1134. constructor Create; override;
  1135. end;
  1136. TfdRGB_UB3 = class(TFormatDescriptor) //3* unsigned byte
  1137. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1138. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1139. constructor Create; override;
  1140. end;
  1141. TfdBGR_UB3 = class(TFormatDescriptor) //3* unsigned byte (inverse)
  1142. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1143. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1144. constructor Create; override;
  1145. end;
  1146. TfdRGBA_UB4 = class(TfdRGB_UB3) //4* unsigned byte
  1147. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1148. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1149. constructor Create; override;
  1150. end;
  1151. TfdBGRA_UB4 = class(TfdBGR_UB3) //4* unsigned byte (inverse)
  1152. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1153. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1154. constructor Create; override;
  1155. end;
  1156. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1157. TfdAlpha_US1 = class(TFormatDescriptor) //1* unsigned short
  1158. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1159. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1160. constructor Create; override;
  1161. end;
  1162. TfdLuminance_US1 = class(TFormatDescriptor) //1* unsigned short
  1163. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1164. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1165. constructor Create; override;
  1166. end;
  1167. TfdUniversal_US1 = class(TFormatDescriptor) //1* unsigned short
  1168. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1169. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1170. constructor Create; override;
  1171. end;
  1172. TfdDepth_US1 = class(TFormatDescriptor) //1* unsigned short
  1173. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1174. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1175. constructor Create; override;
  1176. end;
  1177. TfdLuminanceAlpha_US2 = class(TfdLuminance_US1) //2* unsigned short
  1178. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1179. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1180. constructor Create; override;
  1181. end;
  1182. TfdRGB_US3 = class(TFormatDescriptor) //3* unsigned short
  1183. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1184. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1185. constructor Create; override;
  1186. end;
  1187. TfdBGR_US3 = class(TFormatDescriptor) //3* unsigned short (inverse)
  1188. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1189. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1190. constructor Create; override;
  1191. end;
  1192. TfdRGBA_US4 = class(TfdRGB_US3) //4* unsigned short
  1193. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1194. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1195. constructor Create; override;
  1196. end;
  1197. TfdBGRA_US4 = class(TfdBGR_US3) //4* unsigned short (inverse)
  1198. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1199. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1200. constructor Create; override;
  1201. end;
  1202. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1203. TfdUniversal_UI1 = class(TFormatDescriptor) //1* unsigned int
  1204. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1205. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1206. constructor Create; override;
  1207. end;
  1208. TfdDepth_UI1 = class(TFormatDescriptor) //1* unsigned int
  1209. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1210. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1211. constructor Create; override;
  1212. end;
  1213. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1214. TfdAlpha4 = class(TfdAlpha_UB1)
  1215. constructor Create; override;
  1216. end;
  1217. TfdAlpha8 = class(TfdAlpha_UB1)
  1218. constructor Create; override;
  1219. end;
  1220. TfdAlpha12 = class(TfdAlpha_US1)
  1221. constructor Create; override;
  1222. end;
  1223. TfdAlpha16 = class(TfdAlpha_US1)
  1224. constructor Create; override;
  1225. end;
  1226. TfdLuminance4 = class(TfdLuminance_UB1)
  1227. constructor Create; override;
  1228. end;
  1229. TfdLuminance8 = class(TfdLuminance_UB1)
  1230. constructor Create; override;
  1231. end;
  1232. TfdLuminance12 = class(TfdLuminance_US1)
  1233. constructor Create; override;
  1234. end;
  1235. TfdLuminance16 = class(TfdLuminance_US1)
  1236. constructor Create; override;
  1237. end;
  1238. TfdLuminance4Alpha4 = class(TfdLuminanceAlpha_UB2)
  1239. constructor Create; override;
  1240. end;
  1241. TfdLuminance6Alpha2 = class(TfdLuminanceAlpha_UB2)
  1242. constructor Create; override;
  1243. end;
  1244. TfdLuminance8Alpha8 = class(TfdLuminanceAlpha_UB2)
  1245. constructor Create; override;
  1246. end;
  1247. TfdLuminance12Alpha4 = class(TfdLuminanceAlpha_US2)
  1248. constructor Create; override;
  1249. end;
  1250. TfdLuminance12Alpha12 = class(TfdLuminanceAlpha_US2)
  1251. constructor Create; override;
  1252. end;
  1253. TfdLuminance16Alpha16 = class(TfdLuminanceAlpha_US2)
  1254. constructor Create; override;
  1255. end;
  1256. TfdR3G3B2 = class(TfdUniversal_UB1)
  1257. constructor Create; override;
  1258. end;
  1259. TfdRGB4 = class(TfdUniversal_US1)
  1260. constructor Create; override;
  1261. end;
  1262. TfdR5G6B5 = class(TfdUniversal_US1)
  1263. constructor Create; override;
  1264. end;
  1265. TfdRGB5 = class(TfdUniversal_US1)
  1266. constructor Create; override;
  1267. end;
  1268. TfdRGB8 = class(TfdRGB_UB3)
  1269. constructor Create; override;
  1270. end;
  1271. TfdRGB10 = class(TfdUniversal_UI1)
  1272. constructor Create; override;
  1273. end;
  1274. TfdRGB12 = class(TfdRGB_US3)
  1275. constructor Create; override;
  1276. end;
  1277. TfdRGB16 = class(TfdRGB_US3)
  1278. constructor Create; override;
  1279. end;
  1280. TfdRGBA2 = class(TfdRGBA_UB4)
  1281. constructor Create; override;
  1282. end;
  1283. TfdRGBA4 = class(TfdUniversal_US1)
  1284. constructor Create; override;
  1285. end;
  1286. TfdRGB5A1 = class(TfdUniversal_US1)
  1287. constructor Create; override;
  1288. end;
  1289. TfdRGBA8 = class(TfdRGBA_UB4)
  1290. constructor Create; override;
  1291. end;
  1292. TfdRGB10A2 = class(TfdUniversal_UI1)
  1293. constructor Create; override;
  1294. end;
  1295. TfdRGBA12 = class(TfdRGBA_US4)
  1296. constructor Create; override;
  1297. end;
  1298. TfdRGBA16 = class(TfdRGBA_US4)
  1299. constructor Create; override;
  1300. end;
  1301. TfdBGR4 = class(TfdUniversal_US1)
  1302. constructor Create; override;
  1303. end;
  1304. TfdB5G6R5 = class(TfdUniversal_US1)
  1305. constructor Create; override;
  1306. end;
  1307. TfdBGR5 = class(TfdUniversal_US1)
  1308. constructor Create; override;
  1309. end;
  1310. TfdBGR8 = class(TfdBGR_UB3)
  1311. constructor Create; override;
  1312. end;
  1313. TfdBGR10 = class(TfdUniversal_UI1)
  1314. constructor Create; override;
  1315. end;
  1316. TfdBGR12 = class(TfdBGR_US3)
  1317. constructor Create; override;
  1318. end;
  1319. TfdBGR16 = class(TfdBGR_US3)
  1320. constructor Create; override;
  1321. end;
  1322. TfdBGRA2 = class(TfdBGRA_UB4)
  1323. constructor Create; override;
  1324. end;
  1325. TfdBGRA4 = class(TfdUniversal_US1)
  1326. constructor Create; override;
  1327. end;
  1328. TfdBGR5A1 = class(TfdUniversal_US1)
  1329. constructor Create; override;
  1330. end;
  1331. TfdBGRA8 = class(TfdBGRA_UB4)
  1332. constructor Create; override;
  1333. end;
  1334. TfdBGR10A2 = class(TfdUniversal_UI1)
  1335. constructor Create; override;
  1336. end;
  1337. TfdBGRA12 = class(TfdBGRA_US4)
  1338. constructor Create; override;
  1339. end;
  1340. TfdBGRA16 = class(TfdBGRA_US4)
  1341. constructor Create; override;
  1342. end;
  1343. TfdDepth16 = class(TfdDepth_US1)
  1344. constructor Create; override;
  1345. end;
  1346. TfdDepth24 = class(TfdDepth_UI1)
  1347. constructor Create; override;
  1348. end;
  1349. TfdDepth32 = class(TfdDepth_UI1)
  1350. constructor Create; override;
  1351. end;
  1352. TfdS3tcDtx1RGBA = class(TFormatDescriptor)
  1353. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1354. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1355. constructor Create; override;
  1356. end;
  1357. TfdS3tcDtx3RGBA = class(TFormatDescriptor)
  1358. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1359. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1360. constructor Create; override;
  1361. end;
  1362. TfdS3tcDtx5RGBA = class(TFormatDescriptor)
  1363. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1364. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1365. constructor Create; override;
  1366. end;
  1367. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1368. TbmpBitfieldFormat = class(TFormatDescriptor)
  1369. private
  1370. procedure SetRedMask (const aValue: QWord);
  1371. procedure SetGreenMask(const aValue: QWord);
  1372. procedure SetBlueMask (const aValue: QWord);
  1373. procedure SetAlphaMask(const aValue: QWord);
  1374. procedure Update(aMask: QWord; out aRange: Cardinal; out aShift: Byte);
  1375. public
  1376. property RedMask: QWord read GetRedMask write SetRedMask;
  1377. property GreenMask: QWord read GetGreenMask write SetGreenMask;
  1378. property BlueMask: QWord read GetBlueMask write SetBlueMask;
  1379. property AlphaMask: QWord read GetAlphaMask write SetAlphaMask;
  1380. property PixelSize: Single read fPixelSize write fPixelSize;
  1381. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1382. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1383. end;
  1384. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1385. TbmpColorTableEnty = packed record
  1386. b, g, r, a: Byte;
  1387. end;
  1388. TbmpColorTable = array of TbmpColorTableEnty;
  1389. TbmpColorTableFormat = class(TFormatDescriptor)
  1390. private
  1391. fColorTable: TbmpColorTable;
  1392. public
  1393. property PixelSize: Single read fPixelSize write fPixelSize;
  1394. property ColorTable: TbmpColorTable read fColorTable write fColorTable;
  1395. property Range: TglBitmapColorRec read fRange write fRange;
  1396. property Shift: TShiftRec read fShift write fShift;
  1397. property Format: TglBitmapFormat read fFormat write fFormat;
  1398. procedure CreateColorTable;
  1399. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1400. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1401. destructor Destroy; override;
  1402. end;
  1403. const
  1404. LUMINANCE_WEIGHT_R = 0.30;
  1405. LUMINANCE_WEIGHT_G = 0.59;
  1406. LUMINANCE_WEIGHT_B = 0.11;
  1407. ALPHA_WEIGHT_R = 0.30;
  1408. ALPHA_WEIGHT_G = 0.59;
  1409. ALPHA_WEIGHT_B = 0.11;
  1410. DEPTH_WEIGHT_R = 0.333333333;
  1411. DEPTH_WEIGHT_G = 0.333333333;
  1412. DEPTH_WEIGHT_B = 0.333333333;
  1413. UNSUPPORTED_FORMAT = 'the given format isn''t supported by this function.';
  1414. FORMAT_DESCRIPTOR_CLASSES: array[TglBitmapFormat] of TFormatDescriptorClass = (
  1415. TfdEmpty,
  1416. TfdAlpha4,
  1417. TfdAlpha8,
  1418. TfdAlpha12,
  1419. TfdAlpha16,
  1420. TfdLuminance4,
  1421. TfdLuminance8,
  1422. TfdLuminance12,
  1423. TfdLuminance16,
  1424. TfdLuminance4Alpha4,
  1425. TfdLuminance6Alpha2,
  1426. TfdLuminance8Alpha8,
  1427. TfdLuminance12Alpha4,
  1428. TfdLuminance12Alpha12,
  1429. TfdLuminance16Alpha16,
  1430. TfdR3G3B2,
  1431. TfdRGB4,
  1432. TfdR5G6B5,
  1433. TfdRGB5,
  1434. TfdRGB8,
  1435. TfdRGB10,
  1436. TfdRGB12,
  1437. TfdRGB16,
  1438. TfdRGBA2,
  1439. TfdRGBA4,
  1440. TfdRGB5A1,
  1441. TfdRGBA8,
  1442. TfdRGB10A2,
  1443. TfdRGBA12,
  1444. TfdRGBA16,
  1445. TfdBGR4,
  1446. TfdB5G6R5,
  1447. TfdBGR5,
  1448. TfdBGR8,
  1449. TfdBGR10,
  1450. TfdBGR12,
  1451. TfdBGR16,
  1452. TfdBGRA2,
  1453. TfdBGRA4,
  1454. TfdBGR5A1,
  1455. TfdBGRA8,
  1456. TfdBGR10A2,
  1457. TfdBGRA12,
  1458. TfdBGRA16,
  1459. TfdDepth16,
  1460. TfdDepth24,
  1461. TfdDepth32,
  1462. TfdS3tcDtx1RGBA,
  1463. TfdS3tcDtx3RGBA,
  1464. TfdS3tcDtx5RGBA
  1465. );
  1466. var
  1467. FormatDescriptorCS: TCriticalSection;
  1468. FormatDescriptors: array[TglBitmapFormat] of TFormatDescriptor;
  1469. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1470. constructor EglBitmapUnsupportedFormat.Create(const aFormat: TglBitmapFormat);
  1471. begin
  1472. inherited Create('unsupported format: ' + GetEnumName(TypeInfo(TglBitmapFormat), Integer(aFormat)));
  1473. end;
  1474. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1475. constructor EglBitmapUnsupportedFormat.Create(const aMsg: String; const aFormat: TglBitmapFormat);
  1476. begin
  1477. inherited Create(aMsg + GetEnumName(TypeInfo(TglBitmapFormat), Integer(aFormat)));
  1478. end;
  1479. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1480. function glBitmapPosition(X, Y: Integer): TglBitmapPixelPosition;
  1481. begin
  1482. result.Fields := [];
  1483. if X >= 0 then
  1484. result.Fields := result.Fields + [ffX];
  1485. if Y >= 0 then
  1486. result.Fields := result.Fields + [ffY];
  1487. result.X := Max(0, X);
  1488. result.Y := Max(0, Y);
  1489. end;
  1490. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1491. function glBitmapColorRec(const r, g, b, a: Cardinal): TglBitmapColorRec;
  1492. begin
  1493. result.r := r;
  1494. result.g := g;
  1495. result.b := b;
  1496. result.a := a;
  1497. end;
  1498. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1499. function glBitmapColorRecCmp(const r1, r2: TglBitmapColorRec): Boolean;
  1500. var
  1501. i: Integer;
  1502. begin
  1503. result := false;
  1504. for i := 0 to high(r1.arr) do
  1505. if (r1.arr[i] <> r2.arr[i]) then
  1506. exit;
  1507. result := true;
  1508. end;
  1509. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1510. function glBitmapShiftRec(const r, g, b, a: Byte): TShiftRec;
  1511. begin
  1512. result.r := r;
  1513. result.g := g;
  1514. result.b := b;
  1515. result.a := a;
  1516. end;
  1517. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1518. function FormatGetSupportedFiles(const aFormat: TglBitmapFormat): TglBitmapFileTypes;
  1519. begin
  1520. result := [];
  1521. if (aFormat in [
  1522. //4 bbp
  1523. tfLuminance4,
  1524. //8bpp
  1525. tfR3G3B2, tfLuminance8,
  1526. //16bpp
  1527. tfRGB4, tfRGB5, tfR5G6B5, tfRGB5A1, tfRGBA4,
  1528. tfBGR4, tfBGR5, tfB5G6R5, tfBGR5A1, tfBGRA4,
  1529. //24bpp
  1530. tfBGR8, tfRGB8,
  1531. //32bpp
  1532. tfRGB10, tfRGB10A2, tfRGBA8,
  1533. tfBGR10, tfBGR10A2, tfBGRA8]) then
  1534. result := result + [ftBMP];
  1535. if (aFormat in [
  1536. //8 bpp
  1537. tfLuminance8, tfAlpha8,
  1538. //16 bpp
  1539. tfLuminance16, tfLuminance8Alpha8,
  1540. tfRGB5, tfRGB5A1, tfRGBA4,
  1541. tfBGR5, tfBGR5A1, tfBGRA4,
  1542. //24 bpp
  1543. tfRGB8, tfBGR8,
  1544. //32 bpp
  1545. tfRGB10A2, tfRGBA8, tfBGR10A2, tfBGRA8]) then
  1546. result := result + [ftTGA];
  1547. if (aFormat in [
  1548. //8 bpp
  1549. tfAlpha8, tfLuminance8, tfLuminance4Alpha4, tfLuminance6Alpha2,
  1550. tfR3G3B2, tfRGBA2, tfBGRA2,
  1551. //16 bpp
  1552. tfAlpha16, tfLuminance16, tfLuminance8Alpha8, tfLuminance12Alpha4,
  1553. tfRGB4, tfR5G6B5, tfRGB5, tfRGBA4, tfRGB5A1,
  1554. tfBGR4, tfB5G6R5, tfBGR5, tfBGRA4, tfBGR5A1,
  1555. //24 bpp
  1556. tfRGB8, tfBGR8,
  1557. //32 bbp
  1558. tfLuminance16Alpha16,
  1559. tfRGBA8, tfRGB10A2,
  1560. tfBGRA8, tfBGR10A2,
  1561. //compressed
  1562. tfS3tcDtx1RGBA, tfS3tcDtx3RGBA, tfS3tcDtx5RGBA]) then
  1563. result := result + [ftDDS];
  1564. {$IFDEF GLB_SUPPORT_PNG_WRITE}
  1565. if aFormat in [
  1566. tfAlpha8, tfLuminance8, tfLuminance8Alpha8,
  1567. tfRGB8, tfRGBA8,
  1568. tfBGR8, tfBGRA8] then
  1569. result := result + [ftPNG];
  1570. {$ENDIF}
  1571. {$IFDEF GLB_SUPPORT_JPEG_WRITE}
  1572. if aFormat in [tfAlpha8, tfLuminance8, tfRGB8, tfBGR8] then
  1573. result := result + [ftJPEG];
  1574. {$ENDIF}
  1575. end;
  1576. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1577. function IsPowerOfTwo(aNumber: Integer): Boolean;
  1578. begin
  1579. while (aNumber and 1) = 0 do
  1580. aNumber := aNumber shr 1;
  1581. result := aNumber = 1;
  1582. end;
  1583. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1584. function GetTopMostBit(aBitSet: QWord): Integer;
  1585. begin
  1586. result := 0;
  1587. while aBitSet > 0 do begin
  1588. inc(result);
  1589. aBitSet := aBitSet shr 1;
  1590. end;
  1591. end;
  1592. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1593. function CountSetBits(aBitSet: QWord): Integer;
  1594. begin
  1595. result := 0;
  1596. while aBitSet > 0 do begin
  1597. if (aBitSet and 1) = 1 then
  1598. inc(result);
  1599. aBitSet := aBitSet shr 1;
  1600. end;
  1601. end;
  1602. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1603. function LuminanceWeight(const aPixel: TglBitmapPixelData): Cardinal;
  1604. begin
  1605. result := Trunc(
  1606. LUMINANCE_WEIGHT_R * aPixel.Data.r +
  1607. LUMINANCE_WEIGHT_G * aPixel.Data.g +
  1608. LUMINANCE_WEIGHT_B * aPixel.Data.b);
  1609. end;
  1610. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1611. function DepthWeight(const aPixel: TglBitmapPixelData): Cardinal;
  1612. begin
  1613. result := Trunc(
  1614. DEPTH_WEIGHT_R * aPixel.Data.r +
  1615. DEPTH_WEIGHT_G * aPixel.Data.g +
  1616. DEPTH_WEIGHT_B * aPixel.Data.b);
  1617. end;
  1618. {$IFDEF GLB_NATIVE_OGL}
  1619. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1620. //OpenGLInitialization///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1621. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1622. var
  1623. GL_LibHandle: Pointer = nil;
  1624. function glbGetProcAddress(aProcName: PAnsiChar; aLibHandle: Pointer = nil; const aRaiseOnErr: Boolean = true): Pointer;
  1625. begin
  1626. if not Assigned(aLibHandle) then
  1627. aLibHandle := GL_LibHandle;
  1628. {$IF DEFINED(GLB_WIN)}
  1629. result := GetProcAddress({%H-}HMODULE(aLibHandle), aProcName);
  1630. if Assigned(result) then
  1631. exit;
  1632. if Assigned(wglGetProcAddress) then
  1633. result := wglGetProcAddress(aProcName);
  1634. {$ELSEIF DEFINED(GLB_LINUX)}
  1635. if Assigned(glXGetProcAddress) then begin
  1636. result := glXGetProcAddress(aProcName);
  1637. if Assigned(result) then
  1638. exit;
  1639. end;
  1640. if Assigned(glXGetProcAddressARB) then begin
  1641. result := glXGetProcAddressARB(aProcName);
  1642. if Assigned(result) then
  1643. exit;
  1644. end;
  1645. result := dlsym(aLibHandle, aProcName);
  1646. {$IFEND}
  1647. if not Assigned(result) and aRaiseOnErr then
  1648. raise EglBitmap.Create('unable to load procedure form library: ' + aProcName);
  1649. end;
  1650. {$IFDEF GLB_NATIVE_OGL_DYNAMIC}
  1651. var
  1652. GLU_LibHandle: Pointer = nil;
  1653. OpenGLInitialized: Boolean;
  1654. InitOpenGLCS: TCriticalSection;
  1655. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1656. procedure glbInitOpenGL;
  1657. ////////////////////////////////////////////////////////////////////////////////
  1658. function glbLoadLibrary(const aName: PChar): Pointer;
  1659. begin
  1660. {$IF DEFINED(GLB_WIN)}
  1661. result := {%H-}Pointer(LoadLibrary(aName));
  1662. {$ELSEIF DEFINED(GLB_LINUX)}
  1663. result := dlopen(Name, RTLD_LAZY);
  1664. {$ELSE}
  1665. result := nil;
  1666. {$IFEND}
  1667. end;
  1668. ////////////////////////////////////////////////////////////////////////////////
  1669. function glbFreeLibrary(const aLibHandle: Pointer): Boolean;
  1670. begin
  1671. result := false;
  1672. if not Assigned(aLibHandle) then
  1673. exit;
  1674. {$IF DEFINED(GLB_WIN)}
  1675. Result := FreeLibrary({%H-}HINST(aLibHandle));
  1676. {$ELSEIF DEFINED(GLB_LINUX)}
  1677. Result := dlclose(aLibHandle) = 0;
  1678. {$IFEND}
  1679. end;
  1680. begin
  1681. if Assigned(GL_LibHandle) then
  1682. glbFreeLibrary(GL_LibHandle);
  1683. if Assigned(GLU_LibHandle) then
  1684. glbFreeLibrary(GLU_LibHandle);
  1685. GL_LibHandle := glbLoadLibrary(libopengl);
  1686. if not Assigned(GL_LibHandle) then
  1687. raise EglBitmap.Create('unable to load library: ' + libopengl);
  1688. GLU_LibHandle := glbLoadLibrary(libglu);
  1689. if not Assigned(GLU_LibHandle) then
  1690. raise EglBitmap.Create('unable to load library: ' + libglu);
  1691. {$IF DEFINED(GLB_WIN)}
  1692. wglGetProcAddress := glbGetProcAddress('wglGetProcAddress');
  1693. {$ELSEIF DEFINED(GLB_LINUX)}
  1694. glXGetProcAddress := glbGetProcAddress('glXGetProcAddress');
  1695. glXGetProcAddressARB := glbGetProcAddress('glXGetProcAddressARB');
  1696. {$IFEND}
  1697. glEnable := glbGetProcAddress('glEnable');
  1698. glDisable := glbGetProcAddress('glDisable');
  1699. glGetString := glbGetProcAddress('glGetString');
  1700. glGetIntegerv := glbGetProcAddress('glGetIntegerv');
  1701. glTexParameteri := glbGetProcAddress('glTexParameteri');
  1702. glTexParameteriv := glbGetProcAddress('glTexParameteriv');
  1703. glTexParameterfv := glbGetProcAddress('glTexParameterfv');
  1704. glGetTexParameteriv := glbGetProcAddress('glGetTexParameteriv');
  1705. glGetTexParameterfv := glbGetProcAddress('glGetTexParameterfv');
  1706. glGetTexLevelParameteriv := glbGetProcAddress('glGetTexLevelParameteriv');
  1707. glGetTexLevelParameterfv := glbGetProcAddress('glGetTexLevelParameterfv');
  1708. glTexGeni := glbGetProcAddress('glTexGeni');
  1709. glGenTextures := glbGetProcAddress('glGenTextures');
  1710. glBindTexture := glbGetProcAddress('glBindTexture');
  1711. glDeleteTextures := glbGetProcAddress('glDeleteTextures');
  1712. glAreTexturesResident := glbGetProcAddress('glAreTexturesResident');
  1713. glReadPixels := glbGetProcAddress('glReadPixels');
  1714. glPixelStorei := glbGetProcAddress('glPixelStorei');
  1715. glTexImage1D := glbGetProcAddress('glTexImage1D');
  1716. glTexImage2D := glbGetProcAddress('glTexImage2D');
  1717. glGetTexImage := glbGetProcAddress('glGetTexImage');
  1718. gluBuild1DMipmaps := glbGetProcAddress('gluBuild1DMipmaps', GLU_LibHandle);
  1719. gluBuild2DMipmaps := glbGetProcAddress('gluBuild2DMipmaps', GLU_LibHandle);
  1720. end;
  1721. {$ENDIF}
  1722. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1723. procedure glbReadOpenGLExtensions;
  1724. var
  1725. Buffer: AnsiString;
  1726. MajorVersion, MinorVersion: Integer;
  1727. ///////////////////////////////////////////////////////////////////////////////////////////
  1728. procedure TrimVersionString(aBuffer: AnsiString; out aMajor, aMinor: Integer);
  1729. var
  1730. Separator: Integer;
  1731. begin
  1732. aMinor := 0;
  1733. aMajor := 0;
  1734. Separator := Pos(AnsiString('.'), aBuffer);
  1735. if (Separator > 1) and (Separator < Length(aBuffer)) and
  1736. (aBuffer[Separator - 1] in ['0'..'9']) and
  1737. (aBuffer[Separator + 1] in ['0'..'9']) then begin
  1738. Dec(Separator);
  1739. while (Separator > 0) and (aBuffer[Separator] in ['0'..'9']) do
  1740. Dec(Separator);
  1741. Delete(aBuffer, 1, Separator);
  1742. Separator := Pos(AnsiString('.'), aBuffer) + 1;
  1743. while (Separator <= Length(aBuffer)) and (AnsiChar(aBuffer[Separator]) in ['0'..'9']) do
  1744. Inc(Separator);
  1745. Delete(aBuffer, Separator, 255);
  1746. Separator := Pos(AnsiString('.'), aBuffer);
  1747. aMajor := StrToInt(Copy(String(aBuffer), 1, Separator - 1));
  1748. aMinor := StrToInt(Copy(String(aBuffer), Separator + 1, 1));
  1749. end;
  1750. end;
  1751. ///////////////////////////////////////////////////////////////////////////////////////////
  1752. function CheckExtension(const Extension: AnsiString): Boolean;
  1753. var
  1754. ExtPos: Integer;
  1755. begin
  1756. ExtPos := Pos(Extension, Buffer);
  1757. result := ExtPos > 0;
  1758. if result then
  1759. result := ((ExtPos + Length(Extension) - 1) = Length(Buffer)) or not (Buffer[ExtPos + Length(Extension)] in ['_', 'A'..'Z', 'a'..'z']);
  1760. end;
  1761. ///////////////////////////////////////////////////////////////////////////////////////////
  1762. function CheckVersion(const aMajor, aMinor: Integer): Boolean;
  1763. begin
  1764. result := (MajorVersion > aMajor) or ((MajorVersion = aMajor) and (MinorVersion >= aMinor));
  1765. end;
  1766. begin
  1767. {$IFDEF GLB_NATIVE_OGL_DYNAMIC}
  1768. InitOpenGLCS.Enter;
  1769. try
  1770. if not OpenGLInitialized then begin
  1771. glbInitOpenGL;
  1772. OpenGLInitialized := true;
  1773. end;
  1774. finally
  1775. InitOpenGLCS.Leave;
  1776. end;
  1777. {$ENDIF}
  1778. // Version
  1779. Buffer := glGetString(GL_VERSION);
  1780. TrimVersionString(Buffer, MajorVersion, MinorVersion);
  1781. GL_VERSION_1_2 := CheckVersion(1, 2);
  1782. GL_VERSION_1_3 := CheckVersion(1, 3);
  1783. GL_VERSION_1_4 := CheckVersion(1, 4);
  1784. GL_VERSION_2_0 := CheckVersion(2, 0);
  1785. GL_VERSION_3_3 := CheckVersion(3, 3);
  1786. // Extensions
  1787. Buffer := glGetString(GL_EXTENSIONS);
  1788. GL_ARB_texture_border_clamp := CheckExtension('GL_ARB_texture_border_clamp');
  1789. GL_ARB_texture_non_power_of_two := CheckExtension('GL_ARB_texture_non_power_of_two');
  1790. GL_ARB_texture_swizzle := CheckExtension('GL_ARB_texture_swizzle');
  1791. GL_ARB_texture_cube_map := CheckExtension('GL_ARB_texture_cube_map');
  1792. GL_ARB_texture_rectangle := CheckExtension('GL_ARB_texture_rectangle');
  1793. GL_ARB_texture_mirrored_repeat := CheckExtension('GL_ARB_texture_mirrored_repeat');
  1794. GL_EXT_texture_edge_clamp := CheckExtension('GL_EXT_texture_edge_clamp');
  1795. GL_EXT_texture_filter_anisotropic := CheckExtension('GL_EXT_texture_filter_anisotropic');
  1796. GL_EXT_texture_rectangle := CheckExtension('GL_EXT_texture_rectangle');
  1797. GL_EXT_texture_swizzle := CheckExtension('GL_EXT_texture_swizzle');
  1798. GL_EXT_texture_cube_map := CheckExtension('GL_EXT_texture_cube_map');
  1799. GL_NV_texture_rectangle := CheckExtension('GL_NV_texture_rectangle');
  1800. GL_IBM_texture_mirrored_repeat := CheckExtension('GL_IBM_texture_mirrored_repeat');
  1801. GL_SGIS_generate_mipmap := CheckExtension('GL_SGIS_generate_mipmap');
  1802. if GL_VERSION_1_3 then begin
  1803. glCompressedTexImage1D := glbGetProcAddress('glCompressedTexImage1D');
  1804. glCompressedTexImage2D := glbGetProcAddress('glCompressedTexImage2D');
  1805. glGetCompressedTexImage := glbGetProcAddress('glGetCompressedTexImage');
  1806. end else begin
  1807. glCompressedTexImage1D := glbGetProcAddress('glCompressedTexImage1DARB', nil, false);
  1808. glCompressedTexImage2D := glbGetProcAddress('glCompressedTexImage2DARB', nil, false);
  1809. glGetCompressedTexImage := glbGetProcAddress('glGetCompressedTexImageARB', nil, false);
  1810. end;
  1811. end;
  1812. {$ENDIF}
  1813. {$IFDEF GLB_SDL_IMAGE}
  1814. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1815. // SDL Image Helper /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1816. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1817. function glBitmapRWseek(context: PSDL_RWops; offset: Integer; whence: Integer): Integer; cdecl;
  1818. begin
  1819. result := TStream(context^.unknown.data1).Seek(offset, whence);
  1820. end;
  1821. function glBitmapRWread(context: PSDL_RWops; Ptr: Pointer; size: Integer; maxnum : Integer): Integer; cdecl;
  1822. begin
  1823. result := TStream(context^.unknown.data1).Read(Ptr^, size * maxnum);
  1824. end;
  1825. function glBitmapRWwrite(context: PSDL_RWops; Ptr: Pointer; size: Integer; num: Integer): Integer; cdecl;
  1826. begin
  1827. result := TStream(context^.unknown.data1).Write(Ptr^, size * num);
  1828. end;
  1829. function glBitmapRWclose(context: PSDL_RWops): Integer; cdecl;
  1830. begin
  1831. result := 0;
  1832. end;
  1833. function glBitmapCreateRWops(Stream: TStream): PSDL_RWops;
  1834. begin
  1835. result := SDL_AllocRW;
  1836. if result = nil then
  1837. raise EglBitmapException.Create('glBitmapCreateRWops - SDL_AllocRW failed.');
  1838. result^.seek := glBitmapRWseek;
  1839. result^.read := glBitmapRWread;
  1840. result^.write := glBitmapRWwrite;
  1841. result^.close := glBitmapRWclose;
  1842. result^.unknown.data1 := Stream;
  1843. end;
  1844. {$ENDIF}
  1845. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1846. procedure glBitmapSetDefaultDeleteTextureOnFree(const aDeleteTextureOnFree: Boolean);
  1847. begin
  1848. glBitmapDefaultDeleteTextureOnFree := aDeleteTextureOnFree;
  1849. end;
  1850. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1851. procedure glBitmapSetDefaultFreeDataAfterGenTexture(const aFreeData: Boolean);
  1852. begin
  1853. glBitmapDefaultFreeDataAfterGenTextures := aFreeData;
  1854. end;
  1855. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1856. procedure glBitmapSetDefaultMipmap(const aValue: TglBitmapMipMap);
  1857. begin
  1858. glBitmapDefaultMipmap := aValue;
  1859. end;
  1860. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1861. procedure glBitmapSetDefaultFormat(const aFormat: TglBitmapFormat);
  1862. begin
  1863. glBitmapDefaultFormat := aFormat;
  1864. end;
  1865. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1866. procedure glBitmapSetDefaultFilter(const aMin, aMag: Integer);
  1867. begin
  1868. glBitmapDefaultFilterMin := aMin;
  1869. glBitmapDefaultFilterMag := aMag;
  1870. end;
  1871. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1872. procedure glBitmapSetDefaultWrap(const S: Cardinal = GL_CLAMP_TO_EDGE; const T: Cardinal = GL_CLAMP_TO_EDGE; const R: Cardinal = GL_CLAMP_TO_EDGE);
  1873. begin
  1874. glBitmapDefaultWrapS := S;
  1875. glBitmapDefaultWrapT := T;
  1876. glBitmapDefaultWrapR := R;
  1877. end;
  1878. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1879. procedure glBitmapSetDefaultSwizzle(const r: GLenum = GL_RED; g: GLenum = GL_GREEN; b: GLenum = GL_BLUE; a: GLenum = GL_ALPHA);
  1880. begin
  1881. glDefaultSwizzle[0] := r;
  1882. glDefaultSwizzle[1] := g;
  1883. glDefaultSwizzle[2] := b;
  1884. glDefaultSwizzle[3] := a;
  1885. end;
  1886. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1887. function glBitmapGetDefaultDeleteTextureOnFree: Boolean;
  1888. begin
  1889. result := glBitmapDefaultDeleteTextureOnFree;
  1890. end;
  1891. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1892. function glBitmapGetDefaultFreeDataAfterGenTexture: Boolean;
  1893. begin
  1894. result := glBitmapDefaultFreeDataAfterGenTextures;
  1895. end;
  1896. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1897. function glBitmapGetDefaultMipmap: TglBitmapMipMap;
  1898. begin
  1899. result := glBitmapDefaultMipmap;
  1900. end;
  1901. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1902. function glBitmapGetDefaultFormat: TglBitmapFormat;
  1903. begin
  1904. result := glBitmapDefaultFormat;
  1905. end;
  1906. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1907. procedure glBitmapGetDefaultFilter(var aMin, aMag: GLenum);
  1908. begin
  1909. aMin := glBitmapDefaultFilterMin;
  1910. aMag := glBitmapDefaultFilterMag;
  1911. end;
  1912. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1913. procedure glBitmapGetDefaultTextureWrap(var S, T, R: GLenum);
  1914. begin
  1915. S := glBitmapDefaultWrapS;
  1916. T := glBitmapDefaultWrapT;
  1917. R := glBitmapDefaultWrapR;
  1918. end;
  1919. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1920. procedure glBitmapGetDefaultSwizzle(var r, g, b, a: GLenum);
  1921. begin
  1922. r := glDefaultSwizzle[0];
  1923. g := glDefaultSwizzle[1];
  1924. b := glDefaultSwizzle[2];
  1925. a := glDefaultSwizzle[3];
  1926. end;
  1927. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1928. //TglBitmapFormatDescriptor///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1929. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1930. function TFormatDescriptor.GetRedMask: QWord;
  1931. begin
  1932. result := fRange.r shl fShift.r;
  1933. end;
  1934. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1935. function TFormatDescriptor.GetGreenMask: QWord;
  1936. begin
  1937. result := fRange.g shl fShift.g;
  1938. end;
  1939. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1940. function TFormatDescriptor.GetBlueMask: QWord;
  1941. begin
  1942. result := fRange.b shl fShift.b;
  1943. end;
  1944. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1945. function TFormatDescriptor.GetAlphaMask: QWord;
  1946. begin
  1947. result := fRange.a shl fShift.a;
  1948. end;
  1949. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1950. function TFormatDescriptor.GetIsCompressed: Boolean;
  1951. begin
  1952. result := fIsCompressed;
  1953. end;
  1954. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1955. function TFormatDescriptor.GetHasAlpha: Boolean;
  1956. begin
  1957. result := (fRange.a > 0);
  1958. end;
  1959. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1960. function TFormatDescriptor.GetglFormat: GLenum;
  1961. begin
  1962. result := fglFormat;
  1963. end;
  1964. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1965. function TFormatDescriptor.GetglInternalFormat: GLenum;
  1966. begin
  1967. result := fglInternalFormat;
  1968. end;
  1969. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1970. function TFormatDescriptor.GetglDataFormat: GLenum;
  1971. begin
  1972. result := fglDataFormat;
  1973. end;
  1974. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1975. function TFormatDescriptor.GetComponents: Integer;
  1976. var
  1977. i: Integer;
  1978. begin
  1979. result := 0;
  1980. for i := 0 to 3 do
  1981. if (fRange.arr[i] > 0) then
  1982. inc(result);
  1983. end;
  1984. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1985. function TFormatDescriptor.GetSize(const aSize: TglBitmapPixelPosition): Integer;
  1986. var
  1987. w, h: Integer;
  1988. begin
  1989. if (ffX in aSize.Fields) or (ffY in aSize.Fields) then begin
  1990. w := Max(1, aSize.X);
  1991. h := Max(1, aSize.Y);
  1992. result := GetSize(w, h);
  1993. end else
  1994. result := 0;
  1995. end;
  1996. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1997. function TFormatDescriptor.GetSize(const aWidth, aHeight: Integer): Integer;
  1998. begin
  1999. result := 0;
  2000. if (aWidth <= 0) or (aHeight <= 0) then
  2001. exit;
  2002. result := Ceil(aWidth * aHeight * fPixelSize);
  2003. end;
  2004. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2005. function TFormatDescriptor.CreateMappingData: Pointer;
  2006. begin
  2007. result := nil;
  2008. end;
  2009. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2010. procedure TFormatDescriptor.FreeMappingData(var aMappingData: Pointer);
  2011. begin
  2012. //DUMMY
  2013. end;
  2014. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2015. function TFormatDescriptor.IsEmpty: Boolean;
  2016. begin
  2017. result := (fFormat = tfEmpty);
  2018. end;
  2019. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2020. function TFormatDescriptor.MaskMatch(const aRedMask, aGreenMask, aBlueMask, aAlphaMask: QWord): Boolean;
  2021. begin
  2022. result := false;
  2023. if (aRedMask = 0) and (aGreenMask = 0) and (aBlueMask = 0) and (aAlphaMask = 0) then
  2024. raise EglBitmap.Create('FormatCheckFormat - All Masks are 0');
  2025. if (aRedMask <> RedMask) then
  2026. exit;
  2027. if (aGreenMask <> GreenMask) then
  2028. exit;
  2029. if (aBlueMask <> BlueMask) then
  2030. exit;
  2031. if (aAlphaMask <> AlphaMask) then
  2032. exit;
  2033. result := true;
  2034. end;
  2035. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2036. procedure TFormatDescriptor.PreparePixel(out aPixel: TglBitmapPixelData);
  2037. begin
  2038. FillChar(aPixel{%H-}, SizeOf(aPixel), 0);
  2039. aPixel.Data := fRange;
  2040. aPixel.Range := fRange;
  2041. aPixel.Format := fFormat;
  2042. end;
  2043. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2044. constructor TFormatDescriptor.Create;
  2045. begin
  2046. inherited Create;
  2047. fFormat := tfEmpty;
  2048. fWithAlpha := tfEmpty;
  2049. fWithoutAlpha := tfEmpty;
  2050. fRGBInverted := tfEmpty;
  2051. fUncompressed := tfEmpty;
  2052. fPixelSize := 0.0;
  2053. fIsCompressed := false;
  2054. fglFormat := 0;
  2055. fglInternalFormat := 0;
  2056. fglDataFormat := 0;
  2057. FillChar(fRange, 0, SizeOf(fRange));
  2058. FillChar(fShift, 0, SizeOf(fShift));
  2059. end;
  2060. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2061. //TfdAlpha_UB1////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2062. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2063. procedure TfdAlpha_UB1.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  2064. begin
  2065. aData^ := aPixel.Data.a;
  2066. inc(aData);
  2067. end;
  2068. procedure TfdAlpha_UB1.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  2069. begin
  2070. aPixel.Data.r := 0;
  2071. aPixel.Data.g := 0;
  2072. aPixel.Data.b := 0;
  2073. aPixel.Data.a := aData^;
  2074. inc(aData);
  2075. end;
  2076. constructor TfdAlpha_UB1.Create;
  2077. begin
  2078. inherited Create;
  2079. fPixelSize := 1.0;
  2080. fRange.a := $FF;
  2081. fglFormat := GL_ALPHA;
  2082. fglDataFormat := GL_UNSIGNED_BYTE;
  2083. end;
  2084. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2085. //TfdLuminance_UB1////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2086. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2087. procedure TfdLuminance_UB1.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  2088. begin
  2089. aData^ := LuminanceWeight(aPixel);
  2090. inc(aData);
  2091. end;
  2092. procedure TfdLuminance_UB1.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  2093. begin
  2094. aPixel.Data.r := aData^;
  2095. aPixel.Data.g := aData^;
  2096. aPixel.Data.b := aData^;
  2097. aPixel.Data.a := 0;
  2098. inc(aData);
  2099. end;
  2100. constructor TfdLuminance_UB1.Create;
  2101. begin
  2102. inherited Create;
  2103. fPixelSize := 1.0;
  2104. fRange.r := $FF;
  2105. fRange.g := $FF;
  2106. fRange.b := $FF;
  2107. fglFormat := GL_LUMINANCE;
  2108. fglDataFormat := GL_UNSIGNED_BYTE;
  2109. end;
  2110. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2111. //TfdUniversal_UB1////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2112. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2113. procedure TfdUniversal_UB1.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  2114. var
  2115. i: Integer;
  2116. begin
  2117. aData^ := 0;
  2118. for i := 0 to 3 do
  2119. if (fRange.arr[i] > 0) then
  2120. aData^ := aData^ or ((aPixel.Data.arr[i] and fRange.arr[i]) shl fShift.arr[i]);
  2121. inc(aData);
  2122. end;
  2123. procedure TfdUniversal_UB1.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  2124. var
  2125. i: Integer;
  2126. begin
  2127. for i := 0 to 3 do
  2128. aPixel.Data.arr[i] := (aData^ shr fShift.arr[i]) and fRange.arr[i];
  2129. inc(aData);
  2130. end;
  2131. constructor TfdUniversal_UB1.Create;
  2132. begin
  2133. inherited Create;
  2134. fPixelSize := 1.0;
  2135. end;
  2136. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2137. //TfdLuminanceAlpha_UB2///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2138. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2139. procedure TfdLuminanceAlpha_UB2.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  2140. begin
  2141. inherited Map(aPixel, aData, aMapData);
  2142. aData^ := aPixel.Data.a;
  2143. inc(aData);
  2144. end;
  2145. procedure TfdLuminanceAlpha_UB2.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  2146. begin
  2147. inherited Unmap(aData, aPixel, aMapData);
  2148. aPixel.Data.a := aData^;
  2149. inc(aData);
  2150. end;
  2151. constructor TfdLuminanceAlpha_UB2.Create;
  2152. begin
  2153. inherited Create;
  2154. fPixelSize := 2.0;
  2155. fRange.a := $FF;
  2156. fShift.a := 8;
  2157. fglFormat := GL_LUMINANCE_ALPHA;
  2158. fglDataFormat := GL_UNSIGNED_BYTE;
  2159. end;
  2160. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2161. //TfdRGB_UB3//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2162. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2163. procedure TfdRGB_UB3.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  2164. begin
  2165. aData^ := aPixel.Data.r;
  2166. inc(aData);
  2167. aData^ := aPixel.Data.g;
  2168. inc(aData);
  2169. aData^ := aPixel.Data.b;
  2170. inc(aData);
  2171. end;
  2172. procedure TfdRGB_UB3.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  2173. begin
  2174. aPixel.Data.r := aData^;
  2175. inc(aData);
  2176. aPixel.Data.g := aData^;
  2177. inc(aData);
  2178. aPixel.Data.b := aData^;
  2179. inc(aData);
  2180. aPixel.Data.a := 0;
  2181. end;
  2182. constructor TfdRGB_UB3.Create;
  2183. begin
  2184. inherited Create;
  2185. fPixelSize := 3.0;
  2186. fRange.r := $FF;
  2187. fRange.g := $FF;
  2188. fRange.b := $FF;
  2189. fShift.r := 0;
  2190. fShift.g := 8;
  2191. fShift.b := 16;
  2192. fglFormat := GL_RGB;
  2193. fglDataFormat := GL_UNSIGNED_BYTE;
  2194. end;
  2195. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2196. //TfdBGR_UB3//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2197. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2198. procedure TfdBGR_UB3.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  2199. begin
  2200. aData^ := aPixel.Data.b;
  2201. inc(aData);
  2202. aData^ := aPixel.Data.g;
  2203. inc(aData);
  2204. aData^ := aPixel.Data.r;
  2205. inc(aData);
  2206. end;
  2207. procedure TfdBGR_UB3.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  2208. begin
  2209. aPixel.Data.b := aData^;
  2210. inc(aData);
  2211. aPixel.Data.g := aData^;
  2212. inc(aData);
  2213. aPixel.Data.r := aData^;
  2214. inc(aData);
  2215. aPixel.Data.a := 0;
  2216. end;
  2217. constructor TfdBGR_UB3.Create;
  2218. begin
  2219. fPixelSize := 3.0;
  2220. fRange.r := $FF;
  2221. fRange.g := $FF;
  2222. fRange.b := $FF;
  2223. fShift.r := 16;
  2224. fShift.g := 8;
  2225. fShift.b := 0;
  2226. fglFormat := GL_BGR;
  2227. fglDataFormat := GL_UNSIGNED_BYTE;
  2228. end;
  2229. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2230. //TdfRGBA_UB4/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2231. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2232. procedure TfdRGBA_UB4.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  2233. begin
  2234. inherited Map(aPixel, aData, aMapData);
  2235. aData^ := aPixel.Data.a;
  2236. inc(aData);
  2237. end;
  2238. procedure TfdRGBA_UB4.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  2239. begin
  2240. inherited Unmap(aData, aPixel, aMapData);
  2241. aPixel.Data.a := aData^;
  2242. inc(aData);
  2243. end;
  2244. constructor TfdRGBA_UB4.Create;
  2245. begin
  2246. inherited Create;
  2247. fPixelSize := 4.0;
  2248. fRange.a := $FF;
  2249. fShift.a := 24;
  2250. fglFormat := GL_RGBA;
  2251. fglDataFormat := GL_UNSIGNED_BYTE;
  2252. end;
  2253. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2254. //TfdBGRA_UB4/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2255. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2256. procedure TfdBGRA_UB4.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  2257. begin
  2258. inherited Map(aPixel, aData, aMapData);
  2259. aData^ := aPixel.Data.a;
  2260. inc(aData);
  2261. end;
  2262. procedure TfdBGRA_UB4.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  2263. begin
  2264. inherited Unmap(aData, aPixel, aMapData);
  2265. aPixel.Data.a := aData^;
  2266. inc(aData);
  2267. end;
  2268. constructor TfdBGRA_UB4.Create;
  2269. begin
  2270. inherited Create;
  2271. fPixelSize := 4.0;
  2272. fRange.a := $FF;
  2273. fShift.a := 24;
  2274. fglFormat := GL_BGRA;
  2275. fglDataFormat := GL_UNSIGNED_BYTE;
  2276. end;
  2277. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2278. //TfdAlpha_US1////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2279. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2280. procedure TfdAlpha_US1.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  2281. begin
  2282. PWord(aData)^ := aPixel.Data.a;
  2283. inc(aData, 2);
  2284. end;
  2285. procedure TfdAlpha_US1.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  2286. begin
  2287. aPixel.Data.r := 0;
  2288. aPixel.Data.g := 0;
  2289. aPixel.Data.b := 0;
  2290. aPixel.Data.a := PWord(aData)^;
  2291. inc(aData, 2);
  2292. end;
  2293. constructor TfdAlpha_US1.Create;
  2294. begin
  2295. inherited Create;
  2296. fPixelSize := 2.0;
  2297. fRange.a := $FFFF;
  2298. fglFormat := GL_ALPHA;
  2299. fglDataFormat := GL_UNSIGNED_SHORT;
  2300. end;
  2301. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2302. //TfdLuminance_US1////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2303. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2304. procedure TfdLuminance_US1.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  2305. begin
  2306. PWord(aData)^ := LuminanceWeight(aPixel);
  2307. inc(aData, 2);
  2308. end;
  2309. procedure TfdLuminance_US1.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  2310. begin
  2311. aPixel.Data.r := PWord(aData)^;
  2312. aPixel.Data.g := PWord(aData)^;
  2313. aPixel.Data.b := PWord(aData)^;
  2314. aPixel.Data.a := 0;
  2315. inc(aData, 2);
  2316. end;
  2317. constructor TfdLuminance_US1.Create;
  2318. begin
  2319. inherited Create;
  2320. fPixelSize := 2.0;
  2321. fRange.r := $FFFF;
  2322. fRange.g := $FFFF;
  2323. fRange.b := $FFFF;
  2324. fglFormat := GL_LUMINANCE;
  2325. fglDataFormat := GL_UNSIGNED_SHORT;
  2326. end;
  2327. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2328. //TfdUniversal_US1////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2329. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2330. procedure TfdUniversal_US1.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  2331. var
  2332. i: Integer;
  2333. begin
  2334. PWord(aData)^ := 0;
  2335. for i := 0 to 3 do
  2336. if (fRange.arr[i] > 0) then
  2337. PWord(aData)^ := PWord(aData)^ or ((aPixel.Data.arr[i] and fRange.arr[i]) shl fShift.arr[i]);
  2338. inc(aData, 2);
  2339. end;
  2340. procedure TfdUniversal_US1.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  2341. var
  2342. i: Integer;
  2343. begin
  2344. for i := 0 to 3 do
  2345. aPixel.Data.arr[i] := (PWord(aData)^ shr fShift.arr[i]) and fRange.arr[i];
  2346. inc(aData, 2);
  2347. end;
  2348. constructor TfdUniversal_US1.Create;
  2349. begin
  2350. inherited Create;
  2351. fPixelSize := 2.0;
  2352. end;
  2353. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2354. //TfdDepth_US1////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2355. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2356. procedure TfdDepth_US1.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  2357. begin
  2358. PWord(aData)^ := DepthWeight(aPixel);
  2359. inc(aData, 2);
  2360. end;
  2361. procedure TfdDepth_US1.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  2362. begin
  2363. aPixel.Data.r := PWord(aData)^;
  2364. aPixel.Data.g := PWord(aData)^;
  2365. aPixel.Data.b := PWord(aData)^;
  2366. aPixel.Data.a := 0;
  2367. inc(aData, 2);
  2368. end;
  2369. constructor TfdDepth_US1.Create;
  2370. begin
  2371. inherited Create;
  2372. fPixelSize := 2.0;
  2373. fRange.r := $FFFF;
  2374. fRange.g := $FFFF;
  2375. fRange.b := $FFFF;
  2376. fglFormat := GL_DEPTH_COMPONENT;
  2377. fglDataFormat := GL_UNSIGNED_SHORT;
  2378. end;
  2379. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2380. //TfdLuminanceAlpha_US2///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2381. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2382. procedure TfdLuminanceAlpha_US2.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  2383. begin
  2384. inherited Map(aPixel, aData, aMapData);
  2385. PWord(aData)^ := aPixel.Data.a;
  2386. inc(aData, 2);
  2387. end;
  2388. procedure TfdLuminanceAlpha_US2.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  2389. begin
  2390. inherited Unmap(aData, aPixel, aMapData);
  2391. aPixel.Data.a := PWord(aData)^;
  2392. inc(aData, 2);
  2393. end;
  2394. constructor TfdLuminanceAlpha_US2.Create;
  2395. begin
  2396. inherited Create;
  2397. fPixelSize := 4.0;
  2398. fRange.a := $FFFF;
  2399. fShift.a := 16;
  2400. fglFormat := GL_LUMINANCE_ALPHA;
  2401. fglDataFormat := GL_UNSIGNED_SHORT;
  2402. end;
  2403. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2404. //TfdRGB_US3//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2405. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2406. procedure TfdRGB_US3.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  2407. begin
  2408. PWord(aData)^ := aPixel.Data.r;
  2409. inc(aData, 2);
  2410. PWord(aData)^ := aPixel.Data.g;
  2411. inc(aData, 2);
  2412. PWord(aData)^ := aPixel.Data.b;
  2413. inc(aData, 2);
  2414. end;
  2415. procedure TfdRGB_US3.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  2416. begin
  2417. aPixel.Data.r := PWord(aData)^;
  2418. inc(aData, 2);
  2419. aPixel.Data.g := PWord(aData)^;
  2420. inc(aData, 2);
  2421. aPixel.Data.b := PWord(aData)^;
  2422. inc(aData, 2);
  2423. aPixel.Data.a := 0;
  2424. end;
  2425. constructor TfdRGB_US3.Create;
  2426. begin
  2427. inherited Create;
  2428. fPixelSize := 6.0;
  2429. fRange.r := $FFFF;
  2430. fRange.g := $FFFF;
  2431. fRange.b := $FFFF;
  2432. fShift.r := 0;
  2433. fShift.g := 16;
  2434. fShift.b := 32;
  2435. fglFormat := GL_RGB;
  2436. fglDataFormat := GL_UNSIGNED_SHORT;
  2437. end;
  2438. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2439. //TfdBGR_US3//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2440. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2441. procedure TfdBGR_US3.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  2442. begin
  2443. PWord(aData)^ := aPixel.Data.b;
  2444. inc(aData, 2);
  2445. PWord(aData)^ := aPixel.Data.g;
  2446. inc(aData, 2);
  2447. PWord(aData)^ := aPixel.Data.r;
  2448. inc(aData, 2);
  2449. end;
  2450. procedure TfdBGR_US3.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  2451. begin
  2452. aPixel.Data.b := PWord(aData)^;
  2453. inc(aData, 2);
  2454. aPixel.Data.g := PWord(aData)^;
  2455. inc(aData, 2);
  2456. aPixel.Data.r := PWord(aData)^;
  2457. inc(aData, 2);
  2458. aPixel.Data.a := 0;
  2459. end;
  2460. constructor TfdBGR_US3.Create;
  2461. begin
  2462. inherited Create;
  2463. fPixelSize := 6.0;
  2464. fRange.r := $FFFF;
  2465. fRange.g := $FFFF;
  2466. fRange.b := $FFFF;
  2467. fShift.r := 32;
  2468. fShift.g := 16;
  2469. fShift.b := 0;
  2470. fglFormat := GL_BGR;
  2471. fglDataFormat := GL_UNSIGNED_SHORT;
  2472. end;
  2473. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2474. //TfdRGBA_US4/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2475. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2476. procedure TfdRGBA_US4.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  2477. begin
  2478. inherited Map(aPixel, aData, aMapData);
  2479. PWord(aData)^ := aPixel.Data.a;
  2480. inc(aData, 2);
  2481. end;
  2482. procedure TfdRGBA_US4.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  2483. begin
  2484. inherited Unmap(aData, aPixel, aMapData);
  2485. aPixel.Data.a := PWord(aData)^;
  2486. inc(aData, 2);
  2487. end;
  2488. constructor TfdRGBA_US4.Create;
  2489. begin
  2490. inherited Create;
  2491. fPixelSize := 8.0;
  2492. fRange.a := $FFFF;
  2493. fShift.a := 48;
  2494. fglFormat := GL_RGBA;
  2495. fglDataFormat := GL_UNSIGNED_SHORT;
  2496. end;
  2497. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2498. //TfdBGRA_US4/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2499. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2500. procedure TfdBGRA_US4.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  2501. begin
  2502. inherited Map(aPixel, aData, aMapData);
  2503. PWord(aData)^ := aPixel.Data.a;
  2504. inc(aData, 2);
  2505. end;
  2506. procedure TfdBGRA_US4.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  2507. begin
  2508. inherited Unmap(aData, aPixel, aMapData);
  2509. aPixel.Data.a := PWord(aData)^;
  2510. inc(aData, 2);
  2511. end;
  2512. constructor TfdBGRA_US4.Create;
  2513. begin
  2514. inherited Create;
  2515. fPixelSize := 8.0;
  2516. fRange.a := $FFFF;
  2517. fShift.a := 48;
  2518. fglFormat := GL_BGRA;
  2519. fglDataFormat := GL_UNSIGNED_SHORT;
  2520. end;
  2521. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2522. //TfdUniversal_UI1////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2523. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2524. procedure TfdUniversal_UI1.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  2525. var
  2526. i: Integer;
  2527. begin
  2528. PCardinal(aData)^ := 0;
  2529. for i := 0 to 3 do
  2530. if (fRange.arr[i] > 0) then
  2531. PCardinal(aData)^ := PCardinal(aData)^ or ((aPixel.Data.arr[i] and fRange.arr[i]) shl fShift.arr[i]);
  2532. inc(aData, 4);
  2533. end;
  2534. procedure TfdUniversal_UI1.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  2535. var
  2536. i: Integer;
  2537. begin
  2538. for i := 0 to 3 do
  2539. aPixel.Data.arr[i] := (PCardinal(aData)^ shr fShift.arr[i]) and fRange.arr[i];
  2540. inc(aData, 2);
  2541. end;
  2542. constructor TfdUniversal_UI1.Create;
  2543. begin
  2544. inherited Create;
  2545. fPixelSize := 4.0;
  2546. end;
  2547. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2548. //TfdDepth_UI1////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2549. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2550. procedure TfdDepth_UI1.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  2551. begin
  2552. PCardinal(aData)^ := DepthWeight(aPixel);
  2553. inc(aData, 4);
  2554. end;
  2555. procedure TfdDepth_UI1.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  2556. begin
  2557. aPixel.Data.r := PCardinal(aData)^;
  2558. aPixel.Data.g := PCardinal(aData)^;
  2559. aPixel.Data.b := PCardinal(aData)^;
  2560. aPixel.Data.a := 0;
  2561. inc(aData, 4);
  2562. end;
  2563. constructor TfdDepth_UI1.Create;
  2564. begin
  2565. inherited Create;
  2566. fPixelSize := 4.0;
  2567. fRange.r := $FFFFFFFF;
  2568. fRange.g := $FFFFFFFF;
  2569. fRange.b := $FFFFFFFF;
  2570. fglFormat := GL_DEPTH_COMPONENT;
  2571. fglDataFormat := GL_UNSIGNED_INT;
  2572. end;
  2573. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2574. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2575. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2576. constructor TfdAlpha4.Create;
  2577. begin
  2578. inherited Create;
  2579. fFormat := tfAlpha4;
  2580. fWithAlpha := tfAlpha4;
  2581. fglInternalFormat := GL_ALPHA4;
  2582. end;
  2583. constructor TfdAlpha8.Create;
  2584. begin
  2585. inherited Create;
  2586. fFormat := tfAlpha8;
  2587. fWithAlpha := tfAlpha8;
  2588. fglInternalFormat := GL_ALPHA8;
  2589. end;
  2590. constructor TfdAlpha12.Create;
  2591. begin
  2592. inherited Create;
  2593. fFormat := tfAlpha12;
  2594. fWithAlpha := tfAlpha12;
  2595. fglInternalFormat := GL_ALPHA12;
  2596. end;
  2597. constructor TfdAlpha16.Create;
  2598. begin
  2599. inherited Create;
  2600. fFormat := tfAlpha16;
  2601. fWithAlpha := tfAlpha16;
  2602. fglInternalFormat := GL_ALPHA16;
  2603. end;
  2604. constructor TfdLuminance4.Create;
  2605. begin
  2606. inherited Create;
  2607. fFormat := tfLuminance4;
  2608. fWithAlpha := tfLuminance4Alpha4;
  2609. fWithoutAlpha := tfLuminance4;
  2610. fglInternalFormat := GL_LUMINANCE4;
  2611. end;
  2612. constructor TfdLuminance8.Create;
  2613. begin
  2614. inherited Create;
  2615. fFormat := tfLuminance8;
  2616. fWithAlpha := tfLuminance8Alpha8;
  2617. fWithoutAlpha := tfLuminance8;
  2618. fglInternalFormat := GL_LUMINANCE8;
  2619. end;
  2620. constructor TfdLuminance12.Create;
  2621. begin
  2622. inherited Create;
  2623. fFormat := tfLuminance12;
  2624. fWithAlpha := tfLuminance12Alpha12;
  2625. fWithoutAlpha := tfLuminance12;
  2626. fglInternalFormat := GL_LUMINANCE12;
  2627. end;
  2628. constructor TfdLuminance16.Create;
  2629. begin
  2630. inherited Create;
  2631. fFormat := tfLuminance16;
  2632. fWithAlpha := tfLuminance16Alpha16;
  2633. fWithoutAlpha := tfLuminance16;
  2634. fglInternalFormat := GL_LUMINANCE16;
  2635. end;
  2636. constructor TfdLuminance4Alpha4.Create;
  2637. begin
  2638. inherited Create;
  2639. fFormat := tfLuminance4Alpha4;
  2640. fWithAlpha := tfLuminance4Alpha4;
  2641. fWithoutAlpha := tfLuminance4;
  2642. fglInternalFormat := GL_LUMINANCE4_ALPHA4;
  2643. end;
  2644. constructor TfdLuminance6Alpha2.Create;
  2645. begin
  2646. inherited Create;
  2647. fFormat := tfLuminance6Alpha2;
  2648. fWithAlpha := tfLuminance6Alpha2;
  2649. fWithoutAlpha := tfLuminance8;
  2650. fglInternalFormat := GL_LUMINANCE6_ALPHA2;
  2651. end;
  2652. constructor TfdLuminance8Alpha8.Create;
  2653. begin
  2654. inherited Create;
  2655. fFormat := tfLuminance8Alpha8;
  2656. fWithAlpha := tfLuminance8Alpha8;
  2657. fWithoutAlpha := tfLuminance8;
  2658. fglInternalFormat := GL_LUMINANCE8_ALPHA8;
  2659. end;
  2660. constructor TfdLuminance12Alpha4.Create;
  2661. begin
  2662. inherited Create;
  2663. fFormat := tfLuminance12Alpha4;
  2664. fWithAlpha := tfLuminance12Alpha4;
  2665. fWithoutAlpha := tfLuminance12;
  2666. fglInternalFormat := GL_LUMINANCE12_ALPHA4;
  2667. end;
  2668. constructor TfdLuminance12Alpha12.Create;
  2669. begin
  2670. inherited Create;
  2671. fFormat := tfLuminance12Alpha12;
  2672. fWithAlpha := tfLuminance12Alpha12;
  2673. fWithoutAlpha := tfLuminance12;
  2674. fglInternalFormat := GL_LUMINANCE12_ALPHA12;
  2675. end;
  2676. constructor TfdLuminance16Alpha16.Create;
  2677. begin
  2678. inherited Create;
  2679. fFormat := tfLuminance16Alpha16;
  2680. fWithAlpha := tfLuminance16Alpha16;
  2681. fWithoutAlpha := tfLuminance16;
  2682. fglInternalFormat := GL_LUMINANCE16_ALPHA16;
  2683. end;
  2684. constructor TfdR3G3B2.Create;
  2685. begin
  2686. inherited Create;
  2687. fFormat := tfR3G3B2;
  2688. fWithAlpha := tfRGBA2;
  2689. fWithoutAlpha := tfR3G3B2;
  2690. fRange.r := $7;
  2691. fRange.g := $7;
  2692. fRange.b := $3;
  2693. fShift.r := 0;
  2694. fShift.g := 3;
  2695. fShift.b := 6;
  2696. fglFormat := GL_RGB;
  2697. fglInternalFormat := GL_R3_G3_B2;
  2698. fglDataFormat := GL_UNSIGNED_BYTE_2_3_3_REV;
  2699. end;
  2700. constructor TfdRGB4.Create;
  2701. begin
  2702. inherited Create;
  2703. fFormat := tfRGB4;
  2704. fWithAlpha := tfRGBA4;
  2705. fWithoutAlpha := tfRGB4;
  2706. fRGBInverted := tfBGR4;
  2707. fRange.r := $F;
  2708. fRange.g := $F;
  2709. fRange.b := $F;
  2710. fShift.r := 0;
  2711. fShift.g := 4;
  2712. fShift.b := 8;
  2713. fglFormat := GL_RGBA; //GL_INVALID_OPERATION if not GL_BGRA or GL_RGBA
  2714. fglInternalFormat := GL_RGB4;
  2715. fglDataFormat := GL_UNSIGNED_SHORT_4_4_4_4_REV;
  2716. end;
  2717. constructor TfdR5G6B5.Create;
  2718. begin
  2719. inherited Create;
  2720. fFormat := tfR5G6B5;
  2721. fWithAlpha := tfRGBA4;
  2722. fWithoutAlpha := tfR5G6B5;
  2723. fRGBInverted := tfB5G6R5;
  2724. fRange.r := $1F;
  2725. fRange.g := $3F;
  2726. fRange.b := $1F;
  2727. fShift.r := 0;
  2728. fShift.g := 5;
  2729. fShift.b := 11;
  2730. fglFormat := GL_RGB;
  2731. fglInternalFormat := GL_RGB565;
  2732. fglDataFormat := GL_UNSIGNED_SHORT_5_6_5_REV;
  2733. end;
  2734. constructor TfdRGB5.Create;
  2735. begin
  2736. inherited Create;
  2737. fFormat := tfRGB5;
  2738. fWithAlpha := tfRGB5A1;
  2739. fWithoutAlpha := tfRGB5;
  2740. fRGBInverted := tfBGR5;
  2741. fRange.r := $1F;
  2742. fRange.g := $1F;
  2743. fRange.b := $1F;
  2744. fShift.r := 0;
  2745. fShift.g := 5;
  2746. fShift.b := 10;
  2747. fglFormat := GL_RGBA; //GL_INVALID_OPERATION if not GL_BGRA or GL_RGBA
  2748. fglInternalFormat := GL_RGB5;
  2749. fglDataFormat := GL_UNSIGNED_SHORT_1_5_5_5_REV;
  2750. end;
  2751. constructor TfdRGB8.Create;
  2752. begin
  2753. inherited Create;
  2754. fFormat := tfRGB8;
  2755. fWithAlpha := tfRGBA8;
  2756. fWithoutAlpha := tfRGB8;
  2757. fRGBInverted := tfBGR8;
  2758. fglInternalFormat := GL_RGB8;
  2759. end;
  2760. constructor TfdRGB10.Create;
  2761. begin
  2762. inherited Create;
  2763. fFormat := tfRGB10;
  2764. fWithAlpha := tfRGB10A2;
  2765. fWithoutAlpha := tfRGB10;
  2766. fRGBInverted := tfBGR10;
  2767. fRange.r := $3FF;
  2768. fRange.g := $3FF;
  2769. fRange.b := $3FF;
  2770. fShift.r := 0;
  2771. fShift.g := 10;
  2772. fShift.b := 20;
  2773. fglFormat := GL_RGBA; //GL_INVALID_OPERATION if not GL_BGRA or GL_RGBA
  2774. fglInternalFormat := GL_RGB10;
  2775. fglDataFormat := GL_UNSIGNED_INT_2_10_10_10_REV;
  2776. end;
  2777. constructor TfdRGB12.Create;
  2778. begin
  2779. inherited Create;
  2780. fFormat := tfRGB12;
  2781. fWithAlpha := tfRGBA12;
  2782. fWithoutAlpha := tfRGB12;
  2783. fRGBInverted := tfBGR12;
  2784. fglInternalFormat := GL_RGB12;
  2785. end;
  2786. constructor TfdRGB16.Create;
  2787. begin
  2788. inherited Create;
  2789. fFormat := tfRGB16;
  2790. fWithAlpha := tfRGBA16;
  2791. fWithoutAlpha := tfRGB16;
  2792. fRGBInverted := tfBGR16;
  2793. fglInternalFormat := GL_RGB16;
  2794. end;
  2795. constructor TfdRGBA2.Create;
  2796. begin
  2797. inherited Create;
  2798. fFormat := tfRGBA2;
  2799. fWithAlpha := tfRGBA2;
  2800. fWithoutAlpha := tfR3G3B2;
  2801. fRGBInverted := tfBGRA2;
  2802. fglInternalFormat := GL_RGBA2;
  2803. end;
  2804. constructor TfdRGBA4.Create;
  2805. begin
  2806. inherited Create;
  2807. fFormat := tfRGBA4;
  2808. fWithAlpha := tfRGBA4;
  2809. fWithoutAlpha := tfRGB4;
  2810. fRGBInverted := tfBGRA4;
  2811. fRange.r := $F;
  2812. fRange.g := $F;
  2813. fRange.b := $F;
  2814. fRange.a := $F;
  2815. fShift.r := 0;
  2816. fShift.g := 4;
  2817. fShift.b := 8;
  2818. fShift.a := 12;
  2819. fglFormat := GL_RGBA;
  2820. fglInternalFormat := GL_RGBA4;
  2821. fglDataFormat := GL_UNSIGNED_SHORT_4_4_4_4_REV;
  2822. end;
  2823. constructor TfdRGB5A1.Create;
  2824. begin
  2825. inherited Create;
  2826. fFormat := tfRGB5A1;
  2827. fWithAlpha := tfRGB5A1;
  2828. fWithoutAlpha := tfRGB5;
  2829. fRGBInverted := tfBGR5A1;
  2830. fRange.r := $1F;
  2831. fRange.g := $1F;
  2832. fRange.b := $1F;
  2833. fRange.a := $01;
  2834. fShift.r := 0;
  2835. fShift.g := 5;
  2836. fShift.b := 10;
  2837. fShift.a := 15;
  2838. fglFormat := GL_RGBA;
  2839. fglInternalFormat := GL_RGB5_A1;
  2840. fglDataFormat := GL_UNSIGNED_SHORT_1_5_5_5_REV;
  2841. end;
  2842. constructor TfdRGBA8.Create;
  2843. begin
  2844. inherited Create;
  2845. fFormat := tfRGBA8;
  2846. fWithAlpha := tfRGBA8;
  2847. fWithoutAlpha := tfRGB8;
  2848. fRGBInverted := tfBGRA8;
  2849. fglInternalFormat := GL_RGBA8;
  2850. end;
  2851. constructor TfdRGB10A2.Create;
  2852. begin
  2853. inherited Create;
  2854. fFormat := tfRGB10A2;
  2855. fWithAlpha := tfRGB10A2;
  2856. fWithoutAlpha := tfRGB10;
  2857. fRGBInverted := tfBGR10A2;
  2858. fRange.r := $3FF;
  2859. fRange.g := $3FF;
  2860. fRange.b := $3FF;
  2861. fRange.a := $003;
  2862. fShift.r := 0;
  2863. fShift.g := 10;
  2864. fShift.b := 20;
  2865. fShift.a := 30;
  2866. fglFormat := GL_RGBA;
  2867. fglInternalFormat := GL_RGB10_A2;
  2868. fglDataFormat := GL_UNSIGNED_INT_2_10_10_10_REV;
  2869. end;
  2870. constructor TfdRGBA12.Create;
  2871. begin
  2872. inherited Create;
  2873. fFormat := tfRGBA12;
  2874. fWithAlpha := tfRGBA12;
  2875. fWithoutAlpha := tfRGB12;
  2876. fRGBInverted := tfBGRA12;
  2877. fglInternalFormat := GL_RGBA12;
  2878. end;
  2879. constructor TfdRGBA16.Create;
  2880. begin
  2881. inherited Create;
  2882. fFormat := tfRGBA16;
  2883. fWithAlpha := tfRGBA16;
  2884. fWithoutAlpha := tfRGB16;
  2885. fRGBInverted := tfBGRA16;
  2886. fglInternalFormat := GL_RGBA16;
  2887. end;
  2888. constructor TfdBGR4.Create;
  2889. begin
  2890. inherited Create;
  2891. fPixelSize := 2.0;
  2892. fFormat := tfBGR4;
  2893. fWithAlpha := tfBGRA4;
  2894. fWithoutAlpha := tfBGR4;
  2895. fRGBInverted := tfRGB4;
  2896. fRange.r := $F;
  2897. fRange.g := $F;
  2898. fRange.b := $F;
  2899. fRange.a := $0;
  2900. fShift.r := 8;
  2901. fShift.g := 4;
  2902. fShift.b := 0;
  2903. fShift.a := 0;
  2904. fglFormat := GL_BGRA; //GL_INVALID_OPERATION if not GL_BGRA or GL_RGBA
  2905. fglInternalFormat := GL_RGB4;
  2906. fglDataFormat := GL_UNSIGNED_SHORT_4_4_4_4_REV;
  2907. end;
  2908. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2909. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2910. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2911. constructor TfdB5G6R5.Create;
  2912. begin
  2913. inherited Create;
  2914. fFormat := tfB5G6R5;
  2915. fWithAlpha := tfBGRA4;
  2916. fWithoutAlpha := tfB5G6R5;
  2917. fRGBInverted := tfR5G6B5;
  2918. fRange.r := $1F;
  2919. fRange.g := $3F;
  2920. fRange.b := $1F;
  2921. fShift.r := 11;
  2922. fShift.g := 5;
  2923. fShift.b := 0;
  2924. fglFormat := GL_RGB; //B5G6R5 is only possible as R5G6B5 -> use reverted dataformat
  2925. fglInternalFormat := GL_RGB8;
  2926. fglDataFormat := GL_UNSIGNED_SHORT_5_6_5;
  2927. end;
  2928. constructor TfdBGR5.Create;
  2929. begin
  2930. inherited Create;
  2931. fPixelSize := 2.0;
  2932. fFormat := tfBGR5;
  2933. fWithAlpha := tfBGR5A1;
  2934. fWithoutAlpha := tfBGR5;
  2935. fRGBInverted := tfRGB5;
  2936. fRange.r := $1F;
  2937. fRange.g := $1F;
  2938. fRange.b := $1F;
  2939. fRange.a := $00;
  2940. fShift.r := 10;
  2941. fShift.g := 5;
  2942. fShift.b := 0;
  2943. fShift.a := 0;
  2944. fglFormat := GL_BGRA; //GL_INVALID_OPERATION if not GL_BGRA or GL_RGBA
  2945. fglInternalFormat := GL_RGB5;
  2946. fglDataFormat := GL_UNSIGNED_SHORT_1_5_5_5_REV;
  2947. end;
  2948. constructor TfdBGR8.Create;
  2949. begin
  2950. inherited Create;
  2951. fFormat := tfBGR8;
  2952. fWithAlpha := tfBGRA8;
  2953. fWithoutAlpha := tfBGR8;
  2954. fRGBInverted := tfRGB8;
  2955. fglInternalFormat := GL_RGB8;
  2956. end;
  2957. constructor TfdBGR10.Create;
  2958. begin
  2959. inherited Create;
  2960. fFormat := tfBGR10;
  2961. fWithAlpha := tfBGR10A2;
  2962. fWithoutAlpha := tfBGR10;
  2963. fRGBInverted := tfRGB10;
  2964. fRange.r := $3FF;
  2965. fRange.g := $3FF;
  2966. fRange.b := $3FF;
  2967. fRange.a := $000;
  2968. fShift.r := 20;
  2969. fShift.g := 10;
  2970. fShift.b := 0;
  2971. fShift.a := 0;
  2972. fglFormat := GL_BGRA; //GL_INVALID_OPERATION if not GL_BGRA or GL_RGBA
  2973. fglInternalFormat := GL_RGB10;
  2974. fglDataFormat := GL_UNSIGNED_INT_2_10_10_10_REV;
  2975. end;
  2976. constructor TfdBGR12.Create;
  2977. begin
  2978. inherited Create;
  2979. fFormat := tfBGR12;
  2980. fWithAlpha := tfBGRA12;
  2981. fWithoutAlpha := tfBGR12;
  2982. fRGBInverted := tfRGB12;
  2983. fglInternalFormat := GL_RGB12;
  2984. end;
  2985. constructor TfdBGR16.Create;
  2986. begin
  2987. inherited Create;
  2988. fFormat := tfBGR16;
  2989. fWithAlpha := tfBGRA16;
  2990. fWithoutAlpha := tfBGR16;
  2991. fRGBInverted := tfRGB16;
  2992. fglInternalFormat := GL_RGB16;
  2993. end;
  2994. constructor TfdBGRA2.Create;
  2995. begin
  2996. inherited Create;
  2997. fFormat := tfBGRA2;
  2998. fWithAlpha := tfBGRA4;
  2999. fWithoutAlpha := tfBGR4;
  3000. fRGBInverted := tfRGBA2;
  3001. fglInternalFormat := GL_RGBA2;
  3002. end;
  3003. constructor TfdBGRA4.Create;
  3004. begin
  3005. inherited Create;
  3006. fFormat := tfBGRA4;
  3007. fWithAlpha := tfBGRA4;
  3008. fWithoutAlpha := tfBGR4;
  3009. fRGBInverted := tfRGBA4;
  3010. fRange.r := $F;
  3011. fRange.g := $F;
  3012. fRange.b := $F;
  3013. fRange.a := $F;
  3014. fShift.r := 8;
  3015. fShift.g := 4;
  3016. fShift.b := 0;
  3017. fShift.a := 12;
  3018. fglFormat := GL_BGRA;
  3019. fglInternalFormat := GL_RGBA4;
  3020. fglDataFormat := GL_UNSIGNED_SHORT_4_4_4_4_REV;
  3021. end;
  3022. constructor TfdBGR5A1.Create;
  3023. begin
  3024. inherited Create;
  3025. fFormat := tfBGR5A1;
  3026. fWithAlpha := tfBGR5A1;
  3027. fWithoutAlpha := tfBGR5;
  3028. fRGBInverted := tfRGB5A1;
  3029. fRange.r := $1F;
  3030. fRange.g := $1F;
  3031. fRange.b := $1F;
  3032. fRange.a := $01;
  3033. fShift.r := 10;
  3034. fShift.g := 5;
  3035. fShift.b := 0;
  3036. fShift.a := 15;
  3037. fglFormat := GL_BGRA;
  3038. fglInternalFormat := GL_RGB5_A1;
  3039. fglDataFormat := GL_UNSIGNED_SHORT_1_5_5_5_REV;
  3040. end;
  3041. constructor TfdBGRA8.Create;
  3042. begin
  3043. inherited Create;
  3044. fFormat := tfBGRA8;
  3045. fWithAlpha := tfBGRA8;
  3046. fWithoutAlpha := tfBGR8;
  3047. fRGBInverted := tfRGBA8;
  3048. fglInternalFormat := GL_RGBA8;
  3049. end;
  3050. constructor TfdBGR10A2.Create;
  3051. begin
  3052. inherited Create;
  3053. fFormat := tfBGR10A2;
  3054. fWithAlpha := tfBGR10A2;
  3055. fWithoutAlpha := tfBGR10;
  3056. fRGBInverted := tfRGB10A2;
  3057. fRange.r := $3FF;
  3058. fRange.g := $3FF;
  3059. fRange.b := $3FF;
  3060. fRange.a := $003;
  3061. fShift.r := 20;
  3062. fShift.g := 10;
  3063. fShift.b := 0;
  3064. fShift.a := 30;
  3065. fglFormat := GL_BGRA;
  3066. fglInternalFormat := GL_RGB10_A2;
  3067. fglDataFormat := GL_UNSIGNED_INT_2_10_10_10_REV;
  3068. end;
  3069. constructor TfdBGRA12.Create;
  3070. begin
  3071. inherited Create;
  3072. fFormat := tfBGRA12;
  3073. fWithAlpha := tfBGRA12;
  3074. fWithoutAlpha := tfBGR12;
  3075. fRGBInverted := tfRGBA12;
  3076. fglInternalFormat := GL_RGBA12;
  3077. end;
  3078. constructor TfdBGRA16.Create;
  3079. begin
  3080. inherited Create;
  3081. fFormat := tfBGRA16;
  3082. fWithAlpha := tfBGRA16;
  3083. fWithoutAlpha := tfBGR16;
  3084. fRGBInverted := tfRGBA16;
  3085. fglInternalFormat := GL_RGBA16;
  3086. end;
  3087. constructor TfdDepth16.Create;
  3088. begin
  3089. inherited Create;
  3090. fFormat := tfDepth16;
  3091. fWithAlpha := tfEmpty;
  3092. fWithoutAlpha := tfDepth16;
  3093. fglInternalFormat := GL_DEPTH_COMPONENT16;
  3094. end;
  3095. constructor TfdDepth24.Create;
  3096. begin
  3097. inherited Create;
  3098. fFormat := tfDepth24;
  3099. fWithAlpha := tfEmpty;
  3100. fWithoutAlpha := tfDepth24;
  3101. fglInternalFormat := GL_DEPTH_COMPONENT24;
  3102. end;
  3103. constructor TfdDepth32.Create;
  3104. begin
  3105. inherited Create;
  3106. fFormat := tfDepth32;
  3107. fWithAlpha := tfEmpty;
  3108. fWithoutAlpha := tfDepth32;
  3109. fglInternalFormat := GL_DEPTH_COMPONENT32;
  3110. end;
  3111. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3112. //TfdS3tcDtx1RGBA/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3113. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3114. procedure TfdS3tcDtx1RGBA.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  3115. begin
  3116. raise EglBitmap.Create('mapping for compressed formats is not supported');
  3117. end;
  3118. procedure TfdS3tcDtx1RGBA.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  3119. begin
  3120. raise EglBitmap.Create('mapping for compressed formats is not supported');
  3121. end;
  3122. constructor TfdS3tcDtx1RGBA.Create;
  3123. begin
  3124. inherited Create;
  3125. fFormat := tfS3tcDtx1RGBA;
  3126. fWithAlpha := tfS3tcDtx1RGBA;
  3127. fUncompressed := tfRGB5A1;
  3128. fPixelSize := 0.5;
  3129. fIsCompressed := true;
  3130. fglFormat := GL_COMPRESSED_RGBA;
  3131. fglInternalFormat := GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
  3132. fglDataFormat := GL_UNSIGNED_BYTE;
  3133. end;
  3134. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3135. //TfdS3tcDtx3RGBA/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3136. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3137. procedure TfdS3tcDtx3RGBA.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  3138. begin
  3139. raise EglBitmap.Create('mapping for compressed formats is not supported');
  3140. end;
  3141. procedure TfdS3tcDtx3RGBA.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  3142. begin
  3143. raise EglBitmap.Create('mapping for compressed formats is not supported');
  3144. end;
  3145. constructor TfdS3tcDtx3RGBA.Create;
  3146. begin
  3147. inherited Create;
  3148. fFormat := tfS3tcDtx3RGBA;
  3149. fWithAlpha := tfS3tcDtx3RGBA;
  3150. fUncompressed := tfRGBA8;
  3151. fPixelSize := 1.0;
  3152. fIsCompressed := true;
  3153. fglFormat := GL_COMPRESSED_RGBA;
  3154. fglInternalFormat := GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
  3155. fglDataFormat := GL_UNSIGNED_BYTE;
  3156. end;
  3157. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3158. //TfdS3tcDtx5RGBA/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3159. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3160. procedure TfdS3tcDtx5RGBA.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  3161. begin
  3162. raise EglBitmap.Create('mapping for compressed formats is not supported');
  3163. end;
  3164. procedure TfdS3tcDtx5RGBA.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  3165. begin
  3166. raise EglBitmap.Create('mapping for compressed formats is not supported');
  3167. end;
  3168. constructor TfdS3tcDtx5RGBA.Create;
  3169. begin
  3170. inherited Create;
  3171. fFormat := tfS3tcDtx3RGBA;
  3172. fWithAlpha := tfS3tcDtx3RGBA;
  3173. fUncompressed := tfRGBA8;
  3174. fPixelSize := 1.0;
  3175. fIsCompressed := true;
  3176. fglFormat := GL_COMPRESSED_RGBA;
  3177. fglInternalFormat := GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
  3178. fglDataFormat := GL_UNSIGNED_BYTE;
  3179. end;
  3180. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3181. //TFormatDescriptor///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3182. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3183. class procedure TFormatDescriptor.Init;
  3184. begin
  3185. if not Assigned(FormatDescriptorCS) then
  3186. FormatDescriptorCS := TCriticalSection.Create;
  3187. end;
  3188. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3189. class function TFormatDescriptor.Get(const aFormat: TglBitmapFormat): TFormatDescriptor;
  3190. begin
  3191. FormatDescriptorCS.Enter;
  3192. try
  3193. result := FormatDescriptors[aFormat];
  3194. if not Assigned(result) then begin
  3195. result := FORMAT_DESCRIPTOR_CLASSES[aFormat].Create;
  3196. FormatDescriptors[aFormat] := result;
  3197. end;
  3198. finally
  3199. FormatDescriptorCS.Leave;
  3200. end;
  3201. end;
  3202. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3203. class function TFormatDescriptor.GetWithAlpha(const aFormat: TglBitmapFormat): TFormatDescriptor;
  3204. begin
  3205. result := Get(Get(aFormat).WithAlpha);
  3206. end;
  3207. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3208. class procedure TFormatDescriptor.Clear;
  3209. var
  3210. f: TglBitmapFormat;
  3211. begin
  3212. FormatDescriptorCS.Enter;
  3213. try
  3214. for f := low(FormatDescriptors) to high(FormatDescriptors) do
  3215. FreeAndNil(FormatDescriptors[f]);
  3216. finally
  3217. FormatDescriptorCS.Leave;
  3218. end;
  3219. end;
  3220. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3221. class procedure TFormatDescriptor.Finalize;
  3222. begin
  3223. Clear;
  3224. FreeAndNil(FormatDescriptorCS);
  3225. end;
  3226. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3227. //TBitfieldFormat/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3228. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3229. procedure TbmpBitfieldFormat.SetRedMask(const aValue: QWord);
  3230. begin
  3231. Update(aValue, fRange.r, fShift.r);
  3232. end;
  3233. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3234. procedure TbmpBitfieldFormat.SetGreenMask(const aValue: QWord);
  3235. begin
  3236. Update(aValue, fRange.g, fShift.g);
  3237. end;
  3238. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3239. procedure TbmpBitfieldFormat.SetBlueMask(const aValue: QWord);
  3240. begin
  3241. Update(aValue, fRange.b, fShift.b);
  3242. end;
  3243. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3244. procedure TbmpBitfieldFormat.SetAlphaMask(const aValue: QWord);
  3245. begin
  3246. Update(aValue, fRange.a, fShift.a);
  3247. end;
  3248. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3249. procedure TbmpBitfieldFormat.Update(aMask: QWord; out aRange: Cardinal; out
  3250. aShift: Byte);
  3251. begin
  3252. aShift := 0;
  3253. aRange := 0;
  3254. if (aMask = 0) then
  3255. exit;
  3256. while (aMask > 0) and ((aMask and 1) = 0) do begin
  3257. inc(aShift);
  3258. aMask := aMask shr 1;
  3259. end;
  3260. aRange := 1;
  3261. while (aMask > 0) do begin
  3262. aRange := aRange shl 1;
  3263. aMask := aMask shr 1;
  3264. end;
  3265. dec(aRange);
  3266. fPixelSize := Round(GetTopMostBit(RedMask or GreenMask or BlueMask or AlphaMask) / 8);
  3267. end;
  3268. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3269. procedure TbmpBitfieldFormat.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  3270. var
  3271. data: QWord;
  3272. s: Integer;
  3273. begin
  3274. data :=
  3275. ((aPixel.Data.r and fRange.r) shl fShift.r) or
  3276. ((aPixel.Data.g and fRange.g) shl fShift.g) or
  3277. ((aPixel.Data.b and fRange.b) shl fShift.b) or
  3278. ((aPixel.Data.a and fRange.a) shl fShift.a);
  3279. s := Round(fPixelSize);
  3280. case s of
  3281. 1: aData^ := data;
  3282. 2: PWord(aData)^ := data;
  3283. 4: PCardinal(aData)^ := data;
  3284. 8: PQWord(aData)^ := data;
  3285. else
  3286. raise EglBitmap.CreateFmt('invalid pixel size: %.1f', [fPixelSize]);
  3287. end;
  3288. inc(aData, s);
  3289. end;
  3290. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3291. procedure TbmpBitfieldFormat.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  3292. var
  3293. data: QWord;
  3294. s, i: Integer;
  3295. begin
  3296. s := Round(fPixelSize);
  3297. case s of
  3298. 1: data := aData^;
  3299. 2: data := PWord(aData)^;
  3300. 4: data := PCardinal(aData)^;
  3301. 8: data := PQWord(aData)^;
  3302. else
  3303. raise EglBitmap.CreateFmt('invalid pixel size: %.1f', [fPixelSize]);
  3304. end;
  3305. for i := 0 to 3 do
  3306. aPixel.Data.arr[i] := (data shr fShift.arr[i]) and fRange.arr[i];
  3307. inc(aData, s);
  3308. end;
  3309. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3310. //TColorTableFormat///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3311. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3312. procedure TbmpColorTableFormat.CreateColorTable;
  3313. var
  3314. i: Integer;
  3315. begin
  3316. if not (Format in [tfLuminance4, tfLuminance8, tfR3G3B2]) then
  3317. raise EglBitmap.Create(UNSUPPORTED_FORMAT);
  3318. if (Format = tfLuminance4) then
  3319. SetLength(fColorTable, 16)
  3320. else
  3321. SetLength(fColorTable, 256);
  3322. case Format of
  3323. tfLuminance4: begin
  3324. for i := 0 to High(fColorTable) do begin
  3325. fColorTable[i].r := 16 * i;
  3326. fColorTable[i].g := 16 * i;
  3327. fColorTable[i].b := 16 * i;
  3328. fColorTable[i].a := 0;
  3329. end;
  3330. end;
  3331. tfLuminance8: begin
  3332. for i := 0 to High(fColorTable) do begin
  3333. fColorTable[i].r := i;
  3334. fColorTable[i].g := i;
  3335. fColorTable[i].b := i;
  3336. fColorTable[i].a := 0;
  3337. end;
  3338. end;
  3339. tfR3G3B2: begin
  3340. for i := 0 to High(fColorTable) do begin
  3341. fColorTable[i].r := Round(((i shr Shift.r) and Range.r) / Range.r * 255);
  3342. fColorTable[i].g := Round(((i shr Shift.g) and Range.g) / Range.g * 255);
  3343. fColorTable[i].b := Round(((i shr Shift.b) and Range.b) / Range.b * 255);
  3344. fColorTable[i].a := 0;
  3345. end;
  3346. end;
  3347. end;
  3348. end;
  3349. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3350. procedure TbmpColorTableFormat.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  3351. var
  3352. d: Byte;
  3353. begin
  3354. if not (Format in [tfLuminance4, tfLuminance8, tfR3G3B2]) then
  3355. raise EglBitmap.Create(UNSUPPORTED_FORMAT);
  3356. case Format of
  3357. tfLuminance4: begin
  3358. if (aMapData = nil) then
  3359. aData^ := 0;
  3360. d := LuminanceWeight(aPixel) and Range.r;
  3361. aData^ := aData^ or (d shl (4 - {%H-}PtrUInt(aMapData)));
  3362. inc(PByte(aMapData), 4);
  3363. if ({%H-}PtrUInt(aMapData) >= 8) then begin
  3364. inc(aData);
  3365. aMapData := nil;
  3366. end;
  3367. end;
  3368. tfLuminance8: begin
  3369. aData^ := LuminanceWeight(aPixel) and Range.r;
  3370. inc(aData);
  3371. end;
  3372. tfR3G3B2: begin
  3373. aData^ := Round(
  3374. ((aPixel.Data.r and Range.r) shl Shift.r) or
  3375. ((aPixel.Data.g and Range.g) shl Shift.g) or
  3376. ((aPixel.Data.b and Range.b) shl Shift.b));
  3377. inc(aData);
  3378. end;
  3379. end;
  3380. end;
  3381. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3382. procedure TbmpColorTableFormat.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  3383. var
  3384. idx: QWord;
  3385. s: Integer;
  3386. bits: Byte;
  3387. f: Single;
  3388. begin
  3389. s := Trunc(fPixelSize);
  3390. f := fPixelSize - s;
  3391. bits := Round(8 * f);
  3392. case s of
  3393. 0: idx := (aData^ shr (8 - bits - {%H-}PtrInt(aMapData))) and ((1 shl bits) - 1);
  3394. 1: idx := aData^;
  3395. 2: idx := PWord(aData)^;
  3396. 4: idx := PCardinal(aData)^;
  3397. 8: idx := PQWord(aData)^;
  3398. else
  3399. raise EglBitmap.CreateFmt('invalid pixel size: %.3f', [fPixelSize]);
  3400. end;
  3401. if (idx >= Length(fColorTable)) then
  3402. raise EglBitmap.CreateFmt('invalid color index: %d', [idx]);
  3403. with fColorTable[idx] do begin
  3404. aPixel.Data.r := r;
  3405. aPixel.Data.g := g;
  3406. aPixel.Data.b := b;
  3407. aPixel.Data.a := a;
  3408. end;
  3409. inc(PByte(aMapData), bits);
  3410. if ({%H-}PtrUInt(aMapData) >= 8) then begin
  3411. inc(aData, 1);
  3412. dec(PByte(aMapData), 8);
  3413. end;
  3414. inc(aData, s);
  3415. end;
  3416. destructor TbmpColorTableFormat.Destroy;
  3417. begin
  3418. SetLength(fColorTable, 0);
  3419. inherited Destroy;
  3420. end;
  3421. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3422. //TglBitmap - Helper//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3423. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3424. procedure glBitmapConvertPixel(var aPixel: TglBitmapPixelData; const aSourceFD, aDestFD: TFormatDescriptor);
  3425. var
  3426. i: Integer;
  3427. begin
  3428. for i := 0 to 3 do begin
  3429. if (aSourceFD.Range.arr[i] <> aDestFD.Range.arr[i]) then begin
  3430. if (aSourceFD.Range.arr[i] > 0) then
  3431. aPixel.Data.arr[i] := Round(aPixel.Data.arr[i] / aSourceFD.Range.arr[i] * aDestFD.Range.arr[i])
  3432. else
  3433. aPixel.Data.arr[i] := aDestFD.Range.arr[i];
  3434. end;
  3435. end;
  3436. end;
  3437. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3438. procedure glBitmapConvertCopyFunc(var aFuncRec: TglBitmapFunctionRec);
  3439. begin
  3440. with aFuncRec do begin
  3441. if (Source.Range.r > 0) then
  3442. Dest.Data.r := Source.Data.r;
  3443. if (Source.Range.g > 0) then
  3444. Dest.Data.g := Source.Data.g;
  3445. if (Source.Range.b > 0) then
  3446. Dest.Data.b := Source.Data.b;
  3447. if (Source.Range.a > 0) then
  3448. Dest.Data.a := Source.Data.a;
  3449. end;
  3450. end;
  3451. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3452. procedure glBitmapConvertCalculateRGBAFunc(var aFuncRec: TglBitmapFunctionRec);
  3453. var
  3454. i: Integer;
  3455. begin
  3456. with aFuncRec do begin
  3457. for i := 0 to 3 do
  3458. if (Source.Range.arr[i] > 0) then
  3459. Dest.Data.arr[i] := Round(Dest.Range.arr[i] * Source.Data.arr[i] / Source.Range.arr[i]);
  3460. end;
  3461. end;
  3462. type
  3463. TShiftData = packed record
  3464. case Integer of
  3465. 0: (r, g, b, a: SmallInt);
  3466. 1: (arr: array[0..3] of SmallInt);
  3467. end;
  3468. PShiftData = ^TShiftData;
  3469. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3470. procedure glBitmapConvertShiftRGBAFunc(var aFuncRec: TglBitmapFunctionRec);
  3471. var
  3472. i: Integer;
  3473. begin
  3474. with aFuncRec do
  3475. for i := 0 to 3 do
  3476. if (Source.Range.arr[i] > 0) then
  3477. Dest.Data.arr[i] := Source.Data.arr[i] shr PShiftData(Args)^.arr[i];
  3478. end;
  3479. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3480. procedure glBitmapInvertFunc(var aFuncRec: TglBitmapFunctionRec);
  3481. begin
  3482. with aFuncRec do begin
  3483. Dest.Data := Source.Data;
  3484. if ({%H-}PtrUInt(Args) and $1 > 0) then begin
  3485. Dest.Data.r := Dest.Data.r xor Dest.Range.r;
  3486. Dest.Data.g := Dest.Data.g xor Dest.Range.g;
  3487. Dest.Data.b := Dest.Data.b xor Dest.Range.b;
  3488. end;
  3489. if ({%H-}PtrUInt(Args) and $2 > 0) then begin
  3490. Dest.Data.a := Dest.Data.a xor Dest.Range.a;
  3491. end;
  3492. end;
  3493. end;
  3494. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3495. procedure glBitmapFillWithColorFunc(var aFuncRec: TglBitmapFunctionRec);
  3496. var
  3497. i: Integer;
  3498. begin
  3499. with aFuncRec do begin
  3500. for i := 0 to 3 do
  3501. Dest.Data.arr[i] := PglBitmapPixelData(Args)^.Data.arr[i];
  3502. end;
  3503. end;
  3504. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3505. procedure glBitmapAlphaFunc(var FuncRec: TglBitmapFunctionRec);
  3506. var
  3507. Temp: Single;
  3508. begin
  3509. with FuncRec do begin
  3510. if (FuncRec.Args = nil) then begin //source has no alpha
  3511. Temp :=
  3512. Source.Data.r / Source.Range.r * ALPHA_WEIGHT_R +
  3513. Source.Data.g / Source.Range.g * ALPHA_WEIGHT_G +
  3514. Source.Data.b / Source.Range.b * ALPHA_WEIGHT_B;
  3515. Dest.Data.a := Round(Dest.Range.a * Temp);
  3516. end else
  3517. Dest.Data.a := Round(Source.Data.a / Source.Range.a * Dest.Range.a);
  3518. end;
  3519. end;
  3520. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3521. procedure glBitmapColorKeyAlphaFunc(var FuncRec: TglBitmapFunctionRec);
  3522. type
  3523. PglBitmapPixelData = ^TglBitmapPixelData;
  3524. begin
  3525. with FuncRec do begin
  3526. Dest.Data.r := Source.Data.r;
  3527. Dest.Data.g := Source.Data.g;
  3528. Dest.Data.b := Source.Data.b;
  3529. with PglBitmapPixelData(Args)^ do
  3530. if ((Dest.Data.r <= Data.r) and (Dest.Data.r >= Range.r) and
  3531. (Dest.Data.g <= Data.g) and (Dest.Data.g >= Range.g) and
  3532. (Dest.Data.b <= Data.b) and (Dest.Data.b >= Range.b)) then
  3533. Dest.Data.a := 0
  3534. else
  3535. Dest.Data.a := Dest.Range.a;
  3536. end;
  3537. end;
  3538. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3539. procedure glBitmapValueAlphaFunc(var FuncRec: TglBitmapFunctionRec);
  3540. begin
  3541. with FuncRec do begin
  3542. Dest.Data.r := Source.Data.r;
  3543. Dest.Data.g := Source.Data.g;
  3544. Dest.Data.b := Source.Data.b;
  3545. Dest.Data.a := PCardinal(Args)^;
  3546. end;
  3547. end;
  3548. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3549. procedure SwapRGB(aData: PByte; aWidth: Integer; const aHasAlpha: Boolean);
  3550. type
  3551. PRGBPix = ^TRGBPix;
  3552. TRGBPix = array [0..2] of byte;
  3553. var
  3554. Temp: Byte;
  3555. begin
  3556. while aWidth > 0 do begin
  3557. Temp := PRGBPix(aData)^[0];
  3558. PRGBPix(aData)^[0] := PRGBPix(aData)^[2];
  3559. PRGBPix(aData)^[2] := Temp;
  3560. if aHasAlpha then
  3561. Inc(aData, 4)
  3562. else
  3563. Inc(aData, 3);
  3564. dec(aWidth);
  3565. end;
  3566. end;
  3567. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3568. //TglBitmap - PROTECTED///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3569. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3570. function TglBitmap.GetFormatDesc: TglBitmapFormatDescriptor;
  3571. begin
  3572. result := TFormatDescriptor.Get(Format);
  3573. end;
  3574. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3575. function TglBitmap.GetWidth: Integer;
  3576. begin
  3577. if (ffX in fDimension.Fields) then
  3578. result := fDimension.X
  3579. else
  3580. result := -1;
  3581. end;
  3582. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3583. function TglBitmap.GetHeight: Integer;
  3584. begin
  3585. if (ffY in fDimension.Fields) then
  3586. result := fDimension.Y
  3587. else
  3588. result := -1;
  3589. end;
  3590. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3591. function TglBitmap.GetFileWidth: Integer;
  3592. begin
  3593. result := Max(1, Width);
  3594. end;
  3595. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3596. function TglBitmap.GetFileHeight: Integer;
  3597. begin
  3598. result := Max(1, Height);
  3599. end;
  3600. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3601. procedure TglBitmap.SetCustomData(const aValue: Pointer);
  3602. begin
  3603. if fCustomData = aValue then
  3604. exit;
  3605. fCustomData := aValue;
  3606. end;
  3607. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3608. procedure TglBitmap.SetCustomName(const aValue: String);
  3609. begin
  3610. if fCustomName = aValue then
  3611. exit;
  3612. fCustomName := aValue;
  3613. end;
  3614. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3615. procedure TglBitmap.SetCustomNameW(const aValue: WideString);
  3616. begin
  3617. if fCustomNameW = aValue then
  3618. exit;
  3619. fCustomNameW := aValue;
  3620. end;
  3621. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3622. procedure TglBitmap.SetFreeDataOnDestroy(const aValue: Boolean);
  3623. begin
  3624. if fFreeDataOnDestroy = aValue then
  3625. exit;
  3626. fFreeDataOnDestroy := aValue;
  3627. end;
  3628. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3629. procedure TglBitmap.SetDeleteTextureOnFree(const aValue: Boolean);
  3630. begin
  3631. if fDeleteTextureOnFree = aValue then
  3632. exit;
  3633. fDeleteTextureOnFree := aValue;
  3634. end;
  3635. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3636. procedure TglBitmap.SetFormat(const aValue: TglBitmapFormat);
  3637. begin
  3638. if fFormat = aValue then
  3639. exit;
  3640. if TFormatDescriptor.Get(Format).PixelSize <> TFormatDescriptor.Get(aValue).PixelSize then
  3641. raise EglBitmapUnsupportedFormat.Create(Format);
  3642. SetDataPointer(fData, aValue, Width, Height); //be careful, Data could be freed by this method
  3643. end;
  3644. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3645. procedure TglBitmap.SetFreeDataAfterGenTexture(const aValue: Boolean);
  3646. begin
  3647. if fFreeDataAfterGenTexture = aValue then
  3648. exit;
  3649. fFreeDataAfterGenTexture := aValue;
  3650. end;
  3651. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3652. procedure TglBitmap.SetID(const aValue: Cardinal);
  3653. begin
  3654. if fID = aValue then
  3655. exit;
  3656. fID := aValue;
  3657. end;
  3658. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3659. procedure TglBitmap.SetMipMap(const aValue: TglBitmapMipMap);
  3660. begin
  3661. if fMipMap = aValue then
  3662. exit;
  3663. fMipMap := aValue;
  3664. end;
  3665. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3666. procedure TglBitmap.SetTarget(const aValue: Cardinal);
  3667. begin
  3668. if fTarget = aValue then
  3669. exit;
  3670. fTarget := aValue;
  3671. end;
  3672. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3673. procedure TglBitmap.SetAnisotropic(const aValue: Integer);
  3674. var
  3675. MaxAnisotropic: Integer;
  3676. begin
  3677. fAnisotropic := aValue;
  3678. if (ID > 0) then begin
  3679. if GL_EXT_texture_filter_anisotropic then begin
  3680. if fAnisotropic > 0 then begin
  3681. Bind(false);
  3682. glGetIntegerv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, @MaxAnisotropic);
  3683. if aValue > MaxAnisotropic then
  3684. fAnisotropic := MaxAnisotropic;
  3685. glTexParameteri(Target, GL_TEXTURE_MAX_ANISOTROPY_EXT, fAnisotropic);
  3686. end;
  3687. end else begin
  3688. fAnisotropic := 0;
  3689. end;
  3690. end;
  3691. end;
  3692. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3693. procedure TglBitmap.CreateID;
  3694. begin
  3695. if (ID <> 0) then
  3696. glDeleteTextures(1, @fID);
  3697. glGenTextures(1, @fID);
  3698. Bind(false);
  3699. end;
  3700. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3701. procedure TglBitmap.SetupParameters(out aBuildWithGlu: Boolean);
  3702. begin
  3703. // Set Up Parameters
  3704. SetWrap(fWrapS, fWrapT, fWrapR);
  3705. SetFilter(fFilterMin, fFilterMag);
  3706. SetAnisotropic(fAnisotropic);
  3707. SetBorderColor(fBorderColor[0], fBorderColor[1], fBorderColor[2], fBorderColor[3]);
  3708. if (GL_ARB_texture_swizzle or GL_EXT_texture_swizzle or GL_VERSION_3_3) then
  3709. SetSwizzle(fSwizzle[0], fSwizzle[1], fSwizzle[2], fSwizzle[3]);
  3710. // Mip Maps Generation Mode
  3711. aBuildWithGlu := false;
  3712. if (MipMap = mmMipmap) then begin
  3713. if (GL_VERSION_1_4 or GL_SGIS_generate_mipmap) then
  3714. glTexParameteri(Target, GL_GENERATE_MIPMAP, GL_TRUE)
  3715. else
  3716. aBuildWithGlu := true;
  3717. end else if (MipMap = mmMipmapGlu) then
  3718. aBuildWithGlu := true;
  3719. end;
  3720. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3721. procedure TglBitmap.SetDataPointer(var aData: PByte; const aFormat: TglBitmapFormat;
  3722. const aWidth: Integer; const aHeight: Integer);
  3723. var
  3724. s: Single;
  3725. begin
  3726. if (Data <> aData) then begin
  3727. if (Assigned(Data)) then
  3728. FreeMem(Data);
  3729. fData := aData;
  3730. end;
  3731. if not Assigned(fData) then begin
  3732. fPixelSize := 0;
  3733. fRowSize := 0;
  3734. end else begin
  3735. FillChar(fDimension, SizeOf(fDimension), 0);
  3736. if aWidth <> -1 then begin
  3737. fDimension.Fields := fDimension.Fields + [ffX];
  3738. fDimension.X := aWidth;
  3739. end;
  3740. if aHeight <> -1 then begin
  3741. fDimension.Fields := fDimension.Fields + [ffY];
  3742. fDimension.Y := aHeight;
  3743. end;
  3744. s := TFormatDescriptor.Get(aFormat).PixelSize;
  3745. fFormat := aFormat;
  3746. fPixelSize := Ceil(s);
  3747. fRowSize := Ceil(s * aWidth);
  3748. end;
  3749. end;
  3750. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3751. function TglBitmap.FlipHorz: Boolean;
  3752. begin
  3753. result := false;
  3754. end;
  3755. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3756. function TglBitmap.FlipVert: Boolean;
  3757. begin
  3758. result := false;
  3759. end;
  3760. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3761. //TglBitmap - PUBLIC//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3762. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3763. procedure TglBitmap.AfterConstruction;
  3764. begin
  3765. inherited AfterConstruction;
  3766. fID := 0;
  3767. fTarget := 0;
  3768. fIsResident := false;
  3769. fMipMap := glBitmapDefaultMipmap;
  3770. fFreeDataAfterGenTexture := glBitmapGetDefaultFreeDataAfterGenTexture;
  3771. fDeleteTextureOnFree := glBitmapGetDefaultDeleteTextureOnFree;
  3772. glBitmapGetDefaultFilter (fFilterMin, fFilterMag);
  3773. glBitmapGetDefaultTextureWrap(fWrapS, fWrapT, fWrapR);
  3774. glBitmapGetDefaultSwizzle (fSwizzle[0], fSwizzle[1], fSwizzle[2], fSwizzle[3]);
  3775. end;
  3776. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3777. procedure TglBitmap.BeforeDestruction;
  3778. var
  3779. NewData: PByte;
  3780. begin
  3781. if fFreeDataOnDestroy then begin
  3782. NewData := nil;
  3783. SetDataPointer(NewData, tfEmpty); //be careful, Data could be freed by this method
  3784. end;
  3785. if (fID > 0) and fDeleteTextureOnFree then
  3786. glDeleteTextures(1, @fID);
  3787. inherited BeforeDestruction;
  3788. end;
  3789. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3790. procedure TglBitmap.PrepareResType(var aResource: String; var aResType: PChar);
  3791. var
  3792. TempPos: Integer;
  3793. begin
  3794. if not Assigned(aResType) then begin
  3795. TempPos := Pos('.', aResource);
  3796. aResType := PChar(UpperCase(Copy(aResource, TempPos + 1, Length(aResource) - TempPos)));
  3797. aResource := UpperCase(Copy(aResource, 0, TempPos -1));
  3798. end;
  3799. end;
  3800. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3801. procedure TglBitmap.LoadFromFile(const aFilename: String);
  3802. var
  3803. fs: TFileStream;
  3804. begin
  3805. if not FileExists(aFilename) then
  3806. raise EglBitmap.Create('file does not exist: ' + aFilename);
  3807. fFilename := aFilename;
  3808. fs := TFileStream.Create(fFilename, fmOpenRead);
  3809. try
  3810. fs.Position := 0;
  3811. LoadFromStream(fs);
  3812. finally
  3813. fs.Free;
  3814. end;
  3815. end;
  3816. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3817. procedure TglBitmap.LoadFromStream(const aStream: TStream);
  3818. begin
  3819. {$IFDEF GLB_SUPPORT_PNG_READ}
  3820. if not LoadPNG(aStream) then
  3821. {$ENDIF}
  3822. {$IFDEF GLB_SUPPORT_JPEG_READ}
  3823. if not LoadJPEG(aStream) then
  3824. {$ENDIF}
  3825. if not LoadDDS(aStream) then
  3826. if not LoadTGA(aStream) then
  3827. if not LoadBMP(aStream) then
  3828. raise EglBitmap.Create('LoadFromStream - Couldn''t load Stream. It''s possible to be an unknow Streamtype.');
  3829. end;
  3830. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3831. procedure TglBitmap.LoadFromFunc(const aSize: TglBitmapPixelPosition; const aFunc: TglBitmapFunction;
  3832. const aFormat: TglBitmapFormat; const aArgs: Pointer);
  3833. var
  3834. tmpData: PByte;
  3835. size: Integer;
  3836. begin
  3837. size := TFormatDescriptor.Get(aFormat).GetSize(aSize);
  3838. GetMem(tmpData, size);
  3839. try
  3840. FillChar(tmpData^, size, #$FF);
  3841. SetDataPointer(tmpData, aFormat, aSize.X, aSize.Y); //be careful, Data could be freed by this method
  3842. except
  3843. if Assigned(tmpData) then
  3844. FreeMem(tmpData);
  3845. raise;
  3846. end;
  3847. AddFunc(Self, aFunc, false, aFormat, aArgs);
  3848. end;
  3849. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3850. procedure TglBitmap.LoadFromResource(const aInstance: Cardinal; aResource: String; aResType: PChar);
  3851. var
  3852. rs: TResourceStream;
  3853. begin
  3854. PrepareResType(aResource, aResType);
  3855. rs := TResourceStream.Create(aInstance, aResource, aResType);
  3856. try
  3857. LoadFromStream(rs);
  3858. finally
  3859. rs.Free;
  3860. end;
  3861. end;
  3862. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3863. procedure TglBitmap.LoadFromResourceID(const aInstance: Cardinal; const aResourceID: Integer; const aResType: PChar);
  3864. var
  3865. rs: TResourceStream;
  3866. begin
  3867. rs := TResourceStream.CreateFromID(aInstance, aResourceID, aResType);
  3868. try
  3869. LoadFromStream(rs);
  3870. finally
  3871. rs.Free;
  3872. end;
  3873. end;
  3874. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3875. procedure TglBitmap.SaveToFile(const aFileName: String; const aFileType: TglBitmapFileType);
  3876. var
  3877. fs: TFileStream;
  3878. begin
  3879. fs := TFileStream.Create(aFileName, fmCreate);
  3880. try
  3881. fs.Position := 0;
  3882. SaveToStream(fs, aFileType);
  3883. finally
  3884. fs.Free;
  3885. end;
  3886. end;
  3887. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3888. procedure TglBitmap.SaveToStream(const aStream: TStream; const aFileType: TglBitmapFileType);
  3889. begin
  3890. case aFileType of
  3891. {$IFDEF GLB_SUPPORT_PNG_WRITE}
  3892. ftPNG: SavePNG(aStream);
  3893. {$ENDIF}
  3894. {$IFDEF GLB_SUPPORT_JPEG_WRITE}
  3895. ftJPEG: SaveJPEG(aStream);
  3896. {$ENDIF}
  3897. ftDDS: SaveDDS(aStream);
  3898. ftTGA: SaveTGA(aStream);
  3899. ftBMP: SaveBMP(aStream);
  3900. end;
  3901. end;
  3902. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3903. function TglBitmap.AddFunc(const aFunc: TglBitmapFunction; const aCreateTemp: Boolean; const aArgs: Pointer): Boolean;
  3904. begin
  3905. result := AddFunc(Self, aFunc, aCreateTemp, Format, aArgs);
  3906. end;
  3907. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3908. function TglBitmap.AddFunc(const aSource: TglBitmap; const aFunc: TglBitmapFunction; aCreateTemp: Boolean;
  3909. const aFormat: TglBitmapFormat; const aArgs: Pointer): Boolean;
  3910. var
  3911. DestData, TmpData, SourceData: pByte;
  3912. TempHeight, TempWidth: Integer;
  3913. SourceFD, DestFD: TFormatDescriptor;
  3914. SourceMD, DestMD: Pointer;
  3915. FuncRec: TglBitmapFunctionRec;
  3916. begin
  3917. Assert(Assigned(Data));
  3918. Assert(Assigned(aSource));
  3919. Assert(Assigned(aSource.Data));
  3920. result := false;
  3921. if Assigned(aSource.Data) and ((aSource.Height > 0) or (aSource.Width > 0)) then begin
  3922. SourceFD := TFormatDescriptor.Get(aSource.Format);
  3923. DestFD := TFormatDescriptor.Get(aFormat);
  3924. if (SourceFD.IsCompressed) then
  3925. raise EglBitmapUnsupportedFormat.Create('compressed formats are not supported: ', SourceFD.Format);
  3926. if (DestFD.IsCompressed) then
  3927. raise EglBitmapUnsupportedFormat.Create('compressed formats are not supported: ', DestFD.Format);
  3928. // inkompatible Formats so CreateTemp
  3929. if (SourceFD.PixelSize <> DestFD.PixelSize) then
  3930. aCreateTemp := true;
  3931. // Values
  3932. TempHeight := Max(1, aSource.Height);
  3933. TempWidth := Max(1, aSource.Width);
  3934. FuncRec.Sender := Self;
  3935. FuncRec.Args := aArgs;
  3936. TmpData := nil;
  3937. if aCreateTemp then begin
  3938. GetMem(TmpData, DestFD.GetSize(TempWidth, TempHeight));
  3939. DestData := TmpData;
  3940. end else
  3941. DestData := Data;
  3942. try
  3943. SourceFD.PreparePixel(FuncRec.Source);
  3944. DestFD.PreparePixel (FuncRec.Dest);
  3945. SourceMD := SourceFD.CreateMappingData;
  3946. DestMD := DestFD.CreateMappingData;
  3947. FuncRec.Size := aSource.Dimension;
  3948. FuncRec.Position.Fields := FuncRec.Size.Fields;
  3949. try
  3950. SourceData := aSource.Data;
  3951. FuncRec.Position.Y := 0;
  3952. while FuncRec.Position.Y < TempHeight do begin
  3953. FuncRec.Position.X := 0;
  3954. while FuncRec.Position.X < TempWidth do begin
  3955. SourceFD.Unmap(SourceData, FuncRec.Source, SourceMD);
  3956. aFunc(FuncRec);
  3957. DestFD.Map(FuncRec.Dest, DestData, DestMD);
  3958. inc(FuncRec.Position.X);
  3959. end;
  3960. inc(FuncRec.Position.Y);
  3961. end;
  3962. // Updating Image or InternalFormat
  3963. if aCreateTemp then
  3964. SetDataPointer(TmpData, aFormat, aSource.Width, aSource.Height) //be careful, Data could be freed by this method
  3965. else if (aFormat <> fFormat) then
  3966. Format := aFormat;
  3967. result := true;
  3968. finally
  3969. SourceFD.FreeMappingData(SourceMD);
  3970. DestFD.FreeMappingData(DestMD);
  3971. end;
  3972. except
  3973. if aCreateTemp and Assigned(TmpData) then
  3974. FreeMem(TmpData);
  3975. raise;
  3976. end;
  3977. end;
  3978. end;
  3979. {$IFDEF GLB_SDL}
  3980. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3981. function TglBitmap.AssignToSurface(out aSurface: PSDL_Surface): Boolean;
  3982. var
  3983. Row, RowSize: Integer;
  3984. SourceData, TmpData: PByte;
  3985. TempDepth: Integer;
  3986. FormatDesc: TFormatDescriptor;
  3987. function GetRowPointer(Row: Integer): pByte;
  3988. begin
  3989. result := aSurface.pixels;
  3990. Inc(result, Row * RowSize);
  3991. end;
  3992. begin
  3993. result := false;
  3994. FormatDesc := TFormatDescriptor.Get(Format);
  3995. if FormatDesc.IsCompressed then
  3996. raise EglBitmapUnsupportedFormat.Create(Format);
  3997. if Assigned(Data) then begin
  3998. case Trunc(FormatDesc.PixelSize) of
  3999. 1: TempDepth := 8;
  4000. 2: TempDepth := 16;
  4001. 3: TempDepth := 24;
  4002. 4: TempDepth := 32;
  4003. else
  4004. raise EglBitmapUnsupportedFormat.Create(Format);
  4005. end;
  4006. aSurface := SDL_CreateRGBSurface(SDL_SWSURFACE, Width, Height, TempDepth,
  4007. FormatDesc.RedMask, FormatDesc.GreenMask, FormatDesc.BlueMask, FormatDesc.AlphaMask);
  4008. SourceData := Data;
  4009. RowSize := FormatDesc.GetSize(FileWidth, 1);
  4010. for Row := 0 to FileHeight-1 do begin
  4011. TmpData := GetRowPointer(Row);
  4012. if Assigned(TmpData) then begin
  4013. Move(SourceData^, TmpData^, RowSize);
  4014. inc(SourceData, RowSize);
  4015. end;
  4016. end;
  4017. result := true;
  4018. end;
  4019. end;
  4020. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4021. function TglBitmap.AssignFromSurface(const aSurface: PSDL_Surface): Boolean;
  4022. var
  4023. pSource, pData, pTempData: PByte;
  4024. Row, RowSize, TempWidth, TempHeight: Integer;
  4025. IntFormat: TglBitmapFormat;
  4026. FormatDesc: TFormatDescriptor;
  4027. function GetRowPointer(Row: Integer): pByte;
  4028. begin
  4029. result := aSurface^.pixels;
  4030. Inc(result, Row * RowSize);
  4031. end;
  4032. begin
  4033. result := false;
  4034. if (Assigned(aSurface)) then begin
  4035. with aSurface^.format^ do begin
  4036. for IntFormat := High(TglBitmapFormat) to Low(TglBitmapFormat) do begin
  4037. FormatDesc := TFormatDescriptor.Get(IntFormat);
  4038. if (FormatDesc.MaskMatch(RMask, GMask, BMask, AMask)) then
  4039. break;
  4040. end;
  4041. if (IntFormat = tfEmpty) then
  4042. raise EglBitmapException.Create('AssignFromSurface - Invalid Pixelformat.');
  4043. end;
  4044. TempWidth := aSurface^.w;
  4045. TempHeight := aSurface^.h;
  4046. RowSize := FormatDesc.GetSize(TempWidth, 1);
  4047. GetMem(pData, TempHeight * RowSize);
  4048. try
  4049. pTempData := pData;
  4050. for Row := 0 to TempHeight -1 do begin
  4051. pSource := GetRowPointer(Row);
  4052. if (Assigned(pSource)) then begin
  4053. Move(pSource^, pTempData^, RowSize);
  4054. Inc(pTempData, RowSize);
  4055. end;
  4056. end;
  4057. SetDataPointer(pData, IntFormat, TempWidth, TempHeight); //be careful, Data could be freed by this method
  4058. result := true;
  4059. except
  4060. if Assigned(pData) then
  4061. FreeMem(pData);
  4062. raise;
  4063. end;
  4064. end;
  4065. end;
  4066. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4067. function TglBitmap.AssignAlphaToSurface(out aSurface: PSDL_Surface): Boolean;
  4068. var
  4069. Row, Col, AlphaInterleave: Integer;
  4070. pSource, pDest: PByte;
  4071. function GetRowPointer(Row: Integer): pByte;
  4072. begin
  4073. result := aSurface.pixels;
  4074. Inc(result, Row * Width);
  4075. end;
  4076. begin
  4077. result := false;
  4078. if Assigned(Data) then begin
  4079. if Format in [tfAlpha8, tfLuminance8Alpha8, tfBGRA8, tfRGBA8] then begin
  4080. aSurface := SDL_CreateRGBSurface(SDL_SWSURFACE, Width, Height, 8, $FF, $FF, $FF, 0);
  4081. AlphaInterleave := 0;
  4082. case Format of
  4083. tfLuminance8Alpha8:
  4084. AlphaInterleave := 1;
  4085. tfBGRA8, tfRGBA8:
  4086. AlphaInterleave := 3;
  4087. end;
  4088. pSource := Data;
  4089. for Row := 0 to Height -1 do begin
  4090. pDest := GetRowPointer(Row);
  4091. if Assigned(pDest) then begin
  4092. for Col := 0 to Width -1 do begin
  4093. Inc(pSource, AlphaInterleave);
  4094. pDest^ := pSource^;
  4095. Inc(pDest);
  4096. Inc(pSource);
  4097. end;
  4098. end;
  4099. end;
  4100. result := true;
  4101. end;
  4102. end;
  4103. end;
  4104. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4105. function TglBitmap.AddAlphaFromSurface(const aSurface: PSDL_Surface; const aFunc: TglBitmapFunction = nil; const aArgs: Pointer = nil): Boolean;
  4106. var
  4107. bmp: TglBitmap2D;
  4108. begin
  4109. bmp := TglBitmap2D.Create;
  4110. try
  4111. bmp.AssignFromSurface(aSurface);
  4112. result := AddAlphaFromGlBitmap(bmp, aFunc, aArgs);
  4113. finally
  4114. bmp.Free;
  4115. end;
  4116. end;
  4117. {$ENDIF}
  4118. {$IFDEF GLB_DELPHI}
  4119. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4120. function CreateGrayPalette: HPALETTE;
  4121. var
  4122. Idx: Integer;
  4123. Pal: PLogPalette;
  4124. begin
  4125. GetMem(Pal, SizeOf(TLogPalette) + (SizeOf(TPaletteEntry) * 256));
  4126. Pal.palVersion := $300;
  4127. Pal.palNumEntries := 256;
  4128. for Idx := 0 to Pal.palNumEntries - 1 do begin
  4129. Pal.palPalEntry[Idx].peRed := Idx;
  4130. Pal.palPalEntry[Idx].peGreen := Idx;
  4131. Pal.palPalEntry[Idx].peBlue := Idx;
  4132. Pal.palPalEntry[Idx].peFlags := 0;
  4133. end;
  4134. Result := CreatePalette(Pal^);
  4135. FreeMem(Pal);
  4136. end;
  4137. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4138. function TglBitmap.AssignToBitmap(const aBitmap: TBitmap): Boolean;
  4139. var
  4140. Row: Integer;
  4141. pSource, pData: PByte;
  4142. begin
  4143. result := false;
  4144. if Assigned(Data) then begin
  4145. if Assigned(aBitmap) then begin
  4146. aBitmap.Width := Width;
  4147. aBitmap.Height := Height;
  4148. case Format of
  4149. tfAlpha8, tfLuminance8: begin
  4150. aBitmap.PixelFormat := pf8bit;
  4151. aBitmap.Palette := CreateGrayPalette;
  4152. end;
  4153. tfRGB5A1:
  4154. aBitmap.PixelFormat := pf15bit;
  4155. tfR5G6B5:
  4156. aBitmap.PixelFormat := pf16bit;
  4157. tfRGB8, tfBGR8:
  4158. aBitmap.PixelFormat := pf24bit;
  4159. tfRGBA8, tfBGRA8:
  4160. aBitmap.PixelFormat := pf32bit;
  4161. else
  4162. raise EglBitmap.Create('AssignToBitmap - Invalid Pixelformat.');
  4163. end;
  4164. pSource := Data;
  4165. for Row := 0 to FileHeight -1 do begin
  4166. pData := aBitmap.Scanline[Row];
  4167. Move(pSource^, pData^, fRowSize);
  4168. Inc(pSource, fRowSize);
  4169. if (Format in [tfRGB8, tfRGBA8]) then // swap RGB(A) to BGR(A)
  4170. SwapRGB(pData, FileWidth, Format = tfRGBA8);
  4171. end;
  4172. result := true;
  4173. end;
  4174. end;
  4175. end;
  4176. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4177. function TglBitmap.AssignFromBitmap(const aBitmap: TBitmap): Boolean;
  4178. var
  4179. pSource, pData, pTempData: PByte;
  4180. Row, RowSize, TempWidth, TempHeight: Integer;
  4181. IntFormat: TglBitmapFormat;
  4182. begin
  4183. result := false;
  4184. if (Assigned(aBitmap)) then begin
  4185. case aBitmap.PixelFormat of
  4186. pf8bit:
  4187. IntFormat := tfLuminance8;
  4188. pf15bit:
  4189. IntFormat := tfRGB5A1;
  4190. pf16bit:
  4191. IntFormat := tfR5G6B5;
  4192. pf24bit:
  4193. IntFormat := tfBGR8;
  4194. pf32bit:
  4195. IntFormat := tfBGRA8;
  4196. else
  4197. raise EglBitmap.Create('AssignFromBitmap - Invalid Pixelformat.');
  4198. end;
  4199. TempWidth := aBitmap.Width;
  4200. TempHeight := aBitmap.Height;
  4201. RowSize := TFormatDescriptor.Get(IntFormat).GetSize(TempWidth, 1);
  4202. GetMem(pData, TempHeight * RowSize);
  4203. try
  4204. pTempData := pData;
  4205. for Row := 0 to TempHeight -1 do begin
  4206. pSource := aBitmap.Scanline[Row];
  4207. if (Assigned(pSource)) then begin
  4208. Move(pSource^, pTempData^, RowSize);
  4209. Inc(pTempData, RowSize);
  4210. end;
  4211. end;
  4212. SetDataPointer(pData, IntFormat, TempWidth, TempHeight); //be careful, Data could be freed by this method
  4213. result := true;
  4214. except
  4215. if Assigned(pData) then
  4216. FreeMem(pData);
  4217. raise;
  4218. end;
  4219. end;
  4220. end;
  4221. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4222. function TglBitmap.AssignAlphaToBitmap(const aBitmap: TBitmap): Boolean;
  4223. var
  4224. Row, Col, AlphaInterleave: Integer;
  4225. pSource, pDest: PByte;
  4226. begin
  4227. result := false;
  4228. if Assigned(Data) then begin
  4229. if (Format in [tfAlpha8, tfLuminance8Alpha8, tfRGBA8, tfBGRA8]) then begin
  4230. if Assigned(aBitmap) then begin
  4231. aBitmap.PixelFormat := pf8bit;
  4232. aBitmap.Palette := CreateGrayPalette;
  4233. aBitmap.Width := Width;
  4234. aBitmap.Height := Height;
  4235. case Format of
  4236. tfLuminance8Alpha8:
  4237. AlphaInterleave := 1;
  4238. tfRGBA8, tfBGRA8:
  4239. AlphaInterleave := 3;
  4240. else
  4241. AlphaInterleave := 0;
  4242. end;
  4243. // Copy Data
  4244. pSource := Data;
  4245. for Row := 0 to Height -1 do begin
  4246. pDest := aBitmap.Scanline[Row];
  4247. if Assigned(pDest) then begin
  4248. for Col := 0 to Width -1 do begin
  4249. Inc(pSource, AlphaInterleave);
  4250. pDest^ := pSource^;
  4251. Inc(pDest);
  4252. Inc(pSource);
  4253. end;
  4254. end;
  4255. end;
  4256. result := true;
  4257. end;
  4258. end;
  4259. end;
  4260. end;
  4261. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4262. function TglBitmap.AddAlphaFromBitmap(const aBitmap: TBitmap; const aFunc: TglBitmapFunction; const aArgs: Pointer): Boolean;
  4263. var
  4264. tex: TglBitmap2D;
  4265. begin
  4266. tex := TglBitmap2D.Create;
  4267. try
  4268. tex.AssignFromBitmap(ABitmap);
  4269. result := AddAlphaFromglBitmap(tex, aFunc, aArgs);
  4270. finally
  4271. tex.Free;
  4272. end;
  4273. end;
  4274. {$ENDIF}
  4275. {$IFDEF GLB_LAZARUS}
  4276. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4277. function TglBitmap.AssignToLazIntfImage(const aImage: TLazIntfImage): Boolean;
  4278. var
  4279. rid: TRawImageDescription;
  4280. FormatDesc: TFormatDescriptor;
  4281. begin
  4282. result := false;
  4283. if not Assigned(aImage) or (Format = tfEmpty) then
  4284. exit;
  4285. FormatDesc := TFormatDescriptor.Get(Format);
  4286. if FormatDesc.IsCompressed then
  4287. exit;
  4288. FillChar(rid{%H-}, SizeOf(rid), 0);
  4289. if (Format in [
  4290. tfAlpha4, tfAlpha8, tfAlpha12, tfAlpha16,
  4291. tfLuminance4, tfLuminance8, tfLuminance12, tfLuminance16,
  4292. tfLuminance4Alpha4, tfLuminance8Alpha8, tfLuminance12Alpha4, tfLuminance12Alpha12, tfLuminance16Alpha16]) then
  4293. rid.Format := ricfGray
  4294. else
  4295. rid.Format := ricfRGBA;
  4296. rid.Width := Width;
  4297. rid.Height := Height;
  4298. rid.Depth := CountSetBits(FormatDesc.RedMask or FormatDesc.GreenMask or FormatDesc.BlueMask or FormatDesc.AlphaMask);
  4299. rid.BitOrder := riboBitsInOrder;
  4300. rid.ByteOrder := riboLSBFirst;
  4301. rid.LineOrder := riloTopToBottom;
  4302. rid.LineEnd := rileTight;
  4303. rid.BitsPerPixel := Round(8 * FormatDesc.PixelSize);
  4304. rid.RedPrec := CountSetBits(FormatDesc.Range.r);
  4305. rid.GreenPrec := CountSetBits(FormatDesc.Range.g);
  4306. rid.BluePrec := CountSetBits(FormatDesc.Range.b);
  4307. rid.AlphaPrec := CountSetBits(FormatDesc.Range.a);
  4308. rid.RedShift := FormatDesc.Shift.r;
  4309. rid.GreenShift := FormatDesc.Shift.g;
  4310. rid.BlueShift := FormatDesc.Shift.b;
  4311. rid.AlphaShift := FormatDesc.Shift.a;
  4312. rid.MaskBitsPerPixel := 0;
  4313. rid.PaletteColorCount := 0;
  4314. aImage.DataDescription := rid;
  4315. aImage.CreateData;
  4316. Move(Data^, aImage.PixelData^, FormatDesc.GetSize(Dimension));
  4317. result := true;
  4318. end;
  4319. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4320. function TglBitmap.AssignFromLazIntfImage(const aImage: TLazIntfImage): Boolean;
  4321. var
  4322. f: TglBitmapFormat;
  4323. FormatDesc: TFormatDescriptor;
  4324. ImageData: PByte;
  4325. ImageSize: Integer;
  4326. begin
  4327. result := false;
  4328. if not Assigned(aImage) then
  4329. exit;
  4330. for f := High(f) downto Low(f) do begin
  4331. FormatDesc := TFormatDescriptor.Get(f);
  4332. with aImage.DataDescription do
  4333. if FormatDesc.MaskMatch(
  4334. (QWord(1 shl RedPrec )-1) shl RedShift,
  4335. (QWord(1 shl GreenPrec)-1) shl GreenShift,
  4336. (QWord(1 shl BluePrec )-1) shl BlueShift,
  4337. (QWord(1 shl AlphaPrec)-1) shl AlphaShift) then
  4338. break;
  4339. end;
  4340. if (f = tfEmpty) then
  4341. exit;
  4342. ImageSize := FormatDesc.GetSize(aImage.Width, aImage.Height);
  4343. ImageData := GetMem(ImageSize);
  4344. try
  4345. Move(aImage.PixelData^, ImageData^, (aImage.Width * aImage.Height * aImage.DataDescription.BitsPerPixel) shr 3);
  4346. SetDataPointer(ImageData, f, aImage.Width, aImage.Height); //be careful, Data could be freed by this method
  4347. except
  4348. if Assigned(ImageData) then
  4349. FreeMem(ImageData);
  4350. raise;
  4351. end;
  4352. result := true;
  4353. end;
  4354. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4355. function TglBitmap.AssignAlphaToLazIntfImage(const aImage: TLazIntfImage): Boolean;
  4356. var
  4357. rid: TRawImageDescription;
  4358. FormatDesc: TFormatDescriptor;
  4359. Pixel: TglBitmapPixelData;
  4360. x, y: Integer;
  4361. srcMD: Pointer;
  4362. src, dst: PByte;
  4363. begin
  4364. result := false;
  4365. if not Assigned(aImage) or (Format = tfEmpty) then
  4366. exit;
  4367. FormatDesc := TFormatDescriptor.Get(Format);
  4368. if FormatDesc.IsCompressed or not FormatDesc.HasAlpha then
  4369. exit;
  4370. FillChar(rid{%H-}, SizeOf(rid), 0);
  4371. rid.Format := ricfGray;
  4372. rid.Width := Width;
  4373. rid.Height := Height;
  4374. rid.Depth := CountSetBits(FormatDesc.Range.a);
  4375. rid.BitOrder := riboBitsInOrder;
  4376. rid.ByteOrder := riboLSBFirst;
  4377. rid.LineOrder := riloTopToBottom;
  4378. rid.LineEnd := rileTight;
  4379. rid.BitsPerPixel := 8 * Ceil(rid.Depth / 8);
  4380. rid.RedPrec := CountSetBits(FormatDesc.Range.a);
  4381. rid.GreenPrec := 0;
  4382. rid.BluePrec := 0;
  4383. rid.AlphaPrec := 0;
  4384. rid.RedShift := 0;
  4385. rid.GreenShift := 0;
  4386. rid.BlueShift := 0;
  4387. rid.AlphaShift := 0;
  4388. rid.MaskBitsPerPixel := 0;
  4389. rid.PaletteColorCount := 0;
  4390. aImage.DataDescription := rid;
  4391. aImage.CreateData;
  4392. srcMD := FormatDesc.CreateMappingData;
  4393. try
  4394. FormatDesc.PreparePixel(Pixel);
  4395. src := Data;
  4396. dst := aImage.PixelData;
  4397. for y := 0 to Height-1 do
  4398. for x := 0 to Width-1 do begin
  4399. FormatDesc.Unmap(src, Pixel, srcMD);
  4400. case rid.BitsPerPixel of
  4401. 8: begin
  4402. dst^ := Pixel.Data.a;
  4403. inc(dst);
  4404. end;
  4405. 16: begin
  4406. PWord(dst)^ := Pixel.Data.a;
  4407. inc(dst, 2);
  4408. end;
  4409. 24: begin
  4410. PByteArray(dst)^[0] := PByteArray(@Pixel.Data.a)^[0];
  4411. PByteArray(dst)^[1] := PByteArray(@Pixel.Data.a)^[1];
  4412. PByteArray(dst)^[2] := PByteArray(@Pixel.Data.a)^[2];
  4413. inc(dst, 3);
  4414. end;
  4415. 32: begin
  4416. PCardinal(dst)^ := Pixel.Data.a;
  4417. inc(dst, 4);
  4418. end;
  4419. else
  4420. raise EglBitmapUnsupportedFormat.Create(Format);
  4421. end;
  4422. end;
  4423. finally
  4424. FormatDesc.FreeMappingData(srcMD);
  4425. end;
  4426. result := true;
  4427. end;
  4428. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4429. function TglBitmap.AddAlphaFromLazIntfImage(const aImage: TLazIntfImage; const aFunc: TglBitmapFunction; const aArgs: Pointer): Boolean;
  4430. var
  4431. tex: TglBitmap2D;
  4432. begin
  4433. tex := TglBitmap2D.Create;
  4434. try
  4435. tex.AssignFromLazIntfImage(aImage);
  4436. result := AddAlphaFromglBitmap(tex, aFunc, aArgs);
  4437. finally
  4438. tex.Free;
  4439. end;
  4440. end;
  4441. {$ENDIF}
  4442. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4443. function TglBitmap.AddAlphaFromResource(const aInstance: Cardinal; aResource: String; aResType: PChar;
  4444. const aFunc: TglBitmapFunction; const aArgs: Pointer): Boolean;
  4445. var
  4446. rs: TResourceStream;
  4447. begin
  4448. PrepareResType(aResource, aResType);
  4449. rs := TResourceStream.Create(aInstance, aResource, aResType);
  4450. try
  4451. result := AddAlphaFromStream(rs, aFunc, aArgs);
  4452. finally
  4453. rs.Free;
  4454. end;
  4455. end;
  4456. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4457. function TglBitmap.AddAlphaFromResourceID(const aInstance: Cardinal; const aResourceID: Integer; const aResType: PChar;
  4458. const aFunc: TglBitmapFunction; const aArgs: Pointer): Boolean;
  4459. var
  4460. rs: TResourceStream;
  4461. begin
  4462. rs := TResourceStream.CreateFromID(aInstance, aResourceID, aResType);
  4463. try
  4464. result := AddAlphaFromStream(rs, aFunc, aArgs);
  4465. finally
  4466. rs.Free;
  4467. end;
  4468. end;
  4469. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4470. function TglBitmap.AddAlphaFromFunc(const aFunc: TglBitmapFunction; const aArgs: Pointer): Boolean;
  4471. begin
  4472. if TFormatDescriptor.Get(Format).IsCompressed then
  4473. raise EglBitmapUnsupportedFormat.Create(Format);
  4474. result := AddFunc(Self, aFunc, false, TFormatDescriptor.Get(Format).WithAlpha, aArgs);
  4475. end;
  4476. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4477. function TglBitmap.AddAlphaFromFile(const aFileName: String; const aFunc: TglBitmapFunction; const aArgs: Pointer): Boolean;
  4478. var
  4479. FS: TFileStream;
  4480. begin
  4481. FS := TFileStream.Create(aFileName, fmOpenRead);
  4482. try
  4483. result := AddAlphaFromStream(FS, aFunc, aArgs);
  4484. finally
  4485. FS.Free;
  4486. end;
  4487. end;
  4488. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4489. function TglBitmap.AddAlphaFromStream(const aStream: TStream; const aFunc: TglBitmapFunction; const aArgs: Pointer): Boolean;
  4490. var
  4491. tex: TglBitmap2D;
  4492. begin
  4493. tex := TglBitmap2D.Create(aStream);
  4494. try
  4495. result := AddAlphaFromglBitmap(tex, aFunc, aArgs);
  4496. finally
  4497. tex.Free;
  4498. end;
  4499. end;
  4500. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4501. function TglBitmap.AddAlphaFromGlBitmap(const aBitmap: TglBitmap; aFunc: TglBitmapFunction; const aArgs: Pointer): Boolean;
  4502. var
  4503. DestData, DestData2, SourceData: pByte;
  4504. TempHeight, TempWidth: Integer;
  4505. SourceFD, DestFD: TFormatDescriptor;
  4506. SourceMD, DestMD, DestMD2: Pointer;
  4507. FuncRec: TglBitmapFunctionRec;
  4508. begin
  4509. result := false;
  4510. Assert(Assigned(Data));
  4511. Assert(Assigned(aBitmap));
  4512. Assert(Assigned(aBitmap.Data));
  4513. if ((aBitmap.Width = Width) and (aBitmap.Height = Height)) then begin
  4514. result := ConvertTo(TFormatDescriptor.Get(Format).WithAlpha);
  4515. SourceFD := TFormatDescriptor.Get(aBitmap.Format);
  4516. DestFD := TFormatDescriptor.Get(Format);
  4517. if not Assigned(aFunc) then begin
  4518. aFunc := glBitmapAlphaFunc;
  4519. FuncRec.Args := {%H-}Pointer(SourceFD.HasAlpha);
  4520. end else
  4521. FuncRec.Args := aArgs;
  4522. // Values
  4523. TempHeight := aBitmap.FileHeight;
  4524. TempWidth := aBitmap.FileWidth;
  4525. FuncRec.Sender := Self;
  4526. FuncRec.Size := Dimension;
  4527. FuncRec.Position.Fields := FuncRec.Size.Fields;
  4528. DestData := Data;
  4529. DestData2 := Data;
  4530. SourceData := aBitmap.Data;
  4531. // Mapping
  4532. SourceFD.PreparePixel(FuncRec.Source);
  4533. DestFD.PreparePixel (FuncRec.Dest);
  4534. SourceMD := SourceFD.CreateMappingData;
  4535. DestMD := DestFD.CreateMappingData;
  4536. DestMD2 := DestFD.CreateMappingData;
  4537. try
  4538. FuncRec.Position.Y := 0;
  4539. while FuncRec.Position.Y < TempHeight do begin
  4540. FuncRec.Position.X := 0;
  4541. while FuncRec.Position.X < TempWidth do begin
  4542. SourceFD.Unmap(SourceData, FuncRec.Source, SourceMD);
  4543. DestFD.Unmap (DestData, FuncRec.Dest, DestMD);
  4544. aFunc(FuncRec);
  4545. DestFD.Map(FuncRec.Dest, DestData2, DestMD2);
  4546. inc(FuncRec.Position.X);
  4547. end;
  4548. inc(FuncRec.Position.Y);
  4549. end;
  4550. finally
  4551. SourceFD.FreeMappingData(SourceMD);
  4552. DestFD.FreeMappingData(DestMD);
  4553. DestFD.FreeMappingData(DestMD2);
  4554. end;
  4555. end;
  4556. end;
  4557. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4558. function TglBitmap.AddAlphaFromColorKey(const aRed, aGreen, aBlue: Byte; const aDeviation: Byte): Boolean;
  4559. begin
  4560. result := AddAlphaFromColorKeyFloat(aRed / $FF, aGreen / $FF, aBlue / $FF, aDeviation / $FF);
  4561. end;
  4562. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4563. function TglBitmap.AddAlphaFromColorKeyRange(const aRed, aGreen, aBlue: Cardinal; const aDeviation: Cardinal): Boolean;
  4564. var
  4565. PixelData: TglBitmapPixelData;
  4566. begin
  4567. TFormatDescriptor.GetWithAlpha(Format).PreparePixel(PixelData);
  4568. result := AddAlphaFromColorKeyFloat(
  4569. aRed / PixelData.Range.r,
  4570. aGreen / PixelData.Range.g,
  4571. aBlue / PixelData.Range.b,
  4572. aDeviation / Max(PixelData.Range.r, Max(PixelData.Range.g, PixelData.Range.b)));
  4573. end;
  4574. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4575. function TglBitmap.AddAlphaFromColorKeyFloat(const aRed, aGreen, aBlue: Single; const aDeviation: Single): Boolean;
  4576. var
  4577. values: array[0..2] of Single;
  4578. tmp: Cardinal;
  4579. i: Integer;
  4580. PixelData: TglBitmapPixelData;
  4581. begin
  4582. TFormatDescriptor.GetWithAlpha(Format).PreparePixel(PixelData);
  4583. with PixelData do begin
  4584. values[0] := aRed;
  4585. values[1] := aGreen;
  4586. values[2] := aBlue;
  4587. for i := 0 to 2 do begin
  4588. tmp := Trunc(Range.arr[i] * aDeviation);
  4589. Data.arr[i] := Min(Range.arr[i], Trunc(Range.arr[i] * values[i] + tmp));
  4590. Range.arr[i] := Max(0, Trunc(Range.arr[i] * values[i] - tmp));
  4591. end;
  4592. Data.a := 0;
  4593. Range.a := 0;
  4594. end;
  4595. result := AddAlphaFromFunc(glBitmapColorKeyAlphaFunc, @PixelData);
  4596. end;
  4597. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4598. function TglBitmap.AddAlphaFromValue(const aAlpha: Byte): Boolean;
  4599. begin
  4600. result := AddAlphaFromValueFloat(aAlpha / $FF);
  4601. end;
  4602. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4603. function TglBitmap.AddAlphaFromValueRange(const aAlpha: Cardinal): Boolean;
  4604. var
  4605. PixelData: TglBitmapPixelData;
  4606. begin
  4607. TFormatDescriptor.GetWithAlpha(Format).PreparePixel(PixelData);
  4608. result := AddAlphaFromValueFloat(aAlpha / PixelData.Range.a);
  4609. end;
  4610. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4611. function TglBitmap.AddAlphaFromValueFloat(const aAlpha: Single): Boolean;
  4612. var
  4613. PixelData: TglBitmapPixelData;
  4614. begin
  4615. TFormatDescriptor.GetWithAlpha(Format).PreparePixel(PixelData);
  4616. with PixelData do
  4617. Data.a := Min(Range.a, Max(0, Round(Range.a * aAlpha)));
  4618. result := AddAlphaFromFunc(glBitmapValueAlphaFunc, @PixelData.Data.a);
  4619. end;
  4620. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4621. function TglBitmap.RemoveAlpha: Boolean;
  4622. var
  4623. FormatDesc: TFormatDescriptor;
  4624. begin
  4625. result := false;
  4626. FormatDesc := TFormatDescriptor.Get(Format);
  4627. if Assigned(Data) then begin
  4628. if FormatDesc.IsCompressed or not FormatDesc.HasAlpha then
  4629. raise EglBitmapUnsupportedFormat.Create(Format);
  4630. result := ConvertTo(FormatDesc.WithoutAlpha);
  4631. end;
  4632. end;
  4633. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4634. function TglBitmap.Clone: TglBitmap;
  4635. var
  4636. Temp: TglBitmap;
  4637. TempPtr: PByte;
  4638. Size: Integer;
  4639. begin
  4640. result := nil;
  4641. Temp := (ClassType.Create as TglBitmap);
  4642. try
  4643. // copy texture data if assigned
  4644. if Assigned(Data) then begin
  4645. Size := TFormatDescriptor.Get(Format).GetSize(fDimension);
  4646. GetMem(TempPtr, Size);
  4647. try
  4648. Move(Data^, TempPtr^, Size);
  4649. Temp.SetDataPointer(TempPtr, Format, Width, Height); //be careful, Data could be freed by this method
  4650. except
  4651. if Assigned(TempPtr) then
  4652. FreeMem(TempPtr);
  4653. raise;
  4654. end;
  4655. end else begin
  4656. TempPtr := nil;
  4657. Temp.SetDataPointer(TempPtr, Format, Width, Height); //be careful, Data could be freed by this method
  4658. end;
  4659. // copy properties
  4660. Temp.fID := ID;
  4661. Temp.fTarget := Target;
  4662. Temp.fFormat := Format;
  4663. Temp.fMipMap := MipMap;
  4664. Temp.fAnisotropic := Anisotropic;
  4665. Temp.fBorderColor := fBorderColor;
  4666. Temp.fDeleteTextureOnFree := DeleteTextureOnFree;
  4667. Temp.fFreeDataAfterGenTexture := FreeDataAfterGenTexture;
  4668. Temp.fFilterMin := fFilterMin;
  4669. Temp.fFilterMag := fFilterMag;
  4670. Temp.fWrapS := fWrapS;
  4671. Temp.fWrapT := fWrapT;
  4672. Temp.fWrapR := fWrapR;
  4673. Temp.fFilename := fFilename;
  4674. Temp.fCustomName := fCustomName;
  4675. Temp.fCustomNameW := fCustomNameW;
  4676. Temp.fCustomData := fCustomData;
  4677. result := Temp;
  4678. except
  4679. FreeAndNil(Temp);
  4680. raise;
  4681. end;
  4682. end;
  4683. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4684. function TglBitmap.ConvertTo(const aFormat: TglBitmapFormat): Boolean;
  4685. var
  4686. SourceFD, DestFD: TFormatDescriptor;
  4687. SourcePD, DestPD: TglBitmapPixelData;
  4688. ShiftData: TShiftData;
  4689. function CanCopyDirect: Boolean;
  4690. begin
  4691. result :=
  4692. ((SourcePD.Range.r = DestPD.Range.r) or (SourcePD.Range.r = 0) or (DestPD.Range.r = 0)) and
  4693. ((SourcePD.Range.g = DestPD.Range.g) or (SourcePD.Range.g = 0) or (DestPD.Range.g = 0)) and
  4694. ((SourcePD.Range.b = DestPD.Range.b) or (SourcePD.Range.b = 0) or (DestPD.Range.b = 0)) and
  4695. ((SourcePD.Range.a = DestPD.Range.a) or (SourcePD.Range.a = 0) or (DestPD.Range.a = 0));
  4696. end;
  4697. function CanShift: Boolean;
  4698. begin
  4699. result :=
  4700. ((SourcePD.Range.r >= DestPD.Range.r) or (SourcePD.Range.r = 0) or (DestPD.Range.r = 0)) and
  4701. ((SourcePD.Range.g >= DestPD.Range.g) or (SourcePD.Range.g = 0) or (DestPD.Range.g = 0)) and
  4702. ((SourcePD.Range.b >= DestPD.Range.b) or (SourcePD.Range.b = 0) or (DestPD.Range.b = 0)) and
  4703. ((SourcePD.Range.a >= DestPD.Range.a) or (SourcePD.Range.a = 0) or (DestPD.Range.a = 0));
  4704. end;
  4705. function GetShift(aSource, aDest: Cardinal) : ShortInt;
  4706. begin
  4707. result := 0;
  4708. while (aSource > aDest) and (aSource > 0) do begin
  4709. inc(result);
  4710. aSource := aSource shr 1;
  4711. end;
  4712. end;
  4713. begin
  4714. if (aFormat <> fFormat) and (aFormat <> tfEmpty) then begin
  4715. SourceFD := TFormatDescriptor.Get(Format);
  4716. DestFD := TFormatDescriptor.Get(aFormat);
  4717. SourceFD.PreparePixel(SourcePD);
  4718. DestFD.PreparePixel (DestPD);
  4719. if CanCopyDirect then
  4720. result := AddFunc(Self, glBitmapConvertCopyFunc, false, aFormat)
  4721. else if CanShift then begin
  4722. ShiftData.r := GetShift(SourcePD.Range.r, DestPD.Range.r);
  4723. ShiftData.g := GetShift(SourcePD.Range.g, DestPD.Range.g);
  4724. ShiftData.b := GetShift(SourcePD.Range.b, DestPD.Range.b);
  4725. ShiftData.a := GetShift(SourcePD.Range.a, DestPD.Range.a);
  4726. result := AddFunc(Self, glBitmapConvertShiftRGBAFunc, false, aFormat, @ShiftData);
  4727. end else
  4728. result := AddFunc(Self, glBitmapConvertCalculateRGBAFunc, false, aFormat);
  4729. end else
  4730. result := true;
  4731. end;
  4732. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4733. procedure TglBitmap.Invert(const aUseRGB: Boolean; const aUseAlpha: Boolean);
  4734. begin
  4735. if aUseRGB or aUseAlpha then
  4736. AddFunc(glBitmapInvertFunc, false, {%H-}Pointer(
  4737. ((Byte(aUseAlpha) and 1) shl 1) or
  4738. (Byte(aUseRGB) and 1) ));
  4739. end;
  4740. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4741. procedure TglBitmap.SetBorderColor(const aRed, aGreen, aBlue, aAlpha: Single);
  4742. begin
  4743. fBorderColor[0] := aRed;
  4744. fBorderColor[1] := aGreen;
  4745. fBorderColor[2] := aBlue;
  4746. fBorderColor[3] := aAlpha;
  4747. if (ID > 0) then begin
  4748. Bind(false);
  4749. glTexParameterfv(Target, GL_TEXTURE_BORDER_COLOR, @fBorderColor[0]);
  4750. end;
  4751. end;
  4752. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4753. procedure TglBitmap.FreeData;
  4754. var
  4755. TempPtr: PByte;
  4756. begin
  4757. TempPtr := nil;
  4758. SetDataPointer(TempPtr, tfEmpty); //be careful, Data could be freed by this method
  4759. end;
  4760. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4761. procedure TglBitmap.FillWithColor(const aRed, aGreen, aBlue: Byte;
  4762. const aAlpha: Byte);
  4763. begin
  4764. FillWithColorFloat(aRed/$FF, aGreen/$FF, aBlue/$FF, aAlpha/$FF);
  4765. end;
  4766. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4767. procedure TglBitmap.FillWithColorRange(const aRed, aGreen, aBlue: Cardinal; const aAlpha: Cardinal);
  4768. var
  4769. PixelData: TglBitmapPixelData;
  4770. begin
  4771. TFormatDescriptor.GetWithAlpha(Format).PreparePixel(PixelData);
  4772. FillWithColorFloat(
  4773. aRed / PixelData.Range.r,
  4774. aGreen / PixelData.Range.g,
  4775. aBlue / PixelData.Range.b,
  4776. aAlpha / PixelData.Range.a);
  4777. end;
  4778. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4779. procedure TglBitmap.FillWithColorFloat(const aRed, aGreen, aBlue: Single; const aAlpha: Single);
  4780. var
  4781. PixelData: TglBitmapPixelData;
  4782. begin
  4783. TFormatDescriptor.Get(Format).PreparePixel(PixelData);
  4784. with PixelData do begin
  4785. Data.r := Max(0, Min(Range.r, Trunc(Range.r * aRed)));
  4786. Data.g := Max(0, Min(Range.g, Trunc(Range.g * aGreen)));
  4787. Data.b := Max(0, Min(Range.b, Trunc(Range.b * aBlue)));
  4788. Data.a := Max(0, Min(Range.a, Trunc(Range.a * aAlpha)));
  4789. end;
  4790. AddFunc(glBitmapFillWithColorFunc, false, @PixelData);
  4791. end;
  4792. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4793. procedure TglBitmap.SetFilter(const aMin, aMag: GLenum);
  4794. begin
  4795. //check MIN filter
  4796. case aMin of
  4797. GL_NEAREST:
  4798. fFilterMin := GL_NEAREST;
  4799. GL_LINEAR:
  4800. fFilterMin := GL_LINEAR;
  4801. GL_NEAREST_MIPMAP_NEAREST:
  4802. fFilterMin := GL_NEAREST_MIPMAP_NEAREST;
  4803. GL_LINEAR_MIPMAP_NEAREST:
  4804. fFilterMin := GL_LINEAR_MIPMAP_NEAREST;
  4805. GL_NEAREST_MIPMAP_LINEAR:
  4806. fFilterMin := GL_NEAREST_MIPMAP_LINEAR;
  4807. GL_LINEAR_MIPMAP_LINEAR:
  4808. fFilterMin := GL_LINEAR_MIPMAP_LINEAR;
  4809. else
  4810. raise EglBitmap.Create('SetFilter - Unknow MIN filter.');
  4811. end;
  4812. //check MAG filter
  4813. case aMag of
  4814. GL_NEAREST:
  4815. fFilterMag := GL_NEAREST;
  4816. GL_LINEAR:
  4817. fFilterMag := GL_LINEAR;
  4818. else
  4819. raise EglBitmap.Create('SetFilter - Unknow MAG filter.');
  4820. end;
  4821. //apply filter
  4822. if (ID > 0) then begin
  4823. Bind(false);
  4824. glTexParameteri(Target, GL_TEXTURE_MAG_FILTER, fFilterMag);
  4825. if (MipMap = mmNone) or (Target = GL_TEXTURE_RECTANGLE) then begin
  4826. case fFilterMin of
  4827. GL_NEAREST, GL_LINEAR:
  4828. glTexParameteri(Target, GL_TEXTURE_MIN_FILTER, fFilterMin);
  4829. GL_NEAREST_MIPMAP_NEAREST, GL_NEAREST_MIPMAP_LINEAR:
  4830. glTexParameteri(Target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  4831. GL_LINEAR_MIPMAP_NEAREST, GL_LINEAR_MIPMAP_LINEAR:
  4832. glTexParameteri(Target, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  4833. end;
  4834. end else
  4835. glTexParameteri(Target, GL_TEXTURE_MIN_FILTER, fFilterMin);
  4836. end;
  4837. end;
  4838. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4839. procedure TglBitmap.SetWrap(const S: GLenum; const T: GLenum; const R: GLenum);
  4840. procedure CheckAndSetWrap(const aValue: Cardinal; var aTarget: Cardinal);
  4841. begin
  4842. case aValue of
  4843. GL_CLAMP:
  4844. aTarget := GL_CLAMP;
  4845. GL_REPEAT:
  4846. aTarget := GL_REPEAT;
  4847. GL_CLAMP_TO_EDGE: begin
  4848. if GL_VERSION_1_2 or GL_EXT_texture_edge_clamp then
  4849. aTarget := GL_CLAMP_TO_EDGE
  4850. else
  4851. aTarget := GL_CLAMP;
  4852. end;
  4853. GL_CLAMP_TO_BORDER: begin
  4854. if GL_VERSION_1_3 or GL_ARB_texture_border_clamp then
  4855. aTarget := GL_CLAMP_TO_BORDER
  4856. else
  4857. aTarget := GL_CLAMP;
  4858. end;
  4859. GL_MIRRORED_REPEAT: begin
  4860. if GL_VERSION_1_4 or GL_ARB_texture_mirrored_repeat or GL_IBM_texture_mirrored_repeat then
  4861. aTarget := GL_MIRRORED_REPEAT
  4862. else
  4863. raise EglBitmap.Create('SetWrap - Unsupported Texturewrap GL_MIRRORED_REPEAT (S).');
  4864. end;
  4865. else
  4866. raise EglBitmap.Create('SetWrap - Unknow Texturewrap');
  4867. end;
  4868. end;
  4869. begin
  4870. CheckAndSetWrap(S, fWrapS);
  4871. CheckAndSetWrap(T, fWrapT);
  4872. CheckAndSetWrap(R, fWrapR);
  4873. if (ID > 0) then begin
  4874. Bind(false);
  4875. glTexParameteri(Target, GL_TEXTURE_WRAP_S, fWrapS);
  4876. glTexParameteri(Target, GL_TEXTURE_WRAP_T, fWrapT);
  4877. glTexParameteri(Target, GL_TEXTURE_WRAP_R, fWrapR);
  4878. end;
  4879. end;
  4880. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4881. procedure TglBitmap.SetSwizzle(const r, g, b, a: GLenum);
  4882. procedure CheckAndSetValue(const aValue: GLenum; const aIndex: Integer);
  4883. begin
  4884. if (aValue = GL_ZERO) or (aValue = GL_ONE) or (aValue = GL_ALPHA) or
  4885. (aValue = GL_RED) or (aValue = GL_GREEN) or (aValue = GL_BLUE) then
  4886. fSwizzle[aIndex] := aValue
  4887. else
  4888. raise EglBitmap.Create('SetSwizzle - Unknow Swizle Value');
  4889. end;
  4890. begin
  4891. if not (GL_ARB_texture_swizzle or GL_EXT_texture_swizzle or GL_VERSION_3_3) then
  4892. raise EglBitmapNotSupported.Create('texture swizzle is not supported');
  4893. CheckAndSetValue(r, 0);
  4894. CheckAndSetValue(g, 1);
  4895. CheckAndSetValue(b, 2);
  4896. CheckAndSetValue(a, 3);
  4897. if (ID > 0) then begin
  4898. Bind(false);
  4899. glTexParameteriv(Target, GL_TEXTURE_SWIZZLE_RGBA, @fSwizzle[0]);
  4900. end;
  4901. end;
  4902. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4903. procedure TglBitmap.Bind(const aEnableTextureUnit: Boolean);
  4904. begin
  4905. if aEnableTextureUnit then
  4906. glEnable(Target);
  4907. if (ID > 0) then
  4908. glBindTexture(Target, ID);
  4909. end;
  4910. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4911. procedure TglBitmap.Unbind(const aDisableTextureUnit: Boolean);
  4912. begin
  4913. if aDisableTextureUnit then
  4914. glDisable(Target);
  4915. glBindTexture(Target, 0);
  4916. end;
  4917. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4918. constructor TglBitmap.Create;
  4919. begin
  4920. if (ClassType = TglBitmap) then
  4921. raise EglBitmap.Create('Don''t create TglBitmap directly. Use one of the deviated classes (TglBitmap2D) instead.');
  4922. {$IFDEF GLB_NATIVE_OGL}
  4923. glbReadOpenGLExtensions;
  4924. {$ENDIF}
  4925. inherited Create;
  4926. fFormat := glBitmapGetDefaultFormat;
  4927. fFreeDataOnDestroy := true;
  4928. end;
  4929. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4930. constructor TglBitmap.Create(const aFileName: String);
  4931. begin
  4932. Create;
  4933. LoadFromFile(aFileName);
  4934. end;
  4935. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4936. constructor TglBitmap.Create(const aStream: TStream);
  4937. begin
  4938. Create;
  4939. LoadFromStream(aStream);
  4940. end;
  4941. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4942. constructor TglBitmap.Create(const aSize: TglBitmapPixelPosition; const aFormat: TglBitmapFormat; aData: PByte);
  4943. var
  4944. ImageSize: Integer;
  4945. begin
  4946. Create;
  4947. if not Assigned(aData) then begin
  4948. ImageSize := TFormatDescriptor.Get(aFormat).GetSize(aSize);
  4949. GetMem(aData, ImageSize);
  4950. try
  4951. FillChar(aData^, ImageSize, #$FF);
  4952. SetDataPointer(aData, aFormat, aSize.X, aSize.Y); //be careful, Data could be freed by this method
  4953. except
  4954. if Assigned(aData) then
  4955. FreeMem(aData);
  4956. raise;
  4957. end;
  4958. end else begin
  4959. SetDataPointer(aData, aFormat, aSize.X, aSize.Y); //be careful, Data could be freed by this method
  4960. fFreeDataOnDestroy := false;
  4961. end;
  4962. end;
  4963. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4964. constructor TglBitmap.Create(const aSize: TglBitmapPixelPosition; const aFormat: TglBitmapFormat; const aFunc: TglBitmapFunction; const aArgs: Pointer);
  4965. begin
  4966. Create;
  4967. LoadFromFunc(aSize, aFunc, aFormat, aArgs);
  4968. end;
  4969. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4970. constructor TglBitmap.Create(const aInstance: Cardinal; const aResource: String; const aResType: PChar);
  4971. begin
  4972. Create;
  4973. LoadFromResource(aInstance, aResource, aResType);
  4974. end;
  4975. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4976. constructor TglBitmap.Create(const aInstance: Cardinal; const aResourceID: Integer; const aResType: PChar);
  4977. begin
  4978. Create;
  4979. LoadFromResourceID(aInstance, aResourceID, aResType);
  4980. end;
  4981. {$IFDEF GLB_SUPPORT_PNG_READ}
  4982. {$IF DEFINED(GLB_LAZ_PNG)}
  4983. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4984. //PNG/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4985. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4986. function TglBitmap.LoadPNG(const aStream: TStream): Boolean;
  4987. const
  4988. MAGIC_LEN = 8;
  4989. PNG_MAGIC: String[MAGIC_LEN] = #$89#$50#$4E#$47#$0D#$0A#$1A#$0A;
  4990. var
  4991. png: TPortableNetworkGraphic;
  4992. intf: TLazIntfImage;
  4993. StreamPos: Int64;
  4994. magic: String[MAGIC_LEN];
  4995. begin
  4996. result := true;
  4997. StreamPos := aStream.Position;
  4998. SetLength(magic, MAGIC_LEN);
  4999. aStream.Read(magic[1], MAGIC_LEN);
  5000. aStream.Position := StreamPos;
  5001. if (magic <> PNG_MAGIC) then begin
  5002. result := false;
  5003. exit;
  5004. end;
  5005. png := TPortableNetworkGraphic.Create;
  5006. try try
  5007. png.LoadFromStream(aStream);
  5008. intf := png.CreateIntfImage;
  5009. try try
  5010. AssignFromLazIntfImage(intf);
  5011. except
  5012. result := false;
  5013. aStream.Position := StreamPos;
  5014. exit;
  5015. end;
  5016. finally
  5017. intf.Free;
  5018. end;
  5019. except
  5020. result := false;
  5021. aStream.Position := StreamPos;
  5022. exit;
  5023. end;
  5024. finally
  5025. png.Free;
  5026. end;
  5027. end;
  5028. {$ELSEIF DEFINED(GLB_SDL_IMAGE)}
  5029. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5030. function TglBitmap.LoadPNG(const aStream: TStream): Boolean;
  5031. var
  5032. Surface: PSDL_Surface;
  5033. RWops: PSDL_RWops;
  5034. begin
  5035. result := false;
  5036. RWops := glBitmapCreateRWops(aStream);
  5037. try
  5038. if IMG_isPNG(RWops) > 0 then begin
  5039. Surface := IMG_LoadPNG_RW(RWops);
  5040. try
  5041. AssignFromSurface(Surface);
  5042. result := true;
  5043. finally
  5044. SDL_FreeSurface(Surface);
  5045. end;
  5046. end;
  5047. finally
  5048. SDL_FreeRW(RWops);
  5049. end;
  5050. end;
  5051. {$ELSEIF DEFINED(GLB_LIB_PNG)}
  5052. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5053. procedure glBitmap_libPNG_read_func(png: png_structp; buffer: png_bytep; size: cardinal); cdecl;
  5054. begin
  5055. TStream(png_get_io_ptr(png)).Read(buffer^, size);
  5056. end;
  5057. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5058. function TglBitmap.LoadPNG(const aStream: TStream): Boolean;
  5059. var
  5060. StreamPos: Int64;
  5061. signature: array [0..7] of byte;
  5062. png: png_structp;
  5063. png_info: png_infop;
  5064. TempHeight, TempWidth: Integer;
  5065. Format: TglBitmapFormat;
  5066. png_data: pByte;
  5067. png_rows: array of pByte;
  5068. Row, LineSize: Integer;
  5069. begin
  5070. result := false;
  5071. if not init_libPNG then
  5072. raise Exception.Create('LoadPNG - unable to initialize libPNG.');
  5073. try
  5074. // signature
  5075. StreamPos := aStream.Position;
  5076. aStream.Read(signature{%H-}, 8);
  5077. aStream.Position := StreamPos;
  5078. if png_check_sig(@signature, 8) <> 0 then begin
  5079. // png read struct
  5080. png := png_create_read_struct(PNG_LIBPNG_VER_STRING, nil, nil, nil);
  5081. if png = nil then
  5082. raise EglBitmapException.Create('LoadPng - couldn''t create read struct.');
  5083. // png info
  5084. png_info := png_create_info_struct(png);
  5085. if png_info = nil then begin
  5086. png_destroy_read_struct(@png, nil, nil);
  5087. raise EglBitmapException.Create('LoadPng - couldn''t create info struct.');
  5088. end;
  5089. // set read callback
  5090. png_set_read_fn(png, aStream, glBitmap_libPNG_read_func);
  5091. // read informations
  5092. png_read_info(png, png_info);
  5093. // size
  5094. TempHeight := png_get_image_height(png, png_info);
  5095. TempWidth := png_get_image_width(png, png_info);
  5096. // format
  5097. case png_get_color_type(png, png_info) of
  5098. PNG_COLOR_TYPE_GRAY:
  5099. Format := tfLuminance8;
  5100. PNG_COLOR_TYPE_GRAY_ALPHA:
  5101. Format := tfLuminance8Alpha8;
  5102. PNG_COLOR_TYPE_RGB:
  5103. Format := tfRGB8;
  5104. PNG_COLOR_TYPE_RGB_ALPHA:
  5105. Format := tfRGBA8;
  5106. else
  5107. raise EglBitmapException.Create ('LoadPng - Unsupported Colortype found.');
  5108. end;
  5109. // cut upper 8 bit from 16 bit formats
  5110. if png_get_bit_depth(png, png_info) > 8 then
  5111. png_set_strip_16(png);
  5112. // expand bitdepth smaller than 8
  5113. if png_get_bit_depth(png, png_info) < 8 then
  5114. png_set_expand(png);
  5115. // allocating mem for scanlines
  5116. LineSize := png_get_rowbytes(png, png_info);
  5117. GetMem(png_data, TempHeight * LineSize);
  5118. try
  5119. SetLength(png_rows, TempHeight);
  5120. for Row := Low(png_rows) to High(png_rows) do begin
  5121. png_rows[Row] := png_data;
  5122. Inc(png_rows[Row], Row * LineSize);
  5123. end;
  5124. // read complete image into scanlines
  5125. png_read_image(png, @png_rows[0]);
  5126. // read end
  5127. png_read_end(png, png_info);
  5128. // destroy read struct
  5129. png_destroy_read_struct(@png, @png_info, nil);
  5130. SetLength(png_rows, 0);
  5131. // set new data
  5132. SetDataPointer(png_data, Format, TempWidth, TempHeight); //be careful, Data could be freed by this method
  5133. result := true;
  5134. except
  5135. if Assigned(png_data) then
  5136. FreeMem(png_data);
  5137. raise;
  5138. end;
  5139. end;
  5140. finally
  5141. quit_libPNG;
  5142. end;
  5143. end;
  5144. {$ELSEIF DEFINED(GLB_PNGIMAGE)}
  5145. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5146. function TglBitmap.LoadPNG(const aStream: TStream): Boolean;
  5147. var
  5148. StreamPos: Int64;
  5149. Png: TPNGObject;
  5150. Header: String[8];
  5151. Row, Col, PixSize, LineSize: Integer;
  5152. NewImage, pSource, pDest, pAlpha: pByte;
  5153. PngFormat: TglBitmapFormat;
  5154. FormatDesc: TFormatDescriptor;
  5155. const
  5156. PngHeader: String[8] = #137#80#78#71#13#10#26#10;
  5157. begin
  5158. result := false;
  5159. StreamPos := aStream.Position;
  5160. aStream.Read(Header[0], SizeOf(Header));
  5161. aStream.Position := StreamPos;
  5162. {Test if the header matches}
  5163. if Header = PngHeader then begin
  5164. Png := TPNGObject.Create;
  5165. try
  5166. Png.LoadFromStream(aStream);
  5167. case Png.Header.ColorType of
  5168. COLOR_GRAYSCALE:
  5169. PngFormat := tfLuminance8;
  5170. COLOR_GRAYSCALEALPHA:
  5171. PngFormat := tfLuminance8Alpha8;
  5172. COLOR_RGB:
  5173. PngFormat := tfBGR8;
  5174. COLOR_RGBALPHA:
  5175. PngFormat := tfBGRA8;
  5176. else
  5177. raise EglBitmapException.Create ('LoadPng - Unsupported Colortype found.');
  5178. end;
  5179. FormatDesc := TFormatDescriptor.Get(PngFormat);
  5180. PixSize := Round(FormatDesc.PixelSize);
  5181. LineSize := FormatDesc.GetSize(Png.Header.Width, 1);
  5182. GetMem(NewImage, LineSize * Integer(Png.Header.Height));
  5183. try
  5184. pDest := NewImage;
  5185. case Png.Header.ColorType of
  5186. COLOR_RGB, COLOR_GRAYSCALE:
  5187. begin
  5188. for Row := 0 to Png.Height -1 do begin
  5189. Move (Png.Scanline[Row]^, pDest^, LineSize);
  5190. Inc(pDest, LineSize);
  5191. end;
  5192. end;
  5193. COLOR_RGBALPHA, COLOR_GRAYSCALEALPHA:
  5194. begin
  5195. PixSize := PixSize -1;
  5196. for Row := 0 to Png.Height -1 do begin
  5197. pSource := Png.Scanline[Row];
  5198. pAlpha := pByte(Png.AlphaScanline[Row]);
  5199. for Col := 0 to Png.Width -1 do begin
  5200. Move (pSource^, pDest^, PixSize);
  5201. Inc(pSource, PixSize);
  5202. Inc(pDest, PixSize);
  5203. pDest^ := pAlpha^;
  5204. inc(pAlpha);
  5205. Inc(pDest);
  5206. end;
  5207. end;
  5208. end;
  5209. else
  5210. raise EglBitmapException.Create ('LoadPng - Unsupported Colortype found.');
  5211. end;
  5212. SetDataPointer(NewImage, PngFormat, Png.Header.Width, Png.Header.Height); //be careful, Data could be freed by this method
  5213. result := true;
  5214. except
  5215. if Assigned(NewImage) then
  5216. FreeMem(NewImage);
  5217. raise;
  5218. end;
  5219. finally
  5220. Png.Free;
  5221. end;
  5222. end;
  5223. end;
  5224. {$IFEND}
  5225. {$ENDIF}
  5226. {$IFDEF GLB_SUPPORT_PNG_WRITE}
  5227. {$IFDEF GLB_LIB_PNG}
  5228. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5229. procedure glBitmap_libPNG_write_func(png: png_structp; buffer: png_bytep; size: cardinal); cdecl;
  5230. begin
  5231. TStream(png_get_io_ptr(png)).Write(buffer^, size);
  5232. end;
  5233. {$ENDIF}
  5234. {$IF DEFINED(GLB_LAZ_PNG)}
  5235. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5236. procedure TglBitmap.SavePNG(const aStream: TStream);
  5237. var
  5238. png: TPortableNetworkGraphic;
  5239. intf: TLazIntfImage;
  5240. raw: TRawImage;
  5241. begin
  5242. png := TPortableNetworkGraphic.Create;
  5243. intf := TLazIntfImage.Create(0, 0);
  5244. try
  5245. if not AssignToLazIntfImage(intf) then
  5246. raise EglBitmap.Create('unable to create LazIntfImage from glBitmap');
  5247. intf.GetRawImage(raw);
  5248. png.LoadFromRawImage(raw, false);
  5249. png.SaveToStream(aStream);
  5250. finally
  5251. png.Free;
  5252. intf.Free;
  5253. end;
  5254. end;
  5255. {$ELSEIF DEFINED(GLB_LIB_PNG)}
  5256. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5257. procedure TglBitmap.SavePNG(const aStream: TStream);
  5258. var
  5259. png: png_structp;
  5260. png_info: png_infop;
  5261. png_rows: array of pByte;
  5262. LineSize: Integer;
  5263. ColorType: Integer;
  5264. Row: Integer;
  5265. FormatDesc: TFormatDescriptor;
  5266. begin
  5267. if not (ftPNG in FormatGetSupportedFiles(Format)) then
  5268. raise EglBitmapUnsupportedFormat.Create(Format);
  5269. if not init_libPNG then
  5270. raise Exception.Create('unable to initialize libPNG.');
  5271. try
  5272. case Format of
  5273. tfAlpha8, tfLuminance8:
  5274. ColorType := PNG_COLOR_TYPE_GRAY;
  5275. tfLuminance8Alpha8:
  5276. ColorType := PNG_COLOR_TYPE_GRAY_ALPHA;
  5277. tfBGR8, tfRGB8:
  5278. ColorType := PNG_COLOR_TYPE_RGB;
  5279. tfBGRA8, tfRGBA8:
  5280. ColorType := PNG_COLOR_TYPE_RGBA;
  5281. else
  5282. raise EglBitmapUnsupportedFormat.Create(Format);
  5283. end;
  5284. FormatDesc := TFormatDescriptor.Get(Format);
  5285. LineSize := FormatDesc.GetSize(Width, 1);
  5286. // creating array for scanline
  5287. SetLength(png_rows, Height);
  5288. try
  5289. for Row := 0 to Height - 1 do begin
  5290. png_rows[Row] := Data;
  5291. Inc(png_rows[Row], Row * LineSize)
  5292. end;
  5293. // write struct
  5294. png := png_create_write_struct(PNG_LIBPNG_VER_STRING, nil, nil, nil);
  5295. if png = nil then
  5296. raise EglBitmapException.Create('SavePng - couldn''t create write struct.');
  5297. // create png info
  5298. png_info := png_create_info_struct(png);
  5299. if png_info = nil then begin
  5300. png_destroy_write_struct(@png, nil);
  5301. raise EglBitmapException.Create('SavePng - couldn''t create info struct.');
  5302. end;
  5303. // set read callback
  5304. png_set_write_fn(png, aStream, glBitmap_libPNG_write_func, nil);
  5305. // set compression
  5306. png_set_compression_level(png, 6);
  5307. if Format in [tfBGR8, tfBGRA8] then
  5308. png_set_bgr(png);
  5309. png_set_IHDR(png, png_info, Width, Height, 8, ColorType, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
  5310. png_write_info(png, png_info);
  5311. png_write_image(png, @png_rows[0]);
  5312. png_write_end(png, png_info);
  5313. png_destroy_write_struct(@png, @png_info);
  5314. finally
  5315. SetLength(png_rows, 0);
  5316. end;
  5317. finally
  5318. quit_libPNG;
  5319. end;
  5320. end;
  5321. {$ELSEIF DEFINED(GLB_PNGIMAGE)}
  5322. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5323. procedure TglBitmap.SavePNG(const aStream: TStream);
  5324. var
  5325. Png: TPNGObject;
  5326. pSource, pDest: pByte;
  5327. X, Y, PixSize: Integer;
  5328. ColorType: Cardinal;
  5329. Alpha: Boolean;
  5330. pTemp: pByte;
  5331. Temp: Byte;
  5332. begin
  5333. if not (ftPNG in FormatGetSupportedFiles (Format)) then
  5334. raise EglBitmapUnsupportedFormat.Create(Format);
  5335. case Format of
  5336. tfAlpha8, tfLuminance8: begin
  5337. ColorType := COLOR_GRAYSCALE;
  5338. PixSize := 1;
  5339. Alpha := false;
  5340. end;
  5341. tfLuminance8Alpha8: begin
  5342. ColorType := COLOR_GRAYSCALEALPHA;
  5343. PixSize := 1;
  5344. Alpha := true;
  5345. end;
  5346. tfBGR8, tfRGB8: begin
  5347. ColorType := COLOR_RGB;
  5348. PixSize := 3;
  5349. Alpha := false;
  5350. end;
  5351. tfBGRA8, tfRGBA8: begin
  5352. ColorType := COLOR_RGBALPHA;
  5353. PixSize := 3;
  5354. Alpha := true
  5355. end;
  5356. else
  5357. raise EglBitmapUnsupportedFormat.Create(Format);
  5358. end;
  5359. Png := TPNGObject.CreateBlank(ColorType, 8, Width, Height);
  5360. try
  5361. // Copy ImageData
  5362. pSource := Data;
  5363. for Y := 0 to Height -1 do begin
  5364. pDest := png.ScanLine[Y];
  5365. for X := 0 to Width -1 do begin
  5366. Move(pSource^, pDest^, PixSize);
  5367. Inc(pDest, PixSize);
  5368. Inc(pSource, PixSize);
  5369. if Alpha then begin
  5370. png.AlphaScanline[Y]^[X] := pSource^;
  5371. Inc(pSource);
  5372. end;
  5373. end;
  5374. // convert RGB line to BGR
  5375. if Format in [tfRGB8, tfRGBA8] then begin
  5376. pTemp := png.ScanLine[Y];
  5377. for X := 0 to Width -1 do begin
  5378. Temp := pByteArray(pTemp)^[0];
  5379. pByteArray(pTemp)^[0] := pByteArray(pTemp)^[2];
  5380. pByteArray(pTemp)^[2] := Temp;
  5381. Inc(pTemp, 3);
  5382. end;
  5383. end;
  5384. end;
  5385. // Save to Stream
  5386. Png.CompressionLevel := 6;
  5387. Png.SaveToStream(aStream);
  5388. finally
  5389. FreeAndNil(Png);
  5390. end;
  5391. end;
  5392. {$IFEND}
  5393. {$ENDIF}
  5394. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5395. //JPEG////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5396. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5397. {$IFDEF GLB_LIB_JPEG}
  5398. type
  5399. glBitmap_libJPEG_source_mgr_ptr = ^glBitmap_libJPEG_source_mgr;
  5400. glBitmap_libJPEG_source_mgr = record
  5401. pub: jpeg_source_mgr;
  5402. SrcStream: TStream;
  5403. SrcBuffer: array [1..4096] of byte;
  5404. end;
  5405. glBitmap_libJPEG_dest_mgr_ptr = ^glBitmap_libJPEG_dest_mgr;
  5406. glBitmap_libJPEG_dest_mgr = record
  5407. pub: jpeg_destination_mgr;
  5408. DestStream: TStream;
  5409. DestBuffer: array [1..4096] of byte;
  5410. end;
  5411. procedure glBitmap_libJPEG_error_exit(cinfo: j_common_ptr); cdecl;
  5412. begin
  5413. //DUMMY
  5414. end;
  5415. procedure glBitmap_libJPEG_output_message(cinfo: j_common_ptr); cdecl;
  5416. begin
  5417. //DUMMY
  5418. end;
  5419. procedure glBitmap_libJPEG_init_source(cinfo: j_decompress_ptr); cdecl;
  5420. begin
  5421. //DUMMY
  5422. end;
  5423. procedure glBitmap_libJPEG_term_source(cinfo: j_decompress_ptr); cdecl;
  5424. begin
  5425. //DUMMY
  5426. end;
  5427. procedure glBitmap_libJPEG_init_destination(cinfo: j_compress_ptr); cdecl;
  5428. begin
  5429. //DUMMY
  5430. end;
  5431. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5432. function glBitmap_libJPEG_fill_input_buffer(cinfo: j_decompress_ptr): boolean; cdecl;
  5433. var
  5434. src: glBitmap_libJPEG_source_mgr_ptr;
  5435. bytes: integer;
  5436. begin
  5437. src := glBitmap_libJPEG_source_mgr_ptr(cinfo^.src);
  5438. bytes := src^.SrcStream.Read(src^.SrcBuffer[1], 4096);
  5439. if (bytes <= 0) then begin
  5440. src^.SrcBuffer[1] := $FF;
  5441. src^.SrcBuffer[2] := JPEG_EOI;
  5442. bytes := 2;
  5443. end;
  5444. src^.pub.next_input_byte := @(src^.SrcBuffer[1]);
  5445. src^.pub.bytes_in_buffer := bytes;
  5446. result := true;
  5447. end;
  5448. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5449. procedure glBitmap_libJPEG_skip_input_data(cinfo: j_decompress_ptr; num_bytes: Longint); cdecl;
  5450. var
  5451. src: glBitmap_libJPEG_source_mgr_ptr;
  5452. begin
  5453. src := glBitmap_libJPEG_source_mgr_ptr(cinfo^.src);
  5454. if num_bytes > 0 then begin
  5455. // wanted byte isn't in buffer so set stream position and read buffer
  5456. if num_bytes > src^.pub.bytes_in_buffer then begin
  5457. src^.SrcStream.Position := src^.SrcStream.Position + num_bytes - src^.pub.bytes_in_buffer;
  5458. src^.pub.fill_input_buffer(cinfo);
  5459. end else begin
  5460. // wanted byte is in buffer so only skip
  5461. inc(src^.pub.next_input_byte, num_bytes);
  5462. dec(src^.pub.bytes_in_buffer, num_bytes);
  5463. end;
  5464. end;
  5465. end;
  5466. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5467. function glBitmap_libJPEG_empty_output_buffer(cinfo: j_compress_ptr): boolean; cdecl;
  5468. var
  5469. dest: glBitmap_libJPEG_dest_mgr_ptr;
  5470. begin
  5471. dest := glBitmap_libJPEG_dest_mgr_ptr(cinfo^.dest);
  5472. if dest^.pub.free_in_buffer < Cardinal(Length(dest^.DestBuffer)) then begin
  5473. // write complete buffer
  5474. dest^.DestStream.Write(dest^.DestBuffer[1], SizeOf(dest^.DestBuffer));
  5475. // reset buffer
  5476. dest^.pub.next_output_byte := @dest^.DestBuffer[1];
  5477. dest^.pub.free_in_buffer := Length(dest^.DestBuffer);
  5478. end;
  5479. result := true;
  5480. end;
  5481. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5482. procedure glBitmap_libJPEG_term_destination(cinfo: j_compress_ptr); cdecl;
  5483. var
  5484. Idx: Integer;
  5485. dest: glBitmap_libJPEG_dest_mgr_ptr;
  5486. begin
  5487. dest := glBitmap_libJPEG_dest_mgr_ptr(cinfo^.dest);
  5488. for Idx := Low(dest^.DestBuffer) to High(dest^.DestBuffer) do begin
  5489. // check for endblock
  5490. if (Idx < High(dest^.DestBuffer)) and (dest^.DestBuffer[Idx] = $FF) and (dest^.DestBuffer[Idx +1] = JPEG_EOI) then begin
  5491. // write endblock
  5492. dest^.DestStream.Write(dest^.DestBuffer[Idx], 2);
  5493. // leave
  5494. break;
  5495. end else
  5496. dest^.DestStream.Write(dest^.DestBuffer[Idx], 1);
  5497. end;
  5498. end;
  5499. {$ENDIF}
  5500. {$IFDEF GLB_SUPPORT_JPEG_READ}
  5501. {$IF DEFINED(GLB_LAZ_JPEG)}
  5502. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5503. function TglBitmap.LoadJPEG(const aStream: TStream): Boolean;
  5504. const
  5505. MAGIC_LEN = 2;
  5506. JPEG_MAGIC: String[MAGIC_LEN] = #$FF#$D8;
  5507. var
  5508. jpeg: TJPEGImage;
  5509. intf: TLazIntfImage;
  5510. StreamPos: Int64;
  5511. magic: String[MAGIC_LEN];
  5512. begin
  5513. result := true;
  5514. StreamPos := aStream.Position;
  5515. SetLength(magic, MAGIC_LEN);
  5516. aStream.Read(magic[1], MAGIC_LEN);
  5517. aStream.Position := StreamPos;
  5518. if (magic <> JPEG_MAGIC) then begin
  5519. result := false;
  5520. exit;
  5521. end;
  5522. jpeg := TJPEGImage.Create;
  5523. try try
  5524. jpeg.LoadFromStream(aStream);
  5525. intf := TLazIntfImage.Create(0, 0);
  5526. try try
  5527. intf.LoadFromBitmap(jpeg.BitmapHandle, jpeg.MaskHandle);
  5528. AssignFromLazIntfImage(intf);
  5529. except
  5530. result := false;
  5531. aStream.Position := StreamPos;
  5532. exit;
  5533. end;
  5534. finally
  5535. intf.Free;
  5536. end;
  5537. except
  5538. result := false;
  5539. aStream.Position := StreamPos;
  5540. exit;
  5541. end;
  5542. finally
  5543. jpeg.Free;
  5544. end;
  5545. end;
  5546. {$ELSEIF DEFINED(GLB_SDL_IMAGE)}
  5547. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5548. function TglBitmap.LoadJPEG(const aStream: TStream): Boolean;
  5549. var
  5550. Surface: PSDL_Surface;
  5551. RWops: PSDL_RWops;
  5552. begin
  5553. result := false;
  5554. RWops := glBitmapCreateRWops(aStream);
  5555. try
  5556. if IMG_isJPG(RWops) > 0 then begin
  5557. Surface := IMG_LoadJPG_RW(RWops);
  5558. try
  5559. AssignFromSurface(Surface);
  5560. result := true;
  5561. finally
  5562. SDL_FreeSurface(Surface);
  5563. end;
  5564. end;
  5565. finally
  5566. SDL_FreeRW(RWops);
  5567. end;
  5568. end;
  5569. {$ELSEIF DEFINED(GLB_LIB_JPEG)}
  5570. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5571. function TglBitmap.LoadJPEG(const aStream: TStream): Boolean;
  5572. var
  5573. StreamPos: Int64;
  5574. Temp: array[0..1]of Byte;
  5575. jpeg: jpeg_decompress_struct;
  5576. jpeg_err: jpeg_error_mgr;
  5577. IntFormat: TglBitmapFormat;
  5578. pImage: pByte;
  5579. TempHeight, TempWidth: Integer;
  5580. pTemp: pByte;
  5581. Row: Integer;
  5582. FormatDesc: TFormatDescriptor;
  5583. begin
  5584. result := false;
  5585. if not init_libJPEG then
  5586. raise Exception.Create('LoadJPG - unable to initialize libJPEG.');
  5587. try
  5588. // reading first two bytes to test file and set cursor back to begin
  5589. StreamPos := aStream.Position;
  5590. aStream.Read({%H-}Temp[0], 2);
  5591. aStream.Position := StreamPos;
  5592. // if Bitmap then read file.
  5593. if ((Temp[0] = $FF) and (Temp[1] = $D8)) then begin
  5594. FillChar(jpeg{%H-}, SizeOf(jpeg_decompress_struct), $00);
  5595. FillChar(jpeg_err{%H-}, SizeOf(jpeg_error_mgr), $00);
  5596. // error managment
  5597. jpeg.err := jpeg_std_error(@jpeg_err);
  5598. jpeg_err.error_exit := glBitmap_libJPEG_error_exit;
  5599. jpeg_err.output_message := glBitmap_libJPEG_output_message;
  5600. // decompression struct
  5601. jpeg_create_decompress(@jpeg);
  5602. // allocation space for streaming methods
  5603. jpeg.src := jpeg.mem^.alloc_small(@jpeg, JPOOL_PERMANENT, SizeOf(glBitmap_libJPEG_source_mgr));
  5604. // seeting up custom functions
  5605. with glBitmap_libJPEG_source_mgr_ptr(jpeg.src)^ do begin
  5606. pub.init_source := glBitmap_libJPEG_init_source;
  5607. pub.fill_input_buffer := glBitmap_libJPEG_fill_input_buffer;
  5608. pub.skip_input_data := glBitmap_libJPEG_skip_input_data;
  5609. pub.resync_to_restart := jpeg_resync_to_restart; // use default method
  5610. pub.term_source := glBitmap_libJPEG_term_source;
  5611. pub.bytes_in_buffer := 0; // forces fill_input_buffer on first read
  5612. pub.next_input_byte := nil; // until buffer loaded
  5613. SrcStream := aStream;
  5614. end;
  5615. // set global decoding state
  5616. jpeg.global_state := DSTATE_START;
  5617. // read header of jpeg
  5618. jpeg_read_header(@jpeg, false);
  5619. // setting output parameter
  5620. case jpeg.jpeg_color_space of
  5621. JCS_GRAYSCALE:
  5622. begin
  5623. jpeg.out_color_space := JCS_GRAYSCALE;
  5624. IntFormat := tfLuminance8;
  5625. end;
  5626. else
  5627. jpeg.out_color_space := JCS_RGB;
  5628. IntFormat := tfRGB8;
  5629. end;
  5630. // reading image
  5631. jpeg_start_decompress(@jpeg);
  5632. TempHeight := jpeg.output_height;
  5633. TempWidth := jpeg.output_width;
  5634. FormatDesc := TFormatDescriptor.Get(IntFormat);
  5635. // creating new image
  5636. GetMem(pImage, FormatDesc.GetSize(TempWidth, TempHeight));
  5637. try
  5638. pTemp := pImage;
  5639. for Row := 0 to TempHeight -1 do begin
  5640. jpeg_read_scanlines(@jpeg, @pTemp, 1);
  5641. Inc(pTemp, FormatDesc.GetSize(TempWidth, 1));
  5642. end;
  5643. // finish decompression
  5644. jpeg_finish_decompress(@jpeg);
  5645. // destroy decompression
  5646. jpeg_destroy_decompress(@jpeg);
  5647. SetDataPointer(pImage, IntFormat, TempWidth, TempHeight); //be careful, Data could be freed by this method
  5648. result := true;
  5649. except
  5650. if Assigned(pImage) then
  5651. FreeMem(pImage);
  5652. raise;
  5653. end;
  5654. end;
  5655. finally
  5656. quit_libJPEG;
  5657. end;
  5658. end;
  5659. {$ELSEIF DEFINED(GLB_DELPHI_JPEG)}
  5660. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5661. function TglBitmap.LoadJPEG(const aStream: TStream): Boolean;
  5662. var
  5663. bmp: TBitmap;
  5664. jpg: TJPEGImage;
  5665. StreamPos: Int64;
  5666. Temp: array[0..1]of Byte;
  5667. begin
  5668. result := false;
  5669. // reading first two bytes to test file and set cursor back to begin
  5670. StreamPos := aStream.Position;
  5671. aStream.Read(Temp[0], 2);
  5672. aStream.Position := StreamPos;
  5673. // if Bitmap then read file.
  5674. if ((Temp[0] = $FF) and (Temp[1] = $D8)) then begin
  5675. bmp := TBitmap.Create;
  5676. try
  5677. jpg := TJPEGImage.Create;
  5678. try
  5679. jpg.LoadFromStream(aStream);
  5680. bmp.Assign(jpg);
  5681. result := AssignFromBitmap(bmp);
  5682. finally
  5683. jpg.Free;
  5684. end;
  5685. finally
  5686. bmp.Free;
  5687. end;
  5688. end;
  5689. end;
  5690. {$IFEND}
  5691. {$ENDIF}
  5692. {$IFDEF GLB_SUPPORT_JPEG_WRITE}
  5693. {$IF DEFINED(GLB_LAZ_JPEG)}
  5694. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5695. procedure TglBitmap.SaveJPEG(const aStream: TStream);
  5696. var
  5697. jpeg: TJPEGImage;
  5698. intf: TLazIntfImage;
  5699. raw: TRawImage;
  5700. begin
  5701. jpeg := TJPEGImage.Create;
  5702. intf := TLazIntfImage.Create(0, 0);
  5703. try
  5704. if not AssignToLazIntfImage(intf) then
  5705. raise EglBitmap.Create('unable to create LazIntfImage from glBitmap');
  5706. intf.GetRawImage(raw);
  5707. jpeg.LoadFromRawImage(raw, false);
  5708. jpeg.SaveToStream(aStream);
  5709. finally
  5710. intf.Free;
  5711. jpeg.Free;
  5712. end;
  5713. end;
  5714. {$ELSEIF DEFINED(GLB_LIB_JPEG)}
  5715. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5716. procedure TglBitmap.SaveJPEG(const aStream: TStream);
  5717. var
  5718. jpeg: jpeg_compress_struct;
  5719. jpeg_err: jpeg_error_mgr;
  5720. Row: Integer;
  5721. pTemp, pTemp2: pByte;
  5722. procedure CopyRow(pDest, pSource: pByte);
  5723. var
  5724. X: Integer;
  5725. begin
  5726. for X := 0 to Width - 1 do begin
  5727. pByteArray(pDest)^[0] := pByteArray(pSource)^[2];
  5728. pByteArray(pDest)^[1] := pByteArray(pSource)^[1];
  5729. pByteArray(pDest)^[2] := pByteArray(pSource)^[0];
  5730. Inc(pDest, 3);
  5731. Inc(pSource, 3);
  5732. end;
  5733. end;
  5734. begin
  5735. if not (ftJPEG in FormatGetSupportedFiles(Format)) then
  5736. raise EglBitmapUnsupportedFormat.Create(Format);
  5737. if not init_libJPEG then
  5738. raise Exception.Create('SaveJPG - unable to initialize libJPEG.');
  5739. try
  5740. FillChar(jpeg{%H-}, SizeOf(jpeg_compress_struct), $00);
  5741. FillChar(jpeg_err{%H-}, SizeOf(jpeg_error_mgr), $00);
  5742. // error managment
  5743. jpeg.err := jpeg_std_error(@jpeg_err);
  5744. jpeg_err.error_exit := glBitmap_libJPEG_error_exit;
  5745. jpeg_err.output_message := glBitmap_libJPEG_output_message;
  5746. // compression struct
  5747. jpeg_create_compress(@jpeg);
  5748. // allocation space for streaming methods
  5749. jpeg.dest := jpeg.mem^.alloc_small(@jpeg, JPOOL_PERMANENT, SizeOf(glBitmap_libJPEG_dest_mgr));
  5750. // seeting up custom functions
  5751. with glBitmap_libJPEG_dest_mgr_ptr(jpeg.dest)^ do begin
  5752. pub.init_destination := glBitmap_libJPEG_init_destination;
  5753. pub.empty_output_buffer := glBitmap_libJPEG_empty_output_buffer;
  5754. pub.term_destination := glBitmap_libJPEG_term_destination;
  5755. pub.next_output_byte := @DestBuffer[1];
  5756. pub.free_in_buffer := Length(DestBuffer);
  5757. DestStream := aStream;
  5758. end;
  5759. // very important state
  5760. jpeg.global_state := CSTATE_START;
  5761. jpeg.image_width := Width;
  5762. jpeg.image_height := Height;
  5763. case Format of
  5764. tfAlpha8, tfLuminance8: begin
  5765. jpeg.input_components := 1;
  5766. jpeg.in_color_space := JCS_GRAYSCALE;
  5767. end;
  5768. tfRGB8, tfBGR8: begin
  5769. jpeg.input_components := 3;
  5770. jpeg.in_color_space := JCS_RGB;
  5771. end;
  5772. end;
  5773. jpeg_set_defaults(@jpeg);
  5774. jpeg_set_quality(@jpeg, 95, true);
  5775. jpeg_start_compress(@jpeg, true);
  5776. pTemp := Data;
  5777. if Format = tfBGR8 then
  5778. GetMem(pTemp2, fRowSize)
  5779. else
  5780. pTemp2 := pTemp;
  5781. try
  5782. for Row := 0 to jpeg.image_height -1 do begin
  5783. // prepare row
  5784. if Format = tfBGR8 then
  5785. CopyRow(pTemp2, pTemp)
  5786. else
  5787. pTemp2 := pTemp;
  5788. // write row
  5789. jpeg_write_scanlines(@jpeg, @pTemp2, 1);
  5790. inc(pTemp, fRowSize);
  5791. end;
  5792. finally
  5793. // free memory
  5794. if Format = tfBGR8 then
  5795. FreeMem(pTemp2);
  5796. end;
  5797. jpeg_finish_compress(@jpeg);
  5798. jpeg_destroy_compress(@jpeg);
  5799. finally
  5800. quit_libJPEG;
  5801. end;
  5802. end;
  5803. {$ELSEIF DEFINED(GLB_DELPHI_JPEG)}
  5804. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5805. procedure TglBitmap.SaveJPEG(const aStream: TStream);
  5806. var
  5807. Bmp: TBitmap;
  5808. Jpg: TJPEGImage;
  5809. begin
  5810. if not (ftJPEG in FormatGetSupportedFiles(Format)) then
  5811. raise EglBitmapUnsupportedFormat.Create(Format);
  5812. Bmp := TBitmap.Create;
  5813. try
  5814. Jpg := TJPEGImage.Create;
  5815. try
  5816. AssignToBitmap(Bmp);
  5817. if (Format in [tfAlpha8, tfLuminance8]) then begin
  5818. Jpg.Grayscale := true;
  5819. Jpg.PixelFormat := jf8Bit;
  5820. end;
  5821. Jpg.Assign(Bmp);
  5822. Jpg.SaveToStream(aStream);
  5823. finally
  5824. FreeAndNil(Jpg);
  5825. end;
  5826. finally
  5827. FreeAndNil(Bmp);
  5828. end;
  5829. end;
  5830. {$IFEND}
  5831. {$ENDIF}
  5832. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5833. //BMP/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5834. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5835. const
  5836. BMP_MAGIC = $4D42;
  5837. BMP_COMP_RGB = 0;
  5838. BMP_COMP_RLE8 = 1;
  5839. BMP_COMP_RLE4 = 2;
  5840. BMP_COMP_BITFIELDS = 3;
  5841. type
  5842. TBMPHeader = packed record
  5843. bfType: Word;
  5844. bfSize: Cardinal;
  5845. bfReserved1: Word;
  5846. bfReserved2: Word;
  5847. bfOffBits: Cardinal;
  5848. end;
  5849. TBMPInfo = packed record
  5850. biSize: Cardinal;
  5851. biWidth: Longint;
  5852. biHeight: Longint;
  5853. biPlanes: Word;
  5854. biBitCount: Word;
  5855. biCompression: Cardinal;
  5856. biSizeImage: Cardinal;
  5857. biXPelsPerMeter: Longint;
  5858. biYPelsPerMeter: Longint;
  5859. biClrUsed: Cardinal;
  5860. biClrImportant: Cardinal;
  5861. end;
  5862. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5863. function TglBitmap.LoadBMP(const aStream: TStream): Boolean;
  5864. //////////////////////////////////////////////////////////////////////////////////////////////////
  5865. function ReadInfo(out aInfo: TBMPInfo; out aMask: TglBitmapColorRec): TglBitmapFormat;
  5866. begin
  5867. result := tfEmpty;
  5868. aStream.Read(aInfo{%H-}, SizeOf(aInfo));
  5869. FillChar(aMask{%H-}, SizeOf(aMask), 0);
  5870. //Read Compression
  5871. case aInfo.biCompression of
  5872. BMP_COMP_RLE4,
  5873. BMP_COMP_RLE8: begin
  5874. raise EglBitmap.Create('RLE compression is not supported');
  5875. end;
  5876. BMP_COMP_BITFIELDS: begin
  5877. if (aInfo.biBitCount = 16) or (aInfo.biBitCount = 32) then begin
  5878. aStream.Read(aMask.r, SizeOf(aMask.r));
  5879. aStream.Read(aMask.g, SizeOf(aMask.g));
  5880. aStream.Read(aMask.b, SizeOf(aMask.b));
  5881. aStream.Read(aMask.a, SizeOf(aMask.a));
  5882. end else
  5883. raise EglBitmap.Create('Bitfields are only supported for 16bit and 32bit formats');
  5884. end;
  5885. end;
  5886. //get suitable format
  5887. case aInfo.biBitCount of
  5888. 8: result := tfLuminance8;
  5889. 16: result := tfBGR5;
  5890. 24: result := tfBGR8;
  5891. 32: result := tfBGRA8;
  5892. end;
  5893. end;
  5894. function ReadColorTable(var aFormat: TglBitmapFormat; const aInfo: TBMPInfo): TbmpColorTableFormat;
  5895. var
  5896. i, c: Integer;
  5897. ColorTable: TbmpColorTable;
  5898. begin
  5899. result := nil;
  5900. if (aInfo.biBitCount >= 16) then
  5901. exit;
  5902. aFormat := tfLuminance8;
  5903. c := aInfo.biClrUsed;
  5904. if (c = 0) then
  5905. c := 1 shl aInfo.biBitCount;
  5906. SetLength(ColorTable, c);
  5907. for i := 0 to c-1 do begin
  5908. aStream.Read(ColorTable[i], SizeOf(TbmpColorTableEnty));
  5909. if (ColorTable[i].r <> ColorTable[i].g) or (ColorTable[i].g <> ColorTable[i].b) then
  5910. aFormat := tfRGB8;
  5911. end;
  5912. result := TbmpColorTableFormat.Create;
  5913. result.PixelSize := aInfo.biBitCount / 8;
  5914. result.ColorTable := ColorTable;
  5915. result.Range := glBitmapColorRec($FF, $FF, $FF, $00);
  5916. end;
  5917. //////////////////////////////////////////////////////////////////////////////////////////////////
  5918. function CheckBitfields(var aFormat: TglBitmapFormat; const aMask: TglBitmapColorRec;
  5919. const aInfo: TBMPInfo): TbmpBitfieldFormat;
  5920. var
  5921. TmpFormat: TglBitmapFormat;
  5922. FormatDesc: TFormatDescriptor;
  5923. begin
  5924. result := nil;
  5925. if (aMask.r <> 0) or (aMask.g <> 0) or (aMask.b <> 0) or (aMask.a <> 0) then begin
  5926. for TmpFormat := High(TglBitmapFormat) downto Low(TglBitmapFormat) do begin
  5927. FormatDesc := TFormatDescriptor.Get(TmpFormat);
  5928. if FormatDesc.MaskMatch(aMask.r, aMask.g, aMask.b, aMask.a) then begin
  5929. aFormat := FormatDesc.Format;
  5930. exit;
  5931. end;
  5932. end;
  5933. if (aMask.a = 0) and TFormatDescriptor.Get(aFormat).HasAlpha then
  5934. aFormat := TFormatDescriptor.Get(aFormat).WithoutAlpha;
  5935. if (aMask.a <> 0) and not TFormatDescriptor.Get(aFormat).HasAlpha then
  5936. aFormat := TFormatDescriptor.Get(aFormat).WithAlpha;
  5937. result := TbmpBitfieldFormat.Create;
  5938. result.PixelSize := aInfo.biBitCount / 8;
  5939. result.RedMask := aMask.r;
  5940. result.GreenMask := aMask.g;
  5941. result.BlueMask := aMask.b;
  5942. result.AlphaMask := aMask.a;
  5943. end;
  5944. end;
  5945. var
  5946. //simple types
  5947. StartPos: Int64;
  5948. ImageSize, rbLineSize, wbLineSize, Padding, i: Integer;
  5949. PaddingBuff: Cardinal;
  5950. LineBuf, ImageData, TmpData: PByte;
  5951. SourceMD, DestMD: Pointer;
  5952. BmpFormat: TglBitmapFormat;
  5953. //records
  5954. Mask: TglBitmapColorRec;
  5955. Header: TBMPHeader;
  5956. Info: TBMPInfo;
  5957. //classes
  5958. SpecialFormat: TFormatDescriptor;
  5959. FormatDesc: TFormatDescriptor;
  5960. //////////////////////////////////////////////////////////////////////////////////////////////////
  5961. procedure SpecialFormatReadLine(aData: PByte; aLineBuf: PByte);
  5962. var
  5963. i: Integer;
  5964. Pixel: TglBitmapPixelData;
  5965. begin
  5966. aStream.Read(aLineBuf^, rbLineSize);
  5967. SpecialFormat.PreparePixel(Pixel);
  5968. for i := 0 to Info.biWidth-1 do begin
  5969. SpecialFormat.Unmap(aLineBuf, Pixel, SourceMD);
  5970. glBitmapConvertPixel(Pixel, SpecialFormat, FormatDesc);
  5971. FormatDesc.Map(Pixel, aData, DestMD);
  5972. end;
  5973. end;
  5974. begin
  5975. result := false;
  5976. BmpFormat := tfEmpty;
  5977. SpecialFormat := nil;
  5978. LineBuf := nil;
  5979. SourceMD := nil;
  5980. DestMD := nil;
  5981. // Header
  5982. StartPos := aStream.Position;
  5983. aStream.Read(Header{%H-}, SizeOf(Header));
  5984. if Header.bfType = BMP_MAGIC then begin
  5985. try try
  5986. BmpFormat := ReadInfo(Info, Mask);
  5987. SpecialFormat := ReadColorTable(BmpFormat, Info);
  5988. if not Assigned(SpecialFormat) then
  5989. SpecialFormat := CheckBitfields(BmpFormat, Mask, Info);
  5990. aStream.Position := StartPos + Header.bfOffBits;
  5991. if (BmpFormat <> tfEmpty) then begin
  5992. FormatDesc := TFormatDescriptor.Get(BmpFormat);
  5993. rbLineSize := Round(Info.biWidth * Info.biBitCount / 8); //ReadBuffer LineSize
  5994. wbLineSize := Trunc(Info.biWidth * FormatDesc.PixelSize);
  5995. Padding := (((Info.biWidth * Info.biBitCount + 31) and - 32) shr 3) - rbLineSize;
  5996. //get Memory
  5997. DestMD := FormatDesc.CreateMappingData;
  5998. ImageSize := FormatDesc.GetSize(Info.biWidth, abs(Info.biHeight));
  5999. GetMem(ImageData, ImageSize);
  6000. if Assigned(SpecialFormat) then begin
  6001. GetMem(LineBuf, rbLineSize); //tmp Memory for converting Bitfields
  6002. SourceMD := SpecialFormat.CreateMappingData;
  6003. end;
  6004. //read Data
  6005. try try
  6006. FillChar(ImageData^, ImageSize, $FF);
  6007. TmpData := ImageData;
  6008. if (Info.biHeight > 0) then
  6009. Inc(TmpData, wbLineSize * (Info.biHeight-1));
  6010. for i := 0 to Abs(Info.biHeight)-1 do begin
  6011. if Assigned(SpecialFormat) then
  6012. SpecialFormatReadLine(TmpData, LineBuf) //if is special format read and convert data
  6013. else
  6014. aStream.Read(TmpData^, wbLineSize); //else only read data
  6015. if (Info.biHeight > 0) then
  6016. dec(TmpData, wbLineSize)
  6017. else
  6018. inc(TmpData, wbLineSize);
  6019. aStream.Read(PaddingBuff{%H-}, Padding);
  6020. end;
  6021. SetDataPointer(ImageData, BmpFormat, Info.biWidth, abs(Info.biHeight)); //be careful, Data could be freed by this method
  6022. result := true;
  6023. finally
  6024. if Assigned(LineBuf) then
  6025. FreeMem(LineBuf);
  6026. if Assigned(SourceMD) then
  6027. SpecialFormat.FreeMappingData(SourceMD);
  6028. FormatDesc.FreeMappingData(DestMD);
  6029. end;
  6030. except
  6031. if Assigned(ImageData) then
  6032. FreeMem(ImageData);
  6033. raise;
  6034. end;
  6035. end else
  6036. raise EglBitmap.Create('LoadBMP - No suitable format found');
  6037. except
  6038. aStream.Position := StartPos;
  6039. raise;
  6040. end;
  6041. finally
  6042. FreeAndNil(SpecialFormat);
  6043. end;
  6044. end
  6045. else aStream.Position := StartPos;
  6046. end;
  6047. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6048. procedure TglBitmap.SaveBMP(const aStream: TStream);
  6049. var
  6050. Header: TBMPHeader;
  6051. Info: TBMPInfo;
  6052. Converter: TFormatDescriptor;
  6053. FormatDesc: TFormatDescriptor;
  6054. SourceFD, DestFD: Pointer;
  6055. pData, srcData, dstData, ConvertBuffer: pByte;
  6056. Pixel: TglBitmapPixelData;
  6057. ImageSize, wbLineSize, rbLineSize, Padding, LineIdx, PixelIdx: Integer;
  6058. RedMask, GreenMask, BlueMask, AlphaMask: Cardinal;
  6059. PaddingBuff: Cardinal;
  6060. function GetLineWidth : Integer;
  6061. begin
  6062. result := ((Info.biWidth * Info.biBitCount + 31) and - 32) shr 3;
  6063. end;
  6064. begin
  6065. if not (ftBMP in FormatGetSupportedFiles(Format)) then
  6066. raise EglBitmapUnsupportedFormat.Create(Format);
  6067. Converter := nil;
  6068. FormatDesc := TFormatDescriptor.Get(Format);
  6069. ImageSize := FormatDesc.GetSize(Dimension);
  6070. FillChar(Header{%H-}, SizeOf(Header), 0);
  6071. Header.bfType := BMP_MAGIC;
  6072. Header.bfSize := SizeOf(Header) + SizeOf(Info) + ImageSize;
  6073. Header.bfReserved1 := 0;
  6074. Header.bfReserved2 := 0;
  6075. Header.bfOffBits := SizeOf(Header) + SizeOf(Info);
  6076. FillChar(Info{%H-}, SizeOf(Info), 0);
  6077. Info.biSize := SizeOf(Info);
  6078. Info.biWidth := Width;
  6079. Info.biHeight := Height;
  6080. Info.biPlanes := 1;
  6081. Info.biCompression := BMP_COMP_RGB;
  6082. Info.biSizeImage := ImageSize;
  6083. try
  6084. case Format of
  6085. tfLuminance4: begin
  6086. Info.biBitCount := 4;
  6087. Header.bfSize := Header.bfSize + 16 * SizeOf(Cardinal);
  6088. Header.bfOffBits := Header.bfOffBits + 16 * SizeOf(Cardinal); //16 ColorTable entries
  6089. Converter := TbmpColorTableFormat.Create;
  6090. with (Converter as TbmpColorTableFormat) do begin
  6091. PixelSize := 0.5;
  6092. Format := Format;
  6093. Range := glBitmapColorRec($F, $F, $F, $0);
  6094. CreateColorTable;
  6095. end;
  6096. end;
  6097. tfR3G3B2, tfLuminance8: begin
  6098. Info.biBitCount := 8;
  6099. Header.bfSize := Header.bfSize + 256 * SizeOf(Cardinal);
  6100. Header.bfOffBits := Header.bfOffBits + 256 * SizeOf(Cardinal); //256 ColorTable entries
  6101. Converter := TbmpColorTableFormat.Create;
  6102. with (Converter as TbmpColorTableFormat) do begin
  6103. PixelSize := 1;
  6104. Format := Format;
  6105. if (Format = tfR3G3B2) then begin
  6106. Range := glBitmapColorRec($7, $7, $3, $0);
  6107. Shift := glBitmapShiftRec(0, 3, 6, 0);
  6108. end else
  6109. Range := glBitmapColorRec($FF, $FF, $FF, $0);
  6110. CreateColorTable;
  6111. end;
  6112. end;
  6113. tfRGB4, tfRGB5, tfR5G6B5, tfRGB5A1, tfRGBA4,
  6114. tfBGR4, tfBGR5, tfB5G6R5, tfBGR5A1, tfBGRA4: begin
  6115. Info.biBitCount := 16;
  6116. Info.biCompression := BMP_COMP_BITFIELDS;
  6117. end;
  6118. tfBGR8, tfRGB8: begin
  6119. Info.biBitCount := 24;
  6120. if (Format = tfRGB8) then
  6121. Converter := TfdBGR8.Create; //use BGR8 Format Descriptor to Swap RGB Values
  6122. end;
  6123. tfRGB10, tfRGB10A2, tfRGBA8,
  6124. tfBGR10, tfBGR10A2, tfBGRA8: begin
  6125. Info.biBitCount := 32;
  6126. Info.biCompression := BMP_COMP_BITFIELDS;
  6127. end;
  6128. else
  6129. raise EglBitmapUnsupportedFormat.Create(Format);
  6130. end;
  6131. Info.biXPelsPerMeter := 2835;
  6132. Info.biYPelsPerMeter := 2835;
  6133. // prepare bitmasks
  6134. if Info.biCompression = BMP_COMP_BITFIELDS then begin
  6135. Header.bfSize := Header.bfSize + 4 * SizeOf(Cardinal);
  6136. Header.bfOffBits := Header.bfOffBits + 4 * SizeOf(Cardinal);
  6137. RedMask := FormatDesc.RedMask;
  6138. GreenMask := FormatDesc.GreenMask;
  6139. BlueMask := FormatDesc.BlueMask;
  6140. AlphaMask := FormatDesc.AlphaMask;
  6141. end;
  6142. // headers
  6143. aStream.Write(Header, SizeOf(Header));
  6144. aStream.Write(Info, SizeOf(Info));
  6145. // colortable
  6146. if Assigned(Converter) and (Converter is TbmpColorTableFormat) then
  6147. with (Converter as TbmpColorTableFormat) do
  6148. aStream.Write(ColorTable[0].b,
  6149. SizeOf(TbmpColorTableEnty) * Length(ColorTable));
  6150. // bitmasks
  6151. if Info.biCompression = BMP_COMP_BITFIELDS then begin
  6152. aStream.Write(RedMask, SizeOf(Cardinal));
  6153. aStream.Write(GreenMask, SizeOf(Cardinal));
  6154. aStream.Write(BlueMask, SizeOf(Cardinal));
  6155. aStream.Write(AlphaMask, SizeOf(Cardinal));
  6156. end;
  6157. // image data
  6158. rbLineSize := Round(Info.biWidth * FormatDesc.PixelSize);
  6159. wbLineSize := Round(Info.biWidth * Info.biBitCount / 8);
  6160. Padding := GetLineWidth - wbLineSize;
  6161. PaddingBuff := 0;
  6162. pData := Data;
  6163. inc(pData, (Height-1) * rbLineSize);
  6164. // prepare row buffer. But only for RGB because RGBA supports color masks
  6165. // so it's possible to change color within the image.
  6166. if Assigned(Converter) then begin
  6167. FormatDesc.PreparePixel(Pixel);
  6168. GetMem(ConvertBuffer, wbLineSize);
  6169. SourceFD := FormatDesc.CreateMappingData;
  6170. DestFD := Converter.CreateMappingData;
  6171. end else
  6172. ConvertBuffer := nil;
  6173. try
  6174. for LineIdx := 0 to Height - 1 do begin
  6175. // preparing row
  6176. if Assigned(Converter) then begin
  6177. srcData := pData;
  6178. dstData := ConvertBuffer;
  6179. for PixelIdx := 0 to Info.biWidth-1 do begin
  6180. FormatDesc.Unmap(srcData, Pixel, SourceFD);
  6181. glBitmapConvertPixel(Pixel, FormatDesc, Converter);
  6182. Converter.Map(Pixel, dstData, DestFD);
  6183. end;
  6184. aStream.Write(ConvertBuffer^, wbLineSize);
  6185. end else begin
  6186. aStream.Write(pData^, rbLineSize);
  6187. end;
  6188. dec(pData, rbLineSize);
  6189. if (Padding > 0) then
  6190. aStream.Write(PaddingBuff, Padding);
  6191. end;
  6192. finally
  6193. // destroy row buffer
  6194. if Assigned(ConvertBuffer) then begin
  6195. FormatDesc.FreeMappingData(SourceFD);
  6196. Converter.FreeMappingData(DestFD);
  6197. FreeMem(ConvertBuffer);
  6198. end;
  6199. end;
  6200. finally
  6201. if Assigned(Converter) then
  6202. Converter.Free;
  6203. end;
  6204. end;
  6205. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6206. //TGA/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6207. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6208. type
  6209. TTGAHeader = packed record
  6210. ImageID: Byte;
  6211. ColorMapType: Byte;
  6212. ImageType: Byte;
  6213. //ColorMapSpec: Array[0..4] of Byte;
  6214. ColorMapStart: Word;
  6215. ColorMapLength: Word;
  6216. ColorMapEntrySize: Byte;
  6217. OrigX: Word;
  6218. OrigY: Word;
  6219. Width: Word;
  6220. Height: Word;
  6221. Bpp: Byte;
  6222. ImageDesc: Byte;
  6223. end;
  6224. const
  6225. TGA_UNCOMPRESSED_RGB = 2;
  6226. TGA_UNCOMPRESSED_GRAY = 3;
  6227. TGA_COMPRESSED_RGB = 10;
  6228. TGA_COMPRESSED_GRAY = 11;
  6229. TGA_NONE_COLOR_TABLE = 0;
  6230. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6231. function TglBitmap.LoadTGA(const aStream: TStream): Boolean;
  6232. var
  6233. Header: TTGAHeader;
  6234. ImageData: System.PByte;
  6235. StartPosition: Int64;
  6236. PixelSize, LineSize: Integer;
  6237. tgaFormat: TglBitmapFormat;
  6238. FormatDesc: TFormatDescriptor;
  6239. Counter: packed record
  6240. X, Y: packed record
  6241. low, high, dir: Integer;
  6242. end;
  6243. end;
  6244. const
  6245. CACHE_SIZE = $4000;
  6246. ////////////////////////////////////////////////////////////////////////////////////////
  6247. procedure ReadUncompressed;
  6248. var
  6249. i, j: Integer;
  6250. buf, tmp1, tmp2: System.PByte;
  6251. begin
  6252. buf := nil;
  6253. if (Counter.X.dir < 0) then
  6254. GetMem(buf, LineSize);
  6255. try
  6256. while (Counter.Y.low <> Counter.Y.high + counter.Y.dir) do begin
  6257. tmp1 := ImageData;
  6258. inc(tmp1, (Counter.Y.low * LineSize)); //pointer to LineStart
  6259. if (Counter.X.dir < 0) then begin //flip X
  6260. aStream.Read(buf^, LineSize);
  6261. tmp2 := buf;
  6262. inc(tmp2, LineSize - PixelSize); //pointer to last pixel in line
  6263. for i := 0 to Header.Width-1 do begin //for all pixels in line
  6264. for j := 0 to PixelSize-1 do begin //for all bytes in pixel
  6265. tmp1^ := tmp2^;
  6266. inc(tmp1);
  6267. inc(tmp2);
  6268. end;
  6269. dec(tmp2, 2*PixelSize); //move 2 backwards, because j-loop moved 1 forward
  6270. end;
  6271. end else
  6272. aStream.Read(tmp1^, LineSize);
  6273. inc(Counter.Y.low, Counter.Y.dir); //move to next line index
  6274. end;
  6275. finally
  6276. if Assigned(buf) then
  6277. FreeMem(buf);
  6278. end;
  6279. end;
  6280. ////////////////////////////////////////////////////////////////////////////////////////
  6281. procedure ReadCompressed;
  6282. /////////////////////////////////////////////////////////////////
  6283. var
  6284. TmpData: System.PByte;
  6285. LinePixelsRead: Integer;
  6286. procedure CheckLine;
  6287. begin
  6288. if (LinePixelsRead >= Header.Width) then begin
  6289. LinePixelsRead := 0;
  6290. inc(Counter.Y.low, Counter.Y.dir); //next line index
  6291. TmpData := ImageData;
  6292. inc(TmpData, Counter.Y.low * LineSize); //set line
  6293. if (Counter.X.dir < 0) then //if x flipped then
  6294. inc(TmpData, LineSize - PixelSize); //set last pixel
  6295. end;
  6296. end;
  6297. /////////////////////////////////////////////////////////////////
  6298. var
  6299. Cache: PByte;
  6300. CacheSize, CachePos: Integer;
  6301. procedure CachedRead(out Buffer; Count: Integer);
  6302. var
  6303. BytesRead: Integer;
  6304. begin
  6305. if (CachePos + Count > CacheSize) then begin
  6306. //if buffer overflow save non read bytes
  6307. BytesRead := 0;
  6308. if (CacheSize - CachePos > 0) then begin
  6309. BytesRead := CacheSize - CachePos;
  6310. Move(PByteArray(Cache)^[CachePos], Buffer{%H-}, BytesRead);
  6311. inc(CachePos, BytesRead);
  6312. end;
  6313. //load cache from file
  6314. CacheSize := Min(CACHE_SIZE, aStream.Size - aStream.Position);
  6315. aStream.Read(Cache^, CacheSize);
  6316. CachePos := 0;
  6317. //read rest of requested bytes
  6318. if (Count - BytesRead > 0) then begin
  6319. Move(PByteArray(Cache)^[CachePos], TByteArray(Buffer)[BytesRead], Count - BytesRead);
  6320. inc(CachePos, Count - BytesRead);
  6321. end;
  6322. end else begin
  6323. //if no buffer overflow just read the data
  6324. Move(PByteArray(Cache)^[CachePos], Buffer, Count);
  6325. inc(CachePos, Count);
  6326. end;
  6327. end;
  6328. procedure PixelToBuffer(const aData: PByte; var aBuffer: PByte);
  6329. begin
  6330. case PixelSize of
  6331. 1: begin
  6332. aBuffer^ := aData^;
  6333. inc(aBuffer, Counter.X.dir);
  6334. end;
  6335. 2: begin
  6336. PWord(aBuffer)^ := PWord(aData)^;
  6337. inc(aBuffer, 2 * Counter.X.dir);
  6338. end;
  6339. 3: begin
  6340. PByteArray(aBuffer)^[0] := PByteArray(aData)^[0];
  6341. PByteArray(aBuffer)^[1] := PByteArray(aData)^[1];
  6342. PByteArray(aBuffer)^[2] := PByteArray(aData)^[2];
  6343. inc(aBuffer, 3 * Counter.X.dir);
  6344. end;
  6345. 4: begin
  6346. PCardinal(aBuffer)^ := PCardinal(aData)^;
  6347. inc(aBuffer, 4 * Counter.X.dir);
  6348. end;
  6349. end;
  6350. end;
  6351. var
  6352. TotalPixelsToRead, TotalPixelsRead: Integer;
  6353. Temp: Byte;
  6354. buf: array [0..3] of Byte; //1 pixel is max 32bit long
  6355. PixelRepeat: Boolean;
  6356. PixelsToRead, PixelCount: Integer;
  6357. begin
  6358. CacheSize := 0;
  6359. CachePos := 0;
  6360. TotalPixelsToRead := Header.Width * Header.Height;
  6361. TotalPixelsRead := 0;
  6362. LinePixelsRead := 0;
  6363. GetMem(Cache, CACHE_SIZE);
  6364. try
  6365. TmpData := ImageData;
  6366. inc(TmpData, Counter.Y.low * LineSize); //set line
  6367. if (Counter.X.dir < 0) then //if x flipped then
  6368. inc(TmpData, LineSize - PixelSize); //set last pixel
  6369. repeat
  6370. //read CommandByte
  6371. CachedRead(Temp, 1);
  6372. PixelRepeat := (Temp and $80) > 0;
  6373. PixelsToRead := (Temp and $7F) + 1;
  6374. inc(TotalPixelsRead, PixelsToRead);
  6375. if PixelRepeat then
  6376. CachedRead(buf[0], PixelSize);
  6377. while (PixelsToRead > 0) do begin
  6378. CheckLine;
  6379. PixelCount := Min(Header.Width - LinePixelsRead, PixelsToRead); //max read to EOL or EOF
  6380. while (PixelCount > 0) do begin
  6381. if not PixelRepeat then
  6382. CachedRead(buf[0], PixelSize);
  6383. PixelToBuffer(@buf[0], TmpData);
  6384. inc(LinePixelsRead);
  6385. dec(PixelsToRead);
  6386. dec(PixelCount);
  6387. end;
  6388. end;
  6389. until (TotalPixelsRead >= TotalPixelsToRead);
  6390. finally
  6391. FreeMem(Cache);
  6392. end;
  6393. end;
  6394. function IsGrayFormat: Boolean;
  6395. begin
  6396. result := Header.ImageType in [TGA_UNCOMPRESSED_GRAY, TGA_COMPRESSED_GRAY];
  6397. end;
  6398. begin
  6399. result := false;
  6400. // reading header to test file and set cursor back to begin
  6401. StartPosition := aStream.Position;
  6402. aStream.Read(Header{%H-}, SizeOf(Header));
  6403. // no colormapped files
  6404. if (Header.ColorMapType = TGA_NONE_COLOR_TABLE) and (Header.ImageType in [
  6405. TGA_UNCOMPRESSED_RGB, TGA_UNCOMPRESSED_GRAY, TGA_COMPRESSED_RGB, TGA_COMPRESSED_GRAY]) then
  6406. begin
  6407. try
  6408. if Header.ImageID <> 0 then // skip image ID
  6409. aStream.Position := aStream.Position + Header.ImageID;
  6410. tgaFormat := tfEmpty;
  6411. case Header.Bpp of
  6412. 8: if IsGrayFormat then case (Header.ImageDesc and $F) of
  6413. 0: tgaFormat := tfLuminance8;
  6414. 8: tgaFormat := tfAlpha8;
  6415. end;
  6416. 16: if IsGrayFormat then case (Header.ImageDesc and $F) of
  6417. 0: tgaFormat := tfLuminance16;
  6418. 8: tgaFormat := tfLuminance8Alpha8;
  6419. end else case (Header.ImageDesc and $F) of
  6420. 0: tgaFormat := tfBGR5;
  6421. 1: tgaFormat := tfBGR5A1;
  6422. 4: tgaFormat := tfBGRA4;
  6423. end;
  6424. 24: if not IsGrayFormat then case (Header.ImageDesc and $F) of
  6425. 0: tgaFormat := tfBGR8;
  6426. end;
  6427. 32: if not IsGrayFormat then case (Header.ImageDesc and $F) of
  6428. 2: tgaFormat := tfBGR10A2;
  6429. 8: tgaFormat := tfBGRA8;
  6430. end;
  6431. end;
  6432. if (tgaFormat = tfEmpty) then
  6433. raise EglBitmap.Create('LoadTga - unsupported format');
  6434. FormatDesc := TFormatDescriptor.Get(tgaFormat);
  6435. PixelSize := FormatDesc.GetSize(1, 1);
  6436. LineSize := FormatDesc.GetSize(Header.Width, 1);
  6437. GetMem(ImageData, LineSize * Header.Height);
  6438. try
  6439. //column direction
  6440. if ((Header.ImageDesc and (1 shl 4)) > 0) then begin
  6441. Counter.X.low := Header.Height-1;;
  6442. Counter.X.high := 0;
  6443. Counter.X.dir := -1;
  6444. end else begin
  6445. Counter.X.low := 0;
  6446. Counter.X.high := Header.Height-1;
  6447. Counter.X.dir := 1;
  6448. end;
  6449. // Row direction
  6450. if ((Header.ImageDesc and (1 shl 5)) > 0) then begin
  6451. Counter.Y.low := 0;
  6452. Counter.Y.high := Header.Height-1;
  6453. Counter.Y.dir := 1;
  6454. end else begin
  6455. Counter.Y.low := Header.Height-1;;
  6456. Counter.Y.high := 0;
  6457. Counter.Y.dir := -1;
  6458. end;
  6459. // Read Image
  6460. case Header.ImageType of
  6461. TGA_UNCOMPRESSED_RGB, TGA_UNCOMPRESSED_GRAY:
  6462. ReadUncompressed;
  6463. TGA_COMPRESSED_RGB, TGA_COMPRESSED_GRAY:
  6464. ReadCompressed;
  6465. end;
  6466. SetDataPointer(ImageData, tgaFormat, Header.Width, Header.Height); //be careful, Data could be freed by this method
  6467. result := true;
  6468. except
  6469. if Assigned(ImageData) then
  6470. FreeMem(ImageData);
  6471. raise;
  6472. end;
  6473. finally
  6474. aStream.Position := StartPosition;
  6475. end;
  6476. end
  6477. else aStream.Position := StartPosition;
  6478. end;
  6479. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6480. procedure TglBitmap.SaveTGA(const aStream: TStream);
  6481. var
  6482. Header: TTGAHeader;
  6483. LineSize, Size, x, y: Integer;
  6484. Pixel: TglBitmapPixelData;
  6485. LineBuf, SourceData, DestData: PByte;
  6486. SourceMD, DestMD: Pointer;
  6487. FormatDesc: TFormatDescriptor;
  6488. Converter: TFormatDescriptor;
  6489. begin
  6490. if not (ftTGA in FormatGetSupportedFiles(Format)) then
  6491. raise EglBitmapUnsupportedFormat.Create(Format);
  6492. //prepare header
  6493. FillChar(Header{%H-}, SizeOf(Header), 0);
  6494. //set ImageType
  6495. if (Format in [tfLuminance8, tfLuminance6Alpha2, tfLuminance4Alpha4, tfAlpha8,
  6496. tfLuminance16, tfLuminance12Alpha4, tfLuminance8Alpha8]) then
  6497. Header.ImageType := TGA_UNCOMPRESSED_GRAY
  6498. else
  6499. Header.ImageType := TGA_UNCOMPRESSED_RGB;
  6500. //set BitsPerPixel
  6501. if (Format in [tfLuminance8, tfLuminance6Alpha2, tfLuminance4Alpha4, tfAlpha8]) then
  6502. Header.Bpp := 8
  6503. else if (Format in [tfLuminance16, tfLuminance12Alpha4, tfLuminance8Alpha8,
  6504. tfRGB5, tfBGR5, tfRGB5A1, tfBGR5A1, tfRGBA4, tfBGRA4]) then
  6505. Header.Bpp := 16
  6506. else if (Format in [tfBGR8, tfRGB8]) then
  6507. Header.Bpp := 24
  6508. else
  6509. Header.Bpp := 32;
  6510. //set AlphaBitCount
  6511. case Format of
  6512. tfRGB5A1, tfBGR5A1:
  6513. Header.ImageDesc := 1 and $F;
  6514. tfRGB10A2, tfBGR10A2:
  6515. Header.ImageDesc := 2 and $F;
  6516. tfRGBA4, tfBGRA4:
  6517. Header.ImageDesc := 4 and $F;
  6518. tfAlpha8, tfLuminance8Alpha8, tfRGBA8, tfBGRA8:
  6519. Header.ImageDesc := 8 and $F;
  6520. end;
  6521. Header.Width := Width;
  6522. Header.Height := Height;
  6523. Header.ImageDesc := Header.ImageDesc or $20; //flip y
  6524. aStream.Write(Header, SizeOf(Header));
  6525. // convert RGB(A) to BGR(A)
  6526. Converter := nil;
  6527. FormatDesc := TFormatDescriptor.Get(Format);
  6528. Size := FormatDesc.GetSize(Dimension);
  6529. if Format in [tfRGB5, tfRGB5A1, tfRGBA4, tfRGB8, tfRGB10A2, tfRGBA8] then begin
  6530. if (FormatDesc.RGBInverted = tfEmpty) then
  6531. raise EglBitmap.Create('inverted RGB format is empty');
  6532. Converter := TFormatDescriptor.Get(FormatDesc.RGBInverted);
  6533. if not glBitmapColorRecCmp(Converter.Range, FormatDesc.Range) or
  6534. (Converter.PixelSize <> FormatDesc.PixelSize) then
  6535. raise EglBitmap.Create('invalid inverted RGB format');
  6536. end;
  6537. if Assigned(Converter) then begin
  6538. LineSize := FormatDesc.GetSize(Width, 1);
  6539. GetMem(LineBuf, LineSize);
  6540. SourceMD := FormatDesc.CreateMappingData;
  6541. DestMD := Converter.CreateMappingData;
  6542. try
  6543. SourceData := Data;
  6544. for y := 0 to Height-1 do begin
  6545. DestData := LineBuf;
  6546. for x := 0 to Width-1 do begin
  6547. FormatDesc.Unmap(SourceData, Pixel, SourceMD);
  6548. Converter.Map(Pixel, DestData, DestMD);
  6549. end;
  6550. aStream.Write(LineBuf^, LineSize);
  6551. end;
  6552. finally
  6553. FreeMem(LineBuf);
  6554. FormatDesc.FreeMappingData(SourceMD);
  6555. FormatDesc.FreeMappingData(DestMD);
  6556. end;
  6557. end else
  6558. aStream.Write(Data^, Size);
  6559. end;
  6560. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6561. //DDS/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6562. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6563. const
  6564. DDS_MAGIC: Cardinal = $20534444;
  6565. // DDS_header.dwFlags
  6566. DDSD_CAPS = $00000001;
  6567. DDSD_HEIGHT = $00000002;
  6568. DDSD_WIDTH = $00000004;
  6569. DDSD_PIXELFORMAT = $00001000;
  6570. // DDS_header.sPixelFormat.dwFlags
  6571. DDPF_ALPHAPIXELS = $00000001;
  6572. DDPF_ALPHA = $00000002;
  6573. DDPF_FOURCC = $00000004;
  6574. DDPF_RGB = $00000040;
  6575. DDPF_LUMINANCE = $00020000;
  6576. // DDS_header.sCaps.dwCaps1
  6577. DDSCAPS_TEXTURE = $00001000;
  6578. // DDS_header.sCaps.dwCaps2
  6579. DDSCAPS2_CUBEMAP = $00000200;
  6580. D3DFMT_DXT1 = $31545844;
  6581. D3DFMT_DXT3 = $33545844;
  6582. D3DFMT_DXT5 = $35545844;
  6583. type
  6584. TDDSPixelFormat = packed record
  6585. dwSize: Cardinal;
  6586. dwFlags: Cardinal;
  6587. dwFourCC: Cardinal;
  6588. dwRGBBitCount: Cardinal;
  6589. dwRBitMask: Cardinal;
  6590. dwGBitMask: Cardinal;
  6591. dwBBitMask: Cardinal;
  6592. dwABitMask: Cardinal;
  6593. end;
  6594. TDDSCaps = packed record
  6595. dwCaps1: Cardinal;
  6596. dwCaps2: Cardinal;
  6597. dwDDSX: Cardinal;
  6598. dwReserved: Cardinal;
  6599. end;
  6600. TDDSHeader = packed record
  6601. dwSize: Cardinal;
  6602. dwFlags: Cardinal;
  6603. dwHeight: Cardinal;
  6604. dwWidth: Cardinal;
  6605. dwPitchOrLinearSize: Cardinal;
  6606. dwDepth: Cardinal;
  6607. dwMipMapCount: Cardinal;
  6608. dwReserved: array[0..10] of Cardinal;
  6609. PixelFormat: TDDSPixelFormat;
  6610. Caps: TDDSCaps;
  6611. dwReserved2: Cardinal;
  6612. end;
  6613. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6614. function TglBitmap.LoadDDS(const aStream: TStream): Boolean;
  6615. var
  6616. Header: TDDSHeader;
  6617. Converter: TbmpBitfieldFormat;
  6618. function GetDDSFormat: TglBitmapFormat;
  6619. var
  6620. fd: TFormatDescriptor;
  6621. i: Integer;
  6622. Range: TglBitmapColorRec;
  6623. match: Boolean;
  6624. begin
  6625. result := tfEmpty;
  6626. with Header.PixelFormat do begin
  6627. // Compresses
  6628. if ((dwFlags and DDPF_FOURCC) > 0) then begin
  6629. case Header.PixelFormat.dwFourCC of
  6630. D3DFMT_DXT1: result := tfS3tcDtx1RGBA;
  6631. D3DFMT_DXT3: result := tfS3tcDtx3RGBA;
  6632. D3DFMT_DXT5: result := tfS3tcDtx5RGBA;
  6633. end;
  6634. end else if ((Header.PixelFormat.dwFlags and (DDPF_RGB or DDPF_ALPHAPIXELS or DDPF_LUMINANCE)) > 0) then begin
  6635. //find matching format
  6636. for result := High(TglBitmapFormat) downto Low(TglBitmapFormat) do begin
  6637. fd := TFormatDescriptor.Get(result);
  6638. if fd.MaskMatch(dwRBitMask, dwGBitMask, dwBBitMask, dwABitMask) and
  6639. (8 * fd.PixelSize = dwRGBBitCount) then
  6640. exit;
  6641. end;
  6642. //find format with same Range
  6643. Range.r := dwRBitMask;
  6644. Range.g := dwGBitMask;
  6645. Range.b := dwBBitMask;
  6646. Range.a := dwABitMask;
  6647. for i := 0 to 3 do begin
  6648. while ((Range.arr[i] and 1) = 0) and (Range.arr[i] > 0) do
  6649. Range.arr[i] := Range.arr[i] shr 1;
  6650. end;
  6651. for result := High(TglBitmapFormat) downto Low(TglBitmapFormat) do begin
  6652. fd := TFormatDescriptor.Get(result);
  6653. match := true;
  6654. for i := 0 to 3 do
  6655. if (fd.Range.arr[i] <> Range.arr[i]) then begin
  6656. match := false;
  6657. break;
  6658. end;
  6659. if match then
  6660. break;
  6661. end;
  6662. //no format with same range found -> use default
  6663. if (result = tfEmpty) then begin
  6664. if (dwABitMask > 0) then
  6665. result := tfBGRA8
  6666. else
  6667. result := tfBGR8;
  6668. end;
  6669. Converter := TbmpBitfieldFormat.Create;
  6670. Converter.RedMask := dwRBitMask;
  6671. Converter.GreenMask := dwGBitMask;
  6672. Converter.BlueMask := dwBBitMask;
  6673. Converter.AlphaMask := dwABitMask;
  6674. Converter.PixelSize := dwRGBBitCount / 8;
  6675. end;
  6676. end;
  6677. end;
  6678. var
  6679. StreamPos: Int64;
  6680. x, y, LineSize, RowSize, Magic: Cardinal;
  6681. NewImage, TmpData, RowData, SrcData: System.PByte;
  6682. SourceMD, DestMD: Pointer;
  6683. Pixel: TglBitmapPixelData;
  6684. ddsFormat: TglBitmapFormat;
  6685. FormatDesc: TFormatDescriptor;
  6686. begin
  6687. result := false;
  6688. Converter := nil;
  6689. StreamPos := aStream.Position;
  6690. // Magic
  6691. aStream.Read(Magic{%H-}, sizeof(Magic));
  6692. if (Magic <> DDS_MAGIC) then begin
  6693. aStream.Position := StreamPos;
  6694. exit;
  6695. end;
  6696. //Header
  6697. aStream.Read(Header{%H-}, sizeof(Header));
  6698. if (Header.dwSize <> SizeOf(Header)) or
  6699. ((Header.dwFlags and (DDSD_PIXELFORMAT or DDSD_CAPS or DDSD_WIDTH or DDSD_HEIGHT)) <>
  6700. (DDSD_PIXELFORMAT or DDSD_CAPS or DDSD_WIDTH or DDSD_HEIGHT)) then
  6701. begin
  6702. aStream.Position := StreamPos;
  6703. exit;
  6704. end;
  6705. if ((Header.Caps.dwCaps1 and DDSCAPS2_CUBEMAP) > 0) then
  6706. raise EglBitmap.Create('LoadDDS - CubeMaps are not supported');
  6707. ddsFormat := GetDDSFormat;
  6708. try
  6709. if (ddsFormat = tfEmpty) then
  6710. raise EglBitmap.Create('LoadDDS - unsupported Pixelformat found.');
  6711. FormatDesc := TFormatDescriptor.Get(ddsFormat);
  6712. LineSize := Trunc(Header.dwWidth * FormatDesc.PixelSize);
  6713. GetMem(NewImage, Header.dwHeight * LineSize);
  6714. try
  6715. TmpData := NewImage;
  6716. //Converter needed
  6717. if Assigned(Converter) then begin
  6718. RowSize := Round(Header.dwWidth * Header.PixelFormat.dwRGBBitCount / 8);
  6719. GetMem(RowData, RowSize);
  6720. SourceMD := Converter.CreateMappingData;
  6721. DestMD := FormatDesc.CreateMappingData;
  6722. try
  6723. for y := 0 to Header.dwHeight-1 do begin
  6724. TmpData := NewImage;
  6725. inc(TmpData, y * LineSize);
  6726. SrcData := RowData;
  6727. aStream.Read(SrcData^, RowSize);
  6728. for x := 0 to Header.dwWidth-1 do begin
  6729. Converter.Unmap(SrcData, Pixel, SourceMD);
  6730. glBitmapConvertPixel(Pixel, Converter, FormatDesc);
  6731. FormatDesc.Map(Pixel, TmpData, DestMD);
  6732. end;
  6733. end;
  6734. finally
  6735. Converter.FreeMappingData(SourceMD);
  6736. FormatDesc.FreeMappingData(DestMD);
  6737. FreeMem(RowData);
  6738. end;
  6739. end else
  6740. // Compressed
  6741. if ((Header.PixelFormat.dwFlags and DDPF_FOURCC) > 0) then begin
  6742. RowSize := Header.dwPitchOrLinearSize div Header.dwWidth;
  6743. for Y := 0 to Header.dwHeight-1 do begin
  6744. aStream.Read(TmpData^, RowSize);
  6745. Inc(TmpData, LineSize);
  6746. end;
  6747. end else
  6748. // Uncompressed
  6749. if (Header.PixelFormat.dwFlags and (DDPF_RGB or DDPF_ALPHAPIXELS or DDPF_LUMINANCE)) > 0 then begin
  6750. RowSize := (Header.PixelFormat.dwRGBBitCount * Header.dwWidth) shr 3;
  6751. for Y := 0 to Header.dwHeight-1 do begin
  6752. aStream.Read(TmpData^, RowSize);
  6753. Inc(TmpData, LineSize);
  6754. end;
  6755. end else
  6756. raise EglBitmap.Create('LoadDDS - unsupported Pixelformat found.');
  6757. SetDataPointer(NewImage, ddsFormat, Header.dwWidth, Header.dwHeight); //be careful, Data could be freed by this method
  6758. result := true;
  6759. except
  6760. if Assigned(NewImage) then
  6761. FreeMem(NewImage);
  6762. raise;
  6763. end;
  6764. finally
  6765. FreeAndNil(Converter);
  6766. end;
  6767. end;
  6768. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6769. procedure TglBitmap.SaveDDS(const aStream: TStream);
  6770. var
  6771. Header: TDDSHeader;
  6772. FormatDesc: TFormatDescriptor;
  6773. begin
  6774. if not (ftDDS in FormatGetSupportedFiles(Format)) then
  6775. raise EglBitmapUnsupportedFormat.Create(Format);
  6776. FormatDesc := TFormatDescriptor.Get(Format);
  6777. // Generell
  6778. FillChar(Header{%H-}, SizeOf(Header), 0);
  6779. Header.dwSize := SizeOf(Header);
  6780. Header.dwFlags := DDSD_WIDTH or DDSD_HEIGHT or DDSD_CAPS or DDSD_PIXELFORMAT;
  6781. Header.dwWidth := Max(1, Width);
  6782. Header.dwHeight := Max(1, Height);
  6783. // Caps
  6784. Header.Caps.dwCaps1 := DDSCAPS_TEXTURE;
  6785. // Pixelformat
  6786. Header.PixelFormat.dwSize := sizeof(Header);
  6787. if (FormatDesc.IsCompressed) then begin
  6788. Header.PixelFormat.dwFlags := Header.PixelFormat.dwFlags or DDPF_FOURCC;
  6789. case Format of
  6790. tfS3tcDtx1RGBA: Header.PixelFormat.dwFourCC := D3DFMT_DXT1;
  6791. tfS3tcDtx3RGBA: Header.PixelFormat.dwFourCC := D3DFMT_DXT3;
  6792. tfS3tcDtx5RGBA: Header.PixelFormat.dwFourCC := D3DFMT_DXT5;
  6793. end;
  6794. end else if (Format in [tfAlpha8, tfAlpha16]) then begin
  6795. Header.PixelFormat.dwFlags := Header.PixelFormat.dwFlags or DDPF_ALPHA;
  6796. Header.PixelFormat.dwRGBBitCount := Round(FormatDesc.PixelSize * 8);
  6797. Header.PixelFormat.dwABitMask := FormatDesc.AlphaMask;
  6798. end else if (FormatDesc.RedMask = FormatDesc.GreenMask) and (FormatDesc.GreenMask = FormatDesc.BlueMask) then begin
  6799. Header.PixelFormat.dwFlags := Header.PixelFormat.dwFlags or DDPF_LUMINANCE;
  6800. Header.PixelFormat.dwRGBBitCount := Round(FormatDesc.PixelSize * 8);
  6801. Header.PixelFormat.dwRBitMask := FormatDesc.RedMask;
  6802. Header.PixelFormat.dwABitMask := FormatDesc.AlphaMask;
  6803. end else begin
  6804. Header.PixelFormat.dwFlags := Header.PixelFormat.dwFlags or DDPF_RGB;
  6805. Header.PixelFormat.dwRGBBitCount := Round(FormatDesc.PixelSize * 8);
  6806. Header.PixelFormat.dwRBitMask := FormatDesc.RedMask;
  6807. Header.PixelFormat.dwGBitMask := FormatDesc.GreenMask;
  6808. Header.PixelFormat.dwBBitMask := FormatDesc.BlueMask;
  6809. Header.PixelFormat.dwABitMask := FormatDesc.AlphaMask;
  6810. end;
  6811. if (FormatDesc.HasAlpha) then
  6812. Header.PixelFormat.dwFlags := Header.PixelFormat.dwFlags or DDPF_ALPHAPIXELS;
  6813. aStream.Write(DDS_MAGIC, sizeof(DDS_MAGIC));
  6814. aStream.Write(Header, SizeOf(Header));
  6815. aStream.Write(Data^, FormatDesc.GetSize(Dimension));
  6816. end;
  6817. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6818. //TglBitmap1D/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6819. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6820. procedure TglBitmap1D.SetDataPointer(var aData: PByte; const aFormat: TglBitmapFormat;
  6821. const aWidth: Integer; const aHeight: Integer);
  6822. var
  6823. pTemp: pByte;
  6824. Size: Integer;
  6825. begin
  6826. if (aHeight > 1) then begin
  6827. Size := TFormatDescriptor.Get(aFormat).GetSize(aWidth, 1);
  6828. GetMem(pTemp, Size);
  6829. try
  6830. Move(aData^, pTemp^, Size);
  6831. FreeMem(aData);
  6832. aData := nil;
  6833. except
  6834. FreeMem(pTemp);
  6835. raise;
  6836. end;
  6837. end else
  6838. pTemp := aData;
  6839. inherited SetDataPointer(pTemp, aFormat, aWidth);
  6840. end;
  6841. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6842. function TglBitmap1D.FlipHorz: Boolean;
  6843. var
  6844. Col: Integer;
  6845. pTempDest, pDest, pSource: PByte;
  6846. begin
  6847. result := inherited FlipHorz;
  6848. if Assigned(Data) and not TFormatDescriptor.Get(Format).IsCompressed then begin
  6849. pSource := Data;
  6850. GetMem(pDest, fRowSize);
  6851. try
  6852. pTempDest := pDest;
  6853. Inc(pTempDest, fRowSize);
  6854. for Col := 0 to Width-1 do begin
  6855. dec(pTempDest, fPixelSize); //dec before, because ptr is behind last byte of data
  6856. Move(pSource^, pTempDest^, fPixelSize);
  6857. Inc(pSource, fPixelSize);
  6858. end;
  6859. SetDataPointer(pDest, Format, Width); //be careful, Data could be freed by this method
  6860. result := true;
  6861. except
  6862. if Assigned(pDest) then
  6863. FreeMem(pDest);
  6864. raise;
  6865. end;
  6866. end;
  6867. end;
  6868. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6869. procedure TglBitmap1D.UploadData(const aBuildWithGlu: Boolean);
  6870. var
  6871. FormatDesc: TFormatDescriptor;
  6872. begin
  6873. // Upload data
  6874. FormatDesc := TFormatDescriptor.Get(Format);
  6875. if FormatDesc.IsCompressed then begin
  6876. if not Assigned(glCompressedTexImage1D) then
  6877. raise EglBitmap.Create('compressed formats not supported by video adapter');
  6878. glCompressedTexImage1D(Target, 0, FormatDesc.glInternalFormat, Width, 0, FormatDesc.GetSize(Width, 1), Data)
  6879. end else if aBuildWithGlu then
  6880. gluBuild1DMipmaps(Target, FormatDesc.glInternalFormat, Width, FormatDesc.glFormat, FormatDesc.glDataFormat, Data)
  6881. else
  6882. glTexImage1D(Target, 0, FormatDesc.glInternalFormat, Width, 0, FormatDesc.glFormat, FormatDesc.glDataFormat, Data);
  6883. // Free Data
  6884. if (FreeDataAfterGenTexture) then
  6885. FreeData;
  6886. end;
  6887. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6888. procedure TglBitmap1D.GenTexture(const aTestTextureSize: Boolean);
  6889. var
  6890. BuildWithGlu, TexRec: Boolean;
  6891. TexSize: Integer;
  6892. begin
  6893. if Assigned(Data) then begin
  6894. // Check Texture Size
  6895. if (aTestTextureSize) then begin
  6896. glGetIntegerv(GL_MAX_TEXTURE_SIZE, @TexSize);
  6897. if (Width > TexSize) then
  6898. raise EglBitmapSizeToLarge.Create('TglBitmap1D.GenTexture - The size for the texture is to large. It''s may be not conform with the Hardware.');
  6899. TexRec := (GL_ARB_texture_rectangle or GL_EXT_texture_rectangle or GL_NV_texture_rectangle) and
  6900. (Target = GL_TEXTURE_RECTANGLE);
  6901. if not (IsPowerOfTwo(Width) or GL_ARB_texture_non_power_of_two or GL_VERSION_2_0 or TexRec) then
  6902. raise EglBitmapNonPowerOfTwo.Create('TglBitmap1D.GenTexture - Rendercontex dosn''t support non power of two texture.');
  6903. end;
  6904. CreateId;
  6905. SetupParameters(BuildWithGlu);
  6906. UploadData(BuildWithGlu);
  6907. glAreTexturesResident(1, @fID, @fIsResident);
  6908. end;
  6909. end;
  6910. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6911. procedure TglBitmap1D.AfterConstruction;
  6912. begin
  6913. inherited;
  6914. Target := GL_TEXTURE_1D;
  6915. end;
  6916. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6917. //TglBitmap2D/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6918. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6919. function TglBitmap2D.GetScanline(const aIndex: Integer): Pointer;
  6920. begin
  6921. if (aIndex >= Low(fLines)) and (aIndex <= High(fLines)) then
  6922. result := fLines[aIndex]
  6923. else
  6924. result := nil;
  6925. end;
  6926. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6927. procedure TglBitmap2D.SetDataPointer(var aData: PByte; const aFormat: TglBitmapFormat;
  6928. const aWidth: Integer; const aHeight: Integer);
  6929. var
  6930. Idx, LineWidth: Integer;
  6931. begin
  6932. inherited SetDataPointer(aData, aFormat, aWidth, aHeight);
  6933. if not TFormatDescriptor.Get(aFormat).IsCompressed then begin
  6934. // Assigning Data
  6935. if Assigned(Data) then begin
  6936. SetLength(fLines, GetHeight);
  6937. LineWidth := Trunc(GetWidth * TFormatDescriptor.Get(Format).PixelSize);
  6938. for Idx := 0 to GetHeight-1 do begin
  6939. fLines[Idx] := Data;
  6940. Inc(fLines[Idx], Idx * LineWidth);
  6941. end;
  6942. end
  6943. else SetLength(fLines, 0);
  6944. end else begin
  6945. SetLength(fLines, 0);
  6946. end;
  6947. end;
  6948. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6949. procedure TglBitmap2D.UploadData(const aTarget: GLenum; const aBuildWithGlu: Boolean);
  6950. var
  6951. FormatDesc: TFormatDescriptor;
  6952. begin
  6953. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  6954. FormatDesc := TFormatDescriptor.Get(Format);
  6955. if FormatDesc.IsCompressed then begin
  6956. if not Assigned(glCompressedTexImage2D) then
  6957. raise EglBitmap.Create('compressed formats not supported by video adapter');
  6958. glCompressedTexImage2D(aTarget, 0, FormatDesc.glInternalFormat, Width, Height, 0, FormatDesc.GetSize(fDimension), Data)
  6959. end else if aBuildWithGlu then begin
  6960. gluBuild2DMipmaps(aTarget, FormatDesc.Components, Width, Height,
  6961. FormatDesc.glFormat, FormatDesc.glDataFormat, Data)
  6962. end else begin
  6963. glTexImage2D(aTarget, 0, FormatDesc.glInternalFormat, Width, Height, 0,
  6964. FormatDesc.glFormat, FormatDesc.glDataFormat, Data);
  6965. end;
  6966. // Freigeben
  6967. if (FreeDataAfterGenTexture) then
  6968. FreeData;
  6969. end;
  6970. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6971. procedure TglBitmap2D.AfterConstruction;
  6972. begin
  6973. inherited;
  6974. Target := GL_TEXTURE_2D;
  6975. end;
  6976. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6977. procedure TglBitmap2D.GrabScreen(const aTop, aLeft, aRight, aBottom: Integer; const aFormat: TglBitmapFormat);
  6978. var
  6979. Temp: pByte;
  6980. Size, w, h: Integer;
  6981. FormatDesc: TFormatDescriptor;
  6982. begin
  6983. FormatDesc := TFormatDescriptor.Get(aFormat);
  6984. if FormatDesc.IsCompressed then
  6985. raise EglBitmapUnsupportedFormat.Create(aFormat);
  6986. w := aRight - aLeft;
  6987. h := aBottom - aTop;
  6988. Size := FormatDesc.GetSize(w, h);
  6989. GetMem(Temp, Size);
  6990. try
  6991. glPixelStorei(GL_PACK_ALIGNMENT, 1);
  6992. glReadPixels(aLeft, aTop, w, h, FormatDesc.glFormat, FormatDesc.glDataFormat, Temp);
  6993. SetDataPointer(Temp, aFormat, w, h); //be careful, Data could be freed by this method
  6994. FlipVert;
  6995. except
  6996. if Assigned(Temp) then
  6997. FreeMem(Temp);
  6998. raise;
  6999. end;
  7000. end;
  7001. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7002. procedure TglBitmap2D.GetDataFromTexture;
  7003. var
  7004. Temp: PByte;
  7005. TempWidth, TempHeight: Integer;
  7006. TempIntFormat: Cardinal;
  7007. IntFormat, f: TglBitmapFormat;
  7008. FormatDesc: TFormatDescriptor;
  7009. begin
  7010. Bind;
  7011. // Request Data
  7012. glGetTexLevelParameteriv(Target, 0, GL_TEXTURE_WIDTH, @TempWidth);
  7013. glGetTexLevelParameteriv(Target, 0, GL_TEXTURE_HEIGHT, @TempHeight);
  7014. glGetTexLevelParameteriv(Target, 0, GL_TEXTURE_INTERNAL_FORMAT, @TempIntFormat);
  7015. IntFormat := tfEmpty;
  7016. for f := Low(TglBitmapFormat) to High(TglBitmapFormat) do begin
  7017. FormatDesc := TFormatDescriptor.Get(f);
  7018. if (FormatDesc.glInternalFormat = TempIntFormat) then begin
  7019. IntFormat := FormatDesc.Format;
  7020. break;
  7021. end;
  7022. end;
  7023. // Getting data from OpenGL
  7024. FormatDesc := TFormatDescriptor.Get(IntFormat);
  7025. GetMem(Temp, FormatDesc.GetSize(TempWidth, TempHeight));
  7026. try
  7027. if FormatDesc.IsCompressed then begin
  7028. if not Assigned(glGetCompressedTexImage) then
  7029. raise EglBitmap.Create('compressed formats not supported by video adapter');
  7030. glGetCompressedTexImage(Target, 0, Temp)
  7031. end else
  7032. glGetTexImage(Target, 0, FormatDesc.glFormat, FormatDesc.glDataFormat, Temp);
  7033. SetDataPointer(Temp, IntFormat, TempWidth, TempHeight); //be careful, Data could be freed by this method
  7034. except
  7035. if Assigned(Temp) then
  7036. FreeMem(Temp);
  7037. raise;
  7038. end;
  7039. end;
  7040. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7041. procedure TglBitmap2D.GenTexture(const aTestTextureSize: Boolean);
  7042. var
  7043. BuildWithGlu, PotTex, TexRec: Boolean;
  7044. TexSize: Integer;
  7045. begin
  7046. if Assigned(Data) then begin
  7047. // Check Texture Size
  7048. if (aTestTextureSize) then begin
  7049. glGetIntegerv(GL_MAX_TEXTURE_SIZE, @TexSize);
  7050. if ((Height > TexSize) or (Width > TexSize)) then
  7051. raise EglBitmapSizeToLarge.Create('TglBitmap2D.GenTexture - The size for the texture is to large. It''s may be not conform with the Hardware.');
  7052. PotTex := IsPowerOfTwo(Height) and IsPowerOfTwo(Width);
  7053. TexRec := (GL_ARB_texture_rectangle or GL_EXT_texture_rectangle or GL_NV_texture_rectangle) and (Target = GL_TEXTURE_RECTANGLE);
  7054. if not (PotTex or GL_ARB_texture_non_power_of_two or GL_VERSION_2_0 or TexRec) then
  7055. raise EglBitmapNonPowerOfTwo.Create('TglBitmap2D.GenTexture - Rendercontex dosn''t support non power of two texture.');
  7056. end;
  7057. CreateId;
  7058. SetupParameters(BuildWithGlu);
  7059. UploadData(Target, BuildWithGlu);
  7060. glAreTexturesResident(1, @fID, @fIsResident);
  7061. end;
  7062. end;
  7063. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7064. function TglBitmap2D.FlipHorz: Boolean;
  7065. var
  7066. Col, Row: Integer;
  7067. TempDestData, DestData, SourceData: PByte;
  7068. ImgSize: Integer;
  7069. begin
  7070. result := inherited FlipHorz;
  7071. if Assigned(Data) then begin
  7072. SourceData := Data;
  7073. ImgSize := Height * fRowSize;
  7074. GetMem(DestData, ImgSize);
  7075. try
  7076. TempDestData := DestData;
  7077. Dec(TempDestData, fRowSize + fPixelSize);
  7078. for Row := 0 to Height -1 do begin
  7079. Inc(TempDestData, fRowSize * 2);
  7080. for Col := 0 to Width -1 do begin
  7081. Move(SourceData^, TempDestData^, fPixelSize);
  7082. Inc(SourceData, fPixelSize);
  7083. Dec(TempDestData, fPixelSize);
  7084. end;
  7085. end;
  7086. SetDataPointer(DestData, Format); //be careful, Data could be freed by this method
  7087. result := true;
  7088. except
  7089. if Assigned(DestData) then
  7090. FreeMem(DestData);
  7091. raise;
  7092. end;
  7093. end;
  7094. end;
  7095. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7096. function TglBitmap2D.FlipVert: Boolean;
  7097. var
  7098. Row: Integer;
  7099. TempDestData, DestData, SourceData: PByte;
  7100. begin
  7101. result := inherited FlipVert;
  7102. if Assigned(Data) then begin
  7103. SourceData := Data;
  7104. GetMem(DestData, Height * fRowSize);
  7105. try
  7106. TempDestData := DestData;
  7107. Inc(TempDestData, Width * (Height -1) * fPixelSize);
  7108. for Row := 0 to Height -1 do begin
  7109. Move(SourceData^, TempDestData^, fRowSize);
  7110. Dec(TempDestData, fRowSize);
  7111. Inc(SourceData, fRowSize);
  7112. end;
  7113. SetDataPointer(DestData, Format); //be careful, Data could be freed by this method
  7114. result := true;
  7115. except
  7116. if Assigned(DestData) then
  7117. FreeMem(DestData);
  7118. raise;
  7119. end;
  7120. end;
  7121. end;
  7122. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7123. //TglBitmap2D - ToNormalMap///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7124. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7125. type
  7126. TMatrixItem = record
  7127. X, Y: Integer;
  7128. W: Single;
  7129. end;
  7130. PglBitmapToNormalMapRec = ^TglBitmapToNormalMapRec;
  7131. TglBitmapToNormalMapRec = Record
  7132. Scale: Single;
  7133. Heights: array of Single;
  7134. MatrixU : array of TMatrixItem;
  7135. MatrixV : array of TMatrixItem;
  7136. end;
  7137. const
  7138. ONE_OVER_255 = 1 / 255;
  7139. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7140. procedure glBitmapToNormalMapPrepareFunc(var FuncRec: TglBitmapFunctionRec);
  7141. var
  7142. Val: Single;
  7143. begin
  7144. with FuncRec do begin
  7145. Val :=
  7146. Source.Data.r * LUMINANCE_WEIGHT_R +
  7147. Source.Data.g * LUMINANCE_WEIGHT_G +
  7148. Source.Data.b * LUMINANCE_WEIGHT_B;
  7149. PglBitmapToNormalMapRec(Args)^.Heights[Position.Y * Size.X + Position.X] := Val * ONE_OVER_255;
  7150. end;
  7151. end;
  7152. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7153. procedure glBitmapToNormalMapPrepareAlphaFunc(var FuncRec: TglBitmapFunctionRec);
  7154. begin
  7155. with FuncRec do
  7156. PglBitmapToNormalMapRec(Args)^.Heights[Position.Y * Size.X + Position.X] := Source.Data.a * ONE_OVER_255;
  7157. end;
  7158. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7159. procedure glBitmapToNormalMapFunc (var FuncRec: TglBitmapFunctionRec);
  7160. type
  7161. TVec = Array[0..2] of Single;
  7162. var
  7163. Idx: Integer;
  7164. du, dv: Double;
  7165. Len: Single;
  7166. Vec: TVec;
  7167. function GetHeight(X, Y: Integer): Single;
  7168. begin
  7169. with FuncRec do begin
  7170. X := Max(0, Min(Size.X -1, X));
  7171. Y := Max(0, Min(Size.Y -1, Y));
  7172. result := PglBitmapToNormalMapRec(Args)^.Heights[Y * Size.X + X];
  7173. end;
  7174. end;
  7175. begin
  7176. with FuncRec do begin
  7177. with PglBitmapToNormalMapRec(Args)^ do begin
  7178. du := 0;
  7179. for Idx := Low(MatrixU) to High(MatrixU) do
  7180. du := du + GetHeight(Position.X + MatrixU[Idx].X, Position.Y + MatrixU[Idx].Y) * MatrixU[Idx].W;
  7181. dv := 0;
  7182. for Idx := Low(MatrixU) to High(MatrixU) do
  7183. dv := dv + GetHeight(Position.X + MatrixV[Idx].X, Position.Y + MatrixV[Idx].Y) * MatrixV[Idx].W;
  7184. Vec[0] := -du * Scale;
  7185. Vec[1] := -dv * Scale;
  7186. Vec[2] := 1;
  7187. end;
  7188. // Normalize
  7189. Len := 1 / Sqrt(Sqr(Vec[0]) + Sqr(Vec[1]) + Sqr(Vec[2]));
  7190. if Len <> 0 then begin
  7191. Vec[0] := Vec[0] * Len;
  7192. Vec[1] := Vec[1] * Len;
  7193. Vec[2] := Vec[2] * Len;
  7194. end;
  7195. // Farbe zuweisem
  7196. Dest.Data.r := Trunc((Vec[0] + 1) * 127.5);
  7197. Dest.Data.g := Trunc((Vec[1] + 1) * 127.5);
  7198. Dest.Data.b := Trunc((Vec[2] + 1) * 127.5);
  7199. end;
  7200. end;
  7201. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7202. procedure TglBitmap2D.ToNormalMap(const aFunc: TglBitmapNormalMapFunc; const aScale: Single; const aUseAlpha: Boolean);
  7203. var
  7204. Rec: TglBitmapToNormalMapRec;
  7205. procedure SetEntry (var Matrix: array of TMatrixItem; Index, X, Y: Integer; W: Single);
  7206. begin
  7207. if (Index >= Low(Matrix)) and (Index <= High(Matrix)) then begin
  7208. Matrix[Index].X := X;
  7209. Matrix[Index].Y := Y;
  7210. Matrix[Index].W := W;
  7211. end;
  7212. end;
  7213. begin
  7214. if TFormatDescriptor.Get(Format).IsCompressed then
  7215. raise EglBitmapUnsupportedFormat.Create(Format);
  7216. if aScale > 100 then
  7217. Rec.Scale := 100
  7218. else if aScale < -100 then
  7219. Rec.Scale := -100
  7220. else
  7221. Rec.Scale := aScale;
  7222. SetLength(Rec.Heights, Width * Height);
  7223. try
  7224. case aFunc of
  7225. nm4Samples: begin
  7226. SetLength(Rec.MatrixU, 2);
  7227. SetEntry(Rec.MatrixU, 0, -1, 0, -0.5);
  7228. SetEntry(Rec.MatrixU, 1, 1, 0, 0.5);
  7229. SetLength(Rec.MatrixV, 2);
  7230. SetEntry(Rec.MatrixV, 0, 0, 1, 0.5);
  7231. SetEntry(Rec.MatrixV, 1, 0, -1, -0.5);
  7232. end;
  7233. nmSobel: begin
  7234. SetLength(Rec.MatrixU, 6);
  7235. SetEntry(Rec.MatrixU, 0, -1, 1, -1.0);
  7236. SetEntry(Rec.MatrixU, 1, -1, 0, -2.0);
  7237. SetEntry(Rec.MatrixU, 2, -1, -1, -1.0);
  7238. SetEntry(Rec.MatrixU, 3, 1, 1, 1.0);
  7239. SetEntry(Rec.MatrixU, 4, 1, 0, 2.0);
  7240. SetEntry(Rec.MatrixU, 5, 1, -1, 1.0);
  7241. SetLength(Rec.MatrixV, 6);
  7242. SetEntry(Rec.MatrixV, 0, -1, 1, 1.0);
  7243. SetEntry(Rec.MatrixV, 1, 0, 1, 2.0);
  7244. SetEntry(Rec.MatrixV, 2, 1, 1, 1.0);
  7245. SetEntry(Rec.MatrixV, 3, -1, -1, -1.0);
  7246. SetEntry(Rec.MatrixV, 4, 0, -1, -2.0);
  7247. SetEntry(Rec.MatrixV, 5, 1, -1, -1.0);
  7248. end;
  7249. nm3x3: begin
  7250. SetLength(Rec.MatrixU, 6);
  7251. SetEntry(Rec.MatrixU, 0, -1, 1, -1/6);
  7252. SetEntry(Rec.MatrixU, 1, -1, 0, -1/6);
  7253. SetEntry(Rec.MatrixU, 2, -1, -1, -1/6);
  7254. SetEntry(Rec.MatrixU, 3, 1, 1, 1/6);
  7255. SetEntry(Rec.MatrixU, 4, 1, 0, 1/6);
  7256. SetEntry(Rec.MatrixU, 5, 1, -1, 1/6);
  7257. SetLength(Rec.MatrixV, 6);
  7258. SetEntry(Rec.MatrixV, 0, -1, 1, 1/6);
  7259. SetEntry(Rec.MatrixV, 1, 0, 1, 1/6);
  7260. SetEntry(Rec.MatrixV, 2, 1, 1, 1/6);
  7261. SetEntry(Rec.MatrixV, 3, -1, -1, -1/6);
  7262. SetEntry(Rec.MatrixV, 4, 0, -1, -1/6);
  7263. SetEntry(Rec.MatrixV, 5, 1, -1, -1/6);
  7264. end;
  7265. nm5x5: begin
  7266. SetLength(Rec.MatrixU, 20);
  7267. SetEntry(Rec.MatrixU, 0, -2, 2, -1 / 16);
  7268. SetEntry(Rec.MatrixU, 1, -1, 2, -1 / 10);
  7269. SetEntry(Rec.MatrixU, 2, 1, 2, 1 / 10);
  7270. SetEntry(Rec.MatrixU, 3, 2, 2, 1 / 16);
  7271. SetEntry(Rec.MatrixU, 4, -2, 1, -1 / 10);
  7272. SetEntry(Rec.MatrixU, 5, -1, 1, -1 / 8);
  7273. SetEntry(Rec.MatrixU, 6, 1, 1, 1 / 8);
  7274. SetEntry(Rec.MatrixU, 7, 2, 1, 1 / 10);
  7275. SetEntry(Rec.MatrixU, 8, -2, 0, -1 / 2.8);
  7276. SetEntry(Rec.MatrixU, 9, -1, 0, -0.5);
  7277. SetEntry(Rec.MatrixU, 10, 1, 0, 0.5);
  7278. SetEntry(Rec.MatrixU, 11, 2, 0, 1 / 2.8);
  7279. SetEntry(Rec.MatrixU, 12, -2, -1, -1 / 10);
  7280. SetEntry(Rec.MatrixU, 13, -1, -1, -1 / 8);
  7281. SetEntry(Rec.MatrixU, 14, 1, -1, 1 / 8);
  7282. SetEntry(Rec.MatrixU, 15, 2, -1, 1 / 10);
  7283. SetEntry(Rec.MatrixU, 16, -2, -2, -1 / 16);
  7284. SetEntry(Rec.MatrixU, 17, -1, -2, -1 / 10);
  7285. SetEntry(Rec.MatrixU, 18, 1, -2, 1 / 10);
  7286. SetEntry(Rec.MatrixU, 19, 2, -2, 1 / 16);
  7287. SetLength(Rec.MatrixV, 20);
  7288. SetEntry(Rec.MatrixV, 0, -2, 2, 1 / 16);
  7289. SetEntry(Rec.MatrixV, 1, -1, 2, 1 / 10);
  7290. SetEntry(Rec.MatrixV, 2, 0, 2, 0.25);
  7291. SetEntry(Rec.MatrixV, 3, 1, 2, 1 / 10);
  7292. SetEntry(Rec.MatrixV, 4, 2, 2, 1 / 16);
  7293. SetEntry(Rec.MatrixV, 5, -2, 1, 1 / 10);
  7294. SetEntry(Rec.MatrixV, 6, -1, 1, 1 / 8);
  7295. SetEntry(Rec.MatrixV, 7, 0, 1, 0.5);
  7296. SetEntry(Rec.MatrixV, 8, 1, 1, 1 / 8);
  7297. SetEntry(Rec.MatrixV, 9, 2, 1, 1 / 16);
  7298. SetEntry(Rec.MatrixV, 10, -2, -1, -1 / 16);
  7299. SetEntry(Rec.MatrixV, 11, -1, -1, -1 / 8);
  7300. SetEntry(Rec.MatrixV, 12, 0, -1, -0.5);
  7301. SetEntry(Rec.MatrixV, 13, 1, -1, -1 / 8);
  7302. SetEntry(Rec.MatrixV, 14, 2, -1, -1 / 10);
  7303. SetEntry(Rec.MatrixV, 15, -2, -2, -1 / 16);
  7304. SetEntry(Rec.MatrixV, 16, -1, -2, -1 / 10);
  7305. SetEntry(Rec.MatrixV, 17, 0, -2, -0.25);
  7306. SetEntry(Rec.MatrixV, 18, 1, -2, -1 / 10);
  7307. SetEntry(Rec.MatrixV, 19, 2, -2, -1 / 16);
  7308. end;
  7309. end;
  7310. // Daten Sammeln
  7311. if aUseAlpha and TFormatDescriptor.Get(Format).HasAlpha then
  7312. AddFunc(glBitmapToNormalMapPrepareAlphaFunc, false, @Rec)
  7313. else
  7314. AddFunc(glBitmapToNormalMapPrepareFunc, false, @Rec);
  7315. AddFunc(glBitmapToNormalMapFunc, false, @Rec);
  7316. finally
  7317. SetLength(Rec.Heights, 0);
  7318. end;
  7319. end;
  7320. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7321. //TglBitmapCubeMap////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7322. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7323. procedure TglBitmapCubeMap.GenTexture(const aTestTextureSize: Boolean);
  7324. begin
  7325. Assert(false, 'TglBitmapCubeMap.GenTexture - Don''t call GenTextures directly.');
  7326. end;
  7327. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7328. procedure TglBitmapCubeMap.AfterConstruction;
  7329. begin
  7330. inherited;
  7331. if not (GL_VERSION_1_3 or GL_ARB_texture_cube_map or GL_EXT_texture_cube_map) then
  7332. raise EglBitmap.Create('TglBitmapCubeMap.AfterConstruction - CubeMaps are unsupported.');
  7333. SetWrap;
  7334. Target := GL_TEXTURE_CUBE_MAP;
  7335. fGenMode := GL_REFLECTION_MAP;
  7336. end;
  7337. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7338. procedure TglBitmapCubeMap.GenerateCubeMap(const aCubeTarget: Cardinal; const aTestTextureSize: Boolean);
  7339. var
  7340. BuildWithGlu: Boolean;
  7341. TexSize: Integer;
  7342. begin
  7343. if (aTestTextureSize) then begin
  7344. glGetIntegerv(GL_MAX_CUBE_MAP_TEXTURE_SIZE, @TexSize);
  7345. if (Height > TexSize) or (Width > TexSize) then
  7346. raise EglBitmapSizeToLarge.Create('TglBitmapCubeMap.GenTexture - The size for the Cubemap is to large. It''s may be not conform with the Hardware.');
  7347. if not ((IsPowerOfTwo(Height) and IsPowerOfTwo(Width)) or GL_VERSION_2_0 or GL_ARB_texture_non_power_of_two) then
  7348. raise EglBitmapNonPowerOfTwo.Create('TglBitmapCubeMap.GenTexture - Cubemaps dosn''t support non power of two texture.');
  7349. end;
  7350. if (ID = 0) then
  7351. CreateID;
  7352. SetupParameters(BuildWithGlu);
  7353. UploadData(aCubeTarget, BuildWithGlu);
  7354. end;
  7355. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7356. procedure TglBitmapCubeMap.Bind(const aEnableTexCoordsGen: Boolean; const aEnableTextureUnit: Boolean);
  7357. begin
  7358. inherited Bind (aEnableTextureUnit);
  7359. if aEnableTexCoordsGen then begin
  7360. glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, fGenMode);
  7361. glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, fGenMode);
  7362. glTexGeni(GL_R, GL_TEXTURE_GEN_MODE, fGenMode);
  7363. glEnable(GL_TEXTURE_GEN_S);
  7364. glEnable(GL_TEXTURE_GEN_T);
  7365. glEnable(GL_TEXTURE_GEN_R);
  7366. end;
  7367. end;
  7368. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7369. procedure TglBitmapCubeMap.Unbind(const aDisableTexCoordsGen: Boolean; const aDisableTextureUnit: Boolean);
  7370. begin
  7371. inherited Unbind(aDisableTextureUnit);
  7372. if aDisableTexCoordsGen then begin
  7373. glDisable(GL_TEXTURE_GEN_S);
  7374. glDisable(GL_TEXTURE_GEN_T);
  7375. glDisable(GL_TEXTURE_GEN_R);
  7376. end;
  7377. end;
  7378. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7379. //TglBitmapNormalMap//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7380. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7381. type
  7382. TVec = Array[0..2] of Single;
  7383. TglBitmapNormalMapGetVectorFunc = procedure (out aVec: TVec; const aPosition: TglBitmapPixelPosition; const aHalfSize: Integer);
  7384. PglBitmapNormalMapRec = ^TglBitmapNormalMapRec;
  7385. TglBitmapNormalMapRec = record
  7386. HalfSize : Integer;
  7387. Func: TglBitmapNormalMapGetVectorFunc;
  7388. end;
  7389. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7390. procedure glBitmapNormalMapPosX(out aVec: TVec; const aPosition: TglBitmapPixelPosition; const aHalfSize: Integer);
  7391. begin
  7392. aVec[0] := aHalfSize;
  7393. aVec[1] := - (aPosition.Y + 0.5 - aHalfSize);
  7394. aVec[2] := - (aPosition.X + 0.5 - aHalfSize);
  7395. end;
  7396. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7397. procedure glBitmapNormalMapNegX(out aVec: TVec; const aPosition: TglBitmapPixelPosition; const aHalfSize: Integer);
  7398. begin
  7399. aVec[0] := - aHalfSize;
  7400. aVec[1] := - (aPosition.Y + 0.5 - aHalfSize);
  7401. aVec[2] := aPosition.X + 0.5 - aHalfSize;
  7402. end;
  7403. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7404. procedure glBitmapNormalMapPosY(out aVec: TVec; const aPosition: TglBitmapPixelPosition; const aHalfSize: Integer);
  7405. begin
  7406. aVec[0] := aPosition.X + 0.5 - aHalfSize;
  7407. aVec[1] := aHalfSize;
  7408. aVec[2] := aPosition.Y + 0.5 - aHalfSize;
  7409. end;
  7410. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7411. procedure glBitmapNormalMapNegY(out aVec: TVec; const aPosition: TglBitmapPixelPosition; const aHalfSize: Integer);
  7412. begin
  7413. aVec[0] := aPosition.X + 0.5 - aHalfSize;
  7414. aVec[1] := - aHalfSize;
  7415. aVec[2] := - (aPosition.Y + 0.5 - aHalfSize);
  7416. end;
  7417. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7418. procedure glBitmapNormalMapPosZ(out aVec: TVec; const aPosition: TglBitmapPixelPosition; const aHalfSize: Integer);
  7419. begin
  7420. aVec[0] := aPosition.X + 0.5 - aHalfSize;
  7421. aVec[1] := - (aPosition.Y + 0.5 - aHalfSize);
  7422. aVec[2] := aHalfSize;
  7423. end;
  7424. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7425. procedure glBitmapNormalMapNegZ(out aVec: TVec; const aPosition: TglBitmapPixelPosition; const aHalfSize: Integer);
  7426. begin
  7427. aVec[0] := - (aPosition.X + 0.5 - aHalfSize);
  7428. aVec[1] := - (aPosition.Y + 0.5 - aHalfSize);
  7429. aVec[2] := - aHalfSize;
  7430. end;
  7431. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7432. procedure glBitmapNormalMapFunc(var FuncRec: TglBitmapFunctionRec);
  7433. var
  7434. i: Integer;
  7435. Vec: TVec;
  7436. Len: Single;
  7437. begin
  7438. with FuncRec do begin
  7439. with PglBitmapNormalMapRec(Args)^ do begin
  7440. Func(Vec, Position, HalfSize);
  7441. // Normalize
  7442. Len := 1 / Sqrt(Sqr(Vec[0]) + Sqr(Vec[1]) + Sqr(Vec[2]));
  7443. if Len <> 0 then begin
  7444. Vec[0] := Vec[0] * Len;
  7445. Vec[1] := Vec[1] * Len;
  7446. Vec[2] := Vec[2] * Len;
  7447. end;
  7448. // Scale Vector and AddVectro
  7449. Vec[0] := Vec[0] * 0.5 + 0.5;
  7450. Vec[1] := Vec[1] * 0.5 + 0.5;
  7451. Vec[2] := Vec[2] * 0.5 + 0.5;
  7452. end;
  7453. // Set Color
  7454. for i := 0 to 2 do
  7455. Dest.Data.arr[i] := Round(Vec[i] * 255);
  7456. end;
  7457. end;
  7458. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7459. procedure TglBitmapNormalMap.AfterConstruction;
  7460. begin
  7461. inherited;
  7462. fGenMode := GL_NORMAL_MAP;
  7463. end;
  7464. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7465. procedure TglBitmapNormalMap.GenerateNormalMap(const aSize: Integer; const aTestTextureSize: Boolean);
  7466. var
  7467. Rec: TglBitmapNormalMapRec;
  7468. SizeRec: TglBitmapPixelPosition;
  7469. begin
  7470. Rec.HalfSize := aSize div 2;
  7471. FreeDataAfterGenTexture := false;
  7472. SizeRec.Fields := [ffX, ffY];
  7473. SizeRec.X := aSize;
  7474. SizeRec.Y := aSize;
  7475. // Positive X
  7476. Rec.Func := glBitmapNormalMapPosX;
  7477. LoadFromFunc(SizeRec, glBitmapNormalMapFunc, tfBGR8, @Rec);
  7478. GenerateCubeMap(GL_TEXTURE_CUBE_MAP_POSITIVE_X, aTestTextureSize);
  7479. // Negative X
  7480. Rec.Func := glBitmapNormalMapNegX;
  7481. LoadFromFunc(SizeRec, glBitmapNormalMapFunc, tfBGR8, @Rec);
  7482. GenerateCubeMap(GL_TEXTURE_CUBE_MAP_NEGATIVE_X, aTestTextureSize);
  7483. // Positive Y
  7484. Rec.Func := glBitmapNormalMapPosY;
  7485. LoadFromFunc(SizeRec, glBitmapNormalMapFunc, tfBGR8, @Rec);
  7486. GenerateCubeMap(GL_TEXTURE_CUBE_MAP_POSITIVE_Y, aTestTextureSize);
  7487. // Negative Y
  7488. Rec.Func := glBitmapNormalMapNegY;
  7489. LoadFromFunc(SizeRec, glBitmapNormalMapFunc, tfBGR8, @Rec);
  7490. GenerateCubeMap(GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, aTestTextureSize);
  7491. // Positive Z
  7492. Rec.Func := glBitmapNormalMapPosZ;
  7493. LoadFromFunc(SizeRec, glBitmapNormalMapFunc, tfBGR8, @Rec);
  7494. GenerateCubeMap(GL_TEXTURE_CUBE_MAP_POSITIVE_Z, aTestTextureSize);
  7495. // Negative Z
  7496. Rec.Func := glBitmapNormalMapNegZ;
  7497. LoadFromFunc(SizeRec, glBitmapNormalMapFunc, tfBGR8, @Rec);
  7498. GenerateCubeMap(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, aTestTextureSize);
  7499. end;
  7500. initialization
  7501. glBitmapSetDefaultFormat (tfEmpty);
  7502. glBitmapSetDefaultMipmap (mmMipmap);
  7503. glBitmapSetDefaultFilter (GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR);
  7504. glBitmapSetDefaultWrap (GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
  7505. glBitmapSetDefaultSwizzle(GL_RED, GL_GREEN, GL_BLUE, GL_ALPHA);
  7506. glBitmapSetDefaultFreeDataAfterGenTexture(true);
  7507. glBitmapSetDefaultDeleteTextureOnFree (true);
  7508. TFormatDescriptor.Init;
  7509. {$IFDEF GLB_NATIVE_OGL_DYNAMIC}
  7510. OpenGLInitialized := false;
  7511. InitOpenGLCS := TCriticalSection.Create;
  7512. {$ENDIF}
  7513. finalization
  7514. TFormatDescriptor.Finalize;
  7515. {$IFDEF GLB_NATIVE_OGL}
  7516. if Assigned(GL_LibHandle) then
  7517. glbFreeLibrary(GL_LibHandle);
  7518. {$IFDEF GLB_NATIVE_OGL_DYNAMIC}
  7519. if Assigned(GLU_LibHandle) then
  7520. glbFreeLibrary(GLU_LibHandle);
  7521. FreeAndNil(InitOpenGLCS);
  7522. {$ENDIF}
  7523. {$ENDIF}
  7524. end.