You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

8905 lines
317 KiB

  1. { glBitmap by Steffen Xonna aka Lossy eX (2003-2008)
  2. http://www.opengl24.de/index.php?cat=header&file=glbitmap
  3. modified by Delphi OpenGL Community (http://delphigl.com/) (2013)
  4. The contents of this file are used with permission, subject to
  5. the Mozilla Public License Version 1.1 (the "License"); you may
  6. not use this file except in compliance with the License. You may
  7. obtain a copy of the License at
  8. http://www.mozilla.org/MPL/MPL-1.1.html
  9. The glBitmap is a Delphi/FPC unit that contains several wrapper classes
  10. to manage OpenGL texture objects. Below you can find a list of the main
  11. functionality of this classes:
  12. - load texture data from file (e.g. BMP, TGA, DDS, PNG, JPEG, ...)
  13. - load texture data from several other image objects (e.g. TBitmap, TLazIntfImage, SDL Surface)
  14. - save texture data to file (e.g. BMP, TGA, DDS, PNG, JPEG, ...)
  15. - save texture data to several other image objects (e.g. TBitmap, TLazIntfImage, SDL Surface)
  16. - support for many texture formats (e.g. RGB8, BGR8, RGBA8, BGRA8, ...)
  17. - manage texture properties (e.g. Filter, Clamp, Mipmap, ...)
  18. - upload texture data to video card
  19. - download texture data from video card
  20. - manipulate texture data (e.g. add alpha, remove alpha, convert to other format, switch RGB, ...) }
  21. unit glBitmap;
  22. {$I glBitmapConf.inc}
  23. // Delphi Versions
  24. {$IFDEF fpc}
  25. {$MODE Delphi}
  26. {$MINENUMSIZE DEFAULT}
  27. {$IFDEF CPUI386}
  28. {$DEFINE CPU386}
  29. {$ASMMODE INTEL}
  30. {$ENDIF}
  31. {$IFNDEF WINDOWS}
  32. {$linklib c}
  33. {$ENDIF}
  34. {$ENDIF}
  35. // Operation System
  36. {$IF DEFINED(WIN32) or DEFINED(WIN64) or DEFINED(WINDOWS)}
  37. {$DEFINE GLB_WIN}
  38. {$ELSEIF DEFINED(LINUX)}
  39. {$DEFINE GLB_LINUX}
  40. {$IFEND}
  41. // OpenGL ES
  42. {$IF DEFINED(OPENGL_ES_EXT)} {$DEFINE OPENGL_ES_1_1} {$IFEND}
  43. {$IF DEFINED(OPENGL_ES_3_0)} {$DEFINE OPENGL_ES_2_0} {$IFEND}
  44. {$IF DEFINED(OPENGL_ES_2_0)} {$DEFINE OPENGL_ES_1_1} {$IFEND}
  45. {$IF DEFINED(OPENGL_ES_1_1)} {$DEFINE OPENGL_ES} {$IFEND}
  46. // checking define combinations
  47. //SDL Image
  48. {$IFDEF GLB_SDL_IMAGE}
  49. {$IFNDEF GLB_SDL}
  50. {$MESSAGE warn 'SDL_image won''t work without SDL. SDL will be activated.'}
  51. {$DEFINE GLB_SDL}
  52. {$ENDIF}
  53. {$IFDEF GLB_LAZ_PNG}
  54. {$MESSAGE warn 'The Lazarus TPortableNetworkGraphics will be ignored because you are using SDL_image.'}
  55. {$undef GLB_LAZ_PNG}
  56. {$ENDIF}
  57. {$IFDEF GLB_PNGIMAGE}
  58. {$MESSAGE warn 'The unit pngimage will be ignored because you are using SDL_image.'}
  59. {$undef GLB_PNGIMAGE}
  60. {$ENDIF}
  61. {$IFDEF GLB_LAZ_JPEG}
  62. {$MESSAGE warn 'The Lazarus TJPEGImage will be ignored because you are using SDL_image.'}
  63. {$undef GLB_LAZ_JPEG}
  64. {$ENDIF}
  65. {$IFDEF GLB_DELPHI_JPEG}
  66. {$MESSAGE warn 'The unit JPEG will be ignored because you are using SDL_image.'}
  67. {$undef GLB_DELPHI_JPEG}
  68. {$ENDIF}
  69. {$IFDEF GLB_LIB_PNG}
  70. {$MESSAGE warn 'The library libPNG will be ignored because you are using SDL_image.'}
  71. {$undef GLB_LIB_PNG}
  72. {$ENDIF}
  73. {$IFDEF GLB_LIB_JPEG}
  74. {$MESSAGE warn 'The library libJPEG will be ignored because you are using SDL_image.'}
  75. {$undef GLB_LIB_JPEG}
  76. {$ENDIF}
  77. {$DEFINE GLB_SUPPORT_PNG_READ}
  78. {$DEFINE GLB_SUPPORT_JPEG_READ}
  79. {$ENDIF}
  80. // Lazarus TPortableNetworkGraphic
  81. {$IFDEF GLB_LAZ_PNG}
  82. {$IFNDEF GLB_LAZARUS}
  83. {$MESSAGE warn 'Lazarus TPortableNetworkGraphic won''t work without Lazarus. Lazarus will be activated.'}
  84. {$DEFINE GLB_LAZARUS}
  85. {$ENDIF}
  86. {$IFDEF GLB_PNGIMAGE}
  87. {$MESSAGE warn 'The pngimage will be ignored if you are using Lazarus TPortableNetworkGraphic.'}
  88. {$undef GLB_PNGIMAGE}
  89. {$ENDIF}
  90. {$IFDEF GLB_LIB_PNG}
  91. {$MESSAGE warn 'The library libPNG will be ignored if you are using Lazarus TPortableNetworkGraphic.'}
  92. {$undef GLB_LIB_PNG}
  93. {$ENDIF}
  94. {$DEFINE GLB_SUPPORT_PNG_READ}
  95. {$DEFINE GLB_SUPPORT_PNG_WRITE}
  96. {$ENDIF}
  97. // PNG Image
  98. {$IFDEF GLB_PNGIMAGE}
  99. {$IFDEF GLB_LIB_PNG}
  100. {$MESSAGE warn 'The library libPNG will be ignored if you are using pngimage.'}
  101. {$undef GLB_LIB_PNG}
  102. {$ENDIF}
  103. {$DEFINE GLB_SUPPORT_PNG_READ}
  104. {$DEFINE GLB_SUPPORT_PNG_WRITE}
  105. {$ENDIF}
  106. // libPNG
  107. {$IFDEF GLB_LIB_PNG}
  108. {$DEFINE GLB_SUPPORT_PNG_READ}
  109. {$DEFINE GLB_SUPPORT_PNG_WRITE}
  110. {$ENDIF}
  111. // Lazarus TJPEGImage
  112. {$IFDEF GLB_LAZ_JPEG}
  113. {$IFNDEF GLB_LAZARUS}
  114. {$MESSAGE warn 'Lazarus TJPEGImage won''t work without Lazarus. Lazarus will be activated.'}
  115. {$DEFINE GLB_LAZARUS}
  116. {$ENDIF}
  117. {$IFDEF GLB_DELPHI_JPEG}
  118. {$MESSAGE warn 'The Delphi JPEGImage will be ignored if you are using the Lazarus TJPEGImage.'}
  119. {$undef GLB_DELPHI_JPEG}
  120. {$ENDIF}
  121. {$IFDEF GLB_LIB_JPEG}
  122. {$MESSAGE warn 'The library libJPEG will be ignored if you are using the Lazarus TJPEGImage.'}
  123. {$undef GLB_LIB_JPEG}
  124. {$ENDIF}
  125. {$DEFINE GLB_SUPPORT_JPEG_READ}
  126. {$DEFINE GLB_SUPPORT_JPEG_WRITE}
  127. {$ENDIF}
  128. // JPEG Image
  129. {$IFDEF GLB_DELPHI_JPEG}
  130. {$IFDEF GLB_LIB_JPEG}
  131. {$MESSAGE warn 'The library libJPEG will be ignored if you are using the unit JPEG.'}
  132. {$undef GLB_LIB_JPEG}
  133. {$ENDIF}
  134. {$DEFINE GLB_SUPPORT_JPEG_READ}
  135. {$DEFINE GLB_SUPPORT_JPEG_WRITE}
  136. {$ENDIF}
  137. // libJPEG
  138. {$IFDEF GLB_LIB_JPEG}
  139. {$DEFINE GLB_SUPPORT_JPEG_READ}
  140. {$DEFINE GLB_SUPPORT_JPEG_WRITE}
  141. {$ENDIF}
  142. // general options
  143. {$EXTENDEDSYNTAX ON}
  144. {$LONGSTRINGS ON}
  145. {$ALIGN ON}
  146. {$IFNDEF FPC}
  147. {$OPTIMIZATION ON}
  148. {$ENDIF}
  149. interface
  150. uses
  151. {$IFDEF OPENGL_ES} dglOpenGLES,
  152. {$ELSE} dglOpenGL, {$ENDIF}
  153. {$IF DEFINED(GLB_WIN) AND
  154. DEFINED(GLB_DELPHI)} windows, {$IFEND}
  155. {$IFDEF GLB_SDL} SDL, {$ENDIF}
  156. {$IFDEF GLB_LAZARUS} IntfGraphics, GraphType, Graphics, {$ENDIF}
  157. {$IFDEF GLB_DELPHI} Dialogs, Graphics, Types, {$ENDIF}
  158. {$IFDEF GLB_SDL_IMAGE} SDL_image, {$ENDIF}
  159. {$IFDEF GLB_PNGIMAGE} pngimage, {$ENDIF}
  160. {$IFDEF GLB_LIB_PNG} libPNG, {$ENDIF}
  161. {$IFDEF GLB_DELPHI_JPEG} JPEG, {$ENDIF}
  162. {$IFDEF GLB_LIB_JPEG} libJPEG, {$ENDIF}
  163. Classes, SysUtils;
  164. type
  165. {$IFNDEF fpc}
  166. QWord = System.UInt64;
  167. PQWord = ^QWord;
  168. PtrInt = Longint;
  169. PtrUInt = DWord;
  170. {$ENDIF}
  171. { type that describes the format of the data stored in a texture.
  172. the name of formats is composed of the following constituents:
  173. - multiple channels:
  174. - channel (e.g. R, G, B, A or Alpha, Luminance or X (reserved))
  175. - width of the chanel in bit (4, 8, 16, ...)
  176. - data type (e.g. ub, us, ui)
  177. - number of elements of data types }
  178. TglBitmapFormat = (
  179. tfEmpty = 0,
  180. tfAlpha4ub1, //< 1 x unsigned byte
  181. tfAlpha8ub1, //< 1 x unsigned byte
  182. tfAlpha16us1, //< 1 x unsigned short
  183. tfLuminance4ub1, //< 1 x unsigned byte
  184. tfLuminance8ub1, //< 1 x unsigned byte
  185. tfLuminance16us1, //< 1 x unsigned short
  186. tfLuminance4Alpha4ub2, //< 1 x unsigned byte (lum), 1 x unsigned byte (alpha)
  187. tfLuminance6Alpha2ub2, //< 1 x unsigned byte (lum), 1 x unsigned byte (alpha)
  188. tfLuminance8Alpha8ub2, //< 1 x unsigned byte (lum), 1 x unsigned byte (alpha)
  189. tfLuminance12Alpha4us2, //< 1 x unsigned short (lum), 1 x unsigned short (alpha)
  190. tfLuminance16Alpha16us2, //< 1 x unsigned short (lum), 1 x unsigned short (alpha)
  191. tfR3G3B2ub1, //< 1 x unsigned byte (3bit red, 3bit green, 2bit blue)
  192. tfRGBX4us1, //< 1 x unsigned short (4bit red, 4bit green, 4bit blue, 4bit reserverd)
  193. tfXRGB4us1, //< 1 x unsigned short (4bit reserved, 4bit red, 4bit green, 4bit blue)
  194. tfR5G6B5us1, //< 1 x unsigned short (5bit red, 6bit green, 5bit blue)
  195. tfRGB5X1us1, //< 1 x unsigned short (5bit red, 5bit green, 5bit blue, 1bit reserved)
  196. tfX1RGB5us1, //< 1 x unsigned short (1bit reserved, 5bit red, 5bit green, 5bit blue)
  197. tfRGB8ub3, //< 1 x unsigned byte (red), 1 x unsigned byte (green), 1 x unsigned byte (blue)
  198. tfRGBX8ui1, //< 1 x unsigned int (8bit red, 8bit green, 8bit blue, 8bit reserved)
  199. tfXRGB8ui1, //< 1 x unsigned int (8bit reserved, 8bit red, 8bit green, 8bit blue)
  200. tfRGB10X2ui1, //< 1 x unsigned int (10bit red, 10bit green, 10bit blue, 2bit reserved)
  201. tfX2RGB10ui1, //< 1 x unsigned int (2bit reserved, 10bit red, 10bit green, 10bit blue)
  202. tfRGB16us3, //< 1 x unsigned short (red), 1 x unsigned short (green), 1 x unsigned short (blue)
  203. tfRGBA4us1, //< 1 x unsigned short (4bit red, 4bit green, 4bit blue, 4bit alpha)
  204. tfARGB4us1, //< 1 x unsigned short (4bit alpha, 4bit red, 4bit green, 4bit blue)
  205. tfRGB5A1us1, //< 1 x unsigned short (5bit red, 5bit green, 5bit blue, 1bit alpha)
  206. tfA1RGB5us1, //< 1 x unsigned short (1bit alpha, 5bit red, 5bit green, 5bit blue)
  207. tfRGBA8ub4, //< 1 x unsigned byte (red), 1 x unsigned byte (green), 1 x unsigned byte (blue), 1 x unsigned byte (alpha)
  208. tfRGBA8ui1, //< 1 x unsigned int (8bit red, 8bit green, 8bit blue, 8 bit alpha)
  209. tfARGB8ui1, //< 1 x unsigned int (8 bit alpha, 8bit red, 8bit green, 8bit blue)
  210. tfRGB10A2ui1, //< 1 x unsigned int (10bit red, 10bit green, 10bit blue, 2bit alpha)
  211. tfA2RGB10ui1, //< 1 x unsigned int (2bit alpha, 10bit red, 10bit green, 10bit blue)
  212. tfRGBA16us4, //< 1 x unsigned short (red), 1 x unsigned short (green), 1 x unsigned short (blue), 1 x unsigned short (alpha)
  213. tfBGRX4us1, //< 1 x unsigned short (4bit blue, 4bit green, 4bit red, 4bit reserved)
  214. tfXBGR4us1, //< 1 x unsigned short (4bit reserved, 4bit blue, 4bit green, 4bit red)
  215. tfB5G6R5us1, //< 1 x unsigned short (5bit blue, 6bit green, 5bit red)
  216. tfBGR5X1us1, //< 1 x unsigned short (5bit blue, 5bit green, 5bit red, 1bit reserved)
  217. tfX1BGR5us1, //< 1 x unsigned short (1bit reserved, 5bit blue, 5bit green, 5bit red)
  218. tfBGR8ub3, //< 1 x unsigned byte (blue), 1 x unsigned byte (green), 1 x unsigned byte (red)
  219. tfBGRX8ui1, //< 1 x unsigned int (8bit blue, 8bit green, 8bit red, 8bit reserved)
  220. tfXBGR8ui1, //< 1 x unsigned int (8bit reserved, 8bit blue, 8bit green, 8bit red)
  221. tfBGR10X2ui1, //< 1 x unsigned int (10bit blue, 10bit green, 10bit red, 2bit reserved)
  222. tfX2BGR10ui1, //< 1 x unsigned int (2bit reserved, 10bit blue, 10bit green, 10bit red)
  223. tfBGR16us3, //< 1 x unsigned short (blue), 1 x unsigned short (green), 1 x unsigned short (red)
  224. tfBGRA4us1, //< 1 x unsigned short (4bit blue, 4bit green, 4bit red, 4bit alpha)
  225. tfABGR4us1, //< 1 x unsigned short (4bit alpha, 4bit blue, 4bit green, 4bit red)
  226. tfBGR5A1us1, //< 1 x unsigned short (5bit blue, 5bit green, 5bit red, 1bit alpha)
  227. tfA1BGR5us1, //< 1 x unsigned short (1bit alpha, 5bit blue, 5bit green, 5bit red)
  228. tfBGRA8ub4, //< 1 x unsigned byte (blue), 1 x unsigned byte (green), 1 x unsigned byte (red), 1 x unsigned byte (alpha)
  229. tfBGRA8ui1, //< 1 x unsigned int (8bit blue, 8bit green, 8bit red, 8bit alpha)
  230. tfABGR8ui1, //< 1 x unsigned int (8bit alpha, 8bit blue, 8bit green, 8bit red)
  231. tfBGR10A2ui1, //< 1 x unsigned int (10bit blue, 10bit green, 10bit red, 2bit alpha)
  232. tfA2BGR10ui1, //< 1 x unsigned int (2bit alpha, 10bit blue, 10bit green, 10bit red)
  233. tfBGRA16us4, //< 1 x unsigned short (blue), 1 x unsigned short (green), 1 x unsigned short (red), 1 x unsigned short (alpha)
  234. tfDepth16us1, //< 1 x unsigned short (depth)
  235. tfDepth24ui1, //< 1 x unsigned int (depth)
  236. tfDepth32ui1, //< 1 x unsigned int (depth)
  237. tfS3tcDtx1RGBA,
  238. tfS3tcDtx3RGBA,
  239. tfS3tcDtx5RGBA
  240. );
  241. { type to define suitable file formats }
  242. TglBitmapFileType = (
  243. {$IFDEF GLB_SUPPORT_PNG_WRITE} ftPNG, {$ENDIF} //< Portable Network Graphic file (PNG)
  244. {$IFDEF GLB_SUPPORT_JPEG_WRITE}ftJPEG, {$ENDIF} //< JPEG file
  245. ftDDS, //< Direct Draw Surface file (DDS)
  246. ftTGA, //< Targa Image File (TGA)
  247. ftBMP, //< Windows Bitmap File (BMP)
  248. ftRAW); //< glBitmap RAW file format
  249. TglBitmapFileTypes = set of TglBitmapFileType;
  250. { possible mipmap types }
  251. TglBitmapMipMap = (
  252. mmNone, //< no mipmaps
  253. mmMipmap, //< normal mipmaps
  254. mmMipmapGlu); //< mipmaps generated with glu functions
  255. { possible normal map functions }
  256. TglBitmapNormalMapFunc = (
  257. nm4Samples,
  258. nmSobel,
  259. nm3x3,
  260. nm5x5);
  261. ////////////////////////////////////////////////////////////////////////////////////////////////////
  262. EglBitmap = class(Exception); //< glBitmap exception
  263. EglBitmapNotSupported = class(Exception); //< exception for not supported functions
  264. EglBitmapSizeToLarge = class(EglBitmap); //< exception for to large textures
  265. EglBitmapNonPowerOfTwo = class(EglBitmap); //< exception for non power of two textures
  266. EglBitmapUnsupportedFormat = class(EglBitmap) //< exception for unsupporetd formats
  267. public
  268. constructor Create(const aFormat: TglBitmapFormat); overload;
  269. constructor Create(const aMsg: String; const aFormat: TglBitmapFormat); overload;
  270. end;
  271. ////////////////////////////////////////////////////////////////////////////////////////////////////
  272. { record that stores 4 unsigned integer values }
  273. TglBitmapRec4ui = packed record
  274. case Integer of
  275. 0: (r, g, b, a: Cardinal);
  276. 1: (arr: array[0..3] of Cardinal);
  277. end;
  278. { record that stores 4 unsigned byte values }
  279. TglBitmapRec4ub = packed record
  280. case Integer of
  281. 0: (r, g, b, a: Byte);
  282. 1: (arr: array[0..3] of Byte);
  283. end;
  284. { record that stores 4 unsigned long integer values }
  285. TglBitmapRec4ul = packed record
  286. case Integer of
  287. 0: (r, g, b, a: QWord);
  288. 1: (arr: array[0..3] of QWord);
  289. end;
  290. { structure to store pixel data in }
  291. TglBitmapPixelData = packed record
  292. Data: TglBitmapRec4ui; //< color data for each color channel
  293. Range: TglBitmapRec4ui; //< maximal color value for each channel
  294. Format: TglBitmapFormat; //< format of the pixel
  295. end;
  296. PglBitmapPixelData = ^TglBitmapPixelData;
  297. TglBitmapSizeFields = set of (ffX, ffY);
  298. TglBitmapSize = packed record
  299. Fields: TglBitmapSizeFields;
  300. X: Word;
  301. Y: Word;
  302. end;
  303. TglBitmapPixelPosition = TglBitmapSize;
  304. { describes the properties of a given texture data format }
  305. TglBitmapFormatDescriptor = class(TObject)
  306. private
  307. // cached properties
  308. fBytesPerPixel: Single; //< number of bytes for each pixel
  309. fChannelCount: Integer; //< number of color channels
  310. fMask: TglBitmapRec4ul; //< bitmask for each color channel
  311. fRange: TglBitmapRec4ui; //< maximal value of each color channel
  312. { @return @true if the format has a red color channel, @false otherwise }
  313. function GetHasRed: Boolean;
  314. { @return @true if the format has a green color channel, @false otherwise }
  315. function GetHasGreen: Boolean;
  316. { @return @true if the format has a blue color channel, @false otherwise }
  317. function GetHasBlue: Boolean;
  318. { @return @true if the format has a alpha color channel, @false otherwise }
  319. function GetHasAlpha: Boolean;
  320. { @return @true if the format has any color color channel, @false otherwise }
  321. function GetHasColor: Boolean;
  322. { @return @true if the format is a grayscale format, @false otherwise }
  323. function GetIsGrayscale: Boolean;
  324. { @return @true if the format is supported by OpenGL, @false otherwise }
  325. function GetHasOpenGLSupport: Boolean;
  326. protected
  327. fFormat: TglBitmapFormat; //< format this descriptor belongs to
  328. fWithAlpha: TglBitmapFormat; //< suitable format with alpha channel
  329. fWithoutAlpha: TglBitmapFormat; //< suitable format without alpha channel
  330. fOpenGLFormat: TglBitmapFormat; //< suitable format that is supported by OpenGL
  331. fRGBInverted: TglBitmapFormat; //< suitable format with inverted RGB channels
  332. fUncompressed: TglBitmapFormat; //< suitable format with uncompressed data
  333. fBitsPerPixel: Integer; //< number of bits per pixel
  334. fIsCompressed: Boolean; //< @true if the format is compressed, @false otherwise
  335. fPrecision: TglBitmapRec4ub; //< number of bits for each color channel
  336. fShift: TglBitmapRec4ub; //< bit offset for each color channel
  337. fglFormat: GLenum; //< OpenGL format enum (e.g. GL_RGB)
  338. fglInternalFormat: GLenum; //< OpenGL internal format enum (e.g. GL_RGB8)
  339. fglDataFormat: GLenum; //< OpenGL data format enum (e.g. GL_UNSIGNED_BYTE)
  340. { set values for this format descriptor }
  341. procedure SetValues; virtual;
  342. { calculate cached values }
  343. procedure CalcValues;
  344. public
  345. property Format: TglBitmapFormat read fFormat; //< format this descriptor belongs to
  346. property ChannelCount: Integer read fChannelCount; //< number of color channels
  347. property IsCompressed: Boolean read fIsCompressed; //< @true if the format is compressed, @false otherwise
  348. property BitsPerPixel: Integer read fBitsPerPixel; //< number of bytes per pixel
  349. property BytesPerPixel: Single read fBytesPerPixel; //< number of bits per pixel
  350. property Precision: TglBitmapRec4ub read fPrecision; //< number of bits for each color channel
  351. property Shift: TglBitmapRec4ub read fShift; //< bit offset for each color channel
  352. property Range: TglBitmapRec4ui read fRange; //< maximal value of each color channel
  353. property Mask: TglBitmapRec4ul read fMask; //< bitmask for each color channel
  354. property RGBInverted: TglBitmapFormat read fRGBInverted; //< suitable format with inverted RGB channels
  355. property WithAlpha: TglBitmapFormat read fWithAlpha; //< suitable format with alpha channel
  356. property WithoutAlpha: TglBitmapFormat read fWithoutAlpha; //< suitable format without alpha channel
  357. property OpenGLFormat: TglBitmapFormat read fOpenGLFormat; //< suitable format that is supported by OpenGL
  358. property Uncompressed: TglBitmapFormat read fUncompressed; //< suitable format with uncompressed data
  359. property glFormat: GLenum read fglFormat; //< OpenGL format enum (e.g. GL_RGB)
  360. property glInternalFormat: GLenum read fglInternalFormat; //< OpenGL internal format enum (e.g. GL_RGB8)
  361. property glDataFormat: GLenum read fglDataFormat; //< OpenGL data format enum (e.g. GL_UNSIGNED_BYTE)
  362. property HasRed: Boolean read GetHasRed; //< @true if the format has a red color channel, @false otherwise
  363. property HasGreen: Boolean read GetHasGreen; //< @true if the format has a green color channel, @false otherwise
  364. property HasBlue: Boolean read GetHasBlue; //< @true if the format has a blue color channel, @false otherwise
  365. property HasAlpha: Boolean read GetHasAlpha; //< @true if the format has a alpha color channel, @false otherwise
  366. property HasColor: Boolean read GetHasColor; //< @true if the format has any color color channel, @false otherwise
  367. property IsGrayscale: Boolean read GetIsGrayscale; //< @true if the format is a grayscale format, @false otherwise
  368. property HasOpenGLSupport: Boolean read GetHasOpenGLSupport; //< @true if the format is supported by OpenGL, @false otherwise
  369. function GetSize(const aSize: TglBitmapSize): Integer; overload; virtual;
  370. function GetSize(const aWidth, aHeight: Integer): Integer; overload; virtual;
  371. { constructor }
  372. constructor Create;
  373. public
  374. { get the format descriptor by a given OpenGL internal format
  375. @param aInternalFormat OpenGL internal format to get format descriptor for
  376. @returns suitable format descriptor or tfEmpty-Descriptor }
  377. class function GetByFormat(const aInternalFormat: GLenum): TglBitmapFormatDescriptor; overload;
  378. { get the format descriptor by the given format
  379. @param aFormat format to get descriptor for
  380. @return suitable format descriptor or tfEmpty-Descriptor }
  381. class function GetByFormat(const aFormat: TglBitmapFormat): TglBitmapFormatDescriptor; overload;
  382. end;
  383. ////////////////////////////////////////////////////////////////////////////////////////////////////
  384. TglBitmapData = class;
  385. { structure to store data for converting in }
  386. TglBitmapFunctionRec = record
  387. Sender: TglBitmapData; //< texture object that stores the data to convert
  388. Size: TglBitmapSize; //< size of the texture
  389. Position: TglBitmapPixelPosition; //< position of the currently pixel
  390. Source: TglBitmapPixelData; //< pixel data of the current pixel
  391. Dest: TglBitmapPixelData; //< new data of the pixel (must be filled in)
  392. Args: Pointer; //< user defined args that was passed to the convert function
  393. end;
  394. { callback to use for converting texture data }
  395. TglBitmapFunction = procedure(var FuncRec: TglBitmapFunctionRec);
  396. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  397. { class to store texture data in. used to load, save and
  398. manipulate data before assigned to texture object
  399. all operations on a data object can be done from a background thread }
  400. TglBitmapData = class
  401. private { fields }
  402. fData: PByte; //< texture data
  403. fDimension: TglBitmapSize; //< pixel size of the data
  404. fFormat: TglBitmapFormat; //< format the texture data is stored in
  405. fFilename: String; //< file the data was load from
  406. fScanlines: array of PByte; //< pointer to begin of each line
  407. fHasScanlines: Boolean; //< @true if scanlines are initialized, @false otherwise
  408. private { getter / setter }
  409. { @returns the format descriptor suitable to the texture data format }
  410. function GetFormatDescriptor: TglBitmapFormatDescriptor;
  411. { @returns the width of the texture data (in pixel) or -1 if no data is set }
  412. function GetWidth: Integer;
  413. { @returns the height of the texture data (in pixel) or -1 if no data is set }
  414. function GetHeight: Integer;
  415. { get scanline at index aIndex
  416. @returns Pointer to start of line or @nil }
  417. function GetScanlines(const aIndex: Integer): PByte;
  418. { set new value for the data format. only possible if new format has the same pixel size.
  419. if you want to convert the texture data, see ConvertTo function }
  420. procedure SetFormat(const aValue: TglBitmapFormat);
  421. private { internal misc }
  422. { splits a resource identifier into the resource and it's type
  423. @param aResource resource identifier to split and store name in
  424. @param aResType type of the resource }
  425. procedure PrepareResType(var aResource: String; var aResType: PChar);
  426. { updates scanlines array }
  427. procedure UpdateScanlines;
  428. private { internal load and save }
  429. {$IFDEF GLB_SUPPORT_PNG_READ}
  430. { try to load a PNG from a stream
  431. @param aStream stream to load PNG from
  432. @returns @true on success, @false otherwise }
  433. function LoadPNG(const aStream: TStream): Boolean; virtual;
  434. {$ENDIF}
  435. {$ifdef GLB_SUPPORT_PNG_WRITE}
  436. { save texture data as PNG to stream
  437. @param aStream stream to save data to}
  438. procedure SavePNG(const aStream: TStream); virtual;
  439. {$ENDIF}
  440. {$IFDEF GLB_SUPPORT_JPEG_READ}
  441. { try to load a JPEG from a stream
  442. @param aStream stream to load JPEG from
  443. @returns @true on success, @false otherwise }
  444. function LoadJPEG(const aStream: TStream): Boolean; virtual;
  445. {$ENDIF}
  446. {$IFDEF GLB_SUPPORT_JPEG_WRITE}
  447. { save texture data as JPEG to stream
  448. @param aStream stream to save data to}
  449. procedure SaveJPEG(const aStream: TStream); virtual;
  450. {$ENDIF}
  451. { try to load a RAW image from a stream
  452. @param aStream stream to load RAW image from
  453. @returns @true on success, @false otherwise }
  454. function LoadRAW(const aStream: TStream): Boolean;
  455. { save texture data as RAW image to stream
  456. @param aStream stream to save data to}
  457. procedure SaveRAW(const aStream: TStream);
  458. { try to load a BMP from a stream
  459. @param aStream stream to load BMP from
  460. @returns @true on success, @false otherwise }
  461. function LoadBMP(const aStream: TStream): Boolean;
  462. { save texture data as BMP to stream
  463. @param aStream stream to save data to}
  464. procedure SaveBMP(const aStream: TStream);
  465. { try to load a TGA from a stream
  466. @param aStream stream to load TGA from
  467. @returns @true on success, @false otherwise }
  468. function LoadTGA(const aStream: TStream): Boolean;
  469. { save texture data as TGA to stream
  470. @param aStream stream to save data to}
  471. procedure SaveTGA(const aStream: TStream);
  472. { try to load a DDS from a stream
  473. @param aStream stream to load DDS from
  474. @returns @true on success, @false otherwise }
  475. function LoadDDS(const aStream: TStream): Boolean;
  476. { save texture data as DDS to stream
  477. @param aStream stream to save data to}
  478. procedure SaveDDS(const aStream: TStream);
  479. public { properties }
  480. property Data: PByte read fData; //< texture data (be carefull with this!)
  481. property Dimension: TglBitmapSize read fDimension; //< size of the texture data (in pixel)
  482. property Filename: String read fFilename; //< file the data was loaded from
  483. property Width: Integer read GetWidth; //< width of the texture data (in pixel)
  484. property Height: Integer read GetHeight; //< height of the texture data (in pixel)
  485. property Format: TglBitmapFormat read fFormat write SetFormat; //< format the texture data is stored in
  486. property Scanlines[const aIndex: Integer]: PByte read GetScanlines; //< pointer to begin of line at given index or @nil
  487. property FormatDescriptor: TglBitmapFormatDescriptor read GetFormatDescriptor; //< descriptor object that describes the format of the stored data
  488. public { flip }
  489. { flip texture horizontal
  490. @returns @true in success, @false otherwise }
  491. function FlipHorz: Boolean; virtual;
  492. { flip texture vertical
  493. @returns @true in success, @false otherwise }
  494. function FlipVert: Boolean; virtual;
  495. public { load }
  496. { load a texture from a file
  497. @param aFilename file to load texuture from }
  498. procedure LoadFromFile(const aFilename: String);
  499. { load a texture from a stream
  500. @param aStream stream to load texture from }
  501. procedure LoadFromStream(const aStream: TStream); virtual;
  502. { use a function to generate texture data
  503. @param aSize size of the texture
  504. @param aFormat format of the texture data
  505. @param aFunc callback to use for generation
  506. @param aArgs user defined paramaters (use at will) }
  507. procedure LoadFromFunc(const aSize: TglBitmapSize; const aFormat: TglBitmapFormat; const aFunc: TglBitmapFunction; const aArgs: Pointer = nil);
  508. { load a texture from a resource
  509. @param aInstance resource handle
  510. @param aResource resource indentifier
  511. @param aResType resource type (if known) }
  512. procedure LoadFromResource(const aInstance: Cardinal; aResource: String; aResType: PChar = nil);
  513. { load a texture from a resource id
  514. @param aInstance resource handle
  515. @param aResource resource ID
  516. @param aResType resource type }
  517. procedure LoadFromResourceID(const aInstance: Cardinal; const aResourceID: Integer; const aResType: PChar);
  518. public { save }
  519. { save texture data to a file
  520. @param aFilename filename to store texture in
  521. @param aFileType file type to store data into }
  522. procedure SaveToFile(const aFilename: String; const aFileType: TglBitmapFileType);
  523. { save texture data to a stream
  524. @param aFilename filename to store texture in
  525. @param aFileType file type to store data into }
  526. procedure SaveToStream(const aStream: TStream; const aFileType: TglBitmapFileType); virtual;
  527. public { convert }
  528. { convert texture data using a user defined callback
  529. @param aFunc callback to use for converting
  530. @param aCreateTemp create a temporary buffer to use for converting
  531. @param aArgs user defined paramters (use at will)
  532. @returns @true if converting was successful, @false otherwise }
  533. function Convert(const aFunc: TglBitmapFunction; const aCreateTemp: Boolean; const aArgs: Pointer = nil): Boolean; overload;
  534. { convert texture data using a user defined callback
  535. @param aSource glBitmap to read data from
  536. @param aFunc callback to use for converting
  537. @param aCreateTemp create a temporary buffer to use for converting
  538. @param aFormat format of the new data
  539. @param aArgs user defined paramters (use at will)
  540. @returns @true if converting was successful, @false otherwise }
  541. function Convert(const aSource: TglBitmapData; const aFunc: TglBitmapFunction; aCreateTemp: Boolean;
  542. const aFormat: TglBitmapFormat; const aArgs: Pointer = nil): Boolean; overload;
  543. { convert texture data using a specific format
  544. @param aFormat new format of texture data
  545. @returns @true if converting was successful, @false otherwise }
  546. function ConvertTo(const aFormat: TglBitmapFormat): Boolean; virtual;
  547. {$IFDEF GLB_SDL}
  548. public { SDL }
  549. { assign texture data to SDL surface
  550. @param aSurface SDL surface to write data to
  551. @returns @true on success, @false otherwise }
  552. function AssignToSurface(out aSurface: PSDL_Surface): Boolean;
  553. { assign texture data from SDL surface
  554. @param aSurface SDL surface to read data from
  555. @returns @true on success, @false otherwise }
  556. function AssignFromSurface(const aSurface: PSDL_Surface): Boolean;
  557. { assign alpha channel data to SDL surface
  558. @param aSurface SDL surface to write alpha channel data to
  559. @returns @true on success, @false otherwise }
  560. function AssignAlphaToSurface(out aSurface: PSDL_Surface): Boolean;
  561. { assign alpha channel data from SDL surface
  562. @param aSurface SDL surface to read data from
  563. @param aFunc callback to use for converting
  564. @param aArgs user defined parameters (use at will)
  565. @returns @true on success, @false otherwise }
  566. function AddAlphaFromSurface(const aSurface: PSDL_Surface; const aFunc: TglBitmapFunction = nil; const aArgs: Pointer = nil): Boolean;
  567. {$ENDIF}
  568. {$IFDEF GLB_DELPHI}
  569. public { Delphi }
  570. { assign texture data to TBitmap object
  571. @param aBitmap TBitmap to write data to
  572. @returns @true on success, @false otherwise }
  573. function AssignToBitmap(const aBitmap: TBitmap): Boolean;
  574. { assign texture data from TBitmap object
  575. @param aBitmap TBitmap to read data from
  576. @returns @true on success, @false otherwise }
  577. function AssignFromBitmap(const aBitmap: TBitmap): Boolean;
  578. { assign alpha channel data to TBitmap object
  579. @param aBitmap TBitmap to write data to
  580. @returns @true on success, @false otherwise }
  581. function AssignAlphaToBitmap(const aBitmap: TBitmap): Boolean;
  582. { assign alpha channel data from TBitmap object
  583. @param aBitmap TBitmap to read data from
  584. @param aFunc callback to use for converting
  585. @param aArgs user defined parameters (use at will)
  586. @returns @true on success, @false otherwise }
  587. function AddAlphaFromBitmap(const aBitmap: TBitmap; const aFunc: TglBitmapFunction = nil; const aArgs: Pointer = nil): Boolean;
  588. {$ENDIF}
  589. {$IFDEF GLB_LAZARUS}
  590. public { Lazarus }
  591. { assign texture data to TLazIntfImage object
  592. @param aImage TLazIntfImage to write data to
  593. @returns @true on success, @false otherwise }
  594. function AssignToLazIntfImage(const aImage: TLazIntfImage): Boolean;
  595. { assign texture data from TLazIntfImage object
  596. @param aImage TLazIntfImage to read data from
  597. @returns @true on success, @false otherwise }
  598. function AssignFromLazIntfImage(const aImage: TLazIntfImage): Boolean;
  599. { assign alpha channel data to TLazIntfImage object
  600. @param aImage TLazIntfImage to write data to
  601. @returns @true on success, @false otherwise }
  602. function AssignAlphaToLazIntfImage(const aImage: TLazIntfImage): Boolean;
  603. { assign alpha channel data from TLazIntfImage object
  604. @param aImage TLazIntfImage to read data from
  605. @param aFunc callback to use for converting
  606. @param aArgs user defined parameters (use at will)
  607. @returns @true on success, @false otherwise }
  608. function AddAlphaFromLazIntfImage(const aImage: TLazIntfImage; const aFunc: TglBitmapFunction = nil; const aArgs: Pointer = nil): Boolean;
  609. {$ENDIF}
  610. public { Alpha }
  611. { load alpha channel data from resource
  612. @param aInstance resource handle
  613. @param aResource resource ID
  614. @param aResType resource type
  615. @param aFunc callback to use for converting
  616. @param aArgs user defined parameters (use at will)
  617. @returns @true on success, @false otherwise }
  618. function AddAlphaFromResource(const aInstance: Cardinal; aResource: String; aResType: PChar = nil; const aFunc: TglBitmapFunction = nil; const aArgs: Pointer = nil): Boolean;
  619. { load alpha channel data from resource ID
  620. @param aInstance resource handle
  621. @param aResourceID resource ID
  622. @param aResType resource type
  623. @param aFunc callback to use for converting
  624. @param aArgs user defined parameters (use at will)
  625. @returns @true on success, @false otherwise }
  626. function AddAlphaFromResourceID(const aInstance: Cardinal; const aResourceID: Integer; const aResType: PChar; const aFunc: TglBitmapFunction = nil; const aArgs: Pointer = nil): Boolean;
  627. { add alpha channel data from function
  628. @param aFunc callback to get data from
  629. @param aArgs user defined parameters (use at will)
  630. @returns @true on success, @false otherwise }
  631. function AddAlphaFromFunc(const aFunc: TglBitmapFunction; const aArgs: Pointer = nil): Boolean; virtual;
  632. { add alpha channel data from file (macro for: new glBitmap, LoadFromFile, AddAlphaFromGlBitmap)
  633. @param aFilename file to load alpha channel data from
  634. @param aFunc callback to use for converting
  635. @param aArgs SetFormat user defined parameters (use at will)
  636. @returns @true on success, @false otherwise }
  637. function AddAlphaFromFile(const aFileName: String; const aFunc: TglBitmapFunction = nil; const aArgs: Pointer = nil): Boolean;
  638. { add alpha channel data from stream (macro for: new glBitmap, LoadFromStream, AddAlphaFromGlBitmap)
  639. @param aStream stream to load alpha channel data from
  640. @param aFunc callback to use for converting
  641. @param aArgs user defined parameters (use at will)
  642. @returns @true on success, @false otherwise }
  643. function AddAlphaFromStream(const aStream: TStream; const aFunc: TglBitmapFunction = nil; const aArgs: Pointer = nil): Boolean;
  644. { add alpha channel data from existing glBitmap object
  645. @param aBitmap TglBitmap to copy alpha channel data from
  646. @param aFunc callback to use for converting
  647. @param aArgs user defined parameters (use at will)
  648. @returns @true on success, @false otherwise }
  649. function AddAlphaFromDataObj(const aDataObj: TglBitmapData; aFunc: TglBitmapFunction; const aArgs: Pointer): Boolean;
  650. { add alpha to pixel if the pixels color is greter than the given color value
  651. @param aRed red threshold (0-255)
  652. @param aGreen green threshold (0-255)
  653. @param aBlue blue threshold (0-255)
  654. @param aDeviatation accepted deviatation (0-255)
  655. @returns @true on success, @false otherwise }
  656. function AddAlphaFromColorKey(const aRed, aGreen, aBlue: Byte; const aDeviation: Byte = 0): Boolean;
  657. { add alpha to pixel if the pixels color is greter than the given color value
  658. @param aRed red threshold (0-Range.r)
  659. @param aGreen green threshold (0-Range.g)
  660. @param aBlue blue threshold (0-Range.b)
  661. @param aDeviatation accepted deviatation (0-max(Range.rgb))
  662. @returns @true on success, @false otherwise }
  663. function AddAlphaFromColorKeyRange(const aRed, aGreen, aBlue: Cardinal; const aDeviation: Cardinal = 0): Boolean;
  664. { add alpha to pixel if the pixels color is greter than the given color value
  665. @param aRed red threshold (0.0-1.0)
  666. @param aGreen green threshold (0.0-1.0)
  667. @param aBlue blue threshold (0.0-1.0)
  668. @param aDeviatation accepted deviatation (0.0-1.0)
  669. @returns @true on success, @false otherwise }
  670. function AddAlphaFromColorKeyFloat(const aRed, aGreen, aBlue: Single; const aDeviation: Single = 0): Boolean;
  671. { add a constand alpha value to all pixels
  672. @param aAlpha alpha value to add (0-255)
  673. @returns @true on success, @false otherwise }
  674. function AddAlphaFromValue(const aAlpha: Byte): Boolean;
  675. { add a constand alpha value to all pixels
  676. @param aAlpha alpha value to add (0-max(Range.rgb))
  677. @returns @true on success, @false otherwise }
  678. function AddAlphaFromValueRange(const aAlpha: Cardinal): Boolean;
  679. { add a constand alpha value to all pixels
  680. @param aAlpha alpha value to add (0.0-1.0)
  681. @returns @true on success, @false otherwise }
  682. function AddAlphaFromValueFloat(const aAlpha: Single): Boolean;
  683. { remove alpha channel
  684. @returns @true on success, @false otherwise }
  685. function RemoveAlpha: Boolean; virtual;
  686. public { fill }
  687. { fill complete texture with one color
  688. @param aRed red color for border (0-255)
  689. @param aGreen green color for border (0-255)
  690. @param aBlue blue color for border (0-255)
  691. @param aAlpha alpha color for border (0-255) }
  692. procedure FillWithColor(const aRed, aGreen, aBlue: Byte; const aAlpha: Byte = 255);
  693. { fill complete texture with one color
  694. @param aRed red color for border (0-Range.r)
  695. @param aGreen green color for border (0-Range.g)
  696. @param aBlue blue color for border (0-Range.b)
  697. @param aAlpha alpha color for border (0-Range.a) }
  698. procedure FillWithColorRange(const aRed, aGreen, aBlue: Cardinal; const aAlpha: Cardinal = $FFFFFFFF);
  699. { fill complete texture with one color
  700. @param aRed red color for border (0.0-1.0)
  701. @param aGreen green color for border (0.0-1.0)
  702. @param aBlue blue color for border (0.0-1.0)
  703. @param aAlpha alpha color for border (0.0-1.0) }
  704. procedure FillWithColorFloat(const aRed, aGreen, aBlue: Single; const aAlpha: Single = 1.0);
  705. public { Misc }
  706. { set data pointer of texture data
  707. @param aData pointer to new texture data
  708. @param aFormat format of the data stored at aData
  709. @param aWidth width of the texture data
  710. @param aHeight height of the texture data }
  711. procedure SetData(const aData: PByte; const aFormat: TglBitmapFormat;
  712. const aWidth: Integer = -1; const aHeight: Integer = -1); virtual;
  713. { create a clone of the current object
  714. @returns clone of this object}
  715. function Clone: TglBitmapData;
  716. { invert color data (bitwise not)
  717. @param aRed invert red channel
  718. @param aGreen invert green channel
  719. @param aBlue invert blue channel
  720. @param aAlpha invert alpha channel }
  721. procedure Invert(const aRed, aGreen, aBlue, aAlpha: Boolean);
  722. { create normal map from texture data
  723. @param aFunc normal map function to generate normalmap with
  724. @param aScale scale of the normale stored in the normal map
  725. @param aUseAlpha generate normalmap from alpha channel data (if present) }
  726. procedure GenerateNormalMap(const aFunc: TglBitmapNormalMapFunc = nm3x3;
  727. const aScale: Single = 2; const aUseAlpha: Boolean = false);
  728. public { constructor }
  729. { constructor - creates a texutre data object }
  730. constructor Create; overload;
  731. { constructor - creates a texture data object and loads it from a file
  732. @param aFilename file to load texture from }
  733. constructor Create(const aFileName: String); overload;
  734. { constructor - creates a texture data object and loads it from a stream
  735. @param aStream stream to load texture from }
  736. constructor Create(const aStream: TStream); overload;
  737. { constructor - creates a texture data object with the given size, format and data
  738. @param aSize size of the texture
  739. @param aFormat format of the given data
  740. @param aData texture data - be carefull: the data will now be managed by the texture data object }
  741. constructor Create(const aSize: TglBitmapSize; const aFormat: TglBitmapFormat; aData: PByte = nil); overload;
  742. { constructor - creates a texture data object with the given size and format and uses the given callback to create the data
  743. @param aSize size of the texture
  744. @param aFormat format of the given data
  745. @param aFunc callback to use for generating the data
  746. @param aArgs user defined parameters (use at will) }
  747. constructor Create(const aSize: TglBitmapSize; const aFormat: TglBitmapFormat; const aFunc: TglBitmapFunction; const aArgs: Pointer = nil); overload;
  748. { constructor - creates a texture data object and loads it from a resource
  749. @param aInstance resource handle
  750. @param aResource resource indentifier
  751. @param aResType resource type (if known) }
  752. constructor Create(const aInstance: Cardinal; const aResource: String; const aResType: PChar = nil); overload;
  753. { constructor - creates a texture data object and loads it from a resource
  754. @param aInstance resource handle
  755. @param aResourceID resource ID
  756. @param aResType resource type (if known) }
  757. constructor Create(const aInstance: Cardinal; const aResourceID: Integer; const aResType: PChar); overload;
  758. { destructor }
  759. destructor Destroy; override;
  760. end;
  761. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  762. { base class for all glBitmap classes. used to manage OpenGL texture objects
  763. all operations on a bitmap object must be done from the render thread }
  764. TglBitmap = class
  765. protected
  766. fID: GLuint; //< name of the OpenGL texture object
  767. fTarget: GLuint; //< texture target (e.g. GL_TEXTURE_2D)
  768. fDeleteTextureOnFree: Boolean; //< delete OpenGL texture object when this object is destroyed
  769. // texture properties
  770. fFilterMin: GLenum; //< min filter to apply to the texture
  771. fFilterMag: GLenum; //< mag filter to apply to the texture
  772. fWrapS: GLenum; //< texture wrapping for x axis
  773. fWrapT: GLenum; //< texture wrapping for y axis
  774. fWrapR: GLenum; //< texture wrapping for z axis
  775. fAnisotropic: Integer; //< anisotropic level
  776. fBorderColor: array[0..3] of Single; //< color of the texture border
  777. {$IF NOT DEFINED(OPENGL_ES) OR DEFINED(OPENGL_ES_3_0)}
  778. //Swizzle
  779. fSwizzle: array[0..3] of GLenum; //< color channel swizzle
  780. {$IFEND}
  781. {$IFNDEF OPENGL_ES}
  782. fIsResident: GLboolean; //< @true if OpenGL texture object has data, @false otherwise
  783. {$ENDIF}
  784. fDimension: TglBitmapSize; //< size of this texture
  785. fMipMap: TglBitmapMipMap; //< mipmap type
  786. // CustomData
  787. fCustomData: Pointer; //< user defined data
  788. fCustomName: String; //< user defined name
  789. fCustomNameW: WideString; //< user defined name
  790. protected
  791. { @returns the actual width of the texture }
  792. function GetWidth: Integer; virtual;
  793. { @returns the actual height of the texture }
  794. function GetHeight: Integer; virtual;
  795. protected
  796. { set a new value for fCustomData }
  797. procedure SetCustomData(const aValue: Pointer);
  798. { set a new value for fCustomName }
  799. procedure SetCustomName(const aValue: String);
  800. { set a new value for fCustomNameW }
  801. procedure SetCustomNameW(const aValue: WideString);
  802. { set new value for fDeleteTextureOnFree }
  803. procedure SetDeleteTextureOnFree(const aValue: Boolean);
  804. { set name of OpenGL texture object }
  805. procedure SetID(const aValue: Cardinal);
  806. { set new value for fMipMap }
  807. procedure SetMipMap(const aValue: TglBitmapMipMap);
  808. { set new value for target }
  809. procedure SetTarget(const aValue: Cardinal);
  810. { set new value for fAnisotrophic }
  811. procedure SetAnisotropic(const aValue: Integer);
  812. protected
  813. { create OpenGL texture object (delete exisiting object if exists) }
  814. procedure CreateID;
  815. { setup texture parameters }
  816. procedure SetupParameters({$IFNDEF OPENGL_ES}out aBuildWithGlu: Boolean{$ENDIF});
  817. protected
  818. property Width: Integer read GetWidth; //< the actual width of the texture
  819. property Height: Integer read GetHeight; //< the actual height of the texture
  820. public
  821. property ID: Cardinal read fID write SetID; //< name of the OpenGL texture object
  822. property Target: Cardinal read fTarget write SetTarget; //< texture target (e.g. GL_TEXTURE_2D)
  823. property DeleteTextureOnFree: Boolean read fDeleteTextureOnFree write SetDeleteTextureOnFree; //< delete texture object when this object is destroyed
  824. property MipMap: TglBitmapMipMap read fMipMap write SetMipMap; //< mipmap type
  825. property Anisotropic: Integer read fAnisotropic write SetAnisotropic; //< anisotropic level
  826. property CustomData: Pointer read fCustomData write SetCustomData; //< user defined data (use at will)
  827. property CustomName: String read fCustomName write SetCustomName; //< user defined name (use at will)
  828. property CustomNameW: WideString read fCustomNameW write SetCustomNameW; //< user defined name (as WideString; use at will)
  829. property Dimension: TglBitmapSize read fDimension; //< size of the texture
  830. {$IFNDEF OPENGL_ES}
  831. property IsResident: GLboolean read fIsResident; //< @true if OpenGL texture object has data, @false otherwise
  832. {$ENDIF}
  833. { this method is called after the constructor and sets the default values of this object }
  834. procedure AfterConstruction; override;
  835. { this method is called before the destructor and does some cleanup }
  836. procedure BeforeDestruction; override;
  837. public
  838. {$IFNDEF OPENGL_ES}
  839. { set the new value for texture border color
  840. @param aRed red color for border (0.0-1.0)
  841. @param aGreen green color for border (0.0-1.0)
  842. @param aBlue blue color for border (0.0-1.0)
  843. @param aAlpha alpha color for border (0.0-1.0) }
  844. procedure SetBorderColor(const aRed, aGreen, aBlue, aAlpha: Single);
  845. {$ENDIF}
  846. public
  847. { set new texture filer
  848. @param aMin min filter
  849. @param aMag mag filter }
  850. procedure SetFilter(const aMin, aMag: GLenum);
  851. { set new texture wrapping
  852. @param S texture wrapping for x axis
  853. @param T texture wrapping for y axis
  854. @param R texture wrapping for z axis }
  855. procedure SetWrap(
  856. const S: GLenum = GL_CLAMP_TO_EDGE;
  857. const T: GLenum = GL_CLAMP_TO_EDGE;
  858. const R: GLenum = GL_CLAMP_TO_EDGE);
  859. {$IF NOT DEFINED(OPENGL_ES) OR DEFINED(OPENGL_ES_3_0)}
  860. { set new swizzle
  861. @param r swizzle for red channel
  862. @param g swizzle for green channel
  863. @param b swizzle for blue channel
  864. @param a swizzle for alpha channel }
  865. procedure SetSwizzle(const r, g, b, a: GLenum);
  866. {$IFEND}
  867. public
  868. { bind texture
  869. @param aEnableTextureUnit enable texture unit for this texture (e.g. glEnable(GL_TEXTURE_2D)) }
  870. procedure Bind({$IFNDEF OPENGL_ES}const aEnableTextureUnit: Boolean = true{$ENDIF}); virtual;
  871. { bind texture
  872. @param aDisableTextureUnit disable texture unit for this texture (e.g. glEnable(GL_TEXTURE_2D)) }
  873. procedure Unbind({$IFNDEF OPENGL_ES}const aDisableTextureUnit: Boolean = true{$ENDIF}); virtual;
  874. { upload texture data from given data object to video card
  875. @param aData texture data object that contains the actual data
  876. @param aCheckSize check size before upload and throw exception if something is wrong }
  877. procedure UploadData(const aDataObj: TglBitmapData; const aCheckSize: Boolean = true); virtual;
  878. {$IFNDEF OPENGL_ES}
  879. { download texture data from video card and store it into given data object
  880. @returns @true when download was successfull, @false otherwise }
  881. function DownloadData(const aDataObj: TglBitmapData): Boolean; virtual;
  882. {$ENDIF}
  883. public
  884. { constructor - creates an empty texture }
  885. constructor Create; overload;
  886. { constructor - creates an texture object and uploads the given data }
  887. constructor Create(const aData: TglBitmapData); overload;
  888. end;
  889. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  890. {$IF NOT DEFINED(OPENGL_ES)}
  891. { wrapper class for 1-dimensional textures (OpenGL target = GL_TEXTURE_1D
  892. all operations on a bitmap object must be done from the render thread }
  893. TglBitmap1D = class(TglBitmap)
  894. protected
  895. { upload the texture data to video card
  896. @param aDataObj texture data object that contains the actual data
  897. @param aBuildWithGlu use glu functions to build mipmaps }
  898. procedure UploadDataIntern(const aDataObj: TglBitmapData; const aBuildWithGlu: Boolean);
  899. public
  900. property Width; //< actual with of the texture
  901. { this method is called after constructor and initializes the object }
  902. procedure AfterConstruction; override;
  903. { upload texture data from given data object to video card
  904. @param aData texture data object that contains the actual data
  905. @param aCheckSize check size before upload and throw exception if something is wrong }
  906. procedure UploadData(const aDataObj: TglBitmapData; const aCheckSize: Boolean = true); override;
  907. end;
  908. {$IFEND}
  909. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  910. { wrapper class for 2-dimensional textures (OpenGL target = GL_TEXTURE_2D)
  911. all operations on a bitmap object must be done from the render thread }
  912. TglBitmap2D = class(TglBitmap)
  913. protected
  914. { upload the texture data to video card
  915. @param aDataObj texture data object that contains the actual data
  916. @param aTarget target o upload data to (e.g. GL_TEXTURE_2D)
  917. @param aBuildWithGlu use glu functions to build mipmaps }
  918. procedure UploadDataIntern(const aDataObj: TglBitmapData; const aTarget: GLenum
  919. {$IFNDEF OPENGL_ES}; const aBuildWithGlu: Boolean{$ENDIF});
  920. public
  921. property Width; //< actual width of the texture
  922. property Height; //< actual height of the texture
  923. { this method is called after constructor and initializes the object }
  924. procedure AfterConstruction; override;
  925. { upload texture data from given data object to video card
  926. @param aData texture data object that contains the actual data
  927. @param aCheckSize check size before upload and throw exception if something is wrong }
  928. procedure UploadData(const aDataObj: TglBitmapData; const aCheckSize: Boolean = true); override;
  929. public
  930. { copy a part of the frame buffer to the texture
  931. @param aTop topmost pixel to copy
  932. @param aLeft leftmost pixel to copy
  933. @param aRight rightmost pixel to copy
  934. @param aBottom bottommost pixel to copy
  935. @param aFormat format to store data in
  936. @param aDataObj texture data object to store the data in }
  937. class procedure GrabScreen(const aTop, aLeft, aRight, aBottom: Integer; const aFormat: TglBitmapFormat; const aDataObj: TglBitmapData);
  938. end;
  939. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  940. {$IF NOT DEFINED(OPENGL_ES) OR DEFINED(OPENGL_ES_2_0)}
  941. { wrapper class for cube maps (OpenGL target = GL_TEXTURE_CUBE_MAP)
  942. all operations on a bitmap object must be done from the render thread }
  943. TglBitmapCubeMap = class(TglBitmap2D)
  944. protected
  945. {$IFNDEF OPENGL_ES}
  946. fGenMode: Integer; //< generation mode for the cube map (e.g. GL_REFLECTION_MAP)
  947. {$ENDIF}
  948. public
  949. { this method is called after constructor and initializes the object }
  950. procedure AfterConstruction; override;
  951. { upload texture data from given data object to video card
  952. @param aData texture data object that contains the actual data
  953. @param aCheckSize check size before upload and throw exception if something is wrong }
  954. procedure UploadData(const aDataObj: TglBitmapData; const aCheckSize: Boolean = true); override;
  955. { upload texture data from given data object to video card
  956. @param aData texture data object that contains the actual data
  957. @param aCubeTarget cube map target to upload data to (e.g. GL_TEXTURE_CUBE_MAP_POSITIVE_X)
  958. @param aCheckSize check size before upload and throw exception if something is wrong }
  959. procedure UploadCubeMap(const aDataObj: TglBitmapData; const aCubeTarget: Cardinal; const aCheckSize: Boolean);
  960. { bind texture
  961. @param aEnableTexCoordsGen enable cube map generator
  962. @param aEnableTextureUnit enable texture unit }
  963. procedure Bind({$IFNDEF OPENGL_ES}const aEnableTexCoordsGen: Boolean = true; const aEnableTextureUnit: Boolean = true{$ENDIF}); reintroduce; virtual;
  964. { unbind texture
  965. @param aDisableTexCoordsGen disable cube map generator
  966. @param aDisableTextureUnit disable texture unit }
  967. procedure Unbind({$IFNDEF OPENGL_ES}const aDisableTexCoordsGen: Boolean = true; const aDisableTextureUnit: Boolean = true{$ENDIF}); reintroduce; virtual;
  968. end;
  969. {$IFEND}
  970. {$IF NOT DEFINED(OPENGL_ES) OR DEFINED(OPENGL_ES_2_0)}
  971. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  972. { wrapper class for cube normal maps
  973. all operations on a bitmap object must be done from the render thread }
  974. TglBitmapNormalMap = class(TglBitmapCubeMap)
  975. public
  976. { this method is called after constructor and initializes the object }
  977. procedure AfterConstruction; override;
  978. { create cube normal map from texture data and upload it to video card
  979. @param aSize size of each cube map texture
  980. @param aCheckSize check size before upload and throw exception if something is wrong }
  981. procedure GenerateNormalMap(const aSize: Integer = 32; const aCheckSize: Boolean = true);
  982. end;
  983. {$IFEND}
  984. const
  985. NULL_SIZE: TglBitmapSize = (Fields: []; X: 0; Y: 0);
  986. procedure glBitmapSetDefaultDeleteTextureOnFree(const aDeleteTextureOnFree: Boolean);
  987. procedure glBitmapSetDefaultFreeDataAfterGenTexture(const aFreeData: Boolean);
  988. procedure glBitmapSetDefaultMipmap(const aValue: TglBitmapMipMap);
  989. procedure glBitmapSetDefaultFormat(const aFormat: TglBitmapFormat);
  990. procedure glBitmapSetDefaultFilter(const aMin, aMag: Integer);
  991. procedure glBitmapSetDefaultWrap(
  992. const S: Cardinal = GL_CLAMP_TO_EDGE;
  993. const T: Cardinal = GL_CLAMP_TO_EDGE;
  994. const R: Cardinal = GL_CLAMP_TO_EDGE);
  995. {$IF NOT DEFINED(OPENGL_ES) OR DEFINED(OPENGL_ES_3_0)}
  996. procedure glBitmapSetDefaultSwizzle(const r: GLenum = GL_RED; g: GLenum = GL_GREEN; b: GLenum = GL_BLUE; a: GLenum = GL_ALPHA);
  997. {$IFEND}
  998. function glBitmapGetDefaultDeleteTextureOnFree: Boolean;
  999. function glBitmapGetDefaultFreeDataAfterGenTexture: Boolean;
  1000. function glBitmapGetDefaultMipmap: TglBitmapMipMap;
  1001. function glBitmapGetDefaultFormat: TglBitmapFormat;
  1002. procedure glBitmapGetDefaultFilter(var aMin, aMag: Cardinal);
  1003. procedure glBitmapGetDefaultTextureWrap(var S, T, R: Cardinal);
  1004. {$IF NOT DEFINED(OPENGL_ES) OR DEFINED(OPENGL_ES_3_0)}
  1005. procedure glBitmapGetDefaultSwizzle(var r, g, b, a: GLenum);
  1006. {$IFEND}
  1007. function glBitmapSize(X: Integer = -1; Y: Integer = -1): TglBitmapSize;
  1008. function glBitmapPosition(X: Integer = -1; Y: Integer = -1): TglBitmapPixelPosition;
  1009. function glBitmapRec4ub(const r, g, b, a: Byte): TglBitmapRec4ub;
  1010. function glBitmapRec4ui(const r, g, b, a: Cardinal): TglBitmapRec4ui;
  1011. function glBitmapRec4ul(const r, g, b, a: QWord): TglBitmapRec4ul;
  1012. function glBitmapRec4ubCompare(const r1, r2: TglBitmapRec4ub): Boolean;
  1013. function glBitmapRec4uiCompare(const r1, r2: TglBitmapRec4ui): Boolean;
  1014. function glBitmapCreateTestData(const aFormat: TglBitmapFormat): TglBitmapData;
  1015. {$IFDEF GLB_DELPHI}
  1016. function CreateGrayPalette: HPALETTE;
  1017. {$ENDIF}
  1018. implementation
  1019. uses
  1020. Math, syncobjs, typinfo
  1021. {$IF DEFINED(GLB_SUPPORT_JPEG_READ) AND DEFINED(GLB_LAZ_JPEG)}, FPReadJPEG{$IFEND};
  1022. var
  1023. glBitmapDefaultDeleteTextureOnFree: Boolean;
  1024. glBitmapDefaultFreeDataAfterGenTextures: Boolean;
  1025. glBitmapDefaultFormat: TglBitmapFormat;
  1026. glBitmapDefaultMipmap: TglBitmapMipMap;
  1027. glBitmapDefaultFilterMin: Cardinal;
  1028. glBitmapDefaultFilterMag: Cardinal;
  1029. glBitmapDefaultWrapS: Cardinal;
  1030. glBitmapDefaultWrapT: Cardinal;
  1031. glBitmapDefaultWrapR: Cardinal;
  1032. glDefaultSwizzle: array[0..3] of GLenum;
  1033. ////////////////////////////////////////////////////////////////////////////////////////////////////
  1034. type
  1035. TFormatDescriptor = class(TglBitmapFormatDescriptor)
  1036. public
  1037. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); virtual; abstract;
  1038. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); virtual; abstract;
  1039. function CreateMappingData: Pointer; virtual;
  1040. procedure FreeMappingData(var aMappingData: Pointer); virtual;
  1041. function IsEmpty: Boolean; virtual;
  1042. function MaskMatch(const aMask: TglBitmapRec4ul): Boolean; virtual;
  1043. procedure PreparePixel(out aPixel: TglBitmapPixelData); virtual;
  1044. constructor Create; virtual;
  1045. public
  1046. class procedure Init;
  1047. class function Get(const aFormat: TglBitmapFormat): TFormatDescriptor;
  1048. class function GetAlpha(const aFormat: TglBitmapFormat): TFormatDescriptor;
  1049. class function GetFromMask(const aMask: TglBitmapRec4ul; const aBitCount: Integer = 0): TFormatDescriptor;
  1050. class function GetFromPrecShift(const aPrec, aShift: TglBitmapRec4ub; const aBitCount: Integer): TFormatDescriptor;
  1051. class procedure Clear;
  1052. class procedure Finalize;
  1053. end;
  1054. TFormatDescriptorClass = class of TFormatDescriptor;
  1055. TfdEmpty = class(TFormatDescriptor);
  1056. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1057. TfdAlphaUB1 = class(TFormatDescriptor) //1* unsigned byte
  1058. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1059. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1060. end;
  1061. TfdLuminanceUB1 = class(TFormatDescriptor) //1* unsigned byte
  1062. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1063. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1064. end;
  1065. TfdUniversalUB1 = class(TFormatDescriptor) //1* unsigned byte
  1066. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1067. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1068. end;
  1069. TfdLuminanceAlphaUB2 = class(TfdLuminanceUB1) //2* unsigned byte
  1070. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1071. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1072. end;
  1073. TfdRGBub3 = class(TFormatDescriptor) //3* unsigned byte
  1074. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1075. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1076. end;
  1077. TfdBGRub3 = class(TFormatDescriptor) //3* unsigned byte (inverse)
  1078. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1079. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1080. end;
  1081. TfdRGBAub4 = class(TfdRGBub3) //3* unsigned byte
  1082. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1083. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1084. end;
  1085. TfdBGRAub4 = class(TfdBGRub3) //3* unsigned byte (inverse)
  1086. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1087. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1088. end;
  1089. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1090. TfdAlphaUS1 = class(TFormatDescriptor) //1* unsigned short
  1091. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1092. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1093. end;
  1094. TfdLuminanceUS1 = class(TFormatDescriptor) //1* unsigned short
  1095. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1096. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1097. end;
  1098. TfdUniversalUS1 = class(TFormatDescriptor) //1* unsigned short
  1099. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1100. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1101. end;
  1102. TfdDepthUS1 = class(TFormatDescriptor) //1* unsigned short
  1103. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1104. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1105. end;
  1106. TfdLuminanceAlphaUS2 = class(TfdLuminanceUS1) //2* unsigned short
  1107. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1108. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1109. end;
  1110. TfdRGBus3 = class(TFormatDescriptor) //3* unsigned short
  1111. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1112. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1113. end;
  1114. TfdBGRus3 = class(TFormatDescriptor) //3* unsigned short (inverse)
  1115. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1116. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1117. end;
  1118. TfdRGBAus4 = class(TfdRGBus3) //4* unsigned short
  1119. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1120. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1121. end;
  1122. TfdARGBus4 = class(TfdRGBus3) //4* unsigned short
  1123. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1124. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1125. end;
  1126. TfdBGRAus4 = class(TfdBGRus3) //4* unsigned short (inverse)
  1127. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1128. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1129. end;
  1130. TfdABGRus4 = class(TfdBGRus3) //4* unsigned short (inverse)
  1131. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1132. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1133. end;
  1134. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1135. TfdUniversalUI1 = class(TFormatDescriptor) //1* unsigned int
  1136. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1137. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1138. end;
  1139. TfdDepthUI1 = class(TFormatDescriptor) //1* unsigned int
  1140. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1141. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1142. end;
  1143. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1144. TfdAlpha4ub1 = class(TfdAlphaUB1)
  1145. procedure SetValues; override;
  1146. end;
  1147. TfdAlpha8ub1 = class(TfdAlphaUB1)
  1148. procedure SetValues; override;
  1149. end;
  1150. TfdAlpha16us1 = class(TfdAlphaUS1)
  1151. procedure SetValues; override;
  1152. end;
  1153. TfdLuminance4ub1 = class(TfdLuminanceUB1)
  1154. procedure SetValues; override;
  1155. end;
  1156. TfdLuminance8ub1 = class(TfdLuminanceUB1)
  1157. procedure SetValues; override;
  1158. end;
  1159. TfdLuminance16us1 = class(TfdLuminanceUS1)
  1160. procedure SetValues; override;
  1161. end;
  1162. TfdLuminance4Alpha4ub2 = class(TfdLuminanceAlphaUB2)
  1163. procedure SetValues; override;
  1164. end;
  1165. TfdLuminance6Alpha2ub2 = class(TfdLuminanceAlphaUB2)
  1166. procedure SetValues; override;
  1167. end;
  1168. TfdLuminance8Alpha8ub2 = class(TfdLuminanceAlphaUB2)
  1169. procedure SetValues; override;
  1170. end;
  1171. TfdLuminance12Alpha4us2 = class(TfdLuminanceAlphaUS2)
  1172. procedure SetValues; override;
  1173. end;
  1174. TfdLuminance16Alpha16us2 = class(TfdLuminanceAlphaUS2)
  1175. procedure SetValues; override;
  1176. end;
  1177. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1178. TfdR3G3B2ub1 = class(TfdUniversalUB1)
  1179. procedure SetValues; override;
  1180. end;
  1181. TfdRGBX4us1 = class(TfdUniversalUS1)
  1182. procedure SetValues; override;
  1183. end;
  1184. TfdXRGB4us1 = class(TfdUniversalUS1)
  1185. procedure SetValues; override;
  1186. end;
  1187. TfdR5G6B5us1 = class(TfdUniversalUS1)
  1188. procedure SetValues; override;
  1189. end;
  1190. TfdRGB5X1us1 = class(TfdUniversalUS1)
  1191. procedure SetValues; override;
  1192. end;
  1193. TfdX1RGB5us1 = class(TfdUniversalUS1)
  1194. procedure SetValues; override;
  1195. end;
  1196. TfdRGB8ub3 = class(TfdRGBub3)
  1197. procedure SetValues; override;
  1198. end;
  1199. TfdRGBX8ui1 = class(TfdUniversalUI1)
  1200. procedure SetValues; override;
  1201. end;
  1202. TfdXRGB8ui1 = class(TfdUniversalUI1)
  1203. procedure SetValues; override;
  1204. end;
  1205. TfdRGB10X2ui1 = class(TfdUniversalUI1)
  1206. procedure SetValues; override;
  1207. end;
  1208. TfdX2RGB10ui1 = class(TfdUniversalUI1)
  1209. procedure SetValues; override;
  1210. end;
  1211. TfdRGB16us3 = class(TfdRGBus3)
  1212. procedure SetValues; override;
  1213. end;
  1214. TfdRGBA4us1 = class(TfdUniversalUS1)
  1215. procedure SetValues; override;
  1216. end;
  1217. TfdARGB4us1 = class(TfdUniversalUS1)
  1218. procedure SetValues; override;
  1219. end;
  1220. TfdRGB5A1us1 = class(TfdUniversalUS1)
  1221. procedure SetValues; override;
  1222. end;
  1223. TfdA1RGB5us1 = class(TfdUniversalUS1)
  1224. procedure SetValues; override;
  1225. end;
  1226. TfdRGBA8ui1 = class(TfdUniversalUI1)
  1227. procedure SetValues; override;
  1228. end;
  1229. TfdARGB8ui1 = class(TfdUniversalUI1)
  1230. procedure SetValues; override;
  1231. end;
  1232. TfdRGBA8ub4 = class(TfdRGBAub4)
  1233. procedure SetValues; override;
  1234. end;
  1235. TfdRGB10A2ui1 = class(TfdUniversalUI1)
  1236. procedure SetValues; override;
  1237. end;
  1238. TfdA2RGB10ui1 = class(TfdUniversalUI1)
  1239. procedure SetValues; override;
  1240. end;
  1241. TfdRGBA16us4 = class(TfdRGBAus4)
  1242. procedure SetValues; override;
  1243. end;
  1244. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1245. TfdBGRX4us1 = class(TfdUniversalUS1)
  1246. procedure SetValues; override;
  1247. end;
  1248. TfdXBGR4us1 = class(TfdUniversalUS1)
  1249. procedure SetValues; override;
  1250. end;
  1251. TfdB5G6R5us1 = class(TfdUniversalUS1)
  1252. procedure SetValues; override;
  1253. end;
  1254. TfdBGR5X1us1 = class(TfdUniversalUS1)
  1255. procedure SetValues; override;
  1256. end;
  1257. TfdX1BGR5us1 = class(TfdUniversalUS1)
  1258. procedure SetValues; override;
  1259. end;
  1260. TfdBGR8ub3 = class(TfdBGRub3)
  1261. procedure SetValues; override;
  1262. end;
  1263. TfdBGRX8ui1 = class(TfdUniversalUI1)
  1264. procedure SetValues; override;
  1265. end;
  1266. TfdXBGR8ui1 = class(TfdUniversalUI1)
  1267. procedure SetValues; override;
  1268. end;
  1269. TfdBGR10X2ui1 = class(TfdUniversalUI1)
  1270. procedure SetValues; override;
  1271. end;
  1272. TfdX2BGR10ui1 = class(TfdUniversalUI1)
  1273. procedure SetValues; override;
  1274. end;
  1275. TfdBGR16us3 = class(TfdBGRus3)
  1276. procedure SetValues; override;
  1277. end;
  1278. TfdBGRA4us1 = class(TfdUniversalUS1)
  1279. procedure SetValues; override;
  1280. end;
  1281. TfdABGR4us1 = class(TfdUniversalUS1)
  1282. procedure SetValues; override;
  1283. end;
  1284. TfdBGR5A1us1 = class(TfdUniversalUS1)
  1285. procedure SetValues; override;
  1286. end;
  1287. TfdA1BGR5us1 = class(TfdUniversalUS1)
  1288. procedure SetValues; override;
  1289. end;
  1290. TfdBGRA8ui1 = class(TfdUniversalUI1)
  1291. procedure SetValues; override;
  1292. end;
  1293. TfdABGR8ui1 = class(TfdUniversalUI1)
  1294. procedure SetValues; override;
  1295. end;
  1296. TfdBGRA8ub4 = class(TfdBGRAub4)
  1297. procedure SetValues; override;
  1298. end;
  1299. TfdBGR10A2ui1 = class(TfdUniversalUI1)
  1300. procedure SetValues; override;
  1301. end;
  1302. TfdA2BGR10ui1 = class(TfdUniversalUI1)
  1303. procedure SetValues; override;
  1304. end;
  1305. TfdBGRA16us4 = class(TfdBGRAus4)
  1306. procedure SetValues; override;
  1307. end;
  1308. TfdDepth16us1 = class(TfdDepthUS1)
  1309. procedure SetValues; override;
  1310. end;
  1311. TfdDepth24ui1 = class(TfdDepthUI1)
  1312. procedure SetValues; override;
  1313. end;
  1314. TfdDepth32ui1 = class(TfdDepthUI1)
  1315. procedure SetValues; override;
  1316. end;
  1317. TfdS3tcDtx1RGBA = class(TFormatDescriptor)
  1318. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1319. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1320. procedure SetValues; override;
  1321. end;
  1322. TfdS3tcDtx3RGBA = class(TFormatDescriptor)
  1323. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1324. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1325. procedure SetValues; override;
  1326. end;
  1327. TfdS3tcDtx5RGBA = 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. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1333. TbmpBitfieldFormat = class(TFormatDescriptor)
  1334. public
  1335. procedure SetCustomValues(const aBPP: Integer; aMask: TglBitmapRec4ul); overload;
  1336. procedure SetCustomValues(const aBBP: Integer; const aPrec, aShift: TglBitmapRec4ub); overload;
  1337. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1338. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1339. end;
  1340. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1341. TbmpColorTableEnty = packed record
  1342. b, g, r, a: Byte;
  1343. end;
  1344. TbmpColorTable = array of TbmpColorTableEnty;
  1345. TbmpColorTableFormat = class(TFormatDescriptor)
  1346. private
  1347. fColorTable: TbmpColorTable;
  1348. protected
  1349. procedure SetValues; override;
  1350. public
  1351. property ColorTable: TbmpColorTable read fColorTable write fColorTable;
  1352. procedure SetCustomValues(const aFormat: TglBitmapFormat; const aBPP: Integer; const aPrec, aShift: TglBitmapRec4ub); overload;
  1353. procedure CalcValues;
  1354. procedure CreateColorTable;
  1355. function CreateMappingData: Pointer; override;
  1356. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1357. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1358. destructor Destroy; override;
  1359. end;
  1360. const
  1361. LUMINANCE_WEIGHT_R = 0.30;
  1362. LUMINANCE_WEIGHT_G = 0.59;
  1363. LUMINANCE_WEIGHT_B = 0.11;
  1364. ALPHA_WEIGHT_R = 0.30;
  1365. ALPHA_WEIGHT_G = 0.59;
  1366. ALPHA_WEIGHT_B = 0.11;
  1367. DEPTH_WEIGHT_R = 0.333333333;
  1368. DEPTH_WEIGHT_G = 0.333333333;
  1369. DEPTH_WEIGHT_B = 0.333333333;
  1370. FORMAT_DESCRIPTOR_CLASSES: array[TglBitmapFormat] of TFormatDescriptorClass = (
  1371. TfdEmpty,
  1372. TfdAlpha4ub1,
  1373. TfdAlpha8ub1,
  1374. TfdAlpha16us1,
  1375. TfdLuminance4ub1,
  1376. TfdLuminance8ub1,
  1377. TfdLuminance16us1,
  1378. TfdLuminance4Alpha4ub2,
  1379. TfdLuminance6Alpha2ub2,
  1380. TfdLuminance8Alpha8ub2,
  1381. TfdLuminance12Alpha4us2,
  1382. TfdLuminance16Alpha16us2,
  1383. TfdR3G3B2ub1,
  1384. TfdRGBX4us1,
  1385. TfdXRGB4us1,
  1386. TfdR5G6B5us1,
  1387. TfdRGB5X1us1,
  1388. TfdX1RGB5us1,
  1389. TfdRGB8ub3,
  1390. TfdRGBX8ui1,
  1391. TfdXRGB8ui1,
  1392. TfdRGB10X2ui1,
  1393. TfdX2RGB10ui1,
  1394. TfdRGB16us3,
  1395. TfdRGBA4us1,
  1396. TfdARGB4us1,
  1397. TfdRGB5A1us1,
  1398. TfdA1RGB5us1,
  1399. TfdRGBA8ub4,
  1400. TfdRGBA8ui1,
  1401. TfdARGB8ui1,
  1402. TfdRGB10A2ui1,
  1403. TfdA2RGB10ui1,
  1404. TfdRGBA16us4,
  1405. TfdBGRX4us1,
  1406. TfdXBGR4us1,
  1407. TfdB5G6R5us1,
  1408. TfdBGR5X1us1,
  1409. TfdX1BGR5us1,
  1410. TfdBGR8ub3,
  1411. TfdBGRX8ui1,
  1412. TfdXBGR8ui1,
  1413. TfdBGR10X2ui1,
  1414. TfdX2BGR10ui1,
  1415. TfdBGR16us3,
  1416. TfdBGRA4us1,
  1417. TfdABGR4us1,
  1418. TfdBGR5A1us1,
  1419. TfdA1BGR5us1,
  1420. TfdBGRA8ub4,
  1421. TfdBGRA8ui1,
  1422. TfdABGR8ui1,
  1423. TfdBGR10A2ui1,
  1424. TfdA2BGR10ui1,
  1425. TfdBGRA16us4,
  1426. TfdDepth16us1,
  1427. TfdDepth24ui1,
  1428. TfdDepth32ui1,
  1429. TfdS3tcDtx1RGBA,
  1430. TfdS3tcDtx3RGBA,
  1431. TfdS3tcDtx5RGBA
  1432. );
  1433. var
  1434. FormatDescriptorCS: TCriticalSection;
  1435. FormatDescriptors: array[TglBitmapFormat] of TFormatDescriptor;
  1436. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1437. constructor EglBitmapUnsupportedFormat.Create(const aFormat: TglBitmapFormat);
  1438. begin
  1439. inherited Create('unsupported format: ' + GetEnumName(TypeInfo(TglBitmapFormat), Integer(aFormat)));
  1440. end;
  1441. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1442. constructor EglBitmapUnsupportedFormat.Create(const aMsg: String; const aFormat: TglBitmapFormat);
  1443. begin
  1444. inherited Create(aMsg + GetEnumName(TypeInfo(TglBitmapFormat), Integer(aFormat)));
  1445. end;
  1446. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1447. function glBitmapSize(X: Integer; Y: Integer): TglBitmapSize;
  1448. begin
  1449. result.Fields := [];
  1450. if (X >= 0) then
  1451. result.Fields := result.Fields + [ffX];
  1452. if (Y >= 0) then
  1453. result.Fields := result.Fields + [ffY];
  1454. result.X := Max(0, X);
  1455. result.Y := Max(0, Y);
  1456. end;
  1457. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1458. function glBitmapPosition(X: Integer; Y: Integer): TglBitmapPixelPosition;
  1459. begin
  1460. result := glBitmapSize(X, Y);
  1461. end;
  1462. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1463. function glBitmapRec4ub(const r, g, b, a: Byte): TglBitmapRec4ub;
  1464. begin
  1465. result.r := r;
  1466. result.g := g;
  1467. result.b := b;
  1468. result.a := a;
  1469. end;
  1470. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1471. function glBitmapRec4ui(const r, g, b, a: Cardinal): TglBitmapRec4ui;
  1472. begin
  1473. result.r := r;
  1474. result.g := g;
  1475. result.b := b;
  1476. result.a := a;
  1477. end;
  1478. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1479. function glBitmapRec4ul(const r, g, b, a: QWord): TglBitmapRec4ul;
  1480. begin
  1481. result.r := r;
  1482. result.g := g;
  1483. result.b := b;
  1484. result.a := a;
  1485. end;
  1486. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1487. function glBitmapRec4ubCompare(const r1, r2: TglBitmapRec4ub): Boolean;
  1488. var
  1489. i: Integer;
  1490. begin
  1491. result := false;
  1492. for i := 0 to high(r1.arr) do
  1493. if (r1.arr[i] <> r2.arr[i]) then
  1494. exit;
  1495. result := true;
  1496. end;
  1497. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1498. function glBitmapRec4uiCompare(const r1, r2: TglBitmapRec4ui): Boolean;
  1499. var
  1500. i: Integer;
  1501. begin
  1502. result := false;
  1503. for i := 0 to high(r1.arr) do
  1504. if (r1.arr[i] <> r2.arr[i]) then
  1505. exit;
  1506. result := true;
  1507. end;
  1508. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1509. function glBitmapCreateTestData(const aFormat: TglBitmapFormat): TglBitmapData;
  1510. var
  1511. desc: TFormatDescriptor;
  1512. p, tmp: PByte;
  1513. x, y, i: Integer;
  1514. md: Pointer;
  1515. px: TglBitmapPixelData;
  1516. begin
  1517. result := nil;
  1518. desc := TFormatDescriptor.Get(aFormat);
  1519. if (desc.IsCompressed) or (desc.glFormat = 0) then
  1520. exit;
  1521. p := GetMemory(ceil(25 * desc.BytesPerPixel)); // 5 x 5 pixel
  1522. md := desc.CreateMappingData;
  1523. try
  1524. tmp := p;
  1525. desc.PreparePixel(px);
  1526. for y := 0 to 4 do
  1527. for x := 0 to 4 do begin
  1528. px.Data := glBitmapRec4ui(0, 0, 0, 0);
  1529. for i := 0 to 3 do begin
  1530. if ((y < 3) and (y = i)) or
  1531. ((y = 3) and (i < 3)) or
  1532. ((y = 4) and (i = 3))
  1533. then
  1534. px.Data.arr[i] := Trunc(px.Range.arr[i] / 4 * x)
  1535. else if ((y < 4) and (i = 3)) or
  1536. ((y = 4) and (i < 3))
  1537. then
  1538. px.Data.arr[i] := px.Range.arr[i]
  1539. else
  1540. px.Data.arr[i] := 0; //px.Range.arr[i];
  1541. end;
  1542. desc.Map(px, tmp, md);
  1543. end;
  1544. finally
  1545. desc.FreeMappingData(md);
  1546. end;
  1547. result := TglBitmapData.Create(glBitmapPosition(5, 5), aFormat, p);
  1548. end;
  1549. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1550. function glBitmapShiftRec(const r, g, b, a: Byte): TglBitmapRec4ub;
  1551. begin
  1552. result.r := r;
  1553. result.g := g;
  1554. result.b := b;
  1555. result.a := a;
  1556. end;
  1557. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1558. function FormatGetSupportedFiles(const aFormat: TglBitmapFormat): TglBitmapFileTypes;
  1559. begin
  1560. result := [];
  1561. if (aFormat in [
  1562. //8bpp
  1563. tfAlpha4ub1, tfAlpha8ub1,
  1564. tfLuminance4ub1, tfLuminance8ub1, tfR3G3B2ub1,
  1565. //16bpp
  1566. tfLuminance4Alpha4ub2, tfLuminance6Alpha2ub2, tfLuminance8Alpha8ub2,
  1567. tfRGBX4us1, tfXRGB4us1, tfRGB5X1us1, tfX1RGB5us1, tfR5G6B5us1, tfRGB5A1us1, tfA1RGB5us1, tfRGBA4us1, tfARGB4us1,
  1568. tfBGRX4us1, tfXBGR4us1, tfBGR5X1us1, tfX1BGR5us1, tfB5G6R5us1, tfBGR5A1us1, tfA1BGR5us1, tfBGRA4us1, tfABGR4us1,
  1569. //24bpp
  1570. tfBGR8ub3, tfRGB8ub3,
  1571. //32bpp
  1572. tfRGBX8ui1, tfXRGB8ui1, tfRGB10X2ui1, tfX2RGB10ui1, tfRGBA8ui1, tfARGB8ui1, tfRGBA8ub4, tfRGB10A2ui1, tfA2RGB10ui1,
  1573. tfBGRX8ui1, tfXBGR8ui1, tfBGR10X2ui1, tfX2BGR10ui1, tfBGRA8ui1, tfABGR8ui1, tfBGRA8ub4, tfBGR10A2ui1, tfA2BGR10ui1])
  1574. then
  1575. result := result + [ ftBMP ];
  1576. if (aFormat in [
  1577. //8bbp
  1578. tfAlpha4ub1, tfAlpha8ub1, tfLuminance4ub1, tfLuminance8ub1,
  1579. //16bbp
  1580. tfAlpha16us1, tfLuminance16us1,
  1581. tfLuminance4Alpha4ub2, tfLuminance6Alpha2ub2, tfLuminance8Alpha8ub2,
  1582. tfX1RGB5us1, tfARGB4us1, tfA1RGB5us1, tfDepth16us1,
  1583. //24bbp
  1584. tfBGR8ub3,
  1585. //32bbp
  1586. tfX2RGB10ui1, tfARGB8ui1, tfBGRA8ub4, tfA2RGB10ui1,
  1587. tfDepth24ui1, tfDepth32ui1])
  1588. then
  1589. result := result + [ftTGA];
  1590. if not (aFormat in [tfEmpty, tfRGB16us3, tfBGR16us3]) then
  1591. result := result + [ftDDS];
  1592. {$IFDEF GLB_SUPPORT_PNG_WRITE}
  1593. if aFormat in [
  1594. tfAlpha8ub1, tfLuminance8ub1, tfLuminance8Alpha8ub2,
  1595. tfRGB8ub3, tfRGBA8ui1,
  1596. tfBGR8ub3, tfBGRA8ui1] then
  1597. result := result + [ftPNG];
  1598. {$ENDIF}
  1599. {$IFDEF GLB_SUPPORT_JPEG_WRITE}
  1600. if aFormat in [tfAlpha8ub1, tfLuminance8ub1, tfRGB8ub3, tfBGR8ub3] then
  1601. result := result + [ftJPEG];
  1602. {$ENDIF}
  1603. end;
  1604. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1605. function IsPowerOfTwo(aNumber: Integer): Boolean;
  1606. begin
  1607. while (aNumber and 1) = 0 do
  1608. aNumber := aNumber shr 1;
  1609. result := aNumber = 1;
  1610. end;
  1611. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1612. function GetTopMostBit(aBitSet: QWord): Integer;
  1613. begin
  1614. result := 0;
  1615. while aBitSet > 0 do begin
  1616. inc(result);
  1617. aBitSet := aBitSet shr 1;
  1618. end;
  1619. end;
  1620. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1621. function CountSetBits(aBitSet: QWord): Integer;
  1622. begin
  1623. result := 0;
  1624. while aBitSet > 0 do begin
  1625. if (aBitSet and 1) = 1 then
  1626. inc(result);
  1627. aBitSet := aBitSet shr 1;
  1628. end;
  1629. end;
  1630. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1631. function LuminanceWeight(const aPixel: TglBitmapPixelData): Cardinal;
  1632. begin
  1633. result := Trunc(
  1634. LUMINANCE_WEIGHT_R * aPixel.Data.r +
  1635. LUMINANCE_WEIGHT_G * aPixel.Data.g +
  1636. LUMINANCE_WEIGHT_B * aPixel.Data.b);
  1637. end;
  1638. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1639. function DepthWeight(const aPixel: TglBitmapPixelData): Cardinal;
  1640. begin
  1641. result := Trunc(
  1642. DEPTH_WEIGHT_R * aPixel.Data.r +
  1643. DEPTH_WEIGHT_G * aPixel.Data.g +
  1644. DEPTH_WEIGHT_B * aPixel.Data.b);
  1645. end;
  1646. {$IFDEF GLB_SDL_IMAGE}
  1647. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1648. // SDL Image Helper /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1649. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1650. function glBitmapRWseek(context: PSDL_RWops; offset: Integer; whence: Integer): Integer; cdecl;
  1651. begin
  1652. result := TStream(context^.unknown.data1).Seek(offset, whence);
  1653. end;
  1654. function glBitmapRWread(context: PSDL_RWops; Ptr: Pointer; size: Integer; maxnum : Integer): Integer; cdecl;
  1655. begin
  1656. result := TStream(context^.unknown.data1).Read(Ptr^, size * maxnum);
  1657. end;
  1658. function glBitmapRWwrite(context: PSDL_RWops; Ptr: Pointer; size: Integer; num: Integer): Integer; cdecl;
  1659. begin
  1660. result := TStream(context^.unknown.data1).Write(Ptr^, size * num);
  1661. end;
  1662. function glBitmapRWclose(context: PSDL_RWops): Integer; cdecl;
  1663. begin
  1664. result := 0;
  1665. end;
  1666. function glBitmapCreateRWops(Stream: TStream): PSDL_RWops;
  1667. begin
  1668. result := SDL_AllocRW;
  1669. if result = nil then
  1670. raise EglBitmap.Create('glBitmapCreateRWops - SDL_AllocRW failed.');
  1671. result^.seek := glBitmapRWseek;
  1672. result^.read := glBitmapRWread;
  1673. result^.write := glBitmapRWwrite;
  1674. result^.close := glBitmapRWclose;
  1675. result^.unknown.data1 := Stream;
  1676. end;
  1677. {$ENDIF}
  1678. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1679. procedure glBitmapSetDefaultDeleteTextureOnFree(const aDeleteTextureOnFree: Boolean);
  1680. begin
  1681. glBitmapDefaultDeleteTextureOnFree := aDeleteTextureOnFree;
  1682. end;
  1683. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1684. procedure glBitmapSetDefaultFreeDataAfterGenTexture(const aFreeData: Boolean);
  1685. begin
  1686. glBitmapDefaultFreeDataAfterGenTextures := aFreeData;
  1687. end;
  1688. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1689. procedure glBitmapSetDefaultMipmap(const aValue: TglBitmapMipMap);
  1690. begin
  1691. glBitmapDefaultMipmap := aValue;
  1692. end;
  1693. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1694. procedure glBitmapSetDefaultFormat(const aFormat: TglBitmapFormat);
  1695. begin
  1696. glBitmapDefaultFormat := aFormat;
  1697. end;
  1698. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1699. procedure glBitmapSetDefaultFilter(const aMin, aMag: Integer);
  1700. begin
  1701. glBitmapDefaultFilterMin := aMin;
  1702. glBitmapDefaultFilterMag := aMag;
  1703. end;
  1704. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1705. procedure glBitmapSetDefaultWrap(const S: Cardinal = GL_CLAMP_TO_EDGE; const T: Cardinal = GL_CLAMP_TO_EDGE; const R: Cardinal = GL_CLAMP_TO_EDGE);
  1706. begin
  1707. glBitmapDefaultWrapS := S;
  1708. glBitmapDefaultWrapT := T;
  1709. glBitmapDefaultWrapR := R;
  1710. end;
  1711. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1712. {$IF NOT DEFINED(OPENGL_ES) OR DEFINED(OPENGL_ES_3_0)}
  1713. procedure glBitmapSetDefaultSwizzle(const r: GLenum = GL_RED; g: GLenum = GL_GREEN; b: GLenum = GL_BLUE; a: GLenum = GL_ALPHA);
  1714. begin
  1715. glDefaultSwizzle[0] := r;
  1716. glDefaultSwizzle[1] := g;
  1717. glDefaultSwizzle[2] := b;
  1718. glDefaultSwizzle[3] := a;
  1719. end;
  1720. {$IFEND}
  1721. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1722. function glBitmapGetDefaultDeleteTextureOnFree: Boolean;
  1723. begin
  1724. result := glBitmapDefaultDeleteTextureOnFree;
  1725. end;
  1726. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1727. function glBitmapGetDefaultFreeDataAfterGenTexture: Boolean;
  1728. begin
  1729. result := glBitmapDefaultFreeDataAfterGenTextures;
  1730. end;
  1731. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1732. function glBitmapGetDefaultMipmap: TglBitmapMipMap;
  1733. begin
  1734. result := glBitmapDefaultMipmap;
  1735. end;
  1736. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1737. function glBitmapGetDefaultFormat: TglBitmapFormat;
  1738. begin
  1739. result := glBitmapDefaultFormat;
  1740. end;
  1741. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1742. procedure glBitmapGetDefaultFilter(var aMin, aMag: Cardinal);
  1743. begin
  1744. aMin := glBitmapDefaultFilterMin;
  1745. aMag := glBitmapDefaultFilterMag;
  1746. end;
  1747. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1748. procedure glBitmapGetDefaultTextureWrap(var S, T, R: Cardinal);
  1749. begin
  1750. S := glBitmapDefaultWrapS;
  1751. T := glBitmapDefaultWrapT;
  1752. R := glBitmapDefaultWrapR;
  1753. end;
  1754. {$IF NOT DEFINED(OPENGL_ES) OR DEFINED(OPENGL_ES_3_0)}
  1755. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1756. procedure glBitmapGetDefaultSwizzle(var r, g, b, a: GLenum);
  1757. begin
  1758. r := glDefaultSwizzle[0];
  1759. g := glDefaultSwizzle[1];
  1760. b := glDefaultSwizzle[2];
  1761. a := glDefaultSwizzle[3];
  1762. end;
  1763. {$IFEND}
  1764. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1765. //TFormatDescriptor///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1766. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1767. function TFormatDescriptor.CreateMappingData: Pointer;
  1768. begin
  1769. result := nil;
  1770. end;
  1771. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1772. procedure TFormatDescriptor.FreeMappingData(var aMappingData: Pointer);
  1773. begin
  1774. //DUMMY
  1775. end;
  1776. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1777. function TFormatDescriptor.IsEmpty: Boolean;
  1778. begin
  1779. result := (fFormat = tfEmpty);
  1780. end;
  1781. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1782. function TFormatDescriptor.MaskMatch(const aMask: TglBitmapRec4ul): Boolean;
  1783. var
  1784. i: Integer;
  1785. m: TglBitmapRec4ul;
  1786. begin
  1787. result := false;
  1788. if (aMask.r = 0) and (aMask.g = 0) and (aMask.b = 0) and (aMask.a = 0) then
  1789. raise EglBitmap.Create('FormatCheckFormat - All Masks are 0');
  1790. m := Mask;
  1791. for i := 0 to 3 do
  1792. if (aMask.arr[i] <> m.arr[i]) then
  1793. exit;
  1794. result := true;
  1795. end;
  1796. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1797. procedure TFormatDescriptor.PreparePixel(out aPixel: TglBitmapPixelData);
  1798. begin
  1799. FillChar(aPixel{%H-}, SizeOf(aPixel), 0);
  1800. aPixel.Data := Range;
  1801. aPixel.Format := fFormat;
  1802. aPixel.Range := Range;
  1803. end;
  1804. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1805. constructor TFormatDescriptor.Create;
  1806. begin
  1807. inherited Create;
  1808. end;
  1809. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1810. //TfdAlpha_UB1////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1811. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1812. procedure TfdAlphaUB1.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  1813. begin
  1814. aData^ := aPixel.Data.a;
  1815. inc(aData);
  1816. end;
  1817. procedure TfdAlphaUB1.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  1818. begin
  1819. aPixel.Data.r := 0;
  1820. aPixel.Data.g := 0;
  1821. aPixel.Data.b := 0;
  1822. aPixel.Data.a := aData^;
  1823. inc(aData);
  1824. end;
  1825. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1826. //TfdLuminance_UB1////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1827. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1828. procedure TfdLuminanceUB1.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  1829. begin
  1830. aData^ := LuminanceWeight(aPixel);
  1831. inc(aData);
  1832. end;
  1833. procedure TfdLuminanceUB1.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  1834. begin
  1835. aPixel.Data.r := aData^;
  1836. aPixel.Data.g := aData^;
  1837. aPixel.Data.b := aData^;
  1838. aPixel.Data.a := 0;
  1839. inc(aData);
  1840. end;
  1841. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1842. //TfdUniversal_UB1////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1843. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1844. procedure TfdUniversalUB1.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  1845. var
  1846. i: Integer;
  1847. begin
  1848. aData^ := 0;
  1849. for i := 0 to 3 do
  1850. if (Range.arr[i] > 0) then
  1851. aData^ := aData^ or ((aPixel.Data.arr[i] and Range.arr[i]) shl fShift.arr[i]);
  1852. inc(aData);
  1853. end;
  1854. procedure TfdUniversalUB1.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  1855. var
  1856. i: Integer;
  1857. begin
  1858. for i := 0 to 3 do
  1859. aPixel.Data.arr[i] := (aData^ shr fShift.arr[i]) and Range.arr[i];
  1860. inc(aData);
  1861. end;
  1862. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1863. //TfdLuminanceAlpha_UB2///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1864. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1865. procedure TfdLuminanceAlphaUB2.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  1866. begin
  1867. inherited Map(aPixel, aData, aMapData);
  1868. aData^ := aPixel.Data.a;
  1869. inc(aData);
  1870. end;
  1871. procedure TfdLuminanceAlphaUB2.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  1872. begin
  1873. inherited Unmap(aData, aPixel, aMapData);
  1874. aPixel.Data.a := aData^;
  1875. inc(aData);
  1876. end;
  1877. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1878. //TfdRGB_UB3//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1879. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1880. procedure TfdRGBub3.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  1881. begin
  1882. aData^ := aPixel.Data.r;
  1883. inc(aData);
  1884. aData^ := aPixel.Data.g;
  1885. inc(aData);
  1886. aData^ := aPixel.Data.b;
  1887. inc(aData);
  1888. end;
  1889. procedure TfdRGBub3.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  1890. begin
  1891. aPixel.Data.r := aData^;
  1892. inc(aData);
  1893. aPixel.Data.g := aData^;
  1894. inc(aData);
  1895. aPixel.Data.b := aData^;
  1896. inc(aData);
  1897. aPixel.Data.a := 0;
  1898. end;
  1899. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1900. //TfdBGR_UB3//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1901. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1902. procedure TfdBGRub3.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  1903. begin
  1904. aData^ := aPixel.Data.b;
  1905. inc(aData);
  1906. aData^ := aPixel.Data.g;
  1907. inc(aData);
  1908. aData^ := aPixel.Data.r;
  1909. inc(aData);
  1910. end;
  1911. procedure TfdBGRub3.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  1912. begin
  1913. aPixel.Data.b := aData^;
  1914. inc(aData);
  1915. aPixel.Data.g := aData^;
  1916. inc(aData);
  1917. aPixel.Data.r := aData^;
  1918. inc(aData);
  1919. aPixel.Data.a := 0;
  1920. end;
  1921. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1922. //TfdRGBA_UB4//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1923. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1924. procedure TfdRGBAub4.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  1925. begin
  1926. inherited Map(aPixel, aData, aMapData);
  1927. aData^ := aPixel.Data.a;
  1928. inc(aData);
  1929. end;
  1930. procedure TfdRGBAub4.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  1931. begin
  1932. inherited Unmap(aData, aPixel, aMapData);
  1933. aPixel.Data.a := aData^;
  1934. inc(aData);
  1935. end;
  1936. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1937. //TfdBGRA_UB4//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1938. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1939. procedure TfdBGRAub4.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  1940. begin
  1941. inherited Map(aPixel, aData, aMapData);
  1942. aData^ := aPixel.Data.a;
  1943. inc(aData);
  1944. end;
  1945. procedure TfdBGRAub4.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  1946. begin
  1947. inherited Unmap(aData, aPixel, aMapData);
  1948. aPixel.Data.a := aData^;
  1949. inc(aData);
  1950. end;
  1951. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1952. //TfdAlpha_US1////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1953. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1954. procedure TfdAlphaUS1.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  1955. begin
  1956. PWord(aData)^ := aPixel.Data.a;
  1957. inc(aData, 2);
  1958. end;
  1959. procedure TfdAlphaUS1.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  1960. begin
  1961. aPixel.Data.r := 0;
  1962. aPixel.Data.g := 0;
  1963. aPixel.Data.b := 0;
  1964. aPixel.Data.a := PWord(aData)^;
  1965. inc(aData, 2);
  1966. end;
  1967. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1968. //TfdLuminance_US1////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1969. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1970. procedure TfdLuminanceUS1.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  1971. begin
  1972. PWord(aData)^ := LuminanceWeight(aPixel);
  1973. inc(aData, 2);
  1974. end;
  1975. procedure TfdLuminanceUS1.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  1976. begin
  1977. aPixel.Data.r := PWord(aData)^;
  1978. aPixel.Data.g := PWord(aData)^;
  1979. aPixel.Data.b := PWord(aData)^;
  1980. aPixel.Data.a := 0;
  1981. inc(aData, 2);
  1982. end;
  1983. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1984. //TfdUniversal_US1////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1985. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1986. procedure TfdUniversalUS1.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  1987. var
  1988. i: Integer;
  1989. begin
  1990. PWord(aData)^ := 0;
  1991. for i := 0 to 3 do
  1992. if (Range.arr[i] > 0) then
  1993. PWord(aData)^ := PWord(aData)^ or ((aPixel.Data.arr[i] and Range.arr[i]) shl fShift.arr[i]);
  1994. inc(aData, 2);
  1995. end;
  1996. procedure TfdUniversalUS1.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  1997. var
  1998. i: Integer;
  1999. begin
  2000. for i := 0 to 3 do
  2001. aPixel.Data.arr[i] := (PWord(aData)^ shr fShift.arr[i]) and Range.arr[i];
  2002. inc(aData, 2);
  2003. end;
  2004. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2005. //TfdDepth_US1////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2006. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2007. procedure TfdDepthUS1.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  2008. begin
  2009. PWord(aData)^ := DepthWeight(aPixel);
  2010. inc(aData, 2);
  2011. end;
  2012. procedure TfdDepthUS1.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  2013. begin
  2014. aPixel.Data.r := PWord(aData)^;
  2015. aPixel.Data.g := PWord(aData)^;
  2016. aPixel.Data.b := PWord(aData)^;
  2017. aPixel.Data.a := PWord(aData)^;;
  2018. inc(aData, 2);
  2019. end;
  2020. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2021. //TfdLuminanceAlpha_US2///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2022. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2023. procedure TfdLuminanceAlphaUS2.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  2024. begin
  2025. inherited Map(aPixel, aData, aMapData);
  2026. PWord(aData)^ := aPixel.Data.a;
  2027. inc(aData, 2);
  2028. end;
  2029. procedure TfdLuminanceAlphaUS2.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  2030. begin
  2031. inherited Unmap(aData, aPixel, aMapData);
  2032. aPixel.Data.a := PWord(aData)^;
  2033. inc(aData, 2);
  2034. end;
  2035. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2036. //TfdRGB_US3//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2037. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2038. procedure TfdRGBus3.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  2039. begin
  2040. PWord(aData)^ := aPixel.Data.r;
  2041. inc(aData, 2);
  2042. PWord(aData)^ := aPixel.Data.g;
  2043. inc(aData, 2);
  2044. PWord(aData)^ := aPixel.Data.b;
  2045. inc(aData, 2);
  2046. end;
  2047. procedure TfdRGBus3.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  2048. begin
  2049. aPixel.Data.r := PWord(aData)^;
  2050. inc(aData, 2);
  2051. aPixel.Data.g := PWord(aData)^;
  2052. inc(aData, 2);
  2053. aPixel.Data.b := PWord(aData)^;
  2054. inc(aData, 2);
  2055. aPixel.Data.a := 0;
  2056. end;
  2057. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2058. //TfdBGR_US3//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2059. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2060. procedure TfdBGRus3.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  2061. begin
  2062. PWord(aData)^ := aPixel.Data.b;
  2063. inc(aData, 2);
  2064. PWord(aData)^ := aPixel.Data.g;
  2065. inc(aData, 2);
  2066. PWord(aData)^ := aPixel.Data.r;
  2067. inc(aData, 2);
  2068. end;
  2069. procedure TfdBGRus3.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  2070. begin
  2071. aPixel.Data.b := PWord(aData)^;
  2072. inc(aData, 2);
  2073. aPixel.Data.g := PWord(aData)^;
  2074. inc(aData, 2);
  2075. aPixel.Data.r := PWord(aData)^;
  2076. inc(aData, 2);
  2077. aPixel.Data.a := 0;
  2078. end;
  2079. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2080. //TfdRGBA_US4/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2081. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2082. procedure TfdRGBAus4.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  2083. begin
  2084. inherited Map(aPixel, aData, aMapData);
  2085. PWord(aData)^ := aPixel.Data.a;
  2086. inc(aData, 2);
  2087. end;
  2088. procedure TfdRGBAus4.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  2089. begin
  2090. inherited Unmap(aData, aPixel, aMapData);
  2091. aPixel.Data.a := PWord(aData)^;
  2092. inc(aData, 2);
  2093. end;
  2094. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2095. //TfdARGB_US4/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2096. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2097. procedure TfdARGBus4.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  2098. begin
  2099. PWord(aData)^ := aPixel.Data.a;
  2100. inc(aData, 2);
  2101. inherited Map(aPixel, aData, aMapData);
  2102. end;
  2103. procedure TfdARGBus4.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  2104. begin
  2105. aPixel.Data.a := PWord(aData)^;
  2106. inc(aData, 2);
  2107. inherited Unmap(aData, aPixel, aMapData);
  2108. end;
  2109. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2110. //TfdBGRA_US4/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2111. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2112. procedure TfdBGRAus4.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  2113. begin
  2114. inherited Map(aPixel, aData, aMapData);
  2115. PWord(aData)^ := aPixel.Data.a;
  2116. inc(aData, 2);
  2117. end;
  2118. procedure TfdBGRAus4.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  2119. begin
  2120. inherited Unmap(aData, aPixel, aMapData);
  2121. aPixel.Data.a := PWord(aData)^;
  2122. inc(aData, 2);
  2123. end;
  2124. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2125. //TfdABGR_US4/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2126. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2127. procedure TfdABGRus4.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  2128. begin
  2129. PWord(aData)^ := aPixel.Data.a;
  2130. inc(aData, 2);
  2131. inherited Map(aPixel, aData, aMapData);
  2132. end;
  2133. procedure TfdABGRus4.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  2134. begin
  2135. aPixel.Data.a := PWord(aData)^;
  2136. inc(aData, 2);
  2137. inherited Unmap(aData, aPixel, aMapData);
  2138. end;
  2139. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2140. //TfdUniversal_UI1////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2141. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2142. procedure TfdUniversalUI1.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  2143. var
  2144. i: Integer;
  2145. begin
  2146. PCardinal(aData)^ := 0;
  2147. for i := 0 to 3 do
  2148. if (Range.arr[i] > 0) then
  2149. PCardinal(aData)^ := PCardinal(aData)^ or ((aPixel.Data.arr[i] and Range.arr[i]) shl fShift.arr[i]);
  2150. inc(aData, 4);
  2151. end;
  2152. procedure TfdUniversalUI1.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  2153. var
  2154. i: Integer;
  2155. begin
  2156. for i := 0 to 3 do
  2157. aPixel.Data.arr[i] := (PCardinal(aData)^ shr fShift.arr[i]) and Range.arr[i];
  2158. inc(aData, 2);
  2159. end;
  2160. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2161. //TfdDepth_UI1////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2162. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2163. procedure TfdDepthUI1.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  2164. begin
  2165. PCardinal(aData)^ := DepthWeight(aPixel);
  2166. inc(aData, 4);
  2167. end;
  2168. procedure TfdDepthUI1.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  2169. begin
  2170. aPixel.Data.r := PCardinal(aData)^;
  2171. aPixel.Data.g := PCardinal(aData)^;
  2172. aPixel.Data.b := PCardinal(aData)^;
  2173. aPixel.Data.a := PCardinal(aData)^;
  2174. inc(aData, 4);
  2175. end;
  2176. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2177. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2178. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2179. procedure TfdAlpha4ub1.SetValues;
  2180. begin
  2181. inherited SetValues;
  2182. fBitsPerPixel := 8;
  2183. fFormat := tfAlpha4ub1;
  2184. fWithAlpha := tfAlpha4ub1;
  2185. fPrecision := glBitmapRec4ub(0, 0, 0, 8);
  2186. fShift := glBitmapRec4ub(0, 0, 0, 0);
  2187. {$IFNDEF OPENGL_ES}
  2188. fOpenGLFormat := tfAlpha4ub1;
  2189. fglFormat := GL_ALPHA;
  2190. fglInternalFormat := GL_ALPHA4;
  2191. fglDataFormat := GL_UNSIGNED_BYTE;
  2192. {$ELSE}
  2193. fOpenGLFormat := tfAlpha8ub1;
  2194. {$ENDIF}
  2195. end;
  2196. procedure TfdAlpha8ub1.SetValues;
  2197. begin
  2198. inherited SetValues;
  2199. fBitsPerPixel := 8;
  2200. fFormat := tfAlpha8ub1;
  2201. fWithAlpha := tfAlpha8ub1;
  2202. fPrecision := glBitmapRec4ub(0, 0, 0, 8);
  2203. fShift := glBitmapRec4ub(0, 0, 0, 0);
  2204. fOpenGLFormat := tfAlpha8ub1;
  2205. fglFormat := GL_ALPHA;
  2206. fglInternalFormat := {$IFNDEF OPENGL_ES}GL_ALPHA8{$ELSE}GL_ALPHA{$ENDIF};
  2207. fglDataFormat := GL_UNSIGNED_BYTE;
  2208. end;
  2209. procedure TfdAlpha16us1.SetValues;
  2210. begin
  2211. inherited SetValues;
  2212. fBitsPerPixel := 16;
  2213. fFormat := tfAlpha16us1;
  2214. fWithAlpha := tfAlpha16us1;
  2215. fPrecision := glBitmapRec4ub(0, 0, 0, 16);
  2216. fShift := glBitmapRec4ub(0, 0, 0, 0);
  2217. {$IFNDEF OPENGL_ES}
  2218. fOpenGLFormat := tfAlpha16us1;
  2219. fglFormat := GL_ALPHA;
  2220. fglInternalFormat := GL_ALPHA16;
  2221. fglDataFormat := GL_UNSIGNED_SHORT;
  2222. {$ELSE}
  2223. fOpenGLFormat := tfAlpha8ub1;
  2224. {$ENDIF}
  2225. end;
  2226. procedure TfdLuminance4ub1.SetValues;
  2227. begin
  2228. inherited SetValues;
  2229. fBitsPerPixel := 8;
  2230. fFormat := tfLuminance4ub1;
  2231. fWithAlpha := tfLuminance4Alpha4ub2;
  2232. fWithoutAlpha := tfLuminance4ub1;
  2233. fPrecision := glBitmapRec4ub(8, 8, 8, 0);
  2234. fShift := glBitmapRec4ub(0, 0, 0, 0);
  2235. {$IFNDEF OPENGL_ES}
  2236. fOpenGLFormat := tfLuminance4ub1;
  2237. fglFormat := GL_LUMINANCE;
  2238. fglInternalFormat := GL_LUMINANCE4;
  2239. fglDataFormat := GL_UNSIGNED_BYTE;
  2240. {$ELSE}
  2241. fOpenGLFormat := tfLuminance8ub1;
  2242. {$ENDIF}
  2243. end;
  2244. procedure TfdLuminance8ub1.SetValues;
  2245. begin
  2246. inherited SetValues;
  2247. fBitsPerPixel := 8;
  2248. fFormat := tfLuminance8ub1;
  2249. fWithAlpha := tfLuminance8Alpha8ub2;
  2250. fWithoutAlpha := tfLuminance8ub1;
  2251. fOpenGLFormat := tfLuminance8ub1;
  2252. fPrecision := glBitmapRec4ub(8, 8, 8, 0);
  2253. fShift := glBitmapRec4ub(0, 0, 0, 0);
  2254. fglFormat := GL_LUMINANCE;
  2255. fglInternalFormat := {$IFNDEF OPENGL_ES}GL_LUMINANCE8{$ELSE}GL_LUMINANCE{$ENDIF};
  2256. fglDataFormat := GL_UNSIGNED_BYTE;
  2257. end;
  2258. procedure TfdLuminance16us1.SetValues;
  2259. begin
  2260. inherited SetValues;
  2261. fBitsPerPixel := 16;
  2262. fFormat := tfLuminance16us1;
  2263. fWithAlpha := tfLuminance16Alpha16us2;
  2264. fWithoutAlpha := tfLuminance16us1;
  2265. fPrecision := glBitmapRec4ub(16, 16, 16, 0);
  2266. fShift := glBitmapRec4ub( 0, 0, 0, 0);
  2267. {$IFNDEF OPENGL_ES}
  2268. fOpenGLFormat := tfLuminance16us1;
  2269. fglFormat := GL_LUMINANCE;
  2270. fglInternalFormat := GL_LUMINANCE16;
  2271. fglDataFormat := GL_UNSIGNED_SHORT;
  2272. {$ELSE}
  2273. fOpenGLFormat := tfLuminance8ub1;
  2274. {$ENDIF}
  2275. end;
  2276. procedure TfdLuminance4Alpha4ub2.SetValues;
  2277. begin
  2278. inherited SetValues;
  2279. fBitsPerPixel := 16;
  2280. fFormat := tfLuminance4Alpha4ub2;
  2281. fWithAlpha := tfLuminance4Alpha4ub2;
  2282. fWithoutAlpha := tfLuminance4ub1;
  2283. fPrecision := glBitmapRec4ub(8, 8, 8, 8);
  2284. fShift := glBitmapRec4ub(0, 0, 0, 8);
  2285. {$IFNDEF OPENGL_ES}
  2286. fOpenGLFormat := tfLuminance4Alpha4ub2;
  2287. fglFormat := GL_LUMINANCE_ALPHA;
  2288. fglInternalFormat := GL_LUMINANCE4_ALPHA4;
  2289. fglDataFormat := GL_UNSIGNED_BYTE;
  2290. {$ELSE}
  2291. fOpenGLFormat := tfLuminance8Alpha8ub2;
  2292. {$ENDIF}
  2293. end;
  2294. procedure TfdLuminance6Alpha2ub2.SetValues;
  2295. begin
  2296. inherited SetValues;
  2297. fBitsPerPixel := 16;
  2298. fFormat := tfLuminance6Alpha2ub2;
  2299. fWithAlpha := tfLuminance6Alpha2ub2;
  2300. fWithoutAlpha := tfLuminance8ub1;
  2301. fPrecision := glBitmapRec4ub(8, 8, 8, 8);
  2302. fShift := glBitmapRec4ub(0, 0, 0, 8);
  2303. {$IFNDEF OPENGL_ES}
  2304. fOpenGLFormat := tfLuminance6Alpha2ub2;
  2305. fglFormat := GL_LUMINANCE_ALPHA;
  2306. fglInternalFormat := GL_LUMINANCE6_ALPHA2;
  2307. fglDataFormat := GL_UNSIGNED_BYTE;
  2308. {$ELSE}
  2309. fOpenGLFormat := tfLuminance8Alpha8ub2;
  2310. {$ENDIF}
  2311. end;
  2312. procedure TfdLuminance8Alpha8ub2.SetValues;
  2313. begin
  2314. inherited SetValues;
  2315. fBitsPerPixel := 16;
  2316. fFormat := tfLuminance8Alpha8ub2;
  2317. fWithAlpha := tfLuminance8Alpha8ub2;
  2318. fWithoutAlpha := tfLuminance8ub1;
  2319. fOpenGLFormat := tfLuminance8Alpha8ub2;
  2320. fPrecision := glBitmapRec4ub(8, 8, 8, 8);
  2321. fShift := glBitmapRec4ub(0, 0, 0, 8);
  2322. fglFormat := GL_LUMINANCE_ALPHA;
  2323. fglInternalFormat := {$IFNDEF OPENGL_ES}GL_LUMINANCE8_ALPHA8{$ELSE}GL_LUMINANCE_ALPHA{$ENDIF};
  2324. fglDataFormat := GL_UNSIGNED_BYTE;
  2325. end;
  2326. procedure TfdLuminance12Alpha4us2.SetValues;
  2327. begin
  2328. inherited SetValues;
  2329. fBitsPerPixel := 32;
  2330. fFormat := tfLuminance12Alpha4us2;
  2331. fWithAlpha := tfLuminance12Alpha4us2;
  2332. fWithoutAlpha := tfLuminance16us1;
  2333. fPrecision := glBitmapRec4ub(16, 16, 16, 16);
  2334. fShift := glBitmapRec4ub( 0, 0, 0, 16);
  2335. {$IFNDEF OPENGL_ES}
  2336. fOpenGLFormat := tfLuminance12Alpha4us2;
  2337. fglFormat := GL_LUMINANCE_ALPHA;
  2338. fglInternalFormat := GL_LUMINANCE12_ALPHA4;
  2339. fglDataFormat := GL_UNSIGNED_SHORT;
  2340. {$ELSE}
  2341. fOpenGLFormat := tfLuminance8Alpha8ub2;
  2342. {$ENDIF}
  2343. end;
  2344. procedure TfdLuminance16Alpha16us2.SetValues;
  2345. begin
  2346. inherited SetValues;
  2347. fBitsPerPixel := 32;
  2348. fFormat := tfLuminance16Alpha16us2;
  2349. fWithAlpha := tfLuminance16Alpha16us2;
  2350. fWithoutAlpha := tfLuminance16us1;
  2351. fPrecision := glBitmapRec4ub(16, 16, 16, 16);
  2352. fShift := glBitmapRec4ub( 0, 0, 0, 16);
  2353. {$IFNDEF OPENGL_ES}
  2354. fOpenGLFormat := tfLuminance16Alpha16us2;
  2355. fglFormat := GL_LUMINANCE_ALPHA;
  2356. fglInternalFormat := GL_LUMINANCE16_ALPHA16;
  2357. fglDataFormat := GL_UNSIGNED_SHORT;
  2358. {$ELSE}
  2359. fOpenGLFormat := tfLuminance8Alpha8ub2;
  2360. {$ENDIF}
  2361. end;
  2362. procedure TfdR3G3B2ub1.SetValues;
  2363. begin
  2364. inherited SetValues;
  2365. fBitsPerPixel := 8;
  2366. fFormat := tfR3G3B2ub1;
  2367. fWithAlpha := tfRGBA4us1;
  2368. fWithoutAlpha := tfR3G3B2ub1;
  2369. fRGBInverted := tfEmpty;
  2370. fPrecision := glBitmapRec4ub(3, 3, 2, 0);
  2371. fShift := glBitmapRec4ub(5, 2, 0, 0);
  2372. {$IFNDEF OPENGL_ES}
  2373. fOpenGLFormat := tfR3G3B2ub1;
  2374. fglFormat := GL_RGB;
  2375. fglInternalFormat := GL_R3_G3_B2;
  2376. fglDataFormat := GL_UNSIGNED_BYTE_3_3_2;
  2377. {$ELSE}
  2378. fOpenGLFormat := tfR5G6B5us1;
  2379. {$ENDIF}
  2380. end;
  2381. procedure TfdRGBX4us1.SetValues;
  2382. begin
  2383. inherited SetValues;
  2384. fBitsPerPixel := 16;
  2385. fFormat := tfRGBX4us1;
  2386. fWithAlpha := tfRGBA4us1;
  2387. fWithoutAlpha := tfRGBX4us1;
  2388. fRGBInverted := tfBGRX4us1;
  2389. fPrecision := glBitmapRec4ub( 4, 4, 4, 0);
  2390. fShift := glBitmapRec4ub(12, 8, 4, 0);
  2391. {$IFNDEF OPENGL_ES}
  2392. fOpenGLFormat := tfRGBX4us1;
  2393. fglFormat := GL_RGBA; //GL_INVALID_OPERATION if not GL_BGRA or GL_RGBA
  2394. fglInternalFormat := GL_RGB4;
  2395. fglDataFormat := GL_UNSIGNED_SHORT_4_4_4_4;
  2396. {$ELSE}
  2397. fOpenGLFormat := tfR5G6B5us1;
  2398. {$ENDIF}
  2399. end;
  2400. procedure TfdXRGB4us1.SetValues;
  2401. begin
  2402. inherited SetValues;
  2403. fBitsPerPixel := 16;
  2404. fFormat := tfXRGB4us1;
  2405. fWithAlpha := tfARGB4us1;
  2406. fWithoutAlpha := tfXRGB4us1;
  2407. fRGBInverted := tfXBGR4us1;
  2408. fPrecision := glBitmapRec4ub(4, 4, 4, 0);
  2409. fShift := glBitmapRec4ub(8, 4, 0, 0);
  2410. {$IFNDEF OPENGL_ES}
  2411. fOpenGLFormat := tfXRGB4us1;
  2412. fglFormat := GL_BGRA;
  2413. fglInternalFormat := GL_RGB4;
  2414. fglDataFormat := GL_UNSIGNED_SHORT_4_4_4_4_REV;
  2415. {$ELSE}
  2416. fOpenGLFormat := tfR5G6B5us1;
  2417. {$ENDIF}
  2418. end;
  2419. procedure TfdR5G6B5us1.SetValues;
  2420. begin
  2421. inherited SetValues;
  2422. fBitsPerPixel := 16;
  2423. fFormat := tfR5G6B5us1;
  2424. fWithAlpha := tfRGB5A1us1;
  2425. fWithoutAlpha := tfR5G6B5us1;
  2426. fRGBInverted := tfB5G6R5us1;
  2427. fPrecision := glBitmapRec4ub( 5, 6, 5, 0);
  2428. fShift := glBitmapRec4ub(11, 5, 0, 0);
  2429. {$IF NOT DEFINED(OPENGL_ES) OR DEFINED(OPENGL_ES_2_0)}
  2430. fOpenGLFormat := tfR5G6B5us1;
  2431. fglFormat := GL_RGB;
  2432. fglInternalFormat := GL_RGB565;
  2433. fglDataFormat := GL_UNSIGNED_SHORT_5_6_5;
  2434. {$ELSE}
  2435. fOpenGLFormat := tfRGB8ub3;
  2436. {$IFEND}
  2437. end;
  2438. procedure TfdRGB5X1us1.SetValues;
  2439. begin
  2440. inherited SetValues;
  2441. fBitsPerPixel := 16;
  2442. fFormat := tfRGB5X1us1;
  2443. fWithAlpha := tfRGB5A1us1;
  2444. fWithoutAlpha := tfRGB5X1us1;
  2445. fRGBInverted := tfBGR5X1us1;
  2446. fPrecision := glBitmapRec4ub( 5, 5, 5, 0);
  2447. fShift := glBitmapRec4ub(11, 6, 1, 0);
  2448. {$IFNDEF OPENGL_ES}
  2449. fOpenGLFormat := tfRGB5X1us1;
  2450. fglFormat := GL_RGBA; //GL_INVALID_OPERATION if not GL_BGRA or GL_RGBA
  2451. fglInternalFormat := GL_RGB5;
  2452. fglDataFormat := GL_UNSIGNED_SHORT_5_5_5_1;
  2453. {$ELSE}
  2454. fOpenGLFormat := tfR5G6B5us1;
  2455. {$ENDIF}
  2456. end;
  2457. procedure TfdX1RGB5us1.SetValues;
  2458. begin
  2459. inherited SetValues;
  2460. fBitsPerPixel := 16;
  2461. fFormat := tfX1RGB5us1;
  2462. fWithAlpha := tfA1RGB5us1;
  2463. fWithoutAlpha := tfX1RGB5us1;
  2464. fRGBInverted := tfX1BGR5us1;
  2465. fPrecision := glBitmapRec4ub( 5, 5, 5, 0);
  2466. fShift := glBitmapRec4ub(10, 5, 0, 0);
  2467. {$IFNDEF OPENGL_ES}
  2468. fOpenGLFormat := tfX1RGB5us1;
  2469. fglFormat := GL_BGRA; //GL_INVALID_OPERATION if not GL_BGRA or GL_RGBA
  2470. fglInternalFormat := GL_RGB5;
  2471. fglDataFormat := GL_UNSIGNED_SHORT_1_5_5_5_REV;
  2472. {$ELSE}
  2473. fOpenGLFormat := tfR5G6B5us1;
  2474. {$ENDIF}
  2475. end;
  2476. procedure TfdRGB8ub3.SetValues;
  2477. begin
  2478. inherited SetValues;
  2479. fBitsPerPixel := 24;
  2480. fFormat := tfRGB8ub3;
  2481. fWithAlpha := tfRGBA8ub4;
  2482. fWithoutAlpha := tfRGB8ub3;
  2483. fRGBInverted := tfBGR8ub3;
  2484. fPrecision := glBitmapRec4ub(8, 8, 8, 0);
  2485. fShift := glBitmapRec4ub(0, 8, 16, 0);
  2486. fOpenGLFormat := tfRGB8ub3;
  2487. fglFormat := GL_RGB;
  2488. fglInternalFormat := {$IF NOT DEFINED(OPENGL_ES) OR DEFINED(OPENGL_ES_3_0)}GL_RGB8{$ELSE}GL_RGB{$IFEND};
  2489. fglDataFormat := GL_UNSIGNED_BYTE;
  2490. end;
  2491. procedure TfdRGBX8ui1.SetValues;
  2492. begin
  2493. inherited SetValues;
  2494. fBitsPerPixel := 32;
  2495. fFormat := tfRGBX8ui1;
  2496. fWithAlpha := tfRGBA8ui1;
  2497. fWithoutAlpha := tfRGBX8ui1;
  2498. fRGBInverted := tfBGRX8ui1;
  2499. fPrecision := glBitmapRec4ub( 8, 8, 8, 0);
  2500. fShift := glBitmapRec4ub(24, 16, 8, 0);
  2501. {$IFNDEF OPENGL_ES}
  2502. fOpenGLFormat := tfRGBX8ui1;
  2503. fglFormat := GL_RGBA; //GL_INVALID_OPERATION if not GL_BGRA or GL_RGBA
  2504. fglInternalFormat := GL_RGB8;
  2505. fglDataFormat := GL_UNSIGNED_INT_8_8_8_8;
  2506. {$ELSE}
  2507. fOpenGLFormat := tfRGB8ub3;
  2508. {$ENDIF}
  2509. end;
  2510. procedure TfdXRGB8ui1.SetValues;
  2511. begin
  2512. inherited SetValues;
  2513. fBitsPerPixel := 32;
  2514. fFormat := tfXRGB8ui1;
  2515. fWithAlpha := tfXRGB8ui1;
  2516. fWithoutAlpha := tfXRGB8ui1;
  2517. fOpenGLFormat := tfXRGB8ui1;
  2518. fRGBInverted := tfXBGR8ui1;
  2519. fPrecision := glBitmapRec4ub( 8, 8, 8, 0);
  2520. fShift := glBitmapRec4ub(16, 8, 0, 0);
  2521. {$IFNDEF OPENGL_ES}
  2522. fOpenGLFormat := tfXRGB8ui1;
  2523. fglFormat := GL_BGRA; //GL_INVALID_OPERATION if not GL_BGRA or GL_RGBA
  2524. fglInternalFormat := GL_RGB8;
  2525. fglDataFormat := GL_UNSIGNED_INT_8_8_8_8_REV;
  2526. {$ELSE}
  2527. fOpenGLFormat := tfRGB8ub3;
  2528. {$ENDIF}
  2529. end;
  2530. procedure TfdRGB10X2ui1.SetValues;
  2531. begin
  2532. inherited SetValues;
  2533. fBitsPerPixel := 32;
  2534. fFormat := tfRGB10X2ui1;
  2535. fWithAlpha := tfRGB10A2ui1;
  2536. fWithoutAlpha := tfRGB10X2ui1;
  2537. fRGBInverted := tfBGR10X2ui1;
  2538. fPrecision := glBitmapRec4ub(10, 10, 10, 0);
  2539. fShift := glBitmapRec4ub(22, 12, 2, 0);
  2540. {$IFNDEF OPENGL_ES}
  2541. fOpenGLFormat := tfRGB10X2ui1;
  2542. fglFormat := GL_RGBA; //GL_INVALID_OPERATION if not GL_BGRA or GL_RGBA
  2543. fglInternalFormat := GL_RGB10;
  2544. fglDataFormat := GL_UNSIGNED_INT_10_10_10_2;
  2545. {$ELSE}
  2546. fOpenGLFormat := tfRGB16us3;
  2547. {$ENDIF}
  2548. end;
  2549. procedure TfdX2RGB10ui1.SetValues;
  2550. begin
  2551. inherited SetValues;
  2552. fBitsPerPixel := 32;
  2553. fFormat := tfX2RGB10ui1;
  2554. fWithAlpha := tfA2RGB10ui1;
  2555. fWithoutAlpha := tfX2RGB10ui1;
  2556. fRGBInverted := tfX2BGR10ui1;
  2557. fPrecision := glBitmapRec4ub(10, 10, 10, 0);
  2558. fShift := glBitmapRec4ub(20, 10, 0, 0);
  2559. {$IFNDEF OPENGL_ES}
  2560. fOpenGLFormat := tfX2RGB10ui1;
  2561. fglFormat := GL_BGRA; //GL_INVALID_OPERATION if not GL_BGRA or GL_RGBA
  2562. fglInternalFormat := GL_RGB10;
  2563. fglDataFormat := GL_UNSIGNED_INT_2_10_10_10_REV;
  2564. {$ELSE}
  2565. fOpenGLFormat := tfRGB16us3;
  2566. {$ENDIF}
  2567. end;
  2568. procedure TfdRGB16us3.SetValues;
  2569. begin
  2570. inherited SetValues;
  2571. fBitsPerPixel := 48;
  2572. fFormat := tfRGB16us3;
  2573. fWithAlpha := tfRGBA16us4;
  2574. fWithoutAlpha := tfRGB16us3;
  2575. fRGBInverted := tfBGR16us3;
  2576. fPrecision := glBitmapRec4ub(16, 16, 16, 0);
  2577. fShift := glBitmapRec4ub( 0, 16, 32, 0);
  2578. {$IF NOT DEFINED(OPENGL_ES) OR DEFINED(OPENGL_ES_3_0)}
  2579. fOpenGLFormat := tfRGB16us3;
  2580. fglFormat := GL_RGB;
  2581. fglInternalFormat := {$IFNDEF OPENGL_ES}GL_RGB16{$ELSE}GL_RGB16UI{$ENDIF};
  2582. fglDataFormat := GL_UNSIGNED_SHORT;
  2583. {$ELSE}
  2584. fOpenGLFormat := tfRGB8ub3;
  2585. {$IFEND}
  2586. end;
  2587. procedure TfdRGBA4us1.SetValues;
  2588. begin
  2589. inherited SetValues;
  2590. fBitsPerPixel := 16;
  2591. fFormat := tfRGBA4us1;
  2592. fWithAlpha := tfRGBA4us1;
  2593. fWithoutAlpha := tfRGBX4us1;
  2594. fOpenGLFormat := tfRGBA4us1;
  2595. fRGBInverted := tfBGRA4us1;
  2596. fPrecision := glBitmapRec4ub( 4, 4, 4, 4);
  2597. fShift := glBitmapRec4ub(12, 8, 4, 0);
  2598. fglFormat := GL_RGBA;
  2599. fglInternalFormat := {$IF NOT DEFINED(OPENGL_ES) OR DEFINED(OPENGL_ES_3_0)}GL_RGBA4{$ELSE}GL_RGBA{$IFEND};
  2600. fglDataFormat := GL_UNSIGNED_SHORT_4_4_4_4;
  2601. end;
  2602. procedure TfdARGB4us1.SetValues;
  2603. begin
  2604. inherited SetValues;
  2605. fBitsPerPixel := 16;
  2606. fFormat := tfARGB4us1;
  2607. fWithAlpha := tfARGB4us1;
  2608. fWithoutAlpha := tfXRGB4us1;
  2609. fRGBInverted := tfABGR4us1;
  2610. fPrecision := glBitmapRec4ub( 4, 4, 4, 4);
  2611. fShift := glBitmapRec4ub( 8, 4, 0, 12);
  2612. {$IFNDEF OPENGL_ES}
  2613. fOpenGLFormat := tfARGB4us1;
  2614. fglFormat := GL_BGRA;
  2615. fglInternalFormat := GL_RGBA4;
  2616. fglDataFormat := GL_UNSIGNED_SHORT_4_4_4_4_REV;
  2617. {$ELSE}
  2618. fOpenGLFormat := tfRGBA4us1;
  2619. {$ENDIF}
  2620. end;
  2621. procedure TfdRGB5A1us1.SetValues;
  2622. begin
  2623. inherited SetValues;
  2624. fBitsPerPixel := 16;
  2625. fFormat := tfRGB5A1us1;
  2626. fWithAlpha := tfRGB5A1us1;
  2627. fWithoutAlpha := tfRGB5X1us1;
  2628. fOpenGLFormat := tfRGB5A1us1;
  2629. fRGBInverted := tfBGR5A1us1;
  2630. fPrecision := glBitmapRec4ub( 5, 5, 5, 1);
  2631. fShift := glBitmapRec4ub(11, 6, 1, 0);
  2632. fglFormat := GL_RGBA;
  2633. fglInternalFormat := {$IF NOT DEFINED(OPENGL_ES) OR DEFINED(OPENGL_ES_2_0)}GL_RGB5_A1{$ELSE}GL_RGBA{$IFEND};
  2634. fglDataFormat := GL_UNSIGNED_SHORT_5_5_5_1;
  2635. end;
  2636. procedure TfdA1RGB5us1.SetValues;
  2637. begin
  2638. inherited SetValues;
  2639. fBitsPerPixel := 16;
  2640. fFormat := tfA1RGB5us1;
  2641. fWithAlpha := tfA1RGB5us1;
  2642. fWithoutAlpha := tfX1RGB5us1;
  2643. fRGBInverted := tfA1BGR5us1;
  2644. fPrecision := glBitmapRec4ub( 5, 5, 5, 1);
  2645. fShift := glBitmapRec4ub(10, 5, 0, 15);
  2646. {$IFNDEF OPENGL_ES}
  2647. fOpenGLFormat := tfA1RGB5us1;
  2648. fglFormat := GL_BGRA;
  2649. fglInternalFormat := GL_RGB5_A1;
  2650. fglDataFormat := GL_UNSIGNED_SHORT_1_5_5_5_REV;
  2651. {$ELSE}
  2652. fOpenGLFormat := tfRGB5A1us1;
  2653. {$ENDIF}
  2654. end;
  2655. procedure TfdRGBA8ui1.SetValues;
  2656. begin
  2657. inherited SetValues;
  2658. fBitsPerPixel := 32;
  2659. fFormat := tfRGBA8ui1;
  2660. fWithAlpha := tfRGBA8ui1;
  2661. fWithoutAlpha := tfRGBX8ui1;
  2662. fRGBInverted := tfBGRA8ui1;
  2663. fPrecision := glBitmapRec4ub( 8, 8, 8, 8);
  2664. fShift := glBitmapRec4ub(24, 16, 8, 0);
  2665. {$IFNDEF OPENGL_ES}
  2666. fOpenGLFormat := tfRGBA8ui1;
  2667. fglFormat := GL_RGBA;
  2668. fglInternalFormat := GL_RGBA8;
  2669. fglDataFormat := GL_UNSIGNED_INT_8_8_8_8;
  2670. {$ELSE}
  2671. fOpenGLFormat := tfRGBA8ub4;
  2672. {$ENDIF}
  2673. end;
  2674. procedure TfdARGB8ui1.SetValues;
  2675. begin
  2676. inherited SetValues;
  2677. fBitsPerPixel := 32;
  2678. fFormat := tfARGB8ui1;
  2679. fWithAlpha := tfARGB8ui1;
  2680. fWithoutAlpha := tfXRGB8ui1;
  2681. fRGBInverted := tfABGR8ui1;
  2682. fPrecision := glBitmapRec4ub( 8, 8, 8, 8);
  2683. fShift := glBitmapRec4ub(16, 8, 0, 24);
  2684. {$IFNDEF OPENGL_ES}
  2685. fOpenGLFormat := tfARGB8ui1;
  2686. fglFormat := GL_BGRA;
  2687. fglInternalFormat := GL_RGBA8;
  2688. fglDataFormat := GL_UNSIGNED_INT_8_8_8_8_REV;
  2689. {$ELSE}
  2690. fOpenGLFormat := tfRGBA8ub4;
  2691. {$ENDIF}
  2692. end;
  2693. procedure TfdRGBA8ub4.SetValues;
  2694. begin
  2695. inherited SetValues;
  2696. fBitsPerPixel := 32;
  2697. fFormat := tfRGBA8ub4;
  2698. fWithAlpha := tfRGBA8ub4;
  2699. fWithoutAlpha := tfRGB8ub3;
  2700. fOpenGLFormat := tfRGBA8ub4;
  2701. fRGBInverted := tfBGRA8ub4;
  2702. fPrecision := glBitmapRec4ub( 8, 8, 8, 8);
  2703. fShift := glBitmapRec4ub( 0, 8, 16, 24);
  2704. fglFormat := GL_RGBA;
  2705. fglInternalFormat := {$IF NOT DEFINED(OPENGL_ES) OR DEFINED(OPENGL_ES_3_0)}GL_RGBA8{$ELSE}GL_RGBA{$IFEND};
  2706. fglDataFormat := GL_UNSIGNED_BYTE;
  2707. end;
  2708. procedure TfdRGB10A2ui1.SetValues;
  2709. begin
  2710. inherited SetValues;
  2711. fBitsPerPixel := 32;
  2712. fFormat := tfRGB10A2ui1;
  2713. fWithAlpha := tfRGB10A2ui1;
  2714. fWithoutAlpha := tfRGB10X2ui1;
  2715. fRGBInverted := tfBGR10A2ui1;
  2716. fPrecision := glBitmapRec4ub(10, 10, 10, 2);
  2717. fShift := glBitmapRec4ub(22, 12, 2, 0);
  2718. {$IFNDEF OPENGL_ES}
  2719. fOpenGLFormat := tfRGB10A2ui1;
  2720. fglFormat := GL_RGBA;
  2721. fglInternalFormat := GL_RGB10_A2;
  2722. fglDataFormat := GL_UNSIGNED_INT_10_10_10_2;
  2723. {$ELSE}
  2724. fOpenGLFormat := tfA2RGB10ui1;
  2725. {$ENDIF}
  2726. end;
  2727. procedure TfdA2RGB10ui1.SetValues;
  2728. begin
  2729. inherited SetValues;
  2730. fBitsPerPixel := 32;
  2731. fFormat := tfA2RGB10ui1;
  2732. fWithAlpha := tfA2RGB10ui1;
  2733. fWithoutAlpha := tfX2RGB10ui1;
  2734. fRGBInverted := tfA2BGR10ui1;
  2735. fPrecision := glBitmapRec4ub(10, 10, 10, 2);
  2736. fShift := glBitmapRec4ub(20, 10, 0, 30);
  2737. {$IF NOT DEFINED(OPENGL_ES)}
  2738. fOpenGLFormat := tfA2RGB10ui1;
  2739. fglFormat := GL_BGRA;
  2740. fglInternalFormat := GL_RGB10_A2;
  2741. fglDataFormat := GL_UNSIGNED_INT_2_10_10_10_REV;
  2742. {$ELSEIF DEFINED(OPENGL_ES_3_0)}
  2743. fOpenGLFormat := tfA2RGB10ui1;
  2744. fglFormat := GL_RGBA;
  2745. fglInternalFormat := GL_RGB10_A2;
  2746. fglDataFormat := GL_UNSIGNED_INT_2_10_10_10_REV;
  2747. {$ELSE}
  2748. fOpenGLFormat := tfRGBA8ui1;
  2749. {$IFEND}
  2750. end;
  2751. procedure TfdRGBA16us4.SetValues;
  2752. begin
  2753. inherited SetValues;
  2754. fBitsPerPixel := 64;
  2755. fFormat := tfRGBA16us4;
  2756. fWithAlpha := tfRGBA16us4;
  2757. fWithoutAlpha := tfRGB16us3;
  2758. fRGBInverted := tfBGRA16us4;
  2759. fPrecision := glBitmapRec4ub(16, 16, 16, 16);
  2760. fShift := glBitmapRec4ub( 0, 16, 32, 48);
  2761. {$IF NOT DEFINED(OPENGL_ES) OR DEFINED(OPENGL_ES_3_0)}
  2762. fOpenGLFormat := tfRGBA16us4;
  2763. fglFormat := GL_RGBA;
  2764. fglInternalFormat := {$IFNDEF OPENGL_ES}GL_RGBA16{$ELSE}GL_RGBA16UI{$ENDIF};
  2765. fglDataFormat := GL_UNSIGNED_SHORT;
  2766. {$ELSE}
  2767. fOpenGLFormat := tfRGBA8ub4;
  2768. {$IFEND}
  2769. end;
  2770. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2771. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2772. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2773. procedure TfdBGRX4us1.SetValues;
  2774. begin
  2775. inherited SetValues;
  2776. fBitsPerPixel := 16;
  2777. fFormat := tfBGRX4us1;
  2778. fWithAlpha := tfBGRA4us1;
  2779. fWithoutAlpha := tfBGRX4us1;
  2780. fRGBInverted := tfRGBX4us1;
  2781. fPrecision := glBitmapRec4ub( 4, 4, 4, 0);
  2782. fShift := glBitmapRec4ub( 4, 8, 12, 0);
  2783. {$IFNDEF OPENGL_ES}
  2784. fOpenGLFormat := tfBGRX4us1;
  2785. fglFormat := GL_BGRA; //GL_INVALID_OPERATION if not GL_BGRA or GL_RGBA
  2786. fglInternalFormat := GL_RGB4;
  2787. fglDataFormat := GL_UNSIGNED_SHORT_4_4_4_4;
  2788. {$ELSE}
  2789. fOpenGLFormat := tfR5G6B5us1;
  2790. {$ENDIF}
  2791. end;
  2792. procedure TfdXBGR4us1.SetValues;
  2793. begin
  2794. inherited SetValues;
  2795. fBitsPerPixel := 16;
  2796. fFormat := tfXBGR4us1;
  2797. fWithAlpha := tfABGR4us1;
  2798. fWithoutAlpha := tfXBGR4us1;
  2799. fRGBInverted := tfXRGB4us1;
  2800. fPrecision := glBitmapRec4ub( 4, 4, 4, 0);
  2801. fShift := glBitmapRec4ub( 0, 4, 8, 0);
  2802. {$IFNDEF OPENGL_ES}
  2803. fOpenGLFormat := tfXBGR4us1;
  2804. fglFormat := GL_RGBA; //GL_INVALID_OPERATION if not GL_BGRA or GL_RGBA
  2805. fglInternalFormat := GL_RGB4;
  2806. fglDataFormat := GL_UNSIGNED_SHORT_4_4_4_4_REV;
  2807. {$ELSE}
  2808. fOpenGLFormat := tfR5G6B5us1;
  2809. {$ENDIF}
  2810. end;
  2811. procedure TfdB5G6R5us1.SetValues;
  2812. begin
  2813. inherited SetValues;
  2814. fBitsPerPixel := 16;
  2815. fFormat := tfB5G6R5us1;
  2816. fWithAlpha := tfBGR5A1us1;
  2817. fWithoutAlpha := tfB5G6R5us1;
  2818. fRGBInverted := tfR5G6B5us1;
  2819. fPrecision := glBitmapRec4ub( 5, 6, 5, 0);
  2820. fShift := glBitmapRec4ub( 0, 5, 11, 0);
  2821. {$IFNDEF OPENGL_ES}
  2822. fOpenGLFormat := tfB5G6R5us1;
  2823. fglFormat := GL_RGB;
  2824. fglInternalFormat := GL_RGB565;
  2825. fglDataFormat := GL_UNSIGNED_SHORT_5_6_5_REV;
  2826. {$ELSE}
  2827. fOpenGLFormat := tfR5G6B5us1;
  2828. {$ENDIF}
  2829. end;
  2830. procedure TfdBGR5X1us1.SetValues;
  2831. begin
  2832. inherited SetValues;
  2833. fBitsPerPixel := 16;
  2834. fFormat := tfBGR5X1us1;
  2835. fWithAlpha := tfBGR5A1us1;
  2836. fWithoutAlpha := tfBGR5X1us1;
  2837. fRGBInverted := tfRGB5X1us1;
  2838. fPrecision := glBitmapRec4ub( 5, 5, 5, 0);
  2839. fShift := glBitmapRec4ub( 1, 6, 11, 0);
  2840. {$IFNDEF OPENGL_ES}
  2841. fOpenGLFormat := tfBGR5X1us1;
  2842. fglFormat := GL_BGRA;
  2843. fglInternalFormat := GL_RGB5;
  2844. fglDataFormat := GL_UNSIGNED_SHORT_5_5_5_1;
  2845. {$ELSE}
  2846. fOpenGLFormat := tfR5G6B5us1;
  2847. {$ENDIF}
  2848. end;
  2849. procedure TfdX1BGR5us1.SetValues;
  2850. begin
  2851. inherited SetValues;
  2852. fBitsPerPixel := 16;
  2853. fFormat := tfX1BGR5us1;
  2854. fWithAlpha := tfA1BGR5us1;
  2855. fWithoutAlpha := tfX1BGR5us1;
  2856. fRGBInverted := tfX1RGB5us1;
  2857. fPrecision := glBitmapRec4ub( 5, 5, 5, 0);
  2858. fShift := glBitmapRec4ub( 0, 5, 10, 0);
  2859. {$IFNDEF OPENGL_ES}
  2860. fOpenGLFormat := tfX1BGR5us1;
  2861. fglFormat := GL_RGBA; //GL_INVALID_OPERATION if not GL_BGRA or GL_RGBA
  2862. fglInternalFormat := GL_RGB5;
  2863. fglDataFormat := GL_UNSIGNED_SHORT_1_5_5_5_REV;
  2864. {$ELSE}
  2865. fOpenGLFormat := tfR5G6B5us1;
  2866. {$ENDIF}
  2867. end;
  2868. procedure TfdBGR8ub3.SetValues;
  2869. begin
  2870. inherited SetValues;
  2871. fBitsPerPixel := 24;
  2872. fFormat := tfBGR8ub3;
  2873. fWithAlpha := tfBGRA8ub4;
  2874. fWithoutAlpha := tfBGR8ub3;
  2875. fRGBInverted := tfRGB8ub3;
  2876. fPrecision := glBitmapRec4ub( 8, 8, 8, 0);
  2877. fShift := glBitmapRec4ub(16, 8, 0, 0);
  2878. {$IFNDEF OPENGL_ES}
  2879. fOpenGLFormat := tfBGR8ub3;
  2880. fglFormat := GL_BGR;
  2881. fglInternalFormat := GL_RGB8;
  2882. fglDataFormat := GL_UNSIGNED_BYTE;
  2883. {$ELSE}
  2884. fOpenGLFormat := tfRGB8ub3;
  2885. {$ENDIF}
  2886. end;
  2887. procedure TfdBGRX8ui1.SetValues;
  2888. begin
  2889. inherited SetValues;
  2890. fBitsPerPixel := 32;
  2891. fFormat := tfBGRX8ui1;
  2892. fWithAlpha := tfBGRA8ui1;
  2893. fWithoutAlpha := tfBGRX8ui1;
  2894. fRGBInverted := tfRGBX8ui1;
  2895. fPrecision := glBitmapRec4ub( 8, 8, 8, 0);
  2896. fShift := glBitmapRec4ub( 8, 16, 24, 0);
  2897. {$IFNDEF OPENGL_ES}
  2898. fOpenGLFormat := tfBGRX8ui1;
  2899. fglFormat := GL_BGRA; //GL_INVALID_OPERATION if not GL_BGRA or GL_RGBA
  2900. fglInternalFormat := GL_RGB8;
  2901. fglDataFormat := GL_UNSIGNED_INT_8_8_8_8;
  2902. {$ELSE}
  2903. fOpenGLFormat := tfRGB8ub3;
  2904. {$ENDIF}
  2905. end;
  2906. procedure TfdXBGR8ui1.SetValues;
  2907. begin
  2908. inherited SetValues;
  2909. fBitsPerPixel := 32;
  2910. fFormat := tfXBGR8ui1;
  2911. fWithAlpha := tfABGR8ui1;
  2912. fWithoutAlpha := tfXBGR8ui1;
  2913. fRGBInverted := tfXRGB8ui1;
  2914. fPrecision := glBitmapRec4ub( 8, 8, 8, 0);
  2915. fShift := glBitmapRec4ub( 0, 8, 16, 0);
  2916. {$IFNDEF OPENGL_ES}
  2917. fOpenGLFormat := tfXBGR8ui1;
  2918. fglFormat := GL_RGBA; //GL_INVALID_OPERATION if not GL_BGRA or GL_RGBA
  2919. fglInternalFormat := GL_RGB8;
  2920. fglDataFormat := GL_UNSIGNED_INT_8_8_8_8_REV;
  2921. {$ELSE}
  2922. fOpenGLFormat := tfRGB8ub3;
  2923. {$ENDIF}
  2924. end;
  2925. procedure TfdBGR10X2ui1.SetValues;
  2926. begin
  2927. inherited SetValues;
  2928. fBitsPerPixel := 32;
  2929. fFormat := tfBGR10X2ui1;
  2930. fWithAlpha := tfBGR10A2ui1;
  2931. fWithoutAlpha := tfBGR10X2ui1;
  2932. fRGBInverted := tfRGB10X2ui1;
  2933. fPrecision := glBitmapRec4ub(10, 10, 10, 0);
  2934. fShift := glBitmapRec4ub( 2, 12, 22, 0);
  2935. {$IFNDEF OPENGL_ES}
  2936. fOpenGLFormat := tfBGR10X2ui1;
  2937. fglFormat := GL_BGRA; //GL_INVALID_OPERATION if not GL_BGRA or GL_RGBA
  2938. fglInternalFormat := GL_RGB10;
  2939. fglDataFormat := GL_UNSIGNED_INT_10_10_10_2;
  2940. {$ELSE}
  2941. fOpenGLFormat := tfRGB16us3;
  2942. {$ENDIF}
  2943. end;
  2944. procedure TfdX2BGR10ui1.SetValues;
  2945. begin
  2946. inherited SetValues;
  2947. fBitsPerPixel := 32;
  2948. fFormat := tfX2BGR10ui1;
  2949. fWithAlpha := tfA2BGR10ui1;
  2950. fWithoutAlpha := tfX2BGR10ui1;
  2951. fRGBInverted := tfX2RGB10ui1;
  2952. fPrecision := glBitmapRec4ub(10, 10, 10, 0);
  2953. fShift := glBitmapRec4ub( 0, 10, 20, 0);
  2954. {$IFNDEF OPENGL_ES}
  2955. fOpenGLFormat := tfX2BGR10ui1;
  2956. fglFormat := GL_RGBA; //GL_INVALID_OPERATION if not GL_BGRA or GL_RGBA
  2957. fglInternalFormat := GL_RGB10;
  2958. fglDataFormat := GL_UNSIGNED_INT_2_10_10_10_REV;
  2959. {$ELSE}
  2960. fOpenGLFormat := tfRGB16us3;
  2961. {$ENDIF}
  2962. end;
  2963. procedure TfdBGR16us3.SetValues;
  2964. begin
  2965. inherited SetValues;
  2966. fBitsPerPixel := 48;
  2967. fFormat := tfBGR16us3;
  2968. fWithAlpha := tfBGRA16us4;
  2969. fWithoutAlpha := tfBGR16us3;
  2970. fRGBInverted := tfRGB16us3;
  2971. fPrecision := glBitmapRec4ub(16, 16, 16, 0);
  2972. fShift := glBitmapRec4ub(32, 16, 0, 0);
  2973. {$IFNDEF OPENGL_ES}
  2974. fOpenGLFormat := tfBGR16us3;
  2975. fglFormat := GL_BGR;
  2976. fglInternalFormat := GL_RGB16;
  2977. fglDataFormat := GL_UNSIGNED_SHORT;
  2978. {$ELSE}
  2979. fOpenGLFormat := tfRGB16us3;
  2980. {$ENDIF}
  2981. end;
  2982. procedure TfdBGRA4us1.SetValues;
  2983. begin
  2984. inherited SetValues;
  2985. fBitsPerPixel := 16;
  2986. fFormat := tfBGRA4us1;
  2987. fWithAlpha := tfBGRA4us1;
  2988. fWithoutAlpha := tfBGRX4us1;
  2989. fRGBInverted := tfRGBA4us1;
  2990. fPrecision := glBitmapRec4ub( 4, 4, 4, 4);
  2991. fShift := glBitmapRec4ub( 4, 8, 12, 0);
  2992. {$IFNDEF OPENGL_ES}
  2993. fOpenGLFormat := tfBGRA4us1;
  2994. fglFormat := GL_BGRA;
  2995. fglInternalFormat := GL_RGBA4;
  2996. fglDataFormat := GL_UNSIGNED_SHORT_4_4_4_4;
  2997. {$ELSE}
  2998. fOpenGLFormat := tfRGBA4us1;
  2999. {$ENDIF}
  3000. end;
  3001. procedure TfdABGR4us1.SetValues;
  3002. begin
  3003. inherited SetValues;
  3004. fBitsPerPixel := 16;
  3005. fFormat := tfABGR4us1;
  3006. fWithAlpha := tfABGR4us1;
  3007. fWithoutAlpha := tfXBGR4us1;
  3008. fRGBInverted := tfARGB4us1;
  3009. fPrecision := glBitmapRec4ub( 4, 4, 4, 4);
  3010. fShift := glBitmapRec4ub( 0, 4, 8, 12);
  3011. {$IFNDEF OPENGL_ES}
  3012. fOpenGLFormat := tfABGR4us1;
  3013. fglFormat := GL_RGBA;
  3014. fglInternalFormat := GL_RGBA4;
  3015. fglDataFormat := GL_UNSIGNED_SHORT_4_4_4_4_REV;
  3016. {$ELSE}
  3017. fOpenGLFormat := tfRGBA4us1;
  3018. {$ENDIF}
  3019. end;
  3020. procedure TfdBGR5A1us1.SetValues;
  3021. begin
  3022. inherited SetValues;
  3023. fBitsPerPixel := 16;
  3024. fFormat := tfBGR5A1us1;
  3025. fWithAlpha := tfBGR5A1us1;
  3026. fWithoutAlpha := tfBGR5X1us1;
  3027. fRGBInverted := tfRGB5A1us1;
  3028. fPrecision := glBitmapRec4ub( 5, 5, 5, 1);
  3029. fShift := glBitmapRec4ub( 1, 6, 11, 0);
  3030. {$IFNDEF OPENGL_ES}
  3031. fOpenGLFormat := tfBGR5A1us1;
  3032. fglFormat := GL_BGRA;
  3033. fglInternalFormat := GL_RGB5_A1;
  3034. fglDataFormat := GL_UNSIGNED_SHORT_5_5_5_1;
  3035. {$ELSE}
  3036. fOpenGLFormat := tfRGB5A1us1;
  3037. {$ENDIF}
  3038. end;
  3039. procedure TfdA1BGR5us1.SetValues;
  3040. begin
  3041. inherited SetValues;
  3042. fBitsPerPixel := 16;
  3043. fFormat := tfA1BGR5us1;
  3044. fWithAlpha := tfA1BGR5us1;
  3045. fWithoutAlpha := tfX1BGR5us1;
  3046. fRGBInverted := tfA1RGB5us1;
  3047. fPrecision := glBitmapRec4ub( 5, 5, 5, 1);
  3048. fShift := glBitmapRec4ub( 0, 5, 10, 15);
  3049. {$IFNDEF OPENGL_ES}
  3050. fOpenGLFormat := tfA1BGR5us1;
  3051. fglFormat := GL_RGBA;
  3052. fglInternalFormat := GL_RGB5_A1;
  3053. fglDataFormat := GL_UNSIGNED_SHORT_1_5_5_5_REV;
  3054. {$ELSE}
  3055. fOpenGLFormat := tfRGB5A1us1;
  3056. {$ENDIF}
  3057. end;
  3058. procedure TfdBGRA8ui1.SetValues;
  3059. begin
  3060. inherited SetValues;
  3061. fBitsPerPixel := 32;
  3062. fFormat := tfBGRA8ui1;
  3063. fWithAlpha := tfBGRA8ui1;
  3064. fWithoutAlpha := tfBGRX8ui1;
  3065. fRGBInverted := tfRGBA8ui1;
  3066. fPrecision := glBitmapRec4ub( 8, 8, 8, 8);
  3067. fShift := glBitmapRec4ub( 8, 16, 24, 0);
  3068. {$IFNDEF OPENGL_ES}
  3069. fOpenGLFormat := tfBGRA8ui1;
  3070. fglFormat := GL_BGRA;
  3071. fglInternalFormat := GL_RGBA8;
  3072. fglDataFormat := GL_UNSIGNED_INT_8_8_8_8;
  3073. {$ELSE}
  3074. fOpenGLFormat := tfRGBA8ub4;
  3075. {$ENDIF}
  3076. end;
  3077. procedure TfdABGR8ui1.SetValues;
  3078. begin
  3079. inherited SetValues;
  3080. fBitsPerPixel := 32;
  3081. fFormat := tfABGR8ui1;
  3082. fWithAlpha := tfABGR8ui1;
  3083. fWithoutAlpha := tfXBGR8ui1;
  3084. fRGBInverted := tfARGB8ui1;
  3085. fPrecision := glBitmapRec4ub( 8, 8, 8, 8);
  3086. fShift := glBitmapRec4ub( 0, 8, 16, 24);
  3087. {$IFNDEF OPENGL_ES}
  3088. fOpenGLFormat := tfABGR8ui1;
  3089. fglFormat := GL_RGBA;
  3090. fglInternalFormat := GL_RGBA8;
  3091. fglDataFormat := GL_UNSIGNED_INT_8_8_8_8_REV;
  3092. {$ELSE}
  3093. fOpenGLFormat := tfRGBA8ub4
  3094. {$ENDIF}
  3095. end;
  3096. procedure TfdBGRA8ub4.SetValues;
  3097. begin
  3098. inherited SetValues;
  3099. fBitsPerPixel := 32;
  3100. fFormat := tfBGRA8ub4;
  3101. fWithAlpha := tfBGRA8ub4;
  3102. fWithoutAlpha := tfBGR8ub3;
  3103. fRGBInverted := tfRGBA8ub4;
  3104. fPrecision := glBitmapRec4ub( 8, 8, 8, 8);
  3105. fShift := glBitmapRec4ub(16, 8, 0, 24);
  3106. {$IFNDEF OPENGL_ES}
  3107. fOpenGLFormat := tfBGRA8ub4;
  3108. fglFormat := GL_BGRA;
  3109. fglInternalFormat := GL_RGBA8;
  3110. fglDataFormat := GL_UNSIGNED_BYTE;
  3111. {$ELSE}
  3112. fOpenGLFormat := tfRGBA8ub4;
  3113. {$ENDIF}
  3114. end;
  3115. procedure TfdBGR10A2ui1.SetValues;
  3116. begin
  3117. inherited SetValues;
  3118. fBitsPerPixel := 32;
  3119. fFormat := tfBGR10A2ui1;
  3120. fWithAlpha := tfBGR10A2ui1;
  3121. fWithoutAlpha := tfBGR10X2ui1;
  3122. fRGBInverted := tfRGB10A2ui1;
  3123. fPrecision := glBitmapRec4ub(10, 10, 10, 2);
  3124. fShift := glBitmapRec4ub( 2, 12, 22, 0);
  3125. {$IFNDEF OPENGL_ES}
  3126. fOpenGLFormat := tfBGR10A2ui1;
  3127. fglFormat := GL_BGRA;
  3128. fglInternalFormat := GL_RGB10_A2;
  3129. fglDataFormat := GL_UNSIGNED_INT_10_10_10_2;
  3130. {$ELSE}
  3131. fOpenGLFormat := tfA2RGB10ui1;
  3132. {$ENDIF}
  3133. end;
  3134. procedure TfdA2BGR10ui1.SetValues;
  3135. begin
  3136. inherited SetValues;
  3137. fBitsPerPixel := 32;
  3138. fFormat := tfA2BGR10ui1;
  3139. fWithAlpha := tfA2BGR10ui1;
  3140. fWithoutAlpha := tfX2BGR10ui1;
  3141. fRGBInverted := tfA2RGB10ui1;
  3142. fPrecision := glBitmapRec4ub(10, 10, 10, 2);
  3143. fShift := glBitmapRec4ub( 0, 10, 20, 30);
  3144. {$IFNDEF OPENGL_ES}
  3145. fOpenGLFormat := tfA2BGR10ui1;
  3146. fglFormat := GL_RGBA;
  3147. fglInternalFormat := GL_RGB10_A2;
  3148. fglDataFormat := GL_UNSIGNED_INT_2_10_10_10_REV;
  3149. {$ELSE}
  3150. fOpenGLFormat := tfA2RGB10ui1;
  3151. {$ENDIF}
  3152. end;
  3153. procedure TfdBGRA16us4.SetValues;
  3154. begin
  3155. inherited SetValues;
  3156. fBitsPerPixel := 64;
  3157. fFormat := tfBGRA16us4;
  3158. fWithAlpha := tfBGRA16us4;
  3159. fWithoutAlpha := tfBGR16us3;
  3160. fRGBInverted := tfRGBA16us4;
  3161. fPrecision := glBitmapRec4ub(16, 16, 16, 16);
  3162. fShift := glBitmapRec4ub(32, 16, 0, 48);
  3163. {$IFNDEF OPENGL_ES}
  3164. fOpenGLFormat := tfBGRA16us4;
  3165. fglFormat := GL_BGRA;
  3166. fglInternalFormat := GL_RGBA16;
  3167. fglDataFormat := GL_UNSIGNED_SHORT;
  3168. {$ELSE}
  3169. fOpenGLFormat := tfRGBA16us4;
  3170. {$ENDIF}
  3171. end;
  3172. procedure TfdDepth16us1.SetValues;
  3173. begin
  3174. inherited SetValues;
  3175. fBitsPerPixel := 16;
  3176. fFormat := tfDepth16us1;
  3177. fWithoutAlpha := tfDepth16us1;
  3178. fPrecision := glBitmapRec4ub(16, 16, 16, 16);
  3179. fShift := glBitmapRec4ub( 0, 0, 0, 0);
  3180. {$IF NOT DEFINED (OPENGL_ES) OR DEFINED(OPENGL_ES_2_0)}
  3181. fOpenGLFormat := tfDepth16us1;
  3182. fglFormat := GL_DEPTH_COMPONENT;
  3183. fglInternalFormat := GL_DEPTH_COMPONENT16;
  3184. fglDataFormat := GL_UNSIGNED_SHORT;
  3185. {$IFEND}
  3186. end;
  3187. procedure TfdDepth24ui1.SetValues;
  3188. begin
  3189. inherited SetValues;
  3190. fBitsPerPixel := 32;
  3191. fFormat := tfDepth24ui1;
  3192. fWithoutAlpha := tfDepth24ui1;
  3193. fOpenGLFormat := tfDepth24ui1;
  3194. fPrecision := glBitmapRec4ub(32, 32, 32, 32);
  3195. fShift := glBitmapRec4ub( 0, 0, 0, 0);
  3196. {$IF NOT DEFINED (OPENGL_ES) OR DEFINED(OPENGL_ES_3_0)}
  3197. fOpenGLFormat := tfDepth24ui1;
  3198. fglFormat := GL_DEPTH_COMPONENT;
  3199. fglInternalFormat := GL_DEPTH_COMPONENT24;
  3200. fglDataFormat := GL_UNSIGNED_INT;
  3201. {$IFEND}
  3202. end;
  3203. procedure TfdDepth32ui1.SetValues;
  3204. begin
  3205. inherited SetValues;
  3206. fBitsPerPixel := 32;
  3207. fFormat := tfDepth32ui1;
  3208. fWithoutAlpha := tfDepth32ui1;
  3209. fPrecision := glBitmapRec4ub(32, 32, 32, 32);
  3210. fShift := glBitmapRec4ub( 0, 0, 0, 0);
  3211. {$IF NOT DEFINED(OPENGL_ES)}
  3212. fOpenGLFormat := tfDepth32ui1;
  3213. fglFormat := GL_DEPTH_COMPONENT;
  3214. fglInternalFormat := GL_DEPTH_COMPONENT32;
  3215. fglDataFormat := GL_UNSIGNED_INT;
  3216. {$ELSEIF DEFINED(OPENGL_ES_3_0)}
  3217. fOpenGLFormat := tfDepth24ui1;
  3218. {$ELSEIF DEFINED(OPENGL_ES_2_0)}
  3219. fOpenGLFormat := tfDepth16us1;
  3220. {$IFEND}
  3221. end;
  3222. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3223. //TfdS3tcDtx1RGBA/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3224. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3225. procedure TfdS3tcDtx1RGBA.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  3226. begin
  3227. raise EglBitmap.Create('mapping for compressed formats is not supported');
  3228. end;
  3229. procedure TfdS3tcDtx1RGBA.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  3230. begin
  3231. raise EglBitmap.Create('mapping for compressed formats is not supported');
  3232. end;
  3233. procedure TfdS3tcDtx1RGBA.SetValues;
  3234. begin
  3235. inherited SetValues;
  3236. fFormat := tfS3tcDtx1RGBA;
  3237. fWithAlpha := tfS3tcDtx1RGBA;
  3238. fUncompressed := tfRGB5A1us1;
  3239. fBitsPerPixel := 4;
  3240. fIsCompressed := true;
  3241. {$IFNDEF OPENGL_ES}
  3242. fOpenGLFormat := tfS3tcDtx1RGBA;
  3243. fglFormat := GL_COMPRESSED_RGBA;
  3244. fglInternalFormat := GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
  3245. fglDataFormat := GL_UNSIGNED_BYTE;
  3246. {$ELSE}
  3247. fOpenGLFormat := fUncompressed;
  3248. {$ENDIF}
  3249. end;
  3250. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3251. //TfdS3tcDtx3RGBA/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3252. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3253. procedure TfdS3tcDtx3RGBA.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  3254. begin
  3255. raise EglBitmap.Create('mapping for compressed formats is not supported');
  3256. end;
  3257. procedure TfdS3tcDtx3RGBA.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  3258. begin
  3259. raise EglBitmap.Create('mapping for compressed formats is not supported');
  3260. end;
  3261. procedure TfdS3tcDtx3RGBA.SetValues;
  3262. begin
  3263. inherited SetValues;
  3264. fFormat := tfS3tcDtx3RGBA;
  3265. fWithAlpha := tfS3tcDtx3RGBA;
  3266. fUncompressed := tfRGBA8ub4;
  3267. fBitsPerPixel := 8;
  3268. fIsCompressed := true;
  3269. {$IFNDEF OPENGL_ES}
  3270. fOpenGLFormat := tfS3tcDtx3RGBA;
  3271. fglFormat := GL_COMPRESSED_RGBA;
  3272. fglInternalFormat := GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
  3273. fglDataFormat := GL_UNSIGNED_BYTE;
  3274. {$ELSE}
  3275. fOpenGLFormat := fUncompressed;
  3276. {$ENDIF}
  3277. end;
  3278. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3279. //TfdS3tcDtx5RGBA/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3280. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3281. procedure TfdS3tcDtx5RGBA.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  3282. begin
  3283. raise EglBitmap.Create('mapping for compressed formats is not supported');
  3284. end;
  3285. procedure TfdS3tcDtx5RGBA.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  3286. begin
  3287. raise EglBitmap.Create('mapping for compressed formats is not supported');
  3288. end;
  3289. procedure TfdS3tcDtx5RGBA.SetValues;
  3290. begin
  3291. inherited SetValues;
  3292. fFormat := tfS3tcDtx3RGBA;
  3293. fWithAlpha := tfS3tcDtx3RGBA;
  3294. fUncompressed := tfRGBA8ub4;
  3295. fBitsPerPixel := 8;
  3296. fIsCompressed := true;
  3297. {$IFNDEF OPENGL_ES}
  3298. fOpenGLFormat := tfS3tcDtx3RGBA;
  3299. fglFormat := GL_COMPRESSED_RGBA;
  3300. fglInternalFormat := GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
  3301. fglDataFormat := GL_UNSIGNED_BYTE;
  3302. {$ELSE}
  3303. fOpenGLFormat := fUncompressed;
  3304. {$ENDIF}
  3305. end;
  3306. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3307. //TglBitmapFormatDescriptor///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3308. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3309. function TglBitmapFormatDescriptor.GetHasRed: Boolean;
  3310. begin
  3311. result := (fPrecision.r > 0);
  3312. end;
  3313. function TglBitmapFormatDescriptor.GetHasGreen: Boolean;
  3314. begin
  3315. result := (fPrecision.g > 0);
  3316. end;
  3317. function TglBitmapFormatDescriptor.GetHasBlue: Boolean;
  3318. begin
  3319. result := (fPrecision.b > 0);
  3320. end;
  3321. function TglBitmapFormatDescriptor.GetHasAlpha: Boolean;
  3322. begin
  3323. result := (fPrecision.a > 0);
  3324. end;
  3325. function TglBitmapFormatDescriptor.GetHasColor: Boolean;
  3326. begin
  3327. result := HasRed or HasGreen or HasBlue;
  3328. end;
  3329. function TglBitmapFormatDescriptor.GetIsGrayscale: Boolean;
  3330. begin
  3331. result := (Mask.r = Mask.g) and (Mask.g = Mask.b) and (Mask.r > 0);
  3332. end;
  3333. function TglBitmapFormatDescriptor.GetHasOpenGLSupport: Boolean;
  3334. begin
  3335. result := (OpenGLFormat = Format);
  3336. end;
  3337. procedure TglBitmapFormatDescriptor.SetValues;
  3338. begin
  3339. fFormat := tfEmpty;
  3340. fWithAlpha := tfEmpty;
  3341. fWithoutAlpha := tfEmpty;
  3342. fOpenGLFormat := tfEmpty;
  3343. fRGBInverted := tfEmpty;
  3344. fUncompressed := tfEmpty;
  3345. fBitsPerPixel := 0;
  3346. fIsCompressed := false;
  3347. fglFormat := 0;
  3348. fglInternalFormat := 0;
  3349. fglDataFormat := 0;
  3350. FillChar(fPrecision, 0, SizeOf(fPrecision));
  3351. FillChar(fShift, 0, SizeOf(fShift));
  3352. end;
  3353. procedure TglBitmapFormatDescriptor.CalcValues;
  3354. var
  3355. i: Integer;
  3356. begin
  3357. fBytesPerPixel := fBitsPerPixel / 8;
  3358. fChannelCount := 0;
  3359. for i := 0 to 3 do begin
  3360. if (fPrecision.arr[i] > 0) then
  3361. inc(fChannelCount);
  3362. fRange.arr[i] := (1 shl fPrecision.arr[i]) - 1;
  3363. fMask.arr[i] := fRange.arr[i] shl fShift.arr[i];
  3364. end;
  3365. end;
  3366. function TglBitmapFormatDescriptor.GetSize(const aSize: TglBitmapSize): Integer;
  3367. var
  3368. w, h: Integer;
  3369. begin
  3370. if (ffX in aSize.Fields) or (ffY in aSize.Fields) then begin
  3371. w := Max(1, aSize.X);
  3372. h := Max(1, aSize.Y);
  3373. result := GetSize(w, h);
  3374. end else
  3375. result := 0;
  3376. end;
  3377. function TglBitmapFormatDescriptor.GetSize(const aWidth, aHeight: Integer): Integer;
  3378. begin
  3379. result := 0;
  3380. if (aWidth <= 0) or (aHeight <= 0) then
  3381. exit;
  3382. result := Ceil(aWidth * aHeight * BytesPerPixel);
  3383. end;
  3384. constructor TglBitmapFormatDescriptor.Create;
  3385. begin
  3386. inherited Create;
  3387. SetValues;
  3388. CalcValues;
  3389. end;
  3390. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3391. class function TglBitmapFormatDescriptor.GetByFormat(const aInternalFormat: GLenum): TglBitmapFormatDescriptor;
  3392. var
  3393. f: TglBitmapFormat;
  3394. begin
  3395. for f := Low(TglBitmapFormat) to High(TglBitmapFormat) do begin
  3396. result := TFormatDescriptor.Get(f);
  3397. if (result.glInternalFormat = aInternalFormat) then
  3398. exit;
  3399. end;
  3400. result := TFormatDescriptor.Get(tfEmpty);
  3401. end;
  3402. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3403. class function TglBitmapFormatDescriptor.GetByFormat(const aFormat: TglBitmapFormat): TglBitmapFormatDescriptor;
  3404. begin
  3405. result := TFormatDescriptor.Get(aFormat);
  3406. if not Assigned(result) then
  3407. result := TFormatDescriptor.Get(tfEmpty);
  3408. end;
  3409. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3410. //TFormatDescriptor///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3411. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3412. class procedure TFormatDescriptor.Init;
  3413. begin
  3414. if not Assigned(FormatDescriptorCS) then
  3415. FormatDescriptorCS := TCriticalSection.Create;
  3416. end;
  3417. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3418. class function TFormatDescriptor.Get(const aFormat: TglBitmapFormat): TFormatDescriptor;
  3419. begin
  3420. FormatDescriptorCS.Enter;
  3421. try
  3422. result := FormatDescriptors[aFormat];
  3423. if not Assigned(result) then begin
  3424. result := FORMAT_DESCRIPTOR_CLASSES[aFormat].Create;
  3425. FormatDescriptors[aFormat] := result;
  3426. end;
  3427. finally
  3428. FormatDescriptorCS.Leave;
  3429. end;
  3430. end;
  3431. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3432. class function TFormatDescriptor.GetAlpha(const aFormat: TglBitmapFormat): TFormatDescriptor;
  3433. begin
  3434. result := Get(Get(aFormat).WithAlpha);
  3435. end;
  3436. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3437. class function TFormatDescriptor.GetFromMask(const aMask: TglBitmapRec4ul; const aBitCount: Integer): TFormatDescriptor;
  3438. var
  3439. ft: TglBitmapFormat;
  3440. begin
  3441. // find matching format with OpenGL support
  3442. for ft := High(TglBitmapFormat) downto Low(TglBitmapFormat) do begin
  3443. result := Get(ft);
  3444. if (result.MaskMatch(aMask)) and
  3445. (result.glFormat <> 0) and
  3446. (result.glInternalFormat <> 0) and
  3447. ((aBitCount = 0) or (aBitCount = result.BitsPerPixel))
  3448. then
  3449. exit;
  3450. end;
  3451. // find matching format without OpenGL Support
  3452. for ft := High(TglBitmapFormat) downto Low(TglBitmapFormat) do begin
  3453. result := Get(ft);
  3454. if result.MaskMatch(aMask) and ((aBitCount = 0) or (aBitCount = result.BitsPerPixel)) then
  3455. exit;
  3456. end;
  3457. result := TFormatDescriptor.Get(tfEmpty);
  3458. end;
  3459. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3460. class function TFormatDescriptor.GetFromPrecShift(const aPrec, aShift: TglBitmapRec4ub; const aBitCount: Integer): TFormatDescriptor;
  3461. var
  3462. ft: TglBitmapFormat;
  3463. begin
  3464. // find matching format with OpenGL support
  3465. for ft := High(TglBitmapFormat) downto Low(TglBitmapFormat) do begin
  3466. result := Get(ft);
  3467. if glBitmapRec4ubCompare(result.Shift, aShift) and
  3468. glBitmapRec4ubCompare(result.Precision, aPrec) and
  3469. (result.glFormat <> 0) and
  3470. (result.glInternalFormat <> 0) and
  3471. ((aBitCount = 0) or (aBitCount = result.BitsPerPixel))
  3472. then
  3473. exit;
  3474. end;
  3475. // find matching format without OpenGL Support
  3476. for ft := High(TglBitmapFormat) downto Low(TglBitmapFormat) do begin
  3477. result := Get(ft);
  3478. if glBitmapRec4ubCompare(result.Shift, aShift) and
  3479. glBitmapRec4ubCompare(result.Precision, aPrec) and
  3480. ((aBitCount = 0) or (aBitCount = result.BitsPerPixel)) then
  3481. exit;
  3482. end;
  3483. result := TFormatDescriptor.Get(tfEmpty);
  3484. end;
  3485. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3486. class procedure TFormatDescriptor.Clear;
  3487. var
  3488. f: TglBitmapFormat;
  3489. begin
  3490. FormatDescriptorCS.Enter;
  3491. try
  3492. for f := low(FormatDescriptors) to high(FormatDescriptors) do
  3493. FreeAndNil(FormatDescriptors[f]);
  3494. finally
  3495. FormatDescriptorCS.Leave;
  3496. end;
  3497. end;
  3498. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3499. class procedure TFormatDescriptor.Finalize;
  3500. begin
  3501. Clear;
  3502. FreeAndNil(FormatDescriptorCS);
  3503. end;
  3504. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3505. //TBitfieldFormat/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3506. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3507. procedure TbmpBitfieldFormat.SetCustomValues(const aBPP: Integer; aMask: TglBitmapRec4ul);
  3508. var
  3509. i: Integer;
  3510. begin
  3511. for i := 0 to 3 do begin
  3512. fShift.arr[i] := 0;
  3513. while (aMask.arr[i] > 0) and ((aMask.arr[i] and 1) = 0) do begin
  3514. aMask.arr[i] := aMask.arr[i] shr 1;
  3515. inc(fShift.arr[i]);
  3516. end;
  3517. fPrecision.arr[i] := CountSetBits(aMask.arr[i]);
  3518. end;
  3519. fBitsPerPixel := aBPP;
  3520. CalcValues;
  3521. end;
  3522. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3523. procedure TbmpBitfieldFormat.SetCustomValues(const aBBP: Integer; const aPrec, aShift: TglBitmapRec4ub);
  3524. begin
  3525. fBitsPerPixel := aBBP;
  3526. fPrecision := aPrec;
  3527. fShift := aShift;
  3528. CalcValues;
  3529. end;
  3530. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3531. procedure TbmpBitfieldFormat.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  3532. var
  3533. data: QWord;
  3534. begin
  3535. data :=
  3536. ((aPixel.Data.r and Range.r) shl Shift.r) or
  3537. ((aPixel.Data.g and Range.g) shl Shift.g) or
  3538. ((aPixel.Data.b and Range.b) shl Shift.b) or
  3539. ((aPixel.Data.a and Range.a) shl Shift.a);
  3540. case BitsPerPixel of
  3541. 8: aData^ := data;
  3542. 16: PWord(aData)^ := data;
  3543. 32: PCardinal(aData)^ := data;
  3544. 64: PQWord(aData)^ := data;
  3545. else
  3546. raise EglBitmap.CreateFmt('invalid pixel size: %d', [BitsPerPixel]);
  3547. end;
  3548. inc(aData, Round(BytesPerPixel));
  3549. end;
  3550. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3551. procedure TbmpBitfieldFormat.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  3552. var
  3553. data: QWord;
  3554. i: Integer;
  3555. begin
  3556. case BitsPerPixel of
  3557. 8: data := aData^;
  3558. 16: data := PWord(aData)^;
  3559. 32: data := PCardinal(aData)^;
  3560. 64: data := PQWord(aData)^;
  3561. else
  3562. raise EglBitmap.CreateFmt('invalid pixel size: %d', [BitsPerPixel]);
  3563. end;
  3564. for i := 0 to 3 do
  3565. aPixel.Data.arr[i] := (data shr fShift.arr[i]) and Range.arr[i];
  3566. inc(aData, Round(BytesPerPixel));
  3567. end;
  3568. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3569. //TColorTableFormat///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3570. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3571. procedure TbmpColorTableFormat.SetValues;
  3572. begin
  3573. inherited SetValues;
  3574. fShift := glBitmapRec4ub(8, 8, 8, 0);
  3575. end;
  3576. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3577. procedure TbmpColorTableFormat.SetCustomValues(const aFormat: TglBitmapFormat; const aBPP: Integer; const aPrec, aShift: TglBitmapRec4ub);
  3578. begin
  3579. fFormat := aFormat;
  3580. fBitsPerPixel := aBPP;
  3581. fPrecision := aPrec;
  3582. fShift := aShift;
  3583. CalcValues;
  3584. end;
  3585. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3586. procedure TbmpColorTableFormat.CalcValues;
  3587. begin
  3588. inherited CalcValues;
  3589. end;
  3590. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3591. procedure TbmpColorTableFormat.CreateColorTable;
  3592. var
  3593. i: Integer;
  3594. begin
  3595. SetLength(fColorTable, 256);
  3596. if not HasColor then begin
  3597. // alpha
  3598. for i := 0 to High(fColorTable) do begin
  3599. fColorTable[i].r := Round(((i shr Shift.a) and Range.a) / Range.a * 255);
  3600. fColorTable[i].g := Round(((i shr Shift.a) and Range.a) / Range.a * 255);
  3601. fColorTable[i].b := Round(((i shr Shift.a) and Range.a) / Range.a * 255);
  3602. fColorTable[i].a := 0;
  3603. end;
  3604. end else begin
  3605. // normal
  3606. for i := 0 to High(fColorTable) do begin
  3607. fColorTable[i].r := Round(((i shr Shift.r) and Range.r) / Range.r * 255);
  3608. fColorTable[i].g := Round(((i shr Shift.g) and Range.g) / Range.g * 255);
  3609. fColorTable[i].b := Round(((i shr Shift.b) and Range.b) / Range.b * 255);
  3610. fColorTable[i].a := 0;
  3611. end;
  3612. end;
  3613. end;
  3614. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3615. function TbmpColorTableFormat.CreateMappingData: Pointer;
  3616. begin
  3617. result := Pointer(0);
  3618. end;
  3619. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3620. procedure TbmpColorTableFormat.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  3621. begin
  3622. if (BitsPerPixel <> 8) then
  3623. raise EglBitmapUnsupportedFormat.Create('color table are only supported for 8bit formats');
  3624. if not HasColor then
  3625. // alpha
  3626. aData^ := aPixel.Data.a
  3627. else
  3628. // normal
  3629. aData^ := Round(
  3630. ((aPixel.Data.r shr Shift.r) and Range.r) * LUMINANCE_WEIGHT_R +
  3631. ((aPixel.Data.g shr Shift.g) and Range.g) * LUMINANCE_WEIGHT_G +
  3632. ((aPixel.Data.b shr Shift.b) and Range.b) * LUMINANCE_WEIGHT_B);
  3633. inc(aData);
  3634. end;
  3635. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3636. procedure TbmpColorTableFormat.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  3637. function ReadValue: Byte;
  3638. var
  3639. i: PtrUInt;
  3640. begin
  3641. if (BitsPerPixel = 8) then begin
  3642. result := aData^;
  3643. inc(aData);
  3644. end else begin
  3645. i := {%H-}PtrUInt(aMapData);
  3646. if (BitsPerPixel > 1) then
  3647. result := (aData^ shr i) and ((1 shl BitsPerPixel) - 1)
  3648. else
  3649. result := (aData^ shr (7-i)) and ((1 shl BitsPerPixel) - 1);
  3650. inc(i, BitsPerPixel);
  3651. while (i >= 8) do begin
  3652. inc(aData);
  3653. dec(i, 8);
  3654. end;
  3655. aMapData := {%H-}Pointer(i);
  3656. end;
  3657. end;
  3658. begin
  3659. if (BitsPerPixel > 8) then
  3660. raise EglBitmapUnsupportedFormat.Create('color table are only supported for 8bit formats');
  3661. with fColorTable[ReadValue] do begin
  3662. aPixel.Data.r := r;
  3663. aPixel.Data.g := g;
  3664. aPixel.Data.b := b;
  3665. aPixel.Data.a := a;
  3666. end;
  3667. end;
  3668. destructor TbmpColorTableFormat.Destroy;
  3669. begin
  3670. SetLength(fColorTable, 0);
  3671. inherited Destroy;
  3672. end;
  3673. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3674. //TglBitmap - Helper//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3675. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3676. procedure glBitmapConvertPixel(var aPixel: TglBitmapPixelData; const aSourceFD, aDestFD: TFormatDescriptor);
  3677. var
  3678. i: Integer;
  3679. begin
  3680. for i := 0 to 3 do begin
  3681. if (aSourceFD.Range.arr[i] <> aDestFD.Range.arr[i]) then begin
  3682. if (aSourceFD.Range.arr[i] > 0) then
  3683. aPixel.Data.arr[i] := Round(aPixel.Data.arr[i] / aSourceFD.Range.arr[i] * aDestFD.Range.arr[i])
  3684. else
  3685. aPixel.Data.arr[i] := 0;
  3686. end;
  3687. end;
  3688. end;
  3689. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3690. procedure glBitmapConvertCopyFunc(var aFuncRec: TglBitmapFunctionRec);
  3691. begin
  3692. with aFuncRec do begin
  3693. if (Source.Range.r > 0) then
  3694. Dest.Data.r := Source.Data.r;
  3695. if (Source.Range.g > 0) then
  3696. Dest.Data.g := Source.Data.g;
  3697. if (Source.Range.b > 0) then
  3698. Dest.Data.b := Source.Data.b;
  3699. if (Source.Range.a > 0) then
  3700. Dest.Data.a := Source.Data.a;
  3701. end;
  3702. end;
  3703. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3704. procedure glBitmapConvertCalculateRGBAFunc(var aFuncRec: TglBitmapFunctionRec);
  3705. var
  3706. i: Integer;
  3707. begin
  3708. with aFuncRec do begin
  3709. for i := 0 to 3 do
  3710. if (Source.Range.arr[i] > 0) then
  3711. Dest.Data.arr[i] := Round(Dest.Range.arr[i] * Source.Data.arr[i] / Source.Range.arr[i]);
  3712. end;
  3713. end;
  3714. type
  3715. TShiftData = packed record
  3716. case Integer of
  3717. 0: (r, g, b, a: SmallInt);
  3718. 1: (arr: array[0..3] of SmallInt);
  3719. end;
  3720. PShiftData = ^TShiftData;
  3721. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3722. procedure glBitmapConvertShiftRGBAFunc(var aFuncRec: TglBitmapFunctionRec);
  3723. var
  3724. i: Integer;
  3725. begin
  3726. with aFuncRec do
  3727. for i := 0 to 3 do
  3728. if (Source.Range.arr[i] > 0) then
  3729. Dest.Data.arr[i] := Source.Data.arr[i] shr PShiftData(Args)^.arr[i];
  3730. end;
  3731. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3732. procedure glBitmapInvertFunc(var aFuncRec: TglBitmapFunctionRec);
  3733. var
  3734. i: Integer;
  3735. begin
  3736. with aFuncRec do begin
  3737. Dest.Data := Source.Data;
  3738. for i := 0 to 3 do
  3739. if ({%H-}PtrUInt(Args) and (1 shl i) > 0) then
  3740. Dest.Data.arr[i] := Dest.Data.arr[i] xor Dest.Range.arr[i];
  3741. end;
  3742. end;
  3743. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3744. procedure glBitmapFillWithColorFunc(var aFuncRec: TglBitmapFunctionRec);
  3745. var
  3746. i: Integer;
  3747. begin
  3748. with aFuncRec do begin
  3749. for i := 0 to 3 do
  3750. Dest.Data.arr[i] := PglBitmapPixelData(Args)^.Data.arr[i];
  3751. end;
  3752. end;
  3753. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3754. procedure glBitmapAlphaFunc(var FuncRec: TglBitmapFunctionRec);
  3755. var
  3756. Temp: Single;
  3757. begin
  3758. with FuncRec do begin
  3759. if (FuncRec.Args = nil) then begin //source has no alpha
  3760. Temp :=
  3761. Source.Data.r / Source.Range.r * ALPHA_WEIGHT_R +
  3762. Source.Data.g / Source.Range.g * ALPHA_WEIGHT_G +
  3763. Source.Data.b / Source.Range.b * ALPHA_WEIGHT_B;
  3764. Dest.Data.a := Round(Dest.Range.a * Temp);
  3765. end else
  3766. Dest.Data.a := Round(Source.Data.a / Source.Range.a * Dest.Range.a);
  3767. end;
  3768. end;
  3769. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3770. procedure glBitmapColorKeyAlphaFunc(var FuncRec: TglBitmapFunctionRec);
  3771. type
  3772. PglBitmapPixelData = ^TglBitmapPixelData;
  3773. begin
  3774. with FuncRec do begin
  3775. Dest.Data.r := Source.Data.r;
  3776. Dest.Data.g := Source.Data.g;
  3777. Dest.Data.b := Source.Data.b;
  3778. with PglBitmapPixelData(Args)^ do
  3779. if ((Dest.Data.r <= Data.r) and (Dest.Data.r >= Range.r) and
  3780. (Dest.Data.g <= Data.g) and (Dest.Data.g >= Range.g) and
  3781. (Dest.Data.b <= Data.b) and (Dest.Data.b >= Range.b)) then
  3782. Dest.Data.a := 0
  3783. else
  3784. Dest.Data.a := Dest.Range.a;
  3785. end;
  3786. end;
  3787. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3788. procedure glBitmapValueAlphaFunc(var FuncRec: TglBitmapFunctionRec);
  3789. begin
  3790. with FuncRec do begin
  3791. Dest.Data.r := Source.Data.r;
  3792. Dest.Data.g := Source.Data.g;
  3793. Dest.Data.b := Source.Data.b;
  3794. Dest.Data.a := PCardinal(Args)^;
  3795. end;
  3796. end;
  3797. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3798. procedure SwapRGB(aData: PByte; aWidth: Integer; const aHasAlpha: Boolean);
  3799. type
  3800. PRGBPix = ^TRGBPix;
  3801. TRGBPix = array [0..2] of byte;
  3802. var
  3803. Temp: Byte;
  3804. begin
  3805. while aWidth > 0 do begin
  3806. Temp := PRGBPix(aData)^[0];
  3807. PRGBPix(aData)^[0] := PRGBPix(aData)^[2];
  3808. PRGBPix(aData)^[2] := Temp;
  3809. if aHasAlpha then
  3810. Inc(aData, 4)
  3811. else
  3812. Inc(aData, 3);
  3813. dec(aWidth);
  3814. end;
  3815. end;
  3816. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3817. //TglBitmapData///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3818. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3819. function TglBitmapData.GetFormatDescriptor: TglBitmapFormatDescriptor;
  3820. begin
  3821. result := TFormatDescriptor.Get(fFormat);
  3822. end;
  3823. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3824. function TglBitmapData.GetWidth: Integer;
  3825. begin
  3826. if (ffX in fDimension.Fields) then
  3827. result := fDimension.X
  3828. else
  3829. result := -1;
  3830. end;
  3831. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3832. function TglBitmapData.GetHeight: Integer;
  3833. begin
  3834. if (ffY in fDimension.Fields) then
  3835. result := fDimension.Y
  3836. else
  3837. result := -1;
  3838. end;
  3839. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3840. function TglBitmapData.GetScanlines(const aIndex: Integer): PByte;
  3841. begin
  3842. if fHasScanlines and (aIndex >= Low(fScanlines)) and (aIndex <= High(fScanlines)) then
  3843. result := fScanlines[aIndex]
  3844. else
  3845. result := nil;
  3846. end;
  3847. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3848. procedure TglBitmapData.SetFormat(const aValue: TglBitmapFormat);
  3849. begin
  3850. if fFormat = aValue then
  3851. exit;
  3852. if TFormatDescriptor.Get(Format).BitsPerPixel <> TFormatDescriptor.Get(aValue).BitsPerPixel then
  3853. raise EglBitmapUnsupportedFormat.Create(Format);
  3854. SetData(fData, aValue, Width, Height);
  3855. end;
  3856. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3857. procedure TglBitmapData.PrepareResType(var aResource: String; var aResType: PChar);
  3858. var
  3859. TempPos: Integer;
  3860. begin
  3861. if not Assigned(aResType) then begin
  3862. TempPos := Pos('.', aResource);
  3863. aResType := PChar(UpperCase(Copy(aResource, TempPos + 1, Length(aResource) - TempPos)));
  3864. aResource := UpperCase(Copy(aResource, 0, TempPos -1));
  3865. end;
  3866. end;
  3867. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3868. procedure TglBitmapData.UpdateScanlines;
  3869. var
  3870. w, h, i, LineWidth: Integer;
  3871. begin
  3872. w := Width;
  3873. h := Height;
  3874. fHasScanlines := Assigned(fData) and (w > 0) and (h > 0);
  3875. if fHasScanlines then begin
  3876. SetLength(fScanlines, h);
  3877. LineWidth := Trunc(w * FormatDescriptor.BytesPerPixel);
  3878. for i := 0 to h-1 do begin
  3879. fScanlines[i] := fData;
  3880. Inc(fScanlines[i], i * LineWidth);
  3881. end;
  3882. end else
  3883. SetLength(fScanlines, 0);
  3884. end;
  3885. {$IFDEF GLB_SUPPORT_PNG_READ}
  3886. {$IF DEFINED(GLB_LAZ_PNG)}
  3887. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3888. //PNG/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3889. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3890. function TglBitmapData.LoadPNG(const aStream: TStream): Boolean;
  3891. const
  3892. MAGIC_LEN = 8;
  3893. PNG_MAGIC: String[MAGIC_LEN] = #$89#$50#$4E#$47#$0D#$0A#$1A#$0A;
  3894. var
  3895. reader: TLazReaderPNG;
  3896. intf: TLazIntfImage;
  3897. StreamPos: Int64;
  3898. magic: String[MAGIC_LEN];
  3899. begin
  3900. result := true;
  3901. StreamPos := aStream.Position;
  3902. SetLength(magic, MAGIC_LEN);
  3903. aStream.Read(magic[1], MAGIC_LEN);
  3904. aStream.Position := StreamPos;
  3905. if (magic <> PNG_MAGIC) then begin
  3906. result := false;
  3907. exit;
  3908. end;
  3909. intf := TLazIntfImage.Create(0, 0);
  3910. reader := TLazReaderPNG.Create;
  3911. try try
  3912. reader.UpdateDescription := true;
  3913. reader.ImageRead(aStream, intf);
  3914. AssignFromLazIntfImage(intf);
  3915. except
  3916. result := false;
  3917. aStream.Position := StreamPos;
  3918. exit;
  3919. end;
  3920. finally
  3921. reader.Free;
  3922. intf.Free;
  3923. end;
  3924. end;
  3925. {$ELSEIF DEFINED(GLB_SDL_IMAGE)}
  3926. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3927. function TglBitmapData.LoadPNG(const aStream: TStream): Boolean;
  3928. var
  3929. Surface: PSDL_Surface;
  3930. RWops: PSDL_RWops;
  3931. begin
  3932. result := false;
  3933. RWops := glBitmapCreateRWops(aStream);
  3934. try
  3935. if IMG_isPNG(RWops) > 0 then begin
  3936. Surface := IMG_LoadPNG_RW(RWops);
  3937. try
  3938. AssignFromSurface(Surface);
  3939. result := true;
  3940. finally
  3941. SDL_FreeSurface(Surface);
  3942. end;
  3943. end;
  3944. finally
  3945. SDL_FreeRW(RWops);
  3946. end;
  3947. end;
  3948. {$ELSEIF DEFINED(GLB_LIB_PNG)}
  3949. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3950. procedure glBitmap_libPNG_read_func(png: png_structp; buffer: png_bytep; size: cardinal); cdecl;
  3951. begin
  3952. TStream(png_get_io_ptr(png)).Read(buffer^, size);
  3953. end;
  3954. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3955. function TglBitmapData.LoadPNG(const aStream: TStream): Boolean;
  3956. var
  3957. StreamPos: Int64;
  3958. signature: array [0..7] of byte;
  3959. png: png_structp;
  3960. png_info: png_infop;
  3961. TempHeight, TempWidth: Integer;
  3962. Format: TglBitmapFormat;
  3963. png_data: pByte;
  3964. png_rows: array of pByte;
  3965. Row, LineSize: Integer;
  3966. begin
  3967. result := false;
  3968. if not init_libPNG then
  3969. raise Exception.Create('LoadPNG - unable to initialize libPNG.');
  3970. try
  3971. // signature
  3972. StreamPos := aStream.Position;
  3973. aStream.Read(signature{%H-}, 8);
  3974. aStream.Position := StreamPos;
  3975. if png_check_sig(@signature, 8) <> 0 then begin
  3976. // png read struct
  3977. png := png_create_read_struct(PNG_LIBPNG_VER_STRING, nil, nil, nil);
  3978. if png = nil then
  3979. raise EglBitmapException.Create('LoadPng - couldn''t create read struct.');
  3980. // png info
  3981. png_info := png_create_info_struct(png);
  3982. if png_info = nil then begin
  3983. png_destroy_read_struct(@png, nil, nil);
  3984. raise EglBitmapException.Create('LoadPng - couldn''t create info struct.');
  3985. end;
  3986. // set read callback
  3987. png_set_read_fn(png, aStream, glBitmap_libPNG_read_func);
  3988. // read informations
  3989. png_read_info(png, png_info);
  3990. // size
  3991. TempHeight := png_get_image_height(png, png_info);
  3992. TempWidth := png_get_image_width(png, png_info);
  3993. // format
  3994. case png_get_color_type(png, png_info) of
  3995. PNG_COLOR_TYPE_GRAY:
  3996. Format := tfLuminance8ub1;
  3997. PNG_COLOR_TYPE_GRAY_ALPHA:
  3998. Format := tfLuminance8Alpha8us1;
  3999. PNG_COLOR_TYPE_RGB:
  4000. Format := tfRGB8ub3;
  4001. PNG_COLOR_TYPE_RGB_ALPHA:
  4002. Format := tfRGBA8ub4;
  4003. else
  4004. raise EglBitmapException.Create ('LoadPng - Unsupported Colortype found.');
  4005. end;
  4006. // cut upper 8 bit from 16 bit formats
  4007. if png_get_bit_depth(png, png_info) > 8 then
  4008. png_set_strip_16(png);
  4009. // expand bitdepth smaller than 8
  4010. if png_get_bit_depth(png, png_info) < 8 then
  4011. png_set_expand(png);
  4012. // allocating mem for scanlines
  4013. LineSize := png_get_rowbytes(png, png_info);
  4014. GetMem(png_data, TempHeight * LineSize);
  4015. try
  4016. SetLength(png_rows, TempHeight);
  4017. for Row := Low(png_rows) to High(png_rows) do begin
  4018. png_rows[Row] := png_data;
  4019. Inc(png_rows[Row], Row * LineSize);
  4020. end;
  4021. // read complete image into scanlines
  4022. png_read_image(png, @png_rows[0]);
  4023. // read end
  4024. png_read_end(png, png_info);
  4025. // destroy read struct
  4026. png_destroy_read_struct(@png, @png_info, nil);
  4027. SetLength(png_rows, 0);
  4028. // set new data
  4029. SetData(png_data, Format, TempWidth, TempHeight);
  4030. result := true;
  4031. except
  4032. if Assigned(png_data) then
  4033. FreeMem(png_data);
  4034. raise;
  4035. end;
  4036. end;
  4037. finally
  4038. quit_libPNG;
  4039. end;
  4040. end;
  4041. {$ELSEIF DEFINED(GLB_PNGIMAGE)}
  4042. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4043. function TglBitmapData.LoadPNG(const aStream: TStream): Boolean;
  4044. var
  4045. StreamPos: Int64;
  4046. Png: TPNGObject;
  4047. Header: String[8];
  4048. Row, Col, PixSize, LineSize: Integer;
  4049. NewImage, pSource, pDest, pAlpha: pByte;
  4050. PngFormat: TglBitmapFormat;
  4051. FormatDesc: TFormatDescriptor;
  4052. const
  4053. PngHeader: String[8] = #137#80#78#71#13#10#26#10;
  4054. begin
  4055. result := false;
  4056. StreamPos := aStream.Position;
  4057. aStream.Read(Header[0], SizeOf(Header));
  4058. aStream.Position := StreamPos;
  4059. {Test if the header matches}
  4060. if Header = PngHeader then begin
  4061. Png := TPNGObject.Create;
  4062. try
  4063. Png.LoadFromStream(aStream);
  4064. case Png.Header.ColorType of
  4065. COLOR_GRAYSCALE:
  4066. PngFormat := tfLuminance8ub1;
  4067. COLOR_GRAYSCALEALPHA:
  4068. PngFormat := tfLuminance8Alpha8us1;
  4069. COLOR_RGB:
  4070. PngFormat := tfBGR8ub3;
  4071. COLOR_RGBALPHA:
  4072. PngFormat := tfBGRA8ub4;
  4073. else
  4074. raise EglBitmapException.Create ('LoadPng - Unsupported Colortype found.');
  4075. end;
  4076. FormatDesc := TFormatDescriptor.Get(PngFormat);
  4077. PixSize := Round(FormatDesc.PixelSize);
  4078. LineSize := FormatDesc.GetSize(Png.Header.Width, 1);
  4079. GetMem(NewImage, LineSize * Integer(Png.Header.Height));
  4080. try
  4081. pDest := NewImage;
  4082. case Png.Header.ColorType of
  4083. COLOR_RGB, COLOR_GRAYSCALE:
  4084. begin
  4085. for Row := 0 to Png.Height -1 do begin
  4086. Move (Png.Scanline[Row]^, pDest^, LineSize);
  4087. Inc(pDest, LineSize);
  4088. end;
  4089. end;
  4090. COLOR_RGBALPHA, COLOR_GRAYSCALEALPHA:
  4091. begin
  4092. PixSize := PixSize -1;
  4093. for Row := 0 to Png.Height -1 do begin
  4094. pSource := Png.Scanline[Row];
  4095. pAlpha := pByte(Png.AlphaScanline[Row]);
  4096. for Col := 0 to Png.Width -1 do begin
  4097. Move (pSource^, pDest^, PixSize);
  4098. Inc(pSource, PixSize);
  4099. Inc(pDest, PixSize);
  4100. pDest^ := pAlpha^;
  4101. inc(pAlpha);
  4102. Inc(pDest);
  4103. end;
  4104. end;
  4105. end;
  4106. else
  4107. raise EglBitmapException.Create ('LoadPng - Unsupported Colortype found.');
  4108. end;
  4109. SetData(NewImage, PngFormat, Png.Header.Width, Png.Header.Height);
  4110. result := true;
  4111. except
  4112. if Assigned(NewImage) then
  4113. FreeMem(NewImage);
  4114. raise;
  4115. end;
  4116. finally
  4117. Png.Free;
  4118. end;
  4119. end;
  4120. end;
  4121. {$IFEND}
  4122. {$ENDIF}
  4123. {$IFDEF GLB_SUPPORT_PNG_WRITE}
  4124. {$IFDEF GLB_LIB_PNG}
  4125. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4126. procedure glBitmap_libPNG_write_func(png: png_structp; buffer: png_bytep; size: cardinal); cdecl;
  4127. begin
  4128. TStream(png_get_io_ptr(png)).Write(buffer^, size);
  4129. end;
  4130. {$ENDIF}
  4131. {$IF DEFINED(GLB_LAZ_PNG)}
  4132. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4133. procedure TglBitmapData.SavePNG(const aStream: TStream);
  4134. var
  4135. png: TPortableNetworkGraphic;
  4136. intf: TLazIntfImage;
  4137. raw: TRawImage;
  4138. begin
  4139. png := TPortableNetworkGraphic.Create;
  4140. intf := TLazIntfImage.Create(0, 0);
  4141. try
  4142. if not AssignToLazIntfImage(intf) then
  4143. raise EglBitmap.Create('unable to create LazIntfImage from glBitmap');
  4144. intf.GetRawImage(raw);
  4145. png.LoadFromRawImage(raw, false);
  4146. png.SaveToStream(aStream);
  4147. finally
  4148. png.Free;
  4149. intf.Free;
  4150. end;
  4151. end;
  4152. {$ELSEIF DEFINED(GLB_LIB_PNG)}
  4153. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4154. procedure TglBitmapData.SavePNG(const aStream: TStream);
  4155. var
  4156. png: png_structp;
  4157. png_info: png_infop;
  4158. png_rows: array of pByte;
  4159. LineSize: Integer;
  4160. ColorType: Integer;
  4161. Row: Integer;
  4162. FormatDesc: TFormatDescriptor;
  4163. begin
  4164. if not (ftPNG in FormatGetSupportedFiles(Format)) then
  4165. raise EglBitmapUnsupportedFormat.Create(Format);
  4166. if not init_libPNG then
  4167. raise Exception.Create('unable to initialize libPNG.');
  4168. try
  4169. case Format of
  4170. tfAlpha8ub1, tfLuminance8ub1:
  4171. ColorType := PNG_COLOR_TYPE_GRAY;
  4172. tfLuminance8Alpha8us1:
  4173. ColorType := PNG_COLOR_TYPE_GRAY_ALPHA;
  4174. tfBGR8ub3, tfRGB8ub3:
  4175. ColorType := PNG_COLOR_TYPE_RGB;
  4176. tfBGRA8ub4, tfRGBA8ub4:
  4177. ColorType := PNG_COLOR_TYPE_RGBA;
  4178. else
  4179. raise EglBitmapUnsupportedFormat.Create(Format);
  4180. end;
  4181. FormatDesc := TFormatDescriptor.Get(Format);
  4182. LineSize := FormatDesc.GetSize(Width, 1);
  4183. // creating array for scanline
  4184. SetLength(png_rows, Height);
  4185. try
  4186. for Row := 0 to Height - 1 do begin
  4187. png_rows[Row] := Data;
  4188. Inc(png_rows[Row], Row * LineSize)
  4189. end;
  4190. // write struct
  4191. png := png_create_write_struct(PNG_LIBPNG_VER_STRING, nil, nil, nil);
  4192. if png = nil then
  4193. raise EglBitmapException.Create('SavePng - couldn''t create write struct.');
  4194. // create png info
  4195. png_info := png_create_info_struct(png);
  4196. if png_info = nil then begin
  4197. png_destroy_write_struct(@png, nil);
  4198. raise EglBitmapException.Create('SavePng - couldn''t create info struct.');
  4199. end;
  4200. // set read callback
  4201. png_set_write_fn(png, aStream, glBitmap_libPNG_write_func, nil);
  4202. // set compression
  4203. png_set_compression_level(png, 6);
  4204. if Format in [tfBGR8ub3, tfBGRA8ub4] then
  4205. png_set_bgr(png);
  4206. png_set_IHDR(png, png_info, Width, Height, 8, ColorType, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
  4207. png_write_info(png, png_info);
  4208. png_write_image(png, @png_rows[0]);
  4209. png_write_end(png, png_info);
  4210. png_destroy_write_struct(@png, @png_info);
  4211. finally
  4212. SetLength(png_rows, 0);
  4213. end;
  4214. finally
  4215. quit_libPNG;
  4216. end;
  4217. end;
  4218. {$ELSEIF DEFINED(GLB_PNGIMAGE)}
  4219. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4220. procedure TglBitmapData.SavePNG(const aStream: TStream);
  4221. var
  4222. Png: TPNGObject;
  4223. pSource, pDest: pByte;
  4224. X, Y, PixSize: Integer;
  4225. ColorType: Cardinal;
  4226. Alpha: Boolean;
  4227. pTemp: pByte;
  4228. Temp: Byte;
  4229. begin
  4230. if not (ftPNG in FormatGetSupportedFiles (Format)) then
  4231. raise EglBitmapUnsupportedFormat.Create(Format);
  4232. case Format of
  4233. tfAlpha8ub1, tfLuminance8ub1: begin
  4234. ColorType := COLOR_GRAYSCALE;
  4235. PixSize := 1;
  4236. Alpha := false;
  4237. end;
  4238. tfLuminance8Alpha8us1: begin
  4239. ColorType := COLOR_GRAYSCALEALPHA;
  4240. PixSize := 1;
  4241. Alpha := true;
  4242. end;
  4243. tfBGR8ub3, tfRGB8ub3: begin
  4244. ColorType := COLOR_RGB;
  4245. PixSize := 3;
  4246. Alpha := false;
  4247. end;
  4248. tfBGRA8ub4, tfRGBA8ub4: begin
  4249. ColorType := COLOR_RGBALPHA;
  4250. PixSize := 3;
  4251. Alpha := true
  4252. end;
  4253. else
  4254. raise EglBitmapUnsupportedFormat.Create(Format);
  4255. end;
  4256. Png := TPNGObject.CreateBlank(ColorType, 8, Width, Height);
  4257. try
  4258. // Copy ImageData
  4259. pSource := Data;
  4260. for Y := 0 to Height -1 do begin
  4261. pDest := png.ScanLine[Y];
  4262. for X := 0 to Width -1 do begin
  4263. Move(pSource^, pDest^, PixSize);
  4264. Inc(pDest, PixSize);
  4265. Inc(pSource, PixSize);
  4266. if Alpha then begin
  4267. png.AlphaScanline[Y]^[X] := pSource^;
  4268. Inc(pSource);
  4269. end;
  4270. end;
  4271. // convert RGB line to BGR
  4272. if Format in [tfRGB8ub3, tfRGBA8ub4] then begin
  4273. pTemp := png.ScanLine[Y];
  4274. for X := 0 to Width -1 do begin
  4275. Temp := pByteArray(pTemp)^[0];
  4276. pByteArray(pTemp)^[0] := pByteArray(pTemp)^[2];
  4277. pByteArray(pTemp)^[2] := Temp;
  4278. Inc(pTemp, 3);
  4279. end;
  4280. end;
  4281. end;
  4282. // Save to Stream
  4283. Png.CompressionLevel := 6;
  4284. Png.SaveToStream(aStream);
  4285. finally
  4286. FreeAndNil(Png);
  4287. end;
  4288. end;
  4289. {$IFEND}
  4290. {$ENDIF}
  4291. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4292. //JPEG////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4293. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4294. {$IFDEF GLB_LIB_JPEG}
  4295. type
  4296. glBitmap_libJPEG_source_mgr_ptr = ^glBitmap_libJPEG_source_mgr;
  4297. glBitmap_libJPEG_source_mgr = record
  4298. pub: jpeg_source_mgr;
  4299. SrcStream: TStream;
  4300. SrcBuffer: array [1..4096] of byte;
  4301. end;
  4302. glBitmap_libJPEG_dest_mgr_ptr = ^glBitmap_libJPEG_dest_mgr;
  4303. glBitmap_libJPEG_dest_mgr = record
  4304. pub: jpeg_destination_mgr;
  4305. DestStream: TStream;
  4306. DestBuffer: array [1..4096] of byte;
  4307. end;
  4308. procedure glBitmap_libJPEG_error_exit(cinfo: j_common_ptr); cdecl;
  4309. begin
  4310. //DUMMY
  4311. end;
  4312. procedure glBitmap_libJPEG_output_message(cinfo: j_common_ptr); cdecl;
  4313. begin
  4314. //DUMMY
  4315. end;
  4316. procedure glBitmap_libJPEG_init_source(cinfo: j_decompress_ptr); cdecl;
  4317. begin
  4318. //DUMMY
  4319. end;
  4320. procedure glBitmap_libJPEG_term_source(cinfo: j_decompress_ptr); cdecl;
  4321. begin
  4322. //DUMMY
  4323. end;
  4324. procedure glBitmap_libJPEG_init_destination(cinfo: j_compress_ptr); cdecl;
  4325. begin
  4326. //DUMMY
  4327. end;
  4328. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4329. function glBitmap_libJPEG_fill_input_buffer(cinfo: j_decompress_ptr): boolean; cdecl;
  4330. var
  4331. src: glBitmap_libJPEG_source_mgr_ptr;
  4332. bytes: integer;
  4333. begin
  4334. src := glBitmap_libJPEG_source_mgr_ptr(cinfo^.src);
  4335. bytes := src^.SrcStream.Read(src^.SrcBuffer[1], 4096);
  4336. if (bytes <= 0) then begin
  4337. src^.SrcBuffer[1] := $FF;
  4338. src^.SrcBuffer[2] := JPEG_EOI;
  4339. bytes := 2;
  4340. end;
  4341. src^.pub.next_input_byte := @(src^.SrcBuffer[1]);
  4342. src^.pub.bytes_in_buffer := bytes;
  4343. result := true;
  4344. end;
  4345. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4346. procedure glBitmap_libJPEG_skip_input_data(cinfo: j_decompress_ptr; num_bytes: Longint); cdecl;
  4347. var
  4348. src: glBitmap_libJPEG_source_mgr_ptr;
  4349. begin
  4350. src := glBitmap_libJPEG_source_mgr_ptr(cinfo^.src);
  4351. if num_bytes > 0 then begin
  4352. // wanted byte isn't in buffer so set stream position and read buffer
  4353. if num_bytes > src^.pub.bytes_in_buffer then begin
  4354. src^.SrcStream.Position := src^.SrcStream.Position + num_bytes - src^.pub.bytes_in_buffer;
  4355. src^.pub.fill_input_buffer(cinfo);
  4356. end else begin
  4357. // wanted byte is in buffer so only skip
  4358. inc(src^.pub.next_input_byte, num_bytes);
  4359. dec(src^.pub.bytes_in_buffer, num_bytes);
  4360. end;
  4361. end;
  4362. end;
  4363. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4364. function glBitmap_libJPEG_empty_output_buffer(cinfo: j_compress_ptr): boolean; cdecl;
  4365. var
  4366. dest: glBitmap_libJPEG_dest_mgr_ptr;
  4367. begin
  4368. dest := glBitmap_libJPEG_dest_mgr_ptr(cinfo^.dest);
  4369. if dest^.pub.free_in_buffer < Cardinal(Length(dest^.DestBuffer)) then begin
  4370. // write complete buffer
  4371. dest^.DestStream.Write(dest^.DestBuffer[1], SizeOf(dest^.DestBuffer));
  4372. // reset buffer
  4373. dest^.pub.next_output_byte := @dest^.DestBuffer[1];
  4374. dest^.pub.free_in_buffer := Length(dest^.DestBuffer);
  4375. end;
  4376. result := true;
  4377. end;
  4378. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4379. procedure glBitmap_libJPEG_term_destination(cinfo: j_compress_ptr); cdecl;
  4380. var
  4381. Idx: Integer;
  4382. dest: glBitmap_libJPEG_dest_mgr_ptr;
  4383. begin
  4384. dest := glBitmap_libJPEG_dest_mgr_ptr(cinfo^.dest);
  4385. for Idx := Low(dest^.DestBuffer) to High(dest^.DestBuffer) do begin
  4386. // check for endblock
  4387. if (Idx < High(dest^.DestBuffer)) and (dest^.DestBuffer[Idx] = $FF) and (dest^.DestBuffer[Idx +1] = JPEG_EOI) then begin
  4388. // write endblock
  4389. dest^.DestStream.Write(dest^.DestBuffer[Idx], 2);
  4390. // leave
  4391. break;
  4392. end else
  4393. dest^.DestStream.Write(dest^.DestBuffer[Idx], 1);
  4394. end;
  4395. end;
  4396. {$ENDIF}
  4397. {$IFDEF GLB_SUPPORT_JPEG_READ}
  4398. {$IF DEFINED(GLB_LAZ_JPEG)}
  4399. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4400. function TglBitmapData.LoadJPEG(const aStream: TStream): Boolean;
  4401. const
  4402. MAGIC_LEN = 2;
  4403. JPEG_MAGIC: String[MAGIC_LEN] = #$FF#$D8;
  4404. var
  4405. intf: TLazIntfImage;
  4406. reader: TFPReaderJPEG;
  4407. StreamPos: Int64;
  4408. magic: String[MAGIC_LEN];
  4409. begin
  4410. result := true;
  4411. StreamPos := aStream.Position;
  4412. SetLength(magic, MAGIC_LEN);
  4413. aStream.Read(magic[1], MAGIC_LEN);
  4414. aStream.Position := StreamPos;
  4415. if (magic <> JPEG_MAGIC) then begin
  4416. result := false;
  4417. exit;
  4418. end;
  4419. reader := TFPReaderJPEG.Create;
  4420. intf := TLazIntfImage.Create(0, 0);
  4421. try try
  4422. intf.DataDescription := GetDescriptionFromDevice(0, 0, 0);
  4423. reader.ImageRead(aStream, intf);
  4424. AssignFromLazIntfImage(intf);
  4425. except
  4426. result := false;
  4427. aStream.Position := StreamPos;
  4428. exit;
  4429. end;
  4430. finally
  4431. reader.Free;
  4432. intf.Free;
  4433. end;
  4434. end;
  4435. {$ELSEIF DEFINED(GLB_SDL_IMAGE)}
  4436. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4437. function TglBitmapData.LoadJPEG(const aStream: TStream): Boolean;
  4438. var
  4439. Surface: PSDL_Surface;
  4440. RWops: PSDL_RWops;
  4441. begin
  4442. result := false;
  4443. RWops := glBitmapCreateRWops(aStream);
  4444. try
  4445. if IMG_isJPG(RWops) > 0 then begin
  4446. Surface := IMG_LoadJPG_RW(RWops);
  4447. try
  4448. AssignFromSurface(Surface);
  4449. result := true;
  4450. finally
  4451. SDL_FreeSurface(Surface);
  4452. end;
  4453. end;
  4454. finally
  4455. SDL_FreeRW(RWops);
  4456. end;
  4457. end;
  4458. {$ELSEIF DEFINED(GLB_LIB_JPEG)}
  4459. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4460. function TglBitmapData.LoadJPEG(const aStream: TStream): Boolean;
  4461. var
  4462. StreamPos: Int64;
  4463. Temp: array[0..1]of Byte;
  4464. jpeg: jpeg_decompress_struct;
  4465. jpeg_err: jpeg_error_mgr;
  4466. IntFormat: TglBitmapFormat;
  4467. pImage: pByte;
  4468. TempHeight, TempWidth: Integer;
  4469. pTemp: pByte;
  4470. Row: Integer;
  4471. FormatDesc: TFormatDescriptor;
  4472. begin
  4473. result := false;
  4474. if not init_libJPEG then
  4475. raise Exception.Create('LoadJPG - unable to initialize libJPEG.');
  4476. try
  4477. // reading first two bytes to test file and set cursor back to begin
  4478. StreamPos := aStream.Position;
  4479. aStream.Read({%H-}Temp[0], 2);
  4480. aStream.Position := StreamPos;
  4481. // if Bitmap then read file.
  4482. if ((Temp[0] = $FF) and (Temp[1] = $D8)) then begin
  4483. FillChar(jpeg{%H-}, SizeOf(jpeg_decompress_struct), $00);
  4484. FillChar(jpeg_err{%H-}, SizeOf(jpeg_error_mgr), $00);
  4485. // error managment
  4486. jpeg.err := jpeg_std_error(@jpeg_err);
  4487. jpeg_err.error_exit := glBitmap_libJPEG_error_exit;
  4488. jpeg_err.output_message := glBitmap_libJPEG_output_message;
  4489. // decompression struct
  4490. jpeg_create_decompress(@jpeg);
  4491. // allocation space for streaming methods
  4492. jpeg.src := jpeg.mem^.alloc_small(@jpeg, JPOOL_PERMANENT, SizeOf(glBitmap_libJPEG_source_mgr));
  4493. // seeting up custom functions
  4494. with glBitmap_libJPEG_source_mgr_ptr(jpeg.src)^ do begin
  4495. pub.init_source := glBitmap_libJPEG_init_source;
  4496. pub.fill_input_buffer := glBitmap_libJPEG_fill_input_buffer;
  4497. pub.skip_input_data := glBitmap_libJPEG_skip_input_data;
  4498. pub.resync_to_restart := jpeg_resync_to_restart; // use default method
  4499. pub.term_source := glBitmap_libJPEG_term_source;
  4500. pub.bytes_in_buffer := 0; // forces fill_input_buffer on first read
  4501. pub.next_input_byte := nil; // until buffer loaded
  4502. SrcStream := aStream;
  4503. end;
  4504. // set global decoding state
  4505. jpeg.global_state := DSTATE_START;
  4506. // read header of jpeg
  4507. jpeg_read_header(@jpeg, false);
  4508. // setting output parameter
  4509. case jpeg.jpeg_color_space of
  4510. JCS_GRAYSCALE:
  4511. begin
  4512. jpeg.out_color_space := JCS_GRAYSCALE;
  4513. IntFormat := tfLuminance8ub1;
  4514. end;
  4515. else
  4516. jpeg.out_color_space := JCS_RGB;
  4517. IntFormat := tfRGB8ub3;
  4518. end;
  4519. // reading image
  4520. jpeg_start_decompress(@jpeg);
  4521. TempHeight := jpeg.output_height;
  4522. TempWidth := jpeg.output_width;
  4523. FormatDesc := TFormatDescriptor.Get(IntFormat);
  4524. // creating new image
  4525. GetMem(pImage, FormatDesc.GetSize(TempWidth, TempHeight));
  4526. try
  4527. pTemp := pImage;
  4528. for Row := 0 to TempHeight -1 do begin
  4529. jpeg_read_scanlines(@jpeg, @pTemp, 1);
  4530. Inc(pTemp, FormatDesc.GetSize(TempWidth, 1));
  4531. end;
  4532. // finish decompression
  4533. jpeg_finish_decompress(@jpeg);
  4534. // destroy decompression
  4535. jpeg_destroy_decompress(@jpeg);
  4536. SetData(pImage, IntFormat, TempWidth, TempHeight);
  4537. result := true;
  4538. except
  4539. if Assigned(pImage) then
  4540. FreeMem(pImage);
  4541. raise;
  4542. end;
  4543. end;
  4544. finally
  4545. quit_libJPEG;
  4546. end;
  4547. end;
  4548. {$ELSEIF DEFINED(GLB_DELPHI_JPEG)}
  4549. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4550. function TglBitmapData.LoadJPEG(const aStream: TStream): Boolean;
  4551. var
  4552. bmp: TBitmap;
  4553. jpg: TJPEGImage;
  4554. StreamPos: Int64;
  4555. Temp: array[0..1]of Byte;
  4556. begin
  4557. result := false;
  4558. // reading first two bytes to test file and set cursor back to begin
  4559. StreamPos := aStream.Position;
  4560. aStream.Read(Temp[0], 2);
  4561. aStream.Position := StreamPos;
  4562. // if Bitmap then read file.
  4563. if ((Temp[0] = $FF) and (Temp[1] = $D8)) then begin
  4564. bmp := TBitmap.Create;
  4565. try
  4566. jpg := TJPEGImage.Create;
  4567. try
  4568. jpg.LoadFromStream(aStream);
  4569. bmp.Assign(jpg);
  4570. result := AssignFromBitmap(bmp);
  4571. finally
  4572. jpg.Free;
  4573. end;
  4574. finally
  4575. bmp.Free;
  4576. end;
  4577. end;
  4578. end;
  4579. {$IFEND}
  4580. {$ENDIF}
  4581. {$IFDEF GLB_SUPPORT_JPEG_WRITE}
  4582. {$IF DEFINED(GLB_LAZ_JPEG)}
  4583. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4584. procedure TglBitmapData.SaveJPEG(const aStream: TStream);
  4585. var
  4586. jpeg: TJPEGImage;
  4587. intf: TLazIntfImage;
  4588. raw: TRawImage;
  4589. begin
  4590. jpeg := TJPEGImage.Create;
  4591. intf := TLazIntfImage.Create(0, 0);
  4592. try
  4593. if not AssignToLazIntfImage(intf) then
  4594. raise EglBitmap.Create('unable to create LazIntfImage from glBitmap');
  4595. intf.GetRawImage(raw);
  4596. jpeg.LoadFromRawImage(raw, false);
  4597. jpeg.SaveToStream(aStream);
  4598. finally
  4599. intf.Free;
  4600. jpeg.Free;
  4601. end;
  4602. end;
  4603. {$ELSEIF DEFINED(GLB_LIB_JPEG)}
  4604. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4605. procedure TglBitmapData.SaveJPEG(const aStream: TStream);
  4606. var
  4607. jpeg: jpeg_compress_struct;
  4608. jpeg_err: jpeg_error_mgr;
  4609. Row: Integer;
  4610. pTemp, pTemp2: pByte;
  4611. procedure CopyRow(pDest, pSource: pByte);
  4612. var
  4613. X: Integer;
  4614. begin
  4615. for X := 0 to Width - 1 do begin
  4616. pByteArray(pDest)^[0] := pByteArray(pSource)^[2];
  4617. pByteArray(pDest)^[1] := pByteArray(pSource)^[1];
  4618. pByteArray(pDest)^[2] := pByteArray(pSource)^[0];
  4619. Inc(pDest, 3);
  4620. Inc(pSource, 3);
  4621. end;
  4622. end;
  4623. begin
  4624. if not (ftJPEG in FormatGetSupportedFiles(Format)) then
  4625. raise EglBitmapUnsupportedFormat.Create(Format);
  4626. if not init_libJPEG then
  4627. raise Exception.Create('SaveJPG - unable to initialize libJPEG.');
  4628. try
  4629. FillChar(jpeg{%H-}, SizeOf(jpeg_compress_struct), $00);
  4630. FillChar(jpeg_err{%H-}, SizeOf(jpeg_error_mgr), $00);
  4631. // error managment
  4632. jpeg.err := jpeg_std_error(@jpeg_err);
  4633. jpeg_err.error_exit := glBitmap_libJPEG_error_exit;
  4634. jpeg_err.output_message := glBitmap_libJPEG_output_message;
  4635. // compression struct
  4636. jpeg_create_compress(@jpeg);
  4637. // allocation space for streaming methods
  4638. jpeg.dest := jpeg.mem^.alloc_small(@jpeg, JPOOL_PERMANENT, SizeOf(glBitmap_libJPEG_dest_mgr));
  4639. // seeting up custom functions
  4640. with glBitmap_libJPEG_dest_mgr_ptr(jpeg.dest)^ do begin
  4641. pub.init_destination := glBitmap_libJPEG_init_destination;
  4642. pub.empty_output_buffer := glBitmap_libJPEG_empty_output_buffer;
  4643. pub.term_destination := glBitmap_libJPEG_term_destination;
  4644. pub.next_output_byte := @DestBuffer[1];
  4645. pub.free_in_buffer := Length(DestBuffer);
  4646. DestStream := aStream;
  4647. end;
  4648. // very important state
  4649. jpeg.global_state := CSTATE_START;
  4650. jpeg.image_width := Width;
  4651. jpeg.image_height := Height;
  4652. case Format of
  4653. tfAlpha8ub1, tfLuminance8ub1: begin
  4654. jpeg.input_components := 1;
  4655. jpeg.in_color_space := JCS_GRAYSCALE;
  4656. end;
  4657. tfRGB8ub3, tfBGR8ub3: begin
  4658. jpeg.input_components := 3;
  4659. jpeg.in_color_space := JCS_RGB;
  4660. end;
  4661. end;
  4662. jpeg_set_defaults(@jpeg);
  4663. jpeg_set_quality(@jpeg, 95, true);
  4664. jpeg_start_compress(@jpeg, true);
  4665. pTemp := Data;
  4666. if Format = tfBGR8ub3 then
  4667. GetMem(pTemp2, fRowSize)
  4668. else
  4669. pTemp2 := pTemp;
  4670. try
  4671. for Row := 0 to jpeg.image_height -1 do begin
  4672. // prepare row
  4673. if Format = tfBGR8ub3 then
  4674. CopyRow(pTemp2, pTemp)
  4675. else
  4676. pTemp2 := pTemp;
  4677. // write row
  4678. jpeg_write_scanlines(@jpeg, @pTemp2, 1);
  4679. inc(pTemp, fRowSize);
  4680. end;
  4681. finally
  4682. // free memory
  4683. if Format = tfBGR8ub3 then
  4684. FreeMem(pTemp2);
  4685. end;
  4686. jpeg_finish_compress(@jpeg);
  4687. jpeg_destroy_compress(@jpeg);
  4688. finally
  4689. quit_libJPEG;
  4690. end;
  4691. end;
  4692. {$ELSEIF DEFINED(GLB_DELPHI_JPEG)}
  4693. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4694. procedure TglBitmapData.SaveJPEG(const aStream: TStream);
  4695. var
  4696. Bmp: TBitmap;
  4697. Jpg: TJPEGImage;
  4698. begin
  4699. if not (ftJPEG in FormatGetSupportedFiles(Format)) then
  4700. raise EglBitmapUnsupportedFormat.Create(Format);
  4701. Bmp := TBitmap.Create;
  4702. try
  4703. Jpg := TJPEGImage.Create;
  4704. try
  4705. AssignToBitmap(Bmp);
  4706. if (Format in [tfAlpha8ub1, tfLuminance8ub1]) then begin
  4707. Jpg.Grayscale := true;
  4708. Jpg.PixelFormat := jf8Bit;
  4709. end;
  4710. Jpg.Assign(Bmp);
  4711. Jpg.SaveToStream(aStream);
  4712. finally
  4713. FreeAndNil(Jpg);
  4714. end;
  4715. finally
  4716. FreeAndNil(Bmp);
  4717. end;
  4718. end;
  4719. {$IFEND}
  4720. {$ENDIF}
  4721. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4722. //RAW/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4723. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4724. type
  4725. RawHeader = packed record
  4726. Magic: String[5];
  4727. Version: Byte;
  4728. Width: Integer;
  4729. Height: Integer;
  4730. DataSize: Integer;
  4731. BitsPerPixel: Integer;
  4732. Precision: TglBitmapRec4ub;
  4733. Shift: TglBitmapRec4ub;
  4734. end;
  4735. function TglBitmapData.LoadRAW(const aStream: TStream): Boolean;
  4736. var
  4737. header: RawHeader;
  4738. StartPos: Int64;
  4739. fd: TFormatDescriptor;
  4740. buf: PByte;
  4741. begin
  4742. result := false;
  4743. StartPos := aStream.Position;
  4744. aStream.Read(header{%H-}, SizeOf(header));
  4745. if (header.Magic <> 'glBMP') then begin
  4746. aStream.Position := StartPos;
  4747. exit;
  4748. end;
  4749. fd := TFormatDescriptor.GetFromPrecShift(header.Precision, header.Shift, header.BitsPerPixel);
  4750. if (fd.Format = tfEmpty) then
  4751. raise EglBitmapUnsupportedFormat.Create('no supported format found');
  4752. buf := GetMemory(header.DataSize);
  4753. aStream.Read(buf^, header.DataSize);
  4754. SetData(buf, fd.Format, header.Width, header.Height);
  4755. result := true;
  4756. end;
  4757. procedure TglBitmapData.SaveRAW(const aStream: TStream);
  4758. var
  4759. header: RawHeader;
  4760. fd: TFormatDescriptor;
  4761. begin
  4762. fd := TFormatDescriptor.Get(Format);
  4763. header.Magic := 'glBMP';
  4764. header.Version := 1;
  4765. header.Width := Width;
  4766. header.Height := Height;
  4767. header.DataSize := fd.GetSize(fDimension);
  4768. header.BitsPerPixel := fd.BitsPerPixel;
  4769. header.Precision := fd.Precision;
  4770. header.Shift := fd.Shift;
  4771. aStream.Write(header, SizeOf(header));
  4772. aStream.Write(Data^, header.DataSize);
  4773. end;
  4774. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4775. //BMP/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4776. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4777. const
  4778. BMP_MAGIC = $4D42;
  4779. BMP_COMP_RGB = 0;
  4780. BMP_COMP_RLE8 = 1;
  4781. BMP_COMP_RLE4 = 2;
  4782. BMP_COMP_BITFIELDS = 3;
  4783. type
  4784. TBMPHeader = packed record
  4785. bfType: Word;
  4786. bfSize: Cardinal;
  4787. bfReserved1: Word;
  4788. bfReserved2: Word;
  4789. bfOffBits: Cardinal;
  4790. end;
  4791. TBMPInfo = packed record
  4792. biSize: Cardinal;
  4793. biWidth: Longint;
  4794. biHeight: Longint;
  4795. biPlanes: Word;
  4796. biBitCount: Word;
  4797. biCompression: Cardinal;
  4798. biSizeImage: Cardinal;
  4799. biXPelsPerMeter: Longint;
  4800. biYPelsPerMeter: Longint;
  4801. biClrUsed: Cardinal;
  4802. biClrImportant: Cardinal;
  4803. end;
  4804. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4805. function TglBitmapData.LoadBMP(const aStream: TStream): Boolean;
  4806. //////////////////////////////////////////////////////////////////////////////////////////////////
  4807. function ReadInfo(out aInfo: TBMPInfo; out aMask: TglBitmapRec4ul): TglBitmapFormat;
  4808. var
  4809. tmp, i: Cardinal;
  4810. begin
  4811. result := tfEmpty;
  4812. aStream.Read(aInfo{%H-}, SizeOf(aInfo));
  4813. FillChar(aMask{%H-}, SizeOf(aMask), 0);
  4814. //Read Compression
  4815. case aInfo.biCompression of
  4816. BMP_COMP_RLE4,
  4817. BMP_COMP_RLE8: begin
  4818. raise EglBitmap.Create('RLE compression is not supported');
  4819. end;
  4820. BMP_COMP_BITFIELDS: begin
  4821. if (aInfo.biBitCount = 16) or (aInfo.biBitCount = 32) then begin
  4822. for i := 0 to 2 do begin
  4823. aStream.Read(tmp{%H-}, SizeOf(tmp));
  4824. aMask.arr[i] := tmp;
  4825. end;
  4826. end else
  4827. raise EglBitmap.Create('Bitfields are only supported for 16bit and 32bit formats');
  4828. end;
  4829. end;
  4830. //get suitable format
  4831. case aInfo.biBitCount of
  4832. 8: result := tfLuminance8ub1;
  4833. 16: result := tfX1RGB5us1;
  4834. 24: result := tfBGR8ub3;
  4835. 32: result := tfXRGB8ui1;
  4836. end;
  4837. end;
  4838. function ReadColorTable(var aFormat: TglBitmapFormat; const aInfo: TBMPInfo): TbmpColorTableFormat;
  4839. var
  4840. i, c: Integer;
  4841. fd: TFormatDescriptor;
  4842. ColorTable: TbmpColorTable;
  4843. begin
  4844. result := nil;
  4845. if (aInfo.biBitCount >= 16) then
  4846. exit;
  4847. aFormat := tfLuminance8ub1;
  4848. c := aInfo.biClrUsed;
  4849. if (c = 0) then
  4850. c := 1 shl aInfo.biBitCount;
  4851. SetLength(ColorTable, c);
  4852. for i := 0 to c-1 do begin
  4853. aStream.Read(ColorTable[i], SizeOf(TbmpColorTableEnty));
  4854. if (ColorTable[i].r <> ColorTable[i].g) or (ColorTable[i].g <> ColorTable[i].b) then
  4855. aFormat := tfRGB8ub3;
  4856. end;
  4857. fd := TFormatDescriptor.Get(aFormat);
  4858. result := TbmpColorTableFormat.Create;
  4859. result.ColorTable := ColorTable;
  4860. result.SetCustomValues(aFormat, aInfo.biBitCount, fd.Precision, fd.Shift);
  4861. end;
  4862. //////////////////////////////////////////////////////////////////////////////////////////////////
  4863. function CheckBitfields(var aFormat: TglBitmapFormat; const aMask: TglBitmapRec4ul; const aInfo: TBMPInfo): TbmpBitfieldFormat;
  4864. var
  4865. fd: TFormatDescriptor;
  4866. begin
  4867. result := nil;
  4868. if (aMask.r <> 0) or (aMask.g <> 0) or (aMask.b <> 0) or (aMask.a <> 0) then begin
  4869. // find suitable format ...
  4870. fd := TFormatDescriptor.GetFromMask(aMask);
  4871. if (fd.Format <> tfEmpty) then begin
  4872. aFormat := fd.Format;
  4873. exit;
  4874. end;
  4875. // or create custom bitfield format
  4876. result := TbmpBitfieldFormat.Create;
  4877. result.SetCustomValues(aInfo.biBitCount, aMask);
  4878. end;
  4879. end;
  4880. var
  4881. //simple types
  4882. StartPos: Int64;
  4883. ImageSize, rbLineSize, wbLineSize, Padding, i: Integer;
  4884. PaddingBuff: Cardinal;
  4885. LineBuf, ImageData, TmpData: PByte;
  4886. SourceMD, DestMD: Pointer;
  4887. BmpFormat: TglBitmapFormat;
  4888. //records
  4889. Mask: TglBitmapRec4ul;
  4890. Header: TBMPHeader;
  4891. Info: TBMPInfo;
  4892. //classes
  4893. SpecialFormat: TFormatDescriptor;
  4894. FormatDesc: TFormatDescriptor;
  4895. //////////////////////////////////////////////////////////////////////////////////////////////////
  4896. procedure SpecialFormatReadLine(aData: PByte; aLineBuf: PByte);
  4897. var
  4898. i: Integer;
  4899. Pixel: TglBitmapPixelData;
  4900. begin
  4901. aStream.Read(aLineBuf^, rbLineSize);
  4902. SpecialFormat.PreparePixel(Pixel);
  4903. for i := 0 to Info.biWidth-1 do begin
  4904. SpecialFormat.Unmap(aLineBuf, Pixel, SourceMD);
  4905. glBitmapConvertPixel(Pixel, SpecialFormat, FormatDesc);
  4906. FormatDesc.Map(Pixel, aData, DestMD);
  4907. end;
  4908. end;
  4909. begin
  4910. result := false;
  4911. BmpFormat := tfEmpty;
  4912. SpecialFormat := nil;
  4913. LineBuf := nil;
  4914. SourceMD := nil;
  4915. DestMD := nil;
  4916. // Header
  4917. StartPos := aStream.Position;
  4918. aStream.Read(Header{%H-}, SizeOf(Header));
  4919. if Header.bfType = BMP_MAGIC then begin
  4920. try try
  4921. BmpFormat := ReadInfo(Info, Mask);
  4922. SpecialFormat := ReadColorTable(BmpFormat, Info);
  4923. if not Assigned(SpecialFormat) then
  4924. SpecialFormat := CheckBitfields(BmpFormat, Mask, Info);
  4925. aStream.Position := StartPos + Header.bfOffBits;
  4926. if (BmpFormat <> tfEmpty) then begin
  4927. FormatDesc := TFormatDescriptor.Get(BmpFormat);
  4928. rbLineSize := Round(Info.biWidth * Info.biBitCount / 8); //ReadBuffer LineSize
  4929. wbLineSize := Trunc(Info.biWidth * FormatDesc.BytesPerPixel);
  4930. Padding := (((Info.biWidth * Info.biBitCount + 31) and - 32) shr 3) - rbLineSize;
  4931. //get Memory
  4932. DestMD := FormatDesc.CreateMappingData;
  4933. ImageSize := FormatDesc.GetSize(Info.biWidth, abs(Info.biHeight));
  4934. GetMem(ImageData, ImageSize);
  4935. if Assigned(SpecialFormat) then begin
  4936. GetMem(LineBuf, rbLineSize); //tmp Memory for converting Bitfields
  4937. SourceMD := SpecialFormat.CreateMappingData;
  4938. end;
  4939. //read Data
  4940. try try
  4941. FillChar(ImageData^, ImageSize, $FF);
  4942. TmpData := ImageData;
  4943. if (Info.biHeight > 0) then
  4944. Inc(TmpData, wbLineSize * (Info.biHeight-1));
  4945. for i := 0 to Abs(Info.biHeight)-1 do begin
  4946. if Assigned(SpecialFormat) then
  4947. SpecialFormatReadLine(TmpData, LineBuf) //if is special format read and convert data
  4948. else
  4949. aStream.Read(TmpData^, wbLineSize); //else only read data
  4950. if (Info.biHeight > 0) then
  4951. dec(TmpData, wbLineSize)
  4952. else
  4953. inc(TmpData, wbLineSize);
  4954. aStream.Read(PaddingBuff{%H-}, Padding);
  4955. end;
  4956. SetData(ImageData, BmpFormat, Info.biWidth, abs(Info.biHeight));
  4957. result := true;
  4958. finally
  4959. if Assigned(LineBuf) then
  4960. FreeMem(LineBuf);
  4961. if Assigned(SourceMD) then
  4962. SpecialFormat.FreeMappingData(SourceMD);
  4963. FormatDesc.FreeMappingData(DestMD);
  4964. end;
  4965. except
  4966. if Assigned(ImageData) then
  4967. FreeMem(ImageData);
  4968. raise;
  4969. end;
  4970. end else
  4971. raise EglBitmap.Create('LoadBMP - No suitable format found');
  4972. except
  4973. aStream.Position := StartPos;
  4974. raise;
  4975. end;
  4976. finally
  4977. FreeAndNil(SpecialFormat);
  4978. end;
  4979. end
  4980. else aStream.Position := StartPos;
  4981. end;
  4982. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4983. procedure TglBitmapData.SaveBMP(const aStream: TStream);
  4984. var
  4985. Header: TBMPHeader;
  4986. Info: TBMPInfo;
  4987. Converter: TFormatDescriptor;
  4988. FormatDesc: TFormatDescriptor;
  4989. SourceFD, DestFD: Pointer;
  4990. pData, srcData, dstData, ConvertBuffer: pByte;
  4991. Pixel: TglBitmapPixelData;
  4992. ImageSize, wbLineSize, rbLineSize, Padding, LineIdx, PixelIdx: Integer;
  4993. RedMask, GreenMask, BlueMask, AlphaMask: Cardinal;
  4994. PaddingBuff: Cardinal;
  4995. function GetLineWidth : Integer;
  4996. begin
  4997. result := ((Info.biWidth * Info.biBitCount + 31) and - 32) shr 3;
  4998. end;
  4999. begin
  5000. if not (ftBMP in FormatGetSupportedFiles(Format)) then
  5001. raise EglBitmapUnsupportedFormat.Create(Format);
  5002. Converter := nil;
  5003. FormatDesc := TFormatDescriptor.Get(Format);
  5004. ImageSize := FormatDesc.GetSize(Dimension);
  5005. FillChar(Header{%H-}, SizeOf(Header), 0);
  5006. Header.bfType := BMP_MAGIC;
  5007. Header.bfSize := SizeOf(Header) + SizeOf(Info) + ImageSize;
  5008. Header.bfReserved1 := 0;
  5009. Header.bfReserved2 := 0;
  5010. Header.bfOffBits := SizeOf(Header) + SizeOf(Info);
  5011. FillChar(Info{%H-}, SizeOf(Info), 0);
  5012. Info.biSize := SizeOf(Info);
  5013. Info.biWidth := Width;
  5014. Info.biHeight := Height;
  5015. Info.biPlanes := 1;
  5016. Info.biCompression := BMP_COMP_RGB;
  5017. Info.biSizeImage := ImageSize;
  5018. try
  5019. case Format of
  5020. tfAlpha4ub1, tfAlpha8ub1, tfLuminance4ub1, tfLuminance8ub1, tfR3G3B2ub1:
  5021. begin
  5022. Info.biBitCount := 8;
  5023. Header.bfSize := Header.bfSize + 256 * SizeOf(Cardinal);
  5024. Header.bfOffBits := Header.bfOffBits + 256 * SizeOf(Cardinal); //256 ColorTable entries
  5025. Converter := TbmpColorTableFormat.Create;
  5026. with (Converter as TbmpColorTableFormat) do begin
  5027. SetCustomValues(fFormat, 8, FormatDesc.Precision, FormatDesc.Shift);
  5028. CreateColorTable;
  5029. end;
  5030. end;
  5031. tfLuminance4Alpha4ub2, tfLuminance6Alpha2ub2, tfLuminance8Alpha8ub2,
  5032. tfRGBX4us1, tfXRGB4us1, tfRGB5X1us1, tfX1RGB5us1, tfR5G6B5us1, tfRGB5A1us1, tfA1RGB5us1, tfRGBA4us1, tfARGB4us1,
  5033. tfBGRX4us1, tfXBGR4us1, tfBGR5X1us1, tfX1BGR5us1, tfB5G6R5us1, tfBGR5A1us1, tfA1BGR5us1, tfBGRA4us1, tfABGR4us1:
  5034. begin
  5035. Info.biBitCount := 16;
  5036. Info.biCompression := BMP_COMP_BITFIELDS;
  5037. end;
  5038. tfBGR8ub3, tfRGB8ub3:
  5039. begin
  5040. Info.biBitCount := 24;
  5041. if (Format = tfRGB8ub3) then
  5042. Converter := TfdBGR8ub3.Create; //use BGR8 Format Descriptor to Swap RGB Values
  5043. end;
  5044. tfRGBX8ui1, tfXRGB8ui1, tfRGB10X2ui1, tfX2RGB10ui1, tfRGBA8ui1, tfARGB8ui1, tfRGBA8ub4, tfRGB10A2ui1, tfA2RGB10ui1,
  5045. tfBGRX8ui1, tfXBGR8ui1, tfBGR10X2ui1, tfX2BGR10ui1, tfBGRA8ui1, tfABGR8ui1, tfBGRA8ub4, tfBGR10A2ui1, tfA2BGR10ui1:
  5046. begin
  5047. Info.biBitCount := 32;
  5048. Info.biCompression := BMP_COMP_BITFIELDS;
  5049. end;
  5050. else
  5051. raise EglBitmapUnsupportedFormat.Create(Format);
  5052. end;
  5053. Info.biXPelsPerMeter := 2835;
  5054. Info.biYPelsPerMeter := 2835;
  5055. // prepare bitmasks
  5056. if Info.biCompression = BMP_COMP_BITFIELDS then begin
  5057. Header.bfSize := Header.bfSize + 4 * SizeOf(Cardinal);
  5058. Header.bfOffBits := Header.bfOffBits + 4 * SizeOf(Cardinal);
  5059. RedMask := FormatDesc.Mask.r;
  5060. GreenMask := FormatDesc.Mask.g;
  5061. BlueMask := FormatDesc.Mask.b;
  5062. AlphaMask := FormatDesc.Mask.a;
  5063. end;
  5064. // headers
  5065. aStream.Write(Header, SizeOf(Header));
  5066. aStream.Write(Info, SizeOf(Info));
  5067. // colortable
  5068. if Assigned(Converter) and (Converter is TbmpColorTableFormat) then
  5069. with (Converter as TbmpColorTableFormat) do
  5070. aStream.Write(ColorTable[0].b,
  5071. SizeOf(TbmpColorTableEnty) * Length(ColorTable));
  5072. // bitmasks
  5073. if Info.biCompression = BMP_COMP_BITFIELDS then begin
  5074. aStream.Write(RedMask, SizeOf(Cardinal));
  5075. aStream.Write(GreenMask, SizeOf(Cardinal));
  5076. aStream.Write(BlueMask, SizeOf(Cardinal));
  5077. aStream.Write(AlphaMask, SizeOf(Cardinal));
  5078. end;
  5079. // image data
  5080. rbLineSize := Round(Info.biWidth * FormatDesc.BytesPerPixel);
  5081. wbLineSize := Round(Info.biWidth * Info.biBitCount / 8);
  5082. Padding := GetLineWidth - wbLineSize;
  5083. PaddingBuff := 0;
  5084. pData := Data;
  5085. inc(pData, (Height-1) * rbLineSize);
  5086. // prepare row buffer. But only for RGB because RGBA supports color masks
  5087. // so it's possible to change color within the image.
  5088. if Assigned(Converter) then begin
  5089. FormatDesc.PreparePixel(Pixel);
  5090. GetMem(ConvertBuffer, wbLineSize);
  5091. SourceFD := FormatDesc.CreateMappingData;
  5092. DestFD := Converter.CreateMappingData;
  5093. end else
  5094. ConvertBuffer := nil;
  5095. try
  5096. for LineIdx := 0 to Height - 1 do begin
  5097. // preparing row
  5098. if Assigned(Converter) then begin
  5099. srcData := pData;
  5100. dstData := ConvertBuffer;
  5101. for PixelIdx := 0 to Info.biWidth-1 do begin
  5102. FormatDesc.Unmap(srcData, Pixel, SourceFD);
  5103. glBitmapConvertPixel(Pixel, FormatDesc, Converter);
  5104. Converter.Map(Pixel, dstData, DestFD);
  5105. end;
  5106. aStream.Write(ConvertBuffer^, wbLineSize);
  5107. end else begin
  5108. aStream.Write(pData^, rbLineSize);
  5109. end;
  5110. dec(pData, rbLineSize);
  5111. if (Padding > 0) then
  5112. aStream.Write(PaddingBuff, Padding);
  5113. end;
  5114. finally
  5115. // destroy row buffer
  5116. if Assigned(ConvertBuffer) then begin
  5117. FormatDesc.FreeMappingData(SourceFD);
  5118. Converter.FreeMappingData(DestFD);
  5119. FreeMem(ConvertBuffer);
  5120. end;
  5121. end;
  5122. finally
  5123. if Assigned(Converter) then
  5124. Converter.Free;
  5125. end;
  5126. end;
  5127. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5128. //TGA/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5129. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5130. type
  5131. TTGAHeader = packed record
  5132. ImageID: Byte;
  5133. ColorMapType: Byte;
  5134. ImageType: Byte;
  5135. //ColorMapSpec: Array[0..4] of Byte;
  5136. ColorMapStart: Word;
  5137. ColorMapLength: Word;
  5138. ColorMapEntrySize: Byte;
  5139. OrigX: Word;
  5140. OrigY: Word;
  5141. Width: Word;
  5142. Height: Word;
  5143. Bpp: Byte;
  5144. ImageDesc: Byte;
  5145. end;
  5146. const
  5147. TGA_UNCOMPRESSED_RGB = 2;
  5148. TGA_UNCOMPRESSED_GRAY = 3;
  5149. TGA_COMPRESSED_RGB = 10;
  5150. TGA_COMPRESSED_GRAY = 11;
  5151. TGA_NONE_COLOR_TABLE = 0;
  5152. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5153. function TglBitmapData.LoadTGA(const aStream: TStream): Boolean;
  5154. var
  5155. Header: TTGAHeader;
  5156. ImageData: System.PByte;
  5157. StartPosition: Int64;
  5158. PixelSize, LineSize: Integer;
  5159. tgaFormat: TglBitmapFormat;
  5160. FormatDesc: TFormatDescriptor;
  5161. Counter: packed record
  5162. X, Y: packed record
  5163. low, high, dir: Integer;
  5164. end;
  5165. end;
  5166. const
  5167. CACHE_SIZE = $4000;
  5168. ////////////////////////////////////////////////////////////////////////////////////////
  5169. procedure ReadUncompressed;
  5170. var
  5171. i, j: Integer;
  5172. buf, tmp1, tmp2: System.PByte;
  5173. begin
  5174. buf := nil;
  5175. if (Counter.X.dir < 0) then
  5176. GetMem(buf, LineSize);
  5177. try
  5178. while (Counter.Y.low <> Counter.Y.high + counter.Y.dir) do begin
  5179. tmp1 := ImageData;
  5180. inc(tmp1, (Counter.Y.low * LineSize)); //pointer to LineStart
  5181. if (Counter.X.dir < 0) then begin //flip X
  5182. aStream.Read(buf^, LineSize);
  5183. tmp2 := buf;
  5184. inc(tmp2, LineSize - PixelSize); //pointer to last pixel in line
  5185. for i := 0 to Header.Width-1 do begin //for all pixels in line
  5186. for j := 0 to PixelSize-1 do begin //for all bytes in pixel
  5187. tmp1^ := tmp2^;
  5188. inc(tmp1);
  5189. inc(tmp2);
  5190. end;
  5191. dec(tmp2, 2*PixelSize); //move 2 backwards, because j-loop moved 1 forward
  5192. end;
  5193. end else
  5194. aStream.Read(tmp1^, LineSize);
  5195. inc(Counter.Y.low, Counter.Y.dir); //move to next line index
  5196. end;
  5197. finally
  5198. if Assigned(buf) then
  5199. FreeMem(buf);
  5200. end;
  5201. end;
  5202. ////////////////////////////////////////////////////////////////////////////////////////
  5203. procedure ReadCompressed;
  5204. /////////////////////////////////////////////////////////////////
  5205. var
  5206. TmpData: System.PByte;
  5207. LinePixelsRead: Integer;
  5208. procedure CheckLine;
  5209. begin
  5210. if (LinePixelsRead >= Header.Width) then begin
  5211. LinePixelsRead := 0;
  5212. inc(Counter.Y.low, Counter.Y.dir); //next line index
  5213. TmpData := ImageData;
  5214. inc(TmpData, Counter.Y.low * LineSize); //set line
  5215. if (Counter.X.dir < 0) then //if x flipped then
  5216. inc(TmpData, LineSize - PixelSize); //set last pixel
  5217. end;
  5218. end;
  5219. /////////////////////////////////////////////////////////////////
  5220. var
  5221. Cache: PByte;
  5222. CacheSize, CachePos: Integer;
  5223. procedure CachedRead(out Buffer; Count: Integer);
  5224. var
  5225. BytesRead: Integer;
  5226. begin
  5227. if (CachePos + Count > CacheSize) then begin
  5228. //if buffer overflow save non read bytes
  5229. BytesRead := 0;
  5230. if (CacheSize - CachePos > 0) then begin
  5231. BytesRead := CacheSize - CachePos;
  5232. Move(PByteArray(Cache)^[CachePos], Buffer{%H-}, BytesRead);
  5233. inc(CachePos, BytesRead);
  5234. end;
  5235. //load cache from file
  5236. CacheSize := Min(CACHE_SIZE, aStream.Size - aStream.Position);
  5237. aStream.Read(Cache^, CacheSize);
  5238. CachePos := 0;
  5239. //read rest of requested bytes
  5240. if (Count - BytesRead > 0) then begin
  5241. Move(PByteArray(Cache)^[CachePos], TByteArray(Buffer)[BytesRead], Count - BytesRead);
  5242. inc(CachePos, Count - BytesRead);
  5243. end;
  5244. end else begin
  5245. //if no buffer overflow just read the data
  5246. Move(PByteArray(Cache)^[CachePos], Buffer, Count);
  5247. inc(CachePos, Count);
  5248. end;
  5249. end;
  5250. procedure PixelToBuffer(const aData: PByte; var aBuffer: PByte);
  5251. begin
  5252. case PixelSize of
  5253. 1: begin
  5254. aBuffer^ := aData^;
  5255. inc(aBuffer, Counter.X.dir);
  5256. end;
  5257. 2: begin
  5258. PWord(aBuffer)^ := PWord(aData)^;
  5259. inc(aBuffer, 2 * Counter.X.dir);
  5260. end;
  5261. 3: begin
  5262. PByteArray(aBuffer)^[0] := PByteArray(aData)^[0];
  5263. PByteArray(aBuffer)^[1] := PByteArray(aData)^[1];
  5264. PByteArray(aBuffer)^[2] := PByteArray(aData)^[2];
  5265. inc(aBuffer, 3 * Counter.X.dir);
  5266. end;
  5267. 4: begin
  5268. PCardinal(aBuffer)^ := PCardinal(aData)^;
  5269. inc(aBuffer, 4 * Counter.X.dir);
  5270. end;
  5271. end;
  5272. end;
  5273. var
  5274. TotalPixelsToRead, TotalPixelsRead: Integer;
  5275. Temp: Byte;
  5276. buf: array [0..3] of Byte; //1 pixel is max 32bit long
  5277. PixelRepeat: Boolean;
  5278. PixelsToRead, PixelCount: Integer;
  5279. begin
  5280. CacheSize := 0;
  5281. CachePos := 0;
  5282. TotalPixelsToRead := Header.Width * Header.Height;
  5283. TotalPixelsRead := 0;
  5284. LinePixelsRead := 0;
  5285. GetMem(Cache, CACHE_SIZE);
  5286. try
  5287. TmpData := ImageData;
  5288. inc(TmpData, Counter.Y.low * LineSize); //set line
  5289. if (Counter.X.dir < 0) then //if x flipped then
  5290. inc(TmpData, LineSize - PixelSize); //set last pixel
  5291. repeat
  5292. //read CommandByte
  5293. CachedRead(Temp, 1);
  5294. PixelRepeat := (Temp and $80) > 0;
  5295. PixelsToRead := (Temp and $7F) + 1;
  5296. inc(TotalPixelsRead, PixelsToRead);
  5297. if PixelRepeat then
  5298. CachedRead(buf[0], PixelSize);
  5299. while (PixelsToRead > 0) do begin
  5300. CheckLine;
  5301. PixelCount := Min(Header.Width - LinePixelsRead, PixelsToRead); //max read to EOL or EOF
  5302. while (PixelCount > 0) do begin
  5303. if not PixelRepeat then
  5304. CachedRead(buf[0], PixelSize);
  5305. PixelToBuffer(@buf[0], TmpData);
  5306. inc(LinePixelsRead);
  5307. dec(PixelsToRead);
  5308. dec(PixelCount);
  5309. end;
  5310. end;
  5311. until (TotalPixelsRead >= TotalPixelsToRead);
  5312. finally
  5313. FreeMem(Cache);
  5314. end;
  5315. end;
  5316. function IsGrayFormat: Boolean;
  5317. begin
  5318. result := Header.ImageType in [TGA_UNCOMPRESSED_GRAY, TGA_COMPRESSED_GRAY];
  5319. end;
  5320. begin
  5321. result := false;
  5322. // reading header to test file and set cursor back to begin
  5323. StartPosition := aStream.Position;
  5324. aStream.Read(Header{%H-}, SizeOf(Header));
  5325. // no colormapped files
  5326. if (Header.ColorMapType = TGA_NONE_COLOR_TABLE) and (Header.ImageType in [
  5327. TGA_UNCOMPRESSED_RGB, TGA_UNCOMPRESSED_GRAY, TGA_COMPRESSED_RGB, TGA_COMPRESSED_GRAY]) then
  5328. begin
  5329. try
  5330. if Header.ImageID <> 0 then // skip image ID
  5331. aStream.Position := aStream.Position + Header.ImageID;
  5332. tgaFormat := tfEmpty;
  5333. case Header.Bpp of
  5334. 8: if IsGrayFormat then case (Header.ImageDesc and $F) of
  5335. 0: tgaFormat := tfLuminance8ub1;
  5336. 8: tgaFormat := tfAlpha8ub1;
  5337. end;
  5338. 16: if IsGrayFormat then case (Header.ImageDesc and $F) of
  5339. 0: tgaFormat := tfLuminance16us1;
  5340. 8: tgaFormat := tfLuminance8Alpha8ub2;
  5341. end else case (Header.ImageDesc and $F) of
  5342. 0: tgaFormat := tfX1RGB5us1;
  5343. 1: tgaFormat := tfA1RGB5us1;
  5344. 4: tgaFormat := tfARGB4us1;
  5345. end;
  5346. 24: if not IsGrayFormat then case (Header.ImageDesc and $F) of
  5347. 0: tgaFormat := tfBGR8ub3;
  5348. end;
  5349. 32: if IsGrayFormat then case (Header.ImageDesc and $F) of
  5350. 0: tgaFormat := tfDepth32ui1;
  5351. end else case (Header.ImageDesc and $F) of
  5352. 0: tgaFormat := tfX2RGB10ui1;
  5353. 2: tgaFormat := tfA2RGB10ui1;
  5354. 8: tgaFormat := tfARGB8ui1;
  5355. end;
  5356. end;
  5357. if (tgaFormat = tfEmpty) then
  5358. raise EglBitmap.Create('LoadTga - unsupported format');
  5359. FormatDesc := TFormatDescriptor.Get(tgaFormat);
  5360. PixelSize := FormatDesc.GetSize(1, 1);
  5361. LineSize := FormatDesc.GetSize(Header.Width, 1);
  5362. GetMem(ImageData, LineSize * Header.Height);
  5363. try
  5364. //column direction
  5365. if ((Header.ImageDesc and (1 shl 4)) > 0) then begin
  5366. Counter.X.low := Header.Height-1;;
  5367. Counter.X.high := 0;
  5368. Counter.X.dir := -1;
  5369. end else begin
  5370. Counter.X.low := 0;
  5371. Counter.X.high := Header.Height-1;
  5372. Counter.X.dir := 1;
  5373. end;
  5374. // Row direction
  5375. if ((Header.ImageDesc and (1 shl 5)) > 0) then begin
  5376. Counter.Y.low := 0;
  5377. Counter.Y.high := Header.Height-1;
  5378. Counter.Y.dir := 1;
  5379. end else begin
  5380. Counter.Y.low := Header.Height-1;;
  5381. Counter.Y.high := 0;
  5382. Counter.Y.dir := -1;
  5383. end;
  5384. // Read Image
  5385. case Header.ImageType of
  5386. TGA_UNCOMPRESSED_RGB, TGA_UNCOMPRESSED_GRAY:
  5387. ReadUncompressed;
  5388. TGA_COMPRESSED_RGB, TGA_COMPRESSED_GRAY:
  5389. ReadCompressed;
  5390. end;
  5391. SetData(ImageData, tgaFormat, Header.Width, Header.Height);
  5392. result := true;
  5393. except
  5394. if Assigned(ImageData) then
  5395. FreeMem(ImageData);
  5396. raise;
  5397. end;
  5398. finally
  5399. aStream.Position := StartPosition;
  5400. end;
  5401. end
  5402. else aStream.Position := StartPosition;
  5403. end;
  5404. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5405. procedure TglBitmapData.SaveTGA(const aStream: TStream);
  5406. var
  5407. Header: TTGAHeader;
  5408. Size: Integer;
  5409. FormatDesc: TFormatDescriptor;
  5410. begin
  5411. if not (ftTGA in FormatGetSupportedFiles(Format)) then
  5412. raise EglBitmapUnsupportedFormat.Create(Format);
  5413. //prepare header
  5414. FormatDesc := TFormatDescriptor.Get(Format);
  5415. FillChar(Header{%H-}, SizeOf(Header), 0);
  5416. Header.ImageDesc := CountSetBits(FormatDesc.Range.a) and $F;
  5417. Header.Bpp := FormatDesc.BitsPerPixel;
  5418. Header.Width := Width;
  5419. Header.Height := Height;
  5420. Header.ImageDesc := Header.ImageDesc or $20; //flip y
  5421. if FormatDesc.IsGrayscale or (not FormatDesc.IsGrayscale and not FormatDesc.HasRed and FormatDesc.HasAlpha) then
  5422. Header.ImageType := TGA_UNCOMPRESSED_GRAY
  5423. else
  5424. Header.ImageType := TGA_UNCOMPRESSED_RGB;
  5425. aStream.Write(Header, SizeOf(Header));
  5426. // write Data
  5427. Size := FormatDesc.GetSize(Dimension);
  5428. aStream.Write(Data^, Size);
  5429. end;
  5430. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5431. //DDS/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5432. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5433. const
  5434. DDS_MAGIC: Cardinal = $20534444;
  5435. // DDS_header.dwFlags
  5436. DDSD_CAPS = $00000001;
  5437. DDSD_HEIGHT = $00000002;
  5438. DDSD_WIDTH = $00000004;
  5439. DDSD_PIXELFORMAT = $00001000;
  5440. // DDS_header.sPixelFormat.dwFlags
  5441. DDPF_ALPHAPIXELS = $00000001;
  5442. DDPF_ALPHA = $00000002;
  5443. DDPF_FOURCC = $00000004;
  5444. DDPF_RGB = $00000040;
  5445. DDPF_LUMINANCE = $00020000;
  5446. // DDS_header.sCaps.dwCaps1
  5447. DDSCAPS_TEXTURE = $00001000;
  5448. // DDS_header.sCaps.dwCaps2
  5449. DDSCAPS2_CUBEMAP = $00000200;
  5450. D3DFMT_DXT1 = $31545844;
  5451. D3DFMT_DXT3 = $33545844;
  5452. D3DFMT_DXT5 = $35545844;
  5453. type
  5454. TDDSPixelFormat = packed record
  5455. dwSize: Cardinal;
  5456. dwFlags: Cardinal;
  5457. dwFourCC: Cardinal;
  5458. dwRGBBitCount: Cardinal;
  5459. dwRBitMask: Cardinal;
  5460. dwGBitMask: Cardinal;
  5461. dwBBitMask: Cardinal;
  5462. dwABitMask: Cardinal;
  5463. end;
  5464. TDDSCaps = packed record
  5465. dwCaps1: Cardinal;
  5466. dwCaps2: Cardinal;
  5467. dwDDSX: Cardinal;
  5468. dwReserved: Cardinal;
  5469. end;
  5470. TDDSHeader = packed record
  5471. dwSize: Cardinal;
  5472. dwFlags: Cardinal;
  5473. dwHeight: Cardinal;
  5474. dwWidth: Cardinal;
  5475. dwPitchOrLinearSize: Cardinal;
  5476. dwDepth: Cardinal;
  5477. dwMipMapCount: Cardinal;
  5478. dwReserved: array[0..10] of Cardinal;
  5479. PixelFormat: TDDSPixelFormat;
  5480. Caps: TDDSCaps;
  5481. dwReserved2: Cardinal;
  5482. end;
  5483. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5484. function TglBitmapData.LoadDDS(const aStream: TStream): Boolean;
  5485. var
  5486. Header: TDDSHeader;
  5487. Converter: TbmpBitfieldFormat;
  5488. function GetDDSFormat: TglBitmapFormat;
  5489. var
  5490. fd: TFormatDescriptor;
  5491. i: Integer;
  5492. Mask: TglBitmapRec4ul;
  5493. Range: TglBitmapRec4ui;
  5494. match: Boolean;
  5495. begin
  5496. result := tfEmpty;
  5497. with Header.PixelFormat do begin
  5498. // Compresses
  5499. if ((dwFlags and DDPF_FOURCC) > 0) then begin
  5500. case Header.PixelFormat.dwFourCC of
  5501. D3DFMT_DXT1: result := tfS3tcDtx1RGBA;
  5502. D3DFMT_DXT3: result := tfS3tcDtx3RGBA;
  5503. D3DFMT_DXT5: result := tfS3tcDtx5RGBA;
  5504. end;
  5505. end else if ((dwFlags and (DDPF_RGB or DDPF_ALPHAPIXELS or DDPF_LUMINANCE or DDPF_ALPHA)) > 0) then begin
  5506. // prepare masks
  5507. if ((dwFlags and DDPF_LUMINANCE) = 0) then begin
  5508. Mask.r := dwRBitMask;
  5509. Mask.g := dwGBitMask;
  5510. Mask.b := dwBBitMask;
  5511. end else begin
  5512. Mask.r := dwRBitMask;
  5513. Mask.g := dwRBitMask;
  5514. Mask.b := dwRBitMask;
  5515. end;
  5516. if (dwFlags and DDPF_ALPHAPIXELS > 0) then
  5517. Mask.a := dwABitMask
  5518. else
  5519. Mask.a := 0;;
  5520. //find matching format
  5521. fd := TFormatDescriptor.GetFromMask(Mask, dwRGBBitCount);
  5522. result := fd.Format;
  5523. if (result <> tfEmpty) then
  5524. exit;
  5525. //find format with same Range
  5526. for i := 0 to 3 do
  5527. Range.arr[i] := (2 shl CountSetBits(Mask.arr[i])) - 1;
  5528. for result := High(TglBitmapFormat) downto Low(TglBitmapFormat) do begin
  5529. fd := TFormatDescriptor.Get(result);
  5530. match := true;
  5531. for i := 0 to 3 do
  5532. if (fd.Range.arr[i] <> Range.arr[i]) then begin
  5533. match := false;
  5534. break;
  5535. end;
  5536. if match then
  5537. break;
  5538. end;
  5539. //no format with same range found -> use default
  5540. if (result = tfEmpty) then begin
  5541. if (dwABitMask > 0) then
  5542. result := tfRGBA8ui1
  5543. else
  5544. result := tfRGB8ub3;
  5545. end;
  5546. Converter := TbmpBitfieldFormat.Create;
  5547. Converter.SetCustomValues(dwRGBBitCount, glBitmapRec4ul(dwRBitMask, dwGBitMask, dwBBitMask, dwABitMask));
  5548. end;
  5549. end;
  5550. end;
  5551. var
  5552. StreamPos: Int64;
  5553. x, y, LineSize, RowSize, Magic: Cardinal;
  5554. NewImage, TmpData, RowData, SrcData: System.PByte;
  5555. SourceMD, DestMD: Pointer;
  5556. Pixel: TglBitmapPixelData;
  5557. ddsFormat: TglBitmapFormat;
  5558. FormatDesc: TFormatDescriptor;
  5559. begin
  5560. result := false;
  5561. Converter := nil;
  5562. StreamPos := aStream.Position;
  5563. // Magic
  5564. aStream.Read(Magic{%H-}, sizeof(Magic));
  5565. if (Magic <> DDS_MAGIC) then begin
  5566. aStream.Position := StreamPos;
  5567. exit;
  5568. end;
  5569. //Header
  5570. aStream.Read(Header{%H-}, sizeof(Header));
  5571. if (Header.dwSize <> SizeOf(Header)) or
  5572. ((Header.dwFlags and (DDSD_PIXELFORMAT or DDSD_CAPS or DDSD_WIDTH or DDSD_HEIGHT)) <>
  5573. (DDSD_PIXELFORMAT or DDSD_CAPS or DDSD_WIDTH or DDSD_HEIGHT)) then
  5574. begin
  5575. aStream.Position := StreamPos;
  5576. exit;
  5577. end;
  5578. if ((Header.Caps.dwCaps1 and DDSCAPS2_CUBEMAP) > 0) then
  5579. raise EglBitmap.Create('LoadDDS - CubeMaps are not supported');
  5580. ddsFormat := GetDDSFormat;
  5581. try
  5582. if (ddsFormat = tfEmpty) then
  5583. raise EglBitmap.Create('LoadDDS - unsupported Pixelformat found.');
  5584. FormatDesc := TFormatDescriptor.Get(ddsFormat);
  5585. LineSize := Trunc(Header.dwWidth * FormatDesc.BytesPerPixel);
  5586. GetMem(NewImage, Header.dwHeight * LineSize);
  5587. try
  5588. TmpData := NewImage;
  5589. //Converter needed
  5590. if Assigned(Converter) then begin
  5591. RowSize := Round(Header.dwWidth * Header.PixelFormat.dwRGBBitCount / 8);
  5592. GetMem(RowData, RowSize);
  5593. SourceMD := Converter.CreateMappingData;
  5594. DestMD := FormatDesc.CreateMappingData;
  5595. try
  5596. for y := 0 to Header.dwHeight-1 do begin
  5597. TmpData := NewImage;
  5598. inc(TmpData, y * LineSize);
  5599. SrcData := RowData;
  5600. aStream.Read(SrcData^, RowSize);
  5601. for x := 0 to Header.dwWidth-1 do begin
  5602. Converter.Unmap(SrcData, Pixel, SourceMD);
  5603. glBitmapConvertPixel(Pixel, Converter, FormatDesc);
  5604. FormatDesc.Map(Pixel, TmpData, DestMD);
  5605. end;
  5606. end;
  5607. finally
  5608. Converter.FreeMappingData(SourceMD);
  5609. FormatDesc.FreeMappingData(DestMD);
  5610. FreeMem(RowData);
  5611. end;
  5612. end else
  5613. // Compressed
  5614. if ((Header.PixelFormat.dwFlags and DDPF_FOURCC) > 0) then begin
  5615. RowSize := Header.dwPitchOrLinearSize div Header.dwWidth;
  5616. for Y := 0 to Header.dwHeight-1 do begin
  5617. aStream.Read(TmpData^, RowSize);
  5618. Inc(TmpData, LineSize);
  5619. end;
  5620. end else
  5621. // Uncompressed
  5622. if (Header.PixelFormat.dwFlags and (DDPF_RGB or DDPF_ALPHAPIXELS or DDPF_LUMINANCE)) > 0 then begin
  5623. RowSize := (Header.PixelFormat.dwRGBBitCount * Header.dwWidth) shr 3;
  5624. for Y := 0 to Header.dwHeight-1 do begin
  5625. aStream.Read(TmpData^, RowSize);
  5626. Inc(TmpData, LineSize);
  5627. end;
  5628. end else
  5629. raise EglBitmap.Create('LoadDDS - unsupported Pixelformat found.');
  5630. SetData(NewImage, ddsFormat, Header.dwWidth, Header.dwHeight);
  5631. result := true;
  5632. except
  5633. if Assigned(NewImage) then
  5634. FreeMem(NewImage);
  5635. raise;
  5636. end;
  5637. finally
  5638. FreeAndNil(Converter);
  5639. end;
  5640. end;
  5641. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5642. procedure TglBitmapData.SaveDDS(const aStream: TStream);
  5643. var
  5644. Header: TDDSHeader;
  5645. FormatDesc: TFormatDescriptor;
  5646. begin
  5647. if not (ftDDS in FormatGetSupportedFiles(Format)) then
  5648. raise EglBitmapUnsupportedFormat.Create(Format);
  5649. FormatDesc := TFormatDescriptor.Get(Format);
  5650. // Generell
  5651. FillChar(Header{%H-}, SizeOf(Header), 0);
  5652. Header.dwSize := SizeOf(Header);
  5653. Header.dwFlags := DDSD_WIDTH or DDSD_HEIGHT or DDSD_CAPS or DDSD_PIXELFORMAT;
  5654. Header.dwWidth := Max(1, Width);
  5655. Header.dwHeight := Max(1, Height);
  5656. // Caps
  5657. Header.Caps.dwCaps1 := DDSCAPS_TEXTURE;
  5658. // Pixelformat
  5659. Header.PixelFormat.dwSize := sizeof(Header);
  5660. if (FormatDesc.IsCompressed) then begin
  5661. Header.PixelFormat.dwFlags := Header.PixelFormat.dwFlags or DDPF_FOURCC;
  5662. case Format of
  5663. tfS3tcDtx1RGBA: Header.PixelFormat.dwFourCC := D3DFMT_DXT1;
  5664. tfS3tcDtx3RGBA: Header.PixelFormat.dwFourCC := D3DFMT_DXT3;
  5665. tfS3tcDtx5RGBA: Header.PixelFormat.dwFourCC := D3DFMT_DXT5;
  5666. end;
  5667. end else if not FormatDesc.HasColor and FormatDesc.HasAlpha then begin
  5668. Header.PixelFormat.dwFlags := Header.PixelFormat.dwFlags or DDPF_ALPHA;
  5669. Header.PixelFormat.dwRGBBitCount := FormatDesc.BitsPerPixel;
  5670. Header.PixelFormat.dwABitMask := FormatDesc.Mask.a;
  5671. end else if FormatDesc.IsGrayscale then begin
  5672. Header.PixelFormat.dwFlags := Header.PixelFormat.dwFlags or DDPF_LUMINANCE;
  5673. Header.PixelFormat.dwRGBBitCount := FormatDesc.BitsPerPixel;
  5674. Header.PixelFormat.dwRBitMask := FormatDesc.Mask.r;
  5675. Header.PixelFormat.dwABitMask := FormatDesc.Mask.a;
  5676. end else begin
  5677. Header.PixelFormat.dwFlags := Header.PixelFormat.dwFlags or DDPF_RGB;
  5678. Header.PixelFormat.dwRGBBitCount := FormatDesc.BitsPerPixel;
  5679. Header.PixelFormat.dwRBitMask := FormatDesc.Mask.r;
  5680. Header.PixelFormat.dwGBitMask := FormatDesc.Mask.g;
  5681. Header.PixelFormat.dwBBitMask := FormatDesc.Mask.b;
  5682. Header.PixelFormat.dwABitMask := FormatDesc.Mask.a;
  5683. end;
  5684. if (FormatDesc.HasAlpha) then
  5685. Header.PixelFormat.dwFlags := Header.PixelFormat.dwFlags or DDPF_ALPHAPIXELS;
  5686. aStream.Write(DDS_MAGIC, sizeof(DDS_MAGIC));
  5687. aStream.Write(Header, SizeOf(Header));
  5688. aStream.Write(Data^, FormatDesc.GetSize(Dimension));
  5689. end;
  5690. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5691. function TglBitmapData.FlipHorz: Boolean;
  5692. var
  5693. fd: TglBitmapFormatDescriptor;
  5694. Col, RowSize, PixelSize: Integer;
  5695. pTempDest, pDest, pSource: PByte;
  5696. begin
  5697. result := false;
  5698. fd := FormatDescriptor;
  5699. PixelSize := Ceil(fd.BytesPerPixel);
  5700. RowSize := fd.GetSize(Width, 1);
  5701. if Assigned(Data) and not fd.IsCompressed then begin
  5702. pSource := Data;
  5703. GetMem(pDest, RowSize);
  5704. try
  5705. pTempDest := pDest;
  5706. Inc(pTempDest, RowSize);
  5707. for Col := 0 to Width-1 do begin
  5708. dec(pTempDest, PixelSize); //dec before, because ptr is behind last byte of data
  5709. Move(pSource^, pTempDest^, PixelSize);
  5710. Inc(pSource, PixelSize);
  5711. end;
  5712. SetData(pDest, Format, Width);
  5713. result := true;
  5714. except
  5715. if Assigned(pDest) then
  5716. FreeMem(pDest);
  5717. raise;
  5718. end;
  5719. end;
  5720. end;
  5721. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5722. function TglBitmapData.FlipVert: Boolean;
  5723. var
  5724. fd: TglBitmapFormatDescriptor;
  5725. Row, RowSize, PixelSize: Integer;
  5726. TempDestData, DestData, SourceData: PByte;
  5727. begin
  5728. result := false;
  5729. fd := FormatDescriptor;
  5730. PixelSize := Ceil(fd.BytesPerPixel);
  5731. RowSize := fd.GetSize(Width, 1);
  5732. if Assigned(Data) then begin
  5733. SourceData := Data;
  5734. GetMem(DestData, Height * RowSize);
  5735. try
  5736. TempDestData := DestData;
  5737. Inc(TempDestData, Width * (Height -1) * PixelSize);
  5738. for Row := 0 to Height -1 do begin
  5739. Move(SourceData^, TempDestData^, RowSize);
  5740. Dec(TempDestData, RowSize);
  5741. Inc(SourceData, RowSize);
  5742. end;
  5743. SetData(DestData, Format, Width, Height);
  5744. result := true;
  5745. except
  5746. if Assigned(DestData) then
  5747. FreeMem(DestData);
  5748. raise;
  5749. end;
  5750. end;
  5751. end;
  5752. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5753. procedure TglBitmapData.LoadFromFile(const aFilename: String);
  5754. var
  5755. fs: TFileStream;
  5756. begin
  5757. if not FileExists(aFilename) then
  5758. raise EglBitmap.Create('file does not exist: ' + aFilename);
  5759. fs := TFileStream.Create(aFilename, fmOpenRead);
  5760. try
  5761. fs.Position := 0;
  5762. LoadFromStream(fs);
  5763. fFilename := aFilename;
  5764. finally
  5765. fs.Free;
  5766. end;
  5767. end;
  5768. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5769. procedure TglBitmapData.LoadFromStream(const aStream: TStream);
  5770. begin
  5771. {$IFDEF GLB_SUPPORT_PNG_READ}
  5772. if not LoadPNG(aStream) then
  5773. {$ENDIF}
  5774. {$IFDEF GLB_SUPPORT_JPEG_READ}
  5775. if not LoadJPEG(aStream) then
  5776. {$ENDIF}
  5777. if not LoadDDS(aStream) then
  5778. if not LoadTGA(aStream) then
  5779. if not LoadBMP(aStream) then
  5780. if not LoadRAW(aStream) then
  5781. raise EglBitmap.Create('LoadFromStream - Couldn''t load Stream. It''s possible to be an unknow Streamtype.');
  5782. end;
  5783. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5784. procedure TglBitmapData.LoadFromFunc(const aSize: TglBitmapSize; const aFormat: TglBitmapFormat;
  5785. const aFunc: TglBitmapFunction; const aArgs: Pointer);
  5786. var
  5787. tmpData: PByte;
  5788. size: Integer;
  5789. begin
  5790. size := TFormatDescriptor.Get(aFormat).GetSize(aSize);
  5791. GetMem(tmpData, size);
  5792. try
  5793. FillChar(tmpData^, size, #$FF);
  5794. SetData(tmpData, aFormat, aSize.X, aSize.Y);
  5795. except
  5796. if Assigned(tmpData) then
  5797. FreeMem(tmpData);
  5798. raise;
  5799. end;
  5800. Convert(Self, aFunc, false, aFormat, aArgs);
  5801. end;
  5802. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5803. procedure TglBitmapData.LoadFromResource(const aInstance: Cardinal; aResource: String; aResType: PChar);
  5804. var
  5805. rs: TResourceStream;
  5806. begin
  5807. PrepareResType(aResource, aResType);
  5808. rs := TResourceStream.Create(aInstance, aResource, aResType);
  5809. try
  5810. LoadFromStream(rs);
  5811. finally
  5812. rs.Free;
  5813. end;
  5814. end;
  5815. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5816. procedure TglBitmapData.LoadFromResourceID(const aInstance: Cardinal; const aResourceID: Integer; const aResType: PChar);
  5817. var
  5818. rs: TResourceStream;
  5819. begin
  5820. rs := TResourceStream.CreateFromID(aInstance, aResourceID, aResType);
  5821. try
  5822. LoadFromStream(rs);
  5823. finally
  5824. rs.Free;
  5825. end;
  5826. end;
  5827. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5828. procedure TglBitmapData.SaveToFile(const aFilename: String; const aFileType: TglBitmapFileType);
  5829. var
  5830. fs: TFileStream;
  5831. begin
  5832. fs := TFileStream.Create(aFileName, fmCreate);
  5833. try
  5834. fs.Position := 0;
  5835. SaveToStream(fs, aFileType);
  5836. finally
  5837. fs.Free;
  5838. end;
  5839. end;
  5840. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5841. procedure TglBitmapData.SaveToStream(const aStream: TStream; const aFileType: TglBitmapFileType);
  5842. begin
  5843. case aFileType of
  5844. {$IFDEF GLB_SUPPORT_PNG_WRITE}
  5845. ftPNG: SavePNG(aStream);
  5846. {$ENDIF}
  5847. {$IFDEF GLB_SUPPORT_JPEG_WRITE}
  5848. ftJPEG: SaveJPEG(aStream);
  5849. {$ENDIF}
  5850. ftDDS: SaveDDS(aStream);
  5851. ftTGA: SaveTGA(aStream);
  5852. ftBMP: SaveBMP(aStream);
  5853. ftRAW: SaveRAW(aStream);
  5854. end;
  5855. end;
  5856. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5857. function TglBitmapData.Convert(const aFunc: TglBitmapFunction; const aCreateTemp: Boolean; const aArgs: Pointer): Boolean;
  5858. begin
  5859. result := Convert(Self, aFunc, aCreateTemp, Format, aArgs);
  5860. end;
  5861. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5862. function TglBitmapData.Convert(const aSource: TglBitmapData; const aFunc: TglBitmapFunction; aCreateTemp: Boolean;
  5863. const aFormat: TglBitmapFormat; const aArgs: Pointer): Boolean;
  5864. var
  5865. DestData, TmpData, SourceData: pByte;
  5866. TempHeight, TempWidth: Integer;
  5867. SourceFD, DestFD: TFormatDescriptor;
  5868. SourceMD, DestMD: Pointer;
  5869. FuncRec: TglBitmapFunctionRec;
  5870. begin
  5871. Assert(Assigned(Data));
  5872. Assert(Assigned(aSource));
  5873. Assert(Assigned(aSource.Data));
  5874. result := false;
  5875. if Assigned(aSource.Data) and ((aSource.Height > 0) or (aSource.Width > 0)) then begin
  5876. SourceFD := TFormatDescriptor.Get(aSource.Format);
  5877. DestFD := TFormatDescriptor.Get(aFormat);
  5878. if (SourceFD.IsCompressed) then
  5879. raise EglBitmapUnsupportedFormat.Create('compressed formats are not supported: ', SourceFD.Format);
  5880. if (DestFD.IsCompressed) then
  5881. raise EglBitmapUnsupportedFormat.Create('compressed formats are not supported: ', DestFD.Format);
  5882. // inkompatible Formats so CreateTemp
  5883. if (SourceFD.BitsPerPixel <> DestFD.BitsPerPixel) then
  5884. aCreateTemp := true;
  5885. // Values
  5886. TempHeight := Max(1, aSource.Height);
  5887. TempWidth := Max(1, aSource.Width);
  5888. FuncRec.Sender := Self;
  5889. FuncRec.Args := aArgs;
  5890. TmpData := nil;
  5891. if aCreateTemp then begin
  5892. GetMem(TmpData, DestFD.GetSize(TempWidth, TempHeight));
  5893. DestData := TmpData;
  5894. end else
  5895. DestData := Data;
  5896. try
  5897. SourceFD.PreparePixel(FuncRec.Source);
  5898. DestFD.PreparePixel (FuncRec.Dest);
  5899. SourceMD := SourceFD.CreateMappingData;
  5900. DestMD := DestFD.CreateMappingData;
  5901. FuncRec.Size := aSource.Dimension;
  5902. FuncRec.Position.Fields := FuncRec.Size.Fields;
  5903. try
  5904. SourceData := aSource.Data;
  5905. FuncRec.Position.Y := 0;
  5906. while FuncRec.Position.Y < TempHeight do begin
  5907. FuncRec.Position.X := 0;
  5908. while FuncRec.Position.X < TempWidth do begin
  5909. SourceFD.Unmap(SourceData, FuncRec.Source, SourceMD);
  5910. aFunc(FuncRec);
  5911. DestFD.Map(FuncRec.Dest, DestData, DestMD);
  5912. inc(FuncRec.Position.X);
  5913. end;
  5914. inc(FuncRec.Position.Y);
  5915. end;
  5916. // Updating Image or InternalFormat
  5917. if aCreateTemp then
  5918. SetData(TmpData, aFormat, aSource.Width, aSource.Height)
  5919. else if (aFormat <> fFormat) then
  5920. Format := aFormat;
  5921. result := true;
  5922. finally
  5923. SourceFD.FreeMappingData(SourceMD);
  5924. DestFD.FreeMappingData(DestMD);
  5925. end;
  5926. except
  5927. if aCreateTemp and Assigned(TmpData) then
  5928. FreeMem(TmpData);
  5929. raise;
  5930. end;
  5931. end;
  5932. end;
  5933. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5934. function TglBitmapData.ConvertTo(const aFormat: TglBitmapFormat): Boolean;
  5935. var
  5936. SourceFD, DestFD: TFormatDescriptor;
  5937. SourcePD, DestPD: TglBitmapPixelData;
  5938. ShiftData: TShiftData;
  5939. function DataIsIdentical: Boolean;
  5940. begin
  5941. result := SourceFD.MaskMatch(DestFD.Mask);
  5942. end;
  5943. function CanCopyDirect: Boolean;
  5944. begin
  5945. result :=
  5946. ((SourcePD.Range.r = DestPD.Range.r) or (SourcePD.Range.r = 0) or (DestPD.Range.r = 0)) and
  5947. ((SourcePD.Range.g = DestPD.Range.g) or (SourcePD.Range.g = 0) or (DestPD.Range.g = 0)) and
  5948. ((SourcePD.Range.b = DestPD.Range.b) or (SourcePD.Range.b = 0) or (DestPD.Range.b = 0)) and
  5949. ((SourcePD.Range.a = DestPD.Range.a) or (SourcePD.Range.a = 0) or (DestPD.Range.a = 0));
  5950. end;
  5951. function CanShift: Boolean;
  5952. begin
  5953. result :=
  5954. ((SourcePD.Range.r >= DestPD.Range.r) or (SourcePD.Range.r = 0) or (DestPD.Range.r = 0)) and
  5955. ((SourcePD.Range.g >= DestPD.Range.g) or (SourcePD.Range.g = 0) or (DestPD.Range.g = 0)) and
  5956. ((SourcePD.Range.b >= DestPD.Range.b) or (SourcePD.Range.b = 0) or (DestPD.Range.b = 0)) and
  5957. ((SourcePD.Range.a >= DestPD.Range.a) or (SourcePD.Range.a = 0) or (DestPD.Range.a = 0));
  5958. end;
  5959. function GetShift(aSource, aDest: Cardinal) : ShortInt;
  5960. begin
  5961. result := 0;
  5962. while (aSource > aDest) and (aSource > 0) do begin
  5963. inc(result);
  5964. aSource := aSource shr 1;
  5965. end;
  5966. end;
  5967. begin
  5968. if (aFormat <> fFormat) and (aFormat <> tfEmpty) then begin
  5969. SourceFD := TFormatDescriptor.Get(Format);
  5970. DestFD := TFormatDescriptor.Get(aFormat);
  5971. if DataIsIdentical then begin
  5972. result := true;
  5973. Format := aFormat;
  5974. exit;
  5975. end;
  5976. SourceFD.PreparePixel(SourcePD);
  5977. DestFD.PreparePixel (DestPD);
  5978. if CanCopyDirect then
  5979. result := Convert(Self, glBitmapConvertCopyFunc, false, aFormat)
  5980. else if CanShift then begin
  5981. ShiftData.r := GetShift(SourcePD.Range.r, DestPD.Range.r);
  5982. ShiftData.g := GetShift(SourcePD.Range.g, DestPD.Range.g);
  5983. ShiftData.b := GetShift(SourcePD.Range.b, DestPD.Range.b);
  5984. ShiftData.a := GetShift(SourcePD.Range.a, DestPD.Range.a);
  5985. result := Convert(Self, glBitmapConvertShiftRGBAFunc, false, aFormat, @ShiftData);
  5986. end else
  5987. result := Convert(Self, glBitmapConvertCalculateRGBAFunc, false, aFormat);
  5988. end else
  5989. result := true;
  5990. end;
  5991. {$IFDEF GLB_SDL}
  5992. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5993. function TglBitmapData.AssignToSurface(out aSurface: PSDL_Surface): Boolean;
  5994. var
  5995. Row, RowSize: Integer;
  5996. SourceData, TmpData: PByte;
  5997. TempDepth: Integer;
  5998. FormatDesc: TFormatDescriptor;
  5999. function GetRowPointer(Row: Integer): pByte;
  6000. begin
  6001. result := aSurface.pixels;
  6002. Inc(result, Row * RowSize);
  6003. end;
  6004. begin
  6005. result := false;
  6006. FormatDesc := TFormatDescriptor.Get(Format);
  6007. if FormatDesc.IsCompressed then
  6008. raise EglBitmapUnsupportedFormat.Create(Format);
  6009. if Assigned(Data) then begin
  6010. case Trunc(FormatDesc.PixelSize) of
  6011. 1: TempDepth := 8;
  6012. 2: TempDepth := 16;
  6013. 3: TempDepth := 24;
  6014. 4: TempDepth := 32;
  6015. else
  6016. raise EglBitmapUnsupportedFormat.Create(Format);
  6017. end;
  6018. aSurface := SDL_CreateRGBSurface(SDL_SWSURFACE, Width, Height, TempDepth,
  6019. FormatDesc.RedMask, FormatDesc.GreenMask, FormatDesc.BlueMask, FormatDesc.AlphaMask);
  6020. SourceData := Data;
  6021. RowSize := FormatDesc.GetSize(FileWidth, 1);
  6022. for Row := 0 to FileHeight-1 do begin
  6023. TmpData := GetRowPointer(Row);
  6024. if Assigned(TmpData) then begin
  6025. Move(SourceData^, TmpData^, RowSize);
  6026. inc(SourceData, RowSize);
  6027. end;
  6028. end;
  6029. result := true;
  6030. end;
  6031. end;
  6032. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6033. function TglBitmapData.AssignFromSurface(const aSurface: PSDL_Surface): Boolean;
  6034. var
  6035. pSource, pData, pTempData: PByte;
  6036. Row, RowSize, TempWidth, TempHeight: Integer;
  6037. IntFormat: TglBitmapFormat;
  6038. fd: TFormatDescriptor;
  6039. Mask: TglBitmapMask;
  6040. function GetRowPointer(Row: Integer): pByte;
  6041. begin
  6042. result := aSurface^.pixels;
  6043. Inc(result, Row * RowSize);
  6044. end;
  6045. begin
  6046. result := false;
  6047. if (Assigned(aSurface)) then begin
  6048. with aSurface^.format^ do begin
  6049. Mask.r := RMask;
  6050. Mask.g := GMask;
  6051. Mask.b := BMask;
  6052. Mask.a := AMask;
  6053. IntFormat := TFormatDescriptor.GetFromMask(Mask).Format;
  6054. if (IntFormat = tfEmpty) then
  6055. raise EglBitmap.Create('AssignFromSurface - Invalid Pixelformat.');
  6056. end;
  6057. fd := TFormatDescriptor.Get(IntFormat);
  6058. TempWidth := aSurface^.w;
  6059. TempHeight := aSurface^.h;
  6060. RowSize := fd.GetSize(TempWidth, 1);
  6061. GetMem(pData, TempHeight * RowSize);
  6062. try
  6063. pTempData := pData;
  6064. for Row := 0 to TempHeight -1 do begin
  6065. pSource := GetRowPointer(Row);
  6066. if (Assigned(pSource)) then begin
  6067. Move(pSource^, pTempData^, RowSize);
  6068. Inc(pTempData, RowSize);
  6069. end;
  6070. end;
  6071. SetData(pData, IntFormat, TempWidth, TempHeight);
  6072. result := true;
  6073. except
  6074. if Assigned(pData) then
  6075. FreeMem(pData);
  6076. raise;
  6077. end;
  6078. end;
  6079. end;
  6080. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6081. function TglBitmapData.AssignAlphaToSurface(out aSurface: PSDL_Surface): Boolean;
  6082. var
  6083. Row, Col, AlphaInterleave: Integer;
  6084. pSource, pDest: PByte;
  6085. function GetRowPointer(Row: Integer): pByte;
  6086. begin
  6087. result := aSurface.pixels;
  6088. Inc(result, Row * Width);
  6089. end;
  6090. begin
  6091. result := false;
  6092. if Assigned(Data) then begin
  6093. if Format in [tfAlpha8ub1, tfLuminance8Alpha8ub2, tfBGRA8ub4, tfRGBA8ub4] then begin
  6094. aSurface := SDL_CreateRGBSurface(SDL_SWSURFACE, Width, Height, 8, $FF, $FF, $FF, 0);
  6095. AlphaInterleave := 0;
  6096. case Format of
  6097. tfLuminance8Alpha8ub2:
  6098. AlphaInterleave := 1;
  6099. tfBGRA8ub4, tfRGBA8ub4:
  6100. AlphaInterleave := 3;
  6101. end;
  6102. pSource := Data;
  6103. for Row := 0 to Height -1 do begin
  6104. pDest := GetRowPointer(Row);
  6105. if Assigned(pDest) then begin
  6106. for Col := 0 to Width -1 do begin
  6107. Inc(pSource, AlphaInterleave);
  6108. pDest^ := pSource^;
  6109. Inc(pDest);
  6110. Inc(pSource);
  6111. end;
  6112. end;
  6113. end;
  6114. result := true;
  6115. end;
  6116. end;
  6117. end;
  6118. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6119. function TglBitmapData.AddAlphaFromSurface(const aSurface: PSDL_Surface; const aFunc: TglBitmapFunction = nil; const aArgs: Pointer = nil): Boolean;
  6120. var
  6121. bmp: TglBitmap2D;
  6122. begin
  6123. bmp := TglBitmap2D.Create;
  6124. try
  6125. bmp.AssignFromSurface(aSurface);
  6126. result := AddAlphaFromGlBitmap(bmp, aFunc, aArgs);
  6127. finally
  6128. bmp.Free;
  6129. end;
  6130. end;
  6131. {$ENDIF}
  6132. {$IFDEF GLB_DELPHI}
  6133. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6134. function CreateGrayPalette: HPALETTE;
  6135. var
  6136. Idx: Integer;
  6137. Pal: PLogPalette;
  6138. begin
  6139. GetMem(Pal, SizeOf(TLogPalette) + (SizeOf(TPaletteEntry) * 256));
  6140. Pal.palVersion := $300;
  6141. Pal.palNumEntries := 256;
  6142. for Idx := 0 to Pal.palNumEntries - 1 do begin
  6143. Pal.palPalEntry[Idx].peRed := Idx;
  6144. Pal.palPalEntry[Idx].peGreen := Idx;
  6145. Pal.palPalEntry[Idx].peBlue := Idx;
  6146. Pal.palPalEntry[Idx].peFlags := 0;
  6147. end;
  6148. Result := CreatePalette(Pal^);
  6149. FreeMem(Pal);
  6150. end;
  6151. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6152. function TglBitmapData.AssignToBitmap(const aBitmap: TBitmap): Boolean;
  6153. var
  6154. Row, RowSize: Integer;
  6155. pSource, pData: PByte;
  6156. begin
  6157. result := false;
  6158. if Assigned(Data) then begin
  6159. if Assigned(aBitmap) then begin
  6160. aBitmap.Width := Width;
  6161. aBitmap.Height := Height;
  6162. case Format of
  6163. tfAlpha8ub1, tfLuminance8ub1: begin
  6164. aBitmap.PixelFormat := pf8bit;
  6165. aBitmap.Palette := CreateGrayPalette;
  6166. end;
  6167. tfRGB5A1us1:
  6168. aBitmap.PixelFormat := pf15bit;
  6169. tfR5G6B5us1:
  6170. aBitmap.PixelFormat := pf16bit;
  6171. tfRGB8ub3, tfBGR8ub3:
  6172. aBitmap.PixelFormat := pf24bit;
  6173. tfRGBA8ub4, tfBGRA8ub4:
  6174. aBitmap.PixelFormat := pf32bit;
  6175. else
  6176. raise EglBitmap.Create('AssignToBitmap - Invalid Pixelformat.');
  6177. end;
  6178. RowSize := FormatDescriptor.GetSize(Width, 1);
  6179. pSource := Data;
  6180. for Row := 0 to Height-1 do begin
  6181. pData := aBitmap.Scanline[Row];
  6182. Move(pSource^, pData^, RowSize);
  6183. Inc(pSource, RowSize);
  6184. if (Format in [tfRGB8ub3, tfRGBA8ub4]) then // swap RGB(A) to BGR(A)
  6185. SwapRGB(pData, Width, Format = tfRGBA8ub4);
  6186. end;
  6187. result := true;
  6188. end;
  6189. end;
  6190. end;
  6191. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6192. function TglBitmapData.AssignFromBitmap(const aBitmap: TBitmap): Boolean;
  6193. var
  6194. pSource, pData, pTempData: PByte;
  6195. Row, RowSize, TempWidth, TempHeight: Integer;
  6196. IntFormat: TglBitmapFormat;
  6197. begin
  6198. result := false;
  6199. if (Assigned(aBitmap)) then begin
  6200. case aBitmap.PixelFormat of
  6201. pf8bit:
  6202. IntFormat := tfLuminance8ub1;
  6203. pf15bit:
  6204. IntFormat := tfRGB5A1us1;
  6205. pf16bit:
  6206. IntFormat := tfR5G6B5us1;
  6207. pf24bit:
  6208. IntFormat := tfBGR8ub3;
  6209. pf32bit:
  6210. IntFormat := tfXRGB8ui1;
  6211. else
  6212. raise EglBitmap.Create('AssignFromBitmap - Invalid Pixelformat.');
  6213. end;
  6214. TempWidth := aBitmap.Width;
  6215. TempHeight := aBitmap.Height;
  6216. RowSize := TFormatDescriptor.Get(IntFormat).GetSize(TempWidth, 1);
  6217. GetMem(pData, TempHeight * RowSize);
  6218. try
  6219. pTempData := pData;
  6220. for Row := 0 to TempHeight -1 do begin
  6221. pSource := aBitmap.Scanline[Row];
  6222. if (Assigned(pSource)) then begin
  6223. Move(pSource^, pTempData^, RowSize);
  6224. Inc(pTempData, RowSize);
  6225. end;
  6226. end;
  6227. SetData(pData, IntFormat, TempWidth, TempHeight);
  6228. result := true;
  6229. except
  6230. if Assigned(pData) then
  6231. FreeMem(pData);
  6232. raise;
  6233. end;
  6234. end;
  6235. end;
  6236. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6237. function TglBitmapData.AssignAlphaToBitmap(const aBitmap: TBitmap): Boolean;
  6238. var
  6239. Row, Col, AlphaInterleave: Integer;
  6240. pSource, pDest: PByte;
  6241. begin
  6242. result := false;
  6243. if Assigned(Data) then begin
  6244. if (Format in [tfAlpha8ub1, tfLuminance8Alpha8ub2, tfRGBA8ub4, tfBGRA8ub4]) then begin
  6245. if Assigned(aBitmap) then begin
  6246. aBitmap.PixelFormat := pf8bit;
  6247. aBitmap.Palette := CreateGrayPalette;
  6248. aBitmap.Width := Width;
  6249. aBitmap.Height := Height;
  6250. case Format of
  6251. tfLuminance8Alpha8ub2:
  6252. AlphaInterleave := 1;
  6253. tfRGBA8ub4, tfBGRA8ub4:
  6254. AlphaInterleave := 3;
  6255. else
  6256. AlphaInterleave := 0;
  6257. end;
  6258. // Copy Data
  6259. pSource := Data;
  6260. for Row := 0 to Height -1 do begin
  6261. pDest := aBitmap.Scanline[Row];
  6262. if Assigned(pDest) then begin
  6263. for Col := 0 to Width -1 do begin
  6264. Inc(pSource, AlphaInterleave);
  6265. pDest^ := pSource^;
  6266. Inc(pDest);
  6267. Inc(pSource);
  6268. end;
  6269. end;
  6270. end;
  6271. result := true;
  6272. end;
  6273. end;
  6274. end;
  6275. end;
  6276. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6277. function TglBitmapData.AddAlphaFromBitmap(const aBitmap: TBitmap; const aFunc: TglBitmapFunction; const aArgs: Pointer): Boolean;
  6278. var
  6279. data: TglBitmapData;
  6280. begin
  6281. data := TglBitmapData.Create;
  6282. try
  6283. data.AssignFromBitmap(aBitmap);
  6284. result := AddAlphaFromDataObj(data, aFunc, aArgs);
  6285. finally
  6286. data.Free;
  6287. end;
  6288. end;
  6289. {$ENDIF}
  6290. {$IFDEF GLB_LAZARUS}
  6291. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6292. function TglBitmapData.AssignToLazIntfImage(const aImage: TLazIntfImage): Boolean;
  6293. var
  6294. rid: TRawImageDescription;
  6295. FormatDesc: TFormatDescriptor;
  6296. begin
  6297. if not Assigned(Data) then
  6298. raise EglBitmap.Create('no pixel data assigned. load data before save');
  6299. result := false;
  6300. if not Assigned(aImage) or (Format = tfEmpty) then
  6301. exit;
  6302. FormatDesc := TFormatDescriptor.Get(Format);
  6303. if FormatDesc.IsCompressed then
  6304. exit;
  6305. FillChar(rid{%H-}, SizeOf(rid), 0);
  6306. if FormatDesc.IsGrayscale then
  6307. rid.Format := ricfGray
  6308. else
  6309. rid.Format := ricfRGBA;
  6310. rid.Width := Width;
  6311. rid.Height := Height;
  6312. rid.Depth := FormatDesc.BitsPerPixel;
  6313. rid.BitOrder := riboBitsInOrder;
  6314. rid.ByteOrder := riboLSBFirst;
  6315. rid.LineOrder := riloTopToBottom;
  6316. rid.LineEnd := rileTight;
  6317. rid.BitsPerPixel := FormatDesc.BitsPerPixel;
  6318. rid.RedPrec := CountSetBits(FormatDesc.Range.r);
  6319. rid.GreenPrec := CountSetBits(FormatDesc.Range.g);
  6320. rid.BluePrec := CountSetBits(FormatDesc.Range.b);
  6321. rid.AlphaPrec := CountSetBits(FormatDesc.Range.a);
  6322. rid.RedShift := FormatDesc.Shift.r;
  6323. rid.GreenShift := FormatDesc.Shift.g;
  6324. rid.BlueShift := FormatDesc.Shift.b;
  6325. rid.AlphaShift := FormatDesc.Shift.a;
  6326. rid.MaskBitsPerPixel := 0;
  6327. rid.PaletteColorCount := 0;
  6328. aImage.DataDescription := rid;
  6329. aImage.CreateData;
  6330. if not Assigned(aImage.PixelData) then
  6331. raise EglBitmap.Create('error while creating LazIntfImage');
  6332. Move(Data^, aImage.PixelData^, FormatDesc.GetSize(Dimension));
  6333. result := true;
  6334. end;
  6335. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6336. function TglBitmapData.AssignFromLazIntfImage(const aImage: TLazIntfImage): Boolean;
  6337. var
  6338. f: TglBitmapFormat;
  6339. FormatDesc: TFormatDescriptor;
  6340. ImageData: PByte;
  6341. ImageSize: Integer;
  6342. CanCopy: Boolean;
  6343. Mask: TglBitmapRec4ul;
  6344. procedure CopyConvert;
  6345. var
  6346. bfFormat: TbmpBitfieldFormat;
  6347. pSourceLine, pDestLine: PByte;
  6348. pSourceMD, pDestMD: Pointer;
  6349. Shift, Prec: TglBitmapRec4ub;
  6350. x, y: Integer;
  6351. pixel: TglBitmapPixelData;
  6352. begin
  6353. bfFormat := TbmpBitfieldFormat.Create;
  6354. with aImage.DataDescription do begin
  6355. Prec.r := RedPrec;
  6356. Prec.g := GreenPrec;
  6357. Prec.b := BluePrec;
  6358. Prec.a := AlphaPrec;
  6359. Shift.r := RedShift;
  6360. Shift.g := GreenShift;
  6361. Shift.b := BlueShift;
  6362. Shift.a := AlphaShift;
  6363. bfFormat.SetCustomValues(BitsPerPixel, Prec, Shift);
  6364. end;
  6365. pSourceMD := bfFormat.CreateMappingData;
  6366. pDestMD := FormatDesc.CreateMappingData;
  6367. try
  6368. for y := 0 to aImage.Height-1 do begin
  6369. pSourceLine := aImage.PixelData + y {%H-}* aImage.DataDescription.BytesPerLine;
  6370. pDestLine := ImageData + y * Round(FormatDesc.BytesPerPixel * aImage.Width);
  6371. for x := 0 to aImage.Width-1 do begin
  6372. bfFormat.Unmap(pSourceLine, pixel, pSourceMD);
  6373. FormatDesc.Map(pixel, pDestLine, pDestMD);
  6374. end;
  6375. end;
  6376. finally
  6377. FormatDesc.FreeMappingData(pDestMD);
  6378. bfFormat.FreeMappingData(pSourceMD);
  6379. bfFormat.Free;
  6380. end;
  6381. end;
  6382. begin
  6383. result := false;
  6384. if not Assigned(aImage) then
  6385. exit;
  6386. with aImage.DataDescription do begin
  6387. Mask.r := (QWord(1 shl RedPrec )-1) shl RedShift;
  6388. Mask.g := (QWord(1 shl GreenPrec)-1) shl GreenShift;
  6389. Mask.b := (QWord(1 shl BluePrec )-1) shl BlueShift;
  6390. Mask.a := (QWord(1 shl AlphaPrec)-1) shl AlphaShift;
  6391. end;
  6392. FormatDesc := TFormatDescriptor.GetFromMask(Mask);
  6393. f := FormatDesc.Format;
  6394. if (f = tfEmpty) then
  6395. exit;
  6396. CanCopy :=
  6397. (FormatDesc.BitsPerPixel = aImage.DataDescription.Depth) and
  6398. (aImage.DataDescription.BitsPerPixel = aImage.DataDescription.Depth);
  6399. ImageSize := FormatDesc.GetSize(aImage.Width, aImage.Height);
  6400. ImageData := GetMem(ImageSize);
  6401. try
  6402. if CanCopy then
  6403. Move(aImage.PixelData^, ImageData^, ImageSize)
  6404. else
  6405. CopyConvert;
  6406. SetData(ImageData, f, aImage.Width, aImage.Height);
  6407. except
  6408. if Assigned(ImageData) then
  6409. FreeMem(ImageData);
  6410. raise;
  6411. end;
  6412. result := true;
  6413. end;
  6414. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6415. function TglBitmapData.AssignAlphaToLazIntfImage(const aImage: TLazIntfImage): Boolean;
  6416. var
  6417. rid: TRawImageDescription;
  6418. FormatDesc: TFormatDescriptor;
  6419. Pixel: TglBitmapPixelData;
  6420. x, y: Integer;
  6421. srcMD: Pointer;
  6422. src, dst: PByte;
  6423. begin
  6424. result := false;
  6425. if not Assigned(aImage) or (Format = tfEmpty) then
  6426. exit;
  6427. FormatDesc := TFormatDescriptor.Get(Format);
  6428. if FormatDesc.IsCompressed or not FormatDesc.HasAlpha then
  6429. exit;
  6430. FillChar(rid{%H-}, SizeOf(rid), 0);
  6431. rid.Format := ricfGray;
  6432. rid.Width := Width;
  6433. rid.Height := Height;
  6434. rid.Depth := CountSetBits(FormatDesc.Range.a);
  6435. rid.BitOrder := riboBitsInOrder;
  6436. rid.ByteOrder := riboLSBFirst;
  6437. rid.LineOrder := riloTopToBottom;
  6438. rid.LineEnd := rileTight;
  6439. rid.BitsPerPixel := 8 * Ceil(rid.Depth / 8);
  6440. rid.RedPrec := CountSetBits(FormatDesc.Range.a);
  6441. rid.GreenPrec := 0;
  6442. rid.BluePrec := 0;
  6443. rid.AlphaPrec := 0;
  6444. rid.RedShift := 0;
  6445. rid.GreenShift := 0;
  6446. rid.BlueShift := 0;
  6447. rid.AlphaShift := 0;
  6448. rid.MaskBitsPerPixel := 0;
  6449. rid.PaletteColorCount := 0;
  6450. aImage.DataDescription := rid;
  6451. aImage.CreateData;
  6452. srcMD := FormatDesc.CreateMappingData;
  6453. try
  6454. FormatDesc.PreparePixel(Pixel);
  6455. src := Data;
  6456. dst := aImage.PixelData;
  6457. for y := 0 to Height-1 do
  6458. for x := 0 to Width-1 do begin
  6459. FormatDesc.Unmap(src, Pixel, srcMD);
  6460. case rid.BitsPerPixel of
  6461. 8: begin
  6462. dst^ := Pixel.Data.a;
  6463. inc(dst);
  6464. end;
  6465. 16: begin
  6466. PWord(dst)^ := Pixel.Data.a;
  6467. inc(dst, 2);
  6468. end;
  6469. 24: begin
  6470. PByteArray(dst)^[0] := PByteArray(@Pixel.Data.a)^[0];
  6471. PByteArray(dst)^[1] := PByteArray(@Pixel.Data.a)^[1];
  6472. PByteArray(dst)^[2] := PByteArray(@Pixel.Data.a)^[2];
  6473. inc(dst, 3);
  6474. end;
  6475. 32: begin
  6476. PCardinal(dst)^ := Pixel.Data.a;
  6477. inc(dst, 4);
  6478. end;
  6479. else
  6480. raise EglBitmapUnsupportedFormat.Create(Format);
  6481. end;
  6482. end;
  6483. finally
  6484. FormatDesc.FreeMappingData(srcMD);
  6485. end;
  6486. result := true;
  6487. end;
  6488. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6489. function TglBitmapData.AddAlphaFromLazIntfImage(const aImage: TLazIntfImage; const aFunc: TglBitmapFunction; const aArgs: Pointer): Boolean;
  6490. var
  6491. data: TglBitmapData;
  6492. begin
  6493. data := TglBitmapData.Create;
  6494. try
  6495. data.AssignFromLazIntfImage(aImage);
  6496. result := AddAlphaFromDataObj(data, aFunc, aArgs);
  6497. finally
  6498. data.Free;
  6499. end;
  6500. end;
  6501. {$ENDIF}
  6502. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6503. function TglBitmapData.AddAlphaFromResource(const aInstance: Cardinal; aResource: String; aResType: PChar;
  6504. const aFunc: TglBitmapFunction; const aArgs: Pointer): Boolean;
  6505. var
  6506. rs: TResourceStream;
  6507. begin
  6508. PrepareResType(aResource, aResType);
  6509. rs := TResourceStream.Create(aInstance, aResource, aResType);
  6510. try
  6511. result := AddAlphaFromStream(rs, aFunc, aArgs);
  6512. finally
  6513. rs.Free;
  6514. end;
  6515. end;
  6516. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6517. function TglBitmapData.AddAlphaFromResourceID(const aInstance: Cardinal; const aResourceID: Integer; const aResType: PChar;
  6518. const aFunc: TglBitmapFunction; const aArgs: Pointer): Boolean;
  6519. var
  6520. rs: TResourceStream;
  6521. begin
  6522. rs := TResourceStream.CreateFromID(aInstance, aResourceID, aResType);
  6523. try
  6524. result := AddAlphaFromStream(rs, aFunc, aArgs);
  6525. finally
  6526. rs.Free;
  6527. end;
  6528. end;
  6529. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6530. function TglBitmapData.AddAlphaFromFunc(const aFunc: TglBitmapFunction; const aArgs: Pointer): Boolean;
  6531. begin
  6532. if TFormatDescriptor.Get(Format).IsCompressed then
  6533. raise EglBitmapUnsupportedFormat.Create(Format);
  6534. result := Convert(Self, aFunc, false, TFormatDescriptor.Get(Format).WithAlpha, aArgs);
  6535. end;
  6536. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6537. function TglBitmapData.AddAlphaFromFile(const aFileName: String; const aFunc: TglBitmapFunction; const aArgs: Pointer): Boolean;
  6538. var
  6539. FS: TFileStream;
  6540. begin
  6541. FS := TFileStream.Create(aFileName, fmOpenRead);
  6542. try
  6543. result := AddAlphaFromStream(FS, aFunc, aArgs);
  6544. finally
  6545. FS.Free;
  6546. end;
  6547. end;
  6548. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6549. function TglBitmapData.AddAlphaFromStream(const aStream: TStream; const aFunc: TglBitmapFunction; const aArgs: Pointer): Boolean;
  6550. var
  6551. data: TglBitmapData;
  6552. begin
  6553. data := TglBitmapData.Create(aStream);
  6554. try
  6555. result := AddAlphaFromDataObj(data, aFunc, aArgs);
  6556. finally
  6557. data.Free;
  6558. end;
  6559. end;
  6560. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6561. function TglBitmapData.AddAlphaFromDataObj(const aDataObj: TglBitmapData; aFunc: TglBitmapFunction; const aArgs: Pointer): Boolean;
  6562. var
  6563. DestData, DestData2, SourceData: pByte;
  6564. TempHeight, TempWidth: Integer;
  6565. SourceFD, DestFD: TFormatDescriptor;
  6566. SourceMD, DestMD, DestMD2: Pointer;
  6567. FuncRec: TglBitmapFunctionRec;
  6568. begin
  6569. result := false;
  6570. Assert(Assigned(Data));
  6571. Assert(Assigned(aDataObj));
  6572. Assert(Assigned(aDataObj.Data));
  6573. if ((aDataObj.Width = Width) and (aDataObj.Height = Height)) then begin
  6574. result := ConvertTo(TFormatDescriptor.Get(Format).WithAlpha);
  6575. SourceFD := TFormatDescriptor.Get(aDataObj.Format);
  6576. DestFD := TFormatDescriptor.Get(Format);
  6577. if not Assigned(aFunc) then begin
  6578. aFunc := glBitmapAlphaFunc;
  6579. FuncRec.Args := {%H-}Pointer(SourceFD.HasAlpha);
  6580. end else
  6581. FuncRec.Args := aArgs;
  6582. // Values
  6583. TempWidth := aDataObj.Width;
  6584. TempHeight := aDataObj.Height;
  6585. if (TempWidth <= 0) or (TempHeight <= 0) then
  6586. exit;
  6587. FuncRec.Sender := Self;
  6588. FuncRec.Size := Dimension;
  6589. FuncRec.Position.Fields := FuncRec.Size.Fields;
  6590. DestData := Data;
  6591. DestData2 := Data;
  6592. SourceData := aDataObj.Data;
  6593. // Mapping
  6594. SourceFD.PreparePixel(FuncRec.Source);
  6595. DestFD.PreparePixel (FuncRec.Dest);
  6596. SourceMD := SourceFD.CreateMappingData;
  6597. DestMD := DestFD.CreateMappingData;
  6598. DestMD2 := DestFD.CreateMappingData;
  6599. try
  6600. FuncRec.Position.Y := 0;
  6601. while FuncRec.Position.Y < TempHeight do begin
  6602. FuncRec.Position.X := 0;
  6603. while FuncRec.Position.X < TempWidth do begin
  6604. SourceFD.Unmap(SourceData, FuncRec.Source, SourceMD);
  6605. DestFD.Unmap (DestData, FuncRec.Dest, DestMD);
  6606. aFunc(FuncRec);
  6607. DestFD.Map(FuncRec.Dest, DestData2, DestMD2);
  6608. inc(FuncRec.Position.X);
  6609. end;
  6610. inc(FuncRec.Position.Y);
  6611. end;
  6612. finally
  6613. SourceFD.FreeMappingData(SourceMD);
  6614. DestFD.FreeMappingData(DestMD);
  6615. DestFD.FreeMappingData(DestMD2);
  6616. end;
  6617. end;
  6618. end;
  6619. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6620. function TglBitmapData.AddAlphaFromColorKey(const aRed, aGreen, aBlue: Byte; const aDeviation: Byte): Boolean;
  6621. begin
  6622. result := AddAlphaFromColorKeyFloat(aRed / $FF, aGreen / $FF, aBlue / $FF, aDeviation / $FF);
  6623. end;
  6624. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6625. function TglBitmapData.AddAlphaFromColorKeyRange(const aRed, aGreen, aBlue: Cardinal; const aDeviation: Cardinal): Boolean;
  6626. var
  6627. PixelData: TglBitmapPixelData;
  6628. begin
  6629. TFormatDescriptor.GetAlpha(Format).PreparePixel(PixelData);
  6630. result := AddAlphaFromColorKeyFloat(
  6631. aRed / PixelData.Range.r,
  6632. aGreen / PixelData.Range.g,
  6633. aBlue / PixelData.Range.b,
  6634. aDeviation / Max(PixelData.Range.r, Max(PixelData.Range.g, PixelData.Range.b)));
  6635. end;
  6636. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6637. function TglBitmapData.AddAlphaFromColorKeyFloat(const aRed, aGreen, aBlue: Single; const aDeviation: Single): Boolean;
  6638. var
  6639. values: array[0..2] of Single;
  6640. tmp: Cardinal;
  6641. i: Integer;
  6642. PixelData: TglBitmapPixelData;
  6643. begin
  6644. TFormatDescriptor.GetAlpha(Format).PreparePixel(PixelData);
  6645. with PixelData do begin
  6646. values[0] := aRed;
  6647. values[1] := aGreen;
  6648. values[2] := aBlue;
  6649. for i := 0 to 2 do begin
  6650. tmp := Trunc(Range.arr[i] * aDeviation);
  6651. Data.arr[i] := Min(Range.arr[i], Trunc(Range.arr[i] * values[i] + tmp));
  6652. Range.arr[i] := Max(0, Trunc(Range.arr[i] * values[i] - tmp));
  6653. end;
  6654. Data.a := 0;
  6655. Range.a := 0;
  6656. end;
  6657. result := AddAlphaFromFunc(glBitmapColorKeyAlphaFunc, @PixelData);
  6658. end;
  6659. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6660. function TglBitmapData.AddAlphaFromValue(const aAlpha: Byte): Boolean;
  6661. begin
  6662. result := AddAlphaFromValueFloat(aAlpha / $FF);
  6663. end;
  6664. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6665. function TglBitmapData.AddAlphaFromValueRange(const aAlpha: Cardinal): Boolean;
  6666. var
  6667. PixelData: TglBitmapPixelData;
  6668. begin
  6669. TFormatDescriptor.GetAlpha(Format).PreparePixel(PixelData);
  6670. result := AddAlphaFromValueFloat(aAlpha / PixelData.Range.a);
  6671. end;
  6672. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6673. function TglBitmapData.AddAlphaFromValueFloat(const aAlpha: Single): Boolean;
  6674. var
  6675. PixelData: TglBitmapPixelData;
  6676. begin
  6677. TFormatDescriptor.GetAlpha(Format).PreparePixel(PixelData);
  6678. with PixelData do
  6679. Data.a := Min(Range.a, Max(0, Round(Range.a * aAlpha)));
  6680. result := AddAlphaFromFunc(glBitmapValueAlphaFunc, @PixelData.Data.a);
  6681. end;
  6682. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6683. function TglBitmapData.RemoveAlpha: Boolean;
  6684. var
  6685. FormatDesc: TFormatDescriptor;
  6686. begin
  6687. result := false;
  6688. FormatDesc := TFormatDescriptor.Get(Format);
  6689. if Assigned(Data) then begin
  6690. if FormatDesc.IsCompressed or not FormatDesc.HasAlpha then
  6691. raise EglBitmapUnsupportedFormat.Create(Format);
  6692. result := ConvertTo(FormatDesc.WithoutAlpha);
  6693. end;
  6694. end;
  6695. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6696. procedure TglBitmapData.FillWithColor(const aRed, aGreen, aBlue: Byte;
  6697. const aAlpha: Byte);
  6698. begin
  6699. FillWithColorFloat(aRed/$FF, aGreen/$FF, aBlue/$FF, aAlpha/$FF);
  6700. end;
  6701. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6702. procedure TglBitmapData.FillWithColorRange(const aRed, aGreen, aBlue: Cardinal; const aAlpha: Cardinal);
  6703. var
  6704. PixelData: TglBitmapPixelData;
  6705. begin
  6706. TFormatDescriptor.GetAlpha(Format).PreparePixel(PixelData);
  6707. FillWithColorFloat(
  6708. aRed / PixelData.Range.r,
  6709. aGreen / PixelData.Range.g,
  6710. aBlue / PixelData.Range.b,
  6711. aAlpha / PixelData.Range.a);
  6712. end;
  6713. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6714. procedure TglBitmapData.FillWithColorFloat(const aRed, aGreen, aBlue: Single; const aAlpha: Single);
  6715. var
  6716. PixelData: TglBitmapPixelData;
  6717. begin
  6718. TFormatDescriptor.Get(Format).PreparePixel(PixelData);
  6719. with PixelData do begin
  6720. Data.r := Max(0, Min(Range.r, Trunc(Range.r * aRed)));
  6721. Data.g := Max(0, Min(Range.g, Trunc(Range.g * aGreen)));
  6722. Data.b := Max(0, Min(Range.b, Trunc(Range.b * aBlue)));
  6723. Data.a := Max(0, Min(Range.a, Trunc(Range.a * aAlpha)));
  6724. end;
  6725. Convert(glBitmapFillWithColorFunc, false, @PixelData);
  6726. end;
  6727. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6728. procedure TglBitmapData.SetData(const aData: PByte; const aFormat: TglBitmapFormat; const aWidth: Integer; const aHeight: Integer);
  6729. begin
  6730. if (Data <> aData) then begin
  6731. if (Assigned(Data)) then
  6732. FreeMem(Data);
  6733. fData := aData;
  6734. end;
  6735. if Assigned(fData) then begin
  6736. FillChar(fDimension, SizeOf(fDimension), 0);
  6737. if aWidth <> -1 then begin
  6738. fDimension.Fields := fDimension.Fields + [ffX];
  6739. fDimension.X := aWidth;
  6740. end;
  6741. if aHeight <> -1 then begin
  6742. fDimension.Fields := fDimension.Fields + [ffY];
  6743. fDimension.Y := aHeight;
  6744. end;
  6745. fFormat := aFormat;
  6746. end else
  6747. fFormat := tfEmpty;
  6748. UpdateScanlines;
  6749. end;
  6750. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6751. function TglBitmapData.Clone: TglBitmapData;
  6752. var
  6753. Temp: TglBitmapData;
  6754. TempPtr: PByte;
  6755. Size: Integer;
  6756. begin
  6757. result := nil;
  6758. Temp := (ClassType.Create as TglBitmapData);
  6759. try
  6760. // copy texture data if assigned
  6761. if Assigned(Data) then begin
  6762. Size := TFormatDescriptor.Get(Format).GetSize(fDimension);
  6763. GetMem(TempPtr, Size);
  6764. try
  6765. Move(Data^, TempPtr^, Size);
  6766. Temp.SetData(TempPtr, Format, Width, Height);
  6767. except
  6768. if Assigned(TempPtr) then
  6769. FreeMem(TempPtr);
  6770. raise;
  6771. end;
  6772. end else begin
  6773. TempPtr := nil;
  6774. Temp.SetData(TempPtr, Format, Width, Height);
  6775. end;
  6776. // copy properties
  6777. Temp.fFormat := Format;
  6778. result := Temp;
  6779. except
  6780. FreeAndNil(Temp);
  6781. raise;
  6782. end;
  6783. end;
  6784. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6785. procedure TglBitmapData.Invert(const aRed, aGreen, aBlue, aAlpha: Boolean);
  6786. var
  6787. mask: PtrInt;
  6788. begin
  6789. mask :=
  6790. (Byte(aRed) and 1) or
  6791. ((Byte(aGreen) and 1) shl 1) or
  6792. ((Byte(aBlue) and 1) shl 2) or
  6793. ((Byte(aAlpha) and 1) shl 3);
  6794. if (mask > 0) then
  6795. Convert(glBitmapInvertFunc, false, {%H-}Pointer(mask));
  6796. end;
  6797. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6798. type
  6799. TMatrixItem = record
  6800. X, Y: Integer;
  6801. W: Single;
  6802. end;
  6803. PglBitmapToNormalMapRec = ^TglBitmapToNormalMapRec;
  6804. TglBitmapToNormalMapRec = Record
  6805. Scale: Single;
  6806. Heights: array of Single;
  6807. MatrixU : array of TMatrixItem;
  6808. MatrixV : array of TMatrixItem;
  6809. end;
  6810. const
  6811. ONE_OVER_255 = 1 / 255;
  6812. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6813. procedure glBitmapToNormalMapPrepareFunc(var FuncRec: TglBitmapFunctionRec);
  6814. var
  6815. Val: Single;
  6816. begin
  6817. with FuncRec do begin
  6818. Val :=
  6819. Source.Data.r * LUMINANCE_WEIGHT_R +
  6820. Source.Data.g * LUMINANCE_WEIGHT_G +
  6821. Source.Data.b * LUMINANCE_WEIGHT_B;
  6822. PglBitmapToNormalMapRec(Args)^.Heights[Position.Y * Size.X + Position.X] := Val * ONE_OVER_255;
  6823. end;
  6824. end;
  6825. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6826. procedure glBitmapToNormalMapPrepareAlphaFunc(var FuncRec: TglBitmapFunctionRec);
  6827. begin
  6828. with FuncRec do
  6829. PglBitmapToNormalMapRec(Args)^.Heights[Position.Y * Size.X + Position.X] := Source.Data.a * ONE_OVER_255;
  6830. end;
  6831. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6832. procedure glBitmapToNormalMapFunc (var FuncRec: TglBitmapFunctionRec);
  6833. type
  6834. TVec = Array[0..2] of Single;
  6835. var
  6836. Idx: Integer;
  6837. du, dv: Double;
  6838. Len: Single;
  6839. Vec: TVec;
  6840. function GetHeight(X, Y: Integer): Single;
  6841. begin
  6842. with FuncRec do begin
  6843. X := Max(0, Min(Size.X -1, X));
  6844. Y := Max(0, Min(Size.Y -1, Y));
  6845. result := PglBitmapToNormalMapRec(Args)^.Heights[Y * Size.X + X];
  6846. end;
  6847. end;
  6848. begin
  6849. with FuncRec do begin
  6850. with PglBitmapToNormalMapRec(Args)^ do begin
  6851. du := 0;
  6852. for Idx := Low(MatrixU) to High(MatrixU) do
  6853. du := du + GetHeight(Position.X + MatrixU[Idx].X, Position.Y + MatrixU[Idx].Y) * MatrixU[Idx].W;
  6854. dv := 0;
  6855. for Idx := Low(MatrixU) to High(MatrixU) do
  6856. dv := dv + GetHeight(Position.X + MatrixV[Idx].X, Position.Y + MatrixV[Idx].Y) * MatrixV[Idx].W;
  6857. Vec[0] := -du * Scale;
  6858. Vec[1] := -dv * Scale;
  6859. Vec[2] := 1;
  6860. end;
  6861. // Normalize
  6862. Len := 1 / Sqrt(Sqr(Vec[0]) + Sqr(Vec[1]) + Sqr(Vec[2]));
  6863. if Len <> 0 then begin
  6864. Vec[0] := Vec[0] * Len;
  6865. Vec[1] := Vec[1] * Len;
  6866. Vec[2] := Vec[2] * Len;
  6867. end;
  6868. // Farbe zuweisem
  6869. Dest.Data.r := Trunc((Vec[0] + 1) * 127.5);
  6870. Dest.Data.g := Trunc((Vec[1] + 1) * 127.5);
  6871. Dest.Data.b := Trunc((Vec[2] + 1) * 127.5);
  6872. end;
  6873. end;
  6874. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6875. procedure TglBitmapData.GenerateNormalMap(const aFunc: TglBitmapNormalMapFunc; const aScale: Single; const aUseAlpha: Boolean);
  6876. var
  6877. Rec: TglBitmapToNormalMapRec;
  6878. procedure SetEntry (var Matrix: array of TMatrixItem; Index, X, Y: Integer; W: Single);
  6879. begin
  6880. if (Index >= Low(Matrix)) and (Index <= High(Matrix)) then begin
  6881. Matrix[Index].X := X;
  6882. Matrix[Index].Y := Y;
  6883. Matrix[Index].W := W;
  6884. end;
  6885. end;
  6886. begin
  6887. if TFormatDescriptor.Get(Format).IsCompressed then
  6888. raise EglBitmapUnsupportedFormat.Create(Format);
  6889. if aScale > 100 then
  6890. Rec.Scale := 100
  6891. else if aScale < -100 then
  6892. Rec.Scale := -100
  6893. else
  6894. Rec.Scale := aScale;
  6895. SetLength(Rec.Heights, Width * Height);
  6896. try
  6897. case aFunc of
  6898. nm4Samples: begin
  6899. SetLength(Rec.MatrixU, 2);
  6900. SetEntry(Rec.MatrixU, 0, -1, 0, -0.5);
  6901. SetEntry(Rec.MatrixU, 1, 1, 0, 0.5);
  6902. SetLength(Rec.MatrixV, 2);
  6903. SetEntry(Rec.MatrixV, 0, 0, 1, 0.5);
  6904. SetEntry(Rec.MatrixV, 1, 0, -1, -0.5);
  6905. end;
  6906. nmSobel: begin
  6907. SetLength(Rec.MatrixU, 6);
  6908. SetEntry(Rec.MatrixU, 0, -1, 1, -1.0);
  6909. SetEntry(Rec.MatrixU, 1, -1, 0, -2.0);
  6910. SetEntry(Rec.MatrixU, 2, -1, -1, -1.0);
  6911. SetEntry(Rec.MatrixU, 3, 1, 1, 1.0);
  6912. SetEntry(Rec.MatrixU, 4, 1, 0, 2.0);
  6913. SetEntry(Rec.MatrixU, 5, 1, -1, 1.0);
  6914. SetLength(Rec.MatrixV, 6);
  6915. SetEntry(Rec.MatrixV, 0, -1, 1, 1.0);
  6916. SetEntry(Rec.MatrixV, 1, 0, 1, 2.0);
  6917. SetEntry(Rec.MatrixV, 2, 1, 1, 1.0);
  6918. SetEntry(Rec.MatrixV, 3, -1, -1, -1.0);
  6919. SetEntry(Rec.MatrixV, 4, 0, -1, -2.0);
  6920. SetEntry(Rec.MatrixV, 5, 1, -1, -1.0);
  6921. end;
  6922. nm3x3: begin
  6923. SetLength(Rec.MatrixU, 6);
  6924. SetEntry(Rec.MatrixU, 0, -1, 1, -1/6);
  6925. SetEntry(Rec.MatrixU, 1, -1, 0, -1/6);
  6926. SetEntry(Rec.MatrixU, 2, -1, -1, -1/6);
  6927. SetEntry(Rec.MatrixU, 3, 1, 1, 1/6);
  6928. SetEntry(Rec.MatrixU, 4, 1, 0, 1/6);
  6929. SetEntry(Rec.MatrixU, 5, 1, -1, 1/6);
  6930. SetLength(Rec.MatrixV, 6);
  6931. SetEntry(Rec.MatrixV, 0, -1, 1, 1/6);
  6932. SetEntry(Rec.MatrixV, 1, 0, 1, 1/6);
  6933. SetEntry(Rec.MatrixV, 2, 1, 1, 1/6);
  6934. SetEntry(Rec.MatrixV, 3, -1, -1, -1/6);
  6935. SetEntry(Rec.MatrixV, 4, 0, -1, -1/6);
  6936. SetEntry(Rec.MatrixV, 5, 1, -1, -1/6);
  6937. end;
  6938. nm5x5: begin
  6939. SetLength(Rec.MatrixU, 20);
  6940. SetEntry(Rec.MatrixU, 0, -2, 2, -1 / 16);
  6941. SetEntry(Rec.MatrixU, 1, -1, 2, -1 / 10);
  6942. SetEntry(Rec.MatrixU, 2, 1, 2, 1 / 10);
  6943. SetEntry(Rec.MatrixU, 3, 2, 2, 1 / 16);
  6944. SetEntry(Rec.MatrixU, 4, -2, 1, -1 / 10);
  6945. SetEntry(Rec.MatrixU, 5, -1, 1, -1 / 8);
  6946. SetEntry(Rec.MatrixU, 6, 1, 1, 1 / 8);
  6947. SetEntry(Rec.MatrixU, 7, 2, 1, 1 / 10);
  6948. SetEntry(Rec.MatrixU, 8, -2, 0, -1 / 2.8);
  6949. SetEntry(Rec.MatrixU, 9, -1, 0, -0.5);
  6950. SetEntry(Rec.MatrixU, 10, 1, 0, 0.5);
  6951. SetEntry(Rec.MatrixU, 11, 2, 0, 1 / 2.8);
  6952. SetEntry(Rec.MatrixU, 12, -2, -1, -1 / 10);
  6953. SetEntry(Rec.MatrixU, 13, -1, -1, -1 / 8);
  6954. SetEntry(Rec.MatrixU, 14, 1, -1, 1 / 8);
  6955. SetEntry(Rec.MatrixU, 15, 2, -1, 1 / 10);
  6956. SetEntry(Rec.MatrixU, 16, -2, -2, -1 / 16);
  6957. SetEntry(Rec.MatrixU, 17, -1, -2, -1 / 10);
  6958. SetEntry(Rec.MatrixU, 18, 1, -2, 1 / 10);
  6959. SetEntry(Rec.MatrixU, 19, 2, -2, 1 / 16);
  6960. SetLength(Rec.MatrixV, 20);
  6961. SetEntry(Rec.MatrixV, 0, -2, 2, 1 / 16);
  6962. SetEntry(Rec.MatrixV, 1, -1, 2, 1 / 10);
  6963. SetEntry(Rec.MatrixV, 2, 0, 2, 0.25);
  6964. SetEntry(Rec.MatrixV, 3, 1, 2, 1 / 10);
  6965. SetEntry(Rec.MatrixV, 4, 2, 2, 1 / 16);
  6966. SetEntry(Rec.MatrixV, 5, -2, 1, 1 / 10);
  6967. SetEntry(Rec.MatrixV, 6, -1, 1, 1 / 8);
  6968. SetEntry(Rec.MatrixV, 7, 0, 1, 0.5);
  6969. SetEntry(Rec.MatrixV, 8, 1, 1, 1 / 8);
  6970. SetEntry(Rec.MatrixV, 9, 2, 1, 1 / 16);
  6971. SetEntry(Rec.MatrixV, 10, -2, -1, -1 / 16);
  6972. SetEntry(Rec.MatrixV, 11, -1, -1, -1 / 8);
  6973. SetEntry(Rec.MatrixV, 12, 0, -1, -0.5);
  6974. SetEntry(Rec.MatrixV, 13, 1, -1, -1 / 8);
  6975. SetEntry(Rec.MatrixV, 14, 2, -1, -1 / 10);
  6976. SetEntry(Rec.MatrixV, 15, -2, -2, -1 / 16);
  6977. SetEntry(Rec.MatrixV, 16, -1, -2, -1 / 10);
  6978. SetEntry(Rec.MatrixV, 17, 0, -2, -0.25);
  6979. SetEntry(Rec.MatrixV, 18, 1, -2, -1 / 10);
  6980. SetEntry(Rec.MatrixV, 19, 2, -2, -1 / 16);
  6981. end;
  6982. end;
  6983. // Daten Sammeln
  6984. if aUseAlpha and TFormatDescriptor.Get(Format).HasAlpha then
  6985. Convert(glBitmapToNormalMapPrepareAlphaFunc, false, @Rec)
  6986. else
  6987. Convert(glBitmapToNormalMapPrepareFunc, false, @Rec);
  6988. Convert(glBitmapToNormalMapFunc, false, @Rec);
  6989. finally
  6990. SetLength(Rec.Heights, 0);
  6991. end;
  6992. end;
  6993. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6994. constructor TglBitmapData.Create;
  6995. begin
  6996. inherited Create;
  6997. fFormat := glBitmapDefaultFormat;
  6998. end;
  6999. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7000. constructor TglBitmapData.Create(const aFileName: String);
  7001. begin
  7002. Create;
  7003. LoadFromFile(aFileName);
  7004. end;
  7005. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7006. constructor TglBitmapData.Create(const aStream: TStream);
  7007. begin
  7008. Create;
  7009. LoadFromStream(aStream);
  7010. end;
  7011. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7012. constructor TglBitmapData.Create(const aSize: TglBitmapSize; const aFormat: TglBitmapFormat; aData: PByte);
  7013. var
  7014. ImageSize: Integer;
  7015. begin
  7016. Create;
  7017. if not Assigned(aData) then begin
  7018. ImageSize := TFormatDescriptor.Get(aFormat).GetSize(aSize);
  7019. GetMem(aData, ImageSize);
  7020. try
  7021. FillChar(aData^, ImageSize, #$FF);
  7022. SetData(aData, aFormat, aSize.X, aSize.Y);
  7023. except
  7024. if Assigned(aData) then
  7025. FreeMem(aData);
  7026. raise;
  7027. end;
  7028. end else begin
  7029. SetData(aData, aFormat, aSize.X, aSize.Y);
  7030. end;
  7031. end;
  7032. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7033. constructor TglBitmapData.Create(const aSize: TglBitmapSize; const aFormat: TglBitmapFormat; const aFunc: TglBitmapFunction; const aArgs: Pointer);
  7034. begin
  7035. Create;
  7036. LoadFromFunc(aSize, aFormat, aFunc, aArgs);
  7037. end;
  7038. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7039. constructor TglBitmapData.Create(const aInstance: Cardinal; const aResource: String; const aResType: PChar);
  7040. begin
  7041. Create;
  7042. LoadFromResource(aInstance, aResource, aResType);
  7043. end;
  7044. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7045. constructor TglBitmapData.Create(const aInstance: Cardinal; const aResourceID: Integer; const aResType: PChar);
  7046. begin
  7047. Create;
  7048. LoadFromResourceID(aInstance, aResourceID, aResType);
  7049. end;
  7050. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7051. destructor TglBitmapData.Destroy;
  7052. begin
  7053. SetData(nil, tfEmpty);
  7054. inherited Destroy;
  7055. end;
  7056. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7057. //TglBitmap - PROTECTED///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7058. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7059. function TglBitmap.GetWidth: Integer;
  7060. begin
  7061. if (ffX in fDimension.Fields) then
  7062. result := fDimension.X
  7063. else
  7064. result := -1;
  7065. end;
  7066. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7067. function TglBitmap.GetHeight: Integer;
  7068. begin
  7069. if (ffY in fDimension.Fields) then
  7070. result := fDimension.Y
  7071. else
  7072. result := -1;
  7073. end;
  7074. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7075. procedure TglBitmap.SetCustomData(const aValue: Pointer);
  7076. begin
  7077. if fCustomData = aValue then
  7078. exit;
  7079. fCustomData := aValue;
  7080. end;
  7081. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7082. procedure TglBitmap.SetCustomName(const aValue: String);
  7083. begin
  7084. if fCustomName = aValue then
  7085. exit;
  7086. fCustomName := aValue;
  7087. end;
  7088. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7089. procedure TglBitmap.SetCustomNameW(const aValue: WideString);
  7090. begin
  7091. if fCustomNameW = aValue then
  7092. exit;
  7093. fCustomNameW := aValue;
  7094. end;
  7095. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7096. procedure TglBitmap.SetDeleteTextureOnFree(const aValue: Boolean);
  7097. begin
  7098. if fDeleteTextureOnFree = aValue then
  7099. exit;
  7100. fDeleteTextureOnFree := aValue;
  7101. end;
  7102. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7103. procedure TglBitmap.SetID(const aValue: Cardinal);
  7104. begin
  7105. if fID = aValue then
  7106. exit;
  7107. fID := aValue;
  7108. end;
  7109. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7110. procedure TglBitmap.SetMipMap(const aValue: TglBitmapMipMap);
  7111. begin
  7112. if fMipMap = aValue then
  7113. exit;
  7114. fMipMap := aValue;
  7115. end;
  7116. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7117. procedure TglBitmap.SetTarget(const aValue: Cardinal);
  7118. begin
  7119. if fTarget = aValue then
  7120. exit;
  7121. fTarget := aValue;
  7122. end;
  7123. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7124. procedure TglBitmap.SetAnisotropic(const aValue: Integer);
  7125. {$IF NOT DEFINED(OPENGL_ES) OR DEFINED(OPENGL_ES_EXT)}
  7126. var
  7127. MaxAnisotropic: Integer;
  7128. {$IFEND}
  7129. begin
  7130. fAnisotropic := aValue;
  7131. if (ID > 0) then begin
  7132. {$IF NOT DEFINED(OPENGL_ES) OR DEFINED(OPENGL_ES_EXT)}
  7133. if GL_EXT_texture_filter_anisotropic then begin
  7134. if fAnisotropic > 0 then begin
  7135. Bind({$IFNDEF OPENGL_ES}false{$ENDIF});
  7136. glGetIntegerv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, @MaxAnisotropic);
  7137. if aValue > MaxAnisotropic then
  7138. fAnisotropic := MaxAnisotropic;
  7139. glTexParameteri(Target, GL_TEXTURE_MAX_ANISOTROPY_EXT, fAnisotropic);
  7140. end;
  7141. end else begin
  7142. fAnisotropic := 0;
  7143. end;
  7144. {$ELSE}
  7145. fAnisotropic := 0;
  7146. {$IFEND}
  7147. end;
  7148. end;
  7149. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7150. procedure TglBitmap.CreateID;
  7151. begin
  7152. if (ID <> 0) then
  7153. glDeleteTextures(1, @fID);
  7154. glGenTextures(1, @fID);
  7155. Bind({$IFNDEF OPENGL_ES}false{$ENDIF});
  7156. end;
  7157. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7158. procedure TglBitmap.SetupParameters({$IFNDEF OPENGL_ES}out aBuildWithGlu: Boolean{$ENDIF});
  7159. begin
  7160. // Set Up Parameters
  7161. SetWrap(fWrapS, fWrapT, fWrapR);
  7162. SetFilter(fFilterMin, fFilterMag);
  7163. SetAnisotropic(fAnisotropic);
  7164. {$IFNDEF OPENGL_ES}
  7165. SetBorderColor(fBorderColor[0], fBorderColor[1], fBorderColor[2], fBorderColor[3]);
  7166. if (GL_ARB_texture_swizzle or GL_EXT_texture_swizzle or GL_VERSION_3_3) then
  7167. SetSwizzle(fSwizzle[0], fSwizzle[1], fSwizzle[2], fSwizzle[3]);
  7168. {$ENDIF}
  7169. {$IFNDEF OPENGL_ES}
  7170. // Mip Maps Generation Mode
  7171. aBuildWithGlu := false;
  7172. if (MipMap = mmMipmap) then begin
  7173. if (GL_VERSION_1_4 or GL_SGIS_generate_mipmap) then
  7174. glTexParameteri(Target, GL_GENERATE_MIPMAP, GLint(GL_TRUE))
  7175. else
  7176. aBuildWithGlu := true;
  7177. end else if (MipMap = mmMipmapGlu) then
  7178. aBuildWithGlu := true;
  7179. {$ELSE}
  7180. if (MipMap = mmMipmap) then
  7181. glGenerateMipmap(Target);
  7182. {$ENDIF}
  7183. end;
  7184. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7185. //TglBitmap - PUBLIC//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7186. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7187. procedure TglBitmap.AfterConstruction;
  7188. begin
  7189. inherited AfterConstruction;
  7190. fID := 0;
  7191. fTarget := 0;
  7192. {$IFNDEF OPENGL_ES}
  7193. fIsResident := false;
  7194. {$ENDIF}
  7195. fMipMap := glBitmapDefaultMipmap;
  7196. fDeleteTextureOnFree := glBitmapGetDefaultDeleteTextureOnFree;
  7197. glBitmapGetDefaultFilter (fFilterMin, fFilterMag);
  7198. glBitmapGetDefaultTextureWrap(fWrapS, fWrapT, fWrapR);
  7199. {$IFNDEF OPENGL_ES}
  7200. glBitmapGetDefaultSwizzle (fSwizzle[0], fSwizzle[1], fSwizzle[2], fSwizzle[3]);
  7201. {$ENDIF}
  7202. end;
  7203. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7204. procedure TglBitmap.BeforeDestruction;
  7205. begin
  7206. if (fID > 0) and fDeleteTextureOnFree then
  7207. glDeleteTextures(1, @fID);
  7208. inherited BeforeDestruction;
  7209. end;
  7210. {$IFNDEF OPENGL_ES}
  7211. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7212. procedure TglBitmap.SetBorderColor(const aRed, aGreen, aBlue, aAlpha: Single);
  7213. begin
  7214. fBorderColor[0] := aRed;
  7215. fBorderColor[1] := aGreen;
  7216. fBorderColor[2] := aBlue;
  7217. fBorderColor[3] := aAlpha;
  7218. if (ID > 0) then begin
  7219. Bind(false);
  7220. glTexParameterfv(Target, GL_TEXTURE_BORDER_COLOR, @fBorderColor[0]);
  7221. end;
  7222. end;
  7223. {$ENDIF}
  7224. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7225. procedure TglBitmap.SetFilter(const aMin, aMag: GLenum);
  7226. begin
  7227. //check MIN filter
  7228. case aMin of
  7229. GL_NEAREST:
  7230. fFilterMin := GL_NEAREST;
  7231. GL_LINEAR:
  7232. fFilterMin := GL_LINEAR;
  7233. GL_NEAREST_MIPMAP_NEAREST:
  7234. fFilterMin := GL_NEAREST_MIPMAP_NEAREST;
  7235. GL_LINEAR_MIPMAP_NEAREST:
  7236. fFilterMin := GL_LINEAR_MIPMAP_NEAREST;
  7237. GL_NEAREST_MIPMAP_LINEAR:
  7238. fFilterMin := GL_NEAREST_MIPMAP_LINEAR;
  7239. GL_LINEAR_MIPMAP_LINEAR:
  7240. fFilterMin := GL_LINEAR_MIPMAP_LINEAR;
  7241. else
  7242. raise EglBitmap.Create('SetFilter - Unknow MIN filter.');
  7243. end;
  7244. //check MAG filter
  7245. case aMag of
  7246. GL_NEAREST:
  7247. fFilterMag := GL_NEAREST;
  7248. GL_LINEAR:
  7249. fFilterMag := GL_LINEAR;
  7250. else
  7251. raise EglBitmap.Create('SetFilter - Unknow MAG filter.');
  7252. end;
  7253. //apply filter
  7254. if (ID > 0) then begin
  7255. Bind({$IFNDEF OPENGL_ES}false{$ENDIF});
  7256. glTexParameteri(Target, GL_TEXTURE_MAG_FILTER, fFilterMag);
  7257. if (MipMap = mmNone) {$IFNDEF OPENGL_ES}or (Target = GL_TEXTURE_RECTANGLE){$ENDIF} then begin
  7258. case fFilterMin of
  7259. GL_NEAREST, GL_LINEAR:
  7260. glTexParameteri(Target, GL_TEXTURE_MIN_FILTER, fFilterMin);
  7261. GL_NEAREST_MIPMAP_NEAREST, GL_NEAREST_MIPMAP_LINEAR:
  7262. glTexParameteri(Target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  7263. GL_LINEAR_MIPMAP_NEAREST, GL_LINEAR_MIPMAP_LINEAR:
  7264. glTexParameteri(Target, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  7265. end;
  7266. end else
  7267. glTexParameteri(Target, GL_TEXTURE_MIN_FILTER, fFilterMin);
  7268. end;
  7269. end;
  7270. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7271. procedure TglBitmap.SetWrap(const S: GLenum; const T: GLenum; const R: GLenum);
  7272. procedure CheckAndSetWrap(const aValue: Cardinal; var aTarget: Cardinal);
  7273. begin
  7274. case aValue of
  7275. {$IFNDEF OPENGL_ES}
  7276. GL_CLAMP:
  7277. aTarget := GL_CLAMP;
  7278. {$ENDIF}
  7279. GL_REPEAT:
  7280. aTarget := GL_REPEAT;
  7281. GL_CLAMP_TO_EDGE: begin
  7282. {$IFNDEF OPENGL_ES}
  7283. if not GL_VERSION_1_2 and not GL_EXT_texture_edge_clamp then
  7284. aTarget := GL_CLAMP
  7285. else
  7286. {$ENDIF}
  7287. aTarget := GL_CLAMP_TO_EDGE;
  7288. end;
  7289. {$IFNDEF OPENGL_ES}
  7290. GL_CLAMP_TO_BORDER: begin
  7291. if GL_VERSION_1_3 or GL_ARB_texture_border_clamp then
  7292. aTarget := GL_CLAMP_TO_BORDER
  7293. else
  7294. aTarget := GL_CLAMP;
  7295. end;
  7296. {$ENDIF}
  7297. {$IF NOT DEFINED(OPENGL_ES) OR DEFINED(OPENGL_ES_2_0)}
  7298. GL_MIRRORED_REPEAT: begin
  7299. {$IFNDEF OPENGL_ES}
  7300. if GL_VERSION_1_4 or GL_ARB_texture_mirrored_repeat or GL_IBM_texture_mirrored_repeat then
  7301. {$ELSE}
  7302. if GL_VERSION_2_0 then
  7303. {$ENDIF}
  7304. aTarget := GL_MIRRORED_REPEAT
  7305. else
  7306. raise EglBitmap.Create('SetWrap - Unsupported Texturewrap GL_MIRRORED_REPEAT (S).');
  7307. end;
  7308. {$IFEND}
  7309. else
  7310. raise EglBitmap.Create('SetWrap - Unknow Texturewrap');
  7311. end;
  7312. end;
  7313. begin
  7314. CheckAndSetWrap(S, fWrapS);
  7315. CheckAndSetWrap(T, fWrapT);
  7316. CheckAndSetWrap(R, fWrapR);
  7317. if (ID > 0) then begin
  7318. Bind({$IFNDEF OPENGL_ES}false{$ENDIF});
  7319. glTexParameteri(Target, GL_TEXTURE_WRAP_S, fWrapS);
  7320. glTexParameteri(Target, GL_TEXTURE_WRAP_T, fWrapT);
  7321. {$IF NOT DEFINED(OPENGL_ES) OR DEFINED(OPENGL_ES_3_0)}
  7322. {$IFDEF OPENGL_ES} if GL_VERSION_3_0 then{$ENDIF}
  7323. glTexParameteri(Target, GL_TEXTURE_WRAP_R, fWrapR);
  7324. {$IFEND}
  7325. end;
  7326. end;
  7327. {$IF NOT DEFINED(OPENGL_ES) OR DEFINED(OPENGL_ES_3_0)}
  7328. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7329. procedure TglBitmap.SetSwizzle(const r, g, b, a: GLenum);
  7330. procedure CheckAndSetValue(const aValue: GLenum; const aIndex: Integer);
  7331. begin
  7332. if (aValue = GL_ZERO) or (aValue = GL_ONE) or (aValue = GL_ALPHA) or
  7333. (aValue = GL_RED) or (aValue = GL_GREEN) or (aValue = GL_BLUE) then
  7334. fSwizzle[aIndex] := aValue
  7335. else
  7336. raise EglBitmap.Create('SetSwizzle - Unknow Swizle Value');
  7337. end;
  7338. begin
  7339. {$IFNDEF OPENGL_ES}
  7340. if not (GL_ARB_texture_swizzle or GL_EXT_texture_swizzle or GL_VERSION_3_3) then
  7341. raise EglBitmapNotSupported.Create('texture swizzle is not supported');
  7342. {$ELSE}
  7343. if not GL_VERSION_3_0 then
  7344. raise EglBitmapNotSupported.Create('texture swizzle is not supported');
  7345. {$ENDIF}
  7346. CheckAndSetValue(r, 0);
  7347. CheckAndSetValue(g, 1);
  7348. CheckAndSetValue(b, 2);
  7349. CheckAndSetValue(a, 3);
  7350. if (ID > 0) then begin
  7351. Bind(false);
  7352. {$IFNDEF OPENGL_ES}
  7353. glTexParameteriv(Target, GL_TEXTURE_SWIZZLE_RGBA, PGLint(@fSwizzle[0]));
  7354. {$ELSE}
  7355. glTexParameteriv(Target, GL_TEXTURE_SWIZZLE_R, PGLint(@fSwizzle[0]));
  7356. glTexParameteriv(Target, GL_TEXTURE_SWIZZLE_G, PGLint(@fSwizzle[1]));
  7357. glTexParameteriv(Target, GL_TEXTURE_SWIZZLE_B, PGLint(@fSwizzle[2]));
  7358. glTexParameteriv(Target, GL_TEXTURE_SWIZZLE_A, PGLint(@fSwizzle[3]));
  7359. {$ENDIF}
  7360. end;
  7361. end;
  7362. {$IFEND}
  7363. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7364. procedure TglBitmap.Bind({$IFNDEF OPENGL_ES}const aEnableTextureUnit: Boolean{$ENDIF});
  7365. begin
  7366. {$IFNDEF OPENGL_ES}
  7367. if aEnableTextureUnit then
  7368. glEnable(Target);
  7369. {$ENDIF}
  7370. if (ID > 0) then
  7371. glBindTexture(Target, ID);
  7372. end;
  7373. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7374. procedure TglBitmap.Unbind({$IFNDEF OPENGL_ES}const aDisableTextureUnit: Boolean{$ENDIF});
  7375. begin
  7376. {$IFNDEF OPENGL_ES}
  7377. if aDisableTextureUnit then
  7378. glDisable(Target);
  7379. {$ENDIF}
  7380. glBindTexture(Target, 0);
  7381. end;
  7382. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7383. procedure TglBitmap.UploadData(const aDataObj: TglBitmapData; const aCheckSize: Boolean);
  7384. var
  7385. w, h: Integer;
  7386. begin
  7387. w := aDataObj.Width;
  7388. h := aDataObj.Height;
  7389. fDimension.Fields := [];
  7390. if (w > 0) then
  7391. fDimension.Fields := fDimension.Fields + [ffX];
  7392. if (h > 0) then
  7393. fDimension.Fields := fDimension.Fields + [ffY];
  7394. fDimension.X := w;
  7395. fDimension.Y := h;
  7396. end;
  7397. {$IFNDEF OPENGL_ES}
  7398. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7399. function TglBitmap.DownloadData(const aDataObj: TglBitmapData): Boolean;
  7400. var
  7401. Temp: PByte;
  7402. TempWidth, TempHeight: Integer;
  7403. TempIntFormat: GLint;
  7404. IntFormat: TglBitmapFormat;
  7405. FormatDesc: TFormatDescriptor;
  7406. begin
  7407. result := false;
  7408. Bind;
  7409. // Request Data
  7410. glGetTexLevelParameteriv(Target, 0, GL_TEXTURE_WIDTH, @TempWidth);
  7411. glGetTexLevelParameteriv(Target, 0, GL_TEXTURE_HEIGHT, @TempHeight);
  7412. glGetTexLevelParameteriv(Target, 0, GL_TEXTURE_INTERNAL_FORMAT, @TempIntFormat);
  7413. FormatDesc := (TglBitmapFormatDescriptor.GetByFormat(TempIntFormat) as TFormatDescriptor);
  7414. IntFormat := FormatDesc.Format;
  7415. // Getting data from OpenGL
  7416. FormatDesc := TFormatDescriptor.Get(IntFormat);
  7417. GetMem(Temp, FormatDesc.GetSize(TempWidth, TempHeight));
  7418. try
  7419. glPixelStorei(GL_PACK_ALIGNMENT, 1);
  7420. if FormatDesc.IsCompressed then begin
  7421. if not Assigned(glGetCompressedTexImage) then
  7422. raise EglBitmap.Create('compressed formats not supported by video adapter');
  7423. glGetCompressedTexImage(Target, 0, Temp)
  7424. end else
  7425. glGetTexImage(Target, 0, FormatDesc.glFormat, FormatDesc.glDataFormat, Temp);
  7426. aDataObj.SetData(Temp, IntFormat, TempWidth, TempHeight);
  7427. result := true;
  7428. except
  7429. if Assigned(Temp) then
  7430. FreeMem(Temp);
  7431. raise;
  7432. end;
  7433. end;
  7434. {$ENDIF}
  7435. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7436. constructor TglBitmap.Create;
  7437. begin
  7438. if (ClassType = TglBitmap) then
  7439. raise EglBitmap.Create('Don''t create TglBitmap directly. Use one of the deviated classes (TglBitmap2D) instead.');
  7440. inherited Create;
  7441. end;
  7442. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7443. constructor TglBitmap.Create(const aData: TglBitmapData);
  7444. begin
  7445. Create;
  7446. UploadData(aData);
  7447. end;
  7448. {$IFNDEF OPENGL_ES}
  7449. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7450. //TglBitmap1D/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7451. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7452. procedure TglBitmap1D.UploadDataIntern(const aDataObj: TglBitmapData; const aBuildWithGlu: Boolean);
  7453. var
  7454. fd: TglBitmapFormatDescriptor;
  7455. begin
  7456. // Upload data
  7457. fd := aDataObj.FormatDescriptor;
  7458. if (fd.glFormat = 0) or (fd.glInternalFormat = 0) or (fd.glDataFormat = 0) then
  7459. raise EglBitmap.Create('format is not supported by video adapter, please convert before uploading data');
  7460. if fd.IsCompressed then begin
  7461. if not Assigned(glCompressedTexImage1D) then
  7462. raise EglBitmap.Create('compressed formats not supported by video adapter');
  7463. glCompressedTexImage1D(Target, 0, fd.glInternalFormat, aDataObj.Width, 0, fd.GetSize(aDataObj.Width, 1), aDataObj.Data)
  7464. end else if aBuildWithGlu then
  7465. gluBuild1DMipmaps(Target, fd.glInternalFormat, aDataObj.Width, fd.glFormat, fd.glDataFormat, aDataObj.Data)
  7466. else
  7467. glTexImage1D(Target, 0, fd.glInternalFormat, aDataObj.Width, 0, fd.glFormat, fd.glDataFormat, aDataObj.Data);
  7468. end;
  7469. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7470. procedure TglBitmap1D.AfterConstruction;
  7471. begin
  7472. inherited;
  7473. Target := GL_TEXTURE_1D;
  7474. end;
  7475. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7476. procedure TglBitmap1D.UploadData(const aDataObj: TglBitmapData; const aCheckSize: Boolean);
  7477. var
  7478. BuildWithGlu, TexRec: Boolean;
  7479. TexSize: Integer;
  7480. begin
  7481. if not Assigned(aDataObj) then
  7482. exit;
  7483. // Check Texture Size
  7484. if (aCheckSize) then begin
  7485. glGetIntegerv(GL_MAX_TEXTURE_SIZE, @TexSize);
  7486. if (aDataObj.Width > TexSize) then
  7487. raise EglBitmapSizeToLarge.Create('TglBitmap1D.GenTexture - The size for the texture is to large. It''s may be not conform with the Hardware.');
  7488. TexRec := (GL_ARB_texture_rectangle or GL_EXT_texture_rectangle or GL_NV_texture_rectangle) and
  7489. (Target = GL_TEXTURE_RECTANGLE);
  7490. if not (IsPowerOfTwo(aDataObj.Width) or GL_ARB_texture_non_power_of_two or GL_VERSION_2_0 or TexRec) then
  7491. raise EglBitmapNonPowerOfTwo.Create('TglBitmap1D.GenTexture - Rendercontex dosn''t support non power of two texture.');
  7492. end;
  7493. inherited UploadData(aDataObj, aCheckSize);
  7494. if (fID = 0) then
  7495. CreateID;
  7496. SetupParameters(BuildWithGlu);
  7497. UploadDataIntern(aDataObj, BuildWithGlu);
  7498. glAreTexturesResident(1, @fID, @fIsResident);
  7499. end;
  7500. {$ENDIF}
  7501. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7502. //TglBitmap2D/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7503. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7504. procedure TglBitmap2D.UploadDataIntern(const aDataObj: TglBitmapData; const aTarget: GLenum{$IFNDEF OPENGL_ES}; const aBuildWithGlu: Boolean{$ENDIF});
  7505. var
  7506. fd: TglBitmapFormatDescriptor;
  7507. begin
  7508. fd := aDataObj.FormatDescriptor;
  7509. if (fd.glFormat = 0) or (fd.glInternalFormat = 0) or (fd.glDataFormat = 0) then
  7510. raise EglBitmap.Create('format is not supported by video adapter, please convert before uploading data');
  7511. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  7512. if fd.IsCompressed then begin
  7513. if not Assigned(glCompressedTexImage2D) then
  7514. raise EglBitmap.Create('compressed formats not supported by video adapter');
  7515. glCompressedTexImage2D(aTarget, 0, fd.glInternalFormat, aDataObj.Width, aDataObj.Height, 0, fd.GetSize(fDimension), aDataObj.Data)
  7516. {$IFNDEF OPENGL_ES}
  7517. end else if aBuildWithGlu then begin
  7518. gluBuild2DMipmaps(aTarget, fd.ChannelCount, aDataObj.Width, aDataObj.Height, fd.glFormat, fd.glDataFormat, aDataObj.Data)
  7519. {$ENDIF}
  7520. end else begin
  7521. glTexImage2D(aTarget, 0, fd.glInternalFormat, aDataObj.Width, aDataObj.Height, 0, fd.glFormat, fd.glDataFormat, aDataObj.Data);
  7522. end;
  7523. end;
  7524. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7525. procedure TglBitmap2D.AfterConstruction;
  7526. begin
  7527. inherited;
  7528. Target := GL_TEXTURE_2D;
  7529. end;
  7530. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7531. procedure TglBitmap2D.UploadData(const aDataObj: TglBitmapData; const aCheckSize: Boolean);
  7532. var
  7533. {$IFNDEF OPENGL_ES}
  7534. BuildWithGlu, TexRec: Boolean;
  7535. {$ENDIF}
  7536. PotTex: Boolean;
  7537. TexSize: Integer;
  7538. begin
  7539. if not Assigned(aDataObj) then
  7540. exit;
  7541. // Check Texture Size
  7542. if (aCheckSize) then begin
  7543. glGetIntegerv(GL_MAX_TEXTURE_SIZE, @TexSize);
  7544. if ((aDataObj.Width > TexSize) or (aDataObj.Height > TexSize)) then
  7545. raise EglBitmapSizeToLarge.Create('TglBitmap2D.GenTexture - The size for the texture is to large. It''s may be not conform with the Hardware.');
  7546. PotTex := IsPowerOfTwo(aDataObj.Width) and IsPowerOfTwo(aDataObj.Height);
  7547. {$IF NOT DEFINED(OPENGL_ES)}
  7548. TexRec := (GL_ARB_texture_rectangle or GL_EXT_texture_rectangle or GL_NV_texture_rectangle) and (Target = GL_TEXTURE_RECTANGLE);
  7549. if not (PotTex or GL_ARB_texture_non_power_of_two or GL_VERSION_2_0 or TexRec) then
  7550. raise EglBitmapNonPowerOfTwo.Create('TglBitmap2D.GenTexture - Rendercontex dosn''t support non power of two texture.');
  7551. {$ELSEIF DEFINED(OPENGL_ES_EXT)}
  7552. if not PotTex and not GL_OES_texture_npot then
  7553. raise EglBitmapNonPowerOfTwo.Create('TglBitmap2D.GenTexture - Rendercontex dosn''t support non power of two texture.');
  7554. {$ELSE}
  7555. if not PotTex then
  7556. raise EglBitmapNonPowerOfTwo.Create('TglBitmap2D.GenTexture - Rendercontex dosn''t support non power of two texture.');
  7557. {$IFEND}
  7558. end;
  7559. inherited UploadData(aDataObj, aCheckSize);
  7560. if (fID = 0) then
  7561. CreateID;
  7562. SetupParameters({$IFNDEF OPENGL_ES}BuildWithGlu{$ENDIF});
  7563. UploadDataIntern(aDataObj, Target{$IFNDEF OPENGL_ES}, BuildWithGlu{$ENDIF});
  7564. {$IFNDEF OPENGL_ES}
  7565. glAreTexturesResident(1, @fID, @fIsResident);
  7566. {$ENDIF}
  7567. end;
  7568. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7569. class procedure TglBitmap2D.GrabScreen(const aTop, aLeft, aRight, aBottom: Integer; const aFormat: TglBitmapFormat; const aDataObj: TglBitmapData);
  7570. var
  7571. Temp: pByte;
  7572. Size, w, h: Integer;
  7573. FormatDesc: TFormatDescriptor;
  7574. begin
  7575. FormatDesc := TFormatDescriptor.Get(aFormat);
  7576. if FormatDesc.IsCompressed then
  7577. raise EglBitmapUnsupportedFormat.Create(aFormat);
  7578. w := aRight - aLeft;
  7579. h := aBottom - aTop;
  7580. Size := FormatDesc.GetSize(w, h);
  7581. GetMem(Temp, Size);
  7582. try
  7583. glPixelStorei(GL_PACK_ALIGNMENT, 1);
  7584. glReadPixels(aLeft, aTop, w, h, FormatDesc.glFormat, FormatDesc.glDataFormat, Temp);
  7585. aDataObj.SetData(Temp, aFormat, w, h);
  7586. aDataObj.FlipVert;
  7587. except
  7588. if Assigned(Temp) then
  7589. FreeMem(Temp);
  7590. raise;
  7591. end;
  7592. end;
  7593. {$IF NOT DEFINED(OPENGL_ES) OR DEFINED(OPENGL_ES_2_0)}
  7594. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7595. //TglBitmapCubeMap////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7596. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7597. procedure TglBitmapCubeMap.AfterConstruction;
  7598. begin
  7599. inherited;
  7600. {$IFNDEF OPENGL_ES}
  7601. if not (GL_VERSION_1_3 or GL_ARB_texture_cube_map or GL_EXT_texture_cube_map) then
  7602. raise EglBitmap.Create('TglBitmapCubeMap.AfterConstruction - CubeMaps are unsupported.');
  7603. {$ELSE}
  7604. if not (GL_VERSION_2_0) then
  7605. raise EglBitmap.Create('TglBitmapCubeMap.AfterConstruction - CubeMaps are unsupported.');
  7606. {$ENDIF}
  7607. SetWrap;
  7608. Target := GL_TEXTURE_CUBE_MAP;
  7609. {$IFNDEF OPENGL_ES}
  7610. fGenMode := GL_REFLECTION_MAP;
  7611. {$ENDIF}
  7612. end;
  7613. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7614. procedure TglBitmapCubeMap.UploadData(const aDataObj: TglBitmapData; const aCheckSize: Boolean);
  7615. begin
  7616. Assert(false, 'TglBitmapCubeMap.UploadData - Don''t call UploadData directly, use UploadCubeMap instead');
  7617. end;
  7618. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7619. procedure TglBitmapCubeMap.UploadCubeMap(const aDataObj: TglBitmapData; const aCubeTarget: Cardinal; const aCheckSize: Boolean);
  7620. var
  7621. {$IFNDEF OPENGL_ES}
  7622. BuildWithGlu: Boolean;
  7623. {$ENDIF}
  7624. TexSize: Integer;
  7625. begin
  7626. if (aCheckSize) then begin
  7627. glGetIntegerv(GL_MAX_CUBE_MAP_TEXTURE_SIZE, @TexSize);
  7628. if (aDataObj.Width > TexSize) or (aDataObj.Height > TexSize) then
  7629. raise EglBitmapSizeToLarge.Create('TglBitmapCubeMap.GenerateCubeMap - The size for the Cubemap is to large. It''s may be not conform with the Hardware.');
  7630. {$IF NOT DEFINED(OPENGL_ES)}
  7631. if not ((IsPowerOfTwo(aDataObj.Width) and IsPowerOfTwo(aDataObj.Height)) or GL_VERSION_2_0 or GL_ARB_texture_non_power_of_two) then
  7632. raise EglBitmapNonPowerOfTwo.Create('TglBitmapCubeMap.GenerateCubeMap - Cubemaps dosn''t support non power of two texture.');
  7633. {$ELSEIF DEFINED(OPENGL_ES_EXT)}
  7634. if not (IsPowerOfTwo(aDataObj.Width) and IsPowerOfTwo(aDataObj.Height)) and not GL_OES_texture_npot then
  7635. raise EglBitmapNonPowerOfTwo.Create('TglBitmapCubeMap.GenerateCubeMap - Cubemaps dosn''t support non power of two texture.');
  7636. {$ELSE}
  7637. if not (IsPowerOfTwo(aDataObj.Width) and IsPowerOfTwo(aDataObj.Height)) then
  7638. raise EglBitmapNonPowerOfTwo.Create('TglBitmapCubeMap.GenerateCubeMap - Cubemaps dosn''t support non power of two texture.');
  7639. {$IFEND}
  7640. end;
  7641. inherited UploadData(aDataObj, aCheckSize);
  7642. if (fID = 0) then
  7643. CreateID;
  7644. SetupParameters({$IFNDEF OPENGL_ES}BuildWithGlu{$ENDIF});
  7645. UploadDataIntern(aDataObj, aCubeTarget{$IFNDEF OPENGL_ES}, BuildWithGlu{$ENDIF});
  7646. end;
  7647. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7648. procedure TglBitmapCubeMap.Bind({$IFNDEF OPENGL_ES}const aEnableTexCoordsGen: Boolean; const aEnableTextureUnit: Boolean{$ENDIF});
  7649. begin
  7650. inherited Bind({$IFNDEF OPENGL_ES}aEnableTextureUnit{$ENDIF});
  7651. {$IFNDEF OPENGL_ES}
  7652. if aEnableTexCoordsGen then begin
  7653. glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, fGenMode);
  7654. glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, fGenMode);
  7655. glTexGeni(GL_R, GL_TEXTURE_GEN_MODE, fGenMode);
  7656. glEnable(GL_TEXTURE_GEN_S);
  7657. glEnable(GL_TEXTURE_GEN_T);
  7658. glEnable(GL_TEXTURE_GEN_R);
  7659. end;
  7660. {$ENDIF}
  7661. end;
  7662. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7663. procedure TglBitmapCubeMap.Unbind({$IFNDEF OPENGL_ES}const aDisableTexCoordsGen: Boolean; const aDisableTextureUnit: Boolean{$ENDIF});
  7664. begin
  7665. inherited Unbind({$IFNDEF OPENGL_ES}aDisableTextureUnit{$ENDIF});
  7666. {$IFNDEF OPENGL_ES}
  7667. if aDisableTexCoordsGen then begin
  7668. glDisable(GL_TEXTURE_GEN_S);
  7669. glDisable(GL_TEXTURE_GEN_T);
  7670. glDisable(GL_TEXTURE_GEN_R);
  7671. end;
  7672. {$ENDIF}
  7673. end;
  7674. {$IFEND}
  7675. {$IF NOT DEFINED(OPENGL_ES) OR DEFINED(OPENGL_ES_2_0)}
  7676. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7677. //TglBitmapNormalMap//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7678. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7679. type
  7680. TVec = Array[0..2] of Single;
  7681. TglBitmapNormalMapGetVectorFunc = procedure (out aVec: TVec; const aPosition: TglBitmapPixelPosition; const aHalfSize: Integer);
  7682. PglBitmapNormalMapRec = ^TglBitmapNormalMapRec;
  7683. TglBitmapNormalMapRec = record
  7684. HalfSize : Integer;
  7685. Func: TglBitmapNormalMapGetVectorFunc;
  7686. end;
  7687. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7688. procedure glBitmapNormalMapPosX(out aVec: TVec; const aPosition: TglBitmapPixelPosition; const aHalfSize: Integer);
  7689. begin
  7690. aVec[0] := aHalfSize;
  7691. aVec[1] := - (aPosition.Y + 0.5 - aHalfSize);
  7692. aVec[2] := - (aPosition.X + 0.5 - aHalfSize);
  7693. end;
  7694. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7695. procedure glBitmapNormalMapNegX(out aVec: TVec; const aPosition: TglBitmapPixelPosition; const aHalfSize: Integer);
  7696. begin
  7697. aVec[0] := - aHalfSize;
  7698. aVec[1] := - (aPosition.Y + 0.5 - aHalfSize);
  7699. aVec[2] := aPosition.X + 0.5 - aHalfSize;
  7700. end;
  7701. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7702. procedure glBitmapNormalMapPosY(out aVec: TVec; const aPosition: TglBitmapPixelPosition; const aHalfSize: Integer);
  7703. begin
  7704. aVec[0] := aPosition.X + 0.5 - aHalfSize;
  7705. aVec[1] := aHalfSize;
  7706. aVec[2] := aPosition.Y + 0.5 - aHalfSize;
  7707. end;
  7708. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7709. procedure glBitmapNormalMapNegY(out aVec: TVec; const aPosition: TglBitmapPixelPosition; const aHalfSize: Integer);
  7710. begin
  7711. aVec[0] := aPosition.X + 0.5 - aHalfSize;
  7712. aVec[1] := - aHalfSize;
  7713. aVec[2] := - (aPosition.Y + 0.5 - aHalfSize);
  7714. end;
  7715. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7716. procedure glBitmapNormalMapPosZ(out aVec: TVec; const aPosition: TglBitmapPixelPosition; const aHalfSize: Integer);
  7717. begin
  7718. aVec[0] := aPosition.X + 0.5 - aHalfSize;
  7719. aVec[1] := - (aPosition.Y + 0.5 - aHalfSize);
  7720. aVec[2] := aHalfSize;
  7721. end;
  7722. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7723. procedure glBitmapNormalMapNegZ(out aVec: TVec; const aPosition: TglBitmapPixelPosition; const aHalfSize: Integer);
  7724. begin
  7725. aVec[0] := - (aPosition.X + 0.5 - aHalfSize);
  7726. aVec[1] := - (aPosition.Y + 0.5 - aHalfSize);
  7727. aVec[2] := - aHalfSize;
  7728. end;
  7729. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7730. procedure glBitmapNormalMapFunc(var FuncRec: TglBitmapFunctionRec);
  7731. var
  7732. i: Integer;
  7733. Vec: TVec;
  7734. Len: Single;
  7735. begin
  7736. with FuncRec do begin
  7737. with PglBitmapNormalMapRec(Args)^ do begin
  7738. Func(Vec, Position, HalfSize);
  7739. // Normalize
  7740. Len := 1 / Sqrt(Sqr(Vec[0]) + Sqr(Vec[1]) + Sqr(Vec[2]));
  7741. if Len <> 0 then begin
  7742. Vec[0] := Vec[0] * Len;
  7743. Vec[1] := Vec[1] * Len;
  7744. Vec[2] := Vec[2] * Len;
  7745. end;
  7746. // Scale Vector and AddVectro
  7747. Vec[0] := Vec[0] * 0.5 + 0.5;
  7748. Vec[1] := Vec[1] * 0.5 + 0.5;
  7749. Vec[2] := Vec[2] * 0.5 + 0.5;
  7750. end;
  7751. // Set Color
  7752. for i := 0 to 2 do
  7753. Dest.Data.arr[i] := Round(Vec[i] * 255);
  7754. end;
  7755. end;
  7756. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7757. procedure TglBitmapNormalMap.AfterConstruction;
  7758. begin
  7759. inherited;
  7760. {$IFNDEF OPENGL_ES}
  7761. fGenMode := GL_NORMAL_MAP;
  7762. {$ENDIF}
  7763. end;
  7764. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7765. procedure TglBitmapNormalMap.GenerateNormalMap(const aSize: Integer; const aCheckSize: Boolean);
  7766. var
  7767. Rec: TglBitmapNormalMapRec;
  7768. SizeRec: TglBitmapSize;
  7769. DataObj: TglBitmapData;
  7770. begin
  7771. Rec.HalfSize := aSize div 2;
  7772. SizeRec.Fields := [ffX, ffY];
  7773. SizeRec.X := aSize;
  7774. SizeRec.Y := aSize;
  7775. DataObj := TglBitmapData.Create;
  7776. try
  7777. // Positive X
  7778. Rec.Func := glBitmapNormalMapPosX;
  7779. DataObj.LoadFromFunc(SizeRec, tfBGR8ub3, glBitmapNormalMapFunc, @Rec);
  7780. UploadCubeMap(DataObj, GL_TEXTURE_CUBE_MAP_POSITIVE_X, aCheckSize);
  7781. // Negative X
  7782. Rec.Func := glBitmapNormalMapNegX;
  7783. DataObj.LoadFromFunc(SizeRec, tfBGR8ub3, glBitmapNormalMapFunc, @Rec);
  7784. UploadCubeMap(DataObj, GL_TEXTURE_CUBE_MAP_NEGATIVE_X, aCheckSize);
  7785. // Positive Y
  7786. Rec.Func := glBitmapNormalMapPosY;
  7787. DataObj.LoadFromFunc(SizeRec, tfBGR8ub3, glBitmapNormalMapFunc, @Rec);
  7788. UploadCubeMap(DataObj, GL_TEXTURE_CUBE_MAP_POSITIVE_Y, aCheckSize);
  7789. // Negative Y
  7790. Rec.Func := glBitmapNormalMapNegY;
  7791. DataObj.LoadFromFunc(SizeRec, tfBGR8ub3, glBitmapNormalMapFunc, @Rec);
  7792. UploadCubeMap(DataObj, GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, aCheckSize);
  7793. // Positive Z
  7794. Rec.Func := glBitmapNormalMapPosZ;
  7795. DataObj.LoadFromFunc(SizeRec, tfBGR8ub3, glBitmapNormalMapFunc, @Rec);
  7796. UploadCubeMap(DataObj, GL_TEXTURE_CUBE_MAP_POSITIVE_Z, aCheckSize);
  7797. // Negative Z
  7798. Rec.Func := glBitmapNormalMapNegZ;
  7799. DataObj.LoadFromFunc(SizeRec, tfBGR8ub3, glBitmapNormalMapFunc, @Rec);
  7800. UploadCubeMap(DataObj, GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, aCheckSize);
  7801. finally
  7802. FreeAndNil(DataObj);
  7803. end;
  7804. end;
  7805. {$IFEND}
  7806. initialization
  7807. glBitmapSetDefaultFormat (tfEmpty);
  7808. glBitmapSetDefaultMipmap (mmMipmap);
  7809. glBitmapSetDefaultFilter (GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR);
  7810. glBitmapSetDefaultWrap (GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
  7811. {$IF NOT DEFINED(OPENGL_ES) OR DEFINED(OPENGL_ES_3_0)}
  7812. glBitmapSetDefaultSwizzle(GL_RED, GL_GREEN, GL_BLUE, GL_ALPHA);
  7813. {$IFEND}
  7814. glBitmapSetDefaultFreeDataAfterGenTexture(true);
  7815. glBitmapSetDefaultDeleteTextureOnFree (true);
  7816. TFormatDescriptor.Init;
  7817. finalization
  7818. TFormatDescriptor.Finalize;
  7819. end.