PageRenderTime 34ms CodeModel.GetById 12ms app.highlight 17ms RepoModel.GetById 1ms app.codeStats 0ms

/xbmc/visualizations/XBMCProjectM/libprojectM/stb_image_aug.h

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