Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

8916 righe
318 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 uglcBitmap;
  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. TglcBitmapFormat = TglBitmapFormat;
  984. TglcBitmap2D = TglBitmap2D;
  985. TglcBitmapData = TglBitmapData;
  986. {$IF NOT DEFINED(OPENGL_ES)}
  987. TglcBitmap1D = TglBitmap1D;
  988. TglcBitmapCubeMap = TglBitmapCubeMap;
  989. TglcBitmapNormalMap = TglBitmapNormalMap;
  990. {$ELSEIF DEFINED(OPENGL_ES_2_0)}
  991. TglcBitmapCubeMap = TglBitmapCubeMap;
  992. TglcBitmapNormalMap = TglBitmapNormalMap;
  993. {$IFEND}
  994. const
  995. NULL_SIZE: TglBitmapSize = (Fields: []; X: 0; Y: 0);
  996. procedure glBitmapSetDefaultDeleteTextureOnFree(const aDeleteTextureOnFree: Boolean);
  997. procedure glBitmapSetDefaultFreeDataAfterGenTexture(const aFreeData: Boolean);
  998. procedure glBitmapSetDefaultMipmap(const aValue: TglBitmapMipMap);
  999. procedure glBitmapSetDefaultFormat(const aFormat: TglBitmapFormat);
  1000. procedure glBitmapSetDefaultFilter(const aMin, aMag: Integer);
  1001. procedure glBitmapSetDefaultWrap(
  1002. const S: Cardinal = GL_CLAMP_TO_EDGE;
  1003. const T: Cardinal = GL_CLAMP_TO_EDGE;
  1004. const R: Cardinal = GL_CLAMP_TO_EDGE);
  1005. {$IF NOT DEFINED(OPENGL_ES) OR DEFINED(OPENGL_ES_3_0)}
  1006. procedure glBitmapSetDefaultSwizzle(const r: GLenum = GL_RED; g: GLenum = GL_GREEN; b: GLenum = GL_BLUE; a: GLenum = GL_ALPHA);
  1007. {$IFEND}
  1008. function glBitmapGetDefaultDeleteTextureOnFree: Boolean;
  1009. function glBitmapGetDefaultFreeDataAfterGenTexture: Boolean;
  1010. function glBitmapGetDefaultMipmap: TglBitmapMipMap;
  1011. function glBitmapGetDefaultFormat: TglBitmapFormat;
  1012. procedure glBitmapGetDefaultFilter(var aMin, aMag: Cardinal);
  1013. procedure glBitmapGetDefaultTextureWrap(var S, T, R: Cardinal);
  1014. {$IF NOT DEFINED(OPENGL_ES) OR DEFINED(OPENGL_ES_3_0)}
  1015. procedure glBitmapGetDefaultSwizzle(var r, g, b, a: GLenum);
  1016. {$IFEND}
  1017. function glBitmapSize(X: Integer = -1; Y: Integer = -1): TglBitmapSize;
  1018. function glBitmapPosition(X: Integer = -1; Y: Integer = -1): TglBitmapPixelPosition;
  1019. function glBitmapRec4ub(const r, g, b, a: Byte): TglBitmapRec4ub;
  1020. function glBitmapRec4ui(const r, g, b, a: Cardinal): TglBitmapRec4ui;
  1021. function glBitmapRec4ul(const r, g, b, a: QWord): TglBitmapRec4ul;
  1022. function glBitmapRec4ubCompare(const r1, r2: TglBitmapRec4ub): Boolean;
  1023. function glBitmapRec4uiCompare(const r1, r2: TglBitmapRec4ui): Boolean;
  1024. function glBitmapCreateTestData(const aFormat: TglBitmapFormat): TglBitmapData;
  1025. {$IFDEF GLB_DELPHI}
  1026. function CreateGrayPalette: HPALETTE;
  1027. {$ENDIF}
  1028. implementation
  1029. uses
  1030. Math, syncobjs, typinfo
  1031. {$IF DEFINED(GLB_SUPPORT_JPEG_READ) AND DEFINED(GLB_LAZ_JPEG)}, FPReadJPEG{$IFEND};
  1032. var
  1033. glBitmapDefaultDeleteTextureOnFree: Boolean;
  1034. glBitmapDefaultFreeDataAfterGenTextures: Boolean;
  1035. glBitmapDefaultFormat: TglBitmapFormat;
  1036. glBitmapDefaultMipmap: TglBitmapMipMap;
  1037. glBitmapDefaultFilterMin: Cardinal;
  1038. glBitmapDefaultFilterMag: Cardinal;
  1039. glBitmapDefaultWrapS: Cardinal;
  1040. glBitmapDefaultWrapT: Cardinal;
  1041. glBitmapDefaultWrapR: Cardinal;
  1042. glDefaultSwizzle: array[0..3] of GLenum;
  1043. ////////////////////////////////////////////////////////////////////////////////////////////////////
  1044. type
  1045. TFormatDescriptor = class(TglBitmapFormatDescriptor)
  1046. public
  1047. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); virtual; abstract;
  1048. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); virtual; abstract;
  1049. function CreateMappingData: Pointer; virtual;
  1050. procedure FreeMappingData(var aMappingData: Pointer); virtual;
  1051. function IsEmpty: Boolean; virtual;
  1052. function MaskMatch(const aMask: TglBitmapRec4ul): Boolean; virtual;
  1053. procedure PreparePixel(out aPixel: TglBitmapPixelData); virtual;
  1054. constructor Create; virtual;
  1055. public
  1056. class procedure Init;
  1057. class function Get(const aFormat: TglBitmapFormat): TFormatDescriptor;
  1058. class function GetAlpha(const aFormat: TglBitmapFormat): TFormatDescriptor;
  1059. class function GetFromMask(const aMask: TglBitmapRec4ul; const aBitCount: Integer = 0): TFormatDescriptor;
  1060. class function GetFromPrecShift(const aPrec, aShift: TglBitmapRec4ub; const aBitCount: Integer): TFormatDescriptor;
  1061. class procedure Clear;
  1062. class procedure Finalize;
  1063. end;
  1064. TFormatDescriptorClass = class of TFormatDescriptor;
  1065. TfdEmpty = class(TFormatDescriptor);
  1066. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1067. TfdAlphaUB1 = class(TFormatDescriptor) //1* unsigned byte
  1068. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1069. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1070. end;
  1071. TfdLuminanceUB1 = class(TFormatDescriptor) //1* unsigned byte
  1072. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1073. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1074. end;
  1075. TfdUniversalUB1 = class(TFormatDescriptor) //1* unsigned byte
  1076. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1077. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1078. end;
  1079. TfdLuminanceAlphaUB2 = class(TfdLuminanceUB1) //2* unsigned byte
  1080. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1081. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1082. end;
  1083. TfdRGBub3 = class(TFormatDescriptor) //3* unsigned byte
  1084. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1085. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1086. end;
  1087. TfdBGRub3 = class(TFormatDescriptor) //3* unsigned byte (inverse)
  1088. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1089. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1090. end;
  1091. TfdRGBAub4 = class(TfdRGBub3) //3* unsigned byte
  1092. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1093. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1094. end;
  1095. TfdBGRAub4 = class(TfdBGRub3) //3* unsigned byte (inverse)
  1096. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1097. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1098. end;
  1099. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1100. TfdAlphaUS1 = class(TFormatDescriptor) //1* unsigned short
  1101. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1102. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1103. end;
  1104. TfdLuminanceUS1 = class(TFormatDescriptor) //1* unsigned short
  1105. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1106. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1107. end;
  1108. TfdUniversalUS1 = class(TFormatDescriptor) //1* unsigned short
  1109. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1110. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1111. end;
  1112. TfdDepthUS1 = class(TFormatDescriptor) //1* unsigned short
  1113. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1114. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1115. end;
  1116. TfdLuminanceAlphaUS2 = class(TfdLuminanceUS1) //2* unsigned short
  1117. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1118. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1119. end;
  1120. TfdRGBus3 = class(TFormatDescriptor) //3* unsigned short
  1121. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1122. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1123. end;
  1124. TfdBGRus3 = class(TFormatDescriptor) //3* unsigned short (inverse)
  1125. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1126. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1127. end;
  1128. TfdRGBAus4 = class(TfdRGBus3) //4* unsigned short
  1129. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1130. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1131. end;
  1132. TfdARGBus4 = class(TfdRGBus3) //4* unsigned short
  1133. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1134. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1135. end;
  1136. TfdBGRAus4 = class(TfdBGRus3) //4* unsigned short (inverse)
  1137. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1138. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1139. end;
  1140. TfdABGRus4 = class(TfdBGRus3) //4* unsigned short (inverse)
  1141. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1142. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1143. end;
  1144. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1145. TfdUniversalUI1 = class(TFormatDescriptor) //1* unsigned int
  1146. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1147. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1148. end;
  1149. TfdDepthUI1 = class(TFormatDescriptor) //1* unsigned int
  1150. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1151. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1152. end;
  1153. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1154. TfdAlpha4ub1 = class(TfdAlphaUB1)
  1155. procedure SetValues; override;
  1156. end;
  1157. TfdAlpha8ub1 = class(TfdAlphaUB1)
  1158. procedure SetValues; override;
  1159. end;
  1160. TfdAlpha16us1 = class(TfdAlphaUS1)
  1161. procedure SetValues; override;
  1162. end;
  1163. TfdLuminance4ub1 = class(TfdLuminanceUB1)
  1164. procedure SetValues; override;
  1165. end;
  1166. TfdLuminance8ub1 = class(TfdLuminanceUB1)
  1167. procedure SetValues; override;
  1168. end;
  1169. TfdLuminance16us1 = class(TfdLuminanceUS1)
  1170. procedure SetValues; override;
  1171. end;
  1172. TfdLuminance4Alpha4ub2 = class(TfdLuminanceAlphaUB2)
  1173. procedure SetValues; override;
  1174. end;
  1175. TfdLuminance6Alpha2ub2 = class(TfdLuminanceAlphaUB2)
  1176. procedure SetValues; override;
  1177. end;
  1178. TfdLuminance8Alpha8ub2 = class(TfdLuminanceAlphaUB2)
  1179. procedure SetValues; override;
  1180. end;
  1181. TfdLuminance12Alpha4us2 = class(TfdLuminanceAlphaUS2)
  1182. procedure SetValues; override;
  1183. end;
  1184. TfdLuminance16Alpha16us2 = class(TfdLuminanceAlphaUS2)
  1185. procedure SetValues; override;
  1186. end;
  1187. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1188. TfdR3G3B2ub1 = class(TfdUniversalUB1)
  1189. procedure SetValues; override;
  1190. end;
  1191. TfdRGBX4us1 = class(TfdUniversalUS1)
  1192. procedure SetValues; override;
  1193. end;
  1194. TfdXRGB4us1 = class(TfdUniversalUS1)
  1195. procedure SetValues; override;
  1196. end;
  1197. TfdR5G6B5us1 = class(TfdUniversalUS1)
  1198. procedure SetValues; override;
  1199. end;
  1200. TfdRGB5X1us1 = class(TfdUniversalUS1)
  1201. procedure SetValues; override;
  1202. end;
  1203. TfdX1RGB5us1 = class(TfdUniversalUS1)
  1204. procedure SetValues; override;
  1205. end;
  1206. TfdRGB8ub3 = class(TfdRGBub3)
  1207. procedure SetValues; override;
  1208. end;
  1209. TfdRGBX8ui1 = class(TfdUniversalUI1)
  1210. procedure SetValues; override;
  1211. end;
  1212. TfdXRGB8ui1 = class(TfdUniversalUI1)
  1213. procedure SetValues; override;
  1214. end;
  1215. TfdRGB10X2ui1 = class(TfdUniversalUI1)
  1216. procedure SetValues; override;
  1217. end;
  1218. TfdX2RGB10ui1 = class(TfdUniversalUI1)
  1219. procedure SetValues; override;
  1220. end;
  1221. TfdRGB16us3 = class(TfdRGBus3)
  1222. procedure SetValues; override;
  1223. end;
  1224. TfdRGBA4us1 = class(TfdUniversalUS1)
  1225. procedure SetValues; override;
  1226. end;
  1227. TfdARGB4us1 = class(TfdUniversalUS1)
  1228. procedure SetValues; override;
  1229. end;
  1230. TfdRGB5A1us1 = class(TfdUniversalUS1)
  1231. procedure SetValues; override;
  1232. end;
  1233. TfdA1RGB5us1 = class(TfdUniversalUS1)
  1234. procedure SetValues; override;
  1235. end;
  1236. TfdRGBA8ui1 = class(TfdUniversalUI1)
  1237. procedure SetValues; override;
  1238. end;
  1239. TfdARGB8ui1 = class(TfdUniversalUI1)
  1240. procedure SetValues; override;
  1241. end;
  1242. TfdRGBA8ub4 = class(TfdRGBAub4)
  1243. procedure SetValues; override;
  1244. end;
  1245. TfdRGB10A2ui1 = class(TfdUniversalUI1)
  1246. procedure SetValues; override;
  1247. end;
  1248. TfdA2RGB10ui1 = class(TfdUniversalUI1)
  1249. procedure SetValues; override;
  1250. end;
  1251. TfdRGBA16us4 = class(TfdRGBAus4)
  1252. procedure SetValues; override;
  1253. end;
  1254. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1255. TfdBGRX4us1 = class(TfdUniversalUS1)
  1256. procedure SetValues; override;
  1257. end;
  1258. TfdXBGR4us1 = class(TfdUniversalUS1)
  1259. procedure SetValues; override;
  1260. end;
  1261. TfdB5G6R5us1 = class(TfdUniversalUS1)
  1262. procedure SetValues; override;
  1263. end;
  1264. TfdBGR5X1us1 = class(TfdUniversalUS1)
  1265. procedure SetValues; override;
  1266. end;
  1267. TfdX1BGR5us1 = class(TfdUniversalUS1)
  1268. procedure SetValues; override;
  1269. end;
  1270. TfdBGR8ub3 = class(TfdBGRub3)
  1271. procedure SetValues; override;
  1272. end;
  1273. TfdBGRX8ui1 = class(TfdUniversalUI1)
  1274. procedure SetValues; override;
  1275. end;
  1276. TfdXBGR8ui1 = class(TfdUniversalUI1)
  1277. procedure SetValues; override;
  1278. end;
  1279. TfdBGR10X2ui1 = class(TfdUniversalUI1)
  1280. procedure SetValues; override;
  1281. end;
  1282. TfdX2BGR10ui1 = class(TfdUniversalUI1)
  1283. procedure SetValues; override;
  1284. end;
  1285. TfdBGR16us3 = class(TfdBGRus3)
  1286. procedure SetValues; override;
  1287. end;
  1288. TfdBGRA4us1 = class(TfdUniversalUS1)
  1289. procedure SetValues; override;
  1290. end;
  1291. TfdABGR4us1 = class(TfdUniversalUS1)
  1292. procedure SetValues; override;
  1293. end;
  1294. TfdBGR5A1us1 = class(TfdUniversalUS1)
  1295. procedure SetValues; override;
  1296. end;
  1297. TfdA1BGR5us1 = class(TfdUniversalUS1)
  1298. procedure SetValues; override;
  1299. end;
  1300. TfdBGRA8ui1 = class(TfdUniversalUI1)
  1301. procedure SetValues; override;
  1302. end;
  1303. TfdABGR8ui1 = class(TfdUniversalUI1)
  1304. procedure SetValues; override;
  1305. end;
  1306. TfdBGRA8ub4 = class(TfdBGRAub4)
  1307. procedure SetValues; override;
  1308. end;
  1309. TfdBGR10A2ui1 = class(TfdUniversalUI1)
  1310. procedure SetValues; override;
  1311. end;
  1312. TfdA2BGR10ui1 = class(TfdUniversalUI1)
  1313. procedure SetValues; override;
  1314. end;
  1315. TfdBGRA16us4 = class(TfdBGRAus4)
  1316. procedure SetValues; override;
  1317. end;
  1318. TfdDepth16us1 = class(TfdDepthUS1)
  1319. procedure SetValues; override;
  1320. end;
  1321. TfdDepth24ui1 = class(TfdDepthUI1)
  1322. procedure SetValues; override;
  1323. end;
  1324. TfdDepth32ui1 = class(TfdDepthUI1)
  1325. procedure SetValues; override;
  1326. end;
  1327. TfdS3tcDtx1RGBA = class(TFormatDescriptor)
  1328. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1329. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1330. procedure SetValues; override;
  1331. end;
  1332. TfdS3tcDtx3RGBA = class(TFormatDescriptor)
  1333. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1334. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1335. procedure SetValues; override;
  1336. end;
  1337. TfdS3tcDtx5RGBA = class(TFormatDescriptor)
  1338. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1339. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1340. procedure SetValues; override;
  1341. end;
  1342. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1343. TbmpBitfieldFormat = class(TFormatDescriptor)
  1344. public
  1345. procedure SetCustomValues(const aBPP: Integer; aMask: TglBitmapRec4ul); overload;
  1346. procedure SetCustomValues(const aBBP: Integer; const aPrec, aShift: TglBitmapRec4ub); overload;
  1347. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1348. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1349. end;
  1350. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1351. TbmpColorTableEnty = packed record
  1352. b, g, r, a: Byte;
  1353. end;
  1354. TbmpColorTable = array of TbmpColorTableEnty;
  1355. TbmpColorTableFormat = class(TFormatDescriptor)
  1356. private
  1357. fColorTable: TbmpColorTable;
  1358. protected
  1359. procedure SetValues; override;
  1360. public
  1361. property ColorTable: TbmpColorTable read fColorTable write fColorTable;
  1362. procedure SetCustomValues(const aFormat: TglBitmapFormat; const aBPP: Integer; const aPrec, aShift: TglBitmapRec4ub); overload;
  1363. procedure CalcValues;
  1364. procedure CreateColorTable;
  1365. function CreateMappingData: Pointer; override;
  1366. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1367. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1368. destructor Destroy; override;
  1369. end;
  1370. const
  1371. LUMINANCE_WEIGHT_R = 0.30;
  1372. LUMINANCE_WEIGHT_G = 0.59;
  1373. LUMINANCE_WEIGHT_B = 0.11;
  1374. ALPHA_WEIGHT_R = 0.30;
  1375. ALPHA_WEIGHT_G = 0.59;
  1376. ALPHA_WEIGHT_B = 0.11;
  1377. DEPTH_WEIGHT_R = 0.333333333;
  1378. DEPTH_WEIGHT_G = 0.333333333;
  1379. DEPTH_WEIGHT_B = 0.333333333;
  1380. FORMAT_DESCRIPTOR_CLASSES: array[TglBitmapFormat] of TFormatDescriptorClass = (
  1381. TfdEmpty,
  1382. TfdAlpha4ub1,
  1383. TfdAlpha8ub1,
  1384. TfdAlpha16us1,
  1385. TfdLuminance4ub1,
  1386. TfdLuminance8ub1,
  1387. TfdLuminance16us1,
  1388. TfdLuminance4Alpha4ub2,
  1389. TfdLuminance6Alpha2ub2,
  1390. TfdLuminance8Alpha8ub2,
  1391. TfdLuminance12Alpha4us2,
  1392. TfdLuminance16Alpha16us2,
  1393. TfdR3G3B2ub1,
  1394. TfdRGBX4us1,
  1395. TfdXRGB4us1,
  1396. TfdR5G6B5us1,
  1397. TfdRGB5X1us1,
  1398. TfdX1RGB5us1,
  1399. TfdRGB8ub3,
  1400. TfdRGBX8ui1,
  1401. TfdXRGB8ui1,
  1402. TfdRGB10X2ui1,
  1403. TfdX2RGB10ui1,
  1404. TfdRGB16us3,
  1405. TfdRGBA4us1,
  1406. TfdARGB4us1,
  1407. TfdRGB5A1us1,
  1408. TfdA1RGB5us1,
  1409. TfdRGBA8ub4,
  1410. TfdRGBA8ui1,
  1411. TfdARGB8ui1,
  1412. TfdRGB10A2ui1,
  1413. TfdA2RGB10ui1,
  1414. TfdRGBA16us4,
  1415. TfdBGRX4us1,
  1416. TfdXBGR4us1,
  1417. TfdB5G6R5us1,
  1418. TfdBGR5X1us1,
  1419. TfdX1BGR5us1,
  1420. TfdBGR8ub3,
  1421. TfdBGRX8ui1,
  1422. TfdXBGR8ui1,
  1423. TfdBGR10X2ui1,
  1424. TfdX2BGR10ui1,
  1425. TfdBGR16us3,
  1426. TfdBGRA4us1,
  1427. TfdABGR4us1,
  1428. TfdBGR5A1us1,
  1429. TfdA1BGR5us1,
  1430. TfdBGRA8ub4,
  1431. TfdBGRA8ui1,
  1432. TfdABGR8ui1,
  1433. TfdBGR10A2ui1,
  1434. TfdA2BGR10ui1,
  1435. TfdBGRA16us4,
  1436. TfdDepth16us1,
  1437. TfdDepth24ui1,
  1438. TfdDepth32ui1,
  1439. TfdS3tcDtx1RGBA,
  1440. TfdS3tcDtx3RGBA,
  1441. TfdS3tcDtx5RGBA
  1442. );
  1443. var
  1444. FormatDescriptorCS: TCriticalSection;
  1445. FormatDescriptors: array[TglBitmapFormat] of TFormatDescriptor;
  1446. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1447. constructor EglBitmapUnsupportedFormat.Create(const aFormat: TglBitmapFormat);
  1448. begin
  1449. inherited Create('unsupported format: ' + GetEnumName(TypeInfo(TglBitmapFormat), Integer(aFormat)));
  1450. end;
  1451. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1452. constructor EglBitmapUnsupportedFormat.Create(const aMsg: String; const aFormat: TglBitmapFormat);
  1453. begin
  1454. inherited Create(aMsg + GetEnumName(TypeInfo(TglBitmapFormat), Integer(aFormat)));
  1455. end;
  1456. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1457. function glBitmapSize(X: Integer; Y: Integer): TglBitmapSize;
  1458. begin
  1459. result.Fields := [];
  1460. if (X >= 0) then
  1461. result.Fields := result.Fields + [ffX];
  1462. if (Y >= 0) then
  1463. result.Fields := result.Fields + [ffY];
  1464. result.X := Max(0, X);
  1465. result.Y := Max(0, Y);
  1466. end;
  1467. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1468. function glBitmapPosition(X: Integer; Y: Integer): TglBitmapPixelPosition;
  1469. begin
  1470. result := glBitmapSize(X, Y);
  1471. end;
  1472. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1473. function glBitmapRec4ub(const r, g, b, a: Byte): TglBitmapRec4ub;
  1474. begin
  1475. result.r := r;
  1476. result.g := g;
  1477. result.b := b;
  1478. result.a := a;
  1479. end;
  1480. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1481. function glBitmapRec4ui(const r, g, b, a: Cardinal): TglBitmapRec4ui;
  1482. begin
  1483. result.r := r;
  1484. result.g := g;
  1485. result.b := b;
  1486. result.a := a;
  1487. end;
  1488. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1489. function glBitmapRec4ul(const r, g, b, a: QWord): TglBitmapRec4ul;
  1490. begin
  1491. result.r := r;
  1492. result.g := g;
  1493. result.b := b;
  1494. result.a := a;
  1495. end;
  1496. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1497. function glBitmapRec4ubCompare(const r1, r2: TglBitmapRec4ub): 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 glBitmapRec4uiCompare(const r1, r2: TglBitmapRec4ui): Boolean;
  1509. var
  1510. i: Integer;
  1511. begin
  1512. result := false;
  1513. for i := 0 to high(r1.arr) do
  1514. if (r1.arr[i] <> r2.arr[i]) then
  1515. exit;
  1516. result := true;
  1517. end;
  1518. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1519. function glBitmapCreateTestData(const aFormat: TglBitmapFormat): TglBitmapData;
  1520. var
  1521. desc: TFormatDescriptor;
  1522. p, tmp: PByte;
  1523. x, y, i: Integer;
  1524. md: Pointer;
  1525. px: TglBitmapPixelData;
  1526. begin
  1527. result := nil;
  1528. desc := TFormatDescriptor.Get(aFormat);
  1529. if (desc.IsCompressed) or (desc.glFormat = 0) then
  1530. exit;
  1531. p := GetMemory(ceil(25 * desc.BytesPerPixel)); // 5 x 5 pixel
  1532. md := desc.CreateMappingData;
  1533. try
  1534. tmp := p;
  1535. desc.PreparePixel(px);
  1536. for y := 0 to 4 do
  1537. for x := 0 to 4 do begin
  1538. px.Data := glBitmapRec4ui(0, 0, 0, 0);
  1539. for i := 0 to 3 do begin
  1540. if ((y < 3) and (y = i)) or
  1541. ((y = 3) and (i < 3)) or
  1542. ((y = 4) and (i = 3))
  1543. then
  1544. px.Data.arr[i] := Trunc(px.Range.arr[i] / 4 * x)
  1545. else if ((y < 4) and (i = 3)) or
  1546. ((y = 4) and (i < 3))
  1547. then
  1548. px.Data.arr[i] := px.Range.arr[i]
  1549. else
  1550. px.Data.arr[i] := 0; //px.Range.arr[i];
  1551. end;
  1552. desc.Map(px, tmp, md);
  1553. end;
  1554. finally
  1555. desc.FreeMappingData(md);
  1556. end;
  1557. result := TglBitmapData.Create(glBitmapPosition(5, 5), aFormat, p);
  1558. end;
  1559. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1560. function glBitmapShiftRec(const r, g, b, a: Byte): TglBitmapRec4ub;
  1561. begin
  1562. result.r := r;
  1563. result.g := g;
  1564. result.b := b;
  1565. result.a := a;
  1566. end;
  1567. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1568. function FormatGetSupportedFiles(const aFormat: TglBitmapFormat): TglBitmapFileTypes;
  1569. begin
  1570. result := [];
  1571. if (aFormat in [
  1572. //8bpp
  1573. tfAlpha4ub1, tfAlpha8ub1,
  1574. tfLuminance4ub1, tfLuminance8ub1, tfR3G3B2ub1,
  1575. //16bpp
  1576. tfLuminance4Alpha4ub2, tfLuminance6Alpha2ub2, tfLuminance8Alpha8ub2,
  1577. tfRGBX4us1, tfXRGB4us1, tfRGB5X1us1, tfX1RGB5us1, tfR5G6B5us1, tfRGB5A1us1, tfA1RGB5us1, tfRGBA4us1, tfARGB4us1,
  1578. tfBGRX4us1, tfXBGR4us1, tfBGR5X1us1, tfX1BGR5us1, tfB5G6R5us1, tfBGR5A1us1, tfA1BGR5us1, tfBGRA4us1, tfABGR4us1,
  1579. //24bpp
  1580. tfBGR8ub3, tfRGB8ub3,
  1581. //32bpp
  1582. tfRGBX8ui1, tfXRGB8ui1, tfRGB10X2ui1, tfX2RGB10ui1, tfRGBA8ui1, tfARGB8ui1, tfRGBA8ub4, tfRGB10A2ui1, tfA2RGB10ui1,
  1583. tfBGRX8ui1, tfXBGR8ui1, tfBGR10X2ui1, tfX2BGR10ui1, tfBGRA8ui1, tfABGR8ui1, tfBGRA8ub4, tfBGR10A2ui1, tfA2BGR10ui1])
  1584. then
  1585. result := result + [ ftBMP ];
  1586. if (aFormat in [
  1587. //8bbp
  1588. tfAlpha4ub1, tfAlpha8ub1, tfLuminance4ub1, tfLuminance8ub1,
  1589. //16bbp
  1590. tfAlpha16us1, tfLuminance16us1,
  1591. tfLuminance4Alpha4ub2, tfLuminance6Alpha2ub2, tfLuminance8Alpha8ub2,
  1592. tfX1RGB5us1, tfARGB4us1, tfA1RGB5us1, tfDepth16us1,
  1593. //24bbp
  1594. tfBGR8ub3,
  1595. //32bbp
  1596. tfX2RGB10ui1, tfARGB8ui1, tfBGRA8ub4, tfA2RGB10ui1,
  1597. tfDepth24ui1, tfDepth32ui1])
  1598. then
  1599. result := result + [ftTGA];
  1600. if not (aFormat in [tfEmpty, tfRGB16us3, tfBGR16us3]) then
  1601. result := result + [ftDDS];
  1602. {$IFDEF GLB_SUPPORT_PNG_WRITE}
  1603. if aFormat in [
  1604. tfAlpha8ub1, tfLuminance8ub1, tfLuminance8Alpha8ub2,
  1605. tfRGB8ub3, tfRGBA8ui1,
  1606. tfBGR8ub3, tfBGRA8ui1] then
  1607. result := result + [ftPNG];
  1608. {$ENDIF}
  1609. {$IFDEF GLB_SUPPORT_JPEG_WRITE}
  1610. if aFormat in [tfAlpha8ub1, tfLuminance8ub1, tfRGB8ub3, tfBGR8ub3] then
  1611. result := result + [ftJPEG];
  1612. {$ENDIF}
  1613. end;
  1614. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1615. function IsPowerOfTwo(aNumber: Integer): Boolean;
  1616. begin
  1617. while (aNumber and 1) = 0 do
  1618. aNumber := aNumber shr 1;
  1619. result := aNumber = 1;
  1620. end;
  1621. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1622. function GetTopMostBit(aBitSet: QWord): Integer;
  1623. begin
  1624. result := 0;
  1625. while aBitSet > 0 do begin
  1626. inc(result);
  1627. aBitSet := aBitSet shr 1;
  1628. end;
  1629. end;
  1630. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1631. function CountSetBits(aBitSet: QWord): Integer;
  1632. begin
  1633. result := 0;
  1634. while aBitSet > 0 do begin
  1635. if (aBitSet and 1) = 1 then
  1636. inc(result);
  1637. aBitSet := aBitSet shr 1;
  1638. end;
  1639. end;
  1640. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1641. function LuminanceWeight(const aPixel: TglBitmapPixelData): Cardinal;
  1642. begin
  1643. result := Trunc(
  1644. LUMINANCE_WEIGHT_R * aPixel.Data.r +
  1645. LUMINANCE_WEIGHT_G * aPixel.Data.g +
  1646. LUMINANCE_WEIGHT_B * aPixel.Data.b);
  1647. end;
  1648. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1649. function DepthWeight(const aPixel: TglBitmapPixelData): Cardinal;
  1650. begin
  1651. result := Trunc(
  1652. DEPTH_WEIGHT_R * aPixel.Data.r +
  1653. DEPTH_WEIGHT_G * aPixel.Data.g +
  1654. DEPTH_WEIGHT_B * aPixel.Data.b);
  1655. end;
  1656. {$IFDEF GLB_SDL_IMAGE}
  1657. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1658. // SDL Image Helper /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1659. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1660. function glBitmapRWseek(context: PSDL_RWops; offset: Integer; whence: Integer): Integer; cdecl;
  1661. begin
  1662. result := TStream(context^.unknown.data1).Seek(offset, whence);
  1663. end;
  1664. function glBitmapRWread(context: PSDL_RWops; Ptr: Pointer; size: Integer; maxnum : Integer): Integer; cdecl;
  1665. begin
  1666. result := TStream(context^.unknown.data1).Read(Ptr^, size * maxnum);
  1667. end;
  1668. function glBitmapRWwrite(context: PSDL_RWops; Ptr: Pointer; size: Integer; num: Integer): Integer; cdecl;
  1669. begin
  1670. result := TStream(context^.unknown.data1).Write(Ptr^, size * num);
  1671. end;
  1672. function glBitmapRWclose(context: PSDL_RWops): Integer; cdecl;
  1673. begin
  1674. result := 0;
  1675. end;
  1676. function glBitmapCreateRWops(Stream: TStream): PSDL_RWops;
  1677. begin
  1678. result := SDL_AllocRW;
  1679. if result = nil then
  1680. raise EglBitmap.Create('glBitmapCreateRWops - SDL_AllocRW failed.');
  1681. result^.seek := glBitmapRWseek;
  1682. result^.read := glBitmapRWread;
  1683. result^.write := glBitmapRWwrite;
  1684. result^.close := glBitmapRWclose;
  1685. result^.unknown.data1 := Stream;
  1686. end;
  1687. {$ENDIF}
  1688. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1689. procedure glBitmapSetDefaultDeleteTextureOnFree(const aDeleteTextureOnFree: Boolean);
  1690. begin
  1691. glBitmapDefaultDeleteTextureOnFree := aDeleteTextureOnFree;
  1692. end;
  1693. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1694. procedure glBitmapSetDefaultFreeDataAfterGenTexture(const aFreeData: Boolean);
  1695. begin
  1696. glBitmapDefaultFreeDataAfterGenTextures := aFreeData;
  1697. end;
  1698. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1699. procedure glBitmapSetDefaultMipmap(const aValue: TglBitmapMipMap);
  1700. begin
  1701. glBitmapDefaultMipmap := aValue;
  1702. end;
  1703. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1704. procedure glBitmapSetDefaultFormat(const aFormat: TglBitmapFormat);
  1705. begin
  1706. glBitmapDefaultFormat := aFormat;
  1707. end;
  1708. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1709. procedure glBitmapSetDefaultFilter(const aMin, aMag: Integer);
  1710. begin
  1711. glBitmapDefaultFilterMin := aMin;
  1712. glBitmapDefaultFilterMag := aMag;
  1713. end;
  1714. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1715. procedure glBitmapSetDefaultWrap(const S: Cardinal = GL_CLAMP_TO_EDGE; const T: Cardinal = GL_CLAMP_TO_EDGE; const R: Cardinal = GL_CLAMP_TO_EDGE);
  1716. begin
  1717. glBitmapDefaultWrapS := S;
  1718. glBitmapDefaultWrapT := T;
  1719. glBitmapDefaultWrapR := R;
  1720. end;
  1721. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1722. {$IF NOT DEFINED(OPENGL_ES) OR DEFINED(OPENGL_ES_3_0)}
  1723. procedure glBitmapSetDefaultSwizzle(const r: GLenum = GL_RED; g: GLenum = GL_GREEN; b: GLenum = GL_BLUE; a: GLenum = GL_ALPHA);
  1724. begin
  1725. glDefaultSwizzle[0] := r;
  1726. glDefaultSwizzle[1] := g;
  1727. glDefaultSwizzle[2] := b;
  1728. glDefaultSwizzle[3] := a;
  1729. end;
  1730. {$IFEND}
  1731. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1732. function glBitmapGetDefaultDeleteTextureOnFree: Boolean;
  1733. begin
  1734. result := glBitmapDefaultDeleteTextureOnFree;
  1735. end;
  1736. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1737. function glBitmapGetDefaultFreeDataAfterGenTexture: Boolean;
  1738. begin
  1739. result := glBitmapDefaultFreeDataAfterGenTextures;
  1740. end;
  1741. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1742. function glBitmapGetDefaultMipmap: TglBitmapMipMap;
  1743. begin
  1744. result := glBitmapDefaultMipmap;
  1745. end;
  1746. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1747. function glBitmapGetDefaultFormat: TglBitmapFormat;
  1748. begin
  1749. result := glBitmapDefaultFormat;
  1750. end;
  1751. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1752. procedure glBitmapGetDefaultFilter(var aMin, aMag: Cardinal);
  1753. begin
  1754. aMin := glBitmapDefaultFilterMin;
  1755. aMag := glBitmapDefaultFilterMag;
  1756. end;
  1757. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1758. procedure glBitmapGetDefaultTextureWrap(var S, T, R: Cardinal);
  1759. begin
  1760. S := glBitmapDefaultWrapS;
  1761. T := glBitmapDefaultWrapT;
  1762. R := glBitmapDefaultWrapR;
  1763. end;
  1764. {$IF NOT DEFINED(OPENGL_ES) OR DEFINED(OPENGL_ES_3_0)}
  1765. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1766. procedure glBitmapGetDefaultSwizzle(var r, g, b, a: GLenum);
  1767. begin
  1768. r := glDefaultSwizzle[0];
  1769. g := glDefaultSwizzle[1];
  1770. b := glDefaultSwizzle[2];
  1771. a := glDefaultSwizzle[3];
  1772. end;
  1773. {$IFEND}
  1774. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1775. //TFormatDescriptor///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1776. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1777. function TFormatDescriptor.CreateMappingData: Pointer;
  1778. begin
  1779. result := nil;
  1780. end;
  1781. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1782. procedure TFormatDescriptor.FreeMappingData(var aMappingData: Pointer);
  1783. begin
  1784. //DUMMY
  1785. end;
  1786. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1787. function TFormatDescriptor.IsEmpty: Boolean;
  1788. begin
  1789. result := (fFormat = tfEmpty);
  1790. end;
  1791. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1792. function TFormatDescriptor.MaskMatch(const aMask: TglBitmapRec4ul): Boolean;
  1793. var
  1794. i: Integer;
  1795. m: TglBitmapRec4ul;
  1796. begin
  1797. result := false;
  1798. if (aMask.r = 0) and (aMask.g = 0) and (aMask.b = 0) and (aMask.a = 0) then
  1799. raise EglBitmap.Create('FormatCheckFormat - All Masks are 0');
  1800. m := Mask;
  1801. for i := 0 to 3 do
  1802. if (aMask.arr[i] <> m.arr[i]) then
  1803. exit;
  1804. result := true;
  1805. end;
  1806. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1807. procedure TFormatDescriptor.PreparePixel(out aPixel: TglBitmapPixelData);
  1808. begin
  1809. FillChar(aPixel{%H-}, SizeOf(aPixel), 0);
  1810. aPixel.Data := Range;
  1811. aPixel.Format := fFormat;
  1812. aPixel.Range := Range;
  1813. end;
  1814. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1815. constructor TFormatDescriptor.Create;
  1816. begin
  1817. inherited Create;
  1818. end;
  1819. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1820. //TfdAlpha_UB1////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1821. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1822. procedure TfdAlphaUB1.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  1823. begin
  1824. aData^ := aPixel.Data.a;
  1825. inc(aData);
  1826. end;
  1827. procedure TfdAlphaUB1.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  1828. begin
  1829. aPixel.Data.r := 0;
  1830. aPixel.Data.g := 0;
  1831. aPixel.Data.b := 0;
  1832. aPixel.Data.a := aData^;
  1833. inc(aData);
  1834. end;
  1835. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1836. //TfdLuminance_UB1////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1837. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1838. procedure TfdLuminanceUB1.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  1839. begin
  1840. aData^ := LuminanceWeight(aPixel);
  1841. inc(aData);
  1842. end;
  1843. procedure TfdLuminanceUB1.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  1844. begin
  1845. aPixel.Data.r := aData^;
  1846. aPixel.Data.g := aData^;
  1847. aPixel.Data.b := aData^;
  1848. aPixel.Data.a := 0;
  1849. inc(aData);
  1850. end;
  1851. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1852. //TfdUniversal_UB1////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1853. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1854. procedure TfdUniversalUB1.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  1855. var
  1856. i: Integer;
  1857. begin
  1858. aData^ := 0;
  1859. for i := 0 to 3 do
  1860. if (Range.arr[i] > 0) then
  1861. aData^ := aData^ or ((aPixel.Data.arr[i] and Range.arr[i]) shl fShift.arr[i]);
  1862. inc(aData);
  1863. end;
  1864. procedure TfdUniversalUB1.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  1865. var
  1866. i: Integer;
  1867. begin
  1868. for i := 0 to 3 do
  1869. aPixel.Data.arr[i] := (aData^ shr fShift.arr[i]) and Range.arr[i];
  1870. inc(aData);
  1871. end;
  1872. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1873. //TfdLuminanceAlpha_UB2///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1874. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1875. procedure TfdLuminanceAlphaUB2.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  1876. begin
  1877. inherited Map(aPixel, aData, aMapData);
  1878. aData^ := aPixel.Data.a;
  1879. inc(aData);
  1880. end;
  1881. procedure TfdLuminanceAlphaUB2.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  1882. begin
  1883. inherited Unmap(aData, aPixel, aMapData);
  1884. aPixel.Data.a := aData^;
  1885. inc(aData);
  1886. end;
  1887. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1888. //TfdRGB_UB3//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1889. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1890. procedure TfdRGBub3.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  1891. begin
  1892. aData^ := aPixel.Data.r;
  1893. inc(aData);
  1894. aData^ := aPixel.Data.g;
  1895. inc(aData);
  1896. aData^ := aPixel.Data.b;
  1897. inc(aData);
  1898. end;
  1899. procedure TfdRGBub3.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  1900. begin
  1901. aPixel.Data.r := aData^;
  1902. inc(aData);
  1903. aPixel.Data.g := aData^;
  1904. inc(aData);
  1905. aPixel.Data.b := aData^;
  1906. inc(aData);
  1907. aPixel.Data.a := 0;
  1908. end;
  1909. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1910. //TfdBGR_UB3//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1911. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1912. procedure TfdBGRub3.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  1913. begin
  1914. aData^ := aPixel.Data.b;
  1915. inc(aData);
  1916. aData^ := aPixel.Data.g;
  1917. inc(aData);
  1918. aData^ := aPixel.Data.r;
  1919. inc(aData);
  1920. end;
  1921. procedure TfdBGRub3.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  1922. begin
  1923. aPixel.Data.b := aData^;
  1924. inc(aData);
  1925. aPixel.Data.g := aData^;
  1926. inc(aData);
  1927. aPixel.Data.r := aData^;
  1928. inc(aData);
  1929. aPixel.Data.a := 0;
  1930. end;
  1931. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1932. //TfdRGBA_UB4//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1933. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1934. procedure TfdRGBAub4.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  1935. begin
  1936. inherited Map(aPixel, aData, aMapData);
  1937. aData^ := aPixel.Data.a;
  1938. inc(aData);
  1939. end;
  1940. procedure TfdRGBAub4.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  1941. begin
  1942. inherited Unmap(aData, aPixel, aMapData);
  1943. aPixel.Data.a := aData^;
  1944. inc(aData);
  1945. end;
  1946. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1947. //TfdBGRA_UB4//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1948. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1949. procedure TfdBGRAub4.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  1950. begin
  1951. inherited Map(aPixel, aData, aMapData);
  1952. aData^ := aPixel.Data.a;
  1953. inc(aData);
  1954. end;
  1955. procedure TfdBGRAub4.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  1956. begin
  1957. inherited Unmap(aData, aPixel, aMapData);
  1958. aPixel.Data.a := aData^;
  1959. inc(aData);
  1960. end;
  1961. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1962. //TfdAlpha_US1////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1963. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1964. procedure TfdAlphaUS1.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  1965. begin
  1966. PWord(aData)^ := aPixel.Data.a;
  1967. inc(aData, 2);
  1968. end;
  1969. procedure TfdAlphaUS1.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  1970. begin
  1971. aPixel.Data.r := 0;
  1972. aPixel.Data.g := 0;
  1973. aPixel.Data.b := 0;
  1974. aPixel.Data.a := PWord(aData)^;
  1975. inc(aData, 2);
  1976. end;
  1977. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1978. //TfdLuminance_US1////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1979. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1980. procedure TfdLuminanceUS1.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  1981. begin
  1982. PWord(aData)^ := LuminanceWeight(aPixel);
  1983. inc(aData, 2);
  1984. end;
  1985. procedure TfdLuminanceUS1.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  1986. begin
  1987. aPixel.Data.r := PWord(aData)^;
  1988. aPixel.Data.g := PWord(aData)^;
  1989. aPixel.Data.b := PWord(aData)^;
  1990. aPixel.Data.a := 0;
  1991. inc(aData, 2);
  1992. end;
  1993. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1994. //TfdUniversal_US1////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1995. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1996. procedure TfdUniversalUS1.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  1997. var
  1998. i: Integer;
  1999. begin
  2000. PWord(aData)^ := 0;
  2001. for i := 0 to 3 do
  2002. if (Range.arr[i] > 0) then
  2003. PWord(aData)^ := PWord(aData)^ or ((aPixel.Data.arr[i] and Range.arr[i]) shl fShift.arr[i]);
  2004. inc(aData, 2);
  2005. end;
  2006. procedure TfdUniversalUS1.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  2007. var
  2008. i: Integer;
  2009. begin
  2010. for i := 0 to 3 do
  2011. aPixel.Data.arr[i] := (PWord(aData)^ shr fShift.arr[i]) and Range.arr[i];
  2012. inc(aData, 2);
  2013. end;
  2014. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2015. //TfdDepth_US1////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2016. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2017. procedure TfdDepthUS1.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  2018. begin
  2019. PWord(aData)^ := DepthWeight(aPixel);
  2020. inc(aData, 2);
  2021. end;
  2022. procedure TfdDepthUS1.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  2023. begin
  2024. aPixel.Data.r := PWord(aData)^;
  2025. aPixel.Data.g := PWord(aData)^;
  2026. aPixel.Data.b := PWord(aData)^;
  2027. aPixel.Data.a := PWord(aData)^;;
  2028. inc(aData, 2);
  2029. end;
  2030. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2031. //TfdLuminanceAlpha_US2///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2032. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2033. procedure TfdLuminanceAlphaUS2.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  2034. begin
  2035. inherited Map(aPixel, aData, aMapData);
  2036. PWord(aData)^ := aPixel.Data.a;
  2037. inc(aData, 2);
  2038. end;
  2039. procedure TfdLuminanceAlphaUS2.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  2040. begin
  2041. inherited Unmap(aData, aPixel, aMapData);
  2042. aPixel.Data.a := PWord(aData)^;
  2043. inc(aData, 2);
  2044. end;
  2045. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2046. //TfdRGB_US3//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2047. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2048. procedure TfdRGBus3.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  2049. begin
  2050. PWord(aData)^ := aPixel.Data.r;
  2051. inc(aData, 2);
  2052. PWord(aData)^ := aPixel.Data.g;
  2053. inc(aData, 2);
  2054. PWord(aData)^ := aPixel.Data.b;
  2055. inc(aData, 2);
  2056. end;
  2057. procedure TfdRGBus3.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  2058. begin
  2059. aPixel.Data.r := PWord(aData)^;
  2060. inc(aData, 2);
  2061. aPixel.Data.g := PWord(aData)^;
  2062. inc(aData, 2);
  2063. aPixel.Data.b := PWord(aData)^;
  2064. inc(aData, 2);
  2065. aPixel.Data.a := 0;
  2066. end;
  2067. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2068. //TfdBGR_US3//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2069. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2070. procedure TfdBGRus3.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  2071. begin
  2072. PWord(aData)^ := aPixel.Data.b;
  2073. inc(aData, 2);
  2074. PWord(aData)^ := aPixel.Data.g;
  2075. inc(aData, 2);
  2076. PWord(aData)^ := aPixel.Data.r;
  2077. inc(aData, 2);
  2078. end;
  2079. procedure TfdBGRus3.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  2080. begin
  2081. aPixel.Data.b := PWord(aData)^;
  2082. inc(aData, 2);
  2083. aPixel.Data.g := PWord(aData)^;
  2084. inc(aData, 2);
  2085. aPixel.Data.r := PWord(aData)^;
  2086. inc(aData, 2);
  2087. aPixel.Data.a := 0;
  2088. end;
  2089. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2090. //TfdRGBA_US4/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2091. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2092. procedure TfdRGBAus4.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  2093. begin
  2094. inherited Map(aPixel, aData, aMapData);
  2095. PWord(aData)^ := aPixel.Data.a;
  2096. inc(aData, 2);
  2097. end;
  2098. procedure TfdRGBAus4.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  2099. begin
  2100. inherited Unmap(aData, aPixel, aMapData);
  2101. aPixel.Data.a := PWord(aData)^;
  2102. inc(aData, 2);
  2103. end;
  2104. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2105. //TfdARGB_US4/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2106. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2107. procedure TfdARGBus4.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  2108. begin
  2109. PWord(aData)^ := aPixel.Data.a;
  2110. inc(aData, 2);
  2111. inherited Map(aPixel, aData, aMapData);
  2112. end;
  2113. procedure TfdARGBus4.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  2114. begin
  2115. aPixel.Data.a := PWord(aData)^;
  2116. inc(aData, 2);
  2117. inherited Unmap(aData, aPixel, aMapData);
  2118. end;
  2119. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2120. //TfdBGRA_US4/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2121. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2122. procedure TfdBGRAus4.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  2123. begin
  2124. inherited Map(aPixel, aData, aMapData);
  2125. PWord(aData)^ := aPixel.Data.a;
  2126. inc(aData, 2);
  2127. end;
  2128. procedure TfdBGRAus4.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  2129. begin
  2130. inherited Unmap(aData, aPixel, aMapData);
  2131. aPixel.Data.a := PWord(aData)^;
  2132. inc(aData, 2);
  2133. end;
  2134. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2135. //TfdABGR_US4/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2136. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2137. procedure TfdABGRus4.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  2138. begin
  2139. PWord(aData)^ := aPixel.Data.a;
  2140. inc(aData, 2);
  2141. inherited Map(aPixel, aData, aMapData);
  2142. end;
  2143. procedure TfdABGRus4.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  2144. begin
  2145. aPixel.Data.a := PWord(aData)^;
  2146. inc(aData, 2);
  2147. inherited Unmap(aData, aPixel, aMapData);
  2148. end;
  2149. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2150. //TfdUniversal_UI1////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2151. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2152. procedure TfdUniversalUI1.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  2153. var
  2154. i: Integer;
  2155. begin
  2156. PCardinal(aData)^ := 0;
  2157. for i := 0 to 3 do
  2158. if (Range.arr[i] > 0) then
  2159. PCardinal(aData)^ := PCardinal(aData)^ or ((aPixel.Data.arr[i] and Range.arr[i]) shl fShift.arr[i]);
  2160. inc(aData, 4);
  2161. end;
  2162. procedure TfdUniversalUI1.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  2163. var
  2164. i: Integer;
  2165. begin
  2166. for i := 0 to 3 do
  2167. aPixel.Data.arr[i] := (PCardinal(aData)^ shr fShift.arr[i]) and Range.arr[i];
  2168. inc(aData, 2);
  2169. end;
  2170. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2171. //TfdDepth_UI1////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2172. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2173. procedure TfdDepthUI1.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  2174. begin
  2175. PCardinal(aData)^ := DepthWeight(aPixel);
  2176. inc(aData, 4);
  2177. end;
  2178. procedure TfdDepthUI1.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  2179. begin
  2180. aPixel.Data.r := PCardinal(aData)^;
  2181. aPixel.Data.g := PCardinal(aData)^;
  2182. aPixel.Data.b := PCardinal(aData)^;
  2183. aPixel.Data.a := PCardinal(aData)^;
  2184. inc(aData, 4);
  2185. end;
  2186. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2187. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2188. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2189. procedure TfdAlpha4ub1.SetValues;
  2190. begin
  2191. inherited SetValues;
  2192. fBitsPerPixel := 8;
  2193. fFormat := tfAlpha4ub1;
  2194. fWithAlpha := tfAlpha4ub1;
  2195. fPrecision := glBitmapRec4ub(0, 0, 0, 8);
  2196. fShift := glBitmapRec4ub(0, 0, 0, 0);
  2197. {$IFNDEF OPENGL_ES}
  2198. fOpenGLFormat := tfAlpha4ub1;
  2199. fglFormat := GL_ALPHA;
  2200. fglInternalFormat := GL_ALPHA4;
  2201. fglDataFormat := GL_UNSIGNED_BYTE;
  2202. {$ELSE}
  2203. fOpenGLFormat := tfAlpha8ub1;
  2204. {$ENDIF}
  2205. end;
  2206. procedure TfdAlpha8ub1.SetValues;
  2207. begin
  2208. inherited SetValues;
  2209. fBitsPerPixel := 8;
  2210. fFormat := tfAlpha8ub1;
  2211. fWithAlpha := tfAlpha8ub1;
  2212. fPrecision := glBitmapRec4ub(0, 0, 0, 8);
  2213. fShift := glBitmapRec4ub(0, 0, 0, 0);
  2214. fOpenGLFormat := tfAlpha8ub1;
  2215. fglFormat := GL_ALPHA;
  2216. fglInternalFormat := {$IFNDEF OPENGL_ES}GL_ALPHA8{$ELSE}GL_ALPHA{$ENDIF};
  2217. fglDataFormat := GL_UNSIGNED_BYTE;
  2218. end;
  2219. procedure TfdAlpha16us1.SetValues;
  2220. begin
  2221. inherited SetValues;
  2222. fBitsPerPixel := 16;
  2223. fFormat := tfAlpha16us1;
  2224. fWithAlpha := tfAlpha16us1;
  2225. fPrecision := glBitmapRec4ub(0, 0, 0, 16);
  2226. fShift := glBitmapRec4ub(0, 0, 0, 0);
  2227. {$IFNDEF OPENGL_ES}
  2228. fOpenGLFormat := tfAlpha16us1;
  2229. fglFormat := GL_ALPHA;
  2230. fglInternalFormat := GL_ALPHA16;
  2231. fglDataFormat := GL_UNSIGNED_SHORT;
  2232. {$ELSE}
  2233. fOpenGLFormat := tfAlpha8ub1;
  2234. {$ENDIF}
  2235. end;
  2236. procedure TfdLuminance4ub1.SetValues;
  2237. begin
  2238. inherited SetValues;
  2239. fBitsPerPixel := 8;
  2240. fFormat := tfLuminance4ub1;
  2241. fWithAlpha := tfLuminance4Alpha4ub2;
  2242. fWithoutAlpha := tfLuminance4ub1;
  2243. fPrecision := glBitmapRec4ub(8, 8, 8, 0);
  2244. fShift := glBitmapRec4ub(0, 0, 0, 0);
  2245. {$IFNDEF OPENGL_ES}
  2246. fOpenGLFormat := tfLuminance4ub1;
  2247. fglFormat := GL_LUMINANCE;
  2248. fglInternalFormat := GL_LUMINANCE4;
  2249. fglDataFormat := GL_UNSIGNED_BYTE;
  2250. {$ELSE}
  2251. fOpenGLFormat := tfLuminance8ub1;
  2252. {$ENDIF}
  2253. end;
  2254. procedure TfdLuminance8ub1.SetValues;
  2255. begin
  2256. inherited SetValues;
  2257. fBitsPerPixel := 8;
  2258. fFormat := tfLuminance8ub1;
  2259. fWithAlpha := tfLuminance8Alpha8ub2;
  2260. fWithoutAlpha := tfLuminance8ub1;
  2261. fOpenGLFormat := tfLuminance8ub1;
  2262. fPrecision := glBitmapRec4ub(8, 8, 8, 0);
  2263. fShift := glBitmapRec4ub(0, 0, 0, 0);
  2264. fglFormat := GL_LUMINANCE;
  2265. fglInternalFormat := {$IFNDEF OPENGL_ES}GL_LUMINANCE8{$ELSE}GL_LUMINANCE{$ENDIF};
  2266. fglDataFormat := GL_UNSIGNED_BYTE;
  2267. end;
  2268. procedure TfdLuminance16us1.SetValues;
  2269. begin
  2270. inherited SetValues;
  2271. fBitsPerPixel := 16;
  2272. fFormat := tfLuminance16us1;
  2273. fWithAlpha := tfLuminance16Alpha16us2;
  2274. fWithoutAlpha := tfLuminance16us1;
  2275. fPrecision := glBitmapRec4ub(16, 16, 16, 0);
  2276. fShift := glBitmapRec4ub( 0, 0, 0, 0);
  2277. {$IFNDEF OPENGL_ES}
  2278. fOpenGLFormat := tfLuminance16us1;
  2279. fglFormat := GL_LUMINANCE;
  2280. fglInternalFormat := GL_LUMINANCE16;
  2281. fglDataFormat := GL_UNSIGNED_SHORT;
  2282. {$ELSE}
  2283. fOpenGLFormat := tfLuminance8ub1;
  2284. {$ENDIF}
  2285. end;
  2286. procedure TfdLuminance4Alpha4ub2.SetValues;
  2287. begin
  2288. inherited SetValues;
  2289. fBitsPerPixel := 16;
  2290. fFormat := tfLuminance4Alpha4ub2;
  2291. fWithAlpha := tfLuminance4Alpha4ub2;
  2292. fWithoutAlpha := tfLuminance4ub1;
  2293. fPrecision := glBitmapRec4ub(8, 8, 8, 8);
  2294. fShift := glBitmapRec4ub(0, 0, 0, 8);
  2295. {$IFNDEF OPENGL_ES}
  2296. fOpenGLFormat := tfLuminance4Alpha4ub2;
  2297. fglFormat := GL_LUMINANCE_ALPHA;
  2298. fglInternalFormat := GL_LUMINANCE4_ALPHA4;
  2299. fglDataFormat := GL_UNSIGNED_BYTE;
  2300. {$ELSE}
  2301. fOpenGLFormat := tfLuminance8Alpha8ub2;
  2302. {$ENDIF}
  2303. end;
  2304. procedure TfdLuminance6Alpha2ub2.SetValues;
  2305. begin
  2306. inherited SetValues;
  2307. fBitsPerPixel := 16;
  2308. fFormat := tfLuminance6Alpha2ub2;
  2309. fWithAlpha := tfLuminance6Alpha2ub2;
  2310. fWithoutAlpha := tfLuminance8ub1;
  2311. fPrecision := glBitmapRec4ub(8, 8, 8, 8);
  2312. fShift := glBitmapRec4ub(0, 0, 0, 8);
  2313. {$IFNDEF OPENGL_ES}
  2314. fOpenGLFormat := tfLuminance6Alpha2ub2;
  2315. fglFormat := GL_LUMINANCE_ALPHA;
  2316. fglInternalFormat := GL_LUMINANCE6_ALPHA2;
  2317. fglDataFormat := GL_UNSIGNED_BYTE;
  2318. {$ELSE}
  2319. fOpenGLFormat := tfLuminance8Alpha8ub2;
  2320. {$ENDIF}
  2321. end;
  2322. procedure TfdLuminance8Alpha8ub2.SetValues;
  2323. begin
  2324. inherited SetValues;
  2325. fBitsPerPixel := 16;
  2326. fFormat := tfLuminance8Alpha8ub2;
  2327. fWithAlpha := tfLuminance8Alpha8ub2;
  2328. fWithoutAlpha := tfLuminance8ub1;
  2329. fOpenGLFormat := tfLuminance8Alpha8ub2;
  2330. fPrecision := glBitmapRec4ub(8, 8, 8, 8);
  2331. fShift := glBitmapRec4ub(0, 0, 0, 8);
  2332. fglFormat := GL_LUMINANCE_ALPHA;
  2333. fglInternalFormat := {$IFNDEF OPENGL_ES}GL_LUMINANCE8_ALPHA8{$ELSE}GL_LUMINANCE_ALPHA{$ENDIF};
  2334. fglDataFormat := GL_UNSIGNED_BYTE;
  2335. end;
  2336. procedure TfdLuminance12Alpha4us2.SetValues;
  2337. begin
  2338. inherited SetValues;
  2339. fBitsPerPixel := 32;
  2340. fFormat := tfLuminance12Alpha4us2;
  2341. fWithAlpha := tfLuminance12Alpha4us2;
  2342. fWithoutAlpha := tfLuminance16us1;
  2343. fPrecision := glBitmapRec4ub(16, 16, 16, 16);
  2344. fShift := glBitmapRec4ub( 0, 0, 0, 16);
  2345. {$IFNDEF OPENGL_ES}
  2346. fOpenGLFormat := tfLuminance12Alpha4us2;
  2347. fglFormat := GL_LUMINANCE_ALPHA;
  2348. fglInternalFormat := GL_LUMINANCE12_ALPHA4;
  2349. fglDataFormat := GL_UNSIGNED_SHORT;
  2350. {$ELSE}
  2351. fOpenGLFormat := tfLuminance8Alpha8ub2;
  2352. {$ENDIF}
  2353. end;
  2354. procedure TfdLuminance16Alpha16us2.SetValues;
  2355. begin
  2356. inherited SetValues;
  2357. fBitsPerPixel := 32;
  2358. fFormat := tfLuminance16Alpha16us2;
  2359. fWithAlpha := tfLuminance16Alpha16us2;
  2360. fWithoutAlpha := tfLuminance16us1;
  2361. fPrecision := glBitmapRec4ub(16, 16, 16, 16);
  2362. fShift := glBitmapRec4ub( 0, 0, 0, 16);
  2363. {$IFNDEF OPENGL_ES}
  2364. fOpenGLFormat := tfLuminance16Alpha16us2;
  2365. fglFormat := GL_LUMINANCE_ALPHA;
  2366. fglInternalFormat := GL_LUMINANCE16_ALPHA16;
  2367. fglDataFormat := GL_UNSIGNED_SHORT;
  2368. {$ELSE}
  2369. fOpenGLFormat := tfLuminance8Alpha8ub2;
  2370. {$ENDIF}
  2371. end;
  2372. procedure TfdR3G3B2ub1.SetValues;
  2373. begin
  2374. inherited SetValues;
  2375. fBitsPerPixel := 8;
  2376. fFormat := tfR3G3B2ub1;
  2377. fWithAlpha := tfRGBA4us1;
  2378. fWithoutAlpha := tfR3G3B2ub1;
  2379. fRGBInverted := tfEmpty;
  2380. fPrecision := glBitmapRec4ub(3, 3, 2, 0);
  2381. fShift := glBitmapRec4ub(5, 2, 0, 0);
  2382. {$IFNDEF OPENGL_ES}
  2383. fOpenGLFormat := tfR3G3B2ub1;
  2384. fglFormat := GL_RGB;
  2385. fglInternalFormat := GL_R3_G3_B2;
  2386. fglDataFormat := GL_UNSIGNED_BYTE_3_3_2;
  2387. {$ELSE}
  2388. fOpenGLFormat := tfR5G6B5us1;
  2389. {$ENDIF}
  2390. end;
  2391. procedure TfdRGBX4us1.SetValues;
  2392. begin
  2393. inherited SetValues;
  2394. fBitsPerPixel := 16;
  2395. fFormat := tfRGBX4us1;
  2396. fWithAlpha := tfRGBA4us1;
  2397. fWithoutAlpha := tfRGBX4us1;
  2398. fRGBInverted := tfBGRX4us1;
  2399. fPrecision := glBitmapRec4ub( 4, 4, 4, 0);
  2400. fShift := glBitmapRec4ub(12, 8, 4, 0);
  2401. {$IFNDEF OPENGL_ES}
  2402. fOpenGLFormat := tfRGBX4us1;
  2403. fglFormat := GL_RGBA; //GL_INVALID_OPERATION if not GL_BGRA or GL_RGBA
  2404. fglInternalFormat := GL_RGB4;
  2405. fglDataFormat := GL_UNSIGNED_SHORT_4_4_4_4;
  2406. {$ELSE}
  2407. fOpenGLFormat := tfR5G6B5us1;
  2408. {$ENDIF}
  2409. end;
  2410. procedure TfdXRGB4us1.SetValues;
  2411. begin
  2412. inherited SetValues;
  2413. fBitsPerPixel := 16;
  2414. fFormat := tfXRGB4us1;
  2415. fWithAlpha := tfARGB4us1;
  2416. fWithoutAlpha := tfXRGB4us1;
  2417. fRGBInverted := tfXBGR4us1;
  2418. fPrecision := glBitmapRec4ub(4, 4, 4, 0);
  2419. fShift := glBitmapRec4ub(8, 4, 0, 0);
  2420. {$IFNDEF OPENGL_ES}
  2421. fOpenGLFormat := tfXRGB4us1;
  2422. fglFormat := GL_BGRA;
  2423. fglInternalFormat := GL_RGB4;
  2424. fglDataFormat := GL_UNSIGNED_SHORT_4_4_4_4_REV;
  2425. {$ELSE}
  2426. fOpenGLFormat := tfR5G6B5us1;
  2427. {$ENDIF}
  2428. end;
  2429. procedure TfdR5G6B5us1.SetValues;
  2430. begin
  2431. inherited SetValues;
  2432. fBitsPerPixel := 16;
  2433. fFormat := tfR5G6B5us1;
  2434. fWithAlpha := tfRGB5A1us1;
  2435. fWithoutAlpha := tfR5G6B5us1;
  2436. fRGBInverted := tfB5G6R5us1;
  2437. fPrecision := glBitmapRec4ub( 5, 6, 5, 0);
  2438. fShift := glBitmapRec4ub(11, 5, 0, 0);
  2439. {$IF NOT DEFINED(OPENGL_ES) OR DEFINED(OPENGL_ES_2_0)}
  2440. fOpenGLFormat := tfR5G6B5us1;
  2441. fglFormat := GL_RGB;
  2442. fglInternalFormat := GL_RGB565;
  2443. fglDataFormat := GL_UNSIGNED_SHORT_5_6_5;
  2444. {$ELSE}
  2445. fOpenGLFormat := tfRGB8ub3;
  2446. {$IFEND}
  2447. end;
  2448. procedure TfdRGB5X1us1.SetValues;
  2449. begin
  2450. inherited SetValues;
  2451. fBitsPerPixel := 16;
  2452. fFormat := tfRGB5X1us1;
  2453. fWithAlpha := tfRGB5A1us1;
  2454. fWithoutAlpha := tfRGB5X1us1;
  2455. fRGBInverted := tfBGR5X1us1;
  2456. fPrecision := glBitmapRec4ub( 5, 5, 5, 0);
  2457. fShift := glBitmapRec4ub(11, 6, 1, 0);
  2458. {$IFNDEF OPENGL_ES}
  2459. fOpenGLFormat := tfRGB5X1us1;
  2460. fglFormat := GL_RGBA; //GL_INVALID_OPERATION if not GL_BGRA or GL_RGBA
  2461. fglInternalFormat := GL_RGB5;
  2462. fglDataFormat := GL_UNSIGNED_SHORT_5_5_5_1;
  2463. {$ELSE}
  2464. fOpenGLFormat := tfR5G6B5us1;
  2465. {$ENDIF}
  2466. end;
  2467. procedure TfdX1RGB5us1.SetValues;
  2468. begin
  2469. inherited SetValues;
  2470. fBitsPerPixel := 16;
  2471. fFormat := tfX1RGB5us1;
  2472. fWithAlpha := tfA1RGB5us1;
  2473. fWithoutAlpha := tfX1RGB5us1;
  2474. fRGBInverted := tfX1BGR5us1;
  2475. fPrecision := glBitmapRec4ub( 5, 5, 5, 0);
  2476. fShift := glBitmapRec4ub(10, 5, 0, 0);
  2477. {$IFNDEF OPENGL_ES}
  2478. fOpenGLFormat := tfX1RGB5us1;
  2479. fglFormat := GL_BGRA; //GL_INVALID_OPERATION if not GL_BGRA or GL_RGBA
  2480. fglInternalFormat := GL_RGB5;
  2481. fglDataFormat := GL_UNSIGNED_SHORT_1_5_5_5_REV;
  2482. {$ELSE}
  2483. fOpenGLFormat := tfR5G6B5us1;
  2484. {$ENDIF}
  2485. end;
  2486. procedure TfdRGB8ub3.SetValues;
  2487. begin
  2488. inherited SetValues;
  2489. fBitsPerPixel := 24;
  2490. fFormat := tfRGB8ub3;
  2491. fWithAlpha := tfRGBA8ub4;
  2492. fWithoutAlpha := tfRGB8ub3;
  2493. fRGBInverted := tfBGR8ub3;
  2494. fPrecision := glBitmapRec4ub(8, 8, 8, 0);
  2495. fShift := glBitmapRec4ub(0, 8, 16, 0);
  2496. fOpenGLFormat := tfRGB8ub3;
  2497. fglFormat := GL_RGB;
  2498. fglInternalFormat := {$IF NOT DEFINED(OPENGL_ES) OR DEFINED(OPENGL_ES_3_0)}GL_RGB8{$ELSE}GL_RGB{$IFEND};
  2499. fglDataFormat := GL_UNSIGNED_BYTE;
  2500. end;
  2501. procedure TfdRGBX8ui1.SetValues;
  2502. begin
  2503. inherited SetValues;
  2504. fBitsPerPixel := 32;
  2505. fFormat := tfRGBX8ui1;
  2506. fWithAlpha := tfRGBA8ui1;
  2507. fWithoutAlpha := tfRGBX8ui1;
  2508. fRGBInverted := tfBGRX8ui1;
  2509. fPrecision := glBitmapRec4ub( 8, 8, 8, 0);
  2510. fShift := glBitmapRec4ub(24, 16, 8, 0);
  2511. {$IFNDEF OPENGL_ES}
  2512. fOpenGLFormat := tfRGBX8ui1;
  2513. fglFormat := GL_RGBA; //GL_INVALID_OPERATION if not GL_BGRA or GL_RGBA
  2514. fglInternalFormat := GL_RGB8;
  2515. fglDataFormat := GL_UNSIGNED_INT_8_8_8_8;
  2516. {$ELSE}
  2517. fOpenGLFormat := tfRGB8ub3;
  2518. {$ENDIF}
  2519. end;
  2520. procedure TfdXRGB8ui1.SetValues;
  2521. begin
  2522. inherited SetValues;
  2523. fBitsPerPixel := 32;
  2524. fFormat := tfXRGB8ui1;
  2525. fWithAlpha := tfXRGB8ui1;
  2526. fWithoutAlpha := tfXRGB8ui1;
  2527. fOpenGLFormat := tfXRGB8ui1;
  2528. fRGBInverted := tfXBGR8ui1;
  2529. fPrecision := glBitmapRec4ub( 8, 8, 8, 0);
  2530. fShift := glBitmapRec4ub(16, 8, 0, 0);
  2531. {$IFNDEF OPENGL_ES}
  2532. fOpenGLFormat := tfXRGB8ui1;
  2533. fglFormat := GL_BGRA; //GL_INVALID_OPERATION if not GL_BGRA or GL_RGBA
  2534. fglInternalFormat := GL_RGB8;
  2535. fglDataFormat := GL_UNSIGNED_INT_8_8_8_8_REV;
  2536. {$ELSE}
  2537. fOpenGLFormat := tfRGB8ub3;
  2538. {$ENDIF}
  2539. end;
  2540. procedure TfdRGB10X2ui1.SetValues;
  2541. begin
  2542. inherited SetValues;
  2543. fBitsPerPixel := 32;
  2544. fFormat := tfRGB10X2ui1;
  2545. fWithAlpha := tfRGB10A2ui1;
  2546. fWithoutAlpha := tfRGB10X2ui1;
  2547. fRGBInverted := tfBGR10X2ui1;
  2548. fPrecision := glBitmapRec4ub(10, 10, 10, 0);
  2549. fShift := glBitmapRec4ub(22, 12, 2, 0);
  2550. {$IFNDEF OPENGL_ES}
  2551. fOpenGLFormat := tfRGB10X2ui1;
  2552. fglFormat := GL_RGBA; //GL_INVALID_OPERATION if not GL_BGRA or GL_RGBA
  2553. fglInternalFormat := GL_RGB10;
  2554. fglDataFormat := GL_UNSIGNED_INT_10_10_10_2;
  2555. {$ELSE}
  2556. fOpenGLFormat := tfRGB16us3;
  2557. {$ENDIF}
  2558. end;
  2559. procedure TfdX2RGB10ui1.SetValues;
  2560. begin
  2561. inherited SetValues;
  2562. fBitsPerPixel := 32;
  2563. fFormat := tfX2RGB10ui1;
  2564. fWithAlpha := tfA2RGB10ui1;
  2565. fWithoutAlpha := tfX2RGB10ui1;
  2566. fRGBInverted := tfX2BGR10ui1;
  2567. fPrecision := glBitmapRec4ub(10, 10, 10, 0);
  2568. fShift := glBitmapRec4ub(20, 10, 0, 0);
  2569. {$IFNDEF OPENGL_ES}
  2570. fOpenGLFormat := tfX2RGB10ui1;
  2571. fglFormat := GL_BGRA; //GL_INVALID_OPERATION if not GL_BGRA or GL_RGBA
  2572. fglInternalFormat := GL_RGB10;
  2573. fglDataFormat := GL_UNSIGNED_INT_2_10_10_10_REV;
  2574. {$ELSE}
  2575. fOpenGLFormat := tfRGB16us3;
  2576. {$ENDIF}
  2577. end;
  2578. procedure TfdRGB16us3.SetValues;
  2579. begin
  2580. inherited SetValues;
  2581. fBitsPerPixel := 48;
  2582. fFormat := tfRGB16us3;
  2583. fWithAlpha := tfRGBA16us4;
  2584. fWithoutAlpha := tfRGB16us3;
  2585. fRGBInverted := tfBGR16us3;
  2586. fPrecision := glBitmapRec4ub(16, 16, 16, 0);
  2587. fShift := glBitmapRec4ub( 0, 16, 32, 0);
  2588. {$IF NOT DEFINED(OPENGL_ES) OR DEFINED(OPENGL_ES_3_0)}
  2589. fOpenGLFormat := tfRGB16us3;
  2590. fglFormat := GL_RGB;
  2591. fglInternalFormat := {$IFNDEF OPENGL_ES}GL_RGB16{$ELSE}GL_RGB16UI{$ENDIF};
  2592. fglDataFormat := GL_UNSIGNED_SHORT;
  2593. {$ELSE}
  2594. fOpenGLFormat := tfRGB8ub3;
  2595. {$IFEND}
  2596. end;
  2597. procedure TfdRGBA4us1.SetValues;
  2598. begin
  2599. inherited SetValues;
  2600. fBitsPerPixel := 16;
  2601. fFormat := tfRGBA4us1;
  2602. fWithAlpha := tfRGBA4us1;
  2603. fWithoutAlpha := tfRGBX4us1;
  2604. fOpenGLFormat := tfRGBA4us1;
  2605. fRGBInverted := tfBGRA4us1;
  2606. fPrecision := glBitmapRec4ub( 4, 4, 4, 4);
  2607. fShift := glBitmapRec4ub(12, 8, 4, 0);
  2608. fglFormat := GL_RGBA;
  2609. fglInternalFormat := {$IF NOT DEFINED(OPENGL_ES) OR DEFINED(OPENGL_ES_3_0)}GL_RGBA4{$ELSE}GL_RGBA{$IFEND};
  2610. fglDataFormat := GL_UNSIGNED_SHORT_4_4_4_4;
  2611. end;
  2612. procedure TfdARGB4us1.SetValues;
  2613. begin
  2614. inherited SetValues;
  2615. fBitsPerPixel := 16;
  2616. fFormat := tfARGB4us1;
  2617. fWithAlpha := tfARGB4us1;
  2618. fWithoutAlpha := tfXRGB4us1;
  2619. fRGBInverted := tfABGR4us1;
  2620. fPrecision := glBitmapRec4ub( 4, 4, 4, 4);
  2621. fShift := glBitmapRec4ub( 8, 4, 0, 12);
  2622. {$IFNDEF OPENGL_ES}
  2623. fOpenGLFormat := tfARGB4us1;
  2624. fglFormat := GL_BGRA;
  2625. fglInternalFormat := GL_RGBA4;
  2626. fglDataFormat := GL_UNSIGNED_SHORT_4_4_4_4_REV;
  2627. {$ELSE}
  2628. fOpenGLFormat := tfRGBA4us1;
  2629. {$ENDIF}
  2630. end;
  2631. procedure TfdRGB5A1us1.SetValues;
  2632. begin
  2633. inherited SetValues;
  2634. fBitsPerPixel := 16;
  2635. fFormat := tfRGB5A1us1;
  2636. fWithAlpha := tfRGB5A1us1;
  2637. fWithoutAlpha := tfRGB5X1us1;
  2638. fOpenGLFormat := tfRGB5A1us1;
  2639. fRGBInverted := tfBGR5A1us1;
  2640. fPrecision := glBitmapRec4ub( 5, 5, 5, 1);
  2641. fShift := glBitmapRec4ub(11, 6, 1, 0);
  2642. fglFormat := GL_RGBA;
  2643. fglInternalFormat := {$IF NOT DEFINED(OPENGL_ES) OR DEFINED(OPENGL_ES_2_0)}GL_RGB5_A1{$ELSE}GL_RGBA{$IFEND};
  2644. fglDataFormat := GL_UNSIGNED_SHORT_5_5_5_1;
  2645. end;
  2646. procedure TfdA1RGB5us1.SetValues;
  2647. begin
  2648. inherited SetValues;
  2649. fBitsPerPixel := 16;
  2650. fFormat := tfA1RGB5us1;
  2651. fWithAlpha := tfA1RGB5us1;
  2652. fWithoutAlpha := tfX1RGB5us1;
  2653. fRGBInverted := tfA1BGR5us1;
  2654. fPrecision := glBitmapRec4ub( 5, 5, 5, 1);
  2655. fShift := glBitmapRec4ub(10, 5, 0, 15);
  2656. {$IFNDEF OPENGL_ES}
  2657. fOpenGLFormat := tfA1RGB5us1;
  2658. fglFormat := GL_BGRA;
  2659. fglInternalFormat := GL_RGB5_A1;
  2660. fglDataFormat := GL_UNSIGNED_SHORT_1_5_5_5_REV;
  2661. {$ELSE}
  2662. fOpenGLFormat := tfRGB5A1us1;
  2663. {$ENDIF}
  2664. end;
  2665. procedure TfdRGBA8ui1.SetValues;
  2666. begin
  2667. inherited SetValues;
  2668. fBitsPerPixel := 32;
  2669. fFormat := tfRGBA8ui1;
  2670. fWithAlpha := tfRGBA8ui1;
  2671. fWithoutAlpha := tfRGBX8ui1;
  2672. fRGBInverted := tfBGRA8ui1;
  2673. fPrecision := glBitmapRec4ub( 8, 8, 8, 8);
  2674. fShift := glBitmapRec4ub(24, 16, 8, 0);
  2675. {$IFNDEF OPENGL_ES}
  2676. fOpenGLFormat := tfRGBA8ui1;
  2677. fglFormat := GL_RGBA;
  2678. fglInternalFormat := GL_RGBA8;
  2679. fglDataFormat := GL_UNSIGNED_INT_8_8_8_8;
  2680. {$ELSE}
  2681. fOpenGLFormat := tfRGBA8ub4;
  2682. {$ENDIF}
  2683. end;
  2684. procedure TfdARGB8ui1.SetValues;
  2685. begin
  2686. inherited SetValues;
  2687. fBitsPerPixel := 32;
  2688. fFormat := tfARGB8ui1;
  2689. fWithAlpha := tfARGB8ui1;
  2690. fWithoutAlpha := tfXRGB8ui1;
  2691. fRGBInverted := tfABGR8ui1;
  2692. fPrecision := glBitmapRec4ub( 8, 8, 8, 8);
  2693. fShift := glBitmapRec4ub(16, 8, 0, 24);
  2694. {$IFNDEF OPENGL_ES}
  2695. fOpenGLFormat := tfARGB8ui1;
  2696. fglFormat := GL_BGRA;
  2697. fglInternalFormat := GL_RGBA8;
  2698. fglDataFormat := GL_UNSIGNED_INT_8_8_8_8_REV;
  2699. {$ELSE}
  2700. fOpenGLFormat := tfRGBA8ub4;
  2701. {$ENDIF}
  2702. end;
  2703. procedure TfdRGBA8ub4.SetValues;
  2704. begin
  2705. inherited SetValues;
  2706. fBitsPerPixel := 32;
  2707. fFormat := tfRGBA8ub4;
  2708. fWithAlpha := tfRGBA8ub4;
  2709. fWithoutAlpha := tfRGB8ub3;
  2710. fOpenGLFormat := tfRGBA8ub4;
  2711. fRGBInverted := tfBGRA8ub4;
  2712. fPrecision := glBitmapRec4ub( 8, 8, 8, 8);
  2713. fShift := glBitmapRec4ub( 0, 8, 16, 24);
  2714. fglFormat := GL_RGBA;
  2715. fglInternalFormat := {$IF NOT DEFINED(OPENGL_ES) OR DEFINED(OPENGL_ES_3_0)}GL_RGBA8{$ELSE}GL_RGBA{$IFEND};
  2716. fglDataFormat := GL_UNSIGNED_BYTE;
  2717. end;
  2718. procedure TfdRGB10A2ui1.SetValues;
  2719. begin
  2720. inherited SetValues;
  2721. fBitsPerPixel := 32;
  2722. fFormat := tfRGB10A2ui1;
  2723. fWithAlpha := tfRGB10A2ui1;
  2724. fWithoutAlpha := tfRGB10X2ui1;
  2725. fRGBInverted := tfBGR10A2ui1;
  2726. fPrecision := glBitmapRec4ub(10, 10, 10, 2);
  2727. fShift := glBitmapRec4ub(22, 12, 2, 0);
  2728. {$IFNDEF OPENGL_ES}
  2729. fOpenGLFormat := tfRGB10A2ui1;
  2730. fglFormat := GL_RGBA;
  2731. fglInternalFormat := GL_RGB10_A2;
  2732. fglDataFormat := GL_UNSIGNED_INT_10_10_10_2;
  2733. {$ELSE}
  2734. fOpenGLFormat := tfA2RGB10ui1;
  2735. {$ENDIF}
  2736. end;
  2737. procedure TfdA2RGB10ui1.SetValues;
  2738. begin
  2739. inherited SetValues;
  2740. fBitsPerPixel := 32;
  2741. fFormat := tfA2RGB10ui1;
  2742. fWithAlpha := tfA2RGB10ui1;
  2743. fWithoutAlpha := tfX2RGB10ui1;
  2744. fRGBInverted := tfA2BGR10ui1;
  2745. fPrecision := glBitmapRec4ub(10, 10, 10, 2);
  2746. fShift := glBitmapRec4ub(20, 10, 0, 30);
  2747. {$IF NOT DEFINED(OPENGL_ES)}
  2748. fOpenGLFormat := tfA2RGB10ui1;
  2749. fglFormat := GL_BGRA;
  2750. fglInternalFormat := GL_RGB10_A2;
  2751. fglDataFormat := GL_UNSIGNED_INT_2_10_10_10_REV;
  2752. {$ELSEIF DEFINED(OPENGL_ES_3_0)}
  2753. fOpenGLFormat := tfA2RGB10ui1;
  2754. fglFormat := GL_RGBA;
  2755. fglInternalFormat := GL_RGB10_A2;
  2756. fglDataFormat := GL_UNSIGNED_INT_2_10_10_10_REV;
  2757. {$ELSE}
  2758. fOpenGLFormat := tfRGBA8ui1;
  2759. {$IFEND}
  2760. end;
  2761. procedure TfdRGBA16us4.SetValues;
  2762. begin
  2763. inherited SetValues;
  2764. fBitsPerPixel := 64;
  2765. fFormat := tfRGBA16us4;
  2766. fWithAlpha := tfRGBA16us4;
  2767. fWithoutAlpha := tfRGB16us3;
  2768. fRGBInverted := tfBGRA16us4;
  2769. fPrecision := glBitmapRec4ub(16, 16, 16, 16);
  2770. fShift := glBitmapRec4ub( 0, 16, 32, 48);
  2771. {$IF NOT DEFINED(OPENGL_ES) OR DEFINED(OPENGL_ES_3_0)}
  2772. fOpenGLFormat := tfRGBA16us4;
  2773. fglFormat := GL_RGBA;
  2774. fglInternalFormat := {$IFNDEF OPENGL_ES}GL_RGBA16{$ELSE}GL_RGBA16UI{$ENDIF};
  2775. fglDataFormat := GL_UNSIGNED_SHORT;
  2776. {$ELSE}
  2777. fOpenGLFormat := tfRGBA8ub4;
  2778. {$IFEND}
  2779. end;
  2780. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2781. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2782. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2783. procedure TfdBGRX4us1.SetValues;
  2784. begin
  2785. inherited SetValues;
  2786. fBitsPerPixel := 16;
  2787. fFormat := tfBGRX4us1;
  2788. fWithAlpha := tfBGRA4us1;
  2789. fWithoutAlpha := tfBGRX4us1;
  2790. fRGBInverted := tfRGBX4us1;
  2791. fPrecision := glBitmapRec4ub( 4, 4, 4, 0);
  2792. fShift := glBitmapRec4ub( 4, 8, 12, 0);
  2793. {$IFNDEF OPENGL_ES}
  2794. fOpenGLFormat := tfBGRX4us1;
  2795. fglFormat := GL_BGRA; //GL_INVALID_OPERATION if not GL_BGRA or GL_RGBA
  2796. fglInternalFormat := GL_RGB4;
  2797. fglDataFormat := GL_UNSIGNED_SHORT_4_4_4_4;
  2798. {$ELSE}
  2799. fOpenGLFormat := tfR5G6B5us1;
  2800. {$ENDIF}
  2801. end;
  2802. procedure TfdXBGR4us1.SetValues;
  2803. begin
  2804. inherited SetValues;
  2805. fBitsPerPixel := 16;
  2806. fFormat := tfXBGR4us1;
  2807. fWithAlpha := tfABGR4us1;
  2808. fWithoutAlpha := tfXBGR4us1;
  2809. fRGBInverted := tfXRGB4us1;
  2810. fPrecision := glBitmapRec4ub( 4, 4, 4, 0);
  2811. fShift := glBitmapRec4ub( 0, 4, 8, 0);
  2812. {$IFNDEF OPENGL_ES}
  2813. fOpenGLFormat := tfXBGR4us1;
  2814. fglFormat := GL_RGBA; //GL_INVALID_OPERATION if not GL_BGRA or GL_RGBA
  2815. fglInternalFormat := GL_RGB4;
  2816. fglDataFormat := GL_UNSIGNED_SHORT_4_4_4_4_REV;
  2817. {$ELSE}
  2818. fOpenGLFormat := tfR5G6B5us1;
  2819. {$ENDIF}
  2820. end;
  2821. procedure TfdB5G6R5us1.SetValues;
  2822. begin
  2823. inherited SetValues;
  2824. fBitsPerPixel := 16;
  2825. fFormat := tfB5G6R5us1;
  2826. fWithAlpha := tfBGR5A1us1;
  2827. fWithoutAlpha := tfB5G6R5us1;
  2828. fRGBInverted := tfR5G6B5us1;
  2829. fPrecision := glBitmapRec4ub( 5, 6, 5, 0);
  2830. fShift := glBitmapRec4ub( 0, 5, 11, 0);
  2831. {$IFNDEF OPENGL_ES}
  2832. fOpenGLFormat := tfB5G6R5us1;
  2833. fglFormat := GL_RGB;
  2834. fglInternalFormat := GL_RGB565;
  2835. fglDataFormat := GL_UNSIGNED_SHORT_5_6_5_REV;
  2836. {$ELSE}
  2837. fOpenGLFormat := tfR5G6B5us1;
  2838. {$ENDIF}
  2839. end;
  2840. procedure TfdBGR5X1us1.SetValues;
  2841. begin
  2842. inherited SetValues;
  2843. fBitsPerPixel := 16;
  2844. fFormat := tfBGR5X1us1;
  2845. fWithAlpha := tfBGR5A1us1;
  2846. fWithoutAlpha := tfBGR5X1us1;
  2847. fRGBInverted := tfRGB5X1us1;
  2848. fPrecision := glBitmapRec4ub( 5, 5, 5, 0);
  2849. fShift := glBitmapRec4ub( 1, 6, 11, 0);
  2850. {$IFNDEF OPENGL_ES}
  2851. fOpenGLFormat := tfBGR5X1us1;
  2852. fglFormat := GL_BGRA;
  2853. fglInternalFormat := GL_RGB5;
  2854. fglDataFormat := GL_UNSIGNED_SHORT_5_5_5_1;
  2855. {$ELSE}
  2856. fOpenGLFormat := tfR5G6B5us1;
  2857. {$ENDIF}
  2858. end;
  2859. procedure TfdX1BGR5us1.SetValues;
  2860. begin
  2861. inherited SetValues;
  2862. fBitsPerPixel := 16;
  2863. fFormat := tfX1BGR5us1;
  2864. fWithAlpha := tfA1BGR5us1;
  2865. fWithoutAlpha := tfX1BGR5us1;
  2866. fRGBInverted := tfX1RGB5us1;
  2867. fPrecision := glBitmapRec4ub( 5, 5, 5, 0);
  2868. fShift := glBitmapRec4ub( 0, 5, 10, 0);
  2869. {$IFNDEF OPENGL_ES}
  2870. fOpenGLFormat := tfX1BGR5us1;
  2871. fglFormat := GL_RGBA; //GL_INVALID_OPERATION if not GL_BGRA or GL_RGBA
  2872. fglInternalFormat := GL_RGB5;
  2873. fglDataFormat := GL_UNSIGNED_SHORT_1_5_5_5_REV;
  2874. {$ELSE}
  2875. fOpenGLFormat := tfR5G6B5us1;
  2876. {$ENDIF}
  2877. end;
  2878. procedure TfdBGR8ub3.SetValues;
  2879. begin
  2880. inherited SetValues;
  2881. fBitsPerPixel := 24;
  2882. fFormat := tfBGR8ub3;
  2883. fWithAlpha := tfBGRA8ub4;
  2884. fWithoutAlpha := tfBGR8ub3;
  2885. fRGBInverted := tfRGB8ub3;
  2886. fPrecision := glBitmapRec4ub( 8, 8, 8, 0);
  2887. fShift := glBitmapRec4ub(16, 8, 0, 0);
  2888. {$IFNDEF OPENGL_ES}
  2889. fOpenGLFormat := tfBGR8ub3;
  2890. fglFormat := GL_BGR;
  2891. fglInternalFormat := GL_RGB8;
  2892. fglDataFormat := GL_UNSIGNED_BYTE;
  2893. {$ELSE}
  2894. fOpenGLFormat := tfRGB8ub3;
  2895. {$ENDIF}
  2896. end;
  2897. procedure TfdBGRX8ui1.SetValues;
  2898. begin
  2899. inherited SetValues;
  2900. fBitsPerPixel := 32;
  2901. fFormat := tfBGRX8ui1;
  2902. fWithAlpha := tfBGRA8ui1;
  2903. fWithoutAlpha := tfBGRX8ui1;
  2904. fRGBInverted := tfRGBX8ui1;
  2905. fPrecision := glBitmapRec4ub( 8, 8, 8, 0);
  2906. fShift := glBitmapRec4ub( 8, 16, 24, 0);
  2907. {$IFNDEF OPENGL_ES}
  2908. fOpenGLFormat := tfBGRX8ui1;
  2909. fglFormat := GL_BGRA; //GL_INVALID_OPERATION if not GL_BGRA or GL_RGBA
  2910. fglInternalFormat := GL_RGB8;
  2911. fglDataFormat := GL_UNSIGNED_INT_8_8_8_8;
  2912. {$ELSE}
  2913. fOpenGLFormat := tfRGB8ub3;
  2914. {$ENDIF}
  2915. end;
  2916. procedure TfdXBGR8ui1.SetValues;
  2917. begin
  2918. inherited SetValues;
  2919. fBitsPerPixel := 32;
  2920. fFormat := tfXBGR8ui1;
  2921. fWithAlpha := tfABGR8ui1;
  2922. fWithoutAlpha := tfXBGR8ui1;
  2923. fRGBInverted := tfXRGB8ui1;
  2924. fPrecision := glBitmapRec4ub( 8, 8, 8, 0);
  2925. fShift := glBitmapRec4ub( 0, 8, 16, 0);
  2926. {$IFNDEF OPENGL_ES}
  2927. fOpenGLFormat := tfXBGR8ui1;
  2928. fglFormat := GL_RGBA; //GL_INVALID_OPERATION if not GL_BGRA or GL_RGBA
  2929. fglInternalFormat := GL_RGB8;
  2930. fglDataFormat := GL_UNSIGNED_INT_8_8_8_8_REV;
  2931. {$ELSE}
  2932. fOpenGLFormat := tfRGB8ub3;
  2933. {$ENDIF}
  2934. end;
  2935. procedure TfdBGR10X2ui1.SetValues;
  2936. begin
  2937. inherited SetValues;
  2938. fBitsPerPixel := 32;
  2939. fFormat := tfBGR10X2ui1;
  2940. fWithAlpha := tfBGR10A2ui1;
  2941. fWithoutAlpha := tfBGR10X2ui1;
  2942. fRGBInverted := tfRGB10X2ui1;
  2943. fPrecision := glBitmapRec4ub(10, 10, 10, 0);
  2944. fShift := glBitmapRec4ub( 2, 12, 22, 0);
  2945. {$IFNDEF OPENGL_ES}
  2946. fOpenGLFormat := tfBGR10X2ui1;
  2947. fglFormat := GL_BGRA; //GL_INVALID_OPERATION if not GL_BGRA or GL_RGBA
  2948. fglInternalFormat := GL_RGB10;
  2949. fglDataFormat := GL_UNSIGNED_INT_10_10_10_2;
  2950. {$ELSE}
  2951. fOpenGLFormat := tfRGB16us3;
  2952. {$ENDIF}
  2953. end;
  2954. procedure TfdX2BGR10ui1.SetValues;
  2955. begin
  2956. inherited SetValues;
  2957. fBitsPerPixel := 32;
  2958. fFormat := tfX2BGR10ui1;
  2959. fWithAlpha := tfA2BGR10ui1;
  2960. fWithoutAlpha := tfX2BGR10ui1;
  2961. fRGBInverted := tfX2RGB10ui1;
  2962. fPrecision := glBitmapRec4ub(10, 10, 10, 0);
  2963. fShift := glBitmapRec4ub( 0, 10, 20, 0);
  2964. {$IFNDEF OPENGL_ES}
  2965. fOpenGLFormat := tfX2BGR10ui1;
  2966. fglFormat := GL_RGBA; //GL_INVALID_OPERATION if not GL_BGRA or GL_RGBA
  2967. fglInternalFormat := GL_RGB10;
  2968. fglDataFormat := GL_UNSIGNED_INT_2_10_10_10_REV;
  2969. {$ELSE}
  2970. fOpenGLFormat := tfRGB16us3;
  2971. {$ENDIF}
  2972. end;
  2973. procedure TfdBGR16us3.SetValues;
  2974. begin
  2975. inherited SetValues;
  2976. fBitsPerPixel := 48;
  2977. fFormat := tfBGR16us3;
  2978. fWithAlpha := tfBGRA16us4;
  2979. fWithoutAlpha := tfBGR16us3;
  2980. fRGBInverted := tfRGB16us3;
  2981. fPrecision := glBitmapRec4ub(16, 16, 16, 0);
  2982. fShift := glBitmapRec4ub(32, 16, 0, 0);
  2983. {$IFNDEF OPENGL_ES}
  2984. fOpenGLFormat := tfBGR16us3;
  2985. fglFormat := GL_BGR;
  2986. fglInternalFormat := GL_RGB16;
  2987. fglDataFormat := GL_UNSIGNED_SHORT;
  2988. {$ELSE}
  2989. fOpenGLFormat := tfRGB16us3;
  2990. {$ENDIF}
  2991. end;
  2992. procedure TfdBGRA4us1.SetValues;
  2993. begin
  2994. inherited SetValues;
  2995. fBitsPerPixel := 16;
  2996. fFormat := tfBGRA4us1;
  2997. fWithAlpha := tfBGRA4us1;
  2998. fWithoutAlpha := tfBGRX4us1;
  2999. fRGBInverted := tfRGBA4us1;
  3000. fPrecision := glBitmapRec4ub( 4, 4, 4, 4);
  3001. fShift := glBitmapRec4ub( 4, 8, 12, 0);
  3002. {$IFNDEF OPENGL_ES}
  3003. fOpenGLFormat := tfBGRA4us1;
  3004. fglFormat := GL_BGRA;
  3005. fglInternalFormat := GL_RGBA4;
  3006. fglDataFormat := GL_UNSIGNED_SHORT_4_4_4_4;
  3007. {$ELSE}
  3008. fOpenGLFormat := tfRGBA4us1;
  3009. {$ENDIF}
  3010. end;
  3011. procedure TfdABGR4us1.SetValues;
  3012. begin
  3013. inherited SetValues;
  3014. fBitsPerPixel := 16;
  3015. fFormat := tfABGR4us1;
  3016. fWithAlpha := tfABGR4us1;
  3017. fWithoutAlpha := tfXBGR4us1;
  3018. fRGBInverted := tfARGB4us1;
  3019. fPrecision := glBitmapRec4ub( 4, 4, 4, 4);
  3020. fShift := glBitmapRec4ub( 0, 4, 8, 12);
  3021. {$IFNDEF OPENGL_ES}
  3022. fOpenGLFormat := tfABGR4us1;
  3023. fglFormat := GL_RGBA;
  3024. fglInternalFormat := GL_RGBA4;
  3025. fglDataFormat := GL_UNSIGNED_SHORT_4_4_4_4_REV;
  3026. {$ELSE}
  3027. fOpenGLFormat := tfRGBA4us1;
  3028. {$ENDIF}
  3029. end;
  3030. procedure TfdBGR5A1us1.SetValues;
  3031. begin
  3032. inherited SetValues;
  3033. fBitsPerPixel := 16;
  3034. fFormat := tfBGR5A1us1;
  3035. fWithAlpha := tfBGR5A1us1;
  3036. fWithoutAlpha := tfBGR5X1us1;
  3037. fRGBInverted := tfRGB5A1us1;
  3038. fPrecision := glBitmapRec4ub( 5, 5, 5, 1);
  3039. fShift := glBitmapRec4ub( 1, 6, 11, 0);
  3040. {$IFNDEF OPENGL_ES}
  3041. fOpenGLFormat := tfBGR5A1us1;
  3042. fglFormat := GL_BGRA;
  3043. fglInternalFormat := GL_RGB5_A1;
  3044. fglDataFormat := GL_UNSIGNED_SHORT_5_5_5_1;
  3045. {$ELSE}
  3046. fOpenGLFormat := tfRGB5A1us1;
  3047. {$ENDIF}
  3048. end;
  3049. procedure TfdA1BGR5us1.SetValues;
  3050. begin
  3051. inherited SetValues;
  3052. fBitsPerPixel := 16;
  3053. fFormat := tfA1BGR5us1;
  3054. fWithAlpha := tfA1BGR5us1;
  3055. fWithoutAlpha := tfX1BGR5us1;
  3056. fRGBInverted := tfA1RGB5us1;
  3057. fPrecision := glBitmapRec4ub( 5, 5, 5, 1);
  3058. fShift := glBitmapRec4ub( 0, 5, 10, 15);
  3059. {$IFNDEF OPENGL_ES}
  3060. fOpenGLFormat := tfA1BGR5us1;
  3061. fglFormat := GL_RGBA;
  3062. fglInternalFormat := GL_RGB5_A1;
  3063. fglDataFormat := GL_UNSIGNED_SHORT_1_5_5_5_REV;
  3064. {$ELSE}
  3065. fOpenGLFormat := tfRGB5A1us1;
  3066. {$ENDIF}
  3067. end;
  3068. procedure TfdBGRA8ui1.SetValues;
  3069. begin
  3070. inherited SetValues;
  3071. fBitsPerPixel := 32;
  3072. fFormat := tfBGRA8ui1;
  3073. fWithAlpha := tfBGRA8ui1;
  3074. fWithoutAlpha := tfBGRX8ui1;
  3075. fRGBInverted := tfRGBA8ui1;
  3076. fPrecision := glBitmapRec4ub( 8, 8, 8, 8);
  3077. fShift := glBitmapRec4ub( 8, 16, 24, 0);
  3078. {$IFNDEF OPENGL_ES}
  3079. fOpenGLFormat := tfBGRA8ui1;
  3080. fglFormat := GL_BGRA;
  3081. fglInternalFormat := GL_RGBA8;
  3082. fglDataFormat := GL_UNSIGNED_INT_8_8_8_8;
  3083. {$ELSE}
  3084. fOpenGLFormat := tfRGBA8ub4;
  3085. {$ENDIF}
  3086. end;
  3087. procedure TfdABGR8ui1.SetValues;
  3088. begin
  3089. inherited SetValues;
  3090. fBitsPerPixel := 32;
  3091. fFormat := tfABGR8ui1;
  3092. fWithAlpha := tfABGR8ui1;
  3093. fWithoutAlpha := tfXBGR8ui1;
  3094. fRGBInverted := tfARGB8ui1;
  3095. fPrecision := glBitmapRec4ub( 8, 8, 8, 8);
  3096. fShift := glBitmapRec4ub( 0, 8, 16, 24);
  3097. {$IFNDEF OPENGL_ES}
  3098. fOpenGLFormat := tfABGR8ui1;
  3099. fglFormat := GL_RGBA;
  3100. fglInternalFormat := GL_RGBA8;
  3101. fglDataFormat := GL_UNSIGNED_INT_8_8_8_8_REV;
  3102. {$ELSE}
  3103. fOpenGLFormat := tfRGBA8ub4
  3104. {$ENDIF}
  3105. end;
  3106. procedure TfdBGRA8ub4.SetValues;
  3107. begin
  3108. inherited SetValues;
  3109. fBitsPerPixel := 32;
  3110. fFormat := tfBGRA8ub4;
  3111. fWithAlpha := tfBGRA8ub4;
  3112. fWithoutAlpha := tfBGR8ub3;
  3113. fRGBInverted := tfRGBA8ub4;
  3114. fPrecision := glBitmapRec4ub( 8, 8, 8, 8);
  3115. fShift := glBitmapRec4ub(16, 8, 0, 24);
  3116. {$IFNDEF OPENGL_ES}
  3117. fOpenGLFormat := tfBGRA8ub4;
  3118. fglFormat := GL_BGRA;
  3119. fglInternalFormat := GL_RGBA8;
  3120. fglDataFormat := GL_UNSIGNED_BYTE;
  3121. {$ELSE}
  3122. fOpenGLFormat := tfRGBA8ub4;
  3123. {$ENDIF}
  3124. end;
  3125. procedure TfdBGR10A2ui1.SetValues;
  3126. begin
  3127. inherited SetValues;
  3128. fBitsPerPixel := 32;
  3129. fFormat := tfBGR10A2ui1;
  3130. fWithAlpha := tfBGR10A2ui1;
  3131. fWithoutAlpha := tfBGR10X2ui1;
  3132. fRGBInverted := tfRGB10A2ui1;
  3133. fPrecision := glBitmapRec4ub(10, 10, 10, 2);
  3134. fShift := glBitmapRec4ub( 2, 12, 22, 0);
  3135. {$IFNDEF OPENGL_ES}
  3136. fOpenGLFormat := tfBGR10A2ui1;
  3137. fglFormat := GL_BGRA;
  3138. fglInternalFormat := GL_RGB10_A2;
  3139. fglDataFormat := GL_UNSIGNED_INT_10_10_10_2;
  3140. {$ELSE}
  3141. fOpenGLFormat := tfA2RGB10ui1;
  3142. {$ENDIF}
  3143. end;
  3144. procedure TfdA2BGR10ui1.SetValues;
  3145. begin
  3146. inherited SetValues;
  3147. fBitsPerPixel := 32;
  3148. fFormat := tfA2BGR10ui1;
  3149. fWithAlpha := tfA2BGR10ui1;
  3150. fWithoutAlpha := tfX2BGR10ui1;
  3151. fRGBInverted := tfA2RGB10ui1;
  3152. fPrecision := glBitmapRec4ub(10, 10, 10, 2);
  3153. fShift := glBitmapRec4ub( 0, 10, 20, 30);
  3154. {$IFNDEF OPENGL_ES}
  3155. fOpenGLFormat := tfA2BGR10ui1;
  3156. fglFormat := GL_RGBA;
  3157. fglInternalFormat := GL_RGB10_A2;
  3158. fglDataFormat := GL_UNSIGNED_INT_2_10_10_10_REV;
  3159. {$ELSE}
  3160. fOpenGLFormat := tfA2RGB10ui1;
  3161. {$ENDIF}
  3162. end;
  3163. procedure TfdBGRA16us4.SetValues;
  3164. begin
  3165. inherited SetValues;
  3166. fBitsPerPixel := 64;
  3167. fFormat := tfBGRA16us4;
  3168. fWithAlpha := tfBGRA16us4;
  3169. fWithoutAlpha := tfBGR16us3;
  3170. fRGBInverted := tfRGBA16us4;
  3171. fPrecision := glBitmapRec4ub(16, 16, 16, 16);
  3172. fShift := glBitmapRec4ub(32, 16, 0, 48);
  3173. {$IFNDEF OPENGL_ES}
  3174. fOpenGLFormat := tfBGRA16us4;
  3175. fglFormat := GL_BGRA;
  3176. fglInternalFormat := GL_RGBA16;
  3177. fglDataFormat := GL_UNSIGNED_SHORT;
  3178. {$ELSE}
  3179. fOpenGLFormat := tfRGBA16us4;
  3180. {$ENDIF}
  3181. end;
  3182. procedure TfdDepth16us1.SetValues;
  3183. begin
  3184. inherited SetValues;
  3185. fBitsPerPixel := 16;
  3186. fFormat := tfDepth16us1;
  3187. fWithoutAlpha := tfDepth16us1;
  3188. fPrecision := glBitmapRec4ub(16, 16, 16, 16);
  3189. fShift := glBitmapRec4ub( 0, 0, 0, 0);
  3190. {$IF NOT DEFINED (OPENGL_ES) OR DEFINED(OPENGL_ES_2_0)}
  3191. fOpenGLFormat := tfDepth16us1;
  3192. fglFormat := GL_DEPTH_COMPONENT;
  3193. fglInternalFormat := GL_DEPTH_COMPONENT16;
  3194. fglDataFormat := GL_UNSIGNED_SHORT;
  3195. {$IFEND}
  3196. end;
  3197. procedure TfdDepth24ui1.SetValues;
  3198. begin
  3199. inherited SetValues;
  3200. fBitsPerPixel := 32;
  3201. fFormat := tfDepth24ui1;
  3202. fWithoutAlpha := tfDepth24ui1;
  3203. fOpenGLFormat := tfDepth24ui1;
  3204. fPrecision := glBitmapRec4ub(32, 32, 32, 32);
  3205. fShift := glBitmapRec4ub( 0, 0, 0, 0);
  3206. {$IF NOT DEFINED (OPENGL_ES) OR DEFINED(OPENGL_ES_3_0)}
  3207. fOpenGLFormat := tfDepth24ui1;
  3208. fglFormat := GL_DEPTH_COMPONENT;
  3209. fglInternalFormat := GL_DEPTH_COMPONENT24;
  3210. fglDataFormat := GL_UNSIGNED_INT;
  3211. {$IFEND}
  3212. end;
  3213. procedure TfdDepth32ui1.SetValues;
  3214. begin
  3215. inherited SetValues;
  3216. fBitsPerPixel := 32;
  3217. fFormat := tfDepth32ui1;
  3218. fWithoutAlpha := tfDepth32ui1;
  3219. fPrecision := glBitmapRec4ub(32, 32, 32, 32);
  3220. fShift := glBitmapRec4ub( 0, 0, 0, 0);
  3221. {$IF NOT DEFINED(OPENGL_ES)}
  3222. fOpenGLFormat := tfDepth32ui1;
  3223. fglFormat := GL_DEPTH_COMPONENT;
  3224. fglInternalFormat := GL_DEPTH_COMPONENT32;
  3225. fglDataFormat := GL_UNSIGNED_INT;
  3226. {$ELSEIF DEFINED(OPENGL_ES_3_0)}
  3227. fOpenGLFormat := tfDepth24ui1;
  3228. {$ELSEIF DEFINED(OPENGL_ES_2_0)}
  3229. fOpenGLFormat := tfDepth16us1;
  3230. {$IFEND}
  3231. end;
  3232. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3233. //TfdS3tcDtx1RGBA/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3234. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3235. procedure TfdS3tcDtx1RGBA.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  3236. begin
  3237. raise EglBitmap.Create('mapping for compressed formats is not supported');
  3238. end;
  3239. procedure TfdS3tcDtx1RGBA.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  3240. begin
  3241. raise EglBitmap.Create('mapping for compressed formats is not supported');
  3242. end;
  3243. procedure TfdS3tcDtx1RGBA.SetValues;
  3244. begin
  3245. inherited SetValues;
  3246. fFormat := tfS3tcDtx1RGBA;
  3247. fWithAlpha := tfS3tcDtx1RGBA;
  3248. fUncompressed := tfRGB5A1us1;
  3249. fBitsPerPixel := 4;
  3250. fIsCompressed := true;
  3251. {$IFNDEF OPENGL_ES}
  3252. fOpenGLFormat := tfS3tcDtx1RGBA;
  3253. fglFormat := GL_COMPRESSED_RGBA;
  3254. fglInternalFormat := GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
  3255. fglDataFormat := GL_UNSIGNED_BYTE;
  3256. {$ELSE}
  3257. fOpenGLFormat := fUncompressed;
  3258. {$ENDIF}
  3259. end;
  3260. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3261. //TfdS3tcDtx3RGBA/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3262. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3263. procedure TfdS3tcDtx3RGBA.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  3264. begin
  3265. raise EglBitmap.Create('mapping for compressed formats is not supported');
  3266. end;
  3267. procedure TfdS3tcDtx3RGBA.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  3268. begin
  3269. raise EglBitmap.Create('mapping for compressed formats is not supported');
  3270. end;
  3271. procedure TfdS3tcDtx3RGBA.SetValues;
  3272. begin
  3273. inherited SetValues;
  3274. fFormat := tfS3tcDtx3RGBA;
  3275. fWithAlpha := tfS3tcDtx3RGBA;
  3276. fUncompressed := tfRGBA8ub4;
  3277. fBitsPerPixel := 8;
  3278. fIsCompressed := true;
  3279. {$IFNDEF OPENGL_ES}
  3280. fOpenGLFormat := tfS3tcDtx3RGBA;
  3281. fglFormat := GL_COMPRESSED_RGBA;
  3282. fglInternalFormat := GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
  3283. fglDataFormat := GL_UNSIGNED_BYTE;
  3284. {$ELSE}
  3285. fOpenGLFormat := fUncompressed;
  3286. {$ENDIF}
  3287. end;
  3288. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3289. //TfdS3tcDtx5RGBA/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3290. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3291. procedure TfdS3tcDtx5RGBA.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  3292. begin
  3293. raise EglBitmap.Create('mapping for compressed formats is not supported');
  3294. end;
  3295. procedure TfdS3tcDtx5RGBA.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  3296. begin
  3297. raise EglBitmap.Create('mapping for compressed formats is not supported');
  3298. end;
  3299. procedure TfdS3tcDtx5RGBA.SetValues;
  3300. begin
  3301. inherited SetValues;
  3302. fFormat := tfS3tcDtx3RGBA;
  3303. fWithAlpha := tfS3tcDtx3RGBA;
  3304. fUncompressed := tfRGBA8ub4;
  3305. fBitsPerPixel := 8;
  3306. fIsCompressed := true;
  3307. {$IFNDEF OPENGL_ES}
  3308. fOpenGLFormat := tfS3tcDtx3RGBA;
  3309. fglFormat := GL_COMPRESSED_RGBA;
  3310. fglInternalFormat := GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
  3311. fglDataFormat := GL_UNSIGNED_BYTE;
  3312. {$ELSE}
  3313. fOpenGLFormat := fUncompressed;
  3314. {$ENDIF}
  3315. end;
  3316. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3317. //TglBitmapFormatDescriptor///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3318. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3319. function TglBitmapFormatDescriptor.GetHasRed: Boolean;
  3320. begin
  3321. result := (fPrecision.r > 0);
  3322. end;
  3323. function TglBitmapFormatDescriptor.GetHasGreen: Boolean;
  3324. begin
  3325. result := (fPrecision.g > 0);
  3326. end;
  3327. function TglBitmapFormatDescriptor.GetHasBlue: Boolean;
  3328. begin
  3329. result := (fPrecision.b > 0);
  3330. end;
  3331. function TglBitmapFormatDescriptor.GetHasAlpha: Boolean;
  3332. begin
  3333. result := (fPrecision.a > 0);
  3334. end;
  3335. function TglBitmapFormatDescriptor.GetHasColor: Boolean;
  3336. begin
  3337. result := HasRed or HasGreen or HasBlue;
  3338. end;
  3339. function TglBitmapFormatDescriptor.GetIsGrayscale: Boolean;
  3340. begin
  3341. result := (Mask.r = Mask.g) and (Mask.g = Mask.b) and (Mask.r > 0);
  3342. end;
  3343. function TglBitmapFormatDescriptor.GetHasOpenGLSupport: Boolean;
  3344. begin
  3345. result := (OpenGLFormat = Format);
  3346. end;
  3347. procedure TglBitmapFormatDescriptor.SetValues;
  3348. begin
  3349. fFormat := tfEmpty;
  3350. fWithAlpha := tfEmpty;
  3351. fWithoutAlpha := tfEmpty;
  3352. fOpenGLFormat := tfEmpty;
  3353. fRGBInverted := tfEmpty;
  3354. fUncompressed := tfEmpty;
  3355. fBitsPerPixel := 0;
  3356. fIsCompressed := false;
  3357. fglFormat := 0;
  3358. fglInternalFormat := 0;
  3359. fglDataFormat := 0;
  3360. FillChar(fPrecision, 0, SizeOf(fPrecision));
  3361. FillChar(fShift, 0, SizeOf(fShift));
  3362. end;
  3363. procedure TglBitmapFormatDescriptor.CalcValues;
  3364. var
  3365. i: Integer;
  3366. begin
  3367. fBytesPerPixel := fBitsPerPixel / 8;
  3368. fChannelCount := 0;
  3369. for i := 0 to 3 do begin
  3370. if (fPrecision.arr[i] > 0) then
  3371. inc(fChannelCount);
  3372. fRange.arr[i] := (1 shl fPrecision.arr[i]) - 1;
  3373. fMask.arr[i] := fRange.arr[i] shl fShift.arr[i];
  3374. end;
  3375. end;
  3376. function TglBitmapFormatDescriptor.GetSize(const aSize: TglBitmapSize): Integer;
  3377. var
  3378. w, h: Integer;
  3379. begin
  3380. if (ffX in aSize.Fields) or (ffY in aSize.Fields) then begin
  3381. w := Max(1, aSize.X);
  3382. h := Max(1, aSize.Y);
  3383. result := GetSize(w, h);
  3384. end else
  3385. result := 0;
  3386. end;
  3387. function TglBitmapFormatDescriptor.GetSize(const aWidth, aHeight: Integer): Integer;
  3388. begin
  3389. result := 0;
  3390. if (aWidth <= 0) or (aHeight <= 0) then
  3391. exit;
  3392. result := Ceil(aWidth * aHeight * BytesPerPixel);
  3393. end;
  3394. constructor TglBitmapFormatDescriptor.Create;
  3395. begin
  3396. inherited Create;
  3397. SetValues;
  3398. CalcValues;
  3399. end;
  3400. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3401. class function TglBitmapFormatDescriptor.GetByFormat(const aInternalFormat: GLenum): TglBitmapFormatDescriptor;
  3402. var
  3403. f: TglBitmapFormat;
  3404. begin
  3405. for f := Low(TglBitmapFormat) to High(TglBitmapFormat) do begin
  3406. result := TFormatDescriptor.Get(f);
  3407. if (result.glInternalFormat = aInternalFormat) then
  3408. exit;
  3409. end;
  3410. result := TFormatDescriptor.Get(tfEmpty);
  3411. end;
  3412. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3413. class function TglBitmapFormatDescriptor.GetByFormat(const aFormat: TglBitmapFormat): TglBitmapFormatDescriptor;
  3414. begin
  3415. result := TFormatDescriptor.Get(aFormat);
  3416. if not Assigned(result) then
  3417. result := TFormatDescriptor.Get(tfEmpty);
  3418. end;
  3419. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3420. //TFormatDescriptor///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3421. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3422. class procedure TFormatDescriptor.Init;
  3423. begin
  3424. if not Assigned(FormatDescriptorCS) then
  3425. FormatDescriptorCS := TCriticalSection.Create;
  3426. end;
  3427. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3428. class function TFormatDescriptor.Get(const aFormat: TglBitmapFormat): TFormatDescriptor;
  3429. begin
  3430. FormatDescriptorCS.Enter;
  3431. try
  3432. result := FormatDescriptors[aFormat];
  3433. if not Assigned(result) then begin
  3434. result := FORMAT_DESCRIPTOR_CLASSES[aFormat].Create;
  3435. FormatDescriptors[aFormat] := result;
  3436. end;
  3437. finally
  3438. FormatDescriptorCS.Leave;
  3439. end;
  3440. end;
  3441. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3442. class function TFormatDescriptor.GetAlpha(const aFormat: TglBitmapFormat): TFormatDescriptor;
  3443. begin
  3444. result := Get(Get(aFormat).WithAlpha);
  3445. end;
  3446. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3447. class function TFormatDescriptor.GetFromMask(const aMask: TglBitmapRec4ul; const aBitCount: Integer): TFormatDescriptor;
  3448. var
  3449. ft: TglBitmapFormat;
  3450. begin
  3451. // find matching format with OpenGL support
  3452. for ft := High(TglBitmapFormat) downto Low(TglBitmapFormat) do begin
  3453. result := Get(ft);
  3454. if (result.MaskMatch(aMask)) and
  3455. (result.glFormat <> 0) and
  3456. (result.glInternalFormat <> 0) and
  3457. ((aBitCount = 0) or (aBitCount = result.BitsPerPixel))
  3458. then
  3459. exit;
  3460. end;
  3461. // find matching format without OpenGL Support
  3462. for ft := High(TglBitmapFormat) downto Low(TglBitmapFormat) do begin
  3463. result := Get(ft);
  3464. if result.MaskMatch(aMask) and ((aBitCount = 0) or (aBitCount = result.BitsPerPixel)) then
  3465. exit;
  3466. end;
  3467. result := TFormatDescriptor.Get(tfEmpty);
  3468. end;
  3469. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3470. class function TFormatDescriptor.GetFromPrecShift(const aPrec, aShift: TglBitmapRec4ub; const aBitCount: Integer): TFormatDescriptor;
  3471. var
  3472. ft: TglBitmapFormat;
  3473. begin
  3474. // find matching format with 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. (result.glFormat <> 0) and
  3480. (result.glInternalFormat <> 0) and
  3481. ((aBitCount = 0) or (aBitCount = result.BitsPerPixel))
  3482. then
  3483. exit;
  3484. end;
  3485. // find matching format without OpenGL Support
  3486. for ft := High(TglBitmapFormat) downto Low(TglBitmapFormat) do begin
  3487. result := Get(ft);
  3488. if glBitmapRec4ubCompare(result.Shift, aShift) and
  3489. glBitmapRec4ubCompare(result.Precision, aPrec) and
  3490. ((aBitCount = 0) or (aBitCount = result.BitsPerPixel)) then
  3491. exit;
  3492. end;
  3493. result := TFormatDescriptor.Get(tfEmpty);
  3494. end;
  3495. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3496. class procedure TFormatDescriptor.Clear;
  3497. var
  3498. f: TglBitmapFormat;
  3499. begin
  3500. FormatDescriptorCS.Enter;
  3501. try
  3502. for f := low(FormatDescriptors) to high(FormatDescriptors) do
  3503. FreeAndNil(FormatDescriptors[f]);
  3504. finally
  3505. FormatDescriptorCS.Leave;
  3506. end;
  3507. end;
  3508. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3509. class procedure TFormatDescriptor.Finalize;
  3510. begin
  3511. Clear;
  3512. FreeAndNil(FormatDescriptorCS);
  3513. end;
  3514. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3515. //TBitfieldFormat/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3516. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3517. procedure TbmpBitfieldFormat.SetCustomValues(const aBPP: Integer; aMask: TglBitmapRec4ul);
  3518. var
  3519. i: Integer;
  3520. begin
  3521. for i := 0 to 3 do begin
  3522. fShift.arr[i] := 0;
  3523. while (aMask.arr[i] > 0) and ((aMask.arr[i] and 1) = 0) do begin
  3524. aMask.arr[i] := aMask.arr[i] shr 1;
  3525. inc(fShift.arr[i]);
  3526. end;
  3527. fPrecision.arr[i] := CountSetBits(aMask.arr[i]);
  3528. end;
  3529. fBitsPerPixel := aBPP;
  3530. CalcValues;
  3531. end;
  3532. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3533. procedure TbmpBitfieldFormat.SetCustomValues(const aBBP: Integer; const aPrec, aShift: TglBitmapRec4ub);
  3534. begin
  3535. fBitsPerPixel := aBBP;
  3536. fPrecision := aPrec;
  3537. fShift := aShift;
  3538. CalcValues;
  3539. end;
  3540. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3541. procedure TbmpBitfieldFormat.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  3542. var
  3543. data: QWord;
  3544. begin
  3545. data :=
  3546. ((aPixel.Data.r and Range.r) shl Shift.r) or
  3547. ((aPixel.Data.g and Range.g) shl Shift.g) or
  3548. ((aPixel.Data.b and Range.b) shl Shift.b) or
  3549. ((aPixel.Data.a and Range.a) shl Shift.a);
  3550. case BitsPerPixel of
  3551. 8: aData^ := data;
  3552. 16: PWord(aData)^ := data;
  3553. 32: PCardinal(aData)^ := data;
  3554. 64: PQWord(aData)^ := data;
  3555. else
  3556. raise EglBitmap.CreateFmt('invalid pixel size: %d', [BitsPerPixel]);
  3557. end;
  3558. inc(aData, Round(BytesPerPixel));
  3559. end;
  3560. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3561. procedure TbmpBitfieldFormat.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  3562. var
  3563. data: QWord;
  3564. i: Integer;
  3565. begin
  3566. case BitsPerPixel of
  3567. 8: data := aData^;
  3568. 16: data := PWord(aData)^;
  3569. 32: data := PCardinal(aData)^;
  3570. 64: data := PQWord(aData)^;
  3571. else
  3572. raise EglBitmap.CreateFmt('invalid pixel size: %d', [BitsPerPixel]);
  3573. end;
  3574. for i := 0 to 3 do
  3575. aPixel.Data.arr[i] := (data shr fShift.arr[i]) and Range.arr[i];
  3576. inc(aData, Round(BytesPerPixel));
  3577. end;
  3578. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3579. //TColorTableFormat///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3580. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3581. procedure TbmpColorTableFormat.SetValues;
  3582. begin
  3583. inherited SetValues;
  3584. fShift := glBitmapRec4ub(8, 8, 8, 0);
  3585. end;
  3586. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3587. procedure TbmpColorTableFormat.SetCustomValues(const aFormat: TglBitmapFormat; const aBPP: Integer; const aPrec, aShift: TglBitmapRec4ub);
  3588. begin
  3589. fFormat := aFormat;
  3590. fBitsPerPixel := aBPP;
  3591. fPrecision := aPrec;
  3592. fShift := aShift;
  3593. CalcValues;
  3594. end;
  3595. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3596. procedure TbmpColorTableFormat.CalcValues;
  3597. begin
  3598. inherited CalcValues;
  3599. end;
  3600. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3601. procedure TbmpColorTableFormat.CreateColorTable;
  3602. var
  3603. i: Integer;
  3604. begin
  3605. SetLength(fColorTable, 256);
  3606. if not HasColor then begin
  3607. // alpha
  3608. for i := 0 to High(fColorTable) do begin
  3609. fColorTable[i].r := Round(((i shr Shift.a) and Range.a) / Range.a * 255);
  3610. fColorTable[i].g := Round(((i shr Shift.a) and Range.a) / Range.a * 255);
  3611. fColorTable[i].b := Round(((i shr Shift.a) and Range.a) / Range.a * 255);
  3612. fColorTable[i].a := 0;
  3613. end;
  3614. end else begin
  3615. // normal
  3616. for i := 0 to High(fColorTable) do begin
  3617. fColorTable[i].r := Round(((i shr Shift.r) and Range.r) / Range.r * 255);
  3618. fColorTable[i].g := Round(((i shr Shift.g) and Range.g) / Range.g * 255);
  3619. fColorTable[i].b := Round(((i shr Shift.b) and Range.b) / Range.b * 255);
  3620. fColorTable[i].a := 0;
  3621. end;
  3622. end;
  3623. end;
  3624. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3625. function TbmpColorTableFormat.CreateMappingData: Pointer;
  3626. begin
  3627. result := Pointer(0);
  3628. end;
  3629. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3630. procedure TbmpColorTableFormat.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  3631. begin
  3632. if (BitsPerPixel <> 8) then
  3633. raise EglBitmapUnsupportedFormat.Create('color table are only supported for 8bit formats');
  3634. if not HasColor then
  3635. // alpha
  3636. aData^ := aPixel.Data.a
  3637. else
  3638. // normal
  3639. aData^ := Round(
  3640. ((aPixel.Data.r shr Shift.r) and Range.r) * LUMINANCE_WEIGHT_R +
  3641. ((aPixel.Data.g shr Shift.g) and Range.g) * LUMINANCE_WEIGHT_G +
  3642. ((aPixel.Data.b shr Shift.b) and Range.b) * LUMINANCE_WEIGHT_B);
  3643. inc(aData);
  3644. end;
  3645. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3646. procedure TbmpColorTableFormat.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  3647. function ReadValue: Byte;
  3648. var
  3649. i: PtrUInt;
  3650. begin
  3651. if (BitsPerPixel = 8) then begin
  3652. result := aData^;
  3653. inc(aData);
  3654. end else begin
  3655. i := {%H-}PtrUInt(aMapData);
  3656. if (BitsPerPixel > 1) then
  3657. result := (aData^ shr i) and ((1 shl BitsPerPixel) - 1)
  3658. else
  3659. result := (aData^ shr (7-i)) and ((1 shl BitsPerPixel) - 1);
  3660. inc(i, BitsPerPixel);
  3661. while (i >= 8) do begin
  3662. inc(aData);
  3663. dec(i, 8);
  3664. end;
  3665. aMapData := {%H-}Pointer(i);
  3666. end;
  3667. end;
  3668. begin
  3669. if (BitsPerPixel > 8) then
  3670. raise EglBitmapUnsupportedFormat.Create('color table are only supported for 8bit formats');
  3671. with fColorTable[ReadValue] do begin
  3672. aPixel.Data.r := r;
  3673. aPixel.Data.g := g;
  3674. aPixel.Data.b := b;
  3675. aPixel.Data.a := a;
  3676. end;
  3677. end;
  3678. destructor TbmpColorTableFormat.Destroy;
  3679. begin
  3680. SetLength(fColorTable, 0);
  3681. inherited Destroy;
  3682. end;
  3683. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3684. //TglBitmap - Helper//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3685. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3686. procedure glBitmapConvertPixel(var aPixel: TglBitmapPixelData; const aSourceFD, aDestFD: TFormatDescriptor);
  3687. var
  3688. i: Integer;
  3689. begin
  3690. for i := 0 to 3 do begin
  3691. if (aSourceFD.Range.arr[i] <> aDestFD.Range.arr[i]) then begin
  3692. if (aSourceFD.Range.arr[i] > 0) then
  3693. aPixel.Data.arr[i] := Round(aPixel.Data.arr[i] / aSourceFD.Range.arr[i] * aDestFD.Range.arr[i])
  3694. else
  3695. aPixel.Data.arr[i] := 0;
  3696. end;
  3697. end;
  3698. end;
  3699. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3700. procedure glBitmapConvertCopyFunc(var aFuncRec: TglBitmapFunctionRec);
  3701. begin
  3702. with aFuncRec do begin
  3703. if (Source.Range.r > 0) then
  3704. Dest.Data.r := Source.Data.r;
  3705. if (Source.Range.g > 0) then
  3706. Dest.Data.g := Source.Data.g;
  3707. if (Source.Range.b > 0) then
  3708. Dest.Data.b := Source.Data.b;
  3709. if (Source.Range.a > 0) then
  3710. Dest.Data.a := Source.Data.a;
  3711. end;
  3712. end;
  3713. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3714. procedure glBitmapConvertCalculateRGBAFunc(var aFuncRec: TglBitmapFunctionRec);
  3715. var
  3716. i: Integer;
  3717. begin
  3718. with aFuncRec do begin
  3719. for i := 0 to 3 do
  3720. if (Source.Range.arr[i] > 0) then
  3721. Dest.Data.arr[i] := Round(Dest.Range.arr[i] * Source.Data.arr[i] / Source.Range.arr[i]);
  3722. end;
  3723. end;
  3724. type
  3725. TShiftData = packed record
  3726. case Integer of
  3727. 0: (r, g, b, a: SmallInt);
  3728. 1: (arr: array[0..3] of SmallInt);
  3729. end;
  3730. PShiftData = ^TShiftData;
  3731. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3732. procedure glBitmapConvertShiftRGBAFunc(var aFuncRec: TglBitmapFunctionRec);
  3733. var
  3734. i: Integer;
  3735. begin
  3736. with aFuncRec do
  3737. for i := 0 to 3 do
  3738. if (Source.Range.arr[i] > 0) then
  3739. Dest.Data.arr[i] := Source.Data.arr[i] shr PShiftData(Args)^.arr[i];
  3740. end;
  3741. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3742. procedure glBitmapInvertFunc(var aFuncRec: TglBitmapFunctionRec);
  3743. var
  3744. i: Integer;
  3745. begin
  3746. with aFuncRec do begin
  3747. Dest.Data := Source.Data;
  3748. for i := 0 to 3 do
  3749. if ({%H-}PtrUInt(Args) and (1 shl i) > 0) then
  3750. Dest.Data.arr[i] := Dest.Data.arr[i] xor Dest.Range.arr[i];
  3751. end;
  3752. end;
  3753. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3754. procedure glBitmapFillWithColorFunc(var aFuncRec: TglBitmapFunctionRec);
  3755. var
  3756. i: Integer;
  3757. begin
  3758. with aFuncRec do begin
  3759. for i := 0 to 3 do
  3760. Dest.Data.arr[i] := PglBitmapPixelData(Args)^.Data.arr[i];
  3761. end;
  3762. end;
  3763. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3764. procedure glBitmapAlphaFunc(var FuncRec: TglBitmapFunctionRec);
  3765. var
  3766. Temp: Single;
  3767. begin
  3768. with FuncRec do begin
  3769. if (FuncRec.Args = nil) then begin //source has no alpha
  3770. Temp :=
  3771. Source.Data.r / Source.Range.r * ALPHA_WEIGHT_R +
  3772. Source.Data.g / Source.Range.g * ALPHA_WEIGHT_G +
  3773. Source.Data.b / Source.Range.b * ALPHA_WEIGHT_B;
  3774. Dest.Data.a := Round(Dest.Range.a * Temp);
  3775. end else
  3776. Dest.Data.a := Round(Source.Data.a / Source.Range.a * Dest.Range.a);
  3777. end;
  3778. end;
  3779. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3780. procedure glBitmapColorKeyAlphaFunc(var FuncRec: TglBitmapFunctionRec);
  3781. type
  3782. PglBitmapPixelData = ^TglBitmapPixelData;
  3783. begin
  3784. with FuncRec do begin
  3785. Dest.Data.r := Source.Data.r;
  3786. Dest.Data.g := Source.Data.g;
  3787. Dest.Data.b := Source.Data.b;
  3788. with PglBitmapPixelData(Args)^ do
  3789. if ((Dest.Data.r <= Data.r) and (Dest.Data.r >= Range.r) and
  3790. (Dest.Data.g <= Data.g) and (Dest.Data.g >= Range.g) and
  3791. (Dest.Data.b <= Data.b) and (Dest.Data.b >= Range.b)) then
  3792. Dest.Data.a := 0
  3793. else
  3794. Dest.Data.a := Dest.Range.a;
  3795. end;
  3796. end;
  3797. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3798. procedure glBitmapValueAlphaFunc(var FuncRec: TglBitmapFunctionRec);
  3799. begin
  3800. with FuncRec do begin
  3801. Dest.Data.r := Source.Data.r;
  3802. Dest.Data.g := Source.Data.g;
  3803. Dest.Data.b := Source.Data.b;
  3804. Dest.Data.a := PCardinal(Args)^;
  3805. end;
  3806. end;
  3807. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3808. procedure SwapRGB(aData: PByte; aWidth: Integer; const aHasAlpha: Boolean);
  3809. type
  3810. PRGBPix = ^TRGBPix;
  3811. TRGBPix = array [0..2] of byte;
  3812. var
  3813. Temp: Byte;
  3814. begin
  3815. while aWidth > 0 do begin
  3816. Temp := PRGBPix(aData)^[0];
  3817. PRGBPix(aData)^[0] := PRGBPix(aData)^[2];
  3818. PRGBPix(aData)^[2] := Temp;
  3819. if aHasAlpha then
  3820. Inc(aData, 4)
  3821. else
  3822. Inc(aData, 3);
  3823. dec(aWidth);
  3824. end;
  3825. end;
  3826. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3827. //TglBitmapData///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3828. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3829. function TglBitmapData.GetFormatDescriptor: TglBitmapFormatDescriptor;
  3830. begin
  3831. result := TFormatDescriptor.Get(fFormat);
  3832. end;
  3833. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3834. function TglBitmapData.GetWidth: Integer;
  3835. begin
  3836. if (ffX in fDimension.Fields) then
  3837. result := fDimension.X
  3838. else
  3839. result := -1;
  3840. end;
  3841. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3842. function TglBitmapData.GetHeight: Integer;
  3843. begin
  3844. if (ffY in fDimension.Fields) then
  3845. result := fDimension.Y
  3846. else
  3847. result := -1;
  3848. end;
  3849. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3850. function TglBitmapData.GetScanlines(const aIndex: Integer): PByte;
  3851. begin
  3852. if fHasScanlines and (aIndex >= Low(fScanlines)) and (aIndex <= High(fScanlines)) then
  3853. result := fScanlines[aIndex]
  3854. else
  3855. result := nil;
  3856. end;
  3857. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3858. procedure TglBitmapData.SetFormat(const aValue: TglBitmapFormat);
  3859. begin
  3860. if fFormat = aValue then
  3861. exit;
  3862. if TFormatDescriptor.Get(Format).BitsPerPixel <> TFormatDescriptor.Get(aValue).BitsPerPixel then
  3863. raise EglBitmapUnsupportedFormat.Create(Format);
  3864. SetData(fData, aValue, Width, Height);
  3865. end;
  3866. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3867. procedure TglBitmapData.PrepareResType(var aResource: String; var aResType: PChar);
  3868. var
  3869. TempPos: Integer;
  3870. begin
  3871. if not Assigned(aResType) then begin
  3872. TempPos := Pos('.', aResource);
  3873. aResType := PChar(UpperCase(Copy(aResource, TempPos + 1, Length(aResource) - TempPos)));
  3874. aResource := UpperCase(Copy(aResource, 0, TempPos -1));
  3875. end;
  3876. end;
  3877. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3878. procedure TglBitmapData.UpdateScanlines;
  3879. var
  3880. w, h, i, LineWidth: Integer;
  3881. begin
  3882. w := Width;
  3883. h := Height;
  3884. fHasScanlines := Assigned(fData) and (w > 0) and (h > 0);
  3885. if fHasScanlines then begin
  3886. SetLength(fScanlines, h);
  3887. LineWidth := Trunc(w * FormatDescriptor.BytesPerPixel);
  3888. for i := 0 to h-1 do begin
  3889. fScanlines[i] := fData;
  3890. Inc(fScanlines[i], i * LineWidth);
  3891. end;
  3892. end else
  3893. SetLength(fScanlines, 0);
  3894. end;
  3895. {$IFDEF GLB_SUPPORT_PNG_READ}
  3896. {$IF DEFINED(GLB_LAZ_PNG)}
  3897. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3898. //PNG/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3899. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3900. function TglBitmapData.LoadPNG(const aStream: TStream): Boolean;
  3901. const
  3902. MAGIC_LEN = 8;
  3903. PNG_MAGIC: String[MAGIC_LEN] = #$89#$50#$4E#$47#$0D#$0A#$1A#$0A;
  3904. var
  3905. reader: TLazReaderPNG;
  3906. intf: TLazIntfImage;
  3907. StreamPos: Int64;
  3908. magic: String[MAGIC_LEN];
  3909. begin
  3910. result := true;
  3911. StreamPos := aStream.Position;
  3912. SetLength(magic, MAGIC_LEN);
  3913. aStream.Read(magic[1], MAGIC_LEN);
  3914. aStream.Position := StreamPos;
  3915. if (magic <> PNG_MAGIC) then begin
  3916. result := false;
  3917. exit;
  3918. end;
  3919. intf := TLazIntfImage.Create(0, 0);
  3920. reader := TLazReaderPNG.Create;
  3921. try try
  3922. reader.UpdateDescription := true;
  3923. reader.ImageRead(aStream, intf);
  3924. AssignFromLazIntfImage(intf);
  3925. except
  3926. result := false;
  3927. aStream.Position := StreamPos;
  3928. exit;
  3929. end;
  3930. finally
  3931. reader.Free;
  3932. intf.Free;
  3933. end;
  3934. end;
  3935. {$ELSEIF DEFINED(GLB_SDL_IMAGE)}
  3936. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3937. function TglBitmapData.LoadPNG(const aStream: TStream): Boolean;
  3938. var
  3939. Surface: PSDL_Surface;
  3940. RWops: PSDL_RWops;
  3941. begin
  3942. result := false;
  3943. RWops := glBitmapCreateRWops(aStream);
  3944. try
  3945. if IMG_isPNG(RWops) > 0 then begin
  3946. Surface := IMG_LoadPNG_RW(RWops);
  3947. try
  3948. AssignFromSurface(Surface);
  3949. result := true;
  3950. finally
  3951. SDL_FreeSurface(Surface);
  3952. end;
  3953. end;
  3954. finally
  3955. SDL_FreeRW(RWops);
  3956. end;
  3957. end;
  3958. {$ELSEIF DEFINED(GLB_LIB_PNG)}
  3959. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3960. procedure glBitmap_libPNG_read_func(png: png_structp; buffer: png_bytep; size: cardinal); cdecl;
  3961. begin
  3962. TStream(png_get_io_ptr(png)).Read(buffer^, size);
  3963. end;
  3964. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3965. function TglBitmapData.LoadPNG(const aStream: TStream): Boolean;
  3966. var
  3967. StreamPos: Int64;
  3968. signature: array [0..7] of byte;
  3969. png: png_structp;
  3970. png_info: png_infop;
  3971. TempHeight, TempWidth: Integer;
  3972. Format: TglBitmapFormat;
  3973. png_data: pByte;
  3974. png_rows: array of pByte;
  3975. Row, LineSize: Integer;
  3976. begin
  3977. result := false;
  3978. if not init_libPNG then
  3979. raise Exception.Create('LoadPNG - unable to initialize libPNG.');
  3980. try
  3981. // signature
  3982. StreamPos := aStream.Position;
  3983. aStream.Read(signature{%H-}, 8);
  3984. aStream.Position := StreamPos;
  3985. if png_check_sig(@signature, 8) <> 0 then begin
  3986. // png read struct
  3987. png := png_create_read_struct(PNG_LIBPNG_VER_STRING, nil, nil, nil);
  3988. if png = nil then
  3989. raise EglBitmapException.Create('LoadPng - couldn''t create read struct.');
  3990. // png info
  3991. png_info := png_create_info_struct(png);
  3992. if png_info = nil then begin
  3993. png_destroy_read_struct(@png, nil, nil);
  3994. raise EglBitmapException.Create('LoadPng - couldn''t create info struct.');
  3995. end;
  3996. // set read callback
  3997. png_set_read_fn(png, aStream, glBitmap_libPNG_read_func);
  3998. // read informations
  3999. png_read_info(png, png_info);
  4000. // size
  4001. TempHeight := png_get_image_height(png, png_info);
  4002. TempWidth := png_get_image_width(png, png_info);
  4003. // format
  4004. case png_get_color_type(png, png_info) of
  4005. PNG_COLOR_TYPE_GRAY:
  4006. Format := tfLuminance8ub1;
  4007. PNG_COLOR_TYPE_GRAY_ALPHA:
  4008. Format := tfLuminance8Alpha8us1;
  4009. PNG_COLOR_TYPE_RGB:
  4010. Format := tfRGB8ub3;
  4011. PNG_COLOR_TYPE_RGB_ALPHA:
  4012. Format := tfRGBA8ub4;
  4013. else
  4014. raise EglBitmapException.Create ('LoadPng - Unsupported Colortype found.');
  4015. end;
  4016. // cut upper 8 bit from 16 bit formats
  4017. if png_get_bit_depth(png, png_info) > 8 then
  4018. png_set_strip_16(png);
  4019. // expand bitdepth smaller than 8
  4020. if png_get_bit_depth(png, png_info) < 8 then
  4021. png_set_expand(png);
  4022. // allocating mem for scanlines
  4023. LineSize := png_get_rowbytes(png, png_info);
  4024. GetMem(png_data, TempHeight * LineSize);
  4025. try
  4026. SetLength(png_rows, TempHeight);
  4027. for Row := Low(png_rows) to High(png_rows) do begin
  4028. png_rows[Row] := png_data;
  4029. Inc(png_rows[Row], Row * LineSize);
  4030. end;
  4031. // read complete image into scanlines
  4032. png_read_image(png, @png_rows[0]);
  4033. // read end
  4034. png_read_end(png, png_info);
  4035. // destroy read struct
  4036. png_destroy_read_struct(@png, @png_info, nil);
  4037. SetLength(png_rows, 0);
  4038. // set new data
  4039. SetData(png_data, Format, TempWidth, TempHeight);
  4040. result := true;
  4041. except
  4042. if Assigned(png_data) then
  4043. FreeMem(png_data);
  4044. raise;
  4045. end;
  4046. end;
  4047. finally
  4048. quit_libPNG;
  4049. end;
  4050. end;
  4051. {$ELSEIF DEFINED(GLB_PNGIMAGE)}
  4052. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4053. function TglBitmapData.LoadPNG(const aStream: TStream): Boolean;
  4054. var
  4055. StreamPos: Int64;
  4056. Png: TPNGObject;
  4057. Header: String[8];
  4058. Row, Col, PixSize, LineSize: Integer;
  4059. NewImage, pSource, pDest, pAlpha: pByte;
  4060. PngFormat: TglBitmapFormat;
  4061. FormatDesc: TFormatDescriptor;
  4062. const
  4063. PngHeader: String[8] = #137#80#78#71#13#10#26#10;
  4064. begin
  4065. result := false;
  4066. StreamPos := aStream.Position;
  4067. aStream.Read(Header[0], SizeOf(Header));
  4068. aStream.Position := StreamPos;
  4069. {Test if the header matches}
  4070. if Header = PngHeader then begin
  4071. Png := TPNGObject.Create;
  4072. try
  4073. Png.LoadFromStream(aStream);
  4074. case Png.Header.ColorType of
  4075. COLOR_GRAYSCALE:
  4076. PngFormat := tfLuminance8ub1;
  4077. COLOR_GRAYSCALEALPHA:
  4078. PngFormat := tfLuminance8Alpha8us1;
  4079. COLOR_RGB:
  4080. PngFormat := tfBGR8ub3;
  4081. COLOR_RGBALPHA:
  4082. PngFormat := tfBGRA8ub4;
  4083. else
  4084. raise EglBitmapException.Create ('LoadPng - Unsupported Colortype found.');
  4085. end;
  4086. FormatDesc := TFormatDescriptor.Get(PngFormat);
  4087. PixSize := Round(FormatDesc.PixelSize);
  4088. LineSize := FormatDesc.GetSize(Png.Header.Width, 1);
  4089. GetMem(NewImage, LineSize * Integer(Png.Header.Height));
  4090. try
  4091. pDest := NewImage;
  4092. case Png.Header.ColorType of
  4093. COLOR_RGB, COLOR_GRAYSCALE:
  4094. begin
  4095. for Row := 0 to Png.Height -1 do begin
  4096. Move (Png.Scanline[Row]^, pDest^, LineSize);
  4097. Inc(pDest, LineSize);
  4098. end;
  4099. end;
  4100. COLOR_RGBALPHA, COLOR_GRAYSCALEALPHA:
  4101. begin
  4102. PixSize := PixSize -1;
  4103. for Row := 0 to Png.Height -1 do begin
  4104. pSource := Png.Scanline[Row];
  4105. pAlpha := pByte(Png.AlphaScanline[Row]);
  4106. for Col := 0 to Png.Width -1 do begin
  4107. Move (pSource^, pDest^, PixSize);
  4108. Inc(pSource, PixSize);
  4109. Inc(pDest, PixSize);
  4110. pDest^ := pAlpha^;
  4111. inc(pAlpha);
  4112. Inc(pDest);
  4113. end;
  4114. end;
  4115. end;
  4116. else
  4117. raise EglBitmapException.Create ('LoadPng - Unsupported Colortype found.');
  4118. end;
  4119. SetData(NewImage, PngFormat, Png.Header.Width, Png.Header.Height);
  4120. result := true;
  4121. except
  4122. if Assigned(NewImage) then
  4123. FreeMem(NewImage);
  4124. raise;
  4125. end;
  4126. finally
  4127. Png.Free;
  4128. end;
  4129. end;
  4130. end;
  4131. {$IFEND}
  4132. {$ENDIF}
  4133. {$IFDEF GLB_SUPPORT_PNG_WRITE}
  4134. {$IFDEF GLB_LIB_PNG}
  4135. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4136. procedure glBitmap_libPNG_write_func(png: png_structp; buffer: png_bytep; size: cardinal); cdecl;
  4137. begin
  4138. TStream(png_get_io_ptr(png)).Write(buffer^, size);
  4139. end;
  4140. {$ENDIF}
  4141. {$IF DEFINED(GLB_LAZ_PNG)}
  4142. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4143. procedure TglBitmapData.SavePNG(const aStream: TStream);
  4144. var
  4145. png: TPortableNetworkGraphic;
  4146. intf: TLazIntfImage;
  4147. raw: TRawImage;
  4148. begin
  4149. png := TPortableNetworkGraphic.Create;
  4150. intf := TLazIntfImage.Create(0, 0);
  4151. try
  4152. if not AssignToLazIntfImage(intf) then
  4153. raise EglBitmap.Create('unable to create LazIntfImage from glBitmap');
  4154. intf.GetRawImage(raw);
  4155. png.LoadFromRawImage(raw, false);
  4156. png.SaveToStream(aStream);
  4157. finally
  4158. png.Free;
  4159. intf.Free;
  4160. end;
  4161. end;
  4162. {$ELSEIF DEFINED(GLB_LIB_PNG)}
  4163. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4164. procedure TglBitmapData.SavePNG(const aStream: TStream);
  4165. var
  4166. png: png_structp;
  4167. png_info: png_infop;
  4168. png_rows: array of pByte;
  4169. LineSize: Integer;
  4170. ColorType: Integer;
  4171. Row: Integer;
  4172. FormatDesc: TFormatDescriptor;
  4173. begin
  4174. if not (ftPNG in FormatGetSupportedFiles(Format)) then
  4175. raise EglBitmapUnsupportedFormat.Create(Format);
  4176. if not init_libPNG then
  4177. raise Exception.Create('unable to initialize libPNG.');
  4178. try
  4179. case Format of
  4180. tfAlpha8ub1, tfLuminance8ub1:
  4181. ColorType := PNG_COLOR_TYPE_GRAY;
  4182. tfLuminance8Alpha8us1:
  4183. ColorType := PNG_COLOR_TYPE_GRAY_ALPHA;
  4184. tfBGR8ub3, tfRGB8ub3:
  4185. ColorType := PNG_COLOR_TYPE_RGB;
  4186. tfBGRA8ub4, tfRGBA8ub4:
  4187. ColorType := PNG_COLOR_TYPE_RGBA;
  4188. else
  4189. raise EglBitmapUnsupportedFormat.Create(Format);
  4190. end;
  4191. FormatDesc := TFormatDescriptor.Get(Format);
  4192. LineSize := FormatDesc.GetSize(Width, 1);
  4193. // creating array for scanline
  4194. SetLength(png_rows, Height);
  4195. try
  4196. for Row := 0 to Height - 1 do begin
  4197. png_rows[Row] := Data;
  4198. Inc(png_rows[Row], Row * LineSize)
  4199. end;
  4200. // write struct
  4201. png := png_create_write_struct(PNG_LIBPNG_VER_STRING, nil, nil, nil);
  4202. if png = nil then
  4203. raise EglBitmapException.Create('SavePng - couldn''t create write struct.');
  4204. // create png info
  4205. png_info := png_create_info_struct(png);
  4206. if png_info = nil then begin
  4207. png_destroy_write_struct(@png, nil);
  4208. raise EglBitmapException.Create('SavePng - couldn''t create info struct.');
  4209. end;
  4210. // set read callback
  4211. png_set_write_fn(png, aStream, glBitmap_libPNG_write_func, nil);
  4212. // set compression
  4213. png_set_compression_level(png, 6);
  4214. if Format in [tfBGR8ub3, tfBGRA8ub4] then
  4215. png_set_bgr(png);
  4216. png_set_IHDR(png, png_info, Width, Height, 8, ColorType, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
  4217. png_write_info(png, png_info);
  4218. png_write_image(png, @png_rows[0]);
  4219. png_write_end(png, png_info);
  4220. png_destroy_write_struct(@png, @png_info);
  4221. finally
  4222. SetLength(png_rows, 0);
  4223. end;
  4224. finally
  4225. quit_libPNG;
  4226. end;
  4227. end;
  4228. {$ELSEIF DEFINED(GLB_PNGIMAGE)}
  4229. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4230. procedure TglBitmapData.SavePNG(const aStream: TStream);
  4231. var
  4232. Png: TPNGObject;
  4233. pSource, pDest: pByte;
  4234. X, Y, PixSize: Integer;
  4235. ColorType: Cardinal;
  4236. Alpha: Boolean;
  4237. pTemp: pByte;
  4238. Temp: Byte;
  4239. begin
  4240. if not (ftPNG in FormatGetSupportedFiles (Format)) then
  4241. raise EglBitmapUnsupportedFormat.Create(Format);
  4242. case Format of
  4243. tfAlpha8ub1, tfLuminance8ub1: begin
  4244. ColorType := COLOR_GRAYSCALE;
  4245. PixSize := 1;
  4246. Alpha := false;
  4247. end;
  4248. tfLuminance8Alpha8us1: begin
  4249. ColorType := COLOR_GRAYSCALEALPHA;
  4250. PixSize := 1;
  4251. Alpha := true;
  4252. end;
  4253. tfBGR8ub3, tfRGB8ub3: begin
  4254. ColorType := COLOR_RGB;
  4255. PixSize := 3;
  4256. Alpha := false;
  4257. end;
  4258. tfBGRA8ub4, tfRGBA8ub4: begin
  4259. ColorType := COLOR_RGBALPHA;
  4260. PixSize := 3;
  4261. Alpha := true
  4262. end;
  4263. else
  4264. raise EglBitmapUnsupportedFormat.Create(Format);
  4265. end;
  4266. Png := TPNGObject.CreateBlank(ColorType, 8, Width, Height);
  4267. try
  4268. // Copy ImageData
  4269. pSource := Data;
  4270. for Y := 0 to Height -1 do begin
  4271. pDest := png.ScanLine[Y];
  4272. for X := 0 to Width -1 do begin
  4273. Move(pSource^, pDest^, PixSize);
  4274. Inc(pDest, PixSize);
  4275. Inc(pSource, PixSize);
  4276. if Alpha then begin
  4277. png.AlphaScanline[Y]^[X] := pSource^;
  4278. Inc(pSource);
  4279. end;
  4280. end;
  4281. // convert RGB line to BGR
  4282. if Format in [tfRGB8ub3, tfRGBA8ub4] then begin
  4283. pTemp := png.ScanLine[Y];
  4284. for X := 0 to Width -1 do begin
  4285. Temp := pByteArray(pTemp)^[0];
  4286. pByteArray(pTemp)^[0] := pByteArray(pTemp)^[2];
  4287. pByteArray(pTemp)^[2] := Temp;
  4288. Inc(pTemp, 3);
  4289. end;
  4290. end;
  4291. end;
  4292. // Save to Stream
  4293. Png.CompressionLevel := 6;
  4294. Png.SaveToStream(aStream);
  4295. finally
  4296. FreeAndNil(Png);
  4297. end;
  4298. end;
  4299. {$IFEND}
  4300. {$ENDIF}
  4301. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4302. //JPEG////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4303. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4304. {$IFDEF GLB_LIB_JPEG}
  4305. type
  4306. glBitmap_libJPEG_source_mgr_ptr = ^glBitmap_libJPEG_source_mgr;
  4307. glBitmap_libJPEG_source_mgr = record
  4308. pub: jpeg_source_mgr;
  4309. SrcStream: TStream;
  4310. SrcBuffer: array [1..4096] of byte;
  4311. end;
  4312. glBitmap_libJPEG_dest_mgr_ptr = ^glBitmap_libJPEG_dest_mgr;
  4313. glBitmap_libJPEG_dest_mgr = record
  4314. pub: jpeg_destination_mgr;
  4315. DestStream: TStream;
  4316. DestBuffer: array [1..4096] of byte;
  4317. end;
  4318. procedure glBitmap_libJPEG_error_exit(cinfo: j_common_ptr); cdecl;
  4319. begin
  4320. //DUMMY
  4321. end;
  4322. procedure glBitmap_libJPEG_output_message(cinfo: j_common_ptr); cdecl;
  4323. begin
  4324. //DUMMY
  4325. end;
  4326. procedure glBitmap_libJPEG_init_source(cinfo: j_decompress_ptr); cdecl;
  4327. begin
  4328. //DUMMY
  4329. end;
  4330. procedure glBitmap_libJPEG_term_source(cinfo: j_decompress_ptr); cdecl;
  4331. begin
  4332. //DUMMY
  4333. end;
  4334. procedure glBitmap_libJPEG_init_destination(cinfo: j_compress_ptr); cdecl;
  4335. begin
  4336. //DUMMY
  4337. end;
  4338. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4339. function glBitmap_libJPEG_fill_input_buffer(cinfo: j_decompress_ptr): boolean; cdecl;
  4340. var
  4341. src: glBitmap_libJPEG_source_mgr_ptr;
  4342. bytes: integer;
  4343. begin
  4344. src := glBitmap_libJPEG_source_mgr_ptr(cinfo^.src);
  4345. bytes := src^.SrcStream.Read(src^.SrcBuffer[1], 4096);
  4346. if (bytes <= 0) then begin
  4347. src^.SrcBuffer[1] := $FF;
  4348. src^.SrcBuffer[2] := JPEG_EOI;
  4349. bytes := 2;
  4350. end;
  4351. src^.pub.next_input_byte := @(src^.SrcBuffer[1]);
  4352. src^.pub.bytes_in_buffer := bytes;
  4353. result := true;
  4354. end;
  4355. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4356. procedure glBitmap_libJPEG_skip_input_data(cinfo: j_decompress_ptr; num_bytes: Longint); cdecl;
  4357. var
  4358. src: glBitmap_libJPEG_source_mgr_ptr;
  4359. begin
  4360. src := glBitmap_libJPEG_source_mgr_ptr(cinfo^.src);
  4361. if num_bytes > 0 then begin
  4362. // wanted byte isn't in buffer so set stream position and read buffer
  4363. if num_bytes > src^.pub.bytes_in_buffer then begin
  4364. src^.SrcStream.Position := src^.SrcStream.Position + num_bytes - src^.pub.bytes_in_buffer;
  4365. src^.pub.fill_input_buffer(cinfo);
  4366. end else begin
  4367. // wanted byte is in buffer so only skip
  4368. inc(src^.pub.next_input_byte, num_bytes);
  4369. dec(src^.pub.bytes_in_buffer, num_bytes);
  4370. end;
  4371. end;
  4372. end;
  4373. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4374. function glBitmap_libJPEG_empty_output_buffer(cinfo: j_compress_ptr): boolean; cdecl;
  4375. var
  4376. dest: glBitmap_libJPEG_dest_mgr_ptr;
  4377. begin
  4378. dest := glBitmap_libJPEG_dest_mgr_ptr(cinfo^.dest);
  4379. if dest^.pub.free_in_buffer < Cardinal(Length(dest^.DestBuffer)) then begin
  4380. // write complete buffer
  4381. dest^.DestStream.Write(dest^.DestBuffer[1], SizeOf(dest^.DestBuffer));
  4382. // reset buffer
  4383. dest^.pub.next_output_byte := @dest^.DestBuffer[1];
  4384. dest^.pub.free_in_buffer := Length(dest^.DestBuffer);
  4385. end;
  4386. result := true;
  4387. end;
  4388. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4389. procedure glBitmap_libJPEG_term_destination(cinfo: j_compress_ptr); cdecl;
  4390. var
  4391. Idx: Integer;
  4392. dest: glBitmap_libJPEG_dest_mgr_ptr;
  4393. begin
  4394. dest := glBitmap_libJPEG_dest_mgr_ptr(cinfo^.dest);
  4395. for Idx := Low(dest^.DestBuffer) to High(dest^.DestBuffer) do begin
  4396. // check for endblock
  4397. if (Idx < High(dest^.DestBuffer)) and (dest^.DestBuffer[Idx] = $FF) and (dest^.DestBuffer[Idx +1] = JPEG_EOI) then begin
  4398. // write endblock
  4399. dest^.DestStream.Write(dest^.DestBuffer[Idx], 2);
  4400. // leave
  4401. break;
  4402. end else
  4403. dest^.DestStream.Write(dest^.DestBuffer[Idx], 1);
  4404. end;
  4405. end;
  4406. {$ENDIF}
  4407. {$IFDEF GLB_SUPPORT_JPEG_READ}
  4408. {$IF DEFINED(GLB_LAZ_JPEG)}
  4409. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4410. function TglBitmapData.LoadJPEG(const aStream: TStream): Boolean;
  4411. const
  4412. MAGIC_LEN = 2;
  4413. JPEG_MAGIC: String[MAGIC_LEN] = #$FF#$D8;
  4414. var
  4415. intf: TLazIntfImage;
  4416. reader: TFPReaderJPEG;
  4417. StreamPos: Int64;
  4418. magic: String[MAGIC_LEN];
  4419. begin
  4420. result := true;
  4421. StreamPos := aStream.Position;
  4422. SetLength(magic, MAGIC_LEN);
  4423. aStream.Read(magic[1], MAGIC_LEN);
  4424. aStream.Position := StreamPos;
  4425. if (magic <> JPEG_MAGIC) then begin
  4426. result := false;
  4427. exit;
  4428. end;
  4429. reader := TFPReaderJPEG.Create;
  4430. intf := TLazIntfImage.Create(0, 0);
  4431. try try
  4432. intf.DataDescription := GetDescriptionFromDevice(0, 0, 0);
  4433. reader.ImageRead(aStream, intf);
  4434. AssignFromLazIntfImage(intf);
  4435. except
  4436. result := false;
  4437. aStream.Position := StreamPos;
  4438. exit;
  4439. end;
  4440. finally
  4441. reader.Free;
  4442. intf.Free;
  4443. end;
  4444. end;
  4445. {$ELSEIF DEFINED(GLB_SDL_IMAGE)}
  4446. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4447. function TglBitmapData.LoadJPEG(const aStream: TStream): Boolean;
  4448. var
  4449. Surface: PSDL_Surface;
  4450. RWops: PSDL_RWops;
  4451. begin
  4452. result := false;
  4453. RWops := glBitmapCreateRWops(aStream);
  4454. try
  4455. if IMG_isJPG(RWops) > 0 then begin
  4456. Surface := IMG_LoadJPG_RW(RWops);
  4457. try
  4458. AssignFromSurface(Surface);
  4459. result := true;
  4460. finally
  4461. SDL_FreeSurface(Surface);
  4462. end;
  4463. end;
  4464. finally
  4465. SDL_FreeRW(RWops);
  4466. end;
  4467. end;
  4468. {$ELSEIF DEFINED(GLB_LIB_JPEG)}
  4469. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4470. function TglBitmapData.LoadJPEG(const aStream: TStream): Boolean;
  4471. var
  4472. StreamPos: Int64;
  4473. Temp: array[0..1]of Byte;
  4474. jpeg: jpeg_decompress_struct;
  4475. jpeg_err: jpeg_error_mgr;
  4476. IntFormat: TglBitmapFormat;
  4477. pImage: pByte;
  4478. TempHeight, TempWidth: Integer;
  4479. pTemp: pByte;
  4480. Row: Integer;
  4481. FormatDesc: TFormatDescriptor;
  4482. begin
  4483. result := false;
  4484. if not init_libJPEG then
  4485. raise Exception.Create('LoadJPG - unable to initialize libJPEG.');
  4486. try
  4487. // reading first two bytes to test file and set cursor back to begin
  4488. StreamPos := aStream.Position;
  4489. aStream.Read({%H-}Temp[0], 2);
  4490. aStream.Position := StreamPos;
  4491. // if Bitmap then read file.
  4492. if ((Temp[0] = $FF) and (Temp[1] = $D8)) then begin
  4493. FillChar(jpeg{%H-}, SizeOf(jpeg_decompress_struct), $00);
  4494. FillChar(jpeg_err{%H-}, SizeOf(jpeg_error_mgr), $00);
  4495. // error managment
  4496. jpeg.err := jpeg_std_error(@jpeg_err);
  4497. jpeg_err.error_exit := glBitmap_libJPEG_error_exit;
  4498. jpeg_err.output_message := glBitmap_libJPEG_output_message;
  4499. // decompression struct
  4500. jpeg_create_decompress(@jpeg);
  4501. // allocation space for streaming methods
  4502. jpeg.src := jpeg.mem^.alloc_small(@jpeg, JPOOL_PERMANENT, SizeOf(glBitmap_libJPEG_source_mgr));
  4503. // seeting up custom functions
  4504. with glBitmap_libJPEG_source_mgr_ptr(jpeg.src)^ do begin
  4505. pub.init_source := glBitmap_libJPEG_init_source;
  4506. pub.fill_input_buffer := glBitmap_libJPEG_fill_input_buffer;
  4507. pub.skip_input_data := glBitmap_libJPEG_skip_input_data;
  4508. pub.resync_to_restart := jpeg_resync_to_restart; // use default method
  4509. pub.term_source := glBitmap_libJPEG_term_source;
  4510. pub.bytes_in_buffer := 0; // forces fill_input_buffer on first read
  4511. pub.next_input_byte := nil; // until buffer loaded
  4512. SrcStream := aStream;
  4513. end;
  4514. // set global decoding state
  4515. jpeg.global_state := DSTATE_START;
  4516. // read header of jpeg
  4517. jpeg_read_header(@jpeg, false);
  4518. // setting output parameter
  4519. case jpeg.jpeg_color_space of
  4520. JCS_GRAYSCALE:
  4521. begin
  4522. jpeg.out_color_space := JCS_GRAYSCALE;
  4523. IntFormat := tfLuminance8ub1;
  4524. end;
  4525. else
  4526. jpeg.out_color_space := JCS_RGB;
  4527. IntFormat := tfRGB8ub3;
  4528. end;
  4529. // reading image
  4530. jpeg_start_decompress(@jpeg);
  4531. TempHeight := jpeg.output_height;
  4532. TempWidth := jpeg.output_width;
  4533. FormatDesc := TFormatDescriptor.Get(IntFormat);
  4534. // creating new image
  4535. GetMem(pImage, FormatDesc.GetSize(TempWidth, TempHeight));
  4536. try
  4537. pTemp := pImage;
  4538. for Row := 0 to TempHeight -1 do begin
  4539. jpeg_read_scanlines(@jpeg, @pTemp, 1);
  4540. Inc(pTemp, FormatDesc.GetSize(TempWidth, 1));
  4541. end;
  4542. // finish decompression
  4543. jpeg_finish_decompress(@jpeg);
  4544. // destroy decompression
  4545. jpeg_destroy_decompress(@jpeg);
  4546. SetData(pImage, IntFormat, TempWidth, TempHeight);
  4547. result := true;
  4548. except
  4549. if Assigned(pImage) then
  4550. FreeMem(pImage);
  4551. raise;
  4552. end;
  4553. end;
  4554. finally
  4555. quit_libJPEG;
  4556. end;
  4557. end;
  4558. {$ELSEIF DEFINED(GLB_DELPHI_JPEG)}
  4559. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4560. function TglBitmapData.LoadJPEG(const aStream: TStream): Boolean;
  4561. var
  4562. bmp: TBitmap;
  4563. jpg: TJPEGImage;
  4564. StreamPos: Int64;
  4565. Temp: array[0..1]of Byte;
  4566. begin
  4567. result := false;
  4568. // reading first two bytes to test file and set cursor back to begin
  4569. StreamPos := aStream.Position;
  4570. aStream.Read(Temp[0], 2);
  4571. aStream.Position := StreamPos;
  4572. // if Bitmap then read file.
  4573. if ((Temp[0] = $FF) and (Temp[1] = $D8)) then begin
  4574. bmp := TBitmap.Create;
  4575. try
  4576. jpg := TJPEGImage.Create;
  4577. try
  4578. jpg.LoadFromStream(aStream);
  4579. bmp.Assign(jpg);
  4580. result := AssignFromBitmap(bmp);
  4581. finally
  4582. jpg.Free;
  4583. end;
  4584. finally
  4585. bmp.Free;
  4586. end;
  4587. end;
  4588. end;
  4589. {$IFEND}
  4590. {$ENDIF}
  4591. {$IFDEF GLB_SUPPORT_JPEG_WRITE}
  4592. {$IF DEFINED(GLB_LAZ_JPEG)}
  4593. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4594. procedure TglBitmapData.SaveJPEG(const aStream: TStream);
  4595. var
  4596. jpeg: TJPEGImage;
  4597. intf: TLazIntfImage;
  4598. raw: TRawImage;
  4599. begin
  4600. jpeg := TJPEGImage.Create;
  4601. intf := TLazIntfImage.Create(0, 0);
  4602. try
  4603. if not AssignToLazIntfImage(intf) then
  4604. raise EglBitmap.Create('unable to create LazIntfImage from glBitmap');
  4605. intf.GetRawImage(raw);
  4606. jpeg.LoadFromRawImage(raw, false);
  4607. jpeg.SaveToStream(aStream);
  4608. finally
  4609. intf.Free;
  4610. jpeg.Free;
  4611. end;
  4612. end;
  4613. {$ELSEIF DEFINED(GLB_LIB_JPEG)}
  4614. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4615. procedure TglBitmapData.SaveJPEG(const aStream: TStream);
  4616. var
  4617. jpeg: jpeg_compress_struct;
  4618. jpeg_err: jpeg_error_mgr;
  4619. Row: Integer;
  4620. pTemp, pTemp2: pByte;
  4621. procedure CopyRow(pDest, pSource: pByte);
  4622. var
  4623. X: Integer;
  4624. begin
  4625. for X := 0 to Width - 1 do begin
  4626. pByteArray(pDest)^[0] := pByteArray(pSource)^[2];
  4627. pByteArray(pDest)^[1] := pByteArray(pSource)^[1];
  4628. pByteArray(pDest)^[2] := pByteArray(pSource)^[0];
  4629. Inc(pDest, 3);
  4630. Inc(pSource, 3);
  4631. end;
  4632. end;
  4633. begin
  4634. if not (ftJPEG in FormatGetSupportedFiles(Format)) then
  4635. raise EglBitmapUnsupportedFormat.Create(Format);
  4636. if not init_libJPEG then
  4637. raise Exception.Create('SaveJPG - unable to initialize libJPEG.');
  4638. try
  4639. FillChar(jpeg{%H-}, SizeOf(jpeg_compress_struct), $00);
  4640. FillChar(jpeg_err{%H-}, SizeOf(jpeg_error_mgr), $00);
  4641. // error managment
  4642. jpeg.err := jpeg_std_error(@jpeg_err);
  4643. jpeg_err.error_exit := glBitmap_libJPEG_error_exit;
  4644. jpeg_err.output_message := glBitmap_libJPEG_output_message;
  4645. // compression struct
  4646. jpeg_create_compress(@jpeg);
  4647. // allocation space for streaming methods
  4648. jpeg.dest := jpeg.mem^.alloc_small(@jpeg, JPOOL_PERMANENT, SizeOf(glBitmap_libJPEG_dest_mgr));
  4649. // seeting up custom functions
  4650. with glBitmap_libJPEG_dest_mgr_ptr(jpeg.dest)^ do begin
  4651. pub.init_destination := glBitmap_libJPEG_init_destination;
  4652. pub.empty_output_buffer := glBitmap_libJPEG_empty_output_buffer;
  4653. pub.term_destination := glBitmap_libJPEG_term_destination;
  4654. pub.next_output_byte := @DestBuffer[1];
  4655. pub.free_in_buffer := Length(DestBuffer);
  4656. DestStream := aStream;
  4657. end;
  4658. // very important state
  4659. jpeg.global_state := CSTATE_START;
  4660. jpeg.image_width := Width;
  4661. jpeg.image_height := Height;
  4662. case Format of
  4663. tfAlpha8ub1, tfLuminance8ub1: begin
  4664. jpeg.input_components := 1;
  4665. jpeg.in_color_space := JCS_GRAYSCALE;
  4666. end;
  4667. tfRGB8ub3, tfBGR8ub3: begin
  4668. jpeg.input_components := 3;
  4669. jpeg.in_color_space := JCS_RGB;
  4670. end;
  4671. end;
  4672. jpeg_set_defaults(@jpeg);
  4673. jpeg_set_quality(@jpeg, 95, true);
  4674. jpeg_start_compress(@jpeg, true);
  4675. pTemp := Data;
  4676. if Format = tfBGR8ub3 then
  4677. GetMem(pTemp2, fRowSize)
  4678. else
  4679. pTemp2 := pTemp;
  4680. try
  4681. for Row := 0 to jpeg.image_height -1 do begin
  4682. // prepare row
  4683. if Format = tfBGR8ub3 then
  4684. CopyRow(pTemp2, pTemp)
  4685. else
  4686. pTemp2 := pTemp;
  4687. // write row
  4688. jpeg_write_scanlines(@jpeg, @pTemp2, 1);
  4689. inc(pTemp, fRowSize);
  4690. end;
  4691. finally
  4692. // free memory
  4693. if Format = tfBGR8ub3 then
  4694. FreeMem(pTemp2);
  4695. end;
  4696. jpeg_finish_compress(@jpeg);
  4697. jpeg_destroy_compress(@jpeg);
  4698. finally
  4699. quit_libJPEG;
  4700. end;
  4701. end;
  4702. {$ELSEIF DEFINED(GLB_DELPHI_JPEG)}
  4703. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4704. procedure TglBitmapData.SaveJPEG(const aStream: TStream);
  4705. var
  4706. Bmp: TBitmap;
  4707. Jpg: TJPEGImage;
  4708. begin
  4709. if not (ftJPEG in FormatGetSupportedFiles(Format)) then
  4710. raise EglBitmapUnsupportedFormat.Create(Format);
  4711. Bmp := TBitmap.Create;
  4712. try
  4713. Jpg := TJPEGImage.Create;
  4714. try
  4715. AssignToBitmap(Bmp);
  4716. if (Format in [tfAlpha8ub1, tfLuminance8ub1]) then begin
  4717. Jpg.Grayscale := true;
  4718. Jpg.PixelFormat := jf8Bit;
  4719. end;
  4720. Jpg.Assign(Bmp);
  4721. Jpg.SaveToStream(aStream);
  4722. finally
  4723. FreeAndNil(Jpg);
  4724. end;
  4725. finally
  4726. FreeAndNil(Bmp);
  4727. end;
  4728. end;
  4729. {$IFEND}
  4730. {$ENDIF}
  4731. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4732. //RAW/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4733. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4734. type
  4735. RawHeader = packed record
  4736. Magic: String[5];
  4737. Version: Byte;
  4738. Width: Integer;
  4739. Height: Integer;
  4740. DataSize: Integer;
  4741. BitsPerPixel: Integer;
  4742. Precision: TglBitmapRec4ub;
  4743. Shift: TglBitmapRec4ub;
  4744. end;
  4745. function TglBitmapData.LoadRAW(const aStream: TStream): Boolean;
  4746. var
  4747. header: RawHeader;
  4748. StartPos: Int64;
  4749. fd: TFormatDescriptor;
  4750. buf: PByte;
  4751. begin
  4752. result := false;
  4753. StartPos := aStream.Position;
  4754. aStream.Read(header{%H-}, SizeOf(header));
  4755. if (header.Magic <> 'glBMP') then begin
  4756. aStream.Position := StartPos;
  4757. exit;
  4758. end;
  4759. fd := TFormatDescriptor.GetFromPrecShift(header.Precision, header.Shift, header.BitsPerPixel);
  4760. if (fd.Format = tfEmpty) then
  4761. raise EglBitmapUnsupportedFormat.Create('no supported format found');
  4762. buf := GetMemory(header.DataSize);
  4763. aStream.Read(buf^, header.DataSize);
  4764. SetData(buf, fd.Format, header.Width, header.Height);
  4765. result := true;
  4766. end;
  4767. procedure TglBitmapData.SaveRAW(const aStream: TStream);
  4768. var
  4769. header: RawHeader;
  4770. fd: TFormatDescriptor;
  4771. begin
  4772. fd := TFormatDescriptor.Get(Format);
  4773. header.Magic := 'glBMP';
  4774. header.Version := 1;
  4775. header.Width := Width;
  4776. header.Height := Height;
  4777. header.DataSize := fd.GetSize(fDimension);
  4778. header.BitsPerPixel := fd.BitsPerPixel;
  4779. header.Precision := fd.Precision;
  4780. header.Shift := fd.Shift;
  4781. aStream.Write(header, SizeOf(header));
  4782. aStream.Write(Data^, header.DataSize);
  4783. end;
  4784. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4785. //BMP/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4786. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4787. const
  4788. BMP_MAGIC = $4D42;
  4789. BMP_COMP_RGB = 0;
  4790. BMP_COMP_RLE8 = 1;
  4791. BMP_COMP_RLE4 = 2;
  4792. BMP_COMP_BITFIELDS = 3;
  4793. type
  4794. TBMPHeader = packed record
  4795. bfType: Word;
  4796. bfSize: Cardinal;
  4797. bfReserved1: Word;
  4798. bfReserved2: Word;
  4799. bfOffBits: Cardinal;
  4800. end;
  4801. TBMPInfo = packed record
  4802. biSize: Cardinal;
  4803. biWidth: Longint;
  4804. biHeight: Longint;
  4805. biPlanes: Word;
  4806. biBitCount: Word;
  4807. biCompression: Cardinal;
  4808. biSizeImage: Cardinal;
  4809. biXPelsPerMeter: Longint;
  4810. biYPelsPerMeter: Longint;
  4811. biClrUsed: Cardinal;
  4812. biClrImportant: Cardinal;
  4813. end;
  4814. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4815. function TglBitmapData.LoadBMP(const aStream: TStream): Boolean;
  4816. //////////////////////////////////////////////////////////////////////////////////////////////////
  4817. function ReadInfo(out aInfo: TBMPInfo; out aMask: TglBitmapRec4ul): TglBitmapFormat;
  4818. var
  4819. tmp, i: Cardinal;
  4820. begin
  4821. result := tfEmpty;
  4822. aStream.Read(aInfo{%H-}, SizeOf(aInfo));
  4823. FillChar(aMask{%H-}, SizeOf(aMask), 0);
  4824. //Read Compression
  4825. case aInfo.biCompression of
  4826. BMP_COMP_RLE4,
  4827. BMP_COMP_RLE8: begin
  4828. raise EglBitmap.Create('RLE compression is not supported');
  4829. end;
  4830. BMP_COMP_BITFIELDS: begin
  4831. if (aInfo.biBitCount = 16) or (aInfo.biBitCount = 32) then begin
  4832. for i := 0 to 2 do begin
  4833. aStream.Read(tmp{%H-}, SizeOf(tmp));
  4834. aMask.arr[i] := tmp;
  4835. end;
  4836. end else
  4837. raise EglBitmap.Create('Bitfields are only supported for 16bit and 32bit formats');
  4838. end;
  4839. end;
  4840. //get suitable format
  4841. case aInfo.biBitCount of
  4842. 8: result := tfLuminance8ub1;
  4843. 16: result := tfX1RGB5us1;
  4844. 24: result := tfBGR8ub3;
  4845. 32: result := tfXRGB8ui1;
  4846. end;
  4847. end;
  4848. function ReadColorTable(var aFormat: TglBitmapFormat; const aInfo: TBMPInfo): TbmpColorTableFormat;
  4849. var
  4850. i, c: Integer;
  4851. fd: TFormatDescriptor;
  4852. ColorTable: TbmpColorTable;
  4853. begin
  4854. result := nil;
  4855. if (aInfo.biBitCount >= 16) then
  4856. exit;
  4857. aFormat := tfLuminance8ub1;
  4858. c := aInfo.biClrUsed;
  4859. if (c = 0) then
  4860. c := 1 shl aInfo.biBitCount;
  4861. SetLength(ColorTable, c);
  4862. for i := 0 to c-1 do begin
  4863. aStream.Read(ColorTable[i], SizeOf(TbmpColorTableEnty));
  4864. if (ColorTable[i].r <> ColorTable[i].g) or (ColorTable[i].g <> ColorTable[i].b) then
  4865. aFormat := tfRGB8ub3;
  4866. end;
  4867. fd := TFormatDescriptor.Get(aFormat);
  4868. result := TbmpColorTableFormat.Create;
  4869. result.ColorTable := ColorTable;
  4870. result.SetCustomValues(aFormat, aInfo.biBitCount, fd.Precision, fd.Shift);
  4871. end;
  4872. //////////////////////////////////////////////////////////////////////////////////////////////////
  4873. function CheckBitfields(var aFormat: TglBitmapFormat; const aMask: TglBitmapRec4ul; const aInfo: TBMPInfo): TbmpBitfieldFormat;
  4874. var
  4875. fd: TFormatDescriptor;
  4876. begin
  4877. result := nil;
  4878. if (aMask.r <> 0) or (aMask.g <> 0) or (aMask.b <> 0) or (aMask.a <> 0) then begin
  4879. // find suitable format ...
  4880. fd := TFormatDescriptor.GetFromMask(aMask);
  4881. if (fd.Format <> tfEmpty) then begin
  4882. aFormat := fd.Format;
  4883. exit;
  4884. end;
  4885. // or create custom bitfield format
  4886. result := TbmpBitfieldFormat.Create;
  4887. result.SetCustomValues(aInfo.biBitCount, aMask);
  4888. end;
  4889. end;
  4890. var
  4891. //simple types
  4892. StartPos: Int64;
  4893. ImageSize, rbLineSize, wbLineSize, Padding, i: Integer;
  4894. PaddingBuff: Cardinal;
  4895. LineBuf, ImageData, TmpData: PByte;
  4896. SourceMD, DestMD: Pointer;
  4897. BmpFormat: TglBitmapFormat;
  4898. //records
  4899. Mask: TglBitmapRec4ul;
  4900. Header: TBMPHeader;
  4901. Info: TBMPInfo;
  4902. //classes
  4903. SpecialFormat: TFormatDescriptor;
  4904. FormatDesc: TFormatDescriptor;
  4905. //////////////////////////////////////////////////////////////////////////////////////////////////
  4906. procedure SpecialFormatReadLine(aData: PByte; aLineBuf: PByte);
  4907. var
  4908. i: Integer;
  4909. Pixel: TglBitmapPixelData;
  4910. begin
  4911. aStream.Read(aLineBuf^, rbLineSize);
  4912. SpecialFormat.PreparePixel(Pixel);
  4913. for i := 0 to Info.biWidth-1 do begin
  4914. SpecialFormat.Unmap(aLineBuf, Pixel, SourceMD);
  4915. glBitmapConvertPixel(Pixel, SpecialFormat, FormatDesc);
  4916. FormatDesc.Map(Pixel, aData, DestMD);
  4917. end;
  4918. end;
  4919. begin
  4920. result := false;
  4921. BmpFormat := tfEmpty;
  4922. SpecialFormat := nil;
  4923. LineBuf := nil;
  4924. SourceMD := nil;
  4925. DestMD := nil;
  4926. // Header
  4927. StartPos := aStream.Position;
  4928. aStream.Read(Header{%H-}, SizeOf(Header));
  4929. if Header.bfType = BMP_MAGIC then begin
  4930. try try
  4931. BmpFormat := ReadInfo(Info, Mask);
  4932. SpecialFormat := ReadColorTable(BmpFormat, Info);
  4933. if not Assigned(SpecialFormat) then
  4934. SpecialFormat := CheckBitfields(BmpFormat, Mask, Info);
  4935. aStream.Position := StartPos + Header.bfOffBits;
  4936. if (BmpFormat <> tfEmpty) then begin
  4937. FormatDesc := TFormatDescriptor.Get(BmpFormat);
  4938. rbLineSize := Round(Info.biWidth * Info.biBitCount / 8); //ReadBuffer LineSize
  4939. wbLineSize := Trunc(Info.biWidth * FormatDesc.BytesPerPixel);
  4940. Padding := (((Info.biWidth * Info.biBitCount + 31) and - 32) shr 3) - rbLineSize;
  4941. //get Memory
  4942. DestMD := FormatDesc.CreateMappingData;
  4943. ImageSize := FormatDesc.GetSize(Info.biWidth, abs(Info.biHeight));
  4944. GetMem(ImageData, ImageSize);
  4945. if Assigned(SpecialFormat) then begin
  4946. GetMem(LineBuf, rbLineSize); //tmp Memory for converting Bitfields
  4947. SourceMD := SpecialFormat.CreateMappingData;
  4948. end;
  4949. //read Data
  4950. try try
  4951. FillChar(ImageData^, ImageSize, $FF);
  4952. TmpData := ImageData;
  4953. if (Info.biHeight > 0) then
  4954. Inc(TmpData, wbLineSize * (Info.biHeight-1));
  4955. for i := 0 to Abs(Info.biHeight)-1 do begin
  4956. if Assigned(SpecialFormat) then
  4957. SpecialFormatReadLine(TmpData, LineBuf) //if is special format read and convert data
  4958. else
  4959. aStream.Read(TmpData^, wbLineSize); //else only read data
  4960. if (Info.biHeight > 0) then
  4961. dec(TmpData, wbLineSize)
  4962. else
  4963. inc(TmpData, wbLineSize);
  4964. aStream.Read(PaddingBuff{%H-}, Padding);
  4965. end;
  4966. SetData(ImageData, BmpFormat, Info.biWidth, abs(Info.biHeight));
  4967. result := true;
  4968. finally
  4969. if Assigned(LineBuf) then
  4970. FreeMem(LineBuf);
  4971. if Assigned(SourceMD) then
  4972. SpecialFormat.FreeMappingData(SourceMD);
  4973. FormatDesc.FreeMappingData(DestMD);
  4974. end;
  4975. except
  4976. if Assigned(ImageData) then
  4977. FreeMem(ImageData);
  4978. raise;
  4979. end;
  4980. end else
  4981. raise EglBitmap.Create('LoadBMP - No suitable format found');
  4982. except
  4983. aStream.Position := StartPos;
  4984. raise;
  4985. end;
  4986. finally
  4987. FreeAndNil(SpecialFormat);
  4988. end;
  4989. end
  4990. else aStream.Position := StartPos;
  4991. end;
  4992. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4993. procedure TglBitmapData.SaveBMP(const aStream: TStream);
  4994. var
  4995. Header: TBMPHeader;
  4996. Info: TBMPInfo;
  4997. Converter: TFormatDescriptor;
  4998. FormatDesc: TFormatDescriptor;
  4999. SourceFD, DestFD: Pointer;
  5000. pData, srcData, dstData, ConvertBuffer: pByte;
  5001. Pixel: TglBitmapPixelData;
  5002. ImageSize, wbLineSize, rbLineSize, Padding, LineIdx, PixelIdx: Integer;
  5003. RedMask, GreenMask, BlueMask, AlphaMask: Cardinal;
  5004. PaddingBuff: Cardinal;
  5005. function GetLineWidth : Integer;
  5006. begin
  5007. result := ((Info.biWidth * Info.biBitCount + 31) and - 32) shr 3;
  5008. end;
  5009. begin
  5010. if not (ftBMP in FormatGetSupportedFiles(Format)) then
  5011. raise EglBitmapUnsupportedFormat.Create(Format);
  5012. Converter := nil;
  5013. FormatDesc := TFormatDescriptor.Get(Format);
  5014. ImageSize := FormatDesc.GetSize(Dimension);
  5015. FillChar(Header{%H-}, SizeOf(Header), 0);
  5016. Header.bfType := BMP_MAGIC;
  5017. Header.bfSize := SizeOf(Header) + SizeOf(Info) + ImageSize;
  5018. Header.bfReserved1 := 0;
  5019. Header.bfReserved2 := 0;
  5020. Header.bfOffBits := SizeOf(Header) + SizeOf(Info);
  5021. FillChar(Info{%H-}, SizeOf(Info), 0);
  5022. Info.biSize := SizeOf(Info);
  5023. Info.biWidth := Width;
  5024. Info.biHeight := Height;
  5025. Info.biPlanes := 1;
  5026. Info.biCompression := BMP_COMP_RGB;
  5027. Info.biSizeImage := ImageSize;
  5028. try
  5029. case Format of
  5030. tfAlpha4ub1, tfAlpha8ub1, tfLuminance4ub1, tfLuminance8ub1, tfR3G3B2ub1:
  5031. begin
  5032. Info.biBitCount := 8;
  5033. Header.bfSize := Header.bfSize + 256 * SizeOf(Cardinal);
  5034. Header.bfOffBits := Header.bfOffBits + 256 * SizeOf(Cardinal); //256 ColorTable entries
  5035. Converter := TbmpColorTableFormat.Create;
  5036. with (Converter as TbmpColorTableFormat) do begin
  5037. SetCustomValues(fFormat, 8, FormatDesc.Precision, FormatDesc.Shift);
  5038. CreateColorTable;
  5039. end;
  5040. end;
  5041. tfLuminance4Alpha4ub2, tfLuminance6Alpha2ub2, tfLuminance8Alpha8ub2,
  5042. tfRGBX4us1, tfXRGB4us1, tfRGB5X1us1, tfX1RGB5us1, tfR5G6B5us1, tfRGB5A1us1, tfA1RGB5us1, tfRGBA4us1, tfARGB4us1,
  5043. tfBGRX4us1, tfXBGR4us1, tfBGR5X1us1, tfX1BGR5us1, tfB5G6R5us1, tfBGR5A1us1, tfA1BGR5us1, tfBGRA4us1, tfABGR4us1:
  5044. begin
  5045. Info.biBitCount := 16;
  5046. Info.biCompression := BMP_COMP_BITFIELDS;
  5047. end;
  5048. tfBGR8ub3, tfRGB8ub3:
  5049. begin
  5050. Info.biBitCount := 24;
  5051. if (Format = tfRGB8ub3) then
  5052. Converter := TfdBGR8ub3.Create; //use BGR8 Format Descriptor to Swap RGB Values
  5053. end;
  5054. tfRGBX8ui1, tfXRGB8ui1, tfRGB10X2ui1, tfX2RGB10ui1, tfRGBA8ui1, tfARGB8ui1, tfRGBA8ub4, tfRGB10A2ui1, tfA2RGB10ui1,
  5055. tfBGRX8ui1, tfXBGR8ui1, tfBGR10X2ui1, tfX2BGR10ui1, tfBGRA8ui1, tfABGR8ui1, tfBGRA8ub4, tfBGR10A2ui1, tfA2BGR10ui1:
  5056. begin
  5057. Info.biBitCount := 32;
  5058. Info.biCompression := BMP_COMP_BITFIELDS;
  5059. end;
  5060. else
  5061. raise EglBitmapUnsupportedFormat.Create(Format);
  5062. end;
  5063. Info.biXPelsPerMeter := 2835;
  5064. Info.biYPelsPerMeter := 2835;
  5065. // prepare bitmasks
  5066. if Info.biCompression = BMP_COMP_BITFIELDS then begin
  5067. Header.bfSize := Header.bfSize + 4 * SizeOf(Cardinal);
  5068. Header.bfOffBits := Header.bfOffBits + 4 * SizeOf(Cardinal);
  5069. RedMask := FormatDesc.Mask.r;
  5070. GreenMask := FormatDesc.Mask.g;
  5071. BlueMask := FormatDesc.Mask.b;
  5072. AlphaMask := FormatDesc.Mask.a;
  5073. end;
  5074. // headers
  5075. aStream.Write(Header, SizeOf(Header));
  5076. aStream.Write(Info, SizeOf(Info));
  5077. // colortable
  5078. if Assigned(Converter) and (Converter is TbmpColorTableFormat) then
  5079. with (Converter as TbmpColorTableFormat) do
  5080. aStream.Write(ColorTable[0].b,
  5081. SizeOf(TbmpColorTableEnty) * Length(ColorTable));
  5082. // bitmasks
  5083. if Info.biCompression = BMP_COMP_BITFIELDS then begin
  5084. aStream.Write(RedMask, SizeOf(Cardinal));
  5085. aStream.Write(GreenMask, SizeOf(Cardinal));
  5086. aStream.Write(BlueMask, SizeOf(Cardinal));
  5087. aStream.Write(AlphaMask, SizeOf(Cardinal));
  5088. end;
  5089. // image data
  5090. rbLineSize := Round(Info.biWidth * FormatDesc.BytesPerPixel);
  5091. wbLineSize := Round(Info.biWidth * Info.biBitCount / 8);
  5092. Padding := GetLineWidth - wbLineSize;
  5093. PaddingBuff := 0;
  5094. pData := Data;
  5095. inc(pData, (Height-1) * rbLineSize);
  5096. // prepare row buffer. But only for RGB because RGBA supports color masks
  5097. // so it's possible to change color within the image.
  5098. if Assigned(Converter) then begin
  5099. FormatDesc.PreparePixel(Pixel);
  5100. GetMem(ConvertBuffer, wbLineSize);
  5101. SourceFD := FormatDesc.CreateMappingData;
  5102. DestFD := Converter.CreateMappingData;
  5103. end else
  5104. ConvertBuffer := nil;
  5105. try
  5106. for LineIdx := 0 to Height - 1 do begin
  5107. // preparing row
  5108. if Assigned(Converter) then begin
  5109. srcData := pData;
  5110. dstData := ConvertBuffer;
  5111. for PixelIdx := 0 to Info.biWidth-1 do begin
  5112. FormatDesc.Unmap(srcData, Pixel, SourceFD);
  5113. glBitmapConvertPixel(Pixel, FormatDesc, Converter);
  5114. Converter.Map(Pixel, dstData, DestFD);
  5115. end;
  5116. aStream.Write(ConvertBuffer^, wbLineSize);
  5117. end else begin
  5118. aStream.Write(pData^, rbLineSize);
  5119. end;
  5120. dec(pData, rbLineSize);
  5121. if (Padding > 0) then
  5122. aStream.Write(PaddingBuff, Padding);
  5123. end;
  5124. finally
  5125. // destroy row buffer
  5126. if Assigned(ConvertBuffer) then begin
  5127. FormatDesc.FreeMappingData(SourceFD);
  5128. Converter.FreeMappingData(DestFD);
  5129. FreeMem(ConvertBuffer);
  5130. end;
  5131. end;
  5132. finally
  5133. if Assigned(Converter) then
  5134. Converter.Free;
  5135. end;
  5136. end;
  5137. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5138. //TGA/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5139. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5140. type
  5141. TTGAHeader = packed record
  5142. ImageID: Byte;
  5143. ColorMapType: Byte;
  5144. ImageType: Byte;
  5145. //ColorMapSpec: Array[0..4] of Byte;
  5146. ColorMapStart: Word;
  5147. ColorMapLength: Word;
  5148. ColorMapEntrySize: Byte;
  5149. OrigX: Word;
  5150. OrigY: Word;
  5151. Width: Word;
  5152. Height: Word;
  5153. Bpp: Byte;
  5154. ImageDesc: Byte;
  5155. end;
  5156. const
  5157. TGA_UNCOMPRESSED_RGB = 2;
  5158. TGA_UNCOMPRESSED_GRAY = 3;
  5159. TGA_COMPRESSED_RGB = 10;
  5160. TGA_COMPRESSED_GRAY = 11;
  5161. TGA_NONE_COLOR_TABLE = 0;
  5162. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5163. function TglBitmapData.LoadTGA(const aStream: TStream): Boolean;
  5164. var
  5165. Header: TTGAHeader;
  5166. ImageData: System.PByte;
  5167. StartPosition: Int64;
  5168. PixelSize, LineSize: Integer;
  5169. tgaFormat: TglBitmapFormat;
  5170. FormatDesc: TFormatDescriptor;
  5171. Counter: packed record
  5172. X, Y: packed record
  5173. low, high, dir: Integer;
  5174. end;
  5175. end;
  5176. const
  5177. CACHE_SIZE = $4000;
  5178. ////////////////////////////////////////////////////////////////////////////////////////
  5179. procedure ReadUncompressed;
  5180. var
  5181. i, j: Integer;
  5182. buf, tmp1, tmp2: System.PByte;
  5183. begin
  5184. buf := nil;
  5185. if (Counter.X.dir < 0) then
  5186. GetMem(buf, LineSize);
  5187. try
  5188. while (Counter.Y.low <> Counter.Y.high + counter.Y.dir) do begin
  5189. tmp1 := ImageData;
  5190. inc(tmp1, (Counter.Y.low * LineSize)); //pointer to LineStart
  5191. if (Counter.X.dir < 0) then begin //flip X
  5192. aStream.Read(buf^, LineSize);
  5193. tmp2 := buf;
  5194. inc(tmp2, LineSize - PixelSize); //pointer to last pixel in line
  5195. for i := 0 to Header.Width-1 do begin //for all pixels in line
  5196. for j := 0 to PixelSize-1 do begin //for all bytes in pixel
  5197. tmp1^ := tmp2^;
  5198. inc(tmp1);
  5199. inc(tmp2);
  5200. end;
  5201. dec(tmp2, 2*PixelSize); //move 2 backwards, because j-loop moved 1 forward
  5202. end;
  5203. end else
  5204. aStream.Read(tmp1^, LineSize);
  5205. inc(Counter.Y.low, Counter.Y.dir); //move to next line index
  5206. end;
  5207. finally
  5208. if Assigned(buf) then
  5209. FreeMem(buf);
  5210. end;
  5211. end;
  5212. ////////////////////////////////////////////////////////////////////////////////////////
  5213. procedure ReadCompressed;
  5214. /////////////////////////////////////////////////////////////////
  5215. var
  5216. TmpData: System.PByte;
  5217. LinePixelsRead: Integer;
  5218. procedure CheckLine;
  5219. begin
  5220. if (LinePixelsRead >= Header.Width) then begin
  5221. LinePixelsRead := 0;
  5222. inc(Counter.Y.low, Counter.Y.dir); //next line index
  5223. TmpData := ImageData;
  5224. inc(TmpData, Counter.Y.low * LineSize); //set line
  5225. if (Counter.X.dir < 0) then //if x flipped then
  5226. inc(TmpData, LineSize - PixelSize); //set last pixel
  5227. end;
  5228. end;
  5229. /////////////////////////////////////////////////////////////////
  5230. var
  5231. Cache: PByte;
  5232. CacheSize, CachePos: Integer;
  5233. procedure CachedRead(out Buffer; Count: Integer);
  5234. var
  5235. BytesRead: Integer;
  5236. begin
  5237. if (CachePos + Count > CacheSize) then begin
  5238. //if buffer overflow save non read bytes
  5239. BytesRead := 0;
  5240. if (CacheSize - CachePos > 0) then begin
  5241. BytesRead := CacheSize - CachePos;
  5242. Move(PByteArray(Cache)^[CachePos], Buffer{%H-}, BytesRead);
  5243. inc(CachePos, BytesRead);
  5244. end;
  5245. //load cache from file
  5246. CacheSize := Min(CACHE_SIZE, aStream.Size - aStream.Position);
  5247. aStream.Read(Cache^, CacheSize);
  5248. CachePos := 0;
  5249. //read rest of requested bytes
  5250. if (Count - BytesRead > 0) then begin
  5251. Move(PByteArray(Cache)^[CachePos], TByteArray(Buffer)[BytesRead], Count - BytesRead);
  5252. inc(CachePos, Count - BytesRead);
  5253. end;
  5254. end else begin
  5255. //if no buffer overflow just read the data
  5256. Move(PByteArray(Cache)^[CachePos], Buffer, Count);
  5257. inc(CachePos, Count);
  5258. end;
  5259. end;
  5260. procedure PixelToBuffer(const aData: PByte; var aBuffer: PByte);
  5261. begin
  5262. case PixelSize of
  5263. 1: begin
  5264. aBuffer^ := aData^;
  5265. inc(aBuffer, Counter.X.dir);
  5266. end;
  5267. 2: begin
  5268. PWord(aBuffer)^ := PWord(aData)^;
  5269. inc(aBuffer, 2 * Counter.X.dir);
  5270. end;
  5271. 3: begin
  5272. PByteArray(aBuffer)^[0] := PByteArray(aData)^[0];
  5273. PByteArray(aBuffer)^[1] := PByteArray(aData)^[1];
  5274. PByteArray(aBuffer)^[2] := PByteArray(aData)^[2];
  5275. inc(aBuffer, 3 * Counter.X.dir);
  5276. end;
  5277. 4: begin
  5278. PCardinal(aBuffer)^ := PCardinal(aData)^;
  5279. inc(aBuffer, 4 * Counter.X.dir);
  5280. end;
  5281. end;
  5282. end;
  5283. var
  5284. TotalPixelsToRead, TotalPixelsRead: Integer;
  5285. Temp: Byte;
  5286. buf: array [0..3] of Byte; //1 pixel is max 32bit long
  5287. PixelRepeat: Boolean;
  5288. PixelsToRead, PixelCount: Integer;
  5289. begin
  5290. CacheSize := 0;
  5291. CachePos := 0;
  5292. TotalPixelsToRead := Header.Width * Header.Height;
  5293. TotalPixelsRead := 0;
  5294. LinePixelsRead := 0;
  5295. GetMem(Cache, CACHE_SIZE);
  5296. try
  5297. TmpData := ImageData;
  5298. inc(TmpData, Counter.Y.low * LineSize); //set line
  5299. if (Counter.X.dir < 0) then //if x flipped then
  5300. inc(TmpData, LineSize - PixelSize); //set last pixel
  5301. repeat
  5302. //read CommandByte
  5303. CachedRead(Temp, 1);
  5304. PixelRepeat := (Temp and $80) > 0;
  5305. PixelsToRead := (Temp and $7F) + 1;
  5306. inc(TotalPixelsRead, PixelsToRead);
  5307. if PixelRepeat then
  5308. CachedRead(buf[0], PixelSize);
  5309. while (PixelsToRead > 0) do begin
  5310. CheckLine;
  5311. PixelCount := Min(Header.Width - LinePixelsRead, PixelsToRead); //max read to EOL or EOF
  5312. while (PixelCount > 0) do begin
  5313. if not PixelRepeat then
  5314. CachedRead(buf[0], PixelSize);
  5315. PixelToBuffer(@buf[0], TmpData);
  5316. inc(LinePixelsRead);
  5317. dec(PixelsToRead);
  5318. dec(PixelCount);
  5319. end;
  5320. end;
  5321. until (TotalPixelsRead >= TotalPixelsToRead);
  5322. finally
  5323. FreeMem(Cache);
  5324. end;
  5325. end;
  5326. function IsGrayFormat: Boolean;
  5327. begin
  5328. result := Header.ImageType in [TGA_UNCOMPRESSED_GRAY, TGA_COMPRESSED_GRAY];
  5329. end;
  5330. begin
  5331. result := false;
  5332. // reading header to test file and set cursor back to begin
  5333. StartPosition := aStream.Position;
  5334. aStream.Read(Header{%H-}, SizeOf(Header));
  5335. // no colormapped files
  5336. if (Header.ColorMapType = TGA_NONE_COLOR_TABLE) and (Header.ImageType in [
  5337. TGA_UNCOMPRESSED_RGB, TGA_UNCOMPRESSED_GRAY, TGA_COMPRESSED_RGB, TGA_COMPRESSED_GRAY]) then
  5338. begin
  5339. try
  5340. if Header.ImageID <> 0 then // skip image ID
  5341. aStream.Position := aStream.Position + Header.ImageID;
  5342. tgaFormat := tfEmpty;
  5343. case Header.Bpp of
  5344. 8: if IsGrayFormat then case (Header.ImageDesc and $F) of
  5345. 0: tgaFormat := tfLuminance8ub1;
  5346. 8: tgaFormat := tfAlpha8ub1;
  5347. end;
  5348. 16: if IsGrayFormat then case (Header.ImageDesc and $F) of
  5349. 0: tgaFormat := tfLuminance16us1;
  5350. 8: tgaFormat := tfLuminance8Alpha8ub2;
  5351. end else case (Header.ImageDesc and $F) of
  5352. 0: tgaFormat := tfX1RGB5us1;
  5353. 1: tgaFormat := tfA1RGB5us1;
  5354. 4: tgaFormat := tfARGB4us1;
  5355. end;
  5356. 24: if not IsGrayFormat then case (Header.ImageDesc and $F) of
  5357. 0: tgaFormat := tfBGR8ub3;
  5358. end;
  5359. 32: if IsGrayFormat then case (Header.ImageDesc and $F) of
  5360. 0: tgaFormat := tfDepth32ui1;
  5361. end else case (Header.ImageDesc and $F) of
  5362. 0: tgaFormat := tfX2RGB10ui1;
  5363. 2: tgaFormat := tfA2RGB10ui1;
  5364. 8: tgaFormat := tfARGB8ui1;
  5365. end;
  5366. end;
  5367. if (tgaFormat = tfEmpty) then
  5368. raise EglBitmap.Create('LoadTga - unsupported format');
  5369. FormatDesc := TFormatDescriptor.Get(tgaFormat);
  5370. PixelSize := FormatDesc.GetSize(1, 1);
  5371. LineSize := FormatDesc.GetSize(Header.Width, 1);
  5372. GetMem(ImageData, LineSize * Header.Height);
  5373. try
  5374. //column direction
  5375. if ((Header.ImageDesc and (1 shl 4)) > 0) then begin
  5376. Counter.X.low := Header.Height-1;;
  5377. Counter.X.high := 0;
  5378. Counter.X.dir := -1;
  5379. end else begin
  5380. Counter.X.low := 0;
  5381. Counter.X.high := Header.Height-1;
  5382. Counter.X.dir := 1;
  5383. end;
  5384. // Row direction
  5385. if ((Header.ImageDesc and (1 shl 5)) > 0) then begin
  5386. Counter.Y.low := 0;
  5387. Counter.Y.high := Header.Height-1;
  5388. Counter.Y.dir := 1;
  5389. end else begin
  5390. Counter.Y.low := Header.Height-1;;
  5391. Counter.Y.high := 0;
  5392. Counter.Y.dir := -1;
  5393. end;
  5394. // Read Image
  5395. case Header.ImageType of
  5396. TGA_UNCOMPRESSED_RGB, TGA_UNCOMPRESSED_GRAY:
  5397. ReadUncompressed;
  5398. TGA_COMPRESSED_RGB, TGA_COMPRESSED_GRAY:
  5399. ReadCompressed;
  5400. end;
  5401. SetData(ImageData, tgaFormat, Header.Width, Header.Height);
  5402. result := true;
  5403. except
  5404. if Assigned(ImageData) then
  5405. FreeMem(ImageData);
  5406. raise;
  5407. end;
  5408. finally
  5409. aStream.Position := StartPosition;
  5410. end;
  5411. end
  5412. else aStream.Position := StartPosition;
  5413. end;
  5414. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5415. procedure TglBitmapData.SaveTGA(const aStream: TStream);
  5416. var
  5417. Header: TTGAHeader;
  5418. Size: Integer;
  5419. FormatDesc: TFormatDescriptor;
  5420. begin
  5421. if not (ftTGA in FormatGetSupportedFiles(Format)) then
  5422. raise EglBitmapUnsupportedFormat.Create(Format);
  5423. //prepare header
  5424. FormatDesc := TFormatDescriptor.Get(Format);
  5425. FillChar(Header{%H-}, SizeOf(Header), 0);
  5426. Header.ImageDesc := CountSetBits(FormatDesc.Range.a) and $F;
  5427. Header.Bpp := FormatDesc.BitsPerPixel;
  5428. Header.Width := Width;
  5429. Header.Height := Height;
  5430. Header.ImageDesc := Header.ImageDesc or $20; //flip y
  5431. if FormatDesc.IsGrayscale or (not FormatDesc.IsGrayscale and not FormatDesc.HasRed and FormatDesc.HasAlpha) then
  5432. Header.ImageType := TGA_UNCOMPRESSED_GRAY
  5433. else
  5434. Header.ImageType := TGA_UNCOMPRESSED_RGB;
  5435. aStream.Write(Header, SizeOf(Header));
  5436. // write Data
  5437. Size := FormatDesc.GetSize(Dimension);
  5438. aStream.Write(Data^, Size);
  5439. end;
  5440. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5441. //DDS/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5442. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5443. const
  5444. DDS_MAGIC: Cardinal = $20534444;
  5445. // DDS_header.dwFlags
  5446. DDSD_CAPS = $00000001;
  5447. DDSD_HEIGHT = $00000002;
  5448. DDSD_WIDTH = $00000004;
  5449. DDSD_PIXELFORMAT = $00001000;
  5450. // DDS_header.sPixelFormat.dwFlags
  5451. DDPF_ALPHAPIXELS = $00000001;
  5452. DDPF_ALPHA = $00000002;
  5453. DDPF_FOURCC = $00000004;
  5454. DDPF_RGB = $00000040;
  5455. DDPF_LUMINANCE = $00020000;
  5456. // DDS_header.sCaps.dwCaps1
  5457. DDSCAPS_TEXTURE = $00001000;
  5458. // DDS_header.sCaps.dwCaps2
  5459. DDSCAPS2_CUBEMAP = $00000200;
  5460. D3DFMT_DXT1 = $31545844;
  5461. D3DFMT_DXT3 = $33545844;
  5462. D3DFMT_DXT5 = $35545844;
  5463. type
  5464. TDDSPixelFormat = packed record
  5465. dwSize: Cardinal;
  5466. dwFlags: Cardinal;
  5467. dwFourCC: Cardinal;
  5468. dwRGBBitCount: Cardinal;
  5469. dwRBitMask: Cardinal;
  5470. dwGBitMask: Cardinal;
  5471. dwBBitMask: Cardinal;
  5472. dwABitMask: Cardinal;
  5473. end;
  5474. TDDSCaps = packed record
  5475. dwCaps1: Cardinal;
  5476. dwCaps2: Cardinal;
  5477. dwDDSX: Cardinal;
  5478. dwReserved: Cardinal;
  5479. end;
  5480. TDDSHeader = packed record
  5481. dwSize: Cardinal;
  5482. dwFlags: Cardinal;
  5483. dwHeight: Cardinal;
  5484. dwWidth: Cardinal;
  5485. dwPitchOrLinearSize: Cardinal;
  5486. dwDepth: Cardinal;
  5487. dwMipMapCount: Cardinal;
  5488. dwReserved: array[0..10] of Cardinal;
  5489. PixelFormat: TDDSPixelFormat;
  5490. Caps: TDDSCaps;
  5491. dwReserved2: Cardinal;
  5492. end;
  5493. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5494. function TglBitmapData.LoadDDS(const aStream: TStream): Boolean;
  5495. var
  5496. Header: TDDSHeader;
  5497. Converter: TbmpBitfieldFormat;
  5498. function GetDDSFormat: TglBitmapFormat;
  5499. var
  5500. fd: TFormatDescriptor;
  5501. i: Integer;
  5502. Mask: TglBitmapRec4ul;
  5503. Range: TglBitmapRec4ui;
  5504. match: Boolean;
  5505. begin
  5506. result := tfEmpty;
  5507. with Header.PixelFormat do begin
  5508. // Compresses
  5509. if ((dwFlags and DDPF_FOURCC) > 0) then begin
  5510. case Header.PixelFormat.dwFourCC of
  5511. D3DFMT_DXT1: result := tfS3tcDtx1RGBA;
  5512. D3DFMT_DXT3: result := tfS3tcDtx3RGBA;
  5513. D3DFMT_DXT5: result := tfS3tcDtx5RGBA;
  5514. end;
  5515. end else if ((dwFlags and (DDPF_RGB or DDPF_ALPHAPIXELS or DDPF_LUMINANCE or DDPF_ALPHA)) > 0) then begin
  5516. // prepare masks
  5517. if ((dwFlags and DDPF_LUMINANCE) = 0) then begin
  5518. Mask.r := dwRBitMask;
  5519. Mask.g := dwGBitMask;
  5520. Mask.b := dwBBitMask;
  5521. end else begin
  5522. Mask.r := dwRBitMask;
  5523. Mask.g := dwRBitMask;
  5524. Mask.b := dwRBitMask;
  5525. end;
  5526. if (dwFlags and DDPF_ALPHAPIXELS > 0) then
  5527. Mask.a := dwABitMask
  5528. else
  5529. Mask.a := 0;;
  5530. //find matching format
  5531. fd := TFormatDescriptor.GetFromMask(Mask, dwRGBBitCount);
  5532. result := fd.Format;
  5533. if (result <> tfEmpty) then
  5534. exit;
  5535. //find format with same Range
  5536. for i := 0 to 3 do
  5537. Range.arr[i] := (2 shl CountSetBits(Mask.arr[i])) - 1;
  5538. for result := High(TglBitmapFormat) downto Low(TglBitmapFormat) do begin
  5539. fd := TFormatDescriptor.Get(result);
  5540. match := true;
  5541. for i := 0 to 3 do
  5542. if (fd.Range.arr[i] <> Range.arr[i]) then begin
  5543. match := false;
  5544. break;
  5545. end;
  5546. if match then
  5547. break;
  5548. end;
  5549. //no format with same range found -> use default
  5550. if (result = tfEmpty) then begin
  5551. if (dwABitMask > 0) then
  5552. result := tfRGBA8ui1
  5553. else
  5554. result := tfRGB8ub3;
  5555. end;
  5556. Converter := TbmpBitfieldFormat.Create;
  5557. Converter.SetCustomValues(dwRGBBitCount, glBitmapRec4ul(dwRBitMask, dwGBitMask, dwBBitMask, dwABitMask));
  5558. end;
  5559. end;
  5560. end;
  5561. var
  5562. StreamPos: Int64;
  5563. x, y, LineSize, RowSize, Magic: Cardinal;
  5564. NewImage, TmpData, RowData, SrcData: System.PByte;
  5565. SourceMD, DestMD: Pointer;
  5566. Pixel: TglBitmapPixelData;
  5567. ddsFormat: TglBitmapFormat;
  5568. FormatDesc: TFormatDescriptor;
  5569. begin
  5570. result := false;
  5571. Converter := nil;
  5572. StreamPos := aStream.Position;
  5573. // Magic
  5574. aStream.Read(Magic{%H-}, sizeof(Magic));
  5575. if (Magic <> DDS_MAGIC) then begin
  5576. aStream.Position := StreamPos;
  5577. exit;
  5578. end;
  5579. //Header
  5580. aStream.Read(Header{%H-}, sizeof(Header));
  5581. if (Header.dwSize <> SizeOf(Header)) or
  5582. ((Header.dwFlags and (DDSD_PIXELFORMAT or DDSD_CAPS or DDSD_WIDTH or DDSD_HEIGHT)) <>
  5583. (DDSD_PIXELFORMAT or DDSD_CAPS or DDSD_WIDTH or DDSD_HEIGHT)) then
  5584. begin
  5585. aStream.Position := StreamPos;
  5586. exit;
  5587. end;
  5588. if ((Header.Caps.dwCaps1 and DDSCAPS2_CUBEMAP) > 0) then
  5589. raise EglBitmap.Create('LoadDDS - CubeMaps are not supported');
  5590. ddsFormat := GetDDSFormat;
  5591. try
  5592. if (ddsFormat = tfEmpty) then
  5593. raise EglBitmap.Create('LoadDDS - unsupported Pixelformat found.');
  5594. FormatDesc := TFormatDescriptor.Get(ddsFormat);
  5595. LineSize := Trunc(Header.dwWidth * FormatDesc.BytesPerPixel);
  5596. GetMem(NewImage, Header.dwHeight * LineSize);
  5597. try
  5598. TmpData := NewImage;
  5599. //Converter needed
  5600. if Assigned(Converter) then begin
  5601. RowSize := Round(Header.dwWidth * Header.PixelFormat.dwRGBBitCount / 8);
  5602. GetMem(RowData, RowSize);
  5603. SourceMD := Converter.CreateMappingData;
  5604. DestMD := FormatDesc.CreateMappingData;
  5605. try
  5606. for y := 0 to Header.dwHeight-1 do begin
  5607. TmpData := NewImage;
  5608. inc(TmpData, y * LineSize);
  5609. SrcData := RowData;
  5610. aStream.Read(SrcData^, RowSize);
  5611. for x := 0 to Header.dwWidth-1 do begin
  5612. Converter.Unmap(SrcData, Pixel, SourceMD);
  5613. glBitmapConvertPixel(Pixel, Converter, FormatDesc);
  5614. FormatDesc.Map(Pixel, TmpData, DestMD);
  5615. end;
  5616. end;
  5617. finally
  5618. Converter.FreeMappingData(SourceMD);
  5619. FormatDesc.FreeMappingData(DestMD);
  5620. FreeMem(RowData);
  5621. end;
  5622. end else
  5623. // Compressed
  5624. if ((Header.PixelFormat.dwFlags and DDPF_FOURCC) > 0) then begin
  5625. RowSize := Header.dwPitchOrLinearSize div Header.dwWidth;
  5626. for Y := 0 to Header.dwHeight-1 do begin
  5627. aStream.Read(TmpData^, RowSize);
  5628. Inc(TmpData, LineSize);
  5629. end;
  5630. end else
  5631. // Uncompressed
  5632. if (Header.PixelFormat.dwFlags and (DDPF_RGB or DDPF_ALPHAPIXELS or DDPF_LUMINANCE)) > 0 then begin
  5633. RowSize := (Header.PixelFormat.dwRGBBitCount * Header.dwWidth) shr 3;
  5634. for Y := 0 to Header.dwHeight-1 do begin
  5635. aStream.Read(TmpData^, RowSize);
  5636. Inc(TmpData, LineSize);
  5637. end;
  5638. end else
  5639. raise EglBitmap.Create('LoadDDS - unsupported Pixelformat found.');
  5640. SetData(NewImage, ddsFormat, Header.dwWidth, Header.dwHeight);
  5641. result := true;
  5642. except
  5643. if Assigned(NewImage) then
  5644. FreeMem(NewImage);
  5645. raise;
  5646. end;
  5647. finally
  5648. FreeAndNil(Converter);
  5649. end;
  5650. end;
  5651. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5652. procedure TglBitmapData.SaveDDS(const aStream: TStream);
  5653. var
  5654. Header: TDDSHeader;
  5655. FormatDesc: TFormatDescriptor;
  5656. begin
  5657. if not (ftDDS in FormatGetSupportedFiles(Format)) then
  5658. raise EglBitmapUnsupportedFormat.Create(Format);
  5659. FormatDesc := TFormatDescriptor.Get(Format);
  5660. // Generell
  5661. FillChar(Header{%H-}, SizeOf(Header), 0);
  5662. Header.dwSize := SizeOf(Header);
  5663. Header.dwFlags := DDSD_WIDTH or DDSD_HEIGHT or DDSD_CAPS or DDSD_PIXELFORMAT;
  5664. Header.dwWidth := Max(1, Width);
  5665. Header.dwHeight := Max(1, Height);
  5666. // Caps
  5667. Header.Caps.dwCaps1 := DDSCAPS_TEXTURE;
  5668. // Pixelformat
  5669. Header.PixelFormat.dwSize := sizeof(Header);
  5670. if (FormatDesc.IsCompressed) then begin
  5671. Header.PixelFormat.dwFlags := Header.PixelFormat.dwFlags or DDPF_FOURCC;
  5672. case Format of
  5673. tfS3tcDtx1RGBA: Header.PixelFormat.dwFourCC := D3DFMT_DXT1;
  5674. tfS3tcDtx3RGBA: Header.PixelFormat.dwFourCC := D3DFMT_DXT3;
  5675. tfS3tcDtx5RGBA: Header.PixelFormat.dwFourCC := D3DFMT_DXT5;
  5676. end;
  5677. end else if not FormatDesc.HasColor and FormatDesc.HasAlpha then begin
  5678. Header.PixelFormat.dwFlags := Header.PixelFormat.dwFlags or DDPF_ALPHA;
  5679. Header.PixelFormat.dwRGBBitCount := FormatDesc.BitsPerPixel;
  5680. Header.PixelFormat.dwABitMask := FormatDesc.Mask.a;
  5681. end else if FormatDesc.IsGrayscale then begin
  5682. Header.PixelFormat.dwFlags := Header.PixelFormat.dwFlags or DDPF_LUMINANCE;
  5683. Header.PixelFormat.dwRGBBitCount := FormatDesc.BitsPerPixel;
  5684. Header.PixelFormat.dwRBitMask := FormatDesc.Mask.r;
  5685. Header.PixelFormat.dwABitMask := FormatDesc.Mask.a;
  5686. end else begin
  5687. Header.PixelFormat.dwFlags := Header.PixelFormat.dwFlags or DDPF_RGB;
  5688. Header.PixelFormat.dwRGBBitCount := FormatDesc.BitsPerPixel;
  5689. Header.PixelFormat.dwRBitMask := FormatDesc.Mask.r;
  5690. Header.PixelFormat.dwGBitMask := FormatDesc.Mask.g;
  5691. Header.PixelFormat.dwBBitMask := FormatDesc.Mask.b;
  5692. Header.PixelFormat.dwABitMask := FormatDesc.Mask.a;
  5693. end;
  5694. if (FormatDesc.HasAlpha) then
  5695. Header.PixelFormat.dwFlags := Header.PixelFormat.dwFlags or DDPF_ALPHAPIXELS;
  5696. aStream.Write(DDS_MAGIC, sizeof(DDS_MAGIC));
  5697. aStream.Write(Header, SizeOf(Header));
  5698. aStream.Write(Data^, FormatDesc.GetSize(Dimension));
  5699. end;
  5700. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5701. function TglBitmapData.FlipHorz: Boolean;
  5702. var
  5703. fd: TglBitmapFormatDescriptor;
  5704. Col, RowSize, PixelSize: Integer;
  5705. pTempDest, pDest, pSource: PByte;
  5706. begin
  5707. result := false;
  5708. fd := FormatDescriptor;
  5709. PixelSize := Ceil(fd.BytesPerPixel);
  5710. RowSize := fd.GetSize(Width, 1);
  5711. if Assigned(Data) and not fd.IsCompressed then begin
  5712. pSource := Data;
  5713. GetMem(pDest, RowSize);
  5714. try
  5715. pTempDest := pDest;
  5716. Inc(pTempDest, RowSize);
  5717. for Col := 0 to Width-1 do begin
  5718. dec(pTempDest, PixelSize); //dec before, because ptr is behind last byte of data
  5719. Move(pSource^, pTempDest^, PixelSize);
  5720. Inc(pSource, PixelSize);
  5721. end;
  5722. SetData(pDest, Format, Width);
  5723. result := true;
  5724. except
  5725. if Assigned(pDest) then
  5726. FreeMem(pDest);
  5727. raise;
  5728. end;
  5729. end;
  5730. end;
  5731. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5732. function TglBitmapData.FlipVert: Boolean;
  5733. var
  5734. fd: TglBitmapFormatDescriptor;
  5735. Row, RowSize, PixelSize: Integer;
  5736. TempDestData, DestData, SourceData: PByte;
  5737. begin
  5738. result := false;
  5739. fd := FormatDescriptor;
  5740. PixelSize := Ceil(fd.BytesPerPixel);
  5741. RowSize := fd.GetSize(Width, 1);
  5742. if Assigned(Data) then begin
  5743. SourceData := Data;
  5744. GetMem(DestData, Height * RowSize);
  5745. try
  5746. TempDestData := DestData;
  5747. Inc(TempDestData, Width * (Height -1) * PixelSize);
  5748. for Row := 0 to Height -1 do begin
  5749. Move(SourceData^, TempDestData^, RowSize);
  5750. Dec(TempDestData, RowSize);
  5751. Inc(SourceData, RowSize);
  5752. end;
  5753. SetData(DestData, Format, Width, Height);
  5754. result := true;
  5755. except
  5756. if Assigned(DestData) then
  5757. FreeMem(DestData);
  5758. raise;
  5759. end;
  5760. end;
  5761. end;
  5762. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5763. procedure TglBitmapData.LoadFromFile(const aFilename: String);
  5764. var
  5765. fs: TFileStream;
  5766. begin
  5767. if not FileExists(aFilename) then
  5768. raise EglBitmap.Create('file does not exist: ' + aFilename);
  5769. fs := TFileStream.Create(aFilename, fmOpenRead);
  5770. try
  5771. fs.Position := 0;
  5772. LoadFromStream(fs);
  5773. fFilename := aFilename;
  5774. finally
  5775. fs.Free;
  5776. end;
  5777. end;
  5778. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5779. procedure TglBitmapData.LoadFromStream(const aStream: TStream);
  5780. begin
  5781. {$IFDEF GLB_SUPPORT_PNG_READ}
  5782. if not LoadPNG(aStream) then
  5783. {$ENDIF}
  5784. {$IFDEF GLB_SUPPORT_JPEG_READ}
  5785. if not LoadJPEG(aStream) then
  5786. {$ENDIF}
  5787. if not LoadDDS(aStream) then
  5788. if not LoadTGA(aStream) then
  5789. if not LoadBMP(aStream) then
  5790. if not LoadRAW(aStream) then
  5791. raise EglBitmap.Create('LoadFromStream - Couldn''t load Stream. It''s possible to be an unknow Streamtype.');
  5792. end;
  5793. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5794. procedure TglBitmapData.LoadFromFunc(const aSize: TglBitmapSize; const aFormat: TglBitmapFormat;
  5795. const aFunc: TglBitmapFunction; const aArgs: Pointer);
  5796. var
  5797. tmpData: PByte;
  5798. size: Integer;
  5799. begin
  5800. size := TFormatDescriptor.Get(aFormat).GetSize(aSize);
  5801. GetMem(tmpData, size);
  5802. try
  5803. FillChar(tmpData^, size, #$FF);
  5804. SetData(tmpData, aFormat, aSize.X, aSize.Y);
  5805. except
  5806. if Assigned(tmpData) then
  5807. FreeMem(tmpData);
  5808. raise;
  5809. end;
  5810. Convert(Self, aFunc, false, aFormat, aArgs);
  5811. end;
  5812. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5813. procedure TglBitmapData.LoadFromResource(const aInstance: Cardinal; aResource: String; aResType: PChar);
  5814. var
  5815. rs: TResourceStream;
  5816. begin
  5817. PrepareResType(aResource, aResType);
  5818. rs := TResourceStream.Create(aInstance, aResource, aResType);
  5819. try
  5820. LoadFromStream(rs);
  5821. finally
  5822. rs.Free;
  5823. end;
  5824. end;
  5825. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5826. procedure TglBitmapData.LoadFromResourceID(const aInstance: Cardinal; const aResourceID: Integer; const aResType: PChar);
  5827. var
  5828. rs: TResourceStream;
  5829. begin
  5830. rs := TResourceStream.CreateFromID(aInstance, aResourceID, aResType);
  5831. try
  5832. LoadFromStream(rs);
  5833. finally
  5834. rs.Free;
  5835. end;
  5836. end;
  5837. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5838. procedure TglBitmapData.SaveToFile(const aFilename: String; const aFileType: TglBitmapFileType);
  5839. var
  5840. fs: TFileStream;
  5841. begin
  5842. fs := TFileStream.Create(aFileName, fmCreate);
  5843. try
  5844. fs.Position := 0;
  5845. SaveToStream(fs, aFileType);
  5846. finally
  5847. fs.Free;
  5848. end;
  5849. end;
  5850. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5851. procedure TglBitmapData.SaveToStream(const aStream: TStream; const aFileType: TglBitmapFileType);
  5852. begin
  5853. case aFileType of
  5854. {$IFDEF GLB_SUPPORT_PNG_WRITE}
  5855. ftPNG: SavePNG(aStream);
  5856. {$ENDIF}
  5857. {$IFDEF GLB_SUPPORT_JPEG_WRITE}
  5858. ftJPEG: SaveJPEG(aStream);
  5859. {$ENDIF}
  5860. ftDDS: SaveDDS(aStream);
  5861. ftTGA: SaveTGA(aStream);
  5862. ftBMP: SaveBMP(aStream);
  5863. ftRAW: SaveRAW(aStream);
  5864. end;
  5865. end;
  5866. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5867. function TglBitmapData.Convert(const aFunc: TglBitmapFunction; const aCreateTemp: Boolean; const aArgs: Pointer): Boolean;
  5868. begin
  5869. result := Convert(Self, aFunc, aCreateTemp, Format, aArgs);
  5870. end;
  5871. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5872. function TglBitmapData.Convert(const aSource: TglBitmapData; const aFunc: TglBitmapFunction; aCreateTemp: Boolean;
  5873. const aFormat: TglBitmapFormat; const aArgs: Pointer): Boolean;
  5874. var
  5875. DestData, TmpData, SourceData: pByte;
  5876. TempHeight, TempWidth: Integer;
  5877. SourceFD, DestFD: TFormatDescriptor;
  5878. SourceMD, DestMD: Pointer;
  5879. FuncRec: TglBitmapFunctionRec;
  5880. begin
  5881. Assert(Assigned(Data));
  5882. Assert(Assigned(aSource));
  5883. Assert(Assigned(aSource.Data));
  5884. result := false;
  5885. if Assigned(aSource.Data) and ((aSource.Height > 0) or (aSource.Width > 0)) then begin
  5886. SourceFD := TFormatDescriptor.Get(aSource.Format);
  5887. DestFD := TFormatDescriptor.Get(aFormat);
  5888. if (SourceFD.IsCompressed) then
  5889. raise EglBitmapUnsupportedFormat.Create('compressed formats are not supported: ', SourceFD.Format);
  5890. if (DestFD.IsCompressed) then
  5891. raise EglBitmapUnsupportedFormat.Create('compressed formats are not supported: ', DestFD.Format);
  5892. // inkompatible Formats so CreateTemp
  5893. if (SourceFD.BitsPerPixel <> DestFD.BitsPerPixel) then
  5894. aCreateTemp := true;
  5895. // Values
  5896. TempHeight := Max(1, aSource.Height);
  5897. TempWidth := Max(1, aSource.Width);
  5898. FuncRec.Sender := Self;
  5899. FuncRec.Args := aArgs;
  5900. TmpData := nil;
  5901. if aCreateTemp then begin
  5902. GetMem(TmpData, DestFD.GetSize(TempWidth, TempHeight));
  5903. DestData := TmpData;
  5904. end else
  5905. DestData := Data;
  5906. try
  5907. SourceFD.PreparePixel(FuncRec.Source);
  5908. DestFD.PreparePixel (FuncRec.Dest);
  5909. SourceMD := SourceFD.CreateMappingData;
  5910. DestMD := DestFD.CreateMappingData;
  5911. FuncRec.Size := aSource.Dimension;
  5912. FuncRec.Position.Fields := FuncRec.Size.Fields;
  5913. try
  5914. SourceData := aSource.Data;
  5915. FuncRec.Position.Y := 0;
  5916. while FuncRec.Position.Y < TempHeight do begin
  5917. FuncRec.Position.X := 0;
  5918. while FuncRec.Position.X < TempWidth do begin
  5919. SourceFD.Unmap(SourceData, FuncRec.Source, SourceMD);
  5920. aFunc(FuncRec);
  5921. DestFD.Map(FuncRec.Dest, DestData, DestMD);
  5922. inc(FuncRec.Position.X);
  5923. end;
  5924. inc(FuncRec.Position.Y);
  5925. end;
  5926. // Updating Image or InternalFormat
  5927. if aCreateTemp then
  5928. SetData(TmpData, aFormat, aSource.Width, aSource.Height)
  5929. else if (aFormat <> fFormat) then
  5930. Format := aFormat;
  5931. result := true;
  5932. finally
  5933. SourceFD.FreeMappingData(SourceMD);
  5934. DestFD.FreeMappingData(DestMD);
  5935. end;
  5936. except
  5937. if aCreateTemp and Assigned(TmpData) then
  5938. FreeMem(TmpData);
  5939. raise;
  5940. end;
  5941. end;
  5942. end;
  5943. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5944. function TglBitmapData.ConvertTo(const aFormat: TglBitmapFormat): Boolean;
  5945. var
  5946. SourceFD, DestFD: TFormatDescriptor;
  5947. SourcePD, DestPD: TglBitmapPixelData;
  5948. ShiftData: TShiftData;
  5949. function DataIsIdentical: Boolean;
  5950. begin
  5951. result := SourceFD.MaskMatch(DestFD.Mask);
  5952. end;
  5953. function CanCopyDirect: Boolean;
  5954. begin
  5955. result :=
  5956. ((SourcePD.Range.r = DestPD.Range.r) or (SourcePD.Range.r = 0) or (DestPD.Range.r = 0)) and
  5957. ((SourcePD.Range.g = DestPD.Range.g) or (SourcePD.Range.g = 0) or (DestPD.Range.g = 0)) and
  5958. ((SourcePD.Range.b = DestPD.Range.b) or (SourcePD.Range.b = 0) or (DestPD.Range.b = 0)) and
  5959. ((SourcePD.Range.a = DestPD.Range.a) or (SourcePD.Range.a = 0) or (DestPD.Range.a = 0));
  5960. end;
  5961. function CanShift: Boolean;
  5962. begin
  5963. result :=
  5964. ((SourcePD.Range.r >= DestPD.Range.r) or (SourcePD.Range.r = 0) or (DestPD.Range.r = 0)) and
  5965. ((SourcePD.Range.g >= DestPD.Range.g) or (SourcePD.Range.g = 0) or (DestPD.Range.g = 0)) and
  5966. ((SourcePD.Range.b >= DestPD.Range.b) or (SourcePD.Range.b = 0) or (DestPD.Range.b = 0)) and
  5967. ((SourcePD.Range.a >= DestPD.Range.a) or (SourcePD.Range.a = 0) or (DestPD.Range.a = 0));
  5968. end;
  5969. function GetShift(aSource, aDest: Cardinal) : ShortInt;
  5970. begin
  5971. result := 0;
  5972. while (aSource > aDest) and (aSource > 0) do begin
  5973. inc(result);
  5974. aSource := aSource shr 1;
  5975. end;
  5976. end;
  5977. begin
  5978. if (aFormat <> fFormat) and (aFormat <> tfEmpty) then begin
  5979. SourceFD := TFormatDescriptor.Get(Format);
  5980. DestFD := TFormatDescriptor.Get(aFormat);
  5981. if DataIsIdentical then begin
  5982. result := true;
  5983. Format := aFormat;
  5984. exit;
  5985. end;
  5986. SourceFD.PreparePixel(SourcePD);
  5987. DestFD.PreparePixel (DestPD);
  5988. if CanCopyDirect then
  5989. result := Convert(Self, glBitmapConvertCopyFunc, false, aFormat)
  5990. else if CanShift then begin
  5991. ShiftData.r := GetShift(SourcePD.Range.r, DestPD.Range.r);
  5992. ShiftData.g := GetShift(SourcePD.Range.g, DestPD.Range.g);
  5993. ShiftData.b := GetShift(SourcePD.Range.b, DestPD.Range.b);
  5994. ShiftData.a := GetShift(SourcePD.Range.a, DestPD.Range.a);
  5995. result := Convert(Self, glBitmapConvertShiftRGBAFunc, false, aFormat, @ShiftData);
  5996. end else
  5997. result := Convert(Self, glBitmapConvertCalculateRGBAFunc, false, aFormat);
  5998. end else
  5999. result := true;
  6000. end;
  6001. {$IFDEF GLB_SDL}
  6002. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6003. function TglBitmapData.AssignToSurface(out aSurface: PSDL_Surface): Boolean;
  6004. var
  6005. Row, RowSize: Integer;
  6006. SourceData, TmpData: PByte;
  6007. TempDepth: Integer;
  6008. FormatDesc: TFormatDescriptor;
  6009. function GetRowPointer(Row: Integer): pByte;
  6010. begin
  6011. result := aSurface.pixels;
  6012. Inc(result, Row * RowSize);
  6013. end;
  6014. begin
  6015. result := false;
  6016. FormatDesc := TFormatDescriptor.Get(Format);
  6017. if FormatDesc.IsCompressed then
  6018. raise EglBitmapUnsupportedFormat.Create(Format);
  6019. if Assigned(Data) then begin
  6020. case Trunc(FormatDesc.PixelSize) of
  6021. 1: TempDepth := 8;
  6022. 2: TempDepth := 16;
  6023. 3: TempDepth := 24;
  6024. 4: TempDepth := 32;
  6025. else
  6026. raise EglBitmapUnsupportedFormat.Create(Format);
  6027. end;
  6028. aSurface := SDL_CreateRGBSurface(SDL_SWSURFACE, Width, Height, TempDepth,
  6029. FormatDesc.RedMask, FormatDesc.GreenMask, FormatDesc.BlueMask, FormatDesc.AlphaMask);
  6030. SourceData := Data;
  6031. RowSize := FormatDesc.GetSize(FileWidth, 1);
  6032. for Row := 0 to FileHeight-1 do begin
  6033. TmpData := GetRowPointer(Row);
  6034. if Assigned(TmpData) then begin
  6035. Move(SourceData^, TmpData^, RowSize);
  6036. inc(SourceData, RowSize);
  6037. end;
  6038. end;
  6039. result := true;
  6040. end;
  6041. end;
  6042. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6043. function TglBitmapData.AssignFromSurface(const aSurface: PSDL_Surface): Boolean;
  6044. var
  6045. pSource, pData, pTempData: PByte;
  6046. Row, RowSize, TempWidth, TempHeight: Integer;
  6047. IntFormat: TglBitmapFormat;
  6048. fd: TFormatDescriptor;
  6049. Mask: TglBitmapMask;
  6050. function GetRowPointer(Row: Integer): pByte;
  6051. begin
  6052. result := aSurface^.pixels;
  6053. Inc(result, Row * RowSize);
  6054. end;
  6055. begin
  6056. result := false;
  6057. if (Assigned(aSurface)) then begin
  6058. with aSurface^.format^ do begin
  6059. Mask.r := RMask;
  6060. Mask.g := GMask;
  6061. Mask.b := BMask;
  6062. Mask.a := AMask;
  6063. IntFormat := TFormatDescriptor.GetFromMask(Mask).Format;
  6064. if (IntFormat = tfEmpty) then
  6065. raise EglBitmap.Create('AssignFromSurface - Invalid Pixelformat.');
  6066. end;
  6067. fd := TFormatDescriptor.Get(IntFormat);
  6068. TempWidth := aSurface^.w;
  6069. TempHeight := aSurface^.h;
  6070. RowSize := fd.GetSize(TempWidth, 1);
  6071. GetMem(pData, TempHeight * RowSize);
  6072. try
  6073. pTempData := pData;
  6074. for Row := 0 to TempHeight -1 do begin
  6075. pSource := GetRowPointer(Row);
  6076. if (Assigned(pSource)) then begin
  6077. Move(pSource^, pTempData^, RowSize);
  6078. Inc(pTempData, RowSize);
  6079. end;
  6080. end;
  6081. SetData(pData, IntFormat, TempWidth, TempHeight);
  6082. result := true;
  6083. except
  6084. if Assigned(pData) then
  6085. FreeMem(pData);
  6086. raise;
  6087. end;
  6088. end;
  6089. end;
  6090. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6091. function TglBitmapData.AssignAlphaToSurface(out aSurface: PSDL_Surface): Boolean;
  6092. var
  6093. Row, Col, AlphaInterleave: Integer;
  6094. pSource, pDest: PByte;
  6095. function GetRowPointer(Row: Integer): pByte;
  6096. begin
  6097. result := aSurface.pixels;
  6098. Inc(result, Row * Width);
  6099. end;
  6100. begin
  6101. result := false;
  6102. if Assigned(Data) then begin
  6103. if Format in [tfAlpha8ub1, tfLuminance8Alpha8ub2, tfBGRA8ub4, tfRGBA8ub4] then begin
  6104. aSurface := SDL_CreateRGBSurface(SDL_SWSURFACE, Width, Height, 8, $FF, $FF, $FF, 0);
  6105. AlphaInterleave := 0;
  6106. case Format of
  6107. tfLuminance8Alpha8ub2:
  6108. AlphaInterleave := 1;
  6109. tfBGRA8ub4, tfRGBA8ub4:
  6110. AlphaInterleave := 3;
  6111. end;
  6112. pSource := Data;
  6113. for Row := 0 to Height -1 do begin
  6114. pDest := GetRowPointer(Row);
  6115. if Assigned(pDest) then begin
  6116. for Col := 0 to Width -1 do begin
  6117. Inc(pSource, AlphaInterleave);
  6118. pDest^ := pSource^;
  6119. Inc(pDest);
  6120. Inc(pSource);
  6121. end;
  6122. end;
  6123. end;
  6124. result := true;
  6125. end;
  6126. end;
  6127. end;
  6128. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6129. function TglBitmapData.AddAlphaFromSurface(const aSurface: PSDL_Surface; const aFunc: TglBitmapFunction = nil; const aArgs: Pointer = nil): Boolean;
  6130. var
  6131. bmp: TglBitmap2D;
  6132. begin
  6133. bmp := TglBitmap2D.Create;
  6134. try
  6135. bmp.AssignFromSurface(aSurface);
  6136. result := AddAlphaFromGlBitmap(bmp, aFunc, aArgs);
  6137. finally
  6138. bmp.Free;
  6139. end;
  6140. end;
  6141. {$ENDIF}
  6142. {$IFDEF GLB_DELPHI}
  6143. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6144. function CreateGrayPalette: HPALETTE;
  6145. var
  6146. Idx: Integer;
  6147. Pal: PLogPalette;
  6148. begin
  6149. GetMem(Pal, SizeOf(TLogPalette) + (SizeOf(TPaletteEntry) * 256));
  6150. Pal.palVersion := $300;
  6151. Pal.palNumEntries := 256;
  6152. for Idx := 0 to Pal.palNumEntries - 1 do begin
  6153. Pal.palPalEntry[Idx].peRed := Idx;
  6154. Pal.palPalEntry[Idx].peGreen := Idx;
  6155. Pal.palPalEntry[Idx].peBlue := Idx;
  6156. Pal.palPalEntry[Idx].peFlags := 0;
  6157. end;
  6158. Result := CreatePalette(Pal^);
  6159. FreeMem(Pal);
  6160. end;
  6161. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6162. function TglBitmapData.AssignToBitmap(const aBitmap: TBitmap): Boolean;
  6163. var
  6164. Row, RowSize: Integer;
  6165. pSource, pData: PByte;
  6166. begin
  6167. result := false;
  6168. if Assigned(Data) then begin
  6169. if Assigned(aBitmap) then begin
  6170. aBitmap.Width := Width;
  6171. aBitmap.Height := Height;
  6172. case Format of
  6173. tfAlpha8ub1, tfLuminance8ub1: begin
  6174. aBitmap.PixelFormat := pf8bit;
  6175. aBitmap.Palette := CreateGrayPalette;
  6176. end;
  6177. tfRGB5A1us1:
  6178. aBitmap.PixelFormat := pf15bit;
  6179. tfR5G6B5us1:
  6180. aBitmap.PixelFormat := pf16bit;
  6181. tfRGB8ub3, tfBGR8ub3:
  6182. aBitmap.PixelFormat := pf24bit;
  6183. tfRGBA8ub4, tfBGRA8ub4:
  6184. aBitmap.PixelFormat := pf32bit;
  6185. else
  6186. raise EglBitmap.Create('AssignToBitmap - Invalid Pixelformat.');
  6187. end;
  6188. RowSize := FormatDescriptor.GetSize(Width, 1);
  6189. pSource := Data;
  6190. for Row := 0 to Height-1 do begin
  6191. pData := aBitmap.Scanline[Row];
  6192. Move(pSource^, pData^, RowSize);
  6193. Inc(pSource, RowSize);
  6194. if (Format in [tfRGB8ub3, tfRGBA8ub4]) then // swap RGB(A) to BGR(A)
  6195. SwapRGB(pData, Width, Format = tfRGBA8ub4);
  6196. end;
  6197. result := true;
  6198. end;
  6199. end;
  6200. end;
  6201. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6202. function TglBitmapData.AssignFromBitmap(const aBitmap: TBitmap): Boolean;
  6203. var
  6204. pSource, pData, pTempData: PByte;
  6205. Row, RowSize, TempWidth, TempHeight: Integer;
  6206. IntFormat: TglBitmapFormat;
  6207. begin
  6208. result := false;
  6209. if (Assigned(aBitmap)) then begin
  6210. case aBitmap.PixelFormat of
  6211. pf8bit:
  6212. IntFormat := tfLuminance8ub1;
  6213. pf15bit:
  6214. IntFormat := tfRGB5A1us1;
  6215. pf16bit:
  6216. IntFormat := tfR5G6B5us1;
  6217. pf24bit:
  6218. IntFormat := tfBGR8ub3;
  6219. pf32bit:
  6220. IntFormat := tfBGRA8ub4;
  6221. else
  6222. raise EglBitmap.Create('AssignFromBitmap - Invalid Pixelformat.');
  6223. end;
  6224. TempWidth := aBitmap.Width;
  6225. TempHeight := aBitmap.Height;
  6226. RowSize := TFormatDescriptor.Get(IntFormat).GetSize(TempWidth, 1);
  6227. GetMem(pData, TempHeight * RowSize);
  6228. try
  6229. pTempData := pData;
  6230. for Row := 0 to TempHeight -1 do begin
  6231. pSource := aBitmap.Scanline[Row];
  6232. if (Assigned(pSource)) then begin
  6233. Move(pSource^, pTempData^, RowSize);
  6234. Inc(pTempData, RowSize);
  6235. end;
  6236. end;
  6237. SetData(pData, IntFormat, TempWidth, TempHeight);
  6238. result := true;
  6239. except
  6240. if Assigned(pData) then
  6241. FreeMem(pData);
  6242. raise;
  6243. end;
  6244. end;
  6245. end;
  6246. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6247. function TglBitmapData.AssignAlphaToBitmap(const aBitmap: TBitmap): Boolean;
  6248. var
  6249. Row, Col, AlphaInterleave: Integer;
  6250. pSource, pDest: PByte;
  6251. begin
  6252. result := false;
  6253. if Assigned(Data) then begin
  6254. if (Format in [tfAlpha8ub1, tfLuminance8Alpha8ub2, tfRGBA8ub4, tfBGRA8ub4]) then begin
  6255. if Assigned(aBitmap) then begin
  6256. aBitmap.PixelFormat := pf8bit;
  6257. aBitmap.Palette := CreateGrayPalette;
  6258. aBitmap.Width := Width;
  6259. aBitmap.Height := Height;
  6260. case Format of
  6261. tfLuminance8Alpha8ub2:
  6262. AlphaInterleave := 1;
  6263. tfRGBA8ub4, tfBGRA8ub4:
  6264. AlphaInterleave := 3;
  6265. else
  6266. AlphaInterleave := 0;
  6267. end;
  6268. // Copy Data
  6269. pSource := Data;
  6270. for Row := 0 to Height -1 do begin
  6271. pDest := aBitmap.Scanline[Row];
  6272. if Assigned(pDest) then begin
  6273. for Col := 0 to Width -1 do begin
  6274. Inc(pSource, AlphaInterleave);
  6275. pDest^ := pSource^;
  6276. Inc(pDest);
  6277. Inc(pSource);
  6278. end;
  6279. end;
  6280. end;
  6281. result := true;
  6282. end;
  6283. end;
  6284. end;
  6285. end;
  6286. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6287. function TglBitmapData.AddAlphaFromBitmap(const aBitmap: TBitmap; const aFunc: TglBitmapFunction; const aArgs: Pointer): Boolean;
  6288. var
  6289. data: TglBitmapData;
  6290. begin
  6291. data := TglBitmapData.Create;
  6292. try
  6293. data.AssignFromBitmap(aBitmap);
  6294. result := AddAlphaFromDataObj(data, aFunc, aArgs);
  6295. finally
  6296. data.Free;
  6297. end;
  6298. end;
  6299. {$ENDIF}
  6300. {$IFDEF GLB_LAZARUS}
  6301. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6302. function TglBitmapData.AssignToLazIntfImage(const aImage: TLazIntfImage): Boolean;
  6303. var
  6304. rid: TRawImageDescription;
  6305. FormatDesc: TFormatDescriptor;
  6306. begin
  6307. if not Assigned(Data) then
  6308. raise EglBitmap.Create('no pixel data assigned. load data before save');
  6309. result := false;
  6310. if not Assigned(aImage) or (Format = tfEmpty) then
  6311. exit;
  6312. FormatDesc := TFormatDescriptor.Get(Format);
  6313. if FormatDesc.IsCompressed then
  6314. exit;
  6315. FillChar(rid{%H-}, SizeOf(rid), 0);
  6316. if FormatDesc.IsGrayscale then
  6317. rid.Format := ricfGray
  6318. else
  6319. rid.Format := ricfRGBA;
  6320. rid.Width := Width;
  6321. rid.Height := Height;
  6322. rid.Depth := FormatDesc.BitsPerPixel;
  6323. rid.BitOrder := riboBitsInOrder;
  6324. rid.ByteOrder := riboLSBFirst;
  6325. rid.LineOrder := riloTopToBottom;
  6326. rid.LineEnd := rileTight;
  6327. rid.BitsPerPixel := FormatDesc.BitsPerPixel;
  6328. rid.RedPrec := CountSetBits(FormatDesc.Range.r);
  6329. rid.GreenPrec := CountSetBits(FormatDesc.Range.g);
  6330. rid.BluePrec := CountSetBits(FormatDesc.Range.b);
  6331. rid.AlphaPrec := CountSetBits(FormatDesc.Range.a);
  6332. rid.RedShift := FormatDesc.Shift.r;
  6333. rid.GreenShift := FormatDesc.Shift.g;
  6334. rid.BlueShift := FormatDesc.Shift.b;
  6335. rid.AlphaShift := FormatDesc.Shift.a;
  6336. rid.MaskBitsPerPixel := 0;
  6337. rid.PaletteColorCount := 0;
  6338. aImage.DataDescription := rid;
  6339. aImage.CreateData;
  6340. if not Assigned(aImage.PixelData) then
  6341. raise EglBitmap.Create('error while creating LazIntfImage');
  6342. Move(Data^, aImage.PixelData^, FormatDesc.GetSize(Dimension));
  6343. result := true;
  6344. end;
  6345. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6346. function TglBitmapData.AssignFromLazIntfImage(const aImage: TLazIntfImage): Boolean;
  6347. var
  6348. f: TglBitmapFormat;
  6349. FormatDesc: TFormatDescriptor;
  6350. ImageData: PByte;
  6351. ImageSize: Integer;
  6352. CanCopy: Boolean;
  6353. Mask: TglBitmapRec4ul;
  6354. procedure CopyConvert;
  6355. var
  6356. bfFormat: TbmpBitfieldFormat;
  6357. pSourceLine, pDestLine: PByte;
  6358. pSourceMD, pDestMD: Pointer;
  6359. Shift, Prec: TglBitmapRec4ub;
  6360. x, y: Integer;
  6361. pixel: TglBitmapPixelData;
  6362. begin
  6363. bfFormat := TbmpBitfieldFormat.Create;
  6364. with aImage.DataDescription do begin
  6365. Prec.r := RedPrec;
  6366. Prec.g := GreenPrec;
  6367. Prec.b := BluePrec;
  6368. Prec.a := AlphaPrec;
  6369. Shift.r := RedShift;
  6370. Shift.g := GreenShift;
  6371. Shift.b := BlueShift;
  6372. Shift.a := AlphaShift;
  6373. bfFormat.SetCustomValues(BitsPerPixel, Prec, Shift);
  6374. end;
  6375. pSourceMD := bfFormat.CreateMappingData;
  6376. pDestMD := FormatDesc.CreateMappingData;
  6377. try
  6378. for y := 0 to aImage.Height-1 do begin
  6379. pSourceLine := aImage.PixelData + y {%H-}* aImage.DataDescription.BytesPerLine;
  6380. pDestLine := ImageData + y * Round(FormatDesc.BytesPerPixel * aImage.Width);
  6381. for x := 0 to aImage.Width-1 do begin
  6382. bfFormat.Unmap(pSourceLine, pixel, pSourceMD);
  6383. FormatDesc.Map(pixel, pDestLine, pDestMD);
  6384. end;
  6385. end;
  6386. finally
  6387. FormatDesc.FreeMappingData(pDestMD);
  6388. bfFormat.FreeMappingData(pSourceMD);
  6389. bfFormat.Free;
  6390. end;
  6391. end;
  6392. begin
  6393. result := false;
  6394. if not Assigned(aImage) then
  6395. exit;
  6396. with aImage.DataDescription do begin
  6397. Mask.r := (QWord(1 shl RedPrec )-1) shl RedShift;
  6398. Mask.g := (QWord(1 shl GreenPrec)-1) shl GreenShift;
  6399. Mask.b := (QWord(1 shl BluePrec )-1) shl BlueShift;
  6400. Mask.a := (QWord(1 shl AlphaPrec)-1) shl AlphaShift;
  6401. end;
  6402. FormatDesc := TFormatDescriptor.GetFromMask(Mask);
  6403. f := FormatDesc.Format;
  6404. if (f = tfEmpty) then
  6405. exit;
  6406. CanCopy :=
  6407. (FormatDesc.BitsPerPixel = aImage.DataDescription.Depth) and
  6408. (aImage.DataDescription.BitsPerPixel = aImage.DataDescription.Depth);
  6409. ImageSize := FormatDesc.GetSize(aImage.Width, aImage.Height);
  6410. ImageData := GetMem(ImageSize);
  6411. try
  6412. if CanCopy then
  6413. Move(aImage.PixelData^, ImageData^, ImageSize)
  6414. else
  6415. CopyConvert;
  6416. SetData(ImageData, f, aImage.Width, aImage.Height);
  6417. except
  6418. if Assigned(ImageData) then
  6419. FreeMem(ImageData);
  6420. raise;
  6421. end;
  6422. result := true;
  6423. end;
  6424. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6425. function TglBitmapData.AssignAlphaToLazIntfImage(const aImage: TLazIntfImage): Boolean;
  6426. var
  6427. rid: TRawImageDescription;
  6428. FormatDesc: TFormatDescriptor;
  6429. Pixel: TglBitmapPixelData;
  6430. x, y: Integer;
  6431. srcMD: Pointer;
  6432. src, dst: PByte;
  6433. begin
  6434. result := false;
  6435. if not Assigned(aImage) or (Format = tfEmpty) then
  6436. exit;
  6437. FormatDesc := TFormatDescriptor.Get(Format);
  6438. if FormatDesc.IsCompressed or not FormatDesc.HasAlpha then
  6439. exit;
  6440. FillChar(rid{%H-}, SizeOf(rid), 0);
  6441. rid.Format := ricfGray;
  6442. rid.Width := Width;
  6443. rid.Height := Height;
  6444. rid.Depth := CountSetBits(FormatDesc.Range.a);
  6445. rid.BitOrder := riboBitsInOrder;
  6446. rid.ByteOrder := riboLSBFirst;
  6447. rid.LineOrder := riloTopToBottom;
  6448. rid.LineEnd := rileTight;
  6449. rid.BitsPerPixel := 8 * Ceil(rid.Depth / 8);
  6450. rid.RedPrec := CountSetBits(FormatDesc.Range.a);
  6451. rid.GreenPrec := 0;
  6452. rid.BluePrec := 0;
  6453. rid.AlphaPrec := 0;
  6454. rid.RedShift := 0;
  6455. rid.GreenShift := 0;
  6456. rid.BlueShift := 0;
  6457. rid.AlphaShift := 0;
  6458. rid.MaskBitsPerPixel := 0;
  6459. rid.PaletteColorCount := 0;
  6460. aImage.DataDescription := rid;
  6461. aImage.CreateData;
  6462. srcMD := FormatDesc.CreateMappingData;
  6463. try
  6464. FormatDesc.PreparePixel(Pixel);
  6465. src := Data;
  6466. dst := aImage.PixelData;
  6467. for y := 0 to Height-1 do
  6468. for x := 0 to Width-1 do begin
  6469. FormatDesc.Unmap(src, Pixel, srcMD);
  6470. case rid.BitsPerPixel of
  6471. 8: begin
  6472. dst^ := Pixel.Data.a;
  6473. inc(dst);
  6474. end;
  6475. 16: begin
  6476. PWord(dst)^ := Pixel.Data.a;
  6477. inc(dst, 2);
  6478. end;
  6479. 24: begin
  6480. PByteArray(dst)^[0] := PByteArray(@Pixel.Data.a)^[0];
  6481. PByteArray(dst)^[1] := PByteArray(@Pixel.Data.a)^[1];
  6482. PByteArray(dst)^[2] := PByteArray(@Pixel.Data.a)^[2];
  6483. inc(dst, 3);
  6484. end;
  6485. 32: begin
  6486. PCardinal(dst)^ := Pixel.Data.a;
  6487. inc(dst, 4);
  6488. end;
  6489. else
  6490. raise EglBitmapUnsupportedFormat.Create(Format);
  6491. end;
  6492. end;
  6493. finally
  6494. FormatDesc.FreeMappingData(srcMD);
  6495. end;
  6496. result := true;
  6497. end;
  6498. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6499. function TglBitmapData.AddAlphaFromLazIntfImage(const aImage: TLazIntfImage; const aFunc: TglBitmapFunction; const aArgs: Pointer): Boolean;
  6500. var
  6501. data: TglBitmapData;
  6502. begin
  6503. data := TglBitmapData.Create;
  6504. try
  6505. data.AssignFromLazIntfImage(aImage);
  6506. result := AddAlphaFromDataObj(data, aFunc, aArgs);
  6507. finally
  6508. data.Free;
  6509. end;
  6510. end;
  6511. {$ENDIF}
  6512. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6513. function TglBitmapData.AddAlphaFromResource(const aInstance: Cardinal; aResource: String; aResType: PChar;
  6514. const aFunc: TglBitmapFunction; const aArgs: Pointer): Boolean;
  6515. var
  6516. rs: TResourceStream;
  6517. begin
  6518. PrepareResType(aResource, aResType);
  6519. rs := TResourceStream.Create(aInstance, aResource, aResType);
  6520. try
  6521. result := AddAlphaFromStream(rs, aFunc, aArgs);
  6522. finally
  6523. rs.Free;
  6524. end;
  6525. end;
  6526. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6527. function TglBitmapData.AddAlphaFromResourceID(const aInstance: Cardinal; const aResourceID: Integer; const aResType: PChar;
  6528. const aFunc: TglBitmapFunction; const aArgs: Pointer): Boolean;
  6529. var
  6530. rs: TResourceStream;
  6531. begin
  6532. rs := TResourceStream.CreateFromID(aInstance, aResourceID, aResType);
  6533. try
  6534. result := AddAlphaFromStream(rs, aFunc, aArgs);
  6535. finally
  6536. rs.Free;
  6537. end;
  6538. end;
  6539. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6540. function TglBitmapData.AddAlphaFromFunc(const aFunc: TglBitmapFunction; const aArgs: Pointer): Boolean;
  6541. begin
  6542. if TFormatDescriptor.Get(Format).IsCompressed then
  6543. raise EglBitmapUnsupportedFormat.Create(Format);
  6544. result := Convert(Self, aFunc, false, TFormatDescriptor.Get(Format).WithAlpha, aArgs);
  6545. end;
  6546. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6547. function TglBitmapData.AddAlphaFromFile(const aFileName: String; const aFunc: TglBitmapFunction; const aArgs: Pointer): Boolean;
  6548. var
  6549. FS: TFileStream;
  6550. begin
  6551. FS := TFileStream.Create(aFileName, fmOpenRead);
  6552. try
  6553. result := AddAlphaFromStream(FS, aFunc, aArgs);
  6554. finally
  6555. FS.Free;
  6556. end;
  6557. end;
  6558. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6559. function TglBitmapData.AddAlphaFromStream(const aStream: TStream; const aFunc: TglBitmapFunction; const aArgs: Pointer): Boolean;
  6560. var
  6561. data: TglBitmapData;
  6562. begin
  6563. data := TglBitmapData.Create(aStream);
  6564. try
  6565. result := AddAlphaFromDataObj(data, aFunc, aArgs);
  6566. finally
  6567. data.Free;
  6568. end;
  6569. end;
  6570. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6571. function TglBitmapData.AddAlphaFromDataObj(const aDataObj: TglBitmapData; aFunc: TglBitmapFunction; const aArgs: Pointer): Boolean;
  6572. var
  6573. DestData, DestData2, SourceData: pByte;
  6574. TempHeight, TempWidth: Integer;
  6575. SourceFD, DestFD: TFormatDescriptor;
  6576. SourceMD, DestMD, DestMD2: Pointer;
  6577. FuncRec: TglBitmapFunctionRec;
  6578. begin
  6579. result := false;
  6580. Assert(Assigned(Data));
  6581. Assert(Assigned(aDataObj));
  6582. Assert(Assigned(aDataObj.Data));
  6583. if ((aDataObj.Width = Width) and (aDataObj.Height = Height)) then begin
  6584. result := ConvertTo(TFormatDescriptor.Get(Format).WithAlpha);
  6585. SourceFD := TFormatDescriptor.Get(aDataObj.Format);
  6586. DestFD := TFormatDescriptor.Get(Format);
  6587. if not Assigned(aFunc) then begin
  6588. aFunc := glBitmapAlphaFunc;
  6589. FuncRec.Args := {%H-}Pointer(SourceFD.HasAlpha);
  6590. end else
  6591. FuncRec.Args := aArgs;
  6592. // Values
  6593. TempWidth := aDataObj.Width;
  6594. TempHeight := aDataObj.Height;
  6595. if (TempWidth <= 0) or (TempHeight <= 0) then
  6596. exit;
  6597. FuncRec.Sender := Self;
  6598. FuncRec.Size := Dimension;
  6599. FuncRec.Position.Fields := FuncRec.Size.Fields;
  6600. DestData := Data;
  6601. DestData2 := Data;
  6602. SourceData := aDataObj.Data;
  6603. // Mapping
  6604. SourceFD.PreparePixel(FuncRec.Source);
  6605. DestFD.PreparePixel (FuncRec.Dest);
  6606. SourceMD := SourceFD.CreateMappingData;
  6607. DestMD := DestFD.CreateMappingData;
  6608. DestMD2 := DestFD.CreateMappingData;
  6609. try
  6610. FuncRec.Position.Y := 0;
  6611. while FuncRec.Position.Y < TempHeight do begin
  6612. FuncRec.Position.X := 0;
  6613. while FuncRec.Position.X < TempWidth do begin
  6614. SourceFD.Unmap(SourceData, FuncRec.Source, SourceMD);
  6615. DestFD.Unmap (DestData, FuncRec.Dest, DestMD);
  6616. aFunc(FuncRec);
  6617. DestFD.Map(FuncRec.Dest, DestData2, DestMD2);
  6618. inc(FuncRec.Position.X);
  6619. end;
  6620. inc(FuncRec.Position.Y);
  6621. end;
  6622. finally
  6623. SourceFD.FreeMappingData(SourceMD);
  6624. DestFD.FreeMappingData(DestMD);
  6625. DestFD.FreeMappingData(DestMD2);
  6626. end;
  6627. end;
  6628. end;
  6629. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6630. function TglBitmapData.AddAlphaFromColorKey(const aRed, aGreen, aBlue: Byte; const aDeviation: Byte): Boolean;
  6631. begin
  6632. result := AddAlphaFromColorKeyFloat(aRed / $FF, aGreen / $FF, aBlue / $FF, aDeviation / $FF);
  6633. end;
  6634. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6635. function TglBitmapData.AddAlphaFromColorKeyRange(const aRed, aGreen, aBlue: Cardinal; const aDeviation: Cardinal): Boolean;
  6636. var
  6637. PixelData: TglBitmapPixelData;
  6638. begin
  6639. TFormatDescriptor.GetAlpha(Format).PreparePixel(PixelData);
  6640. result := AddAlphaFromColorKeyFloat(
  6641. aRed / PixelData.Range.r,
  6642. aGreen / PixelData.Range.g,
  6643. aBlue / PixelData.Range.b,
  6644. aDeviation / Max(PixelData.Range.r, Max(PixelData.Range.g, PixelData.Range.b)));
  6645. end;
  6646. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6647. function TglBitmapData.AddAlphaFromColorKeyFloat(const aRed, aGreen, aBlue: Single; const aDeviation: Single): Boolean;
  6648. var
  6649. values: array[0..2] of Single;
  6650. tmp: Cardinal;
  6651. i: Integer;
  6652. PixelData: TglBitmapPixelData;
  6653. begin
  6654. TFormatDescriptor.GetAlpha(Format).PreparePixel(PixelData);
  6655. with PixelData do begin
  6656. values[0] := aRed;
  6657. values[1] := aGreen;
  6658. values[2] := aBlue;
  6659. for i := 0 to 2 do begin
  6660. tmp := Trunc(Range.arr[i] * aDeviation);
  6661. Data.arr[i] := Min(Range.arr[i], Trunc(Range.arr[i] * values[i] + tmp));
  6662. Range.arr[i] := Max(0, Trunc(Range.arr[i] * values[i] - tmp));
  6663. end;
  6664. Data.a := 0;
  6665. Range.a := 0;
  6666. end;
  6667. result := AddAlphaFromFunc(glBitmapColorKeyAlphaFunc, @PixelData);
  6668. end;
  6669. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6670. function TglBitmapData.AddAlphaFromValue(const aAlpha: Byte): Boolean;
  6671. begin
  6672. result := AddAlphaFromValueFloat(aAlpha / $FF);
  6673. end;
  6674. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6675. function TglBitmapData.AddAlphaFromValueRange(const aAlpha: Cardinal): Boolean;
  6676. var
  6677. PixelData: TglBitmapPixelData;
  6678. begin
  6679. TFormatDescriptor.GetAlpha(Format).PreparePixel(PixelData);
  6680. result := AddAlphaFromValueFloat(aAlpha / PixelData.Range.a);
  6681. end;
  6682. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6683. function TglBitmapData.AddAlphaFromValueFloat(const aAlpha: Single): Boolean;
  6684. var
  6685. PixelData: TglBitmapPixelData;
  6686. begin
  6687. TFormatDescriptor.GetAlpha(Format).PreparePixel(PixelData);
  6688. with PixelData do
  6689. Data.a := Min(Range.a, Max(0, Round(Range.a * aAlpha)));
  6690. result := AddAlphaFromFunc(glBitmapValueAlphaFunc, @PixelData.Data.a);
  6691. end;
  6692. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6693. function TglBitmapData.RemoveAlpha: Boolean;
  6694. var
  6695. FormatDesc: TFormatDescriptor;
  6696. begin
  6697. result := false;
  6698. FormatDesc := TFormatDescriptor.Get(Format);
  6699. if Assigned(Data) then begin
  6700. if FormatDesc.IsCompressed or not FormatDesc.HasAlpha then
  6701. raise EglBitmapUnsupportedFormat.Create(Format);
  6702. result := ConvertTo(FormatDesc.WithoutAlpha);
  6703. end;
  6704. end;
  6705. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6706. procedure TglBitmapData.FillWithColor(const aRed, aGreen, aBlue: Byte;
  6707. const aAlpha: Byte);
  6708. begin
  6709. FillWithColorFloat(aRed/$FF, aGreen/$FF, aBlue/$FF, aAlpha/$FF);
  6710. end;
  6711. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6712. procedure TglBitmapData.FillWithColorRange(const aRed, aGreen, aBlue: Cardinal; const aAlpha: Cardinal);
  6713. var
  6714. PixelData: TglBitmapPixelData;
  6715. begin
  6716. TFormatDescriptor.GetAlpha(Format).PreparePixel(PixelData);
  6717. FillWithColorFloat(
  6718. aRed / PixelData.Range.r,
  6719. aGreen / PixelData.Range.g,
  6720. aBlue / PixelData.Range.b,
  6721. aAlpha / PixelData.Range.a);
  6722. end;
  6723. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6724. procedure TglBitmapData.FillWithColorFloat(const aRed, aGreen, aBlue: Single; const aAlpha: Single);
  6725. var
  6726. PixelData: TglBitmapPixelData;
  6727. begin
  6728. TFormatDescriptor.Get(Format).PreparePixel(PixelData);
  6729. with PixelData do begin
  6730. Data.r := Max(0, Min(Range.r, Trunc(Range.r * aRed)));
  6731. Data.g := Max(0, Min(Range.g, Trunc(Range.g * aGreen)));
  6732. Data.b := Max(0, Min(Range.b, Trunc(Range.b * aBlue)));
  6733. Data.a := Max(0, Min(Range.a, Trunc(Range.a * aAlpha)));
  6734. end;
  6735. Convert(glBitmapFillWithColorFunc, false, @PixelData);
  6736. end;
  6737. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6738. procedure TglBitmapData.SetData(const aData: PByte; const aFormat: TglBitmapFormat; const aWidth: Integer; const aHeight: Integer);
  6739. begin
  6740. if (Data <> aData) then begin
  6741. if (Assigned(Data)) then
  6742. FreeMem(Data);
  6743. fData := aData;
  6744. end;
  6745. if Assigned(fData) then begin
  6746. FillChar(fDimension, SizeOf(fDimension), 0);
  6747. if aWidth <> -1 then begin
  6748. fDimension.Fields := fDimension.Fields + [ffX];
  6749. fDimension.X := aWidth;
  6750. end;
  6751. if aHeight <> -1 then begin
  6752. fDimension.Fields := fDimension.Fields + [ffY];
  6753. fDimension.Y := aHeight;
  6754. end;
  6755. fFormat := aFormat;
  6756. end else
  6757. fFormat := tfEmpty;
  6758. UpdateScanlines;
  6759. end;
  6760. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6761. function TglBitmapData.Clone: TglBitmapData;
  6762. var
  6763. Temp: TglBitmapData;
  6764. TempPtr: PByte;
  6765. Size: Integer;
  6766. begin
  6767. result := nil;
  6768. Temp := (ClassType.Create as TglBitmapData);
  6769. try
  6770. // copy texture data if assigned
  6771. if Assigned(Data) then begin
  6772. Size := TFormatDescriptor.Get(Format).GetSize(fDimension);
  6773. GetMem(TempPtr, Size);
  6774. try
  6775. Move(Data^, TempPtr^, Size);
  6776. Temp.SetData(TempPtr, Format, Width, Height);
  6777. except
  6778. if Assigned(TempPtr) then
  6779. FreeMem(TempPtr);
  6780. raise;
  6781. end;
  6782. end else begin
  6783. TempPtr := nil;
  6784. Temp.SetData(TempPtr, Format, Width, Height);
  6785. end;
  6786. // copy properties
  6787. Temp.fFormat := Format;
  6788. result := Temp;
  6789. except
  6790. FreeAndNil(Temp);
  6791. raise;
  6792. end;
  6793. end;
  6794. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6795. procedure TglBitmapData.Invert(const aRed, aGreen, aBlue, aAlpha: Boolean);
  6796. var
  6797. mask: PtrInt;
  6798. begin
  6799. mask :=
  6800. (Byte(aRed) and 1) or
  6801. ((Byte(aGreen) and 1) shl 1) or
  6802. ((Byte(aBlue) and 1) shl 2) or
  6803. ((Byte(aAlpha) and 1) shl 3);
  6804. if (mask > 0) then
  6805. Convert(glBitmapInvertFunc, false, {%H-}Pointer(mask));
  6806. end;
  6807. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6808. type
  6809. TMatrixItem = record
  6810. X, Y: Integer;
  6811. W: Single;
  6812. end;
  6813. PglBitmapToNormalMapRec = ^TglBitmapToNormalMapRec;
  6814. TglBitmapToNormalMapRec = Record
  6815. Scale: Single;
  6816. Heights: array of Single;
  6817. MatrixU : array of TMatrixItem;
  6818. MatrixV : array of TMatrixItem;
  6819. end;
  6820. const
  6821. ONE_OVER_255 = 1 / 255;
  6822. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6823. procedure glBitmapToNormalMapPrepareFunc(var FuncRec: TglBitmapFunctionRec);
  6824. var
  6825. Val: Single;
  6826. begin
  6827. with FuncRec do begin
  6828. Val :=
  6829. Source.Data.r * LUMINANCE_WEIGHT_R +
  6830. Source.Data.g * LUMINANCE_WEIGHT_G +
  6831. Source.Data.b * LUMINANCE_WEIGHT_B;
  6832. PglBitmapToNormalMapRec(Args)^.Heights[Position.Y * Size.X + Position.X] := Val * ONE_OVER_255;
  6833. end;
  6834. end;
  6835. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6836. procedure glBitmapToNormalMapPrepareAlphaFunc(var FuncRec: TglBitmapFunctionRec);
  6837. begin
  6838. with FuncRec do
  6839. PglBitmapToNormalMapRec(Args)^.Heights[Position.Y * Size.X + Position.X] := Source.Data.a * ONE_OVER_255;
  6840. end;
  6841. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6842. procedure glBitmapToNormalMapFunc (var FuncRec: TglBitmapFunctionRec);
  6843. type
  6844. TVec = Array[0..2] of Single;
  6845. var
  6846. Idx: Integer;
  6847. du, dv: Double;
  6848. Len: Single;
  6849. Vec: TVec;
  6850. function GetHeight(X, Y: Integer): Single;
  6851. begin
  6852. with FuncRec do begin
  6853. X := Max(0, Min(Size.X -1, X));
  6854. Y := Max(0, Min(Size.Y -1, Y));
  6855. result := PglBitmapToNormalMapRec(Args)^.Heights[Y * Size.X + X];
  6856. end;
  6857. end;
  6858. begin
  6859. with FuncRec do begin
  6860. with PglBitmapToNormalMapRec(Args)^ do begin
  6861. du := 0;
  6862. for Idx := Low(MatrixU) to High(MatrixU) do
  6863. du := du + GetHeight(Position.X + MatrixU[Idx].X, Position.Y + MatrixU[Idx].Y) * MatrixU[Idx].W;
  6864. dv := 0;
  6865. for Idx := Low(MatrixU) to High(MatrixU) do
  6866. dv := dv + GetHeight(Position.X + MatrixV[Idx].X, Position.Y + MatrixV[Idx].Y) * MatrixV[Idx].W;
  6867. Vec[0] := -du * Scale;
  6868. Vec[1] := -dv * Scale;
  6869. Vec[2] := 1;
  6870. end;
  6871. // Normalize
  6872. Len := 1 / Sqrt(Sqr(Vec[0]) + Sqr(Vec[1]) + Sqr(Vec[2]));
  6873. if Len <> 0 then begin
  6874. Vec[0] := Vec[0] * Len;
  6875. Vec[1] := Vec[1] * Len;
  6876. Vec[2] := Vec[2] * Len;
  6877. end;
  6878. // Farbe zuweisem
  6879. Dest.Data.r := Trunc((Vec[0] + 1) * 127.5);
  6880. Dest.Data.g := Trunc((Vec[1] + 1) * 127.5);
  6881. Dest.Data.b := Trunc((Vec[2] + 1) * 127.5);
  6882. end;
  6883. end;
  6884. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6885. procedure TglBitmapData.GenerateNormalMap(const aFunc: TglBitmapNormalMapFunc; const aScale: Single; const aUseAlpha: Boolean);
  6886. var
  6887. Rec: TglBitmapToNormalMapRec;
  6888. procedure SetEntry (var Matrix: array of TMatrixItem; Index, X, Y: Integer; W: Single);
  6889. begin
  6890. if (Index >= Low(Matrix)) and (Index <= High(Matrix)) then begin
  6891. Matrix[Index].X := X;
  6892. Matrix[Index].Y := Y;
  6893. Matrix[Index].W := W;
  6894. end;
  6895. end;
  6896. begin
  6897. if TFormatDescriptor.Get(Format).IsCompressed then
  6898. raise EglBitmapUnsupportedFormat.Create(Format);
  6899. if aScale > 100 then
  6900. Rec.Scale := 100
  6901. else if aScale < -100 then
  6902. Rec.Scale := -100
  6903. else
  6904. Rec.Scale := aScale;
  6905. SetLength(Rec.Heights, Width * Height);
  6906. try
  6907. case aFunc of
  6908. nm4Samples: begin
  6909. SetLength(Rec.MatrixU, 2);
  6910. SetEntry(Rec.MatrixU, 0, -1, 0, -0.5);
  6911. SetEntry(Rec.MatrixU, 1, 1, 0, 0.5);
  6912. SetLength(Rec.MatrixV, 2);
  6913. SetEntry(Rec.MatrixV, 0, 0, 1, 0.5);
  6914. SetEntry(Rec.MatrixV, 1, 0, -1, -0.5);
  6915. end;
  6916. nmSobel: begin
  6917. SetLength(Rec.MatrixU, 6);
  6918. SetEntry(Rec.MatrixU, 0, -1, 1, -1.0);
  6919. SetEntry(Rec.MatrixU, 1, -1, 0, -2.0);
  6920. SetEntry(Rec.MatrixU, 2, -1, -1, -1.0);
  6921. SetEntry(Rec.MatrixU, 3, 1, 1, 1.0);
  6922. SetEntry(Rec.MatrixU, 4, 1, 0, 2.0);
  6923. SetEntry(Rec.MatrixU, 5, 1, -1, 1.0);
  6924. SetLength(Rec.MatrixV, 6);
  6925. SetEntry(Rec.MatrixV, 0, -1, 1, 1.0);
  6926. SetEntry(Rec.MatrixV, 1, 0, 1, 2.0);
  6927. SetEntry(Rec.MatrixV, 2, 1, 1, 1.0);
  6928. SetEntry(Rec.MatrixV, 3, -1, -1, -1.0);
  6929. SetEntry(Rec.MatrixV, 4, 0, -1, -2.0);
  6930. SetEntry(Rec.MatrixV, 5, 1, -1, -1.0);
  6931. end;
  6932. nm3x3: begin
  6933. SetLength(Rec.MatrixU, 6);
  6934. SetEntry(Rec.MatrixU, 0, -1, 1, -1/6);
  6935. SetEntry(Rec.MatrixU, 1, -1, 0, -1/6);
  6936. SetEntry(Rec.MatrixU, 2, -1, -1, -1/6);
  6937. SetEntry(Rec.MatrixU, 3, 1, 1, 1/6);
  6938. SetEntry(Rec.MatrixU, 4, 1, 0, 1/6);
  6939. SetEntry(Rec.MatrixU, 5, 1, -1, 1/6);
  6940. SetLength(Rec.MatrixV, 6);
  6941. SetEntry(Rec.MatrixV, 0, -1, 1, 1/6);
  6942. SetEntry(Rec.MatrixV, 1, 0, 1, 1/6);
  6943. SetEntry(Rec.MatrixV, 2, 1, 1, 1/6);
  6944. SetEntry(Rec.MatrixV, 3, -1, -1, -1/6);
  6945. SetEntry(Rec.MatrixV, 4, 0, -1, -1/6);
  6946. SetEntry(Rec.MatrixV, 5, 1, -1, -1/6);
  6947. end;
  6948. nm5x5: begin
  6949. SetLength(Rec.MatrixU, 20);
  6950. SetEntry(Rec.MatrixU, 0, -2, 2, -1 / 16);
  6951. SetEntry(Rec.MatrixU, 1, -1, 2, -1 / 10);
  6952. SetEntry(Rec.MatrixU, 2, 1, 2, 1 / 10);
  6953. SetEntry(Rec.MatrixU, 3, 2, 2, 1 / 16);
  6954. SetEntry(Rec.MatrixU, 4, -2, 1, -1 / 10);
  6955. SetEntry(Rec.MatrixU, 5, -1, 1, -1 / 8);
  6956. SetEntry(Rec.MatrixU, 6, 1, 1, 1 / 8);
  6957. SetEntry(Rec.MatrixU, 7, 2, 1, 1 / 10);
  6958. SetEntry(Rec.MatrixU, 8, -2, 0, -1 / 2.8);
  6959. SetEntry(Rec.MatrixU, 9, -1, 0, -0.5);
  6960. SetEntry(Rec.MatrixU, 10, 1, 0, 0.5);
  6961. SetEntry(Rec.MatrixU, 11, 2, 0, 1 / 2.8);
  6962. SetEntry(Rec.MatrixU, 12, -2, -1, -1 / 10);
  6963. SetEntry(Rec.MatrixU, 13, -1, -1, -1 / 8);
  6964. SetEntry(Rec.MatrixU, 14, 1, -1, 1 / 8);
  6965. SetEntry(Rec.MatrixU, 15, 2, -1, 1 / 10);
  6966. SetEntry(Rec.MatrixU, 16, -2, -2, -1 / 16);
  6967. SetEntry(Rec.MatrixU, 17, -1, -2, -1 / 10);
  6968. SetEntry(Rec.MatrixU, 18, 1, -2, 1 / 10);
  6969. SetEntry(Rec.MatrixU, 19, 2, -2, 1 / 16);
  6970. SetLength(Rec.MatrixV, 20);
  6971. SetEntry(Rec.MatrixV, 0, -2, 2, 1 / 16);
  6972. SetEntry(Rec.MatrixV, 1, -1, 2, 1 / 10);
  6973. SetEntry(Rec.MatrixV, 2, 0, 2, 0.25);
  6974. SetEntry(Rec.MatrixV, 3, 1, 2, 1 / 10);
  6975. SetEntry(Rec.MatrixV, 4, 2, 2, 1 / 16);
  6976. SetEntry(Rec.MatrixV, 5, -2, 1, 1 / 10);
  6977. SetEntry(Rec.MatrixV, 6, -1, 1, 1 / 8);
  6978. SetEntry(Rec.MatrixV, 7, 0, 1, 0.5);
  6979. SetEntry(Rec.MatrixV, 8, 1, 1, 1 / 8);
  6980. SetEntry(Rec.MatrixV, 9, 2, 1, 1 / 16);
  6981. SetEntry(Rec.MatrixV, 10, -2, -1, -1 / 16);
  6982. SetEntry(Rec.MatrixV, 11, -1, -1, -1 / 8);
  6983. SetEntry(Rec.MatrixV, 12, 0, -1, -0.5);
  6984. SetEntry(Rec.MatrixV, 13, 1, -1, -1 / 8);
  6985. SetEntry(Rec.MatrixV, 14, 2, -1, -1 / 10);
  6986. SetEntry(Rec.MatrixV, 15, -2, -2, -1 / 16);
  6987. SetEntry(Rec.MatrixV, 16, -1, -2, -1 / 10);
  6988. SetEntry(Rec.MatrixV, 17, 0, -2, -0.25);
  6989. SetEntry(Rec.MatrixV, 18, 1, -2, -1 / 10);
  6990. SetEntry(Rec.MatrixV, 19, 2, -2, -1 / 16);
  6991. end;
  6992. end;
  6993. // Daten Sammeln
  6994. if aUseAlpha and TFormatDescriptor.Get(Format).HasAlpha then
  6995. Convert(glBitmapToNormalMapPrepareAlphaFunc, false, @Rec)
  6996. else
  6997. Convert(glBitmapToNormalMapPrepareFunc, false, @Rec);
  6998. Convert(glBitmapToNormalMapFunc, false, @Rec);
  6999. finally
  7000. SetLength(Rec.Heights, 0);
  7001. end;
  7002. end;
  7003. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7004. constructor TglBitmapData.Create;
  7005. begin
  7006. inherited Create;
  7007. fFormat := glBitmapDefaultFormat;
  7008. end;
  7009. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7010. constructor TglBitmapData.Create(const aFileName: String);
  7011. begin
  7012. Create;
  7013. LoadFromFile(aFileName);
  7014. end;
  7015. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7016. constructor TglBitmapData.Create(const aStream: TStream);
  7017. begin
  7018. Create;
  7019. LoadFromStream(aStream);
  7020. end;
  7021. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7022. constructor TglBitmapData.Create(const aSize: TglBitmapSize; const aFormat: TglBitmapFormat; aData: PByte);
  7023. var
  7024. ImageSize: Integer;
  7025. begin
  7026. Create;
  7027. if not Assigned(aData) then begin
  7028. ImageSize := TFormatDescriptor.Get(aFormat).GetSize(aSize);
  7029. GetMem(aData, ImageSize);
  7030. try
  7031. FillChar(aData^, ImageSize, #$FF);
  7032. SetData(aData, aFormat, aSize.X, aSize.Y);
  7033. except
  7034. if Assigned(aData) then
  7035. FreeMem(aData);
  7036. raise;
  7037. end;
  7038. end else begin
  7039. SetData(aData, aFormat, aSize.X, aSize.Y);
  7040. end;
  7041. end;
  7042. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7043. constructor TglBitmapData.Create(const aSize: TglBitmapSize; const aFormat: TglBitmapFormat; const aFunc: TglBitmapFunction; const aArgs: Pointer);
  7044. begin
  7045. Create;
  7046. LoadFromFunc(aSize, aFormat, aFunc, aArgs);
  7047. end;
  7048. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7049. constructor TglBitmapData.Create(const aInstance: Cardinal; const aResource: String; const aResType: PChar);
  7050. begin
  7051. Create;
  7052. LoadFromResource(aInstance, aResource, aResType);
  7053. end;
  7054. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7055. constructor TglBitmapData.Create(const aInstance: Cardinal; const aResourceID: Integer; const aResType: PChar);
  7056. begin
  7057. Create;
  7058. LoadFromResourceID(aInstance, aResourceID, aResType);
  7059. end;
  7060. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7061. destructor TglBitmapData.Destroy;
  7062. begin
  7063. SetData(nil, tfEmpty);
  7064. inherited Destroy;
  7065. end;
  7066. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7067. //TglBitmap - PROTECTED///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7068. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7069. function TglBitmap.GetWidth: Integer;
  7070. begin
  7071. if (ffX in fDimension.Fields) then
  7072. result := fDimension.X
  7073. else
  7074. result := -1;
  7075. end;
  7076. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7077. function TglBitmap.GetHeight: Integer;
  7078. begin
  7079. if (ffY in fDimension.Fields) then
  7080. result := fDimension.Y
  7081. else
  7082. result := -1;
  7083. end;
  7084. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7085. procedure TglBitmap.SetCustomData(const aValue: Pointer);
  7086. begin
  7087. if fCustomData = aValue then
  7088. exit;
  7089. fCustomData := aValue;
  7090. end;
  7091. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7092. procedure TglBitmap.SetCustomName(const aValue: String);
  7093. begin
  7094. if fCustomName = aValue then
  7095. exit;
  7096. fCustomName := aValue;
  7097. end;
  7098. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7099. procedure TglBitmap.SetCustomNameW(const aValue: WideString);
  7100. begin
  7101. if fCustomNameW = aValue then
  7102. exit;
  7103. fCustomNameW := aValue;
  7104. end;
  7105. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7106. procedure TglBitmap.SetDeleteTextureOnFree(const aValue: Boolean);
  7107. begin
  7108. if fDeleteTextureOnFree = aValue then
  7109. exit;
  7110. fDeleteTextureOnFree := aValue;
  7111. end;
  7112. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7113. procedure TglBitmap.SetID(const aValue: Cardinal);
  7114. begin
  7115. if fID = aValue then
  7116. exit;
  7117. fID := aValue;
  7118. end;
  7119. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7120. procedure TglBitmap.SetMipMap(const aValue: TglBitmapMipMap);
  7121. begin
  7122. if fMipMap = aValue then
  7123. exit;
  7124. fMipMap := aValue;
  7125. end;
  7126. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7127. procedure TglBitmap.SetTarget(const aValue: Cardinal);
  7128. begin
  7129. if fTarget = aValue then
  7130. exit;
  7131. fTarget := aValue;
  7132. end;
  7133. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7134. procedure TglBitmap.SetAnisotropic(const aValue: Integer);
  7135. {$IF NOT DEFINED(OPENGL_ES) OR DEFINED(OPENGL_ES_EXT)}
  7136. var
  7137. MaxAnisotropic: Integer;
  7138. {$IFEND}
  7139. begin
  7140. fAnisotropic := aValue;
  7141. if (ID > 0) then begin
  7142. {$IF NOT DEFINED(OPENGL_ES) OR DEFINED(OPENGL_ES_EXT)}
  7143. if GL_EXT_texture_filter_anisotropic then begin
  7144. if fAnisotropic > 0 then begin
  7145. Bind({$IFNDEF OPENGL_ES}false{$ENDIF});
  7146. glGetIntegerv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, @MaxAnisotropic);
  7147. if aValue > MaxAnisotropic then
  7148. fAnisotropic := MaxAnisotropic;
  7149. glTexParameteri(Target, GL_TEXTURE_MAX_ANISOTROPY_EXT, fAnisotropic);
  7150. end;
  7151. end else begin
  7152. fAnisotropic := 0;
  7153. end;
  7154. {$ELSE}
  7155. fAnisotropic := 0;
  7156. {$IFEND}
  7157. end;
  7158. end;
  7159. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7160. procedure TglBitmap.CreateID;
  7161. begin
  7162. if (ID <> 0) then
  7163. glDeleteTextures(1, @fID);
  7164. glGenTextures(1, @fID);
  7165. Bind({$IFNDEF OPENGL_ES}false{$ENDIF});
  7166. end;
  7167. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7168. procedure TglBitmap.SetupParameters({$IFNDEF OPENGL_ES}out aBuildWithGlu: Boolean{$ENDIF});
  7169. begin
  7170. // Set Up Parameters
  7171. SetWrap(fWrapS, fWrapT, fWrapR);
  7172. SetFilter(fFilterMin, fFilterMag);
  7173. SetAnisotropic(fAnisotropic);
  7174. {$IFNDEF OPENGL_ES}
  7175. SetBorderColor(fBorderColor[0], fBorderColor[1], fBorderColor[2], fBorderColor[3]);
  7176. if (GL_ARB_texture_swizzle or GL_EXT_texture_swizzle or GL_VERSION_3_3) then
  7177. SetSwizzle(fSwizzle[0], fSwizzle[1], fSwizzle[2], fSwizzle[3]);
  7178. {$ENDIF}
  7179. {$IFNDEF OPENGL_ES}
  7180. // Mip Maps Generation Mode
  7181. aBuildWithGlu := false;
  7182. if (MipMap = mmMipmap) then begin
  7183. if (GL_VERSION_1_4 or GL_SGIS_generate_mipmap) then
  7184. glTexParameteri(Target, GL_GENERATE_MIPMAP, GLint(GL_TRUE))
  7185. else
  7186. aBuildWithGlu := true;
  7187. end else if (MipMap = mmMipmapGlu) then
  7188. aBuildWithGlu := true;
  7189. {$ELSE}
  7190. if (MipMap = mmMipmap) then
  7191. glGenerateMipmap(Target);
  7192. {$ENDIF}
  7193. end;
  7194. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7195. //TglBitmap - PUBLIC//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7196. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7197. procedure TglBitmap.AfterConstruction;
  7198. begin
  7199. inherited AfterConstruction;
  7200. fID := 0;
  7201. fTarget := 0;
  7202. {$IFNDEF OPENGL_ES}
  7203. fIsResident := false;
  7204. {$ENDIF}
  7205. fMipMap := glBitmapDefaultMipmap;
  7206. fDeleteTextureOnFree := glBitmapGetDefaultDeleteTextureOnFree;
  7207. glBitmapGetDefaultFilter (fFilterMin, fFilterMag);
  7208. glBitmapGetDefaultTextureWrap(fWrapS, fWrapT, fWrapR);
  7209. {$IFNDEF OPENGL_ES}
  7210. glBitmapGetDefaultSwizzle (fSwizzle[0], fSwizzle[1], fSwizzle[2], fSwizzle[3]);
  7211. {$ENDIF}
  7212. end;
  7213. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7214. procedure TglBitmap.BeforeDestruction;
  7215. begin
  7216. if (fID > 0) and fDeleteTextureOnFree then
  7217. glDeleteTextures(1, @fID);
  7218. inherited BeforeDestruction;
  7219. end;
  7220. {$IFNDEF OPENGL_ES}
  7221. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7222. procedure TglBitmap.SetBorderColor(const aRed, aGreen, aBlue, aAlpha: Single);
  7223. begin
  7224. fBorderColor[0] := aRed;
  7225. fBorderColor[1] := aGreen;
  7226. fBorderColor[2] := aBlue;
  7227. fBorderColor[3] := aAlpha;
  7228. if (ID > 0) then begin
  7229. Bind(false);
  7230. glTexParameterfv(Target, GL_TEXTURE_BORDER_COLOR, @fBorderColor[0]);
  7231. end;
  7232. end;
  7233. {$ENDIF}
  7234. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7235. procedure TglBitmap.SetFilter(const aMin, aMag: GLenum);
  7236. begin
  7237. //check MIN filter
  7238. case aMin of
  7239. GL_NEAREST:
  7240. fFilterMin := GL_NEAREST;
  7241. GL_LINEAR:
  7242. fFilterMin := GL_LINEAR;
  7243. GL_NEAREST_MIPMAP_NEAREST:
  7244. fFilterMin := GL_NEAREST_MIPMAP_NEAREST;
  7245. GL_LINEAR_MIPMAP_NEAREST:
  7246. fFilterMin := GL_LINEAR_MIPMAP_NEAREST;
  7247. GL_NEAREST_MIPMAP_LINEAR:
  7248. fFilterMin := GL_NEAREST_MIPMAP_LINEAR;
  7249. GL_LINEAR_MIPMAP_LINEAR:
  7250. fFilterMin := GL_LINEAR_MIPMAP_LINEAR;
  7251. else
  7252. raise EglBitmap.Create('SetFilter - Unknow MIN filter.');
  7253. end;
  7254. //check MAG filter
  7255. case aMag of
  7256. GL_NEAREST:
  7257. fFilterMag := GL_NEAREST;
  7258. GL_LINEAR:
  7259. fFilterMag := GL_LINEAR;
  7260. else
  7261. raise EglBitmap.Create('SetFilter - Unknow MAG filter.');
  7262. end;
  7263. //apply filter
  7264. if (ID > 0) then begin
  7265. Bind({$IFNDEF OPENGL_ES}false{$ENDIF});
  7266. glTexParameteri(Target, GL_TEXTURE_MAG_FILTER, fFilterMag);
  7267. if (MipMap = mmNone) {$IFNDEF OPENGL_ES}or (Target = GL_TEXTURE_RECTANGLE){$ENDIF} then begin
  7268. case fFilterMin of
  7269. GL_NEAREST, GL_LINEAR:
  7270. glTexParameteri(Target, GL_TEXTURE_MIN_FILTER, fFilterMin);
  7271. GL_NEAREST_MIPMAP_NEAREST, GL_NEAREST_MIPMAP_LINEAR:
  7272. glTexParameteri(Target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  7273. GL_LINEAR_MIPMAP_NEAREST, GL_LINEAR_MIPMAP_LINEAR:
  7274. glTexParameteri(Target, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  7275. end;
  7276. end else
  7277. glTexParameteri(Target, GL_TEXTURE_MIN_FILTER, fFilterMin);
  7278. end;
  7279. end;
  7280. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7281. procedure TglBitmap.SetWrap(const S: GLenum; const T: GLenum; const R: GLenum);
  7282. procedure CheckAndSetWrap(const aValue: Cardinal; var aTarget: Cardinal);
  7283. begin
  7284. case aValue of
  7285. {$IFNDEF OPENGL_ES}
  7286. GL_CLAMP:
  7287. aTarget := GL_CLAMP;
  7288. {$ENDIF}
  7289. GL_REPEAT:
  7290. aTarget := GL_REPEAT;
  7291. GL_CLAMP_TO_EDGE: begin
  7292. {$IFNDEF OPENGL_ES}
  7293. if not GL_VERSION_1_2 and not GL_EXT_texture_edge_clamp then
  7294. aTarget := GL_CLAMP
  7295. else
  7296. {$ENDIF}
  7297. aTarget := GL_CLAMP_TO_EDGE;
  7298. end;
  7299. {$IFNDEF OPENGL_ES}
  7300. GL_CLAMP_TO_BORDER: begin
  7301. if GL_VERSION_1_3 or GL_ARB_texture_border_clamp then
  7302. aTarget := GL_CLAMP_TO_BORDER
  7303. else
  7304. aTarget := GL_CLAMP;
  7305. end;
  7306. {$ENDIF}
  7307. {$IF NOT DEFINED(OPENGL_ES) OR DEFINED(OPENGL_ES_2_0)}
  7308. GL_MIRRORED_REPEAT: begin
  7309. {$IFNDEF OPENGL_ES}
  7310. if GL_VERSION_1_4 or GL_ARB_texture_mirrored_repeat or GL_IBM_texture_mirrored_repeat then
  7311. {$ELSE}
  7312. if GL_VERSION_2_0 then
  7313. {$ENDIF}
  7314. aTarget := GL_MIRRORED_REPEAT
  7315. else
  7316. raise EglBitmap.Create('SetWrap - Unsupported Texturewrap GL_MIRRORED_REPEAT (S).');
  7317. end;
  7318. {$IFEND}
  7319. else
  7320. raise EglBitmap.Create('SetWrap - Unknow Texturewrap');
  7321. end;
  7322. end;
  7323. begin
  7324. CheckAndSetWrap(S, fWrapS);
  7325. CheckAndSetWrap(T, fWrapT);
  7326. CheckAndSetWrap(R, fWrapR);
  7327. if (ID > 0) then begin
  7328. Bind({$IFNDEF OPENGL_ES}false{$ENDIF});
  7329. glTexParameteri(Target, GL_TEXTURE_WRAP_S, fWrapS);
  7330. glTexParameteri(Target, GL_TEXTURE_WRAP_T, fWrapT);
  7331. {$IF NOT DEFINED(OPENGL_ES) OR DEFINED(OPENGL_ES_3_0)}
  7332. {$IFDEF OPENGL_ES} if GL_VERSION_3_0 then{$ENDIF}
  7333. glTexParameteri(Target, GL_TEXTURE_WRAP_R, fWrapR);
  7334. {$IFEND}
  7335. end;
  7336. end;
  7337. {$IF NOT DEFINED(OPENGL_ES) OR DEFINED(OPENGL_ES_3_0)}
  7338. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7339. procedure TglBitmap.SetSwizzle(const r, g, b, a: GLenum);
  7340. procedure CheckAndSetValue(const aValue: GLenum; const aIndex: Integer);
  7341. begin
  7342. if (aValue = GL_ZERO) or (aValue = GL_ONE) or (aValue = GL_ALPHA) or
  7343. (aValue = GL_RED) or (aValue = GL_GREEN) or (aValue = GL_BLUE) then
  7344. fSwizzle[aIndex] := aValue
  7345. else
  7346. raise EglBitmap.Create('SetSwizzle - Unknow Swizle Value');
  7347. end;
  7348. begin
  7349. {$IFNDEF OPENGL_ES}
  7350. if not (GL_ARB_texture_swizzle or GL_EXT_texture_swizzle or GL_VERSION_3_3) then
  7351. raise EglBitmapNotSupported.Create('texture swizzle is not supported');
  7352. {$ELSE}
  7353. if not GL_VERSION_3_0 then
  7354. raise EglBitmapNotSupported.Create('texture swizzle is not supported');
  7355. {$ENDIF}
  7356. CheckAndSetValue(r, 0);
  7357. CheckAndSetValue(g, 1);
  7358. CheckAndSetValue(b, 2);
  7359. CheckAndSetValue(a, 3);
  7360. if (ID > 0) then begin
  7361. Bind(false);
  7362. {$IFNDEF OPENGL_ES}
  7363. glTexParameteriv(Target, GL_TEXTURE_SWIZZLE_RGBA, PGLint(@fSwizzle[0]));
  7364. {$ELSE}
  7365. glTexParameteriv(Target, GL_TEXTURE_SWIZZLE_R, PGLint(@fSwizzle[0]));
  7366. glTexParameteriv(Target, GL_TEXTURE_SWIZZLE_G, PGLint(@fSwizzle[1]));
  7367. glTexParameteriv(Target, GL_TEXTURE_SWIZZLE_B, PGLint(@fSwizzle[2]));
  7368. glTexParameteriv(Target, GL_TEXTURE_SWIZZLE_A, PGLint(@fSwizzle[3]));
  7369. {$ENDIF}
  7370. end;
  7371. end;
  7372. {$IFEND}
  7373. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7374. procedure TglBitmap.Bind({$IFNDEF OPENGL_ES}const aEnableTextureUnit: Boolean{$ENDIF});
  7375. begin
  7376. {$IFNDEF OPENGL_ES}
  7377. if aEnableTextureUnit then
  7378. glEnable(Target);
  7379. {$ENDIF}
  7380. if (ID > 0) then
  7381. glBindTexture(Target, ID);
  7382. end;
  7383. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7384. procedure TglBitmap.Unbind({$IFNDEF OPENGL_ES}const aDisableTextureUnit: Boolean{$ENDIF});
  7385. begin
  7386. {$IFNDEF OPENGL_ES}
  7387. if aDisableTextureUnit then
  7388. glDisable(Target);
  7389. {$ENDIF}
  7390. glBindTexture(Target, 0);
  7391. end;
  7392. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7393. procedure TglBitmap.UploadData(const aDataObj: TglBitmapData; const aCheckSize: Boolean);
  7394. var
  7395. w, h: Integer;
  7396. begin
  7397. w := aDataObj.Width;
  7398. h := aDataObj.Height;
  7399. fDimension.Fields := [];
  7400. if (w > 0) then
  7401. fDimension.Fields := fDimension.Fields + [ffX];
  7402. if (h > 0) then
  7403. fDimension.Fields := fDimension.Fields + [ffY];
  7404. fDimension.X := w;
  7405. fDimension.Y := h;
  7406. end;
  7407. {$IFNDEF OPENGL_ES}
  7408. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7409. function TglBitmap.DownloadData(const aDataObj: TglBitmapData): Boolean;
  7410. var
  7411. Temp: PByte;
  7412. TempWidth, TempHeight: Integer;
  7413. TempIntFormat: GLint;
  7414. IntFormat: TglBitmapFormat;
  7415. FormatDesc: TFormatDescriptor;
  7416. begin
  7417. result := false;
  7418. Bind;
  7419. // Request Data
  7420. glGetTexLevelParameteriv(Target, 0, GL_TEXTURE_WIDTH, @TempWidth);
  7421. glGetTexLevelParameteriv(Target, 0, GL_TEXTURE_HEIGHT, @TempHeight);
  7422. glGetTexLevelParameteriv(Target, 0, GL_TEXTURE_INTERNAL_FORMAT, @TempIntFormat);
  7423. FormatDesc := (TglBitmapFormatDescriptor.GetByFormat(TempIntFormat) as TFormatDescriptor);
  7424. IntFormat := FormatDesc.Format;
  7425. // Getting data from OpenGL
  7426. FormatDesc := TFormatDescriptor.Get(IntFormat);
  7427. GetMem(Temp, FormatDesc.GetSize(TempWidth, TempHeight));
  7428. try
  7429. glPixelStorei(GL_PACK_ALIGNMENT, 1);
  7430. if FormatDesc.IsCompressed then begin
  7431. if not Assigned(glGetCompressedTexImage) then
  7432. raise EglBitmap.Create('compressed formats not supported by video adapter');
  7433. glGetCompressedTexImage(Target, 0, Temp)
  7434. end else
  7435. glGetTexImage(Target, 0, FormatDesc.glFormat, FormatDesc.glDataFormat, Temp);
  7436. aDataObj.SetData(Temp, IntFormat, TempWidth, TempHeight);
  7437. result := true;
  7438. except
  7439. if Assigned(Temp) then
  7440. FreeMem(Temp);
  7441. raise;
  7442. end;
  7443. end;
  7444. {$ENDIF}
  7445. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7446. constructor TglBitmap.Create;
  7447. begin
  7448. if (ClassType = TglBitmap) then
  7449. raise EglBitmap.Create('Don''t create TglBitmap directly. Use one of the deviated classes (TglBitmap2D) instead.');
  7450. inherited Create;
  7451. end;
  7452. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7453. constructor TglBitmap.Create(const aData: TglBitmapData);
  7454. begin
  7455. Create;
  7456. UploadData(aData);
  7457. end;
  7458. {$IFNDEF OPENGL_ES}
  7459. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7460. //TglBitmap1D/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7461. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7462. procedure TglBitmap1D.UploadDataIntern(const aDataObj: TglBitmapData; const aBuildWithGlu: Boolean);
  7463. var
  7464. fd: TglBitmapFormatDescriptor;
  7465. begin
  7466. // Upload data
  7467. fd := aDataObj.FormatDescriptor;
  7468. if (fd.glFormat = 0) or (fd.glInternalFormat = 0) or (fd.glDataFormat = 0) then
  7469. raise EglBitmap.Create('format is not supported by video adapter, please convert before uploading data');
  7470. if fd.IsCompressed then begin
  7471. if not Assigned(glCompressedTexImage1D) then
  7472. raise EglBitmap.Create('compressed formats not supported by video adapter');
  7473. glCompressedTexImage1D(Target, 0, fd.glInternalFormat, aDataObj.Width, 0, fd.GetSize(aDataObj.Width, 1), aDataObj.Data)
  7474. end else if aBuildWithGlu then
  7475. gluBuild1DMipmaps(Target, fd.glInternalFormat, aDataObj.Width, fd.glFormat, fd.glDataFormat, aDataObj.Data)
  7476. else
  7477. glTexImage1D(Target, 0, fd.glInternalFormat, aDataObj.Width, 0, fd.glFormat, fd.glDataFormat, aDataObj.Data);
  7478. end;
  7479. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7480. procedure TglBitmap1D.AfterConstruction;
  7481. begin
  7482. inherited;
  7483. Target := GL_TEXTURE_1D;
  7484. end;
  7485. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7486. procedure TglBitmap1D.UploadData(const aDataObj: TglBitmapData; const aCheckSize: Boolean);
  7487. var
  7488. BuildWithGlu, TexRec: Boolean;
  7489. TexSize: Integer;
  7490. begin
  7491. if not Assigned(aDataObj) then
  7492. exit;
  7493. // Check Texture Size
  7494. if (aCheckSize) then begin
  7495. glGetIntegerv(GL_MAX_TEXTURE_SIZE, @TexSize);
  7496. if (aDataObj.Width > TexSize) then
  7497. raise EglBitmapSizeToLarge.Create('TglBitmap1D.GenTexture - The size for the texture is to large. It''s may be not conform with the Hardware.');
  7498. TexRec := (GL_ARB_texture_rectangle or GL_EXT_texture_rectangle or GL_NV_texture_rectangle) and
  7499. (Target = GL_TEXTURE_RECTANGLE);
  7500. if not (IsPowerOfTwo(aDataObj.Width) or GL_ARB_texture_non_power_of_two or GL_VERSION_2_0 or TexRec) then
  7501. raise EglBitmapNonPowerOfTwo.Create('TglBitmap1D.GenTexture - Rendercontex dosn''t support non power of two texture.');
  7502. end;
  7503. inherited UploadData(aDataObj, aCheckSize);
  7504. if (fID = 0) then
  7505. CreateID;
  7506. SetupParameters(BuildWithGlu);
  7507. UploadDataIntern(aDataObj, BuildWithGlu);
  7508. glAreTexturesResident(1, @fID, @fIsResident);
  7509. end;
  7510. {$ENDIF}
  7511. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7512. //TglBitmap2D/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7513. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7514. procedure TglBitmap2D.UploadDataIntern(const aDataObj: TglBitmapData; const aTarget: GLenum{$IFNDEF OPENGL_ES}; const aBuildWithGlu: Boolean{$ENDIF});
  7515. var
  7516. fd: TglBitmapFormatDescriptor;
  7517. begin
  7518. fd := aDataObj.FormatDescriptor;
  7519. if (fd.glFormat = 0) or (fd.glInternalFormat = 0) or (fd.glDataFormat = 0) then
  7520. raise EglBitmap.Create('format is not supported by video adapter, please convert before uploading data');
  7521. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  7522. if fd.IsCompressed then begin
  7523. if not Assigned(glCompressedTexImage2D) then
  7524. raise EglBitmap.Create('compressed formats not supported by video adapter');
  7525. glCompressedTexImage2D(aTarget, 0, fd.glInternalFormat, aDataObj.Width, aDataObj.Height, 0, fd.GetSize(fDimension), aDataObj.Data)
  7526. {$IFNDEF OPENGL_ES}
  7527. end else if aBuildWithGlu then begin
  7528. gluBuild2DMipmaps(aTarget, fd.ChannelCount, aDataObj.Width, aDataObj.Height, fd.glFormat, fd.glDataFormat, aDataObj.Data)
  7529. {$ENDIF}
  7530. end else begin
  7531. glTexImage2D(aTarget, 0, fd.glInternalFormat, aDataObj.Width, aDataObj.Height, 0, fd.glFormat, fd.glDataFormat, aDataObj.Data);
  7532. end;
  7533. end;
  7534. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7535. procedure TglBitmap2D.AfterConstruction;
  7536. begin
  7537. inherited;
  7538. Target := GL_TEXTURE_2D;
  7539. end;
  7540. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7541. procedure TglBitmap2D.UploadData(const aDataObj: TglBitmapData; const aCheckSize: Boolean);
  7542. var
  7543. {$IFNDEF OPENGL_ES}
  7544. BuildWithGlu, TexRec: Boolean;
  7545. {$ENDIF}
  7546. PotTex: Boolean;
  7547. TexSize: Integer;
  7548. begin
  7549. if not Assigned(aDataObj) then
  7550. exit;
  7551. // Check Texture Size
  7552. if (aCheckSize) then begin
  7553. glGetIntegerv(GL_MAX_TEXTURE_SIZE, @TexSize);
  7554. if ((aDataObj.Width > TexSize) or (aDataObj.Height > TexSize)) then
  7555. raise EglBitmapSizeToLarge.Create('TglBitmap2D.GenTexture - The size for the texture is to large. It''s may be not conform with the Hardware.');
  7556. PotTex := IsPowerOfTwo(aDataObj.Width) and IsPowerOfTwo(aDataObj.Height);
  7557. {$IF NOT DEFINED(OPENGL_ES)}
  7558. TexRec := (GL_ARB_texture_rectangle or GL_EXT_texture_rectangle or GL_NV_texture_rectangle) and (Target = GL_TEXTURE_RECTANGLE);
  7559. if not (PotTex or GL_ARB_texture_non_power_of_two or GL_VERSION_2_0 or TexRec) then
  7560. raise EglBitmapNonPowerOfTwo.Create('TglBitmap2D.GenTexture - Rendercontex dosn''t support non power of two texture.');
  7561. {$ELSEIF DEFINED(OPENGL_ES_EXT)}
  7562. if not PotTex and not GL_OES_texture_npot then
  7563. raise EglBitmapNonPowerOfTwo.Create('TglBitmap2D.GenTexture - Rendercontex dosn''t support non power of two texture.');
  7564. {$ELSE}
  7565. if not PotTex then
  7566. raise EglBitmapNonPowerOfTwo.Create('TglBitmap2D.GenTexture - Rendercontex dosn''t support non power of two texture.');
  7567. {$IFEND}
  7568. end;
  7569. inherited UploadData(aDataObj, aCheckSize);
  7570. if (fID = 0) then
  7571. CreateID;
  7572. SetupParameters({$IFNDEF OPENGL_ES}BuildWithGlu{$ENDIF});
  7573. UploadDataIntern(aDataObj, Target{$IFNDEF OPENGL_ES}, BuildWithGlu{$ENDIF});
  7574. {$IFNDEF OPENGL_ES}
  7575. glAreTexturesResident(1, @fID, @fIsResident);
  7576. {$ENDIF}
  7577. end;
  7578. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7579. class procedure TglBitmap2D.GrabScreen(const aTop, aLeft, aRight, aBottom: Integer; const aFormat: TglBitmapFormat; const aDataObj: TglBitmapData);
  7580. var
  7581. Temp: pByte;
  7582. Size, w, h: Integer;
  7583. FormatDesc: TFormatDescriptor;
  7584. begin
  7585. FormatDesc := TFormatDescriptor.Get(aFormat);
  7586. if FormatDesc.IsCompressed then
  7587. raise EglBitmapUnsupportedFormat.Create(aFormat);
  7588. w := aRight - aLeft;
  7589. h := aBottom - aTop;
  7590. Size := FormatDesc.GetSize(w, h);
  7591. GetMem(Temp, Size);
  7592. try
  7593. glPixelStorei(GL_PACK_ALIGNMENT, 1);
  7594. glReadPixels(aLeft, aTop, w, h, FormatDesc.glFormat, FormatDesc.glDataFormat, Temp);
  7595. aDataObj.SetData(Temp, aFormat, w, h);
  7596. aDataObj.FlipVert;
  7597. except
  7598. if Assigned(Temp) then
  7599. FreeMem(Temp);
  7600. raise;
  7601. end;
  7602. end;
  7603. {$IF NOT DEFINED(OPENGL_ES) OR DEFINED(OPENGL_ES_2_0)}
  7604. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7605. //TglBitmapCubeMap////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7606. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7607. procedure TglBitmapCubeMap.AfterConstruction;
  7608. begin
  7609. inherited;
  7610. {$IFNDEF OPENGL_ES}
  7611. if not (GL_VERSION_1_3 or GL_ARB_texture_cube_map or GL_EXT_texture_cube_map) then
  7612. raise EglBitmap.Create('TglBitmapCubeMap.AfterConstruction - CubeMaps are unsupported.');
  7613. {$ELSE}
  7614. if not (GL_VERSION_2_0) then
  7615. raise EglBitmap.Create('TglBitmapCubeMap.AfterConstruction - CubeMaps are unsupported.');
  7616. {$ENDIF}
  7617. SetWrap;
  7618. Target := GL_TEXTURE_CUBE_MAP;
  7619. {$IFNDEF OPENGL_ES}
  7620. fGenMode := GL_REFLECTION_MAP;
  7621. {$ENDIF}
  7622. end;
  7623. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7624. procedure TglBitmapCubeMap.UploadData(const aDataObj: TglBitmapData; const aCheckSize: Boolean);
  7625. begin
  7626. Assert(false, 'TglBitmapCubeMap.UploadData - Don''t call UploadData directly, use UploadCubeMap instead');
  7627. end;
  7628. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7629. procedure TglBitmapCubeMap.UploadCubeMap(const aDataObj: TglBitmapData; const aCubeTarget: Cardinal; const aCheckSize: Boolean);
  7630. var
  7631. {$IFNDEF OPENGL_ES}
  7632. BuildWithGlu: Boolean;
  7633. {$ENDIF}
  7634. TexSize: Integer;
  7635. begin
  7636. if (aCheckSize) then begin
  7637. glGetIntegerv(GL_MAX_CUBE_MAP_TEXTURE_SIZE, @TexSize);
  7638. if (aDataObj.Width > TexSize) or (aDataObj.Height > TexSize) then
  7639. raise EglBitmapSizeToLarge.Create('TglBitmapCubeMap.GenerateCubeMap - The size for the Cubemap is to large. It''s may be not conform with the Hardware.');
  7640. {$IF NOT DEFINED(OPENGL_ES)}
  7641. if not ((IsPowerOfTwo(aDataObj.Width) and IsPowerOfTwo(aDataObj.Height)) or GL_VERSION_2_0 or GL_ARB_texture_non_power_of_two) then
  7642. raise EglBitmapNonPowerOfTwo.Create('TglBitmapCubeMap.GenerateCubeMap - Cubemaps dosn''t support non power of two texture.');
  7643. {$ELSEIF DEFINED(OPENGL_ES_EXT)}
  7644. if not (IsPowerOfTwo(aDataObj.Width) and IsPowerOfTwo(aDataObj.Height)) and not GL_OES_texture_npot then
  7645. raise EglBitmapNonPowerOfTwo.Create('TglBitmapCubeMap.GenerateCubeMap - Cubemaps dosn''t support non power of two texture.');
  7646. {$ELSE}
  7647. if not (IsPowerOfTwo(aDataObj.Width) and IsPowerOfTwo(aDataObj.Height)) then
  7648. raise EglBitmapNonPowerOfTwo.Create('TglBitmapCubeMap.GenerateCubeMap - Cubemaps dosn''t support non power of two texture.');
  7649. {$IFEND}
  7650. end;
  7651. inherited UploadData(aDataObj, aCheckSize);
  7652. if (fID = 0) then
  7653. CreateID;
  7654. SetupParameters({$IFNDEF OPENGL_ES}BuildWithGlu{$ENDIF});
  7655. UploadDataIntern(aDataObj, aCubeTarget{$IFNDEF OPENGL_ES}, BuildWithGlu{$ENDIF});
  7656. end;
  7657. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7658. procedure TglBitmapCubeMap.Bind({$IFNDEF OPENGL_ES}const aEnableTexCoordsGen: Boolean; const aEnableTextureUnit: Boolean{$ENDIF});
  7659. begin
  7660. inherited Bind({$IFNDEF OPENGL_ES}aEnableTextureUnit{$ENDIF});
  7661. {$IFNDEF OPENGL_ES}
  7662. if aEnableTexCoordsGen then begin
  7663. glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, fGenMode);
  7664. glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, fGenMode);
  7665. glTexGeni(GL_R, GL_TEXTURE_GEN_MODE, fGenMode);
  7666. glEnable(GL_TEXTURE_GEN_S);
  7667. glEnable(GL_TEXTURE_GEN_T);
  7668. glEnable(GL_TEXTURE_GEN_R);
  7669. end;
  7670. {$ENDIF}
  7671. end;
  7672. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7673. procedure TglBitmapCubeMap.Unbind({$IFNDEF OPENGL_ES}const aDisableTexCoordsGen: Boolean; const aDisableTextureUnit: Boolean{$ENDIF});
  7674. begin
  7675. inherited Unbind({$IFNDEF OPENGL_ES}aDisableTextureUnit{$ENDIF});
  7676. {$IFNDEF OPENGL_ES}
  7677. if aDisableTexCoordsGen then begin
  7678. glDisable(GL_TEXTURE_GEN_S);
  7679. glDisable(GL_TEXTURE_GEN_T);
  7680. glDisable(GL_TEXTURE_GEN_R);
  7681. end;
  7682. {$ENDIF}
  7683. end;
  7684. {$IFEND}
  7685. {$IF NOT DEFINED(OPENGL_ES) OR DEFINED(OPENGL_ES_2_0)}
  7686. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7687. //TglBitmapNormalMap//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7688. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7689. type
  7690. TVec = Array[0..2] of Single;
  7691. TglBitmapNormalMapGetVectorFunc = procedure (out aVec: TVec; const aPosition: TglBitmapPixelPosition; const aHalfSize: Integer);
  7692. PglBitmapNormalMapRec = ^TglBitmapNormalMapRec;
  7693. TglBitmapNormalMapRec = record
  7694. HalfSize : Integer;
  7695. Func: TglBitmapNormalMapGetVectorFunc;
  7696. end;
  7697. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7698. procedure glBitmapNormalMapPosX(out aVec: TVec; const aPosition: TglBitmapPixelPosition; const aHalfSize: Integer);
  7699. begin
  7700. aVec[0] := aHalfSize;
  7701. aVec[1] := - (aPosition.Y + 0.5 - aHalfSize);
  7702. aVec[2] := - (aPosition.X + 0.5 - aHalfSize);
  7703. end;
  7704. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7705. procedure glBitmapNormalMapNegX(out aVec: TVec; const aPosition: TglBitmapPixelPosition; const aHalfSize: Integer);
  7706. begin
  7707. aVec[0] := - aHalfSize;
  7708. aVec[1] := - (aPosition.Y + 0.5 - aHalfSize);
  7709. aVec[2] := aPosition.X + 0.5 - aHalfSize;
  7710. end;
  7711. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7712. procedure glBitmapNormalMapPosY(out aVec: TVec; const aPosition: TglBitmapPixelPosition; const aHalfSize: Integer);
  7713. begin
  7714. aVec[0] := aPosition.X + 0.5 - aHalfSize;
  7715. aVec[1] := aHalfSize;
  7716. aVec[2] := aPosition.Y + 0.5 - aHalfSize;
  7717. end;
  7718. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7719. procedure glBitmapNormalMapNegY(out aVec: TVec; const aPosition: TglBitmapPixelPosition; const aHalfSize: Integer);
  7720. begin
  7721. aVec[0] := aPosition.X + 0.5 - aHalfSize;
  7722. aVec[1] := - aHalfSize;
  7723. aVec[2] := - (aPosition.Y + 0.5 - aHalfSize);
  7724. end;
  7725. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7726. procedure glBitmapNormalMapPosZ(out aVec: TVec; const aPosition: TglBitmapPixelPosition; const aHalfSize: Integer);
  7727. begin
  7728. aVec[0] := aPosition.X + 0.5 - aHalfSize;
  7729. aVec[1] := - (aPosition.Y + 0.5 - aHalfSize);
  7730. aVec[2] := aHalfSize;
  7731. end;
  7732. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7733. procedure glBitmapNormalMapNegZ(out aVec: TVec; const aPosition: TglBitmapPixelPosition; const aHalfSize: Integer);
  7734. begin
  7735. aVec[0] := - (aPosition.X + 0.5 - aHalfSize);
  7736. aVec[1] := - (aPosition.Y + 0.5 - aHalfSize);
  7737. aVec[2] := - aHalfSize;
  7738. end;
  7739. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7740. procedure glBitmapNormalMapFunc(var FuncRec: TglBitmapFunctionRec);
  7741. var
  7742. i: Integer;
  7743. Vec: TVec;
  7744. Len: Single;
  7745. begin
  7746. with FuncRec do begin
  7747. with PglBitmapNormalMapRec(Args)^ do begin
  7748. Func(Vec, Position, HalfSize);
  7749. // Normalize
  7750. Len := 1 / Sqrt(Sqr(Vec[0]) + Sqr(Vec[1]) + Sqr(Vec[2]));
  7751. if Len <> 0 then begin
  7752. Vec[0] := Vec[0] * Len;
  7753. Vec[1] := Vec[1] * Len;
  7754. Vec[2] := Vec[2] * Len;
  7755. end;
  7756. // Scale Vector and AddVectro
  7757. Vec[0] := Vec[0] * 0.5 + 0.5;
  7758. Vec[1] := Vec[1] * 0.5 + 0.5;
  7759. Vec[2] := Vec[2] * 0.5 + 0.5;
  7760. end;
  7761. // Set Color
  7762. for i := 0 to 2 do
  7763. Dest.Data.arr[i] := Round(Vec[i] * 255);
  7764. end;
  7765. end;
  7766. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7767. procedure TglBitmapNormalMap.AfterConstruction;
  7768. begin
  7769. inherited;
  7770. {$IFNDEF OPENGL_ES}
  7771. fGenMode := GL_NORMAL_MAP;
  7772. {$ENDIF}
  7773. end;
  7774. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7775. procedure TglBitmapNormalMap.GenerateNormalMap(const aSize: Integer; const aCheckSize: Boolean);
  7776. var
  7777. Rec: TglBitmapNormalMapRec;
  7778. SizeRec: TglBitmapSize;
  7779. DataObj: TglBitmapData;
  7780. begin
  7781. Rec.HalfSize := aSize div 2;
  7782. SizeRec.Fields := [ffX, ffY];
  7783. SizeRec.X := aSize;
  7784. SizeRec.Y := aSize;
  7785. DataObj := TglBitmapData.Create;
  7786. try
  7787. // Positive X
  7788. Rec.Func := glBitmapNormalMapPosX;
  7789. DataObj.LoadFromFunc(SizeRec, tfBGR8ub3, glBitmapNormalMapFunc, @Rec);
  7790. UploadCubeMap(DataObj, GL_TEXTURE_CUBE_MAP_POSITIVE_X, aCheckSize);
  7791. // Negative X
  7792. Rec.Func := glBitmapNormalMapNegX;
  7793. DataObj.LoadFromFunc(SizeRec, tfBGR8ub3, glBitmapNormalMapFunc, @Rec);
  7794. UploadCubeMap(DataObj, GL_TEXTURE_CUBE_MAP_NEGATIVE_X, aCheckSize);
  7795. // Positive Y
  7796. Rec.Func := glBitmapNormalMapPosY;
  7797. DataObj.LoadFromFunc(SizeRec, tfBGR8ub3, glBitmapNormalMapFunc, @Rec);
  7798. UploadCubeMap(DataObj, GL_TEXTURE_CUBE_MAP_POSITIVE_Y, aCheckSize);
  7799. // Negative Y
  7800. Rec.Func := glBitmapNormalMapNegY;
  7801. DataObj.LoadFromFunc(SizeRec, tfBGR8ub3, glBitmapNormalMapFunc, @Rec);
  7802. UploadCubeMap(DataObj, GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, aCheckSize);
  7803. // Positive Z
  7804. Rec.Func := glBitmapNormalMapPosZ;
  7805. DataObj.LoadFromFunc(SizeRec, tfBGR8ub3, glBitmapNormalMapFunc, @Rec);
  7806. UploadCubeMap(DataObj, GL_TEXTURE_CUBE_MAP_POSITIVE_Z, aCheckSize);
  7807. // Negative Z
  7808. Rec.Func := glBitmapNormalMapNegZ;
  7809. DataObj.LoadFromFunc(SizeRec, tfBGR8ub3, glBitmapNormalMapFunc, @Rec);
  7810. UploadCubeMap(DataObj, GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, aCheckSize);
  7811. finally
  7812. FreeAndNil(DataObj);
  7813. end;
  7814. end;
  7815. {$IFEND}
  7816. initialization
  7817. glBitmapSetDefaultFormat (tfEmpty);
  7818. glBitmapSetDefaultMipmap (mmMipmap);
  7819. glBitmapSetDefaultFilter (GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR);
  7820. glBitmapSetDefaultWrap (GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
  7821. {$IF NOT DEFINED(OPENGL_ES) OR DEFINED(OPENGL_ES_3_0)}
  7822. glBitmapSetDefaultSwizzle(GL_RED, GL_GREEN, GL_BLUE, GL_ALPHA);
  7823. {$IFEND}
  7824. glBitmapSetDefaultFreeDataAfterGenTexture(true);
  7825. glBitmapSetDefaultDeleteTextureOnFree (true);
  7826. TFormatDescriptor.Init;
  7827. finalization
  7828. TFormatDescriptor.Finalize;
  7829. end.