Subversion Repositories gelsvn

Rev

Details | Last modification | View Log | RSS feed

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