]> git.cworth.org Git - vogl/blob - src/voglcore/vogl_stb_image.h
Initial vogl checkin
[vogl] / src / voglcore / vogl_stb_image.h
1 /**************************************************************************
2  *
3  * Copyright 2013-2014 RAD Game Tools and Valve Software
4  * Copyright 2010-2014 Rich Geldreich and Tenacious Software LLC
5  * All Rights Reserved.
6  *
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:
13  *
14  * The above copyright notice and this permission notice shall be included in
15  * all copies or substantial portions of the Software.
16  *
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
23  * THE SOFTWARE.
24  *
25  **************************************************************************/
26
27 #ifndef STBI_INCLUDE_STB_IMAGE_H
28 #define STBI_INCLUDE_STB_IMAGE_H
29
30 #include "vogl_core.h"
31
32 ////   begin header file  ////////////////////////////////////////////////////
33 //
34 // Limitations:
35 //    - no progressive/interlaced support (jpeg, png)
36 //    - 8-bit samples only (jpeg, png)
37 //    - not threadsafe
38 //    - channel subsampling of at most 2 in each dimension (jpeg)
39 //    - no delayed line count (jpeg) -- IJG doesn't support either
40 //
41 // Basic usage (see HDR discussion below):
42 //    int x,y,n;
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)
48 //
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
54 //
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.
64 //
65 // An output image with N components has the following components interleaved
66 // in this order in each pixel:
67 //
68 //     N=#comp     components
69 //       1           grey
70 //       2           grey, alpha
71 //       3           red, green, blue
72 //       4           red, green, blue, alpha
73 //
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.
80 //
81 // Paletted PNG and BMP images are automatically depalettized.
82 //
83 //
84 // ===========================================================================
85 //
86 // HDR image support   (disable by defining STBI_NO_HDR)
87 //
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:
94 //
95 //     stbi_hdr_to_ldr_gamma(2.2f);
96 //     stbi_hdr_to_ldr_scale(1.0f);
97 //
98 // (note, do not use _inverse_ constants; stbi_image will invert them
99 // appropriately).
100 //
101 // Additionally, there is a new, parallel interface for loading files as
102 // (linear) floats to preserve the full dynamic range:
103 //
104 //    float *data = stbi_loadf(filename, &x, &y, &n, 0);
105 //
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:
109 //
110 //     stbi_ldr_to_hdr_scale(1.0f);
111 //     stbi_ldr_to_hdr_gamma(2.2f);
112 //
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
116 // not), using:
117 //
118 //     stbi_is_hdr(char *filename);
119
120 #define _CRT_SECURE_NO_WARNINGS
121
122 #ifndef STBI_NO_STDIO
123 #include <stdio.h>
124 #endif
125
126 namespace vogl
127 {
128
129 #define STBI_VERSION 1
130
131     enum
132     {
133         STBI_default = 0, // only used for req_comp
134         STBI_grey = 1,
135         STBI_grey_alpha = 2,
136         STBI_rgb = 3,
137         STBI_rgb_alpha = 4,
138     };
139
140     typedef unsigned char stbi_uc;
141
142 //#ifdef __cplusplus
143 //extern "C" {
144 //#endif
145
146 // WRITING API
147
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);
153 #ifdef _MSC_VER
154     extern int stbi_write_bmp_w(wchar_t const *filename, int x, int y, int comp, const void *data);
155 #endif
156     extern int stbi_write_tga(char const *filename, int x, int y, int comp, const void *data);
157 #ifdef _MSC_VER
158     extern int stbi_write_tga_w(wchar_t const *filename, int x, int y, int comp, const void *data);
159 #endif
160 #endif
161
162 // PRIMARY API - works on images of any type
163
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);
167 #ifdef _MSC_VER
168     extern stbi_uc *stbi_load_w(wchar_t const *filename, int *x, int *y, int *comp, int req_comp);
169 #endif
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);
172 #endif
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
175
176 #ifndef STBI_NO_HDR
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);
180 #endif
181     extern float *stbi_loadf_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp);
182
183     extern void stbi_hdr_to_ldr_gamma(float gamma);
184     extern void stbi_hdr_to_ldr_scale(float scale);
185
186     extern void stbi_ldr_to_hdr_gamma(float gamma);
187     extern void stbi_ldr_to_hdr_scale(float scale);
188
189 #endif // STBI_NO_HDR
190
191     // get a VERY brief reason for failure
192     // NOT THREADSAFE
193     extern const char *stbi_failure_reason(void);
194
195     // free the loaded image -- this is just stb_free()
196     extern void stbi_image_free(void *retval_from_stbi_load);
197
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);
205 #endif
206
207     // ZLIB client - used by PNG, available for other purposes
208
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);
212
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);
215
216     // TYPE-SPECIFIC ACCESS
217
218     // is it a jpeg?
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);
222
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);
227
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);
230 #endif
231
232     // is it a png?
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);
236
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);
243 #endif
244
245     // is it a bmp?
246     extern int stbi_bmp_test_memory(stbi_uc const *buffer, int len);
247
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);
253 #endif
254
255     // is it a tga?
256     extern int stbi_tga_test_memory(stbi_uc const *buffer, int len);
257
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);
263 #endif
264
265     // is it a psd?
266     extern int stbi_psd_test_memory(stbi_uc const *buffer, int len);
267
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);
273 #endif
274
275     // is it an hdr?
276     extern int stbi_hdr_test_memory(stbi_uc const *buffer, int len);
277
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);
283 #endif
284
285     // define new loaders
286     typedef struct
287     {
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);
293 #endif
294     } stbi_loader;
295
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)
298     // NOT THREADSAFE
299     extern int stbi_register_loader(stbi_loader *loader);
300
301 // define faster low-level operations (typically SIMD support)
302 #if STBI_SIMD
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
310     //     'count' pixels
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
315
316     extern void stbi_install_idct(stbi_idct_8x8 func);
317     extern void stbi_install_YCbCr_to_RGB(stbi_YCbCr_to_RGB_run func);
318 #endif // STBI_SIMD
319
320     //#ifdef __cplusplus
321     //}
322     //#endif
323 }
324
325 //
326 //
327 ////   end header file   /////////////////////////////////////////////////////
328 #endif // STBI_INCLUDE_STB_IMAGE_H