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.

433 lines
15 KiB

4 years ago
  1. /**
  2. @mainpage SOIL
  3. Jonathan Dummer
  4. 2007-07-26-10.36
  5. Simple OpenGL Image Library
  6. A tiny c library for uploading images as
  7. textures into OpenGL. Also saving and
  8. loading of images is supported.
  9. I'm using Sean's Tool Box image loader as a base:
  10. http://www.nothings.org/
  11. I'm upgrading it to load TGA and DDS files, and a direct
  12. path for loading DDS files straight into OpenGL textures,
  13. when applicable.
  14. Image Formats:
  15. - BMP load & save
  16. - TGA load & save
  17. - DDS load & save
  18. - PNG load
  19. - JPG load
  20. OpenGL Texture Features:
  21. - resample to power-of-two sizes
  22. - MIPmap generation
  23. - compressed texture S3TC formats (if supported)
  24. - can pre-multiply alpha for you, for better compositing
  25. - can flip image about the y-axis (except pre-compressed DDS files)
  26. Thanks to:
  27. * Sean Barret - for the awesome stb_image
  28. * Dan Venkitachalam - for finding some non-compliant DDS files, and patching some explicit casts
  29. * everybody at gamedev.net
  30. **/
  31. #ifndef HEADER_SIMPLE_OPENGL_IMAGE_LIBRARY
  32. #define HEADER_SIMPLE_OPENGL_IMAGE_LIBRARY
  33. #ifdef __cplusplus
  34. extern "C" {
  35. #endif
  36. /**
  37. The format of images that may be loaded (force_channels).
  38. SOIL_LOAD_AUTO leaves the image in whatever format it was found.
  39. SOIL_LOAD_L forces the image to load as Luminous (greyscale)
  40. SOIL_LOAD_LA forces the image to load as Luminous with Alpha
  41. SOIL_LOAD_RGB forces the image to load as Red Green Blue
  42. SOIL_LOAD_RGBA forces the image to load as Red Green Blue Alpha
  43. **/
  44. enum
  45. {
  46. SOIL_LOAD_AUTO = 0,
  47. SOIL_LOAD_L = 1,
  48. SOIL_LOAD_LA = 2,
  49. SOIL_LOAD_RGB = 3,
  50. SOIL_LOAD_RGBA = 4
  51. };
  52. /**
  53. Passed in as reuse_texture_ID, will cause SOIL to
  54. register a new texture ID using glGenTextures().
  55. If the value passed into reuse_texture_ID > 0 then
  56. SOIL will just re-use that texture ID (great for
  57. reloading image assets in-game!)
  58. **/
  59. enum
  60. {
  61. SOIL_CREATE_NEW_ID = 0
  62. };
  63. /**
  64. flags you can pass into SOIL_load_OGL_texture()
  65. and SOIL_create_OGL_texture().
  66. (note that if SOIL_FLAG_DDS_LOAD_DIRECT is used
  67. the rest of the flags with the exception of
  68. SOIL_FLAG_TEXTURE_REPEATS will be ignored while
  69. loading already-compressed DDS files.)
  70. SOIL_FLAG_POWER_OF_TWO: force the image to be POT
  71. SOIL_FLAG_MIPMAPS: generate mipmaps for the texture
  72. SOIL_FLAG_TEXTURE_REPEATS: otherwise will clamp
  73. SOIL_FLAG_MULTIPLY_ALPHA: for using (GL_ONE,GL_ONE_MINUS_SRC_ALPHA) blending
  74. SOIL_FLAG_INVERT_Y: flip the image vertically
  75. SOIL_FLAG_COMPRESS_TO_DXT: if the card can display them, will convert RGB to DXT1, RGBA to DXT5
  76. SOIL_FLAG_DDS_LOAD_DIRECT: will load DDS files directly without _ANY_ additional processing
  77. SOIL_FLAG_NTSC_SAFE_RGB: clamps RGB components to the range [16,235]
  78. SOIL_FLAG_CoCg_Y: Google YCoCg; RGB=>CoYCg, RGBA=>CoCgAY
  79. SOIL_FLAG_TEXTURE_RECTANGE: uses ARB_texture_rectangle ; pixel indexed & no repeat or MIPmaps or cubemaps
  80. **/
  81. enum
  82. {
  83. SOIL_FLAG_POWER_OF_TWO = 1,
  84. SOIL_FLAG_MIPMAPS = 2,
  85. SOIL_FLAG_TEXTURE_REPEATS = 4,
  86. SOIL_FLAG_MULTIPLY_ALPHA = 8,
  87. SOIL_FLAG_INVERT_Y = 16,
  88. SOIL_FLAG_COMPRESS_TO_DXT = 32,
  89. SOIL_FLAG_DDS_LOAD_DIRECT = 64,
  90. SOIL_FLAG_NTSC_SAFE_RGB = 128,
  91. SOIL_FLAG_CoCg_Y = 256,
  92. SOIL_FLAG_TEXTURE_RECTANGLE = 512
  93. };
  94. /**
  95. The types of images that may be saved.
  96. (TGA supports uncompressed RGB / RGBA)
  97. (BMP supports uncompressed RGB)
  98. (DDS supports DXT1 and DXT5)
  99. **/
  100. enum
  101. {
  102. SOIL_SAVE_TYPE_TGA = 0,
  103. SOIL_SAVE_TYPE_BMP = 1,
  104. SOIL_SAVE_TYPE_DDS = 2
  105. };
  106. /**
  107. Defines the order of faces in a DDS cubemap.
  108. I recommend that you use the same order in single
  109. image cubemap files, so they will be interchangeable
  110. with DDS cubemaps when using SOIL.
  111. **/
  112. #define SOIL_DDS_CUBEMAP_FACE_ORDER "EWUDNS"
  113. /**
  114. The types of internal fake HDR representations
  115. SOIL_HDR_RGBE: RGB * pow( 2.0, A - 128.0 )
  116. SOIL_HDR_RGBdivA: RGB / A
  117. SOIL_HDR_RGBdivA2: RGB / (A*A)
  118. **/
  119. enum
  120. {
  121. SOIL_HDR_RGBE = 0,
  122. SOIL_HDR_RGBdivA = 1,
  123. SOIL_HDR_RGBdivA2 = 2
  124. };
  125. /**
  126. Loads an image from disk into an OpenGL texture.
  127. \param filename the name of the file to upload as a texture
  128. \param force_channels 0-image format, 1-luminous, 2-luminous/alpha, 3-RGB, 4-RGBA
  129. \param reuse_texture_ID 0-generate a new texture ID, otherwise reuse the texture ID (overwriting the old texture)
  130. \param flags can be any of SOIL_FLAG_POWER_OF_TWO | SOIL_FLAG_MIPMAPS | SOIL_FLAG_TEXTURE_REPEATS | SOIL_FLAG_MULTIPLY_ALPHA | SOIL_FLAG_INVERT_Y | SOIL_FLAG_COMPRESS_TO_DXT | SOIL_FLAG_DDS_LOAD_DIRECT
  131. \return 0-failed, otherwise returns the OpenGL texture handle
  132. **/
  133. unsigned int
  134. SOIL_load_OGL_texture
  135. (
  136. const char *filename,
  137. int force_channels,
  138. unsigned int reuse_texture_ID,
  139. unsigned int flags
  140. );
  141. /**
  142. Loads 6 images from disk into an OpenGL cubemap texture.
  143. \param x_pos_file the name of the file to upload as the +x cube face
  144. \param x_neg_file the name of the file to upload as the -x cube face
  145. \param y_pos_file the name of the file to upload as the +y cube face
  146. \param y_neg_file the name of the file to upload as the -y cube face
  147. \param z_pos_file the name of the file to upload as the +z cube face
  148. \param z_neg_file the name of the file to upload as the -z cube face
  149. \param force_channels 0-image format, 1-luminous, 2-luminous/alpha, 3-RGB, 4-RGBA
  150. \param reuse_texture_ID 0-generate a new texture ID, otherwise reuse the texture ID (overwriting the old texture)
  151. \param flags can be any of SOIL_FLAG_POWER_OF_TWO | SOIL_FLAG_MIPMAPS | SOIL_FLAG_TEXTURE_REPEATS | SOIL_FLAG_MULTIPLY_ALPHA | SOIL_FLAG_INVERT_Y | SOIL_FLAG_COMPRESS_TO_DXT | SOIL_FLAG_DDS_LOAD_DIRECT
  152. \return 0-failed, otherwise returns the OpenGL texture handle
  153. **/
  154. unsigned int
  155. SOIL_load_OGL_cubemap
  156. (
  157. const char *x_pos_file,
  158. const char *x_neg_file,
  159. const char *y_pos_file,
  160. const char *y_neg_file,
  161. const char *z_pos_file,
  162. const char *z_neg_file,
  163. int force_channels,
  164. unsigned int reuse_texture_ID,
  165. unsigned int flags
  166. );
  167. /**
  168. Loads 1 image from disk and splits it into an OpenGL cubemap texture.
  169. \param filename the name of the file to upload as a texture
  170. \param face_order the order of the faces in the file, any combination of NSWEUD, for North, South, Up, etc.
  171. \param force_channels 0-image format, 1-luminous, 2-luminous/alpha, 3-RGB, 4-RGBA
  172. \param reuse_texture_ID 0-generate a new texture ID, otherwise reuse the texture ID (overwriting the old texture)
  173. \param flags can be any of SOIL_FLAG_POWER_OF_TWO | SOIL_FLAG_MIPMAPS | SOIL_FLAG_TEXTURE_REPEATS | SOIL_FLAG_MULTIPLY_ALPHA | SOIL_FLAG_INVERT_Y | SOIL_FLAG_COMPRESS_TO_DXT | SOIL_FLAG_DDS_LOAD_DIRECT
  174. \return 0-failed, otherwise returns the OpenGL texture handle
  175. **/
  176. unsigned int
  177. SOIL_load_OGL_single_cubemap
  178. (
  179. const char *filename,
  180. const char face_order[6],
  181. int force_channels,
  182. unsigned int reuse_texture_ID,
  183. unsigned int flags
  184. );
  185. /**
  186. Loads an HDR image from disk into an OpenGL texture.
  187. \param filename the name of the file to upload as a texture
  188. \param fake_HDR_format SOIL_HDR_RGBE, SOIL_HDR_RGBdivA, SOIL_HDR_RGBdivA2
  189. \param reuse_texture_ID 0-generate a new texture ID, otherwise reuse the texture ID (overwriting the old texture)
  190. \param flags can be any of SOIL_FLAG_POWER_OF_TWO | SOIL_FLAG_MIPMAPS | SOIL_FLAG_TEXTURE_REPEATS | SOIL_FLAG_MULTIPLY_ALPHA | SOIL_FLAG_INVERT_Y | SOIL_FLAG_COMPRESS_TO_DXT
  191. \return 0-failed, otherwise returns the OpenGL texture handle
  192. **/
  193. unsigned int
  194. SOIL_load_OGL_HDR_texture
  195. (
  196. const char *filename,
  197. int fake_HDR_format,
  198. int rescale_to_max,
  199. unsigned int reuse_texture_ID,
  200. unsigned int flags
  201. );
  202. /**
  203. Loads an image from RAM into an OpenGL texture.
  204. \param buffer the image data in RAM just as if it were still in a file
  205. \param buffer_length the size of the buffer in bytes
  206. \param force_channels 0-image format, 1-luminous, 2-luminous/alpha, 3-RGB, 4-RGBA
  207. \param reuse_texture_ID 0-generate a new texture ID, otherwise reuse the texture ID (overwriting the old texture)
  208. \param flags can be any of SOIL_FLAG_POWER_OF_TWO | SOIL_FLAG_MIPMAPS | SOIL_FLAG_TEXTURE_REPEATS | SOIL_FLAG_MULTIPLY_ALPHA | SOIL_FLAG_INVERT_Y | SOIL_FLAG_COMPRESS_TO_DXT | SOIL_FLAG_DDS_LOAD_DIRECT
  209. \return 0-failed, otherwise returns the OpenGL texture handle
  210. **/
  211. unsigned int
  212. SOIL_load_OGL_texture_from_memory
  213. (
  214. const unsigned char *const buffer,
  215. int buffer_length,
  216. int force_channels,
  217. unsigned int reuse_texture_ID,
  218. unsigned int flags
  219. );
  220. /**
  221. Loads 6 images from memory into an OpenGL cubemap texture.
  222. \param x_pos_buffer the image data in RAM to upload as the +x cube face
  223. \param x_pos_buffer_length the size of the above buffer
  224. \param x_neg_buffer the image data in RAM to upload as the +x cube face
  225. \param x_neg_buffer_length the size of the above buffer
  226. \param y_pos_buffer the image data in RAM to upload as the +x cube face
  227. \param y_pos_buffer_length the size of the above buffer
  228. \param y_neg_buffer the image data in RAM to upload as the +x cube face
  229. \param y_neg_buffer_length the size of the above buffer
  230. \param z_pos_buffer the image data in RAM to upload as the +x cube face
  231. \param z_pos_buffer_length the size of the above buffer
  232. \param z_neg_buffer the image data in RAM to upload as the +x cube face
  233. \param z_neg_buffer_length the size of the above buffer
  234. \param force_channels 0-image format, 1-luminous, 2-luminous/alpha, 3-RGB, 4-RGBA
  235. \param reuse_texture_ID 0-generate a new texture ID, otherwise reuse the texture ID (overwriting the old texture)
  236. \param flags can be any of SOIL_FLAG_POWER_OF_TWO | SOIL_FLAG_MIPMAPS | SOIL_FLAG_TEXTURE_REPEATS | SOIL_FLAG_MULTIPLY_ALPHA | SOIL_FLAG_INVERT_Y | SOIL_FLAG_COMPRESS_TO_DXT | SOIL_FLAG_DDS_LOAD_DIRECT
  237. \return 0-failed, otherwise returns the OpenGL texture handle
  238. **/
  239. unsigned int
  240. SOIL_load_OGL_cubemap_from_memory
  241. (
  242. const unsigned char *const x_pos_buffer,
  243. int x_pos_buffer_length,
  244. const unsigned char *const x_neg_buffer,
  245. int x_neg_buffer_length,
  246. const unsigned char *const y_pos_buffer,
  247. int y_pos_buffer_length,
  248. const unsigned char *const y_neg_buffer,
  249. int y_neg_buffer_length,
  250. const unsigned char *const z_pos_buffer,
  251. int z_pos_buffer_length,
  252. const unsigned char *const z_neg_buffer,
  253. int z_neg_buffer_length,
  254. int force_channels,
  255. unsigned int reuse_texture_ID,
  256. unsigned int flags
  257. );
  258. /**
  259. Loads 1 image from RAM and splits it into an OpenGL cubemap texture.
  260. \param buffer the image data in RAM just as if it were still in a file
  261. \param buffer_length the size of the buffer in bytes
  262. \param face_order the order of the faces in the file, any combination of NSWEUD, for North, South, Up, etc.
  263. \param force_channels 0-image format, 1-luminous, 2-luminous/alpha, 3-RGB, 4-RGBA
  264. \param reuse_texture_ID 0-generate a new texture ID, otherwise reuse the texture ID (overwriting the old texture)
  265. \param flags can be any of SOIL_FLAG_POWER_OF_TWO | SOIL_FLAG_MIPMAPS | SOIL_FLAG_TEXTURE_REPEATS | SOIL_FLAG_MULTIPLY_ALPHA | SOIL_FLAG_INVERT_Y | SOIL_FLAG_COMPRESS_TO_DXT | SOIL_FLAG_DDS_LOAD_DIRECT
  266. \return 0-failed, otherwise returns the OpenGL texture handle
  267. **/
  268. unsigned int
  269. SOIL_load_OGL_single_cubemap_from_memory
  270. (
  271. const unsigned char *const buffer,
  272. int buffer_length,
  273. const char face_order[6],
  274. int force_channels,
  275. unsigned int reuse_texture_ID,
  276. unsigned int flags
  277. );
  278. /**
  279. Creates a 2D OpenGL texture from raw image data. Note that the raw data is
  280. _NOT_ freed after the upload (so the user can load various versions).
  281. \param data the raw data to be uploaded as an OpenGL texture
  282. \param width the width of the image in pixels
  283. \param height the height of the image in pixels
  284. \param channels the number of channels: 1-luminous, 2-luminous/alpha, 3-RGB, 4-RGBA
  285. \param reuse_texture_ID 0-generate a new texture ID, otherwise reuse the texture ID (overwriting the old texture)
  286. \param flags can be any of SOIL_FLAG_POWER_OF_TWO | SOIL_FLAG_MIPMAPS | SOIL_FLAG_TEXTURE_REPEATS | SOIL_FLAG_MULTIPLY_ALPHA | SOIL_FLAG_INVERT_Y | SOIL_FLAG_COMPRESS_TO_DXT
  287. \return 0-failed, otherwise returns the OpenGL texture handle
  288. **/
  289. unsigned int
  290. SOIL_create_OGL_texture
  291. (
  292. const unsigned char *const data,
  293. int width, int height, int channels,
  294. unsigned int reuse_texture_ID,
  295. unsigned int flags
  296. );
  297. /**
  298. Creates an OpenGL cubemap texture by splitting up 1 image into 6 parts.
  299. \param data the raw data to be uploaded as an OpenGL texture
  300. \param width the width of the image in pixels
  301. \param height the height of the image in pixels
  302. \param channels the number of channels: 1-luminous, 2-luminous/alpha, 3-RGB, 4-RGBA
  303. \param face_order the order of the faces in the file, and combination of NSWEUD, for North, South, Up, etc.
  304. \param reuse_texture_ID 0-generate a new texture ID, otherwise reuse the texture ID (overwriting the old texture)
  305. \param flags can be any of SOIL_FLAG_POWER_OF_TWO | SOIL_FLAG_MIPMAPS | SOIL_FLAG_TEXTURE_REPEATS | SOIL_FLAG_MULTIPLY_ALPHA | SOIL_FLAG_INVERT_Y | SOIL_FLAG_COMPRESS_TO_DXT | SOIL_FLAG_DDS_LOAD_DIRECT
  306. \return 0-failed, otherwise returns the OpenGL texture handle
  307. **/
  308. unsigned int
  309. SOIL_create_OGL_single_cubemap
  310. (
  311. const unsigned char *const data,
  312. int width, int height, int channels,
  313. const char face_order[6],
  314. unsigned int reuse_texture_ID,
  315. unsigned int flags
  316. );
  317. /**
  318. Captures the OpenGL window (RGB) and saves it to disk
  319. \return 0 if it failed, otherwise returns 1
  320. **/
  321. int
  322. SOIL_save_screenshot
  323. (
  324. const char *filename,
  325. int image_type,
  326. int x, int y,
  327. int width, int height
  328. );
  329. /**
  330. Loads an image from disk into an array of unsigned chars.
  331. Note that *channels return the original channel count of the
  332. image. If force_channels was other than SOIL_LOAD_AUTO,
  333. the resulting image has force_channels, but *channels may be
  334. different (if the original image had a different channel
  335. count).
  336. \return 0 if failed, otherwise returns 1
  337. **/
  338. unsigned char*
  339. SOIL_load_image
  340. (
  341. const char *filename,
  342. int *width, int *height, int *channels,
  343. int force_channels
  344. );
  345. /**
  346. Loads an image from memory into an array of unsigned chars.
  347. Note that *channels return the original channel count of the
  348. image. If force_channels was other than SOIL_LOAD_AUTO,
  349. the resulting image has force_channels, but *channels may be
  350. different (if the original image had a different channel
  351. count).
  352. \return 0 if failed, otherwise returns 1
  353. **/
  354. unsigned char*
  355. SOIL_load_image_from_memory
  356. (
  357. const unsigned char *const buffer,
  358. int buffer_length,
  359. int *width, int *height, int *channels,
  360. int force_channels
  361. );
  362. /**
  363. Saves an image from an array of unsigned chars (RGBA) to disk
  364. \return 0 if failed, otherwise returns 1
  365. **/
  366. int
  367. SOIL_save_image
  368. (
  369. const char *filename,
  370. int image_type,
  371. int width, int height, int channels,
  372. const unsigned char *const data
  373. );
  374. /**
  375. Frees the image data (note, this is just C's "free()"...this function is
  376. present mostly so C++ programmers don't forget to use "free()" and call
  377. "delete []" instead [8^)
  378. **/
  379. void
  380. SOIL_free_image_data
  381. (
  382. unsigned char *img_data
  383. );
  384. /**
  385. This function resturn a pointer to a string describing the last thing
  386. that happened inside SOIL. It can be used to determine why an image
  387. failed to load.
  388. **/
  389. const char*
  390. SOIL_last_result
  391. (
  392. void
  393. );
  394. #ifdef __cplusplus
  395. }
  396. #endif
  397. #endif /* HEADER_SIMPLE_OPENGL_IMAGE_LIBRARY */