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.

8902 lines
317 KiB

  1. { glBitmap by Steffen Xonna aka Lossy eX (2003-2008)
  2. http://www.opengl24.de/index.php?cat=header&file=glbitmap
  3. modified by Delphi OpenGL Community (http://delphigl.com/) (2013)
  4. The contents of this file are used with permission, subject to
  5. the Mozilla Public License Version 1.1 (the "License"); you may
  6. not use this file except in compliance with the License. You may
  7. obtain a copy of the License at
  8. http://www.mozilla.org/MPL/MPL-1.1.html
  9. The glBitmap is a Delphi/FPC unit that contains several wrapper classes
  10. to manage OpenGL texture objects. Below you can find a list of the main
  11. functionality of this classes:
  12. - load texture data from file (e.g. BMP, TGA, DDS, PNG, JPEG, ...)
  13. - load texture data from several other image objects (e.g. TBitmap, TLazIntfImage, SDL Surface)
  14. - save texture data to file (e.g. BMP, TGA, DDS, PNG, JPEG, ...)
  15. - save texture data to several other image objects (e.g. TBitmap, TLazIntfImage, SDL Surface)
  16. - support for many texture formats (e.g. RGB8, BGR8, RGBA8, BGRA8, ...)
  17. - manage texture properties (e.g. Filter, Clamp, Mipmap, ...)
  18. - upload texture data to video card
  19. - download texture data from video card
  20. - manipulate texture data (e.g. add alpha, remove alpha, convert to other format, switch RGB, ...) }
  21. unit uglcBitmap;
  22. {$I glBitmapConf.inc}
  23. // Delphi Versions
  24. {$IFDEF fpc}
  25. {$MODE Delphi}
  26. {$IFDEF CPUI386}
  27. {$DEFINE CPU386}
  28. {$ASMMODE INTEL}
  29. {$ENDIF}
  30. {$IFNDEF WINDOWS}
  31. {$linklib c}
  32. {$ENDIF}
  33. {$ENDIF}
  34. // Operation System
  35. {$IF DEFINED(WIN32) or DEFINED(WIN64) or DEFINED(WINDOWS)}
  36. {$DEFINE GLB_WIN}
  37. {$ELSEIF DEFINED(LINUX)}
  38. {$DEFINE GLB_LINUX}
  39. {$IFEND}
  40. // OpenGL ES
  41. {$IF DEFINED(OPENGL_ES_EXT)} {$DEFINE OPENGL_ES_1_1} {$IFEND}
  42. {$IF DEFINED(OPENGL_ES_3_0)} {$DEFINE OPENGL_ES_2_0} {$IFEND}
  43. {$IF DEFINED(OPENGL_ES_2_0)} {$DEFINE OPENGL_ES_1_1} {$IFEND}
  44. {$IF DEFINED(OPENGL_ES_1_1)} {$DEFINE OPENGL_ES} {$IFEND}
  45. // checking define combinations
  46. //SDL Image
  47. {$IFDEF GLB_SDL_IMAGE}
  48. {$IFNDEF GLB_SDL}
  49. {$MESSAGE warn 'SDL_image won''t work without SDL. SDL will be activated.'}
  50. {$DEFINE GLB_SDL}
  51. {$ENDIF}
  52. {$IFDEF GLB_LAZ_PNG}
  53. {$MESSAGE warn 'The Lazarus TPortableNetworkGraphics will be ignored because you are using SDL_image.'}
  54. {$undef GLB_LAZ_PNG}
  55. {$ENDIF}
  56. {$IFDEF GLB_PNGIMAGE}
  57. {$MESSAGE warn 'The unit pngimage will be ignored because you are using SDL_image.'}
  58. {$undef GLB_PNGIMAGE}
  59. {$ENDIF}
  60. {$IFDEF GLB_LAZ_JPEG}
  61. {$MESSAGE warn 'The Lazarus TJPEGImage will be ignored because you are using SDL_image.'}
  62. {$undef GLB_LAZ_JPEG}
  63. {$ENDIF}
  64. {$IFDEF GLB_DELPHI_JPEG}
  65. {$MESSAGE warn 'The unit JPEG will be ignored because you are using SDL_image.'}
  66. {$undef GLB_DELPHI_JPEG}
  67. {$ENDIF}
  68. {$IFDEF GLB_LIB_PNG}
  69. {$MESSAGE warn 'The library libPNG will be ignored because you are using SDL_image.'}
  70. {$undef GLB_LIB_PNG}
  71. {$ENDIF}
  72. {$IFDEF GLB_LIB_JPEG}
  73. {$MESSAGE warn 'The library libJPEG will be ignored because you are using SDL_image.'}
  74. {$undef GLB_LIB_JPEG}
  75. {$ENDIF}
  76. {$DEFINE GLB_SUPPORT_PNG_READ}
  77. {$DEFINE GLB_SUPPORT_JPEG_READ}
  78. {$ENDIF}
  79. // Lazarus TPortableNetworkGraphic
  80. {$IFDEF GLB_LAZ_PNG}
  81. {$IFNDEF GLB_LAZARUS}
  82. {$MESSAGE warn 'Lazarus TPortableNetworkGraphic won''t work without Lazarus. Lazarus will be activated.'}
  83. {$DEFINE GLB_LAZARUS}
  84. {$ENDIF}
  85. {$IFDEF GLB_PNGIMAGE}
  86. {$MESSAGE warn 'The pngimage will be ignored if you are using Lazarus TPortableNetworkGraphic.'}
  87. {$undef GLB_PNGIMAGE}
  88. {$ENDIF}
  89. {$IFDEF GLB_LIB_PNG}
  90. {$MESSAGE warn 'The library libPNG will be ignored if you are using Lazarus TPortableNetworkGraphic.'}
  91. {$undef GLB_LIB_PNG}
  92. {$ENDIF}
  93. {$DEFINE GLB_SUPPORT_PNG_READ}
  94. {$DEFINE GLB_SUPPORT_PNG_WRITE}
  95. {$ENDIF}
  96. // PNG Image
  97. {$IFDEF GLB_PNGIMAGE}
  98. {$IFDEF GLB_LIB_PNG}
  99. {$MESSAGE warn 'The library libPNG will be ignored if you are using pngimage.'}
  100. {$undef GLB_LIB_PNG}
  101. {$ENDIF}
  102. {$DEFINE GLB_SUPPORT_PNG_READ}
  103. {$DEFINE GLB_SUPPORT_PNG_WRITE}
  104. {$ENDIF}
  105. // libPNG
  106. {$IFDEF GLB_LIB_PNG}
  107. {$DEFINE GLB_SUPPORT_PNG_READ}
  108. {$DEFINE GLB_SUPPORT_PNG_WRITE}
  109. {$ENDIF}
  110. // Lazarus TJPEGImage
  111. {$IFDEF GLB_LAZ_JPEG}
  112. {$IFNDEF GLB_LAZARUS}
  113. {$MESSAGE warn 'Lazarus TJPEGImage won''t work without Lazarus. Lazarus will be activated.'}
  114. {$DEFINE GLB_LAZARUS}
  115. {$ENDIF}
  116. {$IFDEF GLB_DELPHI_JPEG}
  117. {$MESSAGE warn 'The Delphi JPEGImage will be ignored if you are using the Lazarus TJPEGImage.'}
  118. {$undef GLB_DELPHI_JPEG}
  119. {$ENDIF}
  120. {$IFDEF GLB_LIB_JPEG}
  121. {$MESSAGE warn 'The library libJPEG will be ignored if you are using the Lazarus TJPEGImage.'}
  122. {$undef GLB_LIB_JPEG}
  123. {$ENDIF}
  124. {$DEFINE GLB_SUPPORT_JPEG_READ}
  125. {$DEFINE GLB_SUPPORT_JPEG_WRITE}
  126. {$ENDIF}
  127. // JPEG Image
  128. {$IFDEF GLB_DELPHI_JPEG}
  129. {$IFDEF GLB_LIB_JPEG}
  130. {$MESSAGE warn 'The library libJPEG will be ignored if you are using the unit JPEG.'}
  131. {$undef GLB_LIB_JPEG}
  132. {$ENDIF}
  133. {$DEFINE GLB_SUPPORT_JPEG_READ}
  134. {$DEFINE GLB_SUPPORT_JPEG_WRITE}
  135. {$ENDIF}
  136. // libJPEG
  137. {$IFDEF GLB_LIB_JPEG}
  138. {$DEFINE GLB_SUPPORT_JPEG_READ}
  139. {$DEFINE GLB_SUPPORT_JPEG_WRITE}
  140. {$ENDIF}
  141. // general options
  142. {$EXTENDEDSYNTAX ON}
  143. {$LONGSTRINGS ON}
  144. {$ALIGN ON}
  145. {$IFNDEF FPC}
  146. {$OPTIMIZATION ON}
  147. {$ENDIF}
  148. interface
  149. uses
  150. {$IFDEF OPENGL_ES} dglOpenGLES,
  151. {$ELSE} dglOpenGL, {$ENDIF}
  152. {$IF DEFINED(GLB_WIN) AND
  153. DEFINED(GLB_DELPHI)} windows, {$IFEND}
  154. {$IFDEF GLB_SDL} SDL, {$ENDIF}
  155. {$IFDEF GLB_LAZARUS} IntfGraphics, GraphType, Graphics, {$ENDIF}
  156. {$IFDEF GLB_DELPHI} Dialogs, Graphics, Types, {$ENDIF}
  157. {$IFDEF GLB_SDL_IMAGE} SDL_image, {$ENDIF}
  158. {$IFDEF GLB_PNGIMAGE} pngimage, {$ENDIF}
  159. {$IFDEF GLB_LIB_PNG} libPNG, {$ENDIF}
  160. {$IFDEF GLB_DELPHI_JPEG} JPEG, {$ENDIF}
  161. {$IFDEF GLB_LIB_JPEG} libJPEG, {$ENDIF}
  162. Classes, SysUtils;
  163. type
  164. {$IFNDEF fpc}
  165. QWord = System.UInt64;
  166. PQWord = ^QWord;
  167. PtrInt = Longint;
  168. PtrUInt = DWord;
  169. {$ENDIF}
  170. { type that describes the format of the data stored in a texture.
  171. the name of formats is composed of the following constituents:
  172. - multiple channels:
  173. - channel (e.g. R, G, B, A or Alpha, Luminance or X (reserved))
  174. - width of the chanel in bit (4, 8, 16, ...)
  175. - data type (e.g. ub, us, ui)
  176. - number of elements of data types }
  177. TglBitmapFormat = (
  178. tfEmpty = 0,
  179. tfAlpha4ub1, //< 1 x unsigned byte
  180. tfAlpha8ub1, //< 1 x unsigned byte
  181. tfAlpha16us1, //< 1 x unsigned short
  182. tfLuminance4ub1, //< 1 x unsigned byte
  183. tfLuminance8ub1, //< 1 x unsigned byte
  184. tfLuminance16us1, //< 1 x unsigned short
  185. tfLuminance4Alpha4ub2, //< 1 x unsigned byte (lum), 1 x unsigned byte (alpha)
  186. tfLuminance6Alpha2ub2, //< 1 x unsigned byte (lum), 1 x unsigned byte (alpha)
  187. tfLuminance8Alpha8ub2, //< 1 x unsigned byte (lum), 1 x unsigned byte (alpha)
  188. tfLuminance12Alpha4us2, //< 1 x unsigned short (lum), 1 x unsigned short (alpha)
  189. tfLuminance16Alpha16us2, //< 1 x unsigned short (lum), 1 x unsigned short (alpha)
  190. tfR3G3B2ub1, //< 1 x unsigned byte (3bit red, 3bit green, 2bit blue)
  191. tfRGBX4us1, //< 1 x unsigned short (4bit red, 4bit green, 4bit blue, 4bit reserverd)
  192. tfXRGB4us1, //< 1 x unsigned short (4bit reserved, 4bit red, 4bit green, 4bit blue)
  193. tfR5G6B5us1, //< 1 x unsigned short (5bit red, 6bit green, 5bit blue)
  194. tfRGB5X1us1, //< 1 x unsigned short (5bit red, 5bit green, 5bit blue, 1bit reserved)
  195. tfX1RGB5us1, //< 1 x unsigned short (1bit reserved, 5bit red, 5bit green, 5bit blue)
  196. tfRGB8ub3, //< 1 x unsigned byte (red), 1 x unsigned byte (green), 1 x unsigned byte (blue)
  197. tfRGBX8ui1, //< 1 x unsigned int (8bit red, 8bit green, 8bit blue, 8bit reserved)
  198. tfXRGB8ui1, //< 1 x unsigned int (8bit reserved, 8bit red, 8bit green, 8bit blue)
  199. tfRGB10X2ui1, //< 1 x unsigned int (10bit red, 10bit green, 10bit blue, 2bit reserved)
  200. tfX2RGB10ui1, //< 1 x unsigned int (2bit reserved, 10bit red, 10bit green, 10bit blue)
  201. tfRGB16us3, //< 1 x unsigned short (red), 1 x unsigned short (green), 1 x unsigned short (blue)
  202. tfRGBA4us1, //< 1 x unsigned short (4bit red, 4bit green, 4bit blue, 4bit alpha)
  203. tfARGB4us1, //< 1 x unsigned short (4bit alpha, 4bit red, 4bit green, 4bit blue)
  204. tfRGB5A1us1, //< 1 x unsigned short (5bit red, 5bit green, 5bit blue, 1bit alpha)
  205. tfA1RGB5us1, //< 1 x unsigned short (1bit alpha, 5bit red, 5bit green, 5bit blue)
  206. 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({$IFNDEF OPENGL_ES}const aEnableTextureUnit: Boolean = true{$ENDIF}); virtual;
  866. { bind texture
  867. @param aDisableTextureUnit disable texture unit for this texture (e.g. glEnable(GL_TEXTURE_2D)) }
  868. procedure Unbind({$IFNDEF OPENGL_ES}const aDisableTextureUnit: Boolean = true{$ENDIF}); 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; const aEnableTextureUnit: Boolean = true{$ENDIF}); 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; const aDisableTextureUnit: Boolean = true{$ENDIF}); 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. TglcBitmapData = TglBitmapData;
  982. {$IF NOT DEFINED(OPENGL_ES)}
  983. TglcBitmap1D = TglBitmap1D;
  984. TglcBitmapCubeMap = TglBitmapCubeMap;
  985. TglcBitmapNormalMap = TglBitmapNormalMap;
  986. {$ELSEIF DEFINED(OPENGL_ES_2_0)}
  987. TglcBitmapCubeMap = TglBitmapCubeMap;
  988. TglcBitmapNormalMap = TglBitmapNormalMap;
  989. {$IFEND}
  990. const
  991. NULL_SIZE: TglBitmapSize = (Fields: []; X: 0; Y: 0);
  992. procedure glBitmapSetDefaultDeleteTextureOnFree(const aDeleteTextureOnFree: Boolean);
  993. procedure glBitmapSetDefaultFreeDataAfterGenTexture(const aFreeData: Boolean);
  994. procedure glBitmapSetDefaultMipmap(const aValue: TglBitmapMipMap);
  995. procedure glBitmapSetDefaultFormat(const aFormat: TglBitmapFormat);
  996. procedure glBitmapSetDefaultFilter(const aMin, aMag: Integer);
  997. procedure glBitmapSetDefaultWrap(
  998. const S: Cardinal = GL_CLAMP_TO_EDGE;
  999. const T: Cardinal = GL_CLAMP_TO_EDGE;
  1000. const R: Cardinal = GL_CLAMP_TO_EDGE);
  1001. {$IF NOT DEFINED(OPENGL_ES) OR DEFINED(OPENGL_ES_3_0)}
  1002. procedure glBitmapSetDefaultSwizzle(const r: GLenum = GL_RED; g: GLenum = GL_GREEN; b: GLenum = GL_BLUE; a: GLenum = GL_ALPHA);
  1003. {$IFEND}
  1004. function glBitmapGetDefaultDeleteTextureOnFree: Boolean;
  1005. function glBitmapGetDefaultFreeDataAfterGenTexture: Boolean;
  1006. function glBitmapGetDefaultMipmap: TglBitmapMipMap;
  1007. function glBitmapGetDefaultFormat: TglBitmapFormat;
  1008. procedure glBitmapGetDefaultFilter(var aMin, aMag: Cardinal);
  1009. procedure glBitmapGetDefaultTextureWrap(var S, T, R: Cardinal);
  1010. {$IF NOT DEFINED(OPENGL_ES) OR DEFINED(OPENGL_ES_3_0)}
  1011. procedure glBitmapGetDefaultSwizzle(var r, g, b, a: GLenum);
  1012. {$IFEND}
  1013. function glBitmapSize(X: Integer = -1; Y: Integer = -1): TglBitmapSize;
  1014. function glBitmapPosition(X: Integer = -1; Y: Integer = -1): TglBitmapPixelPosition;
  1015. function glBitmapRec4ub(const r, g, b, a: Byte): TglBitmapRec4ub;
  1016. function glBitmapRec4ui(const r, g, b, a: Cardinal): TglBitmapRec4ui;
  1017. function glBitmapRec4ul(const r, g, b, a: QWord): TglBitmapRec4ul;
  1018. function glBitmapRec4ubCompare(const r1, r2: TglBitmapRec4ub): Boolean;
  1019. function glBitmapRec4uiCompare(const r1, r2: TglBitmapRec4ui): Boolean;
  1020. function glBitmapCreateTestData(const aFormat: TglBitmapFormat): TglBitmapData;
  1021. {$IFDEF GLB_DELPHI}
  1022. function CreateGrayPalette: HPALETTE;
  1023. {$ENDIF}
  1024. implementation
  1025. uses
  1026. Math, syncobjs, typinfo
  1027. {$IF DEFINED(GLB_SUPPORT_JPEG_READ) AND DEFINED(GLB_LAZ_JPEG)}, FPReadJPEG{$IFEND};
  1028. var
  1029. glBitmapDefaultDeleteTextureOnFree: Boolean;
  1030. glBitmapDefaultFreeDataAfterGenTextures: Boolean;
  1031. glBitmapDefaultFormat: TglBitmapFormat;
  1032. glBitmapDefaultMipmap: TglBitmapMipMap;
  1033. glBitmapDefaultFilterMin: Cardinal;
  1034. glBitmapDefaultFilterMag: Cardinal;
  1035. glBitmapDefaultWrapS: Cardinal;
  1036. glBitmapDefaultWrapT: Cardinal;
  1037. glBitmapDefaultWrapR: Cardinal;
  1038. glDefaultSwizzle: array[0..3] of GLenum;
  1039. ////////////////////////////////////////////////////////////////////////////////////////////////////
  1040. type
  1041. TFormatDescriptor = class(TglBitmapFormatDescriptor)
  1042. public
  1043. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); virtual; abstract;
  1044. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); virtual; abstract;
  1045. function CreateMappingData: Pointer; virtual;
  1046. procedure FreeMappingData(var aMappingData: Pointer); virtual;
  1047. function IsEmpty: Boolean; virtual;
  1048. function MaskMatch(const aMask: TglBitmapRec4ul): Boolean; virtual;
  1049. procedure PreparePixel(out aPixel: TglBitmapPixelData); virtual;
  1050. constructor Create; virtual;
  1051. public
  1052. class procedure Init;
  1053. class function Get(const aFormat: TglBitmapFormat): TFormatDescriptor;
  1054. class function GetAlpha(const aFormat: TglBitmapFormat): TFormatDescriptor;
  1055. class function GetFromMask(const aMask: TglBitmapRec4ul; const aBitCount: Integer = 0): TFormatDescriptor;
  1056. class function GetFromPrecShift(const aPrec, aShift: TglBitmapRec4ub; const aBitCount: Integer): TFormatDescriptor;
  1057. class procedure Clear;
  1058. class procedure Finalize;
  1059. end;
  1060. TFormatDescriptorClass = class of TFormatDescriptor;
  1061. TfdEmpty = class(TFormatDescriptor);
  1062. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1063. TfdAlphaUB1 = class(TFormatDescriptor) //1* unsigned byte
  1064. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1065. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1066. end;
  1067. TfdLuminanceUB1 = class(TFormatDescriptor) //1* unsigned byte
  1068. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1069. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1070. end;
  1071. TfdUniversalUB1 = class(TFormatDescriptor) //1* unsigned byte
  1072. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1073. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1074. end;
  1075. TfdLuminanceAlphaUB2 = class(TfdLuminanceUB1) //2* unsigned byte
  1076. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1077. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1078. end;
  1079. TfdRGBub3 = class(TFormatDescriptor) //3* unsigned byte
  1080. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1081. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1082. end;
  1083. TfdBGRub3 = class(TFormatDescriptor) //3* unsigned byte (inverse)
  1084. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1085. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1086. end;
  1087. TfdRGBAub4 = class(TfdRGBub3) //3* unsigned byte
  1088. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1089. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1090. end;
  1091. TfdBGRAub4 = class(TfdBGRub3) //3* unsigned byte (inverse)
  1092. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1093. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1094. end;
  1095. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1096. TfdAlphaUS1 = class(TFormatDescriptor) //1* unsigned short
  1097. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1098. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1099. end;
  1100. TfdLuminanceUS1 = class(TFormatDescriptor) //1* unsigned short
  1101. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1102. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1103. end;
  1104. TfdUniversalUS1 = class(TFormatDescriptor) //1* unsigned short
  1105. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1106. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1107. end;
  1108. TfdDepthUS1 = class(TFormatDescriptor) //1* unsigned short
  1109. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1110. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1111. end;
  1112. TfdLuminanceAlphaUS2 = class(TfdLuminanceUS1) //2* unsigned short
  1113. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1114. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1115. end;
  1116. TfdRGBus3 = class(TFormatDescriptor) //3* unsigned short
  1117. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1118. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1119. end;
  1120. TfdBGRus3 = class(TFormatDescriptor) //3* unsigned short (inverse)
  1121. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1122. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1123. end;
  1124. TfdRGBAus4 = class(TfdRGBus3) //4* unsigned short
  1125. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1126. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1127. end;
  1128. TfdARGBus4 = class(TfdRGBus3) //4* unsigned short
  1129. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1130. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1131. end;
  1132. TfdBGRAus4 = class(TfdBGRus3) //4* unsigned short (inverse)
  1133. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1134. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1135. end;
  1136. TfdABGRus4 = class(TfdBGRus3) //4* unsigned short (inverse)
  1137. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1138. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1139. end;
  1140. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1141. TfdUniversalUI1 = class(TFormatDescriptor) //1* unsigned int
  1142. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1143. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1144. end;
  1145. TfdDepthUI1 = class(TFormatDescriptor) //1* unsigned int
  1146. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1147. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1148. end;
  1149. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1150. TfdAlpha4ub1 = class(TfdAlphaUB1)
  1151. procedure SetValues; override;
  1152. end;
  1153. TfdAlpha8ub1 = class(TfdAlphaUB1)
  1154. procedure SetValues; override;
  1155. end;
  1156. TfdAlpha16us1 = class(TfdAlphaUS1)
  1157. procedure SetValues; override;
  1158. end;
  1159. TfdLuminance4ub1 = class(TfdLuminanceUB1)
  1160. procedure SetValues; override;
  1161. end;
  1162. TfdLuminance8ub1 = class(TfdLuminanceUB1)
  1163. procedure SetValues; override;
  1164. end;
  1165. TfdLuminance16us1 = class(TfdLuminanceUS1)
  1166. procedure SetValues; override;
  1167. end;
  1168. TfdLuminance4Alpha4ub2 = class(TfdLuminanceAlphaUB2)
  1169. procedure SetValues; override;
  1170. end;
  1171. TfdLuminance6Alpha2ub2 = class(TfdLuminanceAlphaUB2)
  1172. procedure SetValues; override;
  1173. end;
  1174. TfdLuminance8Alpha8ub2 = class(TfdLuminanceAlphaUB2)
  1175. procedure SetValues; override;
  1176. end;
  1177. TfdLuminance12Alpha4us2 = class(TfdLuminanceAlphaUS2)
  1178. procedure SetValues; override;
  1179. end;
  1180. TfdLuminance16Alpha16us2 = class(TfdLuminanceAlphaUS2)
  1181. procedure SetValues; override;
  1182. end;
  1183. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1184. TfdR3G3B2ub1 = class(TfdUniversalUB1)
  1185. procedure SetValues; override;
  1186. end;
  1187. TfdRGBX4us1 = class(TfdUniversalUS1)
  1188. procedure SetValues; override;
  1189. end;
  1190. TfdXRGB4us1 = class(TfdUniversalUS1)
  1191. procedure SetValues; override;
  1192. end;
  1193. TfdR5G6B5us1 = class(TfdUniversalUS1)
  1194. procedure SetValues; override;
  1195. end;
  1196. TfdRGB5X1us1 = class(TfdUniversalUS1)
  1197. procedure SetValues; override;
  1198. end;
  1199. TfdX1RGB5us1 = class(TfdUniversalUS1)
  1200. procedure SetValues; override;
  1201. end;
  1202. TfdRGB8ub3 = class(TfdRGBub3)
  1203. procedure SetValues; override;
  1204. end;
  1205. TfdRGBX8ui1 = class(TfdUniversalUI1)
  1206. procedure SetValues; override;
  1207. end;
  1208. TfdXRGB8ui1 = class(TfdUniversalUI1)
  1209. procedure SetValues; override;
  1210. end;
  1211. TfdRGB10X2ui1 = class(TfdUniversalUI1)
  1212. procedure SetValues; override;
  1213. end;
  1214. TfdX2RGB10ui1 = class(TfdUniversalUI1)
  1215. procedure SetValues; override;
  1216. end;
  1217. TfdRGB16us3 = class(TfdRGBus3)
  1218. procedure SetValues; override;
  1219. end;
  1220. TfdRGBA4us1 = class(TfdUniversalUS1)
  1221. procedure SetValues; override;
  1222. end;
  1223. TfdARGB4us1 = class(TfdUniversalUS1)
  1224. procedure SetValues; override;
  1225. end;
  1226. TfdRGB5A1us1 = class(TfdUniversalUS1)
  1227. procedure SetValues; override;
  1228. end;
  1229. TfdA1RGB5us1 = class(TfdUniversalUS1)
  1230. procedure SetValues; override;
  1231. end;
  1232. TfdRGBA8ui1 = class(TfdUniversalUI1)
  1233. procedure SetValues; override;
  1234. end;
  1235. TfdARGB8ui1 = class(TfdUniversalUI1)
  1236. procedure SetValues; override;
  1237. end;
  1238. TfdRGBA8ub4 = class(TfdRGBAub4)
  1239. procedure SetValues; override;
  1240. end;
  1241. TfdRGB10A2ui1 = class(TfdUniversalUI1)
  1242. procedure SetValues; override;
  1243. end;
  1244. TfdA2RGB10ui1 = class(TfdUniversalUI1)
  1245. procedure SetValues; override;
  1246. end;
  1247. TfdRGBA16us4 = class(TfdRGBAus4)
  1248. procedure SetValues; override;
  1249. end;
  1250. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1251. TfdBGRX4us1 = class(TfdUniversalUS1)
  1252. procedure SetValues; override;
  1253. end;
  1254. TfdXBGR4us1 = class(TfdUniversalUS1)
  1255. procedure SetValues; override;
  1256. end;
  1257. TfdB5G6R5us1 = class(TfdUniversalUS1)
  1258. procedure SetValues; override;
  1259. end;
  1260. TfdBGR5X1us1 = class(TfdUniversalUS1)
  1261. procedure SetValues; override;
  1262. end;
  1263. TfdX1BGR5us1 = class(TfdUniversalUS1)
  1264. procedure SetValues; override;
  1265. end;
  1266. TfdBGR8ub3 = class(TfdBGRub3)
  1267. procedure SetValues; override;
  1268. end;
  1269. TfdBGRX8ui1 = class(TfdUniversalUI1)
  1270. procedure SetValues; override;
  1271. end;
  1272. TfdXBGR8ui1 = class(TfdUniversalUI1)
  1273. procedure SetValues; override;
  1274. end;
  1275. TfdBGR10X2ui1 = class(TfdUniversalUI1)
  1276. procedure SetValues; override;
  1277. end;
  1278. TfdX2BGR10ui1 = class(TfdUniversalUI1)
  1279. procedure SetValues; override;
  1280. end;
  1281. TfdBGR16us3 = class(TfdBGRus3)
  1282. procedure SetValues; override;
  1283. end;
  1284. TfdBGRA4us1 = class(TfdUniversalUS1)
  1285. procedure SetValues; override;
  1286. end;
  1287. TfdABGR4us1 = class(TfdUniversalUS1)
  1288. procedure SetValues; override;
  1289. end;
  1290. TfdBGR5A1us1 = class(TfdUniversalUS1)
  1291. procedure SetValues; override;
  1292. end;
  1293. TfdA1BGR5us1 = class(TfdUniversalUS1)
  1294. procedure SetValues; override;
  1295. end;
  1296. TfdBGRA8ui1 = class(TfdUniversalUI1)
  1297. procedure SetValues; override;
  1298. end;
  1299. TfdABGR8ui1 = class(TfdUniversalUI1)
  1300. procedure SetValues; override;
  1301. end;
  1302. TfdBGRA8ub4 = class(TfdBGRAub4)
  1303. procedure SetValues; override;
  1304. end;
  1305. TfdBGR10A2ui1 = class(TfdUniversalUI1)
  1306. procedure SetValues; override;
  1307. end;
  1308. TfdA2BGR10ui1 = class(TfdUniversalUI1)
  1309. procedure SetValues; override;
  1310. end;
  1311. TfdBGRA16us4 = class(TfdBGRAus4)
  1312. procedure SetValues; override;
  1313. end;
  1314. TfdDepth16us1 = class(TfdDepthUS1)
  1315. procedure SetValues; override;
  1316. end;
  1317. TfdDepth24ui1 = class(TfdDepthUI1)
  1318. procedure SetValues; override;
  1319. end;
  1320. TfdDepth32ui1 = class(TfdDepthUI1)
  1321. procedure SetValues; override;
  1322. end;
  1323. TfdS3tcDtx1RGBA = class(TFormatDescriptor)
  1324. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1325. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1326. procedure SetValues; override;
  1327. end;
  1328. TfdS3tcDtx3RGBA = class(TFormatDescriptor)
  1329. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1330. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1331. procedure SetValues; override;
  1332. end;
  1333. TfdS3tcDtx5RGBA = class(TFormatDescriptor)
  1334. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1335. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1336. procedure SetValues; override;
  1337. end;
  1338. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1339. TbmpBitfieldFormat = class(TFormatDescriptor)
  1340. public
  1341. procedure SetCustomValues(const aBPP: Integer; aMask: TglBitmapRec4ul); overload;
  1342. procedure SetCustomValues(const aBBP: Integer; const aPrec, aShift: TglBitmapRec4ub); overload;
  1343. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1344. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1345. end;
  1346. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1347. TbmpColorTableEnty = packed record
  1348. b, g, r, a: Byte;
  1349. end;
  1350. TbmpColorTable = array of TbmpColorTableEnty;
  1351. TbmpColorTableFormat = class(TFormatDescriptor)
  1352. private
  1353. fColorTable: TbmpColorTable;
  1354. protected
  1355. procedure SetValues; override;
  1356. public
  1357. property ColorTable: TbmpColorTable read fColorTable write fColorTable;
  1358. procedure SetCustomValues(const aFormat: TglBitmapFormat; const aBPP: Integer; const aPrec, aShift: TglBitmapRec4ub); overload;
  1359. procedure CalcValues;
  1360. procedure CreateColorTable;
  1361. function CreateMappingData: Pointer; override;
  1362. procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
  1363. procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
  1364. destructor Destroy; override;
  1365. end;
  1366. const
  1367. LUMINANCE_WEIGHT_R = 0.30;
  1368. LUMINANCE_WEIGHT_G = 0.59;
  1369. LUMINANCE_WEIGHT_B = 0.11;
  1370. ALPHA_WEIGHT_R = 0.30;
  1371. ALPHA_WEIGHT_G = 0.59;
  1372. ALPHA_WEIGHT_B = 0.11;
  1373. DEPTH_WEIGHT_R = 0.333333333;
  1374. DEPTH_WEIGHT_G = 0.333333333;
  1375. DEPTH_WEIGHT_B = 0.333333333;
  1376. FORMAT_DESCRIPTOR_CLASSES: array[TglBitmapFormat] of TFormatDescriptorClass = (
  1377. TfdEmpty,
  1378. TfdAlpha4ub1,
  1379. TfdAlpha8ub1,
  1380. TfdAlpha16us1,
  1381. TfdLuminance4ub1,
  1382. TfdLuminance8ub1,
  1383. TfdLuminance16us1,
  1384. TfdLuminance4Alpha4ub2,
  1385. TfdLuminance6Alpha2ub2,
  1386. TfdLuminance8Alpha8ub2,
  1387. TfdLuminance12Alpha4us2,
  1388. TfdLuminance16Alpha16us2,
  1389. TfdR3G3B2ub1,
  1390. TfdRGBX4us1,
  1391. TfdXRGB4us1,
  1392. TfdR5G6B5us1,
  1393. TfdRGB5X1us1,
  1394. TfdX1RGB5us1,
  1395. TfdRGB8ub3,
  1396. TfdRGBX8ui1,
  1397. TfdXRGB8ui1,
  1398. TfdRGB10X2ui1,
  1399. TfdX2RGB10ui1,
  1400. TfdRGB16us3,
  1401. TfdRGBA4us1,
  1402. TfdARGB4us1,
  1403. TfdRGB5A1us1,
  1404. TfdA1RGB5us1,
  1405. TfdRGBA8ui1,
  1406. TfdARGB8ui1,
  1407. TfdRGBA8ub4,
  1408. TfdRGB10A2ui1,
  1409. TfdA2RGB10ui1,
  1410. TfdRGBA16us4,
  1411. TfdBGRX4us1,
  1412. TfdXBGR4us1,
  1413. TfdB5G6R5us1,
  1414. TfdBGR5X1us1,
  1415. TfdX1BGR5us1,
  1416. TfdBGR8ub3,
  1417. TfdBGRX8ui1,
  1418. TfdXBGR8ui1,
  1419. TfdBGR10X2ui1,
  1420. TfdX2BGR10ui1,
  1421. TfdBGR16us3,
  1422. TfdBGRA4us1,
  1423. TfdABGR4us1,
  1424. TfdBGR5A1us1,
  1425. TfdA1BGR5us1,
  1426. TfdBGRA8ui1,
  1427. TfdABGR8ui1,
  1428. TfdBGRA8ub4,
  1429. TfdBGR10A2ui1,
  1430. TfdA2BGR10ui1,
  1431. TfdBGRA16us4,
  1432. TfdDepth16us1,
  1433. TfdDepth24ui1,
  1434. TfdDepth32ui1,
  1435. TfdS3tcDtx1RGBA,
  1436. TfdS3tcDtx3RGBA,
  1437. TfdS3tcDtx5RGBA
  1438. );
  1439. var
  1440. FormatDescriptorCS: TCriticalSection;
  1441. FormatDescriptors: array[TglBitmapFormat] of TFormatDescriptor;
  1442. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1443. constructor EglBitmapUnsupportedFormat.Create(const aFormat: TglBitmapFormat);
  1444. begin
  1445. inherited Create('unsupported format: ' + GetEnumName(TypeInfo(TglBitmapFormat), Integer(aFormat)));
  1446. end;
  1447. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1448. constructor EglBitmapUnsupportedFormat.Create(const aMsg: String; const aFormat: TglBitmapFormat);
  1449. begin
  1450. inherited Create(aMsg + GetEnumName(TypeInfo(TglBitmapFormat), Integer(aFormat)));
  1451. end;
  1452. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1453. function glBitmapSize(X: Integer; Y: Integer): TglBitmapSize;
  1454. begin
  1455. result.Fields := [];
  1456. if (X >= 0) then
  1457. result.Fields := result.Fields + [ffX];
  1458. if (Y >= 0) then
  1459. result.Fields := result.Fields + [ffY];
  1460. result.X := Max(0, X);
  1461. result.Y := Max(0, Y);
  1462. end;
  1463. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1464. function glBitmapPosition(X: Integer; Y: Integer): TglBitmapPixelPosition;
  1465. begin
  1466. result := glBitmapSize(X, Y);
  1467. end;
  1468. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1469. function glBitmapRec4ub(const r, g, b, a: Byte): TglBitmapRec4ub;
  1470. begin
  1471. result.r := r;
  1472. result.g := g;
  1473. result.b := b;
  1474. result.a := a;
  1475. end;
  1476. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1477. function glBitmapRec4ui(const r, g, b, a: Cardinal): TglBitmapRec4ui;
  1478. begin
  1479. result.r := r;
  1480. result.g := g;
  1481. result.b := b;
  1482. result.a := a;
  1483. end;
  1484. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1485. function glBitmapRec4ul(const r, g, b, a: QWord): TglBitmapRec4ul;
  1486. begin
  1487. result.r := r;
  1488. result.g := g;
  1489. result.b := b;
  1490. result.a := a;
  1491. end;
  1492. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1493. function glBitmapRec4ubCompare(const r1, r2: TglBitmapRec4ub): Boolean;
  1494. var
  1495. i: Integer;
  1496. begin
  1497. result := false;
  1498. for i := 0 to high(r1.arr) do
  1499. if (r1.arr[i] <> r2.arr[i]) then
  1500. exit;
  1501. result := true;
  1502. end;
  1503. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1504. function glBitmapRec4uiCompare(const r1, r2: TglBitmapRec4ui): Boolean;
  1505. var
  1506. i: Integer;
  1507. begin
  1508. result := false;
  1509. for i := 0 to high(r1.arr) do
  1510. if (r1.arr[i] <> r2.arr[i]) then
  1511. exit;
  1512. result := true;
  1513. end;
  1514. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1515. function glBitmapCreateTestData(const aFormat: TglBitmapFormat): TglBitmapData;
  1516. var
  1517. desc: TFormatDescriptor;
  1518. p, tmp: PByte;
  1519. x, y, i: Integer;
  1520. md: Pointer;
  1521. px: TglBitmapPixelData;
  1522. begin
  1523. result := nil;
  1524. desc := TFormatDescriptor.Get(aFormat);
  1525. if (desc.IsCompressed) or (desc.glFormat = 0) then
  1526. exit;
  1527. p := GetMemory(ceil(25 * desc.BytesPerPixel)); // 5 x 5 pixel
  1528. md := desc.CreateMappingData;
  1529. try
  1530. tmp := p;
  1531. desc.PreparePixel(px);
  1532. for y := 0 to 4 do
  1533. for x := 0 to 4 do begin
  1534. px.Data := glBitmapRec4ui(0, 0, 0, 0);
  1535. for i := 0 to 3 do begin
  1536. if ((y < 3) and (y = i)) or
  1537. ((y = 3) and (i < 3)) or
  1538. ((y = 4) and (i = 3))
  1539. then
  1540. px.Data.arr[i] := Trunc(px.Range.arr[i] / 4 * x)
  1541. else if ((y < 4) and (i = 3)) or
  1542. ((y = 4) and (i < 3))
  1543. then
  1544. px.Data.arr[i] := px.Range.arr[i]
  1545. else
  1546. px.Data.arr[i] := 0; //px.Range.arr[i];
  1547. end;
  1548. desc.Map(px, tmp, md);
  1549. end;
  1550. finally
  1551. desc.FreeMappingData(md);
  1552. end;
  1553. result := TglBitmapData.Create(glBitmapPosition(5, 5), aFormat, p);
  1554. end;
  1555. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1556. function glBitmapShiftRec(const r, g, b, a: Byte): TglBitmapRec4ub;
  1557. begin
  1558. result.r := r;
  1559. result.g := g;
  1560. result.b := b;
  1561. result.a := a;
  1562. end;
  1563. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1564. function FormatGetSupportedFiles(const aFormat: TglBitmapFormat): TglBitmapFileTypes;
  1565. begin
  1566. result := [];
  1567. if (aFormat in [
  1568. //8bpp
  1569. tfAlpha4ub1, tfAlpha8ub1,
  1570. tfLuminance4ub1, tfLuminance8ub1, tfR3G3B2ub1,
  1571. //16bpp
  1572. tfLuminance4Alpha4ub2, tfLuminance6Alpha2ub2, tfLuminance8Alpha8ub2,
  1573. tfRGBX4us1, tfXRGB4us1, tfRGB5X1us1, tfX1RGB5us1, tfR5G6B5us1, tfRGB5A1us1, tfA1RGB5us1, tfRGBA4us1, tfARGB4us1,
  1574. tfBGRX4us1, tfXBGR4us1, tfBGR5X1us1, tfX1BGR5us1, tfB5G6R5us1, tfBGR5A1us1, tfA1BGR5us1, tfBGRA4us1, tfABGR4us1,
  1575. //24bpp
  1576. tfBGR8ub3, tfRGB8ub3,
  1577. //32bpp
  1578. tfRGBX8ui1, tfXRGB8ui1, tfRGB10X2ui1, tfX2RGB10ui1, tfRGBA8ui1, tfARGB8ui1, tfRGBA8ub4, tfRGB10A2ui1, tfA2RGB10ui1,
  1579. tfBGRX8ui1, tfXBGR8ui1, tfBGR10X2ui1, tfX2BGR10ui1, tfBGRA8ui1, tfABGR8ui1, tfBGRA8ub4, tfBGR10A2ui1, tfA2BGR10ui1])
  1580. then
  1581. result := result + [ ftBMP ];
  1582. if (aFormat in [
  1583. //8bbp
  1584. tfAlpha4ub1, tfAlpha8ub1, tfLuminance4ub1, tfLuminance8ub1,
  1585. //16bbp
  1586. tfAlpha16us1, tfLuminance16us1,
  1587. tfLuminance4Alpha4ub2, tfLuminance6Alpha2ub2, tfLuminance8Alpha8ub2,
  1588. tfX1RGB5us1, tfARGB4us1, tfA1RGB5us1, tfDepth16us1,
  1589. //24bbp
  1590. tfBGR8ub3,
  1591. //32bbp
  1592. tfX2RGB10ui1, tfARGB8ui1, tfBGRA8ub4, tfA2RGB10ui1,
  1593. tfDepth24ui1, tfDepth32ui1])
  1594. then
  1595. result := result + [ftTGA];
  1596. if not (aFormat in [tfEmpty, tfRGB16us3, tfBGR16us3]) then
  1597. result := result + [ftDDS];
  1598. {$IFDEF GLB_SUPPORT_PNG_WRITE}
  1599. if aFormat in [
  1600. tfAlpha8ub1, tfLuminance8ub1, tfLuminance8Alpha8ub2,
  1601. tfRGB8ub3, tfRGBA8ui1,
  1602. tfBGR8ub3, tfBGRA8ui1] then
  1603. result := result + [ftPNG];
  1604. {$ENDIF}
  1605. {$IFDEF GLB_SUPPORT_JPEG_WRITE}
  1606. if aFormat in [tfAlpha8ub1, tfLuminance8ub1, tfRGB8ub3, tfBGR8ub3] then
  1607. result := result + [ftJPEG];
  1608. {$ENDIF}
  1609. end;
  1610. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1611. function IsPowerOfTwo(aNumber: Integer): Boolean;
  1612. begin
  1613. while (aNumber and 1) = 0 do
  1614. aNumber := aNumber shr 1;
  1615. result := aNumber = 1;
  1616. end;
  1617. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1618. function GetTopMostBit(aBitSet: QWord): Integer;
  1619. begin
  1620. result := 0;
  1621. while aBitSet > 0 do begin
  1622. inc(result);
  1623. aBitSet := aBitSet shr 1;
  1624. end;
  1625. end;
  1626. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1627. function CountSetBits(aBitSet: QWord): Integer;
  1628. begin
  1629. result := 0;
  1630. while aBitSet > 0 do begin
  1631. if (aBitSet and 1) = 1 then
  1632. inc(result);
  1633. aBitSet := aBitSet shr 1;
  1634. end;
  1635. end;
  1636. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1637. function LuminanceWeight(const aPixel: TglBitmapPixelData): Cardinal;
  1638. begin
  1639. result := Trunc(
  1640. LUMINANCE_WEIGHT_R * aPixel.Data.r +
  1641. LUMINANCE_WEIGHT_G * aPixel.Data.g +
  1642. LUMINANCE_WEIGHT_B * aPixel.Data.b);
  1643. end;
  1644. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1645. function DepthWeight(const aPixel: TglBitmapPixelData): Cardinal;
  1646. begin
  1647. result := Trunc(
  1648. DEPTH_WEIGHT_R * aPixel.Data.r +
  1649. DEPTH_WEIGHT_G * aPixel.Data.g +
  1650. DEPTH_WEIGHT_B * aPixel.Data.b);
  1651. end;
  1652. {$IFDEF GLB_SDL_IMAGE}
  1653. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1654. // SDL Image Helper /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1655. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1656. function glBitmapRWseek(context: PSDL_RWops; offset: Integer; whence: Integer): Integer; cdecl;
  1657. begin
  1658. result := TStream(context^.unknown.data1).Seek(offset, whence);
  1659. end;
  1660. function glBitmapRWread(context: PSDL_RWops; Ptr: Pointer; size: Integer; maxnum : Integer): Integer; cdecl;
  1661. begin
  1662. result := TStream(context^.unknown.data1).Read(Ptr^, size * maxnum);
  1663. end;
  1664. function glBitmapRWwrite(context: PSDL_RWops; Ptr: Pointer; size: Integer; num: Integer): Integer; cdecl;
  1665. begin
  1666. result := TStream(context^.unknown.data1).Write(Ptr^, size * num);
  1667. end;
  1668. function glBitmapRWclose(context: PSDL_RWops): Integer; cdecl;
  1669. begin
  1670. result := 0;
  1671. end;
  1672. function glBitmapCreateRWops(Stream: TStream): PSDL_RWops;
  1673. begin
  1674. result := SDL_AllocRW;
  1675. if result = nil then
  1676. raise EglBitmap.Create('glBitmapCreateRWops - SDL_AllocRW failed.');
  1677. result^.seek := glBitmapRWseek;
  1678. result^.read := glBitmapRWread;
  1679. result^.write := glBitmapRWwrite;
  1680. result^.close := glBitmapRWclose;
  1681. result^.unknown.data1 := Stream;
  1682. end;
  1683. {$ENDIF}
  1684. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1685. procedure glBitmapSetDefaultDeleteTextureOnFree(const aDeleteTextureOnFree: Boolean);
  1686. begin
  1687. glBitmapDefaultDeleteTextureOnFree := aDeleteTextureOnFree;
  1688. end;
  1689. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1690. procedure glBitmapSetDefaultFreeDataAfterGenTexture(const aFreeData: Boolean);
  1691. begin
  1692. glBitmapDefaultFreeDataAfterGenTextures := aFreeData;
  1693. end;
  1694. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1695. procedure glBitmapSetDefaultMipmap(const aValue: TglBitmapMipMap);
  1696. begin
  1697. glBitmapDefaultMipmap := aValue;
  1698. end;
  1699. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1700. procedure glBitmapSetDefaultFormat(const aFormat: TglBitmapFormat);
  1701. begin
  1702. glBitmapDefaultFormat := aFormat;
  1703. end;
  1704. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1705. procedure glBitmapSetDefaultFilter(const aMin, aMag: Integer);
  1706. begin
  1707. glBitmapDefaultFilterMin := aMin;
  1708. glBitmapDefaultFilterMag := aMag;
  1709. end;
  1710. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1711. procedure glBitmapSetDefaultWrap(const S: Cardinal = GL_CLAMP_TO_EDGE; const T: Cardinal = GL_CLAMP_TO_EDGE; const R: Cardinal = GL_CLAMP_TO_EDGE);
  1712. begin
  1713. glBitmapDefaultWrapS := S;
  1714. glBitmapDefaultWrapT := T;
  1715. glBitmapDefaultWrapR := R;
  1716. end;
  1717. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1718. {$IF NOT DEFINED(OPENGL_ES) OR DEFINED(OPENGL_ES_3_0)}
  1719. procedure glBitmapSetDefaultSwizzle(const r: GLenum = GL_RED; g: GLenum = GL_GREEN; b: GLenum = GL_BLUE; a: GLenum = GL_ALPHA);
  1720. begin
  1721. glDefaultSwizzle[0] := r;
  1722. glDefaultSwizzle[1] := g;
  1723. glDefaultSwizzle[2] := b;
  1724. glDefaultSwizzle[3] := a;
  1725. end;
  1726. {$IFEND}
  1727. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1728. function glBitmapGetDefaultDeleteTextureOnFree: Boolean;
  1729. begin
  1730. result := glBitmapDefaultDeleteTextureOnFree;
  1731. end;
  1732. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1733. function glBitmapGetDefaultFreeDataAfterGenTexture: Boolean;
  1734. begin
  1735. result := glBitmapDefaultFreeDataAfterGenTextures;
  1736. end;
  1737. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1738. function glBitmapGetDefaultMipmap: TglBitmapMipMap;
  1739. begin
  1740. result := glBitmapDefaultMipmap;
  1741. end;
  1742. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1743. function glBitmapGetDefaultFormat: TglBitmapFormat;
  1744. begin
  1745. result := glBitmapDefaultFormat;
  1746. end;
  1747. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1748. procedure glBitmapGetDefaultFilter(var aMin, aMag: Cardinal);
  1749. begin
  1750. aMin := glBitmapDefaultFilterMin;
  1751. aMag := glBitmapDefaultFilterMag;
  1752. end;
  1753. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1754. procedure glBitmapGetDefaultTextureWrap(var S, T, R: Cardinal);
  1755. begin
  1756. S := glBitmapDefaultWrapS;
  1757. T := glBitmapDefaultWrapT;
  1758. R := glBitmapDefaultWrapR;
  1759. end;
  1760. {$IF NOT DEFINED(OPENGL_ES) OR DEFINED(OPENGL_ES_3_0)}
  1761. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1762. procedure glBitmapGetDefaultSwizzle(var r, g, b, a: GLenum);
  1763. begin
  1764. r := glDefaultSwizzle[0];
  1765. g := glDefaultSwizzle[1];
  1766. b := glDefaultSwizzle[2];
  1767. a := glDefaultSwizzle[3];
  1768. end;
  1769. {$IFEND}
  1770. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1771. //TFormatDescriptor///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1772. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1773. function TFormatDescriptor.CreateMappingData: Pointer;
  1774. begin
  1775. result := nil;
  1776. end;
  1777. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1778. procedure TFormatDescriptor.FreeMappingData(var aMappingData: Pointer);
  1779. begin
  1780. //DUMMY
  1781. end;
  1782. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1783. function TFormatDescriptor.IsEmpty: Boolean;
  1784. begin
  1785. result := (fFormat = tfEmpty);
  1786. end;
  1787. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1788. function TFormatDescriptor.MaskMatch(const aMask: TglBitmapRec4ul): Boolean;
  1789. var
  1790. i: Integer;
  1791. m: TglBitmapRec4ul;
  1792. begin
  1793. result := false;
  1794. if (aMask.r = 0) and (aMask.g = 0) and (aMask.b = 0) and (aMask.a = 0) then
  1795. raise EglBitmap.Create('FormatCheckFormat - All Masks are 0');
  1796. m := Mask;
  1797. for i := 0 to 3 do
  1798. if (aMask.arr[i] <> m.arr[i]) then
  1799. exit;
  1800. result := true;
  1801. end;
  1802. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1803. procedure TFormatDescriptor.PreparePixel(out aPixel: TglBitmapPixelData);
  1804. begin
  1805. FillChar(aPixel{%H-}, SizeOf(aPixel), 0);
  1806. aPixel.Data := Range;
  1807. aPixel.Format := fFormat;
  1808. aPixel.Range := Range;
  1809. end;
  1810. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1811. constructor TFormatDescriptor.Create;
  1812. begin
  1813. inherited Create;
  1814. end;
  1815. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1816. //TfdAlpha_UB1////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1817. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1818. procedure TfdAlphaUB1.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  1819. begin
  1820. aData^ := aPixel.Data.a;
  1821. inc(aData);
  1822. end;
  1823. procedure TfdAlphaUB1.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  1824. begin
  1825. aPixel.Data.r := 0;
  1826. aPixel.Data.g := 0;
  1827. aPixel.Data.b := 0;
  1828. aPixel.Data.a := aData^;
  1829. inc(aData);
  1830. end;
  1831. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1832. //TfdLuminance_UB1////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1833. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1834. procedure TfdLuminanceUB1.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  1835. begin
  1836. aData^ := LuminanceWeight(aPixel);
  1837. inc(aData);
  1838. end;
  1839. procedure TfdLuminanceUB1.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  1840. begin
  1841. aPixel.Data.r := aData^;
  1842. aPixel.Data.g := aData^;
  1843. aPixel.Data.b := aData^;
  1844. aPixel.Data.a := 0;
  1845. inc(aData);
  1846. end;
  1847. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1848. //TfdUniversal_UB1////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1849. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1850. procedure TfdUniversalUB1.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  1851. var
  1852. i: Integer;
  1853. begin
  1854. aData^ := 0;
  1855. for i := 0 to 3 do
  1856. if (Range.arr[i] > 0) then
  1857. aData^ := aData^ or ((aPixel.Data.arr[i] and Range.arr[i]) shl fShift.arr[i]);
  1858. inc(aData);
  1859. end;
  1860. procedure TfdUniversalUB1.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  1861. var
  1862. i: Integer;
  1863. begin
  1864. for i := 0 to 3 do
  1865. aPixel.Data.arr[i] := (aData^ shr fShift.arr[i]) and Range.arr[i];
  1866. inc(aData);
  1867. end;
  1868. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1869. //TfdLuminanceAlpha_UB2///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1870. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1871. procedure TfdLuminanceAlphaUB2.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  1872. begin
  1873. inherited Map(aPixel, aData, aMapData);
  1874. aData^ := aPixel.Data.a;
  1875. inc(aData);
  1876. end;
  1877. procedure TfdLuminanceAlphaUB2.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  1878. begin
  1879. inherited Unmap(aData, aPixel, aMapData);
  1880. aPixel.Data.a := aData^;
  1881. inc(aData);
  1882. end;
  1883. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1884. //TfdRGB_UB3//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1885. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1886. procedure TfdRGBub3.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  1887. begin
  1888. aData^ := aPixel.Data.r;
  1889. inc(aData);
  1890. aData^ := aPixel.Data.g;
  1891. inc(aData);
  1892. aData^ := aPixel.Data.b;
  1893. inc(aData);
  1894. end;
  1895. procedure TfdRGBub3.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  1896. begin
  1897. aPixel.Data.r := aData^;
  1898. inc(aData);
  1899. aPixel.Data.g := aData^;
  1900. inc(aData);
  1901. aPixel.Data.b := aData^;
  1902. inc(aData);
  1903. aPixel.Data.a := 0;
  1904. end;
  1905. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1906. //TfdBGR_UB3//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1907. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1908. procedure TfdBGRub3.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  1909. begin
  1910. aData^ := aPixel.Data.b;
  1911. inc(aData);
  1912. aData^ := aPixel.Data.g;
  1913. inc(aData);
  1914. aData^ := aPixel.Data.r;
  1915. inc(aData);
  1916. end;
  1917. procedure TfdBGRub3.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  1918. begin
  1919. aPixel.Data.b := aData^;
  1920. inc(aData);
  1921. aPixel.Data.g := aData^;
  1922. inc(aData);
  1923. aPixel.Data.r := aData^;
  1924. inc(aData);
  1925. aPixel.Data.a := 0;
  1926. end;
  1927. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1928. //TfdRGBA_UB4//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1929. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1930. procedure TfdRGBAub4.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  1931. begin
  1932. inherited Map(aPixel, aData, aMapData);
  1933. aData^ := aPixel.Data.a;
  1934. inc(aData);
  1935. end;
  1936. procedure TfdRGBAub4.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  1937. begin
  1938. inherited Unmap(aData, aPixel, aMapData);
  1939. aPixel.Data.a := aData^;
  1940. inc(aData);
  1941. end;
  1942. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1943. //TfdBGRA_UB4//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1944. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1945. procedure TfdBGRAub4.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  1946. begin
  1947. inherited Map(aPixel, aData, aMapData);
  1948. aData^ := aPixel.Data.a;
  1949. inc(aData);
  1950. end;
  1951. procedure TfdBGRAub4.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  1952. begin
  1953. inherited Unmap(aData, aPixel, aMapData);
  1954. aPixel.Data.a := aData^;
  1955. inc(aData);
  1956. end;
  1957. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1958. //TfdAlpha_US1////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1959. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1960. procedure TfdAlphaUS1.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  1961. begin
  1962. PWord(aData)^ := aPixel.Data.a;
  1963. inc(aData, 2);
  1964. end;
  1965. procedure TfdAlphaUS1.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  1966. begin
  1967. aPixel.Data.r := 0;
  1968. aPixel.Data.g := 0;
  1969. aPixel.Data.b := 0;
  1970. aPixel.Data.a := PWord(aData)^;
  1971. inc(aData, 2);
  1972. end;
  1973. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1974. //TfdLuminance_US1////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1975. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1976. procedure TfdLuminanceUS1.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  1977. begin
  1978. PWord(aData)^ := LuminanceWeight(aPixel);
  1979. inc(aData, 2);
  1980. end;
  1981. procedure TfdLuminanceUS1.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  1982. begin
  1983. aPixel.Data.r := PWord(aData)^;
  1984. aPixel.Data.g := PWord(aData)^;
  1985. aPixel.Data.b := PWord(aData)^;
  1986. aPixel.Data.a := 0;
  1987. inc(aData, 2);
  1988. end;
  1989. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1990. //TfdUniversal_US1////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1991. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1992. procedure TfdUniversalUS1.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  1993. var
  1994. i: Integer;
  1995. begin
  1996. PWord(aData)^ := 0;
  1997. for i := 0 to 3 do
  1998. if (Range.arr[i] > 0) then
  1999. PWord(aData)^ := PWord(aData)^ or ((aPixel.Data.arr[i] and Range.arr[i]) shl fShift.arr[i]);
  2000. inc(aData, 2);
  2001. end;
  2002. procedure TfdUniversalUS1.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  2003. var
  2004. i: Integer;
  2005. begin
  2006. for i := 0 to 3 do
  2007. aPixel.Data.arr[i] := (PWord(aData)^ shr fShift.arr[i]) and Range.arr[i];
  2008. inc(aData, 2);
  2009. end;
  2010. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2011. //TfdDepth_US1////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2012. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2013. procedure TfdDepthUS1.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  2014. begin
  2015. PWord(aData)^ := DepthWeight(aPixel);
  2016. inc(aData, 2);
  2017. end;
  2018. procedure TfdDepthUS1.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  2019. begin
  2020. aPixel.Data.r := PWord(aData)^;
  2021. aPixel.Data.g := PWord(aData)^;
  2022. aPixel.Data.b := PWord(aData)^;
  2023. aPixel.Data.a := PWord(aData)^;;
  2024. inc(aData, 2);
  2025. end;
  2026. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2027. //TfdLuminanceAlpha_US2///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2028. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2029. procedure TfdLuminanceAlphaUS2.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  2030. begin
  2031. inherited Map(aPixel, aData, aMapData);
  2032. PWord(aData)^ := aPixel.Data.a;
  2033. inc(aData, 2);
  2034. end;
  2035. procedure TfdLuminanceAlphaUS2.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  2036. begin
  2037. inherited Unmap(aData, aPixel, aMapData);
  2038. aPixel.Data.a := PWord(aData)^;
  2039. inc(aData, 2);
  2040. end;
  2041. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2042. //TfdRGB_US3//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2043. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2044. procedure TfdRGBus3.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  2045. begin
  2046. PWord(aData)^ := aPixel.Data.r;
  2047. inc(aData, 2);
  2048. PWord(aData)^ := aPixel.Data.g;
  2049. inc(aData, 2);
  2050. PWord(aData)^ := aPixel.Data.b;
  2051. inc(aData, 2);
  2052. end;
  2053. procedure TfdRGBus3.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  2054. begin
  2055. aPixel.Data.r := PWord(aData)^;
  2056. inc(aData, 2);
  2057. aPixel.Data.g := PWord(aData)^;
  2058. inc(aData, 2);
  2059. aPixel.Data.b := PWord(aData)^;
  2060. inc(aData, 2);
  2061. aPixel.Data.a := 0;
  2062. end;
  2063. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2064. //TfdBGR_US3//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2065. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2066. procedure TfdBGRus3.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  2067. begin
  2068. PWord(aData)^ := aPixel.Data.b;
  2069. inc(aData, 2);
  2070. PWord(aData)^ := aPixel.Data.g;
  2071. inc(aData, 2);
  2072. PWord(aData)^ := aPixel.Data.r;
  2073. inc(aData, 2);
  2074. end;
  2075. procedure TfdBGRus3.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  2076. begin
  2077. aPixel.Data.b := PWord(aData)^;
  2078. inc(aData, 2);
  2079. aPixel.Data.g := PWord(aData)^;
  2080. inc(aData, 2);
  2081. aPixel.Data.r := PWord(aData)^;
  2082. inc(aData, 2);
  2083. aPixel.Data.a := 0;
  2084. end;
  2085. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2086. //TfdRGBA_US4/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2087. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2088. procedure TfdRGBAus4.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  2089. begin
  2090. inherited Map(aPixel, aData, aMapData);
  2091. PWord(aData)^ := aPixel.Data.a;
  2092. inc(aData, 2);
  2093. end;
  2094. procedure TfdRGBAus4.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  2095. begin
  2096. inherited Unmap(aData, aPixel, aMapData);
  2097. aPixel.Data.a := PWord(aData)^;
  2098. inc(aData, 2);
  2099. end;
  2100. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2101. //TfdARGB_US4/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2102. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2103. procedure TfdARGBus4.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  2104. begin
  2105. PWord(aData)^ := aPixel.Data.a;
  2106. inc(aData, 2);
  2107. inherited Map(aPixel, aData, aMapData);
  2108. end;
  2109. procedure TfdARGBus4.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  2110. begin
  2111. aPixel.Data.a := PWord(aData)^;
  2112. inc(aData, 2);
  2113. inherited Unmap(aData, aPixel, aMapData);
  2114. end;
  2115. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2116. //TfdBGRA_US4/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2117. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2118. procedure TfdBGRAus4.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  2119. begin
  2120. inherited Map(aPixel, aData, aMapData);
  2121. PWord(aData)^ := aPixel.Data.a;
  2122. inc(aData, 2);
  2123. end;
  2124. procedure TfdBGRAus4.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  2125. begin
  2126. inherited Unmap(aData, aPixel, aMapData);
  2127. aPixel.Data.a := PWord(aData)^;
  2128. inc(aData, 2);
  2129. end;
  2130. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2131. //TfdABGR_US4/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2132. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2133. procedure TfdABGRus4.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  2134. begin
  2135. PWord(aData)^ := aPixel.Data.a;
  2136. inc(aData, 2);
  2137. inherited Map(aPixel, aData, aMapData);
  2138. end;
  2139. procedure TfdABGRus4.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  2140. begin
  2141. aPixel.Data.a := PWord(aData)^;
  2142. inc(aData, 2);
  2143. inherited Unmap(aData, aPixel, aMapData);
  2144. end;
  2145. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2146. //TfdUniversal_UI1////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2147. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2148. procedure TfdUniversalUI1.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  2149. var
  2150. i: Integer;
  2151. begin
  2152. PCardinal(aData)^ := 0;
  2153. for i := 0 to 3 do
  2154. if (Range.arr[i] > 0) then
  2155. PCardinal(aData)^ := PCardinal(aData)^ or ((aPixel.Data.arr[i] and Range.arr[i]) shl fShift.arr[i]);
  2156. inc(aData, 4);
  2157. end;
  2158. procedure TfdUniversalUI1.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  2159. var
  2160. i: Integer;
  2161. begin
  2162. for i := 0 to 3 do
  2163. aPixel.Data.arr[i] := (PCardinal(aData)^ shr fShift.arr[i]) and Range.arr[i];
  2164. inc(aData, 2);
  2165. end;
  2166. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2167. //TfdDepth_UI1////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2168. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2169. procedure TfdDepthUI1.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  2170. begin
  2171. PCardinal(aData)^ := DepthWeight(aPixel);
  2172. inc(aData, 4);
  2173. end;
  2174. procedure TfdDepthUI1.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  2175. begin
  2176. aPixel.Data.r := PCardinal(aData)^;
  2177. aPixel.Data.g := PCardinal(aData)^;
  2178. aPixel.Data.b := PCardinal(aData)^;
  2179. aPixel.Data.a := PCardinal(aData)^;
  2180. inc(aData, 4);
  2181. end;
  2182. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2183. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2184. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2185. procedure TfdAlpha4ub1.SetValues;
  2186. begin
  2187. inherited SetValues;
  2188. fBitsPerPixel := 8;
  2189. fFormat := tfAlpha4ub1;
  2190. fWithAlpha := tfAlpha4ub1;
  2191. fPrecision := glBitmapRec4ub(0, 0, 0, 8);
  2192. fShift := glBitmapRec4ub(0, 0, 0, 0);
  2193. {$IFNDEF OPENGL_ES}
  2194. fOpenGLFormat := tfAlpha4ub1;
  2195. fglFormat := GL_ALPHA;
  2196. fglInternalFormat := GL_ALPHA4;
  2197. fglDataFormat := GL_UNSIGNED_BYTE;
  2198. {$ELSE}
  2199. fOpenGLFormat := tfAlpha8ub1;
  2200. {$ENDIF}
  2201. end;
  2202. procedure TfdAlpha8ub1.SetValues;
  2203. begin
  2204. inherited SetValues;
  2205. fBitsPerPixel := 8;
  2206. fFormat := tfAlpha8ub1;
  2207. fWithAlpha := tfAlpha8ub1;
  2208. fPrecision := glBitmapRec4ub(0, 0, 0, 8);
  2209. fShift := glBitmapRec4ub(0, 0, 0, 0);
  2210. fOpenGLFormat := tfAlpha8ub1;
  2211. fglFormat := GL_ALPHA;
  2212. fglInternalFormat := {$IFNDEF OPENGL_ES}GL_ALPHA8{$ELSE}GL_ALPHA{$ENDIF};
  2213. fglDataFormat := GL_UNSIGNED_BYTE;
  2214. end;
  2215. procedure TfdAlpha16us1.SetValues;
  2216. begin
  2217. inherited SetValues;
  2218. fBitsPerPixel := 16;
  2219. fFormat := tfAlpha16us1;
  2220. fWithAlpha := tfAlpha16us1;
  2221. fPrecision := glBitmapRec4ub(0, 0, 0, 16);
  2222. fShift := glBitmapRec4ub(0, 0, 0, 0);
  2223. {$IFNDEF OPENGL_ES}
  2224. fOpenGLFormat := tfAlpha16us1;
  2225. fglFormat := GL_ALPHA;
  2226. fglInternalFormat := GL_ALPHA16;
  2227. fglDataFormat := GL_UNSIGNED_SHORT;
  2228. {$ELSE}
  2229. fOpenGLFormat := tfAlpha8ub1;
  2230. {$ENDIF}
  2231. end;
  2232. procedure TfdLuminance4ub1.SetValues;
  2233. begin
  2234. inherited SetValues;
  2235. fBitsPerPixel := 8;
  2236. fFormat := tfLuminance4ub1;
  2237. fWithAlpha := tfLuminance4Alpha4ub2;
  2238. fWithoutAlpha := tfLuminance4ub1;
  2239. fPrecision := glBitmapRec4ub(8, 8, 8, 0);
  2240. fShift := glBitmapRec4ub(0, 0, 0, 0);
  2241. {$IFNDEF OPENGL_ES}
  2242. fOpenGLFormat := tfLuminance4ub1;
  2243. fglFormat := GL_LUMINANCE;
  2244. fglInternalFormat := GL_LUMINANCE4;
  2245. fglDataFormat := GL_UNSIGNED_BYTE;
  2246. {$ELSE}
  2247. fOpenGLFormat := tfLuminance8ub1;
  2248. {$ENDIF}
  2249. end;
  2250. procedure TfdLuminance8ub1.SetValues;
  2251. begin
  2252. inherited SetValues;
  2253. fBitsPerPixel := 8;
  2254. fFormat := tfLuminance8ub1;
  2255. fWithAlpha := tfLuminance8Alpha8ub2;
  2256. fWithoutAlpha := tfLuminance8ub1;
  2257. fOpenGLFormat := tfLuminance8ub1;
  2258. fPrecision := glBitmapRec4ub(8, 8, 8, 0);
  2259. fShift := glBitmapRec4ub(0, 0, 0, 0);
  2260. fglFormat := GL_LUMINANCE;
  2261. fglInternalFormat := {$IFNDEF OPENGL_ES}GL_LUMINANCE8{$ELSE}GL_LUMINANCE{$ENDIF};
  2262. fglDataFormat := GL_UNSIGNED_BYTE;
  2263. end;
  2264. procedure TfdLuminance16us1.SetValues;
  2265. begin
  2266. inherited SetValues;
  2267. fBitsPerPixel := 16;
  2268. fFormat := tfLuminance16us1;
  2269. fWithAlpha := tfLuminance16Alpha16us2;
  2270. fWithoutAlpha := tfLuminance16us1;
  2271. fPrecision := glBitmapRec4ub(16, 16, 16, 0);
  2272. fShift := glBitmapRec4ub( 0, 0, 0, 0);
  2273. {$IFNDEF OPENGL_ES}
  2274. fOpenGLFormat := tfLuminance16us1;
  2275. fglFormat := GL_LUMINANCE;
  2276. fglInternalFormat := GL_LUMINANCE16;
  2277. fglDataFormat := GL_UNSIGNED_SHORT;
  2278. {$ELSE}
  2279. fOpenGLFormat := tfLuminance8ub1;
  2280. {$ENDIF}
  2281. end;
  2282. procedure TfdLuminance4Alpha4ub2.SetValues;
  2283. begin
  2284. inherited SetValues;
  2285. fBitsPerPixel := 16;
  2286. fFormat := tfLuminance4Alpha4ub2;
  2287. fWithAlpha := tfLuminance4Alpha4ub2;
  2288. fWithoutAlpha := tfLuminance4ub1;
  2289. fPrecision := glBitmapRec4ub(8, 8, 8, 8);
  2290. fShift := glBitmapRec4ub(0, 0, 0, 8);
  2291. {$IFNDEF OPENGL_ES}
  2292. fOpenGLFormat := tfLuminance4Alpha4ub2;
  2293. fglFormat := GL_LUMINANCE_ALPHA;
  2294. fglInternalFormat := GL_LUMINANCE4_ALPHA4;
  2295. fglDataFormat := GL_UNSIGNED_BYTE;
  2296. {$ELSE}
  2297. fOpenGLFormat := tfLuminance8Alpha8ub2;
  2298. {$ENDIF}
  2299. end;
  2300. procedure TfdLuminance6Alpha2ub2.SetValues;
  2301. begin
  2302. inherited SetValues;
  2303. fBitsPerPixel := 16;
  2304. fFormat := tfLuminance6Alpha2ub2;
  2305. fWithAlpha := tfLuminance6Alpha2ub2;
  2306. fWithoutAlpha := tfLuminance8ub1;
  2307. fPrecision := glBitmapRec4ub(8, 8, 8, 8);
  2308. fShift := glBitmapRec4ub(0, 0, 0, 8);
  2309. {$IFNDEF OPENGL_ES}
  2310. fOpenGLFormat := tfLuminance6Alpha2ub2;
  2311. fglFormat := GL_LUMINANCE_ALPHA;
  2312. fglInternalFormat := GL_LUMINANCE6_ALPHA2;
  2313. fglDataFormat := GL_UNSIGNED_BYTE;
  2314. {$ELSE}
  2315. fOpenGLFormat := tfLuminance8Alpha8ub2;
  2316. {$ENDIF}
  2317. end;
  2318. procedure TfdLuminance8Alpha8ub2.SetValues;
  2319. begin
  2320. inherited SetValues;
  2321. fBitsPerPixel := 16;
  2322. fFormat := tfLuminance8Alpha8ub2;
  2323. fWithAlpha := tfLuminance8Alpha8ub2;
  2324. fWithoutAlpha := tfLuminance8ub1;
  2325. fOpenGLFormat := tfLuminance8Alpha8ub2;
  2326. fPrecision := glBitmapRec4ub(8, 8, 8, 8);
  2327. fShift := glBitmapRec4ub(0, 0, 0, 8);
  2328. fglFormat := GL_LUMINANCE_ALPHA;
  2329. fglInternalFormat := {$IFNDEF OPENGL_ES}GL_LUMINANCE8_ALPHA8{$ELSE}GL_LUMINANCE_ALPHA{$ENDIF};
  2330. fglDataFormat := GL_UNSIGNED_BYTE;
  2331. end;
  2332. procedure TfdLuminance12Alpha4us2.SetValues;
  2333. begin
  2334. inherited SetValues;
  2335. fBitsPerPixel := 32;
  2336. fFormat := tfLuminance12Alpha4us2;
  2337. fWithAlpha := tfLuminance12Alpha4us2;
  2338. fWithoutAlpha := tfLuminance16us1;
  2339. fPrecision := glBitmapRec4ub(16, 16, 16, 16);
  2340. fShift := glBitmapRec4ub( 0, 0, 0, 16);
  2341. {$IFNDEF OPENGL_ES}
  2342. fOpenGLFormat := tfLuminance12Alpha4us2;
  2343. fglFormat := GL_LUMINANCE_ALPHA;
  2344. fglInternalFormat := GL_LUMINANCE12_ALPHA4;
  2345. fglDataFormat := GL_UNSIGNED_SHORT;
  2346. {$ELSE}
  2347. fOpenGLFormat := tfLuminance8Alpha8ub2;
  2348. {$ENDIF}
  2349. end;
  2350. procedure TfdLuminance16Alpha16us2.SetValues;
  2351. begin
  2352. inherited SetValues;
  2353. fBitsPerPixel := 32;
  2354. fFormat := tfLuminance16Alpha16us2;
  2355. fWithAlpha := tfLuminance16Alpha16us2;
  2356. fWithoutAlpha := tfLuminance16us1;
  2357. fPrecision := glBitmapRec4ub(16, 16, 16, 16);
  2358. fShift := glBitmapRec4ub( 0, 0, 0, 16);
  2359. {$IFNDEF OPENGL_ES}
  2360. fOpenGLFormat := tfLuminance16Alpha16us2;
  2361. fglFormat := GL_LUMINANCE_ALPHA;
  2362. fglInternalFormat := GL_LUMINANCE16_ALPHA16;
  2363. fglDataFormat := GL_UNSIGNED_SHORT;
  2364. {$ELSE}
  2365. fOpenGLFormat := tfLuminance8Alpha8ub2;
  2366. {$ENDIF}
  2367. end;
  2368. procedure TfdR3G3B2ub1.SetValues;
  2369. begin
  2370. inherited SetValues;
  2371. fBitsPerPixel := 8;
  2372. fFormat := tfR3G3B2ub1;
  2373. fWithAlpha := tfRGBA4us1;
  2374. fWithoutAlpha := tfR3G3B2ub1;
  2375. fRGBInverted := tfEmpty;
  2376. fPrecision := glBitmapRec4ub(3, 3, 2, 0);
  2377. fShift := glBitmapRec4ub(5, 2, 0, 0);
  2378. {$IFNDEF OPENGL_ES}
  2379. fOpenGLFormat := tfR3G3B2ub1;
  2380. fglFormat := GL_RGB;
  2381. fglInternalFormat := GL_R3_G3_B2;
  2382. fglDataFormat := GL_UNSIGNED_BYTE_3_3_2;
  2383. {$ELSE}
  2384. fOpenGLFormat := tfR5G6B5us1;
  2385. {$ENDIF}
  2386. end;
  2387. procedure TfdRGBX4us1.SetValues;
  2388. begin
  2389. inherited SetValues;
  2390. fBitsPerPixel := 16;
  2391. fFormat := tfRGBX4us1;
  2392. fWithAlpha := tfRGBA4us1;
  2393. fWithoutAlpha := tfRGBX4us1;
  2394. fRGBInverted := tfBGRX4us1;
  2395. fPrecision := glBitmapRec4ub( 4, 4, 4, 0);
  2396. fShift := glBitmapRec4ub(12, 8, 4, 0);
  2397. {$IFNDEF OPENGL_ES}
  2398. fOpenGLFormat := tfRGBX4us1;
  2399. fglFormat := GL_RGBA; //GL_INVALID_OPERATION if not GL_BGRA or GL_RGBA
  2400. fglInternalFormat := GL_RGB4;
  2401. fglDataFormat := GL_UNSIGNED_SHORT_4_4_4_4;
  2402. {$ELSE}
  2403. fOpenGLFormat := tfR5G6B5us1;
  2404. {$ENDIF}
  2405. end;
  2406. procedure TfdXRGB4us1.SetValues;
  2407. begin
  2408. inherited SetValues;
  2409. fBitsPerPixel := 16;
  2410. fFormat := tfXRGB4us1;
  2411. fWithAlpha := tfARGB4us1;
  2412. fWithoutAlpha := tfXRGB4us1;
  2413. fRGBInverted := tfXBGR4us1;
  2414. fPrecision := glBitmapRec4ub(4, 4, 4, 0);
  2415. fShift := glBitmapRec4ub(8, 4, 0, 0);
  2416. {$IFNDEF OPENGL_ES}
  2417. fOpenGLFormat := tfXRGB4us1;
  2418. fglFormat := GL_BGRA;
  2419. fglInternalFormat := GL_RGB4;
  2420. fglDataFormat := GL_UNSIGNED_SHORT_4_4_4_4_REV;
  2421. {$ELSE}
  2422. fOpenGLFormat := tfR5G6B5us1;
  2423. {$ENDIF}
  2424. end;
  2425. procedure TfdR5G6B5us1.SetValues;
  2426. begin
  2427. inherited SetValues;
  2428. fBitsPerPixel := 16;
  2429. fFormat := tfR5G6B5us1;
  2430. fWithAlpha := tfRGB5A1us1;
  2431. fWithoutAlpha := tfR5G6B5us1;
  2432. fRGBInverted := tfB5G6R5us1;
  2433. fPrecision := glBitmapRec4ub( 5, 6, 5, 0);
  2434. fShift := glBitmapRec4ub(11, 5, 0, 0);
  2435. {$IF NOT DEFINED(OPENGL_ES) OR DEFINED(OPENGL_ES_2_0)}
  2436. fOpenGLFormat := tfR5G6B5us1;
  2437. fglFormat := GL_RGB;
  2438. fglInternalFormat := GL_RGB565;
  2439. fglDataFormat := GL_UNSIGNED_SHORT_5_6_5;
  2440. {$ELSE}
  2441. fOpenGLFormat := tfRGB8ub3;
  2442. {$IFEND}
  2443. end;
  2444. procedure TfdRGB5X1us1.SetValues;
  2445. begin
  2446. inherited SetValues;
  2447. fBitsPerPixel := 16;
  2448. fFormat := tfRGB5X1us1;
  2449. fWithAlpha := tfRGB5A1us1;
  2450. fWithoutAlpha := tfRGB5X1us1;
  2451. fRGBInverted := tfBGR5X1us1;
  2452. fPrecision := glBitmapRec4ub( 5, 5, 5, 0);
  2453. fShift := glBitmapRec4ub(11, 6, 1, 0);
  2454. {$IFNDEF OPENGL_ES}
  2455. fOpenGLFormat := tfRGB5X1us1;
  2456. fglFormat := GL_RGBA; //GL_INVALID_OPERATION if not GL_BGRA or GL_RGBA
  2457. fglInternalFormat := GL_RGB5;
  2458. fglDataFormat := GL_UNSIGNED_SHORT_5_5_5_1;
  2459. {$ELSE}
  2460. fOpenGLFormat := tfR5G6B5us1;
  2461. {$ENDIF}
  2462. end;
  2463. procedure TfdX1RGB5us1.SetValues;
  2464. begin
  2465. inherited SetValues;
  2466. fBitsPerPixel := 16;
  2467. fFormat := tfX1RGB5us1;
  2468. fWithAlpha := tfA1RGB5us1;
  2469. fWithoutAlpha := tfX1RGB5us1;
  2470. fRGBInverted := tfX1BGR5us1;
  2471. fPrecision := glBitmapRec4ub( 5, 5, 5, 0);
  2472. fShift := glBitmapRec4ub(10, 5, 0, 0);
  2473. {$IFNDEF OPENGL_ES}
  2474. fOpenGLFormat := tfX1RGB5us1;
  2475. fglFormat := GL_BGRA; //GL_INVALID_OPERATION if not GL_BGRA or GL_RGBA
  2476. fglInternalFormat := GL_RGB5;
  2477. fglDataFormat := GL_UNSIGNED_SHORT_1_5_5_5_REV;
  2478. {$ELSE}
  2479. fOpenGLFormat := tfR5G6B5us1;
  2480. {$ENDIF}
  2481. end;
  2482. procedure TfdRGB8ub3.SetValues;
  2483. begin
  2484. inherited SetValues;
  2485. fBitsPerPixel := 24;
  2486. fFormat := tfRGB8ub3;
  2487. fWithAlpha := tfRGBA8ub4;
  2488. fWithoutAlpha := tfRGB8ub3;
  2489. fRGBInverted := tfBGR8ub3;
  2490. fPrecision := glBitmapRec4ub(8, 8, 8, 0);
  2491. fShift := glBitmapRec4ub(0, 8, 16, 0);
  2492. fOpenGLFormat := tfRGB8ub3;
  2493. fglFormat := GL_RGB;
  2494. fglInternalFormat := {$IF NOT DEFINED(OPENGL_ES) OR DEFINED(OPENGL_ES_3_0)}GL_RGB8{$ELSE}GL_RGB{$IFEND};
  2495. fglDataFormat := GL_UNSIGNED_BYTE;
  2496. end;
  2497. procedure TfdRGBX8ui1.SetValues;
  2498. begin
  2499. inherited SetValues;
  2500. fBitsPerPixel := 32;
  2501. fFormat := tfRGBX8ui1;
  2502. fWithAlpha := tfRGBA8ui1;
  2503. fWithoutAlpha := tfRGBX8ui1;
  2504. fRGBInverted := tfBGRX8ui1;
  2505. fPrecision := glBitmapRec4ub( 8, 8, 8, 0);
  2506. fShift := glBitmapRec4ub(24, 16, 8, 0);
  2507. {$IFNDEF OPENGL_ES}
  2508. fOpenGLFormat := tfRGBX8ui1;
  2509. fglFormat := GL_RGBA; //GL_INVALID_OPERATION if not GL_BGRA or GL_RGBA
  2510. fglInternalFormat := GL_RGB8;
  2511. fglDataFormat := GL_UNSIGNED_INT_8_8_8_8;
  2512. {$ELSE}
  2513. fOpenGLFormat := tfRGB8ub3;
  2514. {$ENDIF}
  2515. end;
  2516. procedure TfdXRGB8ui1.SetValues;
  2517. begin
  2518. inherited SetValues;
  2519. fBitsPerPixel := 32;
  2520. fFormat := tfXRGB8ui1;
  2521. fWithAlpha := tfXRGB8ui1;
  2522. fWithoutAlpha := tfXRGB8ui1;
  2523. fOpenGLFormat := tfXRGB8ui1;
  2524. fRGBInverted := tfXBGR8ui1;
  2525. fPrecision := glBitmapRec4ub( 8, 8, 8, 0);
  2526. fShift := glBitmapRec4ub(16, 8, 0, 0);
  2527. {$IFNDEF OPENGL_ES}
  2528. fOpenGLFormat := tfXRGB8ui1;
  2529. fglFormat := GL_BGRA; //GL_INVALID_OPERATION if not GL_BGRA or GL_RGBA
  2530. fglInternalFormat := GL_RGB8;
  2531. fglDataFormat := GL_UNSIGNED_INT_8_8_8_8_REV;
  2532. {$ELSE}
  2533. fOpenGLFormat := tfRGB8ub3;
  2534. {$ENDIF}
  2535. end;
  2536. procedure TfdRGB10X2ui1.SetValues;
  2537. begin
  2538. inherited SetValues;
  2539. fBitsPerPixel := 32;
  2540. fFormat := tfRGB10X2ui1;
  2541. fWithAlpha := tfRGB10A2ui1;
  2542. fWithoutAlpha := tfRGB10X2ui1;
  2543. fRGBInverted := tfBGR10X2ui1;
  2544. fPrecision := glBitmapRec4ub(10, 10, 10, 0);
  2545. fShift := glBitmapRec4ub(22, 12, 2, 0);
  2546. {$IFNDEF OPENGL_ES}
  2547. fOpenGLFormat := tfRGB10X2ui1;
  2548. fglFormat := GL_RGBA; //GL_INVALID_OPERATION if not GL_BGRA or GL_RGBA
  2549. fglInternalFormat := GL_RGB10;
  2550. fglDataFormat := GL_UNSIGNED_INT_10_10_10_2;
  2551. {$ELSE}
  2552. fOpenGLFormat := tfRGB16us3;
  2553. {$ENDIF}
  2554. end;
  2555. procedure TfdX2RGB10ui1.SetValues;
  2556. begin
  2557. inherited SetValues;
  2558. fBitsPerPixel := 32;
  2559. fFormat := tfX2RGB10ui1;
  2560. fWithAlpha := tfA2RGB10ui1;
  2561. fWithoutAlpha := tfX2RGB10ui1;
  2562. fRGBInverted := tfX2BGR10ui1;
  2563. fPrecision := glBitmapRec4ub(10, 10, 10, 0);
  2564. fShift := glBitmapRec4ub(20, 10, 0, 0);
  2565. {$IFNDEF OPENGL_ES}
  2566. fOpenGLFormat := tfX2RGB10ui1;
  2567. fglFormat := GL_BGRA; //GL_INVALID_OPERATION if not GL_BGRA or GL_RGBA
  2568. fglInternalFormat := GL_RGB10;
  2569. fglDataFormat := GL_UNSIGNED_INT_2_10_10_10_REV;
  2570. {$ELSE}
  2571. fOpenGLFormat := tfRGB16us3;
  2572. {$ENDIF}
  2573. end;
  2574. procedure TfdRGB16us3.SetValues;
  2575. begin
  2576. inherited SetValues;
  2577. fBitsPerPixel := 48;
  2578. fFormat := tfRGB16us3;
  2579. fWithAlpha := tfRGBA16us4;
  2580. fWithoutAlpha := tfRGB16us3;
  2581. fRGBInverted := tfBGR16us3;
  2582. fPrecision := glBitmapRec4ub(16, 16, 16, 0);
  2583. fShift := glBitmapRec4ub( 0, 16, 32, 0);
  2584. {$IF NOT DEFINED(OPENGL_ES) OR DEFINED(OPENGL_ES_3_0)}
  2585. fOpenGLFormat := tfRGB16us3;
  2586. fglFormat := GL_RGB;
  2587. fglInternalFormat := {$IFNDEF OPENGL_ES}GL_RGB16{$ELSE}GL_RGB16UI{$ENDIF};
  2588. fglDataFormat := GL_UNSIGNED_SHORT;
  2589. {$ELSE}
  2590. fOpenGLFormat := tfRGB8ub3;
  2591. {$IFEND}
  2592. end;
  2593. procedure TfdRGBA4us1.SetValues;
  2594. begin
  2595. inherited SetValues;
  2596. fBitsPerPixel := 16;
  2597. fFormat := tfRGBA4us1;
  2598. fWithAlpha := tfRGBA4us1;
  2599. fWithoutAlpha := tfRGBX4us1;
  2600. fOpenGLFormat := tfRGBA4us1;
  2601. fRGBInverted := tfBGRA4us1;
  2602. fPrecision := glBitmapRec4ub( 4, 4, 4, 4);
  2603. fShift := glBitmapRec4ub(12, 8, 4, 0);
  2604. fglFormat := GL_RGBA;
  2605. fglInternalFormat := {$IF NOT DEFINED(OPENGL_ES) OR DEFINED(OPENGL_ES_3_0)}GL_RGBA8{$ELSE}GL_RGBA{$IFEND};
  2606. fglDataFormat := GL_UNSIGNED_SHORT_4_4_4_4;
  2607. end;
  2608. procedure TfdARGB4us1.SetValues;
  2609. begin
  2610. inherited SetValues;
  2611. fBitsPerPixel := 16;
  2612. fFormat := tfARGB4us1;
  2613. fWithAlpha := tfARGB4us1;
  2614. fWithoutAlpha := tfXRGB4us1;
  2615. fRGBInverted := tfABGR4us1;
  2616. fPrecision := glBitmapRec4ub( 4, 4, 4, 4);
  2617. fShift := glBitmapRec4ub( 8, 4, 0, 12);
  2618. {$IFNDEF OPENGL_ES}
  2619. fOpenGLFormat := tfARGB4us1;
  2620. fglFormat := GL_BGRA;
  2621. fglInternalFormat := GL_RGBA4;
  2622. fglDataFormat := GL_UNSIGNED_SHORT_4_4_4_4_REV;
  2623. {$ELSE}
  2624. fOpenGLFormat := tfRGBA4us1;
  2625. {$ENDIF}
  2626. end;
  2627. procedure TfdRGB5A1us1.SetValues;
  2628. begin
  2629. inherited SetValues;
  2630. fBitsPerPixel := 16;
  2631. fFormat := tfRGB5A1us1;
  2632. fWithAlpha := tfRGB5A1us1;
  2633. fWithoutAlpha := tfRGB5X1us1;
  2634. fOpenGLFormat := tfRGB5A1us1;
  2635. fRGBInverted := tfBGR5A1us1;
  2636. fPrecision := glBitmapRec4ub( 5, 5, 5, 1);
  2637. fShift := glBitmapRec4ub(11, 6, 1, 0);
  2638. fglFormat := GL_RGBA;
  2639. fglInternalFormat := {$IF NOT DEFINED(OPENGL_ES) OR DEFINED(OPENGL_ES_2_0)}GL_RGB5_A1{$ELSE}GL_RGBA{$IFEND};
  2640. fglDataFormat := GL_UNSIGNED_SHORT_5_5_5_1;
  2641. end;
  2642. procedure TfdA1RGB5us1.SetValues;
  2643. begin
  2644. inherited SetValues;
  2645. fBitsPerPixel := 16;
  2646. fFormat := tfA1RGB5us1;
  2647. fWithAlpha := tfA1RGB5us1;
  2648. fWithoutAlpha := tfX1RGB5us1;
  2649. fRGBInverted := tfA1BGR5us1;
  2650. fPrecision := glBitmapRec4ub( 5, 5, 5, 1);
  2651. fShift := glBitmapRec4ub(10, 5, 0, 15);
  2652. {$IFNDEF OPENGL_ES}
  2653. fOpenGLFormat := tfA1RGB5us1;
  2654. fglFormat := GL_BGRA;
  2655. fglInternalFormat := GL_RGB5_A1;
  2656. fglDataFormat := GL_UNSIGNED_SHORT_1_5_5_5_REV;
  2657. {$ELSE}
  2658. fOpenGLFormat := tfRGB5A1us1;
  2659. {$ENDIF}
  2660. end;
  2661. procedure TfdRGBA8ui1.SetValues;
  2662. begin
  2663. inherited SetValues;
  2664. fBitsPerPixel := 32;
  2665. fFormat := tfRGBA8ui1;
  2666. fWithAlpha := tfRGBA8ui1;
  2667. fWithoutAlpha := tfRGBX8ui1;
  2668. fRGBInverted := tfBGRA8ui1;
  2669. fPrecision := glBitmapRec4ub( 8, 8, 8, 8);
  2670. fShift := glBitmapRec4ub(24, 16, 8, 0);
  2671. {$IFNDEF OPENGL_ES}
  2672. fOpenGLFormat := tfRGBA8ui1;
  2673. fglFormat := GL_RGBA;
  2674. fglInternalFormat := GL_RGBA8;
  2675. fglDataFormat := GL_UNSIGNED_INT_8_8_8_8;
  2676. {$ELSE}
  2677. fOpenGLFormat := tfRGBA8ub4;
  2678. {$ENDIF}
  2679. end;
  2680. procedure TfdARGB8ui1.SetValues;
  2681. begin
  2682. inherited SetValues;
  2683. fBitsPerPixel := 32;
  2684. fFormat := tfARGB8ui1;
  2685. fWithAlpha := tfARGB8ui1;
  2686. fWithoutAlpha := tfXRGB8ui1;
  2687. fRGBInverted := tfABGR8ui1;
  2688. fPrecision := glBitmapRec4ub( 8, 8, 8, 8);
  2689. fShift := glBitmapRec4ub(16, 8, 0, 24);
  2690. {$IFNDEF OPENGL_ES}
  2691. fOpenGLFormat := tfARGB8ui1;
  2692. fglFormat := GL_BGRA;
  2693. fglInternalFormat := GL_RGBA8;
  2694. fglDataFormat := GL_UNSIGNED_INT_8_8_8_8_REV;
  2695. {$ELSE}
  2696. fOpenGLFormat := tfRGBA8ub4;
  2697. {$ENDIF}
  2698. end;
  2699. procedure TfdRGBA8ub4.SetValues;
  2700. begin
  2701. inherited SetValues;
  2702. fBitsPerPixel := 32;
  2703. fFormat := tfRGBA8ub4;
  2704. fWithAlpha := tfRGBA8ub4;
  2705. fWithoutAlpha := tfRGB8ub3;
  2706. fOpenGLFormat := tfRGBA8ub4;
  2707. fRGBInverted := tfBGRA8ub4;
  2708. fPrecision := glBitmapRec4ub( 8, 8, 8, 8);
  2709. fShift := glBitmapRec4ub( 0, 8, 16, 24);
  2710. fglFormat := GL_RGBA;
  2711. fglInternalFormat := {$IF NOT DEFINED(OPENGL_ES) OR DEFINED(OPENGL_ES_3_0)}GL_RGBA8{$ELSE}GL_RGBA{$IFEND};
  2712. fglDataFormat := GL_UNSIGNED_BYTE;
  2713. end;
  2714. procedure TfdRGB10A2ui1.SetValues;
  2715. begin
  2716. inherited SetValues;
  2717. fBitsPerPixel := 32;
  2718. fFormat := tfRGB10A2ui1;
  2719. fWithAlpha := tfRGB10A2ui1;
  2720. fWithoutAlpha := tfRGB10X2ui1;
  2721. fRGBInverted := tfBGR10A2ui1;
  2722. fPrecision := glBitmapRec4ub(10, 10, 10, 2);
  2723. fShift := glBitmapRec4ub(22, 12, 2, 0);
  2724. {$IFNDEF OPENGL_ES}
  2725. fOpenGLFormat := tfRGB10A2ui1;
  2726. fglFormat := GL_RGBA;
  2727. fglInternalFormat := GL_RGB10_A2;
  2728. fglDataFormat := GL_UNSIGNED_INT_10_10_10_2;
  2729. {$ELSE}
  2730. fOpenGLFormat := tfA2RGB10ui1;
  2731. {$ENDIF}
  2732. end;
  2733. procedure TfdA2RGB10ui1.SetValues;
  2734. begin
  2735. inherited SetValues;
  2736. fBitsPerPixel := 32;
  2737. fFormat := tfA2RGB10ui1;
  2738. fWithAlpha := tfA2RGB10ui1;
  2739. fWithoutAlpha := tfX2RGB10ui1;
  2740. fRGBInverted := tfA2BGR10ui1;
  2741. fPrecision := glBitmapRec4ub(10, 10, 10, 2);
  2742. fShift := glBitmapRec4ub(20, 10, 0, 30);
  2743. {$IF NOT DEFINED(OPENGL_ES)}
  2744. fOpenGLFormat := tfA2RGB10ui1;
  2745. fglFormat := GL_BGRA;
  2746. fglInternalFormat := GL_RGB10_A2;
  2747. fglDataFormat := GL_UNSIGNED_INT_2_10_10_10_REV;
  2748. {$ELSEIF DEFINED(OPENGL_ES_3_0)}
  2749. fOpenGLFormat := tfA2RGB10ui1;
  2750. fglFormat := GL_RGBA;
  2751. fglInternalFormat := GL_RGB10_A2;
  2752. fglDataFormat := GL_UNSIGNED_INT_2_10_10_10_REV;
  2753. {$ELSE}
  2754. fOpenGLFormat := tfRGBA8ui1;
  2755. {$IFEND}
  2756. end;
  2757. procedure TfdRGBA16us4.SetValues;
  2758. begin
  2759. inherited SetValues;
  2760. fBitsPerPixel := 64;
  2761. fFormat := tfRGBA16us4;
  2762. fWithAlpha := tfRGBA16us4;
  2763. fWithoutAlpha := tfRGB16us3;
  2764. fRGBInverted := tfBGRA16us4;
  2765. fPrecision := glBitmapRec4ub(16, 16, 16, 16);
  2766. fShift := glBitmapRec4ub( 0, 16, 32, 48);
  2767. {$IF NOT DEFINED(OPENGL_ES) OR DEFINED(OPENGL_ES_3_0)}
  2768. fOpenGLFormat := tfRGBA16us4;
  2769. fglFormat := GL_RGBA;
  2770. fglInternalFormat := {$IFNDEF OPENGL_ES}GL_RGBA16{$ELSE}GL_RGBA16UI{$ENDIF};
  2771. fglDataFormat := GL_UNSIGNED_SHORT;
  2772. {$ELSE}
  2773. fOpenGLFormat := tfRGBA8ub4;
  2774. {$IFEND}
  2775. end;
  2776. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2777. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2778. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2779. procedure TfdBGRX4us1.SetValues;
  2780. begin
  2781. inherited SetValues;
  2782. fBitsPerPixel := 16;
  2783. fFormat := tfBGRX4us1;
  2784. fWithAlpha := tfBGRA4us1;
  2785. fWithoutAlpha := tfBGRX4us1;
  2786. fRGBInverted := tfRGBX4us1;
  2787. fPrecision := glBitmapRec4ub( 4, 4, 4, 0);
  2788. fShift := glBitmapRec4ub( 4, 8, 12, 0);
  2789. {$IFNDEF OPENGL_ES}
  2790. fOpenGLFormat := tfBGRX4us1;
  2791. fglFormat := GL_BGRA; //GL_INVALID_OPERATION if not GL_BGRA or GL_RGBA
  2792. fglInternalFormat := GL_RGB4;
  2793. fglDataFormat := GL_UNSIGNED_SHORT_4_4_4_4;
  2794. {$ELSE}
  2795. fOpenGLFormat := tfR5G6B5us1;
  2796. {$ENDIF}
  2797. end;
  2798. procedure TfdXBGR4us1.SetValues;
  2799. begin
  2800. inherited SetValues;
  2801. fBitsPerPixel := 16;
  2802. fFormat := tfXBGR4us1;
  2803. fWithAlpha := tfABGR4us1;
  2804. fWithoutAlpha := tfXBGR4us1;
  2805. fRGBInverted := tfXRGB4us1;
  2806. fPrecision := glBitmapRec4ub( 4, 4, 4, 0);
  2807. fShift := glBitmapRec4ub( 0, 4, 8, 0);
  2808. {$IFNDEF OPENGL_ES}
  2809. fOpenGLFormat := tfXBGR4us1;
  2810. fglFormat := GL_RGBA; //GL_INVALID_OPERATION if not GL_BGRA or GL_RGBA
  2811. fglInternalFormat := GL_RGB4;
  2812. fglDataFormat := GL_UNSIGNED_SHORT_4_4_4_4_REV;
  2813. {$ELSE}
  2814. fOpenGLFormat := tfR5G6B5us1;
  2815. {$ENDIF}
  2816. end;
  2817. procedure TfdB5G6R5us1.SetValues;
  2818. begin
  2819. inherited SetValues;
  2820. fBitsPerPixel := 16;
  2821. fFormat := tfB5G6R5us1;
  2822. fWithAlpha := tfBGR5A1us1;
  2823. fWithoutAlpha := tfB5G6R5us1;
  2824. fRGBInverted := tfR5G6B5us1;
  2825. fPrecision := glBitmapRec4ub( 5, 6, 5, 0);
  2826. fShift := glBitmapRec4ub( 0, 5, 11, 0);
  2827. {$IFNDEF OPENGL_ES}
  2828. fOpenGLFormat := tfB5G6R5us1;
  2829. fglFormat := GL_RGB;
  2830. fglInternalFormat := GL_RGB565;
  2831. fglDataFormat := GL_UNSIGNED_SHORT_5_6_5_REV;
  2832. {$ELSE}
  2833. fOpenGLFormat := tfR5G6B5us1;
  2834. {$ENDIF}
  2835. end;
  2836. procedure TfdBGR5X1us1.SetValues;
  2837. begin
  2838. inherited SetValues;
  2839. fBitsPerPixel := 16;
  2840. fFormat := tfBGR5X1us1;
  2841. fWithAlpha := tfBGR5A1us1;
  2842. fWithoutAlpha := tfBGR5X1us1;
  2843. fRGBInverted := tfRGB5X1us1;
  2844. fPrecision := glBitmapRec4ub( 5, 5, 5, 0);
  2845. fShift := glBitmapRec4ub( 1, 6, 11, 0);
  2846. {$IFNDEF OPENGL_ES}
  2847. fOpenGLFormat := tfBGR5X1us1;
  2848. fglFormat := GL_BGRA;
  2849. fglInternalFormat := GL_RGB5;
  2850. fglDataFormat := GL_UNSIGNED_SHORT_5_5_5_1;
  2851. {$ELSE}
  2852. fOpenGLFormat := tfR5G6B5us1;
  2853. {$ENDIF}
  2854. end;
  2855. procedure TfdX1BGR5us1.SetValues;
  2856. begin
  2857. inherited SetValues;
  2858. fBitsPerPixel := 16;
  2859. fFormat := tfX1BGR5us1;
  2860. fWithAlpha := tfA1BGR5us1;
  2861. fWithoutAlpha := tfX1BGR5us1;
  2862. fRGBInverted := tfX1RGB5us1;
  2863. fPrecision := glBitmapRec4ub( 5, 5, 5, 0);
  2864. fShift := glBitmapRec4ub( 0, 5, 10, 0);
  2865. {$IFNDEF OPENGL_ES}
  2866. fOpenGLFormat := tfX1BGR5us1;
  2867. fglFormat := GL_RGBA; //GL_INVALID_OPERATION if not GL_BGRA or GL_RGBA
  2868. fglInternalFormat := GL_RGB5;
  2869. fglDataFormat := GL_UNSIGNED_SHORT_1_5_5_5_REV;
  2870. {$ELSE}
  2871. fOpenGLFormat := tfR5G6B5us1;
  2872. {$ENDIF}
  2873. end;
  2874. procedure TfdBGR8ub3.SetValues;
  2875. begin
  2876. inherited SetValues;
  2877. fBitsPerPixel := 24;
  2878. fFormat := tfBGR8ub3;
  2879. fWithAlpha := tfBGRA8ub4;
  2880. fWithoutAlpha := tfBGR8ub3;
  2881. fRGBInverted := tfRGB8ub3;
  2882. fPrecision := glBitmapRec4ub( 8, 8, 8, 0);
  2883. fShift := glBitmapRec4ub(16, 8, 0, 0);
  2884. {$IFNDEF OPENGL_ES}
  2885. fOpenGLFormat := tfBGR8ub3;
  2886. fglFormat := GL_BGR;
  2887. fglInternalFormat := GL_RGB8;
  2888. fglDataFormat := GL_UNSIGNED_BYTE;
  2889. {$ELSE}
  2890. fOpenGLFormat := tfRGB8ub3;
  2891. {$ENDIF}
  2892. end;
  2893. procedure TfdBGRX8ui1.SetValues;
  2894. begin
  2895. inherited SetValues;
  2896. fBitsPerPixel := 32;
  2897. fFormat := tfBGRX8ui1;
  2898. fWithAlpha := tfBGRA8ui1;
  2899. fWithoutAlpha := tfBGRX8ui1;
  2900. fRGBInverted := tfRGBX8ui1;
  2901. fPrecision := glBitmapRec4ub( 8, 8, 8, 0);
  2902. fShift := glBitmapRec4ub( 8, 16, 24, 0);
  2903. {$IFNDEF OPENGL_ES}
  2904. fOpenGLFormat := tfBGRX8ui1;
  2905. fglFormat := GL_BGRA; //GL_INVALID_OPERATION if not GL_BGRA or GL_RGBA
  2906. fglInternalFormat := GL_RGB8;
  2907. fglDataFormat := GL_UNSIGNED_INT_8_8_8_8;
  2908. {$ELSE}
  2909. fOpenGLFormat := tfRGB8ub3;
  2910. {$ENDIF}
  2911. end;
  2912. procedure TfdXBGR8ui1.SetValues;
  2913. begin
  2914. inherited SetValues;
  2915. fBitsPerPixel := 32;
  2916. fFormat := tfXBGR8ui1;
  2917. fWithAlpha := tfABGR8ui1;
  2918. fWithoutAlpha := tfXBGR8ui1;
  2919. fRGBInverted := tfXRGB8ui1;
  2920. fPrecision := glBitmapRec4ub( 8, 8, 8, 0);
  2921. fShift := glBitmapRec4ub( 0, 8, 16, 0);
  2922. {$IFNDEF OPENGL_ES}
  2923. fOpenGLFormat := tfXBGR8ui1;
  2924. fglFormat := GL_RGBA; //GL_INVALID_OPERATION if not GL_BGRA or GL_RGBA
  2925. fglInternalFormat := GL_RGB8;
  2926. fglDataFormat := GL_UNSIGNED_INT_8_8_8_8_REV;
  2927. {$ELSE}
  2928. fOpenGLFormat := tfRGB8ub3;
  2929. {$ENDIF}
  2930. end;
  2931. procedure TfdBGR10X2ui1.SetValues;
  2932. begin
  2933. inherited SetValues;
  2934. fBitsPerPixel := 32;
  2935. fFormat := tfBGR10X2ui1;
  2936. fWithAlpha := tfBGR10A2ui1;
  2937. fWithoutAlpha := tfBGR10X2ui1;
  2938. fRGBInverted := tfRGB10X2ui1;
  2939. fPrecision := glBitmapRec4ub(10, 10, 10, 0);
  2940. fShift := glBitmapRec4ub( 2, 12, 22, 0);
  2941. {$IFNDEF OPENGL_ES}
  2942. fOpenGLFormat := tfBGR10X2ui1;
  2943. fglFormat := GL_BGRA; //GL_INVALID_OPERATION if not GL_BGRA or GL_RGBA
  2944. fglInternalFormat := GL_RGB10;
  2945. fglDataFormat := GL_UNSIGNED_INT_10_10_10_2;
  2946. {$ELSE}
  2947. fOpenGLFormat := tfRGB16us3;
  2948. {$ENDIF}
  2949. end;
  2950. procedure TfdX2BGR10ui1.SetValues;
  2951. begin
  2952. inherited SetValues;
  2953. fBitsPerPixel := 32;
  2954. fFormat := tfX2BGR10ui1;
  2955. fWithAlpha := tfA2BGR10ui1;
  2956. fWithoutAlpha := tfX2BGR10ui1;
  2957. fRGBInverted := tfX2RGB10ui1;
  2958. fPrecision := glBitmapRec4ub(10, 10, 10, 0);
  2959. fShift := glBitmapRec4ub( 0, 10, 20, 0);
  2960. {$IFNDEF OPENGL_ES}
  2961. fOpenGLFormat := tfX2BGR10ui1;
  2962. fglFormat := GL_RGBA; //GL_INVALID_OPERATION if not GL_BGRA or GL_RGBA
  2963. fglInternalFormat := GL_RGB10;
  2964. fglDataFormat := GL_UNSIGNED_INT_2_10_10_10_REV;
  2965. {$ELSE}
  2966. fOpenGLFormat := tfRGB16us3;
  2967. {$ENDIF}
  2968. end;
  2969. procedure TfdBGR16us3.SetValues;
  2970. begin
  2971. inherited SetValues;
  2972. fBitsPerPixel := 48;
  2973. fFormat := tfBGR16us3;
  2974. fWithAlpha := tfBGRA16us4;
  2975. fWithoutAlpha := tfBGR16us3;
  2976. fRGBInverted := tfRGB16us3;
  2977. fPrecision := glBitmapRec4ub(16, 16, 16, 0);
  2978. fShift := glBitmapRec4ub(32, 16, 0, 0);
  2979. {$IFNDEF OPENGL_ES}
  2980. fOpenGLFormat := tfBGR16us3;
  2981. fglFormat := GL_BGR;
  2982. fglInternalFormat := GL_RGB16;
  2983. fglDataFormat := GL_UNSIGNED_SHORT;
  2984. {$ELSE}
  2985. fOpenGLFormat := tfRGB16us3;
  2986. {$ENDIF}
  2987. end;
  2988. procedure TfdBGRA4us1.SetValues;
  2989. begin
  2990. inherited SetValues;
  2991. fBitsPerPixel := 16;
  2992. fFormat := tfBGRA4us1;
  2993. fWithAlpha := tfBGRA4us1;
  2994. fWithoutAlpha := tfBGRX4us1;
  2995. fRGBInverted := tfRGBA4us1;
  2996. fPrecision := glBitmapRec4ub( 4, 4, 4, 4);
  2997. fShift := glBitmapRec4ub( 4, 8, 12, 0);
  2998. {$IFNDEF OPENGL_ES}
  2999. fOpenGLFormat := tfBGRA4us1;
  3000. fglFormat := GL_BGRA;
  3001. fglInternalFormat := GL_RGBA4;
  3002. fglDataFormat := GL_UNSIGNED_SHORT_4_4_4_4;
  3003. {$ELSE}
  3004. fOpenGLFormat := tfRGBA4us1;
  3005. {$ENDIF}
  3006. end;
  3007. procedure TfdABGR4us1.SetValues;
  3008. begin
  3009. inherited SetValues;
  3010. fBitsPerPixel := 16;
  3011. fFormat := tfABGR4us1;
  3012. fWithAlpha := tfABGR4us1;
  3013. fWithoutAlpha := tfXBGR4us1;
  3014. fRGBInverted := tfARGB4us1;
  3015. fPrecision := glBitmapRec4ub( 4, 4, 4, 4);
  3016. fShift := glBitmapRec4ub( 0, 4, 8, 12);
  3017. {$IFNDEF OPENGL_ES}
  3018. fOpenGLFormat := tfABGR4us1;
  3019. fglFormat := GL_RGBA;
  3020. fglInternalFormat := GL_RGBA4;
  3021. fglDataFormat := GL_UNSIGNED_SHORT_4_4_4_4_REV;
  3022. {$ELSE}
  3023. fOpenGLFormat := tfRGBA4us1;
  3024. {$ENDIF}
  3025. end;
  3026. procedure TfdBGR5A1us1.SetValues;
  3027. begin
  3028. inherited SetValues;
  3029. fBitsPerPixel := 16;
  3030. fFormat := tfBGR5A1us1;
  3031. fWithAlpha := tfBGR5A1us1;
  3032. fWithoutAlpha := tfBGR5X1us1;
  3033. fRGBInverted := tfRGB5A1us1;
  3034. fPrecision := glBitmapRec4ub( 5, 5, 5, 1);
  3035. fShift := glBitmapRec4ub( 1, 6, 11, 0);
  3036. {$IFNDEF OPENGL_ES}
  3037. fOpenGLFormat := tfBGR5A1us1;
  3038. fglFormat := GL_BGRA;
  3039. fglInternalFormat := GL_RGB5_A1;
  3040. fglDataFormat := GL_UNSIGNED_SHORT_5_5_5_1;
  3041. {$ELSE}
  3042. fOpenGLFormat := tfRGB5A1us1;
  3043. {$ENDIF}
  3044. end;
  3045. procedure TfdA1BGR5us1.SetValues;
  3046. begin
  3047. inherited SetValues;
  3048. fBitsPerPixel := 16;
  3049. fFormat := tfA1BGR5us1;
  3050. fWithAlpha := tfA1BGR5us1;
  3051. fWithoutAlpha := tfX1BGR5us1;
  3052. fRGBInverted := tfA1RGB5us1;
  3053. fPrecision := glBitmapRec4ub( 5, 5, 5, 1);
  3054. fShift := glBitmapRec4ub( 0, 5, 10, 15);
  3055. {$IFNDEF OPENGL_ES}
  3056. fOpenGLFormat := tfA1BGR5us1;
  3057. fglFormat := GL_RGBA;
  3058. fglInternalFormat := GL_RGB5_A1;
  3059. fglDataFormat := GL_UNSIGNED_SHORT_1_5_5_5_REV;
  3060. {$ELSE}
  3061. fOpenGLFormat := tfRGB5A1us1;
  3062. {$ENDIF}
  3063. end;
  3064. procedure TfdBGRA8ui1.SetValues;
  3065. begin
  3066. inherited SetValues;
  3067. fBitsPerPixel := 32;
  3068. fFormat := tfBGRA8ui1;
  3069. fWithAlpha := tfBGRA8ui1;
  3070. fWithoutAlpha := tfBGRX8ui1;
  3071. fRGBInverted := tfRGBA8ui1;
  3072. fPrecision := glBitmapRec4ub( 8, 8, 8, 8);
  3073. fShift := glBitmapRec4ub( 8, 16, 24, 0);
  3074. {$IFNDEF OPENGL_ES}
  3075. fOpenGLFormat := tfBGRA8ui1;
  3076. fglFormat := GL_BGRA;
  3077. fglInternalFormat := GL_RGBA8;
  3078. fglDataFormat := GL_UNSIGNED_INT_8_8_8_8;
  3079. {$ELSE}
  3080. fOpenGLFormat := tfRGBA8ub4;
  3081. {$ENDIF}
  3082. end;
  3083. procedure TfdABGR8ui1.SetValues;
  3084. begin
  3085. inherited SetValues;
  3086. fBitsPerPixel := 32;
  3087. fFormat := tfABGR8ui1;
  3088. fWithAlpha := tfABGR8ui1;
  3089. fWithoutAlpha := tfXBGR8ui1;
  3090. fRGBInverted := tfARGB8ui1;
  3091. fPrecision := glBitmapRec4ub( 8, 8, 8, 8);
  3092. fShift := glBitmapRec4ub( 0, 8, 16, 24);
  3093. {$IFNDEF OPENGL_ES}
  3094. fOpenGLFormat := tfABGR8ui1;
  3095. fglFormat := GL_RGBA;
  3096. fglInternalFormat := GL_RGBA8;
  3097. fglDataFormat := GL_UNSIGNED_INT_8_8_8_8_REV;
  3098. {$ELSE}
  3099. fOpenGLFormat := tfRGBA8ub4
  3100. {$ENDIF}
  3101. end;
  3102. procedure TfdBGRA8ub4.SetValues;
  3103. begin
  3104. inherited SetValues;
  3105. fBitsPerPixel := 32;
  3106. fFormat := tfBGRA8ub4;
  3107. fWithAlpha := tfBGRA8ub4;
  3108. fWithoutAlpha := tfBGR8ub3;
  3109. fRGBInverted := tfRGBA8ub4;
  3110. fPrecision := glBitmapRec4ub( 8, 8, 8, 8);
  3111. fShift := glBitmapRec4ub(16, 8, 0, 24);
  3112. {$IFNDEF OPENGL_ES}
  3113. fOpenGLFormat := tfBGRA8ub4;
  3114. fglFormat := GL_BGRA;
  3115. fglInternalFormat := GL_RGBA8;
  3116. fglDataFormat := GL_UNSIGNED_BYTE;
  3117. {$ELSE}
  3118. fOpenGLFormat := tfRGBA8ub4;
  3119. {$ENDIF}
  3120. end;
  3121. procedure TfdBGR10A2ui1.SetValues;
  3122. begin
  3123. inherited SetValues;
  3124. fBitsPerPixel := 32;
  3125. fFormat := tfBGR10A2ui1;
  3126. fWithAlpha := tfBGR10A2ui1;
  3127. fWithoutAlpha := tfBGR10X2ui1;
  3128. fRGBInverted := tfRGB10A2ui1;
  3129. fPrecision := glBitmapRec4ub(10, 10, 10, 2);
  3130. fShift := glBitmapRec4ub( 2, 12, 22, 0);
  3131. {$IFNDEF OPENGL_ES}
  3132. fOpenGLFormat := tfBGR10A2ui1;
  3133. fglFormat := GL_BGRA;
  3134. fglInternalFormat := GL_RGB10_A2;
  3135. fglDataFormat := GL_UNSIGNED_INT_10_10_10_2;
  3136. {$ELSE}
  3137. fOpenGLFormat := tfA2RGB10ui1;
  3138. {$ENDIF}
  3139. end;
  3140. procedure TfdA2BGR10ui1.SetValues;
  3141. begin
  3142. inherited SetValues;
  3143. fBitsPerPixel := 32;
  3144. fFormat := tfA2BGR10ui1;
  3145. fWithAlpha := tfA2BGR10ui1;
  3146. fWithoutAlpha := tfX2BGR10ui1;
  3147. fRGBInverted := tfA2RGB10ui1;
  3148. fPrecision := glBitmapRec4ub(10, 10, 10, 2);
  3149. fShift := glBitmapRec4ub( 0, 10, 20, 30);
  3150. {$IFNDEF OPENGL_ES}
  3151. fOpenGLFormat := tfA2BGR10ui1;
  3152. fglFormat := GL_RGBA;
  3153. fglInternalFormat := GL_RGB10_A2;
  3154. fglDataFormat := GL_UNSIGNED_INT_2_10_10_10_REV;
  3155. {$ELSE}
  3156. fOpenGLFormat := tfA2RGB10ui1;
  3157. {$ENDIF}
  3158. end;
  3159. procedure TfdBGRA16us4.SetValues;
  3160. begin
  3161. inherited SetValues;
  3162. fBitsPerPixel := 64;
  3163. fFormat := tfBGRA16us4;
  3164. fWithAlpha := tfBGRA16us4;
  3165. fWithoutAlpha := tfBGR16us3;
  3166. fRGBInverted := tfRGBA16us4;
  3167. fPrecision := glBitmapRec4ub(16, 16, 16, 16);
  3168. fShift := glBitmapRec4ub(32, 16, 0, 48);
  3169. {$IFNDEF OPENGL_ES}
  3170. fOpenGLFormat := tfBGRA16us4;
  3171. fglFormat := GL_BGRA;
  3172. fglInternalFormat := GL_RGBA16;
  3173. fglDataFormat := GL_UNSIGNED_SHORT;
  3174. {$ELSE}
  3175. fOpenGLFormat := tfRGBA16us4;
  3176. {$ENDIF}
  3177. end;
  3178. procedure TfdDepth16us1.SetValues;
  3179. begin
  3180. inherited SetValues;
  3181. fBitsPerPixel := 16;
  3182. fFormat := tfDepth16us1;
  3183. fWithoutAlpha := tfDepth16us1;
  3184. fPrecision := glBitmapRec4ub(16, 16, 16, 16);
  3185. fShift := glBitmapRec4ub( 0, 0, 0, 0);
  3186. {$IF NOT DEFINED (OPENGL_ES) OR DEFINED(OPENGL_ES_2_0)}
  3187. fOpenGLFormat := tfDepth16us1;
  3188. fglFormat := GL_DEPTH_COMPONENT;
  3189. fglInternalFormat := GL_DEPTH_COMPONENT16;
  3190. fglDataFormat := GL_UNSIGNED_SHORT;
  3191. {$IFEND}
  3192. end;
  3193. procedure TfdDepth24ui1.SetValues;
  3194. begin
  3195. inherited SetValues;
  3196. fBitsPerPixel := 32;
  3197. fFormat := tfDepth24ui1;
  3198. fWithoutAlpha := tfDepth24ui1;
  3199. fOpenGLFormat := tfDepth24ui1;
  3200. fPrecision := glBitmapRec4ub(32, 32, 32, 32);
  3201. fShift := glBitmapRec4ub( 0, 0, 0, 0);
  3202. {$IF NOT DEFINED (OPENGL_ES) OR DEFINED(OPENGL_ES_3_0)}
  3203. fOpenGLFormat := tfDepth24ui1;
  3204. fglFormat := GL_DEPTH_COMPONENT;
  3205. fglInternalFormat := GL_DEPTH_COMPONENT24;
  3206. fglDataFormat := GL_UNSIGNED_INT;
  3207. {$IFEND}
  3208. end;
  3209. procedure TfdDepth32ui1.SetValues;
  3210. begin
  3211. inherited SetValues;
  3212. fBitsPerPixel := 32;
  3213. fFormat := tfDepth32ui1;
  3214. fWithoutAlpha := tfDepth32ui1;
  3215. fPrecision := glBitmapRec4ub(32, 32, 32, 32);
  3216. fShift := glBitmapRec4ub( 0, 0, 0, 0);
  3217. {$IF NOT DEFINED(OPENGL_ES)}
  3218. fOpenGLFormat := tfDepth32ui1;
  3219. fglFormat := GL_DEPTH_COMPONENT;
  3220. fglInternalFormat := GL_DEPTH_COMPONENT32;
  3221. fglDataFormat := GL_UNSIGNED_INT;
  3222. {$ELSEIF DEFINED(OPENGL_ES_3_0)}
  3223. fOpenGLFormat := tfDepth24ui1;
  3224. {$ELSEIF DEFINED(OPENGL_ES_2_0)}
  3225. fOpenGLFormat := tfDepth16us1;
  3226. {$IFEND}
  3227. end;
  3228. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3229. //TfdS3tcDtx1RGBA/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3230. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3231. procedure TfdS3tcDtx1RGBA.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  3232. begin
  3233. raise EglBitmap.Create('mapping for compressed formats is not supported');
  3234. end;
  3235. procedure TfdS3tcDtx1RGBA.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  3236. begin
  3237. raise EglBitmap.Create('mapping for compressed formats is not supported');
  3238. end;
  3239. procedure TfdS3tcDtx1RGBA.SetValues;
  3240. begin
  3241. inherited SetValues;
  3242. fFormat := tfS3tcDtx1RGBA;
  3243. fWithAlpha := tfS3tcDtx1RGBA;
  3244. fUncompressed := tfRGB5A1us1;
  3245. fBitsPerPixel := 4;
  3246. fIsCompressed := true;
  3247. {$IFNDEF OPENGL_ES}
  3248. fOpenGLFormat := tfS3tcDtx1RGBA;
  3249. fglFormat := GL_COMPRESSED_RGBA;
  3250. fglInternalFormat := GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
  3251. fglDataFormat := GL_UNSIGNED_BYTE;
  3252. {$ELSE}
  3253. fOpenGLFormat := fUncompressed;
  3254. {$ENDIF}
  3255. end;
  3256. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3257. //TfdS3tcDtx3RGBA/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3258. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3259. procedure TfdS3tcDtx3RGBA.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  3260. begin
  3261. raise EglBitmap.Create('mapping for compressed formats is not supported');
  3262. end;
  3263. procedure TfdS3tcDtx3RGBA.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  3264. begin
  3265. raise EglBitmap.Create('mapping for compressed formats is not supported');
  3266. end;
  3267. procedure TfdS3tcDtx3RGBA.SetValues;
  3268. begin
  3269. inherited SetValues;
  3270. fFormat := tfS3tcDtx3RGBA;
  3271. fWithAlpha := tfS3tcDtx3RGBA;
  3272. fUncompressed := tfRGBA8ub4;
  3273. fBitsPerPixel := 8;
  3274. fIsCompressed := true;
  3275. {$IFNDEF OPENGL_ES}
  3276. fOpenGLFormat := tfS3tcDtx3RGBA;
  3277. fglFormat := GL_COMPRESSED_RGBA;
  3278. fglInternalFormat := GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
  3279. fglDataFormat := GL_UNSIGNED_BYTE;
  3280. {$ELSE}
  3281. fOpenGLFormat := fUncompressed;
  3282. {$ENDIF}
  3283. end;
  3284. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3285. //TfdS3tcDtx5RGBA/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3286. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3287. procedure TfdS3tcDtx5RGBA.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  3288. begin
  3289. raise EglBitmap.Create('mapping for compressed formats is not supported');
  3290. end;
  3291. procedure TfdS3tcDtx5RGBA.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  3292. begin
  3293. raise EglBitmap.Create('mapping for compressed formats is not supported');
  3294. end;
  3295. procedure TfdS3tcDtx5RGBA.SetValues;
  3296. begin
  3297. inherited SetValues;
  3298. fFormat := tfS3tcDtx3RGBA;
  3299. fWithAlpha := tfS3tcDtx3RGBA;
  3300. fUncompressed := tfRGBA8ub4;
  3301. fBitsPerPixel := 8;
  3302. fIsCompressed := true;
  3303. {$IFNDEF OPENGL_ES}
  3304. fOpenGLFormat := tfS3tcDtx3RGBA;
  3305. fglFormat := GL_COMPRESSED_RGBA;
  3306. fglInternalFormat := GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
  3307. fglDataFormat := GL_UNSIGNED_BYTE;
  3308. {$ELSE}
  3309. fOpenGLFormat := fUncompressed;
  3310. {$ENDIF}
  3311. end;
  3312. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3313. //TglBitmapFormatDescriptor///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3314. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3315. function TglBitmapFormatDescriptor.GetHasRed: Boolean;
  3316. begin
  3317. result := (fPrecision.r > 0);
  3318. end;
  3319. function TglBitmapFormatDescriptor.GetHasGreen: Boolean;
  3320. begin
  3321. result := (fPrecision.g > 0);
  3322. end;
  3323. function TglBitmapFormatDescriptor.GetHasBlue: Boolean;
  3324. begin
  3325. result := (fPrecision.b > 0);
  3326. end;
  3327. function TglBitmapFormatDescriptor.GetHasAlpha: Boolean;
  3328. begin
  3329. result := (fPrecision.a > 0);
  3330. end;
  3331. function TglBitmapFormatDescriptor.GetHasColor: Boolean;
  3332. begin
  3333. result := HasRed or HasGreen or HasBlue;
  3334. end;
  3335. function TglBitmapFormatDescriptor.GetIsGrayscale: Boolean;
  3336. begin
  3337. result := (Mask.r = Mask.g) and (Mask.g = Mask.b) and (Mask.r > 0);
  3338. end;
  3339. function TglBitmapFormatDescriptor.GetHasOpenGLSupport: Boolean;
  3340. begin
  3341. result := (OpenGLFormat = Format);
  3342. end;
  3343. procedure TglBitmapFormatDescriptor.SetValues;
  3344. begin
  3345. fFormat := tfEmpty;
  3346. fWithAlpha := tfEmpty;
  3347. fWithoutAlpha := tfEmpty;
  3348. fOpenGLFormat := tfEmpty;
  3349. fRGBInverted := tfEmpty;
  3350. fUncompressed := tfEmpty;
  3351. fBitsPerPixel := 0;
  3352. fIsCompressed := false;
  3353. fglFormat := 0;
  3354. fglInternalFormat := 0;
  3355. fglDataFormat := 0;
  3356. FillChar(fPrecision, 0, SizeOf(fPrecision));
  3357. FillChar(fShift, 0, SizeOf(fShift));
  3358. end;
  3359. procedure TglBitmapFormatDescriptor.CalcValues;
  3360. var
  3361. i: Integer;
  3362. begin
  3363. fBytesPerPixel := fBitsPerPixel / 8;
  3364. fChannelCount := 0;
  3365. for i := 0 to 3 do begin
  3366. if (fPrecision.arr[i] > 0) then
  3367. inc(fChannelCount);
  3368. fRange.arr[i] := (1 shl fPrecision.arr[i]) - 1;
  3369. fMask.arr[i] := fRange.arr[i] shl fShift.arr[i];
  3370. end;
  3371. end;
  3372. function TglBitmapFormatDescriptor.GetSize(const aSize: TglBitmapSize): Integer;
  3373. var
  3374. w, h: Integer;
  3375. begin
  3376. if (ffX in aSize.Fields) or (ffY in aSize.Fields) then begin
  3377. w := Max(1, aSize.X);
  3378. h := Max(1, aSize.Y);
  3379. result := GetSize(w, h);
  3380. end else
  3381. result := 0;
  3382. end;
  3383. function TglBitmapFormatDescriptor.GetSize(const aWidth, aHeight: Integer): Integer;
  3384. begin
  3385. result := 0;
  3386. if (aWidth <= 0) or (aHeight <= 0) then
  3387. exit;
  3388. result := Ceil(aWidth * aHeight * BytesPerPixel);
  3389. end;
  3390. constructor TglBitmapFormatDescriptor.Create;
  3391. begin
  3392. inherited Create;
  3393. SetValues;
  3394. CalcValues;
  3395. end;
  3396. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3397. class function TglBitmapFormatDescriptor.GetByFormat(const aInternalFormat: GLenum): TglBitmapFormatDescriptor;
  3398. var
  3399. f: TglBitmapFormat;
  3400. begin
  3401. for f := Low(TglBitmapFormat) to High(TglBitmapFormat) do begin
  3402. result := TFormatDescriptor.Get(f);
  3403. if (result.glInternalFormat = aInternalFormat) then
  3404. exit;
  3405. end;
  3406. result := TFormatDescriptor.Get(tfEmpty);
  3407. end;
  3408. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3409. //TFormatDescriptor///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3410. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3411. class procedure TFormatDescriptor.Init;
  3412. begin
  3413. if not Assigned(FormatDescriptorCS) then
  3414. FormatDescriptorCS := TCriticalSection.Create;
  3415. end;
  3416. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3417. class function TFormatDescriptor.Get(const aFormat: TglBitmapFormat): TFormatDescriptor;
  3418. begin
  3419. FormatDescriptorCS.Enter;
  3420. try
  3421. result := FormatDescriptors[aFormat];
  3422. if not Assigned(result) then begin
  3423. result := FORMAT_DESCRIPTOR_CLASSES[aFormat].Create;
  3424. FormatDescriptors[aFormat] := result;
  3425. end;
  3426. finally
  3427. FormatDescriptorCS.Leave;
  3428. end;
  3429. end;
  3430. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3431. class function TFormatDescriptor.GetAlpha(const aFormat: TglBitmapFormat): TFormatDescriptor;
  3432. begin
  3433. result := Get(Get(aFormat).WithAlpha);
  3434. end;
  3435. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3436. class function TFormatDescriptor.GetFromMask(const aMask: TglBitmapRec4ul; const aBitCount: Integer): TFormatDescriptor;
  3437. var
  3438. ft: TglBitmapFormat;
  3439. begin
  3440. // find matching format with OpenGL support
  3441. for ft := High(TglBitmapFormat) downto Low(TglBitmapFormat) do begin
  3442. result := Get(ft);
  3443. if (result.MaskMatch(aMask)) and
  3444. (result.glFormat <> 0) and
  3445. (result.glInternalFormat <> 0) and
  3446. ((aBitCount = 0) or (aBitCount = result.BitsPerPixel))
  3447. then
  3448. exit;
  3449. end;
  3450. // find matching format without OpenGL Support
  3451. for ft := High(TglBitmapFormat) downto Low(TglBitmapFormat) do begin
  3452. result := Get(ft);
  3453. if result.MaskMatch(aMask) and ((aBitCount = 0) or (aBitCount = result.BitsPerPixel)) then
  3454. exit;
  3455. end;
  3456. result := TFormatDescriptor.Get(tfEmpty);
  3457. end;
  3458. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3459. class function TFormatDescriptor.GetFromPrecShift(const aPrec, aShift: TglBitmapRec4ub; const aBitCount: Integer): TFormatDescriptor;
  3460. var
  3461. ft: TglBitmapFormat;
  3462. begin
  3463. // find matching format with OpenGL support
  3464. for ft := High(TglBitmapFormat) downto Low(TglBitmapFormat) do begin
  3465. result := Get(ft);
  3466. if glBitmapRec4ubCompare(result.Shift, aShift) and
  3467. glBitmapRec4ubCompare(result.Precision, aPrec) and
  3468. (result.glFormat <> 0) and
  3469. (result.glInternalFormat <> 0) and
  3470. ((aBitCount = 0) or (aBitCount = result.BitsPerPixel))
  3471. then
  3472. exit;
  3473. end;
  3474. // find matching format without OpenGL Support
  3475. for ft := High(TglBitmapFormat) downto Low(TglBitmapFormat) do begin
  3476. result := Get(ft);
  3477. if glBitmapRec4ubCompare(result.Shift, aShift) and
  3478. glBitmapRec4ubCompare(result.Precision, aPrec) and
  3479. ((aBitCount = 0) or (aBitCount = result.BitsPerPixel)) then
  3480. exit;
  3481. end;
  3482. result := TFormatDescriptor.Get(tfEmpty);
  3483. end;
  3484. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3485. class procedure TFormatDescriptor.Clear;
  3486. var
  3487. f: TglBitmapFormat;
  3488. begin
  3489. FormatDescriptorCS.Enter;
  3490. try
  3491. for f := low(FormatDescriptors) to high(FormatDescriptors) do
  3492. FreeAndNil(FormatDescriptors[f]);
  3493. finally
  3494. FormatDescriptorCS.Leave;
  3495. end;
  3496. end;
  3497. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3498. class procedure TFormatDescriptor.Finalize;
  3499. begin
  3500. Clear;
  3501. FreeAndNil(FormatDescriptorCS);
  3502. end;
  3503. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3504. //TBitfieldFormat/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3505. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3506. procedure TbmpBitfieldFormat.SetCustomValues(const aBPP: Integer; aMask: TglBitmapRec4ul);
  3507. var
  3508. i: Integer;
  3509. begin
  3510. for i := 0 to 3 do begin
  3511. fShift.arr[i] := 0;
  3512. while (aMask.arr[i] > 0) and ((aMask.arr[i] and 1) = 0) do begin
  3513. aMask.arr[i] := aMask.arr[i] shr 1;
  3514. inc(fShift.arr[i]);
  3515. end;
  3516. fPrecision.arr[i] := CountSetBits(aMask.arr[i]);
  3517. end;
  3518. fBitsPerPixel := aBPP;
  3519. CalcValues;
  3520. end;
  3521. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3522. procedure TbmpBitfieldFormat.SetCustomValues(const aBBP: Integer; const aPrec, aShift: TglBitmapRec4ub);
  3523. begin
  3524. fBitsPerPixel := aBBP;
  3525. fPrecision := aPrec;
  3526. fShift := aShift;
  3527. CalcValues;
  3528. end;
  3529. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3530. procedure TbmpBitfieldFormat.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  3531. var
  3532. data: QWord;
  3533. begin
  3534. data :=
  3535. ((aPixel.Data.r and Range.r) shl Shift.r) or
  3536. ((aPixel.Data.g and Range.g) shl Shift.g) or
  3537. ((aPixel.Data.b and Range.b) shl Shift.b) or
  3538. ((aPixel.Data.a and Range.a) shl Shift.a);
  3539. case BitsPerPixel of
  3540. 8: aData^ := data;
  3541. 16: PWord(aData)^ := data;
  3542. 32: PCardinal(aData)^ := data;
  3543. 64: PQWord(aData)^ := data;
  3544. else
  3545. raise EglBitmap.CreateFmt('invalid pixel size: %d', [BitsPerPixel]);
  3546. end;
  3547. inc(aData, Round(BytesPerPixel));
  3548. end;
  3549. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3550. procedure TbmpBitfieldFormat.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  3551. var
  3552. data: QWord;
  3553. i: Integer;
  3554. begin
  3555. case BitsPerPixel of
  3556. 8: data := aData^;
  3557. 16: data := PWord(aData)^;
  3558. 32: data := PCardinal(aData)^;
  3559. 64: data := PQWord(aData)^;
  3560. else
  3561. raise EglBitmap.CreateFmt('invalid pixel size: %d', [BitsPerPixel]);
  3562. end;
  3563. for i := 0 to 3 do
  3564. aPixel.Data.arr[i] := (data shr fShift.arr[i]) and Range.arr[i];
  3565. inc(aData, Round(BytesPerPixel));
  3566. end;
  3567. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3568. //TColorTableFormat///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3569. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3570. procedure TbmpColorTableFormat.SetValues;
  3571. begin
  3572. inherited SetValues;
  3573. fShift := glBitmapRec4ub(8, 8, 8, 0);
  3574. end;
  3575. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3576. procedure TbmpColorTableFormat.SetCustomValues(const aFormat: TglBitmapFormat; const aBPP: Integer; const aPrec, aShift: TglBitmapRec4ub);
  3577. begin
  3578. fFormat := aFormat;
  3579. fBitsPerPixel := aBPP;
  3580. fPrecision := aPrec;
  3581. fShift := aShift;
  3582. CalcValues;
  3583. end;
  3584. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3585. procedure TbmpColorTableFormat.CalcValues;
  3586. begin
  3587. inherited CalcValues;
  3588. end;
  3589. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3590. procedure TbmpColorTableFormat.CreateColorTable;
  3591. var
  3592. i: Integer;
  3593. begin
  3594. SetLength(fColorTable, 256);
  3595. if not HasColor then begin
  3596. // alpha
  3597. for i := 0 to High(fColorTable) do begin
  3598. fColorTable[i].r := Round(((i shr Shift.a) and Range.a) / Range.a * 255);
  3599. fColorTable[i].g := Round(((i shr Shift.a) and Range.a) / Range.a * 255);
  3600. fColorTable[i].b := Round(((i shr Shift.a) and Range.a) / Range.a * 255);
  3601. fColorTable[i].a := 0;
  3602. end;
  3603. end else begin
  3604. // normal
  3605. for i := 0 to High(fColorTable) do begin
  3606. fColorTable[i].r := Round(((i shr Shift.r) and Range.r) / Range.r * 255);
  3607. fColorTable[i].g := Round(((i shr Shift.g) and Range.g) / Range.g * 255);
  3608. fColorTable[i].b := Round(((i shr Shift.b) and Range.b) / Range.b * 255);
  3609. fColorTable[i].a := 0;
  3610. end;
  3611. end;
  3612. end;
  3613. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3614. function TbmpColorTableFormat.CreateMappingData: Pointer;
  3615. begin
  3616. result := Pointer(0);
  3617. end;
  3618. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3619. procedure TbmpColorTableFormat.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
  3620. begin
  3621. if (BitsPerPixel <> 8) then
  3622. raise EglBitmapUnsupportedFormat.Create('color table are only supported for 8bit formats');
  3623. if not HasColor then
  3624. // alpha
  3625. aData^ := aPixel.Data.a
  3626. else
  3627. // normal
  3628. aData^ := Round(
  3629. ((aPixel.Data.r shr Shift.r) and Range.r) * LUMINANCE_WEIGHT_R +
  3630. ((aPixel.Data.g shr Shift.g) and Range.g) * LUMINANCE_WEIGHT_G +
  3631. ((aPixel.Data.b shr Shift.b) and Range.b) * LUMINANCE_WEIGHT_B);
  3632. inc(aData);
  3633. end;
  3634. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3635. procedure TbmpColorTableFormat.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
  3636. function ReadValue: Byte;
  3637. var
  3638. i: PtrUInt;
  3639. begin
  3640. if (BitsPerPixel = 8) then begin
  3641. result := aData^;
  3642. inc(aData);
  3643. end else begin
  3644. i := {%H-}PtrUInt(aMapData);
  3645. if (BitsPerPixel > 1) then
  3646. result := (aData^ shr i) and ((1 shl BitsPerPixel) - 1)
  3647. else
  3648. result := (aData^ shr (7-i)) and ((1 shl BitsPerPixel) - 1);
  3649. inc(i, BitsPerPixel);
  3650. while (i >= 8) do begin
  3651. inc(aData);
  3652. dec(i, 8);
  3653. end;
  3654. aMapData := {%H-}Pointer(i);
  3655. end;
  3656. end;
  3657. begin
  3658. if (BitsPerPixel > 8) then
  3659. raise EglBitmapUnsupportedFormat.Create('color table are only supported for 8bit formats');
  3660. with fColorTable[ReadValue] do begin
  3661. aPixel.Data.r := r;
  3662. aPixel.Data.g := g;
  3663. aPixel.Data.b := b;
  3664. aPixel.Data.a := a;
  3665. end;
  3666. end;
  3667. destructor TbmpColorTableFormat.Destroy;
  3668. begin
  3669. SetLength(fColorTable, 0);
  3670. inherited Destroy;
  3671. end;
  3672. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3673. //TglBitmap - Helper//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3674. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3675. procedure glBitmapConvertPixel(var aPixel: TglBitmapPixelData; const aSourceFD, aDestFD: TFormatDescriptor);
  3676. var
  3677. i: Integer;
  3678. begin
  3679. for i := 0 to 3 do begin
  3680. if (aSourceFD.Range.arr[i] <> aDestFD.Range.arr[i]) then begin
  3681. if (aSourceFD.Range.arr[i] > 0) then
  3682. aPixel.Data.arr[i] := Round(aPixel.Data.arr[i] / aSourceFD.Range.arr[i] * aDestFD.Range.arr[i])
  3683. else
  3684. aPixel.Data.arr[i] := 0;
  3685. end;
  3686. end;
  3687. end;
  3688. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3689. procedure glBitmapConvertCopyFunc(var aFuncRec: TglBitmapFunctionRec);
  3690. begin
  3691. with aFuncRec do begin
  3692. if (Source.Range.r > 0) then
  3693. Dest.Data.r := Source.Data.r;
  3694. if (Source.Range.g > 0) then
  3695. Dest.Data.g := Source.Data.g;
  3696. if (Source.Range.b > 0) then
  3697. Dest.Data.b := Source.Data.b;
  3698. if (Source.Range.a > 0) then
  3699. Dest.Data.a := Source.Data.a;
  3700. end;
  3701. end;
  3702. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3703. procedure glBitmapConvertCalculateRGBAFunc(var aFuncRec: TglBitmapFunctionRec);
  3704. var
  3705. i: Integer;
  3706. begin
  3707. with aFuncRec do begin
  3708. for i := 0 to 3 do
  3709. if (Source.Range.arr[i] > 0) then
  3710. Dest.Data.arr[i] := Round(Dest.Range.arr[i] * Source.Data.arr[i] / Source.Range.arr[i]);
  3711. end;
  3712. end;
  3713. type
  3714. TShiftData = packed record
  3715. case Integer of
  3716. 0: (r, g, b, a: SmallInt);
  3717. 1: (arr: array[0..3] of SmallInt);
  3718. end;
  3719. PShiftData = ^TShiftData;
  3720. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3721. procedure glBitmapConvertShiftRGBAFunc(var aFuncRec: TglBitmapFunctionRec);
  3722. var
  3723. i: Integer;
  3724. begin
  3725. with aFuncRec do
  3726. for i := 0 to 3 do
  3727. if (Source.Range.arr[i] > 0) then
  3728. Dest.Data.arr[i] := Source.Data.arr[i] shr PShiftData(Args)^.arr[i];
  3729. end;
  3730. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3731. procedure glBitmapInvertFunc(var aFuncRec: TglBitmapFunctionRec);
  3732. var
  3733. i: Integer;
  3734. begin
  3735. with aFuncRec do begin
  3736. Dest.Data := Source.Data;
  3737. for i := 0 to 3 do
  3738. if ({%H-}PtrUInt(Args) and (1 shl i) > 0) then
  3739. Dest.Data.arr[i] := Dest.Data.arr[i] xor Dest.Range.arr[i];
  3740. end;
  3741. end;
  3742. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3743. procedure glBitmapFillWithColorFunc(var aFuncRec: TglBitmapFunctionRec);
  3744. var
  3745. i: Integer;
  3746. begin
  3747. with aFuncRec do begin
  3748. for i := 0 to 3 do
  3749. Dest.Data.arr[i] := PglBitmapPixelData(Args)^.Data.arr[i];
  3750. end;
  3751. end;
  3752. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3753. procedure glBitmapAlphaFunc(var FuncRec: TglBitmapFunctionRec);
  3754. var
  3755. Temp: Single;
  3756. begin
  3757. with FuncRec do begin
  3758. if (FuncRec.Args = nil) then begin //source has no alpha
  3759. Temp :=
  3760. Source.Data.r / Source.Range.r * ALPHA_WEIGHT_R +
  3761. Source.Data.g / Source.Range.g * ALPHA_WEIGHT_G +
  3762. Source.Data.b / Source.Range.b * ALPHA_WEIGHT_B;
  3763. Dest.Data.a := Round(Dest.Range.a * Temp);
  3764. end else
  3765. Dest.Data.a := Round(Source.Data.a / Source.Range.a * Dest.Range.a);
  3766. end;
  3767. end;
  3768. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3769. procedure glBitmapColorKeyAlphaFunc(var FuncRec: TglBitmapFunctionRec);
  3770. type
  3771. PglBitmapPixelData = ^TglBitmapPixelData;
  3772. begin
  3773. with FuncRec do begin
  3774. Dest.Data.r := Source.Data.r;
  3775. Dest.Data.g := Source.Data.g;
  3776. Dest.Data.b := Source.Data.b;
  3777. with PglBitmapPixelData(Args)^ do
  3778. if ((Dest.Data.r <= Data.r) and (Dest.Data.r >= Range.r) and
  3779. (Dest.Data.g <= Data.g) and (Dest.Data.g >= Range.g) and
  3780. (Dest.Data.b <= Data.b) and (Dest.Data.b >= Range.b)) then
  3781. Dest.Data.a := 0
  3782. else
  3783. Dest.Data.a := Dest.Range.a;
  3784. end;
  3785. end;
  3786. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3787. procedure glBitmapValueAlphaFunc(var FuncRec: TglBitmapFunctionRec);
  3788. begin
  3789. with FuncRec do begin
  3790. Dest.Data.r := Source.Data.r;
  3791. Dest.Data.g := Source.Data.g;
  3792. Dest.Data.b := Source.Data.b;
  3793. Dest.Data.a := PCardinal(Args)^;
  3794. end;
  3795. end;
  3796. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3797. procedure SwapRGB(aData: PByte; aWidth: Integer; const aHasAlpha: Boolean);
  3798. type
  3799. PRGBPix = ^TRGBPix;
  3800. TRGBPix = array [0..2] of byte;
  3801. var
  3802. Temp: Byte;
  3803. begin
  3804. while aWidth > 0 do begin
  3805. Temp := PRGBPix(aData)^[0];
  3806. PRGBPix(aData)^[0] := PRGBPix(aData)^[2];
  3807. PRGBPix(aData)^[2] := Temp;
  3808. if aHasAlpha then
  3809. Inc(aData, 4)
  3810. else
  3811. Inc(aData, 3);
  3812. dec(aWidth);
  3813. end;
  3814. end;
  3815. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3816. //TglBitmapData///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3817. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3818. function TglBitmapData.GetFormatDescriptor: TglBitmapFormatDescriptor;
  3819. begin
  3820. result := TFormatDescriptor.Get(fFormat);
  3821. end;
  3822. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3823. function TglBitmapData.GetWidth: Integer;
  3824. begin
  3825. if (ffX in fDimension.Fields) then
  3826. result := fDimension.X
  3827. else
  3828. result := -1;
  3829. end;
  3830. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3831. function TglBitmapData.GetHeight: Integer;
  3832. begin
  3833. if (ffY in fDimension.Fields) then
  3834. result := fDimension.Y
  3835. else
  3836. result := -1;
  3837. end;
  3838. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3839. function TglBitmapData.GetScanlines(const aIndex: Integer): PByte;
  3840. begin
  3841. if fHasScanlines and (aIndex >= Low(fScanlines)) and (aIndex <= High(fScanlines)) then
  3842. result := fScanlines[aIndex]
  3843. else
  3844. result := nil;
  3845. end;
  3846. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3847. procedure TglBitmapData.SetFormat(const aValue: TglBitmapFormat);
  3848. begin
  3849. if fFormat = aValue then
  3850. exit;
  3851. if TFormatDescriptor.Get(Format).BitsPerPixel <> TFormatDescriptor.Get(aValue).BitsPerPixel then
  3852. raise EglBitmapUnsupportedFormat.Create(Format);
  3853. SetData(fData, aValue, Width, Height);
  3854. end;
  3855. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3856. procedure TglBitmapData.PrepareResType(var aResource: String; var aResType: PChar);
  3857. var
  3858. TempPos: Integer;
  3859. begin
  3860. if not Assigned(aResType) then begin
  3861. TempPos := Pos('.', aResource);
  3862. aResType := PChar(UpperCase(Copy(aResource, TempPos + 1, Length(aResource) - TempPos)));
  3863. aResource := UpperCase(Copy(aResource, 0, TempPos -1));
  3864. end;
  3865. end;
  3866. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3867. procedure TglBitmapData.UpdateScanlines;
  3868. var
  3869. w, h, i, LineWidth: Integer;
  3870. begin
  3871. w := Width;
  3872. h := Height;
  3873. fHasScanlines := Assigned(fData) and (w > 0) and (h > 0);
  3874. if fHasScanlines then begin
  3875. SetLength(fScanlines, h);
  3876. LineWidth := Trunc(w * FormatDescriptor.BytesPerPixel);
  3877. for i := 0 to h-1 do begin
  3878. fScanlines[i] := fData;
  3879. Inc(fScanlines[i], i * LineWidth);
  3880. end;
  3881. end else
  3882. SetLength(fScanlines, 0);
  3883. end;
  3884. {$IFDEF GLB_SUPPORT_PNG_READ}
  3885. {$IF DEFINED(GLB_LAZ_PNG)}
  3886. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3887. //PNG/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3888. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3889. function TglBitmapData.LoadPNG(const aStream: TStream): Boolean;
  3890. const
  3891. MAGIC_LEN = 8;
  3892. PNG_MAGIC: String[MAGIC_LEN] = #$89#$50#$4E#$47#$0D#$0A#$1A#$0A;
  3893. var
  3894. reader: TLazReaderPNG;
  3895. intf: TLazIntfImage;
  3896. StreamPos: Int64;
  3897. magic: String[MAGIC_LEN];
  3898. begin
  3899. result := true;
  3900. StreamPos := aStream.Position;
  3901. SetLength(magic, MAGIC_LEN);
  3902. aStream.Read(magic[1], MAGIC_LEN);
  3903. aStream.Position := StreamPos;
  3904. if (magic <> PNG_MAGIC) then begin
  3905. result := false;
  3906. exit;
  3907. end;
  3908. intf := TLazIntfImage.Create(0, 0);
  3909. reader := TLazReaderPNG.Create;
  3910. try try
  3911. reader.UpdateDescription := true;
  3912. reader.ImageRead(aStream, intf);
  3913. AssignFromLazIntfImage(intf);
  3914. except
  3915. result := false;
  3916. aStream.Position := StreamPos;
  3917. exit;
  3918. end;
  3919. finally
  3920. reader.Free;
  3921. intf.Free;
  3922. end;
  3923. end;
  3924. {$ELSEIF DEFINED(GLB_SDL_IMAGE)}
  3925. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3926. function TglBitmapData.LoadPNG(const aStream: TStream): Boolean;
  3927. var
  3928. Surface: PSDL_Surface;
  3929. RWops: PSDL_RWops;
  3930. begin
  3931. result := false;
  3932. RWops := glBitmapCreateRWops(aStream);
  3933. try
  3934. if IMG_isPNG(RWops) > 0 then begin
  3935. Surface := IMG_LoadPNG_RW(RWops);
  3936. try
  3937. AssignFromSurface(Surface);
  3938. result := true;
  3939. finally
  3940. SDL_FreeSurface(Surface);
  3941. end;
  3942. end;
  3943. finally
  3944. SDL_FreeRW(RWops);
  3945. end;
  3946. end;
  3947. {$ELSEIF DEFINED(GLB_LIB_PNG)}
  3948. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3949. procedure glBitmap_libPNG_read_func(png: png_structp; buffer: png_bytep; size: cardinal); cdecl;
  3950. begin
  3951. TStream(png_get_io_ptr(png)).Read(buffer^, size);
  3952. end;
  3953. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3954. function TglBitmapData.LoadPNG(const aStream: TStream): Boolean;
  3955. var
  3956. StreamPos: Int64;
  3957. signature: array [0..7] of byte;
  3958. png: png_structp;
  3959. png_info: png_infop;
  3960. TempHeight, TempWidth: Integer;
  3961. Format: TglBitmapFormat;
  3962. png_data: pByte;
  3963. png_rows: array of pByte;
  3964. Row, LineSize: Integer;
  3965. begin
  3966. result := false;
  3967. if not init_libPNG then
  3968. raise Exception.Create('LoadPNG - unable to initialize libPNG.');
  3969. try
  3970. // signature
  3971. StreamPos := aStream.Position;
  3972. aStream.Read(signature{%H-}, 8);
  3973. aStream.Position := StreamPos;
  3974. if png_check_sig(@signature, 8) <> 0 then begin
  3975. // png read struct
  3976. png := png_create_read_struct(PNG_LIBPNG_VER_STRING, nil, nil, nil);
  3977. if png = nil then
  3978. raise EglBitmapException.Create('LoadPng - couldn''t create read struct.');
  3979. // png info
  3980. png_info := png_create_info_struct(png);
  3981. if png_info = nil then begin
  3982. png_destroy_read_struct(@png, nil, nil);
  3983. raise EglBitmapException.Create('LoadPng - couldn''t create info struct.');
  3984. end;
  3985. // set read callback
  3986. png_set_read_fn(png, aStream, glBitmap_libPNG_read_func);
  3987. // read informations
  3988. png_read_info(png, png_info);
  3989. // size
  3990. TempHeight := png_get_image_height(png, png_info);
  3991. TempWidth := png_get_image_width(png, png_info);
  3992. // format
  3993. case png_get_color_type(png, png_info) of
  3994. PNG_COLOR_TYPE_GRAY:
  3995. Format := tfLuminance8ub1;
  3996. PNG_COLOR_TYPE_GRAY_ALPHA:
  3997. Format := tfLuminance8Alpha8us1;
  3998. PNG_COLOR_TYPE_RGB:
  3999. Format := tfRGB8ub3;
  4000. PNG_COLOR_TYPE_RGB_ALPHA:
  4001. Format := tfRGBA8ub4;
  4002. else
  4003. raise EglBitmapException.Create ('LoadPng - Unsupported Colortype found.');
  4004. end;
  4005. // cut upper 8 bit from 16 bit formats
  4006. if png_get_bit_depth(png, png_info) > 8 then
  4007. png_set_strip_16(png);
  4008. // expand bitdepth smaller than 8
  4009. if png_get_bit_depth(png, png_info) < 8 then
  4010. png_set_expand(png);
  4011. // allocating mem for scanlines
  4012. LineSize := png_get_rowbytes(png, png_info);
  4013. GetMem(png_data, TempHeight * LineSize);
  4014. try
  4015. SetLength(png_rows, TempHeight);
  4016. for Row := Low(png_rows) to High(png_rows) do begin
  4017. png_rows[Row] := png_data;
  4018. Inc(png_rows[Row], Row * LineSize);
  4019. end;
  4020. // read complete image into scanlines
  4021. png_read_image(png, @png_rows[0]);
  4022. // read end
  4023. png_read_end(png, png_info);
  4024. // destroy read struct
  4025. png_destroy_read_struct(@png, @png_info, nil);
  4026. SetLength(png_rows, 0);
  4027. // set new data
  4028. SetData(png_data, Format, TempWidth, TempHeight);
  4029. result := true;
  4030. except
  4031. if Assigned(png_data) then
  4032. FreeMem(png_data);
  4033. raise;
  4034. end;
  4035. end;
  4036. finally
  4037. quit_libPNG;
  4038. end;
  4039. end;
  4040. {$ELSEIF DEFINED(GLB_PNGIMAGE)}
  4041. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4042. function TglBitmapData.LoadPNG(const aStream: TStream): Boolean;
  4043. var
  4044. StreamPos: Int64;
  4045. Png: TPNGObject;
  4046. Header: String[8];
  4047. Row, Col, PixSize, LineSize: Integer;
  4048. NewImage, pSource, pDest, pAlpha: pByte;
  4049. PngFormat: TglBitmapFormat;
  4050. FormatDesc: TFormatDescriptor;
  4051. const
  4052. PngHeader: String[8] = #137#80#78#71#13#10#26#10;
  4053. begin
  4054. result := false;
  4055. StreamPos := aStream.Position;
  4056. aStream.Read(Header[0], SizeOf(Header));
  4057. aStream.Position := StreamPos;
  4058. {Test if the header matches}
  4059. if Header = PngHeader then begin
  4060. Png := TPNGObject.Create;
  4061. try
  4062. Png.LoadFromStream(aStream);
  4063. case Png.Header.ColorType of
  4064. COLOR_GRAYSCALE:
  4065. PngFormat := tfLuminance8ub1;
  4066. COLOR_GRAYSCALEALPHA:
  4067. PngFormat := tfLuminance8Alpha8us1;
  4068. COLOR_RGB:
  4069. PngFormat := tfBGR8ub3;
  4070. COLOR_RGBALPHA:
  4071. PngFormat := tfBGRA8ub4;
  4072. else
  4073. raise EglBitmapException.Create ('LoadPng - Unsupported Colortype found.');
  4074. end;
  4075. FormatDesc := TFormatDescriptor.Get(PngFormat);
  4076. PixSize := Round(FormatDesc.PixelSize);
  4077. LineSize := FormatDesc.GetSize(Png.Header.Width, 1);
  4078. GetMem(NewImage, LineSize * Integer(Png.Header.Height));
  4079. try
  4080. pDest := NewImage;
  4081. case Png.Header.ColorType of
  4082. COLOR_RGB, COLOR_GRAYSCALE:
  4083. begin
  4084. for Row := 0 to Png.Height -1 do begin
  4085. Move (Png.Scanline[Row]^, pDest^, LineSize);
  4086. Inc(pDest, LineSize);
  4087. end;
  4088. end;
  4089. COLOR_RGBALPHA, COLOR_GRAYSCALEALPHA:
  4090. begin
  4091. PixSize := PixSize -1;
  4092. for Row := 0 to Png.Height -1 do begin
  4093. pSource := Png.Scanline[Row];
  4094. pAlpha := pByte(Png.AlphaScanline[Row]);
  4095. for Col := 0 to Png.Width -1 do begin
  4096. Move (pSource^, pDest^, PixSize);
  4097. Inc(pSource, PixSize);
  4098. Inc(pDest, PixSize);
  4099. pDest^ := pAlpha^;
  4100. inc(pAlpha);
  4101. Inc(pDest);
  4102. end;
  4103. end;
  4104. end;
  4105. else
  4106. raise EglBitmapException.Create ('LoadPng - Unsupported Colortype found.');
  4107. end;
  4108. SetData(NewImage, PngFormat, Png.Header.Width, Png.Header.Height);
  4109. result := true;
  4110. except
  4111. if Assigned(NewImage) then
  4112. FreeMem(NewImage);
  4113. raise;
  4114. end;
  4115. finally
  4116. Png.Free;
  4117. end;
  4118. end;
  4119. end;
  4120. {$IFEND}
  4121. {$ENDIF}
  4122. {$IFDEF GLB_SUPPORT_PNG_WRITE}
  4123. {$IFDEF GLB_LIB_PNG}
  4124. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4125. procedure glBitmap_libPNG_write_func(png: png_structp; buffer: png_bytep; size: cardinal); cdecl;
  4126. begin
  4127. TStream(png_get_io_ptr(png)).Write(buffer^, size);
  4128. end;
  4129. {$ENDIF}
  4130. {$IF DEFINED(GLB_LAZ_PNG)}
  4131. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4132. procedure TglBitmapData.SavePNG(const aStream: TStream);
  4133. var
  4134. png: TPortableNetworkGraphic;
  4135. intf: TLazIntfImage;
  4136. raw: TRawImage;
  4137. begin
  4138. png := TPortableNetworkGraphic.Create;
  4139. intf := TLazIntfImage.Create(0, 0);
  4140. try
  4141. if not AssignToLazIntfImage(intf) then
  4142. raise EglBitmap.Create('unable to create LazIntfImage from glBitmap');
  4143. intf.GetRawImage(raw);
  4144. png.LoadFromRawImage(raw, false);
  4145. png.SaveToStream(aStream);
  4146. finally
  4147. png.Free;
  4148. intf.Free;
  4149. end;
  4150. end;
  4151. {$ELSEIF DEFINED(GLB_LIB_PNG)}
  4152. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4153. procedure TglBitmapData.SavePNG(const aStream: TStream);
  4154. var
  4155. png: png_structp;
  4156. png_info: png_infop;
  4157. png_rows: array of pByte;
  4158. LineSize: Integer;
  4159. ColorType: Integer;
  4160. Row: Integer;
  4161. FormatDesc: TFormatDescriptor;
  4162. begin
  4163. if not (ftPNG in FormatGetSupportedFiles(Format)) then
  4164. raise EglBitmapUnsupportedFormat.Create(Format);
  4165. if not init_libPNG then
  4166. raise Exception.Create('unable to initialize libPNG.');
  4167. try
  4168. case Format of
  4169. tfAlpha8ub1, tfLuminance8ub1:
  4170. ColorType := PNG_COLOR_TYPE_GRAY;
  4171. tfLuminance8Alpha8us1:
  4172. ColorType := PNG_COLOR_TYPE_GRAY_ALPHA;
  4173. tfBGR8ub3, tfRGB8ub3:
  4174. ColorType := PNG_COLOR_TYPE_RGB;
  4175. tfBGRA8ub4, tfRGBA8ub4:
  4176. ColorType := PNG_COLOR_TYPE_RGBA;
  4177. else
  4178. raise EglBitmapUnsupportedFormat.Create(Format);
  4179. end;
  4180. FormatDesc := TFormatDescriptor.Get(Format);
  4181. LineSize := FormatDesc.GetSize(Width, 1);
  4182. // creating array for scanline
  4183. SetLength(png_rows, Height);
  4184. try
  4185. for Row := 0 to Height - 1 do begin
  4186. png_rows[Row] := Data;
  4187. Inc(png_rows[Row], Row * LineSize)
  4188. end;
  4189. // write struct
  4190. png := png_create_write_struct(PNG_LIBPNG_VER_STRING, nil, nil, nil);
  4191. if png = nil then
  4192. raise EglBitmapException.Create('SavePng - couldn''t create write struct.');
  4193. // create png info
  4194. png_info := png_create_info_struct(png);
  4195. if png_info = nil then begin
  4196. png_destroy_write_struct(@png, nil);
  4197. raise EglBitmapException.Create('SavePng - couldn''t create info struct.');
  4198. end;
  4199. // set read callback
  4200. png_set_write_fn(png, aStream, glBitmap_libPNG_write_func, nil);
  4201. // set compression
  4202. png_set_compression_level(png, 6);
  4203. if Format in [tfBGR8ub3, tfBGRA8ub4] then
  4204. png_set_bgr(png);
  4205. png_set_IHDR(png, png_info, Width, Height, 8, ColorType, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
  4206. png_write_info(png, png_info);
  4207. png_write_image(png, @png_rows[0]);
  4208. png_write_end(png, png_info);
  4209. png_destroy_write_struct(@png, @png_info);
  4210. finally
  4211. SetLength(png_rows, 0);
  4212. end;
  4213. finally
  4214. quit_libPNG;
  4215. end;
  4216. end;
  4217. {$ELSEIF DEFINED(GLB_PNGIMAGE)}
  4218. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4219. procedure TglBitmapData.SavePNG(const aStream: TStream);
  4220. var
  4221. Png: TPNGObject;
  4222. pSource, pDest: pByte;
  4223. X, Y, PixSize: Integer;
  4224. ColorType: Cardinal;
  4225. Alpha: Boolean;
  4226. pTemp: pByte;
  4227. Temp: Byte;
  4228. begin
  4229. if not (ftPNG in FormatGetSupportedFiles (Format)) then
  4230. raise EglBitmapUnsupportedFormat.Create(Format);
  4231. case Format of
  4232. tfAlpha8ub1, tfLuminance8ub1: begin
  4233. ColorType := COLOR_GRAYSCALE;
  4234. PixSize := 1;
  4235. Alpha := false;
  4236. end;
  4237. tfLuminance8Alpha8us1: begin
  4238. ColorType := COLOR_GRAYSCALEALPHA;
  4239. PixSize := 1;
  4240. Alpha := true;
  4241. end;
  4242. tfBGR8ub3, tfRGB8ub3: begin
  4243. ColorType := COLOR_RGB;
  4244. PixSize := 3;
  4245. Alpha := false;
  4246. end;
  4247. tfBGRA8ub4, tfRGBA8ub4: begin
  4248. ColorType := COLOR_RGBALPHA;
  4249. PixSize := 3;
  4250. Alpha := true
  4251. end;
  4252. else
  4253. raise EglBitmapUnsupportedFormat.Create(Format);
  4254. end;
  4255. Png := TPNGObject.CreateBlank(ColorType, 8, Width, Height);
  4256. try
  4257. // Copy ImageData
  4258. pSource := Data;
  4259. for Y := 0 to Height -1 do begin
  4260. pDest := png.ScanLine[Y];
  4261. for X := 0 to Width -1 do begin
  4262. Move(pSource^, pDest^, PixSize);
  4263. Inc(pDest, PixSize);
  4264. Inc(pSource, PixSize);
  4265. if Alpha then begin
  4266. png.AlphaScanline[Y]^[X] := pSource^;
  4267. Inc(pSource);
  4268. end;
  4269. end;
  4270. // convert RGB line to BGR
  4271. if Format in [tfRGB8ub3, tfRGBA8ub4] then begin
  4272. pTemp := png.ScanLine[Y];
  4273. for X := 0 to Width -1 do begin
  4274. Temp := pByteArray(pTemp)^[0];
  4275. pByteArray(pTemp)^[0] := pByteArray(pTemp)^[2];
  4276. pByteArray(pTemp)^[2] := Temp;
  4277. Inc(pTemp, 3);
  4278. end;
  4279. end;
  4280. end;
  4281. // Save to Stream
  4282. Png.CompressionLevel := 6;
  4283. Png.SaveToStream(aStream);
  4284. finally
  4285. FreeAndNil(Png);
  4286. end;
  4287. end;
  4288. {$IFEND}
  4289. {$ENDIF}
  4290. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4291. //JPEG////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4292. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4293. {$IFDEF GLB_LIB_JPEG}
  4294. type
  4295. glBitmap_libJPEG_source_mgr_ptr = ^glBitmap_libJPEG_source_mgr;
  4296. glBitmap_libJPEG_source_mgr = record
  4297. pub: jpeg_source_mgr;
  4298. SrcStream: TStream;
  4299. SrcBuffer: array [1..4096] of byte;
  4300. end;
  4301. glBitmap_libJPEG_dest_mgr_ptr = ^glBitmap_libJPEG_dest_mgr;
  4302. glBitmap_libJPEG_dest_mgr = record
  4303. pub: jpeg_destination_mgr;
  4304. DestStream: TStream;
  4305. DestBuffer: array [1..4096] of byte;
  4306. end;
  4307. procedure glBitmap_libJPEG_error_exit(cinfo: j_common_ptr); cdecl;
  4308. begin
  4309. //DUMMY
  4310. end;
  4311. procedure glBitmap_libJPEG_output_message(cinfo: j_common_ptr); cdecl;
  4312. begin
  4313. //DUMMY
  4314. end;
  4315. procedure glBitmap_libJPEG_init_source(cinfo: j_decompress_ptr); cdecl;
  4316. begin
  4317. //DUMMY
  4318. end;
  4319. procedure glBitmap_libJPEG_term_source(cinfo: j_decompress_ptr); cdecl;
  4320. begin
  4321. //DUMMY
  4322. end;
  4323. procedure glBitmap_libJPEG_init_destination(cinfo: j_compress_ptr); cdecl;
  4324. begin
  4325. //DUMMY
  4326. end;
  4327. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4328. function glBitmap_libJPEG_fill_input_buffer(cinfo: j_decompress_ptr): boolean; cdecl;
  4329. var
  4330. src: glBitmap_libJPEG_source_mgr_ptr;
  4331. bytes: integer;
  4332. begin
  4333. src := glBitmap_libJPEG_source_mgr_ptr(cinfo^.src);
  4334. bytes := src^.SrcStream.Read(src^.SrcBuffer[1], 4096);
  4335. if (bytes <= 0) then begin
  4336. src^.SrcBuffer[1] := $FF;
  4337. src^.SrcBuffer[2] := JPEG_EOI;
  4338. bytes := 2;
  4339. end;
  4340. src^.pub.next_input_byte := @(src^.SrcBuffer[1]);
  4341. src^.pub.bytes_in_buffer := bytes;
  4342. result := true;
  4343. end;
  4344. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4345. procedure glBitmap_libJPEG_skip_input_data(cinfo: j_decompress_ptr; num_bytes: Longint); cdecl;
  4346. var
  4347. src: glBitmap_libJPEG_source_mgr_ptr;
  4348. begin
  4349. src := glBitmap_libJPEG_source_mgr_ptr(cinfo^.src);
  4350. if num_bytes > 0 then begin
  4351. // wanted byte isn't in buffer so set stream position and read buffer
  4352. if num_bytes > src^.pub.bytes_in_buffer then begin
  4353. src^.SrcStream.Position := src^.SrcStream.Position + num_bytes - src^.pub.bytes_in_buffer;
  4354. src^.pub.fill_input_buffer(cinfo);
  4355. end else begin
  4356. // wanted byte is in buffer so only skip
  4357. inc(src^.pub.next_input_byte, num_bytes);
  4358. dec(src^.pub.bytes_in_buffer, num_bytes);
  4359. end;
  4360. end;
  4361. end;
  4362. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4363. function glBitmap_libJPEG_empty_output_buffer(cinfo: j_compress_ptr): boolean; cdecl;
  4364. var
  4365. dest: glBitmap_libJPEG_dest_mgr_ptr;
  4366. begin
  4367. dest := glBitmap_libJPEG_dest_mgr_ptr(cinfo^.dest);
  4368. if dest^.pub.free_in_buffer < Cardinal(Length(dest^.DestBuffer)) then begin
  4369. // write complete buffer
  4370. dest^.DestStream.Write(dest^.DestBuffer[1], SizeOf(dest^.DestBuffer));
  4371. // reset buffer
  4372. dest^.pub.next_output_byte := @dest^.DestBuffer[1];
  4373. dest^.pub.free_in_buffer := Length(dest^.DestBuffer);
  4374. end;
  4375. result := true;
  4376. end;
  4377. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4378. procedure glBitmap_libJPEG_term_destination(cinfo: j_compress_ptr); cdecl;
  4379. var
  4380. Idx: Integer;
  4381. dest: glBitmap_libJPEG_dest_mgr_ptr;
  4382. begin
  4383. dest := glBitmap_libJPEG_dest_mgr_ptr(cinfo^.dest);
  4384. for Idx := Low(dest^.DestBuffer) to High(dest^.DestBuffer) do begin
  4385. // check for endblock
  4386. if (Idx < High(dest^.DestBuffer)) and (dest^.DestBuffer[Idx] = $FF) and (dest^.DestBuffer[Idx +1] = JPEG_EOI) then begin
  4387. // write endblock
  4388. dest^.DestStream.Write(dest^.DestBuffer[Idx], 2);
  4389. // leave
  4390. break;
  4391. end else
  4392. dest^.DestStream.Write(dest^.DestBuffer[Idx], 1);
  4393. end;
  4394. end;
  4395. {$ENDIF}
  4396. {$IFDEF GLB_SUPPORT_JPEG_READ}
  4397. {$IF DEFINED(GLB_LAZ_JPEG)}
  4398. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4399. function TglBitmapData.LoadJPEG(const aStream: TStream): Boolean;
  4400. const
  4401. MAGIC_LEN = 2;
  4402. JPEG_MAGIC: String[MAGIC_LEN] = #$FF#$D8;
  4403. var
  4404. intf: TLazIntfImage;
  4405. reader: TFPReaderJPEG;
  4406. StreamPos: Int64;
  4407. magic: String[MAGIC_LEN];
  4408. begin
  4409. result := true;
  4410. StreamPos := aStream.Position;
  4411. SetLength(magic, MAGIC_LEN);
  4412. aStream.Read(magic[1], MAGIC_LEN);
  4413. aStream.Position := StreamPos;
  4414. if (magic <> JPEG_MAGIC) then begin
  4415. result := false;
  4416. exit;
  4417. end;
  4418. reader := TFPReaderJPEG.Create;
  4419. intf := TLazIntfImage.Create(0, 0);
  4420. try try
  4421. intf.DataDescription := GetDescriptionFromDevice(0, 0, 0);
  4422. reader.ImageRead(aStream, intf);
  4423. AssignFromLazIntfImage(intf);
  4424. except
  4425. result := false;
  4426. aStream.Position := StreamPos;
  4427. exit;
  4428. end;
  4429. finally
  4430. reader.Free;
  4431. intf.Free;
  4432. end;
  4433. end;
  4434. {$ELSEIF DEFINED(GLB_SDL_IMAGE)}
  4435. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4436. function TglBitmapData.LoadJPEG(const aStream: TStream): Boolean;
  4437. var
  4438. Surface: PSDL_Surface;
  4439. RWops: PSDL_RWops;
  4440. begin
  4441. result := false;
  4442. RWops := glBitmapCreateRWops(aStream);
  4443. try
  4444. if IMG_isJPG(RWops) > 0 then begin
  4445. Surface := IMG_LoadJPG_RW(RWops);
  4446. try
  4447. AssignFromSurface(Surface);
  4448. result := true;
  4449. finally
  4450. SDL_FreeSurface(Surface);
  4451. end;
  4452. end;
  4453. finally
  4454. SDL_FreeRW(RWops);
  4455. end;
  4456. end;
  4457. {$ELSEIF DEFINED(GLB_LIB_JPEG)}
  4458. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4459. function TglBitmapData.LoadJPEG(const aStream: TStream): Boolean;
  4460. var
  4461. StreamPos: Int64;
  4462. Temp: array[0..1]of Byte;
  4463. jpeg: jpeg_decompress_struct;
  4464. jpeg_err: jpeg_error_mgr;
  4465. IntFormat: TglBitmapFormat;
  4466. pImage: pByte;
  4467. TempHeight, TempWidth: Integer;
  4468. pTemp: pByte;
  4469. Row: Integer;
  4470. FormatDesc: TFormatDescriptor;
  4471. begin
  4472. result := false;
  4473. if not init_libJPEG then
  4474. raise Exception.Create('LoadJPG - unable to initialize libJPEG.');
  4475. try
  4476. // reading first two bytes to test file and set cursor back to begin
  4477. StreamPos := aStream.Position;
  4478. aStream.Read({%H-}Temp[0], 2);
  4479. aStream.Position := StreamPos;
  4480. // if Bitmap then read file.
  4481. if ((Temp[0] = $FF) and (Temp[1] = $D8)) then begin
  4482. FillChar(jpeg{%H-}, SizeOf(jpeg_decompress_struct), $00);
  4483. FillChar(jpeg_err{%H-}, SizeOf(jpeg_error_mgr), $00);
  4484. // error managment
  4485. jpeg.err := jpeg_std_error(@jpeg_err);
  4486. jpeg_err.error_exit := glBitmap_libJPEG_error_exit;
  4487. jpeg_err.output_message := glBitmap_libJPEG_output_message;
  4488. // decompression struct
  4489. jpeg_create_decompress(@jpeg);
  4490. // allocation space for streaming methods
  4491. jpeg.src := jpeg.mem^.alloc_small(@jpeg, JPOOL_PERMANENT, SizeOf(glBitmap_libJPEG_source_mgr));
  4492. // seeting up custom functions
  4493. with glBitmap_libJPEG_source_mgr_ptr(jpeg.src)^ do begin
  4494. pub.init_source := glBitmap_libJPEG_init_source;
  4495. pub.fill_input_buffer := glBitmap_libJPEG_fill_input_buffer;
  4496. pub.skip_input_data := glBitmap_libJPEG_skip_input_data;
  4497. pub.resync_to_restart := jpeg_resync_to_restart; // use default method
  4498. pub.term_source := glBitmap_libJPEG_term_source;
  4499. pub.bytes_in_buffer := 0; // forces fill_input_buffer on first read
  4500. pub.next_input_byte := nil; // until buffer loaded
  4501. SrcStream := aStream;
  4502. end;
  4503. // set global decoding state
  4504. jpeg.global_state := DSTATE_START;
  4505. // read header of jpeg
  4506. jpeg_read_header(@jpeg, false);
  4507. // setting output parameter
  4508. case jpeg.jpeg_color_space of
  4509. JCS_GRAYSCALE:
  4510. begin
  4511. jpeg.out_color_space := JCS_GRAYSCALE;
  4512. IntFormat := tfLuminance8ub1;
  4513. end;
  4514. else
  4515. jpeg.out_color_space := JCS_RGB;
  4516. IntFormat := tfRGB8ub3;
  4517. end;
  4518. // reading image
  4519. jpeg_start_decompress(@jpeg);
  4520. TempHeight := jpeg.output_height;
  4521. TempWidth := jpeg.output_width;
  4522. FormatDesc := TFormatDescriptor.Get(IntFormat);
  4523. // creating new image
  4524. GetMem(pImage, FormatDesc.GetSize(TempWidth, TempHeight));
  4525. try
  4526. pTemp := pImage;
  4527. for Row := 0 to TempHeight -1 do begin
  4528. jpeg_read_scanlines(@jpeg, @pTemp, 1);
  4529. Inc(pTemp, FormatDesc.GetSize(TempWidth, 1));
  4530. end;
  4531. // finish decompression
  4532. jpeg_finish_decompress(@jpeg);
  4533. // destroy decompression
  4534. jpeg_destroy_decompress(@jpeg);
  4535. SetData(pImage, IntFormat, TempWidth, TempHeight);
  4536. result := true;
  4537. except
  4538. if Assigned(pImage) then
  4539. FreeMem(pImage);
  4540. raise;
  4541. end;
  4542. end;
  4543. finally
  4544. quit_libJPEG;
  4545. end;
  4546. end;
  4547. {$ELSEIF DEFINED(GLB_DELPHI_JPEG)}
  4548. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4549. function TglBitmapData.LoadJPEG(const aStream: TStream): Boolean;
  4550. var
  4551. bmp: TBitmap;
  4552. jpg: TJPEGImage;
  4553. StreamPos: Int64;
  4554. Temp: array[0..1]of Byte;
  4555. begin
  4556. result := false;
  4557. // reading first two bytes to test file and set cursor back to begin
  4558. StreamPos := aStream.Position;
  4559. aStream.Read(Temp[0], 2);
  4560. aStream.Position := StreamPos;
  4561. // if Bitmap then read file.
  4562. if ((Temp[0] = $FF) and (Temp[1] = $D8)) then begin
  4563. bmp := TBitmap.Create;
  4564. try
  4565. jpg := TJPEGImage.Create;
  4566. try
  4567. jpg.LoadFromStream(aStream);
  4568. bmp.Assign(jpg);
  4569. result := AssignFromBitmap(bmp);
  4570. finally
  4571. jpg.Free;
  4572. end;
  4573. finally
  4574. bmp.Free;
  4575. end;
  4576. end;
  4577. end;
  4578. {$IFEND}
  4579. {$ENDIF}
  4580. {$IFDEF GLB_SUPPORT_JPEG_WRITE}
  4581. {$IF DEFINED(GLB_LAZ_JPEG)}
  4582. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4583. procedure TglBitmapData.SaveJPEG(const aStream: TStream);
  4584. var
  4585. jpeg: TJPEGImage;
  4586. intf: TLazIntfImage;
  4587. raw: TRawImage;
  4588. begin
  4589. jpeg := TJPEGImage.Create;
  4590. intf := TLazIntfImage.Create(0, 0);
  4591. try
  4592. if not AssignToLazIntfImage(intf) then
  4593. raise EglBitmap.Create('unable to create LazIntfImage from glBitmap');
  4594. intf.GetRawImage(raw);
  4595. jpeg.LoadFromRawImage(raw, false);
  4596. jpeg.SaveToStream(aStream);
  4597. finally
  4598. intf.Free;
  4599. jpeg.Free;
  4600. end;
  4601. end;
  4602. {$ELSEIF DEFINED(GLB_LIB_JPEG)}
  4603. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4604. procedure TglBitmapData.SaveJPEG(const aStream: TStream);
  4605. var
  4606. jpeg: jpeg_compress_struct;
  4607. jpeg_err: jpeg_error_mgr;
  4608. Row: Integer;
  4609. pTemp, pTemp2: pByte;
  4610. procedure CopyRow(pDest, pSource: pByte);
  4611. var
  4612. X: Integer;
  4613. begin
  4614. for X := 0 to Width - 1 do begin
  4615. pByteArray(pDest)^[0] := pByteArray(pSource)^[2];
  4616. pByteArray(pDest)^[1] := pByteArray(pSource)^[1];
  4617. pByteArray(pDest)^[2] := pByteArray(pSource)^[0];
  4618. Inc(pDest, 3);
  4619. Inc(pSource, 3);
  4620. end;
  4621. end;
  4622. begin
  4623. if not (ftJPEG in FormatGetSupportedFiles(Format)) then
  4624. raise EglBitmapUnsupportedFormat.Create(Format);
  4625. if not init_libJPEG then
  4626. raise Exception.Create('SaveJPG - unable to initialize libJPEG.');
  4627. try
  4628. FillChar(jpeg{%H-}, SizeOf(jpeg_compress_struct), $00);
  4629. FillChar(jpeg_err{%H-}, SizeOf(jpeg_error_mgr), $00);
  4630. // error managment
  4631. jpeg.err := jpeg_std_error(@jpeg_err);
  4632. jpeg_err.error_exit := glBitmap_libJPEG_error_exit;
  4633. jpeg_err.output_message := glBitmap_libJPEG_output_message;
  4634. // compression struct
  4635. jpeg_create_compress(@jpeg);
  4636. // allocation space for streaming methods
  4637. jpeg.dest := jpeg.mem^.alloc_small(@jpeg, JPOOL_PERMANENT, SizeOf(glBitmap_libJPEG_dest_mgr));
  4638. // seeting up custom functions
  4639. with glBitmap_libJPEG_dest_mgr_ptr(jpeg.dest)^ do begin
  4640. pub.init_destination := glBitmap_libJPEG_init_destination;
  4641. pub.empty_output_buffer := glBitmap_libJPEG_empty_output_buffer;
  4642. pub.term_destination := glBitmap_libJPEG_term_destination;
  4643. pub.next_output_byte := @DestBuffer[1];
  4644. pub.free_in_buffer := Length(DestBuffer);
  4645. DestStream := aStream;
  4646. end;
  4647. // very important state
  4648. jpeg.global_state := CSTATE_START;
  4649. jpeg.image_width := Width;
  4650. jpeg.image_height := Height;
  4651. case Format of
  4652. tfAlpha8ub1, tfLuminance8ub1: begin
  4653. jpeg.input_components := 1;
  4654. jpeg.in_color_space := JCS_GRAYSCALE;
  4655. end;
  4656. tfRGB8ub3, tfBGR8ub3: begin
  4657. jpeg.input_components := 3;
  4658. jpeg.in_color_space := JCS_RGB;
  4659. end;
  4660. end;
  4661. jpeg_set_defaults(@jpeg);
  4662. jpeg_set_quality(@jpeg, 95, true);
  4663. jpeg_start_compress(@jpeg, true);
  4664. pTemp := Data;
  4665. if Format = tfBGR8ub3 then
  4666. GetMem(pTemp2, fRowSize)
  4667. else
  4668. pTemp2 := pTemp;
  4669. try
  4670. for Row := 0 to jpeg.image_height -1 do begin
  4671. // prepare row
  4672. if Format = tfBGR8ub3 then
  4673. CopyRow(pTemp2, pTemp)
  4674. else
  4675. pTemp2 := pTemp;
  4676. // write row
  4677. jpeg_write_scanlines(@jpeg, @pTemp2, 1);
  4678. inc(pTemp, fRowSize);
  4679. end;
  4680. finally
  4681. // free memory
  4682. if Format = tfBGR8ub3 then
  4683. FreeMem(pTemp2);
  4684. end;
  4685. jpeg_finish_compress(@jpeg);
  4686. jpeg_destroy_compress(@jpeg);
  4687. finally
  4688. quit_libJPEG;
  4689. end;
  4690. end;
  4691. {$ELSEIF DEFINED(GLB_DELPHI_JPEG)}
  4692. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4693. procedure TglBitmapData.SaveJPEG(const aStream: TStream);
  4694. var
  4695. Bmp: TBitmap;
  4696. Jpg: TJPEGImage;
  4697. begin
  4698. if not (ftJPEG in FormatGetSupportedFiles(Format)) then
  4699. raise EglBitmapUnsupportedFormat.Create(Format);
  4700. Bmp := TBitmap.Create;
  4701. try
  4702. Jpg := TJPEGImage.Create;
  4703. try
  4704. AssignToBitmap(Bmp);
  4705. if (Format in [tfAlpha8ub1, tfLuminance8ub1]) then begin
  4706. Jpg.Grayscale := true;
  4707. Jpg.PixelFormat := jf8Bit;
  4708. end;
  4709. Jpg.Assign(Bmp);
  4710. Jpg.SaveToStream(aStream);
  4711. finally
  4712. FreeAndNil(Jpg);
  4713. end;
  4714. finally
  4715. FreeAndNil(Bmp);
  4716. end;
  4717. end;
  4718. {$IFEND}
  4719. {$ENDIF}
  4720. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4721. //RAW/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4722. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4723. type
  4724. RawHeader = packed record
  4725. Magic: String[5];
  4726. Version: Byte;
  4727. Width: Integer;
  4728. Height: Integer;
  4729. DataSize: Integer;
  4730. BitsPerPixel: Integer;
  4731. Precision: TglBitmapRec4ub;
  4732. Shift: TglBitmapRec4ub;
  4733. end;
  4734. function TglBitmapData.LoadRAW(const aStream: TStream): Boolean;
  4735. var
  4736. header: RawHeader;
  4737. StartPos: Int64;
  4738. fd: TFormatDescriptor;
  4739. buf: PByte;
  4740. begin
  4741. result := false;
  4742. StartPos := aStream.Position;
  4743. aStream.Read(header{%H-}, SizeOf(header));
  4744. if (header.Magic <> 'glBMP') then begin
  4745. aStream.Position := StartPos;
  4746. exit;
  4747. end;
  4748. fd := TFormatDescriptor.GetFromPrecShift(header.Precision, header.Shift, header.BitsPerPixel);
  4749. if (fd.Format = tfEmpty) then
  4750. raise EglBitmapUnsupportedFormat.Create('no supported format found');
  4751. buf := GetMemory(header.DataSize);
  4752. aStream.Read(buf^, header.DataSize);
  4753. SetData(buf, fd.Format, header.Width, header.Height);
  4754. result := true;
  4755. end;
  4756. procedure TglBitmapData.SaveRAW(const aStream: TStream);
  4757. var
  4758. header: RawHeader;
  4759. fd: TFormatDescriptor;
  4760. begin
  4761. fd := TFormatDescriptor.Get(Format);
  4762. header.Magic := 'glBMP';
  4763. header.Version := 1;
  4764. header.Width := Width;
  4765. header.Height := Height;
  4766. header.DataSize := fd.GetSize(fDimension);
  4767. header.BitsPerPixel := fd.BitsPerPixel;
  4768. header.Precision := fd.Precision;
  4769. header.Shift := fd.Shift;
  4770. aStream.Write(header, SizeOf(header));
  4771. aStream.Write(Data^, header.DataSize);
  4772. end;
  4773. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4774. //BMP/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4775. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4776. const
  4777. BMP_MAGIC = $4D42;
  4778. BMP_COMP_RGB = 0;
  4779. BMP_COMP_RLE8 = 1;
  4780. BMP_COMP_RLE4 = 2;
  4781. BMP_COMP_BITFIELDS = 3;
  4782. type
  4783. TBMPHeader = packed record
  4784. bfType: Word;
  4785. bfSize: Cardinal;
  4786. bfReserved1: Word;
  4787. bfReserved2: Word;
  4788. bfOffBits: Cardinal;
  4789. end;
  4790. TBMPInfo = packed record
  4791. biSize: Cardinal;
  4792. biWidth: Longint;
  4793. biHeight: Longint;
  4794. biPlanes: Word;
  4795. biBitCount: Word;
  4796. biCompression: Cardinal;
  4797. biSizeImage: Cardinal;
  4798. biXPelsPerMeter: Longint;
  4799. biYPelsPerMeter: Longint;
  4800. biClrUsed: Cardinal;
  4801. biClrImportant: Cardinal;
  4802. end;
  4803. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4804. function TglBitmapData.LoadBMP(const aStream: TStream): Boolean;
  4805. //////////////////////////////////////////////////////////////////////////////////////////////////
  4806. function ReadInfo(out aInfo: TBMPInfo; out aMask: TglBitmapRec4ul): TglBitmapFormat;
  4807. var
  4808. tmp, i: Cardinal;
  4809. begin
  4810. result := tfEmpty;
  4811. aStream.Read(aInfo{%H-}, SizeOf(aInfo));
  4812. FillChar(aMask{%H-}, SizeOf(aMask), 0);
  4813. //Read Compression
  4814. case aInfo.biCompression of
  4815. BMP_COMP_RLE4,
  4816. BMP_COMP_RLE8: begin
  4817. raise EglBitmap.Create('RLE compression is not supported');
  4818. end;
  4819. BMP_COMP_BITFIELDS: begin
  4820. if (aInfo.biBitCount = 16) or (aInfo.biBitCount = 32) then begin
  4821. for i := 0 to 2 do begin
  4822. aStream.Read(tmp{%H-}, SizeOf(tmp));
  4823. aMask.arr[i] := tmp;
  4824. end;
  4825. end else
  4826. raise EglBitmap.Create('Bitfields are only supported for 16bit and 32bit formats');
  4827. end;
  4828. end;
  4829. //get suitable format
  4830. case aInfo.biBitCount of
  4831. 8: result := tfLuminance8ub1;
  4832. 16: result := tfX1RGB5us1;
  4833. 24: result := tfBGR8ub3;
  4834. 32: result := tfXRGB8ui1;
  4835. end;
  4836. end;
  4837. function ReadColorTable(var aFormat: TglBitmapFormat; const aInfo: TBMPInfo): TbmpColorTableFormat;
  4838. var
  4839. i, c: Integer;
  4840. fd: TFormatDescriptor;
  4841. ColorTable: TbmpColorTable;
  4842. begin
  4843. result := nil;
  4844. if (aInfo.biBitCount >= 16) then
  4845. exit;
  4846. aFormat := tfLuminance8ub1;
  4847. c := aInfo.biClrUsed;
  4848. if (c = 0) then
  4849. c := 1 shl aInfo.biBitCount;
  4850. SetLength(ColorTable, c);
  4851. for i := 0 to c-1 do begin
  4852. aStream.Read(ColorTable[i], SizeOf(TbmpColorTableEnty));
  4853. if (ColorTable[i].r <> ColorTable[i].g) or (ColorTable[i].g <> ColorTable[i].b) then
  4854. aFormat := tfRGB8ub3;
  4855. end;
  4856. fd := TFormatDescriptor.Get(aFormat);
  4857. result := TbmpColorTableFormat.Create;
  4858. result.ColorTable := ColorTable;
  4859. result.SetCustomValues(aFormat, aInfo.biBitCount, fd.Precision, fd.Shift);
  4860. end;
  4861. //////////////////////////////////////////////////////////////////////////////////////////////////
  4862. function CheckBitfields(var aFormat: TglBitmapFormat; const aMask: TglBitmapRec4ul; const aInfo: TBMPInfo): TbmpBitfieldFormat;
  4863. var
  4864. fd: TFormatDescriptor;
  4865. begin
  4866. result := nil;
  4867. if (aMask.r <> 0) or (aMask.g <> 0) or (aMask.b <> 0) or (aMask.a <> 0) then begin
  4868. // find suitable format ...
  4869. fd := TFormatDescriptor.GetFromMask(aMask);
  4870. if (fd.Format <> tfEmpty) then begin
  4871. aFormat := fd.Format;
  4872. exit;
  4873. end;
  4874. // or create custom bitfield format
  4875. result := TbmpBitfieldFormat.Create;
  4876. result.SetCustomValues(aInfo.biBitCount, aMask);
  4877. end;
  4878. end;
  4879. var
  4880. //simple types
  4881. StartPos: Int64;
  4882. ImageSize, rbLineSize, wbLineSize, Padding, i: Integer;
  4883. PaddingBuff: Cardinal;
  4884. LineBuf, ImageData, TmpData: PByte;
  4885. SourceMD, DestMD: Pointer;
  4886. BmpFormat: TglBitmapFormat;
  4887. //records
  4888. Mask: TglBitmapRec4ul;
  4889. Header: TBMPHeader;
  4890. Info: TBMPInfo;
  4891. //classes
  4892. SpecialFormat: TFormatDescriptor;
  4893. FormatDesc: TFormatDescriptor;
  4894. //////////////////////////////////////////////////////////////////////////////////////////////////
  4895. procedure SpecialFormatReadLine(aData: PByte; aLineBuf: PByte);
  4896. var
  4897. i: Integer;
  4898. Pixel: TglBitmapPixelData;
  4899. begin
  4900. aStream.Read(aLineBuf^, rbLineSize);
  4901. SpecialFormat.PreparePixel(Pixel);
  4902. for i := 0 to Info.biWidth-1 do begin
  4903. SpecialFormat.Unmap(aLineBuf, Pixel, SourceMD);
  4904. glBitmapConvertPixel(Pixel, SpecialFormat, FormatDesc);
  4905. FormatDesc.Map(Pixel, aData, DestMD);
  4906. end;
  4907. end;
  4908. begin
  4909. result := false;
  4910. BmpFormat := tfEmpty;
  4911. SpecialFormat := nil;
  4912. LineBuf := nil;
  4913. SourceMD := nil;
  4914. DestMD := nil;
  4915. // Header
  4916. StartPos := aStream.Position;
  4917. aStream.Read(Header{%H-}, SizeOf(Header));
  4918. if Header.bfType = BMP_MAGIC then begin
  4919. try try
  4920. BmpFormat := ReadInfo(Info, Mask);
  4921. SpecialFormat := ReadColorTable(BmpFormat, Info);
  4922. if not Assigned(SpecialFormat) then
  4923. SpecialFormat := CheckBitfields(BmpFormat, Mask, Info);
  4924. aStream.Position := StartPos + Header.bfOffBits;
  4925. if (BmpFormat <> tfEmpty) then begin
  4926. FormatDesc := TFormatDescriptor.Get(BmpFormat);
  4927. rbLineSize := Round(Info.biWidth * Info.biBitCount / 8); //ReadBuffer LineSize
  4928. wbLineSize := Trunc(Info.biWidth * FormatDesc.BytesPerPixel);
  4929. Padding := (((Info.biWidth * Info.biBitCount + 31) and - 32) shr 3) - rbLineSize;
  4930. //get Memory
  4931. DestMD := FormatDesc.CreateMappingData;
  4932. ImageSize := FormatDesc.GetSize(Info.biWidth, abs(Info.biHeight));
  4933. GetMem(ImageData, ImageSize);
  4934. if Assigned(SpecialFormat) then begin
  4935. GetMem(LineBuf, rbLineSize); //tmp Memory for converting Bitfields
  4936. SourceMD := SpecialFormat.CreateMappingData;
  4937. end;
  4938. //read Data
  4939. try try
  4940. FillChar(ImageData^, ImageSize, $FF);
  4941. TmpData := ImageData;
  4942. if (Info.biHeight > 0) then
  4943. Inc(TmpData, wbLineSize * (Info.biHeight-1));
  4944. for i := 0 to Abs(Info.biHeight)-1 do begin
  4945. if Assigned(SpecialFormat) then
  4946. SpecialFormatReadLine(TmpData, LineBuf) //if is special format read and convert data
  4947. else
  4948. aStream.Read(TmpData^, wbLineSize); //else only read data
  4949. if (Info.biHeight > 0) then
  4950. dec(TmpData, wbLineSize)
  4951. else
  4952. inc(TmpData, wbLineSize);
  4953. aStream.Read(PaddingBuff{%H-}, Padding);
  4954. end;
  4955. SetData(ImageData, BmpFormat, Info.biWidth, abs(Info.biHeight));
  4956. result := true;
  4957. finally
  4958. if Assigned(LineBuf) then
  4959. FreeMem(LineBuf);
  4960. if Assigned(SourceMD) then
  4961. SpecialFormat.FreeMappingData(SourceMD);
  4962. FormatDesc.FreeMappingData(DestMD);
  4963. end;
  4964. except
  4965. if Assigned(ImageData) then
  4966. FreeMem(ImageData);
  4967. raise;
  4968. end;
  4969. end else
  4970. raise EglBitmap.Create('LoadBMP - No suitable format found');
  4971. except
  4972. aStream.Position := StartPos;
  4973. raise;
  4974. end;
  4975. finally
  4976. FreeAndNil(SpecialFormat);
  4977. end;
  4978. end
  4979. else aStream.Position := StartPos;
  4980. end;
  4981. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4982. procedure TglBitmapData.SaveBMP(const aStream: TStream);
  4983. var
  4984. Header: TBMPHeader;
  4985. Info: TBMPInfo;
  4986. Converter: TFormatDescriptor;
  4987. FormatDesc: TFormatDescriptor;
  4988. SourceFD, DestFD: Pointer;
  4989. pData, srcData, dstData, ConvertBuffer: pByte;
  4990. Pixel: TglBitmapPixelData;
  4991. ImageSize, wbLineSize, rbLineSize, Padding, LineIdx, PixelIdx: Integer;
  4992. RedMask, GreenMask, BlueMask, AlphaMask: Cardinal;
  4993. PaddingBuff: Cardinal;
  4994. function GetLineWidth : Integer;
  4995. begin
  4996. result := ((Info.biWidth * Info.biBitCount + 31) and - 32) shr 3;
  4997. end;
  4998. begin
  4999. if not (ftBMP in FormatGetSupportedFiles(Format)) then
  5000. raise EglBitmapUnsupportedFormat.Create(Format);
  5001. Converter := nil;
  5002. FormatDesc := TFormatDescriptor.Get(Format);
  5003. ImageSize := FormatDesc.GetSize(Dimension);
  5004. FillChar(Header{%H-}, SizeOf(Header), 0);
  5005. Header.bfType := BMP_MAGIC;
  5006. Header.bfSize := SizeOf(Header) + SizeOf(Info) + ImageSize;
  5007. Header.bfReserved1 := 0;
  5008. Header.bfReserved2 := 0;
  5009. Header.bfOffBits := SizeOf(Header) + SizeOf(Info);
  5010. FillChar(Info{%H-}, SizeOf(Info), 0);
  5011. Info.biSize := SizeOf(Info);
  5012. Info.biWidth := Width;
  5013. Info.biHeight := Height;
  5014. Info.biPlanes := 1;
  5015. Info.biCompression := BMP_COMP_RGB;
  5016. Info.biSizeImage := ImageSize;
  5017. try
  5018. case Format of
  5019. tfAlpha4ub1, tfAlpha8ub1, tfLuminance4ub1, tfLuminance8ub1, tfR3G3B2ub1:
  5020. begin
  5021. Info.biBitCount := 8;
  5022. Header.bfSize := Header.bfSize + 256 * SizeOf(Cardinal);
  5023. Header.bfOffBits := Header.bfOffBits + 256 * SizeOf(Cardinal); //256 ColorTable entries
  5024. Converter := TbmpColorTableFormat.Create;
  5025. with (Converter as TbmpColorTableFormat) do begin
  5026. SetCustomValues(fFormat, 8, FormatDesc.Precision, FormatDesc.Shift);
  5027. CreateColorTable;
  5028. end;
  5029. end;
  5030. tfLuminance4Alpha4ub2, tfLuminance6Alpha2ub2, tfLuminance8Alpha8ub2,
  5031. tfRGBX4us1, tfXRGB4us1, tfRGB5X1us1, tfX1RGB5us1, tfR5G6B5us1, tfRGB5A1us1, tfA1RGB5us1, tfRGBA4us1, tfARGB4us1,
  5032. tfBGRX4us1, tfXBGR4us1, tfBGR5X1us1, tfX1BGR5us1, tfB5G6R5us1, tfBGR5A1us1, tfA1BGR5us1, tfBGRA4us1, tfABGR4us1:
  5033. begin
  5034. Info.biBitCount := 16;
  5035. Info.biCompression := BMP_COMP_BITFIELDS;
  5036. end;
  5037. tfBGR8ub3, tfRGB8ub3:
  5038. begin
  5039. Info.biBitCount := 24;
  5040. if (Format = tfRGB8ub3) then
  5041. Converter := TfdBGR8ub3.Create; //use BGR8 Format Descriptor to Swap RGB Values
  5042. end;
  5043. tfRGBX8ui1, tfXRGB8ui1, tfRGB10X2ui1, tfX2RGB10ui1, tfRGBA8ui1, tfARGB8ui1, tfRGBA8ub4, tfRGB10A2ui1, tfA2RGB10ui1,
  5044. tfBGRX8ui1, tfXBGR8ui1, tfBGR10X2ui1, tfX2BGR10ui1, tfBGRA8ui1, tfABGR8ui1, tfBGRA8ub4, tfBGR10A2ui1, tfA2BGR10ui1:
  5045. begin
  5046. Info.biBitCount := 32;
  5047. Info.biCompression := BMP_COMP_BITFIELDS;
  5048. end;
  5049. else
  5050. raise EglBitmapUnsupportedFormat.Create(Format);
  5051. end;
  5052. Info.biXPelsPerMeter := 2835;
  5053. Info.biYPelsPerMeter := 2835;
  5054. // prepare bitmasks
  5055. if Info.biCompression = BMP_COMP_BITFIELDS then begin
  5056. Header.bfSize := Header.bfSize + 4 * SizeOf(Cardinal);
  5057. Header.bfOffBits := Header.bfOffBits + 4 * SizeOf(Cardinal);
  5058. RedMask := FormatDesc.Mask.r;
  5059. GreenMask := FormatDesc.Mask.g;
  5060. BlueMask := FormatDesc.Mask.b;
  5061. AlphaMask := FormatDesc.Mask.a;
  5062. end;
  5063. // headers
  5064. aStream.Write(Header, SizeOf(Header));
  5065. aStream.Write(Info, SizeOf(Info));
  5066. // colortable
  5067. if Assigned(Converter) and (Converter is TbmpColorTableFormat) then
  5068. with (Converter as TbmpColorTableFormat) do
  5069. aStream.Write(ColorTable[0].b,
  5070. SizeOf(TbmpColorTableEnty) * Length(ColorTable));
  5071. // bitmasks
  5072. if Info.biCompression = BMP_COMP_BITFIELDS then begin
  5073. aStream.Write(RedMask, SizeOf(Cardinal));
  5074. aStream.Write(GreenMask, SizeOf(Cardinal));
  5075. aStream.Write(BlueMask, SizeOf(Cardinal));
  5076. aStream.Write(AlphaMask, SizeOf(Cardinal));
  5077. end;
  5078. // image data
  5079. rbLineSize := Round(Info.biWidth * FormatDesc.BytesPerPixel);
  5080. wbLineSize := Round(Info.biWidth * Info.biBitCount / 8);
  5081. Padding := GetLineWidth - wbLineSize;
  5082. PaddingBuff := 0;
  5083. pData := Data;
  5084. inc(pData, (Height-1) * rbLineSize);
  5085. // prepare row buffer. But only for RGB because RGBA supports color masks
  5086. // so it's possible to change color within the image.
  5087. if Assigned(Converter) then begin
  5088. FormatDesc.PreparePixel(Pixel);
  5089. GetMem(ConvertBuffer, wbLineSize);
  5090. SourceFD := FormatDesc.CreateMappingData;
  5091. DestFD := Converter.CreateMappingData;
  5092. end else
  5093. ConvertBuffer := nil;
  5094. try
  5095. for LineIdx := 0 to Height - 1 do begin
  5096. // preparing row
  5097. if Assigned(Converter) then begin
  5098. srcData := pData;
  5099. dstData := ConvertBuffer;
  5100. for PixelIdx := 0 to Info.biWidth-1 do begin
  5101. FormatDesc.Unmap(srcData, Pixel, SourceFD);
  5102. glBitmapConvertPixel(Pixel, FormatDesc, Converter);
  5103. Converter.Map(Pixel, dstData, DestFD);
  5104. end;
  5105. aStream.Write(ConvertBuffer^, wbLineSize);
  5106. end else begin
  5107. aStream.Write(pData^, rbLineSize);
  5108. end;
  5109. dec(pData, rbLineSize);
  5110. if (Padding > 0) then
  5111. aStream.Write(PaddingBuff, Padding);
  5112. end;
  5113. finally
  5114. // destroy row buffer
  5115. if Assigned(ConvertBuffer) then begin
  5116. FormatDesc.FreeMappingData(SourceFD);
  5117. Converter.FreeMappingData(DestFD);
  5118. FreeMem(ConvertBuffer);
  5119. end;
  5120. end;
  5121. finally
  5122. if Assigned(Converter) then
  5123. Converter.Free;
  5124. end;
  5125. end;
  5126. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5127. //TGA/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5128. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5129. type
  5130. TTGAHeader = packed record
  5131. ImageID: Byte;
  5132. ColorMapType: Byte;
  5133. ImageType: Byte;
  5134. //ColorMapSpec: Array[0..4] of Byte;
  5135. ColorMapStart: Word;
  5136. ColorMapLength: Word;
  5137. ColorMapEntrySize: Byte;
  5138. OrigX: Word;
  5139. OrigY: Word;
  5140. Width: Word;
  5141. Height: Word;
  5142. Bpp: Byte;
  5143. ImageDesc: Byte;
  5144. end;
  5145. const
  5146. TGA_UNCOMPRESSED_RGB = 2;
  5147. TGA_UNCOMPRESSED_GRAY = 3;
  5148. TGA_COMPRESSED_RGB = 10;
  5149. TGA_COMPRESSED_GRAY = 11;
  5150. TGA_NONE_COLOR_TABLE = 0;
  5151. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5152. function TglBitmapData.LoadTGA(const aStream: TStream): Boolean;
  5153. var
  5154. Header: TTGAHeader;
  5155. ImageData: System.PByte;
  5156. StartPosition: Int64;
  5157. PixelSize, LineSize: Integer;
  5158. tgaFormat: TglBitmapFormat;
  5159. FormatDesc: TFormatDescriptor;
  5160. Counter: packed record
  5161. X, Y: packed record
  5162. low, high, dir: Integer;
  5163. end;
  5164. end;
  5165. const
  5166. CACHE_SIZE = $4000;
  5167. ////////////////////////////////////////////////////////////////////////////////////////
  5168. procedure ReadUncompressed;
  5169. var
  5170. i, j: Integer;
  5171. buf, tmp1, tmp2: System.PByte;
  5172. begin
  5173. buf := nil;
  5174. if (Counter.X.dir < 0) then
  5175. GetMem(buf, LineSize);
  5176. try
  5177. while (Counter.Y.low <> Counter.Y.high + counter.Y.dir) do begin
  5178. tmp1 := ImageData;
  5179. inc(tmp1, (Counter.Y.low * LineSize)); //pointer to LineStart
  5180. if (Counter.X.dir < 0) then begin //flip X
  5181. aStream.Read(buf^, LineSize);
  5182. tmp2 := buf;
  5183. inc(tmp2, LineSize - PixelSize); //pointer to last pixel in line
  5184. for i := 0 to Header.Width-1 do begin //for all pixels in line
  5185. for j := 0 to PixelSize-1 do begin //for all bytes in pixel
  5186. tmp1^ := tmp2^;
  5187. inc(tmp1);
  5188. inc(tmp2);
  5189. end;
  5190. dec(tmp2, 2*PixelSize); //move 2 backwards, because j-loop moved 1 forward
  5191. end;
  5192. end else
  5193. aStream.Read(tmp1^, LineSize);
  5194. inc(Counter.Y.low, Counter.Y.dir); //move to next line index
  5195. end;
  5196. finally
  5197. if Assigned(buf) then
  5198. FreeMem(buf);
  5199. end;
  5200. end;
  5201. ////////////////////////////////////////////////////////////////////////////////////////
  5202. procedure ReadCompressed;
  5203. /////////////////////////////////////////////////////////////////
  5204. var
  5205. TmpData: System.PByte;
  5206. LinePixelsRead: Integer;
  5207. procedure CheckLine;
  5208. begin
  5209. if (LinePixelsRead >= Header.Width) then begin
  5210. LinePixelsRead := 0;
  5211. inc(Counter.Y.low, Counter.Y.dir); //next line index
  5212. TmpData := ImageData;
  5213. inc(TmpData, Counter.Y.low * LineSize); //set line
  5214. if (Counter.X.dir < 0) then //if x flipped then
  5215. inc(TmpData, LineSize - PixelSize); //set last pixel
  5216. end;
  5217. end;
  5218. /////////////////////////////////////////////////////////////////
  5219. var
  5220. Cache: PByte;
  5221. CacheSize, CachePos: Integer;
  5222. procedure CachedRead(out Buffer; Count: Integer);
  5223. var
  5224. BytesRead: Integer;
  5225. begin
  5226. if (CachePos + Count > CacheSize) then begin
  5227. //if buffer overflow save non read bytes
  5228. BytesRead := 0;
  5229. if (CacheSize - CachePos > 0) then begin
  5230. BytesRead := CacheSize - CachePos;
  5231. Move(PByteArray(Cache)^[CachePos], Buffer{%H-}, BytesRead);
  5232. inc(CachePos, BytesRead);
  5233. end;
  5234. //load cache from file
  5235. CacheSize := Min(CACHE_SIZE, aStream.Size - aStream.Position);
  5236. aStream.Read(Cache^, CacheSize);
  5237. CachePos := 0;
  5238. //read rest of requested bytes
  5239. if (Count - BytesRead > 0) then begin
  5240. Move(PByteArray(Cache)^[CachePos], TByteArray(Buffer)[BytesRead], Count - BytesRead);
  5241. inc(CachePos, Count - BytesRead);
  5242. end;
  5243. end else begin
  5244. //if no buffer overflow just read the data
  5245. Move(PByteArray(Cache)^[CachePos], Buffer, Count);
  5246. inc(CachePos, Count);
  5247. end;
  5248. end;
  5249. procedure PixelToBuffer(const aData: PByte; var aBuffer: PByte);
  5250. begin
  5251. case PixelSize of
  5252. 1: begin
  5253. aBuffer^ := aData^;
  5254. inc(aBuffer, Counter.X.dir);
  5255. end;
  5256. 2: begin
  5257. PWord(aBuffer)^ := PWord(aData)^;
  5258. inc(aBuffer, 2 * Counter.X.dir);
  5259. end;
  5260. 3: begin
  5261. PByteArray(aBuffer)^[0] := PByteArray(aData)^[0];
  5262. PByteArray(aBuffer)^[1] := PByteArray(aData)^[1];
  5263. PByteArray(aBuffer)^[2] := PByteArray(aData)^[2];
  5264. inc(aBuffer, 3 * Counter.X.dir);
  5265. end;
  5266. 4: begin
  5267. PCardinal(aBuffer)^ := PCardinal(aData)^;
  5268. inc(aBuffer, 4 * Counter.X.dir);
  5269. end;
  5270. end;
  5271. end;
  5272. var
  5273. TotalPixelsToRead, TotalPixelsRead: Integer;
  5274. Temp: Byte;
  5275. buf: array [0..3] of Byte; //1 pixel is max 32bit long
  5276. PixelRepeat: Boolean;
  5277. PixelsToRead, PixelCount: Integer;
  5278. begin
  5279. CacheSize := 0;
  5280. CachePos := 0;
  5281. TotalPixelsToRead := Header.Width * Header.Height;
  5282. TotalPixelsRead := 0;
  5283. LinePixelsRead := 0;
  5284. GetMem(Cache, CACHE_SIZE);
  5285. try
  5286. TmpData := ImageData;
  5287. inc(TmpData, Counter.Y.low * LineSize); //set line
  5288. if (Counter.X.dir < 0) then //if x flipped then
  5289. inc(TmpData, LineSize - PixelSize); //set last pixel
  5290. repeat
  5291. //read CommandByte
  5292. CachedRead(Temp, 1);
  5293. PixelRepeat := (Temp and $80) > 0;
  5294. PixelsToRead := (Temp and $7F) + 1;
  5295. inc(TotalPixelsRead, PixelsToRead);
  5296. if PixelRepeat then
  5297. CachedRead(buf[0], PixelSize);
  5298. while (PixelsToRead > 0) do begin
  5299. CheckLine;
  5300. PixelCount := Min(Header.Width - LinePixelsRead, PixelsToRead); //max read to EOL or EOF
  5301. while (PixelCount > 0) do begin
  5302. if not PixelRepeat then
  5303. CachedRead(buf[0], PixelSize);
  5304. PixelToBuffer(@buf[0], TmpData);
  5305. inc(LinePixelsRead);
  5306. dec(PixelsToRead);
  5307. dec(PixelCount);
  5308. end;
  5309. end;
  5310. until (TotalPixelsRead >= TotalPixelsToRead);
  5311. finally
  5312. FreeMem(Cache);
  5313. end;
  5314. end;
  5315. function IsGrayFormat: Boolean;
  5316. begin
  5317. result := Header.ImageType in [TGA_UNCOMPRESSED_GRAY, TGA_COMPRESSED_GRAY];
  5318. end;
  5319. begin
  5320. result := false;
  5321. // reading header to test file and set cursor back to begin
  5322. StartPosition := aStream.Position;
  5323. aStream.Read(Header{%H-}, SizeOf(Header));
  5324. // no colormapped files
  5325. if (Header.ColorMapType = TGA_NONE_COLOR_TABLE) and (Header.ImageType in [
  5326. TGA_UNCOMPRESSED_RGB, TGA_UNCOMPRESSED_GRAY, TGA_COMPRESSED_RGB, TGA_COMPRESSED_GRAY]) then
  5327. begin
  5328. try
  5329. if Header.ImageID <> 0 then // skip image ID
  5330. aStream.Position := aStream.Position + Header.ImageID;
  5331. tgaFormat := tfEmpty;
  5332. case Header.Bpp of
  5333. 8: if IsGrayFormat then case (Header.ImageDesc and $F) of
  5334. 0: tgaFormat := tfLuminance8ub1;
  5335. 8: tgaFormat := tfAlpha8ub1;
  5336. end;
  5337. 16: if IsGrayFormat then case (Header.ImageDesc and $F) of
  5338. 0: tgaFormat := tfLuminance16us1;
  5339. 8: tgaFormat := tfLuminance8Alpha8ub2;
  5340. end else case (Header.ImageDesc and $F) of
  5341. 0: tgaFormat := tfX1RGB5us1;
  5342. 1: tgaFormat := tfA1RGB5us1;
  5343. 4: tgaFormat := tfARGB4us1;
  5344. end;
  5345. 24: if not IsGrayFormat then case (Header.ImageDesc and $F) of
  5346. 0: tgaFormat := tfBGR8ub3;
  5347. end;
  5348. 32: if IsGrayFormat then case (Header.ImageDesc and $F) of
  5349. 0: tgaFormat := tfDepth32ui1;
  5350. end else case (Header.ImageDesc and $F) of
  5351. 0: tgaFormat := tfX2RGB10ui1;
  5352. 2: tgaFormat := tfA2RGB10ui1;
  5353. 8: tgaFormat := tfARGB8ui1;
  5354. end;
  5355. end;
  5356. if (tgaFormat = tfEmpty) then
  5357. raise EglBitmap.Create('LoadTga - unsupported format');
  5358. FormatDesc := TFormatDescriptor.Get(tgaFormat);
  5359. PixelSize := FormatDesc.GetSize(1, 1);
  5360. LineSize := FormatDesc.GetSize(Header.Width, 1);
  5361. GetMem(ImageData, LineSize * Header.Height);
  5362. try
  5363. //column direction
  5364. if ((Header.ImageDesc and (1 shl 4)) > 0) then begin
  5365. Counter.X.low := Header.Height-1;;
  5366. Counter.X.high := 0;
  5367. Counter.X.dir := -1;
  5368. end else begin
  5369. Counter.X.low := 0;
  5370. Counter.X.high := Header.Height-1;
  5371. Counter.X.dir := 1;
  5372. end;
  5373. // Row direction
  5374. if ((Header.ImageDesc and (1 shl 5)) > 0) then begin
  5375. Counter.Y.low := 0;
  5376. Counter.Y.high := Header.Height-1;
  5377. Counter.Y.dir := 1;
  5378. end else begin
  5379. Counter.Y.low := Header.Height-1;;
  5380. Counter.Y.high := 0;
  5381. Counter.Y.dir := -1;
  5382. end;
  5383. // Read Image
  5384. case Header.ImageType of
  5385. TGA_UNCOMPRESSED_RGB, TGA_UNCOMPRESSED_GRAY:
  5386. ReadUncompressed;
  5387. TGA_COMPRESSED_RGB, TGA_COMPRESSED_GRAY:
  5388. ReadCompressed;
  5389. end;
  5390. SetData(ImageData, tgaFormat, Header.Width, Header.Height);
  5391. result := true;
  5392. except
  5393. if Assigned(ImageData) then
  5394. FreeMem(ImageData);
  5395. raise;
  5396. end;
  5397. finally
  5398. aStream.Position := StartPosition;
  5399. end;
  5400. end
  5401. else aStream.Position := StartPosition;
  5402. end;
  5403. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5404. procedure TglBitmapData.SaveTGA(const aStream: TStream);
  5405. var
  5406. Header: TTGAHeader;
  5407. Size: Integer;
  5408. FormatDesc: TFormatDescriptor;
  5409. begin
  5410. if not (ftTGA in FormatGetSupportedFiles(Format)) then
  5411. raise EglBitmapUnsupportedFormat.Create(Format);
  5412. //prepare header
  5413. FormatDesc := TFormatDescriptor.Get(Format);
  5414. FillChar(Header{%H-}, SizeOf(Header), 0);
  5415. Header.ImageDesc := CountSetBits(FormatDesc.Range.a) and $F;
  5416. Header.Bpp := FormatDesc.BitsPerPixel;
  5417. Header.Width := Width;
  5418. Header.Height := Height;
  5419. Header.ImageDesc := Header.ImageDesc or $20; //flip y
  5420. if FormatDesc.IsGrayscale or (not FormatDesc.IsGrayscale and not FormatDesc.HasRed and FormatDesc.HasAlpha) then
  5421. Header.ImageType := TGA_UNCOMPRESSED_GRAY
  5422. else
  5423. Header.ImageType := TGA_UNCOMPRESSED_RGB;
  5424. aStream.Write(Header, SizeOf(Header));
  5425. // write Data
  5426. Size := FormatDesc.GetSize(Dimension);
  5427. aStream.Write(Data^, Size);
  5428. end;
  5429. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5430. //DDS/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5431. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5432. const
  5433. DDS_MAGIC: Cardinal = $20534444;
  5434. // DDS_header.dwFlags
  5435. DDSD_CAPS = $00000001;
  5436. DDSD_HEIGHT = $00000002;
  5437. DDSD_WIDTH = $00000004;
  5438. DDSD_PIXELFORMAT = $00001000;
  5439. // DDS_header.sPixelFormat.dwFlags
  5440. DDPF_ALPHAPIXELS = $00000001;
  5441. DDPF_ALPHA = $00000002;
  5442. DDPF_FOURCC = $00000004;
  5443. DDPF_RGB = $00000040;
  5444. DDPF_LUMINANCE = $00020000;
  5445. // DDS_header.sCaps.dwCaps1
  5446. DDSCAPS_TEXTURE = $00001000;
  5447. // DDS_header.sCaps.dwCaps2
  5448. DDSCAPS2_CUBEMAP = $00000200;
  5449. D3DFMT_DXT1 = $31545844;
  5450. D3DFMT_DXT3 = $33545844;
  5451. D3DFMT_DXT5 = $35545844;
  5452. type
  5453. TDDSPixelFormat = packed record
  5454. dwSize: Cardinal;
  5455. dwFlags: Cardinal;
  5456. dwFourCC: Cardinal;
  5457. dwRGBBitCount: Cardinal;
  5458. dwRBitMask: Cardinal;
  5459. dwGBitMask: Cardinal;
  5460. dwBBitMask: Cardinal;
  5461. dwABitMask: Cardinal;
  5462. end;
  5463. TDDSCaps = packed record
  5464. dwCaps1: Cardinal;
  5465. dwCaps2: Cardinal;
  5466. dwDDSX: Cardinal;
  5467. dwReserved: Cardinal;
  5468. end;
  5469. TDDSHeader = packed record
  5470. dwSize: Cardinal;
  5471. dwFlags: Cardinal;
  5472. dwHeight: Cardinal;
  5473. dwWidth: Cardinal;
  5474. dwPitchOrLinearSize: Cardinal;
  5475. dwDepth: Cardinal;
  5476. dwMipMapCount: Cardinal;
  5477. dwReserved: array[0..10] of Cardinal;
  5478. PixelFormat: TDDSPixelFormat;
  5479. Caps: TDDSCaps;
  5480. dwReserved2: Cardinal;
  5481. end;
  5482. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5483. function TglBitmapData.LoadDDS(const aStream: TStream): Boolean;
  5484. var
  5485. Header: TDDSHeader;
  5486. Converter: TbmpBitfieldFormat;
  5487. function GetDDSFormat: TglBitmapFormat;
  5488. var
  5489. fd: TFormatDescriptor;
  5490. i: Integer;
  5491. Mask: TglBitmapRec4ul;
  5492. Range: TglBitmapRec4ui;
  5493. match: Boolean;
  5494. begin
  5495. result := tfEmpty;
  5496. with Header.PixelFormat do begin
  5497. // Compresses
  5498. if ((dwFlags and DDPF_FOURCC) > 0) then begin
  5499. case Header.PixelFormat.dwFourCC of
  5500. D3DFMT_DXT1: result := tfS3tcDtx1RGBA;
  5501. D3DFMT_DXT3: result := tfS3tcDtx3RGBA;
  5502. D3DFMT_DXT5: result := tfS3tcDtx5RGBA;
  5503. end;
  5504. end else if ((dwFlags and (DDPF_RGB or DDPF_ALPHAPIXELS or DDPF_LUMINANCE or DDPF_ALPHA)) > 0) then begin
  5505. // prepare masks
  5506. if ((dwFlags and DDPF_LUMINANCE) = 0) then begin
  5507. Mask.r := dwRBitMask;
  5508. Mask.g := dwGBitMask;
  5509. Mask.b := dwBBitMask;
  5510. end else begin
  5511. Mask.r := dwRBitMask;
  5512. Mask.g := dwRBitMask;
  5513. Mask.b := dwRBitMask;
  5514. end;
  5515. if (dwFlags and DDPF_ALPHAPIXELS > 0) then
  5516. Mask.a := dwABitMask
  5517. else
  5518. Mask.a := 0;;
  5519. //find matching format
  5520. fd := TFormatDescriptor.GetFromMask(Mask, dwRGBBitCount);
  5521. result := fd.Format;
  5522. if (result <> tfEmpty) then
  5523. exit;
  5524. //find format with same Range
  5525. for i := 0 to 3 do
  5526. Range.arr[i] := (2 shl CountSetBits(Mask.arr[i])) - 1;
  5527. for result := High(TglBitmapFormat) downto Low(TglBitmapFormat) do begin
  5528. fd := TFormatDescriptor.Get(result);
  5529. match := true;
  5530. for i := 0 to 3 do
  5531. if (fd.Range.arr[i] <> Range.arr[i]) then begin
  5532. match := false;
  5533. break;
  5534. end;
  5535. if match then
  5536. break;
  5537. end;
  5538. //no format with same range found -> use default
  5539. if (result = tfEmpty) then begin
  5540. if (dwABitMask > 0) then
  5541. result := tfRGBA8ui1
  5542. else
  5543. result := tfRGB8ub3;
  5544. end;
  5545. Converter := TbmpBitfieldFormat.Create;
  5546. Converter.SetCustomValues(dwRGBBitCount, glBitmapRec4ul(dwRBitMask, dwGBitMask, dwBBitMask, dwABitMask));
  5547. end;
  5548. end;
  5549. end;
  5550. var
  5551. StreamPos: Int64;
  5552. x, y, LineSize, RowSize, Magic: Cardinal;
  5553. NewImage, TmpData, RowData, SrcData: System.PByte;
  5554. SourceMD, DestMD: Pointer;
  5555. Pixel: TglBitmapPixelData;
  5556. ddsFormat: TglBitmapFormat;
  5557. FormatDesc: TFormatDescriptor;
  5558. begin
  5559. result := false;
  5560. Converter := nil;
  5561. StreamPos := aStream.Position;
  5562. // Magic
  5563. aStream.Read(Magic{%H-}, sizeof(Magic));
  5564. if (Magic <> DDS_MAGIC) then begin
  5565. aStream.Position := StreamPos;
  5566. exit;
  5567. end;
  5568. //Header
  5569. aStream.Read(Header{%H-}, sizeof(Header));
  5570. if (Header.dwSize <> SizeOf(Header)) or
  5571. ((Header.dwFlags and (DDSD_PIXELFORMAT or DDSD_CAPS or DDSD_WIDTH or DDSD_HEIGHT)) <>
  5572. (DDSD_PIXELFORMAT or DDSD_CAPS or DDSD_WIDTH or DDSD_HEIGHT)) then
  5573. begin
  5574. aStream.Position := StreamPos;
  5575. exit;
  5576. end;
  5577. if ((Header.Caps.dwCaps1 and DDSCAPS2_CUBEMAP) > 0) then
  5578. raise EglBitmap.Create('LoadDDS - CubeMaps are not supported');
  5579. ddsFormat := GetDDSFormat;
  5580. try
  5581. if (ddsFormat = tfEmpty) then
  5582. raise EglBitmap.Create('LoadDDS - unsupported Pixelformat found.');
  5583. FormatDesc := TFormatDescriptor.Get(ddsFormat);
  5584. LineSize := Trunc(Header.dwWidth * FormatDesc.BytesPerPixel);
  5585. GetMem(NewImage, Header.dwHeight * LineSize);
  5586. try
  5587. TmpData := NewImage;
  5588. //Converter needed
  5589. if Assigned(Converter) then begin
  5590. RowSize := Round(Header.dwWidth * Header.PixelFormat.dwRGBBitCount / 8);
  5591. GetMem(RowData, RowSize);
  5592. SourceMD := Converter.CreateMappingData;
  5593. DestMD := FormatDesc.CreateMappingData;
  5594. try
  5595. for y := 0 to Header.dwHeight-1 do begin
  5596. TmpData := NewImage;
  5597. inc(TmpData, y * LineSize);
  5598. SrcData := RowData;
  5599. aStream.Read(SrcData^, RowSize);
  5600. for x := 0 to Header.dwWidth-1 do begin
  5601. Converter.Unmap(SrcData, Pixel, SourceMD);
  5602. glBitmapConvertPixel(Pixel, Converter, FormatDesc);
  5603. FormatDesc.Map(Pixel, TmpData, DestMD);
  5604. end;
  5605. end;
  5606. finally
  5607. Converter.FreeMappingData(SourceMD);
  5608. FormatDesc.FreeMappingData(DestMD);
  5609. FreeMem(RowData);
  5610. end;
  5611. end else
  5612. // Compressed
  5613. if ((Header.PixelFormat.dwFlags and DDPF_FOURCC) > 0) then begin
  5614. RowSize := Header.dwPitchOrLinearSize div Header.dwWidth;
  5615. for Y := 0 to Header.dwHeight-1 do begin
  5616. aStream.Read(TmpData^, RowSize);
  5617. Inc(TmpData, LineSize);
  5618. end;
  5619. end else
  5620. // Uncompressed
  5621. if (Header.PixelFormat.dwFlags and (DDPF_RGB or DDPF_ALPHAPIXELS or DDPF_LUMINANCE)) > 0 then begin
  5622. RowSize := (Header.PixelFormat.dwRGBBitCount * Header.dwWidth) shr 3;
  5623. for Y := 0 to Header.dwHeight-1 do begin
  5624. aStream.Read(TmpData^, RowSize);
  5625. Inc(TmpData, LineSize);
  5626. end;
  5627. end else
  5628. raise EglBitmap.Create('LoadDDS - unsupported Pixelformat found.');
  5629. SetData(NewImage, ddsFormat, Header.dwWidth, Header.dwHeight);
  5630. result := true;
  5631. except
  5632. if Assigned(NewImage) then
  5633. FreeMem(NewImage);
  5634. raise;
  5635. end;
  5636. finally
  5637. FreeAndNil(Converter);
  5638. end;
  5639. end;
  5640. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5641. procedure TglBitmapData.SaveDDS(const aStream: TStream);
  5642. var
  5643. Header: TDDSHeader;
  5644. FormatDesc: TFormatDescriptor;
  5645. begin
  5646. if not (ftDDS in FormatGetSupportedFiles(Format)) then
  5647. raise EglBitmapUnsupportedFormat.Create(Format);
  5648. FormatDesc := TFormatDescriptor.Get(Format);
  5649. // Generell
  5650. FillChar(Header{%H-}, SizeOf(Header), 0);
  5651. Header.dwSize := SizeOf(Header);
  5652. Header.dwFlags := DDSD_WIDTH or DDSD_HEIGHT or DDSD_CAPS or DDSD_PIXELFORMAT;
  5653. Header.dwWidth := Max(1, Width);
  5654. Header.dwHeight := Max(1, Height);
  5655. // Caps
  5656. Header.Caps.dwCaps1 := DDSCAPS_TEXTURE;
  5657. // Pixelformat
  5658. Header.PixelFormat.dwSize := sizeof(Header);
  5659. if (FormatDesc.IsCompressed) then begin
  5660. Header.PixelFormat.dwFlags := Header.PixelFormat.dwFlags or DDPF_FOURCC;
  5661. case Format of
  5662. tfS3tcDtx1RGBA: Header.PixelFormat.dwFourCC := D3DFMT_DXT1;
  5663. tfS3tcDtx3RGBA: Header.PixelFormat.dwFourCC := D3DFMT_DXT3;
  5664. tfS3tcDtx5RGBA: Header.PixelFormat.dwFourCC := D3DFMT_DXT5;
  5665. end;
  5666. end else if not FormatDesc.HasColor and FormatDesc.HasAlpha then begin
  5667. Header.PixelFormat.dwFlags := Header.PixelFormat.dwFlags or DDPF_ALPHA;
  5668. Header.PixelFormat.dwRGBBitCount := FormatDesc.BitsPerPixel;
  5669. Header.PixelFormat.dwABitMask := FormatDesc.Mask.a;
  5670. end else if FormatDesc.IsGrayscale then begin
  5671. Header.PixelFormat.dwFlags := Header.PixelFormat.dwFlags or DDPF_LUMINANCE;
  5672. Header.PixelFormat.dwRGBBitCount := FormatDesc.BitsPerPixel;
  5673. Header.PixelFormat.dwRBitMask := FormatDesc.Mask.r;
  5674. Header.PixelFormat.dwABitMask := FormatDesc.Mask.a;
  5675. end else begin
  5676. Header.PixelFormat.dwFlags := Header.PixelFormat.dwFlags or DDPF_RGB;
  5677. Header.PixelFormat.dwRGBBitCount := FormatDesc.BitsPerPixel;
  5678. Header.PixelFormat.dwRBitMask := FormatDesc.Mask.r;
  5679. Header.PixelFormat.dwGBitMask := FormatDesc.Mask.g;
  5680. Header.PixelFormat.dwBBitMask := FormatDesc.Mask.b;
  5681. Header.PixelFormat.dwABitMask := FormatDesc.Mask.a;
  5682. end;
  5683. if (FormatDesc.HasAlpha) then
  5684. Header.PixelFormat.dwFlags := Header.PixelFormat.dwFlags or DDPF_ALPHAPIXELS;
  5685. aStream.Write(DDS_MAGIC, sizeof(DDS_MAGIC));
  5686. aStream.Write(Header, SizeOf(Header));
  5687. aStream.Write(Data^, FormatDesc.GetSize(Dimension));
  5688. end;
  5689. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5690. function TglBitmapData.FlipHorz: Boolean;
  5691. var
  5692. fd: TglBitmapFormatDescriptor;
  5693. Col, RowSize, PixelSize: Integer;
  5694. pTempDest, pDest, pSource: PByte;
  5695. begin
  5696. result := false;
  5697. fd := FormatDescriptor;
  5698. PixelSize := Ceil(fd.BytesPerPixel);
  5699. RowSize := fd.GetSize(Width, 1);
  5700. if Assigned(Data) and not fd.IsCompressed then begin
  5701. pSource := Data;
  5702. GetMem(pDest, RowSize);
  5703. try
  5704. pTempDest := pDest;
  5705. Inc(pTempDest, RowSize);
  5706. for Col := 0 to Width-1 do begin
  5707. dec(pTempDest, PixelSize); //dec before, because ptr is behind last byte of data
  5708. Move(pSource^, pTempDest^, PixelSize);
  5709. Inc(pSource, PixelSize);
  5710. end;
  5711. SetData(pDest, Format, Width);
  5712. result := true;
  5713. except
  5714. if Assigned(pDest) then
  5715. FreeMem(pDest);
  5716. raise;
  5717. end;
  5718. end;
  5719. end;
  5720. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5721. function TglBitmapData.FlipVert: Boolean;
  5722. var
  5723. fd: TglBitmapFormatDescriptor;
  5724. Row, RowSize, PixelSize: Integer;
  5725. TempDestData, DestData, SourceData: PByte;
  5726. begin
  5727. result := false;
  5728. fd := FormatDescriptor;
  5729. PixelSize := Ceil(fd.BytesPerPixel);
  5730. RowSize := fd.GetSize(Width, 1);
  5731. if Assigned(Data) then begin
  5732. SourceData := Data;
  5733. GetMem(DestData, Height * RowSize);
  5734. try
  5735. TempDestData := DestData;
  5736. Inc(TempDestData, Width * (Height -1) * PixelSize);
  5737. for Row := 0 to Height -1 do begin
  5738. Move(SourceData^, TempDestData^, RowSize);
  5739. Dec(TempDestData, RowSize);
  5740. Inc(SourceData, RowSize);
  5741. end;
  5742. SetData(DestData, Format, Width, Height);
  5743. result := true;
  5744. except
  5745. if Assigned(DestData) then
  5746. FreeMem(DestData);
  5747. raise;
  5748. end;
  5749. end;
  5750. end;
  5751. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5752. procedure TglBitmapData.LoadFromFile(const aFilename: String);
  5753. var
  5754. fs: TFileStream;
  5755. begin
  5756. if not FileExists(aFilename) then
  5757. raise EglBitmap.Create('file does not exist: ' + aFilename);
  5758. fs := TFileStream.Create(aFilename, fmOpenRead);
  5759. try
  5760. fs.Position := 0;
  5761. LoadFromStream(fs);
  5762. fFilename := aFilename;
  5763. finally
  5764. fs.Free;
  5765. end;
  5766. end;
  5767. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5768. procedure TglBitmapData.LoadFromStream(const aStream: TStream);
  5769. begin
  5770. {$IFDEF GLB_SUPPORT_PNG_READ}
  5771. if not LoadPNG(aStream) then
  5772. {$ENDIF}
  5773. {$IFDEF GLB_SUPPORT_JPEG_READ}
  5774. if not LoadJPEG(aStream) then
  5775. {$ENDIF}
  5776. if not LoadDDS(aStream) then
  5777. if not LoadTGA(aStream) then
  5778. if not LoadBMP(aStream) then
  5779. if not LoadRAW(aStream) then
  5780. raise EglBitmap.Create('LoadFromStream - Couldn''t load Stream. It''s possible to be an unknow Streamtype.');
  5781. end;
  5782. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5783. procedure TglBitmapData.LoadFromFunc(const aSize: TglBitmapSize; const aFormat: TglBitmapFormat;
  5784. const aFunc: TglBitmapFunction; const aArgs: Pointer);
  5785. var
  5786. tmpData: PByte;
  5787. size: Integer;
  5788. begin
  5789. size := TFormatDescriptor.Get(aFormat).GetSize(aSize);
  5790. GetMem(tmpData, size);
  5791. try
  5792. FillChar(tmpData^, size, #$FF);
  5793. SetData(tmpData, aFormat, aSize.X, aSize.Y);
  5794. except
  5795. if Assigned(tmpData) then
  5796. FreeMem(tmpData);
  5797. raise;
  5798. end;
  5799. Convert(Self, aFunc, false, aFormat, aArgs);
  5800. end;
  5801. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5802. procedure TglBitmapData.LoadFromResource(const aInstance: Cardinal; aResource: String; aResType: PChar);
  5803. var
  5804. rs: TResourceStream;
  5805. begin
  5806. PrepareResType(aResource, aResType);
  5807. rs := TResourceStream.Create(aInstance, aResource, aResType);
  5808. try
  5809. LoadFromStream(rs);
  5810. finally
  5811. rs.Free;
  5812. end;
  5813. end;
  5814. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5815. procedure TglBitmapData.LoadFromResourceID(const aInstance: Cardinal; const aResourceID: Integer; const aResType: PChar);
  5816. var
  5817. rs: TResourceStream;
  5818. begin
  5819. rs := TResourceStream.CreateFromID(aInstance, aResourceID, aResType);
  5820. try
  5821. LoadFromStream(rs);
  5822. finally
  5823. rs.Free;
  5824. end;
  5825. end;
  5826. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5827. procedure TglBitmapData.SaveToFile(const aFilename: String; const aFileType: TglBitmapFileType);
  5828. var
  5829. fs: TFileStream;
  5830. begin
  5831. fs := TFileStream.Create(aFileName, fmCreate);
  5832. try
  5833. fs.Position := 0;
  5834. SaveToStream(fs, aFileType);
  5835. finally
  5836. fs.Free;
  5837. end;
  5838. end;
  5839. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5840. procedure TglBitmapData.SaveToStream(const aStream: TStream; const aFileType: TglBitmapFileType);
  5841. begin
  5842. case aFileType of
  5843. {$IFDEF GLB_SUPPORT_PNG_WRITE}
  5844. ftPNG: SavePNG(aStream);
  5845. {$ENDIF}
  5846. {$IFDEF GLB_SUPPORT_JPEG_WRITE}
  5847. ftJPEG: SaveJPEG(aStream);
  5848. {$ENDIF}
  5849. ftDDS: SaveDDS(aStream);
  5850. ftTGA: SaveTGA(aStream);
  5851. ftBMP: SaveBMP(aStream);
  5852. ftRAW: SaveRAW(aStream);
  5853. end;
  5854. end;
  5855. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5856. function TglBitmapData.Convert(const aFunc: TglBitmapFunction; const aCreateTemp: Boolean; const aArgs: Pointer): Boolean;
  5857. begin
  5858. result := Convert(Self, aFunc, aCreateTemp, Format, aArgs);
  5859. end;
  5860. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5861. function TglBitmapData.Convert(const aSource: TglBitmapData; const aFunc: TglBitmapFunction; aCreateTemp: Boolean;
  5862. const aFormat: TglBitmapFormat; const aArgs: Pointer): Boolean;
  5863. var
  5864. DestData, TmpData, SourceData: pByte;
  5865. TempHeight, TempWidth: Integer;
  5866. SourceFD, DestFD: TFormatDescriptor;
  5867. SourceMD, DestMD: Pointer;
  5868. FuncRec: TglBitmapFunctionRec;
  5869. begin
  5870. Assert(Assigned(Data));
  5871. Assert(Assigned(aSource));
  5872. Assert(Assigned(aSource.Data));
  5873. result := false;
  5874. if Assigned(aSource.Data) and ((aSource.Height > 0) or (aSource.Width > 0)) then begin
  5875. SourceFD := TFormatDescriptor.Get(aSource.Format);
  5876. DestFD := TFormatDescriptor.Get(aFormat);
  5877. if (SourceFD.IsCompressed) then
  5878. raise EglBitmapUnsupportedFormat.Create('compressed formats are not supported: ', SourceFD.Format);
  5879. if (DestFD.IsCompressed) then
  5880. raise EglBitmapUnsupportedFormat.Create('compressed formats are not supported: ', DestFD.Format);
  5881. // inkompatible Formats so CreateTemp
  5882. if (SourceFD.BitsPerPixel <> DestFD.BitsPerPixel) then
  5883. aCreateTemp := true;
  5884. // Values
  5885. TempHeight := Max(1, aSource.Height);
  5886. TempWidth := Max(1, aSource.Width);
  5887. FuncRec.Sender := Self;
  5888. FuncRec.Args := aArgs;
  5889. TmpData := nil;
  5890. if aCreateTemp then begin
  5891. GetMem(TmpData, DestFD.GetSize(TempWidth, TempHeight));
  5892. DestData := TmpData;
  5893. end else
  5894. DestData := Data;
  5895. try
  5896. SourceFD.PreparePixel(FuncRec.Source);
  5897. DestFD.PreparePixel (FuncRec.Dest);
  5898. SourceMD := SourceFD.CreateMappingData;
  5899. DestMD := DestFD.CreateMappingData;
  5900. FuncRec.Size := aSource.Dimension;
  5901. FuncRec.Position.Fields := FuncRec.Size.Fields;
  5902. try
  5903. SourceData := aSource.Data;
  5904. FuncRec.Position.Y := 0;
  5905. while FuncRec.Position.Y < TempHeight do begin
  5906. FuncRec.Position.X := 0;
  5907. while FuncRec.Position.X < TempWidth do begin
  5908. SourceFD.Unmap(SourceData, FuncRec.Source, SourceMD);
  5909. aFunc(FuncRec);
  5910. DestFD.Map(FuncRec.Dest, DestData, DestMD);
  5911. inc(FuncRec.Position.X);
  5912. end;
  5913. inc(FuncRec.Position.Y);
  5914. end;
  5915. // Updating Image or InternalFormat
  5916. if aCreateTemp then
  5917. SetData(TmpData, aFormat, aSource.Width, aSource.Height)
  5918. else if (aFormat <> fFormat) then
  5919. Format := aFormat;
  5920. result := true;
  5921. finally
  5922. SourceFD.FreeMappingData(SourceMD);
  5923. DestFD.FreeMappingData(DestMD);
  5924. end;
  5925. except
  5926. if aCreateTemp and Assigned(TmpData) then
  5927. FreeMem(TmpData);
  5928. raise;
  5929. end;
  5930. end;
  5931. end;
  5932. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5933. function TglBitmapData.ConvertTo(const aFormat: TglBitmapFormat): Boolean;
  5934. var
  5935. SourceFD, DestFD: TFormatDescriptor;
  5936. SourcePD, DestPD: TglBitmapPixelData;
  5937. ShiftData: TShiftData;
  5938. function DataIsIdentical: Boolean;
  5939. begin
  5940. result := SourceFD.MaskMatch(DestFD.Mask);
  5941. end;
  5942. function CanCopyDirect: Boolean;
  5943. begin
  5944. result :=
  5945. ((SourcePD.Range.r = DestPD.Range.r) or (SourcePD.Range.r = 0) or (DestPD.Range.r = 0)) and
  5946. ((SourcePD.Range.g = DestPD.Range.g) or (SourcePD.Range.g = 0) or (DestPD.Range.g = 0)) and
  5947. ((SourcePD.Range.b = DestPD.Range.b) or (SourcePD.Range.b = 0) or (DestPD.Range.b = 0)) and
  5948. ((SourcePD.Range.a = DestPD.Range.a) or (SourcePD.Range.a = 0) or (DestPD.Range.a = 0));
  5949. end;
  5950. function CanShift: Boolean;
  5951. begin
  5952. result :=
  5953. ((SourcePD.Range.r >= DestPD.Range.r) or (SourcePD.Range.r = 0) or (DestPD.Range.r = 0)) and
  5954. ((SourcePD.Range.g >= DestPD.Range.g) or (SourcePD.Range.g = 0) or (DestPD.Range.g = 0)) and
  5955. ((SourcePD.Range.b >= DestPD.Range.b) or (SourcePD.Range.b = 0) or (DestPD.Range.b = 0)) and
  5956. ((SourcePD.Range.a >= DestPD.Range.a) or (SourcePD.Range.a = 0) or (DestPD.Range.a = 0));
  5957. end;
  5958. function GetShift(aSource, aDest: Cardinal) : ShortInt;
  5959. begin
  5960. result := 0;
  5961. while (aSource > aDest) and (aSource > 0) do begin
  5962. inc(result);
  5963. aSource := aSource shr 1;
  5964. end;
  5965. end;
  5966. begin
  5967. if (aFormat <> fFormat) and (aFormat <> tfEmpty) then begin
  5968. SourceFD := TFormatDescriptor.Get(Format);
  5969. DestFD := TFormatDescriptor.Get(aFormat);
  5970. if DataIsIdentical then begin
  5971. result := true;
  5972. Format := aFormat;
  5973. exit;
  5974. end;
  5975. SourceFD.PreparePixel(SourcePD);
  5976. DestFD.PreparePixel (DestPD);
  5977. if CanCopyDirect then
  5978. result := Convert(Self, glBitmapConvertCopyFunc, false, aFormat)
  5979. else if CanShift then begin
  5980. ShiftData.r := GetShift(SourcePD.Range.r, DestPD.Range.r);
  5981. ShiftData.g := GetShift(SourcePD.Range.g, DestPD.Range.g);
  5982. ShiftData.b := GetShift(SourcePD.Range.b, DestPD.Range.b);
  5983. ShiftData.a := GetShift(SourcePD.Range.a, DestPD.Range.a);
  5984. result := Convert(Self, glBitmapConvertShiftRGBAFunc, false, aFormat, @ShiftData);
  5985. end else
  5986. result := Convert(Self, glBitmapConvertCalculateRGBAFunc, false, aFormat);
  5987. end else
  5988. result := true;
  5989. end;
  5990. {$IFDEF GLB_SDL}
  5991. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5992. function TglBitmapData.AssignToSurface(out aSurface: PSDL_Surface): Boolean;
  5993. var
  5994. Row, RowSize: Integer;
  5995. SourceData, TmpData: PByte;
  5996. TempDepth: Integer;
  5997. FormatDesc: TFormatDescriptor;
  5998. function GetRowPointer(Row: Integer): pByte;
  5999. begin
  6000. result := aSurface.pixels;
  6001. Inc(result, Row * RowSize);
  6002. end;
  6003. begin
  6004. result := false;
  6005. FormatDesc := TFormatDescriptor.Get(Format);
  6006. if FormatDesc.IsCompressed then
  6007. raise EglBitmapUnsupportedFormat.Create(Format);
  6008. if Assigned(Data) then begin
  6009. case Trunc(FormatDesc.PixelSize) of
  6010. 1: TempDepth := 8;
  6011. 2: TempDepth := 16;
  6012. 3: TempDepth := 24;
  6013. 4: TempDepth := 32;
  6014. else
  6015. raise EglBitmapUnsupportedFormat.Create(Format);
  6016. end;
  6017. aSurface := SDL_CreateRGBSurface(SDL_SWSURFACE, Width, Height, TempDepth,
  6018. FormatDesc.RedMask, FormatDesc.GreenMask, FormatDesc.BlueMask, FormatDesc.AlphaMask);
  6019. SourceData := Data;
  6020. RowSize := FormatDesc.GetSize(FileWidth, 1);
  6021. for Row := 0 to FileHeight-1 do begin
  6022. TmpData := GetRowPointer(Row);
  6023. if Assigned(TmpData) then begin
  6024. Move(SourceData^, TmpData^, RowSize);
  6025. inc(SourceData, RowSize);
  6026. end;
  6027. end;
  6028. result := true;
  6029. end;
  6030. end;
  6031. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6032. function TglBitmapData.AssignFromSurface(const aSurface: PSDL_Surface): Boolean;
  6033. var
  6034. pSource, pData, pTempData: PByte;
  6035. Row, RowSize, TempWidth, TempHeight: Integer;
  6036. IntFormat: TglBitmapFormat;
  6037. fd: TFormatDescriptor;
  6038. Mask: TglBitmapMask;
  6039. function GetRowPointer(Row: Integer): pByte;
  6040. begin
  6041. result := aSurface^.pixels;
  6042. Inc(result, Row * RowSize);
  6043. end;
  6044. begin
  6045. result := false;
  6046. if (Assigned(aSurface)) then begin
  6047. with aSurface^.format^ do begin
  6048. Mask.r := RMask;
  6049. Mask.g := GMask;
  6050. Mask.b := BMask;
  6051. Mask.a := AMask;
  6052. IntFormat := TFormatDescriptor.GetFromMask(Mask).Format;
  6053. if (IntFormat = tfEmpty) then
  6054. raise EglBitmap.Create('AssignFromSurface - Invalid Pixelformat.');
  6055. end;
  6056. fd := TFormatDescriptor.Get(IntFormat);
  6057. TempWidth := aSurface^.w;
  6058. TempHeight := aSurface^.h;
  6059. RowSize := fd.GetSize(TempWidth, 1);
  6060. GetMem(pData, TempHeight * RowSize);
  6061. try
  6062. pTempData := pData;
  6063. for Row := 0 to TempHeight -1 do begin
  6064. pSource := GetRowPointer(Row);
  6065. if (Assigned(pSource)) then begin
  6066. Move(pSource^, pTempData^, RowSize);
  6067. Inc(pTempData, RowSize);
  6068. end;
  6069. end;
  6070. SetData(pData, IntFormat, TempWidth, TempHeight);
  6071. result := true;
  6072. except
  6073. if Assigned(pData) then
  6074. FreeMem(pData);
  6075. raise;
  6076. end;
  6077. end;
  6078. end;
  6079. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6080. function TglBitmapData.AssignAlphaToSurface(out aSurface: PSDL_Surface): Boolean;
  6081. var
  6082. Row, Col, AlphaInterleave: Integer;
  6083. pSource, pDest: PByte;
  6084. function GetRowPointer(Row: Integer): pByte;
  6085. begin
  6086. result := aSurface.pixels;
  6087. Inc(result, Row * Width);
  6088. end;
  6089. begin
  6090. result := false;
  6091. if Assigned(Data) then begin
  6092. if Format in [tfAlpha8ub1, tfLuminance8Alpha8ub2, tfBGRA8ub4, tfRGBA8ub4] then begin
  6093. aSurface := SDL_CreateRGBSurface(SDL_SWSURFACE, Width, Height, 8, $FF, $FF, $FF, 0);
  6094. AlphaInterleave := 0;
  6095. case Format of
  6096. tfLuminance8Alpha8ub2:
  6097. AlphaInterleave := 1;
  6098. tfBGRA8ub4, tfRGBA8ub4:
  6099. AlphaInterleave := 3;
  6100. end;
  6101. pSource := Data;
  6102. for Row := 0 to Height -1 do begin
  6103. pDest := GetRowPointer(Row);
  6104. if Assigned(pDest) then begin
  6105. for Col := 0 to Width -1 do begin
  6106. Inc(pSource, AlphaInterleave);
  6107. pDest^ := pSource^;
  6108. Inc(pDest);
  6109. Inc(pSource);
  6110. end;
  6111. end;
  6112. end;
  6113. result := true;
  6114. end;
  6115. end;
  6116. end;
  6117. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6118. function TglBitmapData.AddAlphaFromSurface(const aSurface: PSDL_Surface; const aFunc: TglBitmapFunction = nil; const aArgs: Pointer = nil): Boolean;
  6119. var
  6120. bmp: TglBitmap2D;
  6121. begin
  6122. bmp := TglBitmap2D.Create;
  6123. try
  6124. bmp.AssignFromSurface(aSurface);
  6125. result := AddAlphaFromGlBitmap(bmp, aFunc, aArgs);
  6126. finally
  6127. bmp.Free;
  6128. end;
  6129. end;
  6130. {$ENDIF}
  6131. {$IFDEF GLB_DELPHI}
  6132. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6133. function CreateGrayPalette: HPALETTE;
  6134. var
  6135. Idx: Integer;
  6136. Pal: PLogPalette;
  6137. begin
  6138. GetMem(Pal, SizeOf(TLogPalette) + (SizeOf(TPaletteEntry) * 256));
  6139. Pal.palVersion := $300;
  6140. Pal.palNumEntries := 256;
  6141. for Idx := 0 to Pal.palNumEntries - 1 do begin
  6142. Pal.palPalEntry[Idx].peRed := Idx;
  6143. Pal.palPalEntry[Idx].peGreen := Idx;
  6144. Pal.palPalEntry[Idx].peBlue := Idx;
  6145. Pal.palPalEntry[Idx].peFlags := 0;
  6146. end;
  6147. Result := CreatePalette(Pal^);
  6148. FreeMem(Pal);
  6149. end;
  6150. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6151. function TglBitmapData.AssignToBitmap(const aBitmap: TBitmap): Boolean;
  6152. var
  6153. Row, RowSize: Integer;
  6154. pSource, pData: PByte;
  6155. begin
  6156. result := false;
  6157. if Assigned(Data) then begin
  6158. if Assigned(aBitmap) then begin
  6159. aBitmap.Width := Width;
  6160. aBitmap.Height := Height;
  6161. case Format of
  6162. tfAlpha8ub1, tfLuminance8ub1: begin
  6163. aBitmap.PixelFormat := pf8bit;
  6164. aBitmap.Palette := CreateGrayPalette;
  6165. end;
  6166. tfRGB5A1us1:
  6167. aBitmap.PixelFormat := pf15bit;
  6168. tfR5G6B5us1:
  6169. aBitmap.PixelFormat := pf16bit;
  6170. tfRGB8ub3, tfBGR8ub3:
  6171. aBitmap.PixelFormat := pf24bit;
  6172. tfRGBA8ub4, tfBGRA8ub4:
  6173. aBitmap.PixelFormat := pf32bit;
  6174. else
  6175. raise EglBitmap.Create('AssignToBitmap - Invalid Pixelformat.');
  6176. end;
  6177. RowSize := FormatDescriptor.GetSize(Width, 1);
  6178. pSource := Data;
  6179. for Row := 0 to Height-1 do begin
  6180. pData := aBitmap.Scanline[Row];
  6181. Move(pSource^, pData^, RowSize);
  6182. Inc(pSource, RowSize);
  6183. if (Format in [tfRGB8ub3, tfRGBA8ub4]) then // swap RGB(A) to BGR(A)
  6184. SwapRGB(pData, Width, Format = tfRGBA8ub4);
  6185. end;
  6186. result := true;
  6187. end;
  6188. end;
  6189. end;
  6190. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6191. function TglBitmapData.AssignFromBitmap(const aBitmap: TBitmap): Boolean;
  6192. var
  6193. pSource, pData, pTempData: PByte;
  6194. Row, RowSize, TempWidth, TempHeight: Integer;
  6195. IntFormat: TglBitmapFormat;
  6196. begin
  6197. result := false;
  6198. if (Assigned(aBitmap)) then begin
  6199. case aBitmap.PixelFormat of
  6200. pf8bit:
  6201. IntFormat := tfLuminance8ub1;
  6202. pf15bit:
  6203. IntFormat := tfRGB5A1us1;
  6204. pf16bit:
  6205. IntFormat := tfR5G6B5us1;
  6206. pf24bit:
  6207. IntFormat := tfBGR8ub3;
  6208. pf32bit:
  6209. IntFormat := tfBGRA8ub4;
  6210. else
  6211. raise EglBitmap.Create('AssignFromBitmap - Invalid Pixelformat.');
  6212. end;
  6213. TempWidth := aBitmap.Width;
  6214. TempHeight := aBitmap.Height;
  6215. RowSize := TFormatDescriptor.Get(IntFormat).GetSize(TempWidth, 1);
  6216. GetMem(pData, TempHeight * RowSize);
  6217. try
  6218. pTempData := pData;
  6219. for Row := 0 to TempHeight -1 do begin
  6220. pSource := aBitmap.Scanline[Row];
  6221. if (Assigned(pSource)) then begin
  6222. Move(pSource^, pTempData^, RowSize);
  6223. Inc(pTempData, RowSize);
  6224. end;
  6225. end;
  6226. SetData(pData, IntFormat, TempWidth, TempHeight);
  6227. result := true;
  6228. except
  6229. if Assigned(pData) then
  6230. FreeMem(pData);
  6231. raise;
  6232. end;
  6233. end;
  6234. end;
  6235. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6236. function TglBitmapData.AssignAlphaToBitmap(const aBitmap: TBitmap): Boolean;
  6237. var
  6238. Row, Col, AlphaInterleave: Integer;
  6239. pSource, pDest: PByte;
  6240. begin
  6241. result := false;
  6242. if Assigned(Data) then begin
  6243. if (Format in [tfAlpha8ub1, tfLuminance8Alpha8ub2, tfRGBA8ub4, tfBGRA8ub4]) then begin
  6244. if Assigned(aBitmap) then begin
  6245. aBitmap.PixelFormat := pf8bit;
  6246. aBitmap.Palette := CreateGrayPalette;
  6247. aBitmap.Width := Width;
  6248. aBitmap.Height := Height;
  6249. case Format of
  6250. tfLuminance8Alpha8ub2:
  6251. AlphaInterleave := 1;
  6252. tfRGBA8ub4, tfBGRA8ub4:
  6253. AlphaInterleave := 3;
  6254. else
  6255. AlphaInterleave := 0;
  6256. end;
  6257. // Copy Data
  6258. pSource := Data;
  6259. for Row := 0 to Height -1 do begin
  6260. pDest := aBitmap.Scanline[Row];
  6261. if Assigned(pDest) then begin
  6262. for Col := 0 to Width -1 do begin
  6263. Inc(pSource, AlphaInterleave);
  6264. pDest^ := pSource^;
  6265. Inc(pDest);
  6266. Inc(pSource);
  6267. end;
  6268. end;
  6269. end;
  6270. result := true;
  6271. end;
  6272. end;
  6273. end;
  6274. end;
  6275. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6276. function TglBitmapData.AddAlphaFromBitmap(const aBitmap: TBitmap; const aFunc: TglBitmapFunction; const aArgs: Pointer): Boolean;
  6277. var
  6278. data: TglBitmapData;
  6279. begin
  6280. data := TglBitmapData.Create;
  6281. try
  6282. data.AssignFromBitmap(aBitmap);
  6283. result := AddAlphaFromDataObj(data, aFunc, aArgs);
  6284. finally
  6285. data.Free;
  6286. end;
  6287. end;
  6288. {$ENDIF}
  6289. {$IFDEF GLB_LAZARUS}
  6290. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6291. function TglBitmapData.AssignToLazIntfImage(const aImage: TLazIntfImage): Boolean;
  6292. var
  6293. rid: TRawImageDescription;
  6294. FormatDesc: TFormatDescriptor;
  6295. begin
  6296. if not Assigned(Data) then
  6297. raise EglBitmap.Create('no pixel data assigned. load data before save');
  6298. result := false;
  6299. if not Assigned(aImage) or (Format = tfEmpty) then
  6300. exit;
  6301. FormatDesc := TFormatDescriptor.Get(Format);
  6302. if FormatDesc.IsCompressed then
  6303. exit;
  6304. FillChar(rid{%H-}, SizeOf(rid), 0);
  6305. if FormatDesc.IsGrayscale then
  6306. rid.Format := ricfGray
  6307. else
  6308. rid.Format := ricfRGBA;
  6309. rid.Width := Width;
  6310. rid.Height := Height;
  6311. rid.Depth := FormatDesc.BitsPerPixel;
  6312. rid.BitOrder := riboBitsInOrder;
  6313. rid.ByteOrder := riboLSBFirst;
  6314. rid.LineOrder := riloTopToBottom;
  6315. rid.LineEnd := rileTight;
  6316. rid.BitsPerPixel := FormatDesc.BitsPerPixel;
  6317. rid.RedPrec := CountSetBits(FormatDesc.Range.r);
  6318. rid.GreenPrec := CountSetBits(FormatDesc.Range.g);
  6319. rid.BluePrec := CountSetBits(FormatDesc.Range.b);
  6320. rid.AlphaPrec := CountSetBits(FormatDesc.Range.a);
  6321. rid.RedShift := FormatDesc.Shift.r;
  6322. rid.GreenShift := FormatDesc.Shift.g;
  6323. rid.BlueShift := FormatDesc.Shift.b;
  6324. rid.AlphaShift := FormatDesc.Shift.a;
  6325. rid.MaskBitsPerPixel := 0;
  6326. rid.PaletteColorCount := 0;
  6327. aImage.DataDescription := rid;
  6328. aImage.CreateData;
  6329. if not Assigned(aImage.PixelData) then
  6330. raise EglBitmap.Create('error while creating LazIntfImage');
  6331. Move(Data^, aImage.PixelData^, FormatDesc.GetSize(Dimension));
  6332. result := true;
  6333. end;
  6334. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6335. function TglBitmapData.AssignFromLazIntfImage(const aImage: TLazIntfImage): Boolean;
  6336. var
  6337. f: TglBitmapFormat;
  6338. FormatDesc: TFormatDescriptor;
  6339. ImageData: PByte;
  6340. ImageSize: Integer;
  6341. CanCopy: Boolean;
  6342. Mask: TglBitmapRec4ul;
  6343. procedure CopyConvert;
  6344. var
  6345. bfFormat: TbmpBitfieldFormat;
  6346. pSourceLine, pDestLine: PByte;
  6347. pSourceMD, pDestMD: Pointer;
  6348. Shift, Prec: TglBitmapRec4ub;
  6349. x, y: Integer;
  6350. pixel: TglBitmapPixelData;
  6351. begin
  6352. bfFormat := TbmpBitfieldFormat.Create;
  6353. with aImage.DataDescription do begin
  6354. Prec.r := RedPrec;
  6355. Prec.g := GreenPrec;
  6356. Prec.b := BluePrec;
  6357. Prec.a := AlphaPrec;
  6358. Shift.r := RedShift;
  6359. Shift.g := GreenShift;
  6360. Shift.b := BlueShift;
  6361. Shift.a := AlphaShift;
  6362. bfFormat.SetCustomValues(BitsPerPixel, Prec, Shift);
  6363. end;
  6364. pSourceMD := bfFormat.CreateMappingData;
  6365. pDestMD := FormatDesc.CreateMappingData;
  6366. try
  6367. for y := 0 to aImage.Height-1 do begin
  6368. pSourceLine := aImage.PixelData + y {%H-}* aImage.DataDescription.BytesPerLine;
  6369. pDestLine := ImageData + y * Round(FormatDesc.BytesPerPixel * aImage.Width);
  6370. for x := 0 to aImage.Width-1 do begin
  6371. bfFormat.Unmap(pSourceLine, pixel, pSourceMD);
  6372. FormatDesc.Map(pixel, pDestLine, pDestMD);
  6373. end;
  6374. end;
  6375. finally
  6376. FormatDesc.FreeMappingData(pDestMD);
  6377. bfFormat.FreeMappingData(pSourceMD);
  6378. bfFormat.Free;
  6379. end;
  6380. end;
  6381. begin
  6382. result := false;
  6383. if not Assigned(aImage) then
  6384. exit;
  6385. with aImage.DataDescription do begin
  6386. Mask.r := (QWord(1 shl RedPrec )-1) shl RedShift;
  6387. Mask.g := (QWord(1 shl GreenPrec)-1) shl GreenShift;
  6388. Mask.b := (QWord(1 shl BluePrec )-1) shl BlueShift;
  6389. Mask.a := (QWord(1 shl AlphaPrec)-1) shl AlphaShift;
  6390. end;
  6391. FormatDesc := TFormatDescriptor.GetFromMask(Mask);
  6392. f := FormatDesc.Format;
  6393. if (f = tfEmpty) then
  6394. exit;
  6395. CanCopy :=
  6396. (FormatDesc.BitsPerPixel = aImage.DataDescription.Depth) and
  6397. (aImage.DataDescription.BitsPerPixel = aImage.DataDescription.Depth);
  6398. ImageSize := FormatDesc.GetSize(aImage.Width, aImage.Height);
  6399. ImageData := GetMem(ImageSize);
  6400. try
  6401. if CanCopy then
  6402. Move(aImage.PixelData^, ImageData^, ImageSize)
  6403. else
  6404. CopyConvert;
  6405. SetData(ImageData, f, aImage.Width, aImage.Height);
  6406. except
  6407. if Assigned(ImageData) then
  6408. FreeMem(ImageData);
  6409. raise;
  6410. end;
  6411. result := true;
  6412. end;
  6413. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6414. function TglBitmapData.AssignAlphaToLazIntfImage(const aImage: TLazIntfImage): Boolean;
  6415. var
  6416. rid: TRawImageDescription;
  6417. FormatDesc: TFormatDescriptor;
  6418. Pixel: TglBitmapPixelData;
  6419. x, y: Integer;
  6420. srcMD: Pointer;
  6421. src, dst: PByte;
  6422. begin
  6423. result := false;
  6424. if not Assigned(aImage) or (Format = tfEmpty) then
  6425. exit;
  6426. FormatDesc := TFormatDescriptor.Get(Format);
  6427. if FormatDesc.IsCompressed or not FormatDesc.HasAlpha then
  6428. exit;
  6429. FillChar(rid{%H-}, SizeOf(rid), 0);
  6430. rid.Format := ricfGray;
  6431. rid.Width := Width;
  6432. rid.Height := Height;
  6433. rid.Depth := CountSetBits(FormatDesc.Range.a);
  6434. rid.BitOrder := riboBitsInOrder;
  6435. rid.ByteOrder := riboLSBFirst;
  6436. rid.LineOrder := riloTopToBottom;
  6437. rid.LineEnd := rileTight;
  6438. rid.BitsPerPixel := 8 * Ceil(rid.Depth / 8);
  6439. rid.RedPrec := CountSetBits(FormatDesc.Range.a);
  6440. rid.GreenPrec := 0;
  6441. rid.BluePrec := 0;
  6442. rid.AlphaPrec := 0;
  6443. rid.RedShift := 0;
  6444. rid.GreenShift := 0;
  6445. rid.BlueShift := 0;
  6446. rid.AlphaShift := 0;
  6447. rid.MaskBitsPerPixel := 0;
  6448. rid.PaletteColorCount := 0;
  6449. aImage.DataDescription := rid;
  6450. aImage.CreateData;
  6451. srcMD := FormatDesc.CreateMappingData;
  6452. try
  6453. FormatDesc.PreparePixel(Pixel);
  6454. src := Data;
  6455. dst := aImage.PixelData;
  6456. for y := 0 to Height-1 do
  6457. for x := 0 to Width-1 do begin
  6458. FormatDesc.Unmap(src, Pixel, srcMD);
  6459. case rid.BitsPerPixel of
  6460. 8: begin
  6461. dst^ := Pixel.Data.a;
  6462. inc(dst);
  6463. end;
  6464. 16: begin
  6465. PWord(dst)^ := Pixel.Data.a;
  6466. inc(dst, 2);
  6467. end;
  6468. 24: begin
  6469. PByteArray(dst)^[0] := PByteArray(@Pixel.Data.a)^[0];
  6470. PByteArray(dst)^[1] := PByteArray(@Pixel.Data.a)^[1];
  6471. PByteArray(dst)^[2] := PByteArray(@Pixel.Data.a)^[2];
  6472. inc(dst, 3);
  6473. end;
  6474. 32: begin
  6475. PCardinal(dst)^ := Pixel.Data.a;
  6476. inc(dst, 4);
  6477. end;
  6478. else
  6479. raise EglBitmapUnsupportedFormat.Create(Format);
  6480. end;
  6481. end;
  6482. finally
  6483. FormatDesc.FreeMappingData(srcMD);
  6484. end;
  6485. result := true;
  6486. end;
  6487. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6488. function TglBitmapData.AddAlphaFromLazIntfImage(const aImage: TLazIntfImage; const aFunc: TglBitmapFunction; const aArgs: Pointer): Boolean;
  6489. var
  6490. data: TglBitmapData;
  6491. begin
  6492. data := TglBitmapData.Create;
  6493. try
  6494. data.AssignFromLazIntfImage(aImage);
  6495. result := AddAlphaFromDataObj(data, aFunc, aArgs);
  6496. finally
  6497. data.Free;
  6498. end;
  6499. end;
  6500. {$ENDIF}
  6501. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6502. function TglBitmapData.AddAlphaFromResource(const aInstance: Cardinal; aResource: String; aResType: PChar;
  6503. const aFunc: TglBitmapFunction; const aArgs: Pointer): Boolean;
  6504. var
  6505. rs: TResourceStream;
  6506. begin
  6507. PrepareResType(aResource, aResType);
  6508. rs := TResourceStream.Create(aInstance, aResource, aResType);
  6509. try
  6510. result := AddAlphaFromStream(rs, aFunc, aArgs);
  6511. finally
  6512. rs.Free;
  6513. end;
  6514. end;
  6515. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6516. function TglBitmapData.AddAlphaFromResourceID(const aInstance: Cardinal; const aResourceID: Integer; const aResType: PChar;
  6517. const aFunc: TglBitmapFunction; const aArgs: Pointer): Boolean;
  6518. var
  6519. rs: TResourceStream;
  6520. begin
  6521. rs := TResourceStream.CreateFromID(aInstance, aResourceID, aResType);
  6522. try
  6523. result := AddAlphaFromStream(rs, aFunc, aArgs);
  6524. finally
  6525. rs.Free;
  6526. end;
  6527. end;
  6528. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6529. function TglBitmapData.AddAlphaFromFunc(const aFunc: TglBitmapFunction; const aArgs: Pointer): Boolean;
  6530. begin
  6531. if TFormatDescriptor.Get(Format).IsCompressed then
  6532. raise EglBitmapUnsupportedFormat.Create(Format);
  6533. result := Convert(Self, aFunc, false, TFormatDescriptor.Get(Format).WithAlpha, aArgs);
  6534. end;
  6535. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6536. function TglBitmapData.AddAlphaFromFile(const aFileName: String; const aFunc: TglBitmapFunction; const aArgs: Pointer): Boolean;
  6537. var
  6538. FS: TFileStream;
  6539. begin
  6540. FS := TFileStream.Create(aFileName, fmOpenRead);
  6541. try
  6542. result := AddAlphaFromStream(FS, aFunc, aArgs);
  6543. finally
  6544. FS.Free;
  6545. end;
  6546. end;
  6547. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6548. function TglBitmapData.AddAlphaFromStream(const aStream: TStream; const aFunc: TglBitmapFunction; const aArgs: Pointer): Boolean;
  6549. var
  6550. data: TglBitmapData;
  6551. begin
  6552. data := TglBitmapData.Create(aStream);
  6553. try
  6554. result := AddAlphaFromDataObj(data, aFunc, aArgs);
  6555. finally
  6556. data.Free;
  6557. end;
  6558. end;
  6559. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6560. function TglBitmapData.AddAlphaFromDataObj(const aDataObj: TglBitmapData; aFunc: TglBitmapFunction; const aArgs: Pointer): Boolean;
  6561. var
  6562. DestData, DestData2, SourceData: pByte;
  6563. TempHeight, TempWidth: Integer;
  6564. SourceFD, DestFD: TFormatDescriptor;
  6565. SourceMD, DestMD, DestMD2: Pointer;
  6566. FuncRec: TglBitmapFunctionRec;
  6567. begin
  6568. result := false;
  6569. Assert(Assigned(Data));
  6570. Assert(Assigned(aDataObj));
  6571. Assert(Assigned(aDataObj.Data));
  6572. if ((aDataObj.Width = Width) and (aDataObj.Height = Height)) then begin
  6573. result := ConvertTo(TFormatDescriptor.Get(Format).WithAlpha);
  6574. SourceFD := TFormatDescriptor.Get(aDataObj.Format);
  6575. DestFD := TFormatDescriptor.Get(Format);
  6576. if not Assigned(aFunc) then begin
  6577. aFunc := glBitmapAlphaFunc;
  6578. FuncRec.Args := {%H-}Pointer(SourceFD.HasAlpha);
  6579. end else
  6580. FuncRec.Args := aArgs;
  6581. // Values
  6582. TempWidth := aDataObj.Width;
  6583. TempHeight := aDataObj.Height;
  6584. if (TempWidth <= 0) or (TempHeight <= 0) then
  6585. exit;
  6586. FuncRec.Sender := Self;
  6587. FuncRec.Size := Dimension;
  6588. FuncRec.Position.Fields := FuncRec.Size.Fields;
  6589. DestData := Data;
  6590. DestData2 := Data;
  6591. SourceData := aDataObj.Data;
  6592. // Mapping
  6593. SourceFD.PreparePixel(FuncRec.Source);
  6594. DestFD.PreparePixel (FuncRec.Dest);
  6595. SourceMD := SourceFD.CreateMappingData;
  6596. DestMD := DestFD.CreateMappingData;
  6597. DestMD2 := DestFD.CreateMappingData;
  6598. try
  6599. FuncRec.Position.Y := 0;
  6600. while FuncRec.Position.Y < TempHeight do begin
  6601. FuncRec.Position.X := 0;
  6602. while FuncRec.Position.X < TempWidth do begin
  6603. SourceFD.Unmap(SourceData, FuncRec.Source, SourceMD);
  6604. DestFD.Unmap (DestData, FuncRec.Dest, DestMD);
  6605. aFunc(FuncRec);
  6606. DestFD.Map(FuncRec.Dest, DestData2, DestMD2);
  6607. inc(FuncRec.Position.X);
  6608. end;
  6609. inc(FuncRec.Position.Y);
  6610. end;
  6611. finally
  6612. SourceFD.FreeMappingData(SourceMD);
  6613. DestFD.FreeMappingData(DestMD);
  6614. DestFD.FreeMappingData(DestMD2);
  6615. end;
  6616. end;
  6617. end;
  6618. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6619. function TglBitmapData.AddAlphaFromColorKey(const aRed, aGreen, aBlue: Byte; const aDeviation: Byte): Boolean;
  6620. begin
  6621. result := AddAlphaFromColorKeyFloat(aRed / $FF, aGreen / $FF, aBlue / $FF, aDeviation / $FF);
  6622. end;
  6623. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6624. function TglBitmapData.AddAlphaFromColorKeyRange(const aRed, aGreen, aBlue: Cardinal; const aDeviation: Cardinal): Boolean;
  6625. var
  6626. PixelData: TglBitmapPixelData;
  6627. begin
  6628. TFormatDescriptor.GetAlpha(Format).PreparePixel(PixelData);
  6629. result := AddAlphaFromColorKeyFloat(
  6630. aRed / PixelData.Range.r,
  6631. aGreen / PixelData.Range.g,
  6632. aBlue / PixelData.Range.b,
  6633. aDeviation / Max(PixelData.Range.r, Max(PixelData.Range.g, PixelData.Range.b)));
  6634. end;
  6635. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6636. function TglBitmapData.AddAlphaFromColorKeyFloat(const aRed, aGreen, aBlue: Single; const aDeviation: Single): Boolean;
  6637. var
  6638. values: array[0..2] of Single;
  6639. tmp: Cardinal;
  6640. i: Integer;
  6641. PixelData: TglBitmapPixelData;
  6642. begin
  6643. TFormatDescriptor.GetAlpha(Format).PreparePixel(PixelData);
  6644. with PixelData do begin
  6645. values[0] := aRed;
  6646. values[1] := aGreen;
  6647. values[2] := aBlue;
  6648. for i := 0 to 2 do begin
  6649. tmp := Trunc(Range.arr[i] * aDeviation);
  6650. Data.arr[i] := Min(Range.arr[i], Trunc(Range.arr[i] * values[i] + tmp));
  6651. Range.arr[i] := Max(0, Trunc(Range.arr[i] * values[i] - tmp));
  6652. end;
  6653. Data.a := 0;
  6654. Range.a := 0;
  6655. end;
  6656. result := AddAlphaFromFunc(glBitmapColorKeyAlphaFunc, @PixelData);
  6657. end;
  6658. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6659. function TglBitmapData.AddAlphaFromValue(const aAlpha: Byte): Boolean;
  6660. begin
  6661. result := AddAlphaFromValueFloat(aAlpha / $FF);
  6662. end;
  6663. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6664. function TglBitmapData.AddAlphaFromValueRange(const aAlpha: Cardinal): Boolean;
  6665. var
  6666. PixelData: TglBitmapPixelData;
  6667. begin
  6668. TFormatDescriptor.GetAlpha(Format).PreparePixel(PixelData);
  6669. result := AddAlphaFromValueFloat(aAlpha / PixelData.Range.a);
  6670. end;
  6671. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6672. function TglBitmapData.AddAlphaFromValueFloat(const aAlpha: Single): Boolean;
  6673. var
  6674. PixelData: TglBitmapPixelData;
  6675. begin
  6676. TFormatDescriptor.GetAlpha(Format).PreparePixel(PixelData);
  6677. with PixelData do
  6678. Data.a := Min(Range.a, Max(0, Round(Range.a * aAlpha)));
  6679. result := AddAlphaFromFunc(glBitmapValueAlphaFunc, @PixelData.Data.a);
  6680. end;
  6681. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6682. function TglBitmapData.RemoveAlpha: Boolean;
  6683. var
  6684. FormatDesc: TFormatDescriptor;
  6685. begin
  6686. result := false;
  6687. FormatDesc := TFormatDescriptor.Get(Format);
  6688. if Assigned(Data) then begin
  6689. if FormatDesc.IsCompressed or not FormatDesc.HasAlpha then
  6690. raise EglBitmapUnsupportedFormat.Create(Format);
  6691. result := ConvertTo(FormatDesc.WithoutAlpha);
  6692. end;
  6693. end;
  6694. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6695. procedure TglBitmapData.FillWithColor(const aRed, aGreen, aBlue: Byte;
  6696. const aAlpha: Byte);
  6697. begin
  6698. FillWithColorFloat(aRed/$FF, aGreen/$FF, aBlue/$FF, aAlpha/$FF);
  6699. end;
  6700. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6701. procedure TglBitmapData.FillWithColorRange(const aRed, aGreen, aBlue: Cardinal; const aAlpha: Cardinal);
  6702. var
  6703. PixelData: TglBitmapPixelData;
  6704. begin
  6705. TFormatDescriptor.GetAlpha(Format).PreparePixel(PixelData);
  6706. FillWithColorFloat(
  6707. aRed / PixelData.Range.r,
  6708. aGreen / PixelData.Range.g,
  6709. aBlue / PixelData.Range.b,
  6710. aAlpha / PixelData.Range.a);
  6711. end;
  6712. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6713. procedure TglBitmapData.FillWithColorFloat(const aRed, aGreen, aBlue: Single; const aAlpha: Single);
  6714. var
  6715. PixelData: TglBitmapPixelData;
  6716. begin
  6717. TFormatDescriptor.Get(Format).PreparePixel(PixelData);
  6718. with PixelData do begin
  6719. Data.r := Max(0, Min(Range.r, Trunc(Range.r * aRed)));
  6720. Data.g := Max(0, Min(Range.g, Trunc(Range.g * aGreen)));
  6721. Data.b := Max(0, Min(Range.b, Trunc(Range.b * aBlue)));
  6722. Data.a := Max(0, Min(Range.a, Trunc(Range.a * aAlpha)));
  6723. end;
  6724. Convert(glBitmapFillWithColorFunc, false, @PixelData);
  6725. end;
  6726. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6727. procedure TglBitmapData.SetData(const aData: PByte; const aFormat: TglBitmapFormat; const aWidth: Integer; const aHeight: Integer);
  6728. begin
  6729. if (Data <> aData) then begin
  6730. if (Assigned(Data)) then
  6731. FreeMem(Data);
  6732. fData := aData;
  6733. end;
  6734. if Assigned(fData) then begin
  6735. FillChar(fDimension, SizeOf(fDimension), 0);
  6736. if aWidth <> -1 then begin
  6737. fDimension.Fields := fDimension.Fields + [ffX];
  6738. fDimension.X := aWidth;
  6739. end;
  6740. if aHeight <> -1 then begin
  6741. fDimension.Fields := fDimension.Fields + [ffY];
  6742. fDimension.Y := aHeight;
  6743. end;
  6744. fFormat := aFormat;
  6745. end else
  6746. fFormat := tfEmpty;
  6747. UpdateScanlines;
  6748. end;
  6749. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6750. function TglBitmapData.Clone: TglBitmapData;
  6751. var
  6752. Temp: TglBitmapData;
  6753. TempPtr: PByte;
  6754. Size: Integer;
  6755. begin
  6756. result := nil;
  6757. Temp := (ClassType.Create as TglBitmapData);
  6758. try
  6759. // copy texture data if assigned
  6760. if Assigned(Data) then begin
  6761. Size := TFormatDescriptor.Get(Format).GetSize(fDimension);
  6762. GetMem(TempPtr, Size);
  6763. try
  6764. Move(Data^, TempPtr^, Size);
  6765. Temp.SetData(TempPtr, Format, Width, Height);
  6766. except
  6767. if Assigned(TempPtr) then
  6768. FreeMem(TempPtr);
  6769. raise;
  6770. end;
  6771. end else begin
  6772. TempPtr := nil;
  6773. Temp.SetData(TempPtr, Format, Width, Height);
  6774. end;
  6775. // copy properties
  6776. Temp.fFormat := Format;
  6777. result := Temp;
  6778. except
  6779. FreeAndNil(Temp);
  6780. raise;
  6781. end;
  6782. end;
  6783. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6784. procedure TglBitmapData.Invert(const aRed, aGreen, aBlue, aAlpha: Boolean);
  6785. var
  6786. mask: PtrInt;
  6787. begin
  6788. mask :=
  6789. (Byte(aRed) and 1) or
  6790. ((Byte(aGreen) and 1) shl 1) or
  6791. ((Byte(aBlue) and 1) shl 2) or
  6792. ((Byte(aAlpha) and 1) shl 3);
  6793. if (mask > 0) then
  6794. Convert(glBitmapInvertFunc, false, {%H-}Pointer(mask));
  6795. end;
  6796. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6797. type
  6798. TMatrixItem = record
  6799. X, Y: Integer;
  6800. W: Single;
  6801. end;
  6802. PglBitmapToNormalMapRec = ^TglBitmapToNormalMapRec;
  6803. TglBitmapToNormalMapRec = Record
  6804. Scale: Single;
  6805. Heights: array of Single;
  6806. MatrixU : array of TMatrixItem;
  6807. MatrixV : array of TMatrixItem;
  6808. end;
  6809. const
  6810. ONE_OVER_255 = 1 / 255;
  6811. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6812. procedure glBitmapToNormalMapPrepareFunc(var FuncRec: TglBitmapFunctionRec);
  6813. var
  6814. Val: Single;
  6815. begin
  6816. with FuncRec do begin
  6817. Val :=
  6818. Source.Data.r * LUMINANCE_WEIGHT_R +
  6819. Source.Data.g * LUMINANCE_WEIGHT_G +
  6820. Source.Data.b * LUMINANCE_WEIGHT_B;
  6821. PglBitmapToNormalMapRec(Args)^.Heights[Position.Y * Size.X + Position.X] := Val * ONE_OVER_255;
  6822. end;
  6823. end;
  6824. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6825. procedure glBitmapToNormalMapPrepareAlphaFunc(var FuncRec: TglBitmapFunctionRec);
  6826. begin
  6827. with FuncRec do
  6828. PglBitmapToNormalMapRec(Args)^.Heights[Position.Y * Size.X + Position.X] := Source.Data.a * ONE_OVER_255;
  6829. end;
  6830. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6831. procedure glBitmapToNormalMapFunc (var FuncRec: TglBitmapFunctionRec);
  6832. type
  6833. TVec = Array[0..2] of Single;
  6834. var
  6835. Idx: Integer;
  6836. du, dv: Double;
  6837. Len: Single;
  6838. Vec: TVec;
  6839. function GetHeight(X, Y: Integer): Single;
  6840. begin
  6841. with FuncRec do begin
  6842. X := Max(0, Min(Size.X -1, X));
  6843. Y := Max(0, Min(Size.Y -1, Y));
  6844. result := PglBitmapToNormalMapRec(Args)^.Heights[Y * Size.X + X];
  6845. end;
  6846. end;
  6847. begin
  6848. with FuncRec do begin
  6849. with PglBitmapToNormalMapRec(Args)^ do begin
  6850. du := 0;
  6851. for Idx := Low(MatrixU) to High(MatrixU) do
  6852. du := du + GetHeight(Position.X + MatrixU[Idx].X, Position.Y + MatrixU[Idx].Y) * MatrixU[Idx].W;
  6853. dv := 0;
  6854. for Idx := Low(MatrixU) to High(MatrixU) do
  6855. dv := dv + GetHeight(Position.X + MatrixV[Idx].X, Position.Y + MatrixV[Idx].Y) * MatrixV[Idx].W;
  6856. Vec[0] := -du * Scale;
  6857. Vec[1] := -dv * Scale;
  6858. Vec[2] := 1;
  6859. end;
  6860. // Normalize
  6861. Len := 1 / Sqrt(Sqr(Vec[0]) + Sqr(Vec[1]) + Sqr(Vec[2]));
  6862. if Len <> 0 then begin
  6863. Vec[0] := Vec[0] * Len;
  6864. Vec[1] := Vec[1] * Len;
  6865. Vec[2] := Vec[2] * Len;
  6866. end;
  6867. // Farbe zuweisem
  6868. Dest.Data.r := Trunc((Vec[0] + 1) * 127.5);
  6869. Dest.Data.g := Trunc((Vec[1] + 1) * 127.5);
  6870. Dest.Data.b := Trunc((Vec[2] + 1) * 127.5);
  6871. end;
  6872. end;
  6873. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6874. procedure TglBitmapData.GenerateNormalMap(const aFunc: TglBitmapNormalMapFunc; const aScale: Single; const aUseAlpha: Boolean);
  6875. var
  6876. Rec: TglBitmapToNormalMapRec;
  6877. procedure SetEntry (var Matrix: array of TMatrixItem; Index, X, Y: Integer; W: Single);
  6878. begin
  6879. if (Index >= Low(Matrix)) and (Index <= High(Matrix)) then begin
  6880. Matrix[Index].X := X;
  6881. Matrix[Index].Y := Y;
  6882. Matrix[Index].W := W;
  6883. end;
  6884. end;
  6885. begin
  6886. if TFormatDescriptor.Get(Format).IsCompressed then
  6887. raise EglBitmapUnsupportedFormat.Create(Format);
  6888. if aScale > 100 then
  6889. Rec.Scale := 100
  6890. else if aScale < -100 then
  6891. Rec.Scale := -100
  6892. else
  6893. Rec.Scale := aScale;
  6894. SetLength(Rec.Heights, Width * Height);
  6895. try
  6896. case aFunc of
  6897. nm4Samples: begin
  6898. SetLength(Rec.MatrixU, 2);
  6899. SetEntry(Rec.MatrixU, 0, -1, 0, -0.5);
  6900. SetEntry(Rec.MatrixU, 1, 1, 0, 0.5);
  6901. SetLength(Rec.MatrixV, 2);
  6902. SetEntry(Rec.MatrixV, 0, 0, 1, 0.5);
  6903. SetEntry(Rec.MatrixV, 1, 0, -1, -0.5);
  6904. end;
  6905. nmSobel: begin
  6906. SetLength(Rec.MatrixU, 6);
  6907. SetEntry(Rec.MatrixU, 0, -1, 1, -1.0);
  6908. SetEntry(Rec.MatrixU, 1, -1, 0, -2.0);
  6909. SetEntry(Rec.MatrixU, 2, -1, -1, -1.0);
  6910. SetEntry(Rec.MatrixU, 3, 1, 1, 1.0);
  6911. SetEntry(Rec.MatrixU, 4, 1, 0, 2.0);
  6912. SetEntry(Rec.MatrixU, 5, 1, -1, 1.0);
  6913. SetLength(Rec.MatrixV, 6);
  6914. SetEntry(Rec.MatrixV, 0, -1, 1, 1.0);
  6915. SetEntry(Rec.MatrixV, 1, 0, 1, 2.0);
  6916. SetEntry(Rec.MatrixV, 2, 1, 1, 1.0);
  6917. SetEntry(Rec.MatrixV, 3, -1, -1, -1.0);
  6918. SetEntry(Rec.MatrixV, 4, 0, -1, -2.0);
  6919. SetEntry(Rec.MatrixV, 5, 1, -1, -1.0);
  6920. end;
  6921. nm3x3: begin
  6922. SetLength(Rec.MatrixU, 6);
  6923. SetEntry(Rec.MatrixU, 0, -1, 1, -1/6);
  6924. SetEntry(Rec.MatrixU, 1, -1, 0, -1/6);
  6925. SetEntry(Rec.MatrixU, 2, -1, -1, -1/6);
  6926. SetEntry(Rec.MatrixU, 3, 1, 1, 1/6);
  6927. SetEntry(Rec.MatrixU, 4, 1, 0, 1/6);
  6928. SetEntry(Rec.MatrixU, 5, 1, -1, 1/6);
  6929. SetLength(Rec.MatrixV, 6);
  6930. SetEntry(Rec.MatrixV, 0, -1, 1, 1/6);
  6931. SetEntry(Rec.MatrixV, 1, 0, 1, 1/6);
  6932. SetEntry(Rec.MatrixV, 2, 1, 1, 1/6);
  6933. SetEntry(Rec.MatrixV, 3, -1, -1, -1/6);
  6934. SetEntry(Rec.MatrixV, 4, 0, -1, -1/6);
  6935. SetEntry(Rec.MatrixV, 5, 1, -1, -1/6);
  6936. end;
  6937. nm5x5: begin
  6938. SetLength(Rec.MatrixU, 20);
  6939. SetEntry(Rec.MatrixU, 0, -2, 2, -1 / 16);
  6940. SetEntry(Rec.MatrixU, 1, -1, 2, -1 / 10);
  6941. SetEntry(Rec.MatrixU, 2, 1, 2, 1 / 10);
  6942. SetEntry(Rec.MatrixU, 3, 2, 2, 1 / 16);
  6943. SetEntry(Rec.MatrixU, 4, -2, 1, -1 / 10);
  6944. SetEntry(Rec.MatrixU, 5, -1, 1, -1 / 8);
  6945. SetEntry(Rec.MatrixU, 6, 1, 1, 1 / 8);
  6946. SetEntry(Rec.MatrixU, 7, 2, 1, 1 / 10);
  6947. SetEntry(Rec.MatrixU, 8, -2, 0, -1 / 2.8);
  6948. SetEntry(Rec.MatrixU, 9, -1, 0, -0.5);
  6949. SetEntry(Rec.MatrixU, 10, 1, 0, 0.5);
  6950. SetEntry(Rec.MatrixU, 11, 2, 0, 1 / 2.8);
  6951. SetEntry(Rec.MatrixU, 12, -2, -1, -1 / 10);
  6952. SetEntry(Rec.MatrixU, 13, -1, -1, -1 / 8);
  6953. SetEntry(Rec.MatrixU, 14, 1, -1, 1 / 8);
  6954. SetEntry(Rec.MatrixU, 15, 2, -1, 1 / 10);
  6955. SetEntry(Rec.MatrixU, 16, -2, -2, -1 / 16);
  6956. SetEntry(Rec.MatrixU, 17, -1, -2, -1 / 10);
  6957. SetEntry(Rec.MatrixU, 18, 1, -2, 1 / 10);
  6958. SetEntry(Rec.MatrixU, 19, 2, -2, 1 / 16);
  6959. SetLength(Rec.MatrixV, 20);
  6960. SetEntry(Rec.MatrixV, 0, -2, 2, 1 / 16);
  6961. SetEntry(Rec.MatrixV, 1, -1, 2, 1 / 10);
  6962. SetEntry(Rec.MatrixV, 2, 0, 2, 0.25);
  6963. SetEntry(Rec.MatrixV, 3, 1, 2, 1 / 10);
  6964. SetEntry(Rec.MatrixV, 4, 2, 2, 1 / 16);
  6965. SetEntry(Rec.MatrixV, 5, -2, 1, 1 / 10);
  6966. SetEntry(Rec.MatrixV, 6, -1, 1, 1 / 8);
  6967. SetEntry(Rec.MatrixV, 7, 0, 1, 0.5);
  6968. SetEntry(Rec.MatrixV, 8, 1, 1, 1 / 8);
  6969. SetEntry(Rec.MatrixV, 9, 2, 1, 1 / 16);
  6970. SetEntry(Rec.MatrixV, 10, -2, -1, -1 / 16);
  6971. SetEntry(Rec.MatrixV, 11, -1, -1, -1 / 8);
  6972. SetEntry(Rec.MatrixV, 12, 0, -1, -0.5);
  6973. SetEntry(Rec.MatrixV, 13, 1, -1, -1 / 8);
  6974. SetEntry(Rec.MatrixV, 14, 2, -1, -1 / 10);
  6975. SetEntry(Rec.MatrixV, 15, -2, -2, -1 / 16);
  6976. SetEntry(Rec.MatrixV, 16, -1, -2, -1 / 10);
  6977. SetEntry(Rec.MatrixV, 17, 0, -2, -0.25);
  6978. SetEntry(Rec.MatrixV, 18, 1, -2, -1 / 10);
  6979. SetEntry(Rec.MatrixV, 19, 2, -2, -1 / 16);
  6980. end;
  6981. end;
  6982. // Daten Sammeln
  6983. if aUseAlpha and TFormatDescriptor.Get(Format).HasAlpha then
  6984. Convert(glBitmapToNormalMapPrepareAlphaFunc, false, @Rec)
  6985. else
  6986. Convert(glBitmapToNormalMapPrepareFunc, false, @Rec);
  6987. Convert(glBitmapToNormalMapFunc, false, @Rec);
  6988. finally
  6989. SetLength(Rec.Heights, 0);
  6990. end;
  6991. end;
  6992. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6993. constructor TglBitmapData.Create;
  6994. begin
  6995. inherited Create;
  6996. fFormat := glBitmapDefaultFormat;
  6997. end;
  6998. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6999. constructor TglBitmapData.Create(const aFileName: String);
  7000. begin
  7001. Create;
  7002. LoadFromFile(aFileName);
  7003. end;
  7004. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7005. constructor TglBitmapData.Create(const aStream: TStream);
  7006. begin
  7007. Create;
  7008. LoadFromStream(aStream);
  7009. end;
  7010. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7011. constructor TglBitmapData.Create(const aSize: TglBitmapSize; const aFormat: TglBitmapFormat; aData: PByte);
  7012. var
  7013. ImageSize: Integer;
  7014. begin
  7015. Create;
  7016. if not Assigned(aData) then begin
  7017. ImageSize := TFormatDescriptor.Get(aFormat).GetSize(aSize);
  7018. GetMem(aData, ImageSize);
  7019. try
  7020. FillChar(aData^, ImageSize, #$FF);
  7021. SetData(aData, aFormat, aSize.X, aSize.Y);
  7022. except
  7023. if Assigned(aData) then
  7024. FreeMem(aData);
  7025. raise;
  7026. end;
  7027. end else begin
  7028. SetData(aData, aFormat, aSize.X, aSize.Y);
  7029. end;
  7030. end;
  7031. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7032. constructor TglBitmapData.Create(const aSize: TglBitmapSize; const aFormat: TglBitmapFormat; const aFunc: TglBitmapFunction; const aArgs: Pointer);
  7033. begin
  7034. Create;
  7035. LoadFromFunc(aSize, aFormat, aFunc, aArgs);
  7036. end;
  7037. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7038. constructor TglBitmapData.Create(const aInstance: Cardinal; const aResource: String; const aResType: PChar);
  7039. begin
  7040. Create;
  7041. LoadFromResource(aInstance, aResource, aResType);
  7042. end;
  7043. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7044. constructor TglBitmapData.Create(const aInstance: Cardinal; const aResourceID: Integer; const aResType: PChar);
  7045. begin
  7046. Create;
  7047. LoadFromResourceID(aInstance, aResourceID, aResType);
  7048. end;
  7049. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7050. destructor TglBitmapData.Destroy;
  7051. begin
  7052. SetData(nil, tfEmpty);
  7053. inherited Destroy;
  7054. end;
  7055. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7056. //TglBitmap - PROTECTED///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7057. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7058. function TglBitmap.GetWidth: Integer;
  7059. begin
  7060. if (ffX in fDimension.Fields) then
  7061. result := fDimension.X
  7062. else
  7063. result := -1;
  7064. end;
  7065. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7066. function TglBitmap.GetHeight: Integer;
  7067. begin
  7068. if (ffY in fDimension.Fields) then
  7069. result := fDimension.Y
  7070. else
  7071. result := -1;
  7072. end;
  7073. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7074. procedure TglBitmap.SetCustomData(const aValue: Pointer);
  7075. begin
  7076. if fCustomData = aValue then
  7077. exit;
  7078. fCustomData := aValue;
  7079. end;
  7080. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7081. procedure TglBitmap.SetCustomName(const aValue: String);
  7082. begin
  7083. if fCustomName = aValue then
  7084. exit;
  7085. fCustomName := aValue;
  7086. end;
  7087. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7088. procedure TglBitmap.SetCustomNameW(const aValue: WideString);
  7089. begin
  7090. if fCustomNameW = aValue then
  7091. exit;
  7092. fCustomNameW := aValue;
  7093. end;
  7094. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7095. procedure TglBitmap.SetDeleteTextureOnFree(const aValue: Boolean);
  7096. begin
  7097. if fDeleteTextureOnFree = aValue then
  7098. exit;
  7099. fDeleteTextureOnFree := aValue;
  7100. end;
  7101. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7102. procedure TglBitmap.SetID(const aValue: Cardinal);
  7103. begin
  7104. if fID = aValue then
  7105. exit;
  7106. fID := aValue;
  7107. end;
  7108. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7109. procedure TglBitmap.SetMipMap(const aValue: TglBitmapMipMap);
  7110. begin
  7111. if fMipMap = aValue then
  7112. exit;
  7113. fMipMap := aValue;
  7114. end;
  7115. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7116. procedure TglBitmap.SetTarget(const aValue: Cardinal);
  7117. begin
  7118. if fTarget = aValue then
  7119. exit;
  7120. fTarget := aValue;
  7121. end;
  7122. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7123. procedure TglBitmap.SetAnisotropic(const aValue: Integer);
  7124. {$IF NOT DEFINED(OPENGL_ES) OR DEFINED(OPENGL_ES_EXT)}
  7125. var
  7126. MaxAnisotropic: Integer;
  7127. {$IFEND}
  7128. begin
  7129. fAnisotropic := aValue;
  7130. if (ID > 0) then begin
  7131. {$IF NOT DEFINED(OPENGL_ES) OR DEFINED(OPENGL_ES_EXT)}
  7132. if GL_EXT_texture_filter_anisotropic then begin
  7133. if fAnisotropic > 0 then begin
  7134. Bind({$IFNDEF OPENGL_ES}false{$ENDIF});
  7135. glGetIntegerv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, @MaxAnisotropic);
  7136. if aValue > MaxAnisotropic then
  7137. fAnisotropic := MaxAnisotropic;
  7138. glTexParameteri(Target, GL_TEXTURE_MAX_ANISOTROPY_EXT, fAnisotropic);
  7139. end;
  7140. end else begin
  7141. fAnisotropic := 0;
  7142. end;
  7143. {$ELSE}
  7144. fAnisotropic := 0;
  7145. {$IFEND}
  7146. end;
  7147. end;
  7148. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7149. procedure TglBitmap.CreateID;
  7150. begin
  7151. if (ID <> 0) then
  7152. glDeleteTextures(1, @fID);
  7153. glGenTextures(1, @fID);
  7154. Bind({$IFNDEF OPENGL_ES}false{$ENDIF});
  7155. end;
  7156. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7157. procedure TglBitmap.SetupParameters({$IFNDEF OPENGL_ES}out aBuildWithGlu: Boolean{$ENDIF});
  7158. begin
  7159. // Set Up Parameters
  7160. SetWrap(fWrapS, fWrapT, fWrapR);
  7161. SetFilter(fFilterMin, fFilterMag);
  7162. SetAnisotropic(fAnisotropic);
  7163. {$IFNDEF OPENGL_ES}
  7164. SetBorderColor(fBorderColor[0], fBorderColor[1], fBorderColor[2], fBorderColor[3]);
  7165. if (GL_ARB_texture_swizzle or GL_EXT_texture_swizzle or GL_VERSION_3_3) then
  7166. SetSwizzle(fSwizzle[0], fSwizzle[1], fSwizzle[2], fSwizzle[3]);
  7167. {$ENDIF}
  7168. {$IFNDEF OPENGL_ES}
  7169. // Mip Maps Generation Mode
  7170. aBuildWithGlu := false;
  7171. if (MipMap = mmMipmap) then begin
  7172. if (GL_VERSION_1_4 or GL_SGIS_generate_mipmap) then
  7173. glTexParameteri(Target, GL_GENERATE_MIPMAP, GLint(GL_TRUE))
  7174. else
  7175. aBuildWithGlu := true;
  7176. end else if (MipMap = mmMipmapGlu) then
  7177. aBuildWithGlu := true;
  7178. {$ELSE}
  7179. if (MipMap = mmMipmap) then
  7180. glGenerateMipmap(Target);
  7181. {$ENDIF}
  7182. end;
  7183. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7184. //TglBitmap - PUBLIC//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7185. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7186. procedure TglBitmap.AfterConstruction;
  7187. begin
  7188. inherited AfterConstruction;
  7189. fID := 0;
  7190. fTarget := 0;
  7191. {$IFNDEF OPENGL_ES}
  7192. fIsResident := false;
  7193. {$ENDIF}
  7194. fMipMap := glBitmapDefaultMipmap;
  7195. fDeleteTextureOnFree := glBitmapGetDefaultDeleteTextureOnFree;
  7196. glBitmapGetDefaultFilter (fFilterMin, fFilterMag);
  7197. glBitmapGetDefaultTextureWrap(fWrapS, fWrapT, fWrapR);
  7198. {$IFNDEF OPENGL_ES}
  7199. glBitmapGetDefaultSwizzle (fSwizzle[0], fSwizzle[1], fSwizzle[2], fSwizzle[3]);
  7200. {$ENDIF}
  7201. end;
  7202. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7203. procedure TglBitmap.BeforeDestruction;
  7204. begin
  7205. if (fID > 0) and fDeleteTextureOnFree then
  7206. glDeleteTextures(1, @fID);
  7207. inherited BeforeDestruction;
  7208. end;
  7209. {$IFNDEF OPENGL_ES}
  7210. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7211. procedure TglBitmap.SetBorderColor(const aRed, aGreen, aBlue, aAlpha: Single);
  7212. begin
  7213. fBorderColor[0] := aRed;
  7214. fBorderColor[1] := aGreen;
  7215. fBorderColor[2] := aBlue;
  7216. fBorderColor[3] := aAlpha;
  7217. if (ID > 0) then begin
  7218. Bind(false);
  7219. glTexParameterfv(Target, GL_TEXTURE_BORDER_COLOR, @fBorderColor[0]);
  7220. end;
  7221. end;
  7222. {$ENDIF}
  7223. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7224. procedure TglBitmap.SetFilter(const aMin, aMag: GLenum);
  7225. begin
  7226. //check MIN filter
  7227. case aMin of
  7228. GL_NEAREST:
  7229. fFilterMin := GL_NEAREST;
  7230. GL_LINEAR:
  7231. fFilterMin := GL_LINEAR;
  7232. GL_NEAREST_MIPMAP_NEAREST:
  7233. fFilterMin := GL_NEAREST_MIPMAP_NEAREST;
  7234. GL_LINEAR_MIPMAP_NEAREST:
  7235. fFilterMin := GL_LINEAR_MIPMAP_NEAREST;
  7236. GL_NEAREST_MIPMAP_LINEAR:
  7237. fFilterMin := GL_NEAREST_MIPMAP_LINEAR;
  7238. GL_LINEAR_MIPMAP_LINEAR:
  7239. fFilterMin := GL_LINEAR_MIPMAP_LINEAR;
  7240. else
  7241. raise EglBitmap.Create('SetFilter - Unknow MIN filter.');
  7242. end;
  7243. //check MAG filter
  7244. case aMag of
  7245. GL_NEAREST:
  7246. fFilterMag := GL_NEAREST;
  7247. GL_LINEAR:
  7248. fFilterMag := GL_LINEAR;
  7249. else
  7250. raise EglBitmap.Create('SetFilter - Unknow MAG filter.');
  7251. end;
  7252. //apply filter
  7253. if (ID > 0) then begin
  7254. Bind({$IFNDEF OPENGL_ES}false{$ENDIF});
  7255. glTexParameteri(Target, GL_TEXTURE_MAG_FILTER, fFilterMag);
  7256. if (MipMap = mmNone) {$IFNDEF OPENGL_ES}or (Target = GL_TEXTURE_RECTANGLE){$ENDIF} then begin
  7257. case fFilterMin of
  7258. GL_NEAREST, GL_LINEAR:
  7259. glTexParameteri(Target, GL_TEXTURE_MIN_FILTER, fFilterMin);
  7260. GL_NEAREST_MIPMAP_NEAREST, GL_NEAREST_MIPMAP_LINEAR:
  7261. glTexParameteri(Target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  7262. GL_LINEAR_MIPMAP_NEAREST, GL_LINEAR_MIPMAP_LINEAR:
  7263. glTexParameteri(Target, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  7264. end;
  7265. end else
  7266. glTexParameteri(Target, GL_TEXTURE_MIN_FILTER, fFilterMin);
  7267. end;
  7268. end;
  7269. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7270. procedure TglBitmap.SetWrap(const S: GLenum; const T: GLenum; const R: GLenum);
  7271. procedure CheckAndSetWrap(const aValue: Cardinal; var aTarget: Cardinal);
  7272. begin
  7273. case aValue of
  7274. {$IFNDEF OPENGL_ES}
  7275. GL_CLAMP:
  7276. aTarget := GL_CLAMP;
  7277. {$ENDIF}
  7278. GL_REPEAT:
  7279. aTarget := GL_REPEAT;
  7280. GL_CLAMP_TO_EDGE: begin
  7281. {$IFNDEF OPENGL_ES}
  7282. if not GL_VERSION_1_2 and not GL_EXT_texture_edge_clamp then
  7283. aTarget := GL_CLAMP
  7284. else
  7285. {$ENDIF}
  7286. aTarget := GL_CLAMP_TO_EDGE;
  7287. end;
  7288. {$IFNDEF OPENGL_ES}
  7289. GL_CLAMP_TO_BORDER: begin
  7290. if GL_VERSION_1_3 or GL_ARB_texture_border_clamp then
  7291. aTarget := GL_CLAMP_TO_BORDER
  7292. else
  7293. aTarget := GL_CLAMP;
  7294. end;
  7295. {$ENDIF}
  7296. {$IF NOT DEFINED(OPENGL_ES) OR DEFINED(OPENGL_ES_2_0)}
  7297. GL_MIRRORED_REPEAT: begin
  7298. {$IFNDEF OPENGL_ES}
  7299. if GL_VERSION_1_4 or GL_ARB_texture_mirrored_repeat or GL_IBM_texture_mirrored_repeat then
  7300. {$ELSE}
  7301. if GL_VERSION_2_0 then
  7302. {$ENDIF}
  7303. aTarget := GL_MIRRORED_REPEAT
  7304. else
  7305. raise EglBitmap.Create('SetWrap - Unsupported Texturewrap GL_MIRRORED_REPEAT (S).');
  7306. end;
  7307. {$IFEND}
  7308. else
  7309. raise EglBitmap.Create('SetWrap - Unknow Texturewrap');
  7310. end;
  7311. end;
  7312. begin
  7313. CheckAndSetWrap(S, fWrapS);
  7314. CheckAndSetWrap(T, fWrapT);
  7315. CheckAndSetWrap(R, fWrapR);
  7316. if (ID > 0) then begin
  7317. Bind({$IFNDEF OPENGL_ES}false{$ENDIF});
  7318. glTexParameteri(Target, GL_TEXTURE_WRAP_S, fWrapS);
  7319. glTexParameteri(Target, GL_TEXTURE_WRAP_T, fWrapT);
  7320. {$IF NOT DEFINED(OPENGL_ES) OR DEFINED(OPENGL_ES_3_0)}
  7321. {$IFDEF OPENGL_ES} if GL_VERSION_3_0 then{$ENDIF}
  7322. glTexParameteri(Target, GL_TEXTURE_WRAP_R, fWrapR);
  7323. {$IFEND}
  7324. end;
  7325. end;
  7326. {$IF NOT DEFINED(OPENGL_ES) OR DEFINED(OPENGL_ES_3_0)}
  7327. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7328. procedure TglBitmap.SetSwizzle(const r, g, b, a: GLenum);
  7329. procedure CheckAndSetValue(const aValue: GLenum; const aIndex: Integer);
  7330. begin
  7331. if (aValue = GL_ZERO) or (aValue = GL_ONE) or (aValue = GL_ALPHA) or
  7332. (aValue = GL_RED) or (aValue = GL_GREEN) or (aValue = GL_BLUE) then
  7333. fSwizzle[aIndex] := aValue
  7334. else
  7335. raise EglBitmap.Create('SetSwizzle - Unknow Swizle Value');
  7336. end;
  7337. begin
  7338. {$IFNDEF OPENGL_ES}
  7339. if not (GL_ARB_texture_swizzle or GL_EXT_texture_swizzle or GL_VERSION_3_3) then
  7340. raise EglBitmapNotSupported.Create('texture swizzle is not supported');
  7341. {$ELSE}
  7342. if not GL_VERSION_3_0 then
  7343. raise EglBitmapNotSupported.Create('texture swizzle is not supported');
  7344. {$ENDIF}
  7345. CheckAndSetValue(r, 0);
  7346. CheckAndSetValue(g, 1);
  7347. CheckAndSetValue(b, 2);
  7348. CheckAndSetValue(a, 3);
  7349. if (ID > 0) then begin
  7350. Bind(false);
  7351. {$IFNDEF OPENGL_ES}
  7352. glTexParameteriv(Target, GL_TEXTURE_SWIZZLE_RGBA, PGLint(@fSwizzle[0]));
  7353. {$ELSE}
  7354. glTexParameteriv(Target, GL_TEXTURE_SWIZZLE_R, PGLint(@fSwizzle[0]));
  7355. glTexParameteriv(Target, GL_TEXTURE_SWIZZLE_G, PGLint(@fSwizzle[1]));
  7356. glTexParameteriv(Target, GL_TEXTURE_SWIZZLE_B, PGLint(@fSwizzle[2]));
  7357. glTexParameteriv(Target, GL_TEXTURE_SWIZZLE_A, PGLint(@fSwizzle[3]));
  7358. {$ENDIF}
  7359. end;
  7360. end;
  7361. {$IFEND}
  7362. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7363. procedure TglBitmap.Bind({$IFNDEF OPENGL_ES}const aEnableTextureUnit: Boolean{$ENDIF});
  7364. begin
  7365. {$IFNDEF OPENGL_ES}
  7366. if aEnableTextureUnit then
  7367. glEnable(Target);
  7368. {$ENDIF}
  7369. if (ID > 0) then
  7370. glBindTexture(Target, ID);
  7371. end;
  7372. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7373. procedure TglBitmap.Unbind({$IFNDEF OPENGL_ES}const aDisableTextureUnit: Boolean{$ENDIF});
  7374. begin
  7375. {$IFNDEF OPENGL_ES}
  7376. if aDisableTextureUnit then
  7377. glDisable(Target);
  7378. {$ENDIF}
  7379. glBindTexture(Target, 0);
  7380. end;
  7381. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7382. procedure TglBitmap.UploadData(const aDataObj: TglBitmapData; const aCheckSize: Boolean);
  7383. var
  7384. w, h: Integer;
  7385. begin
  7386. w := aDataObj.Width;
  7387. h := aDataObj.Height;
  7388. fDimension.Fields := [];
  7389. if (w > 0) then
  7390. fDimension.Fields := fDimension.Fields + [ffX];
  7391. if (h > 0) then
  7392. fDimension.Fields := fDimension.Fields + [ffY];
  7393. fDimension.X := w;
  7394. fDimension.Y := h;
  7395. end;
  7396. {$IFNDEF OPENGL_ES}
  7397. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7398. function TglBitmap.DownloadData(const aDataObj: TglBitmapData): Boolean;
  7399. var
  7400. Temp: PByte;
  7401. TempWidth, TempHeight: Integer;
  7402. TempIntFormat: GLint;
  7403. IntFormat: TglBitmapFormat;
  7404. FormatDesc: TFormatDescriptor;
  7405. begin
  7406. result := false;
  7407. Bind;
  7408. // Request Data
  7409. glGetTexLevelParameteriv(Target, 0, GL_TEXTURE_WIDTH, @TempWidth);
  7410. glGetTexLevelParameteriv(Target, 0, GL_TEXTURE_HEIGHT, @TempHeight);
  7411. glGetTexLevelParameteriv(Target, 0, GL_TEXTURE_INTERNAL_FORMAT, @TempIntFormat);
  7412. FormatDesc := (TglBitmapFormatDescriptor.GetByFormat(TempIntFormat) as TFormatDescriptor);
  7413. IntFormat := FormatDesc.Format;
  7414. // Getting data from OpenGL
  7415. FormatDesc := TFormatDescriptor.Get(IntFormat);
  7416. GetMem(Temp, FormatDesc.GetSize(TempWidth, TempHeight));
  7417. try
  7418. if FormatDesc.IsCompressed then begin
  7419. if not Assigned(glGetCompressedTexImage) then
  7420. raise EglBitmap.Create('compressed formats not supported by video adapter');
  7421. glGetCompressedTexImage(Target, 0, Temp)
  7422. end else
  7423. glGetTexImage(Target, 0, FormatDesc.glFormat, FormatDesc.glDataFormat, Temp);
  7424. aDataObj.SetData(Temp, IntFormat, TempWidth, TempHeight);
  7425. result := true;
  7426. except
  7427. if Assigned(Temp) then
  7428. FreeMem(Temp);
  7429. raise;
  7430. end;
  7431. end;
  7432. {$ENDIF}
  7433. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7434. constructor TglBitmap.Create;
  7435. begin
  7436. if (ClassType = TglBitmap) then
  7437. raise EglBitmap.Create('Don''t create TglBitmap directly. Use one of the deviated classes (TglBitmap2D) instead.');
  7438. inherited Create;
  7439. end;
  7440. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7441. constructor TglBitmap.Create(const aData: TglBitmapData);
  7442. begin
  7443. Create;
  7444. UploadData(aData);
  7445. end;
  7446. {$IFNDEF OPENGL_ES}
  7447. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7448. //TglBitmap1D/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7449. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7450. procedure TglBitmap1D.UploadDataIntern(const aDataObj: TglBitmapData; const aBuildWithGlu: Boolean);
  7451. var
  7452. fd: TglBitmapFormatDescriptor;
  7453. begin
  7454. // Upload data
  7455. fd := aDataObj.FormatDescriptor;
  7456. if (fd.glFormat = 0) or (fd.glInternalFormat = 0) or (fd.glDataFormat = 0) then
  7457. raise EglBitmap.Create('format is not supported by video adapter, please convert before uploading data');
  7458. if fd.IsCompressed then begin
  7459. if not Assigned(glCompressedTexImage1D) then
  7460. raise EglBitmap.Create('compressed formats not supported by video adapter');
  7461. glCompressedTexImage1D(Target, 0, fd.glInternalFormat, aDataObj.Width, 0, fd.GetSize(aDataObj.Width, 1), aDataObj.Data)
  7462. end else if aBuildWithGlu then
  7463. gluBuild1DMipmaps(Target, fd.glInternalFormat, aDataObj.Width, fd.glFormat, fd.glDataFormat, aDataObj.Data)
  7464. else
  7465. glTexImage1D(Target, 0, fd.glInternalFormat, aDataObj.Width, 0, fd.glFormat, fd.glDataFormat, aDataObj.Data);
  7466. end;
  7467. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7468. procedure TglBitmap1D.AfterConstruction;
  7469. begin
  7470. inherited;
  7471. Target := GL_TEXTURE_1D;
  7472. end;
  7473. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7474. procedure TglBitmap1D.UploadData(const aDataObj: TglBitmapData; const aCheckSize: Boolean);
  7475. var
  7476. BuildWithGlu, TexRec: Boolean;
  7477. TexSize: Integer;
  7478. begin
  7479. if not Assigned(aDataObj) then
  7480. exit;
  7481. // Check Texture Size
  7482. if (aCheckSize) then begin
  7483. glGetIntegerv(GL_MAX_TEXTURE_SIZE, @TexSize);
  7484. if (aDataObj.Width > TexSize) then
  7485. raise EglBitmapSizeToLarge.Create('TglBitmap1D.GenTexture - The size for the texture is to large. It''s may be not conform with the Hardware.');
  7486. TexRec := (GL_ARB_texture_rectangle or GL_EXT_texture_rectangle or GL_NV_texture_rectangle) and
  7487. (Target = GL_TEXTURE_RECTANGLE);
  7488. if not (IsPowerOfTwo(aDataObj.Width) or GL_ARB_texture_non_power_of_two or GL_VERSION_2_0 or TexRec) then
  7489. raise EglBitmapNonPowerOfTwo.Create('TglBitmap1D.GenTexture - Rendercontex dosn''t support non power of two texture.');
  7490. end;
  7491. if (fID = 0) then
  7492. CreateID;
  7493. SetupParameters(BuildWithGlu);
  7494. UploadDataIntern(aDataObj, BuildWithGlu);
  7495. glAreTexturesResident(1, @fID, @fIsResident);
  7496. inherited UploadData(aDataObj, aCheckSize);
  7497. end;
  7498. {$ENDIF}
  7499. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7500. //TglBitmap2D/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7501. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7502. procedure TglBitmap2D.UploadDataIntern(const aDataObj: TglBitmapData; const aTarget: GLenum{$IFNDEF OPENGL_ES}; const aBuildWithGlu: Boolean{$ENDIF});
  7503. var
  7504. fd: TglBitmapFormatDescriptor;
  7505. begin
  7506. fd := aDataObj.FormatDescriptor;
  7507. if (fd.glFormat = 0) or (fd.glInternalFormat = 0) or (fd.glDataFormat = 0) then
  7508. raise EglBitmap.Create('format is not supported by video adapter, please convert before uploading data');
  7509. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  7510. if fd.IsCompressed then begin
  7511. if not Assigned(glCompressedTexImage2D) then
  7512. raise EglBitmap.Create('compressed formats not supported by video adapter');
  7513. glCompressedTexImage2D(aTarget, 0, fd.glInternalFormat, aDataObj.Width, aDataObj.Height, 0, fd.GetSize(fDimension), aDataObj.Data)
  7514. {$IFNDEF OPENGL_ES}
  7515. end else if aBuildWithGlu then begin
  7516. gluBuild2DMipmaps(aTarget, fd.ChannelCount, aDataObj.Width, aDataObj.Height, fd.glFormat, fd.glDataFormat, aDataObj.Data)
  7517. {$ENDIF}
  7518. end else begin
  7519. glTexImage2D(aTarget, 0, fd.glInternalFormat, aDataObj.Width, aDataObj.Height, 0, fd.glFormat, fd.glDataFormat, aDataObj.Data);
  7520. end;
  7521. end;
  7522. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7523. procedure TglBitmap2D.AfterConstruction;
  7524. begin
  7525. inherited;
  7526. Target := GL_TEXTURE_2D;
  7527. end;
  7528. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7529. procedure TglBitmap2D.UploadData(const aDataObj: TglBitmapData; const aCheckSize: Boolean);
  7530. var
  7531. {$IFNDEF OPENGL_ES}
  7532. BuildWithGlu, TexRec: Boolean;
  7533. {$ENDIF}
  7534. PotTex: Boolean;
  7535. TexSize: Integer;
  7536. begin
  7537. if not Assigned(aDataObj) then
  7538. exit;
  7539. // Check Texture Size
  7540. if (aCheckSize) then begin
  7541. glGetIntegerv(GL_MAX_TEXTURE_SIZE, @TexSize);
  7542. if ((aDataObj.Width > TexSize) or (aDataObj.Height > TexSize)) then
  7543. raise EglBitmapSizeToLarge.Create('TglBitmap2D.GenTexture - The size for the texture is to large. It''s may be not conform with the Hardware.');
  7544. PotTex := IsPowerOfTwo(aDataObj.Width) and IsPowerOfTwo(aDataObj.Height);
  7545. {$IF NOT DEFINED(OPENGL_ES)}
  7546. TexRec := (GL_ARB_texture_rectangle or GL_EXT_texture_rectangle or GL_NV_texture_rectangle) and (Target = GL_TEXTURE_RECTANGLE);
  7547. if not (PotTex or GL_ARB_texture_non_power_of_two or GL_VERSION_2_0 or TexRec) then
  7548. raise EglBitmapNonPowerOfTwo.Create('TglBitmap2D.GenTexture - Rendercontex dosn''t support non power of two texture.');
  7549. {$ELSEIF DEFINED(OPENGL_ES_EXT)}
  7550. if not PotTex and not GL_OES_texture_npot then
  7551. raise EglBitmapNonPowerOfTwo.Create('TglBitmap2D.GenTexture - Rendercontex dosn''t support non power of two texture.');
  7552. {$ELSE}
  7553. if not PotTex then
  7554. raise EglBitmapNonPowerOfTwo.Create('TglBitmap2D.GenTexture - Rendercontex dosn''t support non power of two texture.');
  7555. {$IFEND}
  7556. end;
  7557. if (fID = 0) then
  7558. CreateID;
  7559. SetupParameters({$IFNDEF OPENGL_ES}BuildWithGlu{$ENDIF});
  7560. UploadDataIntern(aDataObj, Target{$IFNDEF OPENGL_ES}, BuildWithGlu{$ENDIF});
  7561. {$IFNDEF OPENGL_ES}
  7562. glAreTexturesResident(1, @fID, @fIsResident);
  7563. {$ENDIF}
  7564. inherited UploadData(aDataObj, aCheckSize);
  7565. end;
  7566. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7567. class procedure TglBitmap2D.GrabScreen(const aTop, aLeft, aRight, aBottom: Integer; const aFormat: TglBitmapFormat; const aDataObj: TglBitmapData);
  7568. var
  7569. Temp: pByte;
  7570. Size, w, h: Integer;
  7571. FormatDesc: TFormatDescriptor;
  7572. begin
  7573. FormatDesc := TFormatDescriptor.Get(aFormat);
  7574. if FormatDesc.IsCompressed then
  7575. raise EglBitmapUnsupportedFormat.Create(aFormat);
  7576. w := aRight - aLeft;
  7577. h := aBottom - aTop;
  7578. Size := FormatDesc.GetSize(w, h);
  7579. GetMem(Temp, Size);
  7580. try
  7581. glPixelStorei(GL_PACK_ALIGNMENT, 1);
  7582. glReadPixels(aLeft, aTop, w, h, FormatDesc.glFormat, FormatDesc.glDataFormat, Temp);
  7583. aDataObj.SetData(Temp, aFormat, w, h);
  7584. aDataObj.FlipVert;
  7585. except
  7586. if Assigned(Temp) then
  7587. FreeMem(Temp);
  7588. raise;
  7589. end;
  7590. end;
  7591. {$IF NOT DEFINED(OPENGL_ES) OR DEFINED(OPENGL_ES_2_0)}
  7592. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7593. //TglBitmapCubeMap////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7594. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7595. procedure TglBitmapCubeMap.AfterConstruction;
  7596. begin
  7597. inherited;
  7598. {$IFNDEF OPENGL_ES}
  7599. if not (GL_VERSION_1_3 or GL_ARB_texture_cube_map or GL_EXT_texture_cube_map) then
  7600. raise EglBitmap.Create('TglBitmapCubeMap.AfterConstruction - CubeMaps are unsupported.');
  7601. {$ELSE}
  7602. if not (GL_VERSION_2_0) then
  7603. raise EglBitmap.Create('TglBitmapCubeMap.AfterConstruction - CubeMaps are unsupported.');
  7604. {$ENDIF}
  7605. SetWrap;
  7606. Target := GL_TEXTURE_CUBE_MAP;
  7607. {$IFNDEF OPENGL_ES}
  7608. fGenMode := GL_REFLECTION_MAP;
  7609. {$ENDIF}
  7610. end;
  7611. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7612. procedure TglBitmapCubeMap.UploadData(const aDataObj: TglBitmapData; const aCheckSize: Boolean);
  7613. begin
  7614. Assert(false, 'TglBitmapCubeMap.UploadData - Don''t call UploadData directly, use UploadCubeMap instead');
  7615. end;
  7616. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7617. procedure TglBitmapCubeMap.UploadCubeMap(const aDataObj: TglBitmapData; const aCubeTarget: Cardinal; const aCheckSize: Boolean);
  7618. var
  7619. {$IFNDEF OPENGL_ES}
  7620. BuildWithGlu: Boolean;
  7621. {$ENDIF}
  7622. TexSize: Integer;
  7623. begin
  7624. if (aCheckSize) then begin
  7625. glGetIntegerv(GL_MAX_CUBE_MAP_TEXTURE_SIZE, @TexSize);
  7626. if (aDataObj.Width > TexSize) or (aDataObj.Height > TexSize) then
  7627. raise EglBitmapSizeToLarge.Create('TglBitmapCubeMap.GenerateCubeMap - The size for the Cubemap is to large. It''s may be not conform with the Hardware.');
  7628. {$IF NOT DEFINED(OPENGL_ES)}
  7629. if not ((IsPowerOfTwo(aDataObj.Width) and IsPowerOfTwo(aDataObj.Height)) or GL_VERSION_2_0 or GL_ARB_texture_non_power_of_two) then
  7630. raise EglBitmapNonPowerOfTwo.Create('TglBitmapCubeMap.GenerateCubeMap - Cubemaps dosn''t support non power of two texture.');
  7631. {$ELSEIF DEFINED(OPENGL_ES_EXT)}
  7632. if not (IsPowerOfTwo(aDataObj.Width) and IsPowerOfTwo(aDataObj.Height)) and not GL_OES_texture_npot then
  7633. raise EglBitmapNonPowerOfTwo.Create('TglBitmapCubeMap.GenerateCubeMap - Cubemaps dosn''t support non power of two texture.');
  7634. {$ELSE}
  7635. if not (IsPowerOfTwo(aDataObj.Width) and IsPowerOfTwo(aDataObj.Height)) then
  7636. raise EglBitmapNonPowerOfTwo.Create('TglBitmapCubeMap.GenerateCubeMap - Cubemaps dosn''t support non power of two texture.');
  7637. {$IFEND}
  7638. end;
  7639. if (fID = 0) then
  7640. CreateID;
  7641. SetupParameters({$IFNDEF OPENGL_ES}BuildWithGlu{$ENDIF});
  7642. UploadDataIntern(aDataObj, aCubeTarget{$IFNDEF OPENGL_ES}, BuildWithGlu{$ENDIF});
  7643. inherited UploadData(aDataObj, aCheckSize);
  7644. end;
  7645. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7646. procedure TglBitmapCubeMap.Bind({$IFNDEF OPENGL_ES}const aEnableTexCoordsGen: Boolean; const aEnableTextureUnit: Boolean{$ENDIF});
  7647. begin
  7648. inherited Bind({$IFNDEF OPENGL_ES}aEnableTextureUnit{$ENDIF});
  7649. {$IFNDEF OPENGL_ES}
  7650. if aEnableTexCoordsGen then begin
  7651. glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, fGenMode);
  7652. glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, fGenMode);
  7653. glTexGeni(GL_R, GL_TEXTURE_GEN_MODE, fGenMode);
  7654. glEnable(GL_TEXTURE_GEN_S);
  7655. glEnable(GL_TEXTURE_GEN_T);
  7656. glEnable(GL_TEXTURE_GEN_R);
  7657. end;
  7658. {$ENDIF}
  7659. end;
  7660. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7661. procedure TglBitmapCubeMap.Unbind({$IFNDEF OPENGL_ES}const aDisableTexCoordsGen: Boolean; const aDisableTextureUnit: Boolean{$ENDIF});
  7662. begin
  7663. inherited Unbind({$IFNDEF OPENGL_ES}aDisableTextureUnit{$ENDIF});
  7664. {$IFNDEF OPENGL_ES}
  7665. if aDisableTexCoordsGen then begin
  7666. glDisable(GL_TEXTURE_GEN_S);
  7667. glDisable(GL_TEXTURE_GEN_T);
  7668. glDisable(GL_TEXTURE_GEN_R);
  7669. end;
  7670. {$ENDIF}
  7671. end;
  7672. {$IFEND}
  7673. {$IF NOT DEFINED(OPENGL_ES) OR DEFINED(OPENGL_ES_2_0)}
  7674. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7675. //TglBitmapNormalMap//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7676. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7677. type
  7678. TVec = Array[0..2] of Single;
  7679. TglBitmapNormalMapGetVectorFunc = procedure (out aVec: TVec; const aPosition: TglBitmapPixelPosition; const aHalfSize: Integer);
  7680. PglBitmapNormalMapRec = ^TglBitmapNormalMapRec;
  7681. TglBitmapNormalMapRec = record
  7682. HalfSize : Integer;
  7683. Func: TglBitmapNormalMapGetVectorFunc;
  7684. end;
  7685. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7686. procedure glBitmapNormalMapPosX(out aVec: TVec; const aPosition: TglBitmapPixelPosition; const aHalfSize: Integer);
  7687. begin
  7688. aVec[0] := aHalfSize;
  7689. aVec[1] := - (aPosition.Y + 0.5 - aHalfSize);
  7690. aVec[2] := - (aPosition.X + 0.5 - aHalfSize);
  7691. end;
  7692. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7693. procedure glBitmapNormalMapNegX(out aVec: TVec; const aPosition: TglBitmapPixelPosition; const aHalfSize: Integer);
  7694. begin
  7695. aVec[0] := - aHalfSize;
  7696. aVec[1] := - (aPosition.Y + 0.5 - aHalfSize);
  7697. aVec[2] := aPosition.X + 0.5 - aHalfSize;
  7698. end;
  7699. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7700. procedure glBitmapNormalMapPosY(out aVec: TVec; const aPosition: TglBitmapPixelPosition; const aHalfSize: Integer);
  7701. begin
  7702. aVec[0] := aPosition.X + 0.5 - aHalfSize;
  7703. aVec[1] := aHalfSize;
  7704. aVec[2] := aPosition.Y + 0.5 - aHalfSize;
  7705. end;
  7706. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7707. procedure glBitmapNormalMapNegY(out aVec: TVec; const aPosition: TglBitmapPixelPosition; const aHalfSize: Integer);
  7708. begin
  7709. aVec[0] := aPosition.X + 0.5 - aHalfSize;
  7710. aVec[1] := - aHalfSize;
  7711. aVec[2] := - (aPosition.Y + 0.5 - aHalfSize);
  7712. end;
  7713. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7714. procedure glBitmapNormalMapPosZ(out aVec: TVec; const aPosition: TglBitmapPixelPosition; const aHalfSize: Integer);
  7715. begin
  7716. aVec[0] := aPosition.X + 0.5 - aHalfSize;
  7717. aVec[1] := - (aPosition.Y + 0.5 - aHalfSize);
  7718. aVec[2] := aHalfSize;
  7719. end;
  7720. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7721. procedure glBitmapNormalMapNegZ(out aVec: TVec; const aPosition: TglBitmapPixelPosition; const aHalfSize: Integer);
  7722. begin
  7723. aVec[0] := - (aPosition.X + 0.5 - aHalfSize);
  7724. aVec[1] := - (aPosition.Y + 0.5 - aHalfSize);
  7725. aVec[2] := - aHalfSize;
  7726. end;
  7727. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7728. procedure glBitmapNormalMapFunc(var FuncRec: TglBitmapFunctionRec);
  7729. var
  7730. i: Integer;
  7731. Vec: TVec;
  7732. Len: Single;
  7733. begin
  7734. with FuncRec do begin
  7735. with PglBitmapNormalMapRec(Args)^ do begin
  7736. Func(Vec, Position, HalfSize);
  7737. // Normalize
  7738. Len := 1 / Sqrt(Sqr(Vec[0]) + Sqr(Vec[1]) + Sqr(Vec[2]));
  7739. if Len <> 0 then begin
  7740. Vec[0] := Vec[0] * Len;
  7741. Vec[1] := Vec[1] * Len;
  7742. Vec[2] := Vec[2] * Len;
  7743. end;
  7744. // Scale Vector and AddVectro
  7745. Vec[0] := Vec[0] * 0.5 + 0.5;
  7746. Vec[1] := Vec[1] * 0.5 + 0.5;
  7747. Vec[2] := Vec[2] * 0.5 + 0.5;
  7748. end;
  7749. // Set Color
  7750. for i := 0 to 2 do
  7751. Dest.Data.arr[i] := Round(Vec[i] * 255);
  7752. end;
  7753. end;
  7754. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7755. procedure TglBitmapNormalMap.AfterConstruction;
  7756. begin
  7757. inherited;
  7758. {$IFNDEF OPENGL_ES}
  7759. fGenMode := GL_NORMAL_MAP;
  7760. {$ENDIF}
  7761. end;
  7762. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7763. procedure TglBitmapNormalMap.GenerateNormalMap(const aSize: Integer; const aCheckSize: Boolean);
  7764. var
  7765. Rec: TglBitmapNormalMapRec;
  7766. SizeRec: TglBitmapSize;
  7767. DataObj: TglBitmapData;
  7768. begin
  7769. Rec.HalfSize := aSize div 2;
  7770. SizeRec.Fields := [ffX, ffY];
  7771. SizeRec.X := aSize;
  7772. SizeRec.Y := aSize;
  7773. DataObj := TglBitmapData.Create;
  7774. try
  7775. // Positive X
  7776. Rec.Func := glBitmapNormalMapPosX;
  7777. DataObj.LoadFromFunc(SizeRec, tfBGR8ub3, glBitmapNormalMapFunc, @Rec);
  7778. UploadCubeMap(DataObj, GL_TEXTURE_CUBE_MAP_POSITIVE_X, aCheckSize);
  7779. // Negative X
  7780. Rec.Func := glBitmapNormalMapNegX;
  7781. DataObj.LoadFromFunc(SizeRec, tfBGR8ub3, glBitmapNormalMapFunc, @Rec);
  7782. UploadCubeMap(DataObj, GL_TEXTURE_CUBE_MAP_NEGATIVE_X, aCheckSize);
  7783. // Positive Y
  7784. Rec.Func := glBitmapNormalMapPosY;
  7785. DataObj.LoadFromFunc(SizeRec, tfBGR8ub3, glBitmapNormalMapFunc, @Rec);
  7786. UploadCubeMap(DataObj, GL_TEXTURE_CUBE_MAP_POSITIVE_Y, aCheckSize);
  7787. // Negative Y
  7788. Rec.Func := glBitmapNormalMapNegY;
  7789. DataObj.LoadFromFunc(SizeRec, tfBGR8ub3, glBitmapNormalMapFunc, @Rec);
  7790. UploadCubeMap(DataObj, GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, aCheckSize);
  7791. // Positive Z
  7792. Rec.Func := glBitmapNormalMapPosZ;
  7793. DataObj.LoadFromFunc(SizeRec, tfBGR8ub3, glBitmapNormalMapFunc, @Rec);
  7794. UploadCubeMap(DataObj, GL_TEXTURE_CUBE_MAP_POSITIVE_Z, aCheckSize);
  7795. // Negative Z
  7796. Rec.Func := glBitmapNormalMapNegZ;
  7797. DataObj.LoadFromFunc(SizeRec, tfBGR8ub3, glBitmapNormalMapFunc, @Rec);
  7798. UploadCubeMap(DataObj, GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, aCheckSize);
  7799. finally
  7800. FreeAndNil(DataObj);
  7801. end;
  7802. end;
  7803. {$IFEND}
  7804. initialization
  7805. glBitmapSetDefaultFormat (tfEmpty);
  7806. glBitmapSetDefaultMipmap (mmMipmap);
  7807. glBitmapSetDefaultFilter (GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR);
  7808. glBitmapSetDefaultWrap (GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
  7809. {$IF NOT DEFINED(OPENGL_ES) OR DEFINED(OPENGL_ES_3_0)}
  7810. glBitmapSetDefaultSwizzle(GL_RED, GL_GREEN, GL_BLUE, GL_ALPHA);
  7811. {$IFEND}
  7812. glBitmapSetDefaultFreeDataAfterGenTexture(true);
  7813. glBitmapSetDefaultDeleteTextureOnFree (true);
  7814. TFormatDescriptor.Init;
  7815. finalization
  7816. TFormatDescriptor.Finalize;
  7817. end.