Subversion Repositories gelsvn

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
597 jab 1
/**
2
 * @file stb_image_aug.h
3
 * @brief public domain JPEG/PNG reader
4
 */
5
/* stbi-1.16 - public domain JPEG/PNG reader - http://nothings.org/stb_image.c
6
                      when you control the images you're loading
7
 
8
   QUICK NOTES:
9
      Primarily of interest to game developers and other people who can
10
          avoid problematic images and only need the trivial interface
11
 
12
      JPEG baseline (no JPEG progressive, no oddball channel decimations)
13
      PNG non-interlaced
14
      BMP non-1bpp, non-RLE
15
      TGA (not sure what subset, if a subset)
16
      PSD (composited view only, no extra channels)
17
      HDR (radiance rgbE format)
18
      writes BMP,TGA (define STBI_NO_WRITE to remove code)
19
      decoded from memory or through stdio FILE (define STBI_NO_STDIO to remove code)
20
      supports installable dequantizing-IDCT, YCbCr-to-RGB conversion (define STBI_SIMD)
21
 
22
   TODO:
23
      stbi_info_*
24
 
25
   history:
26
      1.16   major bugfix - convert_format converted one too many pixels
27
      1.15   initialize some fields for thread safety
28
      1.14   fix threadsafe conversion bug; header-file-only version (#define STBI_HEADER_FILE_ONLY before including)
29
      1.13   threadsafe
30
      1.12   const qualifiers in the API
31
      1.11   Support installable IDCT, colorspace conversion routines
32
      1.10   Fixes for 64-bit (don't use "unsigned long")
33
             optimized upsampling by Fabian "ryg" Giesen
34
      1.09   Fix format-conversion for PSD code (bad global variables!)
35
      1.08   Thatcher Ulrich's PSD code integrated by Nicolas Schulz
36
      1.07   attempt to fix C++ warning/errors again
37
      1.06   attempt to fix C++ warning/errors again
38
      1.05   fix TGA loading to return correct *comp and use good luminance calc
39
      1.04   default float alpha is 1, not 255; use 'void *' for stbi_image_free
40
      1.03   bugfixes to STBI_NO_STDIO, STBI_NO_HDR
41
      1.02   support for (subset of) HDR files, float interface for preferred access to them
42
      1.01   fix bug: possible bug in handling right-side up bmps... not sure
43
             fix bug: the stbi_bmp_load() and stbi_tga_load() functions didn't work at all
44
      1.00   interface to zlib that skips zlib header
45
      0.99   correct handling of alpha in palette
46
      0.98   TGA loader by lonesock; dynamically add loaders (untested)
47
      0.97   jpeg errors on too large a file; also catch another malloc failure
48
      0.96   fix detection of invalid v value - particleman@mollyrocket forum
49
      0.95   during header scan, seek to markers in case of padding
50
      0.94   STBI_NO_STDIO to disable stdio usage; rename all #defines the same
51
      0.93   handle jpegtran output; verbose errors
52
      0.92   read 4,8,16,24,32-bit BMP files of several formats
53
      0.91   output 24-bit Windows 3.0 BMP files
54
      0.90   fix a few more warnings; bump version number to approach 1.0
55
      0.61   bugfixes due to Marc LeBlanc, Christopher Lloyd
56
      0.60   fix compiling as c++
57
      0.59   fix warnings: merge Dave Moore's -Wall fixes
58
      0.58   fix bug: zlib uncompressed mode len/nlen was wrong endian
59
      0.57   fix bug: jpg last huffman symbol before marker was >9 bits but less
60
                      than 16 available
61
      0.56   fix bug: zlib uncompressed mode len vs. nlen
62
      0.55   fix bug: restart_interval not initialized to 0
63
      0.54   allow NULL for 'int *comp'
64
      0.53   fix bug in png 3->4; speedup png decoding
65
      0.52   png handles req_comp=3,4 directly; minor cleanup; jpeg comments
66
      0.51   obey req_comp requests, 1-component jpegs return as 1-component,
67
             on 'test' only check type, not whether we support this variant
68
*/
69
 
70
#ifndef HEADER_STB_IMAGE_AUGMENTED
71
#define HEADER_STB_IMAGE_AUGMENTED
72
 
73
////   begin header file  ////////////////////////////////////////////////////
74
//
75
// Limitations:
76
//    - no progressive/interlaced support (jpeg, png)
77
//    - 8-bit samples only (jpeg, png)
78
//    - not threadsafe
79
//    - channel subsampling of at most 2 in each dimension (jpeg)
80
//    - no delayed line count (jpeg) -- IJG doesn't support either
81
//
82
// Basic usage (see HDR discussion below):
83
//    int x,y,n;
84
//    unsigned char *data = stbi_load(filename, &x, &y, &n, 0);
85
//    // ... process data if not NULL ... 
86
//    // ... x = width, y = height, n = # 8-bit components per pixel ...
87
//    // ... replace '0' with '1'..'4' to force that many components per pixel
88
//    stbi_image_free(data)
89
//
90
// Standard parameters:
91
//    int *x       -- outputs image width in pixels
92
//    int *y       -- outputs image height in pixels
93
//    int *comp    -- outputs # of image components in image file
94
//    int req_comp -- if non-zero, # of image components requested in result
95
//
96
// The return value from an image loader is an 'unsigned char *' which points
97
// to the pixel data. The pixel data consists of *y scanlines of *x pixels,
98
// with each pixel consisting of N interleaved 8-bit components; the first
99
// pixel pointed to is top-left-most in the image. There is no padding between
100
// image scanlines or between pixels, regardless of format. The number of
101
// components N is 'req_comp' if req_comp is non-zero, or *comp otherwise.
102
// If req_comp is non-zero, *comp has the number of components that _would_
103
// have been output otherwise. E.g. if you set req_comp to 4, you will always
104
// get RGBA output, but you can check *comp to easily see if it's opaque.
105
//
106
// An output image with N components has the following components interleaved
107
// in this order in each pixel:
108
//
109
//     N=#comp     components
110
//       1           grey
111
//       2           grey, alpha
112
//       3           red, green, blue
113
//       4           red, green, blue, alpha
114
//
115
// If image loading fails for any reason, the return value will be NULL,
116
// and *x, *y, *comp will be unchanged. The function stbi_failure_reason()
117
// can be queried for an extremely brief, end-user unfriendly explanation
118
// of why the load failed. Define STBI_NO_FAILURE_STRINGS to avoid
119
// compiling these strings at all, and STBI_FAILURE_USERMSG to get slightly
120
// more user-friendly ones.
121
//
122
// Paletted PNG and BMP images are automatically depalettized.
123
//
124
//
125
// ===========================================================================
126
//
127
// HDR image support   (disable by defining STBI_NO_HDR)
128
//
129
// stb_image now supports loading HDR images in general, and currently
130
// the Radiance .HDR file format, although the support is provided
131
// generically. You can still load any file through the existing interface;
132
// if you attempt to load an HDR file, it will be automatically remapped to
133
// LDR, assuming gamma 2.2 and an arbitrary scale factor defaulting to 1;
134
// both of these constants can be reconfigured through this interface:
135
//
136
//     stbi_hdr_to_ldr_gamma(2.2f);
137
//     stbi_hdr_to_ldr_scale(1.0f);
138
//
139
// (note, do not use _inverse_ constants; stbi_image will invert them
140
// appropriately).
141
//
142
// Additionally, there is a new, parallel interface for loading files as
143
// (linear) floats to preserve the full dynamic range:
144
//
145
//    float *data = stbi_loadf(filename, &x, &y, &n, 0);
146
// 
147
// If you load LDR images through this interface, those images will
148
// be promoted to floating point values, run through the inverse of
149
// constants corresponding to the above:
150
//
151
//     stbi_ldr_to_hdr_scale(1.0f);
152
//     stbi_ldr_to_hdr_gamma(2.2f);
153
//
154
// Finally, given a filename (or an open file or memory block--see header
155
// file for details) containing image data, you can query for the "most
156
// appropriate" interface to use (that is, whether the image is HDR or
157
// not), using:
158
//
159
//     stbi_is_hdr(char *filename);
160
 
161
#ifndef STBI_NO_STDIO
162
#include <stdio.h>
163
#endif
164
 
165
#define STBI_VERSION 1
166
 
167
enum
168
{
169
   STBI_default = 0, // only used for req_comp
170
 
171
   STBI_grey       = 1,
172
   STBI_grey_alpha = 2,
173
   STBI_rgb        = 3,
174
   STBI_rgb_alpha  = 4,
175
};
176
 
177
typedef unsigned char stbi_uc;
178
 
179
#ifdef __cplusplus
180
extern "C" {
181
#endif
182
 
183
// WRITING API
184
 
185
#if !defined(STBI_NO_WRITE) && !defined(STBI_NO_STDIO)
186
// write a BMP/TGA file given tightly packed 'comp' channels (no padding, nor bmp-stride-padding)
187
// (you must include the appropriate extension in the filename).
188
// returns TRUE on success, FALSE if couldn't open file, error writing file
189
extern int      stbi_write_bmp       (char const *filename,     int x, int y, int comp, void *data);
190
extern int      stbi_write_tga       (char const *filename,     int x, int y, int comp, void *data);
191
#endif
192
 
193
// PRIMARY API - works on images of any type
194
 
195
// load image by filename, open file, or memory buffer
196
#ifndef STBI_NO_STDIO
197
extern stbi_uc *stbi_load            (char const *filename,     int *x, int *y, int *comp, int req_comp);
198
extern stbi_uc *stbi_load_from_file  (FILE *f,                  int *x, int *y, int *comp, int req_comp);
199
extern int      stbi_info_from_file  (FILE *f,                  int *x, int *y, int *comp);
200
#endif
201
extern stbi_uc *stbi_load_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp);
202
// for stbi_load_from_file, file pointer is left pointing immediately after image
203
 
204
#ifndef STBI_NO_HDR
205
#ifndef STBI_NO_STDIO
206
extern float *stbi_loadf            (char const *filename,     int *x, int *y, int *comp, int req_comp);
207
extern float *stbi_loadf_from_file  (FILE *f,                  int *x, int *y, int *comp, int req_comp);
208
#endif
209
extern float *stbi_loadf_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp);
210
 
211
extern void   stbi_hdr_to_ldr_gamma(float gamma);
212
extern void   stbi_hdr_to_ldr_scale(float scale);
213
 
214
extern void   stbi_ldr_to_hdr_gamma(float gamma);
215
extern void   stbi_ldr_to_hdr_scale(float scale);
216
 
217
#endif // STBI_NO_HDR
218
 
219
// get a VERY brief reason for failure
220
// NOT THREADSAFE
221
extern char    *stbi_failure_reason  (void); 
222
 
223
// free the loaded image -- this is just free()
224
extern void     stbi_image_free      (void *retval_from_stbi_load);
225
 
226
// get image dimensions & components without fully decoding
227
extern int      stbi_info_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp);
228
extern int      stbi_is_hdr_from_memory(stbi_uc const *buffer, int len);
229
#ifndef STBI_NO_STDIO
230
extern int      stbi_info            (char const *filename,     int *x, int *y, int *comp);
231
extern int      stbi_is_hdr          (char const *filename);
232
extern int      stbi_is_hdr_from_file(FILE *f);
233
#endif
234
 
235
// ZLIB client - used by PNG, available for other purposes
236
 
237
extern char *stbi_zlib_decode_malloc_guesssize(const char *buffer, int len, int initial_size, int *outlen);
238
extern char *stbi_zlib_decode_malloc(const char *buffer, int len, int *outlen);
239
extern int   stbi_zlib_decode_buffer(char *obuffer, int olen, const char *ibuffer, int ilen);
240
 
241
extern char *stbi_zlib_decode_noheader_malloc(const char *buffer, int len, int *outlen);
242
extern int   stbi_zlib_decode_noheader_buffer(char *obuffer, int olen, const char *ibuffer, int ilen);
243
 
244
// TYPE-SPECIFIC ACCESS
245
 
246
// is it a jpeg?
247
extern int      stbi_jpeg_test_memory     (stbi_uc const *buffer, int len);
248
extern stbi_uc *stbi_jpeg_load_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp);
249
extern int      stbi_jpeg_info_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp);
250
 
251
#ifndef STBI_NO_STDIO
252
extern stbi_uc *stbi_jpeg_load            (char const *filename,     int *x, int *y, int *comp, int req_comp);
253
extern int      stbi_jpeg_test_file       (FILE *f);
254
extern stbi_uc *stbi_jpeg_load_from_file  (FILE *f,                  int *x, int *y, int *comp, int req_comp);
255
 
256
extern int      stbi_jpeg_info            (char const *filename,     int *x, int *y, int *comp);
257
extern int      stbi_jpeg_info_from_file  (FILE *f,                  int *x, int *y, int *comp);
258
#endif
259
 
260
// is it a png?
261
extern int      stbi_png_test_memory      (stbi_uc const *buffer, int len);
262
extern stbi_uc *stbi_png_load_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp);
263
extern int      stbi_png_info_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp);
264
 
265
#ifndef STBI_NO_STDIO
266
extern stbi_uc *stbi_png_load             (char const *filename,     int *x, int *y, int *comp, int req_comp);
267
extern int      stbi_png_info             (char const *filename,     int *x, int *y, int *comp);
268
extern int      stbi_png_test_file        (FILE *f);
269
extern stbi_uc *stbi_png_load_from_file   (FILE *f,                  int *x, int *y, int *comp, int req_comp);
270
extern int      stbi_png_info_from_file   (FILE *f,                  int *x, int *y, int *comp);
271
#endif
272
 
273
// is it a bmp?
274
extern int      stbi_bmp_test_memory      (stbi_uc const *buffer, int len);
275
 
276
extern stbi_uc *stbi_bmp_load             (char const *filename,     int *x, int *y, int *comp, int req_comp);
277
extern stbi_uc *stbi_bmp_load_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp);
278
#ifndef STBI_NO_STDIO
279
extern int      stbi_bmp_test_file        (FILE *f);
280
extern stbi_uc *stbi_bmp_load_from_file   (FILE *f,                  int *x, int *y, int *comp, int req_comp);
281
#endif
282
 
283
// is it a tga?
284
extern int      stbi_tga_test_memory      (stbi_uc const *buffer, int len);
285
 
286
extern stbi_uc *stbi_tga_load             (char const *filename,     int *x, int *y, int *comp, int req_comp);
287
extern stbi_uc *stbi_tga_load_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp);
288
#ifndef STBI_NO_STDIO
289
extern int      stbi_tga_test_file        (FILE *f);
290
extern stbi_uc *stbi_tga_load_from_file   (FILE *f,                  int *x, int *y, int *comp, int req_comp);
291
#endif
292
 
293
// is it a psd?
294
extern int      stbi_psd_test_memory      (stbi_uc const *buffer, int len);
295
 
296
extern stbi_uc *stbi_psd_load             (char const *filename,     int *x, int *y, int *comp, int req_comp);
297
extern stbi_uc *stbi_psd_load_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp);
298
#ifndef STBI_NO_STDIO
299
extern int      stbi_psd_test_file        (FILE *f);
300
extern stbi_uc *stbi_psd_load_from_file   (FILE *f,                  int *x, int *y, int *comp, int req_comp);
301
#endif
302
 
303
// is it an hdr?
304
extern int      stbi_hdr_test_memory      (stbi_uc const *buffer, int len);
305
 
306
extern float *  stbi_hdr_load             (char const *filename,     int *x, int *y, int *comp, int req_comp);
307
extern float *  stbi_hdr_load_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp);
308
extern stbi_uc *stbi_hdr_load_rgbe        (char const *filename,           int *x, int *y, int *comp, int req_comp);
309
extern float *  stbi_hdr_load_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp);
310
#ifndef STBI_NO_STDIO
311
extern int      stbi_hdr_test_file        (FILE *f);
312
extern float *  stbi_hdr_load_from_file   (FILE *f,                  int *x, int *y, int *comp, int req_comp);
313
extern stbi_uc *stbi_hdr_load_rgbe_file   (FILE *f,                  int *x, int *y, int *comp, int req_comp);
314
#endif
315
 
316
// define new loaders
317
typedef struct
318
{
319
   int       (*test_memory)(stbi_uc const *buffer, int len);
320
   stbi_uc * (*load_from_memory)(stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp);
321
   #ifndef STBI_NO_STDIO
322
   int       (*test_file)(FILE *f);
323
   stbi_uc * (*load_from_file)(FILE *f, int *x, int *y, int *comp, int req_comp);
324
   #endif
325
} stbi_loader;
326
 
327
// register a loader by filling out the above structure (you must defined ALL functions)
328
// returns 1 if added or already added, 0 if not added (too many loaders)
329
// NOT THREADSAFE
330
extern int stbi_register_loader(stbi_loader *loader);
331
 
332
// define faster low-level operations (typically SIMD support)
333
#if STBI_SIMD
334
typedef void (*stbi_idct_8x8)(uint8 *out, int out_stride, short data[64], unsigned short *dequantize);
335
// compute an integer IDCT on "input"
336
//     input[x] = data[x] * dequantize[x]
337
//     write results to 'out': 64 samples, each run of 8 spaced by 'out_stride'
338
//                             CLAMP results to 0..255
339
typedef void (*stbi_YCbCr_to_RGB_run)(uint8 *output, uint8 const *y, uint8 const *cb, uint8 const *cr, int count, int step);
340
// compute a conversion from YCbCr to RGB
341
//     'count' pixels
342
//     write pixels to 'output'; each pixel is 'step' bytes (either 3 or 4; if 4, write '255' as 4th), order R,G,B
343
//     y: Y input channel
344
//     cb: Cb input channel; scale/biased to be 0..255
345
//     cr: Cr input channel; scale/biased to be 0..255
346
 
347
extern void stbi_install_idct(stbi_idct_8x8 func);
348
extern void stbi_install_YCbCr_to_RGB(stbi_YCbCr_to_RGB_run func);
349
#endif // STBI_SIMD
350
 
351
#ifdef __cplusplus
352
}
353
#endif
354
 
355
//
356
//
357
////   end header file   /////////////////////////////////////////////////////
358
#endif // STBI_INCLUDE_STB_IMAGE_H