1 /**************************************************************************
3 * Copyright 2013-2014 RAD Game Tools and Valve Software
4 * Copyright 2010-2014 Rich Geldreich and Tenacious Software LLC
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 **************************************************************************/
27 #ifndef STBI_INCLUDE_STB_IMAGE_H
28 #define STBI_INCLUDE_STB_IMAGE_H
30 #include "vogl_core.h"
32 //// begin header file ////////////////////////////////////////////////////
35 // - no progressive/interlaced support (jpeg, png)
36 // - 8-bit samples only (jpeg, png)
38 // - channel subsampling of at most 2 in each dimension (jpeg)
39 // - no delayed line count (jpeg) -- IJG doesn't support either
41 // Basic usage (see HDR discussion below):
43 // unsigned char *data = stbi_load(filename, &x, &y, &n, 0);
44 // // ... process data if not NULL ...
45 // // ... x = width, y = height, n = # 8-bit components per pixel ...
46 // // ... replace '0' with '1'..'4' to force that many components per pixel
47 // stbi_image_free(data)
49 // Standard parameters:
50 // int *x -- outputs image width in pixels
51 // int *y -- outputs image height in pixels
52 // int *comp -- outputs # of image components in image file
53 // int req_comp -- if non-zero, # of image components requested in result
55 // The return value from an image loader is an 'unsigned char *' which points
56 // to the pixel data. The pixel data consists of *y scanlines of *x pixels,
57 // with each pixel consisting of N interleaved 8-bit components; the first
58 // pixel pointed to is top-left-most in the image. There is no padding between
59 // image scanlines or between pixels, regardless of format. The number of
60 // components N is 'req_comp' if req_comp is non-zero, or *comp otherwise.
61 // If req_comp is non-zero, *comp has the number of components that _would_
62 // have been output otherwise. E.g. if you set req_comp to 4, you will always
63 // get RGBA output, but you can check *comp to easily see if it's opaque.
65 // An output image with N components has the following components interleaved
66 // in this order in each pixel:
72 // 4 red, green, blue, alpha
74 // If image loading fails for any reason, the return value will be NULL,
75 // and *x, *y, *comp will be unchanged. The function stbi_failure_reason()
76 // can be queried for an extremely brief, end-user unfriendly explanation
77 // of why the load failed. Define STBI_NO_FAILURE_STRINGS to avoid
78 // compiling these strings at all, and STBI_FAILURE_USERMSG to get slightly
79 // more user-friendly ones.
81 // Paletted PNG and BMP images are automatically depalettized.
84 // ===========================================================================
86 // HDR image support (disable by defining STBI_NO_HDR)
88 // stb_image now supports loading HDR images in general, and currently
89 // the Radiance .HDR file format, although the support is provided
90 // generically. You can still load any file through the existing interface;
91 // if you attempt to load an HDR file, it will be automatically remapped to
92 // LDR, assuming gamma 2.2 and an arbitrary scale factor defaulting to 1;
93 // both of these constants can be reconfigured through this interface:
95 // stbi_hdr_to_ldr_gamma(2.2f);
96 // stbi_hdr_to_ldr_scale(1.0f);
98 // (note, do not use _inverse_ constants; stbi_image will invert them
101 // Additionally, there is a new, parallel interface for loading files as
102 // (linear) floats to preserve the full dynamic range:
104 // float *data = stbi_loadf(filename, &x, &y, &n, 0);
106 // If you load LDR images through this interface, those images will
107 // be promoted to floating point values, run through the inverse of
108 // constants corresponding to the above:
110 // stbi_ldr_to_hdr_scale(1.0f);
111 // stbi_ldr_to_hdr_gamma(2.2f);
113 // Finally, given a filename (or an open file or memory block--see header
114 // file for details) containing image data, you can query for the "most
115 // appropriate" interface to use (that is, whether the image is HDR or
118 // stbi_is_hdr(char *filename);
120 #define _CRT_SECURE_NO_WARNINGS
122 #ifndef STBI_NO_STDIO
129 #define STBI_VERSION 1
133 STBI_default = 0, // only used for req_comp
140 typedef unsigned char stbi_uc;
148 #if !defined(STBI_NO_WRITE) && !defined(STBI_NO_STDIO)
149 // write a BMP/TGA file given tightly packed 'comp' channels (no padding, nor bmp-stride-padding)
150 // (you must include the appropriate extension in the filename).
151 // returns TRUE on success, FALSE if couldn't open file, error writing file
152 extern int stbi_write_bmp(char const *filename, int x, int y, int comp, const void *data);
154 extern int stbi_write_bmp_w(wchar_t const *filename, int x, int y, int comp, const void *data);
156 extern int stbi_write_tga(char const *filename, int x, int y, int comp, const void *data);
158 extern int stbi_write_tga_w(wchar_t const *filename, int x, int y, int comp, const void *data);
162 // PRIMARY API - works on images of any type
164 // load image by filename, open file, or memory buffer
165 #ifndef STBI_NO_STDIO
166 extern stbi_uc *stbi_load(char const *filename, int *x, int *y, int *comp, int req_comp);
168 extern stbi_uc *stbi_load_w(wchar_t const *filename, int *x, int *y, int *comp, int req_comp);
170 extern stbi_uc *stbi_load_from_file(FILE *f, int *x, int *y, int *comp, int req_comp);
171 extern int stbi_info_from_file(FILE *f, int *x, int *y, int *comp);
173 extern stbi_uc *stbi_load_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp);
174 // for stbi_load_from_file, file pointer is left pointing immediately after image
177 #ifndef STBI_NO_STDIO
178 extern float *stbi_loadf(char const *filename, int *x, int *y, int *comp, int req_comp);
179 extern float *stbi_loadf_from_file(FILE *f, int *x, int *y, int *comp, int req_comp);
181 extern float *stbi_loadf_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp);
183 extern void stbi_hdr_to_ldr_gamma(float gamma);
184 extern void stbi_hdr_to_ldr_scale(float scale);
186 extern void stbi_ldr_to_hdr_gamma(float gamma);
187 extern void stbi_ldr_to_hdr_scale(float scale);
189 #endif // STBI_NO_HDR
191 // get a VERY brief reason for failure
193 extern const char *stbi_failure_reason(void);
195 // free the loaded image -- this is just stb_free()
196 extern void stbi_image_free(void *retval_from_stbi_load);
198 // get image dimensions & components without fully decoding
199 extern int stbi_info_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp);
200 extern int stbi_is_hdr_from_memory(stbi_uc const *buffer, int len);
201 #ifndef STBI_NO_STDIO
202 extern int stbi_info(char const *filename, int *x, int *y, int *comp);
203 extern int stbi_is_hdr(char const *filename);
204 extern int stbi_is_hdr_from_file(FILE *f);
207 // ZLIB client - used by PNG, available for other purposes
209 extern char *stbi_zlib_decode_malloc_guesssize(const char *buffer, int len, int initial_size, int *outlen);
210 extern char *stbi_zlib_decode_malloc(const char *buffer, int len, int *outlen);
211 extern int stbi_zlib_decode_buffer(char *obuffer, int olen, const char *ibuffer, int ilen);
213 extern char *stbi_zlib_decode_noheader_malloc(const char *buffer, int len, int *outlen);
214 extern int stbi_zlib_decode_noheader_buffer(char *obuffer, int olen, const char *ibuffer, int ilen);
216 // TYPE-SPECIFIC ACCESS
219 extern int stbi_jpeg_test_memory(stbi_uc const *buffer, int len);
220 extern stbi_uc *stbi_jpeg_load_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp);
221 extern int stbi_jpeg_info_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp);
223 #ifndef STBI_NO_STDIO
224 extern stbi_uc *stbi_jpeg_load(char const *filename, int *x, int *y, int *comp, int req_comp);
225 extern int stbi_jpeg_test_file(FILE *f);
226 extern stbi_uc *stbi_jpeg_load_from_file(FILE *f, int *x, int *y, int *comp, int req_comp);
228 extern int stbi_jpeg_info(char const *filename, int *x, int *y, int *comp);
229 extern int stbi_jpeg_info_from_file(FILE *f, int *x, int *y, int *comp);
233 extern int stbi_png_test_memory(stbi_uc const *buffer, int len);
234 extern stbi_uc *stbi_png_load_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp);
235 extern int stbi_png_info_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp);
237 #ifndef STBI_NO_STDIO
238 extern stbi_uc *stbi_png_load(char const *filename, int *x, int *y, int *comp, int req_comp);
239 extern int stbi_png_info(char const *filename, int *x, int *y, int *comp);
240 extern int stbi_png_test_file(FILE *f);
241 extern stbi_uc *stbi_png_load_from_file(FILE *f, int *x, int *y, int *comp, int req_comp);
242 extern int stbi_png_info_from_file(FILE *f, int *x, int *y, int *comp);
246 extern int stbi_bmp_test_memory(stbi_uc const *buffer, int len);
248 extern stbi_uc *stbi_bmp_load(char const *filename, int *x, int *y, int *comp, int req_comp);
249 extern stbi_uc *stbi_bmp_load_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp);
250 #ifndef STBI_NO_STDIO
251 extern int stbi_bmp_test_file(FILE *f);
252 extern stbi_uc *stbi_bmp_load_from_file(FILE *f, int *x, int *y, int *comp, int req_comp);
256 extern int stbi_tga_test_memory(stbi_uc const *buffer, int len);
258 extern stbi_uc *stbi_tga_load(char const *filename, int *x, int *y, int *comp, int req_comp);
259 extern stbi_uc *stbi_tga_load_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp);
260 #ifndef STBI_NO_STDIO
261 extern int stbi_tga_test_file(FILE *f);
262 extern stbi_uc *stbi_tga_load_from_file(FILE *f, int *x, int *y, int *comp, int req_comp);
266 extern int stbi_psd_test_memory(stbi_uc const *buffer, int len);
268 extern stbi_uc *stbi_psd_load(char const *filename, int *x, int *y, int *comp, int req_comp);
269 extern stbi_uc *stbi_psd_load_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp);
270 #ifndef STBI_NO_STDIO
271 extern int stbi_psd_test_file(FILE *f);
272 extern stbi_uc *stbi_psd_load_from_file(FILE *f, int *x, int *y, int *comp, int req_comp);
276 extern int stbi_hdr_test_memory(stbi_uc const *buffer, int len);
278 extern float *stbi_hdr_load(char const *filename, int *x, int *y, int *comp, int req_comp);
279 extern float *stbi_hdr_load_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp);
280 #ifndef STBI_NO_STDIO
281 extern int stbi_hdr_test_file(FILE *f);
282 extern float *stbi_hdr_load_from_file(FILE *f, int *x, int *y, int *comp, int req_comp);
285 // define new loaders
288 int (*test_memory)(stbi_uc const *buffer, int len);
289 stbi_uc *(*load_from_memory)(stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp);
290 #ifndef STBI_NO_STDIO
291 int (*test_file)(FILE *f);
292 stbi_uc *(*load_from_file)(FILE *f, int *x, int *y, int *comp, int req_comp);
296 // register a loader by filling out the above structure (you must defined ALL functions)
297 // returns 1 if added or already added, 0 if not added (too many loaders)
299 extern int stbi_register_loader(stbi_loader *loader);
301 // define faster low-level operations (typically SIMD support)
303 typedef void (*stbi_idct_8x8)(uint8 *out, int out_stride, short data[64], unsigned short *dequantize);
304 // compute an integer IDCT on "input"
305 // input[x] = data[x] * dequantize[x]
306 // write results to 'out': 64 samples, each run of 8 spaced by 'out_stride'
307 // CLAMP results to 0..255
308 typedef void (*stbi_YCbCr_to_RGB_run)(uint8 *output, uint8 const *y, uint8 const *cb, uint8 const *cr, int count, int step);
309 // compute a conversion from YCbCr to RGB
311 // write pixels to 'output'; each pixel is 'step' bytes (either 3 or 4; if 4, write '255' as 4th), order R,G,B
312 // y: Y input channel
313 // cb: Cb input channel; scale/biased to be 0..255
314 // cr: Cr input channel; scale/biased to be 0..255
316 extern void stbi_install_idct(stbi_idct_8x8 func);
317 extern void stbi_install_YCbCr_to_RGB(stbi_YCbCr_to_RGB_run func);
327 //// end header file /////////////////////////////////////////////////////
328 #endif // STBI_INCLUDE_STB_IMAGE_H