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.

8897 rivejä
316 KiB

  1. { glBitmap by Steffen Xonna aka Lossy eX (2003-2008)
  2. http://www.opengl24.de/index.php?cat=header&file=glbitmap
  3. modified by Delphi OpenGL Community (http://delphigl.com/) (2013)
  4. The contents of this file are used with permission, subject to
  5. the Mozilla Public License Version 1.1 (the "License"); you may
  6. not use this file except in compliance with the License. You may
  7. obtain a copy of the License at
  8. http://www.mozilla.org/MPL/MPL-1.1.html
  9. The glBitmap is a Delphi/FPC unit that contains several wrapper classes
  10. to manage OpenGL texture objects. Below you can find a list of the main
  11. functionality of this classes:
  12. - load texture data from file (e.g. BMP, TGA, DDS, PNG, JPEG, ...)
  13. - load texture data from several other image objects (e.g. TBitmap, TLazIntfImage, SDL Surface)
  14. - save texture data to file (e.g. BMP, TGA, DDS, PNG, JPEG, ...)
  15. - save texture data to several other image objects (e.g. TBitmap, TLazIntfImage, SDL Surface)
  16. - support for many texture formats (e.g. RGB8, BGR8, RGBA8, BGRA8, ...)
  17. - manage texture properties (e.g. Filter, Clamp, Mipmap, ...)
  18. - upload texture data to video card
  19. - download texture data from video card
  20. - manipulate texture data (e.g. add alpha, remove alpha, convert to other format, switch RGB, ...) }
  21. unit glBitmap;
  22. {$I glBitmapConf.inc}
  23. // Delphi Versions
  24. {$IFDEF fpc}
  25. {$MODE Delphi}
  26. {$IFDEF CPUI386}
  27. {$DEFINE CPU386}
  28. {$ASMMODE INTEL}
  29. {$ENDIF}
  30. {$IFNDEF WINDOWS}
  31. {$linklib c}
  32. {$ENDIF}
  33. {$ENDIF}
  34. // Operation System
  35. {$IF DEFINED(WIN32) or DEFINED(WIN64) or DEFINED(WINDOWS)}
  36. {$DEFINE GLB_WIN}
  37. {$ELSEIF DEFINED(LINUX)}
  38. {$DEFINE GLB_LINUX}
  39. {$IFEND}
  40. // OpenGL ES
  41. {$IF DEFINED(OPENGL_ES_EXT)} {$DEFINE OPENGL_ES_1_1} {$IFEND}
  42. {$IF DEFINED(OPENGL_ES_3_0)} {$DEFINE OPENGL_ES_2_0} {$IFEND}
  43. {$IF DEFINED(OPENGL_ES_2_0)} {$DEFINE OPENGL_ES_1_1} {$IFEND}
  44. {$IF DEFINED(OPENGL_ES_1_1)} {$DEFINE OPENGL_ES} {$IFEND}
  45. // checking define combinations
  46. //SDL Image
  47. {$IFDEF GLB_SDL_IMAGE}
  48. {$IFNDEF GLB_SDL}
  49. {$MESSAGE warn 'SDL_image won''t work without SDL. SDL will be activated.'}
  50. {$DEFINE GLB_SDL}
  51. {$ENDIF}
  52. {$IFDEF GLB_LAZ_PNG}
  53. {$MESSAGE warn 'The Lazarus TPortableNetworkGraphics will be ignored because you are using SDL_image.'}
  54. {$undef GLB_LAZ_PNG}
  55. {$ENDIF}
  56. {$IFDEF GLB_PNGIMAGE}
  57. {$MESSAGE warn 'The unit pngimage will be ignored because you are using SDL_image.'}
  58. {$undef GLB_PNGIMAGE}
  59. {$ENDIF}
  60. {$IFDEF GLB_LAZ_JPEG}
  61. {$MESSAGE warn 'The Lazarus TJPEGImage will be ignored because you are using SDL_image.'}
  62. {$undef GLB_LAZ_JPEG}
  63. {$ENDIF}
  64. {$IFDEF GLB_DELPHI_JPEG}
  65. {$MESSAGE warn 'The unit JPEG will be ignored because you are using SDL_image.'}
  66. {$undef GLB_DELPHI_JPEG}
  67. {$ENDIF}
  68. {$IFDEF GLB_LIB_PNG}
  69. {$MESSAGE warn 'The library libPNG will be ignored because you are using SDL_image.'}
  70. {$undef GLB_LIB_PNG}
  71. {$ENDIF}
  72. {$IFDEF GLB_LIB_JPEG}
  73. {$MESSAGE warn 'The library libJPEG will be ignored because you are using SDL_image.'}
  74. {$undef GLB_LIB_JPEG}
  75. {$ENDIF}
  76. {$DEFINE GLB_SUPPORT_PNG_READ}
  77. {$DEFINE GLB_SUPPORT_JPEG_READ}
  78. {$ENDIF}
  79. // Lazarus TPortableNetworkGraphic
  80. {$IFDEF GLB_LAZ_PNG}
  81. {$IFNDEF GLB_LAZARUS}
  82. {$MESSAGE warn 'Lazarus TPortableNetworkGraphic won''t work without Lazarus. Lazarus will be activated.'}
  83. {$DEFINE GLB_LAZARUS}
  84. {$ENDIF}
  85. {$IFDEF GLB_PNGIMAGE}
  86. {$MESSAGE warn 'The pngimage will be ignored if you are using Lazarus TPortableNetworkGraphic.'}
  87. {$undef GLB_PNGIMAGE}
  88. {$ENDIF}
  89. {$IFDEF GLB_LIB_PNG}
  90. {$MESSAGE warn 'The library libPNG will be ignored if you are using Lazarus TPortableNetworkGraphic.'}
  91. {$undef GLB_LIB_PNG}
  92. {$ENDIF}
  93. {$DEFINE GLB_SUPPORT_PNG_READ}
  94. {$DEFINE GLB_SUPPORT_PNG_WRITE}
  95. {$ENDIF}
  96. // PNG Image
  97. {$IFDEF GLB_PNGIMAGE}
  98. {$IFDEF GLB_LIB_PNG}
  99. {$MESSAGE warn 'The library libPNG will be ignored if you are using pngimage.'}
  100. {$undef GLB_LIB_PNG}
  101. {$ENDIF}
  102. {$DEFINE GLB_SUPPORT_PNG_READ}
  103. {$DEFINE GLB_SUPPORT_PNG_WRITE}
  104. {$ENDIF}
  105. // libPNG
  106. {$IFDEF GLB_LIB_PNG}
  107. {$DEFINE GLB_SUPPORT_PNG_READ}
  108. {$DEFINE GLB_SUPPORT_PNG_WRITE}
  109. {$ENDIF}
  110. // Lazarus TJPEGImage
  111. {$IFDEF GLB_LAZ_JPEG}
  112. {$IFNDEF GLB_LAZARUS}
  113. {$MESSAGE warn 'Lazarus TJPEGImage won''t work without Lazarus. Lazarus will be activated.'}
  114. {$DEFINE GLB_LAZARUS}
  115. {$ENDIF}
  116. {$IFDEF GLB_DELPHI_JPEG}
  117. {$MESSAGE warn 'The Delphi JPEGImage will be ignored if you are using the Lazarus TJPEGImage.'}
  118. {$undef GLB_DELPHI_JPEG}
  119. {$ENDIF}
  120. {$IFDEF GLB_LIB_JPEG}
  121. {$MESSAGE warn 'The library libJPEG will be ignored if you are using the Lazarus TJPEGImage.'}
  122. {$undef GLB_LIB_JPEG}
  123. {$ENDIF}
  124. {$DEFINE GLB_SUPPORT_JPEG_READ}
  125. {$DEFINE GLB_SUPPORT_JPEG_WRITE}
  126. {$ENDIF}
  127. // JPEG Image
  128. {$IFDEF GLB_DELPHI_JPEG}
  129. {$IFDEF GLB_LIB_JPEG}
  130. {$MESSAGE warn 'The library libJPEG will be ignored if you are using the unit JPEG.'}
  131. {$undef GLB_LIB_JPEG}
  132. {$ENDIF}
  133. {$DEFINE GLB_SUPPORT_JPEG_READ}
  134. {$DEFINE GLB_SUPPORT_JPEG_WRITE}
  135. {$ENDIF}
  136. // libJPEG
  137. {$IFDEF GLB_LIB_JPEG}
  138. {$DEFINE GLB_SUPPORT_JPEG_READ}
  139. {$DEFINE GLB_SUPPORT_JPEG_WRITE}
  140. {$ENDIF}
  141. // general options
  142. {$EXTENDEDSYNTAX ON}
  143. {$LONGSTRINGS ON}
  144. {$ALIGN ON}
  145. {$IFNDEF FPC}
  146. {$OPTIMIZATION ON}
  147. {$ENDIF}
  148. interface
  149. uses
  150. {$IFDEF OPENGL_ES} dglOpenGLES,
  151. {$ELSE} dglOpenGL, {$ENDIF}
  152. {$IF DEFINED(GLB_WIN) AND
  153. DEFINED(GLB_DELPHI)} windows, {$IFEND}
  154. {$IFDEF GLB_SDL} SDL, {$ENDIF}
  155. {$IFDEF GLB_LAZARUS} IntfGraphics, GraphType, Graphics, {$ENDIF}
  156. {$IFDEF GLB_DELPHI} Dialogs, Graphics, Types, {$ENDIF}
  157. {$IFDEF GLB_SDL_IMAGE} SDL_image, {$ENDIF}
  158. {$IFDEF GLB_PNGIMAGE} pngimage, {$ENDIF}
  159. {$IFDEF GLB_LIB_PNG} libPNG, {$ENDIF}
  160. {$IFDEF GLB_DELPHI_JPEG} JPEG, {$ENDIF}
  161. {$IFDEF GLB_LIB_JPEG} libJPEG, {$ENDIF}
  162. Classes, SysUtils;
  163. type
  164. {$IFNDEF fpc}
  165. QWord = System.UInt64;
  166. PQWord = ^QWord;
  167. PtrInt = Longint;
  168. PtrUInt = DWord;
  169. {$ENDIF}
  170. { type that describes the format of the data stored in a texture.
  171. the name of formats is composed of the following constituents:
  172. - multiple channels:
  173. - channel (e.g. R, G, B, A or Alpha, Luminance or X (reserved))
  174. - width of the chanel in bit (4, 8, 16, ...)
  175. - data type (e.g. ub, us, ui)
  176. - number of elements of data types }
  177. TglBitmapFormat = (
  178. tfEmpty = 0,
  179. tfAlpha4ub1, //< 1 x unsigned byte
  180. tfAlpha8ub1, //< 1 x unsigned byte
  181. tfAlpha16us1, //< 1 x unsigned short
  182. tfLuminance4ub1, //< 1 x unsigned byte
  183. tfLuminance8ub1, //< 1 x unsigned byte
  184. tfLuminance16us1, //< 1 x unsigned short
  185. tfLuminance4Alpha4ub2, //< 1 x unsigned byte (lum), 1 x unsigned byte (alpha)
  186. tfLuminance6Alpha2ub2, //< 1 x unsigned byte (lum), 1 x unsigned byte (alpha)
  187. tfLuminance8Alpha8ub2, //< 1 x unsigned byte (lum), 1 x unsigned byte (alpha)
  188. tfLuminance12Alpha4us2, //< 1 x unsigned short (lum), 1 x unsigned short (alpha)
  189. tfLuminance16Alpha16us2, //< 1 x unsigned short (lum), 1 x unsigned short (alpha)
  190. tfR3G3B2ub1, //< 1 x unsigned byte (3bit red, 3bit green, 2bit blue)
  191. tfRGBX4us1, //< 1 x unsigned short (4bit red, 4bit green, 4bit blue, 4bit reserverd)
  192. tfXRGB4us1, //< 1 x unsigned short (4bit reserved, 4bit red, 4bit green, 4bit blue)
  193. tfR5G6B5us1, //< 1 x unsigned short (5bit red, 6bit green, 5bit blue)
  194. tfRGB5X1us1, //< 1 x unsigned short (5bit red, 5bit green, 5bit blue, 1bit reserved)
  195. tfX1RGB5us1, //< 1 x unsigned short (1bit reserved, 5bit red, 5bit green, 5bit blue)
  196. tfRGB8ub3, //< 1 x unsigned byte (red), 1 x unsigned byte (green), 1 x unsigned byte (blue)
  197. tfRGBX8ui1, //< 1 x unsigned int (8bit red, 8bit green, 8bit blue, 8bit reserved)
  198. tfXRGB8ui1, //< 1 x unsigned int (8bit reserved, 8bit red, 8bit green, 8bit blue)
  199. tfRGB10X2ui1, //< 1 x unsigned int (10bit red, 10bit green, 10bit blue, 2bit reserved)
  200. tfX2RGB10ui1, //< 1 x unsigned int (2bit reserved, 10bit red, 10bit green, 10bit blue)
  201. tfRGB16us3, //< 1 x unsigned short (red), 1 x unsigned short (green), 1 x unsigned short (blue)
  202. tfRGBA4us1, //< 1 x unsigned short (4bit red, 4bit green, 4bit blue, 4bit alpha)
  203. tfARGB4us1, //< 1 x unsigned short (4bit alpha, 4bit red, 4bit green, 4bit blue)
  204. tfRGB5A1us1, //< 1 x unsigned short (5bit red, 5bit green, 5bit blue, 1bit alpha)
  205. tfA1RGB5us1, //< 1 x unsigned short (1bit alpha, 5bit red, 5bit green, 5bit blue)
  206. tfRGBA8ui1, //< 1 x unsigned int (8bit red, 8bit green, 8bit blue, 8 bit alpha)
  207. tfARGB8ui1, //< 1 x unsigned int (8 bit alpha, 8bit red, 8bit green, 8bit blue)
  208. tfRGBA8ub4, //< 1 x unsigned byte (red), 1 x unsigned byte (green), 1 x unsigned byte (blue), 1 x unsigned byte (alpha)
  209. tfRGB10A2ui1, //< 1 x unsigned int (10bit red, 10bit green, 10bit blue, 2bit alpha)
  210. tfA2RGB10ui1, //< 1 x unsigned int (2bit alpha, 10bit red, 10bit green, 10bit blue)
  211. tfRGBA16us4, //< 1 x unsigned short (red), 1 x unsigned short (green), 1 x unsigned short (blue), 1 x unsigned short (alpha)
  212. tfBGRX4us1, //< 1 x unsigned short (4bit blue, 4bit green, 4bit red, 4bit reserved)
  213. tfXBGR4us1, //< 1 x unsigned short (4bit reserved, 4bit blue, 4bit green, 4bit red)
  214. tfB5G6R5us1, //< 1 x unsigned short (5bit blue, 6bit green, 5bit red)
  215. tfBGR5X1us1, //< 1 x unsigned short (5bit blue, 5bit green, 5bit red, 1bit reserved)
  216. tfX1BGR5us1, //< 1 x unsigned short (1bit reserved, 5bit blue, 5bit green, 5bit red)
  217. tfBGR8ub3, //< 1 x unsigned byte (blue), 1 x unsigned byte (green), 1 x unsigned byte (red)
  218. tfBGRX8ui1, //< 1 x unsigned int (8bit blue, 8bit green, 8bit red, 8bit reserved)
  219. tfXBGR8ui1, //< 1 x unsigned int (8bit reserved, 8bit blue, 8bit green, 8bit red)
  220. tfBGR10X2ui1, //< 1 x unsigned int (10bit blue, 10bit green, 10bit red, 2bit reserved)
  221. tfX2BGR10ui1, //< 1 x unsigned int (2bit reserved, 10bit blue, 10bit green, 10bit red)
  222. tfBGR16us3, //< 1 x unsigned short (blue), 1 x unsigned short (green), 1 x unsigned short (red)
  223. tfBGRA4us1, //< 1 x unsigned short (4bit blue, 4bit green, 4bit red, 4bit alpha)
  224. tfABGR4us1, //< 1 x unsigned short (4bit alpha, 4bit blue, 4bit green, 4bit red)
  225. tfBGR5A1us1, //< 1 x unsigned short (5bit blue, 5bit green, 5bit red, 1bit alpha)
  226. tfA1BGR5us1, //< 1 x unsigned short (1bit alpha, 5bit blue, 5bit green, 5bit red)
  227. tfBGRA8ui1, //< 1 x unsigned int (8bit blue, 8bit green, 8bit red, 8bit alpha)
  228. tfABGR8ui1, //< 1 x unsigned int (8bit alpha, 8bit blue, 8bit green, 8bit red)
  229. tfBGRA8ub4, //< 1 x unsigned byte (blue), 1 x unsigned byte (green), 1 x unsigned byte (red), 1 x unsigned byte (alpha)
  230. tfBGR10A2ui1, //< 1 x unsigned int (10bit blue, 10bit green, 10bit red, 2bit alpha)
  231. tfA2BGR10ui1, //< 1 x unsigned int (2bit alpha, 10bit blue, 10bit green, 10bit red)
  232. tfBGRA16us4, //< 1 x unsigned short (blue), 1 x unsigned short (green), 1 x unsigned short (red), 1 x unsigned short (alpha)
  233. tfDepth16us1, //< 1 x unsigned short (depth)
  234. tfDepth24ui1, //< 1 x unsigned int (depth)
  235. tfDepth32ui1, //< 1 x unsigned int (depth)
  236. tfS3tcDtx1RGBA,
  237. tfS3tcDtx3RGBA,
  238. tfS3tcDtx5RGBA
  239. );
  240. { type to define suitable file formats }
  241. TglBitmapFileType = (
  242. {$IFDEF GLB_SUPPORT_PNG_WRITE} ftPNG, {$ENDIF} //< Portable Network Graphic file (PNG)
  243. {$IFDEF GLB_SUPPORT_JPEG_WRITE}ftJPEG, {$ENDIF} //< JPEG file
  244. ftDDS, //< Direct Draw Surface file (DDS)
  245. ftTGA, //< Targa Image File (TGA)
  246. ftBMP, //< Windows Bitmap File (BMP)
  247. ftRAW); //< glBitmap RAW file format
  248. TglBitmapFileTypes = set of TglBitmapFileType;
  249. { possible mipmap types }
  250. TglBitmapMipMap = (
  251. mmNone, //< no mipmaps
  252. mmMipmap, //< normal mipmaps
  253. mmMipmapGlu); //< mipmaps generated with glu functions
  254. { possible normal map functions }
  255. TglBitmapNormalMapFunc = (
  256. nm4Samples,
  257. nmSobel,
  258. nm3x3,
  259. nm5x5);
  260. ////////////////////////////////////////////////////////////////////////////////////////////////////
  261. EglBitmap = class(Exception); //< glBitmap exception
  262. EglBitmapNotSupported = class(Exception); //< exception for not supported functions
  263. EglBitmapSizeToLarge = class(EglBitmap); //< exception for to large textures
  264. EglBitmapNonPowerOfTwo = class(EglBitmap); //< exception for non power of two textures
  265. EglBitmapUnsupportedFormat = class(EglBitmap) //< exception for unsupporetd formats
  266. public
  267. constructor Create(const aFormat: TglBitmapFormat); overload;
  268. constructor Create(const aMsg: String; const aFormat: TglBitmapFormat); overload;
  269. end;
  270. ////////////////////////////////////////////////////////////////////////////////////////////////////
  271. { record that stores 4 unsigned integer values }
  272. TglBitmapRec4ui = packed record
  273. case Integer of
  274. 0: (r, g, b, a: Cardinal);
  275. 1: (arr: array[0..3] of Cardinal);
  276. end;
  277. { record that stores 4 unsigned byte values }
  278. TglBitmapRec4ub = packed record
  279. case Integer of
  280. 0: (r, g, b, a: Byte);
  281. 1: (arr: array[0..3] of Byte);
  282. end;
  283. { record that stores 4 unsigned long integer values }
  284. TglBitmapRec4ul = packed record
  285. case Integer of
  286. 0: (r, g, b, a: QWord);
  287. 1: (arr: array[0..3] of QWord);
  288. end;
  289. { structure to store pixel data in }
  290. TglBitmapPixelData = packed record
  291. Data: TglBitmapRec4ui; //< color data for each color channel
  292. Range: TglBitmapRec4ui; //< maximal color value for each channel
  293. Format: TglBitmapFormat; //< format of the pixel
  294. end;
  295. PglBitmapPixelData = ^TglBitmapPixelData;
  296. TglBitmapSizeFields = set of (ffX, ffY);
  297. TglBitmapSize = packed record
  298. Fields: TglBitmapSizeFields;
  299. X: Word;
  300. Y: Word;
  301. end;
  302. TglBitmapPixelPosition = TglBitmapSize;
  303. { describes the properties of a given texture data format }
  304. TglBitmapFormatDescriptor = class(TObject)
  305. private
  306. // cached properties
  307. fBytesPerPixel: Single; //< number of bytes for each pixel
  308. fChannelCount: Integer; //< number of color channels
  309. fMask: TglBitmapRec4ul; //< bitmask for each color channel
  310. fRange: TglBitmapRec4ui; //< maximal value of each color channel
  311. { @return @true if the format has a red color channel, @false otherwise }
  312. function GetHasRed: Boolean;
  313. { @return @true if the format has a green color channel, @false otherwise }
  314. function GetHasGreen: Boolean;
  315. { @return @true if the format has a blue color channel, @false otherwise }
  316. function GetHasBlue: Boolean;
  317. { @return @true if the format has a alpha color channel, @false otherwise }
  318. function GetHasAlpha: Boolean;
  319. { @return @true if the format has any color color channel, @false otherwise }
  320. function GetHasColor: Boolean;
  321. { @return @true if the format is a grayscale format, @false otherwise }
  322. function GetIsGrayscale: Boolean;
  323. { @return @true if the format is supported by OpenGL, @false otherwise }
  324. function GetHasOpenGLSupport: Boolean;
  325. protected
  326. fFormat: TglBitmapFormat; //< format this descriptor belongs to
  327. fWithAlpha: TglBitmapFormat; //< suitable format with alpha channel
  328. fWithoutAlpha: TglBitmapFormat; //< suitable format without alpha channel
  329. fOpenGLFormat: TglBitmapFormat; //< suitable format that is supported by OpenGL
  330. fRGBInverted: TglBitmapFormat; //< suitable format with inverted RGB channels
  331. fUncompressed: TglBitmapFormat; //< suitable format with uncompressed data
  332. fBitsPerPixel: Integer; //< number of bits per pixel
  333. fIsCompressed: Boolean; //< @true if the format is compressed, @false otherwise
  334. fPrecision: TglBitmapRec4ub; //< number of bits for each color channel
  335. fShift: TglBitmapRec4ub; //< bit offset for each color channel
  336. fglFormat: GLenum; //< OpenGL format enum (e.g. GL_RGB)
  337. fglInternalFormat: GLenum; //< OpenGL internal format enum (e.g. GL_RGB8)
  338. fglDataFormat: GLenum; //< OpenGL data format enum (e.g. GL_UNSIGNED_BYTE)
  339. { set values for this format descriptor }
  340. procedure SetValues; virtual;
  341. { calculate cached values }
  342. procedure CalcValues;
  343. public
  344. property Format: TglBitmapFormat read fFormat; //< format this descriptor belongs to
  345. property ChannelCount: Integer read fChannelCount; //< number of color channels
  346. property IsCompressed: Boolean read fIsCompressed; //< @true if the format is compressed, @false otherwise
  347. property BitsPerPixel: Integer read fBitsPerPixel; //< number of bytes per pixel
  348. property BytesPerPixel: Single read fBytesPerPixel; //< number of bits per pixel
  349. property Precision: TglBitmapRec4ub read fPrecision; //< number of bits for each color channel
  350. property Shift: TglBitmapRec4ub read fShift; //< bit offset for each color channel
  351. property Range: TglBitmapRec4ui read fRange; //< maximal value of each color channel
  352. property Mask: TglBitmapRec4ul read fMask; //< bitmask for each color channel
  353. property RGBInverted: TglBitmapFormat read fRGBInverted; //< suitable format with inverted RGB channels
  354. property WithAlpha: TglBitmapFormat read fWithAlpha; //< suitable format with alpha channel
  355. property WithoutAlpha: TglBitmapFormat read fWithAlpha; //< suitable format without alpha channel
  356. property OpenGLFormat: TglBitmapFormat read fOpenGLFormat; //< suitable format that is supported by OpenGL
  357. property Uncompressed: TglBitmapFormat read fUncompressed; //< suitable format with uncompressed data
  358. property glFormat: GLenum read fglFormat; //< OpenGL format enum (e.g. GL_RGB)
  359. property glInternalFormat: GLenum read fglInternalFormat; //< OpenGL internal format enum (e.g. GL_RGB8)
  360. property glDataFormat: GLenum read fglDataFormat; //< OpenGL data format enum (e.g. GL_UNSIGNED_BYTE)
  361. property HasRed: Boolean read GetHasRed; //< @true if the format has a red color channel, @false otherwise
  362. property HasGreen: Boolean read GetHasGreen; //< @true if the format has a green color channel, @false otherwise
  363. property HasBlue: Boolean read GetHasBlue; //< @true if the format has a blue color channel, @false otherwise
  364. property HasAlpha: Boolean read GetHasAlpha; //< @true if the format has a alpha color channel, @false otherwise
  365. property HasColor: Boolean read GetHasColor; //< @true if the format has any color color channel, @false otherwise
  366. property IsGrayscale: Boolean read GetIsGrayscale; //< @true if the format is a grayscale format, @false otherwise
  367. property HasOpenGLSupport: Boolean read GetHasOpenGLSupport; //< @true if the format is supported by OpenGL, @false otherwise
  368. function GetSize(const aSize: TglBitmapSize): Integer; overload; virtual;
  369. function GetSize(const aWidth, aHeight: Integer): Integer; overload; virtual;
  370. { constructor }
  371. constructor Create;
  372. public
  373. { get the format descriptor by a given OpenGL internal format
  374. @param aInternalFormat OpenGL internal format to get format descriptor for
  375. @returns suitable format descriptor or tfEmpty-Descriptor }
  376. class function GetByFormat(const aInternalFormat: GLenum): TglBitmapFormatDescriptor;
  377. end;
  378. ////////////////////////////////////////////////////////////////////////////////////////////////////
  379. TglBitmapData = class;
  380. { structure to store data for converting in }
  381. TglBitmapFunctionRec = record
  382. Sender: TglBitmapData; //< texture object that stores the data to convert
  383. Size: TglBitmapSize; //< size of the texture
  384. Position: TglBitmapPixelPosition; //< position of the currently pixel
  385. Source: TglBitmapPixelData; //< pixel data of the current pixel
  386. Dest: TglBitmapPixelData; //< new data of the pixel (must be filled in)
  387. Args: Pointer; //< user defined args that was passed to the convert function
  388. end;
  389. { callback to use for converting texture data }
  390. TglBitmapFunction = procedure(var FuncRec: TglBitmapFunctionRec);
  391. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  392. { class to store texture data in. used to load, save and
  393. manipulate data before assigned to texture object
  394. all operations on a data object can be done from a background thread }
  395. TglBitmapData = class
  396. private { fields }
  397. fData: PByte; //< texture data
  398. fDimension: TglBitmapSize; //< pixel size of the data
  399. fFormat: TglBitmapFormat; //< format the texture data is stored in
  400. fFilename: String; //< file the data was load from
  401. fScanlines: array of PByte; //< pointer to begin of each line
  402. fHasScanlines: Boolean; //< @true if scanlines are initialized, @false otherwise
  403. private { getter / setter }
  404. { @returns the format descriptor suitable to the texture data format }
  405. function GetFormatDescriptor: TglBitmapFormatDescriptor;
  406. { @returns the width of the texture data (in pixel) or -1 if no data is set }
  407. function GetWidth: Integer;
  408. { @returns the height of the texture data (in pixel) or -1 if no data is set }
  409. function GetHeight: Integer;
  410. { get scanline at index aIndex
  411. @returns Pointer to start of line or @nil }
  412. function GetScanlines(const aIndex: Integer): PByte;
  413. { set new value for the data format. only possible if new format has the same pixel size.
  414. if you want to convert the texture data, see ConvertTo function }
  415. procedure SetFormat(const aValue: TglBitmapFormat);
  416. private { internal misc }
  417. { splits a resource identifier into the resource and it's type
  418. @param aResource resource identifier to split and store name in
  419. @param aResType type of the resource }
  420. procedure PrepareResType(var aResource: String; var aResType: PChar);
  421. { updates scanlines array }
  422. procedure UpdateScanlines;
  423. private { internal load and save }
  424. {$IFDEF GLB_SUPPORT_PNG_READ}
  425. { try to load a PNG from a stream
  426. @param aStream stream to load PNG from
  427. @returns @true on success, @false otherwise }
  428. function LoadPNG(const aStream: TStream): Boolean; virtual;
  429. {$ENDIF}
  430. {$ifdef GLB_SUPPORT_PNG_WRITE}
  431. { save texture data as PNG to stream
  432. @param aStream stream to save data to}
  433. procedure SavePNG(const aStream: TStream); virtual;
  434. {$ENDIF}
  435. {$IFDEF GLB_SUPPORT_JPEG_READ}
  436. { try to load a JPEG from a stream
  437. @param aStream stream to load JPEG from
  438. @returns @true on success, @false otherwise }
  439. function LoadJPEG(const aStream: TStream): Boolean; virtual;
  440. {$ENDIF}
  441. {$IFDEF GLB_SUPPORT_JPEG_WRITE}
  442. { save texture data as JPEG to stream
  443. @param aStream stream to save data to}
  444. procedure SaveJPEG(const aStream: TStream); virtual;
  445. {$ENDIF}
  446. { try to load a RAW image from a stream
  447. @param aStream stream to load RAW image from
  448. @returns @true on success, @false otherwise }
  449. function LoadRAW(const aStream: TStream): Boolean;
  450. { save texture data as RAW image to stream
  451. @param aStream stream to save data to}
  452. procedure SaveRAW(const aStream: TStream);
  453. { try to load a BMP from a stream
  454. @param aStream stream to load BMP from
  455. @returns @true on success, @false otherwise }
  456. function LoadBMP(const aStream: TStream): Boolean;
  457. { save texture data as BMP to stream
  458. @param aStream stream to save data to}
  459. procedure SaveBMP(const aStream: TStream);
  460. { try to load a TGA from a stream
  461. @param aStream stream to load TGA from
  462. @returns @true on success, @false otherwise }
  463. function LoadTGA(const aStream: TStream): Boolean;
  464. { save texture data as TGA to stream
  465. @param aStream stream to save data to}
  466. procedure SaveTGA(const aStream: TStream);
  467. { try to load a DDS from a stream
  468. @param aStream stream to load DDS from
  469. @returns @true on success, @false otherwise }
  470. function LoadDDS(const aStream: TStream): Boolean;
  471. { save texture data as DDS to stream
  472. @param aStream stream to save data to}
  473. procedure SaveDDS(const aStream: TStream);
  474. public { properties }
  475. property Data: PByte read fData; //< texture data (be carefull with this!)
  476. property Dimension: TglBitmapSize read fDimension; //< size of the texture data (in pixel)
  477. property Filename: String read fFilename; //< file the data was loaded from
  478. property Width: Integer read GetWidth; //< width of the texture data (in pixel)
  479. property Height: Integer read GetHeight; //< height of the texture data (in pixel)
  480. property Format: TglBitmapFormat read fFormat write SetFormat; //< format the texture data is stored in
  481. property Scanlines[const aIndex: Integer]: PByte read GetScanlines; //< pointer to begin of line at given index or @nil
  482. property FormatDescriptor: TglBitmapFormatDescriptor read GetFormatDescriptor; //< descriptor object that describes the format of the stored data
  483. public { flip }
  484. { flip texture horizontal
  485. @returns @true in success, @false otherwise }
  486. function FlipHorz: Boolean; virtual;
  487. { flip texture vertical
  488. @returns @true in success, @false otherwise }
  489. function FlipVert: Boolean; virtual;
  490. public { load }
  491. { load a texture from a file
  492. @param aFilename file to load texuture from }
  493. procedure LoadFromFile(const aFilename: String);
  494. { load a texture from a stream
  495. @param aStream stream to load texture from }
  496. procedure LoadFromStream(const aStream: TStream); virtual;
  497. { use a function to generate texture data
  498. @param aSize size of the texture
  499. @param aFormat format of the texture data
  500. @param aFunc callback to use for generation
  501. @param aArgs user defined paramaters (use at will) }
  502. procedure LoadFromFunc(const aSize: TglBitmapSize; const aFormat: TglBitmapFormat; const aFunc: TglBitmapFunction; const aArgs: Pointer = nil);
  503. { load a texture from a resource
  504. @param aInstance resource handle
  505. @param aResource resource indentifier
  506. @param aResType resource type (if known) }
  507. procedure LoadFromResource(const aInstance: Cardinal; aResource: String; aResType: PChar = nil);
  508. { load a texture from a resource id
  509. @param aInstance resource handle
  510. @param aResource resource ID
  511. @param aResType resource type }
  512. procedure LoadFromResourceID(const aInstance: Cardinal; const aResourceID: Integer; const aResType: PChar);
  513. public { save }
  514. { save texture data to a file
  515. @param aFilename filename to store texture in
  516. @param aFileType file type to store data into }
  517. procedure SaveToFile(const aFilename: String; const aFileType: TglBitmapFileType);
  518. { save texture data to a stream
  519. @param aFilename filename to store texture in
  520. @param aFileType file type to store data into }
  521. procedure SaveToStream(const aStream: TStream; const aFileType: TglBitmapFileType); virtual;
  522. public { convert }
  523. { convert texture data using a user defined callback
  524. @param aFunc callback to use for converting
  525. @param aCreateTemp create a temporary buffer to use for converting
  526. @param aArgs user defined paramters (use at will)
  527. @returns @true if converting was successful, @false otherwise }
  528. function Convert(const aFunc: TglBitmapFunction; const aCreateTemp: Boolean; const aArgs: Pointer = nil): Boolean; overload;
  529. { convert texture data using a user defined callback
  530. @param aSource glBitmap to read data from
  531. @param aFunc callback to use for converting
  532. @param aCreateTemp create a temporary buffer to use for converting
  533. @param aFormat format of the new data
  534. @param aArgs user defined paramters (use at will)
  535. @returns @true if converting was successful, @false otherwise }
  536. function Convert(const aSource: TglBitmapData; const aFunc: TglBitmapFunction; aCreateTemp: Boolean;
  537. const aFormat: TglBitmapFormat; const aArgs: Pointer = nil): Boolean; overload;
  538. { convert texture data using a specific format
  539. @param aFormat new format of texture data
  540. @returns @true if converting was successful, @false otherwise }
  541. function ConvertTo(const aFormat: TglBitmapFormat): Boolean; virtual;
  542. {$IFDEF GLB_SDL}
  543. public { SDL }
  544. { assign texture data to SDL surface
  545. @param aSurface SDL surface to write data to
  546. @returns @true on success, @false otherwise }
  547. function AssignToSurface(out aSurface: PSDL_Surface): Boolean;
  548. { assign texture data from SDL surface
  549. @param aSurface SDL surface to read data from
  550. @returns @true on success, @false otherwise }
  551. function AssignFromSurface(const aSurface: PSDL_Surface): Boolean;
  552. { assign alpha channel data to SDL surface
  553. @param aSurface SDL surface to write alpha channel data to
  554. @returns @true on success, @false otherwise }
  555. function AssignAlphaToSurface(out aSurface: PSDL_Surface): Boolean;
  556. { assign alpha channel data from SDL surface
  557. @param aSurface SDL surface to read data from
  558. @param aFunc callback to use for converting
  559. @param aArgs user defined parameters (use at will)
  560. @returns @true on success, @false otherwise }
  561. function AddAlphaFromSurface(const aSurface: PSDL_Surface; const aFunc: TglBitmapFunction = nil; const aArgs: Pointer = nil): Boolean;
  562. {$ENDIF}
  563. {$IFDEF GLB_DELPHI}
  564. public { Delphi }
  565. { assign texture data to TBitmap object
  566. @param aBitmap TBitmap to write data to
  567. @returns @true on success, @false otherwise }
  568. function AssignToBitmap(const aBitmap: TBitmap): Boolean;
  569. { assign texture data from TBitmap object
  570. @param aBitmap TBitmap to read data from
  571. @returns @true on success, @false otherwise }
  572. function AssignFromBitmap(const aBitmap: TBitmap): Boolean;
  573. { assign alpha channel data to TBitmap object
  574. @param aBitmap TBitmap to write data to
  575. @returns @true on success, @false otherwise }
  576. function AssignAlphaToBitmap(const aBitmap: TBitmap): Boolean;
  577. { assign alpha channel data from TBitmap object
  578. @param aBitmap TBitmap to read data from
  579. @param aFunc callback to use for converting
  580. @param aArgs user defined parameters (use at will)
  581. @returns @true on success, @false otherwise }
  582. function AddAlphaFromBitmap(const aBitmap: TBitmap; const aFunc: TglBitmapFunction = nil; const aArgs: Pointer = nil): Boolean;
  583. {$ENDIF}
  584. {$IFDEF GLB_LAZARUS}
  585. public { Lazarus }
  586. { assign texture data to TLazIntfImage object
  587. @param aImage TLazIntfImage to write data to
  588. @returns @true on success, @false otherwise }
  589. function AssignToLazIntfImage(const aImage: TLazIntfImage): Boolean;
  590. { assign texture data from TLazIntfImage object
  591. @param aImage TLazIntfImage to read data from
  592. @returns @true on success, @false otherwise }
  593. function AssignFromLazIntfImage(const aImage: TLazIntfImage): Boolean;
  594. { assign alpha channel data to TLazIntfImage object
  595. @param aImage TLazIntfImage to write data to
  596. @returns @true on success, @false otherwise }
  597. function AssignAlphaToLazIntfImage(const aImage: TLazIntfImage): Boolean;
  598. { assign alpha channel data from TLazIntfImage object
  599. @param aImage TLazIntfImage to read data from
  600. @param aFunc callback to use for converting
  601. @param aArgs user defined parameters (use at will)
  602. @returns @true on success, @false otherwise }
  603. function AddAlphaFromLazIntfImage(const aImage: TLazIntfImage; const aFunc: TglBitmapFunction = nil; const aArgs: Pointer = nil): Boolean;
  604. {$ENDIF}
  605. public { Alpha }
  606. { load alpha channel data from resource
  607. @param aInstance resource handle
  608. @param aResource resource ID
  609. @param aResType resource type
  610. @param aFunc callback to use for converting
  611. @param aArgs user defined parameters (use at will)
  612. @returns @true on success, @false otherwise }
  613. function AddAlphaFromResource(const aInstance: Cardinal; aResource: String; aResType: PChar = nil; const aFunc: TglBitmapFunction = nil; const aArgs: Pointer = nil): Boolean;
  614. { load alpha channel data from resource ID
  615. @param aInstance resource handle
  616. @param aResourceID resource ID
  617. @param aResType resource type
  618. @param aFunc callback to use for converting
  619. @param aArgs user defined parameters (use at will)
  620. @returns @true on success, @false otherwise }
  621. function AddAlphaFromResourceID(const aInstance: Cardinal; const aResourceID: Integer; const aResType: PChar; const aFunc: TglBitmapFunction = nil; const aArgs: Pointer = nil): Boolean;
  622. { add alpha channel data from function
  623. @param aFunc callback to get data from
  624. @param aArgs user defined parameters (use at will)
  625. @returns @true on success, @false otherwise }
  626. function AddAlphaFromFunc(const aFunc: TglBitmapFunction; const aArgs: Pointer = nil): Boolean; virtual;
  627. { add alpha channel data from file (macro for: new glBitmap, LoadFromFile, AddAlphaFromGlBitmap)
  628. @param aFilename file to load alpha channel data from
  629. @param aFunc callback to use for converting
  630. @param aArgs SetFormat user defined parameters (use at will)
  631. @returns @true on success, @false otherwise }
  632. function AddAlphaFromFile(const aFileName: String; const aFunc: TglBitmapFunction = nil; const aArgs: Pointer = nil): Boolean;
  633. { add alpha channel data from stream (macro for: new glBitmap, LoadFromStream, AddAlphaFromGlBitmap)
  634. @param aStream stream to load alpha channel data from
  635. @param aFunc callback to use for converting
  636. @param aArgs user defined parameters (use at will)
  637. @returns @true on success, @false otherwise }
  638. function AddAlphaFromStream(const aStream: TStream; const aFunc: TglBitmapFunction = nil; const aArgs: Pointer = nil): Boolean;
  639. { add alpha channel data from existing glBitmap object
  640. @param aBitmap TglBitmap to copy alpha channel data from
  641. @param aFunc callback to use for converting
  642. @param aArgs user defined parameters (use at will)
  643. @returns @true on success, @false otherwise }
  644. function AddAlphaFromDataObj(const aDataObj: TglBitmapData; aFunc: TglBitmapFunction; const aArgs: Pointer): Boolean;
  645. { add alpha to pixel if the pixels color is greter than the given color value
  646. @param aRed red threshold (0-255)
  647. @param aGreen green threshold (0-255)
  648. @param aBlue blue threshold (0-255)
  649. @param aDeviatation accepted deviatation (0-255)
  650. @returns @true on success, @false otherwise }
  651. function AddAlphaFromColorKey(const aRed, aGreen, aBlue: Byte; const aDeviation: Byte = 0): Boolean;
  652. { add alpha to pixel if the pixels color is greter than the given color value
  653. @param aRed red threshold (0-Range.r)
  654. @param aGreen green threshold (0-Range.g)
  655. @param aBlue blue threshold (0-Range.b)
  656. @param aDeviatation accepted deviatation (0-max(Range.rgb))
  657. @returns @true on success, @false otherwise }
  658. function AddAlphaFromColorKeyRange(const aRed, aGreen, aBlue: Cardinal; const aDeviation: Cardinal = 0): Boolean;
  659. { add alpha to pixel if the pixels color is greter than the given color value
  660. @param aRed red threshold (0.0-1.0)
  661. @param aGreen green threshold (0.0-1.0)
  662. @param aBlue blue threshold (0.0-1.0)
  663. @param aDeviatation accepted deviatation (0.0-1.0)
  664. @returns @true on success, @false otherwise }
  665. function AddAlphaFromColorKeyFloat(const aRed, aGreen, aBlue: Single; const aDeviation: Single = 0): Boolean;
  666. { add a constand alpha value to all pixels
  667. @param aAlpha alpha value to add (0-255)
  668. @returns @true on success, @false otherwise }
  669. function AddAlphaFromValue(const aAlpha: Byte): Boolean;
  670. { add a constand alpha value to all pixels
  671. @param aAlpha alpha value to add (0-max(Range.rgb))
  672. @returns @true on success, @false otherwise }
  673. function AddAlphaFromValueRange(const aAlpha: Cardinal): Boolean;
  674. { add a constand alpha value to all pixels
  675. @param aAlpha alpha value to add (0.0-1.0)
  676. @returns @true on success, @false otherwise }
  677. function AddAlphaFromValueFloat(const aAlpha: Single): Boolean;
  678. { remove alpha channel
  679. @returns @true on success, @false otherwise }
  680. function RemoveAlpha: Boolean; virtual;
  681. public { fill }
  682. { fill complete texture with one color
  683. @param aRed red color for border (0-255)
  684. @param aGreen green color for border (0-255)
  685. @param aBlue blue color for border (0-255)
  686. @param aAlpha alpha color for border (0-255) }
  687. procedure FillWithColor(const aRed, aGreen, aBlue: Byte; const aAlpha: Byte = 255);
  688. { fill complete texture with one color
  689. @param aRed red color for border (0-Range.r)
  690. @param aGreen green color for border (0-Range.g)
  691. @param aBlue blue color for border (0-Range.b)
  692. @param aAlpha alpha color for border (0-Range.a) }
  693. procedure FillWithColorRange(const aRed, aGreen, aBlue: Cardinal; const aAlpha: Cardinal = $FFFFFFFF);
  694. { fill complete texture with one color
  695. @param aRed red color for border (0.0-1.0)
  696. @param aGreen green color for border (0.0-1.0)
  697. @param aBlue blue color for border (0.0-1.0)
  698. @param aAlpha alpha color for border (0.0-1.0) }
  699. procedure FillWithColorFloat(const aRed, aGreen, aBlue: Single; const aAlpha: Single = 1.0);
  700. public { Misc }
  701. { set data pointer of texture data
  702. @param aData pointer to new texture data
  703. @param aFormat format of the data stored at aData
  704. @param aWidth width of the texture data
  705. @param aHeight height of the texture data }
  706. procedure SetData(const aData: PByte; const aFormat: TglBitmapFormat;
  707. const aWidth: Integer = -1; const aHeight: Integer = -1); virtual;
  708. { create a clone of the current object
  709. @returns clone of this object}
  710. function Clone: TglBitmapData;
  711. { invert color data (bitwise not)
  712. @param aRed invert red channel
  713. @param aGreen invert green channel
  714. @param aBlue invert blue channel
  715. @param aAlpha invert alpha channel }
  716. procedure Invert(const aRed, aGreen, aBlue, aAlpha: Boolean);
  717. { create normal map from texture data
  718. @param aFunc normal map function to generate normalmap with
  719. @param aScale scale of the normale stored in the normal map
  720. @param aUseAlpha generate normalmap from alpha channel data (if present) }
  721. procedure GenerateNormalMap(const aFunc: TglBitmapNormalMapFunc = nm3x3;
  722. const aScale: Single = 2; const aUseAlpha: Boolean = false);
  723. public { constructor }
  724. { constructor - creates a texutre data object }
  725. constructor Create; overload;
  726. { constructor - creates a texture data object and loads it from a file
  727. @param aFilename file to load texture from }
  728. constructor Create(const aFileName: String); overload;
  729. { constructor - creates a texture data object and loads it from a stream
  730. @param aStream stream to load texture from }
  731. constructor Create(const aStream: TStream); overload;
  732. { constructor - creates a texture data object with the given size, format and data
  733. @param aSize size of the texture
  734. @param aFormat format of the given data
  735. @param aData texture data - be carefull: the data will now be managed by the texture data object }
  736. constructor Create(const aSize: TglBitmapSize; const aFormat: TglBitmapFormat; aData: PByte = nil); overload;
  737. { constructor - creates a texture data object with the given size and format and uses the given callback to create the data
  738. @param aSize size of the texture
  739. @param aFormat format of the given data
  740. @param aFunc callback to use for generating the data
  741. @param aArgs user defined parameters (use at will) }
  742. constructor Create(const aSize: TglBitmapSize; const aFormat: TglBitmapFormat; const aFunc: TglBitmapFunction; const aArgs: Pointer = nil); overload;
  743. { constructor - creates a texture data object and loads it from a resource
  744. @param aInstance resource handle
  745. @param aResource resource indentifier
  746. @param aResType resource type (if known) }
  747. constructor Create(const aInstance: Cardinal; const aResource: String; const aResType: PChar = nil); overload;
  748. { constructor - creates a texture data object and loads it from a resource
  749. @param aInstance resource handle
  750. @param aResourceID resource ID
  751. @param aResType resource type (if known) }
  752. constructor Create(const aInstance: Cardinal; const aResourceID: Integer; const aResType: PChar); overload;
  753. { destructor }
  754. destructor Destroy; override;
  755. end;
  756. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  757. { base class for all glBitmap classes. used to manage OpenGL texture objects
  758. all operations on a bitmap object must be done from the render thread }
  759. TglBitmap = class
  760. protected
  761. fID: GLuint; //< name of the OpenGL texture object
  762. fTarget: GLuint; //< texture target (e.g. GL_TEXTURE_2D)
  763. fDeleteTextureOnFree: Boolean; //< delete OpenGL texture object when this object is destroyed
  764. // texture properties
  765. fFilterMin: GLenum; //< min filter to apply to the texture
  766. fFilterMag: GLenum; //< mag filter to apply to the texture
  767. fWrapS: GLenum; //< texture wrapping for x axis
  768. fWrapT: GLenum; //< texture wrapping for y axis
  769. fWrapR: GLenum; //< texture wrapping for z axis
  770. fAnisotropic: Integer; //< anisotropic level
  771. fBorderColor: array[0..3] of Single; //< color of the texture border
  772. {$IF NOT DEFINED(OPENGL_ES) OR DEFINED(OPENGL_ES_3_0)}
  773. //Swizzle
  774. fSwizzle: array[0..3] of GLenum; //< color channel swizzle
  775. {$IFEND}
  776. {$IFNDEF OPENGL_ES}
  777. fIsResident: GLboolean; //< @true if OpenGL texture object has data, @false otherwise
  778. {$ENDIF}
  779. fDimension: TglBitmapSize; //< size of this texture
  780. fMipMap: TglBitmapMipMap; //< mipmap type
  781. // CustomData
  782. fCustomData: Pointer; //< user defined data
  783. fCustomName: String; //< user defined name
  784. fCustomNameW: WideString; //< user defined name
  785. protected
  786. { @returns the actual width of the texture }
  787. function GetWidth: Integer; virtual;
  788. { @returns the actual height of the texture }
  789. function GetHeight: Integer; virtual;
  790. protected
  791. { set a new value for fCustomData }
  792. procedure SetCustomData(const aValue: Pointer);
  793. { set a new value for fCustomName }
  794. procedure SetCustomName(const aValue: String);
  795. { set a new value for fCustomNameW }
  796. procedure SetCustomNameW(const aValue: WideString);
  797. { set new value for fDeleteTextureOnFree }
  798. procedure SetDeleteTextureOnFree(const aValue: Boolean);
  799. { set name of OpenGL texture object }
  800. procedure SetID(const aValue: Cardinal);
  801. { set new value for fMipMap }
  802. procedure SetMipMap(const aValue: TglBitmapMipMap);
  803. { set new value for target }
  804. procedure SetTarget(const aValue: Cardinal);
  805. { set new value for fAnisotrophic }
  806. procedure SetAnisotropic(const aValue: Integer);
  807. protected
  808. { create OpenGL texture object (delete exisiting object if exists) }
  809. procedure CreateID;
  810. { setup texture parameters }
  811. procedure SetupParameters({$IFNDEF OPENGL_ES}out aBuildWithGlu: Boolean{$ENDIF});
  812. protected
  813. property Width: Integer read GetWidth; //< the actual width of the texture
  814. property Height: Integer read GetHeight; //< the actual height of the texture
  815. public
  816. property ID: Cardinal read fID write SetID; //< name of the OpenGL texture object
  817. property Target: Cardinal read fTarget write SetTarget; //< texture target (e.g. GL_TEXTURE_2D)
  818. property DeleteTextureOnFree: Boolean read fDeleteTextureOnFree write SetDeleteTextureOnFree; //< delete texture object when this object is destroyed
  819. property MipMap: TglBitmapMipMap read fMipMap write SetMipMap; //< mipmap type
  820. property Anisotropic: Integer read fAnisotropic write SetAnisotropic; //< anisotropic level
  821. property CustomData: Pointer read fCustomData write SetCustomData; //< user defined data (use at will)
  822. property CustomName: String read fCustomName write SetCustomName; //< user defined name (use at will)
  823. property CustomNameW: WideString read fCustomNameW write SetCustomNameW; //< user defined name (as WideString; use at will)
  824. property Dimension: TglBitmapSize read fDimension; //< size of the texture
  825. {$IFNDEF OPENGL_ES}
  826. property IsResident: GLboolean read fIsResident; //< @true if OpenGL texture object has data, @false otherwise
  827. {$ENDIF}
  828. { this method is called after the constructor and sets the default values of this object }
  829. procedure AfterConstruction; override;
  830. { this method is called before the destructor and does some cleanup }
  831. procedure BeforeDestruction; override;
  832. public
  833. {$IFNDEF OPENGL_ES}
  834. { set the new value for texture border color
  835. @param aRed red color for border (0.0-1.0)
  836. @param aGreen green color for border (0.0-1.0)
  837. @param aBlue blue color for border (0.0-1.0)
  838. @param aAlpha alpha color for border (0.0-1.0) }
  839. procedure SetBorderColor(const aRed, aGreen, aBlue, aAlpha: Single);
  840. {$ENDIF}
  841. public
  842. { set new texture filer
  843. @param aMin min filter
  844. @param aMag mag filter }
  845. procedure SetFilter(const aMin, aMag: GLenum);
  846. { set new texture wrapping
  847. @param S texture wrapping for x axis
  848. @param T texture wrapping for y axis
  849. @param R texture wrapping for z axis }
  850. procedure SetWrap(
  851. const S: GLenum = GL_CLAMP_TO_EDGE;
  852. const T: GLenum = GL_CLAMP_TO_EDGE;
  853. const R: GLenum = GL_CLAMP_TO_EDGE);
  854. {$IF NOT DEFINED(OPENGL_ES) OR DEFINED(OPENGL_ES_3_0)}
  855. { set new swizzle
  856. @param r swizzle for red channel
  857. @param g swizzle for green channel
  858. @param b swizzle for blue channel
  859. @param a swizzle for alpha channel }
  860. procedure SetSwizzle(const r, g, b, a: GLenum);
  861. {$IFEND}
  862. public
  863. { bind texture
  864. @param aEnableTextureUnit enable texture unit for this texture (e.g. glEnable(GL_TEXTURE_2D)) }
  865. procedure Bind(const aEnableTextureUnit: Boolean = true); virtual;
  866. { bind texture
  867. @param aDisableTextureUnit disable texture unit for this texture (e.g. glEnable(GL_TEXTURE_2D)) }
  868. procedure Unbind(const aDisableTextureUnit: Boolean = true); virtual;
  869. { upload texture data from given data object to video card
  870. @param aData texture data object that contains the actual data
  871. @param aCheckSize check size before upload and throw exception if something is wrong }
  872. procedure UploadData(const aDataObj: TglBitmapData; const aCheckSize: Boolean = true); virtual;
  873. {$IFNDEF OPENGL_ES}
  874. { download texture data from video card and store it into given data object
  875. @returns @true when download was successfull, @false otherwise }
  876. function DownloadData(const aDataObj: TglBitmapData): Boolean; virtual;
  877. {$ENDIF}
  878. public
  879. { constructor - creates an empty texture }
  880. constructor Create; overload;
  881. { constructor - creates an texture object and uploads the given data }
  882. constructor Create(const aData: TglBitmapData); overload;
  883. end;
  884. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  885. {$IF NOT DEFINED(OPENGL_ES)}
  886. { wrapper class for 1-dimensional textures (OpenGL target = GL_TEXTURE_1D
  887. all operations on a bitmap object must be done from the render thread }
  888. TglBitmap1D = class(TglBitmap)
  889. protected
  890. { upload the texture data to video card
  891. @param aDataObj texture data object that contains the actual data
  892. @param aBuildWithGlu use glu functions to build mipmaps }
  893. procedure UploadDataIntern(const aDataObj: TglBitmapData; const aBuildWithGlu: Boolean);
  894. public
  895. property Width; //< actual with of the texture
  896. { this method is called after constructor and initializes the object }
  897. procedure AfterConstruction; override;
  898. { upload texture data from given data object to video card
  899. @param aData texture data object that contains the actual data
  900. @param aCheckSize check size before upload and throw exception if something is wrong }
  901. procedure UploadData(const aDataObj: TglBitmapData; const aCheckSize: Boolean = true); override;
  902. end;
  903. {$IFEND}
  904. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  905. { wrapper class for 2-dimensional textures (OpenGL target = GL_TEXTURE_2D)
  906. all operations on a bitmap object must be done from the render thread }
  907. TglBitmap2D = class(TglBitmap)
  908. protected
  909. { upload the texture data to video card
  910. @param aDataObj texture data object that contains the actual data
  911. @param aTarget target o upload data to (e.g. GL_TEXTURE_2D)
  912. @param aBuildWithGlu use glu functions to build mipmaps }
  913. procedure UploadDataIntern(const aDataObj: TglBitmapData; const aTarget: GLenum
  914. {$IFNDEF OPENGL_ES}; const aBuildWithGlu: Boolean{$ENDIF});
  915. public
  916. property Width; //< actual width of the texture
  917. property Height; //< actual height of the texture
  918. { this method is called after constructor and initializes the object }
  919. procedure AfterConstruction; override;
  920. { upload texture data from given data object to video card
  921. @param aData texture data object that contains the actual data
  922. @param aCheckSize check size before upload and throw exception if something is wrong }
  923. procedure UploadData(const aDataObj: TglBitmapData; const aCheckSize: Boolean = true); override;
  924. public
  925. { copy a part of the frame buffer to the texture
  926. @param aTop topmost pixel to copy
  927. @param aLeft leftmost pixel to copy
  928. @param aRight rightmost pixel to copy
  929. @param aBottom bottommost pixel to copy
  930. @param aFormat format to store data in
  931. @param aDataObj texture data object to store the data in }
  932. class procedure GrabScreen(const aTop, aLeft, aRight, aBottom: Integer; const aFormat: TglBitmapFormat; const aDataObj: TglBitmapData);
  933. end;
  934. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  935. {$IF NOT DEFINED(OPENGL_ES) OR DEFINED(OPENGL_ES_2_0)}
  936. { wrapper class for cube maps (OpenGL target = GL_TEXTURE_CUBE_MAP)
  937. all operations on a bitmap object must be done from the render thread }
  938. TglBitmapCubeMap = class(TglBitmap2D)
  939. protected
  940. {$IFNDEF OPENGL_ES}
  941. fGenMode: Integer; //< generation mode for the cube map (e.g. GL_REFLECTION_MAP)
  942. {$ENDIF}
  943. public
  944. { this method is called after constructor and initializes the object }
  945. procedure AfterConstruction; override;
  946. { upload texture data from given data object to video card
  947. @param aData texture data object that contains the actual data
  948. @param aCheckSize check size before upload and throw exception if something is wrong }
  949. procedure UploadData(const aDataObj: TglBitmapData; const aCheckSize: Boolean = true); override;
  950. { upload texture data from given data object to video card
  951. @param aData texture data object that contains the actual data
  952. @param aCubeTarget cube map target to upload data to (e.g. GL_TEXTURE_CUBE_MAP_POSITIVE_X)
  953. @param aCheckSize check size before upload and throw exception if something is wrong }
  954. procedure UploadCubeMap(const aDataObj: TglBitmapData; const aCubeTarget: Cardinal; const aCheckSize: Boolean);
  955. { bind texture
  956. @param aEnableTexCoordsGen enable cube map generator
  957. @param aEnableTextureUnit enable texture unit }
  958. procedure Bind({$IFNDEF OPENGL_ES}const aEnableTexCoordsGen: Boolean = true;{$ENDIF} const aEnableTextureUnit: Boolean = true); reintroduce; virtual;
  959. { unbind texture
  960. @param aDisableTexCoordsGen disable cube map generator
  961. @param aDisableTextureUnit disable texture unit }
  962. procedure Unbind({$IFNDEF OPENGL_ES}const aDisableTexCoordsGen: Boolean = true;{$ENDIF} const aDisableTextureUnit: Boolean = true); reintroduce; virtual;
  963. end;
  964. {$IFEND}
  965. {$IF NOT DEFINED(OPENGL_ES) OR DEFINED(OPENGL_ES_2_0)}
  966. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  967. { wrapper class for cube normal maps
  968. all operations on a bitmap object must be done from the render thread }
  969. TglBitmapNormalMap = class(TglBitmapCubeMap)
  970. public
  971. { this method is called after constructor and initializes the object }
  972. procedure AfterConstruction; override;
  973. { create cube normal map from texture data and upload it to video card
  974. @param aSize size of each cube map texture
  975. @param aCheckSize check size before upload and throw exception if something is wrong }
  976. procedure GenerateNormalMap(const aSize: Integer = 32; const aCheckSize: Boolean = true);
  977. end;
  978. {$IFEND}
  979. TglcBitmapFormat = TglBitmapFormat;
  980. TglcBitmap2D = TglBitmap2D;
  981. {$IF NOT DEFINED(OPENGL_ES)}
  982. TglcBitmap1D = TglBitmap1D;
  983. TglcBitmapCubeMap = TglBitmapCubeMap;
  984. TglcBitmapNormalMap = TglBitmapNormalMap;
  985. {$ELSEIF DEFINED(OPENGL_ES_2_0)}
  986. TglcBitmapCubeMap = TglBitmapCubeMap;
  987. TglcBitmapNormalMap = TglBitmapNormalMap;
  988. {$IFEND}
  989. const
  990. NULL_SIZE: TglBitmapSize = (Fields: []; X: 0; Y: 0);
  991. procedure glBitmapSetDefaultDeleteTextureOnFree(const aDeleteTextureOnFree: Boolean);
  992. procedure glBitmapSetDefaultFreeDataAfterGenTexture(const aFreeData: Boolean);
  993. procedure glBitmapSetDefaultMipmap(const aValue: TglBitmapMipMap);
  994. procedure glBitmapSetDefaultFormat(const aFormat: TglBitmapFormat);
  995. procedure glBitmapSetDefaultFilter(const aMin, aMag: Integer);
  996. procedure glBitmapSetDefaultWrap(
  997. const S: Cardinal = GL_CLAMP_TO_EDGE;
  998. const T: Cardinal = GL_CLAMP_TO_EDGE;
  999. const R: Cardinal = GL_CLAMP_TO_EDGE);
  1000. {$IF NOT DEFINED(OPENGL_ES) OR DEFINED(OPENGL_ES_3_0)}
  1001. procedure glBitmapSetDefaultSwizzle(const r: GLenum = GL_RED; g: GLenum = GL_GREEN; b: GLenum = GL_BLUE; a: GLenum = GL_ALPHA);
  1002. {$IFEND}
  1003. function glBitmapGetDefaultDeleteTextureOnFree: Boolean;
  1004. function glBitmapGetDefaultFreeDataAfterGenTexture: Boolean;
  1005. function glBitmapGetDefaultMipmap: TglBitmapMipMap;
  1006. function glBitmapGetDefaultFormat: TglBitmapFormat;
  1007. procedure glBitmapGetDefaultFilter(var aMin, aMag: Cardinal);
  1008. procedure glBitmapGetDefaultTextureWrap(var S, T, R: Cardinal);
  1009. {$IF NOT DEFINED(OPENGL_ES) OR DEFINED(OPENGL_ES_3_0)}
  1010. procedure glBitmapGetDefaultSwizzle(var r, g, b, a: GLenum);
  1011. {$IFEND}
  1012. function glBitmapSize(X: Integer = -1; Y: Integer = -1): TglBitmapSize;
  1013. function glBitmapPosition(X: Integer = -1; Y: Integer = -1): TglBitmapPixelPosition;
  1014. function glBitmapRec4ub(const r, g, b, a: Byte): TglBitmapRec4ub;
  1015. function glBitmapRec4ui(const r, g, b, a: Cardinal): TglBitmapRec4ui;
  1016. function glBitmapRec4ul(const r, g, b, a: QWord): TglBitmapRec4ul;
  1017. function glBitmapRec4ubCompare(const r1, r2: TglBitmapRec4ub): Boolean;
  1018. function glBitmapRec4uiCompare(const r1, r2: TglBitmapRec4ui): Boolean;
  1019. function glBitmapCreateTestData(const aFormat: TglBitmapFormat): TglBitmapData;
  1020. {$IFDEF GLB_DELPHI}
  1021. function CreateGrayPalette: HPALETTE;
  1022. {$ENDIF}
  1023. implementation
  1024. uses
  1025. Math, syncobjs, typinfo
  1026. {$IF DEFINED(GLB_SUPPORT_JPEG_READ) AND DEFINED(GLB_LAZ_JPEG)}, FPReadJPEG{$IFEND};
  1027. var
  1028. glBitmapDefaultDeleteTextureOnFree: Boolean;
  1029. glBitmapDefaultFreeDataAfterGenTextures: Boolean;
  1030. glBitmapDefaultFormat: TglBitmapFormat;
  1031. glBitmapDefaultMipmap: TglBitmapMipMap;
  1032. glBitmapDefaultFilterMin: Cardinal;
  1033. glBitmapDefaultFilterMag: Cardinal;
  1034. glBitmapDefaultWrapS: Cardinal;
  1035. glBitmapDefaultWrapT: Cardinal;
  1036. glBitmapDefaultWrapR: Cardinal;
  1037. glDefaultSwizzle: array[0..3] of GLenum;
  1038. ////////////////////////////////////////////////////////////////////////////////////////////////////
  1039. type
  1040. TFormatDescriptor = class(TglBitmapFormatDescriptor)
  1041. public
  1042. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); virtual; abstract;
  1043. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); virtual; abstract;
  1044. function CreateMappingData: Pointer; virtual;
  1045. procedure FreeMappingData(var aMappingData: Pointer); virtual;
  1046. function IsEmpty: Boolean; virtual;
  1047. function MaskMatch(const aMask: TglBitmapRec4ul): Boolean; virtual;
  1048. procedure PreparePixel(out aPixel: TglBitmapPixelData); virtual;
  1049. constructor Create; virtual;
  1050. public
  1051. class procedure Init;
  1052. class function Get(const aFormat: TglBitmapFormat): TFormatDescriptor;
  1053. class function GetAlpha(const aFormat: TglBitmapFormat): TFormatDescriptor;
  1054. class function GetFromMask(const aMask: TglBitmapRec4ul; const aBitCount: Integer = 0): TFormatDescriptor;
  1055. class function GetFromPrecShift(const aPrec, aShift: TglBitmapRec4ub; const aBitCount: Integer): TFormatDescriptor;
  1056. class procedure Clear;
  1057. class procedure Finalize;
  1058. end;
  1059. TFormatDescriptorClass = class of TFormatDescriptor;
  1060. TfdEmpty = class(TFormatDescriptor);
  1061. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1062. TfdAlphaUB1 = class(TFormatDescriptor) //1* unsigned byte
  1063. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1064. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1065. end;
  1066. TfdLuminanceUB1 = class(TFormatDescriptor) //1* unsigned byte
  1067. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1068. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1069. end;
  1070. TfdUniversalUB1 = class(TFormatDescriptor) //1* unsigned byte
  1071. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1072. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1073. end;
  1074. TfdLuminanceAlphaUB2 = class(TfdLuminanceUB1) //2* unsigned byte
  1075. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1076. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1077. end;
  1078. TfdRGBub3 = class(TFormatDescriptor) //3* unsigned byte
  1079. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1080. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1081. end;
  1082. TfdBGRub3 = class(TFormatDescriptor) //3* unsigned byte (inverse)
  1083. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1084. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1085. end;
  1086. TfdRGBAub4 = class(TfdRGBub3) //3* unsigned byte
  1087. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1088. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1089. end;
  1090. TfdBGRAub4 = class(TfdBGRub3) //3* unsigned byte (inverse)
  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. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1095. TfdAlphaUS1 = class(TFormatDescriptor) //1* unsigned short
  1096. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1097. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1098. end;
  1099. TfdLuminanceUS1 = class(TFormatDescriptor) //1* unsigned short
  1100. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1101. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1102. end;
  1103. TfdUniversalUS1 = class(TFormatDescriptor) //1* unsigned short
  1104. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1105. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1106. end;
  1107. TfdDepthUS1 = class(TFormatDescriptor) //1* unsigned short
  1108. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1109. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1110. end;
  1111. TfdLuminanceAlphaUS2 = class(TfdLuminanceUS1) //2* unsigned short
  1112. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1113. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1114. end;
  1115. TfdRGBus3 = class(TFormatDescriptor) //3* unsigned short
  1116. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1117. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1118. end;
  1119. TfdBGRus3 = class(TFormatDescriptor) //3* unsigned short (inverse)
  1120. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1121. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1122. end;
  1123. TfdRGBAus4 = class(TfdRGBus3) //4* unsigned short
  1124. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1125. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1126. end;
  1127. TfdARGBus4 = class(TfdRGBus3) //4* unsigned short
  1128. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1129. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1130. end;
  1131. TfdBGRAus4 = class(TfdBGRus3) //4* unsigned short (inverse)
  1132. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1133. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1134. end;
  1135. TfdABGRus4 = class(TfdBGRus3) //4* unsigned short (inverse)
  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. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1140. TfdUniversalUI1 = class(TFormatDescriptor) //1* unsigned int
  1141. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1142. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1143. end;
  1144. TfdDepthUI1 = class(TFormatDescriptor) //1* unsigned int
  1145. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1146. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1147. end;
  1148. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1149. TfdAlpha4ub1 = class(TfdAlphaUB1)
  1150. procedure SetValues; override;
  1151. end;
  1152. TfdAlpha8ub1 = class(TfdAlphaUB1)
  1153. procedure SetValues; override;
  1154. end;
  1155. TfdAlpha16us1 = class(TfdAlphaUS1)
  1156. procedure SetValues; override;
  1157. end;
  1158. TfdLuminance4ub1 = class(TfdLuminanceUB1)
  1159. procedure SetValues; override;
  1160. end;
  1161. TfdLuminance8ub1 = class(TfdLuminanceUB1)
  1162. procedure SetValues; override;
  1163. end;
  1164. TfdLuminance16us1 = class(TfdLuminanceUS1)
  1165. procedure SetValues; override;
  1166. end;
  1167. TfdLuminance4Alpha4ub2 = class(TfdLuminanceAlphaUB2)
  1168. procedure SetValues; override;
  1169. end;
  1170. TfdLuminance6Alpha2ub2 = class(TfdLuminanceAlphaUB2)
  1171. procedure SetValues; override;
  1172. end;
  1173. TfdLuminance8Alpha8ub2 = class(TfdLuminanceAlphaUB2)
  1174. procedure SetValues; override;
  1175. end;
  1176. TfdLuminance12Alpha4us2 = class(TfdLuminanceAlphaUS2)
  1177. procedure SetValues; override;
  1178. end;
  1179. TfdLuminance16Alpha16us2 = class(TfdLuminanceAlphaUS2)
  1180. procedure SetValues; override;
  1181. end;
  1182. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1183. TfdR3G3B2ub1 = class(TfdUniversalUB1)
  1184. procedure SetValues; override;
  1185. end;
  1186. TfdRGBX4us1 = class(TfdUniversalUS1)
  1187. procedure SetValues; override;
  1188. end;
  1189. TfdXRGB4us1 = class(TfdUniversalUS1)
  1190. procedure SetValues; override;
  1191. end;
  1192. TfdR5G6B5us1 = class(TfdUniversalUS1)
  1193. procedure SetValues; override;
  1194. end;
  1195. TfdRGB5X1us1 = class(TfdUniversalUS1)
  1196. procedure SetValues; override;
  1197. end;
  1198. TfdX1RGB5us1 = class(TfdUniversalUS1)
  1199. procedure SetValues; override;
  1200. end;
  1201. TfdRGB8ub3 = class(TfdRGBub3)
  1202. procedure SetValues; override;
  1203. end;
  1204. TfdRGBX8ui1 = class(TfdUniversalUI1)
  1205. procedure SetValues; override;
  1206. end;
  1207. TfdXRGB8ui1 = class(TfdUniversalUI1)
  1208. procedure SetValues; override;
  1209. end;
  1210. TfdRGB10X2ui1 = class(TfdUniversalUI1)
  1211. procedure SetValues; override;
  1212. end;
  1213. TfdX2RGB10ui1 = class(TfdUniversalUI1)
  1214. procedure SetValues; override;
  1215. end;
  1216. TfdRGB16us3 = class(TfdRGBus3)
  1217. procedure SetValues; override;
  1218. end;
  1219. TfdRGBA4us1 = class(TfdUniversalUS1)
  1220. procedure SetValues; override;
  1221. end;
  1222. TfdARGB4us1 = class(TfdUniversalUS1)
  1223. procedure SetValues; override;
  1224. end;
  1225. TfdRGB5A1us1 = class(TfdUniversalUS1)
  1226. procedure SetValues; override;
  1227. end;
  1228. TfdA1RGB5us1 = class(TfdUniversalUS1)
  1229. procedure SetValues; override;
  1230. end;
  1231. TfdRGBA8ui1 = class(TfdUniversalUI1)
  1232. procedure SetValues; override;
  1233. end;
  1234. TfdARGB8ui1 = class(TfdUniversalUI1)
  1235. procedure SetValues; override;
  1236. end;
  1237. TfdRGBA8ub4 = class(TfdRGBAub4)
  1238. procedure SetValues; override;
  1239. end;
  1240. TfdRGB10A2ui1 = class(TfdUniversalUI1)
  1241. procedure SetValues; override;
  1242. end;
  1243. TfdA2RGB10ui1 = class(TfdUniversalUI1)
  1244. procedure SetValues; override;
  1245. end;
  1246. TfdRGBA16us4 = class(TfdRGBAus4)
  1247. procedure SetValues; override;
  1248. end;
  1249. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1250. TfdBGRX4us1 = class(TfdUniversalUS1)
  1251. procedure SetValues; override;
  1252. end;
  1253. TfdXBGR4us1 = class(TfdUniversalUS1)
  1254. procedure SetValues; override;
  1255. end;
  1256. TfdB5G6R5us1 = class(TfdUniversalUS1)
  1257. procedure SetValues; override;
  1258. end;
  1259. TfdBGR5X1us1 = class(TfdUniversalUS1)
  1260. procedure SetValues; override;
  1261. end;
  1262. TfdX1BGR5us1 = class(TfdUniversalUS1)
  1263. procedure SetValues; override;
  1264. end;
  1265. TfdBGR8ub3 = class(TfdBGRub3)
  1266. procedure SetValues; override;
  1267. end;
  1268. TfdBGRX8ui1 = class(TfdUniversalUI1)
  1269. procedure SetValues; override;
  1270. end;
  1271. TfdXBGR8ui1 = class(TfdUniversalUI1)
  1272. procedure SetValues; override;
  1273. end;
  1274. TfdBGR10X2ui1 = class(TfdUniversalUI1)
  1275. procedure SetValues; override;
  1276. end;
  1277. TfdX2BGR10ui1 = class(TfdUniversalUI1)
  1278. procedure SetValues; override;
  1279. end;
  1280. TfdBGR16us3 = class(TfdBGRus3)
  1281. procedure SetValues; override;
  1282. end;
  1283. TfdBGRA4us1 = class(TfdUniversalUS1)
  1284. procedure SetValues; override;
  1285. end;
  1286. TfdABGR4us1 = class(TfdUniversalUS1)
  1287. procedure SetValues; override;
  1288. end;
  1289. TfdBGR5A1us1 = class(TfdUniversalUS1)
  1290. procedure SetValues; override;
  1291. end;
  1292. TfdA1BGR5us1 = class(TfdUniversalUS1)
  1293. procedure SetValues; override;
  1294. end;
  1295. TfdBGRA8ui1 = class(TfdUniversalUI1)
  1296. procedure SetValues; override;
  1297. end;
  1298. TfdABGR8ui1 = class(TfdUniversalUI1)
  1299. procedure SetValues; override;
  1300. end;
  1301. TfdBGRA8ub4 = class(TfdBGRAub4)
  1302. procedure SetValues; override;
  1303. end;
  1304. TfdBGR10A2ui1 = class(TfdUniversalUI1)
  1305. procedure SetValues; override;
  1306. end;
  1307. TfdA2BGR10ui1 = class(TfdUniversalUI1)
  1308. procedure SetValues; override;
  1309. end;
  1310. TfdBGRA16us4 = class(TfdBGRAus4)
  1311. procedure SetValues; override;
  1312. end;
  1313. TfdDepth16us1 = class(TfdDepthUS1)
  1314. procedure SetValues; override;
  1315. end;
  1316. TfdDepth24ui1 = class(TfdDepthUI1)
  1317. procedure SetValues; override;
  1318. end;
  1319. TfdDepth32ui1 = class(TfdDepthUI1)
  1320. procedure SetValues; override;
  1321. end;
  1322. TfdS3tcDtx1RGBA = 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. TfdS3tcDtx3RGBA = 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. TfdS3tcDtx5RGBA = class(TFormatDescriptor)
  1333. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1334. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1335. procedure SetValues; override;
  1336. end;
  1337. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1338. TbmpBitfieldFormat = class(TFormatDescriptor)
  1339. public
  1340. procedure SetCustomValues(const aBPP: Integer; aMask: TglBitmapRec4ul); overload;
  1341. procedure SetCustomValues(const aBBP: Integer; const aPrec, aShift: TglBitmapRec4ub); overload;
  1342. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1343. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1344. end;
  1345. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1346. TbmpColorTableEnty = packed record
  1347. b, g, r, a: Byte;
  1348. end;
  1349. TbmpColorTable = array of TbmpColorTableEnty;
  1350. TbmpColorTableFormat = class(TFormatDescriptor)
  1351. private
  1352. fColorTable: TbmpColorTable;
  1353. protected
  1354. procedure SetValues; override;
  1355. public
  1356. property ColorTable: TbmpColorTable read fColorTable write fColorTable;
  1357. procedure SetCustomValues(const aFormat: TglBitmapFormat; const aBPP: Integer; const aPrec, aShift: TglBitmapRec4ub); overload;
  1358. procedure CalcValues;
  1359. procedure CreateColorTable;
  1360. function CreateMappingData: Pointer; override;
  1361. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1362. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1363. destructor Destroy; override;
  1364. end;
  1365. const
  1366. LUMINANCE_WEIGHT_R = 0.30;
  1367. LUMINANCE_WEIGHT_G = 0.59;
  1368. LUMINANCE_WEIGHT_B = 0.11;
  1369. ALPHA_WEIGHT_R = 0.30;
  1370. ALPHA_WEIGHT_G = 0.59;
  1371. ALPHA_WEIGHT_B = 0.11;
  1372. DEPTH_WEIGHT_R = 0.333333333;
  1373. DEPTH_WEIGHT_G = 0.333333333;
  1374. DEPTH_WEIGHT_B = 0.333333333;
  1375. FORMAT_DESCRIPTOR_CLASSES: array[TglBitmapFormat] of TFormatDescriptorClass = (
  1376. TfdEmpty,
  1377. TfdAlpha4ub1,
  1378. TfdAlpha8ub1,
  1379. TfdAlpha16us1,
  1380. TfdLuminance4ub1,
  1381. TfdLuminance8ub1,
  1382. TfdLuminance16us1,
  1383. TfdLuminance4Alpha4ub2,
  1384. TfdLuminance6Alpha2ub2,
  1385. TfdLuminance8Alpha8ub2,
  1386. TfdLuminance12Alpha4us2,
  1387. TfdLuminance16Alpha16us2,
  1388. TfdR3G3B2ub1,
  1389. TfdRGBX4us1,
  1390. TfdXRGB4us1,
  1391. TfdR5G6B5us1,
  1392. TfdRGB5X1us1,
  1393. TfdX1RGB5us1,
  1394. TfdRGB8ub3,
  1395. TfdRGBX8ui1,
  1396. TfdXRGB8ui1,
  1397. TfdRGB10X2ui1,
  1398. TfdX2RGB10ui1,
  1399. TfdRGB16us3,
  1400. TfdRGBA4us1,
  1401. TfdARGB4us1,
  1402. TfdRGB5A1us1,
  1403. TfdA1RGB5us1,
  1404. TfdRGBA8ui1,
  1405. TfdARGB8ui1,
  1406. TfdRGBA8ub4,
  1407. TfdRGB10A2ui1,
  1408. TfdA2RGB10ui1,
  1409. TfdRGBA16us4,
  1410. TfdBGRX4us1,
  1411. TfdXBGR4us1,
  1412. TfdB5G6R5us1,
  1413. TfdBGR5X1us1,
  1414. TfdX1BGR5us1,
  1415. TfdBGR8ub3,
  1416. TfdBGRX8ui1,
  1417. TfdXBGR8ui1,
  1418. TfdBGR10X2ui1,
  1419. TfdX2BGR10ui1,
  1420. TfdBGR16us3,
  1421. TfdBGRA4us1,
  1422. TfdABGR4us1,
  1423. TfdBGR5A1us1,
  1424. TfdA1BGR5us1,
  1425. TfdBGRA8ui1,
  1426. TfdABGR8ui1,
  1427. TfdBGRA8ub4,
  1428. TfdBGR10A2ui1,
  1429. TfdA2BGR10ui1,
  1430. TfdBGRA16us4,
  1431. TfdDepth16us1,
  1432. TfdDepth24ui1,
  1433. TfdDepth32ui1,
  1434. TfdS3tcDtx1RGBA,
  1435. TfdS3tcDtx3RGBA,
  1436. TfdS3tcDtx5RGBA
  1437. );
  1438. var
  1439. FormatDescriptorCS: TCriticalSection;
  1440. FormatDescriptors: array[TglBitmapFormat] of TFormatDescriptor;
  1441. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1442. constructor EglBitmapUnsupportedFormat.Create(const aFormat: TglBitmapFormat);
  1443. begin
  1444. inherited Create('unsupported format: ' + GetEnumName(TypeInfo(TglBitmapFormat), Integer(aFormat)));
  1445. end;
  1446. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1447. constructor EglBitmapUnsupportedFormat.Create(const aMsg: String; const aFormat: TglBitmapFormat);
  1448. begin
  1449. inherited Create(aMsg + GetEnumName(TypeInfo(TglBitmapFormat), Integer(aFormat)));
  1450. end;
  1451. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1452. function glBitmapSize(X: Integer; Y: Integer): TglBitmapSize;
  1453. begin
  1454. result.Fields := [];
  1455. if (X >= 0) then
  1456. result.Fields := result.Fields + [ffX];
  1457. if (Y >= 0) then
  1458. result.Fields := result.Fields + [ffY];
  1459. result.X := Max(0, X);
  1460. result.Y := Max(0, Y);
  1461. end;
  1462. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1463. function glBitmapPosition(X: Integer; Y: Integer): TglBitmapPixelPosition;
  1464. begin
  1465. result := glBitmapSize(X, Y);
  1466. end;
  1467. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1468. function glBitmapRec4ub(const r, g, b, a: Byte): TglBitmapRec4ub;
  1469. begin
  1470. result.r := r;
  1471. result.g := g;
  1472. result.b := b;
  1473. result.a := a;
  1474. end;
  1475. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1476. function glBitmapRec4ui(const r, g, b, a: Cardinal): TglBitmapRec4ui;
  1477. begin
  1478. result.r := r;
  1479. result.g := g;
  1480. result.b := b;
  1481. result.a := a;
  1482. end;
  1483. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1484. function glBitmapRec4ul(const r, g, b, a: QWord): TglBitmapRec4ul;
  1485. begin
  1486. result.r := r;
  1487. result.g := g;
  1488. result.b := b;
  1489. result.a := a;
  1490. end;
  1491. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1492. function glBitmapRec4ubCompare(const r1, r2: TglBitmapRec4ub): Boolean;
  1493. var
  1494. i: Integer;
  1495. begin
  1496. result := false;
  1497. for i := 0 to high(r1.arr) do
  1498. if (r1.arr[i] <> r2.arr[i]) then
  1499. exit;
  1500. result := true;
  1501. end;
  1502. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1503. function glBitmapRec4uiCompare(const r1, r2: TglBitmapRec4ui): Boolean;
  1504. var
  1505. i: Integer;
  1506. begin
  1507. result := false;
  1508. for i := 0 to high(r1.arr) do
  1509. if (r1.arr[i] <> r2.arr[i]) then
  1510. exit;
  1511. result := true;
  1512. end;
  1513. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1514. function glBitmapCreateTestData(const aFormat: TglBitmapFormat): TglBitmapData;
  1515. var
  1516. desc: TFormatDescriptor;
  1517. p, tmp: PByte;
  1518. x, y, i: Integer;
  1519. md: Pointer;
  1520. px: TglBitmapPixelData;
  1521. begin
  1522. result := nil;
  1523. desc := TFormatDescriptor.Get(aFormat);
  1524. if (desc.IsCompressed) or (desc.glFormat = 0) then
  1525. exit;
  1526. p := GetMemory(ceil(25 * desc.BytesPerPixel)); // 5 x 5 pixel
  1527. md := desc.CreateMappingData;
  1528. try
  1529. tmp := p;
  1530. desc.PreparePixel(px);
  1531. for y := 0 to 4 do
  1532. for x := 0 to 4 do begin
  1533. px.Data := glBitmapRec4ui(0, 0, 0, 0);
  1534. for i := 0 to 3 do begin
  1535. if ((y < 3) and (y = i)) or
  1536. ((y = 3) and (i < 3)) or
  1537. ((y = 4) and (i = 3))
  1538. then
  1539. px.Data.arr[i] := Trunc(px.Range.arr[i] / 4 * x)
  1540. else if ((y < 4) and (i = 3)) or
  1541. ((y = 4) and (i < 3))
  1542. then
  1543. px.Data.arr[i] := px.Range.arr[i]
  1544. else
  1545. px.Data.arr[i] := 0; //px.Range.arr[i];
  1546. end;
  1547. desc.Map(px, tmp, md);
  1548. end;
  1549. finally
  1550. desc.FreeMappingData(md);
  1551. end;
  1552. result := TglBitmapData.Create(glBitmapPosition(5, 5), aFormat, p);
  1553. end;
  1554. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1555. function glBitmapShiftRec(const r, g, b, a: Byte): TglBitmapRec4ub;
  1556. begin
  1557. result.r := r;
  1558. result.g := g;
  1559. result.b := b;
  1560. result.a := a;
  1561. end;
  1562. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1563. function FormatGetSupportedFiles(const aFormat: TglBitmapFormat): TglBitmapFileTypes;
  1564. begin
  1565. result := [];
  1566. if (aFormat in [
  1567. //8bpp
  1568. tfAlpha4ub1, tfAlpha8ub1,
  1569. tfLuminance4ub1, tfLuminance8ub1, tfR3G3B2ub1,
  1570. //16bpp
  1571. tfLuminance4Alpha4ub2, tfLuminance6Alpha2ub2, tfLuminance8Alpha8ub2,
  1572. tfRGBX4us1, tfXRGB4us1, tfRGB5X1us1, tfX1RGB5us1, tfR5G6B5us1, tfRGB5A1us1, tfA1RGB5us1, tfRGBA4us1, tfARGB4us1,
  1573. tfBGRX4us1, tfXBGR4us1, tfBGR5X1us1, tfX1BGR5us1, tfB5G6R5us1, tfBGR5A1us1, tfA1BGR5us1, tfBGRA4us1, tfABGR4us1,
  1574. //24bpp
  1575. tfBGR8ub3, tfRGB8ub3,
  1576. //32bpp
  1577. tfRGBX8ui1, tfXRGB8ui1, tfRGB10X2ui1, tfX2RGB10ui1, tfRGBA8ui1, tfARGB8ui1, tfRGBA8ub4, tfRGB10A2ui1, tfA2RGB10ui1,
  1578. tfBGRX8ui1, tfXBGR8ui1, tfBGR10X2ui1, tfX2BGR10ui1, tfBGRA8ui1, tfABGR8ui1, tfBGRA8ub4, tfBGR10A2ui1, tfA2BGR10ui1])
  1579. then
  1580. result := result + [ ftBMP ];
  1581. if (aFormat in [
  1582. //8bbp
  1583. tfAlpha4ub1, tfAlpha8ub1, tfLuminance4ub1, tfLuminance8ub1,
  1584. //16bbp
  1585. tfAlpha16us1, tfLuminance16us1,
  1586. tfLuminance4Alpha4ub2, tfLuminance6Alpha2ub2, tfLuminance8Alpha8ub2,
  1587. tfX1RGB5us1, tfARGB4us1, tfA1RGB5us1, tfDepth16us1,
  1588. //24bbp
  1589. tfBGR8ub3,
  1590. //32bbp
  1591. tfX2RGB10ui1, tfARGB8ui1, tfBGRA8ub4, tfA2RGB10ui1,
  1592. tfDepth24ui1, tfDepth32ui1])
  1593. then
  1594. result := result + [ftTGA];
  1595. if not (aFormat in [tfEmpty, tfRGB16us3, tfBGR16us3]) then
  1596. result := result + [ftDDS];
  1597. {$IFDEF GLB_SUPPORT_PNG_WRITE}
  1598. if aFormat in [
  1599. tfAlpha8ub1, tfLuminance8ub1, tfLuminance8Alpha8ub2,
  1600. tfRGB8ub3, tfRGBA8ui1,
  1601. tfBGR8ub3, tfBGRA8ui1] then
  1602. result := result + [ftPNG];
  1603. {$ENDIF}
  1604. {$IFDEF GLB_SUPPORT_JPEG_WRITE}
  1605. if aFormat in [tfAlpha8ub1, tfLuminance8ub1, tfRGB8ub3, tfBGR8ub3] then
  1606. result := result + [ftJPEG];
  1607. {$ENDIF}
  1608. end;
  1609. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1610. function IsPowerOfTwo(aNumber: Integer): Boolean;
  1611. begin
  1612. while (aNumber and 1) = 0 do
  1613. aNumber := aNumber shr 1;
  1614. result := aNumber = 1;
  1615. end;
  1616. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1617. function GetTopMostBit(aBitSet: QWord): Integer;
  1618. begin
  1619. result := 0;
  1620. while aBitSet > 0 do begin
  1621. inc(result);
  1622. aBitSet := aBitSet shr 1;
  1623. end;
  1624. end;
  1625. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1626. function CountSetBits(aBitSet: QWord): Integer;
  1627. begin
  1628. result := 0;
  1629. while aBitSet > 0 do begin
  1630. if (aBitSet and 1) = 1 then
  1631. inc(result);
  1632. aBitSet := aBitSet shr 1;
  1633. end;
  1634. end;
  1635. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1636. function LuminanceWeight(const aPixel: TglBitmapPixelData): Cardinal;
  1637. begin
  1638. result := Trunc(
  1639. LUMINANCE_WEIGHT_R * aPixel.Data.r +
  1640. LUMINANCE_WEIGHT_G * aPixel.Data.g +
  1641. LUMINANCE_WEIGHT_B * aPixel.Data.b);
  1642. end;
  1643. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1644. function DepthWeight(const aPixel: TglBitmapPixelData): Cardinal;
  1645. begin
  1646. result := Trunc(
  1647. DEPTH_WEIGHT_R * aPixel.Data.r +
  1648. DEPTH_WEIGHT_G * aPixel.Data.g +
  1649. DEPTH_WEIGHT_B * aPixel.Data.b);
  1650. end;
  1651. {$IFDEF GLB_SDL_IMAGE}
  1652. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1653. // SDL Image Helper /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1654. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1655. function glBitmapRWseek(context: PSDL_RWops; offset: Integer; whence: Integer): Integer; cdecl;
  1656. begin
  1657. result := TStream(context^.unknown.data1).Seek(offset, whence);
  1658. end;
  1659. function glBitmapRWread(context: PSDL_RWops; Ptr: Pointer; size: Integer; maxnum : Integer): Integer; cdecl;
  1660. begin
  1661. result := TStream(context^.unknown.data1).Read(Ptr^, size * maxnum);
  1662. end;
  1663. function glBitmapRWwrite(context: PSDL_RWops; Ptr: Pointer; size: Integer; num: Integer): Integer; cdecl;
  1664. begin
  1665. result := TStream(context^.unknown.data1).Write(Ptr^, size * num);
  1666. end;
  1667. function glBitmapRWclose(context: PSDL_RWops): Integer; cdecl;
  1668. begin
  1669. result := 0;
  1670. end;
  1671. function glBitmapCreateRWops(Stream: TStream): PSDL_RWops;
  1672. begin
  1673. result := SDL_AllocRW;
  1674. if result = nil then
  1675. raise EglBitmap.Create('glBitmapCreateRWops - SDL_AllocRW failed.');
  1676. result^.seek := glBitmapRWseek;
  1677. result^.read := glBitmapRWread;
  1678. result^.write := glBitmapRWwrite;
  1679. result^.close := glBitmapRWclose;
  1680. result^.unknown.data1 := Stream;
  1681. end;
  1682. {$ENDIF}
  1683. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1684. procedure glBitmapSetDefaultDeleteTextureOnFree(const aDeleteTextureOnFree: Boolean);
  1685. begin
  1686. glBitmapDefaultDeleteTextureOnFree := aDeleteTextureOnFree;
  1687. end;
  1688. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1689. procedure glBitmapSetDefaultFreeDataAfterGenTexture(const aFreeData: Boolean);
  1690. begin
  1691. glBitmapDefaultFreeDataAfterGenTextures := aFreeData;
  1692. end;
  1693. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1694. procedure glBitmapSetDefaultMipmap(const aValue: TglBitmapMipMap);
  1695. begin
  1696. glBitmapDefaultMipmap := aValue;
  1697. end;
  1698. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1699. procedure glBitmapSetDefaultFormat(const aFormat: TglBitmapFormat);
  1700. begin
  1701. glBitmapDefaultFormat := aFormat;
  1702. end;
  1703. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1704. procedure glBitmapSetDefaultFilter(const aMin, aMag: Integer);
  1705. begin
  1706. glBitmapDefaultFilterMin := aMin;
  1707. glBitmapDefaultFilterMag := aMag;
  1708. end;
  1709. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1710. procedure glBitmapSetDefaultWrap(const S: Cardinal = GL_CLAMP_TO_EDGE; const T: Cardinal = GL_CLAMP_TO_EDGE; const R: Cardinal = GL_CLAMP_TO_EDGE);
  1711. begin
  1712. glBitmapDefaultWrapS := S;
  1713. glBitmapDefaultWrapT := T;
  1714. glBitmapDefaultWrapR := R;
  1715. end;
  1716. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1717. {$IF NOT DEFINED(OPENGL_ES) OR DEFINED(OPENGL_ES_3_0)}
  1718. procedure glBitmapSetDefaultSwizzle(const r: GLenum = GL_RED; g: GLenum = GL_GREEN; b: GLenum = GL_BLUE; a: GLenum = GL_ALPHA);
  1719. begin
  1720. glDefaultSwizzle[0] := r;
  1721. glDefaultSwizzle[1] := g;
  1722. glDefaultSwizzle[2] := b;
  1723. glDefaultSwizzle[3] := a;
  1724. end;
  1725. {$IFEND}
  1726. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1727. function glBitmapGetDefaultDeleteTextureOnFree: Boolean;
  1728. begin
  1729. result := glBitmapDefaultDeleteTextureOnFree;
  1730. end;
  1731. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1732. function glBitmapGetDefaultFreeDataAfterGenTexture: Boolean;
  1733. begin
  1734. result := glBitmapDefaultFreeDataAfterGenTextures;
  1735. end;
  1736. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1737. function glBitmapGetDefaultMipmap: TglBitmapMipMap;
  1738. begin
  1739. result := glBitmapDefaultMipmap;
  1740. end;
  1741. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1742. function glBitmapGetDefaultFormat: TglBitmapFormat;
  1743. begin
  1744. result := glBitmapDefaultFormat;
  1745. end;
  1746. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1747. procedure glBitmapGetDefaultFilter(var aMin, aMag: Cardinal);
  1748. begin
  1749. aMin := glBitmapDefaultFilterMin;
  1750. aMag := glBitmapDefaultFilterMag;
  1751. end;
  1752. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1753. procedure glBitmapGetDefaultTextureWrap(var S, T, R: Cardinal);
  1754. begin
  1755. S := glBitmapDefaultWrapS;
  1756. T := glBitmapDefaultWrapT;
  1757. R := glBitmapDefaultWrapR;
  1758. end;
  1759. {$IF NOT DEFINED(OPENGL_ES) OR DEFINED(OPENGL_ES_3_0)}
  1760. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1761. procedure glBitmapGetDefaultSwizzle(var r, g, b, a: GLenum);
  1762. begin
  1763. r := glDefaultSwizzle[0];
  1764. g := glDefaultSwizzle[1];
  1765. b := glDefaultSwizzle[2];
  1766. a := glDefaultSwizzle[3];
  1767. end;
  1768. {$IFEND}
  1769. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1770. //TFormatDescriptor///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1771. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1772. function TFormatDescriptor.CreateMappingData: Pointer;
  1773. begin
  1774. result := nil;
  1775. end;
  1776. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1777. procedure TFormatDescriptor.FreeMappingData(var aMappingData: Pointer);
  1778. begin
  1779. //DUMMY
  1780. end;
  1781. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1782. function TFormatDescriptor.IsEmpty: Boolean;
  1783. begin
  1784. result := (fFormat = tfEmpty);
  1785. end;
  1786. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1787. function TFormatDescriptor.MaskMatch(const aMask: TglBitmapRec4ul): Boolean;
  1788. var
  1789. i: Integer;
  1790. m: TglBitmapRec4ul;
  1791. begin
  1792. result := false;
  1793. if (aMask.r = 0) and (aMask.g = 0) and (aMask.b = 0) and (aMask.a = 0) then
  1794. raise EglBitmap.Create('FormatCheckFormat - All Masks are 0');
  1795. m := Mask;
  1796. for i := 0 to 3 do
  1797. if (aMask.arr[i] <> m.arr[i]) then
  1798. exit;
  1799. result := true;
  1800. end;
  1801. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1802. procedure TFormatDescriptor.PreparePixel(out aPixel: TglBitmapPixelData);
  1803. begin
  1804. FillChar(aPixel{%H-}, SizeOf(aPixel), 0);
  1805. aPixel.Data := Range;
  1806. aPixel.Format := fFormat;
  1807. aPixel.Range := Range;
  1808. end;
  1809. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1810. constructor TFormatDescriptor.Create;
  1811. begin
  1812. inherited Create;
  1813. end;
  1814. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1815. //TfdAlpha_UB1////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1816. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1817. procedure TfdAlphaUB1.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  1818. begin
  1819. aData^ := aPixel.Data.a;
  1820. inc(aData);
  1821. end;
  1822. procedure TfdAlphaUB1.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  1823. begin
  1824. aPixel.Data.r := 0;
  1825. aPixel.Data.g := 0;
  1826. aPixel.Data.b := 0;
  1827. aPixel.Data.a := aData^;
  1828. inc(aData);
  1829. end;
  1830. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1831. //TfdLuminance_UB1////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1832. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1833. procedure TfdLuminanceUB1.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  1834. begin
  1835. aData^ := LuminanceWeight(aPixel);
  1836. inc(aData);
  1837. end;
  1838. procedure TfdLuminanceUB1.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  1839. begin
  1840. aPixel.Data.r := aData^;
  1841. aPixel.Data.g := aData^;
  1842. aPixel.Data.b := aData^;
  1843. aPixel.Data.a := 0;
  1844. inc(aData);
  1845. end;
  1846. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1847. //TfdUniversal_UB1////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1848. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1849. procedure TfdUniversalUB1.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  1850. var
  1851. i: Integer;
  1852. begin
  1853. aData^ := 0;
  1854. for i := 0 to 3 do
  1855. if (Range.arr[i] > 0) then
  1856. aData^ := aData^ or ((aPixel.Data.arr[i] and Range.arr[i]) shl fShift.arr[i]);
  1857. inc(aData);
  1858. end;
  1859. procedure TfdUniversalUB1.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  1860. var
  1861. i: Integer;
  1862. begin
  1863. for i := 0 to 3 do
  1864. aPixel.Data.arr[i] := (aData^ shr fShift.arr[i]) and Range.arr[i];
  1865. inc(aData);
  1866. end;
  1867. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1868. //TfdLuminanceAlpha_UB2///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1869. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1870. procedure TfdLuminanceAlphaUB2.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  1871. begin
  1872. inherited Map(aPixel, aData, aMapData);
  1873. aData^ := aPixel.Data.a;
  1874. inc(aData);
  1875. end;
  1876. procedure TfdLuminanceAlphaUB2.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  1877. begin
  1878. inherited Unmap(aData, aPixel, aMapData);
  1879. aPixel.Data.a := aData^;
  1880. inc(aData);
  1881. end;
  1882. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1883. //TfdRGB_UB3//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1884. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1885. procedure TfdRGBub3.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  1886. begin
  1887. aData^ := aPixel.Data.r;
  1888. inc(aData);
  1889. aData^ := aPixel.Data.g;
  1890. inc(aData);
  1891. aData^ := aPixel.Data.b;
  1892. inc(aData);
  1893. end;
  1894. procedure TfdRGBub3.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  1895. begin
  1896. aPixel.Data.r := aData^;
  1897. inc(aData);
  1898. aPixel.Data.g := aData^;
  1899. inc(aData);
  1900. aPixel.Data.b := aData^;
  1901. inc(aData);
  1902. aPixel.Data.a := 0;
  1903. end;
  1904. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1905. //TfdBGR_UB3//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1906. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1907. procedure TfdBGRub3.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  1908. begin
  1909. aData^ := aPixel.Data.b;
  1910. inc(aData);
  1911. aData^ := aPixel.Data.g;
  1912. inc(aData);
  1913. aData^ := aPixel.Data.r;
  1914. inc(aData);
  1915. end;
  1916. procedure TfdBGRub3.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  1917. begin
  1918. aPixel.Data.b := aData^;
  1919. inc(aData);
  1920. aPixel.Data.g := aData^;
  1921. inc(aData);
  1922. aPixel.Data.r := aData^;
  1923. inc(aData);
  1924. aPixel.Data.a := 0;
  1925. end;
  1926. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1927. //TfdRGBA_UB4//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1928. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1929. procedure TfdRGBAub4.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  1930. begin
  1931. inherited Map(aPixel, aData, aMapData);
  1932. aData^ := aPixel.Data.a;
  1933. inc(aData);
  1934. end;
  1935. procedure TfdRGBAub4.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  1936. begin
  1937. inherited Unmap(aData, aPixel, aMapData);
  1938. aPixel.Data.a := aData^;
  1939. inc(aData);
  1940. end;
  1941. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1942. //TfdBGRA_UB4//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1943. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1944. procedure TfdBGRAub4.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  1945. begin
  1946. inherited Map(aPixel, aData, aMapData);
  1947. aData^ := aPixel.Data.a;
  1948. inc(aData);
  1949. end;
  1950. procedure TfdBGRAub4.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  1951. begin
  1952. inherited Unmap(aData, aPixel, aMapData);
  1953. aPixel.Data.a := aData^;
  1954. inc(aData);
  1955. end;
  1956. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1957. //TfdAlpha_US1////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1958. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1959. procedure TfdAlphaUS1.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  1960. begin
  1961. PWord(aData)^ := aPixel.Data.a;
  1962. inc(aData, 2);
  1963. end;
  1964. procedure TfdAlphaUS1.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  1965. begin
  1966. aPixel.Data.r := 0;
  1967. aPixel.Data.g := 0;
  1968. aPixel.Data.b := 0;
  1969. aPixel.Data.a := PWord(aData)^;
  1970. inc(aData, 2);
  1971. end;
  1972. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1973. //TfdLuminance_US1////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1974. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1975. procedure TfdLuminanceUS1.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  1976. begin
  1977. PWord(aData)^ := LuminanceWeight(aPixel);
  1978. inc(aData, 2);
  1979. end;
  1980. procedure TfdLuminanceUS1.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  1981. begin
  1982. aPixel.Data.r := PWord(aData)^;
  1983. aPixel.Data.g := PWord(aData)^;
  1984. aPixel.Data.b := PWord(aData)^;
  1985. aPixel.Data.a := 0;
  1986. inc(aData, 2);
  1987. end;
  1988. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1989. //TfdUniversal_US1////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1990. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1991. procedure TfdUniversalUS1.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  1992. var
  1993. i: Integer;
  1994. begin
  1995. PWord(aData)^ := 0;
  1996. for i := 0 to 3 do
  1997. if (Range.arr[i] > 0) then
  1998. PWord(aData)^ := PWord(aData)^ or ((aPixel.Data.arr[i] and Range.arr[i]) shl fShift.arr[i]);
  1999. inc(aData, 2);
  2000. end;
  2001. procedure TfdUniversalUS1.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  2002. var
  2003. i: Integer;
  2004. begin
  2005. for i := 0 to 3 do
  2006. aPixel.Data.arr[i] := (PWord(aData)^ shr fShift.arr[i]) and Range.arr[i];
  2007. inc(aData, 2);
  2008. end;
  2009. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2010. //TfdDepth_US1////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2011. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2012. procedure TfdDepthUS1.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  2013. begin
  2014. PWord(aData)^ := DepthWeight(aPixel);
  2015. inc(aData, 2);
  2016. end;
  2017. procedure TfdDepthUS1.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  2018. begin
  2019. aPixel.Data.r := PWord(aData)^;
  2020. aPixel.Data.g := PWord(aData)^;
  2021. aPixel.Data.b := PWord(aData)^;
  2022. aPixel.Data.a := PWord(aData)^;;
  2023. inc(aData, 2);
  2024. end;
  2025. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2026. //TfdLuminanceAlpha_US2///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2027. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2028. procedure TfdLuminanceAlphaUS2.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  2029. begin
  2030. inherited Map(aPixel, aData, aMapData);
  2031. PWord(aData)^ := aPixel.Data.a;
  2032. inc(aData, 2);
  2033. end;
  2034. procedure TfdLuminanceAlphaUS2.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  2035. begin
  2036. inherited Unmap(aData, aPixel, aMapData);
  2037. aPixel.Data.a := PWord(aData)^;
  2038. inc(aData, 2);
  2039. end;
  2040. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2041. //TfdRGB_US3//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2042. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2043. procedure TfdRGBus3.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  2044. begin
  2045. PWord(aData)^ := aPixel.Data.r;
  2046. inc(aData, 2);
  2047. PWord(aData)^ := aPixel.Data.g;
  2048. inc(aData, 2);
  2049. PWord(aData)^ := aPixel.Data.b;
  2050. inc(aData, 2);
  2051. end;
  2052. procedure TfdRGBus3.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  2053. begin
  2054. aPixel.Data.r := PWord(aData)^;
  2055. inc(aData, 2);
  2056. aPixel.Data.g := PWord(aData)^;
  2057. inc(aData, 2);
  2058. aPixel.Data.b := PWord(aData)^;
  2059. inc(aData, 2);
  2060. aPixel.Data.a := 0;
  2061. end;
  2062. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2063. //TfdBGR_US3//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2064. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2065. procedure TfdBGRus3.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  2066. begin
  2067. PWord(aData)^ := aPixel.Data.b;
  2068. inc(aData, 2);
  2069. PWord(aData)^ := aPixel.Data.g;
  2070. inc(aData, 2);
  2071. PWord(aData)^ := aPixel.Data.r;
  2072. inc(aData, 2);
  2073. end;
  2074. procedure TfdBGRus3.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  2075. begin
  2076. aPixel.Data.b := PWord(aData)^;
  2077. inc(aData, 2);
  2078. aPixel.Data.g := PWord(aData)^;
  2079. inc(aData, 2);
  2080. aPixel.Data.r := PWord(aData)^;
  2081. inc(aData, 2);
  2082. aPixel.Data.a := 0;
  2083. end;
  2084. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2085. //TfdRGBA_US4/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2086. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2087. procedure TfdRGBAus4.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  2088. begin
  2089. inherited Map(aPixel, aData, aMapData);
  2090. PWord(aData)^ := aPixel.Data.a;
  2091. inc(aData, 2);
  2092. end;
  2093. procedure TfdRGBAus4.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  2094. begin
  2095. inherited Unmap(aData, aPixel, aMapData);
  2096. aPixel.Data.a := PWord(aData)^;
  2097. inc(aData, 2);
  2098. end;
  2099. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2100. //TfdARGB_US4/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2101. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2102. procedure TfdARGBus4.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  2103. begin
  2104. PWord(aData)^ := aPixel.Data.a;
  2105. inc(aData, 2);
  2106. inherited Map(aPixel, aData, aMapData);
  2107. end;
  2108. procedure TfdARGBus4.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  2109. begin
  2110. aPixel.Data.a := PWord(aData)^;
  2111. inc(aData, 2);
  2112. inherited Unmap(aData, aPixel, aMapData);
  2113. end;
  2114. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2115. //TfdBGRA_US4/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2116. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2117. procedure TfdBGRAus4.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  2118. begin
  2119. inherited Map(aPixel, aData, aMapData);
  2120. PWord(aData)^ := aPixel.Data.a;
  2121. inc(aData, 2);
  2122. end;
  2123. procedure TfdBGRAus4.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  2124. begin
  2125. inherited Unmap(aData, aPixel, aMapData);
  2126. aPixel.Data.a := PWord(aData)^;
  2127. inc(aData, 2);
  2128. end;
  2129. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2130. //TfdABGR_US4/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2131. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2132. procedure TfdABGRus4.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  2133. begin
  2134. PWord(aData)^ := aPixel.Data.a;
  2135. inc(aData, 2);
  2136. inherited Map(aPixel, aData, aMapData);
  2137. end;
  2138. procedure TfdABGRus4.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  2139. begin
  2140. aPixel.Data.a := PWord(aData)^;
  2141. inc(aData, 2);
  2142. inherited Unmap(aData, aPixel, aMapData);
  2143. end;
  2144. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2145. //TfdUniversal_UI1////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2146. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2147. procedure TfdUniversalUI1.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  2148. var
  2149. i: Integer;
  2150. begin
  2151. PCardinal(aData)^ := 0;
  2152. for i := 0 to 3 do
  2153. if (Range.arr[i] > 0) then
  2154. PCardinal(aData)^ := PCardinal(aData)^ or ((aPixel.Data.arr[i] and Range.arr[i]) shl fShift.arr[i]);
  2155. inc(aData, 4);
  2156. end;
  2157. procedure TfdUniversalUI1.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  2158. var
  2159. i: Integer;
  2160. begin
  2161. for i := 0 to 3 do
  2162. aPixel.Data.arr[i] := (PCardinal(aData)^ shr fShift.arr[i]) and Range.arr[i];
  2163. inc(aData, 2);
  2164. end;
  2165. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2166. //TfdDepth_UI1////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2167. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2168. procedure TfdDepthUI1.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  2169. begin
  2170. PCardinal(aData)^ := DepthWeight(aPixel);
  2171. inc(aData, 4);
  2172. end;
  2173. procedure TfdDepthUI1.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  2174. begin
  2175. aPixel.Data.r := PCardinal(aData)^;
  2176. aPixel.Data.g := PCardinal(aData)^;
  2177. aPixel.Data.b := PCardinal(aData)^;
  2178. aPixel.Data.a := PCardinal(aData)^;
  2179. inc(aData, 4);
  2180. end;
  2181. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2182. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2183. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2184. procedure TfdAlpha4ub1.SetValues;
  2185. begin
  2186. inherited SetValues;
  2187. fBitsPerPixel := 8;
  2188. fFormat := tfAlpha4ub1;
  2189. fWithAlpha := tfAlpha4ub1;
  2190. fPrecision := glBitmapRec4ub(0, 0, 0, 8);
  2191. fShift := glBitmapRec4ub(0, 0, 0, 0);
  2192. {$IFNDEF OPENGL_ES}
  2193. fOpenGLFormat := tfAlpha4ub1;
  2194. fglFormat := GL_ALPHA;
  2195. fglInternalFormat := GL_ALPHA4;
  2196. fglDataFormat := GL_UNSIGNED_BYTE;
  2197. {$ELSE}
  2198. fOpenGLFormat := tfAlpha8ub1;
  2199. {$ENDIF}
  2200. end;
  2201. procedure TfdAlpha8ub1.SetValues;
  2202. begin
  2203. inherited SetValues;
  2204. fBitsPerPixel := 8;
  2205. fFormat := tfAlpha8ub1;
  2206. fWithAlpha := tfAlpha8ub1;
  2207. fPrecision := glBitmapRec4ub(0, 0, 0, 8);
  2208. fShift := glBitmapRec4ub(0, 0, 0, 0);
  2209. fOpenGLFormat := tfAlpha8ub1;
  2210. fglFormat := GL_ALPHA;
  2211. fglInternalFormat := {$IFNDEF OPENGL_ES}GL_ALPHA8{$ELSE}GL_ALPHA{$ENDIF};
  2212. fglDataFormat := GL_UNSIGNED_BYTE;
  2213. end;
  2214. procedure TfdAlpha16us1.SetValues;
  2215. begin
  2216. inherited SetValues;
  2217. fBitsPerPixel := 16;
  2218. fFormat := tfAlpha16us1;
  2219. fWithAlpha := tfAlpha16us1;
  2220. fPrecision := glBitmapRec4ub(0, 0, 0, 16);
  2221. fShift := glBitmapRec4ub(0, 0, 0, 0);
  2222. {$IFNDEF OPENGL_ES}
  2223. fOpenGLFormat := tfAlpha16us1;
  2224. fglFormat := GL_ALPHA;
  2225. fglInternalFormat := GL_ALPHA16;
  2226. fglDataFormat := GL_UNSIGNED_SHORT;
  2227. {$ELSE}
  2228. fOpenGLFormat := tfAlpha8ub1;
  2229. {$ENDIF}
  2230. end;
  2231. procedure TfdLuminance4ub1.SetValues;
  2232. begin
  2233. inherited SetValues;
  2234. fBitsPerPixel := 8;
  2235. fFormat := tfLuminance4ub1;
  2236. fWithAlpha := tfLuminance4Alpha4ub2;
  2237. fWithoutAlpha := tfLuminance4ub1;
  2238. fPrecision := glBitmapRec4ub(8, 8, 8, 0);
  2239. fShift := glBitmapRec4ub(0, 0, 0, 0);
  2240. {$IFNDEF OPENGL_ES}
  2241. fOpenGLFormat := tfLuminance4ub1;
  2242. fglFormat := GL_LUMINANCE;
  2243. fglInternalFormat := GL_LUMINANCE4;
  2244. fglDataFormat := GL_UNSIGNED_BYTE;
  2245. {$ELSE}
  2246. fOpenGLFormat := tfLuminance8ub1;
  2247. {$ENDIF}
  2248. end;
  2249. procedure TfdLuminance8ub1.SetValues;
  2250. begin
  2251. inherited SetValues;
  2252. fBitsPerPixel := 8;
  2253. fFormat := tfLuminance8ub1;
  2254. fWithAlpha := tfLuminance8Alpha8ub2;
  2255. fWithoutAlpha := tfLuminance8ub1;
  2256. fOpenGLFormat := tfLuminance8ub1;
  2257. fPrecision := glBitmapRec4ub(8, 8, 8, 0);
  2258. fShift := glBitmapRec4ub(0, 0, 0, 0);
  2259. fglFormat := GL_LUMINANCE;
  2260. fglInternalFormat := {$IFNDEF OPENGL_ES}GL_LUMINANCE8{$ELSE}GL_LUMINANCE{$ENDIF};
  2261. fglDataFormat := GL_UNSIGNED_BYTE;
  2262. end;
  2263. procedure TfdLuminance16us1.SetValues;
  2264. begin
  2265. inherited SetValues;
  2266. fBitsPerPixel := 16;
  2267. fFormat := tfLuminance16us1;
  2268. fWithAlpha := tfLuminance16Alpha16us2;
  2269. fWithoutAlpha := tfLuminance16us1;
  2270. fPrecision := glBitmapRec4ub(16, 16, 16, 0);
  2271. fShift := glBitmapRec4ub( 0, 0, 0, 0);
  2272. {$IFNDEF OPENGL_ES}
  2273. fOpenGLFormat := tfLuminance16us1;
  2274. fglFormat := GL_LUMINANCE;
  2275. fglInternalFormat := GL_LUMINANCE16;
  2276. fglDataFormat := GL_UNSIGNED_SHORT;
  2277. {$ELSE}
  2278. fOpenGLFormat := tfLuminance8ub1;
  2279. {$ENDIF}
  2280. end;
  2281. procedure TfdLuminance4Alpha4ub2.SetValues;
  2282. begin
  2283. inherited SetValues;
  2284. fBitsPerPixel := 16;
  2285. fFormat := tfLuminance4Alpha4ub2;
  2286. fWithAlpha := tfLuminance4Alpha4ub2;
  2287. fWithoutAlpha := tfLuminance4ub1;
  2288. fPrecision := glBitmapRec4ub(8, 8, 8, 8);
  2289. fShift := glBitmapRec4ub(0, 0, 0, 8);
  2290. {$IFNDEF OPENGL_ES}
  2291. fOpenGLFormat := tfLuminance4Alpha4ub2;
  2292. fglFormat := GL_LUMINANCE_ALPHA;
  2293. fglInternalFormat := GL_LUMINANCE4_ALPHA4;
  2294. fglDataFormat := GL_UNSIGNED_BYTE;
  2295. {$ELSE}
  2296. fOpenGLFormat := tfLuminance8Alpha8ub2;
  2297. {$ENDIF}
  2298. end;
  2299. procedure TfdLuminance6Alpha2ub2.SetValues;
  2300. begin
  2301. inherited SetValues;
  2302. fBitsPerPixel := 16;
  2303. fFormat := tfLuminance6Alpha2ub2;
  2304. fWithAlpha := tfLuminance6Alpha2ub2;
  2305. fWithoutAlpha := tfLuminance8ub1;
  2306. fPrecision := glBitmapRec4ub(8, 8, 8, 8);
  2307. fShift := glBitmapRec4ub(0, 0, 0, 8);
  2308. {$IFNDEF OPENGL_ES}
  2309. fOpenGLFormat := tfLuminance6Alpha2ub2;
  2310. fglFormat := GL_LUMINANCE_ALPHA;
  2311. fglInternalFormat := GL_LUMINANCE6_ALPHA2;
  2312. fglDataFormat := GL_UNSIGNED_BYTE;
  2313. {$ELSE}
  2314. fOpenGLFormat := tfLuminance8Alpha8ub2;
  2315. {$ENDIF}
  2316. end;
  2317. procedure TfdLuminance8Alpha8ub2.SetValues;
  2318. begin
  2319. inherited SetValues;
  2320. fBitsPerPixel := 16;
  2321. fFormat := tfLuminance8Alpha8ub2;
  2322. fWithAlpha := tfLuminance8Alpha8ub2;
  2323. fWithoutAlpha := tfLuminance8ub1;
  2324. fOpenGLFormat := tfLuminance8Alpha8ub2;
  2325. fPrecision := glBitmapRec4ub(8, 8, 8, 8);
  2326. fShift := glBitmapRec4ub(0, 0, 0, 8);
  2327. fglFormat := GL_LUMINANCE_ALPHA;
  2328. fglInternalFormat := {$IFNDEF OPENGL_ES}GL_LUMINANCE8_ALPHA8{$ELSE}GL_LUMINANCE_ALPHA{$ENDIF};
  2329. fglDataFormat := GL_UNSIGNED_BYTE;
  2330. end;
  2331. procedure TfdLuminance12Alpha4us2.SetValues;
  2332. begin
  2333. inherited SetValues;
  2334. fBitsPerPixel := 32;
  2335. fFormat := tfLuminance12Alpha4us2;
  2336. fWithAlpha := tfLuminance12Alpha4us2;
  2337. fWithoutAlpha := tfLuminance16us1;
  2338. fPrecision := glBitmapRec4ub(16, 16, 16, 16);
  2339. fShift := glBitmapRec4ub( 0, 0, 0, 16);
  2340. {$IFNDEF OPENGL_ES}
  2341. fOpenGLFormat := tfLuminance12Alpha4us2;
  2342. fglFormat := GL_LUMINANCE_ALPHA;
  2343. fglInternalFormat := GL_LUMINANCE12_ALPHA4;
  2344. fglDataFormat := GL_UNSIGNED_SHORT;
  2345. {$ELSE}
  2346. fOpenGLFormat := tfLuminance8Alpha8ub2;
  2347. {$ENDIF}
  2348. end;
  2349. procedure TfdLuminance16Alpha16us2.SetValues;
  2350. begin
  2351. inherited SetValues;
  2352. fBitsPerPixel := 32;
  2353. fFormat := tfLuminance16Alpha16us2;
  2354. fWithAlpha := tfLuminance16Alpha16us2;
  2355. fWithoutAlpha := tfLuminance16us1;
  2356. fPrecision := glBitmapRec4ub(16, 16, 16, 16);
  2357. fShift := glBitmapRec4ub( 0, 0, 0, 16);
  2358. {$IFNDEF OPENGL_ES}
  2359. fOpenGLFormat := tfLuminance16Alpha16us2;
  2360. fglFormat := GL_LUMINANCE_ALPHA;
  2361. fglInternalFormat := GL_LUMINANCE16_ALPHA16;
  2362. fglDataFormat := GL_UNSIGNED_SHORT;
  2363. {$ELSE}
  2364. fOpenGLFormat := tfLuminance8Alpha8ub2;
  2365. {$ENDIF}
  2366. end;
  2367. procedure TfdR3G3B2ub1.SetValues;
  2368. begin
  2369. inherited SetValues;
  2370. fBitsPerPixel := 8;
  2371. fFormat := tfR3G3B2ub1;
  2372. fWithAlpha := tfRGBA4us1;
  2373. fWithoutAlpha := tfR3G3B2ub1;
  2374. fRGBInverted := tfEmpty;
  2375. fPrecision := glBitmapRec4ub(3, 3, 2, 0);
  2376. fShift := glBitmapRec4ub(5, 2, 0, 0);
  2377. {$IFNDEF OPENGL_ES}
  2378. fOpenGLFormat := tfR3G3B2ub1;
  2379. fglFormat := GL_RGB;
  2380. fglInternalFormat := GL_R3_G3_B2;
  2381. fglDataFormat := GL_UNSIGNED_BYTE_3_3_2;
  2382. {$ELSE}
  2383. fOpenGLFormat := tfR5G6B5us1;
  2384. {$ENDIF}
  2385. end;
  2386. procedure TfdRGBX4us1.SetValues;
  2387. begin
  2388. inherited SetValues;
  2389. fBitsPerPixel := 16;
  2390. fFormat := tfRGBX4us1;
  2391. fWithAlpha := tfRGBA4us1;
  2392. fWithoutAlpha := tfRGBX4us1;
  2393. fRGBInverted := tfBGRX4us1;
  2394. fPrecision := glBitmapRec4ub( 4, 4, 4, 0);
  2395. fShift := glBitmapRec4ub(12, 8, 4, 0);
  2396. {$IFNDEF OPENGL_ES}
  2397. fOpenGLFormat := tfRGBX4us1;
  2398. fglFormat := GL_RGBA; //GL_INVALID_OPERATION if not GL_BGRA or GL_RGBA
  2399. fglInternalFormat := GL_RGB4;
  2400. fglDataFormat := GL_UNSIGNED_SHORT_4_4_4_4;
  2401. {$ELSE}
  2402. fOpenGLFormat := tfR5G6B5us1;
  2403. {$ENDIF}
  2404. end;
  2405. procedure TfdXRGB4us1.SetValues;
  2406. begin
  2407. inherited SetValues;
  2408. fBitsPerPixel := 16;
  2409. fFormat := tfXRGB4us1;
  2410. fWithAlpha := tfARGB4us1;
  2411. fWithoutAlpha := tfXRGB4us1;
  2412. fRGBInverted := tfXBGR4us1;
  2413. fPrecision := glBitmapRec4ub(4, 4, 4, 0);
  2414. fShift := glBitmapRec4ub(8, 4, 0, 0);
  2415. {$IFNDEF OPENGL_ES}
  2416. fOpenGLFormat := tfXRGB4us1;
  2417. fglFormat := GL_BGRA;
  2418. fglInternalFormat := GL_RGB4;
  2419. fglDataFormat := GL_UNSIGNED_SHORT_4_4_4_4_REV;
  2420. {$ELSE}
  2421. fOpenGLFormat := tfR5G6B5us1;
  2422. {$ENDIF}
  2423. end;
  2424. procedure TfdR5G6B5us1.SetValues;
  2425. begin
  2426. inherited SetValues;
  2427. fBitsPerPixel := 16;
  2428. fFormat := tfR5G6B5us1;
  2429. fWithAlpha := tfRGB5A1us1;
  2430. fWithoutAlpha := tfR5G6B5us1;
  2431. fRGBInverted := tfB5G6R5us1;
  2432. fPrecision := glBitmapRec4ub( 5, 6, 5, 0);
  2433. fShift := glBitmapRec4ub(11, 5, 0, 0);
  2434. {$IF NOT DEFINED(OPENGL_ES) OR DEFINED(OPENGL_ES_2_0)}
  2435. fOpenGLFormat := tfR5G6B5us1;
  2436. fglFormat := GL_RGB;
  2437. fglInternalFormat := GL_RGB565;
  2438. fglDataFormat := GL_UNSIGNED_SHORT_5_6_5;
  2439. {$ELSE}
  2440. fOpenGLFormat := tfRGB8ub3;
  2441. {$IFEND}
  2442. end;
  2443. procedure TfdRGB5X1us1.SetValues;
  2444. begin
  2445. inherited SetValues;
  2446. fBitsPerPixel := 16;
  2447. fFormat := tfRGB5X1us1;
  2448. fWithAlpha := tfRGB5A1us1;
  2449. fWithoutAlpha := tfRGB5X1us1;
  2450. fRGBInverted := tfBGR5X1us1;
  2451. fPrecision := glBitmapRec4ub( 5, 5, 5, 0);
  2452. fShift := glBitmapRec4ub(11, 6, 1, 0);
  2453. {$IFNDEF OPENGL_ES}
  2454. fOpenGLFormat := tfRGB5X1us1;
  2455. fglFormat := GL_RGBA; //GL_INVALID_OPERATION if not GL_BGRA or GL_RGBA
  2456. fglInternalFormat := GL_RGB5;
  2457. fglDataFormat := GL_UNSIGNED_SHORT_5_5_5_1;
  2458. {$ELSE}
  2459. fOpenGLFormat := tfR5G6B5us1;
  2460. {$ENDIF}
  2461. end;
  2462. procedure TfdX1RGB5us1.SetValues;
  2463. begin
  2464. inherited SetValues;
  2465. fBitsPerPixel := 16;
  2466. fFormat := tfX1RGB5us1;
  2467. fWithAlpha := tfA1RGB5us1;
  2468. fWithoutAlpha := tfX1RGB5us1;
  2469. fRGBInverted := tfX1BGR5us1;
  2470. fPrecision := glBitmapRec4ub( 5, 5, 5, 0);
  2471. fShift := glBitmapRec4ub(10, 5, 0, 0);
  2472. {$IFNDEF OPENGL_ES}
  2473. fOpenGLFormat := tfX1RGB5us1;
  2474. fglFormat := GL_BGRA; //GL_INVALID_OPERATION if not GL_BGRA or GL_RGBA
  2475. fglInternalFormat := GL_RGB5;
  2476. fglDataFormat := GL_UNSIGNED_SHORT_1_5_5_5_REV;
  2477. {$ELSE}
  2478. fOpenGLFormat := tfR5G6B5us1;
  2479. {$ENDIF}
  2480. end;
  2481. procedure TfdRGB8ub3.SetValues;
  2482. begin
  2483. inherited SetValues;
  2484. fBitsPerPixel := 24;
  2485. fFormat := tfRGB8ub3;
  2486. fWithAlpha := tfRGBA8ub4;
  2487. fWithoutAlpha := tfRGB8ub3;
  2488. fRGBInverted := tfBGR8ub3;
  2489. fPrecision := glBitmapRec4ub(8, 8, 8, 0);
  2490. fShift := glBitmapRec4ub(0, 8, 16, 0);
  2491. fOpenGLFormat := tfRGB8ub3;
  2492. fglFormat := GL_RGB;
  2493. fglInternalFormat := {$IF NOT DEFINED(OPENGL_ES) OR DEFINED(OPENGL_ES_3_0)}GL_RGB8{$ELSE}GL_RGB{$IFEND};
  2494. fglDataFormat := GL_UNSIGNED_BYTE;
  2495. end;
  2496. procedure TfdRGBX8ui1.SetValues;
  2497. begin
  2498. inherited SetValues;
  2499. fBitsPerPixel := 32;
  2500. fFormat := tfRGBX8ui1;
  2501. fWithAlpha := tfRGBA8ui1;
  2502. fWithoutAlpha := tfRGBX8ui1;
  2503. fRGBInverted := tfBGRX8ui1;
  2504. fPrecision := glBitmapRec4ub( 8, 8, 8, 0);
  2505. fShift := glBitmapRec4ub(24, 16, 8, 0);
  2506. {$IFNDEF OPENGL_ES}
  2507. fOpenGLFormat := tfRGBX8ui1;
  2508. fglFormat := GL_RGBA; //GL_INVALID_OPERATION if not GL_BGRA or GL_RGBA
  2509. fglInternalFormat := GL_RGB8;
  2510. fglDataFormat := GL_UNSIGNED_INT_8_8_8_8;
  2511. {$ELSE}
  2512. fOpenGLFormat := tfRGB8ub3;
  2513. {$ENDIF}
  2514. end;
  2515. procedure TfdXRGB8ui1.SetValues;
  2516. begin
  2517. inherited SetValues;
  2518. fBitsPerPixel := 32;
  2519. fFormat := tfXRGB8ui1;
  2520. fWithAlpha := tfXRGB8ui1;
  2521. fWithoutAlpha := tfXRGB8ui1;
  2522. fOpenGLFormat := tfXRGB8ui1;
  2523. fRGBInverted := tfXBGR8ui1;
  2524. fPrecision := glBitmapRec4ub( 8, 8, 8, 0);
  2525. fShift := glBitmapRec4ub(16, 8, 0, 0);
  2526. {$IFNDEF OPENGL_ES}
  2527. fOpenGLFormat := tfXRGB8ui1;
  2528. fglFormat := GL_BGRA; //GL_INVALID_OPERATION if not GL_BGRA or GL_RGBA
  2529. fglInternalFormat := GL_RGB8;
  2530. fglDataFormat := GL_UNSIGNED_INT_8_8_8_8_REV;
  2531. {$ELSE}
  2532. fOpenGLFormat := tfRGB8ub3;
  2533. {$ENDIF}
  2534. end;
  2535. procedure TfdRGB10X2ui1.SetValues;
  2536. begin
  2537. inherited SetValues;
  2538. fBitsPerPixel := 32;
  2539. fFormat := tfRGB10X2ui1;
  2540. fWithAlpha := tfRGB10A2ui1;
  2541. fWithoutAlpha := tfRGB10X2ui1;
  2542. fRGBInverted := tfBGR10X2ui1;
  2543. fPrecision := glBitmapRec4ub(10, 10, 10, 0);
  2544. fShift := glBitmapRec4ub(22, 12, 2, 0);
  2545. {$IFNDEF OPENGL_ES}
  2546. fOpenGLFormat := tfRGB10X2ui1;
  2547. fglFormat := GL_RGBA; //GL_INVALID_OPERATION if not GL_BGRA or GL_RGBA
  2548. fglInternalFormat := GL_RGB10;
  2549. fglDataFormat := GL_UNSIGNED_INT_10_10_10_2;
  2550. {$ELSE}
  2551. fOpenGLFormat := tfRGB16us3;
  2552. {$ENDIF}
  2553. end;
  2554. procedure TfdX2RGB10ui1.SetValues;
  2555. begin
  2556. inherited SetValues;
  2557. fBitsPerPixel := 32;
  2558. fFormat := tfX2RGB10ui1;
  2559. fWithAlpha := tfA2RGB10ui1;
  2560. fWithoutAlpha := tfX2RGB10ui1;
  2561. fRGBInverted := tfX2BGR10ui1;
  2562. fPrecision := glBitmapRec4ub(10, 10, 10, 0);
  2563. fShift := glBitmapRec4ub(20, 10, 0, 0);
  2564. {$IFNDEF OPENGL_ES}
  2565. fOpenGLFormat := tfX2RGB10ui1;
  2566. fglFormat := GL_BGRA; //GL_INVALID_OPERATION if not GL_BGRA or GL_RGBA
  2567. fglInternalFormat := GL_RGB10;
  2568. fglDataFormat := GL_UNSIGNED_INT_2_10_10_10_REV;
  2569. {$ELSE}
  2570. fOpenGLFormat := tfRGB16us3;
  2571. {$ENDIF}
  2572. end;
  2573. procedure TfdRGB16us3.SetValues;
  2574. begin
  2575. inherited SetValues;
  2576. fBitsPerPixel := 48;
  2577. fFormat := tfRGB16us3;
  2578. fWithAlpha := tfRGBA16us4;
  2579. fWithoutAlpha := tfRGB16us3;
  2580. fRGBInverted := tfBGR16us3;
  2581. fPrecision := glBitmapRec4ub(16, 16, 16, 0);
  2582. fShift := glBitmapRec4ub( 0, 16, 32, 0);
  2583. {$IF NOT DEFINED(OPENGL_ES) OR DEFINED(OPENGL_ES_3_0)}
  2584. fOpenGLFormat := tfRGB16us3;
  2585. fglFormat := GL_RGB;
  2586. fglInternalFormat := {$IFNDEF OPENGL_ES}GL_RGB16{$ELSE}GL_RGB16UI{$ENDIF};
  2587. fglDataFormat := GL_UNSIGNED_SHORT;
  2588. {$ELSE}
  2589. fOpenGLFormat := tfRGB8ub3;
  2590. {$IFEND}
  2591. end;
  2592. procedure TfdRGBA4us1.SetValues;
  2593. begin
  2594. inherited SetValues;
  2595. fBitsPerPixel := 16;
  2596. fFormat := tfRGBA4us1;
  2597. fWithAlpha := tfRGBA4us1;
  2598. fWithoutAlpha := tfRGBX4us1;
  2599. fOpenGLFormat := tfRGBA4us1;
  2600. fRGBInverted := tfBGRA4us1;
  2601. fPrecision := glBitmapRec4ub( 4, 4, 4, 4);
  2602. fShift := glBitmapRec4ub(12, 8, 4, 0);
  2603. fglFormat := GL_RGBA;
  2604. fglInternalFormat := {$IF NOT DEFINED(OPENGL_ES) OR DEFINED(OPENGL_ES_3_0)}GL_RGBA8{$ELSE}GL_RGBA{$IFEND};
  2605. fglDataFormat := GL_UNSIGNED_SHORT_4_4_4_4;
  2606. end;
  2607. procedure TfdARGB4us1.SetValues;
  2608. begin
  2609. inherited SetValues;
  2610. fBitsPerPixel := 16;
  2611. fFormat := tfARGB4us1;
  2612. fWithAlpha := tfARGB4us1;
  2613. fWithoutAlpha := tfXRGB4us1;
  2614. fRGBInverted := tfABGR4us1;
  2615. fPrecision := glBitmapRec4ub( 4, 4, 4, 4);
  2616. fShift := glBitmapRec4ub( 8, 4, 0, 12);
  2617. {$IFNDEF OPENGL_ES}
  2618. fOpenGLFormat := tfARGB4us1;
  2619. fglFormat := GL_BGRA;
  2620. fglInternalFormat := GL_RGBA4;
  2621. fglDataFormat := GL_UNSIGNED_SHORT_4_4_4_4_REV;
  2622. {$ELSE}
  2623. fOpenGLFormat := tfRGBA4us1;
  2624. {$ENDIF}
  2625. end;
  2626. procedure TfdRGB5A1us1.SetValues;
  2627. begin
  2628. inherited SetValues;
  2629. fBitsPerPixel := 16;
  2630. fFormat := tfRGB5A1us1;
  2631. fWithAlpha := tfRGB5A1us1;
  2632. fWithoutAlpha := tfRGB5X1us1;
  2633. fOpenGLFormat := tfRGB5A1us1;
  2634. fRGBInverted := tfBGR5A1us1;
  2635. fPrecision := glBitmapRec4ub( 5, 5, 5, 1);
  2636. fShift := glBitmapRec4ub(11, 6, 1, 0);
  2637. fglFormat := GL_RGBA;
  2638. fglInternalFormat := {$IF NOT DEFINED(OPENGL_ES) OR DEFINED(OPENGL_ES_2_0)}GL_RGB5_A1{$ELSE}GL_RGBA{$IFEND};
  2639. fglDataFormat := GL_UNSIGNED_SHORT_5_5_5_1;
  2640. end;
  2641. procedure TfdA1RGB5us1.SetValues;
  2642. begin
  2643. inherited SetValues;
  2644. fBitsPerPixel := 16;
  2645. fFormat := tfA1RGB5us1;
  2646. fWithAlpha := tfA1RGB5us1;
  2647. fWithoutAlpha := tfX1RGB5us1;
  2648. fRGBInverted := tfA1BGR5us1;
  2649. fPrecision := glBitmapRec4ub( 5, 5, 5, 1);
  2650. fShift := glBitmapRec4ub(10, 5, 0, 15);
  2651. {$IFNDEF OPENGL_ES}
  2652. fOpenGLFormat := tfA1RGB5us1;
  2653. fglFormat := GL_BGRA;
  2654. fglInternalFormat := GL_RGB5_A1;
  2655. fglDataFormat := GL_UNSIGNED_SHORT_1_5_5_5_REV;
  2656. {$ELSE}
  2657. fOpenGLFormat := tfRGB5A1us1;
  2658. {$ENDIF}
  2659. end;
  2660. procedure TfdRGBA8ui1.SetValues;
  2661. begin
  2662. inherited SetValues;
  2663. fBitsPerPixel := 32;
  2664. fFormat := tfRGBA8ui1;
  2665. fWithAlpha := tfRGBA8ui1;
  2666. fWithoutAlpha := tfRGBX8ui1;
  2667. fRGBInverted := tfBGRA8ui1;
  2668. fPrecision := glBitmapRec4ub( 8, 8, 8, 8);
  2669. fShift := glBitmapRec4ub(24, 16, 8, 0);
  2670. {$IFNDEF OPENGL_ES}
  2671. fOpenGLFormat := tfRGBA8ui1;
  2672. fglFormat := GL_RGBA;
  2673. fglInternalFormat := GL_RGBA8;
  2674. fglDataFormat := GL_UNSIGNED_INT_8_8_8_8;
  2675. {$ELSE}
  2676. fOpenGLFormat := tfRGBA8ub4;
  2677. {$ENDIF}
  2678. end;
  2679. procedure TfdARGB8ui1.SetValues;
  2680. begin
  2681. inherited SetValues;
  2682. fBitsPerPixel := 32;
  2683. fFormat := tfARGB8ui1;
  2684. fWithAlpha := tfARGB8ui1;
  2685. fWithoutAlpha := tfXRGB8ui1;
  2686. fRGBInverted := tfABGR8ui1;
  2687. fPrecision := glBitmapRec4ub( 8, 8, 8, 8);
  2688. fShift := glBitmapRec4ub(16, 8, 0, 24);
  2689. {$IFNDEF OPENGL_ES}
  2690. fOpenGLFormat := tfARGB8ui1;
  2691. fglFormat := GL_BGRA;
  2692. fglInternalFormat := GL_RGBA8;
  2693. fglDataFormat := GL_UNSIGNED_INT_8_8_8_8_REV;
  2694. {$ELSE}
  2695. fOpenGLFormat := tfRGBA8ub4;
  2696. {$ENDIF}
  2697. end;
  2698. procedure TfdRGBA8ub4.SetValues;
  2699. begin
  2700. inherited SetValues;
  2701. fBitsPerPixel := 32;
  2702. fFormat := tfRGBA8ub4;
  2703. fWithAlpha := tfRGBA8ub4;
  2704. fWithoutAlpha := tfRGB8ub3;
  2705. fOpenGLFormat := tfRGBA8ub4;
  2706. fRGBInverted := tfBGRA8ub4;
  2707. fPrecision := glBitmapRec4ub( 8, 8, 8, 8);
  2708. fShift := glBitmapRec4ub( 0, 8, 16, 24);
  2709. fglFormat := GL_RGBA;
  2710. fglInternalFormat := {$IF NOT DEFINED(OPENGL_ES) OR DEFINED(OPENGL_ES_3_0)}GL_RGBA8{$ELSE}GL_RGBA{$IFEND};
  2711. fglDataFormat := GL_UNSIGNED_BYTE;
  2712. end;
  2713. procedure TfdRGB10A2ui1.SetValues;
  2714. begin
  2715. inherited SetValues;
  2716. fBitsPerPixel := 32;
  2717. fFormat := tfRGB10A2ui1;
  2718. fWithAlpha := tfRGB10A2ui1;
  2719. fWithoutAlpha := tfRGB10X2ui1;
  2720. fRGBInverted := tfBGR10A2ui1;
  2721. fPrecision := glBitmapRec4ub(10, 10, 10, 2);
  2722. fShift := glBitmapRec4ub(22, 12, 2, 0);
  2723. {$IFNDEF OPENGL_ES}
  2724. fOpenGLFormat := tfRGB10A2ui1;
  2725. fglFormat := GL_RGBA;
  2726. fglInternalFormat := GL_RGB10_A2;
  2727. fglDataFormat := GL_UNSIGNED_INT_10_10_10_2;
  2728. {$ELSE}
  2729. fOpenGLFormat := tfA2RGB10ui1;
  2730. {$ENDIF}
  2731. end;
  2732. procedure TfdA2RGB10ui1.SetValues;
  2733. begin
  2734. inherited SetValues;
  2735. fBitsPerPixel := 32;
  2736. fFormat := tfA2RGB10ui1;
  2737. fWithAlpha := tfA2RGB10ui1;
  2738. fWithoutAlpha := tfX2RGB10ui1;
  2739. fRGBInverted := tfA2BGR10ui1;
  2740. fPrecision := glBitmapRec4ub(10, 10, 10, 2);
  2741. fShift := glBitmapRec4ub(20, 10, 0, 30);
  2742. {$IF NOT DEFINED(OPENGL_ES)}
  2743. fOpenGLFormat := tfA2RGB10ui1;
  2744. fglFormat := GL_BGRA;
  2745. fglInternalFormat := GL_RGB10_A2;
  2746. fglDataFormat := GL_UNSIGNED_INT_2_10_10_10_REV;
  2747. {$ELSEIF DEFINED(OPENGL_ES_3_0)}
  2748. fOpenGLFormat := tfA2RGB10ui1;
  2749. fglFormat := GL_RGBA;
  2750. fglInternalFormat := GL_RGB10_A2;
  2751. fglDataFormat := GL_UNSIGNED_INT_2_10_10_10_REV;
  2752. {$ELSE}
  2753. fOpenGLFormat := tfRGBA8ui1;
  2754. {$IFEND}
  2755. end;
  2756. procedure TfdRGBA16us4.SetValues;
  2757. begin
  2758. inherited SetValues;
  2759. fBitsPerPixel := 64;
  2760. fFormat := tfRGBA16us4;
  2761. fWithAlpha := tfRGBA16us4;
  2762. fWithoutAlpha := tfRGB16us3;
  2763. fRGBInverted := tfBGRA16us4;
  2764. fPrecision := glBitmapRec4ub(16, 16, 16, 16);
  2765. fShift := glBitmapRec4ub( 0, 16, 32, 48);
  2766. {$IF NOT DEFINED(OPENGL_ES) OR DEFINED(OPENGL_ES_3_0)}
  2767. fOpenGLFormat := tfRGBA16us4;
  2768. fglFormat := GL_RGBA;
  2769. fglInternalFormat := {$IFNDEF OPENGL_ES}GL_RGBA16{$ELSE}GL_RGBA16UI{$ENDIF};
  2770. fglDataFormat := GL_UNSIGNED_SHORT;
  2771. {$ELSE}
  2772. fOpenGLFormat := tfRGBA8ub4;
  2773. {$IFEND}
  2774. end;
  2775. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2776. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2777. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2778. procedure TfdBGRX4us1.SetValues;
  2779. begin
  2780. inherited SetValues;
  2781. fBitsPerPixel := 16;
  2782. fFormat := tfBGRX4us1;
  2783. fWithAlpha := tfBGRA4us1;
  2784. fWithoutAlpha := tfBGRX4us1;
  2785. fRGBInverted := tfRGBX4us1;
  2786. fPrecision := glBitmapRec4ub( 4, 4, 4, 0);
  2787. fShift := glBitmapRec4ub( 4, 8, 12, 0);
  2788. {$IFNDEF OPENGL_ES}
  2789. fOpenGLFormat := tfBGRX4us1;
  2790. fglFormat := GL_BGRA; //GL_INVALID_OPERATION if not GL_BGRA or GL_RGBA
  2791. fglInternalFormat := GL_RGB4;
  2792. fglDataFormat := GL_UNSIGNED_SHORT_4_4_4_4;
  2793. {$ELSE}
  2794. fOpenGLFormat := tfR5G6B5us1;
  2795. {$ENDIF}
  2796. end;
  2797. procedure TfdXBGR4us1.SetValues;
  2798. begin
  2799. inherited SetValues;
  2800. fBitsPerPixel := 16;
  2801. fFormat := tfXBGR4us1;
  2802. fWithAlpha := tfABGR4us1;
  2803. fWithoutAlpha := tfXBGR4us1;
  2804. fRGBInverted := tfXRGB4us1;
  2805. fPrecision := glBitmapRec4ub( 4, 4, 4, 0);
  2806. fShift := glBitmapRec4ub( 0, 4, 8, 0);
  2807. {$IFNDEF OPENGL_ES}
  2808. fOpenGLFormat := tfXBGR4us1;
  2809. fglFormat := GL_RGBA; //GL_INVALID_OPERATION if not GL_BGRA or GL_RGBA
  2810. fglInternalFormat := GL_RGB4;
  2811. fglDataFormat := GL_UNSIGNED_SHORT_4_4_4_4_REV;
  2812. {$ELSE}
  2813. fOpenGLFormat := tfR5G6B5us1;
  2814. {$ENDIF}
  2815. end;
  2816. procedure TfdB5G6R5us1.SetValues;
  2817. begin
  2818. inherited SetValues;
  2819. fBitsPerPixel := 16;
  2820. fFormat := tfB5G6R5us1;
  2821. fWithAlpha := tfBGR5A1us1;
  2822. fWithoutAlpha := tfB5G6R5us1;
  2823. fRGBInverted := tfR5G6B5us1;
  2824. fPrecision := glBitmapRec4ub( 5, 6, 5, 0);
  2825. fShift := glBitmapRec4ub( 0, 5, 11, 0);
  2826. {$IFNDEF OPENGL_ES}
  2827. fOpenGLFormat := tfB5G6R5us1;
  2828. fglFormat := GL_RGB;
  2829. fglInternalFormat := GL_RGB565;
  2830. fglDataFormat := GL_UNSIGNED_SHORT_5_6_5_REV;
  2831. {$ELSE}
  2832. fOpenGLFormat := tfR5G6B5us1;
  2833. {$ENDIF}
  2834. end;
  2835. procedure TfdBGR5X1us1.SetValues;
  2836. begin
  2837. inherited SetValues;
  2838. fBitsPerPixel := 16;
  2839. fFormat := tfBGR5X1us1;
  2840. fWithAlpha := tfBGR5A1us1;
  2841. fWithoutAlpha := tfBGR5X1us1;
  2842. fRGBInverted := tfRGB5X1us1;
  2843. fPrecision := glBitmapRec4ub( 5, 5, 5, 0);
  2844. fShift := glBitmapRec4ub( 1, 6, 11, 0);
  2845. {$IFNDEF OPENGL_ES}
  2846. fOpenGLFormat := tfBGR5X1us1;
  2847. fglFormat := GL_BGRA;
  2848. fglInternalFormat := GL_RGB5;
  2849. fglDataFormat := GL_UNSIGNED_SHORT_5_5_5_1;
  2850. {$ELSE}
  2851. fOpenGLFormat := tfR5G6B5us1;
  2852. {$ENDIF}
  2853. end;
  2854. procedure TfdX1BGR5us1.SetValues;
  2855. begin
  2856. inherited SetValues;
  2857. fBitsPerPixel := 16;
  2858. fFormat := tfX1BGR5us1;
  2859. fWithAlpha := tfA1BGR5us1;
  2860. fWithoutAlpha := tfX1BGR5us1;
  2861. fRGBInverted := tfX1RGB5us1;
  2862. fPrecision := glBitmapRec4ub( 5, 5, 5, 0);
  2863. fShift := glBitmapRec4ub( 0, 5, 10, 0);
  2864. {$IFNDEF OPENGL_ES}
  2865. fOpenGLFormat := tfX1BGR5us1;
  2866. fglFormat := GL_RGBA; //GL_INVALID_OPERATION if not GL_BGRA or GL_RGBA
  2867. fglInternalFormat := GL_RGB5;
  2868. fglDataFormat := GL_UNSIGNED_SHORT_1_5_5_5_REV;
  2869. {$ELSE}
  2870. fOpenGLFormat := tfR5G6B5us1;
  2871. {$ENDIF}
  2872. end;
  2873. procedure TfdBGR8ub3.SetValues;
  2874. begin
  2875. inherited SetValues;
  2876. fBitsPerPixel := 24;
  2877. fFormat := tfBGR8ub3;
  2878. fWithAlpha := tfBGRA8ub4;
  2879. fWithoutAlpha := tfBGR8ub3;
  2880. fRGBInverted := tfRGB8ub3;
  2881. fPrecision := glBitmapRec4ub( 8, 8, 8, 0);
  2882. fShift := glBitmapRec4ub(16, 8, 0, 0);
  2883. {$IFNDEF OPENGL_ES}
  2884. fOpenGLFormat := tfBGR8ub3;
  2885. fglFormat := GL_BGR;
  2886. fglInternalFormat := GL_RGB8;
  2887. fglDataFormat := GL_UNSIGNED_BYTE;
  2888. {$ELSE}
  2889. fOpenGLFormat := tfRGB8ub3;
  2890. {$ENDIF}
  2891. end;
  2892. procedure TfdBGRX8ui1.SetValues;
  2893. begin
  2894. inherited SetValues;
  2895. fBitsPerPixel := 32;
  2896. fFormat := tfBGRX8ui1;
  2897. fWithAlpha := tfBGRA8ui1;
  2898. fWithoutAlpha := tfBGRX8ui1;
  2899. fRGBInverted := tfRGBX8ui1;
  2900. fPrecision := glBitmapRec4ub( 8, 8, 8, 0);
  2901. fShift := glBitmapRec4ub( 8, 16, 24, 0);
  2902. {$IFNDEF OPENGL_ES}
  2903. fOpenGLFormat := tfBGRX8ui1;
  2904. fglFormat := GL_BGRA; //GL_INVALID_OPERATION if not GL_BGRA or GL_RGBA
  2905. fglInternalFormat := GL_RGB8;
  2906. fglDataFormat := GL_UNSIGNED_INT_8_8_8_8;
  2907. {$ELSE}
  2908. fOpenGLFormat := tfRGB8ub3;
  2909. {$ENDIF}
  2910. end;
  2911. procedure TfdXBGR8ui1.SetValues;
  2912. begin
  2913. inherited SetValues;
  2914. fBitsPerPixel := 32;
  2915. fFormat := tfXBGR8ui1;
  2916. fWithAlpha := tfABGR8ui1;
  2917. fWithoutAlpha := tfXBGR8ui1;
  2918. fRGBInverted := tfXRGB8ui1;
  2919. fPrecision := glBitmapRec4ub( 8, 8, 8, 0);
  2920. fShift := glBitmapRec4ub( 0, 8, 16, 0);
  2921. {$IFNDEF OPENGL_ES}
  2922. fOpenGLFormat := tfXBGR8ui1;
  2923. fglFormat := GL_RGBA; //GL_INVALID_OPERATION if not GL_BGRA or GL_RGBA
  2924. fglInternalFormat := GL_RGB8;
  2925. fglDataFormat := GL_UNSIGNED_INT_8_8_8_8_REV;
  2926. {$ELSE}
  2927. fOpenGLFormat := tfRGB8ub3;
  2928. {$ENDIF}
  2929. end;
  2930. procedure TfdBGR10X2ui1.SetValues;
  2931. begin
  2932. inherited SetValues;
  2933. fBitsPerPixel := 32;
  2934. fFormat := tfBGR10X2ui1;
  2935. fWithAlpha := tfBGR10A2ui1;
  2936. fWithoutAlpha := tfBGR10X2ui1;
  2937. fRGBInverted := tfRGB10X2ui1;
  2938. fPrecision := glBitmapRec4ub(10, 10, 10, 0);
  2939. fShift := glBitmapRec4ub( 2, 12, 22, 0);
  2940. {$IFNDEF OPENGL_ES}
  2941. fOpenGLFormat := tfBGR10X2ui1;
  2942. fglFormat := GL_BGRA; //GL_INVALID_OPERATION if not GL_BGRA or GL_RGBA
  2943. fglInternalFormat := GL_RGB10;
  2944. fglDataFormat := GL_UNSIGNED_INT_10_10_10_2;
  2945. {$ELSE}
  2946. fOpenGLFormat := tfRGB16us3;
  2947. {$ENDIF}
  2948. end;
  2949. procedure TfdX2BGR10ui1.SetValues;
  2950. begin
  2951. inherited SetValues;
  2952. fBitsPerPixel := 32;
  2953. fFormat := tfX2BGR10ui1;
  2954. fWithAlpha := tfA2BGR10ui1;
  2955. fWithoutAlpha := tfX2BGR10ui1;
  2956. fRGBInverted := tfX2RGB10ui1;
  2957. fPrecision := glBitmapRec4ub(10, 10, 10, 0);
  2958. fShift := glBitmapRec4ub( 0, 10, 20, 0);
  2959. {$IFNDEF OPENGL_ES}
  2960. fOpenGLFormat := tfX2BGR10ui1;
  2961. fglFormat := GL_RGBA; //GL_INVALID_OPERATION if not GL_BGRA or GL_RGBA
  2962. fglInternalFormat := GL_RGB10;
  2963. fglDataFormat := GL_UNSIGNED_INT_2_10_10_10_REV;
  2964. {$ELSE}
  2965. fOpenGLFormat := tfRGB16us3;
  2966. {$ENDIF}
  2967. end;
  2968. procedure TfdBGR16us3.SetValues;
  2969. begin
  2970. inherited SetValues;
  2971. fBitsPerPixel := 48;
  2972. fFormat := tfBGR16us3;
  2973. fWithAlpha := tfBGRA16us4;
  2974. fWithoutAlpha := tfBGR16us3;
  2975. fRGBInverted := tfRGB16us3;
  2976. fPrecision := glBitmapRec4ub(16, 16, 16, 0);
  2977. fShift := glBitmapRec4ub(32, 16, 0, 0);
  2978. {$IFNDEF OPENGL_ES}
  2979. fOpenGLFormat := tfBGR16us3;
  2980. fglFormat := GL_BGR;
  2981. fglInternalFormat := GL_RGB16;
  2982. fglDataFormat := GL_UNSIGNED_SHORT;
  2983. {$ELSE}
  2984. fOpenGLFormat := tfRGB16us3;
  2985. {$ENDIF}
  2986. end;
  2987. procedure TfdBGRA4us1.SetValues;
  2988. begin
  2989. inherited SetValues;
  2990. fBitsPerPixel := 16;
  2991. fFormat := tfBGRA4us1;
  2992. fWithAlpha := tfBGRA4us1;
  2993. fWithoutAlpha := tfBGRX4us1;
  2994. fRGBInverted := tfRGBA4us1;
  2995. fPrecision := glBitmapRec4ub( 4, 4, 4, 4);
  2996. fShift := glBitmapRec4ub( 4, 8, 12, 0);
  2997. {$IFNDEF OPENGL_ES}
  2998. fOpenGLFormat := tfBGRA4us1;
  2999. fglFormat := GL_BGRA;
  3000. fglInternalFormat := GL_RGBA4;
  3001. fglDataFormat := GL_UNSIGNED_SHORT_4_4_4_4;
  3002. {$ELSE}
  3003. fOpenGLFormat := tfRGBA4us1;
  3004. {$ENDIF}
  3005. end;
  3006. procedure TfdABGR4us1.SetValues;
  3007. begin
  3008. inherited SetValues;
  3009. fBitsPerPixel := 16;
  3010. fFormat := tfABGR4us1;
  3011. fWithAlpha := tfABGR4us1;
  3012. fWithoutAlpha := tfXBGR4us1;
  3013. fRGBInverted := tfARGB4us1;
  3014. fPrecision := glBitmapRec4ub( 4, 4, 4, 4);
  3015. fShift := glBitmapRec4ub( 0, 4, 8, 12);
  3016. {$IFNDEF OPENGL_ES}
  3017. fOpenGLFormat := tfABGR4us1;
  3018. fglFormat := GL_RGBA;
  3019. fglInternalFormat := GL_RGBA4;
  3020. fglDataFormat := GL_UNSIGNED_SHORT_4_4_4_4_REV;
  3021. {$ELSE}
  3022. fOpenGLFormat := tfRGBA4us1;
  3023. {$ENDIF}
  3024. end;
  3025. procedure TfdBGR5A1us1.SetValues;
  3026. begin
  3027. inherited SetValues;
  3028. fBitsPerPixel := 16;
  3029. fFormat := tfBGR5A1us1;
  3030. fWithAlpha := tfBGR5A1us1;
  3031. fWithoutAlpha := tfBGR5X1us1;
  3032. fRGBInverted := tfRGB5A1us1;
  3033. fPrecision := glBitmapRec4ub( 5, 5, 5, 1);
  3034. fShift := glBitmapRec4ub( 1, 6, 11, 0);
  3035. {$IFNDEF OPENGL_ES}
  3036. fOpenGLFormat := tfBGR5A1us1;
  3037. fglFormat := GL_BGRA;
  3038. fglInternalFormat := GL_RGB5_A1;
  3039. fglDataFormat := GL_UNSIGNED_SHORT_5_5_5_1;
  3040. {$ELSE}
  3041. fOpenGLFormat := tfRGB5A1us1;
  3042. {$ENDIF}
  3043. end;
  3044. procedure TfdA1BGR5us1.SetValues;
  3045. begin
  3046. inherited SetValues;
  3047. fBitsPerPixel := 16;
  3048. fFormat := tfA1BGR5us1;
  3049. fWithAlpha := tfA1BGR5us1;
  3050. fWithoutAlpha := tfX1BGR5us1;
  3051. fRGBInverted := tfA1RGB5us1;
  3052. fPrecision := glBitmapRec4ub( 5, 5, 5, 1);
  3053. fShift := glBitmapRec4ub( 0, 5, 10, 15);
  3054. {$IFNDEF OPENGL_ES}
  3055. fOpenGLFormat := tfA1BGR5us1;
  3056. fglFormat := GL_RGBA;
  3057. fglInternalFormat := GL_RGB5_A1;
  3058. fglDataFormat := GL_UNSIGNED_SHORT_1_5_5_5_REV;
  3059. {$ELSE}
  3060. fOpenGLFormat := tfRGB5A1us1;
  3061. {$ENDIF}
  3062. end;
  3063. procedure TfdBGRA8ui1.SetValues;
  3064. begin
  3065. inherited SetValues;
  3066. fBitsPerPixel := 32;
  3067. fFormat := tfBGRA8ui1;
  3068. fWithAlpha := tfBGRA8ui1;
  3069. fWithoutAlpha := tfBGRX8ui1;
  3070. fRGBInverted := tfRGBA8ui1;
  3071. fPrecision := glBitmapRec4ub( 8, 8, 8, 8);
  3072. fShift := glBitmapRec4ub( 8, 16, 24, 0);
  3073. {$IFNDEF OPENGL_ES}
  3074. fOpenGLFormat := tfBGRA8ui1;
  3075. fglFormat := GL_BGRA;
  3076. fglInternalFormat := GL_RGBA8;
  3077. fglDataFormat := GL_UNSIGNED_INT_8_8_8_8;
  3078. {$ELSE}
  3079. fOpenGLFormat := tfRGBA8ub4;
  3080. {$ENDIF}
  3081. end;
  3082. procedure TfdABGR8ui1.SetValues;
  3083. begin
  3084. inherited SetValues;
  3085. fBitsPerPixel := 32;
  3086. fFormat := tfABGR8ui1;
  3087. fWithAlpha := tfABGR8ui1;
  3088. fWithoutAlpha := tfXBGR8ui1;
  3089. fRGBInverted := tfARGB8ui1;
  3090. fPrecision := glBitmapRec4ub( 8, 8, 8, 8);
  3091. fShift := glBitmapRec4ub( 0, 8, 16, 24);
  3092. {$IFNDEF OPENGL_ES}
  3093. fOpenGLFormat := tfABGR8ui1;
  3094. fglFormat := GL_RGBA;
  3095. fglInternalFormat := GL_RGBA8;
  3096. fglDataFormat := GL_UNSIGNED_INT_8_8_8_8_REV;
  3097. {$ELSE}
  3098. fOpenGLFormat := tfRGBA8ub4
  3099. {$ENDIF}
  3100. end;
  3101. procedure TfdBGRA8ub4.SetValues;
  3102. begin
  3103. inherited SetValues;
  3104. fBitsPerPixel := 32;
  3105. fFormat := tfBGRA8ub4;
  3106. fWithAlpha := tfBGRA8ub4;
  3107. fWithoutAlpha := tfBGR8ub3;
  3108. fRGBInverted := tfRGBA8ub4;
  3109. fPrecision := glBitmapRec4ub( 8, 8, 8, 8);
  3110. fShift := glBitmapRec4ub(16, 8, 0, 24);
  3111. {$IFNDEF OPENGL_ES}
  3112. fOpenGLFormat := tfBGRA8ub4;
  3113. fglFormat := GL_BGRA;
  3114. fglInternalFormat := GL_RGBA8;
  3115. fglDataFormat := GL_UNSIGNED_BYTE;
  3116. {$ELSE}
  3117. fOpenGLFormat := tfRGBA8ub4;
  3118. {$ENDIF}
  3119. end;
  3120. procedure TfdBGR10A2ui1.SetValues;
  3121. begin
  3122. inherited SetValues;
  3123. fBitsPerPixel := 32;
  3124. fFormat := tfBGR10A2ui1;
  3125. fWithAlpha := tfBGR10A2ui1;
  3126. fWithoutAlpha := tfBGR10X2ui1;
  3127. fRGBInverted := tfRGB10A2ui1;
  3128. fPrecision := glBitmapRec4ub(10, 10, 10, 2);
  3129. fShift := glBitmapRec4ub( 2, 12, 22, 0);
  3130. {$IFNDEF OPENGL_ES}
  3131. fOpenGLFormat := tfBGR10A2ui1;
  3132. fglFormat := GL_BGRA;
  3133. fglInternalFormat := GL_RGB10_A2;
  3134. fglDataFormat := GL_UNSIGNED_INT_10_10_10_2;
  3135. {$ELSE}
  3136. fOpenGLFormat := tfA2RGB10ui1;
  3137. {$ENDIF}
  3138. end;
  3139. procedure TfdA2BGR10ui1.SetValues;
  3140. begin
  3141. inherited SetValues;
  3142. fBitsPerPixel := 32;
  3143. fFormat := tfA2BGR10ui1;
  3144. fWithAlpha := tfA2BGR10ui1;
  3145. fWithoutAlpha := tfX2BGR10ui1;
  3146. fRGBInverted := tfA2RGB10ui1;
  3147. fPrecision := glBitmapRec4ub(10, 10, 10, 2);
  3148. fShift := glBitmapRec4ub( 0, 10, 20, 30);
  3149. {$IFNDEF OPENGL_ES}
  3150. fOpenGLFormat := tfA2BGR10ui1;
  3151. fglFormat := GL_RGBA;
  3152. fglInternalFormat := GL_RGB10_A2;
  3153. fglDataFormat := GL_UNSIGNED_INT_2_10_10_10_REV;
  3154. {$ELSE}
  3155. fOpenGLFormat := tfA2RGB10ui1;
  3156. {$ENDIF}
  3157. end;
  3158. procedure TfdBGRA16us4.SetValues;
  3159. begin
  3160. inherited SetValues;
  3161. fBitsPerPixel := 64;
  3162. fFormat := tfBGRA16us4;
  3163. fWithAlpha := tfBGRA16us4;
  3164. fWithoutAlpha := tfBGR16us3;
  3165. fRGBInverted := tfRGBA16us4;
  3166. fPrecision := glBitmapRec4ub(16, 16, 16, 16);
  3167. fShift := glBitmapRec4ub(32, 16, 0, 48);
  3168. {$IFNDEF OPENGL_ES}
  3169. fOpenGLFormat := tfBGRA16us4;
  3170. fglFormat := GL_BGRA;
  3171. fglInternalFormat := GL_RGBA16;
  3172. fglDataFormat := GL_UNSIGNED_SHORT;
  3173. {$ELSE}
  3174. fOpenGLFormat := tfRGBA16us4;
  3175. {$ENDIF}
  3176. end;
  3177. procedure TfdDepth16us1.SetValues;
  3178. begin
  3179. inherited SetValues;
  3180. fBitsPerPixel := 16;
  3181. fFormat := tfDepth16us1;
  3182. fWithoutAlpha := tfDepth16us1;
  3183. fPrecision := glBitmapRec4ub(16, 16, 16, 16);
  3184. fShift := glBitmapRec4ub( 0, 0, 0, 0);
  3185. {$IF NOT DEFINED (OPENGL_ES) OR DEFINED(OPENGL_ES_2_0)}
  3186. fOpenGLFormat := tfDepth16us1;
  3187. fglFormat := GL_DEPTH_COMPONENT;
  3188. fglInternalFormat := GL_DEPTH_COMPONENT16;
  3189. fglDataFormat := GL_UNSIGNED_SHORT;
  3190. {$IFEND}
  3191. end;
  3192. procedure TfdDepth24ui1.SetValues;
  3193. begin
  3194. inherited SetValues;
  3195. fBitsPerPixel := 32;
  3196. fFormat := tfDepth24ui1;
  3197. fWithoutAlpha := tfDepth24ui1;
  3198. fOpenGLFormat := tfDepth24ui1;
  3199. fPrecision := glBitmapRec4ub(32, 32, 32, 32);
  3200. fShift := glBitmapRec4ub( 0, 0, 0, 0);
  3201. {$IF NOT DEFINED (OPENGL_ES) OR DEFINED(OPENGL_ES_3_0)}
  3202. fOpenGLFormat := tfDepth24ui1;
  3203. fglFormat := GL_DEPTH_COMPONENT;
  3204. fglInternalFormat := GL_DEPTH_COMPONENT24;
  3205. fglDataFormat := GL_UNSIGNED_INT;
  3206. {$IFEND}
  3207. end;
  3208. procedure TfdDepth32ui1.SetValues;
  3209. begin
  3210. inherited SetValues;
  3211. fBitsPerPixel := 32;
  3212. fFormat := tfDepth32ui1;
  3213. fWithoutAlpha := tfDepth32ui1;
  3214. fPrecision := glBitmapRec4ub(32, 32, 32, 32);
  3215. fShift := glBitmapRec4ub( 0, 0, 0, 0);
  3216. {$IF NOT DEFINED(OPENGL_ES)}
  3217. fOpenGLFormat := tfDepth32ui1;
  3218. fglFormat := GL_DEPTH_COMPONENT;
  3219. fglInternalFormat := GL_DEPTH_COMPONENT32;
  3220. fglDataFormat := GL_UNSIGNED_INT;
  3221. {$ELSEIF DEFINED(OPENGL_ES_3_0)}
  3222. fOpenGLFormat := tfDepth24ui1;
  3223. {$ELSEIF DEFINED(OPENGL_ES_2_0)}
  3224. fOpenGLFormat := tfDepth16us1;
  3225. {$IFEND}
  3226. end;
  3227. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3228. //TfdS3tcDtx1RGBA/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3229. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3230. procedure TfdS3tcDtx1RGBA.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  3231. begin
  3232. raise EglBitmap.Create('mapping for compressed formats is not supported');
  3233. end;
  3234. procedure TfdS3tcDtx1RGBA.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  3235. begin
  3236. raise EglBitmap.Create('mapping for compressed formats is not supported');
  3237. end;
  3238. procedure TfdS3tcDtx1RGBA.SetValues;
  3239. begin
  3240. inherited SetValues;
  3241. fFormat := tfS3tcDtx1RGBA;
  3242. fWithAlpha := tfS3tcDtx1RGBA;
  3243. fUncompressed := tfRGB5A1us1;
  3244. fBitsPerPixel := 4;
  3245. fIsCompressed := true;
  3246. {$IFNDEF OPENGL_ES}
  3247. fOpenGLFormat := tfS3tcDtx1RGBA;
  3248. fglFormat := GL_COMPRESSED_RGBA;
  3249. fglInternalFormat := GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
  3250. fglDataFormat := GL_UNSIGNED_BYTE;
  3251. {$ELSE}
  3252. fOpenGLFormat := fUncompressed;
  3253. {$ENDIF}
  3254. end;
  3255. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3256. //TfdS3tcDtx3RGBA/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3257. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3258. procedure TfdS3tcDtx3RGBA.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  3259. begin
  3260. raise EglBitmap.Create('mapping for compressed formats is not supported');
  3261. end;
  3262. procedure TfdS3tcDtx3RGBA.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  3263. begin
  3264. raise EglBitmap.Create('mapping for compressed formats is not supported');
  3265. end;
  3266. procedure TfdS3tcDtx3RGBA.SetValues;
  3267. begin
  3268. inherited SetValues;
  3269. fFormat := tfS3tcDtx3RGBA;
  3270. fWithAlpha := tfS3tcDtx3RGBA;
  3271. fUncompressed := tfRGBA8ub4;
  3272. fBitsPerPixel := 8;
  3273. fIsCompressed := true;
  3274. {$IFNDEF OPENGL_ES}
  3275. fOpenGLFormat := tfS3tcDtx3RGBA;
  3276. fglFormat := GL_COMPRESSED_RGBA;
  3277. fglInternalFormat := GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
  3278. fglDataFormat := GL_UNSIGNED_BYTE;
  3279. {$ELSE}
  3280. fOpenGLFormat := fUncompressed;
  3281. {$ENDIF}
  3282. end;
  3283. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3284. //TfdS3tcDtx5RGBA/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3285. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3286. procedure TfdS3tcDtx5RGBA.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  3287. begin
  3288. raise EglBitmap.Create('mapping for compressed formats is not supported');
  3289. end;
  3290. procedure TfdS3tcDtx5RGBA.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  3291. begin
  3292. raise EglBitmap.Create('mapping for compressed formats is not supported');
  3293. end;
  3294. procedure TfdS3tcDtx5RGBA.SetValues;
  3295. begin
  3296. inherited SetValues;
  3297. fFormat := tfS3tcDtx3RGBA;
  3298. fWithAlpha := tfS3tcDtx3RGBA;
  3299. fUncompressed := tfRGBA8ub4;
  3300. fBitsPerPixel := 8;
  3301. fIsCompressed := true;
  3302. {$IFNDEF OPENGL_ES}
  3303. fOpenGLFormat := tfS3tcDtx3RGBA;
  3304. fglFormat := GL_COMPRESSED_RGBA;
  3305. fglInternalFormat := GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
  3306. fglDataFormat := GL_UNSIGNED_BYTE;
  3307. {$ELSE}
  3308. fOpenGLFormat := fUncompressed;
  3309. {$ENDIF}
  3310. end;
  3311. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3312. //TglBitmapFormatDescriptor///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3313. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3314. function TglBitmapFormatDescriptor.GetHasRed: Boolean;
  3315. begin
  3316. result := (fPrecision.r > 0);
  3317. end;
  3318. function TglBitmapFormatDescriptor.GetHasGreen: Boolean;
  3319. begin
  3320. result := (fPrecision.g > 0);
  3321. end;
  3322. function TglBitmapFormatDescriptor.GetHasBlue: Boolean;
  3323. begin
  3324. result := (fPrecision.b > 0);
  3325. end;
  3326. function TglBitmapFormatDescriptor.GetHasAlpha: Boolean;
  3327. begin
  3328. result := (fPrecision.a > 0);
  3329. end;
  3330. function TglBitmapFormatDescriptor.GetHasColor: Boolean;
  3331. begin
  3332. result := HasRed or HasGreen or HasBlue;
  3333. end;
  3334. function TglBitmapFormatDescriptor.GetIsGrayscale: Boolean;
  3335. begin
  3336. result := (Mask.r = Mask.g) and (Mask.g = Mask.b) and (Mask.r > 0);
  3337. end;
  3338. function TglBitmapFormatDescriptor.GetHasOpenGLSupport: Boolean;
  3339. begin
  3340. result := (OpenGLFormat = Format);
  3341. end;
  3342. procedure TglBitmapFormatDescriptor.SetValues;
  3343. begin
  3344. fFormat := tfEmpty;
  3345. fWithAlpha := tfEmpty;
  3346. fWithoutAlpha := tfEmpty;
  3347. fOpenGLFormat := tfEmpty;
  3348. fRGBInverted := tfEmpty;
  3349. fUncompressed := tfEmpty;
  3350. fBitsPerPixel := 0;
  3351. fIsCompressed := false;
  3352. fglFormat := 0;
  3353. fglInternalFormat := 0;
  3354. fglDataFormat := 0;
  3355. FillChar(fPrecision, 0, SizeOf(fPrecision));
  3356. FillChar(fShift, 0, SizeOf(fShift));
  3357. end;
  3358. procedure TglBitmapFormatDescriptor.CalcValues;
  3359. var
  3360. i: Integer;
  3361. begin
  3362. fBytesPerPixel := fBitsPerPixel / 8;
  3363. fChannelCount := 0;
  3364. for i := 0 to 3 do begin
  3365. if (fPrecision.arr[i] > 0) then
  3366. inc(fChannelCount);
  3367. fRange.arr[i] := (1 shl fPrecision.arr[i]) - 1;
  3368. fMask.arr[i] := fRange.arr[i] shl fShift.arr[i];
  3369. end;
  3370. end;
  3371. function TglBitmapFormatDescriptor.GetSize(const aSize: TglBitmapSize): Integer;
  3372. var
  3373. w, h: Integer;
  3374. begin
  3375. if (ffX in aSize.Fields) or (ffY in aSize.Fields) then begin
  3376. w := Max(1, aSize.X);
  3377. h := Max(1, aSize.Y);
  3378. result := GetSize(w, h);
  3379. end else
  3380. result := 0;
  3381. end;
  3382. function TglBitmapFormatDescriptor.GetSize(const aWidth, aHeight: Integer): Integer;
  3383. begin
  3384. result := 0;
  3385. if (aWidth <= 0) or (aHeight <= 0) then
  3386. exit;
  3387. result := Ceil(aWidth * aHeight * BytesPerPixel);
  3388. end;
  3389. constructor TglBitmapFormatDescriptor.Create;
  3390. begin
  3391. inherited Create;
  3392. SetValues;
  3393. CalcValues;
  3394. end;
  3395. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3396. class function TglBitmapFormatDescriptor.GetByFormat(const aInternalFormat: GLenum): TglBitmapFormatDescriptor;
  3397. var
  3398. f: TglBitmapFormat;
  3399. begin
  3400. for f := Low(TglBitmapFormat) to High(TglBitmapFormat) do begin
  3401. result := TFormatDescriptor.Get(f);
  3402. if (result.glInternalFormat = aInternalFormat) then
  3403. exit;
  3404. end;
  3405. result := TFormatDescriptor.Get(tfEmpty);
  3406. end;
  3407. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3408. //TFormatDescriptor///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3409. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3410. class procedure TFormatDescriptor.Init;
  3411. begin
  3412. if not Assigned(FormatDescriptorCS) then
  3413. FormatDescriptorCS := TCriticalSection.Create;
  3414. end;
  3415. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3416. class function TFormatDescriptor.Get(const aFormat: TglBitmapFormat): TFormatDescriptor;
  3417. begin
  3418. FormatDescriptorCS.Enter;
  3419. try
  3420. result := FormatDescriptors[aFormat];
  3421. if not Assigned(result) then begin
  3422. result := FORMAT_DESCRIPTOR_CLASSES[aFormat].Create;
  3423. FormatDescriptors[aFormat] := result;
  3424. end;
  3425. finally
  3426. FormatDescriptorCS.Leave;
  3427. end;
  3428. end;
  3429. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3430. class function TFormatDescriptor.GetAlpha(const aFormat: TglBitmapFormat): TFormatDescriptor;
  3431. begin
  3432. result := Get(Get(aFormat).WithAlpha);
  3433. end;
  3434. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3435. class function TFormatDescriptor.GetFromMask(const aMask: TglBitmapRec4ul; const aBitCount: Integer): TFormatDescriptor;
  3436. var
  3437. ft: TglBitmapFormat;
  3438. begin
  3439. // find matching format with OpenGL support
  3440. for ft := High(TglBitmapFormat) downto Low(TglBitmapFormat) do begin
  3441. result := Get(ft);
  3442. if (result.MaskMatch(aMask)) and
  3443. (result.glFormat <> 0) and
  3444. (result.glInternalFormat <> 0) and
  3445. ((aBitCount = 0) or (aBitCount = result.BitsPerPixel))
  3446. then
  3447. exit;
  3448. end;
  3449. // find matching format without OpenGL Support
  3450. for ft := High(TglBitmapFormat) downto Low(TglBitmapFormat) do begin
  3451. result := Get(ft);
  3452. if result.MaskMatch(aMask) and ((aBitCount = 0) or (aBitCount = result.BitsPerPixel)) then
  3453. exit;
  3454. end;
  3455. result := TFormatDescriptor.Get(tfEmpty);
  3456. end;
  3457. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3458. class function TFormatDescriptor.GetFromPrecShift(const aPrec, aShift: TglBitmapRec4ub; const aBitCount: Integer): TFormatDescriptor;
  3459. var
  3460. ft: TglBitmapFormat;
  3461. begin
  3462. // find matching format with OpenGL support
  3463. for ft := High(TglBitmapFormat) downto Low(TglBitmapFormat) do begin
  3464. result := Get(ft);
  3465. if glBitmapRec4ubCompare(result.Shift, aShift) and
  3466. glBitmapRec4ubCompare(result.Precision, aPrec) and
  3467. (result.glFormat <> 0) and
  3468. (result.glInternalFormat <> 0) and
  3469. ((aBitCount = 0) or (aBitCount = result.BitsPerPixel))
  3470. then
  3471. exit;
  3472. end;
  3473. // find matching format without OpenGL Support
  3474. for ft := High(TglBitmapFormat) downto Low(TglBitmapFormat) do begin
  3475. result := Get(ft);
  3476. if glBitmapRec4ubCompare(result.Shift, aShift) and
  3477. glBitmapRec4ubCompare(result.Precision, aPrec) and
  3478. ((aBitCount = 0) or (aBitCount = result.BitsPerPixel)) then
  3479. exit;
  3480. end;
  3481. result := TFormatDescriptor.Get(tfEmpty);
  3482. end;
  3483. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3484. class procedure TFormatDescriptor.Clear;
  3485. var
  3486. f: TglBitmapFormat;
  3487. begin
  3488. FormatDescriptorCS.Enter;
  3489. try
  3490. for f := low(FormatDescriptors) to high(FormatDescriptors) do
  3491. FreeAndNil(FormatDescriptors[f]);
  3492. finally
  3493. FormatDescriptorCS.Leave;
  3494. end;
  3495. end;
  3496. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3497. class procedure TFormatDescriptor.Finalize;
  3498. begin
  3499. Clear;
  3500. FreeAndNil(FormatDescriptorCS);
  3501. end;
  3502. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3503. //TBitfieldFormat/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3504. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3505. procedure TbmpBitfieldFormat.SetCustomValues(const aBPP: Integer; aMask: TglBitmapRec4ul);
  3506. var
  3507. i: Integer;
  3508. begin
  3509. for i := 0 to 3 do begin
  3510. fShift.arr[i] := 0;
  3511. while (aMask.arr[i] > 0) and ((aMask.arr[i] and 1) = 0) do begin
  3512. aMask.arr[i] := aMask.arr[i] shr 1;
  3513. inc(fShift.arr[i]);
  3514. end;
  3515. fPrecision.arr[i] := CountSetBits(aMask.arr[i]);
  3516. end;
  3517. fBitsPerPixel := aBPP;
  3518. CalcValues;
  3519. end;
  3520. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3521. procedure TbmpBitfieldFormat.SetCustomValues(const aBBP: Integer; const aPrec, aShift: TglBitmapRec4ub);
  3522. begin
  3523. fBitsPerPixel := aBBP;
  3524. fPrecision := aPrec;
  3525. fShift := aShift;
  3526. CalcValues;
  3527. end;
  3528. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3529. procedure TbmpBitfieldFormat.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  3530. var
  3531. data: QWord;
  3532. begin
  3533. data :=
  3534. ((aPixel.Data.r and Range.r) shl Shift.r) or
  3535. ((aPixel.Data.g and Range.g) shl Shift.g) or
  3536. ((aPixel.Data.b and Range.b) shl Shift.b) or
  3537. ((aPixel.Data.a and Range.a) shl Shift.a);
  3538. case BitsPerPixel of
  3539. 8: aData^ := data;
  3540. 16: PWord(aData)^ := data;
  3541. 32: PCardinal(aData)^ := data;
  3542. 64: PQWord(aData)^ := data;
  3543. else
  3544. raise EglBitmap.CreateFmt('invalid pixel size: %d', [BitsPerPixel]);
  3545. end;
  3546. inc(aData, Round(BytesPerPixel));
  3547. end;
  3548. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3549. procedure TbmpBitfieldFormat.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  3550. var
  3551. data: QWord;
  3552. i: Integer;
  3553. begin
  3554. case BitsPerPixel of
  3555. 8: data := aData^;
  3556. 16: data := PWord(aData)^;
  3557. 32: data := PCardinal(aData)^;
  3558. 64: data := PQWord(aData)^;
  3559. else
  3560. raise EglBitmap.CreateFmt('invalid pixel size: %d', [BitsPerPixel]);
  3561. end;
  3562. for i := 0 to 3 do
  3563. aPixel.Data.arr[i] := (data shr fShift.arr[i]) and Range.arr[i];
  3564. inc(aData, Round(BytesPerPixel));
  3565. end;
  3566. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3567. //TColorTableFormat///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3568. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3569. procedure TbmpColorTableFormat.SetValues;
  3570. begin
  3571. inherited SetValues;
  3572. fShift := glBitmapRec4ub(8, 8, 8, 0);
  3573. end;
  3574. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3575. procedure TbmpColorTableFormat.SetCustomValues(const aFormat: TglBitmapFormat; const aBPP: Integer; const aPrec, aShift: TglBitmapRec4ub);
  3576. begin
  3577. fFormat := aFormat;
  3578. fBitsPerPixel := aBPP;
  3579. fPrecision := aPrec;
  3580. fShift := aShift;
  3581. CalcValues;
  3582. end;
  3583. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3584. procedure TbmpColorTableFormat.CalcValues;
  3585. begin
  3586. inherited CalcValues;
  3587. end;
  3588. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3589. procedure TbmpColorTableFormat.CreateColorTable;
  3590. var
  3591. i: Integer;
  3592. begin
  3593. SetLength(fColorTable, 256);
  3594. if not HasColor then begin
  3595. // alpha
  3596. for i := 0 to High(fColorTable) do begin
  3597. fColorTable[i].r := Round(((i shr Shift.a) and Range.a) / Range.a * 255);
  3598. fColorTable[i].g := Round(((i shr Shift.a) and Range.a) / Range.a * 255);
  3599. fColorTable[i].b := Round(((i shr Shift.a) and Range.a) / Range.a * 255);
  3600. fColorTable[i].a := 0;
  3601. end;
  3602. end else begin
  3603. // normal
  3604. for i := 0 to High(fColorTable) do begin
  3605. fColorTable[i].r := Round(((i shr Shift.r) and Range.r) / Range.r * 255);
  3606. fColorTable[i].g := Round(((i shr Shift.g) and Range.g) / Range.g * 255);
  3607. fColorTable[i].b := Round(((i shr Shift.b) and Range.b) / Range.b * 255);
  3608. fColorTable[i].a := 0;
  3609. end;
  3610. end;
  3611. end;
  3612. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3613. function TbmpColorTableFormat.CreateMappingData: Pointer;
  3614. begin
  3615. result := Pointer(0);
  3616. end;
  3617. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3618. procedure TbmpColorTableFormat.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  3619. begin
  3620. if (BitsPerPixel <> 8) then
  3621. raise EglBitmapUnsupportedFormat.Create('color table are only supported for 8bit formats');
  3622. if not HasColor then
  3623. // alpha
  3624. aData^ := aPixel.Data.a
  3625. else
  3626. // normal
  3627. aData^ := Round(
  3628. ((aPixel.Data.r shr Shift.r) and Range.r) * LUMINANCE_WEIGHT_R +
  3629. ((aPixel.Data.g shr Shift.g) and Range.g) * LUMINANCE_WEIGHT_G +
  3630. ((aPixel.Data.b shr Shift.b) and Range.b) * LUMINANCE_WEIGHT_B);
  3631. inc(aData);
  3632. end;
  3633. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3634. procedure TbmpColorTableFormat.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  3635. function ReadValue: Byte;
  3636. var
  3637. i: PtrUInt;
  3638. begin
  3639. if (BitsPerPixel = 8) then begin
  3640. result := aData^;
  3641. inc(aData);
  3642. end else begin
  3643. i := {%H-}PtrUInt(aMapData);
  3644. if (BitsPerPixel > 1) then
  3645. result := (aData^ shr i) and ((1 shl BitsPerPixel) - 1)
  3646. else
  3647. result := (aData^ shr (7-i)) and ((1 shl BitsPerPixel) - 1);
  3648. inc(i, BitsPerPixel);
  3649. while (i >= 8) do begin
  3650. inc(aData);
  3651. dec(i, 8);
  3652. end;
  3653. aMapData := {%H-}Pointer(i);
  3654. end;
  3655. end;
  3656. begin
  3657. if (BitsPerPixel > 8) then
  3658. raise EglBitmapUnsupportedFormat.Create('color table are only supported for 8bit formats');
  3659. with fColorTable[ReadValue] do begin
  3660. aPixel.Data.r := r;
  3661. aPixel.Data.g := g;
  3662. aPixel.Data.b := b;
  3663. aPixel.Data.a := a;
  3664. end;
  3665. end;
  3666. destructor TbmpColorTableFormat.Destroy;
  3667. begin
  3668. SetLength(fColorTable, 0);
  3669. inherited Destroy;
  3670. end;
  3671. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3672. //TglBitmap - Helper//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3673. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3674. procedure glBitmapConvertPixel(var aPixel: TglBitmapPixelData; const aSourceFD, aDestFD: TFormatDescriptor);
  3675. var
  3676. i: Integer;
  3677. begin
  3678. for i := 0 to 3 do begin
  3679. if (aSourceFD.Range.arr[i] <> aDestFD.Range.arr[i]) then begin
  3680. if (aSourceFD.Range.arr[i] > 0) then
  3681. aPixel.Data.arr[i] := Round(aPixel.Data.arr[i] / aSourceFD.Range.arr[i] * aDestFD.Range.arr[i])
  3682. else
  3683. aPixel.Data.arr[i] := 0;
  3684. end;
  3685. end;
  3686. end;
  3687. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3688. procedure glBitmapConvertCopyFunc(var aFuncRec: TglBitmapFunctionRec);
  3689. begin
  3690. with aFuncRec do begin
  3691. if (Source.Range.r > 0) then
  3692. Dest.Data.r := Source.Data.r;
  3693. if (Source.Range.g > 0) then
  3694. Dest.Data.g := Source.Data.g;
  3695. if (Source.Range.b > 0) then
  3696. Dest.Data.b := Source.Data.b;
  3697. if (Source.Range.a > 0) then
  3698. Dest.Data.a := Source.Data.a;
  3699. end;
  3700. end;
  3701. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3702. procedure glBitmapConvertCalculateRGBAFunc(var aFuncRec: TglBitmapFunctionRec);
  3703. var
  3704. i: Integer;
  3705. begin
  3706. with aFuncRec do begin
  3707. for i := 0 to 3 do
  3708. if (Source.Range.arr[i] > 0) then
  3709. Dest.Data.arr[i] := Round(Dest.Range.arr[i] * Source.Data.arr[i] / Source.Range.arr[i]);
  3710. end;
  3711. end;
  3712. type
  3713. TShiftData = packed record
  3714. case Integer of
  3715. 0: (r, g, b, a: SmallInt);
  3716. 1: (arr: array[0..3] of SmallInt);
  3717. end;
  3718. PShiftData = ^TShiftData;
  3719. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3720. procedure glBitmapConvertShiftRGBAFunc(var aFuncRec: TglBitmapFunctionRec);
  3721. var
  3722. i: Integer;
  3723. begin
  3724. with aFuncRec do
  3725. for i := 0 to 3 do
  3726. if (Source.Range.arr[i] > 0) then
  3727. Dest.Data.arr[i] := Source.Data.arr[i] shr PShiftData(Args)^.arr[i];
  3728. end;
  3729. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3730. procedure glBitmapInvertFunc(var aFuncRec: TglBitmapFunctionRec);
  3731. var
  3732. i: Integer;
  3733. begin
  3734. with aFuncRec do begin
  3735. Dest.Data := Source.Data;
  3736. for i := 0 to 3 do
  3737. if ({%H-}PtrUInt(Args) and (1 shl i) > 0) then
  3738. Dest.Data.arr[i] := Dest.Data.arr[i] xor Dest.Range.arr[i];
  3739. end;
  3740. end;
  3741. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3742. procedure glBitmapFillWithColorFunc(var aFuncRec: TglBitmapFunctionRec);
  3743. var
  3744. i: Integer;
  3745. begin
  3746. with aFuncRec do begin
  3747. for i := 0 to 3 do
  3748. Dest.Data.arr[i] := PglBitmapPixelData(Args)^.Data.arr[i];
  3749. end;
  3750. end;
  3751. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3752. procedure glBitmapAlphaFunc(var FuncRec: TglBitmapFunctionRec);
  3753. var
  3754. Temp: Single;
  3755. begin
  3756. with FuncRec do begin
  3757. if (FuncRec.Args = nil) then begin //source has no alpha
  3758. Temp :=
  3759. Source.Data.r / Source.Range.r * ALPHA_WEIGHT_R +
  3760. Source.Data.g / Source.Range.g * ALPHA_WEIGHT_G +
  3761. Source.Data.b / Source.Range.b * ALPHA_WEIGHT_B;
  3762. Dest.Data.a := Round(Dest.Range.a * Temp);
  3763. end else
  3764. Dest.Data.a := Round(Source.Data.a / Source.Range.a * Dest.Range.a);
  3765. end;
  3766. end;
  3767. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3768. procedure glBitmapColorKeyAlphaFunc(var FuncRec: TglBitmapFunctionRec);
  3769. type
  3770. PglBitmapPixelData = ^TglBitmapPixelData;
  3771. begin
  3772. with FuncRec do begin
  3773. Dest.Data.r := Source.Data.r;
  3774. Dest.Data.g := Source.Data.g;
  3775. Dest.Data.b := Source.Data.b;
  3776. with PglBitmapPixelData(Args)^ do
  3777. if ((Dest.Data.r <= Data.r) and (Dest.Data.r >= Range.r) and
  3778. (Dest.Data.g <= Data.g) and (Dest.Data.g >= Range.g) and
  3779. (Dest.Data.b <= Data.b) and (Dest.Data.b >= Range.b)) then
  3780. Dest.Data.a := 0
  3781. else
  3782. Dest.Data.a := Dest.Range.a;
  3783. end;
  3784. end;
  3785. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3786. procedure glBitmapValueAlphaFunc(var FuncRec: TglBitmapFunctionRec);
  3787. begin
  3788. with FuncRec do begin
  3789. Dest.Data.r := Source.Data.r;
  3790. Dest.Data.g := Source.Data.g;
  3791. Dest.Data.b := Source.Data.b;
  3792. Dest.Data.a := PCardinal(Args)^;
  3793. end;
  3794. end;
  3795. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3796. procedure SwapRGB(aData: PByte; aWidth: Integer; const aHasAlpha: Boolean);
  3797. type
  3798. PRGBPix = ^TRGBPix;
  3799. TRGBPix = array [0..2] of byte;
  3800. var
  3801. Temp: Byte;
  3802. begin
  3803. while aWidth > 0 do begin
  3804. Temp := PRGBPix(aData)^[0];
  3805. PRGBPix(aData)^[0] := PRGBPix(aData)^[2];
  3806. PRGBPix(aData)^[2] := Temp;
  3807. if aHasAlpha then
  3808. Inc(aData, 4)
  3809. else
  3810. Inc(aData, 3);
  3811. dec(aWidth);
  3812. end;
  3813. end;
  3814. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3815. //TglBitmapData///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3816. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3817. function TglBitmapData.GetFormatDescriptor: TglBitmapFormatDescriptor;
  3818. begin
  3819. result := TFormatDescriptor.Get(fFormat);
  3820. end;
  3821. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3822. function TglBitmapData.GetWidth: Integer;
  3823. begin
  3824. if (ffX in fDimension.Fields) then
  3825. result := fDimension.X
  3826. else
  3827. result := -1;
  3828. end;
  3829. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3830. function TglBitmapData.GetHeight: Integer;
  3831. begin
  3832. if (ffY in fDimension.Fields) then
  3833. result := fDimension.Y
  3834. else
  3835. result := -1;
  3836. end;
  3837. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3838. function TglBitmapData.GetScanlines(const aIndex: Integer): PByte;
  3839. begin
  3840. if fHasScanlines and (aIndex >= Low(fScanlines)) and (aIndex <= High(fScanlines)) then
  3841. result := fScanlines[aIndex]
  3842. else
  3843. result := nil;
  3844. end;
  3845. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3846. procedure TglBitmapData.SetFormat(const aValue: TglBitmapFormat);
  3847. begin
  3848. if fFormat = aValue then
  3849. exit;
  3850. if TFormatDescriptor.Get(Format).BitsPerPixel <> TFormatDescriptor.Get(aValue).BitsPerPixel then
  3851. raise EglBitmapUnsupportedFormat.Create(Format);
  3852. SetData(fData, aValue, Width, Height);
  3853. end;
  3854. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3855. procedure TglBitmapData.PrepareResType(var aResource: String; var aResType: PChar);
  3856. var
  3857. TempPos: Integer;
  3858. begin
  3859. if not Assigned(aResType) then begin
  3860. TempPos := Pos('.', aResource);
  3861. aResType := PChar(UpperCase(Copy(aResource, TempPos + 1, Length(aResource) - TempPos)));
  3862. aResource := UpperCase(Copy(aResource, 0, TempPos -1));
  3863. end;
  3864. end;
  3865. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3866. procedure TglBitmapData.UpdateScanlines;
  3867. var
  3868. w, h, i, LineWidth: Integer;
  3869. begin
  3870. w := Width;
  3871. h := Height;
  3872. fHasScanlines := Assigned(fData) and (w > 0) and (h > 0);
  3873. if fHasScanlines then begin
  3874. SetLength(fScanlines, h);
  3875. LineWidth := Trunc(w * FormatDescriptor.BytesPerPixel);
  3876. for i := 0 to h-1 do begin
  3877. fScanlines[i] := fData;
  3878. Inc(fScanlines[i], i * LineWidth);
  3879. end;
  3880. end else
  3881. SetLength(fScanlines, 0);
  3882. end;
  3883. {$IFDEF GLB_SUPPORT_PNG_READ}
  3884. {$IF DEFINED(GLB_LAZ_PNG)}
  3885. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3886. //PNG/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3887. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3888. function TglBitmapData.LoadPNG(const aStream: TStream): Boolean;
  3889. const
  3890. MAGIC_LEN = 8;
  3891. PNG_MAGIC: String[MAGIC_LEN] = #$89#$50#$4E#$47#$0D#$0A#$1A#$0A;
  3892. var
  3893. reader: TLazReaderPNG;
  3894. intf: TLazIntfImage;
  3895. StreamPos: Int64;
  3896. magic: String[MAGIC_LEN];
  3897. begin
  3898. result := true;
  3899. StreamPos := aStream.Position;
  3900. SetLength(magic, MAGIC_LEN);
  3901. aStream.Read(magic[1], MAGIC_LEN);
  3902. aStream.Position := StreamPos;
  3903. if (magic <> PNG_MAGIC) then begin
  3904. result := false;
  3905. exit;
  3906. end;
  3907. intf := TLazIntfImage.Create(0, 0);
  3908. reader := TLazReaderPNG.Create;
  3909. try try
  3910. reader.UpdateDescription := true;
  3911. reader.ImageRead(aStream, intf);
  3912. AssignFromLazIntfImage(intf);
  3913. except
  3914. result := false;
  3915. aStream.Position := StreamPos;
  3916. exit;
  3917. end;
  3918. finally
  3919. reader.Free;
  3920. intf.Free;
  3921. end;
  3922. end;
  3923. {$ELSEIF DEFINED(GLB_SDL_IMAGE)}
  3924. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3925. function TglBitmapData.LoadPNG(const aStream: TStream): Boolean;
  3926. var
  3927. Surface: PSDL_Surface;
  3928. RWops: PSDL_RWops;
  3929. begin
  3930. result := false;
  3931. RWops := glBitmapCreateRWops(aStream);
  3932. try
  3933. if IMG_isPNG(RWops) > 0 then begin
  3934. Surface := IMG_LoadPNG_RW(RWops);
  3935. try
  3936. AssignFromSurface(Surface);
  3937. result := true;
  3938. finally
  3939. SDL_FreeSurface(Surface);
  3940. end;
  3941. end;
  3942. finally
  3943. SDL_FreeRW(RWops);
  3944. end;
  3945. end;
  3946. {$ELSEIF DEFINED(GLB_LIB_PNG)}
  3947. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3948. procedure glBitmap_libPNG_read_func(png: png_structp; buffer: png_bytep; size: cardinal); cdecl;
  3949. begin
  3950. TStream(png_get_io_ptr(png)).Read(buffer^, size);
  3951. end;
  3952. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3953. function TglBitmapData.LoadPNG(const aStream: TStream): Boolean;
  3954. var
  3955. StreamPos: Int64;
  3956. signature: array [0..7] of byte;
  3957. png: png_structp;
  3958. png_info: png_infop;
  3959. TempHeight, TempWidth: Integer;
  3960. Format: TglBitmapFormat;
  3961. png_data: pByte;
  3962. png_rows: array of pByte;
  3963. Row, LineSize: Integer;
  3964. begin
  3965. result := false;
  3966. if not init_libPNG then
  3967. raise Exception.Create('LoadPNG - unable to initialize libPNG.');
  3968. try
  3969. // signature
  3970. StreamPos := aStream.Position;
  3971. aStream.Read(signature{%H-}, 8);
  3972. aStream.Position := StreamPos;
  3973. if png_check_sig(@signature, 8) <> 0 then begin
  3974. // png read struct
  3975. png := png_create_read_struct(PNG_LIBPNG_VER_STRING, nil, nil, nil);
  3976. if png = nil then
  3977. raise EglBitmapException.Create('LoadPng - couldn''t create read struct.');
  3978. // png info
  3979. png_info := png_create_info_struct(png);
  3980. if png_info = nil then begin
  3981. png_destroy_read_struct(@png, nil, nil);
  3982. raise EglBitmapException.Create('LoadPng - couldn''t create info struct.');
  3983. end;
  3984. // set read callback
  3985. png_set_read_fn(png, aStream, glBitmap_libPNG_read_func);
  3986. // read informations
  3987. png_read_info(png, png_info);
  3988. // size
  3989. TempHeight := png_get_image_height(png, png_info);
  3990. TempWidth := png_get_image_width(png, png_info);
  3991. // format
  3992. case png_get_color_type(png, png_info) of
  3993. PNG_COLOR_TYPE_GRAY:
  3994. Format := tfLuminance8ub1;
  3995. PNG_COLOR_TYPE_GRAY_ALPHA:
  3996. Format := tfLuminance8Alpha8us1;
  3997. PNG_COLOR_TYPE_RGB:
  3998. Format := tfRGB8ub3;
  3999. PNG_COLOR_TYPE_RGB_ALPHA:
  4000. Format := tfRGBA8ub4;
  4001. else
  4002. raise EglBitmapException.Create ('LoadPng - Unsupported Colortype found.');
  4003. end;
  4004. // cut upper 8 bit from 16 bit formats
  4005. if png_get_bit_depth(png, png_info) > 8 then
  4006. png_set_strip_16(png);
  4007. // expand bitdepth smaller than 8
  4008. if png_get_bit_depth(png, png_info) < 8 then
  4009. png_set_expand(png);
  4010. // allocating mem for scanlines
  4011. LineSize := png_get_rowbytes(png, png_info);
  4012. GetMem(png_data, TempHeight * LineSize);
  4013. try
  4014. SetLength(png_rows, TempHeight);
  4015. for Row := Low(png_rows) to High(png_rows) do begin
  4016. png_rows[Row] := png_data;
  4017. Inc(png_rows[Row], Row * LineSize);
  4018. end;
  4019. // read complete image into scanlines
  4020. png_read_image(png, @png_rows[0]);
  4021. // read end
  4022. png_read_end(png, png_info);
  4023. // destroy read struct
  4024. png_destroy_read_struct(@png, @png_info, nil);
  4025. SetLength(png_rows, 0);
  4026. // set new data
  4027. SetData(png_data, Format, TempWidth, TempHeight);
  4028. result := true;
  4029. except
  4030. if Assigned(png_data) then
  4031. FreeMem(png_data);
  4032. raise;
  4033. end;
  4034. end;
  4035. finally
  4036. quit_libPNG;
  4037. end;
  4038. end;
  4039. {$ELSEIF DEFINED(GLB_PNGIMAGE)}
  4040. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4041. function TglBitmapData.LoadPNG(const aStream: TStream): Boolean;
  4042. var
  4043. StreamPos: Int64;
  4044. Png: TPNGObject;
  4045. Header: String[8];
  4046. Row, Col, PixSize, LineSize: Integer;
  4047. NewImage, pSource, pDest, pAlpha: pByte;
  4048. PngFormat: TglBitmapFormat;
  4049. FormatDesc: TFormatDescriptor;
  4050. const
  4051. PngHeader: String[8] = #137#80#78#71#13#10#26#10;
  4052. begin
  4053. result := false;
  4054. StreamPos := aStream.Position;
  4055. aStream.Read(Header[0], SizeOf(Header));
  4056. aStream.Position := StreamPos;
  4057. {Test if the header matches}
  4058. if Header = PngHeader then begin
  4059. Png := TPNGObject.Create;
  4060. try
  4061. Png.LoadFromStream(aStream);
  4062. case Png.Header.ColorType of
  4063. COLOR_GRAYSCALE:
  4064. PngFormat := tfLuminance8ub1;
  4065. COLOR_GRAYSCALEALPHA:
  4066. PngFormat := tfLuminance8Alpha8us1;
  4067. COLOR_RGB:
  4068. PngFormat := tfBGR8ub3;
  4069. COLOR_RGBALPHA:
  4070. PngFormat := tfBGRA8ub4;
  4071. else
  4072. raise EglBitmapException.Create ('LoadPng - Unsupported Colortype found.');
  4073. end;
  4074. FormatDesc := TFormatDescriptor.Get(PngFormat);
  4075. PixSize := Round(FormatDesc.PixelSize);
  4076. LineSize := FormatDesc.GetSize(Png.Header.Width, 1);
  4077. GetMem(NewImage, LineSize * Integer(Png.Header.Height));
  4078. try
  4079. pDest := NewImage;
  4080. case Png.Header.ColorType of
  4081. COLOR_RGB, COLOR_GRAYSCALE:
  4082. begin
  4083. for Row := 0 to Png.Height -1 do begin
  4084. Move (Png.Scanline[Row]^, pDest^, LineSize);
  4085. Inc(pDest, LineSize);
  4086. end;
  4087. end;
  4088. COLOR_RGBALPHA, COLOR_GRAYSCALEALPHA:
  4089. begin
  4090. PixSize := PixSize -1;
  4091. for Row := 0 to Png.Height -1 do begin
  4092. pSource := Png.Scanline[Row];
  4093. pAlpha := pByte(Png.AlphaScanline[Row]);
  4094. for Col := 0 to Png.Width -1 do begin
  4095. Move (pSource^, pDest^, PixSize);
  4096. Inc(pSource, PixSize);
  4097. Inc(pDest, PixSize);
  4098. pDest^ := pAlpha^;
  4099. inc(pAlpha);
  4100. Inc(pDest);
  4101. end;
  4102. end;
  4103. end;
  4104. else
  4105. raise EglBitmapException.Create ('LoadPng - Unsupported Colortype found.');
  4106. end;
  4107. SetData(NewImage, PngFormat, Png.Header.Width, Png.Header.Height);
  4108. result := true;
  4109. except
  4110. if Assigned(NewImage) then
  4111. FreeMem(NewImage);
  4112. raise;
  4113. end;
  4114. finally
  4115. Png.Free;
  4116. end;
  4117. end;
  4118. end;
  4119. {$IFEND}
  4120. {$ENDIF}
  4121. {$IFDEF GLB_SUPPORT_PNG_WRITE}
  4122. {$IFDEF GLB_LIB_PNG}
  4123. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4124. procedure glBitmap_libPNG_write_func(png: png_structp; buffer: png_bytep; size: cardinal); cdecl;
  4125. begin
  4126. TStream(png_get_io_ptr(png)).Write(buffer^, size);
  4127. end;
  4128. {$ENDIF}
  4129. {$IF DEFINED(GLB_LAZ_PNG)}
  4130. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4131. procedure TglBitmapData.SavePNG(const aStream: TStream);
  4132. var
  4133. png: TPortableNetworkGraphic;
  4134. intf: TLazIntfImage;
  4135. raw: TRawImage;
  4136. begin
  4137. png := TPortableNetworkGraphic.Create;
  4138. intf := TLazIntfImage.Create(0, 0);
  4139. try
  4140. if not AssignToLazIntfImage(intf) then
  4141. raise EglBitmap.Create('unable to create LazIntfImage from glBitmap');
  4142. intf.GetRawImage(raw);
  4143. png.LoadFromRawImage(raw, false);
  4144. png.SaveToStream(aStream);
  4145. finally
  4146. png.Free;
  4147. intf.Free;
  4148. end;
  4149. end;
  4150. {$ELSEIF DEFINED(GLB_LIB_PNG)}
  4151. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4152. procedure TglBitmapData.SavePNG(const aStream: TStream);
  4153. var
  4154. png: png_structp;
  4155. png_info: png_infop;
  4156. png_rows: array of pByte;
  4157. LineSize: Integer;
  4158. ColorType: Integer;
  4159. Row: Integer;
  4160. FormatDesc: TFormatDescriptor;
  4161. begin
  4162. if not (ftPNG in FormatGetSupportedFiles(Format)) then
  4163. raise EglBitmapUnsupportedFormat.Create(Format);
  4164. if not init_libPNG then
  4165. raise Exception.Create('unable to initialize libPNG.');
  4166. try
  4167. case Format of
  4168. tfAlpha8ub1, tfLuminance8ub1:
  4169. ColorType := PNG_COLOR_TYPE_GRAY;
  4170. tfLuminance8Alpha8us1:
  4171. ColorType := PNG_COLOR_TYPE_GRAY_ALPHA;
  4172. tfBGR8ub3, tfRGB8ub3:
  4173. ColorType := PNG_COLOR_TYPE_RGB;
  4174. tfBGRA8ub4, tfRGBA8ub4:
  4175. ColorType := PNG_COLOR_TYPE_RGBA;
  4176. else
  4177. raise EglBitmapUnsupportedFormat.Create(Format);
  4178. end;
  4179. FormatDesc := TFormatDescriptor.Get(Format);
  4180. LineSize := FormatDesc.GetSize(Width, 1);
  4181. // creating array for scanline
  4182. SetLength(png_rows, Height);
  4183. try
  4184. for Row := 0 to Height - 1 do begin
  4185. png_rows[Row] := Data;
  4186. Inc(png_rows[Row], Row * LineSize)
  4187. end;
  4188. // write struct
  4189. png := png_create_write_struct(PNG_LIBPNG_VER_STRING, nil, nil, nil);
  4190. if png = nil then
  4191. raise EglBitmapException.Create('SavePng - couldn''t create write struct.');
  4192. // create png info
  4193. png_info := png_create_info_struct(png);
  4194. if png_info = nil then begin
  4195. png_destroy_write_struct(@png, nil);
  4196. raise EglBitmapException.Create('SavePng - couldn''t create info struct.');
  4197. end;
  4198. // set read callback
  4199. png_set_write_fn(png, aStream, glBitmap_libPNG_write_func, nil);
  4200. // set compression
  4201. png_set_compression_level(png, 6);
  4202. if Format in [tfBGR8ub3, tfBGRA8ub4] then
  4203. png_set_bgr(png);
  4204. png_set_IHDR(png, png_info, Width, Height, 8, ColorType, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
  4205. png_write_info(png, png_info);
  4206. png_write_image(png, @png_rows[0]);
  4207. png_write_end(png, png_info);
  4208. png_destroy_write_struct(@png, @png_info);
  4209. finally
  4210. SetLength(png_rows, 0);
  4211. end;
  4212. finally
  4213. quit_libPNG;
  4214. end;
  4215. end;
  4216. {$ELSEIF DEFINED(GLB_PNGIMAGE)}
  4217. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4218. procedure TglBitmapData.SavePNG(const aStream: TStream);
  4219. var
  4220. Png: TPNGObject;
  4221. pSource, pDest: pByte;
  4222. X, Y, PixSize: Integer;
  4223. ColorType: Cardinal;
  4224. Alpha: Boolean;
  4225. pTemp: pByte;
  4226. Temp: Byte;
  4227. begin
  4228. if not (ftPNG in FormatGetSupportedFiles (Format)) then
  4229. raise EglBitmapUnsupportedFormat.Create(Format);
  4230. case Format of
  4231. tfAlpha8ub1, tfLuminance8ub1: begin
  4232. ColorType := COLOR_GRAYSCALE;
  4233. PixSize := 1;
  4234. Alpha := false;
  4235. end;
  4236. tfLuminance8Alpha8us1: begin
  4237. ColorType := COLOR_GRAYSCALEALPHA;
  4238. PixSize := 1;
  4239. Alpha := true;
  4240. end;
  4241. tfBGR8ub3, tfRGB8ub3: begin
  4242. ColorType := COLOR_RGB;
  4243. PixSize := 3;
  4244. Alpha := false;
  4245. end;
  4246. tfBGRA8ub4, tfRGBA8ub4: begin
  4247. ColorType := COLOR_RGBALPHA;
  4248. PixSize := 3;
  4249. Alpha := true
  4250. end;
  4251. else
  4252. raise EglBitmapUnsupportedFormat.Create(Format);
  4253. end;
  4254. Png := TPNGObject.CreateBlank(ColorType, 8, Width, Height);
  4255. try
  4256. // Copy ImageData
  4257. pSource := Data;
  4258. for Y := 0 to Height -1 do begin
  4259. pDest := png.ScanLine[Y];
  4260. for X := 0 to Width -1 do begin
  4261. Move(pSource^, pDest^, PixSize);
  4262. Inc(pDest, PixSize);
  4263. Inc(pSource, PixSize);
  4264. if Alpha then begin
  4265. png.AlphaScanline[Y]^[X] := pSource^;
  4266. Inc(pSource);
  4267. end;
  4268. end;
  4269. // convert RGB line to BGR
  4270. if Format in [tfRGB8ub3, tfRGBA8ub4] then begin
  4271. pTemp := png.ScanLine[Y];
  4272. for X := 0 to Width -1 do begin
  4273. Temp := pByteArray(pTemp)^[0];
  4274. pByteArray(pTemp)^[0] := pByteArray(pTemp)^[2];
  4275. pByteArray(pTemp)^[2] := Temp;
  4276. Inc(pTemp, 3);
  4277. end;
  4278. end;
  4279. end;
  4280. // Save to Stream
  4281. Png.CompressionLevel := 6;
  4282. Png.SaveToStream(aStream);
  4283. finally
  4284. FreeAndNil(Png);
  4285. end;
  4286. end;
  4287. {$IFEND}
  4288. {$ENDIF}
  4289. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4290. //JPEG////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4291. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4292. {$IFDEF GLB_LIB_JPEG}
  4293. type
  4294. glBitmap_libJPEG_source_mgr_ptr = ^glBitmap_libJPEG_source_mgr;
  4295. glBitmap_libJPEG_source_mgr = record
  4296. pub: jpeg_source_mgr;
  4297. SrcStream: TStream;
  4298. SrcBuffer: array [1..4096] of byte;
  4299. end;
  4300. glBitmap_libJPEG_dest_mgr_ptr = ^glBitmap_libJPEG_dest_mgr;
  4301. glBitmap_libJPEG_dest_mgr = record
  4302. pub: jpeg_destination_mgr;
  4303. DestStream: TStream;
  4304. DestBuffer: array [1..4096] of byte;
  4305. end;
  4306. procedure glBitmap_libJPEG_error_exit(cinfo: j_common_ptr); cdecl;
  4307. begin
  4308. //DUMMY
  4309. end;
  4310. procedure glBitmap_libJPEG_output_message(cinfo: j_common_ptr); cdecl;
  4311. begin
  4312. //DUMMY
  4313. end;
  4314. procedure glBitmap_libJPEG_init_source(cinfo: j_decompress_ptr); cdecl;
  4315. begin
  4316. //DUMMY
  4317. end;
  4318. procedure glBitmap_libJPEG_term_source(cinfo: j_decompress_ptr); cdecl;
  4319. begin
  4320. //DUMMY
  4321. end;
  4322. procedure glBitmap_libJPEG_init_destination(cinfo: j_compress_ptr); cdecl;
  4323. begin
  4324. //DUMMY
  4325. end;
  4326. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4327. function glBitmap_libJPEG_fill_input_buffer(cinfo: j_decompress_ptr): boolean; cdecl;
  4328. var
  4329. src: glBitmap_libJPEG_source_mgr_ptr;
  4330. bytes: integer;
  4331. begin
  4332. src := glBitmap_libJPEG_source_mgr_ptr(cinfo^.src);
  4333. bytes := src^.SrcStream.Read(src^.SrcBuffer[1], 4096);
  4334. if (bytes <= 0) then begin
  4335. src^.SrcBuffer[1] := $FF;
  4336. src^.SrcBuffer[2] := JPEG_EOI;
  4337. bytes := 2;
  4338. end;
  4339. src^.pub.next_input_byte := @(src^.SrcBuffer[1]);
  4340. src^.pub.bytes_in_buffer := bytes;
  4341. result := true;
  4342. end;
  4343. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4344. procedure glBitmap_libJPEG_skip_input_data(cinfo: j_decompress_ptr; num_bytes: Longint); cdecl;
  4345. var
  4346. src: glBitmap_libJPEG_source_mgr_ptr;
  4347. begin
  4348. src := glBitmap_libJPEG_source_mgr_ptr(cinfo^.src);
  4349. if num_bytes > 0 then begin
  4350. // wanted byte isn't in buffer so set stream position and read buffer
  4351. if num_bytes > src^.pub.bytes_in_buffer then begin
  4352. src^.SrcStream.Position := src^.SrcStream.Position + num_bytes - src^.pub.bytes_in_buffer;
  4353. src^.pub.fill_input_buffer(cinfo);
  4354. end else begin
  4355. // wanted byte is in buffer so only skip
  4356. inc(src^.pub.next_input_byte, num_bytes);
  4357. dec(src^.pub.bytes_in_buffer, num_bytes);
  4358. end;
  4359. end;
  4360. end;
  4361. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4362. function glBitmap_libJPEG_empty_output_buffer(cinfo: j_compress_ptr): boolean; cdecl;
  4363. var
  4364. dest: glBitmap_libJPEG_dest_mgr_ptr;
  4365. begin
  4366. dest := glBitmap_libJPEG_dest_mgr_ptr(cinfo^.dest);
  4367. if dest^.pub.free_in_buffer < Cardinal(Length(dest^.DestBuffer)) then begin
  4368. // write complete buffer
  4369. dest^.DestStream.Write(dest^.DestBuffer[1], SizeOf(dest^.DestBuffer));
  4370. // reset buffer
  4371. dest^.pub.next_output_byte := @dest^.DestBuffer[1];
  4372. dest^.pub.free_in_buffer := Length(dest^.DestBuffer);
  4373. end;
  4374. result := true;
  4375. end;
  4376. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4377. procedure glBitmap_libJPEG_term_destination(cinfo: j_compress_ptr); cdecl;
  4378. var
  4379. Idx: Integer;
  4380. dest: glBitmap_libJPEG_dest_mgr_ptr;
  4381. begin
  4382. dest := glBitmap_libJPEG_dest_mgr_ptr(cinfo^.dest);
  4383. for Idx := Low(dest^.DestBuffer) to High(dest^.DestBuffer) do begin
  4384. // check for endblock
  4385. if (Idx < High(dest^.DestBuffer)) and (dest^.DestBuffer[Idx] = $FF) and (dest^.DestBuffer[Idx +1] = JPEG_EOI) then begin
  4386. // write endblock
  4387. dest^.DestStream.Write(dest^.DestBuffer[Idx], 2);
  4388. // leave
  4389. break;
  4390. end else
  4391. dest^.DestStream.Write(dest^.DestBuffer[Idx], 1);
  4392. end;
  4393. end;
  4394. {$ENDIF}
  4395. {$IFDEF GLB_SUPPORT_JPEG_READ}
  4396. {$IF DEFINED(GLB_LAZ_JPEG)}
  4397. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4398. function TglBitmapData.LoadJPEG(const aStream: TStream): Boolean;
  4399. const
  4400. MAGIC_LEN = 2;
  4401. JPEG_MAGIC: String[MAGIC_LEN] = #$FF#$D8;
  4402. var
  4403. intf: TLazIntfImage;
  4404. reader: TFPReaderJPEG;
  4405. StreamPos: Int64;
  4406. magic: String[MAGIC_LEN];
  4407. begin
  4408. result := true;
  4409. StreamPos := aStream.Position;
  4410. SetLength(magic, MAGIC_LEN);
  4411. aStream.Read(magic[1], MAGIC_LEN);
  4412. aStream.Position := StreamPos;
  4413. if (magic <> JPEG_MAGIC) then begin
  4414. result := false;
  4415. exit;
  4416. end;
  4417. reader := TFPReaderJPEG.Create;
  4418. intf := TLazIntfImage.Create(0, 0);
  4419. try try
  4420. intf.DataDescription := GetDescriptionFromDevice(0, 0, 0);
  4421. reader.ImageRead(aStream, intf);
  4422. AssignFromLazIntfImage(intf);
  4423. except
  4424. result := false;
  4425. aStream.Position := StreamPos;
  4426. exit;
  4427. end;
  4428. finally
  4429. reader.Free;
  4430. intf.Free;
  4431. end;
  4432. end;
  4433. {$ELSEIF DEFINED(GLB_SDL_IMAGE)}
  4434. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4435. function TglBitmapData.LoadJPEG(const aStream: TStream): Boolean;
  4436. var
  4437. Surface: PSDL_Surface;
  4438. RWops: PSDL_RWops;
  4439. begin
  4440. result := false;
  4441. RWops := glBitmapCreateRWops(aStream);
  4442. try
  4443. if IMG_isJPG(RWops) > 0 then begin
  4444. Surface := IMG_LoadJPG_RW(RWops);
  4445. try
  4446. AssignFromSurface(Surface);
  4447. result := true;
  4448. finally
  4449. SDL_FreeSurface(Surface);
  4450. end;
  4451. end;
  4452. finally
  4453. SDL_FreeRW(RWops);
  4454. end;
  4455. end;
  4456. {$ELSEIF DEFINED(GLB_LIB_JPEG)}
  4457. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4458. function TglBitmapData.LoadJPEG(const aStream: TStream): Boolean;
  4459. var
  4460. StreamPos: Int64;
  4461. Temp: array[0..1]of Byte;
  4462. jpeg: jpeg_decompress_struct;
  4463. jpeg_err: jpeg_error_mgr;
  4464. IntFormat: TglBitmapFormat;
  4465. pImage: pByte;
  4466. TempHeight, TempWidth: Integer;
  4467. pTemp: pByte;
  4468. Row: Integer;
  4469. FormatDesc: TFormatDescriptor;
  4470. begin
  4471. result := false;
  4472. if not init_libJPEG then
  4473. raise Exception.Create('LoadJPG - unable to initialize libJPEG.');
  4474. try
  4475. // reading first two bytes to test file and set cursor back to begin
  4476. StreamPos := aStream.Position;
  4477. aStream.Read({%H-}Temp[0], 2);
  4478. aStream.Position := StreamPos;
  4479. // if Bitmap then read file.
  4480. if ((Temp[0] = $FF) and (Temp[1] = $D8)) then begin
  4481. FillChar(jpeg{%H-}, SizeOf(jpeg_decompress_struct), $00);
  4482. FillChar(jpeg_err{%H-}, SizeOf(jpeg_error_mgr), $00);
  4483. // error managment
  4484. jpeg.err := jpeg_std_error(@jpeg_err);
  4485. jpeg_err.error_exit := glBitmap_libJPEG_error_exit;
  4486. jpeg_err.output_message := glBitmap_libJPEG_output_message;
  4487. // decompression struct
  4488. jpeg_create_decompress(@jpeg);
  4489. // allocation space for streaming methods
  4490. jpeg.src := jpeg.mem^.alloc_small(@jpeg, JPOOL_PERMANENT, SizeOf(glBitmap_libJPEG_source_mgr));
  4491. // seeting up custom functions
  4492. with glBitmap_libJPEG_source_mgr_ptr(jpeg.src)^ do begin
  4493. pub.init_source := glBitmap_libJPEG_init_source;
  4494. pub.fill_input_buffer := glBitmap_libJPEG_fill_input_buffer;
  4495. pub.skip_input_data := glBitmap_libJPEG_skip_input_data;
  4496. pub.resync_to_restart := jpeg_resync_to_restart; // use default method
  4497. pub.term_source := glBitmap_libJPEG_term_source;
  4498. pub.bytes_in_buffer := 0; // forces fill_input_buffer on first read
  4499. pub.next_input_byte := nil; // until buffer loaded
  4500. SrcStream := aStream;
  4501. end;
  4502. // set global decoding state
  4503. jpeg.global_state := DSTATE_START;
  4504. // read header of jpeg
  4505. jpeg_read_header(@jpeg, false);
  4506. // setting output parameter
  4507. case jpeg.jpeg_color_space of
  4508. JCS_GRAYSCALE:
  4509. begin
  4510. jpeg.out_color_space := JCS_GRAYSCALE;
  4511. IntFormat := tfLuminance8ub1;
  4512. end;
  4513. else
  4514. jpeg.out_color_space := JCS_RGB;
  4515. IntFormat := tfRGB8ub3;
  4516. end;
  4517. // reading image
  4518. jpeg_start_decompress(@jpeg);
  4519. TempHeight := jpeg.output_height;
  4520. TempWidth := jpeg.output_width;
  4521. FormatDesc := TFormatDescriptor.Get(IntFormat);
  4522. // creating new image
  4523. GetMem(pImage, FormatDesc.GetSize(TempWidth, TempHeight));
  4524. try
  4525. pTemp := pImage;
  4526. for Row := 0 to TempHeight -1 do begin
  4527. jpeg_read_scanlines(@jpeg, @pTemp, 1);
  4528. Inc(pTemp, FormatDesc.GetSize(TempWidth, 1));
  4529. end;
  4530. // finish decompression
  4531. jpeg_finish_decompress(@jpeg);
  4532. // destroy decompression
  4533. jpeg_destroy_decompress(@jpeg);
  4534. SetData(pImage, IntFormat, TempWidth, TempHeight);
  4535. result := true;
  4536. except
  4537. if Assigned(pImage) then
  4538. FreeMem(pImage);
  4539. raise;
  4540. end;
  4541. end;
  4542. finally
  4543. quit_libJPEG;
  4544. end;
  4545. end;
  4546. {$ELSEIF DEFINED(GLB_DELPHI_JPEG)}
  4547. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4548. function TglBitmapData.LoadJPEG(const aStream: TStream): Boolean;
  4549. var
  4550. bmp: TBitmap;
  4551. jpg: TJPEGImage;
  4552. StreamPos: Int64;
  4553. Temp: array[0..1]of Byte;
  4554. begin
  4555. result := false;
  4556. // reading first two bytes to test file and set cursor back to begin
  4557. StreamPos := aStream.Position;
  4558. aStream.Read(Temp[0], 2);
  4559. aStream.Position := StreamPos;
  4560. // if Bitmap then read file.
  4561. if ((Temp[0] = $FF) and (Temp[1] = $D8)) then begin
  4562. bmp := TBitmap.Create;
  4563. try
  4564. jpg := TJPEGImage.Create;
  4565. try
  4566. jpg.LoadFromStream(aStream);
  4567. bmp.Assign(jpg);
  4568. result := AssignFromBitmap(bmp);
  4569. finally
  4570. jpg.Free;
  4571. end;
  4572. finally
  4573. bmp.Free;
  4574. end;
  4575. end;
  4576. end;
  4577. {$IFEND}
  4578. {$ENDIF}
  4579. {$IFDEF GLB_SUPPORT_JPEG_WRITE}
  4580. {$IF DEFINED(GLB_LAZ_JPEG)}
  4581. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4582. procedure TglBitmapData.SaveJPEG(const aStream: TStream);
  4583. var
  4584. jpeg: TJPEGImage;
  4585. intf: TLazIntfImage;
  4586. raw: TRawImage;
  4587. begin
  4588. jpeg := TJPEGImage.Create;
  4589. intf := TLazIntfImage.Create(0, 0);
  4590. try
  4591. if not AssignToLazIntfImage(intf) then
  4592. raise EglBitmap.Create('unable to create LazIntfImage from glBitmap');
  4593. intf.GetRawImage(raw);
  4594. jpeg.LoadFromRawImage(raw, false);
  4595. jpeg.SaveToStream(aStream);
  4596. finally
  4597. intf.Free;
  4598. jpeg.Free;
  4599. end;
  4600. end;
  4601. {$ELSEIF DEFINED(GLB_LIB_JPEG)}
  4602. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4603. procedure TglBitmapData.SaveJPEG(const aStream: TStream);
  4604. var
  4605. jpeg: jpeg_compress_struct;
  4606. jpeg_err: jpeg_error_mgr;
  4607. Row: Integer;
  4608. pTemp, pTemp2: pByte;
  4609. procedure CopyRow(pDest, pSource: pByte);
  4610. var
  4611. X: Integer;
  4612. begin
  4613. for X := 0 to Width - 1 do begin
  4614. pByteArray(pDest)^[0] := pByteArray(pSource)^[2];
  4615. pByteArray(pDest)^[1] := pByteArray(pSource)^[1];
  4616. pByteArray(pDest)^[2] := pByteArray(pSource)^[0];
  4617. Inc(pDest, 3);
  4618. Inc(pSource, 3);
  4619. end;
  4620. end;
  4621. begin
  4622. if not (ftJPEG in FormatGetSupportedFiles(Format)) then
  4623. raise EglBitmapUnsupportedFormat.Create(Format);
  4624. if not init_libJPEG then
  4625. raise Exception.Create('SaveJPG - unable to initialize libJPEG.');
  4626. try
  4627. FillChar(jpeg{%H-}, SizeOf(jpeg_compress_struct), $00);
  4628. FillChar(jpeg_err{%H-}, SizeOf(jpeg_error_mgr), $00);
  4629. // error managment
  4630. jpeg.err := jpeg_std_error(@jpeg_err);
  4631. jpeg_err.error_exit := glBitmap_libJPEG_error_exit;
  4632. jpeg_err.output_message := glBitmap_libJPEG_output_message;
  4633. // compression struct
  4634. jpeg_create_compress(@jpeg);
  4635. // allocation space for streaming methods
  4636. jpeg.dest := jpeg.mem^.alloc_small(@jpeg, JPOOL_PERMANENT, SizeOf(glBitmap_libJPEG_dest_mgr));
  4637. // seeting up custom functions
  4638. with glBitmap_libJPEG_dest_mgr_ptr(jpeg.dest)^ do begin
  4639. pub.init_destination := glBitmap_libJPEG_init_destination;
  4640. pub.empty_output_buffer := glBitmap_libJPEG_empty_output_buffer;
  4641. pub.term_destination := glBitmap_libJPEG_term_destination;
  4642. pub.next_output_byte := @DestBuffer[1];
  4643. pub.free_in_buffer := Length(DestBuffer);
  4644. DestStream := aStream;
  4645. end;
  4646. // very important state
  4647. jpeg.global_state := CSTATE_START;
  4648. jpeg.image_width := Width;
  4649. jpeg.image_height := Height;
  4650. case Format of
  4651. tfAlpha8ub1, tfLuminance8ub1: begin
  4652. jpeg.input_components := 1;
  4653. jpeg.in_color_space := JCS_GRAYSCALE;
  4654. end;
  4655. tfRGB8ub3, tfBGR8ub3: begin
  4656. jpeg.input_components := 3;
  4657. jpeg.in_color_space := JCS_RGB;
  4658. end;
  4659. end;
  4660. jpeg_set_defaults(@jpeg);
  4661. jpeg_set_quality(@jpeg, 95, true);
  4662. jpeg_start_compress(@jpeg, true);
  4663. pTemp := Data;
  4664. if Format = tfBGR8ub3 then
  4665. GetMem(pTemp2, fRowSize)
  4666. else
  4667. pTemp2 := pTemp;
  4668. try
  4669. for Row := 0 to jpeg.image_height -1 do begin
  4670. // prepare row
  4671. if Format = tfBGR8ub3 then
  4672. CopyRow(pTemp2, pTemp)
  4673. else
  4674. pTemp2 := pTemp;
  4675. // write row
  4676. jpeg_write_scanlines(@jpeg, @pTemp2, 1);
  4677. inc(pTemp, fRowSize);
  4678. end;
  4679. finally
  4680. // free memory
  4681. if Format = tfBGR8ub3 then
  4682. FreeMem(pTemp2);
  4683. end;
  4684. jpeg_finish_compress(@jpeg);
  4685. jpeg_destroy_compress(@jpeg);
  4686. finally
  4687. quit_libJPEG;
  4688. end;
  4689. end;
  4690. {$ELSEIF DEFINED(GLB_DELPHI_JPEG)}
  4691. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4692. procedure TglBitmapData.SaveJPEG(const aStream: TStream);
  4693. var
  4694. Bmp: TBitmap;
  4695. Jpg: TJPEGImage;
  4696. begin
  4697. if not (ftJPEG in FormatGetSupportedFiles(Format)) then
  4698. raise EglBitmapUnsupportedFormat.Create(Format);
  4699. Bmp := TBitmap.Create;
  4700. try
  4701. Jpg := TJPEGImage.Create;
  4702. try
  4703. AssignToBitmap(Bmp);
  4704. if (Format in [tfAlpha8ub1, tfLuminance8ub1]) then begin
  4705. Jpg.Grayscale := true;
  4706. Jpg.PixelFormat := jf8Bit;
  4707. end;
  4708. Jpg.Assign(Bmp);
  4709. Jpg.SaveToStream(aStream);
  4710. finally
  4711. FreeAndNil(Jpg);
  4712. end;
  4713. finally
  4714. FreeAndNil(Bmp);
  4715. end;
  4716. end;
  4717. {$IFEND}
  4718. {$ENDIF}
  4719. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4720. //RAW/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4721. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4722. type
  4723. RawHeader = packed record
  4724. Magic: String[5];
  4725. Version: Byte;
  4726. Width: Integer;
  4727. Height: Integer;
  4728. DataSize: Integer;
  4729. BitsPerPixel: Integer;
  4730. Precision: TglBitmapRec4ub;
  4731. Shift: TglBitmapRec4ub;
  4732. end;
  4733. function TglBitmapData.LoadRAW(const aStream: TStream): Boolean;
  4734. var
  4735. header: RawHeader;
  4736. StartPos: Int64;
  4737. fd: TFormatDescriptor;
  4738. buf: PByte;
  4739. begin
  4740. result := false;
  4741. StartPos := aStream.Position;
  4742. aStream.Read(header{%H-}, SizeOf(header));
  4743. if (header.Magic <> 'glBMP') then begin
  4744. aStream.Position := StartPos;
  4745. exit;
  4746. end;
  4747. fd := TFormatDescriptor.GetFromPrecShift(header.Precision, header.Shift, header.BitsPerPixel);
  4748. if (fd.Format = tfEmpty) then
  4749. raise EglBitmapUnsupportedFormat.Create('no supported format found');
  4750. buf := GetMemory(header.DataSize);
  4751. aStream.Read(buf^, header.DataSize);
  4752. SetData(buf, fd.Format, header.Width, header.Height);
  4753. result := true;
  4754. end;
  4755. procedure TglBitmapData.SaveRAW(const aStream: TStream);
  4756. var
  4757. header: RawHeader;
  4758. fd: TFormatDescriptor;
  4759. begin
  4760. fd := TFormatDescriptor.Get(Format);
  4761. header.Magic := 'glBMP';
  4762. header.Version := 1;
  4763. header.Width := Width;
  4764. header.Height := Height;
  4765. header.DataSize := fd.GetSize(fDimension);
  4766. header.BitsPerPixel := fd.BitsPerPixel;
  4767. header.Precision := fd.Precision;
  4768. header.Shift := fd.Shift;
  4769. aStream.Write(header, SizeOf(header));
  4770. aStream.Write(Data^, header.DataSize);
  4771. end;
  4772. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4773. //BMP/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4774. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4775. const
  4776. BMP_MAGIC = $4D42;
  4777. BMP_COMP_RGB = 0;
  4778. BMP_COMP_RLE8 = 1;
  4779. BMP_COMP_RLE4 = 2;
  4780. BMP_COMP_BITFIELDS = 3;
  4781. type
  4782. TBMPHeader = packed record
  4783. bfType: Word;
  4784. bfSize: Cardinal;
  4785. bfReserved1: Word;
  4786. bfReserved2: Word;
  4787. bfOffBits: Cardinal;
  4788. end;
  4789. TBMPInfo = packed record
  4790. biSize: Cardinal;
  4791. biWidth: Longint;
  4792. biHeight: Longint;
  4793. biPlanes: Word;
  4794. biBitCount: Word;
  4795. biCompression: Cardinal;
  4796. biSizeImage: Cardinal;
  4797. biXPelsPerMeter: Longint;
  4798. biYPelsPerMeter: Longint;
  4799. biClrUsed: Cardinal;
  4800. biClrImportant: Cardinal;
  4801. end;
  4802. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4803. function TglBitmapData.LoadBMP(const aStream: TStream): Boolean;
  4804. //////////////////////////////////////////////////////////////////////////////////////////////////
  4805. function ReadInfo(out aInfo: TBMPInfo; out aMask: TglBitmapRec4ul): TglBitmapFormat;
  4806. var
  4807. tmp, i: Cardinal;
  4808. begin
  4809. result := tfEmpty;
  4810. aStream.Read(aInfo{%H-}, SizeOf(aInfo));
  4811. FillChar(aMask{%H-}, SizeOf(aMask), 0);
  4812. //Read Compression
  4813. case aInfo.biCompression of
  4814. BMP_COMP_RLE4,
  4815. BMP_COMP_RLE8: begin
  4816. raise EglBitmap.Create('RLE compression is not supported');
  4817. end;
  4818. BMP_COMP_BITFIELDS: begin
  4819. if (aInfo.biBitCount = 16) or (aInfo.biBitCount = 32) then begin
  4820. for i := 0 to 2 do begin
  4821. aStream.Read(tmp{%H-}, SizeOf(tmp));
  4822. aMask.arr[i] := tmp;
  4823. end;
  4824. end else
  4825. raise EglBitmap.Create('Bitfields are only supported for 16bit and 32bit formats');
  4826. end;
  4827. end;
  4828. //get suitable format
  4829. case aInfo.biBitCount of
  4830. 8: result := tfLuminance8ub1;
  4831. 16: result := tfX1RGB5us1;
  4832. 24: result := tfBGR8ub3;
  4833. 32: result := tfXRGB8ui1;
  4834. end;
  4835. end;
  4836. function ReadColorTable(var aFormat: TglBitmapFormat; const aInfo: TBMPInfo): TbmpColorTableFormat;
  4837. var
  4838. i, c: Integer;
  4839. fd: TFormatDescriptor;
  4840. ColorTable: TbmpColorTable;
  4841. begin
  4842. result := nil;
  4843. if (aInfo.biBitCount >= 16) then
  4844. exit;
  4845. aFormat := tfLuminance8ub1;
  4846. c := aInfo.biClrUsed;
  4847. if (c = 0) then
  4848. c := 1 shl aInfo.biBitCount;
  4849. SetLength(ColorTable, c);
  4850. for i := 0 to c-1 do begin
  4851. aStream.Read(ColorTable[i], SizeOf(TbmpColorTableEnty));
  4852. if (ColorTable[i].r <> ColorTable[i].g) or (ColorTable[i].g <> ColorTable[i].b) then
  4853. aFormat := tfRGB8ub3;
  4854. end;
  4855. fd := TFormatDescriptor.Get(aFormat);
  4856. result := TbmpColorTableFormat.Create;
  4857. result.ColorTable := ColorTable;
  4858. result.SetCustomValues(aFormat, aInfo.biBitCount, fd.Precision, fd.Shift);
  4859. end;
  4860. //////////////////////////////////////////////////////////////////////////////////////////////////
  4861. function CheckBitfields(var aFormat: TglBitmapFormat; const aMask: TglBitmapRec4ul; const aInfo: TBMPInfo): TbmpBitfieldFormat;
  4862. var
  4863. fd: TFormatDescriptor;
  4864. begin
  4865. result := nil;
  4866. if (aMask.r <> 0) or (aMask.g <> 0) or (aMask.b <> 0) or (aMask.a <> 0) then begin
  4867. // find suitable format ...
  4868. fd := TFormatDescriptor.GetFromMask(aMask);
  4869. if (fd.Format <> tfEmpty) then begin
  4870. aFormat := fd.Format;
  4871. exit;
  4872. end;
  4873. // or create custom bitfield format
  4874. result := TbmpBitfieldFormat.Create;
  4875. result.SetCustomValues(aInfo.biBitCount, aMask);
  4876. end;
  4877. end;
  4878. var
  4879. //simple types
  4880. StartPos: Int64;
  4881. ImageSize, rbLineSize, wbLineSize, Padding, i: Integer;
  4882. PaddingBuff: Cardinal;
  4883. LineBuf, ImageData, TmpData: PByte;
  4884. SourceMD, DestMD: Pointer;
  4885. BmpFormat: TglBitmapFormat;
  4886. //records
  4887. Mask: TglBitmapRec4ul;
  4888. Header: TBMPHeader;
  4889. Info: TBMPInfo;
  4890. //classes
  4891. SpecialFormat: TFormatDescriptor;
  4892. FormatDesc: TFormatDescriptor;
  4893. //////////////////////////////////////////////////////////////////////////////////////////////////
  4894. procedure SpecialFormatReadLine(aData: PByte; aLineBuf: PByte);
  4895. var
  4896. i: Integer;
  4897. Pixel: TglBitmapPixelData;
  4898. begin
  4899. aStream.Read(aLineBuf^, rbLineSize);
  4900. SpecialFormat.PreparePixel(Pixel);
  4901. for i := 0 to Info.biWidth-1 do begin
  4902. SpecialFormat.Unmap(aLineBuf, Pixel, SourceMD);
  4903. glBitmapConvertPixel(Pixel, SpecialFormat, FormatDesc);
  4904. FormatDesc.Map(Pixel, aData, DestMD);
  4905. end;
  4906. end;
  4907. begin
  4908. result := false;
  4909. BmpFormat := tfEmpty;
  4910. SpecialFormat := nil;
  4911. LineBuf := nil;
  4912. SourceMD := nil;
  4913. DestMD := nil;
  4914. // Header
  4915. StartPos := aStream.Position;
  4916. aStream.Read(Header{%H-}, SizeOf(Header));
  4917. if Header.bfType = BMP_MAGIC then begin
  4918. try try
  4919. BmpFormat := ReadInfo(Info, Mask);
  4920. SpecialFormat := ReadColorTable(BmpFormat, Info);
  4921. if not Assigned(SpecialFormat) then
  4922. SpecialFormat := CheckBitfields(BmpFormat, Mask, Info);
  4923. aStream.Position := StartPos + Header.bfOffBits;
  4924. if (BmpFormat <> tfEmpty) then begin
  4925. FormatDesc := TFormatDescriptor.Get(BmpFormat);
  4926. rbLineSize := Round(Info.biWidth * Info.biBitCount / 8); //ReadBuffer LineSize
  4927. wbLineSize := Trunc(Info.biWidth * FormatDesc.BytesPerPixel);
  4928. Padding := (((Info.biWidth * Info.biBitCount + 31) and - 32) shr 3) - rbLineSize;
  4929. //get Memory
  4930. DestMD := FormatDesc.CreateMappingData;
  4931. ImageSize := FormatDesc.GetSize(Info.biWidth, abs(Info.biHeight));
  4932. GetMem(ImageData, ImageSize);
  4933. if Assigned(SpecialFormat) then begin
  4934. GetMem(LineBuf, rbLineSize); //tmp Memory for converting Bitfields
  4935. SourceMD := SpecialFormat.CreateMappingData;
  4936. end;
  4937. //read Data
  4938. try try
  4939. FillChar(ImageData^, ImageSize, $FF);
  4940. TmpData := ImageData;
  4941. if (Info.biHeight > 0) then
  4942. Inc(TmpData, wbLineSize * (Info.biHeight-1));
  4943. for i := 0 to Abs(Info.biHeight)-1 do begin
  4944. if Assigned(SpecialFormat) then
  4945. SpecialFormatReadLine(TmpData, LineBuf) //if is special format read and convert data
  4946. else
  4947. aStream.Read(TmpData^, wbLineSize); //else only read data
  4948. if (Info.biHeight > 0) then
  4949. dec(TmpData, wbLineSize)
  4950. else
  4951. inc(TmpData, wbLineSize);
  4952. aStream.Read(PaddingBuff{%H-}, Padding);
  4953. end;
  4954. SetData(ImageData, BmpFormat, Info.biWidth, abs(Info.biHeight));
  4955. result := true;
  4956. finally
  4957. if Assigned(LineBuf) then
  4958. FreeMem(LineBuf);
  4959. if Assigned(SourceMD) then
  4960. SpecialFormat.FreeMappingData(SourceMD);
  4961. FormatDesc.FreeMappingData(DestMD);
  4962. end;
  4963. except
  4964. if Assigned(ImageData) then
  4965. FreeMem(ImageData);
  4966. raise;
  4967. end;
  4968. end else
  4969. raise EglBitmap.Create('LoadBMP - No suitable format found');
  4970. except
  4971. aStream.Position := StartPos;
  4972. raise;
  4973. end;
  4974. finally
  4975. FreeAndNil(SpecialFormat);
  4976. end;
  4977. end
  4978. else aStream.Position := StartPos;
  4979. end;
  4980. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4981. procedure TglBitmapData.SaveBMP(const aStream: TStream);
  4982. var
  4983. Header: TBMPHeader;
  4984. Info: TBMPInfo;
  4985. Converter: TFormatDescriptor;
  4986. FormatDesc: TFormatDescriptor;
  4987. SourceFD, DestFD: Pointer;
  4988. pData, srcData, dstData, ConvertBuffer: pByte;
  4989. Pixel: TglBitmapPixelData;
  4990. ImageSize, wbLineSize, rbLineSize, Padding, LineIdx, PixelIdx: Integer;
  4991. RedMask, GreenMask, BlueMask, AlphaMask: Cardinal;
  4992. PaddingBuff: Cardinal;
  4993. function GetLineWidth : Integer;
  4994. begin
  4995. result := ((Info.biWidth * Info.biBitCount + 31) and - 32) shr 3;
  4996. end;
  4997. begin
  4998. if not (ftBMP in FormatGetSupportedFiles(Format)) then
  4999. raise EglBitmapUnsupportedFormat.Create(Format);
  5000. Converter := nil;
  5001. FormatDesc := TFormatDescriptor.Get(Format);
  5002. ImageSize := FormatDesc.GetSize(Dimension);
  5003. FillChar(Header{%H-}, SizeOf(Header), 0);
  5004. Header.bfType := BMP_MAGIC;
  5005. Header.bfSize := SizeOf(Header) + SizeOf(Info) + ImageSize;
  5006. Header.bfReserved1 := 0;
  5007. Header.bfReserved2 := 0;
  5008. Header.bfOffBits := SizeOf(Header) + SizeOf(Info);
  5009. FillChar(Info{%H-}, SizeOf(Info), 0);
  5010. Info.biSize := SizeOf(Info);
  5011. Info.biWidth := Width;
  5012. Info.biHeight := Height;
  5013. Info.biPlanes := 1;
  5014. Info.biCompression := BMP_COMP_RGB;
  5015. Info.biSizeImage := ImageSize;
  5016. try
  5017. case Format of
  5018. tfAlpha4ub1, tfAlpha8ub1, tfLuminance4ub1, tfLuminance8ub1, tfR3G3B2ub1:
  5019. begin
  5020. Info.biBitCount := 8;
  5021. Header.bfSize := Header.bfSize + 256 * SizeOf(Cardinal);
  5022. Header.bfOffBits := Header.bfOffBits + 256 * SizeOf(Cardinal); //256 ColorTable entries
  5023. Converter := TbmpColorTableFormat.Create;
  5024. with (Converter as TbmpColorTableFormat) do begin
  5025. SetCustomValues(fFormat, 8, FormatDesc.Precision, FormatDesc.Shift);
  5026. CreateColorTable;
  5027. end;
  5028. end;
  5029. tfLuminance4Alpha4ub2, tfLuminance6Alpha2ub2, tfLuminance8Alpha8ub2,
  5030. tfRGBX4us1, tfXRGB4us1, tfRGB5X1us1, tfX1RGB5us1, tfR5G6B5us1, tfRGB5A1us1, tfA1RGB5us1, tfRGBA4us1, tfARGB4us1,
  5031. tfBGRX4us1, tfXBGR4us1, tfBGR5X1us1, tfX1BGR5us1, tfB5G6R5us1, tfBGR5A1us1, tfA1BGR5us1, tfBGRA4us1, tfABGR4us1:
  5032. begin
  5033. Info.biBitCount := 16;
  5034. Info.biCompression := BMP_COMP_BITFIELDS;
  5035. end;
  5036. tfBGR8ub3, tfRGB8ub3:
  5037. begin
  5038. Info.biBitCount := 24;
  5039. if (Format = tfRGB8ub3) then
  5040. Converter := TfdBGR8ub3.Create; //use BGR8 Format Descriptor to Swap RGB Values
  5041. end;
  5042. tfRGBX8ui1, tfXRGB8ui1, tfRGB10X2ui1, tfX2RGB10ui1, tfRGBA8ui1, tfARGB8ui1, tfRGBA8ub4, tfRGB10A2ui1, tfA2RGB10ui1,
  5043. tfBGRX8ui1, tfXBGR8ui1, tfBGR10X2ui1, tfX2BGR10ui1, tfBGRA8ui1, tfABGR8ui1, tfBGRA8ub4, tfBGR10A2ui1, tfA2BGR10ui1:
  5044. begin
  5045. Info.biBitCount := 32;
  5046. Info.biCompression := BMP_COMP_BITFIELDS;
  5047. end;
  5048. else
  5049. raise EglBitmapUnsupportedFormat.Create(Format);
  5050. end;
  5051. Info.biXPelsPerMeter := 2835;
  5052. Info.biYPelsPerMeter := 2835;
  5053. // prepare bitmasks
  5054. if Info.biCompression = BMP_COMP_BITFIELDS then begin
  5055. Header.bfSize := Header.bfSize + 4 * SizeOf(Cardinal);
  5056. Header.bfOffBits := Header.bfOffBits + 4 * SizeOf(Cardinal);
  5057. RedMask := FormatDesc.Mask.r;
  5058. GreenMask := FormatDesc.Mask.g;
  5059. BlueMask := FormatDesc.Mask.b;
  5060. AlphaMask := FormatDesc.Mask.a;
  5061. end;
  5062. // headers
  5063. aStream.Write(Header, SizeOf(Header));
  5064. aStream.Write(Info, SizeOf(Info));
  5065. // colortable
  5066. if Assigned(Converter) and (Converter is TbmpColorTableFormat) then
  5067. with (Converter as TbmpColorTableFormat) do
  5068. aStream.Write(ColorTable[0].b,
  5069. SizeOf(TbmpColorTableEnty) * Length(ColorTable));
  5070. // bitmasks
  5071. if Info.biCompression = BMP_COMP_BITFIELDS then begin
  5072. aStream.Write(RedMask, SizeOf(Cardinal));
  5073. aStream.Write(GreenMask, SizeOf(Cardinal));
  5074. aStream.Write(BlueMask, SizeOf(Cardinal));
  5075. aStream.Write(AlphaMask, SizeOf(Cardinal));
  5076. end;
  5077. // image data
  5078. rbLineSize := Round(Info.biWidth * FormatDesc.BytesPerPixel);
  5079. wbLineSize := Round(Info.biWidth * Info.biBitCount / 8);
  5080. Padding := GetLineWidth - wbLineSize;
  5081. PaddingBuff := 0;
  5082. pData := Data;
  5083. inc(pData, (Height-1) * rbLineSize);
  5084. // prepare row buffer. But only for RGB because RGBA supports color masks
  5085. // so it's possible to change color within the image.
  5086. if Assigned(Converter) then begin
  5087. FormatDesc.PreparePixel(Pixel);
  5088. GetMem(ConvertBuffer, wbLineSize);
  5089. SourceFD := FormatDesc.CreateMappingData;
  5090. DestFD := Converter.CreateMappingData;
  5091. end else
  5092. ConvertBuffer := nil;
  5093. try
  5094. for LineIdx := 0 to Height - 1 do begin
  5095. // preparing row
  5096. if Assigned(Converter) then begin
  5097. srcData := pData;
  5098. dstData := ConvertBuffer;
  5099. for PixelIdx := 0 to Info.biWidth-1 do begin
  5100. FormatDesc.Unmap(srcData, Pixel, SourceFD);
  5101. glBitmapConvertPixel(Pixel, FormatDesc, Converter);
  5102. Converter.Map(Pixel, dstData, DestFD);
  5103. end;
  5104. aStream.Write(ConvertBuffer^, wbLineSize);
  5105. end else begin
  5106. aStream.Write(pData^, rbLineSize);
  5107. end;
  5108. dec(pData, rbLineSize);
  5109. if (Padding > 0) then
  5110. aStream.Write(PaddingBuff, Padding);
  5111. end;
  5112. finally
  5113. // destroy row buffer
  5114. if Assigned(ConvertBuffer) then begin
  5115. FormatDesc.FreeMappingData(SourceFD);
  5116. Converter.FreeMappingData(DestFD);
  5117. FreeMem(ConvertBuffer);
  5118. end;
  5119. end;
  5120. finally
  5121. if Assigned(Converter) then
  5122. Converter.Free;
  5123. end;
  5124. end;
  5125. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5126. //TGA/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5127. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5128. type
  5129. TTGAHeader = packed record
  5130. ImageID: Byte;
  5131. ColorMapType: Byte;
  5132. ImageType: Byte;
  5133. //ColorMapSpec: Array[0..4] of Byte;
  5134. ColorMapStart: Word;
  5135. ColorMapLength: Word;
  5136. ColorMapEntrySize: Byte;
  5137. OrigX: Word;
  5138. OrigY: Word;
  5139. Width: Word;
  5140. Height: Word;
  5141. Bpp: Byte;
  5142. ImageDesc: Byte;
  5143. end;
  5144. const
  5145. TGA_UNCOMPRESSED_RGB = 2;
  5146. TGA_UNCOMPRESSED_GRAY = 3;
  5147. TGA_COMPRESSED_RGB = 10;
  5148. TGA_COMPRESSED_GRAY = 11;
  5149. TGA_NONE_COLOR_TABLE = 0;
  5150. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5151. function TglBitmapData.LoadTGA(const aStream: TStream): Boolean;
  5152. var
  5153. Header: TTGAHeader;
  5154. ImageData: System.PByte;
  5155. StartPosition: Int64;
  5156. PixelSize, LineSize: Integer;
  5157. tgaFormat: TglBitmapFormat;
  5158. FormatDesc: TFormatDescriptor;
  5159. Counter: packed record
  5160. X, Y: packed record
  5161. low, high, dir: Integer;
  5162. end;
  5163. end;
  5164. const
  5165. CACHE_SIZE = $4000;
  5166. ////////////////////////////////////////////////////////////////////////////////////////
  5167. procedure ReadUncompressed;
  5168. var
  5169. i, j: Integer;
  5170. buf, tmp1, tmp2: System.PByte;
  5171. begin
  5172. buf := nil;
  5173. if (Counter.X.dir < 0) then
  5174. GetMem(buf, LineSize);
  5175. try
  5176. while (Counter.Y.low <> Counter.Y.high + counter.Y.dir) do begin
  5177. tmp1 := ImageData;
  5178. inc(tmp1, (Counter.Y.low * LineSize)); //pointer to LineStart
  5179. if (Counter.X.dir < 0) then begin //flip X
  5180. aStream.Read(buf^, LineSize);
  5181. tmp2 := buf;
  5182. inc(tmp2, LineSize - PixelSize); //pointer to last pixel in line
  5183. for i := 0 to Header.Width-1 do begin //for all pixels in line
  5184. for j := 0 to PixelSize-1 do begin //for all bytes in pixel
  5185. tmp1^ := tmp2^;
  5186. inc(tmp1);
  5187. inc(tmp2);
  5188. end;
  5189. dec(tmp2, 2*PixelSize); //move 2 backwards, because j-loop moved 1 forward
  5190. end;
  5191. end else
  5192. aStream.Read(tmp1^, LineSize);
  5193. inc(Counter.Y.low, Counter.Y.dir); //move to next line index
  5194. end;
  5195. finally
  5196. if Assigned(buf) then
  5197. FreeMem(buf);
  5198. end;
  5199. end;
  5200. ////////////////////////////////////////////////////////////////////////////////////////
  5201. procedure ReadCompressed;
  5202. /////////////////////////////////////////////////////////////////
  5203. var
  5204. TmpData: System.PByte;
  5205. LinePixelsRead: Integer;
  5206. procedure CheckLine;
  5207. begin
  5208. if (LinePixelsRead >= Header.Width) then begin
  5209. LinePixelsRead := 0;
  5210. inc(Counter.Y.low, Counter.Y.dir); //next line index
  5211. TmpData := ImageData;
  5212. inc(TmpData, Counter.Y.low * LineSize); //set line
  5213. if (Counter.X.dir < 0) then //if x flipped then
  5214. inc(TmpData, LineSize - PixelSize); //set last pixel
  5215. end;
  5216. end;
  5217. /////////////////////////////////////////////////////////////////
  5218. var
  5219. Cache: PByte;
  5220. CacheSize, CachePos: Integer;
  5221. procedure CachedRead(out Buffer; Count: Integer);
  5222. var
  5223. BytesRead: Integer;
  5224. begin
  5225. if (CachePos + Count > CacheSize) then begin
  5226. //if buffer overflow save non read bytes
  5227. BytesRead := 0;
  5228. if (CacheSize - CachePos > 0) then begin
  5229. BytesRead := CacheSize - CachePos;
  5230. Move(PByteArray(Cache)^[CachePos], Buffer{%H-}, BytesRead);
  5231. inc(CachePos, BytesRead);
  5232. end;
  5233. //load cache from file
  5234. CacheSize := Min(CACHE_SIZE, aStream.Size - aStream.Position);
  5235. aStream.Read(Cache^, CacheSize);
  5236. CachePos := 0;
  5237. //read rest of requested bytes
  5238. if (Count - BytesRead > 0) then begin
  5239. Move(PByteArray(Cache)^[CachePos], TByteArray(Buffer)[BytesRead], Count - BytesRead);
  5240. inc(CachePos, Count - BytesRead);
  5241. end;
  5242. end else begin
  5243. //if no buffer overflow just read the data
  5244. Move(PByteArray(Cache)^[CachePos], Buffer, Count);
  5245. inc(CachePos, Count);
  5246. end;
  5247. end;
  5248. procedure PixelToBuffer(const aData: PByte; var aBuffer: PByte);
  5249. begin
  5250. case PixelSize of
  5251. 1: begin
  5252. aBuffer^ := aData^;
  5253. inc(aBuffer, Counter.X.dir);
  5254. end;
  5255. 2: begin
  5256. PWord(aBuffer)^ := PWord(aData)^;
  5257. inc(aBuffer, 2 * Counter.X.dir);
  5258. end;
  5259. 3: begin
  5260. PByteArray(aBuffer)^[0] := PByteArray(aData)^[0];
  5261. PByteArray(aBuffer)^[1] := PByteArray(aData)^[1];
  5262. PByteArray(aBuffer)^[2] := PByteArray(aData)^[2];
  5263. inc(aBuffer, 3 * Counter.X.dir);
  5264. end;
  5265. 4: begin
  5266. PCardinal(aBuffer)^ := PCardinal(aData)^;
  5267. inc(aBuffer, 4 * Counter.X.dir);
  5268. end;
  5269. end;
  5270. end;
  5271. var
  5272. TotalPixelsToRead, TotalPixelsRead: Integer;
  5273. Temp: Byte;
  5274. buf: array [0..3] of Byte; //1 pixel is max 32bit long
  5275. PixelRepeat: Boolean;
  5276. PixelsToRead, PixelCount: Integer;
  5277. begin
  5278. CacheSize := 0;
  5279. CachePos := 0;
  5280. TotalPixelsToRead := Header.Width * Header.Height;
  5281. TotalPixelsRead := 0;
  5282. LinePixelsRead := 0;
  5283. GetMem(Cache, CACHE_SIZE);
  5284. try
  5285. TmpData := ImageData;
  5286. inc(TmpData, Counter.Y.low * LineSize); //set line
  5287. if (Counter.X.dir < 0) then //if x flipped then
  5288. inc(TmpData, LineSize - PixelSize); //set last pixel
  5289. repeat
  5290. //read CommandByte
  5291. CachedRead(Temp, 1);
  5292. PixelRepeat := (Temp and $80) > 0;
  5293. PixelsToRead := (Temp and $7F) + 1;
  5294. inc(TotalPixelsRead, PixelsToRead);
  5295. if PixelRepeat then
  5296. CachedRead(buf[0], PixelSize);
  5297. while (PixelsToRead > 0) do begin
  5298. CheckLine;
  5299. PixelCount := Min(Header.Width - LinePixelsRead, PixelsToRead); //max read to EOL or EOF
  5300. while (PixelCount > 0) do begin
  5301. if not PixelRepeat then
  5302. CachedRead(buf[0], PixelSize);
  5303. PixelToBuffer(@buf[0], TmpData);
  5304. inc(LinePixelsRead);
  5305. dec(PixelsToRead);
  5306. dec(PixelCount);
  5307. end;
  5308. end;
  5309. until (TotalPixelsRead >= TotalPixelsToRead);
  5310. finally
  5311. FreeMem(Cache);
  5312. end;
  5313. end;
  5314. function IsGrayFormat: Boolean;
  5315. begin
  5316. result := Header.ImageType in [TGA_UNCOMPRESSED_GRAY, TGA_COMPRESSED_GRAY];
  5317. end;
  5318. begin
  5319. result := false;
  5320. // reading header to test file and set cursor back to begin
  5321. StartPosition := aStream.Position;
  5322. aStream.Read(Header{%H-}, SizeOf(Header));
  5323. // no colormapped files
  5324. if (Header.ColorMapType = TGA_NONE_COLOR_TABLE) and (Header.ImageType in [
  5325. TGA_UNCOMPRESSED_RGB, TGA_UNCOMPRESSED_GRAY, TGA_COMPRESSED_RGB, TGA_COMPRESSED_GRAY]) then
  5326. begin
  5327. try
  5328. if Header.ImageID <> 0 then // skip image ID
  5329. aStream.Position := aStream.Position + Header.ImageID;
  5330. tgaFormat := tfEmpty;
  5331. case Header.Bpp of
  5332. 8: if IsGrayFormat then case (Header.ImageDesc and $F) of
  5333. 0: tgaFormat := tfLuminance8ub1;
  5334. 8: tgaFormat := tfAlpha8ub1;
  5335. end;
  5336. 16: if IsGrayFormat then case (Header.ImageDesc and $F) of
  5337. 0: tgaFormat := tfLuminance16us1;
  5338. 8: tgaFormat := tfLuminance8Alpha8ub2;
  5339. end else case (Header.ImageDesc and $F) of
  5340. 0: tgaFormat := tfX1RGB5us1;
  5341. 1: tgaFormat := tfA1RGB5us1;
  5342. 4: tgaFormat := tfARGB4us1;
  5343. end;
  5344. 24: if not IsGrayFormat then case (Header.ImageDesc and $F) of
  5345. 0: tgaFormat := tfBGR8ub3;
  5346. end;
  5347. 32: if IsGrayFormat then case (Header.ImageDesc and $F) of
  5348. 0: tgaFormat := tfDepth32ui1;
  5349. end else case (Header.ImageDesc and $F) of
  5350. 0: tgaFormat := tfX2RGB10ui1;
  5351. 2: tgaFormat := tfA2RGB10ui1;
  5352. 8: tgaFormat := tfARGB8ui1;
  5353. end;
  5354. end;
  5355. if (tgaFormat = tfEmpty) then
  5356. raise EglBitmap.Create('LoadTga - unsupported format');
  5357. FormatDesc := TFormatDescriptor.Get(tgaFormat);
  5358. PixelSize := FormatDesc.GetSize(1, 1);
  5359. LineSize := FormatDesc.GetSize(Header.Width, 1);
  5360. GetMem(ImageData, LineSize * Header.Height);
  5361. try
  5362. //column direction
  5363. if ((Header.ImageDesc and (1 shl 4)) > 0) then begin
  5364. Counter.X.low := Header.Height-1;;
  5365. Counter.X.high := 0;
  5366. Counter.X.dir := -1;
  5367. end else begin
  5368. Counter.X.low := 0;
  5369. Counter.X.high := Header.Height-1;
  5370. Counter.X.dir := 1;
  5371. end;
  5372. // Row direction
  5373. if ((Header.ImageDesc and (1 shl 5)) > 0) then begin
  5374. Counter.Y.low := 0;
  5375. Counter.Y.high := Header.Height-1;
  5376. Counter.Y.dir := 1;
  5377. end else begin
  5378. Counter.Y.low := Header.Height-1;;
  5379. Counter.Y.high := 0;
  5380. Counter.Y.dir := -1;
  5381. end;
  5382. // Read Image
  5383. case Header.ImageType of
  5384. TGA_UNCOMPRESSED_RGB, TGA_UNCOMPRESSED_GRAY:
  5385. ReadUncompressed;
  5386. TGA_COMPRESSED_RGB, TGA_COMPRESSED_GRAY:
  5387. ReadCompressed;
  5388. end;
  5389. SetData(ImageData, tgaFormat, Header.Width, Header.Height);
  5390. result := true;
  5391. except
  5392. if Assigned(ImageData) then
  5393. FreeMem(ImageData);
  5394. raise;
  5395. end;
  5396. finally
  5397. aStream.Position := StartPosition;
  5398. end;
  5399. end
  5400. else aStream.Position := StartPosition;
  5401. end;
  5402. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5403. procedure TglBitmapData.SaveTGA(const aStream: TStream);
  5404. var
  5405. Header: TTGAHeader;
  5406. Size: Integer;
  5407. FormatDesc: TFormatDescriptor;
  5408. begin
  5409. if not (ftTGA in FormatGetSupportedFiles(Format)) then
  5410. raise EglBitmapUnsupportedFormat.Create(Format);
  5411. //prepare header
  5412. FormatDesc := TFormatDescriptor.Get(Format);
  5413. FillChar(Header{%H-}, SizeOf(Header), 0);
  5414. Header.ImageDesc := CountSetBits(FormatDesc.Range.a) and $F;
  5415. Header.Bpp := FormatDesc.BitsPerPixel;
  5416. Header.Width := Width;
  5417. Header.Height := Height;
  5418. Header.ImageDesc := Header.ImageDesc or $20; //flip y
  5419. if FormatDesc.IsGrayscale or (not FormatDesc.IsGrayscale and not FormatDesc.HasRed and FormatDesc.HasAlpha) then
  5420. Header.ImageType := TGA_UNCOMPRESSED_GRAY
  5421. else
  5422. Header.ImageType := TGA_UNCOMPRESSED_RGB;
  5423. aStream.Write(Header, SizeOf(Header));
  5424. // write Data
  5425. Size := FormatDesc.GetSize(Dimension);
  5426. aStream.Write(Data^, Size);
  5427. end;
  5428. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5429. //DDS/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5430. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5431. const
  5432. DDS_MAGIC: Cardinal = $20534444;
  5433. // DDS_header.dwFlags
  5434. DDSD_CAPS = $00000001;
  5435. DDSD_HEIGHT = $00000002;
  5436. DDSD_WIDTH = $00000004;
  5437. DDSD_PIXELFORMAT = $00001000;
  5438. // DDS_header.sPixelFormat.dwFlags
  5439. DDPF_ALPHAPIXELS = $00000001;
  5440. DDPF_ALPHA = $00000002;
  5441. DDPF_FOURCC = $00000004;
  5442. DDPF_RGB = $00000040;
  5443. DDPF_LUMINANCE = $00020000;
  5444. // DDS_header.sCaps.dwCaps1
  5445. DDSCAPS_TEXTURE = $00001000;
  5446. // DDS_header.sCaps.dwCaps2
  5447. DDSCAPS2_CUBEMAP = $00000200;
  5448. D3DFMT_DXT1 = $31545844;
  5449. D3DFMT_DXT3 = $33545844;
  5450. D3DFMT_DXT5 = $35545844;
  5451. type
  5452. TDDSPixelFormat = packed record
  5453. dwSize: Cardinal;
  5454. dwFlags: Cardinal;
  5455. dwFourCC: Cardinal;
  5456. dwRGBBitCount: Cardinal;
  5457. dwRBitMask: Cardinal;
  5458. dwGBitMask: Cardinal;
  5459. dwBBitMask: Cardinal;
  5460. dwABitMask: Cardinal;
  5461. end;
  5462. TDDSCaps = packed record
  5463. dwCaps1: Cardinal;
  5464. dwCaps2: Cardinal;
  5465. dwDDSX: Cardinal;
  5466. dwReserved: Cardinal;
  5467. end;
  5468. TDDSHeader = packed record
  5469. dwSize: Cardinal;
  5470. dwFlags: Cardinal;
  5471. dwHeight: Cardinal;
  5472. dwWidth: Cardinal;
  5473. dwPitchOrLinearSize: Cardinal;
  5474. dwDepth: Cardinal;
  5475. dwMipMapCount: Cardinal;
  5476. dwReserved: array[0..10] of Cardinal;
  5477. PixelFormat: TDDSPixelFormat;
  5478. Caps: TDDSCaps;
  5479. dwReserved2: Cardinal;
  5480. end;
  5481. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5482. function TglBitmapData.LoadDDS(const aStream: TStream): Boolean;
  5483. var
  5484. Header: TDDSHeader;
  5485. Converter: TbmpBitfieldFormat;
  5486. function GetDDSFormat: TglBitmapFormat;
  5487. var
  5488. fd: TFormatDescriptor;
  5489. i: Integer;
  5490. Mask: TglBitmapRec4ul;
  5491. Range: TglBitmapRec4ui;
  5492. match: Boolean;
  5493. begin
  5494. result := tfEmpty;
  5495. with Header.PixelFormat do begin
  5496. // Compresses
  5497. if ((dwFlags and DDPF_FOURCC) > 0) then begin
  5498. case Header.PixelFormat.dwFourCC of
  5499. D3DFMT_DXT1: result := tfS3tcDtx1RGBA;
  5500. D3DFMT_DXT3: result := tfS3tcDtx3RGBA;
  5501. D3DFMT_DXT5: result := tfS3tcDtx5RGBA;
  5502. end;
  5503. end else if ((dwFlags and (DDPF_RGB or DDPF_ALPHAPIXELS or DDPF_LUMINANCE or DDPF_ALPHA)) > 0) then begin
  5504. // prepare masks
  5505. if ((dwFlags and DDPF_LUMINANCE) = 0) then begin
  5506. Mask.r := dwRBitMask;
  5507. Mask.g := dwGBitMask;
  5508. Mask.b := dwBBitMask;
  5509. end else begin
  5510. Mask.r := dwRBitMask;
  5511. Mask.g := dwRBitMask;
  5512. Mask.b := dwRBitMask;
  5513. end;
  5514. if (dwFlags and DDPF_ALPHAPIXELS > 0) then
  5515. Mask.a := dwABitMask
  5516. else
  5517. Mask.a := 0;;
  5518. //find matching format
  5519. fd := TFormatDescriptor.GetFromMask(Mask, dwRGBBitCount);
  5520. result := fd.Format;
  5521. if (result <> tfEmpty) then
  5522. exit;
  5523. //find format with same Range
  5524. for i := 0 to 3 do
  5525. Range.arr[i] := (2 shl CountSetBits(Mask.arr[i])) - 1;
  5526. for result := High(TglBitmapFormat) downto Low(TglBitmapFormat) do begin
  5527. fd := TFormatDescriptor.Get(result);
  5528. match := true;
  5529. for i := 0 to 3 do
  5530. if (fd.Range.arr[i] <> Range.arr[i]) then begin
  5531. match := false;
  5532. break;
  5533. end;
  5534. if match then
  5535. break;
  5536. end;
  5537. //no format with same range found -> use default
  5538. if (result = tfEmpty) then begin
  5539. if (dwABitMask > 0) then
  5540. result := tfRGBA8ui1
  5541. else
  5542. result := tfRGB8ub3;
  5543. end;
  5544. Converter := TbmpBitfieldFormat.Create;
  5545. Converter.SetCustomValues(dwRGBBitCount, glBitmapRec4ul(dwRBitMask, dwGBitMask, dwBBitMask, dwABitMask));
  5546. end;
  5547. end;
  5548. end;
  5549. var
  5550. StreamPos: Int64;
  5551. x, y, LineSize, RowSize, Magic: Cardinal;
  5552. NewImage, TmpData, RowData, SrcData: System.PByte;
  5553. SourceMD, DestMD: Pointer;
  5554. Pixel: TglBitmapPixelData;
  5555. ddsFormat: TglBitmapFormat;
  5556. FormatDesc: TFormatDescriptor;
  5557. begin
  5558. result := false;
  5559. Converter := nil;
  5560. StreamPos := aStream.Position;
  5561. // Magic
  5562. aStream.Read(Magic{%H-}, sizeof(Magic));
  5563. if (Magic <> DDS_MAGIC) then begin
  5564. aStream.Position := StreamPos;
  5565. exit;
  5566. end;
  5567. //Header
  5568. aStream.Read(Header{%H-}, sizeof(Header));
  5569. if (Header.dwSize <> SizeOf(Header)) or
  5570. ((Header.dwFlags and (DDSD_PIXELFORMAT or DDSD_CAPS or DDSD_WIDTH or DDSD_HEIGHT)) <>
  5571. (DDSD_PIXELFORMAT or DDSD_CAPS or DDSD_WIDTH or DDSD_HEIGHT)) then
  5572. begin
  5573. aStream.Position := StreamPos;
  5574. exit;
  5575. end;
  5576. if ((Header.Caps.dwCaps1 and DDSCAPS2_CUBEMAP) > 0) then
  5577. raise EglBitmap.Create('LoadDDS - CubeMaps are not supported');
  5578. ddsFormat := GetDDSFormat;
  5579. try
  5580. if (ddsFormat = tfEmpty) then
  5581. raise EglBitmap.Create('LoadDDS - unsupported Pixelformat found.');
  5582. FormatDesc := TFormatDescriptor.Get(ddsFormat);
  5583. LineSize := Trunc(Header.dwWidth * FormatDesc.BytesPerPixel);
  5584. GetMem(NewImage, Header.dwHeight * LineSize);
  5585. try
  5586. TmpData := NewImage;
  5587. //Converter needed
  5588. if Assigned(Converter) then begin
  5589. RowSize := Round(Header.dwWidth * Header.PixelFormat.dwRGBBitCount / 8);
  5590. GetMem(RowData, RowSize);
  5591. SourceMD := Converter.CreateMappingData;
  5592. DestMD := FormatDesc.CreateMappingData;
  5593. try
  5594. for y := 0 to Header.dwHeight-1 do begin
  5595. TmpData := NewImage;
  5596. inc(TmpData, y * LineSize);
  5597. SrcData := RowData;
  5598. aStream.Read(SrcData^, RowSize);
  5599. for x := 0 to Header.dwWidth-1 do begin
  5600. Converter.Unmap(SrcData, Pixel, SourceMD);
  5601. glBitmapConvertPixel(Pixel, Converter, FormatDesc);
  5602. FormatDesc.Map(Pixel, TmpData, DestMD);
  5603. end;
  5604. end;
  5605. finally
  5606. Converter.FreeMappingData(SourceMD);
  5607. FormatDesc.FreeMappingData(DestMD);
  5608. FreeMem(RowData);
  5609. end;
  5610. end else
  5611. // Compressed
  5612. if ((Header.PixelFormat.dwFlags and DDPF_FOURCC) > 0) then begin
  5613. RowSize := Header.dwPitchOrLinearSize div Header.dwWidth;
  5614. for Y := 0 to Header.dwHeight-1 do begin
  5615. aStream.Read(TmpData^, RowSize);
  5616. Inc(TmpData, LineSize);
  5617. end;
  5618. end else
  5619. // Uncompressed
  5620. if (Header.PixelFormat.dwFlags and (DDPF_RGB or DDPF_ALPHAPIXELS or DDPF_LUMINANCE)) > 0 then begin
  5621. RowSize := (Header.PixelFormat.dwRGBBitCount * Header.dwWidth) shr 3;
  5622. for Y := 0 to Header.dwHeight-1 do begin
  5623. aStream.Read(TmpData^, RowSize);
  5624. Inc(TmpData, LineSize);
  5625. end;
  5626. end else
  5627. raise EglBitmap.Create('LoadDDS - unsupported Pixelformat found.');
  5628. SetData(NewImage, ddsFormat, Header.dwWidth, Header.dwHeight);
  5629. result := true;
  5630. except
  5631. if Assigned(NewImage) then
  5632. FreeMem(NewImage);
  5633. raise;
  5634. end;
  5635. finally
  5636. FreeAndNil(Converter);
  5637. end;
  5638. end;
  5639. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5640. procedure TglBitmapData.SaveDDS(const aStream: TStream);
  5641. var
  5642. Header: TDDSHeader;
  5643. FormatDesc: TFormatDescriptor;
  5644. begin
  5645. if not (ftDDS in FormatGetSupportedFiles(Format)) then
  5646. raise EglBitmapUnsupportedFormat.Create(Format);
  5647. FormatDesc := TFormatDescriptor.Get(Format);
  5648. // Generell
  5649. FillChar(Header{%H-}, SizeOf(Header), 0);
  5650. Header.dwSize := SizeOf(Header);
  5651. Header.dwFlags := DDSD_WIDTH or DDSD_HEIGHT or DDSD_CAPS or DDSD_PIXELFORMAT;
  5652. Header.dwWidth := Max(1, Width);
  5653. Header.dwHeight := Max(1, Height);
  5654. // Caps
  5655. Header.Caps.dwCaps1 := DDSCAPS_TEXTURE;
  5656. // Pixelformat
  5657. Header.PixelFormat.dwSize := sizeof(Header);
  5658. if (FormatDesc.IsCompressed) then begin
  5659. Header.PixelFormat.dwFlags := Header.PixelFormat.dwFlags or DDPF_FOURCC;
  5660. case Format of
  5661. tfS3tcDtx1RGBA: Header.PixelFormat.dwFourCC := D3DFMT_DXT1;
  5662. tfS3tcDtx3RGBA: Header.PixelFormat.dwFourCC := D3DFMT_DXT3;
  5663. tfS3tcDtx5RGBA: Header.PixelFormat.dwFourCC := D3DFMT_DXT5;
  5664. end;
  5665. end else if not FormatDesc.HasColor and FormatDesc.HasAlpha then begin
  5666. Header.PixelFormat.dwFlags := Header.PixelFormat.dwFlags or DDPF_ALPHA;
  5667. Header.PixelFormat.dwRGBBitCount := FormatDesc.BitsPerPixel;
  5668. Header.PixelFormat.dwABitMask := FormatDesc.Mask.a;
  5669. end else if FormatDesc.IsGrayscale then begin
  5670. Header.PixelFormat.dwFlags := Header.PixelFormat.dwFlags or DDPF_LUMINANCE;
  5671. Header.PixelFormat.dwRGBBitCount := FormatDesc.BitsPerPixel;
  5672. Header.PixelFormat.dwRBitMask := FormatDesc.Mask.r;
  5673. Header.PixelFormat.dwABitMask := FormatDesc.Mask.a;
  5674. end else begin
  5675. Header.PixelFormat.dwFlags := Header.PixelFormat.dwFlags or DDPF_RGB;
  5676. Header.PixelFormat.dwRGBBitCount := FormatDesc.BitsPerPixel;
  5677. Header.PixelFormat.dwRBitMask := FormatDesc.Mask.r;
  5678. Header.PixelFormat.dwGBitMask := FormatDesc.Mask.g;
  5679. Header.PixelFormat.dwBBitMask := FormatDesc.Mask.b;
  5680. Header.PixelFormat.dwABitMask := FormatDesc.Mask.a;
  5681. end;
  5682. if (FormatDesc.HasAlpha) then
  5683. Header.PixelFormat.dwFlags := Header.PixelFormat.dwFlags or DDPF_ALPHAPIXELS;
  5684. aStream.Write(DDS_MAGIC, sizeof(DDS_MAGIC));
  5685. aStream.Write(Header, SizeOf(Header));
  5686. aStream.Write(Data^, FormatDesc.GetSize(Dimension));
  5687. end;
  5688. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5689. function TglBitmapData.FlipHorz: Boolean;
  5690. var
  5691. fd: TglBitmapFormatDescriptor;
  5692. Col, RowSize, PixelSize: Integer;
  5693. pTempDest, pDest, pSource: PByte;
  5694. begin
  5695. result := false;
  5696. fd := FormatDescriptor;
  5697. PixelSize := Ceil(fd.BytesPerPixel);
  5698. RowSize := fd.GetSize(Width, 1);
  5699. if Assigned(Data) and not fd.IsCompressed then begin
  5700. pSource := Data;
  5701. GetMem(pDest, RowSize);
  5702. try
  5703. pTempDest := pDest;
  5704. Inc(pTempDest, RowSize);
  5705. for Col := 0 to Width-1 do begin
  5706. dec(pTempDest, PixelSize); //dec before, because ptr is behind last byte of data
  5707. Move(pSource^, pTempDest^, PixelSize);
  5708. Inc(pSource, PixelSize);
  5709. end;
  5710. SetData(pDest, Format, Width);
  5711. result := true;
  5712. except
  5713. if Assigned(pDest) then
  5714. FreeMem(pDest);
  5715. raise;
  5716. end;
  5717. end;
  5718. end;
  5719. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5720. function TglBitmapData.FlipVert: Boolean;
  5721. var
  5722. fd: TglBitmapFormatDescriptor;
  5723. Row, RowSize, PixelSize: Integer;
  5724. TempDestData, DestData, SourceData: PByte;
  5725. begin
  5726. result := false;
  5727. fd := FormatDescriptor;
  5728. PixelSize := Ceil(fd.BytesPerPixel);
  5729. RowSize := fd.GetSize(Width, 1);
  5730. if Assigned(Data) then begin
  5731. SourceData := Data;
  5732. GetMem(DestData, Height * RowSize);
  5733. try
  5734. TempDestData := DestData;
  5735. Inc(TempDestData, Width * (Height -1) * PixelSize);
  5736. for Row := 0 to Height -1 do begin
  5737. Move(SourceData^, TempDestData^, RowSize);
  5738. Dec(TempDestData, RowSize);
  5739. Inc(SourceData, RowSize);
  5740. end;
  5741. SetData(DestData, Format, Width, Height);
  5742. result := true;
  5743. except
  5744. if Assigned(DestData) then
  5745. FreeMem(DestData);
  5746. raise;
  5747. end;
  5748. end;
  5749. end;
  5750. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5751. procedure TglBitmapData.LoadFromFile(const aFilename: String);
  5752. var
  5753. fs: TFileStream;
  5754. begin
  5755. if not FileExists(aFilename) then
  5756. raise EglBitmap.Create('file does not exist: ' + aFilename);
  5757. fs := TFileStream.Create(aFilename, fmOpenRead);
  5758. try
  5759. fs.Position := 0;
  5760. LoadFromStream(fs);
  5761. fFilename := aFilename;
  5762. finally
  5763. fs.Free;
  5764. end;
  5765. end;
  5766. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5767. procedure TglBitmapData.LoadFromStream(const aStream: TStream);
  5768. begin
  5769. {$IFDEF GLB_SUPPORT_PNG_READ}
  5770. if not LoadPNG(aStream) then
  5771. {$ENDIF}
  5772. {$IFDEF GLB_SUPPORT_JPEG_READ}
  5773. if not LoadJPEG(aStream) then
  5774. {$ENDIF}
  5775. if not LoadDDS(aStream) then
  5776. if not LoadTGA(aStream) then
  5777. if not LoadBMP(aStream) then
  5778. if not LoadRAW(aStream) then
  5779. raise EglBitmap.Create('LoadFromStream - Couldn''t load Stream. It''s possible to be an unknow Streamtype.');
  5780. end;
  5781. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5782. procedure TglBitmapData.LoadFromFunc(const aSize: TglBitmapSize; const aFormat: TglBitmapFormat;
  5783. const aFunc: TglBitmapFunction; const aArgs: Pointer);
  5784. var
  5785. tmpData: PByte;
  5786. size: Integer;
  5787. begin
  5788. size := TFormatDescriptor.Get(aFormat).GetSize(aSize);
  5789. GetMem(tmpData, size);
  5790. try
  5791. FillChar(tmpData^, size, #$FF);
  5792. SetData(tmpData, aFormat, aSize.X, aSize.Y);
  5793. except
  5794. if Assigned(tmpData) then
  5795. FreeMem(tmpData);
  5796. raise;
  5797. end;
  5798. Convert(Self, aFunc, false, aFormat, aArgs);
  5799. end;
  5800. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5801. procedure TglBitmapData.LoadFromResource(const aInstance: Cardinal; aResource: String; aResType: PChar);
  5802. var
  5803. rs: TResourceStream;
  5804. begin
  5805. PrepareResType(aResource, aResType);
  5806. rs := TResourceStream.Create(aInstance, aResource, aResType);
  5807. try
  5808. LoadFromStream(rs);
  5809. finally
  5810. rs.Free;
  5811. end;
  5812. end;
  5813. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5814. procedure TglBitmapData.LoadFromResourceID(const aInstance: Cardinal; const aResourceID: Integer; const aResType: PChar);
  5815. var
  5816. rs: TResourceStream;
  5817. begin
  5818. rs := TResourceStream.CreateFromID(aInstance, aResourceID, aResType);
  5819. try
  5820. LoadFromStream(rs);
  5821. finally
  5822. rs.Free;
  5823. end;
  5824. end;
  5825. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5826. procedure TglBitmapData.SaveToFile(const aFilename: String; const aFileType: TglBitmapFileType);
  5827. var
  5828. fs: TFileStream;
  5829. begin
  5830. fs := TFileStream.Create(aFileName, fmCreate);
  5831. try
  5832. fs.Position := 0;
  5833. SaveToStream(fs, aFileType);
  5834. finally
  5835. fs.Free;
  5836. end;
  5837. end;
  5838. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5839. procedure TglBitmapData.SaveToStream(const aStream: TStream; const aFileType: TglBitmapFileType);
  5840. begin
  5841. case aFileType of
  5842. {$IFDEF GLB_SUPPORT_PNG_WRITE}
  5843. ftPNG: SavePNG(aStream);
  5844. {$ENDIF}
  5845. {$IFDEF GLB_SUPPORT_JPEG_WRITE}
  5846. ftJPEG: SaveJPEG(aStream);
  5847. {$ENDIF}
  5848. ftDDS: SaveDDS(aStream);
  5849. ftTGA: SaveTGA(aStream);
  5850. ftBMP: SaveBMP(aStream);
  5851. ftRAW: SaveRAW(aStream);
  5852. end;
  5853. end;
  5854. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5855. function TglBitmapData.Convert(const aFunc: TglBitmapFunction; const aCreateTemp: Boolean; const aArgs: Pointer): Boolean;
  5856. begin
  5857. result := Convert(Self, aFunc, aCreateTemp, Format, aArgs);
  5858. end;
  5859. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5860. function TglBitmapData.Convert(const aSource: TglBitmapData; const aFunc: TglBitmapFunction; aCreateTemp: Boolean;
  5861. const aFormat: TglBitmapFormat; const aArgs: Pointer): Boolean;
  5862. var
  5863. DestData, TmpData, SourceData: pByte;
  5864. TempHeight, TempWidth: Integer;
  5865. SourceFD, DestFD: TFormatDescriptor;
  5866. SourceMD, DestMD: Pointer;
  5867. FuncRec: TglBitmapFunctionRec;
  5868. begin
  5869. Assert(Assigned(Data));
  5870. Assert(Assigned(aSource));
  5871. Assert(Assigned(aSource.Data));
  5872. result := false;
  5873. if Assigned(aSource.Data) and ((aSource.Height > 0) or (aSource.Width > 0)) then begin
  5874. SourceFD := TFormatDescriptor.Get(aSource.Format);
  5875. DestFD := TFormatDescriptor.Get(aFormat);
  5876. if (SourceFD.IsCompressed) then
  5877. raise EglBitmapUnsupportedFormat.Create('compressed formats are not supported: ', SourceFD.Format);
  5878. if (DestFD.IsCompressed) then
  5879. raise EglBitmapUnsupportedFormat.Create('compressed formats are not supported: ', DestFD.Format);
  5880. // inkompatible Formats so CreateTemp
  5881. if (SourceFD.BitsPerPixel <> DestFD.BitsPerPixel) then
  5882. aCreateTemp := true;
  5883. // Values
  5884. TempHeight := Max(1, aSource.Height);
  5885. TempWidth := Max(1, aSource.Width);
  5886. FuncRec.Sender := Self;
  5887. FuncRec.Args := aArgs;
  5888. TmpData := nil;
  5889. if aCreateTemp then begin
  5890. GetMem(TmpData, DestFD.GetSize(TempWidth, TempHeight));
  5891. DestData := TmpData;
  5892. end else
  5893. DestData := Data;
  5894. try
  5895. SourceFD.PreparePixel(FuncRec.Source);
  5896. DestFD.PreparePixel (FuncRec.Dest);
  5897. SourceMD := SourceFD.CreateMappingData;
  5898. DestMD := DestFD.CreateMappingData;
  5899. FuncRec.Size := aSource.Dimension;
  5900. FuncRec.Position.Fields := FuncRec.Size.Fields;
  5901. try
  5902. SourceData := aSource.Data;
  5903. FuncRec.Position.Y := 0;
  5904. while FuncRec.Position.Y < TempHeight do begin
  5905. FuncRec.Position.X := 0;
  5906. while FuncRec.Position.X < TempWidth do begin
  5907. SourceFD.Unmap(SourceData, FuncRec.Source, SourceMD);
  5908. aFunc(FuncRec);
  5909. DestFD.Map(FuncRec.Dest, DestData, DestMD);
  5910. inc(FuncRec.Position.X);
  5911. end;
  5912. inc(FuncRec.Position.Y);
  5913. end;
  5914. // Updating Image or InternalFormat
  5915. if aCreateTemp then
  5916. SetData(TmpData, aFormat, aSource.Width, aSource.Height)
  5917. else if (aFormat <> fFormat) then
  5918. Format := aFormat;
  5919. result := true;
  5920. finally
  5921. SourceFD.FreeMappingData(SourceMD);
  5922. DestFD.FreeMappingData(DestMD);
  5923. end;
  5924. except
  5925. if aCreateTemp and Assigned(TmpData) then
  5926. FreeMem(TmpData);
  5927. raise;
  5928. end;
  5929. end;
  5930. end;
  5931. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5932. function TglBitmapData.ConvertTo(const aFormat: TglBitmapFormat): Boolean;
  5933. var
  5934. SourceFD, DestFD: TFormatDescriptor;
  5935. SourcePD, DestPD: TglBitmapPixelData;
  5936. ShiftData: TShiftData;
  5937. function DataIsIdentical: Boolean;
  5938. begin
  5939. result := SourceFD.MaskMatch(DestFD.Mask);
  5940. end;
  5941. function CanCopyDirect: Boolean;
  5942. begin
  5943. result :=
  5944. ((SourcePD.Range.r = DestPD.Range.r) or (SourcePD.Range.r = 0) or (DestPD.Range.r = 0)) and
  5945. ((SourcePD.Range.g = DestPD.Range.g) or (SourcePD.Range.g = 0) or (DestPD.Range.g = 0)) and
  5946. ((SourcePD.Range.b = DestPD.Range.b) or (SourcePD.Range.b = 0) or (DestPD.Range.b = 0)) and
  5947. ((SourcePD.Range.a = DestPD.Range.a) or (SourcePD.Range.a = 0) or (DestPD.Range.a = 0));
  5948. end;
  5949. function CanShift: Boolean;
  5950. begin
  5951. result :=
  5952. ((SourcePD.Range.r >= DestPD.Range.r) or (SourcePD.Range.r = 0) or (DestPD.Range.r = 0)) and
  5953. ((SourcePD.Range.g >= DestPD.Range.g) or (SourcePD.Range.g = 0) or (DestPD.Range.g = 0)) and
  5954. ((SourcePD.Range.b >= DestPD.Range.b) or (SourcePD.Range.b = 0) or (DestPD.Range.b = 0)) and
  5955. ((SourcePD.Range.a >= DestPD.Range.a) or (SourcePD.Range.a = 0) or (DestPD.Range.a = 0));
  5956. end;
  5957. function GetShift(aSource, aDest: Cardinal) : ShortInt;
  5958. begin
  5959. result := 0;
  5960. while (aSource > aDest) and (aSource > 0) do begin
  5961. inc(result);
  5962. aSource := aSource shr 1;
  5963. end;
  5964. end;
  5965. begin
  5966. if (aFormat <> fFormat) and (aFormat <> tfEmpty) then begin
  5967. SourceFD := TFormatDescriptor.Get(Format);
  5968. DestFD := TFormatDescriptor.Get(aFormat);
  5969. if DataIsIdentical then begin
  5970. result := true;
  5971. Format := aFormat;
  5972. exit;
  5973. end;
  5974. SourceFD.PreparePixel(SourcePD);
  5975. DestFD.PreparePixel (DestPD);
  5976. if CanCopyDirect then
  5977. result := Convert(Self, glBitmapConvertCopyFunc, false, aFormat)
  5978. else if CanShift then begin
  5979. ShiftData.r := GetShift(SourcePD.Range.r, DestPD.Range.r);
  5980. ShiftData.g := GetShift(SourcePD.Range.g, DestPD.Range.g);
  5981. ShiftData.b := GetShift(SourcePD.Range.b, DestPD.Range.b);
  5982. ShiftData.a := GetShift(SourcePD.Range.a, DestPD.Range.a);
  5983. result := Convert(Self, glBitmapConvertShiftRGBAFunc, false, aFormat, @ShiftData);
  5984. end else
  5985. result := Convert(Self, glBitmapConvertCalculateRGBAFunc, false, aFormat);
  5986. end else
  5987. result := true;
  5988. end;
  5989. {$IFDEF GLB_SDL}
  5990. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5991. function TglBitmapData.AssignToSurface(out aSurface: PSDL_Surface): Boolean;
  5992. var
  5993. Row, RowSize: Integer;
  5994. SourceData, TmpData: PByte;
  5995. TempDepth: Integer;
  5996. FormatDesc: TFormatDescriptor;
  5997. function GetRowPointer(Row: Integer): pByte;
  5998. begin
  5999. result := aSurface.pixels;
  6000. Inc(result, Row * RowSize);
  6001. end;
  6002. begin
  6003. result := false;
  6004. FormatDesc := TFormatDescriptor.Get(Format);
  6005. if FormatDesc.IsCompressed then
  6006. raise EglBitmapUnsupportedFormat.Create(Format);
  6007. if Assigned(Data) then begin
  6008. case Trunc(FormatDesc.PixelSize) of
  6009. 1: TempDepth := 8;
  6010. 2: TempDepth := 16;
  6011. 3: TempDepth := 24;
  6012. 4: TempDepth := 32;
  6013. else
  6014. raise EglBitmapUnsupportedFormat.Create(Format);
  6015. end;
  6016. aSurface := SDL_CreateRGBSurface(SDL_SWSURFACE, Width, Height, TempDepth,
  6017. FormatDesc.RedMask, FormatDesc.GreenMask, FormatDesc.BlueMask, FormatDesc.AlphaMask);
  6018. SourceData := Data;
  6019. RowSize := FormatDesc.GetSize(FileWidth, 1);
  6020. for Row := 0 to FileHeight-1 do begin
  6021. TmpData := GetRowPointer(Row);
  6022. if Assigned(TmpData) then begin
  6023. Move(SourceData^, TmpData^, RowSize);
  6024. inc(SourceData, RowSize);
  6025. end;
  6026. end;
  6027. result := true;
  6028. end;
  6029. end;
  6030. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6031. function TglBitmapData.AssignFromSurface(const aSurface: PSDL_Surface): Boolean;
  6032. var
  6033. pSource, pData, pTempData: PByte;
  6034. Row, RowSize, TempWidth, TempHeight: Integer;
  6035. IntFormat: TglBitmapFormat;
  6036. fd: TFormatDescriptor;
  6037. Mask: TglBitmapMask;
  6038. function GetRowPointer(Row: Integer): pByte;
  6039. begin
  6040. result := aSurface^.pixels;
  6041. Inc(result, Row * RowSize);
  6042. end;
  6043. begin
  6044. result := false;
  6045. if (Assigned(aSurface)) then begin
  6046. with aSurface^.format^ do begin
  6047. Mask.r := RMask;
  6048. Mask.g := GMask;
  6049. Mask.b := BMask;
  6050. Mask.a := AMask;
  6051. IntFormat := TFormatDescriptor.GetFromMask(Mask).Format;
  6052. if (IntFormat = tfEmpty) then
  6053. raise EglBitmap.Create('AssignFromSurface - Invalid Pixelformat.');
  6054. end;
  6055. fd := TFormatDescriptor.Get(IntFormat);
  6056. TempWidth := aSurface^.w;
  6057. TempHeight := aSurface^.h;
  6058. RowSize := fd.GetSize(TempWidth, 1);
  6059. GetMem(pData, TempHeight * RowSize);
  6060. try
  6061. pTempData := pData;
  6062. for Row := 0 to TempHeight -1 do begin
  6063. pSource := GetRowPointer(Row);
  6064. if (Assigned(pSource)) then begin
  6065. Move(pSource^, pTempData^, RowSize);
  6066. Inc(pTempData, RowSize);
  6067. end;
  6068. end;
  6069. SetData(pData, IntFormat, TempWidth, TempHeight);
  6070. result := true;
  6071. except
  6072. if Assigned(pData) then
  6073. FreeMem(pData);
  6074. raise;
  6075. end;
  6076. end;
  6077. end;
  6078. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6079. function TglBitmapData.AssignAlphaToSurface(out aSurface: PSDL_Surface): Boolean;
  6080. var
  6081. Row, Col, AlphaInterleave: Integer;
  6082. pSource, pDest: PByte;
  6083. function GetRowPointer(Row: Integer): pByte;
  6084. begin
  6085. result := aSurface.pixels;
  6086. Inc(result, Row * Width);
  6087. end;
  6088. begin
  6089. result := false;
  6090. if Assigned(Data) then begin
  6091. if Format in [tfAlpha8ub1, tfLuminance8Alpha8ub2, tfBGRA8ub4, tfRGBA8ub4] then begin
  6092. aSurface := SDL_CreateRGBSurface(SDL_SWSURFACE, Width, Height, 8, $FF, $FF, $FF, 0);
  6093. AlphaInterleave := 0;
  6094. case Format of
  6095. tfLuminance8Alpha8ub2:
  6096. AlphaInterleave := 1;
  6097. tfBGRA8ub4, tfRGBA8ub4:
  6098. AlphaInterleave := 3;
  6099. end;
  6100. pSource := Data;
  6101. for Row := 0 to Height -1 do begin
  6102. pDest := GetRowPointer(Row);
  6103. if Assigned(pDest) then begin
  6104. for Col := 0 to Width -1 do begin
  6105. Inc(pSource, AlphaInterleave);
  6106. pDest^ := pSource^;
  6107. Inc(pDest);
  6108. Inc(pSource);
  6109. end;
  6110. end;
  6111. end;
  6112. result := true;
  6113. end;
  6114. end;
  6115. end;
  6116. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6117. function TglBitmapData.AddAlphaFromSurface(const aSurface: PSDL_Surface; const aFunc: TglBitmapFunction = nil; const aArgs: Pointer = nil): Boolean;
  6118. var
  6119. bmp: TglBitmap2D;
  6120. begin
  6121. bmp := TglBitmap2D.Create;
  6122. try
  6123. bmp.AssignFromSurface(aSurface);
  6124. result := AddAlphaFromGlBitmap(bmp, aFunc, aArgs);
  6125. finally
  6126. bmp.Free;
  6127. end;
  6128. end;
  6129. {$ENDIF}
  6130. {$IFDEF GLB_DELPHI}
  6131. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6132. function CreateGrayPalette: HPALETTE;
  6133. var
  6134. Idx: Integer;
  6135. Pal: PLogPalette;
  6136. begin
  6137. GetMem(Pal, SizeOf(TLogPalette) + (SizeOf(TPaletteEntry) * 256));
  6138. Pal.palVersion := $300;
  6139. Pal.palNumEntries := 256;
  6140. for Idx := 0 to Pal.palNumEntries - 1 do begin
  6141. Pal.palPalEntry[Idx].peRed := Idx;
  6142. Pal.palPalEntry[Idx].peGreen := Idx;
  6143. Pal.palPalEntry[Idx].peBlue := Idx;
  6144. Pal.palPalEntry[Idx].peFlags := 0;
  6145. end;
  6146. Result := CreatePalette(Pal^);
  6147. FreeMem(Pal);
  6148. end;
  6149. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6150. function TglBitmapData.AssignToBitmap(const aBitmap: TBitmap): Boolean;
  6151. var
  6152. Row, RowSize: Integer;
  6153. pSource, pData: PByte;
  6154. begin
  6155. result := false;
  6156. if Assigned(Data) then begin
  6157. if Assigned(aBitmap) then begin
  6158. aBitmap.Width := Width;
  6159. aBitmap.Height := Height;
  6160. case Format of
  6161. tfAlpha8ub1, tfLuminance8ub1: begin
  6162. aBitmap.PixelFormat := pf8bit;
  6163. aBitmap.Palette := CreateGrayPalette;
  6164. end;
  6165. tfRGB5A1us1:
  6166. aBitmap.PixelFormat := pf15bit;
  6167. tfR5G6B5us1:
  6168. aBitmap.PixelFormat := pf16bit;
  6169. tfRGB8ub3, tfBGR8ub3:
  6170. aBitmap.PixelFormat := pf24bit;
  6171. tfRGBA8ub4, tfBGRA8ub4:
  6172. aBitmap.PixelFormat := pf32bit;
  6173. else
  6174. raise EglBitmap.Create('AssignToBitmap - Invalid Pixelformat.');
  6175. end;
  6176. RowSize := FormatDescriptor.GetSize(Width, 1);
  6177. pSource := Data;
  6178. for Row := 0 to Height-1 do begin
  6179. pData := aBitmap.Scanline[Row];
  6180. Move(pSource^, pData^, RowSize);
  6181. Inc(pSource, RowSize);
  6182. if (Format in [tfRGB8ub3, tfRGBA8ub4]) then // swap RGB(A) to BGR(A)
  6183. SwapRGB(pData, Width, Format = tfRGBA8ub4);
  6184. end;
  6185. result := true;
  6186. end;
  6187. end;
  6188. end;
  6189. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6190. function TglBitmapData.AssignFromBitmap(const aBitmap: TBitmap): Boolean;
  6191. var
  6192. pSource, pData, pTempData: PByte;
  6193. Row, RowSize, TempWidth, TempHeight: Integer;
  6194. IntFormat: TglBitmapFormat;
  6195. begin
  6196. result := false;
  6197. if (Assigned(aBitmap)) then begin
  6198. case aBitmap.PixelFormat of
  6199. pf8bit:
  6200. IntFormat := tfLuminance8ub1;
  6201. pf15bit:
  6202. IntFormat := tfRGB5A1us1;
  6203. pf16bit:
  6204. IntFormat := tfR5G6B5us1;
  6205. pf24bit:
  6206. IntFormat := tfBGR8ub3;
  6207. pf32bit:
  6208. IntFormat := tfBGRA8ub4;
  6209. else
  6210. raise EglBitmap.Create('AssignFromBitmap - Invalid Pixelformat.');
  6211. end;
  6212. TempWidth := aBitmap.Width;
  6213. TempHeight := aBitmap.Height;
  6214. RowSize := TFormatDescriptor.Get(IntFormat).GetSize(TempWidth, 1);
  6215. GetMem(pData, TempHeight * RowSize);
  6216. try
  6217. pTempData := pData;
  6218. for Row := 0 to TempHeight -1 do begin
  6219. pSource := aBitmap.Scanline[Row];
  6220. if (Assigned(pSource)) then begin
  6221. Move(pSource^, pTempData^, RowSize);
  6222. Inc(pTempData, RowSize);
  6223. end;
  6224. end;
  6225. SetData(pData, IntFormat, TempWidth, TempHeight);
  6226. result := true;
  6227. except
  6228. if Assigned(pData) then
  6229. FreeMem(pData);
  6230. raise;
  6231. end;
  6232. end;
  6233. end;
  6234. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6235. function TglBitmapData.AssignAlphaToBitmap(const aBitmap: TBitmap): Boolean;
  6236. var
  6237. Row, Col, AlphaInterleave: Integer;
  6238. pSource, pDest: PByte;
  6239. begin
  6240. result := false;
  6241. if Assigned(Data) then begin
  6242. if (Format in [tfAlpha8ub1, tfLuminance8Alpha8ub2, tfRGBA8ub4, tfBGRA8ub4]) then begin
  6243. if Assigned(aBitmap) then begin
  6244. aBitmap.PixelFormat := pf8bit;
  6245. aBitmap.Palette := CreateGrayPalette;
  6246. aBitmap.Width := Width;
  6247. aBitmap.Height := Height;
  6248. case Format of
  6249. tfLuminance8Alpha8ub2:
  6250. AlphaInterleave := 1;
  6251. tfRGBA8ub4, tfBGRA8ub4:
  6252. AlphaInterleave := 3;
  6253. else
  6254. AlphaInterleave := 0;
  6255. end;
  6256. // Copy Data
  6257. pSource := Data;
  6258. for Row := 0 to Height -1 do begin
  6259. pDest := aBitmap.Scanline[Row];
  6260. if Assigned(pDest) then begin
  6261. for Col := 0 to Width -1 do begin
  6262. Inc(pSource, AlphaInterleave);
  6263. pDest^ := pSource^;
  6264. Inc(pDest);
  6265. Inc(pSource);
  6266. end;
  6267. end;
  6268. end;
  6269. result := true;
  6270. end;
  6271. end;
  6272. end;
  6273. end;
  6274. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6275. function TglBitmapData.AddAlphaFromBitmap(const aBitmap: TBitmap; const aFunc: TglBitmapFunction; const aArgs: Pointer): Boolean;
  6276. var
  6277. data: TglBitmapData;
  6278. begin
  6279. data := TglBitmapData.Create;
  6280. try
  6281. data.AssignFromBitmap(aBitmap);
  6282. result := AddAlphaFromDataObj(data, aFunc, aArgs);
  6283. finally
  6284. data.Free;
  6285. end;
  6286. end;
  6287. {$ENDIF}
  6288. {$IFDEF GLB_LAZARUS}
  6289. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6290. function TglBitmapData.AssignToLazIntfImage(const aImage: TLazIntfImage): Boolean;
  6291. var
  6292. rid: TRawImageDescription;
  6293. FormatDesc: TFormatDescriptor;
  6294. begin
  6295. if not Assigned(Data) then
  6296. raise EglBitmap.Create('no pixel data assigned. load data before save');
  6297. result := false;
  6298. if not Assigned(aImage) or (Format = tfEmpty) then
  6299. exit;
  6300. FormatDesc := TFormatDescriptor.Get(Format);
  6301. if FormatDesc.IsCompressed then
  6302. exit;
  6303. FillChar(rid{%H-}, SizeOf(rid), 0);
  6304. if FormatDesc.IsGrayscale then
  6305. rid.Format := ricfGray
  6306. else
  6307. rid.Format := ricfRGBA;
  6308. rid.Width := Width;
  6309. rid.Height := Height;
  6310. rid.Depth := FormatDesc.BitsPerPixel;
  6311. rid.BitOrder := riboBitsInOrder;
  6312. rid.ByteOrder := riboLSBFirst;
  6313. rid.LineOrder := riloTopToBottom;
  6314. rid.LineEnd := rileTight;
  6315. rid.BitsPerPixel := FormatDesc.BitsPerPixel;
  6316. rid.RedPrec := CountSetBits(FormatDesc.Range.r);
  6317. rid.GreenPrec := CountSetBits(FormatDesc.Range.g);
  6318. rid.BluePrec := CountSetBits(FormatDesc.Range.b);
  6319. rid.AlphaPrec := CountSetBits(FormatDesc.Range.a);
  6320. rid.RedShift := FormatDesc.Shift.r;
  6321. rid.GreenShift := FormatDesc.Shift.g;
  6322. rid.BlueShift := FormatDesc.Shift.b;
  6323. rid.AlphaShift := FormatDesc.Shift.a;
  6324. rid.MaskBitsPerPixel := 0;
  6325. rid.PaletteColorCount := 0;
  6326. aImage.DataDescription := rid;
  6327. aImage.CreateData;
  6328. if not Assigned(aImage.PixelData) then
  6329. raise EglBitmap.Create('error while creating LazIntfImage');
  6330. Move(Data^, aImage.PixelData^, FormatDesc.GetSize(Dimension));
  6331. result := true;
  6332. end;
  6333. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6334. function TglBitmapData.AssignFromLazIntfImage(const aImage: TLazIntfImage): Boolean;
  6335. var
  6336. f: TglBitmapFormat;
  6337. FormatDesc: TFormatDescriptor;
  6338. ImageData: PByte;
  6339. ImageSize: Integer;
  6340. CanCopy: Boolean;
  6341. Mask: TglBitmapRec4ul;
  6342. procedure CopyConvert;
  6343. var
  6344. bfFormat: TbmpBitfieldFormat;
  6345. pSourceLine, pDestLine: PByte;
  6346. pSourceMD, pDestMD: Pointer;
  6347. Shift, Prec: TglBitmapRec4ub;
  6348. x, y: Integer;
  6349. pixel: TglBitmapPixelData;
  6350. begin
  6351. bfFormat := TbmpBitfieldFormat.Create;
  6352. with aImage.DataDescription do begin
  6353. Prec.r := RedPrec;
  6354. Prec.g := GreenPrec;
  6355. Prec.b := BluePrec;
  6356. Prec.a := AlphaPrec;
  6357. Shift.r := RedShift;
  6358. Shift.g := GreenShift;
  6359. Shift.b := BlueShift;
  6360. Shift.a := AlphaShift;
  6361. bfFormat.SetCustomValues(BitsPerPixel, Prec, Shift);
  6362. end;
  6363. pSourceMD := bfFormat.CreateMappingData;
  6364. pDestMD := FormatDesc.CreateMappingData;
  6365. try
  6366. for y := 0 to aImage.Height-1 do begin
  6367. pSourceLine := aImage.PixelData + y {%H-}* aImage.DataDescription.BytesPerLine;
  6368. pDestLine := ImageData + y * Round(FormatDesc.BytesPerPixel * aImage.Width);
  6369. for x := 0 to aImage.Width-1 do begin
  6370. bfFormat.Unmap(pSourceLine, pixel, pSourceMD);
  6371. FormatDesc.Map(pixel, pDestLine, pDestMD);
  6372. end;
  6373. end;
  6374. finally
  6375. FormatDesc.FreeMappingData(pDestMD);
  6376. bfFormat.FreeMappingData(pSourceMD);
  6377. bfFormat.Free;
  6378. end;
  6379. end;
  6380. begin
  6381. result := false;
  6382. if not Assigned(aImage) then
  6383. exit;
  6384. with aImage.DataDescription do begin
  6385. Mask.r := (QWord(1 shl RedPrec )-1) shl RedShift;
  6386. Mask.g := (QWord(1 shl GreenPrec)-1) shl GreenShift;
  6387. Mask.b := (QWord(1 shl BluePrec )-1) shl BlueShift;
  6388. Mask.a := (QWord(1 shl AlphaPrec)-1) shl AlphaShift;
  6389. end;
  6390. FormatDesc := TFormatDescriptor.GetFromMask(Mask);
  6391. f := FormatDesc.Format;
  6392. if (f = tfEmpty) then
  6393. exit;
  6394. CanCopy :=
  6395. (FormatDesc.BitsPerPixel = aImage.DataDescription.Depth) and
  6396. (aImage.DataDescription.BitsPerPixel = aImage.DataDescription.Depth);
  6397. ImageSize := FormatDesc.GetSize(aImage.Width, aImage.Height);
  6398. ImageData := GetMem(ImageSize);
  6399. try
  6400. if CanCopy then
  6401. Move(aImage.PixelData^, ImageData^, ImageSize)
  6402. else
  6403. CopyConvert;
  6404. SetData(ImageData, f, aImage.Width, aImage.Height);
  6405. except
  6406. if Assigned(ImageData) then
  6407. FreeMem(ImageData);
  6408. raise;
  6409. end;
  6410. result := true;
  6411. end;
  6412. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6413. function TglBitmapData.AssignAlphaToLazIntfImage(const aImage: TLazIntfImage): Boolean;
  6414. var
  6415. rid: TRawImageDescription;
  6416. FormatDesc: TFormatDescriptor;
  6417. Pixel: TglBitmapPixelData;
  6418. x, y: Integer;
  6419. srcMD: Pointer;
  6420. src, dst: PByte;
  6421. begin
  6422. result := false;
  6423. if not Assigned(aImage) or (Format = tfEmpty) then
  6424. exit;
  6425. FormatDesc := TFormatDescriptor.Get(Format);
  6426. if FormatDesc.IsCompressed or not FormatDesc.HasAlpha then
  6427. exit;
  6428. FillChar(rid{%H-}, SizeOf(rid), 0);
  6429. rid.Format := ricfGray;
  6430. rid.Width := Width;
  6431. rid.Height := Height;
  6432. rid.Depth := CountSetBits(FormatDesc.Range.a);
  6433. rid.BitOrder := riboBitsInOrder;
  6434. rid.ByteOrder := riboLSBFirst;
  6435. rid.LineOrder := riloTopToBottom;
  6436. rid.LineEnd := rileTight;
  6437. rid.BitsPerPixel := 8 * Ceil(rid.Depth / 8);
  6438. rid.RedPrec := CountSetBits(FormatDesc.Range.a);
  6439. rid.GreenPrec := 0;
  6440. rid.BluePrec := 0;
  6441. rid.AlphaPrec := 0;
  6442. rid.RedShift := 0;
  6443. rid.GreenShift := 0;
  6444. rid.BlueShift := 0;
  6445. rid.AlphaShift := 0;
  6446. rid.MaskBitsPerPixel := 0;
  6447. rid.PaletteColorCount := 0;
  6448. aImage.DataDescription := rid;
  6449. aImage.CreateData;
  6450. srcMD := FormatDesc.CreateMappingData;
  6451. try
  6452. FormatDesc.PreparePixel(Pixel);
  6453. src := Data;
  6454. dst := aImage.PixelData;
  6455. for y := 0 to Height-1 do
  6456. for x := 0 to Width-1 do begin
  6457. FormatDesc.Unmap(src, Pixel, srcMD);
  6458. case rid.BitsPerPixel of
  6459. 8: begin
  6460. dst^ := Pixel.Data.a;
  6461. inc(dst);
  6462. end;
  6463. 16: begin
  6464. PWord(dst)^ := Pixel.Data.a;
  6465. inc(dst, 2);
  6466. end;
  6467. 24: begin
  6468. PByteArray(dst)^[0] := PByteArray(@Pixel.Data.a)^[0];
  6469. PByteArray(dst)^[1] := PByteArray(@Pixel.Data.a)^[1];
  6470. PByteArray(dst)^[2] := PByteArray(@Pixel.Data.a)^[2];
  6471. inc(dst, 3);
  6472. end;
  6473. 32: begin
  6474. PCardinal(dst)^ := Pixel.Data.a;
  6475. inc(dst, 4);
  6476. end;
  6477. else
  6478. raise EglBitmapUnsupportedFormat.Create(Format);
  6479. end;
  6480. end;
  6481. finally
  6482. FormatDesc.FreeMappingData(srcMD);
  6483. end;
  6484. result := true;
  6485. end;
  6486. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6487. function TglBitmapData.AddAlphaFromLazIntfImage(const aImage: TLazIntfImage; const aFunc: TglBitmapFunction; const aArgs: Pointer): Boolean;
  6488. var
  6489. data: TglBitmapData;
  6490. begin
  6491. data := TglBitmapData.Create;
  6492. try
  6493. data.AssignFromLazIntfImage(aImage);
  6494. result := AddAlphaFromDataObj(data, aFunc, aArgs);
  6495. finally
  6496. data.Free;
  6497. end;
  6498. end;
  6499. {$ENDIF}
  6500. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6501. function TglBitmapData.AddAlphaFromResource(const aInstance: Cardinal; aResource: String; aResType: PChar;
  6502. const aFunc: TglBitmapFunction; const aArgs: Pointer): Boolean;
  6503. var
  6504. rs: TResourceStream;
  6505. begin
  6506. PrepareResType(aResource, aResType);
  6507. rs := TResourceStream.Create(aInstance, aResource, aResType);
  6508. try
  6509. result := AddAlphaFromStream(rs, aFunc, aArgs);
  6510. finally
  6511. rs.Free;
  6512. end;
  6513. end;
  6514. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6515. function TglBitmapData.AddAlphaFromResourceID(const aInstance: Cardinal; const aResourceID: Integer; const aResType: PChar;
  6516. const aFunc: TglBitmapFunction; const aArgs: Pointer): Boolean;
  6517. var
  6518. rs: TResourceStream;
  6519. begin
  6520. rs := TResourceStream.CreateFromID(aInstance, aResourceID, aResType);
  6521. try
  6522. result := AddAlphaFromStream(rs, aFunc, aArgs);
  6523. finally
  6524. rs.Free;
  6525. end;
  6526. end;
  6527. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6528. function TglBitmapData.AddAlphaFromFunc(const aFunc: TglBitmapFunction; const aArgs: Pointer): Boolean;
  6529. begin
  6530. if TFormatDescriptor.Get(Format).IsCompressed then
  6531. raise EglBitmapUnsupportedFormat.Create(Format);
  6532. result := Convert(Self, aFunc, false, TFormatDescriptor.Get(Format).WithAlpha, aArgs);
  6533. end;
  6534. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6535. function TglBitmapData.AddAlphaFromFile(const aFileName: String; const aFunc: TglBitmapFunction; const aArgs: Pointer): Boolean;
  6536. var
  6537. FS: TFileStream;
  6538. begin
  6539. FS := TFileStream.Create(aFileName, fmOpenRead);
  6540. try
  6541. result := AddAlphaFromStream(FS, aFunc, aArgs);
  6542. finally
  6543. FS.Free;
  6544. end;
  6545. end;
  6546. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6547. function TglBitmapData.AddAlphaFromStream(const aStream: TStream; const aFunc: TglBitmapFunction; const aArgs: Pointer): Boolean;
  6548. var
  6549. data: TglBitmapData;
  6550. begin
  6551. data := TglBitmapData.Create(aStream);
  6552. try
  6553. result := AddAlphaFromDataObj(data, aFunc, aArgs);
  6554. finally
  6555. data.Free;
  6556. end;
  6557. end;
  6558. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6559. function TglBitmapData.AddAlphaFromDataObj(const aDataObj: TglBitmapData; aFunc: TglBitmapFunction; const aArgs: Pointer): Boolean;
  6560. var
  6561. DestData, DestData2, SourceData: pByte;
  6562. TempHeight, TempWidth: Integer;
  6563. SourceFD, DestFD: TFormatDescriptor;
  6564. SourceMD, DestMD, DestMD2: Pointer;
  6565. FuncRec: TglBitmapFunctionRec;
  6566. begin
  6567. result := false;
  6568. Assert(Assigned(Data));
  6569. Assert(Assigned(aDataObj));
  6570. Assert(Assigned(aDataObj.Data));
  6571. if ((aDataObj.Width = Width) and (aDataObj.Height = Height)) then begin
  6572. result := ConvertTo(TFormatDescriptor.Get(Format).WithAlpha);
  6573. SourceFD := TFormatDescriptor.Get(aDataObj.Format);
  6574. DestFD := TFormatDescriptor.Get(Format);
  6575. if not Assigned(aFunc) then begin
  6576. aFunc := glBitmapAlphaFunc;
  6577. FuncRec.Args := {%H-}Pointer(SourceFD.HasAlpha);
  6578. end else
  6579. FuncRec.Args := aArgs;
  6580. // Values
  6581. TempWidth := aDataObj.Width;
  6582. TempHeight := aDataObj.Height;
  6583. if (TempWidth <= 0) or (TempHeight <= 0) then
  6584. exit;
  6585. FuncRec.Sender := Self;
  6586. FuncRec.Size := Dimension;
  6587. FuncRec.Position.Fields := FuncRec.Size.Fields;
  6588. DestData := Data;
  6589. DestData2 := Data;
  6590. SourceData := aDataObj.Data;
  6591. // Mapping
  6592. SourceFD.PreparePixel(FuncRec.Source);
  6593. DestFD.PreparePixel (FuncRec.Dest);
  6594. SourceMD := SourceFD.CreateMappingData;
  6595. DestMD := DestFD.CreateMappingData;
  6596. DestMD2 := DestFD.CreateMappingData;
  6597. try
  6598. FuncRec.Position.Y := 0;
  6599. while FuncRec.Position.Y < TempHeight do begin
  6600. FuncRec.Position.X := 0;
  6601. while FuncRec.Position.X < TempWidth do begin
  6602. SourceFD.Unmap(SourceData, FuncRec.Source, SourceMD);
  6603. DestFD.Unmap (DestData, FuncRec.Dest, DestMD);
  6604. aFunc(FuncRec);
  6605. DestFD.Map(FuncRec.Dest, DestData2, DestMD2);
  6606. inc(FuncRec.Position.X);
  6607. end;
  6608. inc(FuncRec.Position.Y);
  6609. end;
  6610. finally
  6611. SourceFD.FreeMappingData(SourceMD);
  6612. DestFD.FreeMappingData(DestMD);
  6613. DestFD.FreeMappingData(DestMD2);
  6614. end;
  6615. end;
  6616. end;
  6617. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6618. function TglBitmapData.AddAlphaFromColorKey(const aRed, aGreen, aBlue: Byte; const aDeviation: Byte): Boolean;
  6619. begin
  6620. result := AddAlphaFromColorKeyFloat(aRed / $FF, aGreen / $FF, aBlue / $FF, aDeviation / $FF);
  6621. end;
  6622. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6623. function TglBitmapData.AddAlphaFromColorKeyRange(const aRed, aGreen, aBlue: Cardinal; const aDeviation: Cardinal): Boolean;
  6624. var
  6625. PixelData: TglBitmapPixelData;
  6626. begin
  6627. TFormatDescriptor.GetAlpha(Format).PreparePixel(PixelData);
  6628. result := AddAlphaFromColorKeyFloat(
  6629. aRed / PixelData.Range.r,
  6630. aGreen / PixelData.Range.g,
  6631. aBlue / PixelData.Range.b,
  6632. aDeviation / Max(PixelData.Range.r, Max(PixelData.Range.g, PixelData.Range.b)));
  6633. end;
  6634. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6635. function TglBitmapData.AddAlphaFromColorKeyFloat(const aRed, aGreen, aBlue: Single; const aDeviation: Single): Boolean;
  6636. var
  6637. values: array[0..2] of Single;
  6638. tmp: Cardinal;
  6639. i: Integer;
  6640. PixelData: TglBitmapPixelData;
  6641. begin
  6642. TFormatDescriptor.GetAlpha(Format).PreparePixel(PixelData);
  6643. with PixelData do begin
  6644. values[0] := aRed;
  6645. values[1] := aGreen;
  6646. values[2] := aBlue;
  6647. for i := 0 to 2 do begin
  6648. tmp := Trunc(Range.arr[i] * aDeviation);
  6649. Data.arr[i] := Min(Range.arr[i], Trunc(Range.arr[i] * values[i] + tmp));
  6650. Range.arr[i] := Max(0, Trunc(Range.arr[i] * values[i] - tmp));
  6651. end;
  6652. Data.a := 0;
  6653. Range.a := 0;
  6654. end;
  6655. result := AddAlphaFromFunc(glBitmapColorKeyAlphaFunc, @PixelData);
  6656. end;
  6657. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6658. function TglBitmapData.AddAlphaFromValue(const aAlpha: Byte): Boolean;
  6659. begin
  6660. result := AddAlphaFromValueFloat(aAlpha / $FF);
  6661. end;
  6662. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6663. function TglBitmapData.AddAlphaFromValueRange(const aAlpha: Cardinal): Boolean;
  6664. var
  6665. PixelData: TglBitmapPixelData;
  6666. begin
  6667. TFormatDescriptor.GetAlpha(Format).PreparePixel(PixelData);
  6668. result := AddAlphaFromValueFloat(aAlpha / PixelData.Range.a);
  6669. end;
  6670. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6671. function TglBitmapData.AddAlphaFromValueFloat(const aAlpha: Single): Boolean;
  6672. var
  6673. PixelData: TglBitmapPixelData;
  6674. begin
  6675. TFormatDescriptor.GetAlpha(Format).PreparePixel(PixelData);
  6676. with PixelData do
  6677. Data.a := Min(Range.a, Max(0, Round(Range.a * aAlpha)));
  6678. result := AddAlphaFromFunc(glBitmapValueAlphaFunc, @PixelData.Data.a);
  6679. end;
  6680. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6681. function TglBitmapData.RemoveAlpha: Boolean;
  6682. var
  6683. FormatDesc: TFormatDescriptor;
  6684. begin
  6685. result := false;
  6686. FormatDesc := TFormatDescriptor.Get(Format);
  6687. if Assigned(Data) then begin
  6688. if FormatDesc.IsCompressed or not FormatDesc.HasAlpha then
  6689. raise EglBitmapUnsupportedFormat.Create(Format);
  6690. result := ConvertTo(FormatDesc.WithoutAlpha);
  6691. end;
  6692. end;
  6693. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6694. procedure TglBitmapData.FillWithColor(const aRed, aGreen, aBlue: Byte;
  6695. const aAlpha: Byte);
  6696. begin
  6697. FillWithColorFloat(aRed/$FF, aGreen/$FF, aBlue/$FF, aAlpha/$FF);
  6698. end;
  6699. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6700. procedure TglBitmapData.FillWithColorRange(const aRed, aGreen, aBlue: Cardinal; const aAlpha: Cardinal);
  6701. var
  6702. PixelData: TglBitmapPixelData;
  6703. begin
  6704. TFormatDescriptor.GetAlpha(Format).PreparePixel(PixelData);
  6705. FillWithColorFloat(
  6706. aRed / PixelData.Range.r,
  6707. aGreen / PixelData.Range.g,
  6708. aBlue / PixelData.Range.b,
  6709. aAlpha / PixelData.Range.a);
  6710. end;
  6711. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6712. procedure TglBitmapData.FillWithColorFloat(const aRed, aGreen, aBlue: Single; const aAlpha: Single);
  6713. var
  6714. PixelData: TglBitmapPixelData;
  6715. begin
  6716. TFormatDescriptor.Get(Format).PreparePixel(PixelData);
  6717. with PixelData do begin
  6718. Data.r := Max(0, Min(Range.r, Trunc(Range.r * aRed)));
  6719. Data.g := Max(0, Min(Range.g, Trunc(Range.g * aGreen)));
  6720. Data.b := Max(0, Min(Range.b, Trunc(Range.b * aBlue)));
  6721. Data.a := Max(0, Min(Range.a, Trunc(Range.a * aAlpha)));
  6722. end;
  6723. Convert(glBitmapFillWithColorFunc, false, @PixelData);
  6724. end;
  6725. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6726. procedure TglBitmapData.SetData(const aData: PByte; const aFormat: TglBitmapFormat; const aWidth: Integer; const aHeight: Integer);
  6727. begin
  6728. if (Data <> aData) then begin
  6729. if (Assigned(Data)) then
  6730. FreeMem(Data);
  6731. fData := aData;
  6732. end;
  6733. if Assigned(fData) then begin
  6734. FillChar(fDimension, SizeOf(fDimension), 0);
  6735. if aWidth <> -1 then begin
  6736. fDimension.Fields := fDimension.Fields + [ffX];
  6737. fDimension.X := aWidth;
  6738. end;
  6739. if aHeight <> -1 then begin
  6740. fDimension.Fields := fDimension.Fields + [ffY];
  6741. fDimension.Y := aHeight;
  6742. end;
  6743. fFormat := aFormat;
  6744. end else
  6745. fFormat := tfEmpty;
  6746. UpdateScanlines;
  6747. end;
  6748. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6749. function TglBitmapData.Clone: TglBitmapData;
  6750. var
  6751. Temp: TglBitmapData;
  6752. TempPtr: PByte;
  6753. Size: Integer;
  6754. begin
  6755. result := nil;
  6756. Temp := (ClassType.Create as TglBitmapData);
  6757. try
  6758. // copy texture data if assigned
  6759. if Assigned(Data) then begin
  6760. Size := TFormatDescriptor.Get(Format).GetSize(fDimension);
  6761. GetMem(TempPtr, Size);
  6762. try
  6763. Move(Data^, TempPtr^, Size);
  6764. Temp.SetData(TempPtr, Format, Width, Height);
  6765. except
  6766. if Assigned(TempPtr) then
  6767. FreeMem(TempPtr);
  6768. raise;
  6769. end;
  6770. end else begin
  6771. TempPtr := nil;
  6772. Temp.SetData(TempPtr, Format, Width, Height);
  6773. end;
  6774. // copy properties
  6775. Temp.fFormat := Format;
  6776. result := Temp;
  6777. except
  6778. FreeAndNil(Temp);
  6779. raise;
  6780. end;
  6781. end;
  6782. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6783. procedure TglBitmapData.Invert(const aRed, aGreen, aBlue, aAlpha: Boolean);
  6784. var
  6785. mask: PtrInt;
  6786. begin
  6787. mask :=
  6788. (Byte(aRed) and 1) or
  6789. ((Byte(aGreen) and 1) shl 1) or
  6790. ((Byte(aBlue) and 1) shl 2) or
  6791. ((Byte(aAlpha) and 1) shl 3);
  6792. if (mask > 0) then
  6793. Convert(glBitmapInvertFunc, false, {%H-}Pointer(mask));
  6794. end;
  6795. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6796. type
  6797. TMatrixItem = record
  6798. X, Y: Integer;
  6799. W: Single;
  6800. end;
  6801. PglBitmapToNormalMapRec = ^TglBitmapToNormalMapRec;
  6802. TglBitmapToNormalMapRec = Record
  6803. Scale: Single;
  6804. Heights: array of Single;
  6805. MatrixU : array of TMatrixItem;
  6806. MatrixV : array of TMatrixItem;
  6807. end;
  6808. const
  6809. ONE_OVER_255 = 1 / 255;
  6810. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6811. procedure glBitmapToNormalMapPrepareFunc(var FuncRec: TglBitmapFunctionRec);
  6812. var
  6813. Val: Single;
  6814. begin
  6815. with FuncRec do begin
  6816. Val :=
  6817. Source.Data.r * LUMINANCE_WEIGHT_R +
  6818. Source.Data.g * LUMINANCE_WEIGHT_G +
  6819. Source.Data.b * LUMINANCE_WEIGHT_B;
  6820. PglBitmapToNormalMapRec(Args)^.Heights[Position.Y * Size.X + Position.X] := Val * ONE_OVER_255;
  6821. end;
  6822. end;
  6823. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6824. procedure glBitmapToNormalMapPrepareAlphaFunc(var FuncRec: TglBitmapFunctionRec);
  6825. begin
  6826. with FuncRec do
  6827. PglBitmapToNormalMapRec(Args)^.Heights[Position.Y * Size.X + Position.X] := Source.Data.a * ONE_OVER_255;
  6828. end;
  6829. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6830. procedure glBitmapToNormalMapFunc (var FuncRec: TglBitmapFunctionRec);
  6831. type
  6832. TVec = Array[0..2] of Single;
  6833. var
  6834. Idx: Integer;
  6835. du, dv: Double;
  6836. Len: Single;
  6837. Vec: TVec;
  6838. function GetHeight(X, Y: Integer): Single;
  6839. begin
  6840. with FuncRec do begin
  6841. X := Max(0, Min(Size.X -1, X));
  6842. Y := Max(0, Min(Size.Y -1, Y));
  6843. result := PglBitmapToNormalMapRec(Args)^.Heights[Y * Size.X + X];
  6844. end;
  6845. end;
  6846. begin
  6847. with FuncRec do begin
  6848. with PglBitmapToNormalMapRec(Args)^ do begin
  6849. du := 0;
  6850. for Idx := Low(MatrixU) to High(MatrixU) do
  6851. du := du + GetHeight(Position.X + MatrixU[Idx].X, Position.Y + MatrixU[Idx].Y) * MatrixU[Idx].W;
  6852. dv := 0;
  6853. for Idx := Low(MatrixU) to High(MatrixU) do
  6854. dv := dv + GetHeight(Position.X + MatrixV[Idx].X, Position.Y + MatrixV[Idx].Y) * MatrixV[Idx].W;
  6855. Vec[0] := -du * Scale;
  6856. Vec[1] := -dv * Scale;
  6857. Vec[2] := 1;
  6858. end;
  6859. // Normalize
  6860. Len := 1 / Sqrt(Sqr(Vec[0]) + Sqr(Vec[1]) + Sqr(Vec[2]));
  6861. if Len <> 0 then begin
  6862. Vec[0] := Vec[0] * Len;
  6863. Vec[1] := Vec[1] * Len;
  6864. Vec[2] := Vec[2] * Len;
  6865. end;
  6866. // Farbe zuweisem
  6867. Dest.Data.r := Trunc((Vec[0] + 1) * 127.5);
  6868. Dest.Data.g := Trunc((Vec[1] + 1) * 127.5);
  6869. Dest.Data.b := Trunc((Vec[2] + 1) * 127.5);
  6870. end;
  6871. end;
  6872. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6873. procedure TglBitmapData.GenerateNormalMap(const aFunc: TglBitmapNormalMapFunc; const aScale: Single; const aUseAlpha: Boolean);
  6874. var
  6875. Rec: TglBitmapToNormalMapRec;
  6876. procedure SetEntry (var Matrix: array of TMatrixItem; Index, X, Y: Integer; W: Single);
  6877. begin
  6878. if (Index >= Low(Matrix)) and (Index <= High(Matrix)) then begin
  6879. Matrix[Index].X := X;
  6880. Matrix[Index].Y := Y;
  6881. Matrix[Index].W := W;
  6882. end;
  6883. end;
  6884. begin
  6885. if TFormatDescriptor.Get(Format).IsCompressed then
  6886. raise EglBitmapUnsupportedFormat.Create(Format);
  6887. if aScale > 100 then
  6888. Rec.Scale := 100
  6889. else if aScale < -100 then
  6890. Rec.Scale := -100
  6891. else
  6892. Rec.Scale := aScale;
  6893. SetLength(Rec.Heights, Width * Height);
  6894. try
  6895. case aFunc of
  6896. nm4Samples: begin
  6897. SetLength(Rec.MatrixU, 2);
  6898. SetEntry(Rec.MatrixU, 0, -1, 0, -0.5);
  6899. SetEntry(Rec.MatrixU, 1, 1, 0, 0.5);
  6900. SetLength(Rec.MatrixV, 2);
  6901. SetEntry(Rec.MatrixV, 0, 0, 1, 0.5);
  6902. SetEntry(Rec.MatrixV, 1, 0, -1, -0.5);
  6903. end;
  6904. nmSobel: begin
  6905. SetLength(Rec.MatrixU, 6);
  6906. SetEntry(Rec.MatrixU, 0, -1, 1, -1.0);
  6907. SetEntry(Rec.MatrixU, 1, -1, 0, -2.0);
  6908. SetEntry(Rec.MatrixU, 2, -1, -1, -1.0);
  6909. SetEntry(Rec.MatrixU, 3, 1, 1, 1.0);
  6910. SetEntry(Rec.MatrixU, 4, 1, 0, 2.0);
  6911. SetEntry(Rec.MatrixU, 5, 1, -1, 1.0);
  6912. SetLength(Rec.MatrixV, 6);
  6913. SetEntry(Rec.MatrixV, 0, -1, 1, 1.0);
  6914. SetEntry(Rec.MatrixV, 1, 0, 1, 2.0);
  6915. SetEntry(Rec.MatrixV, 2, 1, 1, 1.0);
  6916. SetEntry(Rec.MatrixV, 3, -1, -1, -1.0);
  6917. SetEntry(Rec.MatrixV, 4, 0, -1, -2.0);
  6918. SetEntry(Rec.MatrixV, 5, 1, -1, -1.0);
  6919. end;
  6920. nm3x3: begin
  6921. SetLength(Rec.MatrixU, 6);
  6922. SetEntry(Rec.MatrixU, 0, -1, 1, -1/6);
  6923. SetEntry(Rec.MatrixU, 1, -1, 0, -1/6);
  6924. SetEntry(Rec.MatrixU, 2, -1, -1, -1/6);
  6925. SetEntry(Rec.MatrixU, 3, 1, 1, 1/6);
  6926. SetEntry(Rec.MatrixU, 4, 1, 0, 1/6);
  6927. SetEntry(Rec.MatrixU, 5, 1, -1, 1/6);
  6928. SetLength(Rec.MatrixV, 6);
  6929. SetEntry(Rec.MatrixV, 0, -1, 1, 1/6);
  6930. SetEntry(Rec.MatrixV, 1, 0, 1, 1/6);
  6931. SetEntry(Rec.MatrixV, 2, 1, 1, 1/6);
  6932. SetEntry(Rec.MatrixV, 3, -1, -1, -1/6);
  6933. SetEntry(Rec.MatrixV, 4, 0, -1, -1/6);
  6934. SetEntry(Rec.MatrixV, 5, 1, -1, -1/6);
  6935. end;
  6936. nm5x5: begin
  6937. SetLength(Rec.MatrixU, 20);
  6938. SetEntry(Rec.MatrixU, 0, -2, 2, -1 / 16);
  6939. SetEntry(Rec.MatrixU, 1, -1, 2, -1 / 10);
  6940. SetEntry(Rec.MatrixU, 2, 1, 2, 1 / 10);
  6941. SetEntry(Rec.MatrixU, 3, 2, 2, 1 / 16);
  6942. SetEntry(Rec.MatrixU, 4, -2, 1, -1 / 10);
  6943. SetEntry(Rec.MatrixU, 5, -1, 1, -1 / 8);
  6944. SetEntry(Rec.MatrixU, 6, 1, 1, 1 / 8);
  6945. SetEntry(Rec.MatrixU, 7, 2, 1, 1 / 10);
  6946. SetEntry(Rec.MatrixU, 8, -2, 0, -1 / 2.8);
  6947. SetEntry(Rec.MatrixU, 9, -1, 0, -0.5);
  6948. SetEntry(Rec.MatrixU, 10, 1, 0, 0.5);
  6949. SetEntry(Rec.MatrixU, 11, 2, 0, 1 / 2.8);
  6950. SetEntry(Rec.MatrixU, 12, -2, -1, -1 / 10);
  6951. SetEntry(Rec.MatrixU, 13, -1, -1, -1 / 8);
  6952. SetEntry(Rec.MatrixU, 14, 1, -1, 1 / 8);
  6953. SetEntry(Rec.MatrixU, 15, 2, -1, 1 / 10);
  6954. SetEntry(Rec.MatrixU, 16, -2, -2, -1 / 16);
  6955. SetEntry(Rec.MatrixU, 17, -1, -2, -1 / 10);
  6956. SetEntry(Rec.MatrixU, 18, 1, -2, 1 / 10);
  6957. SetEntry(Rec.MatrixU, 19, 2, -2, 1 / 16);
  6958. SetLength(Rec.MatrixV, 20);
  6959. SetEntry(Rec.MatrixV, 0, -2, 2, 1 / 16);
  6960. SetEntry(Rec.MatrixV, 1, -1, 2, 1 / 10);
  6961. SetEntry(Rec.MatrixV, 2, 0, 2, 0.25);
  6962. SetEntry(Rec.MatrixV, 3, 1, 2, 1 / 10);
  6963. SetEntry(Rec.MatrixV, 4, 2, 2, 1 / 16);
  6964. SetEntry(Rec.MatrixV, 5, -2, 1, 1 / 10);
  6965. SetEntry(Rec.MatrixV, 6, -1, 1, 1 / 8);
  6966. SetEntry(Rec.MatrixV, 7, 0, 1, 0.5);
  6967. SetEntry(Rec.MatrixV, 8, 1, 1, 1 / 8);
  6968. SetEntry(Rec.MatrixV, 9, 2, 1, 1 / 16);
  6969. SetEntry(Rec.MatrixV, 10, -2, -1, -1 / 16);
  6970. SetEntry(Rec.MatrixV, 11, -1, -1, -1 / 8);
  6971. SetEntry(Rec.MatrixV, 12, 0, -1, -0.5);
  6972. SetEntry(Rec.MatrixV, 13, 1, -1, -1 / 8);
  6973. SetEntry(Rec.MatrixV, 14, 2, -1, -1 / 10);
  6974. SetEntry(Rec.MatrixV, 15, -2, -2, -1 / 16);
  6975. SetEntry(Rec.MatrixV, 16, -1, -2, -1 / 10);
  6976. SetEntry(Rec.MatrixV, 17, 0, -2, -0.25);
  6977. SetEntry(Rec.MatrixV, 18, 1, -2, -1 / 10);
  6978. SetEntry(Rec.MatrixV, 19, 2, -2, -1 / 16);
  6979. end;
  6980. end;
  6981. // Daten Sammeln
  6982. if aUseAlpha and TFormatDescriptor.Get(Format).HasAlpha then
  6983. Convert(glBitmapToNormalMapPrepareAlphaFunc, false, @Rec)
  6984. else
  6985. Convert(glBitmapToNormalMapPrepareFunc, false, @Rec);
  6986. Convert(glBitmapToNormalMapFunc, false, @Rec);
  6987. finally
  6988. SetLength(Rec.Heights, 0);
  6989. end;
  6990. end;
  6991. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6992. constructor TglBitmapData.Create;
  6993. begin
  6994. inherited Create;
  6995. fFormat := glBitmapDefaultFormat;
  6996. end;
  6997. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6998. constructor TglBitmapData.Create(const aFileName: String);
  6999. begin
  7000. Create;
  7001. LoadFromFile(aFileName);
  7002. end;
  7003. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7004. constructor TglBitmapData.Create(const aStream: TStream);
  7005. begin
  7006. Create;
  7007. LoadFromStream(aStream);
  7008. end;
  7009. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7010. constructor TglBitmapData.Create(const aSize: TglBitmapSize; const aFormat: TglBitmapFormat; aData: PByte);
  7011. var
  7012. ImageSize: Integer;
  7013. begin
  7014. Create;
  7015. if not Assigned(aData) then begin
  7016. ImageSize := TFormatDescriptor.Get(aFormat).GetSize(aSize);
  7017. GetMem(aData, ImageSize);
  7018. try
  7019. FillChar(aData^, ImageSize, #$FF);
  7020. SetData(aData, aFormat, aSize.X, aSize.Y);
  7021. except
  7022. if Assigned(aData) then
  7023. FreeMem(aData);
  7024. raise;
  7025. end;
  7026. end else begin
  7027. SetData(aData, aFormat, aSize.X, aSize.Y);
  7028. end;
  7029. end;
  7030. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7031. constructor TglBitmapData.Create(const aSize: TglBitmapSize; const aFormat: TglBitmapFormat; const aFunc: TglBitmapFunction; const aArgs: Pointer);
  7032. begin
  7033. Create;
  7034. LoadFromFunc(aSize, aFormat, aFunc, aArgs);
  7035. end;
  7036. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7037. constructor TglBitmapData.Create(const aInstance: Cardinal; const aResource: String; const aResType: PChar);
  7038. begin
  7039. Create;
  7040. LoadFromResource(aInstance, aResource, aResType);
  7041. end;
  7042. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7043. constructor TglBitmapData.Create(const aInstance: Cardinal; const aResourceID: Integer; const aResType: PChar);
  7044. begin
  7045. Create;
  7046. LoadFromResourceID(aInstance, aResourceID, aResType);
  7047. end;
  7048. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7049. destructor TglBitmapData.Destroy;
  7050. begin
  7051. SetData(nil, tfEmpty);
  7052. inherited Destroy;
  7053. end;
  7054. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7055. //TglBitmap - PROTECTED///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7056. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7057. function TglBitmap.GetWidth: Integer;
  7058. begin
  7059. if (ffX in fDimension.Fields) then
  7060. result := fDimension.X
  7061. else
  7062. result := -1;
  7063. end;
  7064. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7065. function TglBitmap.GetHeight: Integer;
  7066. begin
  7067. if (ffY in fDimension.Fields) then
  7068. result := fDimension.Y
  7069. else
  7070. result := -1;
  7071. end;
  7072. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7073. procedure TglBitmap.SetCustomData(const aValue: Pointer);
  7074. begin
  7075. if fCustomData = aValue then
  7076. exit;
  7077. fCustomData := aValue;
  7078. end;
  7079. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7080. procedure TglBitmap.SetCustomName(const aValue: String);
  7081. begin
  7082. if fCustomName = aValue then
  7083. exit;
  7084. fCustomName := aValue;
  7085. end;
  7086. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7087. procedure TglBitmap.SetCustomNameW(const aValue: WideString);
  7088. begin
  7089. if fCustomNameW = aValue then
  7090. exit;
  7091. fCustomNameW := aValue;
  7092. end;
  7093. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7094. procedure TglBitmap.SetDeleteTextureOnFree(const aValue: Boolean);
  7095. begin
  7096. if fDeleteTextureOnFree = aValue then
  7097. exit;
  7098. fDeleteTextureOnFree := aValue;
  7099. end;
  7100. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7101. procedure TglBitmap.SetID(const aValue: Cardinal);
  7102. begin
  7103. if fID = aValue then
  7104. exit;
  7105. fID := aValue;
  7106. end;
  7107. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7108. procedure TglBitmap.SetMipMap(const aValue: TglBitmapMipMap);
  7109. begin
  7110. if fMipMap = aValue then
  7111. exit;
  7112. fMipMap := aValue;
  7113. end;
  7114. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7115. procedure TglBitmap.SetTarget(const aValue: Cardinal);
  7116. begin
  7117. if fTarget = aValue then
  7118. exit;
  7119. fTarget := aValue;
  7120. end;
  7121. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7122. procedure TglBitmap.SetAnisotropic(const aValue: Integer);
  7123. {$IF NOT DEFINED(OPENGL_ES) OR DEFINED(OPENGL_ES_EXT)}
  7124. var
  7125. MaxAnisotropic: Integer;
  7126. {$IFEND}
  7127. begin
  7128. fAnisotropic := aValue;
  7129. if (ID > 0) then begin
  7130. {$IF NOT DEFINED(OPENGL_ES) OR DEFINED(OPENGL_ES_EXT)}
  7131. if GL_EXT_texture_filter_anisotropic then begin
  7132. if fAnisotropic > 0 then begin
  7133. Bind(false);
  7134. glGetIntegerv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, @MaxAnisotropic);
  7135. if aValue > MaxAnisotropic then
  7136. fAnisotropic := MaxAnisotropic;
  7137. glTexParameteri(Target, GL_TEXTURE_MAX_ANISOTROPY_EXT, fAnisotropic);
  7138. end;
  7139. end else begin
  7140. fAnisotropic := 0;
  7141. end;
  7142. {$ELSE}
  7143. fAnisotropic := 0;
  7144. {$IFEND}
  7145. end;
  7146. end;
  7147. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7148. procedure TglBitmap.CreateID;
  7149. begin
  7150. if (ID <> 0) then
  7151. glDeleteTextures(1, @fID);
  7152. glGenTextures(1, @fID);
  7153. Bind(false);
  7154. end;
  7155. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7156. procedure TglBitmap.SetupParameters({$IFNDEF OPENGL_ES}out aBuildWithGlu: Boolean{$ENDIF});
  7157. begin
  7158. // Set Up Parameters
  7159. SetWrap(fWrapS, fWrapT, fWrapR);
  7160. SetFilter(fFilterMin, fFilterMag);
  7161. SetAnisotropic(fAnisotropic);
  7162. {$IFNDEF OPENGL_ES}
  7163. SetBorderColor(fBorderColor[0], fBorderColor[1], fBorderColor[2], fBorderColor[3]);
  7164. if (GL_ARB_texture_swizzle or GL_EXT_texture_swizzle or GL_VERSION_3_3) then
  7165. SetSwizzle(fSwizzle[0], fSwizzle[1], fSwizzle[2], fSwizzle[3]);
  7166. {$ENDIF}
  7167. {$IFNDEF OPENGL_ES}
  7168. // Mip Maps Generation Mode
  7169. aBuildWithGlu := false;
  7170. if (MipMap = mmMipmap) then begin
  7171. if (GL_VERSION_1_4 or GL_SGIS_generate_mipmap) then
  7172. glTexParameteri(Target, GL_GENERATE_MIPMAP, GL_TRUE)
  7173. else
  7174. aBuildWithGlu := true;
  7175. end else if (MipMap = mmMipmapGlu) then
  7176. aBuildWithGlu := true;
  7177. {$ELSE}
  7178. if (MipMap = mmMipmap) then
  7179. glTexParameteri(Target, GL_GENERATE_MIPMAP, GL_TRUE);
  7180. {$ENDIF}
  7181. end;
  7182. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7183. //TglBitmap - PUBLIC//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7184. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7185. procedure TglBitmap.AfterConstruction;
  7186. begin
  7187. inherited AfterConstruction;
  7188. fID := 0;
  7189. fTarget := 0;
  7190. {$IFNDEF OPENGL_ES}
  7191. fIsResident := false;
  7192. {$ENDIF}
  7193. fMipMap := glBitmapDefaultMipmap;
  7194. fDeleteTextureOnFree := glBitmapGetDefaultDeleteTextureOnFree;
  7195. glBitmapGetDefaultFilter (fFilterMin, fFilterMag);
  7196. glBitmapGetDefaultTextureWrap(fWrapS, fWrapT, fWrapR);
  7197. {$IFNDEF OPENGL_ES}
  7198. glBitmapGetDefaultSwizzle (fSwizzle[0], fSwizzle[1], fSwizzle[2], fSwizzle[3]);
  7199. {$ENDIF}
  7200. end;
  7201. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7202. procedure TglBitmap.BeforeDestruction;
  7203. begin
  7204. if (fID > 0) and fDeleteTextureOnFree then
  7205. glDeleteTextures(1, @fID);
  7206. inherited BeforeDestruction;
  7207. end;
  7208. {$IFNDEF OPENGL_ES}
  7209. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7210. procedure TglBitmap.SetBorderColor(const aRed, aGreen, aBlue, aAlpha: Single);
  7211. begin
  7212. fBorderColor[0] := aRed;
  7213. fBorderColor[1] := aGreen;
  7214. fBorderColor[2] := aBlue;
  7215. fBorderColor[3] := aAlpha;
  7216. if (ID > 0) then begin
  7217. Bind(false);
  7218. glTexParameterfv(Target, GL_TEXTURE_BORDER_COLOR, @fBorderColor[0]);
  7219. end;
  7220. end;
  7221. {$ENDIF}
  7222. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7223. procedure TglBitmap.SetFilter(const aMin, aMag: GLenum);
  7224. begin
  7225. //check MIN filter
  7226. case aMin of
  7227. GL_NEAREST:
  7228. fFilterMin := GL_NEAREST;
  7229. GL_LINEAR:
  7230. fFilterMin := GL_LINEAR;
  7231. GL_NEAREST_MIPMAP_NEAREST:
  7232. fFilterMin := GL_NEAREST_MIPMAP_NEAREST;
  7233. GL_LINEAR_MIPMAP_NEAREST:
  7234. fFilterMin := GL_LINEAR_MIPMAP_NEAREST;
  7235. GL_NEAREST_MIPMAP_LINEAR:
  7236. fFilterMin := GL_NEAREST_MIPMAP_LINEAR;
  7237. GL_LINEAR_MIPMAP_LINEAR:
  7238. fFilterMin := GL_LINEAR_MIPMAP_LINEAR;
  7239. else
  7240. raise EglBitmap.Create('SetFilter - Unknow MIN filter.');
  7241. end;
  7242. //check MAG filter
  7243. case aMag of
  7244. GL_NEAREST:
  7245. fFilterMag := GL_NEAREST;
  7246. GL_LINEAR:
  7247. fFilterMag := GL_LINEAR;
  7248. else
  7249. raise EglBitmap.Create('SetFilter - Unknow MAG filter.');
  7250. end;
  7251. //apply filter
  7252. if (ID > 0) then begin
  7253. Bind(false);
  7254. glTexParameteri(Target, GL_TEXTURE_MAG_FILTER, fFilterMag);
  7255. if (MipMap = mmNone) {$IFNDEF OPENGL_ES}or (Target = GL_TEXTURE_RECTANGLE){$ENDIF} then begin
  7256. case fFilterMin of
  7257. GL_NEAREST, GL_LINEAR:
  7258. glTexParameteri(Target, GL_TEXTURE_MIN_FILTER, fFilterMin);
  7259. GL_NEAREST_MIPMAP_NEAREST, GL_NEAREST_MIPMAP_LINEAR:
  7260. glTexParameteri(Target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  7261. GL_LINEAR_MIPMAP_NEAREST, GL_LINEAR_MIPMAP_LINEAR:
  7262. glTexParameteri(Target, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  7263. end;
  7264. end else
  7265. glTexParameteri(Target, GL_TEXTURE_MIN_FILTER, fFilterMin);
  7266. end;
  7267. end;
  7268. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7269. procedure TglBitmap.SetWrap(const S: GLenum; const T: GLenum; const R: GLenum);
  7270. procedure CheckAndSetWrap(const aValue: Cardinal; var aTarget: Cardinal);
  7271. begin
  7272. case aValue of
  7273. {$IFNDEF OPENGL_ES}
  7274. GL_CLAMP:
  7275. aTarget := GL_CLAMP;
  7276. {$ENDIF}
  7277. GL_REPEAT:
  7278. aTarget := GL_REPEAT;
  7279. GL_CLAMP_TO_EDGE: begin
  7280. {$IFNDEF OPENGL_ES}
  7281. if not GL_VERSION_1_2 and not GL_EXT_texture_edge_clamp then
  7282. aTarget := GL_CLAMP
  7283. else
  7284. {$ENDIF}
  7285. aTarget := GL_CLAMP_TO_EDGE;
  7286. end;
  7287. {$IFNDEF OPENGL_ES}
  7288. GL_CLAMP_TO_BORDER: begin
  7289. if GL_VERSION_1_3 or GL_ARB_texture_border_clamp then
  7290. aTarget := GL_CLAMP_TO_BORDER
  7291. else
  7292. aTarget := GL_CLAMP;
  7293. end;
  7294. {$ENDIF}
  7295. {$IF NOT DEFINED(OPENGL_ES) OR DEFINED(OPENGL_ES_2_0)}
  7296. GL_MIRRORED_REPEAT: begin
  7297. {$IFNDEF OPENGL_ES}
  7298. if GL_VERSION_1_4 or GL_ARB_texture_mirrored_repeat or GL_IBM_texture_mirrored_repeat then
  7299. {$ELSE}
  7300. if GL_VERSION_2_0 then
  7301. {$ENDIF}
  7302. aTarget := GL_MIRRORED_REPEAT
  7303. else
  7304. raise EglBitmap.Create('SetWrap - Unsupported Texturewrap GL_MIRRORED_REPEAT (S).');
  7305. end;
  7306. {$IFEND}
  7307. else
  7308. raise EglBitmap.Create('SetWrap - Unknow Texturewrap');
  7309. end;
  7310. end;
  7311. begin
  7312. CheckAndSetWrap(S, fWrapS);
  7313. CheckAndSetWrap(T, fWrapT);
  7314. CheckAndSetWrap(R, fWrapR);
  7315. if (ID > 0) then begin
  7316. Bind(false);
  7317. glTexParameteri(Target, GL_TEXTURE_WRAP_S, fWrapS);
  7318. glTexParameteri(Target, GL_TEXTURE_WRAP_T, fWrapT);
  7319. {$IF NOT DEFINED(OPENGL_ES) OR DEFINED(OPENGL_ES_3_0)}
  7320. {$IFDEF OPENGL_ES} if GL_VERSION_3_0 then{$ENDIF}
  7321. glTexParameteri(Target, GL_TEXTURE_WRAP_R, fWrapR);
  7322. {$IFEND}
  7323. end;
  7324. end;
  7325. {$IF NOT DEFINED(OPENGL_ES) OR DEFINED(OPENGL_ES_3_0)}
  7326. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7327. procedure TglBitmap.SetSwizzle(const r, g, b, a: GLenum);
  7328. procedure CheckAndSetValue(const aValue: GLenum; const aIndex: Integer);
  7329. begin
  7330. if (aValue = GL_ZERO) or (aValue = GL_ONE) or (aValue = GL_ALPHA) or
  7331. (aValue = GL_RED) or (aValue = GL_GREEN) or (aValue = GL_BLUE) then
  7332. fSwizzle[aIndex] := aValue
  7333. else
  7334. raise EglBitmap.Create('SetSwizzle - Unknow Swizle Value');
  7335. end;
  7336. begin
  7337. {$IFNDEF OPENGL_ES}
  7338. if not (GL_ARB_texture_swizzle or GL_EXT_texture_swizzle or GL_VERSION_3_3) then
  7339. raise EglBitmapNotSupported.Create('texture swizzle is not supported');
  7340. {$ELSE}
  7341. if not GL_VERSION_3_0 then
  7342. raise EglBitmapNotSupported.Create('texture swizzle is not supported');
  7343. {$ENDIF}
  7344. CheckAndSetValue(r, 0);
  7345. CheckAndSetValue(g, 1);
  7346. CheckAndSetValue(b, 2);
  7347. CheckAndSetValue(a, 3);
  7348. if (ID > 0) then begin
  7349. Bind(false);
  7350. {$IFNDEF OPENGL_ES}
  7351. glTexParameteriv(Target, GL_TEXTURE_SWIZZLE_RGBA, PGLint(@fSwizzle[0]));
  7352. {$ELSE}
  7353. glTexParameteriv(Target, GL_TEXTURE_SWIZZLE_R, PGLint(@fSwizzle[0]));
  7354. glTexParameteriv(Target, GL_TEXTURE_SWIZZLE_G, PGLint(@fSwizzle[1]));
  7355. glTexParameteriv(Target, GL_TEXTURE_SWIZZLE_B, PGLint(@fSwizzle[2]));
  7356. glTexParameteriv(Target, GL_TEXTURE_SWIZZLE_A, PGLint(@fSwizzle[3]));
  7357. {$ENDIF}
  7358. end;
  7359. end;
  7360. {$IFEND}
  7361. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7362. procedure TglBitmap.Bind(const aEnableTextureUnit: Boolean);
  7363. begin
  7364. if aEnableTextureUnit then
  7365. glEnable(Target);
  7366. if (ID > 0) then
  7367. glBindTexture(Target, ID);
  7368. end;
  7369. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7370. procedure TglBitmap.Unbind(const aDisableTextureUnit: Boolean);
  7371. begin
  7372. if aDisableTextureUnit then
  7373. glDisable(Target);
  7374. glBindTexture(Target, 0);
  7375. end;
  7376. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7377. procedure TglBitmap.UploadData(const aDataObj: TglBitmapData; const aCheckSize: Boolean);
  7378. var
  7379. w, h: Integer;
  7380. begin
  7381. w := aDataObj.Width;
  7382. h := aDataObj.Height;
  7383. fDimension.Fields := [];
  7384. if (w > 0) then
  7385. fDimension.Fields := fDimension.Fields + [ffX];
  7386. if (h > 0) then
  7387. fDimension.Fields := fDimension.Fields + [ffY];
  7388. fDimension.X := w;
  7389. fDimension.Y := h;
  7390. end;
  7391. {$IFNDEF OPENGL_ES}
  7392. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7393. function TglBitmap.DownloadData(const aDataObj: TglBitmapData): Boolean;
  7394. var
  7395. Temp: PByte;
  7396. TempWidth, TempHeight: Integer;
  7397. TempIntFormat: GLint;
  7398. IntFormat: TglBitmapFormat;
  7399. FormatDesc: TFormatDescriptor;
  7400. begin
  7401. result := false;
  7402. Bind;
  7403. // Request Data
  7404. glGetTexLevelParameteriv(Target, 0, GL_TEXTURE_WIDTH, @TempWidth);
  7405. glGetTexLevelParameteriv(Target, 0, GL_TEXTURE_HEIGHT, @TempHeight);
  7406. glGetTexLevelParameteriv(Target, 0, GL_TEXTURE_INTERNAL_FORMAT, @TempIntFormat);
  7407. FormatDesc := (TglBitmapFormatDescriptor.GetByFormat(TempIntFormat) as TFormatDescriptor);
  7408. IntFormat := FormatDesc.Format;
  7409. // Getting data from OpenGL
  7410. FormatDesc := TFormatDescriptor.Get(IntFormat);
  7411. GetMem(Temp, FormatDesc.GetSize(TempWidth, TempHeight));
  7412. try
  7413. if FormatDesc.IsCompressed then begin
  7414. if not Assigned(glGetCompressedTexImage) then
  7415. raise EglBitmap.Create('compressed formats not supported by video adapter');
  7416. glGetCompressedTexImage(Target, 0, Temp)
  7417. end else
  7418. glGetTexImage(Target, 0, FormatDesc.glFormat, FormatDesc.glDataFormat, Temp);
  7419. aDataObj.SetData(Temp, IntFormat, TempWidth, TempHeight);
  7420. result := true;
  7421. except
  7422. if Assigned(Temp) then
  7423. FreeMem(Temp);
  7424. raise;
  7425. end;
  7426. end;
  7427. {$ENDIF}
  7428. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7429. constructor TglBitmap.Create;
  7430. begin
  7431. if (ClassType = TglBitmap) then
  7432. raise EglBitmap.Create('Don''t create TglBitmap directly. Use one of the deviated classes (TglBitmap2D) instead.');
  7433. inherited Create;
  7434. end;
  7435. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7436. constructor TglBitmap.Create(const aData: TglBitmapData);
  7437. begin
  7438. Create;
  7439. UploadData(aData);
  7440. end;
  7441. {$IFNDEF OPENGL_ES}
  7442. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7443. //TglBitmap1D/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7444. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7445. procedure TglBitmap1D.UploadDataIntern(const aDataObj: TglBitmapData; const aBuildWithGlu: Boolean);
  7446. var
  7447. fd: TglBitmapFormatDescriptor;
  7448. begin
  7449. // Upload data
  7450. fd := aDataObj.FormatDescriptor;
  7451. if (fd.glFormat = 0) or (fd.glInternalFormat = 0) or (fd.glDataFormat = 0) then
  7452. raise EglBitmap.Create('format is not supported by video adapter, please convert before uploading data');
  7453. if fd.IsCompressed then begin
  7454. if not Assigned(glCompressedTexImage1D) then
  7455. raise EglBitmap.Create('compressed formats not supported by video adapter');
  7456. glCompressedTexImage1D(Target, 0, fd.glInternalFormat, aDataObj.Width, 0, fd.GetSize(aDataObj.Width, 1), aDataObj.Data)
  7457. end else if aBuildWithGlu then
  7458. gluBuild1DMipmaps(Target, fd.glInternalFormat, aDataObj.Width, fd.glFormat, fd.glDataFormat, aDataObj.Data)
  7459. else
  7460. glTexImage1D(Target, 0, fd.glInternalFormat, aDataObj.Width, 0, fd.glFormat, fd.glDataFormat, aDataObj.Data);
  7461. end;
  7462. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7463. procedure TglBitmap1D.AfterConstruction;
  7464. begin
  7465. inherited;
  7466. Target := GL_TEXTURE_1D;
  7467. end;
  7468. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7469. procedure TglBitmap1D.UploadData(const aDataObj: TglBitmapData; const aCheckSize: Boolean);
  7470. var
  7471. BuildWithGlu, TexRec: Boolean;
  7472. TexSize: Integer;
  7473. begin
  7474. if not Assigned(aDataObj) then
  7475. exit;
  7476. // Check Texture Size
  7477. if (aCheckSize) then begin
  7478. glGetIntegerv(GL_MAX_TEXTURE_SIZE, @TexSize);
  7479. if (aDataObj.Width > TexSize) then
  7480. raise EglBitmapSizeToLarge.Create('TglBitmap1D.GenTexture - The size for the texture is to large. It''s may be not conform with the Hardware.');
  7481. TexRec := (GL_ARB_texture_rectangle or GL_EXT_texture_rectangle or GL_NV_texture_rectangle) and
  7482. (Target = GL_TEXTURE_RECTANGLE);
  7483. if not (IsPowerOfTwo(aDataObj.Width) or GL_ARB_texture_non_power_of_two or GL_VERSION_2_0 or TexRec) then
  7484. raise EglBitmapNonPowerOfTwo.Create('TglBitmap1D.GenTexture - Rendercontex dosn''t support non power of two texture.');
  7485. end;
  7486. if (fID = 0) then
  7487. CreateID;
  7488. SetupParameters(BuildWithGlu);
  7489. UploadDataIntern(aDataObj, BuildWithGlu);
  7490. glAreTexturesResident(1, @fID, @fIsResident);
  7491. inherited UploadData(aDataObj, aCheckSize);
  7492. end;
  7493. {$ENDIF}
  7494. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7495. //TglBitmap2D/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7496. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7497. procedure TglBitmap2D.UploadDataIntern(const aDataObj: TglBitmapData; const aTarget: GLenum; const aBuildWithGlu: Boolean);
  7498. var
  7499. fd: TglBitmapFormatDescriptor;
  7500. begin
  7501. fd := aDataObj.FormatDescriptor;
  7502. if (fd.glFormat = 0) or (fd.glInternalFormat = 0) or (fd.glDataFormat = 0) then
  7503. raise EglBitmap.Create('format is not supported by video adapter, please convert before uploading data');
  7504. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  7505. if fd.IsCompressed then begin
  7506. if not Assigned(glCompressedTexImage2D) then
  7507. raise EglBitmap.Create('compressed formats not supported by video adapter');
  7508. glCompressedTexImage2D(aTarget, 0, fd.glInternalFormat, aDataObj.Width, aDataObj.Height, 0, fd.GetSize(fDimension), aDataObj.Data)
  7509. {$IFNDEF OPENGL_ES}
  7510. end else if aBuildWithGlu then begin
  7511. gluBuild2DMipmaps(aTarget, fd.ChannelCount, aDataObj.Width, aDataObj.Height, fd.glFormat, fd.glDataFormat, aDataObj.Data)
  7512. {$ENDIF}
  7513. end else begin
  7514. glTexImage2D(aTarget, 0, fd.glInternalFormat, aDataObj.Width, aDataObj.Height, 0, fd.glFormat, fd.glDataFormat, aDataObj.Data);
  7515. end;
  7516. end;
  7517. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7518. procedure TglBitmap2D.AfterConstruction;
  7519. begin
  7520. inherited;
  7521. Target := GL_TEXTURE_2D;
  7522. end;
  7523. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7524. procedure TglBitmap2D.UploadData(const aDataObj: TglBitmapData; const aCheckSize: Boolean);
  7525. var
  7526. {$IFNDEF OPENGL_ES}
  7527. BuildWithGlu, TexRec: Boolean;
  7528. {$ENDIF}
  7529. PotTex: Boolean;
  7530. TexSize: Integer;
  7531. begin
  7532. if not Assigned(aDataObj) then
  7533. exit;
  7534. // Check Texture Size
  7535. if (aCheckSize) then begin
  7536. glGetIntegerv(GL_MAX_TEXTURE_SIZE, @TexSize);
  7537. if ((aDataObj.Width > TexSize) or (aDataObj.Height > TexSize)) then
  7538. raise EglBitmapSizeToLarge.Create('TglBitmap2D.GenTexture - The size for the texture is to large. It''s may be not conform with the Hardware.');
  7539. PotTex := IsPowerOfTwo(aDataObj.Width) and IsPowerOfTwo(aDataObj.Height);
  7540. {$IF NOT DEFINED(OPENGL_ES)}
  7541. TexRec := (GL_ARB_texture_rectangle or GL_EXT_texture_rectangle or GL_NV_texture_rectangle) and (Target = GL_TEXTURE_RECTANGLE);
  7542. if not (PotTex or GL_ARB_texture_non_power_of_two or GL_VERSION_2_0 or TexRec) then
  7543. raise EglBitmapNonPowerOfTwo.Create('TglBitmap2D.GenTexture - Rendercontex dosn''t support non power of two texture.');
  7544. {$ELSEIF DEFINED(OPENGL_ES_EXT)}
  7545. if not PotTex and not GL_OES_texture_npot then
  7546. raise EglBitmapNonPowerOfTwo.Create('TglBitmap2D.GenTexture - Rendercontex dosn''t support non power of two texture.');
  7547. {$ELSE}
  7548. if not PotTex then
  7549. raise EglBitmapNonPowerOfTwo.Create('TglBitmap2D.GenTexture - Rendercontex dosn''t support non power of two texture.');
  7550. {$IFEND}
  7551. end;
  7552. if (fID = 0) then
  7553. CreateID;
  7554. SetupParameters({$IFNDEF OPENGL_ES}BuildWithGlu{$ENDIF});
  7555. UploadDataIntern(aDataObj, Target{$IFNDEF OPENGL_ES}, BuildWithGlu{$ENDIF});
  7556. {$IFNDEF OPENGL_ES}
  7557. glAreTexturesResident(1, @fID, @fIsResident);
  7558. {$ENDIF}
  7559. inherited UploadData(aDataObj, aCheckSize);
  7560. end;
  7561. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7562. class procedure TglBitmap2D.GrabScreen(const aTop, aLeft, aRight, aBottom: Integer; const aFormat: TglBitmapFormat; const aDataObj: TglBitmapData);
  7563. var
  7564. Temp: pByte;
  7565. Size, w, h: Integer;
  7566. FormatDesc: TFormatDescriptor;
  7567. begin
  7568. FormatDesc := TFormatDescriptor.Get(aFormat);
  7569. if FormatDesc.IsCompressed then
  7570. raise EglBitmapUnsupportedFormat.Create(aFormat);
  7571. w := aRight - aLeft;
  7572. h := aBottom - aTop;
  7573. Size := FormatDesc.GetSize(w, h);
  7574. GetMem(Temp, Size);
  7575. try
  7576. glPixelStorei(GL_PACK_ALIGNMENT, 1);
  7577. glReadPixels(aLeft, aTop, w, h, FormatDesc.glFormat, FormatDesc.glDataFormat, Temp);
  7578. aDataObj.SetData(Temp, aFormat, w, h);
  7579. aDataObj.FlipVert;
  7580. except
  7581. if Assigned(Temp) then
  7582. FreeMem(Temp);
  7583. raise;
  7584. end;
  7585. end;
  7586. {$IF NOT DEFINED(OPENGL_ES) OR DEFINED(OPENGL_ES_2_0)}
  7587. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7588. //TglBitmapCubeMap////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7589. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7590. procedure TglBitmapCubeMap.AfterConstruction;
  7591. begin
  7592. inherited;
  7593. {$IFNDEF OPENGL_ES}
  7594. if not (GL_VERSION_1_3 or GL_ARB_texture_cube_map or GL_EXT_texture_cube_map) then
  7595. raise EglBitmap.Create('TglBitmapCubeMap.AfterConstruction - CubeMaps are unsupported.');
  7596. {$ELSE}
  7597. if not (GL_VERSION_2_0) then
  7598. raise EglBitmap.Create('TglBitmapCubeMap.AfterConstruction - CubeMaps are unsupported.');
  7599. {$ENDIF}
  7600. SetWrap;
  7601. Target := GL_TEXTURE_CUBE_MAP;
  7602. {$IFNDEF OPENGL_ES}
  7603. fGenMode := GL_REFLECTION_MAP;
  7604. {$ENDIF}
  7605. end;
  7606. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7607. procedure TglBitmapCubeMap.UploadData(const aDataObj: TglBitmapData; const aCheckSize: Boolean);
  7608. begin
  7609. Assert(false, 'TglBitmapCubeMap.UploadData - Don''t call UploadData directly, use UploadCubeMap instead');
  7610. end;
  7611. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7612. procedure TglBitmapCubeMap.UploadCubeMap(const aDataObj: TglBitmapData; const aCubeTarget: Cardinal; const aCheckSize: Boolean);
  7613. var
  7614. {$IFNDEF OPENGL_ES}
  7615. BuildWithGlu: Boolean;
  7616. {$ENDIF}
  7617. TexSize: Integer;
  7618. begin
  7619. if (aCheckSize) then begin
  7620. glGetIntegerv(GL_MAX_CUBE_MAP_TEXTURE_SIZE, @TexSize);
  7621. if (aDataObj.Width > TexSize) or (aDataObj.Height > TexSize) then
  7622. raise EglBitmapSizeToLarge.Create('TglBitmapCubeMap.GenerateCubeMap - The size for the Cubemap is to large. It''s may be not conform with the Hardware.');
  7623. {$IF NOT DEFINED(OPENGL_ES)}
  7624. if not ((IsPowerOfTwo(aDataObj.Width) and IsPowerOfTwo(aDataObj.Height)) or GL_VERSION_2_0 or GL_ARB_texture_non_power_of_two) then
  7625. raise EglBitmapNonPowerOfTwo.Create('TglBitmapCubeMap.GenerateCubeMap - Cubemaps dosn''t support non power of two texture.');
  7626. {$ELSEIF DEFINED(OPENGL_ES_EXT)}
  7627. if not (IsPowerOfTwo(aDataObj.Width) and IsPowerOfTwo(aDataObj.Height)) and not GL_OES_texture_npot then
  7628. raise EglBitmapNonPowerOfTwo.Create('TglBitmapCubeMap.GenerateCubeMap - Cubemaps dosn''t support non power of two texture.');
  7629. {$ELSE}
  7630. if not (IsPowerOfTwo(aDataObj.Width) and IsPowerOfTwo(aDataObj.Height)) then
  7631. raise EglBitmapNonPowerOfTwo.Create('TglBitmapCubeMap.GenerateCubeMap - Cubemaps dosn''t support non power of two texture.');
  7632. {$IFEND}
  7633. end;
  7634. if (fID = 0) then
  7635. CreateID;
  7636. SetupParameters({$IFNDEF OPENGL_ES}BuildWithGlu{$ENDIF});
  7637. UploadDataIntern(aDataObj, aCubeTarget{$IFNDEF OPENGL_ES}, BuildWithGlu{$ENDIF});
  7638. inherited UploadData(aDataObj, aCheckSize);
  7639. end;
  7640. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7641. procedure TglBitmapCubeMap.Bind({$IFNDEF OPENGL_ES}const aEnableTexCoordsGen: Boolean;{$ENDIF} const aEnableTextureUnit: Boolean);
  7642. begin
  7643. inherited Bind (aEnableTextureUnit);
  7644. {$IFNDEF OPENGL_ES}
  7645. if aEnableTexCoordsGen then begin
  7646. glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, fGenMode);
  7647. glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, fGenMode);
  7648. glTexGeni(GL_R, GL_TEXTURE_GEN_MODE, fGenMode);
  7649. glEnable(GL_TEXTURE_GEN_S);
  7650. glEnable(GL_TEXTURE_GEN_T);
  7651. glEnable(GL_TEXTURE_GEN_R);
  7652. end;
  7653. {$ENDIF}
  7654. end;
  7655. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7656. procedure TglBitmapCubeMap.Unbind({$IFNDEF OPENGL_ES}const aDisableTexCoordsGen: Boolean;{$ENDIF} const aDisableTextureUnit: Boolean);
  7657. begin
  7658. inherited Unbind(aDisableTextureUnit);
  7659. {$IFNDEF OPENGL_ES}
  7660. if aDisableTexCoordsGen then begin
  7661. glDisable(GL_TEXTURE_GEN_S);
  7662. glDisable(GL_TEXTURE_GEN_T);
  7663. glDisable(GL_TEXTURE_GEN_R);
  7664. end;
  7665. {$ENDIF}
  7666. end;
  7667. {$IFEND}
  7668. {$IF NOT DEFINED(OPENGL_ES) OR DEFINED(OPENGL_ES_2_0)}
  7669. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7670. //TglBitmapNormalMap//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7671. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7672. type
  7673. TVec = Array[0..2] of Single;
  7674. TglBitmapNormalMapGetVectorFunc = procedure (out aVec: TVec; const aPosition: TglBitmapPixelPosition; const aHalfSize: Integer);
  7675. PglBitmapNormalMapRec = ^TglBitmapNormalMapRec;
  7676. TglBitmapNormalMapRec = record
  7677. HalfSize : Integer;
  7678. Func: TglBitmapNormalMapGetVectorFunc;
  7679. end;
  7680. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7681. procedure glBitmapNormalMapPosX(out aVec: TVec; const aPosition: TglBitmapPixelPosition; const aHalfSize: Integer);
  7682. begin
  7683. aVec[0] := aHalfSize;
  7684. aVec[1] := - (aPosition.Y + 0.5 - aHalfSize);
  7685. aVec[2] := - (aPosition.X + 0.5 - aHalfSize);
  7686. end;
  7687. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7688. procedure glBitmapNormalMapNegX(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 glBitmapNormalMapPosY(out aVec: TVec; const aPosition: TglBitmapPixelPosition; const aHalfSize: Integer);
  7696. begin
  7697. aVec[0] := aPosition.X + 0.5 - aHalfSize;
  7698. aVec[1] := aHalfSize;
  7699. aVec[2] := aPosition.Y + 0.5 - aHalfSize;
  7700. end;
  7701. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7702. procedure glBitmapNormalMapNegY(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 glBitmapNormalMapPosZ(out aVec: TVec; const aPosition: TglBitmapPixelPosition; const aHalfSize: Integer);
  7710. begin
  7711. aVec[0] := aPosition.X + 0.5 - aHalfSize;
  7712. aVec[1] := - (aPosition.Y + 0.5 - aHalfSize);
  7713. aVec[2] := aHalfSize;
  7714. end;
  7715. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7716. procedure glBitmapNormalMapNegZ(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 glBitmapNormalMapFunc(var FuncRec: TglBitmapFunctionRec);
  7724. var
  7725. i: Integer;
  7726. Vec: TVec;
  7727. Len: Single;
  7728. begin
  7729. with FuncRec do begin
  7730. with PglBitmapNormalMapRec(Args)^ do begin
  7731. Func(Vec, Position, HalfSize);
  7732. // Normalize
  7733. Len := 1 / Sqrt(Sqr(Vec[0]) + Sqr(Vec[1]) + Sqr(Vec[2]));
  7734. if Len <> 0 then begin
  7735. Vec[0] := Vec[0] * Len;
  7736. Vec[1] := Vec[1] * Len;
  7737. Vec[2] := Vec[2] * Len;
  7738. end;
  7739. // Scale Vector and AddVectro
  7740. Vec[0] := Vec[0] * 0.5 + 0.5;
  7741. Vec[1] := Vec[1] * 0.5 + 0.5;
  7742. Vec[2] := Vec[2] * 0.5 + 0.5;
  7743. end;
  7744. // Set Color
  7745. for i := 0 to 2 do
  7746. Dest.Data.arr[i] := Round(Vec[i] * 255);
  7747. end;
  7748. end;
  7749. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7750. procedure TglBitmapNormalMap.AfterConstruction;
  7751. begin
  7752. inherited;
  7753. {$IFNDEF OPENGL_ES}
  7754. fGenMode := GL_NORMAL_MAP;
  7755. {$ENDIF}
  7756. end;
  7757. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7758. procedure TglBitmapNormalMap.GenerateNormalMap(const aSize: Integer; const aCheckSize: Boolean);
  7759. var
  7760. Rec: TglBitmapNormalMapRec;
  7761. SizeRec: TglBitmapSize;
  7762. DataObj: TglBitmapData;
  7763. begin
  7764. Rec.HalfSize := aSize div 2;
  7765. SizeRec.Fields := [ffX, ffY];
  7766. SizeRec.X := aSize;
  7767. SizeRec.Y := aSize;
  7768. DataObj := TglBitmapData.Create;
  7769. try
  7770. // Positive X
  7771. Rec.Func := glBitmapNormalMapPosX;
  7772. DataObj.LoadFromFunc(SizeRec, tfBGR8ub3, glBitmapNormalMapFunc, @Rec);
  7773. UploadCubeMap(DataObj, GL_TEXTURE_CUBE_MAP_POSITIVE_X, aCheckSize);
  7774. // Negative X
  7775. Rec.Func := glBitmapNormalMapNegX;
  7776. DataObj.LoadFromFunc(SizeRec, tfBGR8ub3, glBitmapNormalMapFunc, @Rec);
  7777. UploadCubeMap(DataObj, GL_TEXTURE_CUBE_MAP_NEGATIVE_X, aCheckSize);
  7778. // Positive Y
  7779. Rec.Func := glBitmapNormalMapPosY;
  7780. DataObj.LoadFromFunc(SizeRec, tfBGR8ub3, glBitmapNormalMapFunc, @Rec);
  7781. UploadCubeMap(DataObj, GL_TEXTURE_CUBE_MAP_POSITIVE_Y, aCheckSize);
  7782. // Negative Y
  7783. Rec.Func := glBitmapNormalMapNegY;
  7784. DataObj.LoadFromFunc(SizeRec, tfBGR8ub3, glBitmapNormalMapFunc, @Rec);
  7785. UploadCubeMap(DataObj, GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, aCheckSize);
  7786. // Positive Z
  7787. Rec.Func := glBitmapNormalMapPosZ;
  7788. DataObj.LoadFromFunc(SizeRec, tfBGR8ub3, glBitmapNormalMapFunc, @Rec);
  7789. UploadCubeMap(DataObj, GL_TEXTURE_CUBE_MAP_POSITIVE_Z, aCheckSize);
  7790. // Negative Z
  7791. Rec.Func := glBitmapNormalMapNegZ;
  7792. DataObj.LoadFromFunc(SizeRec, tfBGR8ub3, glBitmapNormalMapFunc, @Rec);
  7793. UploadCubeMap(DataObj, GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, aCheckSize);
  7794. finally
  7795. FreeAndNil(DataObj);
  7796. end;
  7797. end;
  7798. {$IFEND}
  7799. initialization
  7800. glBitmapSetDefaultFormat (tfEmpty);
  7801. glBitmapSetDefaultMipmap (mmMipmap);
  7802. glBitmapSetDefaultFilter (GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR);
  7803. glBitmapSetDefaultWrap (GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
  7804. {$IF NOT DEFINED(OPENGL_ES) OR DEFINED(OPENGL_ES_3_0)}
  7805. glBitmapSetDefaultSwizzle(GL_RED, GL_GREEN, GL_BLUE, GL_ALPHA);
  7806. {$IFEND}
  7807. glBitmapSetDefaultFreeDataAfterGenTexture(true);
  7808. glBitmapSetDefaultDeleteTextureOnFree (true);
  7809. TFormatDescriptor.Init;
  7810. finalization
  7811. TFormatDescriptor.Finalize;
  7812. end.