Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

8904 linhas
317 KiB

  1. { glBitmap by Steffen Xonna aka Lossy eX (2003-2008)
  2. http://www.opengl24.de/index.php?cat=header&file=glbitmap
  3. modified by Delphi OpenGL Community (http://delphigl.com/) (2013)
  4. The contents of this file are used with permission, subject to
  5. the Mozilla Public License Version 1.1 (the "License"); you may
  6. not use this file except in compliance with the License. You may
  7. obtain a copy of the License at
  8. http://www.mozilla.org/MPL/MPL-1.1.html
  9. The glBitmap is a Delphi/FPC unit that contains several wrapper classes
  10. to manage OpenGL texture objects. Below you can find a list of the main
  11. functionality of this classes:
  12. - load texture data from file (e.g. BMP, TGA, DDS, PNG, JPEG, ...)
  13. - load texture data from several other image objects (e.g. TBitmap, TLazIntfImage, SDL Surface)
  14. - save texture data to file (e.g. BMP, TGA, DDS, PNG, JPEG, ...)
  15. - save texture data to several other image objects (e.g. TBitmap, TLazIntfImage, SDL Surface)
  16. - support for many texture formats (e.g. RGB8, BGR8, RGBA8, BGRA8, ...)
  17. - manage texture properties (e.g. Filter, Clamp, Mipmap, ...)
  18. - upload texture data to video card
  19. - download texture data from video card
  20. - manipulate texture data (e.g. add alpha, remove alpha, convert to other format, switch RGB, ...) }
  21. unit glBitmap;
  22. {$I glBitmapConf.inc}
  23. // Delphi Versions
  24. {$IFDEF fpc}
  25. {$MODE Delphi}
  26. {$IFDEF CPUI386}
  27. {$DEFINE CPU386}
  28. {$ASMMODE INTEL}
  29. {$ENDIF}
  30. {$IFNDEF WINDOWS}
  31. {$linklib c}
  32. {$ENDIF}
  33. {$ENDIF}
  34. // Operation System
  35. {$IF DEFINED(WIN32) or DEFINED(WIN64) or DEFINED(WINDOWS)}
  36. {$DEFINE GLB_WIN}
  37. {$ELSEIF DEFINED(LINUX)}
  38. {$DEFINE GLB_LINUX}
  39. {$IFEND}
  40. // OpenGL ES
  41. {$IF DEFINED(OPENGL_ES_EXT)} {$DEFINE OPENGL_ES_1_1} {$IFEND}
  42. {$IF DEFINED(OPENGL_ES_3_0)} {$DEFINE OPENGL_ES_2_0} {$IFEND}
  43. {$IF DEFINED(OPENGL_ES_2_0)} {$DEFINE OPENGL_ES_1_1} {$IFEND}
  44. {$IF DEFINED(OPENGL_ES_1_1)} {$DEFINE OPENGL_ES} {$IFEND}
  45. // checking define combinations
  46. //SDL Image
  47. {$IFDEF GLB_SDL_IMAGE}
  48. {$IFNDEF GLB_SDL}
  49. {$MESSAGE warn 'SDL_image won''t work without SDL. SDL will be activated.'}
  50. {$DEFINE GLB_SDL}
  51. {$ENDIF}
  52. {$IFDEF GLB_LAZ_PNG}
  53. {$MESSAGE warn 'The Lazarus TPortableNetworkGraphics will be ignored because you are using SDL_image.'}
  54. {$undef GLB_LAZ_PNG}
  55. {$ENDIF}
  56. {$IFDEF GLB_PNGIMAGE}
  57. {$MESSAGE warn 'The unit pngimage will be ignored because you are using SDL_image.'}
  58. {$undef GLB_PNGIMAGE}
  59. {$ENDIF}
  60. {$IFDEF GLB_LAZ_JPEG}
  61. {$MESSAGE warn 'The Lazarus TJPEGImage will be ignored because you are using SDL_image.'}
  62. {$undef GLB_LAZ_JPEG}
  63. {$ENDIF}
  64. {$IFDEF GLB_DELPHI_JPEG}
  65. {$MESSAGE warn 'The unit JPEG will be ignored because you are using SDL_image.'}
  66. {$undef GLB_DELPHI_JPEG}
  67. {$ENDIF}
  68. {$IFDEF GLB_LIB_PNG}
  69. {$MESSAGE warn 'The library libPNG will be ignored because you are using SDL_image.'}
  70. {$undef GLB_LIB_PNG}
  71. {$ENDIF}
  72. {$IFDEF GLB_LIB_JPEG}
  73. {$MESSAGE warn 'The library libJPEG will be ignored because you are using SDL_image.'}
  74. {$undef GLB_LIB_JPEG}
  75. {$ENDIF}
  76. {$DEFINE GLB_SUPPORT_PNG_READ}
  77. {$DEFINE GLB_SUPPORT_JPEG_READ}
  78. {$ENDIF}
  79. // Lazarus TPortableNetworkGraphic
  80. {$IFDEF GLB_LAZ_PNG}
  81. {$IFNDEF GLB_LAZARUS}
  82. {$MESSAGE warn 'Lazarus TPortableNetworkGraphic won''t work without Lazarus. Lazarus will be activated.'}
  83. {$DEFINE GLB_LAZARUS}
  84. {$ENDIF}
  85. {$IFDEF GLB_PNGIMAGE}
  86. {$MESSAGE warn 'The pngimage will be ignored if you are using Lazarus TPortableNetworkGraphic.'}
  87. {$undef GLB_PNGIMAGE}
  88. {$ENDIF}
  89. {$IFDEF GLB_LIB_PNG}
  90. {$MESSAGE warn 'The library libPNG will be ignored if you are using Lazarus TPortableNetworkGraphic.'}
  91. {$undef GLB_LIB_PNG}
  92. {$ENDIF}
  93. {$DEFINE GLB_SUPPORT_PNG_READ}
  94. {$DEFINE GLB_SUPPORT_PNG_WRITE}
  95. {$ENDIF}
  96. // PNG Image
  97. {$IFDEF GLB_PNGIMAGE}
  98. {$IFDEF GLB_LIB_PNG}
  99. {$MESSAGE warn 'The library libPNG will be ignored if you are using pngimage.'}
  100. {$undef GLB_LIB_PNG}
  101. {$ENDIF}
  102. {$DEFINE GLB_SUPPORT_PNG_READ}
  103. {$DEFINE GLB_SUPPORT_PNG_WRITE}
  104. {$ENDIF}
  105. // libPNG
  106. {$IFDEF GLB_LIB_PNG}
  107. {$DEFINE GLB_SUPPORT_PNG_READ}
  108. {$DEFINE GLB_SUPPORT_PNG_WRITE}
  109. {$ENDIF}
  110. // Lazarus TJPEGImage
  111. {$IFDEF GLB_LAZ_JPEG}
  112. {$IFNDEF GLB_LAZARUS}
  113. {$MESSAGE warn 'Lazarus TJPEGImage won''t work without Lazarus. Lazarus will be activated.'}
  114. {$DEFINE GLB_LAZARUS}
  115. {$ENDIF}
  116. {$IFDEF GLB_DELPHI_JPEG}
  117. {$MESSAGE warn 'The Delphi JPEGImage will be ignored if you are using the Lazarus TJPEGImage.'}
  118. {$undef GLB_DELPHI_JPEG}
  119. {$ENDIF}
  120. {$IFDEF GLB_LIB_JPEG}
  121. {$MESSAGE warn 'The library libJPEG will be ignored if you are using the Lazarus TJPEGImage.'}
  122. {$undef GLB_LIB_JPEG}
  123. {$ENDIF}
  124. {$DEFINE GLB_SUPPORT_JPEG_READ}
  125. {$DEFINE GLB_SUPPORT_JPEG_WRITE}
  126. {$ENDIF}
  127. // JPEG Image
  128. {$IFDEF GLB_DELPHI_JPEG}
  129. {$IFDEF GLB_LIB_JPEG}
  130. {$MESSAGE warn 'The library libJPEG will be ignored if you are using the unit JPEG.'}
  131. {$undef GLB_LIB_JPEG}
  132. {$ENDIF}
  133. {$DEFINE GLB_SUPPORT_JPEG_READ}
  134. {$DEFINE GLB_SUPPORT_JPEG_WRITE}
  135. {$ENDIF}
  136. // libJPEG
  137. {$IFDEF GLB_LIB_JPEG}
  138. {$DEFINE GLB_SUPPORT_JPEG_READ}
  139. {$DEFINE GLB_SUPPORT_JPEG_WRITE}
  140. {$ENDIF}
  141. // general options
  142. {$EXTENDEDSYNTAX ON}
  143. {$LONGSTRINGS ON}
  144. {$ALIGN ON}
  145. {$IFNDEF FPC}
  146. {$OPTIMIZATION ON}
  147. {$ENDIF}
  148. interface
  149. uses
  150. {$IFDEF OPENGL_ES} dglOpenGLES,
  151. {$ELSE} dglOpenGL, {$ENDIF}
  152. {$IF DEFINED(GLB_WIN) AND
  153. DEFINED(GLB_DELPHI)} windows, {$IFEND}
  154. {$IFDEF GLB_SDL} SDL, {$ENDIF}
  155. {$IFDEF GLB_LAZARUS} IntfGraphics, GraphType, Graphics, {$ENDIF}
  156. {$IFDEF GLB_DELPHI} Dialogs, Graphics, Types, {$ENDIF}
  157. {$IFDEF GLB_SDL_IMAGE} SDL_image, {$ENDIF}
  158. {$IFDEF GLB_PNGIMAGE} pngimage, {$ENDIF}
  159. {$IFDEF GLB_LIB_PNG} libPNG, {$ENDIF}
  160. {$IFDEF GLB_DELPHI_JPEG} JPEG, {$ENDIF}
  161. {$IFDEF GLB_LIB_JPEG} libJPEG, {$ENDIF}
  162. Classes, SysUtils;
  163. type
  164. {$IFNDEF fpc}
  165. QWord = System.UInt64;
  166. PQWord = ^QWord;
  167. PtrInt = Longint;
  168. PtrUInt = DWord;
  169. {$ENDIF}
  170. { type that describes the format of the data stored in a texture.
  171. the name of formats is composed of the following constituents:
  172. - multiple channels:
  173. - channel (e.g. R, G, B, A or Alpha, Luminance or X (reserved))
  174. - width of the chanel in bit (4, 8, 16, ...)
  175. - data type (e.g. ub, us, ui)
  176. - number of elements of data types }
  177. TglBitmapFormat = (
  178. tfEmpty = 0,
  179. tfAlpha4ub1, //< 1 x unsigned byte
  180. tfAlpha8ub1, //< 1 x unsigned byte
  181. tfAlpha16us1, //< 1 x unsigned short
  182. tfLuminance4ub1, //< 1 x unsigned byte
  183. tfLuminance8ub1, //< 1 x unsigned byte
  184. tfLuminance16us1, //< 1 x unsigned short
  185. tfLuminance4Alpha4ub2, //< 1 x unsigned byte (lum), 1 x unsigned byte (alpha)
  186. tfLuminance6Alpha2ub2, //< 1 x unsigned byte (lum), 1 x unsigned byte (alpha)
  187. tfLuminance8Alpha8ub2, //< 1 x unsigned byte (lum), 1 x unsigned byte (alpha)
  188. tfLuminance12Alpha4us2, //< 1 x unsigned short (lum), 1 x unsigned short (alpha)
  189. tfLuminance16Alpha16us2, //< 1 x unsigned short (lum), 1 x unsigned short (alpha)
  190. tfR3G3B2ub1, //< 1 x unsigned byte (3bit red, 3bit green, 2bit blue)
  191. tfRGBX4us1, //< 1 x unsigned short (4bit red, 4bit green, 4bit blue, 4bit reserverd)
  192. tfXRGB4us1, //< 1 x unsigned short (4bit reserved, 4bit red, 4bit green, 4bit blue)
  193. tfR5G6B5us1, //< 1 x unsigned short (5bit red, 6bit green, 5bit blue)
  194. tfRGB5X1us1, //< 1 x unsigned short (5bit red, 5bit green, 5bit blue, 1bit reserved)
  195. tfX1RGB5us1, //< 1 x unsigned short (1bit reserved, 5bit red, 5bit green, 5bit blue)
  196. tfRGB8ub3, //< 1 x unsigned byte (red), 1 x unsigned byte (green), 1 x unsigned byte (blue)
  197. tfRGBX8ui1, //< 1 x unsigned int (8bit red, 8bit green, 8bit blue, 8bit reserved)
  198. tfXRGB8ui1, //< 1 x unsigned int (8bit reserved, 8bit red, 8bit green, 8bit blue)
  199. tfRGB10X2ui1, //< 1 x unsigned int (10bit red, 10bit green, 10bit blue, 2bit reserved)
  200. tfX2RGB10ui1, //< 1 x unsigned int (2bit reserved, 10bit red, 10bit green, 10bit blue)
  201. tfRGB16us3, //< 1 x unsigned short (red), 1 x unsigned short (green), 1 x unsigned short (blue)
  202. tfRGBA4us1, //< 1 x unsigned short (4bit red, 4bit green, 4bit blue, 4bit alpha)
  203. tfARGB4us1, //< 1 x unsigned short (4bit alpha, 4bit red, 4bit green, 4bit blue)
  204. tfRGB5A1us1, //< 1 x unsigned short (5bit red, 5bit green, 5bit blue, 1bit alpha)
  205. tfA1RGB5us1, //< 1 x unsigned short (1bit alpha, 5bit red, 5bit green, 5bit blue)
  206. tfRGBA8ub4, //< 1 x unsigned byte (red), 1 x unsigned byte (green), 1 x unsigned byte (blue), 1 x unsigned byte (alpha)
  207. tfRGBA8ui1, //< 1 x unsigned int (8bit red, 8bit green, 8bit blue, 8 bit alpha)
  208. tfARGB8ui1, //< 1 x unsigned int (8 bit alpha, 8bit red, 8bit green, 8bit blue)
  209. tfRGB10A2ui1, //< 1 x unsigned int (10bit red, 10bit green, 10bit blue, 2bit alpha)
  210. tfA2RGB10ui1, //< 1 x unsigned int (2bit alpha, 10bit red, 10bit green, 10bit blue)
  211. tfRGBA16us4, //< 1 x unsigned short (red), 1 x unsigned short (green), 1 x unsigned short (blue), 1 x unsigned short (alpha)
  212. tfBGRX4us1, //< 1 x unsigned short (4bit blue, 4bit green, 4bit red, 4bit reserved)
  213. tfXBGR4us1, //< 1 x unsigned short (4bit reserved, 4bit blue, 4bit green, 4bit red)
  214. tfB5G6R5us1, //< 1 x unsigned short (5bit blue, 6bit green, 5bit red)
  215. tfBGR5X1us1, //< 1 x unsigned short (5bit blue, 5bit green, 5bit red, 1bit reserved)
  216. tfX1BGR5us1, //< 1 x unsigned short (1bit reserved, 5bit blue, 5bit green, 5bit red)
  217. tfBGR8ub3, //< 1 x unsigned byte (blue), 1 x unsigned byte (green), 1 x unsigned byte (red)
  218. tfBGRX8ui1, //< 1 x unsigned int (8bit blue, 8bit green, 8bit red, 8bit reserved)
  219. tfXBGR8ui1, //< 1 x unsigned int (8bit reserved, 8bit blue, 8bit green, 8bit red)
  220. tfBGR10X2ui1, //< 1 x unsigned int (10bit blue, 10bit green, 10bit red, 2bit reserved)
  221. tfX2BGR10ui1, //< 1 x unsigned int (2bit reserved, 10bit blue, 10bit green, 10bit red)
  222. tfBGR16us3, //< 1 x unsigned short (blue), 1 x unsigned short (green), 1 x unsigned short (red)
  223. tfBGRA4us1, //< 1 x unsigned short (4bit blue, 4bit green, 4bit red, 4bit alpha)
  224. tfABGR4us1, //< 1 x unsigned short (4bit alpha, 4bit blue, 4bit green, 4bit red)
  225. tfBGR5A1us1, //< 1 x unsigned short (5bit blue, 5bit green, 5bit red, 1bit alpha)
  226. tfA1BGR5us1, //< 1 x unsigned short (1bit alpha, 5bit blue, 5bit green, 5bit red)
  227. tfBGRA8ub4, //< 1 x unsigned byte (blue), 1 x unsigned byte (green), 1 x unsigned byte (red), 1 x unsigned byte (alpha)
  228. tfBGRA8ui1, //< 1 x unsigned int (8bit blue, 8bit green, 8bit red, 8bit alpha)
  229. tfABGR8ui1, //< 1 x unsigned int (8bit alpha, 8bit blue, 8bit green, 8bit red)
  230. tfBGR10A2ui1, //< 1 x unsigned int (10bit blue, 10bit green, 10bit red, 2bit alpha)
  231. tfA2BGR10ui1, //< 1 x unsigned int (2bit alpha, 10bit blue, 10bit green, 10bit red)
  232. tfBGRA16us4, //< 1 x unsigned short (blue), 1 x unsigned short (green), 1 x unsigned short (red), 1 x unsigned short (alpha)
  233. tfDepth16us1, //< 1 x unsigned short (depth)
  234. tfDepth24ui1, //< 1 x unsigned int (depth)
  235. tfDepth32ui1, //< 1 x unsigned int (depth)
  236. tfS3tcDtx1RGBA,
  237. tfS3tcDtx3RGBA,
  238. tfS3tcDtx5RGBA
  239. );
  240. { type to define suitable file formats }
  241. TglBitmapFileType = (
  242. {$IFDEF GLB_SUPPORT_PNG_WRITE} ftPNG, {$ENDIF} //< Portable Network Graphic file (PNG)
  243. {$IFDEF GLB_SUPPORT_JPEG_WRITE}ftJPEG, {$ENDIF} //< JPEG file
  244. ftDDS, //< Direct Draw Surface file (DDS)
  245. ftTGA, //< Targa Image File (TGA)
  246. ftBMP, //< Windows Bitmap File (BMP)
  247. ftRAW); //< glBitmap RAW file format
  248. TglBitmapFileTypes = set of TglBitmapFileType;
  249. { possible mipmap types }
  250. TglBitmapMipMap = (
  251. mmNone, //< no mipmaps
  252. mmMipmap, //< normal mipmaps
  253. mmMipmapGlu); //< mipmaps generated with glu functions
  254. { possible normal map functions }
  255. TglBitmapNormalMapFunc = (
  256. nm4Samples,
  257. nmSobel,
  258. nm3x3,
  259. nm5x5);
  260. ////////////////////////////////////////////////////////////////////////////////////////////////////
  261. EglBitmap = class(Exception); //< glBitmap exception
  262. EglBitmapNotSupported = class(Exception); //< exception for not supported functions
  263. EglBitmapSizeToLarge = class(EglBitmap); //< exception for to large textures
  264. EglBitmapNonPowerOfTwo = class(EglBitmap); //< exception for non power of two textures
  265. EglBitmapUnsupportedFormat = class(EglBitmap) //< exception for unsupporetd formats
  266. public
  267. constructor Create(const aFormat: TglBitmapFormat); overload;
  268. constructor Create(const aMsg: String; const aFormat: TglBitmapFormat); overload;
  269. end;
  270. ////////////////////////////////////////////////////////////////////////////////////////////////////
  271. { record that stores 4 unsigned integer values }
  272. TglBitmapRec4ui = packed record
  273. case Integer of
  274. 0: (r, g, b, a: Cardinal);
  275. 1: (arr: array[0..3] of Cardinal);
  276. end;
  277. { record that stores 4 unsigned byte values }
  278. TglBitmapRec4ub = packed record
  279. case Integer of
  280. 0: (r, g, b, a: Byte);
  281. 1: (arr: array[0..3] of Byte);
  282. end;
  283. { record that stores 4 unsigned long integer values }
  284. TglBitmapRec4ul = packed record
  285. case Integer of
  286. 0: (r, g, b, a: QWord);
  287. 1: (arr: array[0..3] of QWord);
  288. end;
  289. { structure to store pixel data in }
  290. TglBitmapPixelData = packed record
  291. Data: TglBitmapRec4ui; //< color data for each color channel
  292. Range: TglBitmapRec4ui; //< maximal color value for each channel
  293. Format: TglBitmapFormat; //< format of the pixel
  294. end;
  295. PglBitmapPixelData = ^TglBitmapPixelData;
  296. TglBitmapSizeFields = set of (ffX, ffY);
  297. TglBitmapSize = packed record
  298. Fields: TglBitmapSizeFields;
  299. X: Word;
  300. Y: Word;
  301. end;
  302. TglBitmapPixelPosition = TglBitmapSize;
  303. { describes the properties of a given texture data format }
  304. TglBitmapFormatDescriptor = class(TObject)
  305. private
  306. // cached properties
  307. fBytesPerPixel: Single; //< number of bytes for each pixel
  308. fChannelCount: Integer; //< number of color channels
  309. fMask: TglBitmapRec4ul; //< bitmask for each color channel
  310. fRange: TglBitmapRec4ui; //< maximal value of each color channel
  311. { @return @true if the format has a red color channel, @false otherwise }
  312. function GetHasRed: Boolean;
  313. { @return @true if the format has a green color channel, @false otherwise }
  314. function GetHasGreen: Boolean;
  315. { @return @true if the format has a blue color channel, @false otherwise }
  316. function GetHasBlue: Boolean;
  317. { @return @true if the format has a alpha color channel, @false otherwise }
  318. function GetHasAlpha: Boolean;
  319. { @return @true if the format has any color color channel, @false otherwise }
  320. function GetHasColor: Boolean;
  321. { @return @true if the format is a grayscale format, @false otherwise }
  322. function GetIsGrayscale: Boolean;
  323. { @return @true if the format is supported by OpenGL, @false otherwise }
  324. function GetHasOpenGLSupport: Boolean;
  325. protected
  326. fFormat: TglBitmapFormat; //< format this descriptor belongs to
  327. fWithAlpha: TglBitmapFormat; //< suitable format with alpha channel
  328. fWithoutAlpha: TglBitmapFormat; //< suitable format without alpha channel
  329. fOpenGLFormat: TglBitmapFormat; //< suitable format that is supported by OpenGL
  330. fRGBInverted: TglBitmapFormat; //< suitable format with inverted RGB channels
  331. fUncompressed: TglBitmapFormat; //< suitable format with uncompressed data
  332. fBitsPerPixel: Integer; //< number of bits per pixel
  333. fIsCompressed: Boolean; //< @true if the format is compressed, @false otherwise
  334. fPrecision: TglBitmapRec4ub; //< number of bits for each color channel
  335. fShift: TglBitmapRec4ub; //< bit offset for each color channel
  336. fglFormat: GLenum; //< OpenGL format enum (e.g. GL_RGB)
  337. fglInternalFormat: GLenum; //< OpenGL internal format enum (e.g. GL_RGB8)
  338. fglDataFormat: GLenum; //< OpenGL data format enum (e.g. GL_UNSIGNED_BYTE)
  339. { set values for this format descriptor }
  340. procedure SetValues; virtual;
  341. { calculate cached values }
  342. procedure CalcValues;
  343. public
  344. property Format: TglBitmapFormat read fFormat; //< format this descriptor belongs to
  345. property ChannelCount: Integer read fChannelCount; //< number of color channels
  346. property IsCompressed: Boolean read fIsCompressed; //< @true if the format is compressed, @false otherwise
  347. property BitsPerPixel: Integer read fBitsPerPixel; //< number of bytes per pixel
  348. property BytesPerPixel: Single read fBytesPerPixel; //< number of bits per pixel
  349. property Precision: TglBitmapRec4ub read fPrecision; //< number of bits for each color channel
  350. property Shift: TglBitmapRec4ub read fShift; //< bit offset for each color channel
  351. property Range: TglBitmapRec4ui read fRange; //< maximal value of each color channel
  352. property Mask: TglBitmapRec4ul read fMask; //< bitmask for each color channel
  353. property RGBInverted: TglBitmapFormat read fRGBInverted; //< suitable format with inverted RGB channels
  354. property WithAlpha: TglBitmapFormat read fWithAlpha; //< suitable format with alpha channel
  355. property WithoutAlpha: TglBitmapFormat read fWithAlpha; //< suitable format without alpha channel
  356. property OpenGLFormat: TglBitmapFormat read fOpenGLFormat; //< suitable format that is supported by OpenGL
  357. property Uncompressed: TglBitmapFormat read fUncompressed; //< suitable format with uncompressed data
  358. property glFormat: GLenum read fglFormat; //< OpenGL format enum (e.g. GL_RGB)
  359. property glInternalFormat: GLenum read fglInternalFormat; //< OpenGL internal format enum (e.g. GL_RGB8)
  360. property glDataFormat: GLenum read fglDataFormat; //< OpenGL data format enum (e.g. GL_UNSIGNED_BYTE)
  361. property HasRed: Boolean read GetHasRed; //< @true if the format has a red color channel, @false otherwise
  362. property HasGreen: Boolean read GetHasGreen; //< @true if the format has a green color channel, @false otherwise
  363. property HasBlue: Boolean read GetHasBlue; //< @true if the format has a blue color channel, @false otherwise
  364. property HasAlpha: Boolean read GetHasAlpha; //< @true if the format has a alpha color channel, @false otherwise
  365. property HasColor: Boolean read GetHasColor; //< @true if the format has any color color channel, @false otherwise
  366. property IsGrayscale: Boolean read GetIsGrayscale; //< @true if the format is a grayscale format, @false otherwise
  367. property HasOpenGLSupport: Boolean read GetHasOpenGLSupport; //< @true if the format is supported by OpenGL, @false otherwise
  368. function GetSize(const aSize: TglBitmapSize): Integer; overload; virtual;
  369. function GetSize(const aWidth, aHeight: Integer): Integer; overload; virtual;
  370. { constructor }
  371. constructor Create;
  372. public
  373. { get the format descriptor by a given OpenGL internal format
  374. @param aInternalFormat OpenGL internal format to get format descriptor for
  375. @returns suitable format descriptor or tfEmpty-Descriptor }
  376. class function GetByFormat(const aInternalFormat: GLenum): TglBitmapFormatDescriptor; overload;
  377. { get the format descriptor by the given format
  378. @param aFormat format to get descriptor for
  379. @return suitable format descriptor or tfEmpty-Descriptor }
  380. class function GetByFormat(const aFormat: TglBitmapFormat): TglBitmapFormatDescriptor; overload;
  381. end;
  382. ////////////////////////////////////////////////////////////////////////////////////////////////////
  383. TglBitmapData = class;
  384. { structure to store data for converting in }
  385. TglBitmapFunctionRec = record
  386. Sender: TglBitmapData; //< texture object that stores the data to convert
  387. Size: TglBitmapSize; //< size of the texture
  388. Position: TglBitmapPixelPosition; //< position of the currently pixel
  389. Source: TglBitmapPixelData; //< pixel data of the current pixel
  390. Dest: TglBitmapPixelData; //< new data of the pixel (must be filled in)
  391. Args: Pointer; //< user defined args that was passed to the convert function
  392. end;
  393. { callback to use for converting texture data }
  394. TglBitmapFunction = procedure(var FuncRec: TglBitmapFunctionRec);
  395. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  396. { class to store texture data in. used to load, save and
  397. manipulate data before assigned to texture object
  398. all operations on a data object can be done from a background thread }
  399. TglBitmapData = class
  400. private { fields }
  401. fData: PByte; //< texture data
  402. fDimension: TglBitmapSize; //< pixel size of the data
  403. fFormat: TglBitmapFormat; //< format the texture data is stored in
  404. fFilename: String; //< file the data was load from
  405. fScanlines: array of PByte; //< pointer to begin of each line
  406. fHasScanlines: Boolean; //< @true if scanlines are initialized, @false otherwise
  407. private { getter / setter }
  408. { @returns the format descriptor suitable to the texture data format }
  409. function GetFormatDescriptor: TglBitmapFormatDescriptor;
  410. { @returns the width of the texture data (in pixel) or -1 if no data is set }
  411. function GetWidth: Integer;
  412. { @returns the height of the texture data (in pixel) or -1 if no data is set }
  413. function GetHeight: Integer;
  414. { get scanline at index aIndex
  415. @returns Pointer to start of line or @nil }
  416. function GetScanlines(const aIndex: Integer): PByte;
  417. { set new value for the data format. only possible if new format has the same pixel size.
  418. if you want to convert the texture data, see ConvertTo function }
  419. procedure SetFormat(const aValue: TglBitmapFormat);
  420. private { internal misc }
  421. { splits a resource identifier into the resource and it's type
  422. @param aResource resource identifier to split and store name in
  423. @param aResType type of the resource }
  424. procedure PrepareResType(var aResource: String; var aResType: PChar);
  425. { updates scanlines array }
  426. procedure UpdateScanlines;
  427. private { internal load and save }
  428. {$IFDEF GLB_SUPPORT_PNG_READ}
  429. { try to load a PNG from a stream
  430. @param aStream stream to load PNG from
  431. @returns @true on success, @false otherwise }
  432. function LoadPNG(const aStream: TStream): Boolean; virtual;
  433. {$ENDIF}
  434. {$ifdef GLB_SUPPORT_PNG_WRITE}
  435. { save texture data as PNG to stream
  436. @param aStream stream to save data to}
  437. procedure SavePNG(const aStream: TStream); virtual;
  438. {$ENDIF}
  439. {$IFDEF GLB_SUPPORT_JPEG_READ}
  440. { try to load a JPEG from a stream
  441. @param aStream stream to load JPEG from
  442. @returns @true on success, @false otherwise }
  443. function LoadJPEG(const aStream: TStream): Boolean; virtual;
  444. {$ENDIF}
  445. {$IFDEF GLB_SUPPORT_JPEG_WRITE}
  446. { save texture data as JPEG to stream
  447. @param aStream stream to save data to}
  448. procedure SaveJPEG(const aStream: TStream); virtual;
  449. {$ENDIF}
  450. { try to load a RAW image from a stream
  451. @param aStream stream to load RAW image from
  452. @returns @true on success, @false otherwise }
  453. function LoadRAW(const aStream: TStream): Boolean;
  454. { save texture data as RAW image to stream
  455. @param aStream stream to save data to}
  456. procedure SaveRAW(const aStream: TStream);
  457. { try to load a BMP from a stream
  458. @param aStream stream to load BMP from
  459. @returns @true on success, @false otherwise }
  460. function LoadBMP(const aStream: TStream): Boolean;
  461. { save texture data as BMP to stream
  462. @param aStream stream to save data to}
  463. procedure SaveBMP(const aStream: TStream);
  464. { try to load a TGA from a stream
  465. @param aStream stream to load TGA from
  466. @returns @true on success, @false otherwise }
  467. function LoadTGA(const aStream: TStream): Boolean;
  468. { save texture data as TGA to stream
  469. @param aStream stream to save data to}
  470. procedure SaveTGA(const aStream: TStream);
  471. { try to load a DDS from a stream
  472. @param aStream stream to load DDS from
  473. @returns @true on success, @false otherwise }
  474. function LoadDDS(const aStream: TStream): Boolean;
  475. { save texture data as DDS to stream
  476. @param aStream stream to save data to}
  477. procedure SaveDDS(const aStream: TStream);
  478. public { properties }
  479. property Data: PByte read fData; //< texture data (be carefull with this!)
  480. property Dimension: TglBitmapSize read fDimension; //< size of the texture data (in pixel)
  481. property Filename: String read fFilename; //< file the data was loaded from
  482. property Width: Integer read GetWidth; //< width of the texture data (in pixel)
  483. property Height: Integer read GetHeight; //< height of the texture data (in pixel)
  484. property Format: TglBitmapFormat read fFormat write SetFormat; //< format the texture data is stored in
  485. property Scanlines[const aIndex: Integer]: PByte read GetScanlines; //< pointer to begin of line at given index or @nil
  486. property FormatDescriptor: TglBitmapFormatDescriptor read GetFormatDescriptor; //< descriptor object that describes the format of the stored data
  487. public { flip }
  488. { flip texture horizontal
  489. @returns @true in success, @false otherwise }
  490. function FlipHorz: Boolean; virtual;
  491. { flip texture vertical
  492. @returns @true in success, @false otherwise }
  493. function FlipVert: Boolean; virtual;
  494. public { load }
  495. { load a texture from a file
  496. @param aFilename file to load texuture from }
  497. procedure LoadFromFile(const aFilename: String);
  498. { load a texture from a stream
  499. @param aStream stream to load texture from }
  500. procedure LoadFromStream(const aStream: TStream); virtual;
  501. { use a function to generate texture data
  502. @param aSize size of the texture
  503. @param aFormat format of the texture data
  504. @param aFunc callback to use for generation
  505. @param aArgs user defined paramaters (use at will) }
  506. procedure LoadFromFunc(const aSize: TglBitmapSize; const aFormat: TglBitmapFormat; const aFunc: TglBitmapFunction; const aArgs: Pointer = nil);
  507. { load a texture from a resource
  508. @param aInstance resource handle
  509. @param aResource resource indentifier
  510. @param aResType resource type (if known) }
  511. procedure LoadFromResource(const aInstance: Cardinal; aResource: String; aResType: PChar = nil);
  512. { load a texture from a resource id
  513. @param aInstance resource handle
  514. @param aResource resource ID
  515. @param aResType resource type }
  516. procedure LoadFromResourceID(const aInstance: Cardinal; const aResourceID: Integer; const aResType: PChar);
  517. public { save }
  518. { save texture data to a file
  519. @param aFilename filename to store texture in
  520. @param aFileType file type to store data into }
  521. procedure SaveToFile(const aFilename: String; const aFileType: TglBitmapFileType);
  522. { save texture data to a stream
  523. @param aFilename filename to store texture in
  524. @param aFileType file type to store data into }
  525. procedure SaveToStream(const aStream: TStream; const aFileType: TglBitmapFileType); virtual;
  526. public { convert }
  527. { convert texture data using a user defined callback
  528. @param aFunc callback to use for converting
  529. @param aCreateTemp create a temporary buffer to use for converting
  530. @param aArgs user defined paramters (use at will)
  531. @returns @true if converting was successful, @false otherwise }
  532. function Convert(const aFunc: TglBitmapFunction; const aCreateTemp: Boolean; const aArgs: Pointer = nil): Boolean; overload;
  533. { convert texture data using a user defined callback
  534. @param aSource glBitmap to read data from
  535. @param aFunc callback to use for converting
  536. @param aCreateTemp create a temporary buffer to use for converting
  537. @param aFormat format of the new data
  538. @param aArgs user defined paramters (use at will)
  539. @returns @true if converting was successful, @false otherwise }
  540. function Convert(const aSource: TglBitmapData; const aFunc: TglBitmapFunction; aCreateTemp: Boolean;
  541. const aFormat: TglBitmapFormat; const aArgs: Pointer = nil): Boolean; overload;
  542. { convert texture data using a specific format
  543. @param aFormat new format of texture data
  544. @returns @true if converting was successful, @false otherwise }
  545. function ConvertTo(const aFormat: TglBitmapFormat): Boolean; virtual;
  546. {$IFDEF GLB_SDL}
  547. public { SDL }
  548. { assign texture data to SDL surface
  549. @param aSurface SDL surface to write data to
  550. @returns @true on success, @false otherwise }
  551. function AssignToSurface(out aSurface: PSDL_Surface): Boolean;
  552. { assign texture data from SDL surface
  553. @param aSurface SDL surface to read data from
  554. @returns @true on success, @false otherwise }
  555. function AssignFromSurface(const aSurface: PSDL_Surface): Boolean;
  556. { assign alpha channel data to SDL surface
  557. @param aSurface SDL surface to write alpha channel data to
  558. @returns @true on success, @false otherwise }
  559. function AssignAlphaToSurface(out aSurface: PSDL_Surface): Boolean;
  560. { assign alpha channel data from SDL surface
  561. @param aSurface SDL surface to read data from
  562. @param aFunc callback to use for converting
  563. @param aArgs user defined parameters (use at will)
  564. @returns @true on success, @false otherwise }
  565. function AddAlphaFromSurface(const aSurface: PSDL_Surface; const aFunc: TglBitmapFunction = nil; const aArgs: Pointer = nil): Boolean;
  566. {$ENDIF}
  567. {$IFDEF GLB_DELPHI}
  568. public { Delphi }
  569. { assign texture data to TBitmap object
  570. @param aBitmap TBitmap to write data to
  571. @returns @true on success, @false otherwise }
  572. function AssignToBitmap(const aBitmap: TBitmap): Boolean;
  573. { assign texture data from TBitmap object
  574. @param aBitmap TBitmap to read data from
  575. @returns @true on success, @false otherwise }
  576. function AssignFromBitmap(const aBitmap: TBitmap): Boolean;
  577. { assign alpha channel data to TBitmap object
  578. @param aBitmap TBitmap to write data to
  579. @returns @true on success, @false otherwise }
  580. function AssignAlphaToBitmap(const aBitmap: TBitmap): Boolean;
  581. { assign alpha channel data from TBitmap object
  582. @param aBitmap TBitmap to read data from
  583. @param aFunc callback to use for converting
  584. @param aArgs user defined parameters (use at will)
  585. @returns @true on success, @false otherwise }
  586. function AddAlphaFromBitmap(const aBitmap: TBitmap; const aFunc: TglBitmapFunction = nil; const aArgs: Pointer = nil): Boolean;
  587. {$ENDIF}
  588. {$IFDEF GLB_LAZARUS}
  589. public { Lazarus }
  590. { assign texture data to TLazIntfImage object
  591. @param aImage TLazIntfImage to write data to
  592. @returns @true on success, @false otherwise }
  593. function AssignToLazIntfImage(const aImage: TLazIntfImage): Boolean;
  594. { assign texture data from TLazIntfImage object
  595. @param aImage TLazIntfImage to read data from
  596. @returns @true on success, @false otherwise }
  597. function AssignFromLazIntfImage(const aImage: TLazIntfImage): Boolean;
  598. { assign alpha channel data to TLazIntfImage object
  599. @param aImage TLazIntfImage to write data to
  600. @returns @true on success, @false otherwise }
  601. function AssignAlphaToLazIntfImage(const aImage: TLazIntfImage): Boolean;
  602. { assign alpha channel data from TLazIntfImage object
  603. @param aImage TLazIntfImage to read data from
  604. @param aFunc callback to use for converting
  605. @param aArgs user defined parameters (use at will)
  606. @returns @true on success, @false otherwise }
  607. function AddAlphaFromLazIntfImage(const aImage: TLazIntfImage; const aFunc: TglBitmapFunction = nil; const aArgs: Pointer = nil): Boolean;
  608. {$ENDIF}
  609. public { Alpha }
  610. { load alpha channel data from resource
  611. @param aInstance resource handle
  612. @param aResource resource ID
  613. @param aResType resource type
  614. @param aFunc callback to use for converting
  615. @param aArgs user defined parameters (use at will)
  616. @returns @true on success, @false otherwise }
  617. function AddAlphaFromResource(const aInstance: Cardinal; aResource: String; aResType: PChar = nil; const aFunc: TglBitmapFunction = nil; const aArgs: Pointer = nil): Boolean;
  618. { load alpha channel data from resource ID
  619. @param aInstance resource handle
  620. @param aResourceID resource ID
  621. @param aResType resource type
  622. @param aFunc callback to use for converting
  623. @param aArgs user defined parameters (use at will)
  624. @returns @true on success, @false otherwise }
  625. function AddAlphaFromResourceID(const aInstance: Cardinal; const aResourceID: Integer; const aResType: PChar; const aFunc: TglBitmapFunction = nil; const aArgs: Pointer = nil): Boolean;
  626. { add alpha channel data from function
  627. @param aFunc callback to get data from
  628. @param aArgs user defined parameters (use at will)
  629. @returns @true on success, @false otherwise }
  630. function AddAlphaFromFunc(const aFunc: TglBitmapFunction; const aArgs: Pointer = nil): Boolean; virtual;
  631. { add alpha channel data from file (macro for: new glBitmap, LoadFromFile, AddAlphaFromGlBitmap)
  632. @param aFilename file to load alpha channel data from
  633. @param aFunc callback to use for converting
  634. @param aArgs SetFormat user defined parameters (use at will)
  635. @returns @true on success, @false otherwise }
  636. function AddAlphaFromFile(const aFileName: String; const aFunc: TglBitmapFunction = nil; const aArgs: Pointer = nil): Boolean;
  637. { add alpha channel data from stream (macro for: new glBitmap, LoadFromStream, AddAlphaFromGlBitmap)
  638. @param aStream stream to load alpha channel data from
  639. @param aFunc callback to use for converting
  640. @param aArgs user defined parameters (use at will)
  641. @returns @true on success, @false otherwise }
  642. function AddAlphaFromStream(const aStream: TStream; const aFunc: TglBitmapFunction = nil; const aArgs: Pointer = nil): Boolean;
  643. { add alpha channel data from existing glBitmap object
  644. @param aBitmap TglBitmap to copy alpha channel data from
  645. @param aFunc callback to use for converting
  646. @param aArgs user defined parameters (use at will)
  647. @returns @true on success, @false otherwise }
  648. function AddAlphaFromDataObj(const aDataObj: TglBitmapData; aFunc: TglBitmapFunction; const aArgs: Pointer): Boolean;
  649. { add alpha to pixel if the pixels color is greter than the given color value
  650. @param aRed red threshold (0-255)
  651. @param aGreen green threshold (0-255)
  652. @param aBlue blue threshold (0-255)
  653. @param aDeviatation accepted deviatation (0-255)
  654. @returns @true on success, @false otherwise }
  655. function AddAlphaFromColorKey(const aRed, aGreen, aBlue: Byte; const aDeviation: Byte = 0): Boolean;
  656. { add alpha to pixel if the pixels color is greter than the given color value
  657. @param aRed red threshold (0-Range.r)
  658. @param aGreen green threshold (0-Range.g)
  659. @param aBlue blue threshold (0-Range.b)
  660. @param aDeviatation accepted deviatation (0-max(Range.rgb))
  661. @returns @true on success, @false otherwise }
  662. function AddAlphaFromColorKeyRange(const aRed, aGreen, aBlue: Cardinal; const aDeviation: Cardinal = 0): Boolean;
  663. { add alpha to pixel if the pixels color is greter than the given color value
  664. @param aRed red threshold (0.0-1.0)
  665. @param aGreen green threshold (0.0-1.0)
  666. @param aBlue blue threshold (0.0-1.0)
  667. @param aDeviatation accepted deviatation (0.0-1.0)
  668. @returns @true on success, @false otherwise }
  669. function AddAlphaFromColorKeyFloat(const aRed, aGreen, aBlue: Single; const aDeviation: Single = 0): Boolean;
  670. { add a constand alpha value to all pixels
  671. @param aAlpha alpha value to add (0-255)
  672. @returns @true on success, @false otherwise }
  673. function AddAlphaFromValue(const aAlpha: Byte): Boolean;
  674. { add a constand alpha value to all pixels
  675. @param aAlpha alpha value to add (0-max(Range.rgb))
  676. @returns @true on success, @false otherwise }
  677. function AddAlphaFromValueRange(const aAlpha: Cardinal): Boolean;
  678. { add a constand alpha value to all pixels
  679. @param aAlpha alpha value to add (0.0-1.0)
  680. @returns @true on success, @false otherwise }
  681. function AddAlphaFromValueFloat(const aAlpha: Single): Boolean;
  682. { remove alpha channel
  683. @returns @true on success, @false otherwise }
  684. function RemoveAlpha: Boolean; virtual;
  685. public { fill }
  686. { fill complete texture with one color
  687. @param aRed red color for border (0-255)
  688. @param aGreen green color for border (0-255)
  689. @param aBlue blue color for border (0-255)
  690. @param aAlpha alpha color for border (0-255) }
  691. procedure FillWithColor(const aRed, aGreen, aBlue: Byte; const aAlpha: Byte = 255);
  692. { fill complete texture with one color
  693. @param aRed red color for border (0-Range.r)
  694. @param aGreen green color for border (0-Range.g)
  695. @param aBlue blue color for border (0-Range.b)
  696. @param aAlpha alpha color for border (0-Range.a) }
  697. procedure FillWithColorRange(const aRed, aGreen, aBlue: Cardinal; const aAlpha: Cardinal = $FFFFFFFF);
  698. { fill complete texture with one color
  699. @param aRed red color for border (0.0-1.0)
  700. @param aGreen green color for border (0.0-1.0)
  701. @param aBlue blue color for border (0.0-1.0)
  702. @param aAlpha alpha color for border (0.0-1.0) }
  703. procedure FillWithColorFloat(const aRed, aGreen, aBlue: Single; const aAlpha: Single = 1.0);
  704. public { Misc }
  705. { set data pointer of texture data
  706. @param aData pointer to new texture data
  707. @param aFormat format of the data stored at aData
  708. @param aWidth width of the texture data
  709. @param aHeight height of the texture data }
  710. procedure SetData(const aData: PByte; const aFormat: TglBitmapFormat;
  711. const aWidth: Integer = -1; const aHeight: Integer = -1); virtual;
  712. { create a clone of the current object
  713. @returns clone of this object}
  714. function Clone: TglBitmapData;
  715. { invert color data (bitwise not)
  716. @param aRed invert red channel
  717. @param aGreen invert green channel
  718. @param aBlue invert blue channel
  719. @param aAlpha invert alpha channel }
  720. procedure Invert(const aRed, aGreen, aBlue, aAlpha: Boolean);
  721. { create normal map from texture data
  722. @param aFunc normal map function to generate normalmap with
  723. @param aScale scale of the normale stored in the normal map
  724. @param aUseAlpha generate normalmap from alpha channel data (if present) }
  725. procedure GenerateNormalMap(const aFunc: TglBitmapNormalMapFunc = nm3x3;
  726. const aScale: Single = 2; const aUseAlpha: Boolean = false);
  727. public { constructor }
  728. { constructor - creates a texutre data object }
  729. constructor Create; overload;
  730. { constructor - creates a texture data object and loads it from a file
  731. @param aFilename file to load texture from }
  732. constructor Create(const aFileName: String); overload;
  733. { constructor - creates a texture data object and loads it from a stream
  734. @param aStream stream to load texture from }
  735. constructor Create(const aStream: TStream); overload;
  736. { constructor - creates a texture data object with the given size, format and data
  737. @param aSize size of the texture
  738. @param aFormat format of the given data
  739. @param aData texture data - be carefull: the data will now be managed by the texture data object }
  740. constructor Create(const aSize: TglBitmapSize; const aFormat: TglBitmapFormat; aData: PByte = nil); overload;
  741. { constructor - creates a texture data object with the given size and format and uses the given callback to create the data
  742. @param aSize size of the texture
  743. @param aFormat format of the given data
  744. @param aFunc callback to use for generating the data
  745. @param aArgs user defined parameters (use at will) }
  746. constructor Create(const aSize: TglBitmapSize; const aFormat: TglBitmapFormat; const aFunc: TglBitmapFunction; const aArgs: Pointer = nil); overload;
  747. { constructor - creates a texture data object and loads it from a resource
  748. @param aInstance resource handle
  749. @param aResource resource indentifier
  750. @param aResType resource type (if known) }
  751. constructor Create(const aInstance: Cardinal; const aResource: String; const aResType: PChar = nil); overload;
  752. { constructor - creates a texture data object and loads it from a resource
  753. @param aInstance resource handle
  754. @param aResourceID resource ID
  755. @param aResType resource type (if known) }
  756. constructor Create(const aInstance: Cardinal; const aResourceID: Integer; const aResType: PChar); overload;
  757. { destructor }
  758. destructor Destroy; override;
  759. end;
  760. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  761. { base class for all glBitmap classes. used to manage OpenGL texture objects
  762. all operations on a bitmap object must be done from the render thread }
  763. TglBitmap = class
  764. protected
  765. fID: GLuint; //< name of the OpenGL texture object
  766. fTarget: GLuint; //< texture target (e.g. GL_TEXTURE_2D)
  767. fDeleteTextureOnFree: Boolean; //< delete OpenGL texture object when this object is destroyed
  768. // texture properties
  769. fFilterMin: GLenum; //< min filter to apply to the texture
  770. fFilterMag: GLenum; //< mag filter to apply to the texture
  771. fWrapS: GLenum; //< texture wrapping for x axis
  772. fWrapT: GLenum; //< texture wrapping for y axis
  773. fWrapR: GLenum; //< texture wrapping for z axis
  774. fAnisotropic: Integer; //< anisotropic level
  775. fBorderColor: array[0..3] of Single; //< color of the texture border
  776. {$IF NOT DEFINED(OPENGL_ES) OR DEFINED(OPENGL_ES_3_0)}
  777. //Swizzle
  778. fSwizzle: array[0..3] of GLenum; //< color channel swizzle
  779. {$IFEND}
  780. {$IFNDEF OPENGL_ES}
  781. fIsResident: GLboolean; //< @true if OpenGL texture object has data, @false otherwise
  782. {$ENDIF}
  783. fDimension: TglBitmapSize; //< size of this texture
  784. fMipMap: TglBitmapMipMap; //< mipmap type
  785. // CustomData
  786. fCustomData: Pointer; //< user defined data
  787. fCustomName: String; //< user defined name
  788. fCustomNameW: WideString; //< user defined name
  789. protected
  790. { @returns the actual width of the texture }
  791. function GetWidth: Integer; virtual;
  792. { @returns the actual height of the texture }
  793. function GetHeight: Integer; virtual;
  794. protected
  795. { set a new value for fCustomData }
  796. procedure SetCustomData(const aValue: Pointer);
  797. { set a new value for fCustomName }
  798. procedure SetCustomName(const aValue: String);
  799. { set a new value for fCustomNameW }
  800. procedure SetCustomNameW(const aValue: WideString);
  801. { set new value for fDeleteTextureOnFree }
  802. procedure SetDeleteTextureOnFree(const aValue: Boolean);
  803. { set name of OpenGL texture object }
  804. procedure SetID(const aValue: Cardinal);
  805. { set new value for fMipMap }
  806. procedure SetMipMap(const aValue: TglBitmapMipMap);
  807. { set new value for target }
  808. procedure SetTarget(const aValue: Cardinal);
  809. { set new value for fAnisotrophic }
  810. procedure SetAnisotropic(const aValue: Integer);
  811. protected
  812. { create OpenGL texture object (delete exisiting object if exists) }
  813. procedure CreateID;
  814. { setup texture parameters }
  815. procedure SetupParameters({$IFNDEF OPENGL_ES}out aBuildWithGlu: Boolean{$ENDIF});
  816. protected
  817. property Width: Integer read GetWidth; //< the actual width of the texture
  818. property Height: Integer read GetHeight; //< the actual height of the texture
  819. public
  820. property ID: Cardinal read fID write SetID; //< name of the OpenGL texture object
  821. property Target: Cardinal read fTarget write SetTarget; //< texture target (e.g. GL_TEXTURE_2D)
  822. property DeleteTextureOnFree: Boolean read fDeleteTextureOnFree write SetDeleteTextureOnFree; //< delete texture object when this object is destroyed
  823. property MipMap: TglBitmapMipMap read fMipMap write SetMipMap; //< mipmap type
  824. property Anisotropic: Integer read fAnisotropic write SetAnisotropic; //< anisotropic level
  825. property CustomData: Pointer read fCustomData write SetCustomData; //< user defined data (use at will)
  826. property CustomName: String read fCustomName write SetCustomName; //< user defined name (use at will)
  827. property CustomNameW: WideString read fCustomNameW write SetCustomNameW; //< user defined name (as WideString; use at will)
  828. property Dimension: TglBitmapSize read fDimension; //< size of the texture
  829. {$IFNDEF OPENGL_ES}
  830. property IsResident: GLboolean read fIsResident; //< @true if OpenGL texture object has data, @false otherwise
  831. {$ENDIF}
  832. { this method is called after the constructor and sets the default values of this object }
  833. procedure AfterConstruction; override;
  834. { this method is called before the destructor and does some cleanup }
  835. procedure BeforeDestruction; override;
  836. public
  837. {$IFNDEF OPENGL_ES}
  838. { set the new value for texture border color
  839. @param aRed red color for border (0.0-1.0)
  840. @param aGreen green color for border (0.0-1.0)
  841. @param aBlue blue color for border (0.0-1.0)
  842. @param aAlpha alpha color for border (0.0-1.0) }
  843. procedure SetBorderColor(const aRed, aGreen, aBlue, aAlpha: Single);
  844. {$ENDIF}
  845. public
  846. { set new texture filer
  847. @param aMin min filter
  848. @param aMag mag filter }
  849. procedure SetFilter(const aMin, aMag: GLenum);
  850. { set new texture wrapping
  851. @param S texture wrapping for x axis
  852. @param T texture wrapping for y axis
  853. @param R texture wrapping for z axis }
  854. procedure SetWrap(
  855. const S: GLenum = GL_CLAMP_TO_EDGE;
  856. const T: GLenum = GL_CLAMP_TO_EDGE;
  857. const R: GLenum = GL_CLAMP_TO_EDGE);
  858. {$IF NOT DEFINED(OPENGL_ES) OR DEFINED(OPENGL_ES_3_0)}
  859. { set new swizzle
  860. @param r swizzle for red channel
  861. @param g swizzle for green channel
  862. @param b swizzle for blue channel
  863. @param a swizzle for alpha channel }
  864. procedure SetSwizzle(const r, g, b, a: GLenum);
  865. {$IFEND}
  866. public
  867. { bind texture
  868. @param aEnableTextureUnit enable texture unit for this texture (e.g. glEnable(GL_TEXTURE_2D)) }
  869. procedure Bind({$IFNDEF OPENGL_ES}const aEnableTextureUnit: Boolean = true{$ENDIF}); virtual;
  870. { bind texture
  871. @param aDisableTextureUnit disable texture unit for this texture (e.g. glEnable(GL_TEXTURE_2D)) }
  872. procedure Unbind({$IFNDEF OPENGL_ES}const aDisableTextureUnit: Boolean = true{$ENDIF}); virtual;
  873. { upload texture data from given data object to video card
  874. @param aData texture data object that contains the actual data
  875. @param aCheckSize check size before upload and throw exception if something is wrong }
  876. procedure UploadData(const aDataObj: TglBitmapData; const aCheckSize: Boolean = true); virtual;
  877. {$IFNDEF OPENGL_ES}
  878. { download texture data from video card and store it into given data object
  879. @returns @true when download was successfull, @false otherwise }
  880. function DownloadData(const aDataObj: TglBitmapData): Boolean; virtual;
  881. {$ENDIF}
  882. public
  883. { constructor - creates an empty texture }
  884. constructor Create; overload;
  885. { constructor - creates an texture object and uploads the given data }
  886. constructor Create(const aData: TglBitmapData); overload;
  887. end;
  888. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  889. {$IF NOT DEFINED(OPENGL_ES)}
  890. { wrapper class for 1-dimensional textures (OpenGL target = GL_TEXTURE_1D
  891. all operations on a bitmap object must be done from the render thread }
  892. TglBitmap1D = class(TglBitmap)
  893. protected
  894. { upload the texture data to video card
  895. @param aDataObj texture data object that contains the actual data
  896. @param aBuildWithGlu use glu functions to build mipmaps }
  897. procedure UploadDataIntern(const aDataObj: TglBitmapData; const aBuildWithGlu: Boolean);
  898. public
  899. property Width; //< actual with of the texture
  900. { this method is called after constructor and initializes the object }
  901. procedure AfterConstruction; override;
  902. { upload texture data from given data object to video card
  903. @param aData texture data object that contains the actual data
  904. @param aCheckSize check size before upload and throw exception if something is wrong }
  905. procedure UploadData(const aDataObj: TglBitmapData; const aCheckSize: Boolean = true); override;
  906. end;
  907. {$IFEND}
  908. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  909. { wrapper class for 2-dimensional textures (OpenGL target = GL_TEXTURE_2D)
  910. all operations on a bitmap object must be done from the render thread }
  911. TglBitmap2D = class(TglBitmap)
  912. protected
  913. { upload the texture data to video card
  914. @param aDataObj texture data object that contains the actual data
  915. @param aTarget target o upload data to (e.g. GL_TEXTURE_2D)
  916. @param aBuildWithGlu use glu functions to build mipmaps }
  917. procedure UploadDataIntern(const aDataObj: TglBitmapData; const aTarget: GLenum
  918. {$IFNDEF OPENGL_ES}; const aBuildWithGlu: Boolean{$ENDIF});
  919. public
  920. property Width; //< actual width of the texture
  921. property Height; //< actual height of the texture
  922. { this method is called after constructor and initializes the object }
  923. procedure AfterConstruction; override;
  924. { upload texture data from given data object to video card
  925. @param aData texture data object that contains the actual data
  926. @param aCheckSize check size before upload and throw exception if something is wrong }
  927. procedure UploadData(const aDataObj: TglBitmapData; const aCheckSize: Boolean = true); override;
  928. public
  929. { copy a part of the frame buffer to the texture
  930. @param aTop topmost pixel to copy
  931. @param aLeft leftmost pixel to copy
  932. @param aRight rightmost pixel to copy
  933. @param aBottom bottommost pixel to copy
  934. @param aFormat format to store data in
  935. @param aDataObj texture data object to store the data in }
  936. class procedure GrabScreen(const aTop, aLeft, aRight, aBottom: Integer; const aFormat: TglBitmapFormat; const aDataObj: TglBitmapData);
  937. end;
  938. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  939. {$IF NOT DEFINED(OPENGL_ES) OR DEFINED(OPENGL_ES_2_0)}
  940. { wrapper class for cube maps (OpenGL target = GL_TEXTURE_CUBE_MAP)
  941. all operations on a bitmap object must be done from the render thread }
  942. TglBitmapCubeMap = class(TglBitmap2D)
  943. protected
  944. {$IFNDEF OPENGL_ES}
  945. fGenMode: Integer; //< generation mode for the cube map (e.g. GL_REFLECTION_MAP)
  946. {$ENDIF}
  947. public
  948. { this method is called after constructor and initializes the object }
  949. procedure AfterConstruction; override;
  950. { upload texture data from given data object to video card
  951. @param aData texture data object that contains the actual data
  952. @param aCheckSize check size before upload and throw exception if something is wrong }
  953. procedure UploadData(const aDataObj: TglBitmapData; const aCheckSize: Boolean = true); override;
  954. { upload texture data from given data object to video card
  955. @param aData texture data object that contains the actual data
  956. @param aCubeTarget cube map target to upload data to (e.g. GL_TEXTURE_CUBE_MAP_POSITIVE_X)
  957. @param aCheckSize check size before upload and throw exception if something is wrong }
  958. procedure UploadCubeMap(const aDataObj: TglBitmapData; const aCubeTarget: Cardinal; const aCheckSize: Boolean);
  959. { bind texture
  960. @param aEnableTexCoordsGen enable cube map generator
  961. @param aEnableTextureUnit enable texture unit }
  962. procedure Bind({$IFNDEF OPENGL_ES}const aEnableTexCoordsGen: Boolean = true; const aEnableTextureUnit: Boolean = true{$ENDIF}); reintroduce; virtual;
  963. { unbind texture
  964. @param aDisableTexCoordsGen disable cube map generator
  965. @param aDisableTextureUnit disable texture unit }
  966. procedure Unbind({$IFNDEF OPENGL_ES}const aDisableTexCoordsGen: Boolean = true; const aDisableTextureUnit: Boolean = true{$ENDIF}); reintroduce; virtual;
  967. end;
  968. {$IFEND}
  969. {$IF NOT DEFINED(OPENGL_ES) OR DEFINED(OPENGL_ES_2_0)}
  970. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  971. { wrapper class for cube normal maps
  972. all operations on a bitmap object must be done from the render thread }
  973. TglBitmapNormalMap = class(TglBitmapCubeMap)
  974. public
  975. { this method is called after constructor and initializes the object }
  976. procedure AfterConstruction; override;
  977. { create cube normal map from texture data and upload it to video card
  978. @param aSize size of each cube map texture
  979. @param aCheckSize check size before upload and throw exception if something is wrong }
  980. procedure GenerateNormalMap(const aSize: Integer = 32; const aCheckSize: Boolean = true);
  981. end;
  982. {$IFEND}
  983. const
  984. NULL_SIZE: TglBitmapSize = (Fields: []; X: 0; Y: 0);
  985. procedure glBitmapSetDefaultDeleteTextureOnFree(const aDeleteTextureOnFree: Boolean);
  986. procedure glBitmapSetDefaultFreeDataAfterGenTexture(const aFreeData: Boolean);
  987. procedure glBitmapSetDefaultMipmap(const aValue: TglBitmapMipMap);
  988. procedure glBitmapSetDefaultFormat(const aFormat: TglBitmapFormat);
  989. procedure glBitmapSetDefaultFilter(const aMin, aMag: Integer);
  990. procedure glBitmapSetDefaultWrap(
  991. const S: Cardinal = GL_CLAMP_TO_EDGE;
  992. const T: Cardinal = GL_CLAMP_TO_EDGE;
  993. const R: Cardinal = GL_CLAMP_TO_EDGE);
  994. {$IF NOT DEFINED(OPENGL_ES) OR DEFINED(OPENGL_ES_3_0)}
  995. procedure glBitmapSetDefaultSwizzle(const r: GLenum = GL_RED; g: GLenum = GL_GREEN; b: GLenum = GL_BLUE; a: GLenum = GL_ALPHA);
  996. {$IFEND}
  997. function glBitmapGetDefaultDeleteTextureOnFree: Boolean;
  998. function glBitmapGetDefaultFreeDataAfterGenTexture: Boolean;
  999. function glBitmapGetDefaultMipmap: TglBitmapMipMap;
  1000. function glBitmapGetDefaultFormat: TglBitmapFormat;
  1001. procedure glBitmapGetDefaultFilter(var aMin, aMag: Cardinal);
  1002. procedure glBitmapGetDefaultTextureWrap(var S, T, R: Cardinal);
  1003. {$IF NOT DEFINED(OPENGL_ES) OR DEFINED(OPENGL_ES_3_0)}
  1004. procedure glBitmapGetDefaultSwizzle(var r, g, b, a: GLenum);
  1005. {$IFEND}
  1006. function glBitmapSize(X: Integer = -1; Y: Integer = -1): TglBitmapSize;
  1007. function glBitmapPosition(X: Integer = -1; Y: Integer = -1): TglBitmapPixelPosition;
  1008. function glBitmapRec4ub(const r, g, b, a: Byte): TglBitmapRec4ub;
  1009. function glBitmapRec4ui(const r, g, b, a: Cardinal): TglBitmapRec4ui;
  1010. function glBitmapRec4ul(const r, g, b, a: QWord): TglBitmapRec4ul;
  1011. function glBitmapRec4ubCompare(const r1, r2: TglBitmapRec4ub): Boolean;
  1012. function glBitmapRec4uiCompare(const r1, r2: TglBitmapRec4ui): Boolean;
  1013. function glBitmapCreateTestData(const aFormat: TglBitmapFormat): TglBitmapData;
  1014. {$IFDEF GLB_DELPHI}
  1015. function CreateGrayPalette: HPALETTE;
  1016. {$ENDIF}
  1017. implementation
  1018. uses
  1019. Math, syncobjs, typinfo
  1020. {$IF DEFINED(GLB_SUPPORT_JPEG_READ) AND DEFINED(GLB_LAZ_JPEG)}, FPReadJPEG{$IFEND};
  1021. var
  1022. glBitmapDefaultDeleteTextureOnFree: Boolean;
  1023. glBitmapDefaultFreeDataAfterGenTextures: Boolean;
  1024. glBitmapDefaultFormat: TglBitmapFormat;
  1025. glBitmapDefaultMipmap: TglBitmapMipMap;
  1026. glBitmapDefaultFilterMin: Cardinal;
  1027. glBitmapDefaultFilterMag: Cardinal;
  1028. glBitmapDefaultWrapS: Cardinal;
  1029. glBitmapDefaultWrapT: Cardinal;
  1030. glBitmapDefaultWrapR: Cardinal;
  1031. glDefaultSwizzle: array[0..3] of GLenum;
  1032. ////////////////////////////////////////////////////////////////////////////////////////////////////
  1033. type
  1034. TFormatDescriptor = class(TglBitmapFormatDescriptor)
  1035. public
  1036. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); virtual; abstract;
  1037. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); virtual; abstract;
  1038. function CreateMappingData: Pointer; virtual;
  1039. procedure FreeMappingData(var aMappingData: Pointer); virtual;
  1040. function IsEmpty: Boolean; virtual;
  1041. function MaskMatch(const aMask: TglBitmapRec4ul): Boolean; virtual;
  1042. procedure PreparePixel(out aPixel: TglBitmapPixelData); virtual;
  1043. constructor Create; virtual;
  1044. public
  1045. class procedure Init;
  1046. class function Get(const aFormat: TglBitmapFormat): TFormatDescriptor;
  1047. class function GetAlpha(const aFormat: TglBitmapFormat): TFormatDescriptor;
  1048. class function GetFromMask(const aMask: TglBitmapRec4ul; const aBitCount: Integer = 0): TFormatDescriptor;
  1049. class function GetFromPrecShift(const aPrec, aShift: TglBitmapRec4ub; const aBitCount: Integer): TFormatDescriptor;
  1050. class procedure Clear;
  1051. class procedure Finalize;
  1052. end;
  1053. TFormatDescriptorClass = class of TFormatDescriptor;
  1054. TfdEmpty = class(TFormatDescriptor);
  1055. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1056. TfdAlphaUB1 = class(TFormatDescriptor) //1* unsigned byte
  1057. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1058. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1059. end;
  1060. TfdLuminanceUB1 = class(TFormatDescriptor) //1* unsigned byte
  1061. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1062. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1063. end;
  1064. TfdUniversalUB1 = class(TFormatDescriptor) //1* unsigned byte
  1065. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1066. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1067. end;
  1068. TfdLuminanceAlphaUB2 = class(TfdLuminanceUB1) //2* unsigned byte
  1069. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1070. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1071. end;
  1072. TfdRGBub3 = class(TFormatDescriptor) //3* unsigned byte
  1073. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1074. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1075. end;
  1076. TfdBGRub3 = class(TFormatDescriptor) //3* unsigned byte (inverse)
  1077. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1078. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1079. end;
  1080. TfdRGBAub4 = class(TfdRGBub3) //3* unsigned byte
  1081. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1082. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1083. end;
  1084. TfdBGRAub4 = class(TfdBGRub3) //3* unsigned byte (inverse)
  1085. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1086. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1087. end;
  1088. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1089. TfdAlphaUS1 = class(TFormatDescriptor) //1* unsigned short
  1090. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1091. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1092. end;
  1093. TfdLuminanceUS1 = class(TFormatDescriptor) //1* unsigned short
  1094. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1095. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1096. end;
  1097. TfdUniversalUS1 = class(TFormatDescriptor) //1* unsigned short
  1098. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1099. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1100. end;
  1101. TfdDepthUS1 = class(TFormatDescriptor) //1* unsigned short
  1102. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1103. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1104. end;
  1105. TfdLuminanceAlphaUS2 = class(TfdLuminanceUS1) //2* unsigned short
  1106. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1107. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1108. end;
  1109. TfdRGBus3 = class(TFormatDescriptor) //3* unsigned short
  1110. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1111. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1112. end;
  1113. TfdBGRus3 = class(TFormatDescriptor) //3* unsigned short (inverse)
  1114. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1115. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1116. end;
  1117. TfdRGBAus4 = class(TfdRGBus3) //4* unsigned short
  1118. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1119. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1120. end;
  1121. TfdARGBus4 = class(TfdRGBus3) //4* unsigned short
  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. end;
  1125. TfdBGRAus4 = class(TfdBGRus3) //4* unsigned short (inverse)
  1126. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1127. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1128. end;
  1129. TfdABGRus4 = class(TfdBGRus3) //4* unsigned short (inverse)
  1130. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1131. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1132. end;
  1133. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1134. TfdUniversalUI1 = class(TFormatDescriptor) //1* unsigned int
  1135. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1136. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1137. end;
  1138. TfdDepthUI1 = class(TFormatDescriptor) //1* unsigned int
  1139. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1140. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1141. end;
  1142. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1143. TfdAlpha4ub1 = class(TfdAlphaUB1)
  1144. procedure SetValues; override;
  1145. end;
  1146. TfdAlpha8ub1 = class(TfdAlphaUB1)
  1147. procedure SetValues; override;
  1148. end;
  1149. TfdAlpha16us1 = class(TfdAlphaUS1)
  1150. procedure SetValues; override;
  1151. end;
  1152. TfdLuminance4ub1 = class(TfdLuminanceUB1)
  1153. procedure SetValues; override;
  1154. end;
  1155. TfdLuminance8ub1 = class(TfdLuminanceUB1)
  1156. procedure SetValues; override;
  1157. end;
  1158. TfdLuminance16us1 = class(TfdLuminanceUS1)
  1159. procedure SetValues; override;
  1160. end;
  1161. TfdLuminance4Alpha4ub2 = class(TfdLuminanceAlphaUB2)
  1162. procedure SetValues; override;
  1163. end;
  1164. TfdLuminance6Alpha2ub2 = class(TfdLuminanceAlphaUB2)
  1165. procedure SetValues; override;
  1166. end;
  1167. TfdLuminance8Alpha8ub2 = class(TfdLuminanceAlphaUB2)
  1168. procedure SetValues; override;
  1169. end;
  1170. TfdLuminance12Alpha4us2 = class(TfdLuminanceAlphaUS2)
  1171. procedure SetValues; override;
  1172. end;
  1173. TfdLuminance16Alpha16us2 = class(TfdLuminanceAlphaUS2)
  1174. procedure SetValues; override;
  1175. end;
  1176. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1177. TfdR3G3B2ub1 = class(TfdUniversalUB1)
  1178. procedure SetValues; override;
  1179. end;
  1180. TfdRGBX4us1 = class(TfdUniversalUS1)
  1181. procedure SetValues; override;
  1182. end;
  1183. TfdXRGB4us1 = class(TfdUniversalUS1)
  1184. procedure SetValues; override;
  1185. end;
  1186. TfdR5G6B5us1 = class(TfdUniversalUS1)
  1187. procedure SetValues; override;
  1188. end;
  1189. TfdRGB5X1us1 = class(TfdUniversalUS1)
  1190. procedure SetValues; override;
  1191. end;
  1192. TfdX1RGB5us1 = class(TfdUniversalUS1)
  1193. procedure SetValues; override;
  1194. end;
  1195. TfdRGB8ub3 = class(TfdRGBub3)
  1196. procedure SetValues; override;
  1197. end;
  1198. TfdRGBX8ui1 = class(TfdUniversalUI1)
  1199. procedure SetValues; override;
  1200. end;
  1201. TfdXRGB8ui1 = class(TfdUniversalUI1)
  1202. procedure SetValues; override;
  1203. end;
  1204. TfdRGB10X2ui1 = class(TfdUniversalUI1)
  1205. procedure SetValues; override;
  1206. end;
  1207. TfdX2RGB10ui1 = class(TfdUniversalUI1)
  1208. procedure SetValues; override;
  1209. end;
  1210. TfdRGB16us3 = class(TfdRGBus3)
  1211. procedure SetValues; override;
  1212. end;
  1213. TfdRGBA4us1 = class(TfdUniversalUS1)
  1214. procedure SetValues; override;
  1215. end;
  1216. TfdARGB4us1 = class(TfdUniversalUS1)
  1217. procedure SetValues; override;
  1218. end;
  1219. TfdRGB5A1us1 = class(TfdUniversalUS1)
  1220. procedure SetValues; override;
  1221. end;
  1222. TfdA1RGB5us1 = class(TfdUniversalUS1)
  1223. procedure SetValues; override;
  1224. end;
  1225. TfdRGBA8ui1 = class(TfdUniversalUI1)
  1226. procedure SetValues; override;
  1227. end;
  1228. TfdARGB8ui1 = class(TfdUniversalUI1)
  1229. procedure SetValues; override;
  1230. end;
  1231. TfdRGBA8ub4 = class(TfdRGBAub4)
  1232. procedure SetValues; override;
  1233. end;
  1234. TfdRGB10A2ui1 = class(TfdUniversalUI1)
  1235. procedure SetValues; override;
  1236. end;
  1237. TfdA2RGB10ui1 = class(TfdUniversalUI1)
  1238. procedure SetValues; override;
  1239. end;
  1240. TfdRGBA16us4 = class(TfdRGBAus4)
  1241. procedure SetValues; override;
  1242. end;
  1243. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1244. TfdBGRX4us1 = class(TfdUniversalUS1)
  1245. procedure SetValues; override;
  1246. end;
  1247. TfdXBGR4us1 = class(TfdUniversalUS1)
  1248. procedure SetValues; override;
  1249. end;
  1250. TfdB5G6R5us1 = class(TfdUniversalUS1)
  1251. procedure SetValues; override;
  1252. end;
  1253. TfdBGR5X1us1 = class(TfdUniversalUS1)
  1254. procedure SetValues; override;
  1255. end;
  1256. TfdX1BGR5us1 = class(TfdUniversalUS1)
  1257. procedure SetValues; override;
  1258. end;
  1259. TfdBGR8ub3 = class(TfdBGRub3)
  1260. procedure SetValues; override;
  1261. end;
  1262. TfdBGRX8ui1 = class(TfdUniversalUI1)
  1263. procedure SetValues; override;
  1264. end;
  1265. TfdXBGR8ui1 = class(TfdUniversalUI1)
  1266. procedure SetValues; override;
  1267. end;
  1268. TfdBGR10X2ui1 = class(TfdUniversalUI1)
  1269. procedure SetValues; override;
  1270. end;
  1271. TfdX2BGR10ui1 = class(TfdUniversalUI1)
  1272. procedure SetValues; override;
  1273. end;
  1274. TfdBGR16us3 = class(TfdBGRus3)
  1275. procedure SetValues; override;
  1276. end;
  1277. TfdBGRA4us1 = class(TfdUniversalUS1)
  1278. procedure SetValues; override;
  1279. end;
  1280. TfdABGR4us1 = class(TfdUniversalUS1)
  1281. procedure SetValues; override;
  1282. end;
  1283. TfdBGR5A1us1 = class(TfdUniversalUS1)
  1284. procedure SetValues; override;
  1285. end;
  1286. TfdA1BGR5us1 = class(TfdUniversalUS1)
  1287. procedure SetValues; override;
  1288. end;
  1289. TfdBGRA8ui1 = class(TfdUniversalUI1)
  1290. procedure SetValues; override;
  1291. end;
  1292. TfdABGR8ui1 = class(TfdUniversalUI1)
  1293. procedure SetValues; override;
  1294. end;
  1295. TfdBGRA8ub4 = class(TfdBGRAub4)
  1296. procedure SetValues; override;
  1297. end;
  1298. TfdBGR10A2ui1 = class(TfdUniversalUI1)
  1299. procedure SetValues; override;
  1300. end;
  1301. TfdA2BGR10ui1 = class(TfdUniversalUI1)
  1302. procedure SetValues; override;
  1303. end;
  1304. TfdBGRA16us4 = class(TfdBGRAus4)
  1305. procedure SetValues; override;
  1306. end;
  1307. TfdDepth16us1 = class(TfdDepthUS1)
  1308. procedure SetValues; override;
  1309. end;
  1310. TfdDepth24ui1 = class(TfdDepthUI1)
  1311. procedure SetValues; override;
  1312. end;
  1313. TfdDepth32ui1 = class(TfdDepthUI1)
  1314. procedure SetValues; override;
  1315. end;
  1316. TfdS3tcDtx1RGBA = class(TFormatDescriptor)
  1317. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1318. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1319. procedure SetValues; override;
  1320. end;
  1321. TfdS3tcDtx3RGBA = class(TFormatDescriptor)
  1322. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1323. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1324. procedure SetValues; override;
  1325. end;
  1326. TfdS3tcDtx5RGBA = class(TFormatDescriptor)
  1327. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1328. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1329. procedure SetValues; override;
  1330. end;
  1331. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1332. TbmpBitfieldFormat = class(TFormatDescriptor)
  1333. public
  1334. procedure SetCustomValues(const aBPP: Integer; aMask: TglBitmapRec4ul); overload;
  1335. procedure SetCustomValues(const aBBP: Integer; const aPrec, aShift: TglBitmapRec4ub); overload;
  1336. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1337. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1338. end;
  1339. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1340. TbmpColorTableEnty = packed record
  1341. b, g, r, a: Byte;
  1342. end;
  1343. TbmpColorTable = array of TbmpColorTableEnty;
  1344. TbmpColorTableFormat = class(TFormatDescriptor)
  1345. private
  1346. fColorTable: TbmpColorTable;
  1347. protected
  1348. procedure SetValues; override;
  1349. public
  1350. property ColorTable: TbmpColorTable read fColorTable write fColorTable;
  1351. procedure SetCustomValues(const aFormat: TglBitmapFormat; const aBPP: Integer; const aPrec, aShift: TglBitmapRec4ub); overload;
  1352. procedure CalcValues;
  1353. procedure CreateColorTable;
  1354. function CreateMappingData: Pointer; override;
  1355. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1356. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1357. destructor Destroy; override;
  1358. end;
  1359. const
  1360. LUMINANCE_WEIGHT_R = 0.30;
  1361. LUMINANCE_WEIGHT_G = 0.59;
  1362. LUMINANCE_WEIGHT_B = 0.11;
  1363. ALPHA_WEIGHT_R = 0.30;
  1364. ALPHA_WEIGHT_G = 0.59;
  1365. ALPHA_WEIGHT_B = 0.11;
  1366. DEPTH_WEIGHT_R = 0.333333333;
  1367. DEPTH_WEIGHT_G = 0.333333333;
  1368. DEPTH_WEIGHT_B = 0.333333333;
  1369. FORMAT_DESCRIPTOR_CLASSES: array[TglBitmapFormat] of TFormatDescriptorClass = (
  1370. TfdEmpty,
  1371. TfdAlpha4ub1,
  1372. TfdAlpha8ub1,
  1373. TfdAlpha16us1,
  1374. TfdLuminance4ub1,
  1375. TfdLuminance8ub1,
  1376. TfdLuminance16us1,
  1377. TfdLuminance4Alpha4ub2,
  1378. TfdLuminance6Alpha2ub2,
  1379. TfdLuminance8Alpha8ub2,
  1380. TfdLuminance12Alpha4us2,
  1381. TfdLuminance16Alpha16us2,
  1382. TfdR3G3B2ub1,
  1383. TfdRGBX4us1,
  1384. TfdXRGB4us1,
  1385. TfdR5G6B5us1,
  1386. TfdRGB5X1us1,
  1387. TfdX1RGB5us1,
  1388. TfdRGB8ub3,
  1389. TfdRGBX8ui1,
  1390. TfdXRGB8ui1,
  1391. TfdRGB10X2ui1,
  1392. TfdX2RGB10ui1,
  1393. TfdRGB16us3,
  1394. TfdRGBA4us1,
  1395. TfdARGB4us1,
  1396. TfdRGB5A1us1,
  1397. TfdA1RGB5us1,
  1398. TfdRGBA8ub4,
  1399. TfdRGBA8ui1,
  1400. TfdARGB8ui1,
  1401. TfdRGB10A2ui1,
  1402. TfdA2RGB10ui1,
  1403. TfdRGBA16us4,
  1404. TfdBGRX4us1,
  1405. TfdXBGR4us1,
  1406. TfdB5G6R5us1,
  1407. TfdBGR5X1us1,
  1408. TfdX1BGR5us1,
  1409. TfdBGR8ub3,
  1410. TfdBGRX8ui1,
  1411. TfdXBGR8ui1,
  1412. TfdBGR10X2ui1,
  1413. TfdX2BGR10ui1,
  1414. TfdBGR16us3,
  1415. TfdBGRA4us1,
  1416. TfdABGR4us1,
  1417. TfdBGR5A1us1,
  1418. TfdA1BGR5us1,
  1419. TfdBGRA8ub4,
  1420. TfdBGRA8ui1,
  1421. TfdABGR8ui1,
  1422. TfdBGR10A2ui1,
  1423. TfdA2BGR10ui1,
  1424. TfdBGRA16us4,
  1425. TfdDepth16us1,
  1426. TfdDepth24ui1,
  1427. TfdDepth32ui1,
  1428. TfdS3tcDtx1RGBA,
  1429. TfdS3tcDtx3RGBA,
  1430. TfdS3tcDtx5RGBA
  1431. );
  1432. var
  1433. FormatDescriptorCS: TCriticalSection;
  1434. FormatDescriptors: array[TglBitmapFormat] of TFormatDescriptor;
  1435. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1436. constructor EglBitmapUnsupportedFormat.Create(const aFormat: TglBitmapFormat);
  1437. begin
  1438. inherited Create('unsupported format: ' + GetEnumName(TypeInfo(TglBitmapFormat), Integer(aFormat)));
  1439. end;
  1440. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1441. constructor EglBitmapUnsupportedFormat.Create(const aMsg: String; const aFormat: TglBitmapFormat);
  1442. begin
  1443. inherited Create(aMsg + GetEnumName(TypeInfo(TglBitmapFormat), Integer(aFormat)));
  1444. end;
  1445. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1446. function glBitmapSize(X: Integer; Y: Integer): TglBitmapSize;
  1447. begin
  1448. result.Fields := [];
  1449. if (X >= 0) then
  1450. result.Fields := result.Fields + [ffX];
  1451. if (Y >= 0) then
  1452. result.Fields := result.Fields + [ffY];
  1453. result.X := Max(0, X);
  1454. result.Y := Max(0, Y);
  1455. end;
  1456. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1457. function glBitmapPosition(X: Integer; Y: Integer): TglBitmapPixelPosition;
  1458. begin
  1459. result := glBitmapSize(X, Y);
  1460. end;
  1461. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1462. function glBitmapRec4ub(const r, g, b, a: Byte): TglBitmapRec4ub;
  1463. begin
  1464. result.r := r;
  1465. result.g := g;
  1466. result.b := b;
  1467. result.a := a;
  1468. end;
  1469. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1470. function glBitmapRec4ui(const r, g, b, a: Cardinal): TglBitmapRec4ui;
  1471. begin
  1472. result.r := r;
  1473. result.g := g;
  1474. result.b := b;
  1475. result.a := a;
  1476. end;
  1477. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1478. function glBitmapRec4ul(const r, g, b, a: QWord): TglBitmapRec4ul;
  1479. begin
  1480. result.r := r;
  1481. result.g := g;
  1482. result.b := b;
  1483. result.a := a;
  1484. end;
  1485. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1486. function glBitmapRec4ubCompare(const r1, r2: TglBitmapRec4ub): Boolean;
  1487. var
  1488. i: Integer;
  1489. begin
  1490. result := false;
  1491. for i := 0 to high(r1.arr) do
  1492. if (r1.arr[i] <> r2.arr[i]) then
  1493. exit;
  1494. result := true;
  1495. end;
  1496. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1497. function glBitmapRec4uiCompare(const r1, r2: TglBitmapRec4ui): Boolean;
  1498. var
  1499. i: Integer;
  1500. begin
  1501. result := false;
  1502. for i := 0 to high(r1.arr) do
  1503. if (r1.arr[i] <> r2.arr[i]) then
  1504. exit;
  1505. result := true;
  1506. end;
  1507. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1508. function glBitmapCreateTestData(const aFormat: TglBitmapFormat): TglBitmapData;
  1509. var
  1510. desc: TFormatDescriptor;
  1511. p, tmp: PByte;
  1512. x, y, i: Integer;
  1513. md: Pointer;
  1514. px: TglBitmapPixelData;
  1515. begin
  1516. result := nil;
  1517. desc := TFormatDescriptor.Get(aFormat);
  1518. if (desc.IsCompressed) or (desc.glFormat = 0) then
  1519. exit;
  1520. p := GetMemory(ceil(25 * desc.BytesPerPixel)); // 5 x 5 pixel
  1521. md := desc.CreateMappingData;
  1522. try
  1523. tmp := p;
  1524. desc.PreparePixel(px);
  1525. for y := 0 to 4 do
  1526. for x := 0 to 4 do begin
  1527. px.Data := glBitmapRec4ui(0, 0, 0, 0);
  1528. for i := 0 to 3 do begin
  1529. if ((y < 3) and (y = i)) or
  1530. ((y = 3) and (i < 3)) or
  1531. ((y = 4) and (i = 3))
  1532. then
  1533. px.Data.arr[i] := Trunc(px.Range.arr[i] / 4 * x)
  1534. else if ((y < 4) and (i = 3)) or
  1535. ((y = 4) and (i < 3))
  1536. then
  1537. px.Data.arr[i] := px.Range.arr[i]
  1538. else
  1539. px.Data.arr[i] := 0; //px.Range.arr[i];
  1540. end;
  1541. desc.Map(px, tmp, md);
  1542. end;
  1543. finally
  1544. desc.FreeMappingData(md);
  1545. end;
  1546. result := TglBitmapData.Create(glBitmapPosition(5, 5), aFormat, p);
  1547. end;
  1548. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1549. function glBitmapShiftRec(const r, g, b, a: Byte): TglBitmapRec4ub;
  1550. begin
  1551. result.r := r;
  1552. result.g := g;
  1553. result.b := b;
  1554. result.a := a;
  1555. end;
  1556. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1557. function FormatGetSupportedFiles(const aFormat: TglBitmapFormat): TglBitmapFileTypes;
  1558. begin
  1559. result := [];
  1560. if (aFormat in [
  1561. //8bpp
  1562. tfAlpha4ub1, tfAlpha8ub1,
  1563. tfLuminance4ub1, tfLuminance8ub1, tfR3G3B2ub1,
  1564. //16bpp
  1565. tfLuminance4Alpha4ub2, tfLuminance6Alpha2ub2, tfLuminance8Alpha8ub2,
  1566. tfRGBX4us1, tfXRGB4us1, tfRGB5X1us1, tfX1RGB5us1, tfR5G6B5us1, tfRGB5A1us1, tfA1RGB5us1, tfRGBA4us1, tfARGB4us1,
  1567. tfBGRX4us1, tfXBGR4us1, tfBGR5X1us1, tfX1BGR5us1, tfB5G6R5us1, tfBGR5A1us1, tfA1BGR5us1, tfBGRA4us1, tfABGR4us1,
  1568. //24bpp
  1569. tfBGR8ub3, tfRGB8ub3,
  1570. //32bpp
  1571. tfRGBX8ui1, tfXRGB8ui1, tfRGB10X2ui1, tfX2RGB10ui1, tfRGBA8ui1, tfARGB8ui1, tfRGBA8ub4, tfRGB10A2ui1, tfA2RGB10ui1,
  1572. tfBGRX8ui1, tfXBGR8ui1, tfBGR10X2ui1, tfX2BGR10ui1, tfBGRA8ui1, tfABGR8ui1, tfBGRA8ub4, tfBGR10A2ui1, tfA2BGR10ui1])
  1573. then
  1574. result := result + [ ftBMP ];
  1575. if (aFormat in [
  1576. //8bbp
  1577. tfAlpha4ub1, tfAlpha8ub1, tfLuminance4ub1, tfLuminance8ub1,
  1578. //16bbp
  1579. tfAlpha16us1, tfLuminance16us1,
  1580. tfLuminance4Alpha4ub2, tfLuminance6Alpha2ub2, tfLuminance8Alpha8ub2,
  1581. tfX1RGB5us1, tfARGB4us1, tfA1RGB5us1, tfDepth16us1,
  1582. //24bbp
  1583. tfBGR8ub3,
  1584. //32bbp
  1585. tfX2RGB10ui1, tfARGB8ui1, tfBGRA8ub4, tfA2RGB10ui1,
  1586. tfDepth24ui1, tfDepth32ui1])
  1587. then
  1588. result := result + [ftTGA];
  1589. if not (aFormat in [tfEmpty, tfRGB16us3, tfBGR16us3]) then
  1590. result := result + [ftDDS];
  1591. {$IFDEF GLB_SUPPORT_PNG_WRITE}
  1592. if aFormat in [
  1593. tfAlpha8ub1, tfLuminance8ub1, tfLuminance8Alpha8ub2,
  1594. tfRGB8ub3, tfRGBA8ui1,
  1595. tfBGR8ub3, tfBGRA8ui1] then
  1596. result := result + [ftPNG];
  1597. {$ENDIF}
  1598. {$IFDEF GLB_SUPPORT_JPEG_WRITE}
  1599. if aFormat in [tfAlpha8ub1, tfLuminance8ub1, tfRGB8ub3, tfBGR8ub3] then
  1600. result := result + [ftJPEG];
  1601. {$ENDIF}
  1602. end;
  1603. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1604. function IsPowerOfTwo(aNumber: Integer): Boolean;
  1605. begin
  1606. while (aNumber and 1) = 0 do
  1607. aNumber := aNumber shr 1;
  1608. result := aNumber = 1;
  1609. end;
  1610. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1611. function GetTopMostBit(aBitSet: QWord): Integer;
  1612. begin
  1613. result := 0;
  1614. while aBitSet > 0 do begin
  1615. inc(result);
  1616. aBitSet := aBitSet shr 1;
  1617. end;
  1618. end;
  1619. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1620. function CountSetBits(aBitSet: QWord): Integer;
  1621. begin
  1622. result := 0;
  1623. while aBitSet > 0 do begin
  1624. if (aBitSet and 1) = 1 then
  1625. inc(result);
  1626. aBitSet := aBitSet shr 1;
  1627. end;
  1628. end;
  1629. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1630. function LuminanceWeight(const aPixel: TglBitmapPixelData): Cardinal;
  1631. begin
  1632. result := Trunc(
  1633. LUMINANCE_WEIGHT_R * aPixel.Data.r +
  1634. LUMINANCE_WEIGHT_G * aPixel.Data.g +
  1635. LUMINANCE_WEIGHT_B * aPixel.Data.b);
  1636. end;
  1637. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1638. function DepthWeight(const aPixel: TglBitmapPixelData): Cardinal;
  1639. begin
  1640. result := Trunc(
  1641. DEPTH_WEIGHT_R * aPixel.Data.r +
  1642. DEPTH_WEIGHT_G * aPixel.Data.g +
  1643. DEPTH_WEIGHT_B * aPixel.Data.b);
  1644. end;
  1645. {$IFDEF GLB_SDL_IMAGE}
  1646. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1647. // SDL Image Helper /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1648. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1649. function glBitmapRWseek(context: PSDL_RWops; offset: Integer; whence: Integer): Integer; cdecl;
  1650. begin
  1651. result := TStream(context^.unknown.data1).Seek(offset, whence);
  1652. end;
  1653. function glBitmapRWread(context: PSDL_RWops; Ptr: Pointer; size: Integer; maxnum : Integer): Integer; cdecl;
  1654. begin
  1655. result := TStream(context^.unknown.data1).Read(Ptr^, size * maxnum);
  1656. end;
  1657. function glBitmapRWwrite(context: PSDL_RWops; Ptr: Pointer; size: Integer; num: Integer): Integer; cdecl;
  1658. begin
  1659. result := TStream(context^.unknown.data1).Write(Ptr^, size * num);
  1660. end;
  1661. function glBitmapRWclose(context: PSDL_RWops): Integer; cdecl;
  1662. begin
  1663. result := 0;
  1664. end;
  1665. function glBitmapCreateRWops(Stream: TStream): PSDL_RWops;
  1666. begin
  1667. result := SDL_AllocRW;
  1668. if result = nil then
  1669. raise EglBitmap.Create('glBitmapCreateRWops - SDL_AllocRW failed.');
  1670. result^.seek := glBitmapRWseek;
  1671. result^.read := glBitmapRWread;
  1672. result^.write := glBitmapRWwrite;
  1673. result^.close := glBitmapRWclose;
  1674. result^.unknown.data1 := Stream;
  1675. end;
  1676. {$ENDIF}
  1677. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1678. procedure glBitmapSetDefaultDeleteTextureOnFree(const aDeleteTextureOnFree: Boolean);
  1679. begin
  1680. glBitmapDefaultDeleteTextureOnFree := aDeleteTextureOnFree;
  1681. end;
  1682. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1683. procedure glBitmapSetDefaultFreeDataAfterGenTexture(const aFreeData: Boolean);
  1684. begin
  1685. glBitmapDefaultFreeDataAfterGenTextures := aFreeData;
  1686. end;
  1687. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1688. procedure glBitmapSetDefaultMipmap(const aValue: TglBitmapMipMap);
  1689. begin
  1690. glBitmapDefaultMipmap := aValue;
  1691. end;
  1692. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1693. procedure glBitmapSetDefaultFormat(const aFormat: TglBitmapFormat);
  1694. begin
  1695. glBitmapDefaultFormat := aFormat;
  1696. end;
  1697. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1698. procedure glBitmapSetDefaultFilter(const aMin, aMag: Integer);
  1699. begin
  1700. glBitmapDefaultFilterMin := aMin;
  1701. glBitmapDefaultFilterMag := aMag;
  1702. end;
  1703. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1704. procedure glBitmapSetDefaultWrap(const S: Cardinal = GL_CLAMP_TO_EDGE; const T: Cardinal = GL_CLAMP_TO_EDGE; const R: Cardinal = GL_CLAMP_TO_EDGE);
  1705. begin
  1706. glBitmapDefaultWrapS := S;
  1707. glBitmapDefaultWrapT := T;
  1708. glBitmapDefaultWrapR := R;
  1709. end;
  1710. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1711. {$IF NOT DEFINED(OPENGL_ES) OR DEFINED(OPENGL_ES_3_0)}
  1712. procedure glBitmapSetDefaultSwizzle(const r: GLenum = GL_RED; g: GLenum = GL_GREEN; b: GLenum = GL_BLUE; a: GLenum = GL_ALPHA);
  1713. begin
  1714. glDefaultSwizzle[0] := r;
  1715. glDefaultSwizzle[1] := g;
  1716. glDefaultSwizzle[2] := b;
  1717. glDefaultSwizzle[3] := a;
  1718. end;
  1719. {$IFEND}
  1720. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1721. function glBitmapGetDefaultDeleteTextureOnFree: Boolean;
  1722. begin
  1723. result := glBitmapDefaultDeleteTextureOnFree;
  1724. end;
  1725. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1726. function glBitmapGetDefaultFreeDataAfterGenTexture: Boolean;
  1727. begin
  1728. result := glBitmapDefaultFreeDataAfterGenTextures;
  1729. end;
  1730. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1731. function glBitmapGetDefaultMipmap: TglBitmapMipMap;
  1732. begin
  1733. result := glBitmapDefaultMipmap;
  1734. end;
  1735. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1736. function glBitmapGetDefaultFormat: TglBitmapFormat;
  1737. begin
  1738. result := glBitmapDefaultFormat;
  1739. end;
  1740. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1741. procedure glBitmapGetDefaultFilter(var aMin, aMag: Cardinal);
  1742. begin
  1743. aMin := glBitmapDefaultFilterMin;
  1744. aMag := glBitmapDefaultFilterMag;
  1745. end;
  1746. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1747. procedure glBitmapGetDefaultTextureWrap(var S, T, R: Cardinal);
  1748. begin
  1749. S := glBitmapDefaultWrapS;
  1750. T := glBitmapDefaultWrapT;
  1751. R := glBitmapDefaultWrapR;
  1752. end;
  1753. {$IF NOT DEFINED(OPENGL_ES) OR DEFINED(OPENGL_ES_3_0)}
  1754. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1755. procedure glBitmapGetDefaultSwizzle(var r, g, b, a: GLenum);
  1756. begin
  1757. r := glDefaultSwizzle[0];
  1758. g := glDefaultSwizzle[1];
  1759. b := glDefaultSwizzle[2];
  1760. a := glDefaultSwizzle[3];
  1761. end;
  1762. {$IFEND}
  1763. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1764. //TFormatDescriptor///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1765. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1766. function TFormatDescriptor.CreateMappingData: Pointer;
  1767. begin
  1768. result := nil;
  1769. end;
  1770. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1771. procedure TFormatDescriptor.FreeMappingData(var aMappingData: Pointer);
  1772. begin
  1773. //DUMMY
  1774. end;
  1775. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1776. function TFormatDescriptor.IsEmpty: Boolean;
  1777. begin
  1778. result := (fFormat = tfEmpty);
  1779. end;
  1780. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1781. function TFormatDescriptor.MaskMatch(const aMask: TglBitmapRec4ul): Boolean;
  1782. var
  1783. i: Integer;
  1784. m: TglBitmapRec4ul;
  1785. begin
  1786. result := false;
  1787. if (aMask.r = 0) and (aMask.g = 0) and (aMask.b = 0) and (aMask.a = 0) then
  1788. raise EglBitmap.Create('FormatCheckFormat - All Masks are 0');
  1789. m := Mask;
  1790. for i := 0 to 3 do
  1791. if (aMask.arr[i] <> m.arr[i]) then
  1792. exit;
  1793. result := true;
  1794. end;
  1795. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1796. procedure TFormatDescriptor.PreparePixel(out aPixel: TglBitmapPixelData);
  1797. begin
  1798. FillChar(aPixel{%H-}, SizeOf(aPixel), 0);
  1799. aPixel.Data := Range;
  1800. aPixel.Format := fFormat;
  1801. aPixel.Range := Range;
  1802. end;
  1803. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1804. constructor TFormatDescriptor.Create;
  1805. begin
  1806. inherited Create;
  1807. end;
  1808. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1809. //TfdAlpha_UB1////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1810. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1811. procedure TfdAlphaUB1.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  1812. begin
  1813. aData^ := aPixel.Data.a;
  1814. inc(aData);
  1815. end;
  1816. procedure TfdAlphaUB1.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  1817. begin
  1818. aPixel.Data.r := 0;
  1819. aPixel.Data.g := 0;
  1820. aPixel.Data.b := 0;
  1821. aPixel.Data.a := aData^;
  1822. inc(aData);
  1823. end;
  1824. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1825. //TfdLuminance_UB1////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1826. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1827. procedure TfdLuminanceUB1.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  1828. begin
  1829. aData^ := LuminanceWeight(aPixel);
  1830. inc(aData);
  1831. end;
  1832. procedure TfdLuminanceUB1.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  1833. begin
  1834. aPixel.Data.r := aData^;
  1835. aPixel.Data.g := aData^;
  1836. aPixel.Data.b := aData^;
  1837. aPixel.Data.a := 0;
  1838. inc(aData);
  1839. end;
  1840. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1841. //TfdUniversal_UB1////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1842. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1843. procedure TfdUniversalUB1.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  1844. var
  1845. i: Integer;
  1846. begin
  1847. aData^ := 0;
  1848. for i := 0 to 3 do
  1849. if (Range.arr[i] > 0) then
  1850. aData^ := aData^ or ((aPixel.Data.arr[i] and Range.arr[i]) shl fShift.arr[i]);
  1851. inc(aData);
  1852. end;
  1853. procedure TfdUniversalUB1.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  1854. var
  1855. i: Integer;
  1856. begin
  1857. for i := 0 to 3 do
  1858. aPixel.Data.arr[i] := (aData^ shr fShift.arr[i]) and Range.arr[i];
  1859. inc(aData);
  1860. end;
  1861. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1862. //TfdLuminanceAlpha_UB2///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1863. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1864. procedure TfdLuminanceAlphaUB2.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  1865. begin
  1866. inherited Map(aPixel, aData, aMapData);
  1867. aData^ := aPixel.Data.a;
  1868. inc(aData);
  1869. end;
  1870. procedure TfdLuminanceAlphaUB2.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  1871. begin
  1872. inherited Unmap(aData, aPixel, aMapData);
  1873. aPixel.Data.a := aData^;
  1874. inc(aData);
  1875. end;
  1876. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1877. //TfdRGB_UB3//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1878. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1879. procedure TfdRGBub3.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  1880. begin
  1881. aData^ := aPixel.Data.r;
  1882. inc(aData);
  1883. aData^ := aPixel.Data.g;
  1884. inc(aData);
  1885. aData^ := aPixel.Data.b;
  1886. inc(aData);
  1887. end;
  1888. procedure TfdRGBub3.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  1889. begin
  1890. aPixel.Data.r := aData^;
  1891. inc(aData);
  1892. aPixel.Data.g := aData^;
  1893. inc(aData);
  1894. aPixel.Data.b := aData^;
  1895. inc(aData);
  1896. aPixel.Data.a := 0;
  1897. end;
  1898. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1899. //TfdBGR_UB3//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1900. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1901. procedure TfdBGRub3.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  1902. begin
  1903. aData^ := aPixel.Data.b;
  1904. inc(aData);
  1905. aData^ := aPixel.Data.g;
  1906. inc(aData);
  1907. aData^ := aPixel.Data.r;
  1908. inc(aData);
  1909. end;
  1910. procedure TfdBGRub3.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  1911. begin
  1912. aPixel.Data.b := aData^;
  1913. inc(aData);
  1914. aPixel.Data.g := aData^;
  1915. inc(aData);
  1916. aPixel.Data.r := aData^;
  1917. inc(aData);
  1918. aPixel.Data.a := 0;
  1919. end;
  1920. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1921. //TfdRGBA_UB4//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1922. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1923. procedure TfdRGBAub4.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  1924. begin
  1925. inherited Map(aPixel, aData, aMapData);
  1926. aData^ := aPixel.Data.a;
  1927. inc(aData);
  1928. end;
  1929. procedure TfdRGBAub4.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  1930. begin
  1931. inherited Unmap(aData, aPixel, aMapData);
  1932. aPixel.Data.a := aData^;
  1933. inc(aData);
  1934. end;
  1935. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1936. //TfdBGRA_UB4//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1937. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1938. procedure TfdBGRAub4.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  1939. begin
  1940. inherited Map(aPixel, aData, aMapData);
  1941. aData^ := aPixel.Data.a;
  1942. inc(aData);
  1943. end;
  1944. procedure TfdBGRAub4.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  1945. begin
  1946. inherited Unmap(aData, aPixel, aMapData);
  1947. aPixel.Data.a := aData^;
  1948. inc(aData);
  1949. end;
  1950. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1951. //TfdAlpha_US1////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1952. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1953. procedure TfdAlphaUS1.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  1954. begin
  1955. PWord(aData)^ := aPixel.Data.a;
  1956. inc(aData, 2);
  1957. end;
  1958. procedure TfdAlphaUS1.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  1959. begin
  1960. aPixel.Data.r := 0;
  1961. aPixel.Data.g := 0;
  1962. aPixel.Data.b := 0;
  1963. aPixel.Data.a := PWord(aData)^;
  1964. inc(aData, 2);
  1965. end;
  1966. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1967. //TfdLuminance_US1////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1968. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1969. procedure TfdLuminanceUS1.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  1970. begin
  1971. PWord(aData)^ := LuminanceWeight(aPixel);
  1972. inc(aData, 2);
  1973. end;
  1974. procedure TfdLuminanceUS1.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  1975. begin
  1976. aPixel.Data.r := PWord(aData)^;
  1977. aPixel.Data.g := PWord(aData)^;
  1978. aPixel.Data.b := PWord(aData)^;
  1979. aPixel.Data.a := 0;
  1980. inc(aData, 2);
  1981. end;
  1982. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1983. //TfdUniversal_US1////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1984. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1985. procedure TfdUniversalUS1.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  1986. var
  1987. i: Integer;
  1988. begin
  1989. PWord(aData)^ := 0;
  1990. for i := 0 to 3 do
  1991. if (Range.arr[i] > 0) then
  1992. PWord(aData)^ := PWord(aData)^ or ((aPixel.Data.arr[i] and Range.arr[i]) shl fShift.arr[i]);
  1993. inc(aData, 2);
  1994. end;
  1995. procedure TfdUniversalUS1.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  1996. var
  1997. i: Integer;
  1998. begin
  1999. for i := 0 to 3 do
  2000. aPixel.Data.arr[i] := (PWord(aData)^ shr fShift.arr[i]) and Range.arr[i];
  2001. inc(aData, 2);
  2002. end;
  2003. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2004. //TfdDepth_US1////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2005. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2006. procedure TfdDepthUS1.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  2007. begin
  2008. PWord(aData)^ := DepthWeight(aPixel);
  2009. inc(aData, 2);
  2010. end;
  2011. procedure TfdDepthUS1.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  2012. begin
  2013. aPixel.Data.r := PWord(aData)^;
  2014. aPixel.Data.g := PWord(aData)^;
  2015. aPixel.Data.b := PWord(aData)^;
  2016. aPixel.Data.a := PWord(aData)^;;
  2017. inc(aData, 2);
  2018. end;
  2019. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2020. //TfdLuminanceAlpha_US2///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2021. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2022. procedure TfdLuminanceAlphaUS2.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  2023. begin
  2024. inherited Map(aPixel, aData, aMapData);
  2025. PWord(aData)^ := aPixel.Data.a;
  2026. inc(aData, 2);
  2027. end;
  2028. procedure TfdLuminanceAlphaUS2.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  2029. begin
  2030. inherited Unmap(aData, aPixel, aMapData);
  2031. aPixel.Data.a := PWord(aData)^;
  2032. inc(aData, 2);
  2033. end;
  2034. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2035. //TfdRGB_US3//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2036. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2037. procedure TfdRGBus3.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  2038. begin
  2039. PWord(aData)^ := aPixel.Data.r;
  2040. inc(aData, 2);
  2041. PWord(aData)^ := aPixel.Data.g;
  2042. inc(aData, 2);
  2043. PWord(aData)^ := aPixel.Data.b;
  2044. inc(aData, 2);
  2045. end;
  2046. procedure TfdRGBus3.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  2047. begin
  2048. aPixel.Data.r := PWord(aData)^;
  2049. inc(aData, 2);
  2050. aPixel.Data.g := PWord(aData)^;
  2051. inc(aData, 2);
  2052. aPixel.Data.b := PWord(aData)^;
  2053. inc(aData, 2);
  2054. aPixel.Data.a := 0;
  2055. end;
  2056. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2057. //TfdBGR_US3//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2058. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2059. procedure TfdBGRus3.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  2060. begin
  2061. PWord(aData)^ := aPixel.Data.b;
  2062. inc(aData, 2);
  2063. PWord(aData)^ := aPixel.Data.g;
  2064. inc(aData, 2);
  2065. PWord(aData)^ := aPixel.Data.r;
  2066. inc(aData, 2);
  2067. end;
  2068. procedure TfdBGRus3.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  2069. begin
  2070. aPixel.Data.b := PWord(aData)^;
  2071. inc(aData, 2);
  2072. aPixel.Data.g := PWord(aData)^;
  2073. inc(aData, 2);
  2074. aPixel.Data.r := PWord(aData)^;
  2075. inc(aData, 2);
  2076. aPixel.Data.a := 0;
  2077. end;
  2078. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2079. //TfdRGBA_US4/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2080. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2081. procedure TfdRGBAus4.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  2082. begin
  2083. inherited Map(aPixel, aData, aMapData);
  2084. PWord(aData)^ := aPixel.Data.a;
  2085. inc(aData, 2);
  2086. end;
  2087. procedure TfdRGBAus4.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  2088. begin
  2089. inherited Unmap(aData, aPixel, aMapData);
  2090. aPixel.Data.a := PWord(aData)^;
  2091. inc(aData, 2);
  2092. end;
  2093. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2094. //TfdARGB_US4/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2095. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2096. procedure TfdARGBus4.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  2097. begin
  2098. PWord(aData)^ := aPixel.Data.a;
  2099. inc(aData, 2);
  2100. inherited Map(aPixel, aData, aMapData);
  2101. end;
  2102. procedure TfdARGBus4.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  2103. begin
  2104. aPixel.Data.a := PWord(aData)^;
  2105. inc(aData, 2);
  2106. inherited Unmap(aData, aPixel, aMapData);
  2107. end;
  2108. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2109. //TfdBGRA_US4/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2110. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2111. procedure TfdBGRAus4.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  2112. begin
  2113. inherited Map(aPixel, aData, aMapData);
  2114. PWord(aData)^ := aPixel.Data.a;
  2115. inc(aData, 2);
  2116. end;
  2117. procedure TfdBGRAus4.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  2118. begin
  2119. inherited Unmap(aData, aPixel, aMapData);
  2120. aPixel.Data.a := PWord(aData)^;
  2121. inc(aData, 2);
  2122. end;
  2123. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2124. //TfdABGR_US4/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2125. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2126. procedure TfdABGRus4.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  2127. begin
  2128. PWord(aData)^ := aPixel.Data.a;
  2129. inc(aData, 2);
  2130. inherited Map(aPixel, aData, aMapData);
  2131. end;
  2132. procedure TfdABGRus4.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  2133. begin
  2134. aPixel.Data.a := PWord(aData)^;
  2135. inc(aData, 2);
  2136. inherited Unmap(aData, aPixel, aMapData);
  2137. end;
  2138. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2139. //TfdUniversal_UI1////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2140. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2141. procedure TfdUniversalUI1.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  2142. var
  2143. i: Integer;
  2144. begin
  2145. PCardinal(aData)^ := 0;
  2146. for i := 0 to 3 do
  2147. if (Range.arr[i] > 0) then
  2148. PCardinal(aData)^ := PCardinal(aData)^ or ((aPixel.Data.arr[i] and Range.arr[i]) shl fShift.arr[i]);
  2149. inc(aData, 4);
  2150. end;
  2151. procedure TfdUniversalUI1.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  2152. var
  2153. i: Integer;
  2154. begin
  2155. for i := 0 to 3 do
  2156. aPixel.Data.arr[i] := (PCardinal(aData)^ shr fShift.arr[i]) and Range.arr[i];
  2157. inc(aData, 2);
  2158. end;
  2159. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2160. //TfdDepth_UI1////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2161. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2162. procedure TfdDepthUI1.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  2163. begin
  2164. PCardinal(aData)^ := DepthWeight(aPixel);
  2165. inc(aData, 4);
  2166. end;
  2167. procedure TfdDepthUI1.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  2168. begin
  2169. aPixel.Data.r := PCardinal(aData)^;
  2170. aPixel.Data.g := PCardinal(aData)^;
  2171. aPixel.Data.b := PCardinal(aData)^;
  2172. aPixel.Data.a := PCardinal(aData)^;
  2173. inc(aData, 4);
  2174. end;
  2175. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2176. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2177. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2178. procedure TfdAlpha4ub1.SetValues;
  2179. begin
  2180. inherited SetValues;
  2181. fBitsPerPixel := 8;
  2182. fFormat := tfAlpha4ub1;
  2183. fWithAlpha := tfAlpha4ub1;
  2184. fPrecision := glBitmapRec4ub(0, 0, 0, 8);
  2185. fShift := glBitmapRec4ub(0, 0, 0, 0);
  2186. {$IFNDEF OPENGL_ES}
  2187. fOpenGLFormat := tfAlpha4ub1;
  2188. fglFormat := GL_ALPHA;
  2189. fglInternalFormat := GL_ALPHA4;
  2190. fglDataFormat := GL_UNSIGNED_BYTE;
  2191. {$ELSE}
  2192. fOpenGLFormat := tfAlpha8ub1;
  2193. {$ENDIF}
  2194. end;
  2195. procedure TfdAlpha8ub1.SetValues;
  2196. begin
  2197. inherited SetValues;
  2198. fBitsPerPixel := 8;
  2199. fFormat := tfAlpha8ub1;
  2200. fWithAlpha := tfAlpha8ub1;
  2201. fPrecision := glBitmapRec4ub(0, 0, 0, 8);
  2202. fShift := glBitmapRec4ub(0, 0, 0, 0);
  2203. fOpenGLFormat := tfAlpha8ub1;
  2204. fglFormat := GL_ALPHA;
  2205. fglInternalFormat := {$IFNDEF OPENGL_ES}GL_ALPHA8{$ELSE}GL_ALPHA{$ENDIF};
  2206. fglDataFormat := GL_UNSIGNED_BYTE;
  2207. end;
  2208. procedure TfdAlpha16us1.SetValues;
  2209. begin
  2210. inherited SetValues;
  2211. fBitsPerPixel := 16;
  2212. fFormat := tfAlpha16us1;
  2213. fWithAlpha := tfAlpha16us1;
  2214. fPrecision := glBitmapRec4ub(0, 0, 0, 16);
  2215. fShift := glBitmapRec4ub(0, 0, 0, 0);
  2216. {$IFNDEF OPENGL_ES}
  2217. fOpenGLFormat := tfAlpha16us1;
  2218. fglFormat := GL_ALPHA;
  2219. fglInternalFormat := GL_ALPHA16;
  2220. fglDataFormat := GL_UNSIGNED_SHORT;
  2221. {$ELSE}
  2222. fOpenGLFormat := tfAlpha8ub1;
  2223. {$ENDIF}
  2224. end;
  2225. procedure TfdLuminance4ub1.SetValues;
  2226. begin
  2227. inherited SetValues;
  2228. fBitsPerPixel := 8;
  2229. fFormat := tfLuminance4ub1;
  2230. fWithAlpha := tfLuminance4Alpha4ub2;
  2231. fWithoutAlpha := tfLuminance4ub1;
  2232. fPrecision := glBitmapRec4ub(8, 8, 8, 0);
  2233. fShift := glBitmapRec4ub(0, 0, 0, 0);
  2234. {$IFNDEF OPENGL_ES}
  2235. fOpenGLFormat := tfLuminance4ub1;
  2236. fglFormat := GL_LUMINANCE;
  2237. fglInternalFormat := GL_LUMINANCE4;
  2238. fglDataFormat := GL_UNSIGNED_BYTE;
  2239. {$ELSE}
  2240. fOpenGLFormat := tfLuminance8ub1;
  2241. {$ENDIF}
  2242. end;
  2243. procedure TfdLuminance8ub1.SetValues;
  2244. begin
  2245. inherited SetValues;
  2246. fBitsPerPixel := 8;
  2247. fFormat := tfLuminance8ub1;
  2248. fWithAlpha := tfLuminance8Alpha8ub2;
  2249. fWithoutAlpha := tfLuminance8ub1;
  2250. fOpenGLFormat := tfLuminance8ub1;
  2251. fPrecision := glBitmapRec4ub(8, 8, 8, 0);
  2252. fShift := glBitmapRec4ub(0, 0, 0, 0);
  2253. fglFormat := GL_LUMINANCE;
  2254. fglInternalFormat := {$IFNDEF OPENGL_ES}GL_LUMINANCE8{$ELSE}GL_LUMINANCE{$ENDIF};
  2255. fglDataFormat := GL_UNSIGNED_BYTE;
  2256. end;
  2257. procedure TfdLuminance16us1.SetValues;
  2258. begin
  2259. inherited SetValues;
  2260. fBitsPerPixel := 16;
  2261. fFormat := tfLuminance16us1;
  2262. fWithAlpha := tfLuminance16Alpha16us2;
  2263. fWithoutAlpha := tfLuminance16us1;
  2264. fPrecision := glBitmapRec4ub(16, 16, 16, 0);
  2265. fShift := glBitmapRec4ub( 0, 0, 0, 0);
  2266. {$IFNDEF OPENGL_ES}
  2267. fOpenGLFormat := tfLuminance16us1;
  2268. fglFormat := GL_LUMINANCE;
  2269. fglInternalFormat := GL_LUMINANCE16;
  2270. fglDataFormat := GL_UNSIGNED_SHORT;
  2271. {$ELSE}
  2272. fOpenGLFormat := tfLuminance8ub1;
  2273. {$ENDIF}
  2274. end;
  2275. procedure TfdLuminance4Alpha4ub2.SetValues;
  2276. begin
  2277. inherited SetValues;
  2278. fBitsPerPixel := 16;
  2279. fFormat := tfLuminance4Alpha4ub2;
  2280. fWithAlpha := tfLuminance4Alpha4ub2;
  2281. fWithoutAlpha := tfLuminance4ub1;
  2282. fPrecision := glBitmapRec4ub(8, 8, 8, 8);
  2283. fShift := glBitmapRec4ub(0, 0, 0, 8);
  2284. {$IFNDEF OPENGL_ES}
  2285. fOpenGLFormat := tfLuminance4Alpha4ub2;
  2286. fglFormat := GL_LUMINANCE_ALPHA;
  2287. fglInternalFormat := GL_LUMINANCE4_ALPHA4;
  2288. fglDataFormat := GL_UNSIGNED_BYTE;
  2289. {$ELSE}
  2290. fOpenGLFormat := tfLuminance8Alpha8ub2;
  2291. {$ENDIF}
  2292. end;
  2293. procedure TfdLuminance6Alpha2ub2.SetValues;
  2294. begin
  2295. inherited SetValues;
  2296. fBitsPerPixel := 16;
  2297. fFormat := tfLuminance6Alpha2ub2;
  2298. fWithAlpha := tfLuminance6Alpha2ub2;
  2299. fWithoutAlpha := tfLuminance8ub1;
  2300. fPrecision := glBitmapRec4ub(8, 8, 8, 8);
  2301. fShift := glBitmapRec4ub(0, 0, 0, 8);
  2302. {$IFNDEF OPENGL_ES}
  2303. fOpenGLFormat := tfLuminance6Alpha2ub2;
  2304. fglFormat := GL_LUMINANCE_ALPHA;
  2305. fglInternalFormat := GL_LUMINANCE6_ALPHA2;
  2306. fglDataFormat := GL_UNSIGNED_BYTE;
  2307. {$ELSE}
  2308. fOpenGLFormat := tfLuminance8Alpha8ub2;
  2309. {$ENDIF}
  2310. end;
  2311. procedure TfdLuminance8Alpha8ub2.SetValues;
  2312. begin
  2313. inherited SetValues;
  2314. fBitsPerPixel := 16;
  2315. fFormat := tfLuminance8Alpha8ub2;
  2316. fWithAlpha := tfLuminance8Alpha8ub2;
  2317. fWithoutAlpha := tfLuminance8ub1;
  2318. fOpenGLFormat := tfLuminance8Alpha8ub2;
  2319. fPrecision := glBitmapRec4ub(8, 8, 8, 8);
  2320. fShift := glBitmapRec4ub(0, 0, 0, 8);
  2321. fglFormat := GL_LUMINANCE_ALPHA;
  2322. fglInternalFormat := {$IFNDEF OPENGL_ES}GL_LUMINANCE8_ALPHA8{$ELSE}GL_LUMINANCE_ALPHA{$ENDIF};
  2323. fglDataFormat := GL_UNSIGNED_BYTE;
  2324. end;
  2325. procedure TfdLuminance12Alpha4us2.SetValues;
  2326. begin
  2327. inherited SetValues;
  2328. fBitsPerPixel := 32;
  2329. fFormat := tfLuminance12Alpha4us2;
  2330. fWithAlpha := tfLuminance12Alpha4us2;
  2331. fWithoutAlpha := tfLuminance16us1;
  2332. fPrecision := glBitmapRec4ub(16, 16, 16, 16);
  2333. fShift := glBitmapRec4ub( 0, 0, 0, 16);
  2334. {$IFNDEF OPENGL_ES}
  2335. fOpenGLFormat := tfLuminance12Alpha4us2;
  2336. fglFormat := GL_LUMINANCE_ALPHA;
  2337. fglInternalFormat := GL_LUMINANCE12_ALPHA4;
  2338. fglDataFormat := GL_UNSIGNED_SHORT;
  2339. {$ELSE}
  2340. fOpenGLFormat := tfLuminance8Alpha8ub2;
  2341. {$ENDIF}
  2342. end;
  2343. procedure TfdLuminance16Alpha16us2.SetValues;
  2344. begin
  2345. inherited SetValues;
  2346. fBitsPerPixel := 32;
  2347. fFormat := tfLuminance16Alpha16us2;
  2348. fWithAlpha := tfLuminance16Alpha16us2;
  2349. fWithoutAlpha := tfLuminance16us1;
  2350. fPrecision := glBitmapRec4ub(16, 16, 16, 16);
  2351. fShift := glBitmapRec4ub( 0, 0, 0, 16);
  2352. {$IFNDEF OPENGL_ES}
  2353. fOpenGLFormat := tfLuminance16Alpha16us2;
  2354. fglFormat := GL_LUMINANCE_ALPHA;
  2355. fglInternalFormat := GL_LUMINANCE16_ALPHA16;
  2356. fglDataFormat := GL_UNSIGNED_SHORT;
  2357. {$ELSE}
  2358. fOpenGLFormat := tfLuminance8Alpha8ub2;
  2359. {$ENDIF}
  2360. end;
  2361. procedure TfdR3G3B2ub1.SetValues;
  2362. begin
  2363. inherited SetValues;
  2364. fBitsPerPixel := 8;
  2365. fFormat := tfR3G3B2ub1;
  2366. fWithAlpha := tfRGBA4us1;
  2367. fWithoutAlpha := tfR3G3B2ub1;
  2368. fRGBInverted := tfEmpty;
  2369. fPrecision := glBitmapRec4ub(3, 3, 2, 0);
  2370. fShift := glBitmapRec4ub(5, 2, 0, 0);
  2371. {$IFNDEF OPENGL_ES}
  2372. fOpenGLFormat := tfR3G3B2ub1;
  2373. fglFormat := GL_RGB;
  2374. fglInternalFormat := GL_R3_G3_B2;
  2375. fglDataFormat := GL_UNSIGNED_BYTE_3_3_2;
  2376. {$ELSE}
  2377. fOpenGLFormat := tfR5G6B5us1;
  2378. {$ENDIF}
  2379. end;
  2380. procedure TfdRGBX4us1.SetValues;
  2381. begin
  2382. inherited SetValues;
  2383. fBitsPerPixel := 16;
  2384. fFormat := tfRGBX4us1;
  2385. fWithAlpha := tfRGBA4us1;
  2386. fWithoutAlpha := tfRGBX4us1;
  2387. fRGBInverted := tfBGRX4us1;
  2388. fPrecision := glBitmapRec4ub( 4, 4, 4, 0);
  2389. fShift := glBitmapRec4ub(12, 8, 4, 0);
  2390. {$IFNDEF OPENGL_ES}
  2391. fOpenGLFormat := tfRGBX4us1;
  2392. fglFormat := GL_RGBA; //GL_INVALID_OPERATION if not GL_BGRA or GL_RGBA
  2393. fglInternalFormat := GL_RGB4;
  2394. fglDataFormat := GL_UNSIGNED_SHORT_4_4_4_4;
  2395. {$ELSE}
  2396. fOpenGLFormat := tfR5G6B5us1;
  2397. {$ENDIF}
  2398. end;
  2399. procedure TfdXRGB4us1.SetValues;
  2400. begin
  2401. inherited SetValues;
  2402. fBitsPerPixel := 16;
  2403. fFormat := tfXRGB4us1;
  2404. fWithAlpha := tfARGB4us1;
  2405. fWithoutAlpha := tfXRGB4us1;
  2406. fRGBInverted := tfXBGR4us1;
  2407. fPrecision := glBitmapRec4ub(4, 4, 4, 0);
  2408. fShift := glBitmapRec4ub(8, 4, 0, 0);
  2409. {$IFNDEF OPENGL_ES}
  2410. fOpenGLFormat := tfXRGB4us1;
  2411. fglFormat := GL_BGRA;
  2412. fglInternalFormat := GL_RGB4;
  2413. fglDataFormat := GL_UNSIGNED_SHORT_4_4_4_4_REV;
  2414. {$ELSE}
  2415. fOpenGLFormat := tfR5G6B5us1;
  2416. {$ENDIF}
  2417. end;
  2418. procedure TfdR5G6B5us1.SetValues;
  2419. begin
  2420. inherited SetValues;
  2421. fBitsPerPixel := 16;
  2422. fFormat := tfR5G6B5us1;
  2423. fWithAlpha := tfRGB5A1us1;
  2424. fWithoutAlpha := tfR5G6B5us1;
  2425. fRGBInverted := tfB5G6R5us1;
  2426. fPrecision := glBitmapRec4ub( 5, 6, 5, 0);
  2427. fShift := glBitmapRec4ub(11, 5, 0, 0);
  2428. {$IF NOT DEFINED(OPENGL_ES) OR DEFINED(OPENGL_ES_2_0)}
  2429. fOpenGLFormat := tfR5G6B5us1;
  2430. fglFormat := GL_RGB;
  2431. fglInternalFormat := GL_RGB565;
  2432. fglDataFormat := GL_UNSIGNED_SHORT_5_6_5;
  2433. {$ELSE}
  2434. fOpenGLFormat := tfRGB8ub3;
  2435. {$IFEND}
  2436. end;
  2437. procedure TfdRGB5X1us1.SetValues;
  2438. begin
  2439. inherited SetValues;
  2440. fBitsPerPixel := 16;
  2441. fFormat := tfRGB5X1us1;
  2442. fWithAlpha := tfRGB5A1us1;
  2443. fWithoutAlpha := tfRGB5X1us1;
  2444. fRGBInverted := tfBGR5X1us1;
  2445. fPrecision := glBitmapRec4ub( 5, 5, 5, 0);
  2446. fShift := glBitmapRec4ub(11, 6, 1, 0);
  2447. {$IFNDEF OPENGL_ES}
  2448. fOpenGLFormat := tfRGB5X1us1;
  2449. fglFormat := GL_RGBA; //GL_INVALID_OPERATION if not GL_BGRA or GL_RGBA
  2450. fglInternalFormat := GL_RGB5;
  2451. fglDataFormat := GL_UNSIGNED_SHORT_5_5_5_1;
  2452. {$ELSE}
  2453. fOpenGLFormat := tfR5G6B5us1;
  2454. {$ENDIF}
  2455. end;
  2456. procedure TfdX1RGB5us1.SetValues;
  2457. begin
  2458. inherited SetValues;
  2459. fBitsPerPixel := 16;
  2460. fFormat := tfX1RGB5us1;
  2461. fWithAlpha := tfA1RGB5us1;
  2462. fWithoutAlpha := tfX1RGB5us1;
  2463. fRGBInverted := tfX1BGR5us1;
  2464. fPrecision := glBitmapRec4ub( 5, 5, 5, 0);
  2465. fShift := glBitmapRec4ub(10, 5, 0, 0);
  2466. {$IFNDEF OPENGL_ES}
  2467. fOpenGLFormat := tfX1RGB5us1;
  2468. fglFormat := GL_BGRA; //GL_INVALID_OPERATION if not GL_BGRA or GL_RGBA
  2469. fglInternalFormat := GL_RGB5;
  2470. fglDataFormat := GL_UNSIGNED_SHORT_1_5_5_5_REV;
  2471. {$ELSE}
  2472. fOpenGLFormat := tfR5G6B5us1;
  2473. {$ENDIF}
  2474. end;
  2475. procedure TfdRGB8ub3.SetValues;
  2476. begin
  2477. inherited SetValues;
  2478. fBitsPerPixel := 24;
  2479. fFormat := tfRGB8ub3;
  2480. fWithAlpha := tfRGBA8ub4;
  2481. fWithoutAlpha := tfRGB8ub3;
  2482. fRGBInverted := tfBGR8ub3;
  2483. fPrecision := glBitmapRec4ub(8, 8, 8, 0);
  2484. fShift := glBitmapRec4ub(0, 8, 16, 0);
  2485. fOpenGLFormat := tfRGB8ub3;
  2486. fglFormat := GL_RGB;
  2487. fglInternalFormat := {$IF NOT DEFINED(OPENGL_ES) OR DEFINED(OPENGL_ES_3_0)}GL_RGB8{$ELSE}GL_RGB{$IFEND};
  2488. fglDataFormat := GL_UNSIGNED_BYTE;
  2489. end;
  2490. procedure TfdRGBX8ui1.SetValues;
  2491. begin
  2492. inherited SetValues;
  2493. fBitsPerPixel := 32;
  2494. fFormat := tfRGBX8ui1;
  2495. fWithAlpha := tfRGBA8ui1;
  2496. fWithoutAlpha := tfRGBX8ui1;
  2497. fRGBInverted := tfBGRX8ui1;
  2498. fPrecision := glBitmapRec4ub( 8, 8, 8, 0);
  2499. fShift := glBitmapRec4ub(24, 16, 8, 0);
  2500. {$IFNDEF OPENGL_ES}
  2501. fOpenGLFormat := tfRGBX8ui1;
  2502. fglFormat := GL_RGBA; //GL_INVALID_OPERATION if not GL_BGRA or GL_RGBA
  2503. fglInternalFormat := GL_RGB8;
  2504. fglDataFormat := GL_UNSIGNED_INT_8_8_8_8;
  2505. {$ELSE}
  2506. fOpenGLFormat := tfRGB8ub3;
  2507. {$ENDIF}
  2508. end;
  2509. procedure TfdXRGB8ui1.SetValues;
  2510. begin
  2511. inherited SetValues;
  2512. fBitsPerPixel := 32;
  2513. fFormat := tfXRGB8ui1;
  2514. fWithAlpha := tfXRGB8ui1;
  2515. fWithoutAlpha := tfXRGB8ui1;
  2516. fOpenGLFormat := tfXRGB8ui1;
  2517. fRGBInverted := tfXBGR8ui1;
  2518. fPrecision := glBitmapRec4ub( 8, 8, 8, 0);
  2519. fShift := glBitmapRec4ub(16, 8, 0, 0);
  2520. {$IFNDEF OPENGL_ES}
  2521. fOpenGLFormat := tfXRGB8ui1;
  2522. fglFormat := GL_BGRA; //GL_INVALID_OPERATION if not GL_BGRA or GL_RGBA
  2523. fglInternalFormat := GL_RGB8;
  2524. fglDataFormat := GL_UNSIGNED_INT_8_8_8_8_REV;
  2525. {$ELSE}
  2526. fOpenGLFormat := tfRGB8ub3;
  2527. {$ENDIF}
  2528. end;
  2529. procedure TfdRGB10X2ui1.SetValues;
  2530. begin
  2531. inherited SetValues;
  2532. fBitsPerPixel := 32;
  2533. fFormat := tfRGB10X2ui1;
  2534. fWithAlpha := tfRGB10A2ui1;
  2535. fWithoutAlpha := tfRGB10X2ui1;
  2536. fRGBInverted := tfBGR10X2ui1;
  2537. fPrecision := glBitmapRec4ub(10, 10, 10, 0);
  2538. fShift := glBitmapRec4ub(22, 12, 2, 0);
  2539. {$IFNDEF OPENGL_ES}
  2540. fOpenGLFormat := tfRGB10X2ui1;
  2541. fglFormat := GL_RGBA; //GL_INVALID_OPERATION if not GL_BGRA or GL_RGBA
  2542. fglInternalFormat := GL_RGB10;
  2543. fglDataFormat := GL_UNSIGNED_INT_10_10_10_2;
  2544. {$ELSE}
  2545. fOpenGLFormat := tfRGB16us3;
  2546. {$ENDIF}
  2547. end;
  2548. procedure TfdX2RGB10ui1.SetValues;
  2549. begin
  2550. inherited SetValues;
  2551. fBitsPerPixel := 32;
  2552. fFormat := tfX2RGB10ui1;
  2553. fWithAlpha := tfA2RGB10ui1;
  2554. fWithoutAlpha := tfX2RGB10ui1;
  2555. fRGBInverted := tfX2BGR10ui1;
  2556. fPrecision := glBitmapRec4ub(10, 10, 10, 0);
  2557. fShift := glBitmapRec4ub(20, 10, 0, 0);
  2558. {$IFNDEF OPENGL_ES}
  2559. fOpenGLFormat := tfX2RGB10ui1;
  2560. fglFormat := GL_BGRA; //GL_INVALID_OPERATION if not GL_BGRA or GL_RGBA
  2561. fglInternalFormat := GL_RGB10;
  2562. fglDataFormat := GL_UNSIGNED_INT_2_10_10_10_REV;
  2563. {$ELSE}
  2564. fOpenGLFormat := tfRGB16us3;
  2565. {$ENDIF}
  2566. end;
  2567. procedure TfdRGB16us3.SetValues;
  2568. begin
  2569. inherited SetValues;
  2570. fBitsPerPixel := 48;
  2571. fFormat := tfRGB16us3;
  2572. fWithAlpha := tfRGBA16us4;
  2573. fWithoutAlpha := tfRGB16us3;
  2574. fRGBInverted := tfBGR16us3;
  2575. fPrecision := glBitmapRec4ub(16, 16, 16, 0);
  2576. fShift := glBitmapRec4ub( 0, 16, 32, 0);
  2577. {$IF NOT DEFINED(OPENGL_ES) OR DEFINED(OPENGL_ES_3_0)}
  2578. fOpenGLFormat := tfRGB16us3;
  2579. fglFormat := GL_RGB;
  2580. fglInternalFormat := {$IFNDEF OPENGL_ES}GL_RGB16{$ELSE}GL_RGB16UI{$ENDIF};
  2581. fglDataFormat := GL_UNSIGNED_SHORT;
  2582. {$ELSE}
  2583. fOpenGLFormat := tfRGB8ub3;
  2584. {$IFEND}
  2585. end;
  2586. procedure TfdRGBA4us1.SetValues;
  2587. begin
  2588. inherited SetValues;
  2589. fBitsPerPixel := 16;
  2590. fFormat := tfRGBA4us1;
  2591. fWithAlpha := tfRGBA4us1;
  2592. fWithoutAlpha := tfRGBX4us1;
  2593. fOpenGLFormat := tfRGBA4us1;
  2594. fRGBInverted := tfBGRA4us1;
  2595. fPrecision := glBitmapRec4ub( 4, 4, 4, 4);
  2596. fShift := glBitmapRec4ub(12, 8, 4, 0);
  2597. fglFormat := GL_RGBA;
  2598. fglInternalFormat := {$IF NOT DEFINED(OPENGL_ES) OR DEFINED(OPENGL_ES_3_0)}GL_RGBA4{$ELSE}GL_RGBA{$IFEND};
  2599. fglDataFormat := GL_UNSIGNED_SHORT_4_4_4_4;
  2600. end;
  2601. procedure TfdARGB4us1.SetValues;
  2602. begin
  2603. inherited SetValues;
  2604. fBitsPerPixel := 16;
  2605. fFormat := tfARGB4us1;
  2606. fWithAlpha := tfARGB4us1;
  2607. fWithoutAlpha := tfXRGB4us1;
  2608. fRGBInverted := tfABGR4us1;
  2609. fPrecision := glBitmapRec4ub( 4, 4, 4, 4);
  2610. fShift := glBitmapRec4ub( 8, 4, 0, 12);
  2611. {$IFNDEF OPENGL_ES}
  2612. fOpenGLFormat := tfARGB4us1;
  2613. fglFormat := GL_BGRA;
  2614. fglInternalFormat := GL_RGBA4;
  2615. fglDataFormat := GL_UNSIGNED_SHORT_4_4_4_4_REV;
  2616. {$ELSE}
  2617. fOpenGLFormat := tfRGBA4us1;
  2618. {$ENDIF}
  2619. end;
  2620. procedure TfdRGB5A1us1.SetValues;
  2621. begin
  2622. inherited SetValues;
  2623. fBitsPerPixel := 16;
  2624. fFormat := tfRGB5A1us1;
  2625. fWithAlpha := tfRGB5A1us1;
  2626. fWithoutAlpha := tfRGB5X1us1;
  2627. fOpenGLFormat := tfRGB5A1us1;
  2628. fRGBInverted := tfBGR5A1us1;
  2629. fPrecision := glBitmapRec4ub( 5, 5, 5, 1);
  2630. fShift := glBitmapRec4ub(11, 6, 1, 0);
  2631. fglFormat := GL_RGBA;
  2632. fglInternalFormat := {$IF NOT DEFINED(OPENGL_ES) OR DEFINED(OPENGL_ES_2_0)}GL_RGB5_A1{$ELSE}GL_RGBA{$IFEND};
  2633. fglDataFormat := GL_UNSIGNED_SHORT_5_5_5_1;
  2634. end;
  2635. procedure TfdA1RGB5us1.SetValues;
  2636. begin
  2637. inherited SetValues;
  2638. fBitsPerPixel := 16;
  2639. fFormat := tfA1RGB5us1;
  2640. fWithAlpha := tfA1RGB5us1;
  2641. fWithoutAlpha := tfX1RGB5us1;
  2642. fRGBInverted := tfA1BGR5us1;
  2643. fPrecision := glBitmapRec4ub( 5, 5, 5, 1);
  2644. fShift := glBitmapRec4ub(10, 5, 0, 15);
  2645. {$IFNDEF OPENGL_ES}
  2646. fOpenGLFormat := tfA1RGB5us1;
  2647. fglFormat := GL_BGRA;
  2648. fglInternalFormat := GL_RGB5_A1;
  2649. fglDataFormat := GL_UNSIGNED_SHORT_1_5_5_5_REV;
  2650. {$ELSE}
  2651. fOpenGLFormat := tfRGB5A1us1;
  2652. {$ENDIF}
  2653. end;
  2654. procedure TfdRGBA8ui1.SetValues;
  2655. begin
  2656. inherited SetValues;
  2657. fBitsPerPixel := 32;
  2658. fFormat := tfRGBA8ui1;
  2659. fWithAlpha := tfRGBA8ui1;
  2660. fWithoutAlpha := tfRGBX8ui1;
  2661. fRGBInverted := tfBGRA8ui1;
  2662. fPrecision := glBitmapRec4ub( 8, 8, 8, 8);
  2663. fShift := glBitmapRec4ub(24, 16, 8, 0);
  2664. {$IFNDEF OPENGL_ES}
  2665. fOpenGLFormat := tfRGBA8ui1;
  2666. fglFormat := GL_RGBA;
  2667. fglInternalFormat := GL_RGBA8;
  2668. fglDataFormat := GL_UNSIGNED_INT_8_8_8_8;
  2669. {$ELSE}
  2670. fOpenGLFormat := tfRGBA8ub4;
  2671. {$ENDIF}
  2672. end;
  2673. procedure TfdARGB8ui1.SetValues;
  2674. begin
  2675. inherited SetValues;
  2676. fBitsPerPixel := 32;
  2677. fFormat := tfARGB8ui1;
  2678. fWithAlpha := tfARGB8ui1;
  2679. fWithoutAlpha := tfXRGB8ui1;
  2680. fRGBInverted := tfABGR8ui1;
  2681. fPrecision := glBitmapRec4ub( 8, 8, 8, 8);
  2682. fShift := glBitmapRec4ub(16, 8, 0, 24);
  2683. {$IFNDEF OPENGL_ES}
  2684. fOpenGLFormat := tfARGB8ui1;
  2685. fglFormat := GL_BGRA;
  2686. fglInternalFormat := GL_RGBA8;
  2687. fglDataFormat := GL_UNSIGNED_INT_8_8_8_8_REV;
  2688. {$ELSE}
  2689. fOpenGLFormat := tfRGBA8ub4;
  2690. {$ENDIF}
  2691. end;
  2692. procedure TfdRGBA8ub4.SetValues;
  2693. begin
  2694. inherited SetValues;
  2695. fBitsPerPixel := 32;
  2696. fFormat := tfRGBA8ub4;
  2697. fWithAlpha := tfRGBA8ub4;
  2698. fWithoutAlpha := tfRGB8ub3;
  2699. fOpenGLFormat := tfRGBA8ub4;
  2700. fRGBInverted := tfBGRA8ub4;
  2701. fPrecision := glBitmapRec4ub( 8, 8, 8, 8);
  2702. fShift := glBitmapRec4ub( 0, 8, 16, 24);
  2703. fglFormat := GL_RGBA;
  2704. fglInternalFormat := {$IF NOT DEFINED(OPENGL_ES) OR DEFINED(OPENGL_ES_3_0)}GL_RGBA8{$ELSE}GL_RGBA{$IFEND};
  2705. fglDataFormat := GL_UNSIGNED_BYTE;
  2706. end;
  2707. procedure TfdRGB10A2ui1.SetValues;
  2708. begin
  2709. inherited SetValues;
  2710. fBitsPerPixel := 32;
  2711. fFormat := tfRGB10A2ui1;
  2712. fWithAlpha := tfRGB10A2ui1;
  2713. fWithoutAlpha := tfRGB10X2ui1;
  2714. fRGBInverted := tfBGR10A2ui1;
  2715. fPrecision := glBitmapRec4ub(10, 10, 10, 2);
  2716. fShift := glBitmapRec4ub(22, 12, 2, 0);
  2717. {$IFNDEF OPENGL_ES}
  2718. fOpenGLFormat := tfRGB10A2ui1;
  2719. fglFormat := GL_RGBA;
  2720. fglInternalFormat := GL_RGB10_A2;
  2721. fglDataFormat := GL_UNSIGNED_INT_10_10_10_2;
  2722. {$ELSE}
  2723. fOpenGLFormat := tfA2RGB10ui1;
  2724. {$ENDIF}
  2725. end;
  2726. procedure TfdA2RGB10ui1.SetValues;
  2727. begin
  2728. inherited SetValues;
  2729. fBitsPerPixel := 32;
  2730. fFormat := tfA2RGB10ui1;
  2731. fWithAlpha := tfA2RGB10ui1;
  2732. fWithoutAlpha := tfX2RGB10ui1;
  2733. fRGBInverted := tfA2BGR10ui1;
  2734. fPrecision := glBitmapRec4ub(10, 10, 10, 2);
  2735. fShift := glBitmapRec4ub(20, 10, 0, 30);
  2736. {$IF NOT DEFINED(OPENGL_ES)}
  2737. fOpenGLFormat := tfA2RGB10ui1;
  2738. fglFormat := GL_BGRA;
  2739. fglInternalFormat := GL_RGB10_A2;
  2740. fglDataFormat := GL_UNSIGNED_INT_2_10_10_10_REV;
  2741. {$ELSEIF DEFINED(OPENGL_ES_3_0)}
  2742. fOpenGLFormat := tfA2RGB10ui1;
  2743. fglFormat := GL_RGBA;
  2744. fglInternalFormat := GL_RGB10_A2;
  2745. fglDataFormat := GL_UNSIGNED_INT_2_10_10_10_REV;
  2746. {$ELSE}
  2747. fOpenGLFormat := tfRGBA8ui1;
  2748. {$IFEND}
  2749. end;
  2750. procedure TfdRGBA16us4.SetValues;
  2751. begin
  2752. inherited SetValues;
  2753. fBitsPerPixel := 64;
  2754. fFormat := tfRGBA16us4;
  2755. fWithAlpha := tfRGBA16us4;
  2756. fWithoutAlpha := tfRGB16us3;
  2757. fRGBInverted := tfBGRA16us4;
  2758. fPrecision := glBitmapRec4ub(16, 16, 16, 16);
  2759. fShift := glBitmapRec4ub( 0, 16, 32, 48);
  2760. {$IF NOT DEFINED(OPENGL_ES) OR DEFINED(OPENGL_ES_3_0)}
  2761. fOpenGLFormat := tfRGBA16us4;
  2762. fglFormat := GL_RGBA;
  2763. fglInternalFormat := {$IFNDEF OPENGL_ES}GL_RGBA16{$ELSE}GL_RGBA16UI{$ENDIF};
  2764. fglDataFormat := GL_UNSIGNED_SHORT;
  2765. {$ELSE}
  2766. fOpenGLFormat := tfRGBA8ub4;
  2767. {$IFEND}
  2768. end;
  2769. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2770. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2771. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2772. procedure TfdBGRX4us1.SetValues;
  2773. begin
  2774. inherited SetValues;
  2775. fBitsPerPixel := 16;
  2776. fFormat := tfBGRX4us1;
  2777. fWithAlpha := tfBGRA4us1;
  2778. fWithoutAlpha := tfBGRX4us1;
  2779. fRGBInverted := tfRGBX4us1;
  2780. fPrecision := glBitmapRec4ub( 4, 4, 4, 0);
  2781. fShift := glBitmapRec4ub( 4, 8, 12, 0);
  2782. {$IFNDEF OPENGL_ES}
  2783. fOpenGLFormat := tfBGRX4us1;
  2784. fglFormat := GL_BGRA; //GL_INVALID_OPERATION if not GL_BGRA or GL_RGBA
  2785. fglInternalFormat := GL_RGB4;
  2786. fglDataFormat := GL_UNSIGNED_SHORT_4_4_4_4;
  2787. {$ELSE}
  2788. fOpenGLFormat := tfR5G6B5us1;
  2789. {$ENDIF}
  2790. end;
  2791. procedure TfdXBGR4us1.SetValues;
  2792. begin
  2793. inherited SetValues;
  2794. fBitsPerPixel := 16;
  2795. fFormat := tfXBGR4us1;
  2796. fWithAlpha := tfABGR4us1;
  2797. fWithoutAlpha := tfXBGR4us1;
  2798. fRGBInverted := tfXRGB4us1;
  2799. fPrecision := glBitmapRec4ub( 4, 4, 4, 0);
  2800. fShift := glBitmapRec4ub( 0, 4, 8, 0);
  2801. {$IFNDEF OPENGL_ES}
  2802. fOpenGLFormat := tfXBGR4us1;
  2803. fglFormat := GL_RGBA; //GL_INVALID_OPERATION if not GL_BGRA or GL_RGBA
  2804. fglInternalFormat := GL_RGB4;
  2805. fglDataFormat := GL_UNSIGNED_SHORT_4_4_4_4_REV;
  2806. {$ELSE}
  2807. fOpenGLFormat := tfR5G6B5us1;
  2808. {$ENDIF}
  2809. end;
  2810. procedure TfdB5G6R5us1.SetValues;
  2811. begin
  2812. inherited SetValues;
  2813. fBitsPerPixel := 16;
  2814. fFormat := tfB5G6R5us1;
  2815. fWithAlpha := tfBGR5A1us1;
  2816. fWithoutAlpha := tfB5G6R5us1;
  2817. fRGBInverted := tfR5G6B5us1;
  2818. fPrecision := glBitmapRec4ub( 5, 6, 5, 0);
  2819. fShift := glBitmapRec4ub( 0, 5, 11, 0);
  2820. {$IFNDEF OPENGL_ES}
  2821. fOpenGLFormat := tfB5G6R5us1;
  2822. fglFormat := GL_RGB;
  2823. fglInternalFormat := GL_RGB565;
  2824. fglDataFormat := GL_UNSIGNED_SHORT_5_6_5_REV;
  2825. {$ELSE}
  2826. fOpenGLFormat := tfR5G6B5us1;
  2827. {$ENDIF}
  2828. end;
  2829. procedure TfdBGR5X1us1.SetValues;
  2830. begin
  2831. inherited SetValues;
  2832. fBitsPerPixel := 16;
  2833. fFormat := tfBGR5X1us1;
  2834. fWithAlpha := tfBGR5A1us1;
  2835. fWithoutAlpha := tfBGR5X1us1;
  2836. fRGBInverted := tfRGB5X1us1;
  2837. fPrecision := glBitmapRec4ub( 5, 5, 5, 0);
  2838. fShift := glBitmapRec4ub( 1, 6, 11, 0);
  2839. {$IFNDEF OPENGL_ES}
  2840. fOpenGLFormat := tfBGR5X1us1;
  2841. fglFormat := GL_BGRA;
  2842. fglInternalFormat := GL_RGB5;
  2843. fglDataFormat := GL_UNSIGNED_SHORT_5_5_5_1;
  2844. {$ELSE}
  2845. fOpenGLFormat := tfR5G6B5us1;
  2846. {$ENDIF}
  2847. end;
  2848. procedure TfdX1BGR5us1.SetValues;
  2849. begin
  2850. inherited SetValues;
  2851. fBitsPerPixel := 16;
  2852. fFormat := tfX1BGR5us1;
  2853. fWithAlpha := tfA1BGR5us1;
  2854. fWithoutAlpha := tfX1BGR5us1;
  2855. fRGBInverted := tfX1RGB5us1;
  2856. fPrecision := glBitmapRec4ub( 5, 5, 5, 0);
  2857. fShift := glBitmapRec4ub( 0, 5, 10, 0);
  2858. {$IFNDEF OPENGL_ES}
  2859. fOpenGLFormat := tfX1BGR5us1;
  2860. fglFormat := GL_RGBA; //GL_INVALID_OPERATION if not GL_BGRA or GL_RGBA
  2861. fglInternalFormat := GL_RGB5;
  2862. fglDataFormat := GL_UNSIGNED_SHORT_1_5_5_5_REV;
  2863. {$ELSE}
  2864. fOpenGLFormat := tfR5G6B5us1;
  2865. {$ENDIF}
  2866. end;
  2867. procedure TfdBGR8ub3.SetValues;
  2868. begin
  2869. inherited SetValues;
  2870. fBitsPerPixel := 24;
  2871. fFormat := tfBGR8ub3;
  2872. fWithAlpha := tfBGRA8ub4;
  2873. fWithoutAlpha := tfBGR8ub3;
  2874. fRGBInverted := tfRGB8ub3;
  2875. fPrecision := glBitmapRec4ub( 8, 8, 8, 0);
  2876. fShift := glBitmapRec4ub(16, 8, 0, 0);
  2877. {$IFNDEF OPENGL_ES}
  2878. fOpenGLFormat := tfBGR8ub3;
  2879. fglFormat := GL_BGR;
  2880. fglInternalFormat := GL_RGB8;
  2881. fglDataFormat := GL_UNSIGNED_BYTE;
  2882. {$ELSE}
  2883. fOpenGLFormat := tfRGB8ub3;
  2884. {$ENDIF}
  2885. end;
  2886. procedure TfdBGRX8ui1.SetValues;
  2887. begin
  2888. inherited SetValues;
  2889. fBitsPerPixel := 32;
  2890. fFormat := tfBGRX8ui1;
  2891. fWithAlpha := tfBGRA8ui1;
  2892. fWithoutAlpha := tfBGRX8ui1;
  2893. fRGBInverted := tfRGBX8ui1;
  2894. fPrecision := glBitmapRec4ub( 8, 8, 8, 0);
  2895. fShift := glBitmapRec4ub( 8, 16, 24, 0);
  2896. {$IFNDEF OPENGL_ES}
  2897. fOpenGLFormat := tfBGRX8ui1;
  2898. fglFormat := GL_BGRA; //GL_INVALID_OPERATION if not GL_BGRA or GL_RGBA
  2899. fglInternalFormat := GL_RGB8;
  2900. fglDataFormat := GL_UNSIGNED_INT_8_8_8_8;
  2901. {$ELSE}
  2902. fOpenGLFormat := tfRGB8ub3;
  2903. {$ENDIF}
  2904. end;
  2905. procedure TfdXBGR8ui1.SetValues;
  2906. begin
  2907. inherited SetValues;
  2908. fBitsPerPixel := 32;
  2909. fFormat := tfXBGR8ui1;
  2910. fWithAlpha := tfABGR8ui1;
  2911. fWithoutAlpha := tfXBGR8ui1;
  2912. fRGBInverted := tfXRGB8ui1;
  2913. fPrecision := glBitmapRec4ub( 8, 8, 8, 0);
  2914. fShift := glBitmapRec4ub( 0, 8, 16, 0);
  2915. {$IFNDEF OPENGL_ES}
  2916. fOpenGLFormat := tfXBGR8ui1;
  2917. fglFormat := GL_RGBA; //GL_INVALID_OPERATION if not GL_BGRA or GL_RGBA
  2918. fglInternalFormat := GL_RGB8;
  2919. fglDataFormat := GL_UNSIGNED_INT_8_8_8_8_REV;
  2920. {$ELSE}
  2921. fOpenGLFormat := tfRGB8ub3;
  2922. {$ENDIF}
  2923. end;
  2924. procedure TfdBGR10X2ui1.SetValues;
  2925. begin
  2926. inherited SetValues;
  2927. fBitsPerPixel := 32;
  2928. fFormat := tfBGR10X2ui1;
  2929. fWithAlpha := tfBGR10A2ui1;
  2930. fWithoutAlpha := tfBGR10X2ui1;
  2931. fRGBInverted := tfRGB10X2ui1;
  2932. fPrecision := glBitmapRec4ub(10, 10, 10, 0);
  2933. fShift := glBitmapRec4ub( 2, 12, 22, 0);
  2934. {$IFNDEF OPENGL_ES}
  2935. fOpenGLFormat := tfBGR10X2ui1;
  2936. fglFormat := GL_BGRA; //GL_INVALID_OPERATION if not GL_BGRA or GL_RGBA
  2937. fglInternalFormat := GL_RGB10;
  2938. fglDataFormat := GL_UNSIGNED_INT_10_10_10_2;
  2939. {$ELSE}
  2940. fOpenGLFormat := tfRGB16us3;
  2941. {$ENDIF}
  2942. end;
  2943. procedure TfdX2BGR10ui1.SetValues;
  2944. begin
  2945. inherited SetValues;
  2946. fBitsPerPixel := 32;
  2947. fFormat := tfX2BGR10ui1;
  2948. fWithAlpha := tfA2BGR10ui1;
  2949. fWithoutAlpha := tfX2BGR10ui1;
  2950. fRGBInverted := tfX2RGB10ui1;
  2951. fPrecision := glBitmapRec4ub(10, 10, 10, 0);
  2952. fShift := glBitmapRec4ub( 0, 10, 20, 0);
  2953. {$IFNDEF OPENGL_ES}
  2954. fOpenGLFormat := tfX2BGR10ui1;
  2955. fglFormat := GL_RGBA; //GL_INVALID_OPERATION if not GL_BGRA or GL_RGBA
  2956. fglInternalFormat := GL_RGB10;
  2957. fglDataFormat := GL_UNSIGNED_INT_2_10_10_10_REV;
  2958. {$ELSE}
  2959. fOpenGLFormat := tfRGB16us3;
  2960. {$ENDIF}
  2961. end;
  2962. procedure TfdBGR16us3.SetValues;
  2963. begin
  2964. inherited SetValues;
  2965. fBitsPerPixel := 48;
  2966. fFormat := tfBGR16us3;
  2967. fWithAlpha := tfBGRA16us4;
  2968. fWithoutAlpha := tfBGR16us3;
  2969. fRGBInverted := tfRGB16us3;
  2970. fPrecision := glBitmapRec4ub(16, 16, 16, 0);
  2971. fShift := glBitmapRec4ub(32, 16, 0, 0);
  2972. {$IFNDEF OPENGL_ES}
  2973. fOpenGLFormat := tfBGR16us3;
  2974. fglFormat := GL_BGR;
  2975. fglInternalFormat := GL_RGB16;
  2976. fglDataFormat := GL_UNSIGNED_SHORT;
  2977. {$ELSE}
  2978. fOpenGLFormat := tfRGB16us3;
  2979. {$ENDIF}
  2980. end;
  2981. procedure TfdBGRA4us1.SetValues;
  2982. begin
  2983. inherited SetValues;
  2984. fBitsPerPixel := 16;
  2985. fFormat := tfBGRA4us1;
  2986. fWithAlpha := tfBGRA4us1;
  2987. fWithoutAlpha := tfBGRX4us1;
  2988. fRGBInverted := tfRGBA4us1;
  2989. fPrecision := glBitmapRec4ub( 4, 4, 4, 4);
  2990. fShift := glBitmapRec4ub( 4, 8, 12, 0);
  2991. {$IFNDEF OPENGL_ES}
  2992. fOpenGLFormat := tfBGRA4us1;
  2993. fglFormat := GL_BGRA;
  2994. fglInternalFormat := GL_RGBA4;
  2995. fglDataFormat := GL_UNSIGNED_SHORT_4_4_4_4;
  2996. {$ELSE}
  2997. fOpenGLFormat := tfRGBA4us1;
  2998. {$ENDIF}
  2999. end;
  3000. procedure TfdABGR4us1.SetValues;
  3001. begin
  3002. inherited SetValues;
  3003. fBitsPerPixel := 16;
  3004. fFormat := tfABGR4us1;
  3005. fWithAlpha := tfABGR4us1;
  3006. fWithoutAlpha := tfXBGR4us1;
  3007. fRGBInverted := tfARGB4us1;
  3008. fPrecision := glBitmapRec4ub( 4, 4, 4, 4);
  3009. fShift := glBitmapRec4ub( 0, 4, 8, 12);
  3010. {$IFNDEF OPENGL_ES}
  3011. fOpenGLFormat := tfABGR4us1;
  3012. fglFormat := GL_RGBA;
  3013. fglInternalFormat := GL_RGBA4;
  3014. fglDataFormat := GL_UNSIGNED_SHORT_4_4_4_4_REV;
  3015. {$ELSE}
  3016. fOpenGLFormat := tfRGBA4us1;
  3017. {$ENDIF}
  3018. end;
  3019. procedure TfdBGR5A1us1.SetValues;
  3020. begin
  3021. inherited SetValues;
  3022. fBitsPerPixel := 16;
  3023. fFormat := tfBGR5A1us1;
  3024. fWithAlpha := tfBGR5A1us1;
  3025. fWithoutAlpha := tfBGR5X1us1;
  3026. fRGBInverted := tfRGB5A1us1;
  3027. fPrecision := glBitmapRec4ub( 5, 5, 5, 1);
  3028. fShift := glBitmapRec4ub( 1, 6, 11, 0);
  3029. {$IFNDEF OPENGL_ES}
  3030. fOpenGLFormat := tfBGR5A1us1;
  3031. fglFormat := GL_BGRA;
  3032. fglInternalFormat := GL_RGB5_A1;
  3033. fglDataFormat := GL_UNSIGNED_SHORT_5_5_5_1;
  3034. {$ELSE}
  3035. fOpenGLFormat := tfRGB5A1us1;
  3036. {$ENDIF}
  3037. end;
  3038. procedure TfdA1BGR5us1.SetValues;
  3039. begin
  3040. inherited SetValues;
  3041. fBitsPerPixel := 16;
  3042. fFormat := tfA1BGR5us1;
  3043. fWithAlpha := tfA1BGR5us1;
  3044. fWithoutAlpha := tfX1BGR5us1;
  3045. fRGBInverted := tfA1RGB5us1;
  3046. fPrecision := glBitmapRec4ub( 5, 5, 5, 1);
  3047. fShift := glBitmapRec4ub( 0, 5, 10, 15);
  3048. {$IFNDEF OPENGL_ES}
  3049. fOpenGLFormat := tfA1BGR5us1;
  3050. fglFormat := GL_RGBA;
  3051. fglInternalFormat := GL_RGB5_A1;
  3052. fglDataFormat := GL_UNSIGNED_SHORT_1_5_5_5_REV;
  3053. {$ELSE}
  3054. fOpenGLFormat := tfRGB5A1us1;
  3055. {$ENDIF}
  3056. end;
  3057. procedure TfdBGRA8ui1.SetValues;
  3058. begin
  3059. inherited SetValues;
  3060. fBitsPerPixel := 32;
  3061. fFormat := tfBGRA8ui1;
  3062. fWithAlpha := tfBGRA8ui1;
  3063. fWithoutAlpha := tfBGRX8ui1;
  3064. fRGBInverted := tfRGBA8ui1;
  3065. fPrecision := glBitmapRec4ub( 8, 8, 8, 8);
  3066. fShift := glBitmapRec4ub( 8, 16, 24, 0);
  3067. {$IFNDEF OPENGL_ES}
  3068. fOpenGLFormat := tfBGRA8ui1;
  3069. fglFormat := GL_BGRA;
  3070. fglInternalFormat := GL_RGBA8;
  3071. fglDataFormat := GL_UNSIGNED_INT_8_8_8_8;
  3072. {$ELSE}
  3073. fOpenGLFormat := tfRGBA8ub4;
  3074. {$ENDIF}
  3075. end;
  3076. procedure TfdABGR8ui1.SetValues;
  3077. begin
  3078. inherited SetValues;
  3079. fBitsPerPixel := 32;
  3080. fFormat := tfABGR8ui1;
  3081. fWithAlpha := tfABGR8ui1;
  3082. fWithoutAlpha := tfXBGR8ui1;
  3083. fRGBInverted := tfARGB8ui1;
  3084. fPrecision := glBitmapRec4ub( 8, 8, 8, 8);
  3085. fShift := glBitmapRec4ub( 0, 8, 16, 24);
  3086. {$IFNDEF OPENGL_ES}
  3087. fOpenGLFormat := tfABGR8ui1;
  3088. fglFormat := GL_RGBA;
  3089. fglInternalFormat := GL_RGBA8;
  3090. fglDataFormat := GL_UNSIGNED_INT_8_8_8_8_REV;
  3091. {$ELSE}
  3092. fOpenGLFormat := tfRGBA8ub4
  3093. {$ENDIF}
  3094. end;
  3095. procedure TfdBGRA8ub4.SetValues;
  3096. begin
  3097. inherited SetValues;
  3098. fBitsPerPixel := 32;
  3099. fFormat := tfBGRA8ub4;
  3100. fWithAlpha := tfBGRA8ub4;
  3101. fWithoutAlpha := tfBGR8ub3;
  3102. fRGBInverted := tfRGBA8ub4;
  3103. fPrecision := glBitmapRec4ub( 8, 8, 8, 8);
  3104. fShift := glBitmapRec4ub(16, 8, 0, 24);
  3105. {$IFNDEF OPENGL_ES}
  3106. fOpenGLFormat := tfBGRA8ub4;
  3107. fglFormat := GL_BGRA;
  3108. fglInternalFormat := GL_RGBA8;
  3109. fglDataFormat := GL_UNSIGNED_BYTE;
  3110. {$ELSE}
  3111. fOpenGLFormat := tfRGBA8ub4;
  3112. {$ENDIF}
  3113. end;
  3114. procedure TfdBGR10A2ui1.SetValues;
  3115. begin
  3116. inherited SetValues;
  3117. fBitsPerPixel := 32;
  3118. fFormat := tfBGR10A2ui1;
  3119. fWithAlpha := tfBGR10A2ui1;
  3120. fWithoutAlpha := tfBGR10X2ui1;
  3121. fRGBInverted := tfRGB10A2ui1;
  3122. fPrecision := glBitmapRec4ub(10, 10, 10, 2);
  3123. fShift := glBitmapRec4ub( 2, 12, 22, 0);
  3124. {$IFNDEF OPENGL_ES}
  3125. fOpenGLFormat := tfBGR10A2ui1;
  3126. fglFormat := GL_BGRA;
  3127. fglInternalFormat := GL_RGB10_A2;
  3128. fglDataFormat := GL_UNSIGNED_INT_10_10_10_2;
  3129. {$ELSE}
  3130. fOpenGLFormat := tfA2RGB10ui1;
  3131. {$ENDIF}
  3132. end;
  3133. procedure TfdA2BGR10ui1.SetValues;
  3134. begin
  3135. inherited SetValues;
  3136. fBitsPerPixel := 32;
  3137. fFormat := tfA2BGR10ui1;
  3138. fWithAlpha := tfA2BGR10ui1;
  3139. fWithoutAlpha := tfX2BGR10ui1;
  3140. fRGBInverted := tfA2RGB10ui1;
  3141. fPrecision := glBitmapRec4ub(10, 10, 10, 2);
  3142. fShift := glBitmapRec4ub( 0, 10, 20, 30);
  3143. {$IFNDEF OPENGL_ES}
  3144. fOpenGLFormat := tfA2BGR10ui1;
  3145. fglFormat := GL_RGBA;
  3146. fglInternalFormat := GL_RGB10_A2;
  3147. fglDataFormat := GL_UNSIGNED_INT_2_10_10_10_REV;
  3148. {$ELSE}
  3149. fOpenGLFormat := tfA2RGB10ui1;
  3150. {$ENDIF}
  3151. end;
  3152. procedure TfdBGRA16us4.SetValues;
  3153. begin
  3154. inherited SetValues;
  3155. fBitsPerPixel := 64;
  3156. fFormat := tfBGRA16us4;
  3157. fWithAlpha := tfBGRA16us4;
  3158. fWithoutAlpha := tfBGR16us3;
  3159. fRGBInverted := tfRGBA16us4;
  3160. fPrecision := glBitmapRec4ub(16, 16, 16, 16);
  3161. fShift := glBitmapRec4ub(32, 16, 0, 48);
  3162. {$IFNDEF OPENGL_ES}
  3163. fOpenGLFormat := tfBGRA16us4;
  3164. fglFormat := GL_BGRA;
  3165. fglInternalFormat := GL_RGBA16;
  3166. fglDataFormat := GL_UNSIGNED_SHORT;
  3167. {$ELSE}
  3168. fOpenGLFormat := tfRGBA16us4;
  3169. {$ENDIF}
  3170. end;
  3171. procedure TfdDepth16us1.SetValues;
  3172. begin
  3173. inherited SetValues;
  3174. fBitsPerPixel := 16;
  3175. fFormat := tfDepth16us1;
  3176. fWithoutAlpha := tfDepth16us1;
  3177. fPrecision := glBitmapRec4ub(16, 16, 16, 16);
  3178. fShift := glBitmapRec4ub( 0, 0, 0, 0);
  3179. {$IF NOT DEFINED (OPENGL_ES) OR DEFINED(OPENGL_ES_2_0)}
  3180. fOpenGLFormat := tfDepth16us1;
  3181. fglFormat := GL_DEPTH_COMPONENT;
  3182. fglInternalFormat := GL_DEPTH_COMPONENT16;
  3183. fglDataFormat := GL_UNSIGNED_SHORT;
  3184. {$IFEND}
  3185. end;
  3186. procedure TfdDepth24ui1.SetValues;
  3187. begin
  3188. inherited SetValues;
  3189. fBitsPerPixel := 32;
  3190. fFormat := tfDepth24ui1;
  3191. fWithoutAlpha := tfDepth24ui1;
  3192. fOpenGLFormat := tfDepth24ui1;
  3193. fPrecision := glBitmapRec4ub(32, 32, 32, 32);
  3194. fShift := glBitmapRec4ub( 0, 0, 0, 0);
  3195. {$IF NOT DEFINED (OPENGL_ES) OR DEFINED(OPENGL_ES_3_0)}
  3196. fOpenGLFormat := tfDepth24ui1;
  3197. fglFormat := GL_DEPTH_COMPONENT;
  3198. fglInternalFormat := GL_DEPTH_COMPONENT24;
  3199. fglDataFormat := GL_UNSIGNED_INT;
  3200. {$IFEND}
  3201. end;
  3202. procedure TfdDepth32ui1.SetValues;
  3203. begin
  3204. inherited SetValues;
  3205. fBitsPerPixel := 32;
  3206. fFormat := tfDepth32ui1;
  3207. fWithoutAlpha := tfDepth32ui1;
  3208. fPrecision := glBitmapRec4ub(32, 32, 32, 32);
  3209. fShift := glBitmapRec4ub( 0, 0, 0, 0);
  3210. {$IF NOT DEFINED(OPENGL_ES)}
  3211. fOpenGLFormat := tfDepth32ui1;
  3212. fglFormat := GL_DEPTH_COMPONENT;
  3213. fglInternalFormat := GL_DEPTH_COMPONENT32;
  3214. fglDataFormat := GL_UNSIGNED_INT;
  3215. {$ELSEIF DEFINED(OPENGL_ES_3_0)}
  3216. fOpenGLFormat := tfDepth24ui1;
  3217. {$ELSEIF DEFINED(OPENGL_ES_2_0)}
  3218. fOpenGLFormat := tfDepth16us1;
  3219. {$IFEND}
  3220. end;
  3221. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3222. //TfdS3tcDtx1RGBA/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3223. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3224. procedure TfdS3tcDtx1RGBA.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  3225. begin
  3226. raise EglBitmap.Create('mapping for compressed formats is not supported');
  3227. end;
  3228. procedure TfdS3tcDtx1RGBA.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  3229. begin
  3230. raise EglBitmap.Create('mapping for compressed formats is not supported');
  3231. end;
  3232. procedure TfdS3tcDtx1RGBA.SetValues;
  3233. begin
  3234. inherited SetValues;
  3235. fFormat := tfS3tcDtx1RGBA;
  3236. fWithAlpha := tfS3tcDtx1RGBA;
  3237. fUncompressed := tfRGB5A1us1;
  3238. fBitsPerPixel := 4;
  3239. fIsCompressed := true;
  3240. {$IFNDEF OPENGL_ES}
  3241. fOpenGLFormat := tfS3tcDtx1RGBA;
  3242. fglFormat := GL_COMPRESSED_RGBA;
  3243. fglInternalFormat := GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
  3244. fglDataFormat := GL_UNSIGNED_BYTE;
  3245. {$ELSE}
  3246. fOpenGLFormat := fUncompressed;
  3247. {$ENDIF}
  3248. end;
  3249. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3250. //TfdS3tcDtx3RGBA/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3251. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3252. procedure TfdS3tcDtx3RGBA.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  3253. begin
  3254. raise EglBitmap.Create('mapping for compressed formats is not supported');
  3255. end;
  3256. procedure TfdS3tcDtx3RGBA.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  3257. begin
  3258. raise EglBitmap.Create('mapping for compressed formats is not supported');
  3259. end;
  3260. procedure TfdS3tcDtx3RGBA.SetValues;
  3261. begin
  3262. inherited SetValues;
  3263. fFormat := tfS3tcDtx3RGBA;
  3264. fWithAlpha := tfS3tcDtx3RGBA;
  3265. fUncompressed := tfRGBA8ub4;
  3266. fBitsPerPixel := 8;
  3267. fIsCompressed := true;
  3268. {$IFNDEF OPENGL_ES}
  3269. fOpenGLFormat := tfS3tcDtx3RGBA;
  3270. fglFormat := GL_COMPRESSED_RGBA;
  3271. fglInternalFormat := GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
  3272. fglDataFormat := GL_UNSIGNED_BYTE;
  3273. {$ELSE}
  3274. fOpenGLFormat := fUncompressed;
  3275. {$ENDIF}
  3276. end;
  3277. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3278. //TfdS3tcDtx5RGBA/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3279. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3280. procedure TfdS3tcDtx5RGBA.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  3281. begin
  3282. raise EglBitmap.Create('mapping for compressed formats is not supported');
  3283. end;
  3284. procedure TfdS3tcDtx5RGBA.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  3285. begin
  3286. raise EglBitmap.Create('mapping for compressed formats is not supported');
  3287. end;
  3288. procedure TfdS3tcDtx5RGBA.SetValues;
  3289. begin
  3290. inherited SetValues;
  3291. fFormat := tfS3tcDtx3RGBA;
  3292. fWithAlpha := tfS3tcDtx3RGBA;
  3293. fUncompressed := tfRGBA8ub4;
  3294. fBitsPerPixel := 8;
  3295. fIsCompressed := true;
  3296. {$IFNDEF OPENGL_ES}
  3297. fOpenGLFormat := tfS3tcDtx3RGBA;
  3298. fglFormat := GL_COMPRESSED_RGBA;
  3299. fglInternalFormat := GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
  3300. fglDataFormat := GL_UNSIGNED_BYTE;
  3301. {$ELSE}
  3302. fOpenGLFormat := fUncompressed;
  3303. {$ENDIF}
  3304. end;
  3305. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3306. //TglBitmapFormatDescriptor///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3307. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3308. function TglBitmapFormatDescriptor.GetHasRed: Boolean;
  3309. begin
  3310. result := (fPrecision.r > 0);
  3311. end;
  3312. function TglBitmapFormatDescriptor.GetHasGreen: Boolean;
  3313. begin
  3314. result := (fPrecision.g > 0);
  3315. end;
  3316. function TglBitmapFormatDescriptor.GetHasBlue: Boolean;
  3317. begin
  3318. result := (fPrecision.b > 0);
  3319. end;
  3320. function TglBitmapFormatDescriptor.GetHasAlpha: Boolean;
  3321. begin
  3322. result := (fPrecision.a > 0);
  3323. end;
  3324. function TglBitmapFormatDescriptor.GetHasColor: Boolean;
  3325. begin
  3326. result := HasRed or HasGreen or HasBlue;
  3327. end;
  3328. function TglBitmapFormatDescriptor.GetIsGrayscale: Boolean;
  3329. begin
  3330. result := (Mask.r = Mask.g) and (Mask.g = Mask.b) and (Mask.r > 0);
  3331. end;
  3332. function TglBitmapFormatDescriptor.GetHasOpenGLSupport: Boolean;
  3333. begin
  3334. result := (OpenGLFormat = Format);
  3335. end;
  3336. procedure TglBitmapFormatDescriptor.SetValues;
  3337. begin
  3338. fFormat := tfEmpty;
  3339. fWithAlpha := tfEmpty;
  3340. fWithoutAlpha := tfEmpty;
  3341. fOpenGLFormat := tfEmpty;
  3342. fRGBInverted := tfEmpty;
  3343. fUncompressed := tfEmpty;
  3344. fBitsPerPixel := 0;
  3345. fIsCompressed := false;
  3346. fglFormat := 0;
  3347. fglInternalFormat := 0;
  3348. fglDataFormat := 0;
  3349. FillChar(fPrecision, 0, SizeOf(fPrecision));
  3350. FillChar(fShift, 0, SizeOf(fShift));
  3351. end;
  3352. procedure TglBitmapFormatDescriptor.CalcValues;
  3353. var
  3354. i: Integer;
  3355. begin
  3356. fBytesPerPixel := fBitsPerPixel / 8;
  3357. fChannelCount := 0;
  3358. for i := 0 to 3 do begin
  3359. if (fPrecision.arr[i] > 0) then
  3360. inc(fChannelCount);
  3361. fRange.arr[i] := (1 shl fPrecision.arr[i]) - 1;
  3362. fMask.arr[i] := fRange.arr[i] shl fShift.arr[i];
  3363. end;
  3364. end;
  3365. function TglBitmapFormatDescriptor.GetSize(const aSize: TglBitmapSize): Integer;
  3366. var
  3367. w, h: Integer;
  3368. begin
  3369. if (ffX in aSize.Fields) or (ffY in aSize.Fields) then begin
  3370. w := Max(1, aSize.X);
  3371. h := Max(1, aSize.Y);
  3372. result := GetSize(w, h);
  3373. end else
  3374. result := 0;
  3375. end;
  3376. function TglBitmapFormatDescriptor.GetSize(const aWidth, aHeight: Integer): Integer;
  3377. begin
  3378. result := 0;
  3379. if (aWidth <= 0) or (aHeight <= 0) then
  3380. exit;
  3381. result := Ceil(aWidth * aHeight * BytesPerPixel);
  3382. end;
  3383. constructor TglBitmapFormatDescriptor.Create;
  3384. begin
  3385. inherited Create;
  3386. SetValues;
  3387. CalcValues;
  3388. end;
  3389. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3390. class function TglBitmapFormatDescriptor.GetByFormat(const aInternalFormat: GLenum): TglBitmapFormatDescriptor;
  3391. var
  3392. f: TglBitmapFormat;
  3393. begin
  3394. for f := Low(TglBitmapFormat) to High(TglBitmapFormat) do begin
  3395. result := TFormatDescriptor.Get(f);
  3396. if (result.glInternalFormat = aInternalFormat) then
  3397. exit;
  3398. end;
  3399. result := TFormatDescriptor.Get(tfEmpty);
  3400. end;
  3401. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3402. class function TglBitmapFormatDescriptor.GetByFormat(const aFormat: TglBitmapFormat): TglBitmapFormatDescriptor;
  3403. begin
  3404. result := TFormatDescriptor.Get(aFormat);
  3405. if not Assigned(result) then
  3406. result := TFormatDescriptor.Get(tfEmpty);
  3407. end;
  3408. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3409. //TFormatDescriptor///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3410. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3411. class procedure TFormatDescriptor.Init;
  3412. begin
  3413. if not Assigned(FormatDescriptorCS) then
  3414. FormatDescriptorCS := TCriticalSection.Create;
  3415. end;
  3416. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3417. class function TFormatDescriptor.Get(const aFormat: TglBitmapFormat): TFormatDescriptor;
  3418. begin
  3419. FormatDescriptorCS.Enter;
  3420. try
  3421. result := FormatDescriptors[aFormat];
  3422. if not Assigned(result) then begin
  3423. result := FORMAT_DESCRIPTOR_CLASSES[aFormat].Create;
  3424. FormatDescriptors[aFormat] := result;
  3425. end;
  3426. finally
  3427. FormatDescriptorCS.Leave;
  3428. end;
  3429. end;
  3430. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3431. class function TFormatDescriptor.GetAlpha(const aFormat: TglBitmapFormat): TFormatDescriptor;
  3432. begin
  3433. result := Get(Get(aFormat).WithAlpha);
  3434. end;
  3435. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3436. class function TFormatDescriptor.GetFromMask(const aMask: TglBitmapRec4ul; const aBitCount: Integer): TFormatDescriptor;
  3437. var
  3438. ft: TglBitmapFormat;
  3439. begin
  3440. // find matching format with OpenGL support
  3441. for ft := High(TglBitmapFormat) downto Low(TglBitmapFormat) do begin
  3442. result := Get(ft);
  3443. if (result.MaskMatch(aMask)) and
  3444. (result.glFormat <> 0) and
  3445. (result.glInternalFormat <> 0) and
  3446. ((aBitCount = 0) or (aBitCount = result.BitsPerPixel))
  3447. then
  3448. exit;
  3449. end;
  3450. // find matching format without OpenGL Support
  3451. for ft := High(TglBitmapFormat) downto Low(TglBitmapFormat) do begin
  3452. result := Get(ft);
  3453. if result.MaskMatch(aMask) and ((aBitCount = 0) or (aBitCount = result.BitsPerPixel)) then
  3454. exit;
  3455. end;
  3456. result := TFormatDescriptor.Get(tfEmpty);
  3457. end;
  3458. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3459. class function TFormatDescriptor.GetFromPrecShift(const aPrec, aShift: TglBitmapRec4ub; const aBitCount: Integer): TFormatDescriptor;
  3460. var
  3461. ft: TglBitmapFormat;
  3462. begin
  3463. // find matching format with OpenGL support
  3464. for ft := High(TglBitmapFormat) downto Low(TglBitmapFormat) do begin
  3465. result := Get(ft);
  3466. if glBitmapRec4ubCompare(result.Shift, aShift) and
  3467. glBitmapRec4ubCompare(result.Precision, aPrec) and
  3468. (result.glFormat <> 0) and
  3469. (result.glInternalFormat <> 0) and
  3470. ((aBitCount = 0) or (aBitCount = result.BitsPerPixel))
  3471. then
  3472. exit;
  3473. end;
  3474. // find matching format without OpenGL Support
  3475. for ft := High(TglBitmapFormat) downto Low(TglBitmapFormat) do begin
  3476. result := Get(ft);
  3477. if glBitmapRec4ubCompare(result.Shift, aShift) and
  3478. glBitmapRec4ubCompare(result.Precision, aPrec) and
  3479. ((aBitCount = 0) or (aBitCount = result.BitsPerPixel)) then
  3480. exit;
  3481. end;
  3482. result := TFormatDescriptor.Get(tfEmpty);
  3483. end;
  3484. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3485. class procedure TFormatDescriptor.Clear;
  3486. var
  3487. f: TglBitmapFormat;
  3488. begin
  3489. FormatDescriptorCS.Enter;
  3490. try
  3491. for f := low(FormatDescriptors) to high(FormatDescriptors) do
  3492. FreeAndNil(FormatDescriptors[f]);
  3493. finally
  3494. FormatDescriptorCS.Leave;
  3495. end;
  3496. end;
  3497. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3498. class procedure TFormatDescriptor.Finalize;
  3499. begin
  3500. Clear;
  3501. FreeAndNil(FormatDescriptorCS);
  3502. end;
  3503. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3504. //TBitfieldFormat/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3505. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3506. procedure TbmpBitfieldFormat.SetCustomValues(const aBPP: Integer; aMask: TglBitmapRec4ul);
  3507. var
  3508. i: Integer;
  3509. begin
  3510. for i := 0 to 3 do begin
  3511. fShift.arr[i] := 0;
  3512. while (aMask.arr[i] > 0) and ((aMask.arr[i] and 1) = 0) do begin
  3513. aMask.arr[i] := aMask.arr[i] shr 1;
  3514. inc(fShift.arr[i]);
  3515. end;
  3516. fPrecision.arr[i] := CountSetBits(aMask.arr[i]);
  3517. end;
  3518. fBitsPerPixel := aBPP;
  3519. CalcValues;
  3520. end;
  3521. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3522. procedure TbmpBitfieldFormat.SetCustomValues(const aBBP: Integer; const aPrec, aShift: TglBitmapRec4ub);
  3523. begin
  3524. fBitsPerPixel := aBBP;
  3525. fPrecision := aPrec;
  3526. fShift := aShift;
  3527. CalcValues;
  3528. end;
  3529. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3530. procedure TbmpBitfieldFormat.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  3531. var
  3532. data: QWord;
  3533. begin
  3534. data :=
  3535. ((aPixel.Data.r and Range.r) shl Shift.r) or
  3536. ((aPixel.Data.g and Range.g) shl Shift.g) or
  3537. ((aPixel.Data.b and Range.b) shl Shift.b) or
  3538. ((aPixel.Data.a and Range.a) shl Shift.a);
  3539. case BitsPerPixel of
  3540. 8: aData^ := data;
  3541. 16: PWord(aData)^ := data;
  3542. 32: PCardinal(aData)^ := data;
  3543. 64: PQWord(aData)^ := data;
  3544. else
  3545. raise EglBitmap.CreateFmt('invalid pixel size: %d', [BitsPerPixel]);
  3546. end;
  3547. inc(aData, Round(BytesPerPixel));
  3548. end;
  3549. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3550. procedure TbmpBitfieldFormat.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  3551. var
  3552. data: QWord;
  3553. i: Integer;
  3554. begin
  3555. case BitsPerPixel of
  3556. 8: data := aData^;
  3557. 16: data := PWord(aData)^;
  3558. 32: data := PCardinal(aData)^;
  3559. 64: data := PQWord(aData)^;
  3560. else
  3561. raise EglBitmap.CreateFmt('invalid pixel size: %d', [BitsPerPixel]);
  3562. end;
  3563. for i := 0 to 3 do
  3564. aPixel.Data.arr[i] := (data shr fShift.arr[i]) and Range.arr[i];
  3565. inc(aData, Round(BytesPerPixel));
  3566. end;
  3567. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3568. //TColorTableFormat///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3569. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3570. procedure TbmpColorTableFormat.SetValues;
  3571. begin
  3572. inherited SetValues;
  3573. fShift := glBitmapRec4ub(8, 8, 8, 0);
  3574. end;
  3575. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3576. procedure TbmpColorTableFormat.SetCustomValues(const aFormat: TglBitmapFormat; const aBPP: Integer; const aPrec, aShift: TglBitmapRec4ub);
  3577. begin
  3578. fFormat := aFormat;
  3579. fBitsPerPixel := aBPP;
  3580. fPrecision := aPrec;
  3581. fShift := aShift;
  3582. CalcValues;
  3583. end;
  3584. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3585. procedure TbmpColorTableFormat.CalcValues;
  3586. begin
  3587. inherited CalcValues;
  3588. end;
  3589. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3590. procedure TbmpColorTableFormat.CreateColorTable;
  3591. var
  3592. i: Integer;
  3593. begin
  3594. SetLength(fColorTable, 256);
  3595. if not HasColor then begin
  3596. // alpha
  3597. for i := 0 to High(fColorTable) do begin
  3598. fColorTable[i].r := Round(((i shr Shift.a) and Range.a) / Range.a * 255);
  3599. fColorTable[i].g := Round(((i shr Shift.a) and Range.a) / Range.a * 255);
  3600. fColorTable[i].b := Round(((i shr Shift.a) and Range.a) / Range.a * 255);
  3601. fColorTable[i].a := 0;
  3602. end;
  3603. end else begin
  3604. // normal
  3605. for i := 0 to High(fColorTable) do begin
  3606. fColorTable[i].r := Round(((i shr Shift.r) and Range.r) / Range.r * 255);
  3607. fColorTable[i].g := Round(((i shr Shift.g) and Range.g) / Range.g * 255);
  3608. fColorTable[i].b := Round(((i shr Shift.b) and Range.b) / Range.b * 255);
  3609. fColorTable[i].a := 0;
  3610. end;
  3611. end;
  3612. end;
  3613. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3614. function TbmpColorTableFormat.CreateMappingData: Pointer;
  3615. begin
  3616. result := Pointer(0);
  3617. end;
  3618. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3619. procedure TbmpColorTableFormat.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  3620. begin
  3621. if (BitsPerPixel <> 8) then
  3622. raise EglBitmapUnsupportedFormat.Create('color table are only supported for 8bit formats');
  3623. if not HasColor then
  3624. // alpha
  3625. aData^ := aPixel.Data.a
  3626. else
  3627. // normal
  3628. aData^ := Round(
  3629. ((aPixel.Data.r shr Shift.r) and Range.r) * LUMINANCE_WEIGHT_R +
  3630. ((aPixel.Data.g shr Shift.g) and Range.g) * LUMINANCE_WEIGHT_G +
  3631. ((aPixel.Data.b shr Shift.b) and Range.b) * LUMINANCE_WEIGHT_B);
  3632. inc(aData);
  3633. end;
  3634. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3635. procedure TbmpColorTableFormat.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  3636. function ReadValue: Byte;
  3637. var
  3638. i: PtrUInt;
  3639. begin
  3640. if (BitsPerPixel = 8) then begin
  3641. result := aData^;
  3642. inc(aData);
  3643. end else begin
  3644. i := {%H-}PtrUInt(aMapData);
  3645. if (BitsPerPixel > 1) then
  3646. result := (aData^ shr i) and ((1 shl BitsPerPixel) - 1)
  3647. else
  3648. result := (aData^ shr (7-i)) and ((1 shl BitsPerPixel) - 1);
  3649. inc(i, BitsPerPixel);
  3650. while (i >= 8) do begin
  3651. inc(aData);
  3652. dec(i, 8);
  3653. end;
  3654. aMapData := {%H-}Pointer(i);
  3655. end;
  3656. end;
  3657. begin
  3658. if (BitsPerPixel > 8) then
  3659. raise EglBitmapUnsupportedFormat.Create('color table are only supported for 8bit formats');
  3660. with fColorTable[ReadValue] do begin
  3661. aPixel.Data.r := r;
  3662. aPixel.Data.g := g;
  3663. aPixel.Data.b := b;
  3664. aPixel.Data.a := a;
  3665. end;
  3666. end;
  3667. destructor TbmpColorTableFormat.Destroy;
  3668. begin
  3669. SetLength(fColorTable, 0);
  3670. inherited Destroy;
  3671. end;
  3672. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3673. //TglBitmap - Helper//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3674. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3675. procedure glBitmapConvertPixel(var aPixel: TglBitmapPixelData; const aSourceFD, aDestFD: TFormatDescriptor);
  3676. var
  3677. i: Integer;
  3678. begin
  3679. for i := 0 to 3 do begin
  3680. if (aSourceFD.Range.arr[i] <> aDestFD.Range.arr[i]) then begin
  3681. if (aSourceFD.Range.arr[i] > 0) then
  3682. aPixel.Data.arr[i] := Round(aPixel.Data.arr[i] / aSourceFD.Range.arr[i] * aDestFD.Range.arr[i])
  3683. else
  3684. aPixel.Data.arr[i] := 0;
  3685. end;
  3686. end;
  3687. end;
  3688. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3689. procedure glBitmapConvertCopyFunc(var aFuncRec: TglBitmapFunctionRec);
  3690. begin
  3691. with aFuncRec do begin
  3692. if (Source.Range.r > 0) then
  3693. Dest.Data.r := Source.Data.r;
  3694. if (Source.Range.g > 0) then
  3695. Dest.Data.g := Source.Data.g;
  3696. if (Source.Range.b > 0) then
  3697. Dest.Data.b := Source.Data.b;
  3698. if (Source.Range.a > 0) then
  3699. Dest.Data.a := Source.Data.a;
  3700. end;
  3701. end;
  3702. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3703. procedure glBitmapConvertCalculateRGBAFunc(var aFuncRec: TglBitmapFunctionRec);
  3704. var
  3705. i: Integer;
  3706. begin
  3707. with aFuncRec do begin
  3708. for i := 0 to 3 do
  3709. if (Source.Range.arr[i] > 0) then
  3710. Dest.Data.arr[i] := Round(Dest.Range.arr[i] * Source.Data.arr[i] / Source.Range.arr[i]);
  3711. end;
  3712. end;
  3713. type
  3714. TShiftData = packed record
  3715. case Integer of
  3716. 0: (r, g, b, a: SmallInt);
  3717. 1: (arr: array[0..3] of SmallInt);
  3718. end;
  3719. PShiftData = ^TShiftData;
  3720. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3721. procedure glBitmapConvertShiftRGBAFunc(var aFuncRec: TglBitmapFunctionRec);
  3722. var
  3723. i: Integer;
  3724. begin
  3725. with aFuncRec do
  3726. for i := 0 to 3 do
  3727. if (Source.Range.arr[i] > 0) then
  3728. Dest.Data.arr[i] := Source.Data.arr[i] shr PShiftData(Args)^.arr[i];
  3729. end;
  3730. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3731. procedure glBitmapInvertFunc(var aFuncRec: TglBitmapFunctionRec);
  3732. var
  3733. i: Integer;
  3734. begin
  3735. with aFuncRec do begin
  3736. Dest.Data := Source.Data;
  3737. for i := 0 to 3 do
  3738. if ({%H-}PtrUInt(Args) and (1 shl i) > 0) then
  3739. Dest.Data.arr[i] := Dest.Data.arr[i] xor Dest.Range.arr[i];
  3740. end;
  3741. end;
  3742. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3743. procedure glBitmapFillWithColorFunc(var aFuncRec: TglBitmapFunctionRec);
  3744. var
  3745. i: Integer;
  3746. begin
  3747. with aFuncRec do begin
  3748. for i := 0 to 3 do
  3749. Dest.Data.arr[i] := PglBitmapPixelData(Args)^.Data.arr[i];
  3750. end;
  3751. end;
  3752. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3753. procedure glBitmapAlphaFunc(var FuncRec: TglBitmapFunctionRec);
  3754. var
  3755. Temp: Single;
  3756. begin
  3757. with FuncRec do begin
  3758. if (FuncRec.Args = nil) then begin //source has no alpha
  3759. Temp :=
  3760. Source.Data.r / Source.Range.r * ALPHA_WEIGHT_R +
  3761. Source.Data.g / Source.Range.g * ALPHA_WEIGHT_G +
  3762. Source.Data.b / Source.Range.b * ALPHA_WEIGHT_B;
  3763. Dest.Data.a := Round(Dest.Range.a * Temp);
  3764. end else
  3765. Dest.Data.a := Round(Source.Data.a / Source.Range.a * Dest.Range.a);
  3766. end;
  3767. end;
  3768. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3769. procedure glBitmapColorKeyAlphaFunc(var FuncRec: TglBitmapFunctionRec);
  3770. type
  3771. PglBitmapPixelData = ^TglBitmapPixelData;
  3772. begin
  3773. with FuncRec do begin
  3774. Dest.Data.r := Source.Data.r;
  3775. Dest.Data.g := Source.Data.g;
  3776. Dest.Data.b := Source.Data.b;
  3777. with PglBitmapPixelData(Args)^ do
  3778. if ((Dest.Data.r <= Data.r) and (Dest.Data.r >= Range.r) and
  3779. (Dest.Data.g <= Data.g) and (Dest.Data.g >= Range.g) and
  3780. (Dest.Data.b <= Data.b) and (Dest.Data.b >= Range.b)) then
  3781. Dest.Data.a := 0
  3782. else
  3783. Dest.Data.a := Dest.Range.a;
  3784. end;
  3785. end;
  3786. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3787. procedure glBitmapValueAlphaFunc(var FuncRec: TglBitmapFunctionRec);
  3788. begin
  3789. with FuncRec do begin
  3790. Dest.Data.r := Source.Data.r;
  3791. Dest.Data.g := Source.Data.g;
  3792. Dest.Data.b := Source.Data.b;
  3793. Dest.Data.a := PCardinal(Args)^;
  3794. end;
  3795. end;
  3796. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3797. procedure SwapRGB(aData: PByte; aWidth: Integer; const aHasAlpha: Boolean);
  3798. type
  3799. PRGBPix = ^TRGBPix;
  3800. TRGBPix = array [0..2] of byte;
  3801. var
  3802. Temp: Byte;
  3803. begin
  3804. while aWidth > 0 do begin
  3805. Temp := PRGBPix(aData)^[0];
  3806. PRGBPix(aData)^[0] := PRGBPix(aData)^[2];
  3807. PRGBPix(aData)^[2] := Temp;
  3808. if aHasAlpha then
  3809. Inc(aData, 4)
  3810. else
  3811. Inc(aData, 3);
  3812. dec(aWidth);
  3813. end;
  3814. end;
  3815. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3816. //TglBitmapData///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3817. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3818. function TglBitmapData.GetFormatDescriptor: TglBitmapFormatDescriptor;
  3819. begin
  3820. result := TFormatDescriptor.Get(fFormat);
  3821. end;
  3822. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3823. function TglBitmapData.GetWidth: Integer;
  3824. begin
  3825. if (ffX in fDimension.Fields) then
  3826. result := fDimension.X
  3827. else
  3828. result := -1;
  3829. end;
  3830. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3831. function TglBitmapData.GetHeight: Integer;
  3832. begin
  3833. if (ffY in fDimension.Fields) then
  3834. result := fDimension.Y
  3835. else
  3836. result := -1;
  3837. end;
  3838. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3839. function TglBitmapData.GetScanlines(const aIndex: Integer): PByte;
  3840. begin
  3841. if fHasScanlines and (aIndex >= Low(fScanlines)) and (aIndex <= High(fScanlines)) then
  3842. result := fScanlines[aIndex]
  3843. else
  3844. result := nil;
  3845. end;
  3846. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3847. procedure TglBitmapData.SetFormat(const aValue: TglBitmapFormat);
  3848. begin
  3849. if fFormat = aValue then
  3850. exit;
  3851. if TFormatDescriptor.Get(Format).BitsPerPixel <> TFormatDescriptor.Get(aValue).BitsPerPixel then
  3852. raise EglBitmapUnsupportedFormat.Create(Format);
  3853. SetData(fData, aValue, Width, Height);
  3854. end;
  3855. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3856. procedure TglBitmapData.PrepareResType(var aResource: String; var aResType: PChar);
  3857. var
  3858. TempPos: Integer;
  3859. begin
  3860. if not Assigned(aResType) then begin
  3861. TempPos := Pos('.', aResource);
  3862. aResType := PChar(UpperCase(Copy(aResource, TempPos + 1, Length(aResource) - TempPos)));
  3863. aResource := UpperCase(Copy(aResource, 0, TempPos -1));
  3864. end;
  3865. end;
  3866. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3867. procedure TglBitmapData.UpdateScanlines;
  3868. var
  3869. w, h, i, LineWidth: Integer;
  3870. begin
  3871. w := Width;
  3872. h := Height;
  3873. fHasScanlines := Assigned(fData) and (w > 0) and (h > 0);
  3874. if fHasScanlines then begin
  3875. SetLength(fScanlines, h);
  3876. LineWidth := Trunc(w * FormatDescriptor.BytesPerPixel);
  3877. for i := 0 to h-1 do begin
  3878. fScanlines[i] := fData;
  3879. Inc(fScanlines[i], i * LineWidth);
  3880. end;
  3881. end else
  3882. SetLength(fScanlines, 0);
  3883. end;
  3884. {$IFDEF GLB_SUPPORT_PNG_READ}
  3885. {$IF DEFINED(GLB_LAZ_PNG)}
  3886. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3887. //PNG/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3888. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3889. function TglBitmapData.LoadPNG(const aStream: TStream): Boolean;
  3890. const
  3891. MAGIC_LEN = 8;
  3892. PNG_MAGIC: String[MAGIC_LEN] = #$89#$50#$4E#$47#$0D#$0A#$1A#$0A;
  3893. var
  3894. reader: TLazReaderPNG;
  3895. intf: TLazIntfImage;
  3896. StreamPos: Int64;
  3897. magic: String[MAGIC_LEN];
  3898. begin
  3899. result := true;
  3900. StreamPos := aStream.Position;
  3901. SetLength(magic, MAGIC_LEN);
  3902. aStream.Read(magic[1], MAGIC_LEN);
  3903. aStream.Position := StreamPos;
  3904. if (magic <> PNG_MAGIC) then begin
  3905. result := false;
  3906. exit;
  3907. end;
  3908. intf := TLazIntfImage.Create(0, 0);
  3909. reader := TLazReaderPNG.Create;
  3910. try try
  3911. reader.UpdateDescription := true;
  3912. reader.ImageRead(aStream, intf);
  3913. AssignFromLazIntfImage(intf);
  3914. except
  3915. result := false;
  3916. aStream.Position := StreamPos;
  3917. exit;
  3918. end;
  3919. finally
  3920. reader.Free;
  3921. intf.Free;
  3922. end;
  3923. end;
  3924. {$ELSEIF DEFINED(GLB_SDL_IMAGE)}
  3925. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3926. function TglBitmapData.LoadPNG(const aStream: TStream): Boolean;
  3927. var
  3928. Surface: PSDL_Surface;
  3929. RWops: PSDL_RWops;
  3930. begin
  3931. result := false;
  3932. RWops := glBitmapCreateRWops(aStream);
  3933. try
  3934. if IMG_isPNG(RWops) > 0 then begin
  3935. Surface := IMG_LoadPNG_RW(RWops);
  3936. try
  3937. AssignFromSurface(Surface);
  3938. result := true;
  3939. finally
  3940. SDL_FreeSurface(Surface);
  3941. end;
  3942. end;
  3943. finally
  3944. SDL_FreeRW(RWops);
  3945. end;
  3946. end;
  3947. {$ELSEIF DEFINED(GLB_LIB_PNG)}
  3948. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3949. procedure glBitmap_libPNG_read_func(png: png_structp; buffer: png_bytep; size: cardinal); cdecl;
  3950. begin
  3951. TStream(png_get_io_ptr(png)).Read(buffer^, size);
  3952. end;
  3953. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3954. function TglBitmapData.LoadPNG(const aStream: TStream): Boolean;
  3955. var
  3956. StreamPos: Int64;
  3957. signature: array [0..7] of byte;
  3958. png: png_structp;
  3959. png_info: png_infop;
  3960. TempHeight, TempWidth: Integer;
  3961. Format: TglBitmapFormat;
  3962. png_data: pByte;
  3963. png_rows: array of pByte;
  3964. Row, LineSize: Integer;
  3965. begin
  3966. result := false;
  3967. if not init_libPNG then
  3968. raise Exception.Create('LoadPNG - unable to initialize libPNG.');
  3969. try
  3970. // signature
  3971. StreamPos := aStream.Position;
  3972. aStream.Read(signature{%H-}, 8);
  3973. aStream.Position := StreamPos;
  3974. if png_check_sig(@signature, 8) <> 0 then begin
  3975. // png read struct
  3976. png := png_create_read_struct(PNG_LIBPNG_VER_STRING, nil, nil, nil);
  3977. if png = nil then
  3978. raise EglBitmapException.Create('LoadPng - couldn''t create read struct.');
  3979. // png info
  3980. png_info := png_create_info_struct(png);
  3981. if png_info = nil then begin
  3982. png_destroy_read_struct(@png, nil, nil);
  3983. raise EglBitmapException.Create('LoadPng - couldn''t create info struct.');
  3984. end;
  3985. // set read callback
  3986. png_set_read_fn(png, aStream, glBitmap_libPNG_read_func);
  3987. // read informations
  3988. png_read_info(png, png_info);
  3989. // size
  3990. TempHeight := png_get_image_height(png, png_info);
  3991. TempWidth := png_get_image_width(png, png_info);
  3992. // format
  3993. case png_get_color_type(png, png_info) of
  3994. PNG_COLOR_TYPE_GRAY:
  3995. Format := tfLuminance8ub1;
  3996. PNG_COLOR_TYPE_GRAY_ALPHA:
  3997. Format := tfLuminance8Alpha8us1;
  3998. PNG_COLOR_TYPE_RGB:
  3999. Format := tfRGB8ub3;
  4000. PNG_COLOR_TYPE_RGB_ALPHA:
  4001. Format := tfRGBA8ub4;
  4002. else
  4003. raise EglBitmapException.Create ('LoadPng - Unsupported Colortype found.');
  4004. end;
  4005. // cut upper 8 bit from 16 bit formats
  4006. if png_get_bit_depth(png, png_info) > 8 then
  4007. png_set_strip_16(png);
  4008. // expand bitdepth smaller than 8
  4009. if png_get_bit_depth(png, png_info) < 8 then
  4010. png_set_expand(png);
  4011. // allocating mem for scanlines
  4012. LineSize := png_get_rowbytes(png, png_info);
  4013. GetMem(png_data, TempHeight * LineSize);
  4014. try
  4015. SetLength(png_rows, TempHeight);
  4016. for Row := Low(png_rows) to High(png_rows) do begin
  4017. png_rows[Row] := png_data;
  4018. Inc(png_rows[Row], Row * LineSize);
  4019. end;
  4020. // read complete image into scanlines
  4021. png_read_image(png, @png_rows[0]);
  4022. // read end
  4023. png_read_end(png, png_info);
  4024. // destroy read struct
  4025. png_destroy_read_struct(@png, @png_info, nil);
  4026. SetLength(png_rows, 0);
  4027. // set new data
  4028. SetData(png_data, Format, TempWidth, TempHeight);
  4029. result := true;
  4030. except
  4031. if Assigned(png_data) then
  4032. FreeMem(png_data);
  4033. raise;
  4034. end;
  4035. end;
  4036. finally
  4037. quit_libPNG;
  4038. end;
  4039. end;
  4040. {$ELSEIF DEFINED(GLB_PNGIMAGE)}
  4041. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4042. function TglBitmapData.LoadPNG(const aStream: TStream): Boolean;
  4043. var
  4044. StreamPos: Int64;
  4045. Png: TPNGObject;
  4046. Header: String[8];
  4047. Row, Col, PixSize, LineSize: Integer;
  4048. NewImage, pSource, pDest, pAlpha: pByte;
  4049. PngFormat: TglBitmapFormat;
  4050. FormatDesc: TFormatDescriptor;
  4051. const
  4052. PngHeader: String[8] = #137#80#78#71#13#10#26#10;
  4053. begin
  4054. result := false;
  4055. StreamPos := aStream.Position;
  4056. aStream.Read(Header[0], SizeOf(Header));
  4057. aStream.Position := StreamPos;
  4058. {Test if the header matches}
  4059. if Header = PngHeader then begin
  4060. Png := TPNGObject.Create;
  4061. try
  4062. Png.LoadFromStream(aStream);
  4063. case Png.Header.ColorType of
  4064. COLOR_GRAYSCALE:
  4065. PngFormat := tfLuminance8ub1;
  4066. COLOR_GRAYSCALEALPHA:
  4067. PngFormat := tfLuminance8Alpha8us1;
  4068. COLOR_RGB:
  4069. PngFormat := tfBGR8ub3;
  4070. COLOR_RGBALPHA:
  4071. PngFormat := tfBGRA8ub4;
  4072. else
  4073. raise EglBitmapException.Create ('LoadPng - Unsupported Colortype found.');
  4074. end;
  4075. FormatDesc := TFormatDescriptor.Get(PngFormat);
  4076. PixSize := Round(FormatDesc.PixelSize);
  4077. LineSize := FormatDesc.GetSize(Png.Header.Width, 1);
  4078. GetMem(NewImage, LineSize * Integer(Png.Header.Height));
  4079. try
  4080. pDest := NewImage;
  4081. case Png.Header.ColorType of
  4082. COLOR_RGB, COLOR_GRAYSCALE:
  4083. begin
  4084. for Row := 0 to Png.Height -1 do begin
  4085. Move (Png.Scanline[Row]^, pDest^, LineSize);
  4086. Inc(pDest, LineSize);
  4087. end;
  4088. end;
  4089. COLOR_RGBALPHA, COLOR_GRAYSCALEALPHA:
  4090. begin
  4091. PixSize := PixSize -1;
  4092. for Row := 0 to Png.Height -1 do begin
  4093. pSource := Png.Scanline[Row];
  4094. pAlpha := pByte(Png.AlphaScanline[Row]);
  4095. for Col := 0 to Png.Width -1 do begin
  4096. Move (pSource^, pDest^, PixSize);
  4097. Inc(pSource, PixSize);
  4098. Inc(pDest, PixSize);
  4099. pDest^ := pAlpha^;
  4100. inc(pAlpha);
  4101. Inc(pDest);
  4102. end;
  4103. end;
  4104. end;
  4105. else
  4106. raise EglBitmapException.Create ('LoadPng - Unsupported Colortype found.');
  4107. end;
  4108. SetData(NewImage, PngFormat, Png.Header.Width, Png.Header.Height);
  4109. result := true;
  4110. except
  4111. if Assigned(NewImage) then
  4112. FreeMem(NewImage);
  4113. raise;
  4114. end;
  4115. finally
  4116. Png.Free;
  4117. end;
  4118. end;
  4119. end;
  4120. {$IFEND}
  4121. {$ENDIF}
  4122. {$IFDEF GLB_SUPPORT_PNG_WRITE}
  4123. {$IFDEF GLB_LIB_PNG}
  4124. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4125. procedure glBitmap_libPNG_write_func(png: png_structp; buffer: png_bytep; size: cardinal); cdecl;
  4126. begin
  4127. TStream(png_get_io_ptr(png)).Write(buffer^, size);
  4128. end;
  4129. {$ENDIF}
  4130. {$IF DEFINED(GLB_LAZ_PNG)}
  4131. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4132. procedure TglBitmapData.SavePNG(const aStream: TStream);
  4133. var
  4134. png: TPortableNetworkGraphic;
  4135. intf: TLazIntfImage;
  4136. raw: TRawImage;
  4137. begin
  4138. png := TPortableNetworkGraphic.Create;
  4139. intf := TLazIntfImage.Create(0, 0);
  4140. try
  4141. if not AssignToLazIntfImage(intf) then
  4142. raise EglBitmap.Create('unable to create LazIntfImage from glBitmap');
  4143. intf.GetRawImage(raw);
  4144. png.LoadFromRawImage(raw, false);
  4145. png.SaveToStream(aStream);
  4146. finally
  4147. png.Free;
  4148. intf.Free;
  4149. end;
  4150. end;
  4151. {$ELSEIF DEFINED(GLB_LIB_PNG)}
  4152. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4153. procedure TglBitmapData.SavePNG(const aStream: TStream);
  4154. var
  4155. png: png_structp;
  4156. png_info: png_infop;
  4157. png_rows: array of pByte;
  4158. LineSize: Integer;
  4159. ColorType: Integer;
  4160. Row: Integer;
  4161. FormatDesc: TFormatDescriptor;
  4162. begin
  4163. if not (ftPNG in FormatGetSupportedFiles(Format)) then
  4164. raise EglBitmapUnsupportedFormat.Create(Format);
  4165. if not init_libPNG then
  4166. raise Exception.Create('unable to initialize libPNG.');
  4167. try
  4168. case Format of
  4169. tfAlpha8ub1, tfLuminance8ub1:
  4170. ColorType := PNG_COLOR_TYPE_GRAY;
  4171. tfLuminance8Alpha8us1:
  4172. ColorType := PNG_COLOR_TYPE_GRAY_ALPHA;
  4173. tfBGR8ub3, tfRGB8ub3:
  4174. ColorType := PNG_COLOR_TYPE_RGB;
  4175. tfBGRA8ub4, tfRGBA8ub4:
  4176. ColorType := PNG_COLOR_TYPE_RGBA;
  4177. else
  4178. raise EglBitmapUnsupportedFormat.Create(Format);
  4179. end;
  4180. FormatDesc := TFormatDescriptor.Get(Format);
  4181. LineSize := FormatDesc.GetSize(Width, 1);
  4182. // creating array for scanline
  4183. SetLength(png_rows, Height);
  4184. try
  4185. for Row := 0 to Height - 1 do begin
  4186. png_rows[Row] := Data;
  4187. Inc(png_rows[Row], Row * LineSize)
  4188. end;
  4189. // write struct
  4190. png := png_create_write_struct(PNG_LIBPNG_VER_STRING, nil, nil, nil);
  4191. if png = nil then
  4192. raise EglBitmapException.Create('SavePng - couldn''t create write struct.');
  4193. // create png info
  4194. png_info := png_create_info_struct(png);
  4195. if png_info = nil then begin
  4196. png_destroy_write_struct(@png, nil);
  4197. raise EglBitmapException.Create('SavePng - couldn''t create info struct.');
  4198. end;
  4199. // set read callback
  4200. png_set_write_fn(png, aStream, glBitmap_libPNG_write_func, nil);
  4201. // set compression
  4202. png_set_compression_level(png, 6);
  4203. if Format in [tfBGR8ub3, tfBGRA8ub4] then
  4204. png_set_bgr(png);
  4205. png_set_IHDR(png, png_info, Width, Height, 8, ColorType, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
  4206. png_write_info(png, png_info);
  4207. png_write_image(png, @png_rows[0]);
  4208. png_write_end(png, png_info);
  4209. png_destroy_write_struct(@png, @png_info);
  4210. finally
  4211. SetLength(png_rows, 0);
  4212. end;
  4213. finally
  4214. quit_libPNG;
  4215. end;
  4216. end;
  4217. {$ELSEIF DEFINED(GLB_PNGIMAGE)}
  4218. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4219. procedure TglBitmapData.SavePNG(const aStream: TStream);
  4220. var
  4221. Png: TPNGObject;
  4222. pSource, pDest: pByte;
  4223. X, Y, PixSize: Integer;
  4224. ColorType: Cardinal;
  4225. Alpha: Boolean;
  4226. pTemp: pByte;
  4227. Temp: Byte;
  4228. begin
  4229. if not (ftPNG in FormatGetSupportedFiles (Format)) then
  4230. raise EglBitmapUnsupportedFormat.Create(Format);
  4231. case Format of
  4232. tfAlpha8ub1, tfLuminance8ub1: begin
  4233. ColorType := COLOR_GRAYSCALE;
  4234. PixSize := 1;
  4235. Alpha := false;
  4236. end;
  4237. tfLuminance8Alpha8us1: begin
  4238. ColorType := COLOR_GRAYSCALEALPHA;
  4239. PixSize := 1;
  4240. Alpha := true;
  4241. end;
  4242. tfBGR8ub3, tfRGB8ub3: begin
  4243. ColorType := COLOR_RGB;
  4244. PixSize := 3;
  4245. Alpha := false;
  4246. end;
  4247. tfBGRA8ub4, tfRGBA8ub4: begin
  4248. ColorType := COLOR_RGBALPHA;
  4249. PixSize := 3;
  4250. Alpha := true
  4251. end;
  4252. else
  4253. raise EglBitmapUnsupportedFormat.Create(Format);
  4254. end;
  4255. Png := TPNGObject.CreateBlank(ColorType, 8, Width, Height);
  4256. try
  4257. // Copy ImageData
  4258. pSource := Data;
  4259. for Y := 0 to Height -1 do begin
  4260. pDest := png.ScanLine[Y];
  4261. for X := 0 to Width -1 do begin
  4262. Move(pSource^, pDest^, PixSize);
  4263. Inc(pDest, PixSize);
  4264. Inc(pSource, PixSize);
  4265. if Alpha then begin
  4266. png.AlphaScanline[Y]^[X] := pSource^;
  4267. Inc(pSource);
  4268. end;
  4269. end;
  4270. // convert RGB line to BGR
  4271. if Format in [tfRGB8ub3, tfRGBA8ub4] then begin
  4272. pTemp := png.ScanLine[Y];
  4273. for X := 0 to Width -1 do begin
  4274. Temp := pByteArray(pTemp)^[0];
  4275. pByteArray(pTemp)^[0] := pByteArray(pTemp)^[2];
  4276. pByteArray(pTemp)^[2] := Temp;
  4277. Inc(pTemp, 3);
  4278. end;
  4279. end;
  4280. end;
  4281. // Save to Stream
  4282. Png.CompressionLevel := 6;
  4283. Png.SaveToStream(aStream);
  4284. finally
  4285. FreeAndNil(Png);
  4286. end;
  4287. end;
  4288. {$IFEND}
  4289. {$ENDIF}
  4290. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4291. //JPEG////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4292. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4293. {$IFDEF GLB_LIB_JPEG}
  4294. type
  4295. glBitmap_libJPEG_source_mgr_ptr = ^glBitmap_libJPEG_source_mgr;
  4296. glBitmap_libJPEG_source_mgr = record
  4297. pub: jpeg_source_mgr;
  4298. SrcStream: TStream;
  4299. SrcBuffer: array [1..4096] of byte;
  4300. end;
  4301. glBitmap_libJPEG_dest_mgr_ptr = ^glBitmap_libJPEG_dest_mgr;
  4302. glBitmap_libJPEG_dest_mgr = record
  4303. pub: jpeg_destination_mgr;
  4304. DestStream: TStream;
  4305. DestBuffer: array [1..4096] of byte;
  4306. end;
  4307. procedure glBitmap_libJPEG_error_exit(cinfo: j_common_ptr); cdecl;
  4308. begin
  4309. //DUMMY
  4310. end;
  4311. procedure glBitmap_libJPEG_output_message(cinfo: j_common_ptr); cdecl;
  4312. begin
  4313. //DUMMY
  4314. end;
  4315. procedure glBitmap_libJPEG_init_source(cinfo: j_decompress_ptr); cdecl;
  4316. begin
  4317. //DUMMY
  4318. end;
  4319. procedure glBitmap_libJPEG_term_source(cinfo: j_decompress_ptr); cdecl;
  4320. begin
  4321. //DUMMY
  4322. end;
  4323. procedure glBitmap_libJPEG_init_destination(cinfo: j_compress_ptr); cdecl;
  4324. begin
  4325. //DUMMY
  4326. end;
  4327. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4328. function glBitmap_libJPEG_fill_input_buffer(cinfo: j_decompress_ptr): boolean; cdecl;
  4329. var
  4330. src: glBitmap_libJPEG_source_mgr_ptr;
  4331. bytes: integer;
  4332. begin
  4333. src := glBitmap_libJPEG_source_mgr_ptr(cinfo^.src);
  4334. bytes := src^.SrcStream.Read(src^.SrcBuffer[1], 4096);
  4335. if (bytes <= 0) then begin
  4336. src^.SrcBuffer[1] := $FF;
  4337. src^.SrcBuffer[2] := JPEG_EOI;
  4338. bytes := 2;
  4339. end;
  4340. src^.pub.next_input_byte := @(src^.SrcBuffer[1]);
  4341. src^.pub.bytes_in_buffer := bytes;
  4342. result := true;
  4343. end;
  4344. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4345. procedure glBitmap_libJPEG_skip_input_data(cinfo: j_decompress_ptr; num_bytes: Longint); cdecl;
  4346. var
  4347. src: glBitmap_libJPEG_source_mgr_ptr;
  4348. begin
  4349. src := glBitmap_libJPEG_source_mgr_ptr(cinfo^.src);
  4350. if num_bytes > 0 then begin
  4351. // wanted byte isn't in buffer so set stream position and read buffer
  4352. if num_bytes > src^.pub.bytes_in_buffer then begin
  4353. src^.SrcStream.Position := src^.SrcStream.Position + num_bytes - src^.pub.bytes_in_buffer;
  4354. src^.pub.fill_input_buffer(cinfo);
  4355. end else begin
  4356. // wanted byte is in buffer so only skip
  4357. inc(src^.pub.next_input_byte, num_bytes);
  4358. dec(src^.pub.bytes_in_buffer, num_bytes);
  4359. end;
  4360. end;
  4361. end;
  4362. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4363. function glBitmap_libJPEG_empty_output_buffer(cinfo: j_compress_ptr): boolean; cdecl;
  4364. var
  4365. dest: glBitmap_libJPEG_dest_mgr_ptr;
  4366. begin
  4367. dest := glBitmap_libJPEG_dest_mgr_ptr(cinfo^.dest);
  4368. if dest^.pub.free_in_buffer < Cardinal(Length(dest^.DestBuffer)) then begin
  4369. // write complete buffer
  4370. dest^.DestStream.Write(dest^.DestBuffer[1], SizeOf(dest^.DestBuffer));
  4371. // reset buffer
  4372. dest^.pub.next_output_byte := @dest^.DestBuffer[1];
  4373. dest^.pub.free_in_buffer := Length(dest^.DestBuffer);
  4374. end;
  4375. result := true;
  4376. end;
  4377. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4378. procedure glBitmap_libJPEG_term_destination(cinfo: j_compress_ptr); cdecl;
  4379. var
  4380. Idx: Integer;
  4381. dest: glBitmap_libJPEG_dest_mgr_ptr;
  4382. begin
  4383. dest := glBitmap_libJPEG_dest_mgr_ptr(cinfo^.dest);
  4384. for Idx := Low(dest^.DestBuffer) to High(dest^.DestBuffer) do begin
  4385. // check for endblock
  4386. if (Idx < High(dest^.DestBuffer)) and (dest^.DestBuffer[Idx] = $FF) and (dest^.DestBuffer[Idx +1] = JPEG_EOI) then begin
  4387. // write endblock
  4388. dest^.DestStream.Write(dest^.DestBuffer[Idx], 2);
  4389. // leave
  4390. break;
  4391. end else
  4392. dest^.DestStream.Write(dest^.DestBuffer[Idx], 1);
  4393. end;
  4394. end;
  4395. {$ENDIF}
  4396. {$IFDEF GLB_SUPPORT_JPEG_READ}
  4397. {$IF DEFINED(GLB_LAZ_JPEG)}
  4398. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4399. function TglBitmapData.LoadJPEG(const aStream: TStream): Boolean;
  4400. const
  4401. MAGIC_LEN = 2;
  4402. JPEG_MAGIC: String[MAGIC_LEN] = #$FF#$D8;
  4403. var
  4404. intf: TLazIntfImage;
  4405. reader: TFPReaderJPEG;
  4406. StreamPos: Int64;
  4407. magic: String[MAGIC_LEN];
  4408. begin
  4409. result := true;
  4410. StreamPos := aStream.Position;
  4411. SetLength(magic, MAGIC_LEN);
  4412. aStream.Read(magic[1], MAGIC_LEN);
  4413. aStream.Position := StreamPos;
  4414. if (magic <> JPEG_MAGIC) then begin
  4415. result := false;
  4416. exit;
  4417. end;
  4418. reader := TFPReaderJPEG.Create;
  4419. intf := TLazIntfImage.Create(0, 0);
  4420. try try
  4421. intf.DataDescription := GetDescriptionFromDevice(0, 0, 0);
  4422. reader.ImageRead(aStream, intf);
  4423. AssignFromLazIntfImage(intf);
  4424. except
  4425. result := false;
  4426. aStream.Position := StreamPos;
  4427. exit;
  4428. end;
  4429. finally
  4430. reader.Free;
  4431. intf.Free;
  4432. end;
  4433. end;
  4434. {$ELSEIF DEFINED(GLB_SDL_IMAGE)}
  4435. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4436. function TglBitmapData.LoadJPEG(const aStream: TStream): Boolean;
  4437. var
  4438. Surface: PSDL_Surface;
  4439. RWops: PSDL_RWops;
  4440. begin
  4441. result := false;
  4442. RWops := glBitmapCreateRWops(aStream);
  4443. try
  4444. if IMG_isJPG(RWops) > 0 then begin
  4445. Surface := IMG_LoadJPG_RW(RWops);
  4446. try
  4447. AssignFromSurface(Surface);
  4448. result := true;
  4449. finally
  4450. SDL_FreeSurface(Surface);
  4451. end;
  4452. end;
  4453. finally
  4454. SDL_FreeRW(RWops);
  4455. end;
  4456. end;
  4457. {$ELSEIF DEFINED(GLB_LIB_JPEG)}
  4458. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4459. function TglBitmapData.LoadJPEG(const aStream: TStream): Boolean;
  4460. var
  4461. StreamPos: Int64;
  4462. Temp: array[0..1]of Byte;
  4463. jpeg: jpeg_decompress_struct;
  4464. jpeg_err: jpeg_error_mgr;
  4465. IntFormat: TglBitmapFormat;
  4466. pImage: pByte;
  4467. TempHeight, TempWidth: Integer;
  4468. pTemp: pByte;
  4469. Row: Integer;
  4470. FormatDesc: TFormatDescriptor;
  4471. begin
  4472. result := false;
  4473. if not init_libJPEG then
  4474. raise Exception.Create('LoadJPG - unable to initialize libJPEG.');
  4475. try
  4476. // reading first two bytes to test file and set cursor back to begin
  4477. StreamPos := aStream.Position;
  4478. aStream.Read({%H-}Temp[0], 2);
  4479. aStream.Position := StreamPos;
  4480. // if Bitmap then read file.
  4481. if ((Temp[0] = $FF) and (Temp[1] = $D8)) then begin
  4482. FillChar(jpeg{%H-}, SizeOf(jpeg_decompress_struct), $00);
  4483. FillChar(jpeg_err{%H-}, SizeOf(jpeg_error_mgr), $00);
  4484. // error managment
  4485. jpeg.err := jpeg_std_error(@jpeg_err);
  4486. jpeg_err.error_exit := glBitmap_libJPEG_error_exit;
  4487. jpeg_err.output_message := glBitmap_libJPEG_output_message;
  4488. // decompression struct
  4489. jpeg_create_decompress(@jpeg);
  4490. // allocation space for streaming methods
  4491. jpeg.src := jpeg.mem^.alloc_small(@jpeg, JPOOL_PERMANENT, SizeOf(glBitmap_libJPEG_source_mgr));
  4492. // seeting up custom functions
  4493. with glBitmap_libJPEG_source_mgr_ptr(jpeg.src)^ do begin
  4494. pub.init_source := glBitmap_libJPEG_init_source;
  4495. pub.fill_input_buffer := glBitmap_libJPEG_fill_input_buffer;
  4496. pub.skip_input_data := glBitmap_libJPEG_skip_input_data;
  4497. pub.resync_to_restart := jpeg_resync_to_restart; // use default method
  4498. pub.term_source := glBitmap_libJPEG_term_source;
  4499. pub.bytes_in_buffer := 0; // forces fill_input_buffer on first read
  4500. pub.next_input_byte := nil; // until buffer loaded
  4501. SrcStream := aStream;
  4502. end;
  4503. // set global decoding state
  4504. jpeg.global_state := DSTATE_START;
  4505. // read header of jpeg
  4506. jpeg_read_header(@jpeg, false);
  4507. // setting output parameter
  4508. case jpeg.jpeg_color_space of
  4509. JCS_GRAYSCALE:
  4510. begin
  4511. jpeg.out_color_space := JCS_GRAYSCALE;
  4512. IntFormat := tfLuminance8ub1;
  4513. end;
  4514. else
  4515. jpeg.out_color_space := JCS_RGB;
  4516. IntFormat := tfRGB8ub3;
  4517. end;
  4518. // reading image
  4519. jpeg_start_decompress(@jpeg);
  4520. TempHeight := jpeg.output_height;
  4521. TempWidth := jpeg.output_width;
  4522. FormatDesc := TFormatDescriptor.Get(IntFormat);
  4523. // creating new image
  4524. GetMem(pImage, FormatDesc.GetSize(TempWidth, TempHeight));
  4525. try
  4526. pTemp := pImage;
  4527. for Row := 0 to TempHeight -1 do begin
  4528. jpeg_read_scanlines(@jpeg, @pTemp, 1);
  4529. Inc(pTemp, FormatDesc.GetSize(TempWidth, 1));
  4530. end;
  4531. // finish decompression
  4532. jpeg_finish_decompress(@jpeg);
  4533. // destroy decompression
  4534. jpeg_destroy_decompress(@jpeg);
  4535. SetData(pImage, IntFormat, TempWidth, TempHeight);
  4536. result := true;
  4537. except
  4538. if Assigned(pImage) then
  4539. FreeMem(pImage);
  4540. raise;
  4541. end;
  4542. end;
  4543. finally
  4544. quit_libJPEG;
  4545. end;
  4546. end;
  4547. {$ELSEIF DEFINED(GLB_DELPHI_JPEG)}
  4548. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4549. function TglBitmapData.LoadJPEG(const aStream: TStream): Boolean;
  4550. var
  4551. bmp: TBitmap;
  4552. jpg: TJPEGImage;
  4553. StreamPos: Int64;
  4554. Temp: array[0..1]of Byte;
  4555. begin
  4556. result := false;
  4557. // reading first two bytes to test file and set cursor back to begin
  4558. StreamPos := aStream.Position;
  4559. aStream.Read(Temp[0], 2);
  4560. aStream.Position := StreamPos;
  4561. // if Bitmap then read file.
  4562. if ((Temp[0] = $FF) and (Temp[1] = $D8)) then begin
  4563. bmp := TBitmap.Create;
  4564. try
  4565. jpg := TJPEGImage.Create;
  4566. try
  4567. jpg.LoadFromStream(aStream);
  4568. bmp.Assign(jpg);
  4569. result := AssignFromBitmap(bmp);
  4570. finally
  4571. jpg.Free;
  4572. end;
  4573. finally
  4574. bmp.Free;
  4575. end;
  4576. end;
  4577. end;
  4578. {$IFEND}
  4579. {$ENDIF}
  4580. {$IFDEF GLB_SUPPORT_JPEG_WRITE}
  4581. {$IF DEFINED(GLB_LAZ_JPEG)}
  4582. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4583. procedure TglBitmapData.SaveJPEG(const aStream: TStream);
  4584. var
  4585. jpeg: TJPEGImage;
  4586. intf: TLazIntfImage;
  4587. raw: TRawImage;
  4588. begin
  4589. jpeg := TJPEGImage.Create;
  4590. intf := TLazIntfImage.Create(0, 0);
  4591. try
  4592. if not AssignToLazIntfImage(intf) then
  4593. raise EglBitmap.Create('unable to create LazIntfImage from glBitmap');
  4594. intf.GetRawImage(raw);
  4595. jpeg.LoadFromRawImage(raw, false);
  4596. jpeg.SaveToStream(aStream);
  4597. finally
  4598. intf.Free;
  4599. jpeg.Free;
  4600. end;
  4601. end;
  4602. {$ELSEIF DEFINED(GLB_LIB_JPEG)}
  4603. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4604. procedure TglBitmapData.SaveJPEG(const aStream: TStream);
  4605. var
  4606. jpeg: jpeg_compress_struct;
  4607. jpeg_err: jpeg_error_mgr;
  4608. Row: Integer;
  4609. pTemp, pTemp2: pByte;
  4610. procedure CopyRow(pDest, pSource: pByte);
  4611. var
  4612. X: Integer;
  4613. begin
  4614. for X := 0 to Width - 1 do begin
  4615. pByteArray(pDest)^[0] := pByteArray(pSource)^[2];
  4616. pByteArray(pDest)^[1] := pByteArray(pSource)^[1];
  4617. pByteArray(pDest)^[2] := pByteArray(pSource)^[0];
  4618. Inc(pDest, 3);
  4619. Inc(pSource, 3);
  4620. end;
  4621. end;
  4622. begin
  4623. if not (ftJPEG in FormatGetSupportedFiles(Format)) then
  4624. raise EglBitmapUnsupportedFormat.Create(Format);
  4625. if not init_libJPEG then
  4626. raise Exception.Create('SaveJPG - unable to initialize libJPEG.');
  4627. try
  4628. FillChar(jpeg{%H-}, SizeOf(jpeg_compress_struct), $00);
  4629. FillChar(jpeg_err{%H-}, SizeOf(jpeg_error_mgr), $00);
  4630. // error managment
  4631. jpeg.err := jpeg_std_error(@jpeg_err);
  4632. jpeg_err.error_exit := glBitmap_libJPEG_error_exit;
  4633. jpeg_err.output_message := glBitmap_libJPEG_output_message;
  4634. // compression struct
  4635. jpeg_create_compress(@jpeg);
  4636. // allocation space for streaming methods
  4637. jpeg.dest := jpeg.mem^.alloc_small(@jpeg, JPOOL_PERMANENT, SizeOf(glBitmap_libJPEG_dest_mgr));
  4638. // seeting up custom functions
  4639. with glBitmap_libJPEG_dest_mgr_ptr(jpeg.dest)^ do begin
  4640. pub.init_destination := glBitmap_libJPEG_init_destination;
  4641. pub.empty_output_buffer := glBitmap_libJPEG_empty_output_buffer;
  4642. pub.term_destination := glBitmap_libJPEG_term_destination;
  4643. pub.next_output_byte := @DestBuffer[1];
  4644. pub.free_in_buffer := Length(DestBuffer);
  4645. DestStream := aStream;
  4646. end;
  4647. // very important state
  4648. jpeg.global_state := CSTATE_START;
  4649. jpeg.image_width := Width;
  4650. jpeg.image_height := Height;
  4651. case Format of
  4652. tfAlpha8ub1, tfLuminance8ub1: begin
  4653. jpeg.input_components := 1;
  4654. jpeg.in_color_space := JCS_GRAYSCALE;
  4655. end;
  4656. tfRGB8ub3, tfBGR8ub3: begin
  4657. jpeg.input_components := 3;
  4658. jpeg.in_color_space := JCS_RGB;
  4659. end;
  4660. end;
  4661. jpeg_set_defaults(@jpeg);
  4662. jpeg_set_quality(@jpeg, 95, true);
  4663. jpeg_start_compress(@jpeg, true);
  4664. pTemp := Data;
  4665. if Format = tfBGR8ub3 then
  4666. GetMem(pTemp2, fRowSize)
  4667. else
  4668. pTemp2 := pTemp;
  4669. try
  4670. for Row := 0 to jpeg.image_height -1 do begin
  4671. // prepare row
  4672. if Format = tfBGR8ub3 then
  4673. CopyRow(pTemp2, pTemp)
  4674. else
  4675. pTemp2 := pTemp;
  4676. // write row
  4677. jpeg_write_scanlines(@jpeg, @pTemp2, 1);
  4678. inc(pTemp, fRowSize);
  4679. end;
  4680. finally
  4681. // free memory
  4682. if Format = tfBGR8ub3 then
  4683. FreeMem(pTemp2);
  4684. end;
  4685. jpeg_finish_compress(@jpeg);
  4686. jpeg_destroy_compress(@jpeg);
  4687. finally
  4688. quit_libJPEG;
  4689. end;
  4690. end;
  4691. {$ELSEIF DEFINED(GLB_DELPHI_JPEG)}
  4692. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4693. procedure TglBitmapData.SaveJPEG(const aStream: TStream);
  4694. var
  4695. Bmp: TBitmap;
  4696. Jpg: TJPEGImage;
  4697. begin
  4698. if not (ftJPEG in FormatGetSupportedFiles(Format)) then
  4699. raise EglBitmapUnsupportedFormat.Create(Format);
  4700. Bmp := TBitmap.Create;
  4701. try
  4702. Jpg := TJPEGImage.Create;
  4703. try
  4704. AssignToBitmap(Bmp);
  4705. if (Format in [tfAlpha8ub1, tfLuminance8ub1]) then begin
  4706. Jpg.Grayscale := true;
  4707. Jpg.PixelFormat := jf8Bit;
  4708. end;
  4709. Jpg.Assign(Bmp);
  4710. Jpg.SaveToStream(aStream);
  4711. finally
  4712. FreeAndNil(Jpg);
  4713. end;
  4714. finally
  4715. FreeAndNil(Bmp);
  4716. end;
  4717. end;
  4718. {$IFEND}
  4719. {$ENDIF}
  4720. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4721. //RAW/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4722. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4723. type
  4724. RawHeader = packed record
  4725. Magic: String[5];
  4726. Version: Byte;
  4727. Width: Integer;
  4728. Height: Integer;
  4729. DataSize: Integer;
  4730. BitsPerPixel: Integer;
  4731. Precision: TglBitmapRec4ub;
  4732. Shift: TglBitmapRec4ub;
  4733. end;
  4734. function TglBitmapData.LoadRAW(const aStream: TStream): Boolean;
  4735. var
  4736. header: RawHeader;
  4737. StartPos: Int64;
  4738. fd: TFormatDescriptor;
  4739. buf: PByte;
  4740. begin
  4741. result := false;
  4742. StartPos := aStream.Position;
  4743. aStream.Read(header{%H-}, SizeOf(header));
  4744. if (header.Magic <> 'glBMP') then begin
  4745. aStream.Position := StartPos;
  4746. exit;
  4747. end;
  4748. fd := TFormatDescriptor.GetFromPrecShift(header.Precision, header.Shift, header.BitsPerPixel);
  4749. if (fd.Format = tfEmpty) then
  4750. raise EglBitmapUnsupportedFormat.Create('no supported format found');
  4751. buf := GetMemory(header.DataSize);
  4752. aStream.Read(buf^, header.DataSize);
  4753. SetData(buf, fd.Format, header.Width, header.Height);
  4754. result := true;
  4755. end;
  4756. procedure TglBitmapData.SaveRAW(const aStream: TStream);
  4757. var
  4758. header: RawHeader;
  4759. fd: TFormatDescriptor;
  4760. begin
  4761. fd := TFormatDescriptor.Get(Format);
  4762. header.Magic := 'glBMP';
  4763. header.Version := 1;
  4764. header.Width := Width;
  4765. header.Height := Height;
  4766. header.DataSize := fd.GetSize(fDimension);
  4767. header.BitsPerPixel := fd.BitsPerPixel;
  4768. header.Precision := fd.Precision;
  4769. header.Shift := fd.Shift;
  4770. aStream.Write(header, SizeOf(header));
  4771. aStream.Write(Data^, header.DataSize);
  4772. end;
  4773. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4774. //BMP/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4775. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4776. const
  4777. BMP_MAGIC = $4D42;
  4778. BMP_COMP_RGB = 0;
  4779. BMP_COMP_RLE8 = 1;
  4780. BMP_COMP_RLE4 = 2;
  4781. BMP_COMP_BITFIELDS = 3;
  4782. type
  4783. TBMPHeader = packed record
  4784. bfType: Word;
  4785. bfSize: Cardinal;
  4786. bfReserved1: Word;
  4787. bfReserved2: Word;
  4788. bfOffBits: Cardinal;
  4789. end;
  4790. TBMPInfo = packed record
  4791. biSize: Cardinal;
  4792. biWidth: Longint;
  4793. biHeight: Longint;
  4794. biPlanes: Word;
  4795. biBitCount: Word;
  4796. biCompression: Cardinal;
  4797. biSizeImage: Cardinal;
  4798. biXPelsPerMeter: Longint;
  4799. biYPelsPerMeter: Longint;
  4800. biClrUsed: Cardinal;
  4801. biClrImportant: Cardinal;
  4802. end;
  4803. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4804. function TglBitmapData.LoadBMP(const aStream: TStream): Boolean;
  4805. //////////////////////////////////////////////////////////////////////////////////////////////////
  4806. function ReadInfo(out aInfo: TBMPInfo; out aMask: TglBitmapRec4ul): TglBitmapFormat;
  4807. var
  4808. tmp, i: Cardinal;
  4809. begin
  4810. result := tfEmpty;
  4811. aStream.Read(aInfo{%H-}, SizeOf(aInfo));
  4812. FillChar(aMask{%H-}, SizeOf(aMask), 0);
  4813. //Read Compression
  4814. case aInfo.biCompression of
  4815. BMP_COMP_RLE4,
  4816. BMP_COMP_RLE8: begin
  4817. raise EglBitmap.Create('RLE compression is not supported');
  4818. end;
  4819. BMP_COMP_BITFIELDS: begin
  4820. if (aInfo.biBitCount = 16) or (aInfo.biBitCount = 32) then begin
  4821. for i := 0 to 2 do begin
  4822. aStream.Read(tmp{%H-}, SizeOf(tmp));
  4823. aMask.arr[i] := tmp;
  4824. end;
  4825. end else
  4826. raise EglBitmap.Create('Bitfields are only supported for 16bit and 32bit formats');
  4827. end;
  4828. end;
  4829. //get suitable format
  4830. case aInfo.biBitCount of
  4831. 8: result := tfLuminance8ub1;
  4832. 16: result := tfX1RGB5us1;
  4833. 24: result := tfBGR8ub3;
  4834. 32: result := tfXRGB8ui1;
  4835. end;
  4836. end;
  4837. function ReadColorTable(var aFormat: TglBitmapFormat; const aInfo: TBMPInfo): TbmpColorTableFormat;
  4838. var
  4839. i, c: Integer;
  4840. fd: TFormatDescriptor;
  4841. ColorTable: TbmpColorTable;
  4842. begin
  4843. result := nil;
  4844. if (aInfo.biBitCount >= 16) then
  4845. exit;
  4846. aFormat := tfLuminance8ub1;
  4847. c := aInfo.biClrUsed;
  4848. if (c = 0) then
  4849. c := 1 shl aInfo.biBitCount;
  4850. SetLength(ColorTable, c);
  4851. for i := 0 to c-1 do begin
  4852. aStream.Read(ColorTable[i], SizeOf(TbmpColorTableEnty));
  4853. if (ColorTable[i].r <> ColorTable[i].g) or (ColorTable[i].g <> ColorTable[i].b) then
  4854. aFormat := tfRGB8ub3;
  4855. end;
  4856. fd := TFormatDescriptor.Get(aFormat);
  4857. result := TbmpColorTableFormat.Create;
  4858. result.ColorTable := ColorTable;
  4859. result.SetCustomValues(aFormat, aInfo.biBitCount, fd.Precision, fd.Shift);
  4860. end;
  4861. //////////////////////////////////////////////////////////////////////////////////////////////////
  4862. function CheckBitfields(var aFormat: TglBitmapFormat; const aMask: TglBitmapRec4ul; const aInfo: TBMPInfo): TbmpBitfieldFormat;
  4863. var
  4864. fd: TFormatDescriptor;
  4865. begin
  4866. result := nil;
  4867. if (aMask.r <> 0) or (aMask.g <> 0) or (aMask.b <> 0) or (aMask.a <> 0) then begin
  4868. // find suitable format ...
  4869. fd := TFormatDescriptor.GetFromMask(aMask);
  4870. if (fd.Format <> tfEmpty) then begin
  4871. aFormat := fd.Format;
  4872. exit;
  4873. end;
  4874. // or create custom bitfield format
  4875. result := TbmpBitfieldFormat.Create;
  4876. result.SetCustomValues(aInfo.biBitCount, aMask);
  4877. end;
  4878. end;
  4879. var
  4880. //simple types
  4881. StartPos: Int64;
  4882. ImageSize, rbLineSize, wbLineSize, Padding, i: Integer;
  4883. PaddingBuff: Cardinal;
  4884. LineBuf, ImageData, TmpData: PByte;
  4885. SourceMD, DestMD: Pointer;
  4886. BmpFormat: TglBitmapFormat;
  4887. //records
  4888. Mask: TglBitmapRec4ul;
  4889. Header: TBMPHeader;
  4890. Info: TBMPInfo;
  4891. //classes
  4892. SpecialFormat: TFormatDescriptor;
  4893. FormatDesc: TFormatDescriptor;
  4894. //////////////////////////////////////////////////////////////////////////////////////////////////
  4895. procedure SpecialFormatReadLine(aData: PByte; aLineBuf: PByte);
  4896. var
  4897. i: Integer;
  4898. Pixel: TglBitmapPixelData;
  4899. begin
  4900. aStream.Read(aLineBuf^, rbLineSize);
  4901. SpecialFormat.PreparePixel(Pixel);
  4902. for i := 0 to Info.biWidth-1 do begin
  4903. SpecialFormat.Unmap(aLineBuf, Pixel, SourceMD);
  4904. glBitmapConvertPixel(Pixel, SpecialFormat, FormatDesc);
  4905. FormatDesc.Map(Pixel, aData, DestMD);
  4906. end;
  4907. end;
  4908. begin
  4909. result := false;
  4910. BmpFormat := tfEmpty;
  4911. SpecialFormat := nil;
  4912. LineBuf := nil;
  4913. SourceMD := nil;
  4914. DestMD := nil;
  4915. // Header
  4916. StartPos := aStream.Position;
  4917. aStream.Read(Header{%H-}, SizeOf(Header));
  4918. if Header.bfType = BMP_MAGIC then begin
  4919. try try
  4920. BmpFormat := ReadInfo(Info, Mask);
  4921. SpecialFormat := ReadColorTable(BmpFormat, Info);
  4922. if not Assigned(SpecialFormat) then
  4923. SpecialFormat := CheckBitfields(BmpFormat, Mask, Info);
  4924. aStream.Position := StartPos + Header.bfOffBits;
  4925. if (BmpFormat <> tfEmpty) then begin
  4926. FormatDesc := TFormatDescriptor.Get(BmpFormat);
  4927. rbLineSize := Round(Info.biWidth * Info.biBitCount / 8); //ReadBuffer LineSize
  4928. wbLineSize := Trunc(Info.biWidth * FormatDesc.BytesPerPixel);
  4929. Padding := (((Info.biWidth * Info.biBitCount + 31) and - 32) shr 3) - rbLineSize;
  4930. //get Memory
  4931. DestMD := FormatDesc.CreateMappingData;
  4932. ImageSize := FormatDesc.GetSize(Info.biWidth, abs(Info.biHeight));
  4933. GetMem(ImageData, ImageSize);
  4934. if Assigned(SpecialFormat) then begin
  4935. GetMem(LineBuf, rbLineSize); //tmp Memory for converting Bitfields
  4936. SourceMD := SpecialFormat.CreateMappingData;
  4937. end;
  4938. //read Data
  4939. try try
  4940. FillChar(ImageData^, ImageSize, $FF);
  4941. TmpData := ImageData;
  4942. if (Info.biHeight > 0) then
  4943. Inc(TmpData, wbLineSize * (Info.biHeight-1));
  4944. for i := 0 to Abs(Info.biHeight)-1 do begin
  4945. if Assigned(SpecialFormat) then
  4946. SpecialFormatReadLine(TmpData, LineBuf) //if is special format read and convert data
  4947. else
  4948. aStream.Read(TmpData^, wbLineSize); //else only read data
  4949. if (Info.biHeight > 0) then
  4950. dec(TmpData, wbLineSize)
  4951. else
  4952. inc(TmpData, wbLineSize);
  4953. aStream.Read(PaddingBuff{%H-}, Padding);
  4954. end;
  4955. SetData(ImageData, BmpFormat, Info.biWidth, abs(Info.biHeight));
  4956. result := true;
  4957. finally
  4958. if Assigned(LineBuf) then
  4959. FreeMem(LineBuf);
  4960. if Assigned(SourceMD) then
  4961. SpecialFormat.FreeMappingData(SourceMD);
  4962. FormatDesc.FreeMappingData(DestMD);
  4963. end;
  4964. except
  4965. if Assigned(ImageData) then
  4966. FreeMem(ImageData);
  4967. raise;
  4968. end;
  4969. end else
  4970. raise EglBitmap.Create('LoadBMP - No suitable format found');
  4971. except
  4972. aStream.Position := StartPos;
  4973. raise;
  4974. end;
  4975. finally
  4976. FreeAndNil(SpecialFormat);
  4977. end;
  4978. end
  4979. else aStream.Position := StartPos;
  4980. end;
  4981. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4982. procedure TglBitmapData.SaveBMP(const aStream: TStream);
  4983. var
  4984. Header: TBMPHeader;
  4985. Info: TBMPInfo;
  4986. Converter: TFormatDescriptor;
  4987. FormatDesc: TFormatDescriptor;
  4988. SourceFD, DestFD: Pointer;
  4989. pData, srcData, dstData, ConvertBuffer: pByte;
  4990. Pixel: TglBitmapPixelData;
  4991. ImageSize, wbLineSize, rbLineSize, Padding, LineIdx, PixelIdx: Integer;
  4992. RedMask, GreenMask, BlueMask, AlphaMask: Cardinal;
  4993. PaddingBuff: Cardinal;
  4994. function GetLineWidth : Integer;
  4995. begin
  4996. result := ((Info.biWidth * Info.biBitCount + 31) and - 32) shr 3;
  4997. end;
  4998. begin
  4999. if not (ftBMP in FormatGetSupportedFiles(Format)) then
  5000. raise EglBitmapUnsupportedFormat.Create(Format);
  5001. Converter := nil;
  5002. FormatDesc := TFormatDescriptor.Get(Format);
  5003. ImageSize := FormatDesc.GetSize(Dimension);
  5004. FillChar(Header{%H-}, SizeOf(Header), 0);
  5005. Header.bfType := BMP_MAGIC;
  5006. Header.bfSize := SizeOf(Header) + SizeOf(Info) + ImageSize;
  5007. Header.bfReserved1 := 0;
  5008. Header.bfReserved2 := 0;
  5009. Header.bfOffBits := SizeOf(Header) + SizeOf(Info);
  5010. FillChar(Info{%H-}, SizeOf(Info), 0);
  5011. Info.biSize := SizeOf(Info);
  5012. Info.biWidth := Width;
  5013. Info.biHeight := Height;
  5014. Info.biPlanes := 1;
  5015. Info.biCompression := BMP_COMP_RGB;
  5016. Info.biSizeImage := ImageSize;
  5017. try
  5018. case Format of
  5019. tfAlpha4ub1, tfAlpha8ub1, tfLuminance4ub1, tfLuminance8ub1, tfR3G3B2ub1:
  5020. begin
  5021. Info.biBitCount := 8;
  5022. Header.bfSize := Header.bfSize + 256 * SizeOf(Cardinal);
  5023. Header.bfOffBits := Header.bfOffBits + 256 * SizeOf(Cardinal); //256 ColorTable entries
  5024. Converter := TbmpColorTableFormat.Create;
  5025. with (Converter as TbmpColorTableFormat) do begin
  5026. SetCustomValues(fFormat, 8, FormatDesc.Precision, FormatDesc.Shift);
  5027. CreateColorTable;
  5028. end;
  5029. end;
  5030. tfLuminance4Alpha4ub2, tfLuminance6Alpha2ub2, tfLuminance8Alpha8ub2,
  5031. tfRGBX4us1, tfXRGB4us1, tfRGB5X1us1, tfX1RGB5us1, tfR5G6B5us1, tfRGB5A1us1, tfA1RGB5us1, tfRGBA4us1, tfARGB4us1,
  5032. tfBGRX4us1, tfXBGR4us1, tfBGR5X1us1, tfX1BGR5us1, tfB5G6R5us1, tfBGR5A1us1, tfA1BGR5us1, tfBGRA4us1, tfABGR4us1:
  5033. begin
  5034. Info.biBitCount := 16;
  5035. Info.biCompression := BMP_COMP_BITFIELDS;
  5036. end;
  5037. tfBGR8ub3, tfRGB8ub3:
  5038. begin
  5039. Info.biBitCount := 24;
  5040. if (Format = tfRGB8ub3) then
  5041. Converter := TfdBGR8ub3.Create; //use BGR8 Format Descriptor to Swap RGB Values
  5042. end;
  5043. tfRGBX8ui1, tfXRGB8ui1, tfRGB10X2ui1, tfX2RGB10ui1, tfRGBA8ui1, tfARGB8ui1, tfRGBA8ub4, tfRGB10A2ui1, tfA2RGB10ui1,
  5044. tfBGRX8ui1, tfXBGR8ui1, tfBGR10X2ui1, tfX2BGR10ui1, tfBGRA8ui1, tfABGR8ui1, tfBGRA8ub4, tfBGR10A2ui1, tfA2BGR10ui1:
  5045. begin
  5046. Info.biBitCount := 32;
  5047. Info.biCompression := BMP_COMP_BITFIELDS;
  5048. end;
  5049. else
  5050. raise EglBitmapUnsupportedFormat.Create(Format);
  5051. end;
  5052. Info.biXPelsPerMeter := 2835;
  5053. Info.biYPelsPerMeter := 2835;
  5054. // prepare bitmasks
  5055. if Info.biCompression = BMP_COMP_BITFIELDS then begin
  5056. Header.bfSize := Header.bfSize + 4 * SizeOf(Cardinal);
  5057. Header.bfOffBits := Header.bfOffBits + 4 * SizeOf(Cardinal);
  5058. RedMask := FormatDesc.Mask.r;
  5059. GreenMask := FormatDesc.Mask.g;
  5060. BlueMask := FormatDesc.Mask.b;
  5061. AlphaMask := FormatDesc.Mask.a;
  5062. end;
  5063. // headers
  5064. aStream.Write(Header, SizeOf(Header));
  5065. aStream.Write(Info, SizeOf(Info));
  5066. // colortable
  5067. if Assigned(Converter) and (Converter is TbmpColorTableFormat) then
  5068. with (Converter as TbmpColorTableFormat) do
  5069. aStream.Write(ColorTable[0].b,
  5070. SizeOf(TbmpColorTableEnty) * Length(ColorTable));
  5071. // bitmasks
  5072. if Info.biCompression = BMP_COMP_BITFIELDS then begin
  5073. aStream.Write(RedMask, SizeOf(Cardinal));
  5074. aStream.Write(GreenMask, SizeOf(Cardinal));
  5075. aStream.Write(BlueMask, SizeOf(Cardinal));
  5076. aStream.Write(AlphaMask, SizeOf(Cardinal));
  5077. end;
  5078. // image data
  5079. rbLineSize := Round(Info.biWidth * FormatDesc.BytesPerPixel);
  5080. wbLineSize := Round(Info.biWidth * Info.biBitCount / 8);
  5081. Padding := GetLineWidth - wbLineSize;
  5082. PaddingBuff := 0;
  5083. pData := Data;
  5084. inc(pData, (Height-1) * rbLineSize);
  5085. // prepare row buffer. But only for RGB because RGBA supports color masks
  5086. // so it's possible to change color within the image.
  5087. if Assigned(Converter) then begin
  5088. FormatDesc.PreparePixel(Pixel);
  5089. GetMem(ConvertBuffer, wbLineSize);
  5090. SourceFD := FormatDesc.CreateMappingData;
  5091. DestFD := Converter.CreateMappingData;
  5092. end else
  5093. ConvertBuffer := nil;
  5094. try
  5095. for LineIdx := 0 to Height - 1 do begin
  5096. // preparing row
  5097. if Assigned(Converter) then begin
  5098. srcData := pData;
  5099. dstData := ConvertBuffer;
  5100. for PixelIdx := 0 to Info.biWidth-1 do begin
  5101. FormatDesc.Unmap(srcData, Pixel, SourceFD);
  5102. glBitmapConvertPixel(Pixel, FormatDesc, Converter);
  5103. Converter.Map(Pixel, dstData, DestFD);
  5104. end;
  5105. aStream.Write(ConvertBuffer^, wbLineSize);
  5106. end else begin
  5107. aStream.Write(pData^, rbLineSize);
  5108. end;
  5109. dec(pData, rbLineSize);
  5110. if (Padding > 0) then
  5111. aStream.Write(PaddingBuff, Padding);
  5112. end;
  5113. finally
  5114. // destroy row buffer
  5115. if Assigned(ConvertBuffer) then begin
  5116. FormatDesc.FreeMappingData(SourceFD);
  5117. Converter.FreeMappingData(DestFD);
  5118. FreeMem(ConvertBuffer);
  5119. end;
  5120. end;
  5121. finally
  5122. if Assigned(Converter) then
  5123. Converter.Free;
  5124. end;
  5125. end;
  5126. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5127. //TGA/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5128. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5129. type
  5130. TTGAHeader = packed record
  5131. ImageID: Byte;
  5132. ColorMapType: Byte;
  5133. ImageType: Byte;
  5134. //ColorMapSpec: Array[0..4] of Byte;
  5135. ColorMapStart: Word;
  5136. ColorMapLength: Word;
  5137. ColorMapEntrySize: Byte;
  5138. OrigX: Word;
  5139. OrigY: Word;
  5140. Width: Word;
  5141. Height: Word;
  5142. Bpp: Byte;
  5143. ImageDesc: Byte;
  5144. end;
  5145. const
  5146. TGA_UNCOMPRESSED_RGB = 2;
  5147. TGA_UNCOMPRESSED_GRAY = 3;
  5148. TGA_COMPRESSED_RGB = 10;
  5149. TGA_COMPRESSED_GRAY = 11;
  5150. TGA_NONE_COLOR_TABLE = 0;
  5151. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5152. function TglBitmapData.LoadTGA(const aStream: TStream): Boolean;
  5153. var
  5154. Header: TTGAHeader;
  5155. ImageData: System.PByte;
  5156. StartPosition: Int64;
  5157. PixelSize, LineSize: Integer;
  5158. tgaFormat: TglBitmapFormat;
  5159. FormatDesc: TFormatDescriptor;
  5160. Counter: packed record
  5161. X, Y: packed record
  5162. low, high, dir: Integer;
  5163. end;
  5164. end;
  5165. const
  5166. CACHE_SIZE = $4000;
  5167. ////////////////////////////////////////////////////////////////////////////////////////
  5168. procedure ReadUncompressed;
  5169. var
  5170. i, j: Integer;
  5171. buf, tmp1, tmp2: System.PByte;
  5172. begin
  5173. buf := nil;
  5174. if (Counter.X.dir < 0) then
  5175. GetMem(buf, LineSize);
  5176. try
  5177. while (Counter.Y.low <> Counter.Y.high + counter.Y.dir) do begin
  5178. tmp1 := ImageData;
  5179. inc(tmp1, (Counter.Y.low * LineSize)); //pointer to LineStart
  5180. if (Counter.X.dir < 0) then begin //flip X
  5181. aStream.Read(buf^, LineSize);
  5182. tmp2 := buf;
  5183. inc(tmp2, LineSize - PixelSize); //pointer to last pixel in line
  5184. for i := 0 to Header.Width-1 do begin //for all pixels in line
  5185. for j := 0 to PixelSize-1 do begin //for all bytes in pixel
  5186. tmp1^ := tmp2^;
  5187. inc(tmp1);
  5188. inc(tmp2);
  5189. end;
  5190. dec(tmp2, 2*PixelSize); //move 2 backwards, because j-loop moved 1 forward
  5191. end;
  5192. end else
  5193. aStream.Read(tmp1^, LineSize);
  5194. inc(Counter.Y.low, Counter.Y.dir); //move to next line index
  5195. end;
  5196. finally
  5197. if Assigned(buf) then
  5198. FreeMem(buf);
  5199. end;
  5200. end;
  5201. ////////////////////////////////////////////////////////////////////////////////////////
  5202. procedure ReadCompressed;
  5203. /////////////////////////////////////////////////////////////////
  5204. var
  5205. TmpData: System.PByte;
  5206. LinePixelsRead: Integer;
  5207. procedure CheckLine;
  5208. begin
  5209. if (LinePixelsRead >= Header.Width) then begin
  5210. LinePixelsRead := 0;
  5211. inc(Counter.Y.low, Counter.Y.dir); //next line index
  5212. TmpData := ImageData;
  5213. inc(TmpData, Counter.Y.low * LineSize); //set line
  5214. if (Counter.X.dir < 0) then //if x flipped then
  5215. inc(TmpData, LineSize - PixelSize); //set last pixel
  5216. end;
  5217. end;
  5218. /////////////////////////////////////////////////////////////////
  5219. var
  5220. Cache: PByte;
  5221. CacheSize, CachePos: Integer;
  5222. procedure CachedRead(out Buffer; Count: Integer);
  5223. var
  5224. BytesRead: Integer;
  5225. begin
  5226. if (CachePos + Count > CacheSize) then begin
  5227. //if buffer overflow save non read bytes
  5228. BytesRead := 0;
  5229. if (CacheSize - CachePos > 0) then begin
  5230. BytesRead := CacheSize - CachePos;
  5231. Move(PByteArray(Cache)^[CachePos], Buffer{%H-}, BytesRead);
  5232. inc(CachePos, BytesRead);
  5233. end;
  5234. //load cache from file
  5235. CacheSize := Min(CACHE_SIZE, aStream.Size - aStream.Position);
  5236. aStream.Read(Cache^, CacheSize);
  5237. CachePos := 0;
  5238. //read rest of requested bytes
  5239. if (Count - BytesRead > 0) then begin
  5240. Move(PByteArray(Cache)^[CachePos], TByteArray(Buffer)[BytesRead], Count - BytesRead);
  5241. inc(CachePos, Count - BytesRead);
  5242. end;
  5243. end else begin
  5244. //if no buffer overflow just read the data
  5245. Move(PByteArray(Cache)^[CachePos], Buffer, Count);
  5246. inc(CachePos, Count);
  5247. end;
  5248. end;
  5249. procedure PixelToBuffer(const aData: PByte; var aBuffer: PByte);
  5250. begin
  5251. case PixelSize of
  5252. 1: begin
  5253. aBuffer^ := aData^;
  5254. inc(aBuffer, Counter.X.dir);
  5255. end;
  5256. 2: begin
  5257. PWord(aBuffer)^ := PWord(aData)^;
  5258. inc(aBuffer, 2 * Counter.X.dir);
  5259. end;
  5260. 3: begin
  5261. PByteArray(aBuffer)^[0] := PByteArray(aData)^[0];
  5262. PByteArray(aBuffer)^[1] := PByteArray(aData)^[1];
  5263. PByteArray(aBuffer)^[2] := PByteArray(aData)^[2];
  5264. inc(aBuffer, 3 * Counter.X.dir);
  5265. end;
  5266. 4: begin
  5267. PCardinal(aBuffer)^ := PCardinal(aData)^;
  5268. inc(aBuffer, 4 * Counter.X.dir);
  5269. end;
  5270. end;
  5271. end;
  5272. var
  5273. TotalPixelsToRead, TotalPixelsRead: Integer;
  5274. Temp: Byte;
  5275. buf: array [0..3] of Byte; //1 pixel is max 32bit long
  5276. PixelRepeat: Boolean;
  5277. PixelsToRead, PixelCount: Integer;
  5278. begin
  5279. CacheSize := 0;
  5280. CachePos := 0;
  5281. TotalPixelsToRead := Header.Width * Header.Height;
  5282. TotalPixelsRead := 0;
  5283. LinePixelsRead := 0;
  5284. GetMem(Cache, CACHE_SIZE);
  5285. try
  5286. TmpData := ImageData;
  5287. inc(TmpData, Counter.Y.low * LineSize); //set line
  5288. if (Counter.X.dir < 0) then //if x flipped then
  5289. inc(TmpData, LineSize - PixelSize); //set last pixel
  5290. repeat
  5291. //read CommandByte
  5292. CachedRead(Temp, 1);
  5293. PixelRepeat := (Temp and $80) > 0;
  5294. PixelsToRead := (Temp and $7F) + 1;
  5295. inc(TotalPixelsRead, PixelsToRead);
  5296. if PixelRepeat then
  5297. CachedRead(buf[0], PixelSize);
  5298. while (PixelsToRead > 0) do begin
  5299. CheckLine;
  5300. PixelCount := Min(Header.Width - LinePixelsRead, PixelsToRead); //max read to EOL or EOF
  5301. while (PixelCount > 0) do begin
  5302. if not PixelRepeat then
  5303. CachedRead(buf[0], PixelSize);
  5304. PixelToBuffer(@buf[0], TmpData);
  5305. inc(LinePixelsRead);
  5306. dec(PixelsToRead);
  5307. dec(PixelCount);
  5308. end;
  5309. end;
  5310. until (TotalPixelsRead >= TotalPixelsToRead);
  5311. finally
  5312. FreeMem(Cache);
  5313. end;
  5314. end;
  5315. function IsGrayFormat: Boolean;
  5316. begin
  5317. result := Header.ImageType in [TGA_UNCOMPRESSED_GRAY, TGA_COMPRESSED_GRAY];
  5318. end;
  5319. begin
  5320. result := false;
  5321. // reading header to test file and set cursor back to begin
  5322. StartPosition := aStream.Position;
  5323. aStream.Read(Header{%H-}, SizeOf(Header));
  5324. // no colormapped files
  5325. if (Header.ColorMapType = TGA_NONE_COLOR_TABLE) and (Header.ImageType in [
  5326. TGA_UNCOMPRESSED_RGB, TGA_UNCOMPRESSED_GRAY, TGA_COMPRESSED_RGB, TGA_COMPRESSED_GRAY]) then
  5327. begin
  5328. try
  5329. if Header.ImageID <> 0 then // skip image ID
  5330. aStream.Position := aStream.Position + Header.ImageID;
  5331. tgaFormat := tfEmpty;
  5332. case Header.Bpp of
  5333. 8: if IsGrayFormat then case (Header.ImageDesc and $F) of
  5334. 0: tgaFormat := tfLuminance8ub1;
  5335. 8: tgaFormat := tfAlpha8ub1;
  5336. end;
  5337. 16: if IsGrayFormat then case (Header.ImageDesc and $F) of
  5338. 0: tgaFormat := tfLuminance16us1;
  5339. 8: tgaFormat := tfLuminance8Alpha8ub2;
  5340. end else case (Header.ImageDesc and $F) of
  5341. 0: tgaFormat := tfX1RGB5us1;
  5342. 1: tgaFormat := tfA1RGB5us1;
  5343. 4: tgaFormat := tfARGB4us1;
  5344. end;
  5345. 24: if not IsGrayFormat then case (Header.ImageDesc and $F) of
  5346. 0: tgaFormat := tfBGR8ub3;
  5347. end;
  5348. 32: if IsGrayFormat then case (Header.ImageDesc and $F) of
  5349. 0: tgaFormat := tfDepth32ui1;
  5350. end else case (Header.ImageDesc and $F) of
  5351. 0: tgaFormat := tfX2RGB10ui1;
  5352. 2: tgaFormat := tfA2RGB10ui1;
  5353. 8: tgaFormat := tfARGB8ui1;
  5354. end;
  5355. end;
  5356. if (tgaFormat = tfEmpty) then
  5357. raise EglBitmap.Create('LoadTga - unsupported format');
  5358. FormatDesc := TFormatDescriptor.Get(tgaFormat);
  5359. PixelSize := FormatDesc.GetSize(1, 1);
  5360. LineSize := FormatDesc.GetSize(Header.Width, 1);
  5361. GetMem(ImageData, LineSize * Header.Height);
  5362. try
  5363. //column direction
  5364. if ((Header.ImageDesc and (1 shl 4)) > 0) then begin
  5365. Counter.X.low := Header.Height-1;;
  5366. Counter.X.high := 0;
  5367. Counter.X.dir := -1;
  5368. end else begin
  5369. Counter.X.low := 0;
  5370. Counter.X.high := Header.Height-1;
  5371. Counter.X.dir := 1;
  5372. end;
  5373. // Row direction
  5374. if ((Header.ImageDesc and (1 shl 5)) > 0) then begin
  5375. Counter.Y.low := 0;
  5376. Counter.Y.high := Header.Height-1;
  5377. Counter.Y.dir := 1;
  5378. end else begin
  5379. Counter.Y.low := Header.Height-1;;
  5380. Counter.Y.high := 0;
  5381. Counter.Y.dir := -1;
  5382. end;
  5383. // Read Image
  5384. case Header.ImageType of
  5385. TGA_UNCOMPRESSED_RGB, TGA_UNCOMPRESSED_GRAY:
  5386. ReadUncompressed;
  5387. TGA_COMPRESSED_RGB, TGA_COMPRESSED_GRAY:
  5388. ReadCompressed;
  5389. end;
  5390. SetData(ImageData, tgaFormat, Header.Width, Header.Height);
  5391. result := true;
  5392. except
  5393. if Assigned(ImageData) then
  5394. FreeMem(ImageData);
  5395. raise;
  5396. end;
  5397. finally
  5398. aStream.Position := StartPosition;
  5399. end;
  5400. end
  5401. else aStream.Position := StartPosition;
  5402. end;
  5403. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5404. procedure TglBitmapData.SaveTGA(const aStream: TStream);
  5405. var
  5406. Header: TTGAHeader;
  5407. Size: Integer;
  5408. FormatDesc: TFormatDescriptor;
  5409. begin
  5410. if not (ftTGA in FormatGetSupportedFiles(Format)) then
  5411. raise EglBitmapUnsupportedFormat.Create(Format);
  5412. //prepare header
  5413. FormatDesc := TFormatDescriptor.Get(Format);
  5414. FillChar(Header{%H-}, SizeOf(Header), 0);
  5415. Header.ImageDesc := CountSetBits(FormatDesc.Range.a) and $F;
  5416. Header.Bpp := FormatDesc.BitsPerPixel;
  5417. Header.Width := Width;
  5418. Header.Height := Height;
  5419. Header.ImageDesc := Header.ImageDesc or $20; //flip y
  5420. if FormatDesc.IsGrayscale or (not FormatDesc.IsGrayscale and not FormatDesc.HasRed and FormatDesc.HasAlpha) then
  5421. Header.ImageType := TGA_UNCOMPRESSED_GRAY
  5422. else
  5423. Header.ImageType := TGA_UNCOMPRESSED_RGB;
  5424. aStream.Write(Header, SizeOf(Header));
  5425. // write Data
  5426. Size := FormatDesc.GetSize(Dimension);
  5427. aStream.Write(Data^, Size);
  5428. end;
  5429. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5430. //DDS/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5431. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5432. const
  5433. DDS_MAGIC: Cardinal = $20534444;
  5434. // DDS_header.dwFlags
  5435. DDSD_CAPS = $00000001;
  5436. DDSD_HEIGHT = $00000002;
  5437. DDSD_WIDTH = $00000004;
  5438. DDSD_PIXELFORMAT = $00001000;
  5439. // DDS_header.sPixelFormat.dwFlags
  5440. DDPF_ALPHAPIXELS = $00000001;
  5441. DDPF_ALPHA = $00000002;
  5442. DDPF_FOURCC = $00000004;
  5443. DDPF_RGB = $00000040;
  5444. DDPF_LUMINANCE = $00020000;
  5445. // DDS_header.sCaps.dwCaps1
  5446. DDSCAPS_TEXTURE = $00001000;
  5447. // DDS_header.sCaps.dwCaps2
  5448. DDSCAPS2_CUBEMAP = $00000200;
  5449. D3DFMT_DXT1 = $31545844;
  5450. D3DFMT_DXT3 = $33545844;
  5451. D3DFMT_DXT5 = $35545844;
  5452. type
  5453. TDDSPixelFormat = packed record
  5454. dwSize: Cardinal;
  5455. dwFlags: Cardinal;
  5456. dwFourCC: Cardinal;
  5457. dwRGBBitCount: Cardinal;
  5458. dwRBitMask: Cardinal;
  5459. dwGBitMask: Cardinal;
  5460. dwBBitMask: Cardinal;
  5461. dwABitMask: Cardinal;
  5462. end;
  5463. TDDSCaps = packed record
  5464. dwCaps1: Cardinal;
  5465. dwCaps2: Cardinal;
  5466. dwDDSX: Cardinal;
  5467. dwReserved: Cardinal;
  5468. end;
  5469. TDDSHeader = packed record
  5470. dwSize: Cardinal;
  5471. dwFlags: Cardinal;
  5472. dwHeight: Cardinal;
  5473. dwWidth: Cardinal;
  5474. dwPitchOrLinearSize: Cardinal;
  5475. dwDepth: Cardinal;
  5476. dwMipMapCount: Cardinal;
  5477. dwReserved: array[0..10] of Cardinal;
  5478. PixelFormat: TDDSPixelFormat;
  5479. Caps: TDDSCaps;
  5480. dwReserved2: Cardinal;
  5481. end;
  5482. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5483. function TglBitmapData.LoadDDS(const aStream: TStream): Boolean;
  5484. var
  5485. Header: TDDSHeader;
  5486. Converter: TbmpBitfieldFormat;
  5487. function GetDDSFormat: TglBitmapFormat;
  5488. var
  5489. fd: TFormatDescriptor;
  5490. i: Integer;
  5491. Mask: TglBitmapRec4ul;
  5492. Range: TglBitmapRec4ui;
  5493. match: Boolean;
  5494. begin
  5495. result := tfEmpty;
  5496. with Header.PixelFormat do begin
  5497. // Compresses
  5498. if ((dwFlags and DDPF_FOURCC) > 0) then begin
  5499. case Header.PixelFormat.dwFourCC of
  5500. D3DFMT_DXT1: result := tfS3tcDtx1RGBA;
  5501. D3DFMT_DXT3: result := tfS3tcDtx3RGBA;
  5502. D3DFMT_DXT5: result := tfS3tcDtx5RGBA;
  5503. end;
  5504. end else if ((dwFlags and (DDPF_RGB or DDPF_ALPHAPIXELS or DDPF_LUMINANCE or DDPF_ALPHA)) > 0) then begin
  5505. // prepare masks
  5506. if ((dwFlags and DDPF_LUMINANCE) = 0) then begin
  5507. Mask.r := dwRBitMask;
  5508. Mask.g := dwGBitMask;
  5509. Mask.b := dwBBitMask;
  5510. end else begin
  5511. Mask.r := dwRBitMask;
  5512. Mask.g := dwRBitMask;
  5513. Mask.b := dwRBitMask;
  5514. end;
  5515. if (dwFlags and DDPF_ALPHAPIXELS > 0) then
  5516. Mask.a := dwABitMask
  5517. else
  5518. Mask.a := 0;;
  5519. //find matching format
  5520. fd := TFormatDescriptor.GetFromMask(Mask, dwRGBBitCount);
  5521. result := fd.Format;
  5522. if (result <> tfEmpty) then
  5523. exit;
  5524. //find format with same Range
  5525. for i := 0 to 3 do
  5526. Range.arr[i] := (2 shl CountSetBits(Mask.arr[i])) - 1;
  5527. for result := High(TglBitmapFormat) downto Low(TglBitmapFormat) do begin
  5528. fd := TFormatDescriptor.Get(result);
  5529. match := true;
  5530. for i := 0 to 3 do
  5531. if (fd.Range.arr[i] <> Range.arr[i]) then begin
  5532. match := false;
  5533. break;
  5534. end;
  5535. if match then
  5536. break;
  5537. end;
  5538. //no format with same range found -> use default
  5539. if (result = tfEmpty) then begin
  5540. if (dwABitMask > 0) then
  5541. result := tfRGBA8ui1
  5542. else
  5543. result := tfRGB8ub3;
  5544. end;
  5545. Converter := TbmpBitfieldFormat.Create;
  5546. Converter.SetCustomValues(dwRGBBitCount, glBitmapRec4ul(dwRBitMask, dwGBitMask, dwBBitMask, dwABitMask));
  5547. end;
  5548. end;
  5549. end;
  5550. var
  5551. StreamPos: Int64;
  5552. x, y, LineSize, RowSize, Magic: Cardinal;
  5553. NewImage, TmpData, RowData, SrcData: System.PByte;
  5554. SourceMD, DestMD: Pointer;
  5555. Pixel: TglBitmapPixelData;
  5556. ddsFormat: TglBitmapFormat;
  5557. FormatDesc: TFormatDescriptor;
  5558. begin
  5559. result := false;
  5560. Converter := nil;
  5561. StreamPos := aStream.Position;
  5562. // Magic
  5563. aStream.Read(Magic{%H-}, sizeof(Magic));
  5564. if (Magic <> DDS_MAGIC) then begin
  5565. aStream.Position := StreamPos;
  5566. exit;
  5567. end;
  5568. //Header
  5569. aStream.Read(Header{%H-}, sizeof(Header));
  5570. if (Header.dwSize <> SizeOf(Header)) or
  5571. ((Header.dwFlags and (DDSD_PIXELFORMAT or DDSD_CAPS or DDSD_WIDTH or DDSD_HEIGHT)) <>
  5572. (DDSD_PIXELFORMAT or DDSD_CAPS or DDSD_WIDTH or DDSD_HEIGHT)) then
  5573. begin
  5574. aStream.Position := StreamPos;
  5575. exit;
  5576. end;
  5577. if ((Header.Caps.dwCaps1 and DDSCAPS2_CUBEMAP) > 0) then
  5578. raise EglBitmap.Create('LoadDDS - CubeMaps are not supported');
  5579. ddsFormat := GetDDSFormat;
  5580. try
  5581. if (ddsFormat = tfEmpty) then
  5582. raise EglBitmap.Create('LoadDDS - unsupported Pixelformat found.');
  5583. FormatDesc := TFormatDescriptor.Get(ddsFormat);
  5584. LineSize := Trunc(Header.dwWidth * FormatDesc.BytesPerPixel);
  5585. GetMem(NewImage, Header.dwHeight * LineSize);
  5586. try
  5587. TmpData := NewImage;
  5588. //Converter needed
  5589. if Assigned(Converter) then begin
  5590. RowSize := Round(Header.dwWidth * Header.PixelFormat.dwRGBBitCount / 8);
  5591. GetMem(RowData, RowSize);
  5592. SourceMD := Converter.CreateMappingData;
  5593. DestMD := FormatDesc.CreateMappingData;
  5594. try
  5595. for y := 0 to Header.dwHeight-1 do begin
  5596. TmpData := NewImage;
  5597. inc(TmpData, y * LineSize);
  5598. SrcData := RowData;
  5599. aStream.Read(SrcData^, RowSize);
  5600. for x := 0 to Header.dwWidth-1 do begin
  5601. Converter.Unmap(SrcData, Pixel, SourceMD);
  5602. glBitmapConvertPixel(Pixel, Converter, FormatDesc);
  5603. FormatDesc.Map(Pixel, TmpData, DestMD);
  5604. end;
  5605. end;
  5606. finally
  5607. Converter.FreeMappingData(SourceMD);
  5608. FormatDesc.FreeMappingData(DestMD);
  5609. FreeMem(RowData);
  5610. end;
  5611. end else
  5612. // Compressed
  5613. if ((Header.PixelFormat.dwFlags and DDPF_FOURCC) > 0) then begin
  5614. RowSize := Header.dwPitchOrLinearSize div Header.dwWidth;
  5615. for Y := 0 to Header.dwHeight-1 do begin
  5616. aStream.Read(TmpData^, RowSize);
  5617. Inc(TmpData, LineSize);
  5618. end;
  5619. end else
  5620. // Uncompressed
  5621. if (Header.PixelFormat.dwFlags and (DDPF_RGB or DDPF_ALPHAPIXELS or DDPF_LUMINANCE)) > 0 then begin
  5622. RowSize := (Header.PixelFormat.dwRGBBitCount * Header.dwWidth) shr 3;
  5623. for Y := 0 to Header.dwHeight-1 do begin
  5624. aStream.Read(TmpData^, RowSize);
  5625. Inc(TmpData, LineSize);
  5626. end;
  5627. end else
  5628. raise EglBitmap.Create('LoadDDS - unsupported Pixelformat found.');
  5629. SetData(NewImage, ddsFormat, Header.dwWidth, Header.dwHeight);
  5630. result := true;
  5631. except
  5632. if Assigned(NewImage) then
  5633. FreeMem(NewImage);
  5634. raise;
  5635. end;
  5636. finally
  5637. FreeAndNil(Converter);
  5638. end;
  5639. end;
  5640. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5641. procedure TglBitmapData.SaveDDS(const aStream: TStream);
  5642. var
  5643. Header: TDDSHeader;
  5644. FormatDesc: TFormatDescriptor;
  5645. begin
  5646. if not (ftDDS in FormatGetSupportedFiles(Format)) then
  5647. raise EglBitmapUnsupportedFormat.Create(Format);
  5648. FormatDesc := TFormatDescriptor.Get(Format);
  5649. // Generell
  5650. FillChar(Header{%H-}, SizeOf(Header), 0);
  5651. Header.dwSize := SizeOf(Header);
  5652. Header.dwFlags := DDSD_WIDTH or DDSD_HEIGHT or DDSD_CAPS or DDSD_PIXELFORMAT;
  5653. Header.dwWidth := Max(1, Width);
  5654. Header.dwHeight := Max(1, Height);
  5655. // Caps
  5656. Header.Caps.dwCaps1 := DDSCAPS_TEXTURE;
  5657. // Pixelformat
  5658. Header.PixelFormat.dwSize := sizeof(Header);
  5659. if (FormatDesc.IsCompressed) then begin
  5660. Header.PixelFormat.dwFlags := Header.PixelFormat.dwFlags or DDPF_FOURCC;
  5661. case Format of
  5662. tfS3tcDtx1RGBA: Header.PixelFormat.dwFourCC := D3DFMT_DXT1;
  5663. tfS3tcDtx3RGBA: Header.PixelFormat.dwFourCC := D3DFMT_DXT3;
  5664. tfS3tcDtx5RGBA: Header.PixelFormat.dwFourCC := D3DFMT_DXT5;
  5665. end;
  5666. end else if not FormatDesc.HasColor and FormatDesc.HasAlpha then begin
  5667. Header.PixelFormat.dwFlags := Header.PixelFormat.dwFlags or DDPF_ALPHA;
  5668. Header.PixelFormat.dwRGBBitCount := FormatDesc.BitsPerPixel;
  5669. Header.PixelFormat.dwABitMask := FormatDesc.Mask.a;
  5670. end else if FormatDesc.IsGrayscale then begin
  5671. Header.PixelFormat.dwFlags := Header.PixelFormat.dwFlags or DDPF_LUMINANCE;
  5672. Header.PixelFormat.dwRGBBitCount := FormatDesc.BitsPerPixel;
  5673. Header.PixelFormat.dwRBitMask := FormatDesc.Mask.r;
  5674. Header.PixelFormat.dwABitMask := FormatDesc.Mask.a;
  5675. end else begin
  5676. Header.PixelFormat.dwFlags := Header.PixelFormat.dwFlags or DDPF_RGB;
  5677. Header.PixelFormat.dwRGBBitCount := FormatDesc.BitsPerPixel;
  5678. Header.PixelFormat.dwRBitMask := FormatDesc.Mask.r;
  5679. Header.PixelFormat.dwGBitMask := FormatDesc.Mask.g;
  5680. Header.PixelFormat.dwBBitMask := FormatDesc.Mask.b;
  5681. Header.PixelFormat.dwABitMask := FormatDesc.Mask.a;
  5682. end;
  5683. if (FormatDesc.HasAlpha) then
  5684. Header.PixelFormat.dwFlags := Header.PixelFormat.dwFlags or DDPF_ALPHAPIXELS;
  5685. aStream.Write(DDS_MAGIC, sizeof(DDS_MAGIC));
  5686. aStream.Write(Header, SizeOf(Header));
  5687. aStream.Write(Data^, FormatDesc.GetSize(Dimension));
  5688. end;
  5689. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5690. function TglBitmapData.FlipHorz: Boolean;
  5691. var
  5692. fd: TglBitmapFormatDescriptor;
  5693. Col, RowSize, PixelSize: Integer;
  5694. pTempDest, pDest, pSource: PByte;
  5695. begin
  5696. result := false;
  5697. fd := FormatDescriptor;
  5698. PixelSize := Ceil(fd.BytesPerPixel);
  5699. RowSize := fd.GetSize(Width, 1);
  5700. if Assigned(Data) and not fd.IsCompressed then begin
  5701. pSource := Data;
  5702. GetMem(pDest, RowSize);
  5703. try
  5704. pTempDest := pDest;
  5705. Inc(pTempDest, RowSize);
  5706. for Col := 0 to Width-1 do begin
  5707. dec(pTempDest, PixelSize); //dec before, because ptr is behind last byte of data
  5708. Move(pSource^, pTempDest^, PixelSize);
  5709. Inc(pSource, PixelSize);
  5710. end;
  5711. SetData(pDest, Format, Width);
  5712. result := true;
  5713. except
  5714. if Assigned(pDest) then
  5715. FreeMem(pDest);
  5716. raise;
  5717. end;
  5718. end;
  5719. end;
  5720. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5721. function TglBitmapData.FlipVert: Boolean;
  5722. var
  5723. fd: TglBitmapFormatDescriptor;
  5724. Row, RowSize, PixelSize: Integer;
  5725. TempDestData, DestData, SourceData: PByte;
  5726. begin
  5727. result := false;
  5728. fd := FormatDescriptor;
  5729. PixelSize := Ceil(fd.BytesPerPixel);
  5730. RowSize := fd.GetSize(Width, 1);
  5731. if Assigned(Data) then begin
  5732. SourceData := Data;
  5733. GetMem(DestData, Height * RowSize);
  5734. try
  5735. TempDestData := DestData;
  5736. Inc(TempDestData, Width * (Height -1) * PixelSize);
  5737. for Row := 0 to Height -1 do begin
  5738. Move(SourceData^, TempDestData^, RowSize);
  5739. Dec(TempDestData, RowSize);
  5740. Inc(SourceData, RowSize);
  5741. end;
  5742. SetData(DestData, Format, Width, Height);
  5743. result := true;
  5744. except
  5745. if Assigned(DestData) then
  5746. FreeMem(DestData);
  5747. raise;
  5748. end;
  5749. end;
  5750. end;
  5751. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5752. procedure TglBitmapData.LoadFromFile(const aFilename: String);
  5753. var
  5754. fs: TFileStream;
  5755. begin
  5756. if not FileExists(aFilename) then
  5757. raise EglBitmap.Create('file does not exist: ' + aFilename);
  5758. fs := TFileStream.Create(aFilename, fmOpenRead);
  5759. try
  5760. fs.Position := 0;
  5761. LoadFromStream(fs);
  5762. fFilename := aFilename;
  5763. finally
  5764. fs.Free;
  5765. end;
  5766. end;
  5767. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5768. procedure TglBitmapData.LoadFromStream(const aStream: TStream);
  5769. begin
  5770. {$IFDEF GLB_SUPPORT_PNG_READ}
  5771. if not LoadPNG(aStream) then
  5772. {$ENDIF}
  5773. {$IFDEF GLB_SUPPORT_JPEG_READ}
  5774. if not LoadJPEG(aStream) then
  5775. {$ENDIF}
  5776. if not LoadDDS(aStream) then
  5777. if not LoadTGA(aStream) then
  5778. if not LoadBMP(aStream) then
  5779. if not LoadRAW(aStream) then
  5780. raise EglBitmap.Create('LoadFromStream - Couldn''t load Stream. It''s possible to be an unknow Streamtype.');
  5781. end;
  5782. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5783. procedure TglBitmapData.LoadFromFunc(const aSize: TglBitmapSize; const aFormat: TglBitmapFormat;
  5784. const aFunc: TglBitmapFunction; const aArgs: Pointer);
  5785. var
  5786. tmpData: PByte;
  5787. size: Integer;
  5788. begin
  5789. size := TFormatDescriptor.Get(aFormat).GetSize(aSize);
  5790. GetMem(tmpData, size);
  5791. try
  5792. FillChar(tmpData^, size, #$FF);
  5793. SetData(tmpData, aFormat, aSize.X, aSize.Y);
  5794. except
  5795. if Assigned(tmpData) then
  5796. FreeMem(tmpData);
  5797. raise;
  5798. end;
  5799. Convert(Self, aFunc, false, aFormat, aArgs);
  5800. end;
  5801. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5802. procedure TglBitmapData.LoadFromResource(const aInstance: Cardinal; aResource: String; aResType: PChar);
  5803. var
  5804. rs: TResourceStream;
  5805. begin
  5806. PrepareResType(aResource, aResType);
  5807. rs := TResourceStream.Create(aInstance, aResource, aResType);
  5808. try
  5809. LoadFromStream(rs);
  5810. finally
  5811. rs.Free;
  5812. end;
  5813. end;
  5814. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5815. procedure TglBitmapData.LoadFromResourceID(const aInstance: Cardinal; const aResourceID: Integer; const aResType: PChar);
  5816. var
  5817. rs: TResourceStream;
  5818. begin
  5819. rs := TResourceStream.CreateFromID(aInstance, aResourceID, aResType);
  5820. try
  5821. LoadFromStream(rs);
  5822. finally
  5823. rs.Free;
  5824. end;
  5825. end;
  5826. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5827. procedure TglBitmapData.SaveToFile(const aFilename: String; const aFileType: TglBitmapFileType);
  5828. var
  5829. fs: TFileStream;
  5830. begin
  5831. fs := TFileStream.Create(aFileName, fmCreate);
  5832. try
  5833. fs.Position := 0;
  5834. SaveToStream(fs, aFileType);
  5835. finally
  5836. fs.Free;
  5837. end;
  5838. end;
  5839. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5840. procedure TglBitmapData.SaveToStream(const aStream: TStream; const aFileType: TglBitmapFileType);
  5841. begin
  5842. case aFileType of
  5843. {$IFDEF GLB_SUPPORT_PNG_WRITE}
  5844. ftPNG: SavePNG(aStream);
  5845. {$ENDIF}
  5846. {$IFDEF GLB_SUPPORT_JPEG_WRITE}
  5847. ftJPEG: SaveJPEG(aStream);
  5848. {$ENDIF}
  5849. ftDDS: SaveDDS(aStream);
  5850. ftTGA: SaveTGA(aStream);
  5851. ftBMP: SaveBMP(aStream);
  5852. ftRAW: SaveRAW(aStream);
  5853. end;
  5854. end;
  5855. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5856. function TglBitmapData.Convert(const aFunc: TglBitmapFunction; const aCreateTemp: Boolean; const aArgs: Pointer): Boolean;
  5857. begin
  5858. result := Convert(Self, aFunc, aCreateTemp, Format, aArgs);
  5859. end;
  5860. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5861. function TglBitmapData.Convert(const aSource: TglBitmapData; const aFunc: TglBitmapFunction; aCreateTemp: Boolean;
  5862. const aFormat: TglBitmapFormat; const aArgs: Pointer): Boolean;
  5863. var
  5864. DestData, TmpData, SourceData: pByte;
  5865. TempHeight, TempWidth: Integer;
  5866. SourceFD, DestFD: TFormatDescriptor;
  5867. SourceMD, DestMD: Pointer;
  5868. FuncRec: TglBitmapFunctionRec;
  5869. begin
  5870. Assert(Assigned(Data));
  5871. Assert(Assigned(aSource));
  5872. Assert(Assigned(aSource.Data));
  5873. result := false;
  5874. if Assigned(aSource.Data) and ((aSource.Height > 0) or (aSource.Width > 0)) then begin
  5875. SourceFD := TFormatDescriptor.Get(aSource.Format);
  5876. DestFD := TFormatDescriptor.Get(aFormat);
  5877. if (SourceFD.IsCompressed) then
  5878. raise EglBitmapUnsupportedFormat.Create('compressed formats are not supported: ', SourceFD.Format);
  5879. if (DestFD.IsCompressed) then
  5880. raise EglBitmapUnsupportedFormat.Create('compressed formats are not supported: ', DestFD.Format);
  5881. // inkompatible Formats so CreateTemp
  5882. if (SourceFD.BitsPerPixel <> DestFD.BitsPerPixel) then
  5883. aCreateTemp := true;
  5884. // Values
  5885. TempHeight := Max(1, aSource.Height);
  5886. TempWidth := Max(1, aSource.Width);
  5887. FuncRec.Sender := Self;
  5888. FuncRec.Args := aArgs;
  5889. TmpData := nil;
  5890. if aCreateTemp then begin
  5891. GetMem(TmpData, DestFD.GetSize(TempWidth, TempHeight));
  5892. DestData := TmpData;
  5893. end else
  5894. DestData := Data;
  5895. try
  5896. SourceFD.PreparePixel(FuncRec.Source);
  5897. DestFD.PreparePixel (FuncRec.Dest);
  5898. SourceMD := SourceFD.CreateMappingData;
  5899. DestMD := DestFD.CreateMappingData;
  5900. FuncRec.Size := aSource.Dimension;
  5901. FuncRec.Position.Fields := FuncRec.Size.Fields;
  5902. try
  5903. SourceData := aSource.Data;
  5904. FuncRec.Position.Y := 0;
  5905. while FuncRec.Position.Y < TempHeight do begin
  5906. FuncRec.Position.X := 0;
  5907. while FuncRec.Position.X < TempWidth do begin
  5908. SourceFD.Unmap(SourceData, FuncRec.Source, SourceMD);
  5909. aFunc(FuncRec);
  5910. DestFD.Map(FuncRec.Dest, DestData, DestMD);
  5911. inc(FuncRec.Position.X);
  5912. end;
  5913. inc(FuncRec.Position.Y);
  5914. end;
  5915. // Updating Image or InternalFormat
  5916. if aCreateTemp then
  5917. SetData(TmpData, aFormat, aSource.Width, aSource.Height)
  5918. else if (aFormat <> fFormat) then
  5919. Format := aFormat;
  5920. result := true;
  5921. finally
  5922. SourceFD.FreeMappingData(SourceMD);
  5923. DestFD.FreeMappingData(DestMD);
  5924. end;
  5925. except
  5926. if aCreateTemp and Assigned(TmpData) then
  5927. FreeMem(TmpData);
  5928. raise;
  5929. end;
  5930. end;
  5931. end;
  5932. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5933. function TglBitmapData.ConvertTo(const aFormat: TglBitmapFormat): Boolean;
  5934. var
  5935. SourceFD, DestFD: TFormatDescriptor;
  5936. SourcePD, DestPD: TglBitmapPixelData;
  5937. ShiftData: TShiftData;
  5938. function DataIsIdentical: Boolean;
  5939. begin
  5940. result := SourceFD.MaskMatch(DestFD.Mask);
  5941. end;
  5942. function CanCopyDirect: Boolean;
  5943. begin
  5944. result :=
  5945. ((SourcePD.Range.r = DestPD.Range.r) or (SourcePD.Range.r = 0) or (DestPD.Range.r = 0)) and
  5946. ((SourcePD.Range.g = DestPD.Range.g) or (SourcePD.Range.g = 0) or (DestPD.Range.g = 0)) and
  5947. ((SourcePD.Range.b = DestPD.Range.b) or (SourcePD.Range.b = 0) or (DestPD.Range.b = 0)) and
  5948. ((SourcePD.Range.a = DestPD.Range.a) or (SourcePD.Range.a = 0) or (DestPD.Range.a = 0));
  5949. end;
  5950. function CanShift: Boolean;
  5951. begin
  5952. result :=
  5953. ((SourcePD.Range.r >= DestPD.Range.r) or (SourcePD.Range.r = 0) or (DestPD.Range.r = 0)) and
  5954. ((SourcePD.Range.g >= DestPD.Range.g) or (SourcePD.Range.g = 0) or (DestPD.Range.g = 0)) and
  5955. ((SourcePD.Range.b >= DestPD.Range.b) or (SourcePD.Range.b = 0) or (DestPD.Range.b = 0)) and
  5956. ((SourcePD.Range.a >= DestPD.Range.a) or (SourcePD.Range.a = 0) or (DestPD.Range.a = 0));
  5957. end;
  5958. function GetShift(aSource, aDest: Cardinal) : ShortInt;
  5959. begin
  5960. result := 0;
  5961. while (aSource > aDest) and (aSource > 0) do begin
  5962. inc(result);
  5963. aSource := aSource shr 1;
  5964. end;
  5965. end;
  5966. begin
  5967. if (aFormat <> fFormat) and (aFormat <> tfEmpty) then begin
  5968. SourceFD := TFormatDescriptor.Get(Format);
  5969. DestFD := TFormatDescriptor.Get(aFormat);
  5970. if DataIsIdentical then begin
  5971. result := true;
  5972. Format := aFormat;
  5973. exit;
  5974. end;
  5975. SourceFD.PreparePixel(SourcePD);
  5976. DestFD.PreparePixel (DestPD);
  5977. if CanCopyDirect then
  5978. result := Convert(Self, glBitmapConvertCopyFunc, false, aFormat)
  5979. else if CanShift then begin
  5980. ShiftData.r := GetShift(SourcePD.Range.r, DestPD.Range.r);
  5981. ShiftData.g := GetShift(SourcePD.Range.g, DestPD.Range.g);
  5982. ShiftData.b := GetShift(SourcePD.Range.b, DestPD.Range.b);
  5983. ShiftData.a := GetShift(SourcePD.Range.a, DestPD.Range.a);
  5984. result := Convert(Self, glBitmapConvertShiftRGBAFunc, false, aFormat, @ShiftData);
  5985. end else
  5986. result := Convert(Self, glBitmapConvertCalculateRGBAFunc, false, aFormat);
  5987. end else
  5988. result := true;
  5989. end;
  5990. {$IFDEF GLB_SDL}
  5991. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5992. function TglBitmapData.AssignToSurface(out aSurface: PSDL_Surface): Boolean;
  5993. var
  5994. Row, RowSize: Integer;
  5995. SourceData, TmpData: PByte;
  5996. TempDepth: Integer;
  5997. FormatDesc: TFormatDescriptor;
  5998. function GetRowPointer(Row: Integer): pByte;
  5999. begin
  6000. result := aSurface.pixels;
  6001. Inc(result, Row * RowSize);
  6002. end;
  6003. begin
  6004. result := false;
  6005. FormatDesc := TFormatDescriptor.Get(Format);
  6006. if FormatDesc.IsCompressed then
  6007. raise EglBitmapUnsupportedFormat.Create(Format);
  6008. if Assigned(Data) then begin
  6009. case Trunc(FormatDesc.PixelSize) of
  6010. 1: TempDepth := 8;
  6011. 2: TempDepth := 16;
  6012. 3: TempDepth := 24;
  6013. 4: TempDepth := 32;
  6014. else
  6015. raise EglBitmapUnsupportedFormat.Create(Format);
  6016. end;
  6017. aSurface := SDL_CreateRGBSurface(SDL_SWSURFACE, Width, Height, TempDepth,
  6018. FormatDesc.RedMask, FormatDesc.GreenMask, FormatDesc.BlueMask, FormatDesc.AlphaMask);
  6019. SourceData := Data;
  6020. RowSize := FormatDesc.GetSize(FileWidth, 1);
  6021. for Row := 0 to FileHeight-1 do begin
  6022. TmpData := GetRowPointer(Row);
  6023. if Assigned(TmpData) then begin
  6024. Move(SourceData^, TmpData^, RowSize);
  6025. inc(SourceData, RowSize);
  6026. end;
  6027. end;
  6028. result := true;
  6029. end;
  6030. end;
  6031. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6032. function TglBitmapData.AssignFromSurface(const aSurface: PSDL_Surface): Boolean;
  6033. var
  6034. pSource, pData, pTempData: PByte;
  6035. Row, RowSize, TempWidth, TempHeight: Integer;
  6036. IntFormat: TglBitmapFormat;
  6037. fd: TFormatDescriptor;
  6038. Mask: TglBitmapMask;
  6039. function GetRowPointer(Row: Integer): pByte;
  6040. begin
  6041. result := aSurface^.pixels;
  6042. Inc(result, Row * RowSize);
  6043. end;
  6044. begin
  6045. result := false;
  6046. if (Assigned(aSurface)) then begin
  6047. with aSurface^.format^ do begin
  6048. Mask.r := RMask;
  6049. Mask.g := GMask;
  6050. Mask.b := BMask;
  6051. Mask.a := AMask;
  6052. IntFormat := TFormatDescriptor.GetFromMask(Mask).Format;
  6053. if (IntFormat = tfEmpty) then
  6054. raise EglBitmap.Create('AssignFromSurface - Invalid Pixelformat.');
  6055. end;
  6056. fd := TFormatDescriptor.Get(IntFormat);
  6057. TempWidth := aSurface^.w;
  6058. TempHeight := aSurface^.h;
  6059. RowSize := fd.GetSize(TempWidth, 1);
  6060. GetMem(pData, TempHeight * RowSize);
  6061. try
  6062. pTempData := pData;
  6063. for Row := 0 to TempHeight -1 do begin
  6064. pSource := GetRowPointer(Row);
  6065. if (Assigned(pSource)) then begin
  6066. Move(pSource^, pTempData^, RowSize);
  6067. Inc(pTempData, RowSize);
  6068. end;
  6069. end;
  6070. SetData(pData, IntFormat, TempWidth, TempHeight);
  6071. result := true;
  6072. except
  6073. if Assigned(pData) then
  6074. FreeMem(pData);
  6075. raise;
  6076. end;
  6077. end;
  6078. end;
  6079. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6080. function TglBitmapData.AssignAlphaToSurface(out aSurface: PSDL_Surface): Boolean;
  6081. var
  6082. Row, Col, AlphaInterleave: Integer;
  6083. pSource, pDest: PByte;
  6084. function GetRowPointer(Row: Integer): pByte;
  6085. begin
  6086. result := aSurface.pixels;
  6087. Inc(result, Row * Width);
  6088. end;
  6089. begin
  6090. result := false;
  6091. if Assigned(Data) then begin
  6092. if Format in [tfAlpha8ub1, tfLuminance8Alpha8ub2, tfBGRA8ub4, tfRGBA8ub4] then begin
  6093. aSurface := SDL_CreateRGBSurface(SDL_SWSURFACE, Width, Height, 8, $FF, $FF, $FF, 0);
  6094. AlphaInterleave := 0;
  6095. case Format of
  6096. tfLuminance8Alpha8ub2:
  6097. AlphaInterleave := 1;
  6098. tfBGRA8ub4, tfRGBA8ub4:
  6099. AlphaInterleave := 3;
  6100. end;
  6101. pSource := Data;
  6102. for Row := 0 to Height -1 do begin
  6103. pDest := GetRowPointer(Row);
  6104. if Assigned(pDest) then begin
  6105. for Col := 0 to Width -1 do begin
  6106. Inc(pSource, AlphaInterleave);
  6107. pDest^ := pSource^;
  6108. Inc(pDest);
  6109. Inc(pSource);
  6110. end;
  6111. end;
  6112. end;
  6113. result := true;
  6114. end;
  6115. end;
  6116. end;
  6117. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6118. function TglBitmapData.AddAlphaFromSurface(const aSurface: PSDL_Surface; const aFunc: TglBitmapFunction = nil; const aArgs: Pointer = nil): Boolean;
  6119. var
  6120. bmp: TglBitmap2D;
  6121. begin
  6122. bmp := TglBitmap2D.Create;
  6123. try
  6124. bmp.AssignFromSurface(aSurface);
  6125. result := AddAlphaFromGlBitmap(bmp, aFunc, aArgs);
  6126. finally
  6127. bmp.Free;
  6128. end;
  6129. end;
  6130. {$ENDIF}
  6131. {$IFDEF GLB_DELPHI}
  6132. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6133. function CreateGrayPalette: HPALETTE;
  6134. var
  6135. Idx: Integer;
  6136. Pal: PLogPalette;
  6137. begin
  6138. GetMem(Pal, SizeOf(TLogPalette) + (SizeOf(TPaletteEntry) * 256));
  6139. Pal.palVersion := $300;
  6140. Pal.palNumEntries := 256;
  6141. for Idx := 0 to Pal.palNumEntries - 1 do begin
  6142. Pal.palPalEntry[Idx].peRed := Idx;
  6143. Pal.palPalEntry[Idx].peGreen := Idx;
  6144. Pal.palPalEntry[Idx].peBlue := Idx;
  6145. Pal.palPalEntry[Idx].peFlags := 0;
  6146. end;
  6147. Result := CreatePalette(Pal^);
  6148. FreeMem(Pal);
  6149. end;
  6150. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6151. function TglBitmapData.AssignToBitmap(const aBitmap: TBitmap): Boolean;
  6152. var
  6153. Row, RowSize: Integer;
  6154. pSource, pData: PByte;
  6155. begin
  6156. result := false;
  6157. if Assigned(Data) then begin
  6158. if Assigned(aBitmap) then begin
  6159. aBitmap.Width := Width;
  6160. aBitmap.Height := Height;
  6161. case Format of
  6162. tfAlpha8ub1, tfLuminance8ub1: begin
  6163. aBitmap.PixelFormat := pf8bit;
  6164. aBitmap.Palette := CreateGrayPalette;
  6165. end;
  6166. tfRGB5A1us1:
  6167. aBitmap.PixelFormat := pf15bit;
  6168. tfR5G6B5us1:
  6169. aBitmap.PixelFormat := pf16bit;
  6170. tfRGB8ub3, tfBGR8ub3:
  6171. aBitmap.PixelFormat := pf24bit;
  6172. tfRGBA8ub4, tfBGRA8ub4:
  6173. aBitmap.PixelFormat := pf32bit;
  6174. else
  6175. raise EglBitmap.Create('AssignToBitmap - Invalid Pixelformat.');
  6176. end;
  6177. RowSize := FormatDescriptor.GetSize(Width, 1);
  6178. pSource := Data;
  6179. for Row := 0 to Height-1 do begin
  6180. pData := aBitmap.Scanline[Row];
  6181. Move(pSource^, pData^, RowSize);
  6182. Inc(pSource, RowSize);
  6183. if (Format in [tfRGB8ub3, tfRGBA8ub4]) then // swap RGB(A) to BGR(A)
  6184. SwapRGB(pData, Width, Format = tfRGBA8ub4);
  6185. end;
  6186. result := true;
  6187. end;
  6188. end;
  6189. end;
  6190. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6191. function TglBitmapData.AssignFromBitmap(const aBitmap: TBitmap): Boolean;
  6192. var
  6193. pSource, pData, pTempData: PByte;
  6194. Row, RowSize, TempWidth, TempHeight: Integer;
  6195. IntFormat: TglBitmapFormat;
  6196. begin
  6197. result := false;
  6198. if (Assigned(aBitmap)) then begin
  6199. case aBitmap.PixelFormat of
  6200. pf8bit:
  6201. IntFormat := tfLuminance8ub1;
  6202. pf15bit:
  6203. IntFormat := tfRGB5A1us1;
  6204. pf16bit:
  6205. IntFormat := tfR5G6B5us1;
  6206. pf24bit:
  6207. IntFormat := tfBGR8ub3;
  6208. pf32bit:
  6209. IntFormat := tfBGRA8ub4;
  6210. else
  6211. raise EglBitmap.Create('AssignFromBitmap - Invalid Pixelformat.');
  6212. end;
  6213. TempWidth := aBitmap.Width;
  6214. TempHeight := aBitmap.Height;
  6215. RowSize := TFormatDescriptor.Get(IntFormat).GetSize(TempWidth, 1);
  6216. GetMem(pData, TempHeight * RowSize);
  6217. try
  6218. pTempData := pData;
  6219. for Row := 0 to TempHeight -1 do begin
  6220. pSource := aBitmap.Scanline[Row];
  6221. if (Assigned(pSource)) then begin
  6222. Move(pSource^, pTempData^, RowSize);
  6223. Inc(pTempData, RowSize);
  6224. end;
  6225. end;
  6226. SetData(pData, IntFormat, TempWidth, TempHeight);
  6227. result := true;
  6228. except
  6229. if Assigned(pData) then
  6230. FreeMem(pData);
  6231. raise;
  6232. end;
  6233. end;
  6234. end;
  6235. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6236. function TglBitmapData.AssignAlphaToBitmap(const aBitmap: TBitmap): Boolean;
  6237. var
  6238. Row, Col, AlphaInterleave: Integer;
  6239. pSource, pDest: PByte;
  6240. begin
  6241. result := false;
  6242. if Assigned(Data) then begin
  6243. if (Format in [tfAlpha8ub1, tfLuminance8Alpha8ub2, tfRGBA8ub4, tfBGRA8ub4]) then begin
  6244. if Assigned(aBitmap) then begin
  6245. aBitmap.PixelFormat := pf8bit;
  6246. aBitmap.Palette := CreateGrayPalette;
  6247. aBitmap.Width := Width;
  6248. aBitmap.Height := Height;
  6249. case Format of
  6250. tfLuminance8Alpha8ub2:
  6251. AlphaInterleave := 1;
  6252. tfRGBA8ub4, tfBGRA8ub4:
  6253. AlphaInterleave := 3;
  6254. else
  6255. AlphaInterleave := 0;
  6256. end;
  6257. // Copy Data
  6258. pSource := Data;
  6259. for Row := 0 to Height -1 do begin
  6260. pDest := aBitmap.Scanline[Row];
  6261. if Assigned(pDest) then begin
  6262. for Col := 0 to Width -1 do begin
  6263. Inc(pSource, AlphaInterleave);
  6264. pDest^ := pSource^;
  6265. Inc(pDest);
  6266. Inc(pSource);
  6267. end;
  6268. end;
  6269. end;
  6270. result := true;
  6271. end;
  6272. end;
  6273. end;
  6274. end;
  6275. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6276. function TglBitmapData.AddAlphaFromBitmap(const aBitmap: TBitmap; const aFunc: TglBitmapFunction; const aArgs: Pointer): Boolean;
  6277. var
  6278. data: TglBitmapData;
  6279. begin
  6280. data := TglBitmapData.Create;
  6281. try
  6282. data.AssignFromBitmap(aBitmap);
  6283. result := AddAlphaFromDataObj(data, aFunc, aArgs);
  6284. finally
  6285. data.Free;
  6286. end;
  6287. end;
  6288. {$ENDIF}
  6289. {$IFDEF GLB_LAZARUS}
  6290. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6291. function TglBitmapData.AssignToLazIntfImage(const aImage: TLazIntfImage): Boolean;
  6292. var
  6293. rid: TRawImageDescription;
  6294. FormatDesc: TFormatDescriptor;
  6295. begin
  6296. if not Assigned(Data) then
  6297. raise EglBitmap.Create('no pixel data assigned. load data before save');
  6298. result := false;
  6299. if not Assigned(aImage) or (Format = tfEmpty) then
  6300. exit;
  6301. FormatDesc := TFormatDescriptor.Get(Format);
  6302. if FormatDesc.IsCompressed then
  6303. exit;
  6304. FillChar(rid{%H-}, SizeOf(rid), 0);
  6305. if FormatDesc.IsGrayscale then
  6306. rid.Format := ricfGray
  6307. else
  6308. rid.Format := ricfRGBA;
  6309. rid.Width := Width;
  6310. rid.Height := Height;
  6311. rid.Depth := FormatDesc.BitsPerPixel;
  6312. rid.BitOrder := riboBitsInOrder;
  6313. rid.ByteOrder := riboLSBFirst;
  6314. rid.LineOrder := riloTopToBottom;
  6315. rid.LineEnd := rileTight;
  6316. rid.BitsPerPixel := FormatDesc.BitsPerPixel;
  6317. rid.RedPrec := CountSetBits(FormatDesc.Range.r);
  6318. rid.GreenPrec := CountSetBits(FormatDesc.Range.g);
  6319. rid.BluePrec := CountSetBits(FormatDesc.Range.b);
  6320. rid.AlphaPrec := CountSetBits(FormatDesc.Range.a);
  6321. rid.RedShift := FormatDesc.Shift.r;
  6322. rid.GreenShift := FormatDesc.Shift.g;
  6323. rid.BlueShift := FormatDesc.Shift.b;
  6324. rid.AlphaShift := FormatDesc.Shift.a;
  6325. rid.MaskBitsPerPixel := 0;
  6326. rid.PaletteColorCount := 0;
  6327. aImage.DataDescription := rid;
  6328. aImage.CreateData;
  6329. if not Assigned(aImage.PixelData) then
  6330. raise EglBitmap.Create('error while creating LazIntfImage');
  6331. Move(Data^, aImage.PixelData^, FormatDesc.GetSize(Dimension));
  6332. result := true;
  6333. end;
  6334. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6335. function TglBitmapData.AssignFromLazIntfImage(const aImage: TLazIntfImage): Boolean;
  6336. var
  6337. f: TglBitmapFormat;
  6338. FormatDesc: TFormatDescriptor;
  6339. ImageData: PByte;
  6340. ImageSize: Integer;
  6341. CanCopy: Boolean;
  6342. Mask: TglBitmapRec4ul;
  6343. procedure CopyConvert;
  6344. var
  6345. bfFormat: TbmpBitfieldFormat;
  6346. pSourceLine, pDestLine: PByte;
  6347. pSourceMD, pDestMD: Pointer;
  6348. Shift, Prec: TglBitmapRec4ub;
  6349. x, y: Integer;
  6350. pixel: TglBitmapPixelData;
  6351. begin
  6352. bfFormat := TbmpBitfieldFormat.Create;
  6353. with aImage.DataDescription do begin
  6354. Prec.r := RedPrec;
  6355. Prec.g := GreenPrec;
  6356. Prec.b := BluePrec;
  6357. Prec.a := AlphaPrec;
  6358. Shift.r := RedShift;
  6359. Shift.g := GreenShift;
  6360. Shift.b := BlueShift;
  6361. Shift.a := AlphaShift;
  6362. bfFormat.SetCustomValues(BitsPerPixel, Prec, Shift);
  6363. end;
  6364. pSourceMD := bfFormat.CreateMappingData;
  6365. pDestMD := FormatDesc.CreateMappingData;
  6366. try
  6367. for y := 0 to aImage.Height-1 do begin
  6368. pSourceLine := aImage.PixelData + y {%H-}* aImage.DataDescription.BytesPerLine;
  6369. pDestLine := ImageData + y * Round(FormatDesc.BytesPerPixel * aImage.Width);
  6370. for x := 0 to aImage.Width-1 do begin
  6371. bfFormat.Unmap(pSourceLine, pixel, pSourceMD);
  6372. FormatDesc.Map(pixel, pDestLine, pDestMD);
  6373. end;
  6374. end;
  6375. finally
  6376. FormatDesc.FreeMappingData(pDestMD);
  6377. bfFormat.FreeMappingData(pSourceMD);
  6378. bfFormat.Free;
  6379. end;
  6380. end;
  6381. begin
  6382. result := false;
  6383. if not Assigned(aImage) then
  6384. exit;
  6385. with aImage.DataDescription do begin
  6386. Mask.r := (QWord(1 shl RedPrec )-1) shl RedShift;
  6387. Mask.g := (QWord(1 shl GreenPrec)-1) shl GreenShift;
  6388. Mask.b := (QWord(1 shl BluePrec )-1) shl BlueShift;
  6389. Mask.a := (QWord(1 shl AlphaPrec)-1) shl AlphaShift;
  6390. end;
  6391. FormatDesc := TFormatDescriptor.GetFromMask(Mask);
  6392. f := FormatDesc.Format;
  6393. if (f = tfEmpty) then
  6394. exit;
  6395. CanCopy :=
  6396. (FormatDesc.BitsPerPixel = aImage.DataDescription.Depth) and
  6397. (aImage.DataDescription.BitsPerPixel = aImage.DataDescription.Depth);
  6398. ImageSize := FormatDesc.GetSize(aImage.Width, aImage.Height);
  6399. ImageData := GetMem(ImageSize);
  6400. try
  6401. if CanCopy then
  6402. Move(aImage.PixelData^, ImageData^, ImageSize)
  6403. else
  6404. CopyConvert;
  6405. SetData(ImageData, f, aImage.Width, aImage.Height);
  6406. except
  6407. if Assigned(ImageData) then
  6408. FreeMem(ImageData);
  6409. raise;
  6410. end;
  6411. result := true;
  6412. end;
  6413. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6414. function TglBitmapData.AssignAlphaToLazIntfImage(const aImage: TLazIntfImage): Boolean;
  6415. var
  6416. rid: TRawImageDescription;
  6417. FormatDesc: TFormatDescriptor;
  6418. Pixel: TglBitmapPixelData;
  6419. x, y: Integer;
  6420. srcMD: Pointer;
  6421. src, dst: PByte;
  6422. begin
  6423. result := false;
  6424. if not Assigned(aImage) or (Format = tfEmpty) then
  6425. exit;
  6426. FormatDesc := TFormatDescriptor.Get(Format);
  6427. if FormatDesc.IsCompressed or not FormatDesc.HasAlpha then
  6428. exit;
  6429. FillChar(rid{%H-}, SizeOf(rid), 0);
  6430. rid.Format := ricfGray;
  6431. rid.Width := Width;
  6432. rid.Height := Height;
  6433. rid.Depth := CountSetBits(FormatDesc.Range.a);
  6434. rid.BitOrder := riboBitsInOrder;
  6435. rid.ByteOrder := riboLSBFirst;
  6436. rid.LineOrder := riloTopToBottom;
  6437. rid.LineEnd := rileTight;
  6438. rid.BitsPerPixel := 8 * Ceil(rid.Depth / 8);
  6439. rid.RedPrec := CountSetBits(FormatDesc.Range.a);
  6440. rid.GreenPrec := 0;
  6441. rid.BluePrec := 0;
  6442. rid.AlphaPrec := 0;
  6443. rid.RedShift := 0;
  6444. rid.GreenShift := 0;
  6445. rid.BlueShift := 0;
  6446. rid.AlphaShift := 0;
  6447. rid.MaskBitsPerPixel := 0;
  6448. rid.PaletteColorCount := 0;
  6449. aImage.DataDescription := rid;
  6450. aImage.CreateData;
  6451. srcMD := FormatDesc.CreateMappingData;
  6452. try
  6453. FormatDesc.PreparePixel(Pixel);
  6454. src := Data;
  6455. dst := aImage.PixelData;
  6456. for y := 0 to Height-1 do
  6457. for x := 0 to Width-1 do begin
  6458. FormatDesc.Unmap(src, Pixel, srcMD);
  6459. case rid.BitsPerPixel of
  6460. 8: begin
  6461. dst^ := Pixel.Data.a;
  6462. inc(dst);
  6463. end;
  6464. 16: begin
  6465. PWord(dst)^ := Pixel.Data.a;
  6466. inc(dst, 2);
  6467. end;
  6468. 24: begin
  6469. PByteArray(dst)^[0] := PByteArray(@Pixel.Data.a)^[0];
  6470. PByteArray(dst)^[1] := PByteArray(@Pixel.Data.a)^[1];
  6471. PByteArray(dst)^[2] := PByteArray(@Pixel.Data.a)^[2];
  6472. inc(dst, 3);
  6473. end;
  6474. 32: begin
  6475. PCardinal(dst)^ := Pixel.Data.a;
  6476. inc(dst, 4);
  6477. end;
  6478. else
  6479. raise EglBitmapUnsupportedFormat.Create(Format);
  6480. end;
  6481. end;
  6482. finally
  6483. FormatDesc.FreeMappingData(srcMD);
  6484. end;
  6485. result := true;
  6486. end;
  6487. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6488. function TglBitmapData.AddAlphaFromLazIntfImage(const aImage: TLazIntfImage; const aFunc: TglBitmapFunction; const aArgs: Pointer): Boolean;
  6489. var
  6490. data: TglBitmapData;
  6491. begin
  6492. data := TglBitmapData.Create;
  6493. try
  6494. data.AssignFromLazIntfImage(aImage);
  6495. result := AddAlphaFromDataObj(data, aFunc, aArgs);
  6496. finally
  6497. data.Free;
  6498. end;
  6499. end;
  6500. {$ENDIF}
  6501. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6502. function TglBitmapData.AddAlphaFromResource(const aInstance: Cardinal; aResource: String; aResType: PChar;
  6503. const aFunc: TglBitmapFunction; const aArgs: Pointer): Boolean;
  6504. var
  6505. rs: TResourceStream;
  6506. begin
  6507. PrepareResType(aResource, aResType);
  6508. rs := TResourceStream.Create(aInstance, aResource, aResType);
  6509. try
  6510. result := AddAlphaFromStream(rs, aFunc, aArgs);
  6511. finally
  6512. rs.Free;
  6513. end;
  6514. end;
  6515. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6516. function TglBitmapData.AddAlphaFromResourceID(const aInstance: Cardinal; const aResourceID: Integer; const aResType: PChar;
  6517. const aFunc: TglBitmapFunction; const aArgs: Pointer): Boolean;
  6518. var
  6519. rs: TResourceStream;
  6520. begin
  6521. rs := TResourceStream.CreateFromID(aInstance, aResourceID, aResType);
  6522. try
  6523. result := AddAlphaFromStream(rs, aFunc, aArgs);
  6524. finally
  6525. rs.Free;
  6526. end;
  6527. end;
  6528. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6529. function TglBitmapData.AddAlphaFromFunc(const aFunc: TglBitmapFunction; const aArgs: Pointer): Boolean;
  6530. begin
  6531. if TFormatDescriptor.Get(Format).IsCompressed then
  6532. raise EglBitmapUnsupportedFormat.Create(Format);
  6533. result := Convert(Self, aFunc, false, TFormatDescriptor.Get(Format).WithAlpha, aArgs);
  6534. end;
  6535. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6536. function TglBitmapData.AddAlphaFromFile(const aFileName: String; const aFunc: TglBitmapFunction; const aArgs: Pointer): Boolean;
  6537. var
  6538. FS: TFileStream;
  6539. begin
  6540. FS := TFileStream.Create(aFileName, fmOpenRead);
  6541. try
  6542. result := AddAlphaFromStream(FS, aFunc, aArgs);
  6543. finally
  6544. FS.Free;
  6545. end;
  6546. end;
  6547. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6548. function TglBitmapData.AddAlphaFromStream(const aStream: TStream; const aFunc: TglBitmapFunction; const aArgs: Pointer): Boolean;
  6549. var
  6550. data: TglBitmapData;
  6551. begin
  6552. data := TglBitmapData.Create(aStream);
  6553. try
  6554. result := AddAlphaFromDataObj(data, aFunc, aArgs);
  6555. finally
  6556. data.Free;
  6557. end;
  6558. end;
  6559. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6560. function TglBitmapData.AddAlphaFromDataObj(const aDataObj: TglBitmapData; aFunc: TglBitmapFunction; const aArgs: Pointer): Boolean;
  6561. var
  6562. DestData, DestData2, SourceData: pByte;
  6563. TempHeight, TempWidth: Integer;
  6564. SourceFD, DestFD: TFormatDescriptor;
  6565. SourceMD, DestMD, DestMD2: Pointer;
  6566. FuncRec: TglBitmapFunctionRec;
  6567. begin
  6568. result := false;
  6569. Assert(Assigned(Data));
  6570. Assert(Assigned(aDataObj));
  6571. Assert(Assigned(aDataObj.Data));
  6572. if ((aDataObj.Width = Width) and (aDataObj.Height = Height)) then begin
  6573. result := ConvertTo(TFormatDescriptor.Get(Format).WithAlpha);
  6574. SourceFD := TFormatDescriptor.Get(aDataObj.Format);
  6575. DestFD := TFormatDescriptor.Get(Format);
  6576. if not Assigned(aFunc) then begin
  6577. aFunc := glBitmapAlphaFunc;
  6578. FuncRec.Args := {%H-}Pointer(SourceFD.HasAlpha);
  6579. end else
  6580. FuncRec.Args := aArgs;
  6581. // Values
  6582. TempWidth := aDataObj.Width;
  6583. TempHeight := aDataObj.Height;
  6584. if (TempWidth <= 0) or (TempHeight <= 0) then
  6585. exit;
  6586. FuncRec.Sender := Self;
  6587. FuncRec.Size := Dimension;
  6588. FuncRec.Position.Fields := FuncRec.Size.Fields;
  6589. DestData := Data;
  6590. DestData2 := Data;
  6591. SourceData := aDataObj.Data;
  6592. // Mapping
  6593. SourceFD.PreparePixel(FuncRec.Source);
  6594. DestFD.PreparePixel (FuncRec.Dest);
  6595. SourceMD := SourceFD.CreateMappingData;
  6596. DestMD := DestFD.CreateMappingData;
  6597. DestMD2 := DestFD.CreateMappingData;
  6598. try
  6599. FuncRec.Position.Y := 0;
  6600. while FuncRec.Position.Y < TempHeight do begin
  6601. FuncRec.Position.X := 0;
  6602. while FuncRec.Position.X < TempWidth do begin
  6603. SourceFD.Unmap(SourceData, FuncRec.Source, SourceMD);
  6604. DestFD.Unmap (DestData, FuncRec.Dest, DestMD);
  6605. aFunc(FuncRec);
  6606. DestFD.Map(FuncRec.Dest, DestData2, DestMD2);
  6607. inc(FuncRec.Position.X);
  6608. end;
  6609. inc(FuncRec.Position.Y);
  6610. end;
  6611. finally
  6612. SourceFD.FreeMappingData(SourceMD);
  6613. DestFD.FreeMappingData(DestMD);
  6614. DestFD.FreeMappingData(DestMD2);
  6615. end;
  6616. end;
  6617. end;
  6618. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6619. function TglBitmapData.AddAlphaFromColorKey(const aRed, aGreen, aBlue: Byte; const aDeviation: Byte): Boolean;
  6620. begin
  6621. result := AddAlphaFromColorKeyFloat(aRed / $FF, aGreen / $FF, aBlue / $FF, aDeviation / $FF);
  6622. end;
  6623. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6624. function TglBitmapData.AddAlphaFromColorKeyRange(const aRed, aGreen, aBlue: Cardinal; const aDeviation: Cardinal): Boolean;
  6625. var
  6626. PixelData: TglBitmapPixelData;
  6627. begin
  6628. TFormatDescriptor.GetAlpha(Format).PreparePixel(PixelData);
  6629. result := AddAlphaFromColorKeyFloat(
  6630. aRed / PixelData.Range.r,
  6631. aGreen / PixelData.Range.g,
  6632. aBlue / PixelData.Range.b,
  6633. aDeviation / Max(PixelData.Range.r, Max(PixelData.Range.g, PixelData.Range.b)));
  6634. end;
  6635. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6636. function TglBitmapData.AddAlphaFromColorKeyFloat(const aRed, aGreen, aBlue: Single; const aDeviation: Single): Boolean;
  6637. var
  6638. values: array[0..2] of Single;
  6639. tmp: Cardinal;
  6640. i: Integer;
  6641. PixelData: TglBitmapPixelData;
  6642. begin
  6643. TFormatDescriptor.GetAlpha(Format).PreparePixel(PixelData);
  6644. with PixelData do begin
  6645. values[0] := aRed;
  6646. values[1] := aGreen;
  6647. values[2] := aBlue;
  6648. for i := 0 to 2 do begin
  6649. tmp := Trunc(Range.arr[i] * aDeviation);
  6650. Data.arr[i] := Min(Range.arr[i], Trunc(Range.arr[i] * values[i] + tmp));
  6651. Range.arr[i] := Max(0, Trunc(Range.arr[i] * values[i] - tmp));
  6652. end;
  6653. Data.a := 0;
  6654. Range.a := 0;
  6655. end;
  6656. result := AddAlphaFromFunc(glBitmapColorKeyAlphaFunc, @PixelData);
  6657. end;
  6658. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6659. function TglBitmapData.AddAlphaFromValue(const aAlpha: Byte): Boolean;
  6660. begin
  6661. result := AddAlphaFromValueFloat(aAlpha / $FF);
  6662. end;
  6663. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6664. function TglBitmapData.AddAlphaFromValueRange(const aAlpha: Cardinal): Boolean;
  6665. var
  6666. PixelData: TglBitmapPixelData;
  6667. begin
  6668. TFormatDescriptor.GetAlpha(Format).PreparePixel(PixelData);
  6669. result := AddAlphaFromValueFloat(aAlpha / PixelData.Range.a);
  6670. end;
  6671. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6672. function TglBitmapData.AddAlphaFromValueFloat(const aAlpha: Single): Boolean;
  6673. var
  6674. PixelData: TglBitmapPixelData;
  6675. begin
  6676. TFormatDescriptor.GetAlpha(Format).PreparePixel(PixelData);
  6677. with PixelData do
  6678. Data.a := Min(Range.a, Max(0, Round(Range.a * aAlpha)));
  6679. result := AddAlphaFromFunc(glBitmapValueAlphaFunc, @PixelData.Data.a);
  6680. end;
  6681. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6682. function TglBitmapData.RemoveAlpha: Boolean;
  6683. var
  6684. FormatDesc: TFormatDescriptor;
  6685. begin
  6686. result := false;
  6687. FormatDesc := TFormatDescriptor.Get(Format);
  6688. if Assigned(Data) then begin
  6689. if FormatDesc.IsCompressed or not FormatDesc.HasAlpha then
  6690. raise EglBitmapUnsupportedFormat.Create(Format);
  6691. result := ConvertTo(FormatDesc.WithoutAlpha);
  6692. end;
  6693. end;
  6694. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6695. procedure TglBitmapData.FillWithColor(const aRed, aGreen, aBlue: Byte;
  6696. const aAlpha: Byte);
  6697. begin
  6698. FillWithColorFloat(aRed/$FF, aGreen/$FF, aBlue/$FF, aAlpha/$FF);
  6699. end;
  6700. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6701. procedure TglBitmapData.FillWithColorRange(const aRed, aGreen, aBlue: Cardinal; const aAlpha: Cardinal);
  6702. var
  6703. PixelData: TglBitmapPixelData;
  6704. begin
  6705. TFormatDescriptor.GetAlpha(Format).PreparePixel(PixelData);
  6706. FillWithColorFloat(
  6707. aRed / PixelData.Range.r,
  6708. aGreen / PixelData.Range.g,
  6709. aBlue / PixelData.Range.b,
  6710. aAlpha / PixelData.Range.a);
  6711. end;
  6712. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6713. procedure TglBitmapData.FillWithColorFloat(const aRed, aGreen, aBlue: Single; const aAlpha: Single);
  6714. var
  6715. PixelData: TglBitmapPixelData;
  6716. begin
  6717. TFormatDescriptor.Get(Format).PreparePixel(PixelData);
  6718. with PixelData do begin
  6719. Data.r := Max(0, Min(Range.r, Trunc(Range.r * aRed)));
  6720. Data.g := Max(0, Min(Range.g, Trunc(Range.g * aGreen)));
  6721. Data.b := Max(0, Min(Range.b, Trunc(Range.b * aBlue)));
  6722. Data.a := Max(0, Min(Range.a, Trunc(Range.a * aAlpha)));
  6723. end;
  6724. Convert(glBitmapFillWithColorFunc, false, @PixelData);
  6725. end;
  6726. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6727. procedure TglBitmapData.SetData(const aData: PByte; const aFormat: TglBitmapFormat; const aWidth: Integer; const aHeight: Integer);
  6728. begin
  6729. if (Data <> aData) then begin
  6730. if (Assigned(Data)) then
  6731. FreeMem(Data);
  6732. fData := aData;
  6733. end;
  6734. if Assigned(fData) then begin
  6735. FillChar(fDimension, SizeOf(fDimension), 0);
  6736. if aWidth <> -1 then begin
  6737. fDimension.Fields := fDimension.Fields + [ffX];
  6738. fDimension.X := aWidth;
  6739. end;
  6740. if aHeight <> -1 then begin
  6741. fDimension.Fields := fDimension.Fields + [ffY];
  6742. fDimension.Y := aHeight;
  6743. end;
  6744. fFormat := aFormat;
  6745. end else
  6746. fFormat := tfEmpty;
  6747. UpdateScanlines;
  6748. end;
  6749. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6750. function TglBitmapData.Clone: TglBitmapData;
  6751. var
  6752. Temp: TglBitmapData;
  6753. TempPtr: PByte;
  6754. Size: Integer;
  6755. begin
  6756. result := nil;
  6757. Temp := (ClassType.Create as TglBitmapData);
  6758. try
  6759. // copy texture data if assigned
  6760. if Assigned(Data) then begin
  6761. Size := TFormatDescriptor.Get(Format).GetSize(fDimension);
  6762. GetMem(TempPtr, Size);
  6763. try
  6764. Move(Data^, TempPtr^, Size);
  6765. Temp.SetData(TempPtr, Format, Width, Height);
  6766. except
  6767. if Assigned(TempPtr) then
  6768. FreeMem(TempPtr);
  6769. raise;
  6770. end;
  6771. end else begin
  6772. TempPtr := nil;
  6773. Temp.SetData(TempPtr, Format, Width, Height);
  6774. end;
  6775. // copy properties
  6776. Temp.fFormat := Format;
  6777. result := Temp;
  6778. except
  6779. FreeAndNil(Temp);
  6780. raise;
  6781. end;
  6782. end;
  6783. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6784. procedure TglBitmapData.Invert(const aRed, aGreen, aBlue, aAlpha: Boolean);
  6785. var
  6786. mask: PtrInt;
  6787. begin
  6788. mask :=
  6789. (Byte(aRed) and 1) or
  6790. ((Byte(aGreen) and 1) shl 1) or
  6791. ((Byte(aBlue) and 1) shl 2) or
  6792. ((Byte(aAlpha) and 1) shl 3);
  6793. if (mask > 0) then
  6794. Convert(glBitmapInvertFunc, false, {%H-}Pointer(mask));
  6795. end;
  6796. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6797. type
  6798. TMatrixItem = record
  6799. X, Y: Integer;
  6800. W: Single;
  6801. end;
  6802. PglBitmapToNormalMapRec = ^TglBitmapToNormalMapRec;
  6803. TglBitmapToNormalMapRec = Record
  6804. Scale: Single;
  6805. Heights: array of Single;
  6806. MatrixU : array of TMatrixItem;
  6807. MatrixV : array of TMatrixItem;
  6808. end;
  6809. const
  6810. ONE_OVER_255 = 1 / 255;
  6811. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6812. procedure glBitmapToNormalMapPrepareFunc(var FuncRec: TglBitmapFunctionRec);
  6813. var
  6814. Val: Single;
  6815. begin
  6816. with FuncRec do begin
  6817. Val :=
  6818. Source.Data.r * LUMINANCE_WEIGHT_R +
  6819. Source.Data.g * LUMINANCE_WEIGHT_G +
  6820. Source.Data.b * LUMINANCE_WEIGHT_B;
  6821. PglBitmapToNormalMapRec(Args)^.Heights[Position.Y * Size.X + Position.X] := Val * ONE_OVER_255;
  6822. end;
  6823. end;
  6824. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6825. procedure glBitmapToNormalMapPrepareAlphaFunc(var FuncRec: TglBitmapFunctionRec);
  6826. begin
  6827. with FuncRec do
  6828. PglBitmapToNormalMapRec(Args)^.Heights[Position.Y * Size.X + Position.X] := Source.Data.a * ONE_OVER_255;
  6829. end;
  6830. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6831. procedure glBitmapToNormalMapFunc (var FuncRec: TglBitmapFunctionRec);
  6832. type
  6833. TVec = Array[0..2] of Single;
  6834. var
  6835. Idx: Integer;
  6836. du, dv: Double;
  6837. Len: Single;
  6838. Vec: TVec;
  6839. function GetHeight(X, Y: Integer): Single;
  6840. begin
  6841. with FuncRec do begin
  6842. X := Max(0, Min(Size.X -1, X));
  6843. Y := Max(0, Min(Size.Y -1, Y));
  6844. result := PglBitmapToNormalMapRec(Args)^.Heights[Y * Size.X + X];
  6845. end;
  6846. end;
  6847. begin
  6848. with FuncRec do begin
  6849. with PglBitmapToNormalMapRec(Args)^ do begin
  6850. du := 0;
  6851. for Idx := Low(MatrixU) to High(MatrixU) do
  6852. du := du + GetHeight(Position.X + MatrixU[Idx].X, Position.Y + MatrixU[Idx].Y) * MatrixU[Idx].W;
  6853. dv := 0;
  6854. for Idx := Low(MatrixU) to High(MatrixU) do
  6855. dv := dv + GetHeight(Position.X + MatrixV[Idx].X, Position.Y + MatrixV[Idx].Y) * MatrixV[Idx].W;
  6856. Vec[0] := -du * Scale;
  6857. Vec[1] := -dv * Scale;
  6858. Vec[2] := 1;
  6859. end;
  6860. // Normalize
  6861. Len := 1 / Sqrt(Sqr(Vec[0]) + Sqr(Vec[1]) + Sqr(Vec[2]));
  6862. if Len <> 0 then begin
  6863. Vec[0] := Vec[0] * Len;
  6864. Vec[1] := Vec[1] * Len;
  6865. Vec[2] := Vec[2] * Len;
  6866. end;
  6867. // Farbe zuweisem
  6868. Dest.Data.r := Trunc((Vec[0] + 1) * 127.5);
  6869. Dest.Data.g := Trunc((Vec[1] + 1) * 127.5);
  6870. Dest.Data.b := Trunc((Vec[2] + 1) * 127.5);
  6871. end;
  6872. end;
  6873. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6874. procedure TglBitmapData.GenerateNormalMap(const aFunc: TglBitmapNormalMapFunc; const aScale: Single; const aUseAlpha: Boolean);
  6875. var
  6876. Rec: TglBitmapToNormalMapRec;
  6877. procedure SetEntry (var Matrix: array of TMatrixItem; Index, X, Y: Integer; W: Single);
  6878. begin
  6879. if (Index >= Low(Matrix)) and (Index <= High(Matrix)) then begin
  6880. Matrix[Index].X := X;
  6881. Matrix[Index].Y := Y;
  6882. Matrix[Index].W := W;
  6883. end;
  6884. end;
  6885. begin
  6886. if TFormatDescriptor.Get(Format).IsCompressed then
  6887. raise EglBitmapUnsupportedFormat.Create(Format);
  6888. if aScale > 100 then
  6889. Rec.Scale := 100
  6890. else if aScale < -100 then
  6891. Rec.Scale := -100
  6892. else
  6893. Rec.Scale := aScale;
  6894. SetLength(Rec.Heights, Width * Height);
  6895. try
  6896. case aFunc of
  6897. nm4Samples: begin
  6898. SetLength(Rec.MatrixU, 2);
  6899. SetEntry(Rec.MatrixU, 0, -1, 0, -0.5);
  6900. SetEntry(Rec.MatrixU, 1, 1, 0, 0.5);
  6901. SetLength(Rec.MatrixV, 2);
  6902. SetEntry(Rec.MatrixV, 0, 0, 1, 0.5);
  6903. SetEntry(Rec.MatrixV, 1, 0, -1, -0.5);
  6904. end;
  6905. nmSobel: begin
  6906. SetLength(Rec.MatrixU, 6);
  6907. SetEntry(Rec.MatrixU, 0, -1, 1, -1.0);
  6908. SetEntry(Rec.MatrixU, 1, -1, 0, -2.0);
  6909. SetEntry(Rec.MatrixU, 2, -1, -1, -1.0);
  6910. SetEntry(Rec.MatrixU, 3, 1, 1, 1.0);
  6911. SetEntry(Rec.MatrixU, 4, 1, 0, 2.0);
  6912. SetEntry(Rec.MatrixU, 5, 1, -1, 1.0);
  6913. SetLength(Rec.MatrixV, 6);
  6914. SetEntry(Rec.MatrixV, 0, -1, 1, 1.0);
  6915. SetEntry(Rec.MatrixV, 1, 0, 1, 2.0);
  6916. SetEntry(Rec.MatrixV, 2, 1, 1, 1.0);
  6917. SetEntry(Rec.MatrixV, 3, -1, -1, -1.0);
  6918. SetEntry(Rec.MatrixV, 4, 0, -1, -2.0);
  6919. SetEntry(Rec.MatrixV, 5, 1, -1, -1.0);
  6920. end;
  6921. nm3x3: begin
  6922. SetLength(Rec.MatrixU, 6);
  6923. SetEntry(Rec.MatrixU, 0, -1, 1, -1/6);
  6924. SetEntry(Rec.MatrixU, 1, -1, 0, -1/6);
  6925. SetEntry(Rec.MatrixU, 2, -1, -1, -1/6);
  6926. SetEntry(Rec.MatrixU, 3, 1, 1, 1/6);
  6927. SetEntry(Rec.MatrixU, 4, 1, 0, 1/6);
  6928. SetEntry(Rec.MatrixU, 5, 1, -1, 1/6);
  6929. SetLength(Rec.MatrixV, 6);
  6930. SetEntry(Rec.MatrixV, 0, -1, 1, 1/6);
  6931. SetEntry(Rec.MatrixV, 1, 0, 1, 1/6);
  6932. SetEntry(Rec.MatrixV, 2, 1, 1, 1/6);
  6933. SetEntry(Rec.MatrixV, 3, -1, -1, -1/6);
  6934. SetEntry(Rec.MatrixV, 4, 0, -1, -1/6);
  6935. SetEntry(Rec.MatrixV, 5, 1, -1, -1/6);
  6936. end;
  6937. nm5x5: begin
  6938. SetLength(Rec.MatrixU, 20);
  6939. SetEntry(Rec.MatrixU, 0, -2, 2, -1 / 16);
  6940. SetEntry(Rec.MatrixU, 1, -1, 2, -1 / 10);
  6941. SetEntry(Rec.MatrixU, 2, 1, 2, 1 / 10);
  6942. SetEntry(Rec.MatrixU, 3, 2, 2, 1 / 16);
  6943. SetEntry(Rec.MatrixU, 4, -2, 1, -1 / 10);
  6944. SetEntry(Rec.MatrixU, 5, -1, 1, -1 / 8);
  6945. SetEntry(Rec.MatrixU, 6, 1, 1, 1 / 8);
  6946. SetEntry(Rec.MatrixU, 7, 2, 1, 1 / 10);
  6947. SetEntry(Rec.MatrixU, 8, -2, 0, -1 / 2.8);
  6948. SetEntry(Rec.MatrixU, 9, -1, 0, -0.5);
  6949. SetEntry(Rec.MatrixU, 10, 1, 0, 0.5);
  6950. SetEntry(Rec.MatrixU, 11, 2, 0, 1 / 2.8);
  6951. SetEntry(Rec.MatrixU, 12, -2, -1, -1 / 10);
  6952. SetEntry(Rec.MatrixU, 13, -1, -1, -1 / 8);
  6953. SetEntry(Rec.MatrixU, 14, 1, -1, 1 / 8);
  6954. SetEntry(Rec.MatrixU, 15, 2, -1, 1 / 10);
  6955. SetEntry(Rec.MatrixU, 16, -2, -2, -1 / 16);
  6956. SetEntry(Rec.MatrixU, 17, -1, -2, -1 / 10);
  6957. SetEntry(Rec.MatrixU, 18, 1, -2, 1 / 10);
  6958. SetEntry(Rec.MatrixU, 19, 2, -2, 1 / 16);
  6959. SetLength(Rec.MatrixV, 20);
  6960. SetEntry(Rec.MatrixV, 0, -2, 2, 1 / 16);
  6961. SetEntry(Rec.MatrixV, 1, -1, 2, 1 / 10);
  6962. SetEntry(Rec.MatrixV, 2, 0, 2, 0.25);
  6963. SetEntry(Rec.MatrixV, 3, 1, 2, 1 / 10);
  6964. SetEntry(Rec.MatrixV, 4, 2, 2, 1 / 16);
  6965. SetEntry(Rec.MatrixV, 5, -2, 1, 1 / 10);
  6966. SetEntry(Rec.MatrixV, 6, -1, 1, 1 / 8);
  6967. SetEntry(Rec.MatrixV, 7, 0, 1, 0.5);
  6968. SetEntry(Rec.MatrixV, 8, 1, 1, 1 / 8);
  6969. SetEntry(Rec.MatrixV, 9, 2, 1, 1 / 16);
  6970. SetEntry(Rec.MatrixV, 10, -2, -1, -1 / 16);
  6971. SetEntry(Rec.MatrixV, 11, -1, -1, -1 / 8);
  6972. SetEntry(Rec.MatrixV, 12, 0, -1, -0.5);
  6973. SetEntry(Rec.MatrixV, 13, 1, -1, -1 / 8);
  6974. SetEntry(Rec.MatrixV, 14, 2, -1, -1 / 10);
  6975. SetEntry(Rec.MatrixV, 15, -2, -2, -1 / 16);
  6976. SetEntry(Rec.MatrixV, 16, -1, -2, -1 / 10);
  6977. SetEntry(Rec.MatrixV, 17, 0, -2, -0.25);
  6978. SetEntry(Rec.MatrixV, 18, 1, -2, -1 / 10);
  6979. SetEntry(Rec.MatrixV, 19, 2, -2, -1 / 16);
  6980. end;
  6981. end;
  6982. // Daten Sammeln
  6983. if aUseAlpha and TFormatDescriptor.Get(Format).HasAlpha then
  6984. Convert(glBitmapToNormalMapPrepareAlphaFunc, false, @Rec)
  6985. else
  6986. Convert(glBitmapToNormalMapPrepareFunc, false, @Rec);
  6987. Convert(glBitmapToNormalMapFunc, false, @Rec);
  6988. finally
  6989. SetLength(Rec.Heights, 0);
  6990. end;
  6991. end;
  6992. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6993. constructor TglBitmapData.Create;
  6994. begin
  6995. inherited Create;
  6996. fFormat := glBitmapDefaultFormat;
  6997. end;
  6998. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6999. constructor TglBitmapData.Create(const aFileName: String);
  7000. begin
  7001. Create;
  7002. LoadFromFile(aFileName);
  7003. end;
  7004. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7005. constructor TglBitmapData.Create(const aStream: TStream);
  7006. begin
  7007. Create;
  7008. LoadFromStream(aStream);
  7009. end;
  7010. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7011. constructor TglBitmapData.Create(const aSize: TglBitmapSize; const aFormat: TglBitmapFormat; aData: PByte);
  7012. var
  7013. ImageSize: Integer;
  7014. begin
  7015. Create;
  7016. if not Assigned(aData) then begin
  7017. ImageSize := TFormatDescriptor.Get(aFormat).GetSize(aSize);
  7018. GetMem(aData, ImageSize);
  7019. try
  7020. FillChar(aData^, ImageSize, #$FF);
  7021. SetData(aData, aFormat, aSize.X, aSize.Y);
  7022. except
  7023. if Assigned(aData) then
  7024. FreeMem(aData);
  7025. raise;
  7026. end;
  7027. end else begin
  7028. SetData(aData, aFormat, aSize.X, aSize.Y);
  7029. end;
  7030. end;
  7031. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7032. constructor TglBitmapData.Create(const aSize: TglBitmapSize; const aFormat: TglBitmapFormat; const aFunc: TglBitmapFunction; const aArgs: Pointer);
  7033. begin
  7034. Create;
  7035. LoadFromFunc(aSize, aFormat, aFunc, aArgs);
  7036. end;
  7037. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7038. constructor TglBitmapData.Create(const aInstance: Cardinal; const aResource: String; const aResType: PChar);
  7039. begin
  7040. Create;
  7041. LoadFromResource(aInstance, aResource, aResType);
  7042. end;
  7043. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7044. constructor TglBitmapData.Create(const aInstance: Cardinal; const aResourceID: Integer; const aResType: PChar);
  7045. begin
  7046. Create;
  7047. LoadFromResourceID(aInstance, aResourceID, aResType);
  7048. end;
  7049. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7050. destructor TglBitmapData.Destroy;
  7051. begin
  7052. SetData(nil, tfEmpty);
  7053. inherited Destroy;
  7054. end;
  7055. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7056. //TglBitmap - PROTECTED///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7057. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7058. function TglBitmap.GetWidth: Integer;
  7059. begin
  7060. if (ffX in fDimension.Fields) then
  7061. result := fDimension.X
  7062. else
  7063. result := -1;
  7064. end;
  7065. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7066. function TglBitmap.GetHeight: Integer;
  7067. begin
  7068. if (ffY in fDimension.Fields) then
  7069. result := fDimension.Y
  7070. else
  7071. result := -1;
  7072. end;
  7073. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7074. procedure TglBitmap.SetCustomData(const aValue: Pointer);
  7075. begin
  7076. if fCustomData = aValue then
  7077. exit;
  7078. fCustomData := aValue;
  7079. end;
  7080. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7081. procedure TglBitmap.SetCustomName(const aValue: String);
  7082. begin
  7083. if fCustomName = aValue then
  7084. exit;
  7085. fCustomName := aValue;
  7086. end;
  7087. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7088. procedure TglBitmap.SetCustomNameW(const aValue: WideString);
  7089. begin
  7090. if fCustomNameW = aValue then
  7091. exit;
  7092. fCustomNameW := aValue;
  7093. end;
  7094. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7095. procedure TglBitmap.SetDeleteTextureOnFree(const aValue: Boolean);
  7096. begin
  7097. if fDeleteTextureOnFree = aValue then
  7098. exit;
  7099. fDeleteTextureOnFree := aValue;
  7100. end;
  7101. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7102. procedure TglBitmap.SetID(const aValue: Cardinal);
  7103. begin
  7104. if fID = aValue then
  7105. exit;
  7106. fID := aValue;
  7107. end;
  7108. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7109. procedure TglBitmap.SetMipMap(const aValue: TglBitmapMipMap);
  7110. begin
  7111. if fMipMap = aValue then
  7112. exit;
  7113. fMipMap := aValue;
  7114. end;
  7115. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7116. procedure TglBitmap.SetTarget(const aValue: Cardinal);
  7117. begin
  7118. if fTarget = aValue then
  7119. exit;
  7120. fTarget := aValue;
  7121. end;
  7122. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7123. procedure TglBitmap.SetAnisotropic(const aValue: Integer);
  7124. {$IF NOT DEFINED(OPENGL_ES) OR DEFINED(OPENGL_ES_EXT)}
  7125. var
  7126. MaxAnisotropic: Integer;
  7127. {$IFEND}
  7128. begin
  7129. fAnisotropic := aValue;
  7130. if (ID > 0) then begin
  7131. {$IF NOT DEFINED(OPENGL_ES) OR DEFINED(OPENGL_ES_EXT)}
  7132. if GL_EXT_texture_filter_anisotropic then begin
  7133. if fAnisotropic > 0 then begin
  7134. Bind({$IFNDEF OPENGL_ES}false{$ENDIF});
  7135. glGetIntegerv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, @MaxAnisotropic);
  7136. if aValue > MaxAnisotropic then
  7137. fAnisotropic := MaxAnisotropic;
  7138. glTexParameteri(Target, GL_TEXTURE_MAX_ANISOTROPY_EXT, fAnisotropic);
  7139. end;
  7140. end else begin
  7141. fAnisotropic := 0;
  7142. end;
  7143. {$ELSE}
  7144. fAnisotropic := 0;
  7145. {$IFEND}
  7146. end;
  7147. end;
  7148. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7149. procedure TglBitmap.CreateID;
  7150. begin
  7151. if (ID <> 0) then
  7152. glDeleteTextures(1, @fID);
  7153. glGenTextures(1, @fID);
  7154. Bind({$IFNDEF OPENGL_ES}false{$ENDIF});
  7155. end;
  7156. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7157. procedure TglBitmap.SetupParameters({$IFNDEF OPENGL_ES}out aBuildWithGlu: Boolean{$ENDIF});
  7158. begin
  7159. // Set Up Parameters
  7160. SetWrap(fWrapS, fWrapT, fWrapR);
  7161. SetFilter(fFilterMin, fFilterMag);
  7162. SetAnisotropic(fAnisotropic);
  7163. {$IFNDEF OPENGL_ES}
  7164. SetBorderColor(fBorderColor[0], fBorderColor[1], fBorderColor[2], fBorderColor[3]);
  7165. if (GL_ARB_texture_swizzle or GL_EXT_texture_swizzle or GL_VERSION_3_3) then
  7166. SetSwizzle(fSwizzle[0], fSwizzle[1], fSwizzle[2], fSwizzle[3]);
  7167. {$ENDIF}
  7168. {$IFNDEF OPENGL_ES}
  7169. // Mip Maps Generation Mode
  7170. aBuildWithGlu := false;
  7171. if (MipMap = mmMipmap) then begin
  7172. if (GL_VERSION_1_4 or GL_SGIS_generate_mipmap) then
  7173. glTexParameteri(Target, GL_GENERATE_MIPMAP, GLint(GL_TRUE))
  7174. else
  7175. aBuildWithGlu := true;
  7176. end else if (MipMap = mmMipmapGlu) then
  7177. aBuildWithGlu := true;
  7178. {$ELSE}
  7179. if (MipMap = mmMipmap) then
  7180. glGenerateMipmap(Target);
  7181. {$ENDIF}
  7182. end;
  7183. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7184. //TglBitmap - PUBLIC//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7185. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7186. procedure TglBitmap.AfterConstruction;
  7187. begin
  7188. inherited AfterConstruction;
  7189. fID := 0;
  7190. fTarget := 0;
  7191. {$IFNDEF OPENGL_ES}
  7192. fIsResident := false;
  7193. {$ENDIF}
  7194. fMipMap := glBitmapDefaultMipmap;
  7195. fDeleteTextureOnFree := glBitmapGetDefaultDeleteTextureOnFree;
  7196. glBitmapGetDefaultFilter (fFilterMin, fFilterMag);
  7197. glBitmapGetDefaultTextureWrap(fWrapS, fWrapT, fWrapR);
  7198. {$IFNDEF OPENGL_ES}
  7199. glBitmapGetDefaultSwizzle (fSwizzle[0], fSwizzle[1], fSwizzle[2], fSwizzle[3]);
  7200. {$ENDIF}
  7201. end;
  7202. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7203. procedure TglBitmap.BeforeDestruction;
  7204. begin
  7205. if (fID > 0) and fDeleteTextureOnFree then
  7206. glDeleteTextures(1, @fID);
  7207. inherited BeforeDestruction;
  7208. end;
  7209. {$IFNDEF OPENGL_ES}
  7210. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7211. procedure TglBitmap.SetBorderColor(const aRed, aGreen, aBlue, aAlpha: Single);
  7212. begin
  7213. fBorderColor[0] := aRed;
  7214. fBorderColor[1] := aGreen;
  7215. fBorderColor[2] := aBlue;
  7216. fBorderColor[3] := aAlpha;
  7217. if (ID > 0) then begin
  7218. Bind(false);
  7219. glTexParameterfv(Target, GL_TEXTURE_BORDER_COLOR, @fBorderColor[0]);
  7220. end;
  7221. end;
  7222. {$ENDIF}
  7223. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7224. procedure TglBitmap.SetFilter(const aMin, aMag: GLenum);
  7225. begin
  7226. //check MIN filter
  7227. case aMin of
  7228. GL_NEAREST:
  7229. fFilterMin := GL_NEAREST;
  7230. GL_LINEAR:
  7231. fFilterMin := GL_LINEAR;
  7232. GL_NEAREST_MIPMAP_NEAREST:
  7233. fFilterMin := GL_NEAREST_MIPMAP_NEAREST;
  7234. GL_LINEAR_MIPMAP_NEAREST:
  7235. fFilterMin := GL_LINEAR_MIPMAP_NEAREST;
  7236. GL_NEAREST_MIPMAP_LINEAR:
  7237. fFilterMin := GL_NEAREST_MIPMAP_LINEAR;
  7238. GL_LINEAR_MIPMAP_LINEAR:
  7239. fFilterMin := GL_LINEAR_MIPMAP_LINEAR;
  7240. else
  7241. raise EglBitmap.Create('SetFilter - Unknow MIN filter.');
  7242. end;
  7243. //check MAG filter
  7244. case aMag of
  7245. GL_NEAREST:
  7246. fFilterMag := GL_NEAREST;
  7247. GL_LINEAR:
  7248. fFilterMag := GL_LINEAR;
  7249. else
  7250. raise EglBitmap.Create('SetFilter - Unknow MAG filter.');
  7251. end;
  7252. //apply filter
  7253. if (ID > 0) then begin
  7254. Bind({$IFNDEF OPENGL_ES}false{$ENDIF});
  7255. glTexParameteri(Target, GL_TEXTURE_MAG_FILTER, fFilterMag);
  7256. if (MipMap = mmNone) {$IFNDEF OPENGL_ES}or (Target = GL_TEXTURE_RECTANGLE){$ENDIF} then begin
  7257. case fFilterMin of
  7258. GL_NEAREST, GL_LINEAR:
  7259. glTexParameteri(Target, GL_TEXTURE_MIN_FILTER, fFilterMin);
  7260. GL_NEAREST_MIPMAP_NEAREST, GL_NEAREST_MIPMAP_LINEAR:
  7261. glTexParameteri(Target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  7262. GL_LINEAR_MIPMAP_NEAREST, GL_LINEAR_MIPMAP_LINEAR:
  7263. glTexParameteri(Target, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  7264. end;
  7265. end else
  7266. glTexParameteri(Target, GL_TEXTURE_MIN_FILTER, fFilterMin);
  7267. end;
  7268. end;
  7269. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7270. procedure TglBitmap.SetWrap(const S: GLenum; const T: GLenum; const R: GLenum);
  7271. procedure CheckAndSetWrap(const aValue: Cardinal; var aTarget: Cardinal);
  7272. begin
  7273. case aValue of
  7274. {$IFNDEF OPENGL_ES}
  7275. GL_CLAMP:
  7276. aTarget := GL_CLAMP;
  7277. {$ENDIF}
  7278. GL_REPEAT:
  7279. aTarget := GL_REPEAT;
  7280. GL_CLAMP_TO_EDGE: begin
  7281. {$IFNDEF OPENGL_ES}
  7282. if not GL_VERSION_1_2 and not GL_EXT_texture_edge_clamp then
  7283. aTarget := GL_CLAMP
  7284. else
  7285. {$ENDIF}
  7286. aTarget := GL_CLAMP_TO_EDGE;
  7287. end;
  7288. {$IFNDEF OPENGL_ES}
  7289. GL_CLAMP_TO_BORDER: begin
  7290. if GL_VERSION_1_3 or GL_ARB_texture_border_clamp then
  7291. aTarget := GL_CLAMP_TO_BORDER
  7292. else
  7293. aTarget := GL_CLAMP;
  7294. end;
  7295. {$ENDIF}
  7296. {$IF NOT DEFINED(OPENGL_ES) OR DEFINED(OPENGL_ES_2_0)}
  7297. GL_MIRRORED_REPEAT: begin
  7298. {$IFNDEF OPENGL_ES}
  7299. if GL_VERSION_1_4 or GL_ARB_texture_mirrored_repeat or GL_IBM_texture_mirrored_repeat then
  7300. {$ELSE}
  7301. if GL_VERSION_2_0 then
  7302. {$ENDIF}
  7303. aTarget := GL_MIRRORED_REPEAT
  7304. else
  7305. raise EglBitmap.Create('SetWrap - Unsupported Texturewrap GL_MIRRORED_REPEAT (S).');
  7306. end;
  7307. {$IFEND}
  7308. else
  7309. raise EglBitmap.Create('SetWrap - Unknow Texturewrap');
  7310. end;
  7311. end;
  7312. begin
  7313. CheckAndSetWrap(S, fWrapS);
  7314. CheckAndSetWrap(T, fWrapT);
  7315. CheckAndSetWrap(R, fWrapR);
  7316. if (ID > 0) then begin
  7317. Bind({$IFNDEF OPENGL_ES}false{$ENDIF});
  7318. glTexParameteri(Target, GL_TEXTURE_WRAP_S, fWrapS);
  7319. glTexParameteri(Target, GL_TEXTURE_WRAP_T, fWrapT);
  7320. {$IF NOT DEFINED(OPENGL_ES) OR DEFINED(OPENGL_ES_3_0)}
  7321. {$IFDEF OPENGL_ES} if GL_VERSION_3_0 then{$ENDIF}
  7322. glTexParameteri(Target, GL_TEXTURE_WRAP_R, fWrapR);
  7323. {$IFEND}
  7324. end;
  7325. end;
  7326. {$IF NOT DEFINED(OPENGL_ES) OR DEFINED(OPENGL_ES_3_0)}
  7327. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7328. procedure TglBitmap.SetSwizzle(const r, g, b, a: GLenum);
  7329. procedure CheckAndSetValue(const aValue: GLenum; const aIndex: Integer);
  7330. begin
  7331. if (aValue = GL_ZERO) or (aValue = GL_ONE) or (aValue = GL_ALPHA) or
  7332. (aValue = GL_RED) or (aValue = GL_GREEN) or (aValue = GL_BLUE) then
  7333. fSwizzle[aIndex] := aValue
  7334. else
  7335. raise EglBitmap.Create('SetSwizzle - Unknow Swizle Value');
  7336. end;
  7337. begin
  7338. {$IFNDEF OPENGL_ES}
  7339. if not (GL_ARB_texture_swizzle or GL_EXT_texture_swizzle or GL_VERSION_3_3) then
  7340. raise EglBitmapNotSupported.Create('texture swizzle is not supported');
  7341. {$ELSE}
  7342. if not GL_VERSION_3_0 then
  7343. raise EglBitmapNotSupported.Create('texture swizzle is not supported');
  7344. {$ENDIF}
  7345. CheckAndSetValue(r, 0);
  7346. CheckAndSetValue(g, 1);
  7347. CheckAndSetValue(b, 2);
  7348. CheckAndSetValue(a, 3);
  7349. if (ID > 0) then begin
  7350. Bind(false);
  7351. {$IFNDEF OPENGL_ES}
  7352. glTexParameteriv(Target, GL_TEXTURE_SWIZZLE_RGBA, PGLint(@fSwizzle[0]));
  7353. {$ELSE}
  7354. glTexParameteriv(Target, GL_TEXTURE_SWIZZLE_R, PGLint(@fSwizzle[0]));
  7355. glTexParameteriv(Target, GL_TEXTURE_SWIZZLE_G, PGLint(@fSwizzle[1]));
  7356. glTexParameteriv(Target, GL_TEXTURE_SWIZZLE_B, PGLint(@fSwizzle[2]));
  7357. glTexParameteriv(Target, GL_TEXTURE_SWIZZLE_A, PGLint(@fSwizzle[3]));
  7358. {$ENDIF}
  7359. end;
  7360. end;
  7361. {$IFEND}
  7362. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7363. procedure TglBitmap.Bind({$IFNDEF OPENGL_ES}const aEnableTextureUnit: Boolean{$ENDIF});
  7364. begin
  7365. {$IFNDEF OPENGL_ES}
  7366. if aEnableTextureUnit then
  7367. glEnable(Target);
  7368. {$ENDIF}
  7369. if (ID > 0) then
  7370. glBindTexture(Target, ID);
  7371. end;
  7372. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7373. procedure TglBitmap.Unbind({$IFNDEF OPENGL_ES}const aDisableTextureUnit: Boolean{$ENDIF});
  7374. begin
  7375. {$IFNDEF OPENGL_ES}
  7376. if aDisableTextureUnit then
  7377. glDisable(Target);
  7378. {$ENDIF}
  7379. glBindTexture(Target, 0);
  7380. end;
  7381. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7382. procedure TglBitmap.UploadData(const aDataObj: TglBitmapData; const aCheckSize: Boolean);
  7383. var
  7384. w, h: Integer;
  7385. begin
  7386. w := aDataObj.Width;
  7387. h := aDataObj.Height;
  7388. fDimension.Fields := [];
  7389. if (w > 0) then
  7390. fDimension.Fields := fDimension.Fields + [ffX];
  7391. if (h > 0) then
  7392. fDimension.Fields := fDimension.Fields + [ffY];
  7393. fDimension.X := w;
  7394. fDimension.Y := h;
  7395. end;
  7396. {$IFNDEF OPENGL_ES}
  7397. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7398. function TglBitmap.DownloadData(const aDataObj: TglBitmapData): Boolean;
  7399. var
  7400. Temp: PByte;
  7401. TempWidth, TempHeight: Integer;
  7402. TempIntFormat: GLint;
  7403. IntFormat: TglBitmapFormat;
  7404. FormatDesc: TFormatDescriptor;
  7405. begin
  7406. result := false;
  7407. Bind;
  7408. // Request Data
  7409. glGetTexLevelParameteriv(Target, 0, GL_TEXTURE_WIDTH, @TempWidth);
  7410. glGetTexLevelParameteriv(Target, 0, GL_TEXTURE_HEIGHT, @TempHeight);
  7411. glGetTexLevelParameteriv(Target, 0, GL_TEXTURE_INTERNAL_FORMAT, @TempIntFormat);
  7412. FormatDesc := (TglBitmapFormatDescriptor.GetByFormat(TempIntFormat) as TFormatDescriptor);
  7413. IntFormat := FormatDesc.Format;
  7414. // Getting data from OpenGL
  7415. FormatDesc := TFormatDescriptor.Get(IntFormat);
  7416. GetMem(Temp, FormatDesc.GetSize(TempWidth, TempHeight));
  7417. try
  7418. glPixelStorei(GL_PACK_ALIGNMENT, 1);
  7419. if FormatDesc.IsCompressed then begin
  7420. if not Assigned(glGetCompressedTexImage) then
  7421. raise EglBitmap.Create('compressed formats not supported by video adapter');
  7422. glGetCompressedTexImage(Target, 0, Temp)
  7423. end else
  7424. glGetTexImage(Target, 0, FormatDesc.glFormat, FormatDesc.glDataFormat, Temp);
  7425. aDataObj.SetData(Temp, IntFormat, TempWidth, TempHeight);
  7426. result := true;
  7427. except
  7428. if Assigned(Temp) then
  7429. FreeMem(Temp);
  7430. raise;
  7431. end;
  7432. end;
  7433. {$ENDIF}
  7434. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7435. constructor TglBitmap.Create;
  7436. begin
  7437. if (ClassType = TglBitmap) then
  7438. raise EglBitmap.Create('Don''t create TglBitmap directly. Use one of the deviated classes (TglBitmap2D) instead.');
  7439. inherited Create;
  7440. end;
  7441. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7442. constructor TglBitmap.Create(const aData: TglBitmapData);
  7443. begin
  7444. Create;
  7445. UploadData(aData);
  7446. end;
  7447. {$IFNDEF OPENGL_ES}
  7448. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7449. //TglBitmap1D/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7450. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7451. procedure TglBitmap1D.UploadDataIntern(const aDataObj: TglBitmapData; const aBuildWithGlu: Boolean);
  7452. var
  7453. fd: TglBitmapFormatDescriptor;
  7454. begin
  7455. // Upload data
  7456. fd := aDataObj.FormatDescriptor;
  7457. if (fd.glFormat = 0) or (fd.glInternalFormat = 0) or (fd.glDataFormat = 0) then
  7458. raise EglBitmap.Create('format is not supported by video adapter, please convert before uploading data');
  7459. if fd.IsCompressed then begin
  7460. if not Assigned(glCompressedTexImage1D) then
  7461. raise EglBitmap.Create('compressed formats not supported by video adapter');
  7462. glCompressedTexImage1D(Target, 0, fd.glInternalFormat, aDataObj.Width, 0, fd.GetSize(aDataObj.Width, 1), aDataObj.Data)
  7463. end else if aBuildWithGlu then
  7464. gluBuild1DMipmaps(Target, fd.glInternalFormat, aDataObj.Width, fd.glFormat, fd.glDataFormat, aDataObj.Data)
  7465. else
  7466. glTexImage1D(Target, 0, fd.glInternalFormat, aDataObj.Width, 0, fd.glFormat, fd.glDataFormat, aDataObj.Data);
  7467. end;
  7468. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7469. procedure TglBitmap1D.AfterConstruction;
  7470. begin
  7471. inherited;
  7472. Target := GL_TEXTURE_1D;
  7473. end;
  7474. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7475. procedure TglBitmap1D.UploadData(const aDataObj: TglBitmapData; const aCheckSize: Boolean);
  7476. var
  7477. BuildWithGlu, TexRec: Boolean;
  7478. TexSize: Integer;
  7479. begin
  7480. if not Assigned(aDataObj) then
  7481. exit;
  7482. // Check Texture Size
  7483. if (aCheckSize) then begin
  7484. glGetIntegerv(GL_MAX_TEXTURE_SIZE, @TexSize);
  7485. if (aDataObj.Width > TexSize) then
  7486. raise EglBitmapSizeToLarge.Create('TglBitmap1D.GenTexture - The size for the texture is to large. It''s may be not conform with the Hardware.');
  7487. TexRec := (GL_ARB_texture_rectangle or GL_EXT_texture_rectangle or GL_NV_texture_rectangle) and
  7488. (Target = GL_TEXTURE_RECTANGLE);
  7489. if not (IsPowerOfTwo(aDataObj.Width) or GL_ARB_texture_non_power_of_two or GL_VERSION_2_0 or TexRec) then
  7490. raise EglBitmapNonPowerOfTwo.Create('TglBitmap1D.GenTexture - Rendercontex dosn''t support non power of two texture.');
  7491. end;
  7492. if (fID = 0) then
  7493. CreateID;
  7494. SetupParameters(BuildWithGlu);
  7495. UploadDataIntern(aDataObj, BuildWithGlu);
  7496. glAreTexturesResident(1, @fID, @fIsResident);
  7497. inherited UploadData(aDataObj, aCheckSize);
  7498. end;
  7499. {$ENDIF}
  7500. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7501. //TglBitmap2D/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7502. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7503. procedure TglBitmap2D.UploadDataIntern(const aDataObj: TglBitmapData; const aTarget: GLenum{$IFNDEF OPENGL_ES}; const aBuildWithGlu: Boolean{$ENDIF});
  7504. var
  7505. fd: TglBitmapFormatDescriptor;
  7506. begin
  7507. fd := aDataObj.FormatDescriptor;
  7508. if (fd.glFormat = 0) or (fd.glInternalFormat = 0) or (fd.glDataFormat = 0) then
  7509. raise EglBitmap.Create('format is not supported by video adapter, please convert before uploading data');
  7510. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  7511. if fd.IsCompressed then begin
  7512. if not Assigned(glCompressedTexImage2D) then
  7513. raise EglBitmap.Create('compressed formats not supported by video adapter');
  7514. glCompressedTexImage2D(aTarget, 0, fd.glInternalFormat, aDataObj.Width, aDataObj.Height, 0, fd.GetSize(fDimension), aDataObj.Data)
  7515. {$IFNDEF OPENGL_ES}
  7516. end else if aBuildWithGlu then begin
  7517. gluBuild2DMipmaps(aTarget, fd.ChannelCount, aDataObj.Width, aDataObj.Height, fd.glFormat, fd.glDataFormat, aDataObj.Data)
  7518. {$ENDIF}
  7519. end else begin
  7520. glTexImage2D(aTarget, 0, fd.glInternalFormat, aDataObj.Width, aDataObj.Height, 0, fd.glFormat, fd.glDataFormat, aDataObj.Data);
  7521. end;
  7522. end;
  7523. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7524. procedure TglBitmap2D.AfterConstruction;
  7525. begin
  7526. inherited;
  7527. Target := GL_TEXTURE_2D;
  7528. end;
  7529. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7530. procedure TglBitmap2D.UploadData(const aDataObj: TglBitmapData; const aCheckSize: Boolean);
  7531. var
  7532. {$IFNDEF OPENGL_ES}
  7533. BuildWithGlu, TexRec: Boolean;
  7534. {$ENDIF}
  7535. PotTex: Boolean;
  7536. TexSize: Integer;
  7537. begin
  7538. if not Assigned(aDataObj) then
  7539. exit;
  7540. // Check Texture Size
  7541. if (aCheckSize) then begin
  7542. glGetIntegerv(GL_MAX_TEXTURE_SIZE, @TexSize);
  7543. if ((aDataObj.Width > TexSize) or (aDataObj.Height > TexSize)) then
  7544. raise EglBitmapSizeToLarge.Create('TglBitmap2D.GenTexture - The size for the texture is to large. It''s may be not conform with the Hardware.');
  7545. PotTex := IsPowerOfTwo(aDataObj.Width) and IsPowerOfTwo(aDataObj.Height);
  7546. {$IF NOT DEFINED(OPENGL_ES)}
  7547. TexRec := (GL_ARB_texture_rectangle or GL_EXT_texture_rectangle or GL_NV_texture_rectangle) and (Target = GL_TEXTURE_RECTANGLE);
  7548. if not (PotTex or GL_ARB_texture_non_power_of_two or GL_VERSION_2_0 or TexRec) then
  7549. raise EglBitmapNonPowerOfTwo.Create('TglBitmap2D.GenTexture - Rendercontex dosn''t support non power of two texture.');
  7550. {$ELSEIF DEFINED(OPENGL_ES_EXT)}
  7551. if not PotTex and not GL_OES_texture_npot then
  7552. raise EglBitmapNonPowerOfTwo.Create('TglBitmap2D.GenTexture - Rendercontex dosn''t support non power of two texture.');
  7553. {$ELSE}
  7554. if not PotTex then
  7555. raise EglBitmapNonPowerOfTwo.Create('TglBitmap2D.GenTexture - Rendercontex dosn''t support non power of two texture.');
  7556. {$IFEND}
  7557. end;
  7558. if (fID = 0) then
  7559. CreateID;
  7560. SetupParameters({$IFNDEF OPENGL_ES}BuildWithGlu{$ENDIF});
  7561. UploadDataIntern(aDataObj, Target{$IFNDEF OPENGL_ES}, BuildWithGlu{$ENDIF});
  7562. {$IFNDEF OPENGL_ES}
  7563. glAreTexturesResident(1, @fID, @fIsResident);
  7564. {$ENDIF}
  7565. inherited UploadData(aDataObj, aCheckSize);
  7566. end;
  7567. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7568. class procedure TglBitmap2D.GrabScreen(const aTop, aLeft, aRight, aBottom: Integer; const aFormat: TglBitmapFormat; const aDataObj: TglBitmapData);
  7569. var
  7570. Temp: pByte;
  7571. Size, w, h: Integer;
  7572. FormatDesc: TFormatDescriptor;
  7573. begin
  7574. FormatDesc := TFormatDescriptor.Get(aFormat);
  7575. if FormatDesc.IsCompressed then
  7576. raise EglBitmapUnsupportedFormat.Create(aFormat);
  7577. w := aRight - aLeft;
  7578. h := aBottom - aTop;
  7579. Size := FormatDesc.GetSize(w, h);
  7580. GetMem(Temp, Size);
  7581. try
  7582. glPixelStorei(GL_PACK_ALIGNMENT, 1);
  7583. glReadPixels(aLeft, aTop, w, h, FormatDesc.glFormat, FormatDesc.glDataFormat, Temp);
  7584. aDataObj.SetData(Temp, aFormat, w, h);
  7585. aDataObj.FlipVert;
  7586. except
  7587. if Assigned(Temp) then
  7588. FreeMem(Temp);
  7589. raise;
  7590. end;
  7591. end;
  7592. {$IF NOT DEFINED(OPENGL_ES) OR DEFINED(OPENGL_ES_2_0)}
  7593. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7594. //TglBitmapCubeMap////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7595. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7596. procedure TglBitmapCubeMap.AfterConstruction;
  7597. begin
  7598. inherited;
  7599. {$IFNDEF OPENGL_ES}
  7600. if not (GL_VERSION_1_3 or GL_ARB_texture_cube_map or GL_EXT_texture_cube_map) then
  7601. raise EglBitmap.Create('TglBitmapCubeMap.AfterConstruction - CubeMaps are unsupported.');
  7602. {$ELSE}
  7603. if not (GL_VERSION_2_0) then
  7604. raise EglBitmap.Create('TglBitmapCubeMap.AfterConstruction - CubeMaps are unsupported.');
  7605. {$ENDIF}
  7606. SetWrap;
  7607. Target := GL_TEXTURE_CUBE_MAP;
  7608. {$IFNDEF OPENGL_ES}
  7609. fGenMode := GL_REFLECTION_MAP;
  7610. {$ENDIF}
  7611. end;
  7612. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7613. procedure TglBitmapCubeMap.UploadData(const aDataObj: TglBitmapData; const aCheckSize: Boolean);
  7614. begin
  7615. Assert(false, 'TglBitmapCubeMap.UploadData - Don''t call UploadData directly, use UploadCubeMap instead');
  7616. end;
  7617. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7618. procedure TglBitmapCubeMap.UploadCubeMap(const aDataObj: TglBitmapData; const aCubeTarget: Cardinal; const aCheckSize: Boolean);
  7619. var
  7620. {$IFNDEF OPENGL_ES}
  7621. BuildWithGlu: Boolean;
  7622. {$ENDIF}
  7623. TexSize: Integer;
  7624. begin
  7625. if (aCheckSize) then begin
  7626. glGetIntegerv(GL_MAX_CUBE_MAP_TEXTURE_SIZE, @TexSize);
  7627. if (aDataObj.Width > TexSize) or (aDataObj.Height > TexSize) then
  7628. raise EglBitmapSizeToLarge.Create('TglBitmapCubeMap.GenerateCubeMap - The size for the Cubemap is to large. It''s may be not conform with the Hardware.');
  7629. {$IF NOT DEFINED(OPENGL_ES)}
  7630. if not ((IsPowerOfTwo(aDataObj.Width) and IsPowerOfTwo(aDataObj.Height)) or GL_VERSION_2_0 or GL_ARB_texture_non_power_of_two) then
  7631. raise EglBitmapNonPowerOfTwo.Create('TglBitmapCubeMap.GenerateCubeMap - Cubemaps dosn''t support non power of two texture.');
  7632. {$ELSEIF DEFINED(OPENGL_ES_EXT)}
  7633. if not (IsPowerOfTwo(aDataObj.Width) and IsPowerOfTwo(aDataObj.Height)) and not GL_OES_texture_npot then
  7634. raise EglBitmapNonPowerOfTwo.Create('TglBitmapCubeMap.GenerateCubeMap - Cubemaps dosn''t support non power of two texture.');
  7635. {$ELSE}
  7636. if not (IsPowerOfTwo(aDataObj.Width) and IsPowerOfTwo(aDataObj.Height)) then
  7637. raise EglBitmapNonPowerOfTwo.Create('TglBitmapCubeMap.GenerateCubeMap - Cubemaps dosn''t support non power of two texture.');
  7638. {$IFEND}
  7639. end;
  7640. if (fID = 0) then
  7641. CreateID;
  7642. SetupParameters({$IFNDEF OPENGL_ES}BuildWithGlu{$ENDIF});
  7643. UploadDataIntern(aDataObj, aCubeTarget{$IFNDEF OPENGL_ES}, BuildWithGlu{$ENDIF});
  7644. inherited UploadData(aDataObj, aCheckSize);
  7645. end;
  7646. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7647. procedure TglBitmapCubeMap.Bind({$IFNDEF OPENGL_ES}const aEnableTexCoordsGen: Boolean; const aEnableTextureUnit: Boolean{$ENDIF});
  7648. begin
  7649. inherited Bind({$IFNDEF OPENGL_ES}aEnableTextureUnit{$ENDIF});
  7650. {$IFNDEF OPENGL_ES}
  7651. if aEnableTexCoordsGen then begin
  7652. glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, fGenMode);
  7653. glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, fGenMode);
  7654. glTexGeni(GL_R, GL_TEXTURE_GEN_MODE, fGenMode);
  7655. glEnable(GL_TEXTURE_GEN_S);
  7656. glEnable(GL_TEXTURE_GEN_T);
  7657. glEnable(GL_TEXTURE_GEN_R);
  7658. end;
  7659. {$ENDIF}
  7660. end;
  7661. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7662. procedure TglBitmapCubeMap.Unbind({$IFNDEF OPENGL_ES}const aDisableTexCoordsGen: Boolean; const aDisableTextureUnit: Boolean{$ENDIF});
  7663. begin
  7664. inherited Unbind({$IFNDEF OPENGL_ES}aDisableTextureUnit{$ENDIF});
  7665. {$IFNDEF OPENGL_ES}
  7666. if aDisableTexCoordsGen then begin
  7667. glDisable(GL_TEXTURE_GEN_S);
  7668. glDisable(GL_TEXTURE_GEN_T);
  7669. glDisable(GL_TEXTURE_GEN_R);
  7670. end;
  7671. {$ENDIF}
  7672. end;
  7673. {$IFEND}
  7674. {$IF NOT DEFINED(OPENGL_ES) OR DEFINED(OPENGL_ES_2_0)}
  7675. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7676. //TglBitmapNormalMap//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7677. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7678. type
  7679. TVec = Array[0..2] of Single;
  7680. TglBitmapNormalMapGetVectorFunc = procedure (out aVec: TVec; const aPosition: TglBitmapPixelPosition; const aHalfSize: Integer);
  7681. PglBitmapNormalMapRec = ^TglBitmapNormalMapRec;
  7682. TglBitmapNormalMapRec = record
  7683. HalfSize : Integer;
  7684. Func: TglBitmapNormalMapGetVectorFunc;
  7685. end;
  7686. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7687. procedure glBitmapNormalMapPosX(out aVec: TVec; const aPosition: TglBitmapPixelPosition; const aHalfSize: Integer);
  7688. begin
  7689. aVec[0] := aHalfSize;
  7690. aVec[1] := - (aPosition.Y + 0.5 - aHalfSize);
  7691. aVec[2] := - (aPosition.X + 0.5 - aHalfSize);
  7692. end;
  7693. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7694. procedure glBitmapNormalMapNegX(out aVec: TVec; const aPosition: TglBitmapPixelPosition; const aHalfSize: Integer);
  7695. begin
  7696. aVec[0] := - aHalfSize;
  7697. aVec[1] := - (aPosition.Y + 0.5 - aHalfSize);
  7698. aVec[2] := aPosition.X + 0.5 - aHalfSize;
  7699. end;
  7700. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7701. procedure glBitmapNormalMapPosY(out aVec: TVec; const aPosition: TglBitmapPixelPosition; const aHalfSize: Integer);
  7702. begin
  7703. aVec[0] := aPosition.X + 0.5 - aHalfSize;
  7704. aVec[1] := aHalfSize;
  7705. aVec[2] := aPosition.Y + 0.5 - aHalfSize;
  7706. end;
  7707. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7708. procedure glBitmapNormalMapNegY(out aVec: TVec; const aPosition: TglBitmapPixelPosition; const aHalfSize: Integer);
  7709. begin
  7710. aVec[0] := aPosition.X + 0.5 - aHalfSize;
  7711. aVec[1] := - aHalfSize;
  7712. aVec[2] := - (aPosition.Y + 0.5 - aHalfSize);
  7713. end;
  7714. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7715. procedure glBitmapNormalMapPosZ(out aVec: TVec; const aPosition: TglBitmapPixelPosition; const aHalfSize: Integer);
  7716. begin
  7717. aVec[0] := aPosition.X + 0.5 - aHalfSize;
  7718. aVec[1] := - (aPosition.Y + 0.5 - aHalfSize);
  7719. aVec[2] := aHalfSize;
  7720. end;
  7721. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7722. procedure glBitmapNormalMapNegZ(out aVec: TVec; const aPosition: TglBitmapPixelPosition; const aHalfSize: Integer);
  7723. begin
  7724. aVec[0] := - (aPosition.X + 0.5 - aHalfSize);
  7725. aVec[1] := - (aPosition.Y + 0.5 - aHalfSize);
  7726. aVec[2] := - aHalfSize;
  7727. end;
  7728. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7729. procedure glBitmapNormalMapFunc(var FuncRec: TglBitmapFunctionRec);
  7730. var
  7731. i: Integer;
  7732. Vec: TVec;
  7733. Len: Single;
  7734. begin
  7735. with FuncRec do begin
  7736. with PglBitmapNormalMapRec(Args)^ do begin
  7737. Func(Vec, Position, HalfSize);
  7738. // Normalize
  7739. Len := 1 / Sqrt(Sqr(Vec[0]) + Sqr(Vec[1]) + Sqr(Vec[2]));
  7740. if Len <> 0 then begin
  7741. Vec[0] := Vec[0] * Len;
  7742. Vec[1] := Vec[1] * Len;
  7743. Vec[2] := Vec[2] * Len;
  7744. end;
  7745. // Scale Vector and AddVectro
  7746. Vec[0] := Vec[0] * 0.5 + 0.5;
  7747. Vec[1] := Vec[1] * 0.5 + 0.5;
  7748. Vec[2] := Vec[2] * 0.5 + 0.5;
  7749. end;
  7750. // Set Color
  7751. for i := 0 to 2 do
  7752. Dest.Data.arr[i] := Round(Vec[i] * 255);
  7753. end;
  7754. end;
  7755. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7756. procedure TglBitmapNormalMap.AfterConstruction;
  7757. begin
  7758. inherited;
  7759. {$IFNDEF OPENGL_ES}
  7760. fGenMode := GL_NORMAL_MAP;
  7761. {$ENDIF}
  7762. end;
  7763. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7764. procedure TglBitmapNormalMap.GenerateNormalMap(const aSize: Integer; const aCheckSize: Boolean);
  7765. var
  7766. Rec: TglBitmapNormalMapRec;
  7767. SizeRec: TglBitmapSize;
  7768. DataObj: TglBitmapData;
  7769. begin
  7770. Rec.HalfSize := aSize div 2;
  7771. SizeRec.Fields := [ffX, ffY];
  7772. SizeRec.X := aSize;
  7773. SizeRec.Y := aSize;
  7774. DataObj := TglBitmapData.Create;
  7775. try
  7776. // Positive X
  7777. Rec.Func := glBitmapNormalMapPosX;
  7778. DataObj.LoadFromFunc(SizeRec, tfBGR8ub3, glBitmapNormalMapFunc, @Rec);
  7779. UploadCubeMap(DataObj, GL_TEXTURE_CUBE_MAP_POSITIVE_X, aCheckSize);
  7780. // Negative X
  7781. Rec.Func := glBitmapNormalMapNegX;
  7782. DataObj.LoadFromFunc(SizeRec, tfBGR8ub3, glBitmapNormalMapFunc, @Rec);
  7783. UploadCubeMap(DataObj, GL_TEXTURE_CUBE_MAP_NEGATIVE_X, aCheckSize);
  7784. // Positive Y
  7785. Rec.Func := glBitmapNormalMapPosY;
  7786. DataObj.LoadFromFunc(SizeRec, tfBGR8ub3, glBitmapNormalMapFunc, @Rec);
  7787. UploadCubeMap(DataObj, GL_TEXTURE_CUBE_MAP_POSITIVE_Y, aCheckSize);
  7788. // Negative Y
  7789. Rec.Func := glBitmapNormalMapNegY;
  7790. DataObj.LoadFromFunc(SizeRec, tfBGR8ub3, glBitmapNormalMapFunc, @Rec);
  7791. UploadCubeMap(DataObj, GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, aCheckSize);
  7792. // Positive Z
  7793. Rec.Func := glBitmapNormalMapPosZ;
  7794. DataObj.LoadFromFunc(SizeRec, tfBGR8ub3, glBitmapNormalMapFunc, @Rec);
  7795. UploadCubeMap(DataObj, GL_TEXTURE_CUBE_MAP_POSITIVE_Z, aCheckSize);
  7796. // Negative Z
  7797. Rec.Func := glBitmapNormalMapNegZ;
  7798. DataObj.LoadFromFunc(SizeRec, tfBGR8ub3, glBitmapNormalMapFunc, @Rec);
  7799. UploadCubeMap(DataObj, GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, aCheckSize);
  7800. finally
  7801. FreeAndNil(DataObj);
  7802. end;
  7803. end;
  7804. {$IFEND}
  7805. initialization
  7806. glBitmapSetDefaultFormat (tfEmpty);
  7807. glBitmapSetDefaultMipmap (mmMipmap);
  7808. glBitmapSetDefaultFilter (GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR);
  7809. glBitmapSetDefaultWrap (GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
  7810. {$IF NOT DEFINED(OPENGL_ES) OR DEFINED(OPENGL_ES_3_0)}
  7811. glBitmapSetDefaultSwizzle(GL_RED, GL_GREEN, GL_BLUE, GL_ALPHA);
  7812. {$IFEND}
  7813. glBitmapSetDefaultFreeDataAfterGenTexture(true);
  7814. glBitmapSetDefaultDeleteTextureOnFree (true);
  7815. TFormatDescriptor.Init;
  7816. finalization
  7817. TFormatDescriptor.Finalize;
  7818. end.