github.com/cellofellow/gopkg@v0.0.0-20140722061823-eec0544a62ad/image/jxr/jxrlib/test/jpeg/stb_image.c (about)

     1  /* stbi-1.29 - public domain JPEG/PNG reader - http://nothings.org/stb_image.c
     2     when you control the images you're loading
     3                                       no warranty implied; use at your own risk
     4  
     5     QUICK NOTES:
     6        Primarily of interest to game developers and other people who can
     7            avoid problematic images and only need the trivial interface
     8  
     9        JPEG baseline (no JPEG progressive)
    10        PNG 8-bit only
    11  
    12        TGA (not sure what subset, if a subset)
    13        BMP non-1bpp, non-RLE
    14        PSD (composited view only, no extra channels)
    15  
    16        GIF (*comp always reports as 4-channel)
    17        HDR (radiance rgbE format)
    18        PIC (Softimage PIC)
    19  
    20        - decoded from memory or through stdio FILE (define STBI_NO_STDIO to remove code)
    21        - supports installable dequantizing-IDCT, YCbCr-to-RGB conversion (define STBI_SIMD)
    22  
    23     Latest revisions:
    24        1.29 (2010-08-16) various warning fixes from Aurelien Pocheville
    25        1.28 (2010-08-01) fix bug in GIF palette transparency (SpartanJ)
    26        1.27 (2010-08-01) cast-to-uint8 to fix warnings (Laurent Gomila)
    27                          allow trailing 0s at end of image data (Laurent Gomila)
    28        1.26 (2010-07-24) fix bug in file buffering for PNG reported by SpartanJ
    29        1.25 (2010-07-17) refix trans_data warning (Won Chun)
    30        1.24 (2010-07-12) perf improvements reading from files
    31                          minor perf improvements for jpeg
    32                          deprecated type-specific functions in hope of feedback
    33                          attempt to fix trans_data warning (Won Chun)
    34        1.23              fixed bug in iPhone support
    35        1.22 (2010-07-10) removed image *writing* support to stb_image_write.h
    36                          stbi_info support from Jetro Lauha
    37                          GIF support from Jean-Marc Lienher
    38                          iPhone PNG-extensions from James Brown
    39                          warning-fixes from Nicolas Schulz and Janez Zemva
    40        1.21              fix use of 'uint8' in header (reported by jon blow)
    41        1.20              added support for Softimage PIC, by Tom Seddon
    42  
    43     See end of file for full revision history.
    44  
    45     TODO:
    46        stbi_info support for BMP,PSD,HDR,PIC
    47        rewrite stbi_info and load_file variations to share file handling code
    48             (current system allows individual functions to be called directly,
    49             since each does all the work, but I doubt anyone uses this in practice)
    50  
    51  
    52   ============================    Contributors    =========================
    53  
    54   Image formats                                Optimizations & bugfixes
    55      Sean Barrett (jpeg, png, bmp)                Fabian "ryg" Giesen
    56      Nicolas Schulz (hdr, psd)
    57      Jonathan Dummer (tga)                     Bug fixes & warning fixes
    58      Jean-Marc Lienher (gif)                      Marc LeBlanc
    59      Tom Seddon (pic)                             Christpher Lloyd
    60      Thatcher Ulrich (psd)                        Dave Moore
    61                                                   Won Chun
    62                                                   the Horde3D community
    63   Extensions, features                            Janez Zemva
    64      Jetro Lauha (stbi_info)                      Jonathan Blow
    65      James "moose2000" Brown (iPhone PNG)         Laurent Gomila
    66                                                   Aruelien Pocheville
    67  
    68   If your name should be here but isn't, let Sean know.
    69  
    70  */
    71  
    72  #ifndef STBI_INCLUDE_STB_IMAGE_H
    73  #define STBI_INCLUDE_STB_IMAGE_H
    74  
    75  // To get a header file for this, either cut and paste the header,
    76  // or create stb_image.h, #define STBI_HEADER_FILE_ONLY, and
    77  // then include stb_image.c from it.
    78  
    79  ////   begin header file  ////////////////////////////////////////////////////
    80  //
    81  // Limitations:
    82  //    - no jpeg progressive support
    83  //    - non-HDR formats support 8-bit samples only (jpeg, png)
    84  //    - no delayed line count (jpeg) -- IJG doesn't support either
    85  //    - no 1-bit BMP
    86  //    - GIF always returns *comp=4
    87  //
    88  // Basic usage (see HDR discussion below):
    89  //    int x,y,n;
    90  //    unsigned char *data = stbi_load(filename, &x, &y, &n, 0);
    91  //    // ... process data if not NULL ...
    92  //    // ... x = width, y = height, n = # 8-bit components per pixel ...
    93  //    // ... replace '0' with '1'..'4' to force that many components per pixel
    94  //    stbi_image_free(data)
    95  //
    96  // Standard parameters:
    97  //    int *x       -- outputs image width in pixels
    98  //    int *y       -- outputs image height in pixels
    99  //    int *comp    -- outputs # of image components in image file
   100  //    int req_comp -- if non-zero, # of image components requested in result
   101  //
   102  // The return value from an image loader is an 'unsigned char *' which points
   103  // to the pixel data. The pixel data consists of *y scanlines of *x pixels,
   104  // with each pixel consisting of N interleaved 8-bit components; the first
   105  // pixel pointed to is top-left-most in the image. There is no padding between
   106  // image scanlines or between pixels, regardless of format. The number of
   107  // components N is 'req_comp' if req_comp is non-zero, or *comp otherwise.
   108  // If req_comp is non-zero, *comp has the number of components that _would_
   109  // have been output otherwise. E.g. if you set req_comp to 4, you will always
   110  // get RGBA output, but you can check *comp to easily see if it's opaque.
   111  //
   112  // An output image with N components has the following components interleaved
   113  // in this order in each pixel:
   114  //
   115  //     N=#comp     components
   116  //       1           grey
   117  //       2           grey, alpha
   118  //       3           red, green, blue
   119  //       4           red, green, blue, alpha
   120  //
   121  // If image loading fails for any reason, the return value will be NULL,
   122  // and *x, *y, *comp will be unchanged. The function stbi_failure_reason()
   123  // can be queried for an extremely brief, end-user unfriendly explanation
   124  // of why the load failed. Define STBI_NO_FAILURE_STRINGS to avoid
   125  // compiling these strings at all, and STBI_FAILURE_USERMSG to get slightly
   126  // more user-friendly ones.
   127  //
   128  // Paletted PNG, BMP, GIF, and PIC images are automatically depalettized.
   129  //
   130  // ===========================================================================
   131  //
   132  // iPhone PNG support:
   133  //
   134  // By default we convert iphone-formatted PNGs back to RGB; nominally they
   135  // would silently load as BGR, except the existing code should have just
   136  // failed on such iPhone PNGs. But you can disable this conversion by
   137  // by calling stbi_convert_iphone_png_to_rgb(0), in which case
   138  // you will always just get the native iphone "format" through.
   139  //
   140  // Call stbi_set_unpremultiply_on_load(1) as well to force a divide per
   141  // pixel to remove any premultiplied alpha *only* if the image file explicitly
   142  // says there's premultiplied data (currently only happens in iPhone images,
   143  // and only if iPhone convert-to-rgb processing is on).
   144  //
   145  // ===========================================================================
   146  //
   147  // HDR image support   (disable by defining STBI_NO_HDR)
   148  //
   149  // stb_image now supports loading HDR images in general, and currently
   150  // the Radiance .HDR file format, although the support is provided
   151  // generically. You can still load any file through the existing interface;
   152  // if you attempt to load an HDR file, it will be automatically remapped to
   153  // LDR, assuming gamma 2.2 and an arbitrary scale factor defaulting to 1;
   154  // both of these constants can be reconfigured through this interface:
   155  //
   156  //     stbi_hdr_to_ldr_gamma(2.2f);
   157  //     stbi_hdr_to_ldr_scale(1.0f);
   158  //
   159  // (note, do not use _inverse_ constants; stbi_image will invert them
   160  // appropriately).
   161  //
   162  // Additionally, there is a new, parallel interface for loading files as
   163  // (linear) floats to preserve the full dynamic range:
   164  //
   165  //    float *data = stbi_loadf(filename, &x, &y, &n, 0);
   166  //
   167  // If you load LDR images through this interface, those images will
   168  // be promoted to floating point values, run through the inverse of
   169  // constants corresponding to the above:
   170  //
   171  //     stbi_ldr_to_hdr_scale(1.0f);
   172  //     stbi_ldr_to_hdr_gamma(2.2f);
   173  //
   174  // Finally, given a filename (or an open file or memory block--see header
   175  // file for details) containing image data, you can query for the "most
   176  // appropriate" interface to use (that is, whether the image is HDR or
   177  // not), using:
   178  //
   179  //     stbi_is_hdr(char *filename);
   180  
   181  #ifndef STBI_NO_STDIO
   182  #include <stdio.h>
   183  #endif
   184  
   185  #define STBI_VERSION 1
   186  
   187  enum
   188  {
   189     STBI_default = 0, // only used for req_comp
   190  
   191     STBI_grey       = 1,
   192     STBI_grey_alpha = 2,
   193     STBI_rgb        = 3,
   194     STBI_rgb_alpha  = 4
   195  };
   196  
   197  typedef unsigned char stbi_uc;
   198  
   199  #ifdef __cplusplus
   200  extern "C" {
   201  #endif
   202  
   203  // PRIMARY API - works on images of any type
   204  
   205  // load image by filename, open file, or memory buffer
   206  extern stbi_uc *stbi_load_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp);
   207  
   208  #ifndef STBI_NO_STDIO
   209  extern stbi_uc *stbi_load            (char const *filename,     int *x, int *y, int *comp, int req_comp);
   210  extern stbi_uc *stbi_load_from_file  (FILE *f,                  int *x, int *y, int *comp, int req_comp);
   211  // for stbi_load_from_file, file pointer is left pointing immediately after image
   212  #endif
   213  
   214  #ifndef STBI_NO_HDR
   215     extern float *stbi_loadf_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp);
   216  
   217     #ifndef STBI_NO_STDIO
   218     extern float *stbi_loadf            (char const *filename,   int *x, int *y, int *comp, int req_comp);
   219     extern float *stbi_loadf_from_file  (FILE *f,                int *x, int *y, int *comp, int req_comp);
   220     #endif
   221  
   222     extern void   stbi_hdr_to_ldr_gamma(float gamma);
   223     extern void   stbi_hdr_to_ldr_scale(float scale);
   224  
   225     extern void   stbi_ldr_to_hdr_gamma(float gamma);
   226     extern void   stbi_ldr_to_hdr_scale(float scale);
   227  #endif // STBI_NO_HDR
   228  
   229  // get a VERY brief reason for failure
   230  // NOT THREADSAFE
   231  extern const char *stbi_failure_reason  (void);
   232  
   233  // free the loaded image -- this is just free()
   234  extern void     stbi_image_free      (void *retval_from_stbi_load);
   235  
   236  // get image dimensions & components without fully decoding
   237  extern int      stbi_info_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp);
   238  extern int      stbi_is_hdr_from_memory(stbi_uc const *buffer, int len);
   239  
   240  #ifndef STBI_NO_STDIO
   241  extern int      stbi_info            (char const *filename,     int *x, int *y, int *comp);
   242  extern int      stbi_info_from_file  (FILE *f,                  int *x, int *y, int *comp);
   243  
   244  extern int      stbi_is_hdr          (char const *filename);
   245  extern int      stbi_is_hdr_from_file(FILE *f);
   246  #endif
   247  
   248  // for image formats that explicitly notate that they have premultiplied alpha,
   249  // we just return the colors as stored in the file. set this flag to force
   250  // unpremultiplication. results are undefined if the unpremultiply overflow.
   251  extern void stbi_set_unpremultiply_on_load(int flag_true_if_should_unpremultiply);
   252  
   253  // indicate whether we should process iphone images back to canonical format,
   254  // or just pass them through "as-is"
   255  extern void stbi_convert_iphone_png_to_rgb(int flag_true_if_should_convert);
   256  
   257  
   258  // ZLIB client - used by PNG, available for other purposes
   259  
   260  extern char *stbi_zlib_decode_malloc_guesssize(const char *buffer, int len, int initial_size, int *outlen);
   261  extern char *stbi_zlib_decode_malloc(const char *buffer, int len, int *outlen);
   262  extern int   stbi_zlib_decode_buffer(char *obuffer, int olen, const char *ibuffer, int ilen);
   263  
   264  extern char *stbi_zlib_decode_noheader_malloc(const char *buffer, int len, int *outlen);
   265  extern int   stbi_zlib_decode_noheader_buffer(char *obuffer, int olen, const char *ibuffer, int ilen);
   266  
   267  // define new loaders
   268  typedef struct
   269  {
   270     int       (*test_memory)(stbi_uc const *buffer, int len);
   271     stbi_uc * (*load_from_memory)(stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp);
   272     #ifndef STBI_NO_STDIO
   273     int       (*test_file)(FILE *f);
   274     stbi_uc * (*load_from_file)(FILE *f, int *x, int *y, int *comp, int req_comp);
   275     #endif
   276  } stbi_loader;
   277  
   278  // register a loader by filling out the above structure (you must define ALL functions)
   279  // returns 1 if added or already added, 0 if not added (too many loaders)
   280  // NOT THREADSAFE
   281  extern int stbi_register_loader(stbi_loader *loader);
   282  
   283  // define faster low-level operations (typically SIMD support)
   284  #ifdef STBI_SIMD
   285  typedef void (*stbi_idct_8x8)(stbi_uc *out, int out_stride, short data[64], unsigned short *dequantize);
   286  // compute an integer IDCT on "input"
   287  //     input[x] = data[x] * dequantize[x]
   288  //     write results to 'out': 64 samples, each run of 8 spaced by 'out_stride'
   289  //                             CLAMP results to 0..255
   290  typedef void (*stbi_YCbCr_to_RGB_run)(stbi_uc *output, stbi_uc const  *y, stbi_uc const *cb, stbi_uc const *cr, int count, int step);
   291  // compute a conversion from YCbCr to RGB
   292  //     'count' pixels
   293  //     write pixels to 'output'; each pixel is 'step' bytes (either 3 or 4; if 4, write '255' as 4th), order R,G,B
   294  //     y: Y input channel
   295  //     cb: Cb input channel; scale/biased to be 0..255
   296  //     cr: Cr input channel; scale/biased to be 0..255
   297  
   298  extern void stbi_install_idct(stbi_idct_8x8 func);
   299  extern void stbi_install_YCbCr_to_RGB(stbi_YCbCr_to_RGB_run func);
   300  #endif // STBI_SIMD
   301  
   302  
   303  
   304  
   305  // TYPE-SPECIFIC ACCESS
   306  
   307  #ifdef STBI_TYPE_SPECIFIC_FUNCTIONS
   308  
   309  // is it a jpeg?
   310  extern int      stbi_jpeg_test_memory     (stbi_uc const *buffer, int len);
   311  extern stbi_uc *stbi_jpeg_load_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp);
   312  extern int      stbi_jpeg_info_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp);
   313  
   314  #ifndef STBI_NO_STDIO
   315  extern stbi_uc *stbi_jpeg_load            (char const *filename,     int *x, int *y, int *comp, int req_comp);
   316  extern int      stbi_jpeg_test_file       (FILE *f);
   317  extern stbi_uc *stbi_jpeg_load_from_file  (FILE *f,                  int *x, int *y, int *comp, int req_comp);
   318  
   319  extern int      stbi_jpeg_info            (char const *filename,     int *x, int *y, int *comp);
   320  extern int      stbi_jpeg_info_from_file  (FILE *f,                  int *x, int *y, int *comp);
   321  #endif
   322  
   323  // is it a png?
   324  extern int      stbi_png_test_memory      (stbi_uc const *buffer, int len);
   325  extern stbi_uc *stbi_png_load_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp);
   326  extern int      stbi_png_info_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp);
   327  
   328  #ifndef STBI_NO_STDIO
   329  extern stbi_uc *stbi_png_load             (char const *filename,     int *x, int *y, int *comp, int req_comp);
   330  extern int      stbi_png_info             (char const *filename,     int *x, int *y, int *comp);
   331  extern int      stbi_png_test_file        (FILE *f);
   332  extern stbi_uc *stbi_png_load_from_file   (FILE *f,                  int *x, int *y, int *comp, int req_comp);
   333  extern int      stbi_png_info_from_file   (FILE *f,                  int *x, int *y, int *comp);
   334  #endif
   335  
   336  // is it a bmp?
   337  extern int      stbi_bmp_test_memory      (stbi_uc const *buffer, int len);
   338  
   339  extern stbi_uc *stbi_bmp_load             (char const *filename,     int *x, int *y, int *comp, int req_comp);
   340  extern stbi_uc *stbi_bmp_load_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp);
   341  #ifndef STBI_NO_STDIO
   342  extern int      stbi_bmp_test_file        (FILE *f);
   343  extern stbi_uc *stbi_bmp_load_from_file   (FILE *f,                  int *x, int *y, int *comp, int req_comp);
   344  #endif
   345  
   346  // is it a tga?
   347  extern int      stbi_tga_test_memory      (stbi_uc const *buffer, int len);
   348  
   349  extern stbi_uc *stbi_tga_load             (char const *filename,     int *x, int *y, int *comp, int req_comp);
   350  extern stbi_uc *stbi_tga_load_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp);
   351  #ifndef STBI_NO_STDIO
   352  extern int      stbi_tga_test_file        (FILE *f);
   353  extern stbi_uc *stbi_tga_load_from_file   (FILE *f,                  int *x, int *y, int *comp, int req_comp);
   354  #endif
   355  
   356  // is it a psd?
   357  extern int      stbi_psd_test_memory      (stbi_uc const *buffer, int len);
   358  
   359  extern stbi_uc *stbi_psd_load             (char const *filename,     int *x, int *y, int *comp, int req_comp);
   360  extern stbi_uc *stbi_psd_load_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp);
   361  #ifndef STBI_NO_STDIO
   362  extern int      stbi_psd_test_file        (FILE *f);
   363  extern stbi_uc *stbi_psd_load_from_file   (FILE *f,                  int *x, int *y, int *comp, int req_comp);
   364  #endif
   365  
   366  // is it an hdr?
   367  extern int      stbi_hdr_test_memory      (stbi_uc const *buffer, int len);
   368  
   369  extern float *  stbi_hdr_load             (char const *filename,     int *x, int *y, int *comp, int req_comp);
   370  extern float *  stbi_hdr_load_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp);
   371  #ifndef STBI_NO_STDIO
   372  extern int      stbi_hdr_test_file        (FILE *f);
   373  extern float *  stbi_hdr_load_from_file   (FILE *f,                  int *x, int *y, int *comp, int req_comp);
   374  #endif
   375  
   376  // is it a pic?
   377  extern int      stbi_pic_test_memory      (stbi_uc const *buffer, int len);
   378  
   379  extern stbi_uc *stbi_pic_load             (char const *filename,     int *x, int *y, int *comp, int req_comp);
   380  extern stbi_uc *stbi_pic_load_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp);
   381  #ifndef STBI_NO_STDIO
   382  extern int      stbi_pic_test_file        (FILE *f);
   383  extern stbi_uc *stbi_pic_load_from_file   (FILE *f,                  int *x, int *y, int *comp, int req_comp);
   384  #endif
   385  
   386  // is it a gif?
   387  extern int      stbi_gif_test_memory      (stbi_uc const *buffer, int len);
   388  
   389  extern stbi_uc *stbi_gif_load             (char const *filename,     int *x, int *y, int *comp, int req_comp);
   390  extern stbi_uc *stbi_gif_load_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp);
   391  extern int      stbi_gif_info_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp);
   392  
   393  #ifndef STBI_NO_STDIO
   394  extern int      stbi_gif_test_file        (FILE *f);
   395  extern stbi_uc *stbi_gif_load_from_file   (FILE *f,                  int *x, int *y, int *comp, int req_comp);
   396  extern int      stbi_gif_info             (char const *filename,     int *x, int *y, int *comp);
   397  extern int      stbi_gif_info_from_file   (FILE *f,                  int *x, int *y, int *comp);
   398  #endif
   399  
   400  #endif//STBI_TYPE_SPECIFIC_FUNCTIONS
   401  
   402  
   403  
   404  
   405  #ifdef __cplusplus
   406  }
   407  #endif
   408  
   409  //
   410  //
   411  ////   end header file   /////////////////////////////////////////////////////
   412  #endif // STBI_INCLUDE_STB_IMAGE_H
   413  
   414  #ifndef STBI_HEADER_FILE_ONLY
   415  
   416  #ifndef STBI_NO_HDR
   417  #include <math.h>  // ldexp
   418  #include <string.h> // strcmp
   419  #endif
   420  
   421  #ifndef STBI_NO_STDIO
   422  #include <stdio.h>
   423  #endif
   424  #include <stdlib.h>
   425  #include <memory.h>
   426  #include <assert.h>
   427  #include <stdarg.h>
   428  
   429  #if !defined(_MSC_VER) && !defined(__forceinline)
   430    #ifdef __cplusplus
   431    #define __forceinline inline
   432    #else
   433    #define __forceinline
   434    #endif
   435  #endif
   436  
   437  
   438  // implementation:
   439  typedef unsigned char uint8;
   440  typedef unsigned short uint16;
   441  typedef   signed short  int16;
   442  typedef unsigned int   uint32;
   443  typedef   signed int    int32;
   444  typedef unsigned int   uint;
   445  
   446  // should produce compiler error if size is wrong
   447  typedef unsigned char validate_uint32[sizeof(uint32)==4 ? 1 : -1];
   448  
   449  #if defined(STBI_NO_STDIO) && !defined(STBI_NO_WRITE)
   450  #define STBI_NO_WRITE
   451  #endif
   452  
   453  #define STBI_NOTUSED(v)  v=v
   454  
   455  #ifdef _MSC_VER
   456  #define STBI_HAS_LRTOL
   457  #endif
   458  
   459  #ifdef STBI_HAS_LRTOL
   460     #define stbi_lrot(x,y)  _lrotl(x,y)
   461  #else
   462     #define stbi_lrot(x,y)  (((x) << (y)) | ((x) >> (32 - (y))))
   463  #endif
   464  
   465  //////////////////////////////////////////////////////////////////////////////
   466  //
   467  // Generic API that works on all image types
   468  //
   469  
   470  // deprecated functions
   471  
   472  // is it a jpeg?
   473  extern int      stbi_jpeg_test_memory     (stbi_uc const *buffer, int len);
   474  extern stbi_uc *stbi_jpeg_load_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp);
   475  extern int      stbi_jpeg_info_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp);
   476  
   477  #ifndef STBI_NO_STDIO
   478  extern stbi_uc *stbi_jpeg_load            (char const *filename,     int *x, int *y, int *comp, int req_comp);
   479  extern int      stbi_jpeg_test_file       (FILE *f);
   480  extern stbi_uc *stbi_jpeg_load_from_file  (FILE *f,                  int *x, int *y, int *comp, int req_comp);
   481  
   482  extern int      stbi_jpeg_info            (char const *filename,     int *x, int *y, int *comp);
   483  extern int      stbi_jpeg_info_from_file  (FILE *f,                  int *x, int *y, int *comp);
   484  #endif
   485  
   486  // is it a png?
   487  extern int      stbi_png_test_memory      (stbi_uc const *buffer, int len);
   488  extern stbi_uc *stbi_png_load_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp);
   489  extern int      stbi_png_info_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp);
   490  
   491  #ifndef STBI_NO_STDIO
   492  extern stbi_uc *stbi_png_load             (char const *filename,     int *x, int *y, int *comp, int req_comp);
   493  extern int      stbi_png_info             (char const *filename,     int *x, int *y, int *comp);
   494  extern int      stbi_png_test_file        (FILE *f);
   495  extern stbi_uc *stbi_png_load_from_file   (FILE *f,                  int *x, int *y, int *comp, int req_comp);
   496  extern int      stbi_png_info_from_file   (FILE *f,                  int *x, int *y, int *comp);
   497  #endif
   498  
   499  // is it a bmp?
   500  extern int      stbi_bmp_test_memory      (stbi_uc const *buffer, int len);
   501  
   502  extern stbi_uc *stbi_bmp_load             (char const *filename,     int *x, int *y, int *comp, int req_comp);
   503  extern stbi_uc *stbi_bmp_load_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp);
   504  #ifndef STBI_NO_STDIO
   505  extern int      stbi_bmp_test_file        (FILE *f);
   506  extern stbi_uc *stbi_bmp_load_from_file   (FILE *f,                  int *x, int *y, int *comp, int req_comp);
   507  #endif
   508  
   509  // is it a tga?
   510  extern int      stbi_tga_test_memory      (stbi_uc const *buffer, int len);
   511  
   512  extern stbi_uc *stbi_tga_load             (char const *filename,     int *x, int *y, int *comp, int req_comp);
   513  extern stbi_uc *stbi_tga_load_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp);
   514  #ifndef STBI_NO_STDIO
   515  extern int      stbi_tga_test_file        (FILE *f);
   516  extern stbi_uc *stbi_tga_load_from_file   (FILE *f,                  int *x, int *y, int *comp, int req_comp);
   517  #endif
   518  
   519  // is it a psd?
   520  extern int      stbi_psd_test_memory      (stbi_uc const *buffer, int len);
   521  
   522  extern stbi_uc *stbi_psd_load             (char const *filename,     int *x, int *y, int *comp, int req_comp);
   523  extern stbi_uc *stbi_psd_load_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp);
   524  #ifndef STBI_NO_STDIO
   525  extern int      stbi_psd_test_file        (FILE *f);
   526  extern stbi_uc *stbi_psd_load_from_file   (FILE *f,                  int *x, int *y, int *comp, int req_comp);
   527  #endif
   528  
   529  // is it an hdr?
   530  extern int      stbi_hdr_test_memory      (stbi_uc const *buffer, int len);
   531  
   532  extern float *  stbi_hdr_load             (char const *filename,     int *x, int *y, int *comp, int req_comp);
   533  extern float *  stbi_hdr_load_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp);
   534  #ifndef STBI_NO_STDIO
   535  extern int      stbi_hdr_test_file        (FILE *f);
   536  extern float *  stbi_hdr_load_from_file   (FILE *f,                  int *x, int *y, int *comp, int req_comp);
   537  #endif
   538  
   539  // is it a pic?
   540  extern int      stbi_pic_test_memory      (stbi_uc const *buffer, int len);
   541  
   542  extern stbi_uc *stbi_pic_load             (char const *filename,     int *x, int *y, int *comp, int req_comp);
   543  extern stbi_uc *stbi_pic_load_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp);
   544  #ifndef STBI_NO_STDIO
   545  extern int      stbi_pic_test_file        (FILE *f);
   546  extern stbi_uc *stbi_pic_load_from_file   (FILE *f,                  int *x, int *y, int *comp, int req_comp);
   547  #endif
   548  
   549  // is it a gif?
   550  extern int      stbi_gif_test_memory      (stbi_uc const *buffer, int len);
   551  
   552  extern stbi_uc *stbi_gif_load             (char const *filename,     int *x, int *y, int *comp, int req_comp);
   553  extern stbi_uc *stbi_gif_load_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp);
   554  extern int      stbi_gif_info_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp);
   555  
   556  #ifndef STBI_NO_STDIO
   557  extern int      stbi_gif_test_file        (FILE *f);
   558  extern stbi_uc *stbi_gif_load_from_file   (FILE *f,                  int *x, int *y, int *comp, int req_comp);
   559  extern int      stbi_gif_info             (char const *filename,     int *x, int *y, int *comp);
   560  extern int      stbi_gif_info_from_file   (FILE *f,                  int *x, int *y, int *comp);
   561  #endif
   562  
   563  
   564  // this is not threadsafe
   565  static const char *failure_reason;
   566  
   567  const char *stbi_failure_reason(void)
   568  {
   569     return failure_reason;
   570  }
   571  
   572  static int e(const char *str)
   573  {
   574     failure_reason = str;
   575     return 0;
   576  }
   577  
   578  #ifdef STBI_NO_FAILURE_STRINGS
   579     #define e(x,y)  0
   580  #elif defined(STBI_FAILURE_USERMSG)
   581     #define e(x,y)  e(y)
   582  #else
   583     #define e(x,y)  e(x)
   584  #endif
   585  
   586  #define epf(x,y)   ((float *) (e(x,y)?NULL:NULL))
   587  #define epuc(x,y)  ((unsigned char *) (e(x,y)?NULL:NULL))
   588  
   589  void stbi_image_free(void *retval_from_stbi_load)
   590  {
   591     free(retval_from_stbi_load);
   592  }
   593  
   594  #define MAX_LOADERS  32
   595  stbi_loader *loaders[MAX_LOADERS];
   596  static int max_loaders = 0;
   597  
   598  int stbi_register_loader(stbi_loader *loader)
   599  {
   600     int i;
   601     for (i=0; i < MAX_LOADERS; ++i) {
   602        // already present?
   603        if (loaders[i] == loader)
   604           return 1;
   605        // end of the list?
   606        if (loaders[i] == NULL) {
   607           loaders[i] = loader;
   608           max_loaders = i+1;
   609           return 1;
   610        }
   611     }
   612     // no room for it
   613     return 0;
   614  }
   615  
   616  #ifndef STBI_NO_HDR
   617  static float   *ldr_to_hdr(stbi_uc *data, int x, int y, int comp);
   618  static stbi_uc *hdr_to_ldr(float   *data, int x, int y, int comp);
   619  #endif
   620  
   621  #ifndef STBI_NO_STDIO
   622  unsigned char *stbi_load(char const *filename, int *x, int *y, int *comp, int req_comp)
   623  {
   624     FILE *f = fopen(filename, "rb");
   625     unsigned char *result;
   626     if (!f) return epuc("can't fopen", "Unable to open file");
   627     result = stbi_load_from_file(f,x,y,comp,req_comp);
   628     fclose(f);
   629     return result;
   630  }
   631  
   632  unsigned char *stbi_load_from_file(FILE *f, int *x, int *y, int *comp, int req_comp)
   633  {
   634     int i;
   635     if (stbi_jpeg_test_file(f)) return stbi_jpeg_load_from_file(f,x,y,comp,req_comp);
   636     if (stbi_png_test_file(f))  return stbi_png_load_from_file(f,x,y,comp,req_comp);
   637     if (stbi_bmp_test_file(f))  return stbi_bmp_load_from_file(f,x,y,comp,req_comp);
   638     if (stbi_gif_test_file(f))  return stbi_gif_load_from_file(f,x,y,comp,req_comp);
   639     if (stbi_psd_test_file(f))  return stbi_psd_load_from_file(f,x,y,comp,req_comp);
   640     if (stbi_pic_test_file(f))  return stbi_pic_load_from_file(f,x,y,comp,req_comp);
   641  
   642     #ifndef STBI_NO_HDR
   643     if (stbi_hdr_test_file(f)) {
   644        float *hdr = stbi_hdr_load_from_file(f, x,y,comp,req_comp);
   645        return hdr_to_ldr(hdr, *x, *y, req_comp ? req_comp : *comp);
   646     }
   647     #endif
   648  
   649     for (i=0; i < max_loaders; ++i)
   650        if (loaders[i]->test_file(f))
   651           return loaders[i]->load_from_file(f,x,y,comp,req_comp);
   652     // test tga last because it's a crappy test!
   653     if (stbi_tga_test_file(f))
   654        return stbi_tga_load_from_file(f,x,y,comp,req_comp);
   655     return epuc("unknown image type", "Image not of any known type, or corrupt");
   656  }
   657  #endif
   658  
   659  unsigned char *stbi_load_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp)
   660  {
   661     int i;
   662     if (stbi_jpeg_test_memory(buffer,len)) return stbi_jpeg_load_from_memory(buffer,len,x,y,comp,req_comp);
   663     if (stbi_png_test_memory(buffer,len))  return stbi_png_load_from_memory(buffer,len,x,y,comp,req_comp);
   664     if (stbi_bmp_test_memory(buffer,len))  return stbi_bmp_load_from_memory(buffer,len,x,y,comp,req_comp);
   665     if (stbi_gif_test_memory(buffer,len))  return stbi_gif_load_from_memory(buffer,len,x,y,comp,req_comp);
   666     if (stbi_psd_test_memory(buffer,len))  return stbi_psd_load_from_memory(buffer,len,x,y,comp,req_comp);
   667     if (stbi_pic_test_memory(buffer,len))  return stbi_pic_load_from_memory(buffer,len,x,y,comp,req_comp);
   668  
   669     #ifndef STBI_NO_HDR
   670     if (stbi_hdr_test_memory(buffer, len)) {
   671        float *hdr = stbi_hdr_load_from_memory(buffer, len,x,y,comp,req_comp);
   672        return hdr_to_ldr(hdr, *x, *y, req_comp ? req_comp : *comp);
   673     }
   674     #endif
   675  
   676     for (i=0; i < max_loaders; ++i)
   677        if (loaders[i]->test_memory(buffer,len))
   678           return loaders[i]->load_from_memory(buffer,len,x,y,comp,req_comp);
   679     // test tga last because it's a crappy test!
   680     if (stbi_tga_test_memory(buffer,len))
   681        return stbi_tga_load_from_memory(buffer,len,x,y,comp,req_comp);
   682     return epuc("unknown image type", "Image not of any known type, or corrupt");
   683  }
   684  
   685  #ifndef STBI_NO_HDR
   686  
   687  #ifndef STBI_NO_STDIO
   688  float *stbi_loadf(char const *filename, int *x, int *y, int *comp, int req_comp)
   689  {
   690     FILE *f = fopen(filename, "rb");
   691     float *result;
   692     if (!f) return epf("can't fopen", "Unable to open file");
   693     result = stbi_loadf_from_file(f,x,y,comp,req_comp);
   694     fclose(f);
   695     return result;
   696  }
   697  
   698  float *stbi_loadf_from_file(FILE *f, int *x, int *y, int *comp, int req_comp)
   699  {
   700     unsigned char *data;
   701     #ifndef STBI_NO_HDR
   702     if (stbi_hdr_test_file(f))
   703        return stbi_hdr_load_from_file(f,x,y,comp,req_comp);
   704     #endif
   705     data = stbi_load_from_file(f, x, y, comp, req_comp);
   706     if (data)
   707        return ldr_to_hdr(data, *x, *y, req_comp ? req_comp : *comp);
   708     return epf("unknown image type", "Image not of any known type, or corrupt");
   709  }
   710  #endif
   711  
   712  float *stbi_loadf_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp)
   713  {
   714     stbi_uc *data;
   715     #ifndef STBI_NO_HDR
   716     if (stbi_hdr_test_memory(buffer, len))
   717        return stbi_hdr_load_from_memory(buffer, len,x,y,comp,req_comp);
   718     #endif
   719     data = stbi_load_from_memory(buffer, len, x, y, comp, req_comp);
   720     if (data)
   721        return ldr_to_hdr(data, *x, *y, req_comp ? req_comp : *comp);
   722     return epf("unknown image type", "Image not of any known type, or corrupt");
   723  }
   724  #endif
   725  
   726  // these is-hdr-or-not is defined independent of whether STBI_NO_HDR is
   727  // defined, for API simplicity; if STBI_NO_HDR is defined, it always
   728  // reports false!
   729  
   730  int stbi_is_hdr_from_memory(stbi_uc const *buffer, int len)
   731  {
   732     #ifndef STBI_NO_HDR
   733     return stbi_hdr_test_memory(buffer, len);
   734     #else
   735     STBI_NOTUSED(buffer);
   736     STBI_NOTUSED(len);
   737     return 0;
   738     #endif
   739  }
   740  
   741  #ifndef STBI_NO_STDIO
   742  extern int      stbi_is_hdr          (char const *filename)
   743  {
   744     FILE *f = fopen(filename, "rb");
   745     int result=0;
   746     if (f) {
   747        result = stbi_is_hdr_from_file(f);
   748        fclose(f);
   749     }
   750     return result;
   751  }
   752  
   753  extern int      stbi_is_hdr_from_file(FILE *f)
   754  {
   755     #ifndef STBI_NO_HDR
   756     return stbi_hdr_test_file(f);
   757     #else
   758     return 0;
   759     #endif
   760  }
   761  
   762  #endif
   763  
   764  #ifndef STBI_NO_HDR
   765  static float h2l_gamma_i=1.0f/2.2f, h2l_scale_i=1.0f;
   766  static float l2h_gamma=2.2f, l2h_scale=1.0f;
   767  
   768  void   stbi_hdr_to_ldr_gamma(float gamma) { h2l_gamma_i = 1/gamma; }
   769  void   stbi_hdr_to_ldr_scale(float scale) { h2l_scale_i = 1/scale; }
   770  
   771  void   stbi_ldr_to_hdr_gamma(float gamma) { l2h_gamma = gamma; }
   772  void   stbi_ldr_to_hdr_scale(float scale) { l2h_scale = scale; }
   773  #endif
   774  
   775  
   776  //////////////////////////////////////////////////////////////////////////////
   777  //
   778  // Common code used by all image loaders
   779  //
   780  
   781  enum
   782  {
   783     SCAN_load=0,
   784     SCAN_type,
   785     SCAN_header
   786  };
   787  
   788  typedef struct
   789  {
   790     uint32 img_x, img_y;
   791     int img_n, img_out_n;
   792  
   793     #ifndef STBI_NO_STDIO
   794     FILE  *img_file;
   795     int buflen;
   796     uint8 buffer_start[128];
   797     int from_file;
   798     #endif
   799     uint8 *img_buffer, *img_buffer_end;
   800  } stbi;
   801  
   802  #ifndef STBI_NO_STDIO
   803  static void start_file(stbi *s, FILE *f)
   804  {
   805     s->img_file = f;
   806     s->buflen = sizeof(s->buffer_start);
   807     s->img_buffer_end = s->buffer_start + s->buflen;
   808     s->img_buffer = s->img_buffer_end;
   809     s->from_file = 1;
   810  }
   811  #endif
   812  
   813  static void start_mem(stbi *s, uint8 const *buffer, int len)
   814  {
   815  #ifndef STBI_NO_STDIO
   816     s->img_file = NULL;
   817     s->from_file = 0;
   818  #endif
   819     s->img_buffer = (uint8 *) buffer;
   820     s->img_buffer_end = (uint8 *) buffer+len;
   821  }
   822  
   823  #ifndef STBI_NO_STDIO
   824  static void refill_buffer(stbi *s)
   825  {
   826     int n = (int)fread(s->buffer_start, 1, s->buflen, s->img_file);
   827     if (n == 0) {
   828        s->from_file = 0;
   829        s->img_buffer = s->img_buffer_end-1;
   830        *s->img_buffer = 0;
   831     } else {
   832        s->img_buffer = s->buffer_start;
   833        s->img_buffer_end = s->buffer_start + n;
   834     }
   835  }
   836  #endif
   837  
   838  __forceinline static int get8(stbi *s)
   839  {
   840     if (s->img_buffer < s->img_buffer_end)
   841        return *s->img_buffer++;
   842  #ifndef STBI_NO_STDIO
   843     if (s->from_file) {
   844        refill_buffer(s);
   845        return *s->img_buffer++;
   846     }
   847  #endif
   848     return 0;
   849  }
   850  
   851  __forceinline static int at_eof(stbi *s)
   852  {
   853  #ifndef STBI_NO_STDIO
   854     if (s->img_file) {
   855        if (!feof(s->img_file)) return 0;
   856        // if feof() is true, check if buffer = end
   857        // special case: we've only got the special 0 character at the end
   858        if (s->from_file == 0) return 1;
   859     }
   860  #endif
   861     return s->img_buffer >= s->img_buffer_end;
   862  }
   863  
   864  __forceinline static uint8 get8u(stbi *s)
   865  {
   866     return (uint8) get8(s);
   867  }
   868  
   869  static void skip(stbi *s, int n)
   870  {
   871  #ifndef STBI_NO_STDIO
   872     if (s->img_file) {
   873        int blen = (int)(s->img_buffer_end - s->img_buffer);
   874        if (blen < n) {
   875           s->img_buffer = s->img_buffer_end;
   876           fseek(s->img_file, n - blen, SEEK_CUR);
   877           return;
   878        }
   879     }
   880  #endif
   881     s->img_buffer += n;
   882  }
   883  
   884  static int getn(stbi *s, stbi_uc *buffer, int n)
   885  {
   886  #ifndef STBI_NO_STDIO
   887     if (s->img_file) {
   888        int blen = (int)(s->img_buffer_end - s->img_buffer);
   889        if (blen < n) {
   890           int res;
   891           memcpy(buffer, s->img_buffer, blen);
   892           res = ((int) fread(buffer + blen, 1, n - blen, s->img_file) == (n-blen));
   893           s->img_buffer = s->img_buffer_end;
   894           return res;
   895        }
   896     }
   897  #endif
   898     if (s->img_buffer+n <= s->img_buffer_end) {
   899        memcpy(buffer, s->img_buffer, n);
   900        s->img_buffer += n;
   901        return 1;
   902     } else
   903        return 0;
   904  }
   905  
   906  static int get16(stbi *s)
   907  {
   908     int z = get8(s);
   909     return (z << 8) + get8(s);
   910  }
   911  
   912  static uint32 get32(stbi *s)
   913  {
   914     uint32 z = get16(s);
   915     return (z << 16) + get16(s);
   916  }
   917  
   918  static int get16le(stbi *s)
   919  {
   920     int z = get8(s);
   921     return z + (get8(s) << 8);
   922  }
   923  
   924  static uint32 get32le(stbi *s)
   925  {
   926     uint32 z = get16le(s);
   927     return z + (get16le(s) << 16);
   928  }
   929  
   930  //////////////////////////////////////////////////////////////////////////////
   931  //
   932  //  generic converter from built-in img_n to req_comp
   933  //    individual types do this automatically as much as possible (e.g. jpeg
   934  //    does all cases internally since it needs to colorspace convert anyway,
   935  //    and it never has alpha, so very few cases ). png can automatically
   936  //    interleave an alpha=255 channel, but falls back to this for other cases
   937  //
   938  //  assume data buffer is malloced, so malloc a new one and free that one
   939  //  only failure mode is malloc failing
   940  
   941  static uint8 compute_y(int r, int g, int b)
   942  {
   943     return (uint8) (((r*77) + (g*150) +  (29*b)) >> 8);
   944  }
   945  
   946  static unsigned char *convert_format(unsigned char *data, int img_n, int req_comp, uint x, uint y)
   947  {
   948     int i,j;
   949     unsigned char *good;
   950  
   951     if (req_comp == img_n) return data;
   952     assert(req_comp >= 1 && req_comp <= 4);
   953  
   954     good = (unsigned char *) malloc(req_comp * x * y);
   955     if (good == NULL) {
   956        free(data);
   957        return epuc("outofmem", "Out of memory");
   958     }
   959  
   960     for (j=0; j < (int) y; ++j) {
   961        unsigned char *src  = data + j * x * img_n   ;
   962        unsigned char *dest = good + j * x * req_comp;
   963  
   964        #define COMBO(a,b)  ((a)*8+(b))
   965        #define CASE(a,b)   case COMBO(a,b): for(i=x-1; i >= 0; --i, src += a, dest += b)
   966        // convert source image with img_n components to one with req_comp components;
   967        // avoid switch per pixel, so use switch per scanline and massive macros
   968        switch (COMBO(img_n, req_comp)) {
   969           CASE(1,2) dest[0]=src[0], dest[1]=255; break;
   970           CASE(1,3) dest[0]=dest[1]=dest[2]=src[0]; break;
   971           CASE(1,4) dest[0]=dest[1]=dest[2]=src[0], dest[3]=255; break;
   972           CASE(2,1) dest[0]=src[0]; break;
   973           CASE(2,3) dest[0]=dest[1]=dest[2]=src[0]; break;
   974           CASE(2,4) dest[0]=dest[1]=dest[2]=src[0], dest[3]=src[1]; break;
   975           CASE(3,4) dest[0]=src[0],dest[1]=src[1],dest[2]=src[2],dest[3]=255; break;
   976           CASE(3,1) dest[0]=compute_y(src[0],src[1],src[2]); break;
   977           CASE(3,2) dest[0]=compute_y(src[0],src[1],src[2]), dest[1] = 255; break;
   978           CASE(4,1) dest[0]=compute_y(src[0],src[1],src[2]); break;
   979           CASE(4,2) dest[0]=compute_y(src[0],src[1],src[2]), dest[1] = src[3]; break;
   980           CASE(4,3) dest[0]=src[0],dest[1]=src[1],dest[2]=src[2]; break;
   981           default: assert(0);
   982        }
   983        #undef CASE
   984     }
   985  
   986     free(data);
   987     return good;
   988  }
   989  
   990  #ifndef STBI_NO_HDR
   991  static float   *ldr_to_hdr(stbi_uc *data, int x, int y, int comp)
   992  {
   993     int i,k,n;
   994     float *output = (float *) malloc(x * y * comp * sizeof(float));
   995     if (output == NULL) { free(data); return epf("outofmem", "Out of memory"); }
   996     // compute number of non-alpha components
   997     if (comp & 1) n = comp; else n = comp-1;
   998     for (i=0; i < x*y; ++i) {
   999        for (k=0; k < n; ++k) {
  1000           output[i*comp + k] = (float) pow(data[i*comp+k]/255.0f, l2h_gamma) * l2h_scale;
  1001        }
  1002        if (k < comp) output[i*comp + k] = data[i*comp+k]/255.0f;
  1003     }
  1004     free(data);
  1005     return output;
  1006  }
  1007  
  1008  #define float2int(x)   ((int) (x))
  1009  static stbi_uc *hdr_to_ldr(float   *data, int x, int y, int comp)
  1010  {
  1011     int i,k,n;
  1012     stbi_uc *output = (stbi_uc *) malloc(x * y * comp);
  1013     if (output == NULL) { free(data); return epuc("outofmem", "Out of memory"); }
  1014     // compute number of non-alpha components
  1015     if (comp & 1) n = comp; else n = comp-1;
  1016     for (i=0; i < x*y; ++i) {
  1017        for (k=0; k < n; ++k) {
  1018           float z = (float) pow(data[i*comp+k]*h2l_scale_i, h2l_gamma_i) * 255 + 0.5f;
  1019           if (z < 0) z = 0;
  1020           if (z > 255) z = 255;
  1021           output[i*comp + k] = (uint8) float2int(z);
  1022        }
  1023        if (k < comp) {
  1024           float z = data[i*comp+k] * 255 + 0.5f;
  1025           if (z < 0) z = 0;
  1026           if (z > 255) z = 255;
  1027           output[i*comp + k] = (uint8) float2int(z);
  1028        }
  1029     }
  1030     free(data);
  1031     return output;
  1032  }
  1033  #endif
  1034  
  1035  //////////////////////////////////////////////////////////////////////////////
  1036  //
  1037  //  "baseline" JPEG/JFIF decoder (not actually fully baseline implementation)
  1038  //
  1039  //    simple implementation
  1040  //      - channel subsampling of at most 2 in each dimension
  1041  //      - doesn't support delayed output of y-dimension
  1042  //      - simple interface (only one output format: 8-bit interleaved RGB)
  1043  //      - doesn't try to recover corrupt jpegs
  1044  //      - doesn't allow partial loading, loading multiple at once
  1045  //      - still fast on x86 (copying globals into locals doesn't help x86)
  1046  //      - allocates lots of intermediate memory (full size of all components)
  1047  //        - non-interleaved case requires this anyway
  1048  //        - allows good upsampling (see next)
  1049  //    high-quality
  1050  //      - upsampled channels are bilinearly interpolated, even across blocks
  1051  //      - quality integer IDCT derived from IJG's 'slow'
  1052  //    performance
  1053  //      - fast huffman; reasonable integer IDCT
  1054  //      - uses a lot of intermediate memory, could cache poorly
  1055  //      - load http://nothings.org/remote/anemones.jpg 3 times on 2.8Ghz P4
  1056  //          stb_jpeg:   1.34 seconds (MSVC6, default release build)
  1057  //          stb_jpeg:   1.06 seconds (MSVC6, processor = Pentium Pro)
  1058  //          IJL11.dll:  1.08 seconds (compiled by intel)
  1059  //          IJG 1998:   0.98 seconds (MSVC6, makefile provided by IJG)
  1060  //          IJG 1998:   0.95 seconds (MSVC6, makefile + proc=PPro)
  1061  
  1062  // huffman decoding acceleration
  1063  #define FAST_BITS   9  // larger handles more cases; smaller stomps less cache
  1064  
  1065  typedef struct
  1066  {
  1067     uint8  fast[1 << FAST_BITS];
  1068     // weirdly, repacking this into AoS is a 10% speed loss, instead of a win
  1069     uint16 code[256];
  1070     uint8  values[256];
  1071     uint8  size[257];
  1072     unsigned int maxcode[18];
  1073     int    delta[17];   // old 'firstsymbol' - old 'firstcode'
  1074  } huffman;
  1075  
  1076  typedef struct
  1077  {
  1078     #ifdef STBI_SIMD
  1079     unsigned short dequant2[4][64];
  1080     #endif
  1081     stbi s;
  1082     huffman huff_dc[4];
  1083     huffman huff_ac[4];
  1084     uint8 dequant[4][64];
  1085  
  1086  // sizes for components, interleaved MCUs
  1087     int img_h_max, img_v_max;
  1088     int img_mcu_x, img_mcu_y;
  1089     int img_mcu_w, img_mcu_h;
  1090  
  1091  // definition of jpeg image component
  1092     struct
  1093     {
  1094        int id;
  1095        int h,v;
  1096        int tq;
  1097        int hd,ha;
  1098        int dc_pred;
  1099  
  1100        int x,y,w2,h2;
  1101        uint8 *data;
  1102        void *raw_data;
  1103        uint8 *linebuf;
  1104     } img_comp[4];
  1105  
  1106     uint32         code_buffer; // jpeg entropy-coded buffer
  1107     int            code_bits;   // number of valid bits
  1108     unsigned char  marker;      // marker seen while filling entropy buffer
  1109     int            nomore;      // flag if we saw a marker so must stop
  1110  
  1111     int scan_n, order[4];
  1112     int restart_interval, todo;
  1113  } jpeg;
  1114  
  1115  static int build_huffman(huffman *h, int *count)
  1116  {
  1117     int i,j,k=0,code;
  1118     // build size list for each symbol (from JPEG spec)
  1119     for (i=0; i < 16; ++i)
  1120        for (j=0; j < count[i]; ++j)
  1121           h->size[k++] = (uint8) (i+1);
  1122     h->size[k] = 0;
  1123  
  1124     // compute actual symbols (from jpeg spec)
  1125     code = 0;
  1126     k = 0;
  1127     for(j=1; j <= 16; ++j) {
  1128        // compute delta to add to code to compute symbol id
  1129        h->delta[j] = k - code;
  1130        if (h->size[k] == j) {
  1131           while (h->size[k] == j)
  1132              h->code[k++] = (uint16) (code++);
  1133           if (code-1 >= (1 << j)) return e("bad code lengths","Corrupt JPEG");
  1134        }
  1135        // compute largest code + 1 for this size, preshifted as needed later
  1136        h->maxcode[j] = code << (16-j);
  1137        code <<= 1;
  1138     }
  1139     h->maxcode[j] = 0xffffffff;
  1140  
  1141     // build non-spec acceleration table; 255 is flag for not-accelerated
  1142     memset(h->fast, 255, 1 << FAST_BITS);
  1143     for (i=0; i < k; ++i) {
  1144        int s = h->size[i];
  1145        if (s <= FAST_BITS) {
  1146           int c = h->code[i] << (FAST_BITS-s);
  1147           int m = 1 << (FAST_BITS-s);
  1148           for (j=0; j < m; ++j) {
  1149              h->fast[c+j] = (uint8) i;
  1150           }
  1151        }
  1152     }
  1153     return 1;
  1154  }
  1155  
  1156  static void grow_buffer_unsafe(jpeg *j)
  1157  {
  1158     do {
  1159        int b = j->nomore ? 0 : get8(&j->s);
  1160        if (b == 0xff) {
  1161           int c = get8(&j->s);
  1162           if (c != 0) {
  1163              j->marker = (unsigned char) c;
  1164              j->nomore = 1;
  1165              return;
  1166           }
  1167        }
  1168        j->code_buffer |= b << (24 - j->code_bits);
  1169        j->code_bits += 8;
  1170     } while (j->code_bits <= 24);
  1171  }
  1172  
  1173  // (1 << n) - 1
  1174  static uint32 bmask[17]={0,1,3,7,15,31,63,127,255,511,1023,2047,4095,8191,16383,32767,65535};
  1175  
  1176  // decode a jpeg huffman value from the bitstream
  1177  __forceinline static int decode(jpeg *j, huffman *h)
  1178  {
  1179     unsigned int temp;
  1180     int c,k;
  1181  
  1182     if (j->code_bits < 16) grow_buffer_unsafe(j);
  1183  
  1184     // look at the top FAST_BITS and determine what symbol ID it is,
  1185     // if the code is <= FAST_BITS
  1186     c = (j->code_buffer >> (32 - FAST_BITS)) & ((1 << FAST_BITS)-1);
  1187     k = h->fast[c];
  1188     if (k < 255) {
  1189        int s = h->size[k];
  1190        if (s > j->code_bits)
  1191           return -1;
  1192        j->code_buffer <<= s;
  1193        j->code_bits -= s;
  1194        return h->values[k];
  1195     }
  1196  
  1197     // naive test is to shift the code_buffer down so k bits are
  1198     // valid, then test against maxcode. To speed this up, we've
  1199     // preshifted maxcode left so that it has (16-k) 0s at the
  1200     // end; in other words, regardless of the number of bits, it
  1201     // wants to be compared against something shifted to have 16;
  1202     // that way we don't need to shift inside the loop.
  1203     temp = j->code_buffer >> 16;
  1204     for (k=FAST_BITS+1 ; ; ++k)
  1205        if (temp < h->maxcode[k])
  1206           break;
  1207     if (k == 17) {
  1208        // error! code not found
  1209        j->code_bits -= 16;
  1210        return -1;
  1211     }
  1212  
  1213     if (k > j->code_bits)
  1214        return -1;
  1215  
  1216     // convert the huffman code to the symbol id
  1217     c = ((j->code_buffer >> (32 - k)) & bmask[k]) + h->delta[k];
  1218     assert((((j->code_buffer) >> (32 - h->size[c])) & bmask[h->size[c]]) == h->code[c]);
  1219  
  1220     // convert the id to a symbol
  1221     j->code_bits -= k;
  1222     j->code_buffer <<= k;
  1223     return h->values[c];
  1224  }
  1225  
  1226  // combined JPEG 'receive' and JPEG 'extend', since baseline
  1227  // always extends everything it receives.
  1228  __forceinline static int extend_receive(jpeg *j, int n)
  1229  {
  1230     unsigned int m = 1 << (n-1);
  1231     unsigned int k;
  1232     if (j->code_bits < n) grow_buffer_unsafe(j);
  1233  
  1234     #if 1
  1235     k = stbi_lrot(j->code_buffer, n);
  1236     j->code_buffer = k & ~bmask[n];
  1237     k &= bmask[n];
  1238     j->code_bits -= n;
  1239     #else
  1240     k = (j->code_buffer >> (32 - n)) & bmask[n];
  1241     j->code_bits -= n;
  1242     j->code_buffer <<= n;
  1243     #endif
  1244     // the following test is probably a random branch that won't
  1245     // predict well. I tried to table accelerate it but failed.
  1246     // maybe it's compiling as a conditional move?
  1247     if (k < m)
  1248        return (-1 << n) + k + 1;
  1249     else
  1250        return k;
  1251  }
  1252  
  1253  // given a value that's at position X in the zigzag stream,
  1254  // where does it appear in the 8x8 matrix coded as row-major?
  1255  static uint8 dezigzag[64+15] =
  1256  {
  1257      0,  1,  8, 16,  9,  2,  3, 10,
  1258     17, 24, 32, 25, 18, 11,  4,  5,
  1259     12, 19, 26, 33, 40, 48, 41, 34,
  1260     27, 20, 13,  6,  7, 14, 21, 28,
  1261     35, 42, 49, 56, 57, 50, 43, 36,
  1262     29, 22, 15, 23, 30, 37, 44, 51,
  1263     58, 59, 52, 45, 38, 31, 39, 46,
  1264     53, 60, 61, 54, 47, 55, 62, 63,
  1265     // let corrupt input sample past end
  1266     63, 63, 63, 63, 63, 63, 63, 63,
  1267     63, 63, 63, 63, 63, 63, 63
  1268  };
  1269  
  1270  // decode one 64-entry block--
  1271  static int decode_block(jpeg *j, short data[64], huffman *hdc, huffman *hac, int b)
  1272  {
  1273     int diff,dc,k;
  1274     int t = decode(j, hdc);
  1275     if (t < 0) return e("bad huffman code","Corrupt JPEG");
  1276  
  1277     // 0 all the ac values now so we can do it 32-bits at a time
  1278     memset(data,0,64*sizeof(data[0]));
  1279  
  1280     diff = t ? extend_receive(j, t) : 0;
  1281     dc = j->img_comp[b].dc_pred + diff;
  1282     j->img_comp[b].dc_pred = dc;
  1283     data[0] = (short) dc;
  1284  
  1285     // decode AC components, see JPEG spec
  1286     k = 1;
  1287     do {
  1288        int r,s;
  1289        int rs = decode(j, hac);
  1290        if (rs < 0) return e("bad huffman code","Corrupt JPEG");
  1291        s = rs & 15;
  1292        r = rs >> 4;
  1293        if (s == 0) {
  1294           if (rs != 0xf0) break; // end block
  1295           k += 16;
  1296        } else {
  1297           k += r;
  1298           // decode into unzigzag'd location
  1299           data[dezigzag[k++]] = (short) extend_receive(j,s);
  1300        }
  1301     } while (k < 64);
  1302     return 1;
  1303  }
  1304  
  1305  // take a -128..127 value and clamp it and convert to 0..255
  1306  __forceinline static uint8 clamp(int x)
  1307  {
  1308     // trick to use a single test to catch both cases
  1309     if ((unsigned int) x > 255) {
  1310        if (x < 0) return 0;
  1311        if (x > 255) return 255;
  1312     }
  1313     return (uint8) x;
  1314  }
  1315  
  1316  #define f2f(x)  (int) (((x) * 4096 + 0.5))
  1317  #define fsh(x)  ((x) << 12)
  1318  
  1319  // derived from jidctint -- DCT_ISLOW
  1320  #define IDCT_1D(s0,s1,s2,s3,s4,s5,s6,s7)       \
  1321     int t0,t1,t2,t3,p1,p2,p3,p4,p5,x0,x1,x2,x3; \
  1322     p2 = s2;                                    \
  1323     p3 = s6;                                    \
  1324     p1 = (p2+p3) * f2f(0.5411961f);             \
  1325     t2 = p1 + p3*f2f(-1.847759065f);            \
  1326     t3 = p1 + p2*f2f( 0.765366865f);            \
  1327     p2 = s0;                                    \
  1328     p3 = s4;                                    \
  1329     t0 = fsh(p2+p3);                            \
  1330     t1 = fsh(p2-p3);                            \
  1331     x0 = t0+t3;                                 \
  1332     x3 = t0-t3;                                 \
  1333     x1 = t1+t2;                                 \
  1334     x2 = t1-t2;                                 \
  1335     t0 = s7;                                    \
  1336     t1 = s5;                                    \
  1337     t2 = s3;                                    \
  1338     t3 = s1;                                    \
  1339     p3 = t0+t2;                                 \
  1340     p4 = t1+t3;                                 \
  1341     p1 = t0+t3;                                 \
  1342     p2 = t1+t2;                                 \
  1343     p5 = (p3+p4)*f2f( 1.175875602f);            \
  1344     t0 = t0*f2f( 0.298631336f);                 \
  1345     t1 = t1*f2f( 2.053119869f);                 \
  1346     t2 = t2*f2f( 3.072711026f);                 \
  1347     t3 = t3*f2f( 1.501321110f);                 \
  1348     p1 = p5 + p1*f2f(-0.899976223f);            \
  1349     p2 = p5 + p2*f2f(-2.562915447f);            \
  1350     p3 = p3*f2f(-1.961570560f);                 \
  1351     p4 = p4*f2f(-0.390180644f);                 \
  1352     t3 += p1+p4;                                \
  1353     t2 += p2+p3;                                \
  1354     t1 += p2+p4;                                \
  1355     t0 += p1+p3;
  1356  
  1357  #ifdef STBI_SIMD
  1358  typedef unsigned short stbi_dequantize_t;
  1359  #else
  1360  typedef uint8 stbi_dequantize_t;
  1361  #endif
  1362  
  1363  // .344 seconds on 3*anemones.jpg
  1364  static void idct_block(uint8 *out, int out_stride, short data[64], stbi_dequantize_t *dequantize)
  1365  {
  1366     int i,val[64],*v=val;
  1367     stbi_dequantize_t *dq = dequantize;
  1368     uint8 *o;
  1369     short *d = data;
  1370  
  1371     // columns
  1372     for (i=0; i < 8; ++i,++d,++dq, ++v) {
  1373        // if all zeroes, shortcut -- this avoids dequantizing 0s and IDCTing
  1374        if (d[ 8]==0 && d[16]==0 && d[24]==0 && d[32]==0
  1375             && d[40]==0 && d[48]==0 && d[56]==0) {
  1376           //    no shortcut                 0     seconds
  1377           //    (1|2|3|4|5|6|7)==0          0     seconds
  1378           //    all separate               -0.047 seconds
  1379           //    1 && 2|3 && 4|5 && 6|7:    -0.047 seconds
  1380           int dcterm = d[0] * dq[0] << 2;
  1381           v[0] = v[8] = v[16] = v[24] = v[32] = v[40] = v[48] = v[56] = dcterm;
  1382        } else {
  1383           IDCT_1D(d[ 0]*dq[ 0],d[ 8]*dq[ 8],d[16]*dq[16],d[24]*dq[24],
  1384                   d[32]*dq[32],d[40]*dq[40],d[48]*dq[48],d[56]*dq[56])
  1385           // constants scaled things up by 1<<12; let's bring them back
  1386           // down, but keep 2 extra bits of precision
  1387           x0 += 512; x1 += 512; x2 += 512; x3 += 512;
  1388           v[ 0] = (x0+t3) >> 10;
  1389           v[56] = (x0-t3) >> 10;
  1390           v[ 8] = (x1+t2) >> 10;
  1391           v[48] = (x1-t2) >> 10;
  1392           v[16] = (x2+t1) >> 10;
  1393           v[40] = (x2-t1) >> 10;
  1394           v[24] = (x3+t0) >> 10;
  1395           v[32] = (x3-t0) >> 10;
  1396        }
  1397     }
  1398  
  1399     for (i=0, v=val, o=out; i < 8; ++i,v+=8,o+=out_stride) {
  1400        // no fast case since the first 1D IDCT spread components out
  1401        IDCT_1D(v[0],v[1],v[2],v[3],v[4],v[5],v[6],v[7])
  1402        // constants scaled things up by 1<<12, plus we had 1<<2 from first
  1403        // loop, plus horizontal and vertical each scale by sqrt(8) so together
  1404        // we've got an extra 1<<3, so 1<<17 total we need to remove.
  1405        // so we want to round that, which means adding 0.5 * 1<<17,
  1406        // aka 65536. Also, we'll end up with -128 to 127 that we want
  1407        // to encode as 0..255 by adding 128, so we'll add that before the shift
  1408        x0 += 65536 + (128<<17);
  1409        x1 += 65536 + (128<<17);
  1410        x2 += 65536 + (128<<17);
  1411        x3 += 65536 + (128<<17);
  1412        // tried computing the shifts into temps, or'ing the temps to see
  1413        // if any were out of range, but that was slower
  1414        o[0] = clamp((x0+t3) >> 17);
  1415        o[7] = clamp((x0-t3) >> 17);
  1416        o[1] = clamp((x1+t2) >> 17);
  1417        o[6] = clamp((x1-t2) >> 17);
  1418        o[2] = clamp((x2+t1) >> 17);
  1419        o[5] = clamp((x2-t1) >> 17);
  1420        o[3] = clamp((x3+t0) >> 17);
  1421        o[4] = clamp((x3-t0) >> 17);
  1422     }
  1423  }
  1424  
  1425  #ifdef STBI_SIMD
  1426  static stbi_idct_8x8 stbi_idct_installed = idct_block;
  1427  
  1428  extern void stbi_install_idct(stbi_idct_8x8 func)
  1429  {
  1430     stbi_idct_installed = func;
  1431  }
  1432  #endif
  1433  
  1434  #define MARKER_none  0xff
  1435  // if there's a pending marker from the entropy stream, return that
  1436  // otherwise, fetch from the stream and get a marker. if there's no
  1437  // marker, return 0xff, which is never a valid marker value
  1438  static uint8 get_marker(jpeg *j)
  1439  {
  1440     uint8 x;
  1441     if (j->marker != MARKER_none) { x = j->marker; j->marker = MARKER_none; return x; }
  1442     x = get8u(&j->s);
  1443     if (x != 0xff) return MARKER_none;
  1444     while (x == 0xff)
  1445        x = get8u(&j->s);
  1446     return x;
  1447  }
  1448  
  1449  // in each scan, we'll have scan_n components, and the order
  1450  // of the components is specified by order[]
  1451  #define RESTART(x)     ((x) >= 0xd0 && (x) <= 0xd7)
  1452  
  1453  // after a restart interval, reset the entropy decoder and
  1454  // the dc prediction
  1455  static void reset(jpeg *j)
  1456  {
  1457     j->code_bits = 0;
  1458     j->code_buffer = 0;
  1459     j->nomore = 0;
  1460     j->img_comp[0].dc_pred = j->img_comp[1].dc_pred = j->img_comp[2].dc_pred = 0;
  1461     j->marker = MARKER_none;
  1462     j->todo = j->restart_interval ? j->restart_interval : 0x7fffffff;
  1463     // no more than 1<<31 MCUs if no restart_interal? that's plenty safe,
  1464     // since we don't even allow 1<<30 pixels
  1465  }
  1466  
  1467  static int parse_entropy_coded_data(jpeg *z)
  1468  {
  1469     reset(z);
  1470     if (z->scan_n == 1) {
  1471        int i,j;
  1472        #ifdef STBI_SIMD
  1473        __declspec(align(16))
  1474        #endif
  1475        short data[64];
  1476        int n = z->order[0];
  1477        // non-interleaved data, we just need to process one block at a time,
  1478        // in trivial scanline order
  1479        // number of blocks to do just depends on how many actual "pixels" this
  1480        // component has, independent of interleaved MCU blocking and such
  1481        int w = (z->img_comp[n].x+7) >> 3;
  1482        int h = (z->img_comp[n].y+7) >> 3;
  1483        for (j=0; j < h; ++j) {
  1484           for (i=0; i < w; ++i) {
  1485              if (!decode_block(z, data, z->huff_dc+z->img_comp[n].hd, z->huff_ac+z->img_comp[n].ha, n)) return 0;
  1486              #ifdef STBI_SIMD
  1487              stbi_idct_installed(z->img_comp[n].data+z->img_comp[n].w2*j*8+i*8, z->img_comp[n].w2, data, z->dequant2[z->img_comp[n].tq]);
  1488              #else
  1489              idct_block(z->img_comp[n].data+z->img_comp[n].w2*j*8+i*8, z->img_comp[n].w2, data, z->dequant[z->img_comp[n].tq]);
  1490              #endif
  1491              // every data block is an MCU, so countdown the restart interval
  1492              if (--z->todo <= 0) {
  1493                 if (z->code_bits < 24) grow_buffer_unsafe(z);
  1494                 // if it's NOT a restart, then just bail, so we get corrupt data
  1495                 // rather than no data
  1496                 if (!RESTART(z->marker)) return 1;
  1497                 reset(z);
  1498              }
  1499           }
  1500        }
  1501     } else { // interleaved!
  1502        int i,j,k,x,y;
  1503        short data[64];
  1504        for (j=0; j < z->img_mcu_y; ++j) {
  1505           for (i=0; i < z->img_mcu_x; ++i) {
  1506              // scan an interleaved mcu... process scan_n components in order
  1507              for (k=0; k < z->scan_n; ++k) {
  1508                 int n = z->order[k];
  1509                 // scan out an mcu's worth of this component; that's just determined
  1510                 // by the basic H and V specified for the component
  1511                 for (y=0; y < z->img_comp[n].v; ++y) {
  1512                    for (x=0; x < z->img_comp[n].h; ++x) {
  1513                       int x2 = (i*z->img_comp[n].h + x)*8;
  1514                       int y2 = (j*z->img_comp[n].v + y)*8;
  1515                       if (!decode_block(z, data, z->huff_dc+z->img_comp[n].hd, z->huff_ac+z->img_comp[n].ha, n)) return 0;
  1516                       #ifdef STBI_SIMD
  1517                       stbi_idct_installed(z->img_comp[n].data+z->img_comp[n].w2*y2+x2, z->img_comp[n].w2, data, z->dequant2[z->img_comp[n].tq]);
  1518                       #else
  1519                       idct_block(z->img_comp[n].data+z->img_comp[n].w2*y2+x2, z->img_comp[n].w2, data, z->dequant[z->img_comp[n].tq]);
  1520                       #endif
  1521                    }
  1522                 }
  1523              }
  1524              // after all interleaved components, that's an interleaved MCU,
  1525              // so now count down the restart interval
  1526              if (--z->todo <= 0) {
  1527                 if (z->code_bits < 24) grow_buffer_unsafe(z);
  1528                 // if it's NOT a restart, then just bail, so we get corrupt data
  1529                 // rather than no data
  1530                 if (!RESTART(z->marker)) return 1;
  1531                 reset(z);
  1532              }
  1533           }
  1534        }
  1535     }
  1536     return 1;
  1537  }
  1538  
  1539  static int process_marker(jpeg *z, int m)
  1540  {
  1541     int L;
  1542     switch (m) {
  1543        case MARKER_none: // no marker found
  1544           return e("expected marker","Corrupt JPEG");
  1545  
  1546        case 0xC2: // SOF - progressive
  1547           return e("progressive jpeg","JPEG format not supported (progressive)");
  1548  
  1549        case 0xDD: // DRI - specify restart interval
  1550           if (get16(&z->s) != 4) return e("bad DRI len","Corrupt JPEG");
  1551           z->restart_interval = get16(&z->s);
  1552           return 1;
  1553  
  1554        case 0xDB: // DQT - define quantization table
  1555           L = get16(&z->s)-2;
  1556           while (L > 0) {
  1557              int q = get8(&z->s);
  1558              int p = q >> 4;
  1559              int t = q & 15,i;
  1560              if (p != 0) return e("bad DQT type","Corrupt JPEG");
  1561              if (t > 3) return e("bad DQT table","Corrupt JPEG");
  1562              for (i=0; i < 64; ++i)
  1563                 z->dequant[t][dezigzag[i]] = get8u(&z->s);
  1564              #ifdef STBI_SIMD
  1565              for (i=0; i < 64; ++i)
  1566                 z->dequant2[t][i] = z->dequant[t][i];
  1567              #endif
  1568              L -= 65;
  1569           }
  1570           return L==0;
  1571  
  1572        case 0xC4: // DHT - define huffman table
  1573           L = get16(&z->s)-2;
  1574           while (L > 0) {
  1575              uint8 *v;
  1576              int sizes[16],i,m=0;
  1577              int q = get8(&z->s);
  1578              int tc = q >> 4;
  1579              int th = q & 15;
  1580              if (tc > 1 || th > 3) return e("bad DHT header","Corrupt JPEG");
  1581              for (i=0; i < 16; ++i) {
  1582                 sizes[i] = get8(&z->s);
  1583                 m += sizes[i];
  1584              }
  1585              L -= 17;
  1586              if (tc == 0) {
  1587                 if (!build_huffman(z->huff_dc+th, sizes)) return 0;
  1588                 v = z->huff_dc[th].values;
  1589              } else {
  1590                 if (!build_huffman(z->huff_ac+th, sizes)) return 0;
  1591                 v = z->huff_ac[th].values;
  1592              }
  1593              for (i=0; i < m; ++i)
  1594                 v[i] = get8u(&z->s);
  1595              L -= m;
  1596           }
  1597           return L==0;
  1598     }
  1599     // check for comment block or APP blocks
  1600     if ((m >= 0xE0 && m <= 0xEF) || m == 0xFE) {
  1601        skip(&z->s, get16(&z->s)-2);
  1602        return 1;
  1603     }
  1604     return 0;
  1605  }
  1606  
  1607  // after we see SOS
  1608  static int process_scan_header(jpeg *z)
  1609  {
  1610     int i;
  1611     int Ls = get16(&z->s);
  1612     z->scan_n = get8(&z->s);
  1613     if (z->scan_n < 1 || z->scan_n > 4 || z->scan_n > (int) z->s.img_n) return e("bad SOS component count","Corrupt JPEG");
  1614     if (Ls != 6+2*z->scan_n) return e("bad SOS len","Corrupt JPEG");
  1615     for (i=0; i < z->scan_n; ++i) {
  1616        int id = get8(&z->s), which;
  1617        int q = get8(&z->s);
  1618        for (which = 0; which < z->s.img_n; ++which)
  1619           if (z->img_comp[which].id == id)
  1620              break;
  1621        if (which == z->s.img_n) return 0;
  1622        z->img_comp[which].hd = q >> 4;   if (z->img_comp[which].hd > 3) return e("bad DC huff","Corrupt JPEG");
  1623        z->img_comp[which].ha = q & 15;   if (z->img_comp[which].ha > 3) return e("bad AC huff","Corrupt JPEG");
  1624        z->order[i] = which;
  1625     }
  1626     if (get8(&z->s) != 0) return e("bad SOS","Corrupt JPEG");
  1627     get8(&z->s); // should be 63, but might be 0
  1628     if (get8(&z->s) != 0) return e("bad SOS","Corrupt JPEG");
  1629  
  1630     return 1;
  1631  }
  1632  
  1633  static int process_frame_header(jpeg *z, int scan)
  1634  {
  1635     stbi *s = &z->s;
  1636     int Lf,p,i,q, h_max=1,v_max=1,c;
  1637     Lf = get16(s);         if (Lf < 11) return e("bad SOF len","Corrupt JPEG"); // JPEG
  1638     p  = get8(s);          if (p != 8) return e("only 8-bit","JPEG format not supported: 8-bit only"); // JPEG baseline
  1639     s->img_y = get16(s);   if (s->img_y == 0) return e("no header height", "JPEG format not supported: delayed height"); // Legal, but we don't handle it--but neither does IJG
  1640     s->img_x = get16(s);   if (s->img_x == 0) return e("0 width","Corrupt JPEG"); // JPEG requires
  1641     c = get8(s);
  1642     if (c != 3 && c != 1) return e("bad component count","Corrupt JPEG");    // JFIF requires
  1643     s->img_n = c;
  1644     for (i=0; i < c; ++i) {
  1645        z->img_comp[i].data = NULL;
  1646        z->img_comp[i].linebuf = NULL;
  1647     }
  1648  
  1649     if (Lf != 8+3*s->img_n) return e("bad SOF len","Corrupt JPEG");
  1650  
  1651     for (i=0; i < s->img_n; ++i) {
  1652        z->img_comp[i].id = get8(s);
  1653        if (z->img_comp[i].id != i+1)   // JFIF requires
  1654           if (z->img_comp[i].id != i)  // some version of jpegtran outputs non-JFIF-compliant files!
  1655              return e("bad component ID","Corrupt JPEG");
  1656        q = get8(s);
  1657        z->img_comp[i].h = (q >> 4);  if (!z->img_comp[i].h || z->img_comp[i].h > 4) return e("bad H","Corrupt JPEG");
  1658        z->img_comp[i].v = q & 15;    if (!z->img_comp[i].v || z->img_comp[i].v > 4) return e("bad V","Corrupt JPEG");
  1659        z->img_comp[i].tq = get8(s);  if (z->img_comp[i].tq > 3) return e("bad TQ","Corrupt JPEG");
  1660     }
  1661  
  1662     if (scan != SCAN_load) return 1;
  1663  
  1664     if ((1 << 30) / s->img_x / s->img_n < s->img_y) return e("too large", "Image too large to decode");
  1665  
  1666     for (i=0; i < s->img_n; ++i) {
  1667        if (z->img_comp[i].h > h_max) h_max = z->img_comp[i].h;
  1668        if (z->img_comp[i].v > v_max) v_max = z->img_comp[i].v;
  1669     }
  1670  
  1671     // compute interleaved mcu info
  1672     z->img_h_max = h_max;
  1673     z->img_v_max = v_max;
  1674     z->img_mcu_w = h_max * 8;
  1675     z->img_mcu_h = v_max * 8;
  1676     z->img_mcu_x = (s->img_x + z->img_mcu_w-1) / z->img_mcu_w;
  1677     z->img_mcu_y = (s->img_y + z->img_mcu_h-1) / z->img_mcu_h;
  1678  
  1679     for (i=0; i < s->img_n; ++i) {
  1680        // number of effective pixels (e.g. for non-interleaved MCU)
  1681        z->img_comp[i].x = (s->img_x * z->img_comp[i].h + h_max-1) / h_max;
  1682        z->img_comp[i].y = (s->img_y * z->img_comp[i].v + v_max-1) / v_max;
  1683        // to simplify generation, we'll allocate enough memory to decode
  1684        // the bogus oversized data from using interleaved MCUs and their
  1685        // big blocks (e.g. a 16x16 iMCU on an image of width 33); we won't
  1686        // discard the extra data until colorspace conversion
  1687        z->img_comp[i].w2 = z->img_mcu_x * z->img_comp[i].h * 8;
  1688        z->img_comp[i].h2 = z->img_mcu_y * z->img_comp[i].v * 8;
  1689        z->img_comp[i].raw_data = malloc(z->img_comp[i].w2 * z->img_comp[i].h2+15);
  1690        if (z->img_comp[i].raw_data == NULL) {
  1691           for(--i; i >= 0; --i) {
  1692              free(z->img_comp[i].raw_data);
  1693              z->img_comp[i].data = NULL;
  1694           }
  1695           return e("outofmem", "Out of memory");
  1696        }
  1697        // align blocks for installable-idct using mmx/sse
  1698        z->img_comp[i].data = (uint8*) (((size_t) z->img_comp[i].raw_data + 15) & ~15);
  1699        z->img_comp[i].linebuf = NULL;
  1700     }
  1701  
  1702     return 1;
  1703  }
  1704  
  1705  // use comparisons since in some cases we handle more than one case (e.g. SOF)
  1706  #define DNL(x)         ((x) == 0xdc)
  1707  #define SOI(x)         ((x) == 0xd8)
  1708  #define EOI(x)         ((x) == 0xd9)
  1709  #define SOF(x)         ((x) == 0xc0 || (x) == 0xc1)
  1710  #define SOS(x)         ((x) == 0xda)
  1711  
  1712  static int decode_jpeg_header(jpeg *z, int scan)
  1713  {
  1714     int m;
  1715     z->marker = MARKER_none; // initialize cached marker to empty
  1716     m = get_marker(z);
  1717     if (!SOI(m)) return e("no SOI","Corrupt JPEG");
  1718     if (scan == SCAN_type) return 1;
  1719     m = get_marker(z);
  1720     while (!SOF(m)) {
  1721        if (!process_marker(z,m)) return 0;
  1722        m = get_marker(z);
  1723        while (m == MARKER_none) {
  1724           // some files have extra padding after their blocks, so ok, we'll scan
  1725           if (at_eof(&z->s)) return e("no SOF", "Corrupt JPEG");
  1726           m = get_marker(z);
  1727        }
  1728     }
  1729     if (!process_frame_header(z, scan)) return 0;
  1730     return 1;
  1731  }
  1732  
  1733  static int decode_jpeg_image(jpeg *j)
  1734  {
  1735     int m;
  1736     j->restart_interval = 0;
  1737     if (!decode_jpeg_header(j, SCAN_load)) return 0;
  1738     m = get_marker(j);
  1739     while (!EOI(m)) {
  1740        if (SOS(m)) {
  1741           if (!process_scan_header(j)) return 0;
  1742           if (!parse_entropy_coded_data(j)) return 0;
  1743           if (j->marker == MARKER_none ) {
  1744              // handle 0s at the end of image data from IP Kamera 9060
  1745              while (!at_eof(&j->s)) {
  1746                 int x = get8(&j->s);
  1747                 if (x == 255) {
  1748                    j->marker = get8u(&j->s);
  1749                    break;
  1750                 } else if (x != 0) {
  1751                    return 0;
  1752                 }
  1753              }
  1754              // if we reach eof without hitting a marker, get_marker() below will fail and we'll eventually return 0
  1755           }
  1756        } else {
  1757           if (!process_marker(j, m)) return 0;
  1758        }
  1759        m = get_marker(j);
  1760     }
  1761     return 1;
  1762  }
  1763  
  1764  // static jfif-centered resampling (across block boundaries)
  1765  
  1766  typedef uint8 *(*resample_row_func)(uint8 *out, uint8 *in0, uint8 *in1,
  1767                                      int w, int hs);
  1768  
  1769  #define div4(x) ((uint8) ((x) >> 2))
  1770  
  1771  static uint8 *resample_row_1(uint8 *out, uint8 *in_near, uint8 *in_far, int w, int hs)
  1772  {
  1773     STBI_NOTUSED(out);
  1774     STBI_NOTUSED(in_far);
  1775     STBI_NOTUSED(w);
  1776     STBI_NOTUSED(hs);
  1777     return in_near;
  1778  }
  1779  
  1780  static uint8* resample_row_v_2(uint8 *out, uint8 *in_near, uint8 *in_far, int w, int hs)
  1781  {
  1782     // need to generate two samples vertically for every one in input
  1783     int i;
  1784     STBI_NOTUSED(hs);
  1785     for (i=0; i < w; ++i)
  1786        out[i] = div4(3*in_near[i] + in_far[i] + 2);
  1787     return out;
  1788  }
  1789  
  1790  static uint8*  resample_row_h_2(uint8 *out, uint8 *in_near, uint8 *in_far, int w, int hs)
  1791  {
  1792     // need to generate two samples horizontally for every one in input
  1793     int i;
  1794     uint8 *input = in_near;
  1795  
  1796     if (w == 1) {
  1797        // if only one sample, can't do any interpolation
  1798        out[0] = out[1] = input[0];
  1799        return out;
  1800     }
  1801  
  1802     out[0] = input[0];
  1803     out[1] = div4(input[0]*3 + input[1] + 2);
  1804     for (i=1; i < w-1; ++i) {
  1805        int n = 3*input[i]+2;
  1806        out[i*2+0] = div4(n+input[i-1]);
  1807        out[i*2+1] = div4(n+input[i+1]);
  1808     }
  1809     out[i*2+0] = div4(input[w-2]*3 + input[w-1] + 2);
  1810     out[i*2+1] = input[w-1];
  1811  
  1812     STBI_NOTUSED(in_far);
  1813     STBI_NOTUSED(hs);
  1814  
  1815     return out;
  1816  }
  1817  
  1818  #define div16(x) ((uint8) ((x) >> 4))
  1819  
  1820  static uint8 *resample_row_hv_2(uint8 *out, uint8 *in_near, uint8 *in_far, int w, int hs)
  1821  {
  1822     // need to generate 2x2 samples for every one in input
  1823     int i,t0,t1;
  1824     if (w == 1) {
  1825        out[0] = out[1] = div4(3*in_near[0] + in_far[0] + 2);
  1826        return out;
  1827     }
  1828  
  1829     t1 = 3*in_near[0] + in_far[0];
  1830     out[0] = div4(t1+2);
  1831     for (i=1; i < w; ++i) {
  1832        t0 = t1;
  1833        t1 = 3*in_near[i]+in_far[i];
  1834        out[i*2-1] = div16(3*t0 + t1 + 8);
  1835        out[i*2  ] = div16(3*t1 + t0 + 8);
  1836     }
  1837     out[w*2-1] = div4(t1+2);
  1838  
  1839     STBI_NOTUSED(hs);
  1840  
  1841     return out;
  1842  }
  1843  
  1844  static uint8 *resample_row_generic(uint8 *out, uint8 *in_near, uint8 *in_far, int w, int hs)
  1845  {
  1846     // resample with nearest-neighbor
  1847     int i,j;
  1848     in_far = in_far;
  1849     for (i=0; i < w; ++i)
  1850        for (j=0; j < hs; ++j)
  1851           out[i*hs+j] = in_near[i];
  1852     return out;
  1853  }
  1854  
  1855  #define float2fixed(x)  ((int) ((x) * 65536 + 0.5))
  1856  
  1857  // 0.38 seconds on 3*anemones.jpg   (0.25 with processor = Pro)
  1858  // VC6 without processor=Pro is generating multiple LEAs per multiply!
  1859  static void YCbCr_to_RGB_row(uint8 *out, const uint8 *y, const uint8 *pcb, const uint8 *pcr, int count, int step)
  1860  {
  1861     int i;
  1862     for (i=0; i < count; ++i) {
  1863        int y_fixed = (y[i] << 16) + 32768; // rounding
  1864        int r,g,b;
  1865        int cr = pcr[i] - 128;
  1866        int cb = pcb[i] - 128;
  1867        r = y_fixed + cr*float2fixed(1.40200f);
  1868        g = y_fixed - cr*float2fixed(0.71414f) - cb*float2fixed(0.34414f);
  1869        b = y_fixed                            + cb*float2fixed(1.77200f);
  1870        r >>= 16;
  1871        g >>= 16;
  1872        b >>= 16;
  1873        if ((unsigned) r > 255) { if (r < 0) r = 0; else r = 255; }
  1874        if ((unsigned) g > 255) { if (g < 0) g = 0; else g = 255; }
  1875        if ((unsigned) b > 255) { if (b < 0) b = 0; else b = 255; }
  1876        out[0] = (uint8)r;
  1877        out[1] = (uint8)g;
  1878        out[2] = (uint8)b;
  1879        out[3] = 255;
  1880        out += step;
  1881     }
  1882  }
  1883  
  1884  #ifdef STBI_SIMD
  1885  static stbi_YCbCr_to_RGB_run stbi_YCbCr_installed = YCbCr_to_RGB_row;
  1886  
  1887  void stbi_install_YCbCr_to_RGB(stbi_YCbCr_to_RGB_run func)
  1888  {
  1889     stbi_YCbCr_installed = func;
  1890  }
  1891  #endif
  1892  
  1893  
  1894  // clean up the temporary component buffers
  1895  static void cleanup_jpeg(jpeg *j)
  1896  {
  1897     int i;
  1898     for (i=0; i < j->s.img_n; ++i) {
  1899        if (j->img_comp[i].data) {
  1900           free(j->img_comp[i].raw_data);
  1901           j->img_comp[i].data = NULL;
  1902        }
  1903        if (j->img_comp[i].linebuf) {
  1904           free(j->img_comp[i].linebuf);
  1905           j->img_comp[i].linebuf = NULL;
  1906        }
  1907     }
  1908  }
  1909  
  1910  typedef struct
  1911  {
  1912     resample_row_func resample;
  1913     uint8 *line0,*line1;
  1914     int hs,vs;   // expansion factor in each axis
  1915     int w_lores; // horizontal pixels pre-expansion
  1916     int ystep;   // how far through vertical expansion we are
  1917     int ypos;    // which pre-expansion row we're on
  1918  } stbi_resample;
  1919  
  1920  static uint8 *load_jpeg_image(jpeg *z, int *out_x, int *out_y, int *comp, int req_comp)
  1921  {
  1922     int n, decode_n;
  1923     // validate req_comp
  1924     if (req_comp < 0 || req_comp > 4) return epuc("bad req_comp", "Internal error");
  1925     z->s.img_n = 0;
  1926  
  1927     // load a jpeg image from whichever source
  1928     if (!decode_jpeg_image(z)) { cleanup_jpeg(z); return NULL; }
  1929  
  1930     // determine actual number of components to generate
  1931     n = req_comp ? req_comp : z->s.img_n;
  1932  
  1933     if (z->s.img_n == 3 && n < 3)
  1934        decode_n = 1;
  1935     else
  1936        decode_n = z->s.img_n;
  1937  
  1938     // resample and color-convert
  1939     {
  1940        int k;
  1941        uint i,j;
  1942        uint8 *output;
  1943        uint8 *coutput[4];
  1944  
  1945        stbi_resample res_comp[4];
  1946  
  1947        for (k=0; k < decode_n; ++k) {
  1948           stbi_resample *r = &res_comp[k];
  1949  
  1950           // allocate line buffer big enough for upsampling off the edges
  1951           // with upsample factor of 4
  1952           z->img_comp[k].linebuf = (uint8 *) malloc(z->s.img_x + 3);
  1953           if (!z->img_comp[k].linebuf) { cleanup_jpeg(z); return epuc("outofmem", "Out of memory"); }
  1954  
  1955           r->hs      = z->img_h_max / z->img_comp[k].h;
  1956           r->vs      = z->img_v_max / z->img_comp[k].v;
  1957           r->ystep   = r->vs >> 1;
  1958           r->w_lores = (z->s.img_x + r->hs-1) / r->hs;
  1959           r->ypos    = 0;
  1960           r->line0   = r->line1 = z->img_comp[k].data;
  1961  
  1962           if      (r->hs == 1 && r->vs == 1) r->resample = resample_row_1;
  1963           else if (r->hs == 1 && r->vs == 2) r->resample = resample_row_v_2;
  1964           else if (r->hs == 2 && r->vs == 1) r->resample = resample_row_h_2;
  1965           else if (r->hs == 2 && r->vs == 2) r->resample = resample_row_hv_2;
  1966           else                               r->resample = resample_row_generic;
  1967        }
  1968  
  1969        // can't error after this so, this is safe
  1970        output = (uint8 *) malloc(n * z->s.img_x * z->s.img_y + 1);
  1971        if (!output) { cleanup_jpeg(z); return epuc("outofmem", "Out of memory"); }
  1972  
  1973        // now go ahead and resample
  1974        for (j=0; j < z->s.img_y; ++j) {
  1975           uint8 *out = output + n * z->s.img_x * j;
  1976           for (k=0; k < decode_n; ++k) {
  1977              stbi_resample *r = &res_comp[k];
  1978              int y_bot = r->ystep >= (r->vs >> 1);
  1979              coutput[k] = r->resample(z->img_comp[k].linebuf,
  1980                                       y_bot ? r->line1 : r->line0,
  1981                                       y_bot ? r->line0 : r->line1,
  1982                                       r->w_lores, r->hs);
  1983              if (++r->ystep >= r->vs) {
  1984                 r->ystep = 0;
  1985                 r->line0 = r->line1;
  1986                 if (++r->ypos < z->img_comp[k].y)
  1987                    r->line1 += z->img_comp[k].w2;
  1988              }
  1989           }
  1990           if (n >= 3) {
  1991              uint8 *y = coutput[0];
  1992              if (z->s.img_n == 3) {
  1993                 #ifdef STBI_SIMD
  1994                 stbi_YCbCr_installed(out, y, coutput[1], coutput[2], z->s.img_x, n);
  1995                 #else
  1996                 YCbCr_to_RGB_row(out, y, coutput[1], coutput[2], z->s.img_x, n);
  1997                 #endif
  1998              } else
  1999                 for (i=0; i < z->s.img_x; ++i) {
  2000                    out[0] = out[1] = out[2] = y[i];
  2001                    out[3] = 255; // not used if n==3
  2002                    out += n;
  2003                 }
  2004           } else {
  2005              uint8 *y = coutput[0];
  2006              if (n == 1)
  2007                 for (i=0; i < z->s.img_x; ++i) out[i] = y[i];
  2008              else
  2009                 for (i=0; i < z->s.img_x; ++i) *out++ = y[i], *out++ = 255;
  2010           }
  2011        }
  2012        cleanup_jpeg(z);
  2013        *out_x = z->s.img_x;
  2014        *out_y = z->s.img_y;
  2015        if (comp) *comp  = z->s.img_n; // report original components, not output
  2016        return output;
  2017     }
  2018  }
  2019  
  2020  #ifndef STBI_NO_STDIO
  2021  unsigned char *stbi_jpeg_load_from_file(FILE *f, int *x, int *y, int *comp, int req_comp)
  2022  {
  2023     jpeg j;
  2024     start_file(&j.s, f);
  2025     return load_jpeg_image(&j, x,y,comp,req_comp);
  2026  }
  2027  
  2028  unsigned char *stbi_jpeg_load(char const *filename, int *x, int *y, int *comp, int req_comp)
  2029  {
  2030     unsigned char *data;
  2031     FILE *f = fopen(filename, "rb");
  2032     if (!f) return NULL;
  2033     data = stbi_jpeg_load_from_file(f,x,y,comp,req_comp);
  2034     fclose(f);
  2035     return data;
  2036  }
  2037  #endif
  2038  
  2039  unsigned char *stbi_jpeg_load_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp)
  2040  {
  2041     #ifdef STBI_SMALL_STACK
  2042     unsigned char *result;
  2043     jpeg *j = (jpeg *) malloc(sizeof(*j));
  2044     start_mem(&j->s, buffer, len);
  2045     result = load_jpeg_image(j,x,y,comp,req_comp);
  2046     free(j);
  2047     return result;
  2048     #else
  2049     jpeg j;
  2050     start_mem(&j.s, buffer,len);
  2051     return load_jpeg_image(&j, x,y,comp,req_comp);
  2052     #endif
  2053  }
  2054  
  2055  static int stbi_jpeg_info_raw(jpeg *j, int *x, int *y, int *comp)
  2056  {
  2057     if (!decode_jpeg_header(j, SCAN_header))
  2058        return 0;
  2059     if (x) *x = j->s.img_x;
  2060     if (y) *y = j->s.img_y;
  2061     if (comp) *comp = j->s.img_n;
  2062     return 1;
  2063  }
  2064  
  2065  #ifndef STBI_NO_STDIO
  2066  int stbi_jpeg_test_file(FILE *f)
  2067  {
  2068     int n,r;
  2069     jpeg j;
  2070     n = ftell(f);
  2071     start_file(&j.s, f);
  2072     r = decode_jpeg_header(&j, SCAN_type);
  2073     fseek(f,n,SEEK_SET);
  2074     return r;
  2075  }
  2076  
  2077  int stbi_jpeg_info_from_file(FILE *f, int *x, int *y, int *comp)
  2078  {
  2079      jpeg j;
  2080      long n = ftell(f);
  2081      int res;
  2082      start_file(&j.s, f);
  2083      res = stbi_jpeg_info_raw(&j, x, y, comp);
  2084      fseek(f, n, SEEK_SET);
  2085      return res;
  2086  }
  2087  
  2088  int stbi_jpeg_info(char const *filename, int *x, int *y, int *comp)
  2089  {
  2090      FILE *f = fopen(filename, "rb");
  2091      int result;
  2092      if (!f) return e("can't fopen", "Unable to open file");
  2093      result = stbi_jpeg_info_from_file(f, x, y, comp);
  2094      fclose(f);
  2095      return result;
  2096  }
  2097  #endif
  2098  
  2099  int stbi_jpeg_test_memory(stbi_uc const *buffer, int len)
  2100  {
  2101     jpeg j;
  2102     start_mem(&j.s, buffer,len);
  2103     return decode_jpeg_header(&j, SCAN_type);
  2104  }
  2105  
  2106  int stbi_jpeg_info_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp)
  2107  {
  2108      jpeg j;
  2109      start_mem(&j.s, buffer, len);
  2110      return stbi_jpeg_info_raw(&j, x, y, comp);
  2111  }
  2112  
  2113  #ifndef STBI_NO_STDIO
  2114  extern int      stbi_jpeg_info            (char const *filename,           int *x, int *y, int *comp);
  2115  extern int      stbi_jpeg_info_from_file  (FILE *f,                  int *x, int *y, int *comp);
  2116  #endif
  2117  extern int      stbi_jpeg_info_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp);
  2118  
  2119  // public domain zlib decode    v0.2  Sean Barrett 2006-11-18
  2120  //    simple implementation
  2121  //      - all input must be provided in an upfront buffer
  2122  //      - all output is written to a single output buffer (can malloc/realloc)
  2123  //    performance
  2124  //      - fast huffman
  2125  
  2126  // fast-way is faster to check than jpeg huffman, but slow way is slower
  2127  #define ZFAST_BITS  9 // accelerate all cases in default tables
  2128  #define ZFAST_MASK  ((1 << ZFAST_BITS) - 1)
  2129  
  2130  // zlib-style huffman encoding
  2131  // (jpegs packs from left, zlib from right, so can't share code)
  2132  typedef struct
  2133  {
  2134     uint16 fast[1 << ZFAST_BITS];
  2135     uint16 firstcode[16];
  2136     int maxcode[17];
  2137     uint16 firstsymbol[16];
  2138     uint8  size[288];
  2139     uint16 value[288];
  2140  } zhuffman;
  2141  
  2142  __forceinline static int bitreverse16(int n)
  2143  {
  2144    n = ((n & 0xAAAA) >>  1) | ((n & 0x5555) << 1);
  2145    n = ((n & 0xCCCC) >>  2) | ((n & 0x3333) << 2);
  2146    n = ((n & 0xF0F0) >>  4) | ((n & 0x0F0F) << 4);
  2147    n = ((n & 0xFF00) >>  8) | ((n & 0x00FF) << 8);
  2148    return n;
  2149  }
  2150  
  2151  __forceinline static int bit_reverse(int v, int bits)
  2152  {
  2153     assert(bits <= 16);
  2154     // to bit reverse n bits, reverse 16 and shift
  2155     // e.g. 11 bits, bit reverse and shift away 5
  2156     return bitreverse16(v) >> (16-bits);
  2157  }
  2158  
  2159  static int zbuild_huffman(zhuffman *z, uint8 *sizelist, int num)
  2160  {
  2161     int i,k=0;
  2162     int code, next_code[16], sizes[17];
  2163  
  2164     // DEFLATE spec for generating codes
  2165     memset(sizes, 0, sizeof(sizes));
  2166     memset(z->fast, 255, sizeof(z->fast));
  2167     for (i=0; i < num; ++i)
  2168        ++sizes[sizelist[i]];
  2169     sizes[0] = 0;
  2170     for (i=1; i < 16; ++i)
  2171        assert(sizes[i] <= (1 << i));
  2172     code = 0;
  2173     for (i=1; i < 16; ++i) {
  2174        next_code[i] = code;
  2175        z->firstcode[i] = (uint16) code;
  2176        z->firstsymbol[i] = (uint16) k;
  2177        code = (code + sizes[i]);
  2178        if (sizes[i])
  2179           if (code-1 >= (1 << i)) return e("bad codelengths","Corrupt JPEG");
  2180        z->maxcode[i] = code << (16-i); // preshift for inner loop
  2181        code <<= 1;
  2182        k += sizes[i];
  2183     }
  2184     z->maxcode[16] = 0x10000; // sentinel
  2185     for (i=0; i < num; ++i) {
  2186        int s = sizelist[i];
  2187        if (s) {
  2188           int c = next_code[s] - z->firstcode[s] + z->firstsymbol[s];
  2189           z->size[c] = (uint8)s;
  2190           z->value[c] = (uint16)i;
  2191           if (s <= ZFAST_BITS) {
  2192              int k = bit_reverse(next_code[s],s);
  2193              while (k < (1 << ZFAST_BITS)) {
  2194                 z->fast[k] = (uint16) c;
  2195                 k += (1 << s);
  2196              }
  2197           }
  2198           ++next_code[s];
  2199        }
  2200     }
  2201     return 1;
  2202  }
  2203  
  2204  // zlib-from-memory implementation for PNG reading
  2205  //    because PNG allows splitting the zlib stream arbitrarily,
  2206  //    and it's annoying structurally to have PNG call ZLIB call PNG,
  2207  //    we require PNG read all the IDATs and combine them into a single
  2208  //    memory buffer
  2209  
  2210  typedef struct
  2211  {
  2212     uint8 *zbuffer, *zbuffer_end;
  2213     int num_bits;
  2214     uint32 code_buffer;
  2215  
  2216     char *zout;
  2217     char *zout_start;
  2218     char *zout_end;
  2219     int   z_expandable;
  2220  
  2221     zhuffman z_length, z_distance;
  2222  } zbuf;
  2223  
  2224  __forceinline static int zget8(zbuf *z)
  2225  {
  2226     if (z->zbuffer >= z->zbuffer_end) return 0;
  2227     return *z->zbuffer++;
  2228  }
  2229  
  2230  static void fill_bits(zbuf *z)
  2231  {
  2232     do {
  2233        assert(z->code_buffer < (1U << z->num_bits));
  2234        z->code_buffer |= zget8(z) << z->num_bits;
  2235        z->num_bits += 8;
  2236     } while (z->num_bits <= 24);
  2237  }
  2238  
  2239  __forceinline static unsigned int zreceive(zbuf *z, int n)
  2240  {
  2241     unsigned int k;
  2242     if (z->num_bits < n) fill_bits(z);
  2243     k = z->code_buffer & ((1 << n) - 1);
  2244     z->code_buffer >>= n;
  2245     z->num_bits -= n;
  2246     return k;
  2247  }
  2248  
  2249  __forceinline static int zhuffman_decode(zbuf *a, zhuffman *z)
  2250  {
  2251     int b,s,k;
  2252     if (a->num_bits < 16) fill_bits(a);
  2253     b = z->fast[a->code_buffer & ZFAST_MASK];
  2254     if (b < 0xffff) {
  2255        s = z->size[b];
  2256        a->code_buffer >>= s;
  2257        a->num_bits -= s;
  2258        return z->value[b];
  2259     }
  2260  
  2261     // not resolved by fast table, so compute it the slow way
  2262     // use jpeg approach, which requires MSbits at top
  2263     k = bit_reverse(a->code_buffer, 16);
  2264     for (s=ZFAST_BITS+1; ; ++s)
  2265        if (k < z->maxcode[s])
  2266           break;
  2267     if (s == 16) return -1; // invalid code!
  2268     // code size is s, so:
  2269     b = (k >> (16-s)) - z->firstcode[s] + z->firstsymbol[s];
  2270     assert(z->size[b] == s);
  2271     a->code_buffer >>= s;
  2272     a->num_bits -= s;
  2273     return z->value[b];
  2274  }
  2275  
  2276  static int expand(zbuf *z, int n)  // need to make room for n bytes
  2277  {
  2278     char *q;
  2279     int cur, limit;
  2280     if (!z->z_expandable) return e("output buffer limit","Corrupt PNG");
  2281     cur   = (int) (z->zout     - z->zout_start);
  2282     limit = (int) (z->zout_end - z->zout_start);
  2283     while (cur + n > limit)
  2284        limit *= 2;
  2285     q = (char *) realloc(z->zout_start, limit);
  2286     if (q == NULL) return e("outofmem", "Out of memory");
  2287     z->zout_start = q;
  2288     z->zout       = q + cur;
  2289     z->zout_end   = q + limit;
  2290     return 1;
  2291  }
  2292  
  2293  static int length_base[31] = {
  2294     3,4,5,6,7,8,9,10,11,13,
  2295     15,17,19,23,27,31,35,43,51,59,
  2296     67,83,99,115,131,163,195,227,258,0,0 };
  2297  
  2298  static int length_extra[31]=
  2299  { 0,0,0,0,0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,0,0,0 };
  2300  
  2301  static int dist_base[32] = { 1,2,3,4,5,7,9,13,17,25,33,49,65,97,129,193,
  2302  257,385,513,769,1025,1537,2049,3073,4097,6145,8193,12289,16385,24577,0,0};
  2303  
  2304  static int dist_extra[32] =
  2305  { 0,0,0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,13};
  2306  
  2307  static int parse_huffman_block(zbuf *a)
  2308  {
  2309     for(;;) {
  2310        int z = zhuffman_decode(a, &a->z_length);
  2311        if (z < 256) {
  2312           if (z < 0) return e("bad huffman code","Corrupt PNG"); // error in huffman codes
  2313           if (a->zout >= a->zout_end) if (!expand(a, 1)) return 0;
  2314           *a->zout++ = (char) z;
  2315        } else {
  2316           uint8 *p;
  2317           int len,dist;
  2318           if (z == 256) return 1;
  2319           z -= 257;
  2320           len = length_base[z];
  2321           if (length_extra[z]) len += zreceive(a, length_extra[z]);
  2322           z = zhuffman_decode(a, &a->z_distance);
  2323           if (z < 0) return e("bad huffman code","Corrupt PNG");
  2324           dist = dist_base[z];
  2325           if (dist_extra[z]) dist += zreceive(a, dist_extra[z]);
  2326           if (a->zout - a->zout_start < dist) return e("bad dist","Corrupt PNG");
  2327           if (a->zout + len > a->zout_end) if (!expand(a, len)) return 0;
  2328           p = (uint8 *) (a->zout - dist);
  2329           while (len--)
  2330              *a->zout++ = *p++;
  2331        }
  2332     }
  2333  }
  2334  
  2335  static int compute_huffman_codes(zbuf *a)
  2336  {
  2337     static uint8 length_dezigzag[19] = { 16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15 };
  2338     zhuffman z_codelength;
  2339     uint8 lencodes[286+32+137];//padding for maximum single op
  2340     uint8 codelength_sizes[19];
  2341     int i,n;
  2342  
  2343     int hlit  = zreceive(a,5) + 257;
  2344     int hdist = zreceive(a,5) + 1;
  2345     int hclen = zreceive(a,4) + 4;
  2346  
  2347     memset(codelength_sizes, 0, sizeof(codelength_sizes));
  2348     for (i=0; i < hclen; ++i) {
  2349        int s = zreceive(a,3);
  2350        codelength_sizes[length_dezigzag[i]] = (uint8) s;
  2351     }
  2352     if (!zbuild_huffman(&z_codelength, codelength_sizes, 19)) return 0;
  2353  
  2354     n = 0;
  2355     while (n < hlit + hdist) {
  2356        int c = zhuffman_decode(a, &z_codelength);
  2357        assert(c >= 0 && c < 19);
  2358        if (c < 16)
  2359           lencodes[n++] = (uint8) c;
  2360        else if (c == 16) {
  2361           c = zreceive(a,2)+3;
  2362           memset(lencodes+n, lencodes[n-1], c);
  2363           n += c;
  2364        } else if (c == 17) {
  2365           c = zreceive(a,3)+3;
  2366           memset(lencodes+n, 0, c);
  2367           n += c;
  2368        } else {
  2369           assert(c == 18);
  2370           c = zreceive(a,7)+11;
  2371           memset(lencodes+n, 0, c);
  2372           n += c;
  2373        }
  2374     }
  2375     if (n != hlit+hdist) return e("bad codelengths","Corrupt PNG");
  2376     if (!zbuild_huffman(&a->z_length, lencodes, hlit)) return 0;
  2377     if (!zbuild_huffman(&a->z_distance, lencodes+hlit, hdist)) return 0;
  2378     return 1;
  2379  }
  2380  
  2381  static int parse_uncompressed_block(zbuf *a)
  2382  {
  2383     uint8 header[4];
  2384     int len,nlen,k;
  2385     if (a->num_bits & 7)
  2386        zreceive(a, a->num_bits & 7); // discard
  2387     // drain the bit-packed data into header
  2388     k = 0;
  2389     while (a->num_bits > 0) {
  2390        header[k++] = (uint8) (a->code_buffer & 255); // wtf this warns?
  2391        a->code_buffer >>= 8;
  2392        a->num_bits -= 8;
  2393     }
  2394     assert(a->num_bits == 0);
  2395     // now fill header the normal way
  2396     while (k < 4)
  2397        header[k++] = (uint8) zget8(a);
  2398     len  = header[1] * 256 + header[0];
  2399     nlen = header[3] * 256 + header[2];
  2400     if (nlen != (len ^ 0xffff)) return e("zlib corrupt","Corrupt PNG");
  2401     if (a->zbuffer + len > a->zbuffer_end) return e("read past buffer","Corrupt PNG");
  2402     if (a->zout + len > a->zout_end)
  2403        if (!expand(a, len)) return 0;
  2404     memcpy(a->zout, a->zbuffer, len);
  2405     a->zbuffer += len;
  2406     a->zout += len;
  2407     return 1;
  2408  }
  2409  
  2410  static int parse_zlib_header(zbuf *a)
  2411  {
  2412     int cmf   = zget8(a);
  2413     int cm    = cmf & 15;
  2414     /* int cinfo = cmf >> 4; */
  2415     int flg   = zget8(a);
  2416     if ((cmf*256+flg) % 31 != 0) return e("bad zlib header","Corrupt PNG"); // zlib spec
  2417     if (flg & 32) return e("no preset dict","Corrupt PNG"); // preset dictionary not allowed in png
  2418     if (cm != 8) return e("bad compression","Corrupt PNG"); // DEFLATE required for png
  2419     // window = 1 << (8 + cinfo)... but who cares, we fully buffer output
  2420     return 1;
  2421  }
  2422  
  2423  // @TODO: should statically initialize these for optimal thread safety
  2424  static uint8 default_length[288], default_distance[32];
  2425  static void init_defaults(void)
  2426  {
  2427     int i;   // use <= to match clearly with spec
  2428     for (i=0; i <= 143; ++i)     default_length[i]   = 8;
  2429     for (   ; i <= 255; ++i)     default_length[i]   = 9;
  2430     for (   ; i <= 279; ++i)     default_length[i]   = 7;
  2431     for (   ; i <= 287; ++i)     default_length[i]   = 8;
  2432  
  2433     for (i=0; i <=  31; ++i)     default_distance[i] = 5;
  2434  }
  2435  
  2436  int stbi_png_partial; // a quick hack to only allow decoding some of a PNG... I should implement real streaming support instead
  2437  static int parse_zlib(zbuf *a, int parse_header)
  2438  {
  2439     int final, type;
  2440     if (parse_header)
  2441        if (!parse_zlib_header(a)) return 0;
  2442     a->num_bits = 0;
  2443     a->code_buffer = 0;
  2444     do {
  2445        final = zreceive(a,1);
  2446        type = zreceive(a,2);
  2447        if (type == 0) {
  2448           if (!parse_uncompressed_block(a)) return 0;
  2449        } else if (type == 3) {
  2450           return 0;
  2451        } else {
  2452           if (type == 1) {
  2453              // use fixed code lengths
  2454              if (!default_distance[31]) init_defaults();
  2455              if (!zbuild_huffman(&a->z_length  , default_length  , 288)) return 0;
  2456              if (!zbuild_huffman(&a->z_distance, default_distance,  32)) return 0;
  2457           } else {
  2458              if (!compute_huffman_codes(a)) return 0;
  2459           }
  2460           if (!parse_huffman_block(a)) return 0;
  2461        }
  2462        if (stbi_png_partial && a->zout - a->zout_start > 65536)
  2463           break;
  2464     } while (!final);
  2465     return 1;
  2466  }
  2467  
  2468  static int do_zlib(zbuf *a, char *obuf, int olen, int exp, int parse_header)
  2469  {
  2470     a->zout_start = obuf;
  2471     a->zout       = obuf;
  2472     a->zout_end   = obuf + olen;
  2473     a->z_expandable = exp;
  2474  
  2475     return parse_zlib(a, parse_header);
  2476  }
  2477  
  2478  char *stbi_zlib_decode_malloc_guesssize(const char *buffer, int len, int initial_size, int *outlen)
  2479  {
  2480     zbuf a;
  2481     char *p = (char *) malloc(initial_size);
  2482     if (p == NULL) return NULL;
  2483     a.zbuffer = (uint8 *) buffer;
  2484     a.zbuffer_end = (uint8 *) buffer + len;
  2485     if (do_zlib(&a, p, initial_size, 1, 1)) {
  2486        if (outlen) *outlen = (int) (a.zout - a.zout_start);
  2487        return a.zout_start;
  2488     } else {
  2489        free(a.zout_start);
  2490        return NULL;
  2491     }
  2492  }
  2493  
  2494  char *stbi_zlib_decode_malloc(char const *buffer, int len, int *outlen)
  2495  {
  2496     return stbi_zlib_decode_malloc_guesssize(buffer, len, 16384, outlen);
  2497  }
  2498  
  2499  char *stbi_zlib_decode_malloc_guesssize_headerflag(const char *buffer, int len, int initial_size, int *outlen, int parse_header)
  2500  {
  2501     zbuf a;
  2502     char *p = (char *) malloc(initial_size);
  2503     if (p == NULL) return NULL;
  2504     a.zbuffer = (uint8 *) buffer;
  2505     a.zbuffer_end = (uint8 *) buffer + len;
  2506     if (do_zlib(&a, p, initial_size, 1, parse_header)) {
  2507        if (outlen) *outlen = (int) (a.zout - a.zout_start);
  2508        return a.zout_start;
  2509     } else {
  2510        free(a.zout_start);
  2511        return NULL;
  2512     }
  2513  }
  2514  
  2515  int stbi_zlib_decode_buffer(char *obuffer, int olen, char const *ibuffer, int ilen)
  2516  {
  2517     zbuf a;
  2518     a.zbuffer = (uint8 *) ibuffer;
  2519     a.zbuffer_end = (uint8 *) ibuffer + ilen;
  2520     if (do_zlib(&a, obuffer, olen, 0, 1))
  2521        return (int) (a.zout - a.zout_start);
  2522     else
  2523        return -1;
  2524  }
  2525  
  2526  char *stbi_zlib_decode_noheader_malloc(char const *buffer, int len, int *outlen)
  2527  {
  2528     zbuf a;
  2529     char *p = (char *) malloc(16384);
  2530     if (p == NULL) return NULL;
  2531     a.zbuffer = (uint8 *) buffer;
  2532     a.zbuffer_end = (uint8 *) buffer+len;
  2533     if (do_zlib(&a, p, 16384, 1, 0)) {
  2534        if (outlen) *outlen = (int) (a.zout - a.zout_start);
  2535        return a.zout_start;
  2536     } else {
  2537        free(a.zout_start);
  2538        return NULL;
  2539     }
  2540  }
  2541  
  2542  int stbi_zlib_decode_noheader_buffer(char *obuffer, int olen, const char *ibuffer, int ilen)
  2543  {
  2544     zbuf a;
  2545     a.zbuffer = (uint8 *) ibuffer;
  2546     a.zbuffer_end = (uint8 *) ibuffer + ilen;
  2547     if (do_zlib(&a, obuffer, olen, 0, 0))
  2548        return (int) (a.zout - a.zout_start);
  2549     else
  2550        return -1;
  2551  }
  2552  
  2553  // public domain "baseline" PNG decoder   v0.10  Sean Barrett 2006-11-18
  2554  //    simple implementation
  2555  //      - only 8-bit samples
  2556  //      - no CRC checking
  2557  //      - allocates lots of intermediate memory
  2558  //        - avoids problem of streaming data between subsystems
  2559  //        - avoids explicit window management
  2560  //    performance
  2561  //      - uses stb_zlib, a PD zlib implementation with fast huffman decoding
  2562  
  2563  
  2564  typedef struct
  2565  {
  2566     uint32 length;
  2567     uint32 type;
  2568  } chunk;
  2569  
  2570  #define PNG_TYPE(a,b,c,d)  (((a) << 24) + ((b) << 16) + ((c) << 8) + (d))
  2571  
  2572  static chunk get_chunk_header(stbi *s)
  2573  {
  2574     chunk c;
  2575     c.length = get32(s);
  2576     c.type   = get32(s);
  2577     return c;
  2578  }
  2579  
  2580  static int check_png_header(stbi *s)
  2581  {
  2582     static uint8 png_sig[8] = { 137,80,78,71,13,10,26,10 };
  2583     int i;
  2584     for (i=0; i < 8; ++i)
  2585        if (get8(s) != png_sig[i]) return e("bad png sig","Not a PNG");
  2586     return 1;
  2587  }
  2588  
  2589  typedef struct
  2590  {
  2591     stbi s;
  2592     uint8 *idata, *expanded, *out;
  2593  } png;
  2594  
  2595  
  2596  enum {
  2597     F_none=0, F_sub=1, F_up=2, F_avg=3, F_paeth=4,
  2598     F_avg_first, F_paeth_first
  2599  };
  2600  
  2601  static uint8 first_row_filter[5] =
  2602  {
  2603     F_none, F_sub, F_none, F_avg_first, F_paeth_first
  2604  };
  2605  
  2606  static int paeth(int a, int b, int c)
  2607  {
  2608     int p = a + b - c;
  2609     int pa = abs(p-a);
  2610     int pb = abs(p-b);
  2611     int pc = abs(p-c);
  2612     if (pa <= pb && pa <= pc) return a;
  2613     if (pb <= pc) return b;
  2614     return c;
  2615  }
  2616  
  2617  // create the png data from post-deflated data
  2618  static int create_png_image_raw(png *a, uint8 *raw, uint32 raw_len, int out_n, uint32 x, uint32 y)
  2619  {
  2620     stbi *s = &a->s;
  2621     uint32 i,j,stride = x*out_n;
  2622     int k;
  2623     int img_n = s->img_n; // copy it into a local for later
  2624     assert(out_n == s->img_n || out_n == s->img_n+1);
  2625     if (stbi_png_partial) y = 1;
  2626     a->out = (uint8 *) malloc(x * y * out_n);
  2627     if (!a->out) return e("outofmem", "Out of memory");
  2628     if (!stbi_png_partial) {
  2629        if (s->img_x == x && s->img_y == y) {
  2630           if (raw_len != (img_n * x + 1) * y) return e("not enough pixels","Corrupt PNG");
  2631        } else { // interlaced:
  2632           if (raw_len < (img_n * x + 1) * y) return e("not enough pixels","Corrupt PNG");
  2633        }
  2634     }
  2635     for (j=0; j < y; ++j) {
  2636        uint8 *cur = a->out + stride*j;
  2637        uint8 *prior = cur - stride;
  2638        int filter = *raw++;
  2639        if (filter > 4) return e("invalid filter","Corrupt PNG");
  2640        // if first row, use special filter that doesn't sample previous row
  2641        if (j == 0) filter = first_row_filter[filter];
  2642        // handle first pixel explicitly
  2643        for (k=0; k < img_n; ++k) {
  2644           switch (filter) {
  2645              case F_none       : cur[k] = raw[k]; break;
  2646              case F_sub        : cur[k] = raw[k]; break;
  2647              case F_up         : cur[k] = raw[k] + prior[k]; break;
  2648              case F_avg        : cur[k] = raw[k] + (prior[k]>>1); break;
  2649              case F_paeth      : cur[k] = (uint8) (raw[k] + paeth(0,prior[k],0)); break;
  2650              case F_avg_first  : cur[k] = raw[k]; break;
  2651              case F_paeth_first: cur[k] = raw[k]; break;
  2652           }
  2653        }
  2654        if (img_n != out_n) cur[img_n] = 255;
  2655        raw += img_n;
  2656        cur += out_n;
  2657        prior += out_n;
  2658        // this is a little gross, so that we don't switch per-pixel or per-component
  2659        if (img_n == out_n) {
  2660           #define CASE(f) \
  2661               case f:     \
  2662                  for (i=x-1; i >= 1; --i, raw+=img_n,cur+=img_n,prior+=img_n) \
  2663                     for (k=0; k < img_n; ++k)
  2664           switch (filter) {
  2665              CASE(F_none)  cur[k] = raw[k]; break;
  2666              CASE(F_sub)   cur[k] = raw[k] + cur[k-img_n]; break;
  2667              CASE(F_up)    cur[k] = raw[k] + prior[k]; break;
  2668              CASE(F_avg)   cur[k] = raw[k] + ((prior[k] + cur[k-img_n])>>1); break;
  2669              CASE(F_paeth)  cur[k] = (uint8) (raw[k] + paeth(cur[k-img_n],prior[k],prior[k-img_n])); break;
  2670              CASE(F_avg_first)    cur[k] = raw[k] + (cur[k-img_n] >> 1); break;
  2671              CASE(F_paeth_first)  cur[k] = (uint8) (raw[k] + paeth(cur[k-img_n],0,0)); break;
  2672           }
  2673           #undef CASE
  2674        } else {
  2675           assert(img_n+1 == out_n);
  2676           #define CASE(f) \
  2677               case f:     \
  2678                  for (i=x-1; i >= 1; --i, cur[img_n]=255,raw+=img_n,cur+=out_n,prior+=out_n) \
  2679                     for (k=0; k < img_n; ++k)
  2680           switch (filter) {
  2681              CASE(F_none)  cur[k] = raw[k]; break;
  2682              CASE(F_sub)   cur[k] = raw[k] + cur[k-out_n]; break;
  2683              CASE(F_up)    cur[k] = raw[k] + prior[k]; break;
  2684              CASE(F_avg)   cur[k] = raw[k] + ((prior[k] + cur[k-out_n])>>1); break;
  2685              CASE(F_paeth)  cur[k] = (uint8) (raw[k] + paeth(cur[k-out_n],prior[k],prior[k-out_n])); break;
  2686              CASE(F_avg_first)    cur[k] = raw[k] + (cur[k-out_n] >> 1); break;
  2687              CASE(F_paeth_first)  cur[k] = (uint8) (raw[k] + paeth(cur[k-out_n],0,0)); break;
  2688           }
  2689           #undef CASE
  2690        }
  2691     }
  2692     return 1;
  2693  }
  2694  
  2695  static int create_png_image(png *a, uint8 *raw, uint32 raw_len, int out_n, int interlaced)
  2696  {
  2697     uint8 *final;
  2698     int p;
  2699     int save;
  2700     if (!interlaced)
  2701        return create_png_image_raw(a, raw, raw_len, out_n, a->s.img_x, a->s.img_y);
  2702     save = stbi_png_partial;
  2703     stbi_png_partial = 0;
  2704  
  2705     // de-interlacing
  2706     final = (uint8 *) malloc(a->s.img_x * a->s.img_y * out_n);
  2707     for (p=0; p < 7; ++p) {
  2708        int xorig[] = { 0,4,0,2,0,1,0 };
  2709        int yorig[] = { 0,0,4,0,2,0,1 };
  2710        int xspc[]  = { 8,8,4,4,2,2,1 };
  2711        int yspc[]  = { 8,8,8,4,4,2,2 };
  2712        int i,j,x,y;
  2713        // pass1_x[4] = 0, pass1_x[5] = 1, pass1_x[12] = 1
  2714        x = (a->s.img_x - xorig[p] + xspc[p]-1) / xspc[p];
  2715        y = (a->s.img_y - yorig[p] + yspc[p]-1) / yspc[p];
  2716        if (x && y) {
  2717           if (!create_png_image_raw(a, raw, raw_len, out_n, x, y)) {
  2718              free(final);
  2719              return 0;
  2720           }
  2721           for (j=0; j < y; ++j)
  2722              for (i=0; i < x; ++i)
  2723                 memcpy(final + (j*yspc[p]+yorig[p])*a->s.img_x*out_n + (i*xspc[p]+xorig[p])*out_n,
  2724                        a->out + (j*x+i)*out_n, out_n);
  2725           free(a->out);
  2726           raw += (x*out_n+1)*y;
  2727           raw_len -= (x*out_n+1)*y;
  2728        }
  2729     }
  2730     a->out = final;
  2731  
  2732     stbi_png_partial = save;
  2733     return 1;
  2734  }
  2735  
  2736  static int compute_transparency(png *z, uint8 tc[3], int out_n)
  2737  {
  2738     stbi *s = &z->s;
  2739     uint32 i, pixel_count = s->img_x * s->img_y;
  2740     uint8 *p = z->out;
  2741  
  2742     // compute color-based transparency, assuming we've
  2743     // already got 255 as the alpha value in the output
  2744     assert(out_n == 2 || out_n == 4);
  2745  
  2746     if (out_n == 2) {
  2747        for (i=0; i < pixel_count; ++i) {
  2748           p[1] = (p[0] == tc[0] ? 0 : 255);
  2749           p += 2;
  2750        }
  2751     } else {
  2752        for (i=0; i < pixel_count; ++i) {
  2753           if (p[0] == tc[0] && p[1] == tc[1] && p[2] == tc[2])
  2754              p[3] = 0;
  2755           p += 4;
  2756        }
  2757     }
  2758     return 1;
  2759  }
  2760  
  2761  static int expand_palette(png *a, uint8 *palette, int len, int pal_img_n)
  2762  {
  2763     uint32 i, pixel_count = a->s.img_x * a->s.img_y;
  2764     uint8 *p, *temp_out, *orig = a->out;
  2765  
  2766     p = (uint8 *) malloc(pixel_count * pal_img_n);
  2767     if (p == NULL) return e("outofmem", "Out of memory");
  2768  
  2769     // between here and free(out) below, exitting would leak
  2770     temp_out = p;
  2771  
  2772     if (pal_img_n == 3) {
  2773        for (i=0; i < pixel_count; ++i) {
  2774           int n = orig[i]*4;
  2775           p[0] = palette[n  ];
  2776           p[1] = palette[n+1];
  2777           p[2] = palette[n+2];
  2778           p += 3;
  2779        }
  2780     } else {
  2781        for (i=0; i < pixel_count; ++i) {
  2782           int n = orig[i]*4;
  2783           p[0] = palette[n  ];
  2784           p[1] = palette[n+1];
  2785           p[2] = palette[n+2];
  2786           p[3] = palette[n+3];
  2787           p += 4;
  2788        }
  2789     }
  2790     free(a->out);
  2791     a->out = temp_out;
  2792  
  2793     STBI_NOTUSED(len);
  2794  
  2795     return 1;
  2796  }
  2797  
  2798  static int stbi_unpremultiply_on_load = 0;
  2799  static int stbi_de_iphone_flag = 0;
  2800  
  2801  void stbi_set_unpremultiply_on_load(int flag_true_if_should_unpremultiply)
  2802  {
  2803     stbi_unpremultiply_on_load = flag_true_if_should_unpremultiply;
  2804  }
  2805  void stbi_convert_iphone_png_to_rgb(int flag_true_if_should_convert)
  2806  {
  2807     stbi_de_iphone_flag = flag_true_if_should_convert;
  2808  }
  2809  
  2810  static void stbi_de_iphone(png *z)
  2811  {
  2812     stbi *s = &z->s;
  2813     uint32 i, pixel_count = s->img_x * s->img_y;
  2814     uint8 *p = z->out;
  2815  
  2816     if (s->img_out_n == 3) {  // convert bgr to rgb
  2817        for (i=0; i < pixel_count; ++i) {
  2818           uint8 t = p[0];
  2819           p[0] = p[2];
  2820           p[2] = t;
  2821           p += 3;
  2822        }
  2823     } else {
  2824        assert(s->img_out_n == 4);
  2825        if (stbi_unpremultiply_on_load) {
  2826           // convert bgr to rgb and unpremultiply
  2827           for (i=0; i < pixel_count; ++i) {
  2828              uint8 a = p[3];
  2829              uint8 t = p[0];
  2830              if (a) {
  2831                 p[0] = p[2] * 255 / a;
  2832                 p[1] = p[1] * 255 / a;
  2833                 p[2] =  t   * 255 / a;
  2834              } else {
  2835                 p[0] = p[2];
  2836                 p[2] = t;
  2837              }
  2838              p += 4;
  2839           }
  2840        } else {
  2841           // convert bgr to rgb
  2842           for (i=0; i < pixel_count; ++i) {
  2843              uint8 t = p[0];
  2844              p[0] = p[2];
  2845              p[2] = t;
  2846              p += 4;
  2847           }
  2848        }
  2849     }
  2850  }
  2851  
  2852  static int parse_png_file(png *z, int scan, int req_comp)
  2853  {
  2854     uint8 palette[1024], pal_img_n=0;
  2855     uint8 has_trans=0, tc[3];
  2856     uint32 ioff=0, idata_limit=0, i, pal_len=0;
  2857     int first=1,k,interlace=0, iphone=0;
  2858     stbi *s = &z->s;
  2859  
  2860     if (!check_png_header(s)) return 0;
  2861  
  2862     if (scan == SCAN_type) return 1;
  2863  
  2864     for (;;) {
  2865        chunk c = get_chunk_header(s);
  2866        switch (c.type) {
  2867           case PNG_TYPE('C','g','B','I'):
  2868              iphone = stbi_de_iphone_flag;
  2869              skip(s, c.length);
  2870              break;
  2871           case PNG_TYPE('I','H','D','R'): {
  2872              int depth,color,comp,filter;
  2873              if (!first) return e("multiple IHDR","Corrupt PNG");
  2874              first = 0;
  2875              if (c.length != 13) return e("bad IHDR len","Corrupt PNG");
  2876              s->img_x = get32(s); if (s->img_x > (1 << 24)) return e("too large","Very large image (corrupt?)");
  2877              s->img_y = get32(s); if (s->img_y > (1 << 24)) return e("too large","Very large image (corrupt?)");
  2878              depth = get8(s);  if (depth != 8)        return e("8bit only","PNG not supported: 8-bit only");
  2879              color = get8(s);  if (color > 6)         return e("bad ctype","Corrupt PNG");
  2880              if (color == 3) pal_img_n = 3; else if (color & 1) return e("bad ctype","Corrupt PNG");
  2881              comp  = get8(s);  if (comp) return e("bad comp method","Corrupt PNG");
  2882              filter= get8(s);  if (filter) return e("bad filter method","Corrupt PNG");
  2883              interlace = get8(s); if (interlace>1) return e("bad interlace method","Corrupt PNG");
  2884              if (!s->img_x || !s->img_y) return e("0-pixel image","Corrupt PNG");
  2885              if (!pal_img_n) {
  2886                 s->img_n = (color & 2 ? 3 : 1) + (color & 4 ? 1 : 0);
  2887                 if ((1 << 30) / s->img_x / s->img_n < s->img_y) return e("too large", "Image too large to decode");
  2888                 if (scan == SCAN_header) return 1;
  2889              } else {
  2890                 // if paletted, then pal_n is our final components, and
  2891                 // img_n is # components to decompress/filter.
  2892                 s->img_n = 1;
  2893                 if ((1 << 30) / s->img_x / 4 < s->img_y) return e("too large","Corrupt PNG");
  2894                 // if SCAN_header, have to scan to see if we have a tRNS
  2895              }
  2896              break;
  2897           }
  2898  
  2899           case PNG_TYPE('P','L','T','E'):  {
  2900              if (first) return e("first not IHDR", "Corrupt PNG");
  2901              if (c.length > 256*3) return e("invalid PLTE","Corrupt PNG");
  2902              pal_len = c.length / 3;
  2903              if (pal_len * 3 != c.length) return e("invalid PLTE","Corrupt PNG");
  2904              for (i=0; i < pal_len; ++i) {
  2905                 palette[i*4+0] = get8u(s);
  2906                 palette[i*4+1] = get8u(s);
  2907                 palette[i*4+2] = get8u(s);
  2908                 palette[i*4+3] = 255;
  2909              }
  2910              break;
  2911           }
  2912  
  2913           case PNG_TYPE('t','R','N','S'): {
  2914              if (first) return e("first not IHDR", "Corrupt PNG");
  2915              if (z->idata) return e("tRNS after IDAT","Corrupt PNG");
  2916              if (pal_img_n) {
  2917                 if (scan == SCAN_header) { s->img_n = 4; return 1; }
  2918                 if (pal_len == 0) return e("tRNS before PLTE","Corrupt PNG");
  2919                 if (c.length > pal_len) return e("bad tRNS len","Corrupt PNG");
  2920                 pal_img_n = 4;
  2921                 for (i=0; i < c.length; ++i)
  2922                    palette[i*4+3] = get8u(s);
  2923              } else {
  2924                 if (!(s->img_n & 1)) return e("tRNS with alpha","Corrupt PNG");
  2925                 if (c.length != (uint32) s->img_n*2) return e("bad tRNS len","Corrupt PNG");
  2926                 has_trans = 1;
  2927                 for (k=0; k < s->img_n; ++k)
  2928                    tc[k] = (uint8) get16(s); // non 8-bit images will be larger
  2929              }
  2930              break;
  2931           }
  2932  
  2933           case PNG_TYPE('I','D','A','T'): {
  2934              if (first) return e("first not IHDR", "Corrupt PNG");
  2935              if (pal_img_n && !pal_len) return e("no PLTE","Corrupt PNG");
  2936              if (scan == SCAN_header) { s->img_n = pal_img_n; return 1; }
  2937              if (ioff + c.length > idata_limit) {
  2938                 uint8 *p;
  2939                 if (idata_limit == 0) idata_limit = c.length > 4096 ? c.length : 4096;
  2940                 while (ioff + c.length > idata_limit)
  2941                    idata_limit *= 2;
  2942                 p = (uint8 *) realloc(z->idata, idata_limit); if (p == NULL) return e("outofmem", "Out of memory");
  2943                 z->idata = p;
  2944              }
  2945              if (!getn(s, z->idata+ioff,c.length)) return e("outofdata","Corrupt PNG");
  2946              ioff += c.length;
  2947              break;
  2948           }
  2949  
  2950           case PNG_TYPE('I','E','N','D'): {
  2951              uint32 raw_len;
  2952              if (first) return e("first not IHDR", "Corrupt PNG");
  2953              if (scan != SCAN_load) return 1;
  2954              if (z->idata == NULL) return e("no IDAT","Corrupt PNG");
  2955              z->expanded = (uint8 *) stbi_zlib_decode_malloc_guesssize_headerflag((char *) z->idata, ioff, 16384, (int *) &raw_len, !iphone);
  2956              if (z->expanded == NULL) return 0; // zlib should set error
  2957              free(z->idata); z->idata = NULL;
  2958              if ((req_comp == s->img_n+1 && req_comp != 3 && !pal_img_n) || has_trans)
  2959                 s->img_out_n = s->img_n+1;
  2960              else
  2961                 s->img_out_n = s->img_n;
  2962              if (!create_png_image(z, z->expanded, raw_len, s->img_out_n, interlace)) return 0;
  2963              if (has_trans)
  2964                 if (!compute_transparency(z, tc, s->img_out_n)) return 0;
  2965              if (iphone && s->img_out_n > 2)
  2966                 stbi_de_iphone(z);
  2967              if (pal_img_n) {
  2968                 // pal_img_n == 3 or 4
  2969                 s->img_n = pal_img_n; // record the actual colors we had
  2970                 s->img_out_n = pal_img_n;
  2971                 if (req_comp >= 3) s->img_out_n = req_comp;
  2972                 if (!expand_palette(z, palette, pal_len, s->img_out_n))
  2973                    return 0;
  2974              }
  2975              free(z->expanded); z->expanded = NULL;
  2976              return 1;
  2977           }
  2978  
  2979           default:
  2980              // if critical, fail
  2981              if (first) return e("first not IHDR", "Corrupt PNG");
  2982              if ((c.type & (1 << 29)) == 0) {
  2983                 #ifndef STBI_NO_FAILURE_STRINGS
  2984                 // not threadsafe
  2985                 static char invalid_chunk[] = "XXXX chunk not known";
  2986                 invalid_chunk[0] = (uint8) (c.type >> 24);
  2987                 invalid_chunk[1] = (uint8) (c.type >> 16);
  2988                 invalid_chunk[2] = (uint8) (c.type >>  8);
  2989                 invalid_chunk[3] = (uint8) (c.type >>  0);
  2990                 #endif
  2991                 return e(invalid_chunk, "PNG not supported: unknown chunk type");
  2992              }
  2993              skip(s, c.length);
  2994              break;
  2995        }
  2996        // end of chunk, read and skip CRC
  2997        get32(s);
  2998     }
  2999  }
  3000  
  3001  static unsigned char *do_png(png *p, int *x, int *y, int *n, int req_comp)
  3002  {
  3003     unsigned char *result=NULL;
  3004     p->expanded = NULL;
  3005     p->idata = NULL;
  3006     p->out = NULL;
  3007     if (req_comp < 0 || req_comp > 4) return epuc("bad req_comp", "Internal error");
  3008     if (parse_png_file(p, SCAN_load, req_comp)) {
  3009        result = p->out;
  3010        p->out = NULL;
  3011        if (req_comp && req_comp != p->s.img_out_n) {
  3012           result = convert_format(result, p->s.img_out_n, req_comp, p->s.img_x, p->s.img_y);
  3013           p->s.img_out_n = req_comp;
  3014           if (result == NULL) return result;
  3015        }
  3016        *x = p->s.img_x;
  3017        *y = p->s.img_y;
  3018        if (n) *n = p->s.img_n;
  3019     }
  3020     free(p->out);      p->out      = NULL;
  3021     free(p->expanded); p->expanded = NULL;
  3022     free(p->idata);    p->idata    = NULL;
  3023  
  3024     return result;
  3025  }
  3026  
  3027  #ifndef STBI_NO_STDIO
  3028  unsigned char *stbi_png_load_from_file(FILE *f, int *x, int *y, int *comp, int req_comp)
  3029  {
  3030     png p;
  3031     start_file(&p.s, f);
  3032     return do_png(&p, x,y,comp,req_comp);
  3033  }
  3034  
  3035  unsigned char *stbi_png_load(char const *filename, int *x, int *y, int *comp, int req_comp)
  3036  {
  3037     unsigned char *data;
  3038     FILE *f = fopen(filename, "rb");
  3039     if (!f) return NULL;
  3040     data = stbi_png_load_from_file(f,x,y,comp,req_comp);
  3041     fclose(f);
  3042     return data;
  3043  }
  3044  #endif
  3045  
  3046  unsigned char *stbi_png_load_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp)
  3047  {
  3048     png p;
  3049     start_mem(&p.s, buffer,len);
  3050     return do_png(&p, x,y,comp,req_comp);
  3051  }
  3052  
  3053  #ifndef STBI_NO_STDIO
  3054  int stbi_png_test_file(FILE *f)
  3055  {
  3056     png p;
  3057     int n,r;
  3058     n = ftell(f);
  3059     start_file(&p.s, f);
  3060     r = parse_png_file(&p, SCAN_type,STBI_default);
  3061     fseek(f,n,SEEK_SET);
  3062     return r;
  3063  }
  3064  #endif
  3065  
  3066  int stbi_png_test_memory(stbi_uc const *buffer, int len)
  3067  {
  3068     png p;
  3069     start_mem(&p.s, buffer, len);
  3070     return parse_png_file(&p, SCAN_type,STBI_default);
  3071  }
  3072  
  3073  static int stbi_png_info_raw(png *p, int *x, int *y, int *comp)
  3074  {
  3075     if (!parse_png_file(p, SCAN_header, 0))
  3076        return 0;
  3077     if (x) *x = p->s.img_x;
  3078     if (y) *y = p->s.img_y;
  3079     if (comp) *comp = p->s.img_n;
  3080     return 1;
  3081  }
  3082  
  3083  #ifndef STBI_NO_STDIO
  3084  int      stbi_png_info             (char const *filename,           int *x, int *y, int *comp)
  3085  {
  3086     int res;
  3087     FILE *f = fopen(filename, "rb");
  3088     if (!f) return 0;
  3089     res = stbi_png_info_from_file(f, x, y, comp);
  3090     fclose(f);
  3091     return res;
  3092  }
  3093  
  3094  int stbi_png_info_from_file(FILE *f, int *x, int *y, int *comp)
  3095  {
  3096     png p;
  3097     int res;
  3098     long n = ftell(f);
  3099     start_file(&p.s, f);
  3100     res = stbi_png_info_raw(&p, x, y, comp);
  3101     fseek(f, n, SEEK_SET);
  3102     return res;
  3103  }
  3104  #endif // !STBI_NO_STDIO
  3105  
  3106  int stbi_png_info_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp)
  3107  {
  3108     png p;
  3109     start_mem(&p.s, buffer, len);
  3110     return stbi_png_info_raw(&p, x, y, comp);
  3111  }
  3112  
  3113  // Microsoft/Windows BMP image
  3114  
  3115  static int bmp_test(stbi *s)
  3116  {
  3117     int sz;
  3118     if (get8(s) != 'B') return 0;
  3119     if (get8(s) != 'M') return 0;
  3120     get32le(s); // discard filesize
  3121     get16le(s); // discard reserved
  3122     get16le(s); // discard reserved
  3123     get32le(s); // discard data offset
  3124     sz = get32le(s);
  3125     if (sz == 12 || sz == 40 || sz == 56 || sz == 108) return 1;
  3126     return 0;
  3127  }
  3128  
  3129  #ifndef STBI_NO_STDIO
  3130  int      stbi_bmp_test_file        (FILE *f)
  3131  {
  3132     stbi s;
  3133     int r,n = ftell(f);
  3134     start_file(&s,f);
  3135     r = bmp_test(&s);
  3136     fseek(f,n,SEEK_SET);
  3137     return r;
  3138  }
  3139  #endif
  3140  
  3141  int      stbi_bmp_test_memory      (stbi_uc const *buffer, int len)
  3142  {
  3143     stbi s;
  3144     start_mem(&s, buffer, len);
  3145     return bmp_test(&s);
  3146  }
  3147  
  3148  // returns 0..31 for the highest set bit
  3149  static int high_bit(unsigned int z)
  3150  {
  3151     int n=0;
  3152     if (z == 0) return -1;
  3153     if (z >= 0x10000) n += 16, z >>= 16;
  3154     if (z >= 0x00100) n +=  8, z >>=  8;
  3155     if (z >= 0x00010) n +=  4, z >>=  4;
  3156     if (z >= 0x00004) n +=  2, z >>=  2;
  3157     if (z >= 0x00002) n +=  1, z >>=  1;
  3158     return n;
  3159  }
  3160  
  3161  static int bitcount(unsigned int a)
  3162  {
  3163     a = (a & 0x55555555) + ((a >>  1) & 0x55555555); // max 2
  3164     a = (a & 0x33333333) + ((a >>  2) & 0x33333333); // max 4
  3165     a = (a + (a >> 4)) & 0x0f0f0f0f; // max 8 per 4, now 8 bits
  3166     a = (a + (a >> 8)); // max 16 per 8 bits
  3167     a = (a + (a >> 16)); // max 32 per 8 bits
  3168     return a & 0xff;
  3169  }
  3170  
  3171  static int shiftsigned(int v, int shift, int bits)
  3172  {
  3173     int result;
  3174     int z=0;
  3175  
  3176     if (shift < 0) v <<= -shift;
  3177     else v >>= shift;
  3178     result = v;
  3179  
  3180     z = bits;
  3181     while (z < 8) {
  3182        result += v >> z;
  3183        z += bits;
  3184     }
  3185     return result;
  3186  }
  3187  
  3188  static stbi_uc *bmp_load(stbi *s, int *x, int *y, int *comp, int req_comp)
  3189  {
  3190     uint8 *out;
  3191     unsigned int mr=0,mg=0,mb=0,ma=0, fake_a=0;
  3192     stbi_uc pal[256][4];
  3193     int psize=0,i,j,compress=0,width;
  3194     int bpp, flip_vertically, pad, target, offset, hsz;
  3195     if (get8(s) != 'B' || get8(s) != 'M') return epuc("not BMP", "Corrupt BMP");
  3196     get32le(s); // discard filesize
  3197     get16le(s); // discard reserved
  3198     get16le(s); // discard reserved
  3199     offset = get32le(s);
  3200     hsz = get32le(s);
  3201     if (hsz != 12 && hsz != 40 && hsz != 56 && hsz != 108) return epuc("unknown BMP", "BMP type not supported: unknown");
  3202     if (hsz == 12) {
  3203        s->img_x = get16le(s);
  3204        s->img_y = get16le(s);
  3205     } else {
  3206        s->img_x = get32le(s);
  3207        s->img_y = get32le(s);
  3208     }
  3209     if (get16le(s) != 1) return epuc("bad BMP", "bad BMP");
  3210     bpp = get16le(s);
  3211     if (bpp == 1) return epuc("monochrome", "BMP type not supported: 1-bit");
  3212     flip_vertically = ((int) s->img_y) > 0;
  3213     s->img_y = abs((int) s->img_y);
  3214     if (hsz == 12) {
  3215        if (bpp < 24)
  3216           psize = (offset - 14 - 24) / 3;
  3217     } else {
  3218        compress = get32le(s);
  3219        if (compress == 1 || compress == 2) return epuc("BMP RLE", "BMP type not supported: RLE");
  3220        get32le(s); // discard sizeof
  3221        get32le(s); // discard hres
  3222        get32le(s); // discard vres
  3223        get32le(s); // discard colorsused
  3224        get32le(s); // discard max important
  3225        if (hsz == 40 || hsz == 56) {
  3226           if (hsz == 56) {
  3227              get32le(s);
  3228              get32le(s);
  3229              get32le(s);
  3230              get32le(s);
  3231           }
  3232           if (bpp == 16 || bpp == 32) {
  3233              mr = mg = mb = 0;
  3234              if (compress == 0) {
  3235                 if (bpp == 32) {
  3236                    mr = 0xffu << 16;
  3237                    mg = 0xffu <<  8;
  3238                    mb = 0xffu <<  0;
  3239                    ma = 0xffu << 24;
  3240                    fake_a = 1; // @TODO: check for cases like alpha value is all 0 and switch it to 255
  3241                 } else {
  3242                    mr = 31u << 10;
  3243                    mg = 31u <<  5;
  3244                    mb = 31u <<  0;
  3245                 }
  3246              } else if (compress == 3) {
  3247                 mr = get32le(s);
  3248                 mg = get32le(s);
  3249                 mb = get32le(s);
  3250                 // not documented, but generated by photoshop and handled by mspaint
  3251                 if (mr == mg && mg == mb) {
  3252                    // ?!?!?
  3253                    return epuc("bad BMP", "bad BMP");
  3254                 }
  3255              } else
  3256                 return epuc("bad BMP", "bad BMP");
  3257           }
  3258        } else {
  3259           assert(hsz == 108);
  3260           mr = get32le(s);
  3261           mg = get32le(s);
  3262           mb = get32le(s);
  3263           ma = get32le(s);
  3264           get32le(s); // discard color space
  3265           for (i=0; i < 12; ++i)
  3266              get32le(s); // discard color space parameters
  3267        }
  3268        if (bpp < 16)
  3269           psize = (offset - 14 - hsz) >> 2;
  3270     }
  3271     s->img_n = ma ? 4 : 3;
  3272     if (req_comp && req_comp >= 3) // we can directly decode 3 or 4
  3273        target = req_comp;
  3274     else
  3275        target = s->img_n; // if they want monochrome, we'll post-convert
  3276     out = (stbi_uc *) malloc(target * s->img_x * s->img_y);
  3277     if (!out) return epuc("outofmem", "Out of memory");
  3278     if (bpp < 16) {
  3279        int z=0;
  3280        if (psize == 0 || psize > 256) { free(out); return epuc("invalid", "Corrupt BMP"); }
  3281        for (i=0; i < psize; ++i) {
  3282           pal[i][2] = get8u(s);
  3283           pal[i][1] = get8u(s);
  3284           pal[i][0] = get8u(s);
  3285           if (hsz != 12) get8(s);
  3286           pal[i][3] = 255;
  3287        }
  3288        skip(s, offset - 14 - hsz - psize * (hsz == 12 ? 3 : 4));
  3289        if (bpp == 4) width = (s->img_x + 1) >> 1;
  3290        else if (bpp == 8) width = s->img_x;
  3291        else { free(out); return epuc("bad bpp", "Corrupt BMP"); }
  3292        pad = (-width)&3;
  3293        for (j=0; j < (int) s->img_y; ++j) {
  3294           for (i=0; i < (int) s->img_x; i += 2) {
  3295              int v=get8(s),v2=0;
  3296              if (bpp == 4) {
  3297                 v2 = v & 15;
  3298                 v >>= 4;
  3299              }
  3300              out[z++] = pal[v][0];
  3301              out[z++] = pal[v][1];
  3302              out[z++] = pal[v][2];
  3303              if (target == 4) out[z++] = 255;
  3304              if (i+1 == (int) s->img_x) break;
  3305              v = (bpp == 8) ? get8(s) : v2;
  3306              out[z++] = pal[v][0];
  3307              out[z++] = pal[v][1];
  3308              out[z++] = pal[v][2];
  3309              if (target == 4) out[z++] = 255;
  3310           }
  3311           skip(s, pad);
  3312        }
  3313     } else {
  3314        int rshift=0,gshift=0,bshift=0,ashift=0,rcount=0,gcount=0,bcount=0,acount=0;
  3315        int z = 0;
  3316        int easy=0;
  3317        skip(s, offset - 14 - hsz);
  3318        if (bpp == 24) width = 3 * s->img_x;
  3319        else if (bpp == 16) width = 2*s->img_x;
  3320        else /* bpp = 32 and pad = 0 */ width=0;
  3321        pad = (-width) & 3;
  3322        if (bpp == 24) {
  3323           easy = 1;
  3324        } else if (bpp == 32) {
  3325           if (mb == 0xff && mg == 0xff00 && mr == 0xff000000 && ma == 0xff000000)
  3326              easy = 2;
  3327        }
  3328        if (!easy) {
  3329           if (!mr || !mg || !mb) return epuc("bad masks", "Corrupt BMP");
  3330           // right shift amt to put high bit in position #7
  3331           rshift = high_bit(mr)-7; rcount = bitcount(mr);
  3332           gshift = high_bit(mg)-7; gcount = bitcount(mr);
  3333           bshift = high_bit(mb)-7; bcount = bitcount(mr);
  3334           ashift = high_bit(ma)-7; acount = bitcount(mr);
  3335        }
  3336        for (j=0; j < (int) s->img_y; ++j) {
  3337           if (easy) {
  3338              for (i=0; i < (int) s->img_x; ++i) {
  3339                 int a;
  3340                 out[z+2] = get8u(s);
  3341                 out[z+1] = get8u(s);
  3342                 out[z+0] = get8u(s);
  3343                 z += 3;
  3344                 a = (easy == 2 ? get8(s) : 255);
  3345                 if (target == 4) out[z++] = (uint8) a;
  3346              }
  3347           } else {
  3348              for (i=0; i < (int) s->img_x; ++i) {
  3349                 uint32 v = (bpp == 16 ? get16le(s) : get32le(s));
  3350                 int a;
  3351                 out[z++] = (uint8) shiftsigned(v & mr, rshift, rcount);
  3352                 out[z++] = (uint8) shiftsigned(v & mg, gshift, gcount);
  3353                 out[z++] = (uint8) shiftsigned(v & mb, bshift, bcount);
  3354                 a = (ma ? shiftsigned(v & ma, ashift, acount) : 255);
  3355                 if (target == 4) out[z++] = (uint8) a;
  3356              }
  3357           }
  3358           skip(s, pad);
  3359        }
  3360     }
  3361     if (flip_vertically) {
  3362        stbi_uc t;
  3363        for (j=0; j < (int) s->img_y>>1; ++j) {
  3364           stbi_uc *p1 = out +      j     *s->img_x*target;
  3365           stbi_uc *p2 = out + (s->img_y-1-j)*s->img_x*target;
  3366           for (i=0; i < (int) s->img_x*target; ++i) {
  3367              t = p1[i], p1[i] = p2[i], p2[i] = t;
  3368           }
  3369        }
  3370     }
  3371  
  3372     if (req_comp && req_comp != target) {
  3373        out = convert_format(out, target, req_comp, s->img_x, s->img_y);
  3374        if (out == NULL) return out; // convert_format frees input on failure
  3375     }
  3376  
  3377     *x = s->img_x;
  3378     *y = s->img_y;
  3379     if (comp) *comp = target;
  3380     return out;
  3381  }
  3382  
  3383  #ifndef STBI_NO_STDIO
  3384  stbi_uc *stbi_bmp_load             (char const *filename,           int *x, int *y, int *comp, int req_comp)
  3385  {
  3386     stbi_uc *data;
  3387     FILE *f = fopen(filename, "rb");
  3388     if (!f) return NULL;
  3389     data = stbi_bmp_load_from_file(f, x,y,comp,req_comp);
  3390     fclose(f);
  3391     return data;
  3392  }
  3393  
  3394  stbi_uc *stbi_bmp_load_from_file   (FILE *f,                  int *x, int *y, int *comp, int req_comp)
  3395  {
  3396     stbi s;
  3397     start_file(&s, f);
  3398     return bmp_load(&s, x,y,comp,req_comp);
  3399  }
  3400  #endif
  3401  
  3402  stbi_uc *stbi_bmp_load_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp)
  3403  {
  3404     stbi s;
  3405     start_mem(&s, buffer, len);
  3406     return bmp_load(&s, x,y,comp,req_comp);
  3407  }
  3408  
  3409  // Targa Truevision - TGA
  3410  // by Jonathan Dummer
  3411  
  3412  static int tga_info(stbi *s, int *x, int *y, int *comp)
  3413  {
  3414      int tga_w, tga_h, tga_comp;
  3415      int sz;
  3416      get8u(s);                   // discard Offset
  3417      sz = get8u(s);              // color type
  3418      if( sz > 1 ) return 0;      // only RGB or indexed allowed
  3419      sz = get8u(s);              // image type
  3420      // only RGB or grey allowed, +/- RLE
  3421      if ((sz != 1) && (sz != 2) && (sz != 3) && (sz != 9) && (sz != 10) && (sz != 11)) return 0;
  3422      get16le(s);                 // discard palette start
  3423      get16le(s);                 // discard palette length
  3424      get8(s);                    // discard bits per palette color entry
  3425      get16le(s);                 // discard x origin
  3426      get16le(s);                 // discard y origin
  3427      tga_w = get16le(s);
  3428      if( tga_w < 1 ) return 0;   // test width
  3429      tga_h = get16le(s);
  3430      if( tga_h < 1 ) return 0;   // test height
  3431      sz = get8(s);               // bits per pixel
  3432      // only RGB or RGBA or grey allowed
  3433      if ((sz != 8) && (sz != 16) && (sz != 24) && (sz != 32)) return 0;
  3434      tga_comp = sz;
  3435      if (x) *x = tga_w;
  3436      if (y) *y = tga_h;
  3437      if (comp) *comp = tga_comp / 8;
  3438      return 1;                   // seems to have passed everything
  3439  }
  3440  
  3441  #ifndef STBI_NO_STDIO
  3442  int stbi_tga_info_from_file(FILE *f, int *x, int *y, int *comp)
  3443  {
  3444      stbi s;
  3445      int r;
  3446      long n = ftell(f);
  3447      start_file(&s, f);
  3448      r = tga_info(&s, x, y, comp);
  3449      fseek(f, n, SEEK_SET);
  3450      return r;
  3451  }
  3452  #endif
  3453  
  3454  int stbi_tga_info_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp)
  3455  {
  3456      stbi s;
  3457      start_mem(&s, buffer, len);
  3458      return tga_info(&s, x, y, comp);
  3459  }
  3460  
  3461  static int tga_test(stbi *s)
  3462  {
  3463     int sz;
  3464     get8u(s);      //   discard Offset
  3465     sz = get8u(s);   //   color type
  3466     if ( sz > 1 ) return 0;   //   only RGB or indexed allowed
  3467     sz = get8u(s);   //   image type
  3468     if ( (sz != 1) && (sz != 2) && (sz != 3) && (sz != 9) && (sz != 10) && (sz != 11) ) return 0;   //   only RGB or grey allowed, +/- RLE
  3469     get16(s);      //   discard palette start
  3470     get16(s);      //   discard palette length
  3471     get8(s);         //   discard bits per palette color entry
  3472     get16(s);      //   discard x origin
  3473     get16(s);      //   discard y origin
  3474     if ( get16(s) < 1 ) return 0;      //   test width
  3475     if ( get16(s) < 1 ) return 0;      //   test height
  3476     sz = get8(s);   //   bits per pixel
  3477     if ( (sz != 8) && (sz != 16) && (sz != 24) && (sz != 32) ) return 0;   //   only RGB or RGBA or grey allowed
  3478     return 1;      //   seems to have passed everything
  3479  }
  3480  
  3481  #ifndef STBI_NO_STDIO
  3482  int      stbi_tga_test_file        (FILE *f)
  3483  {
  3484     stbi s;
  3485     int r,n = ftell(f);
  3486     start_file(&s, f);
  3487     r = tga_test(&s);
  3488     fseek(f,n,SEEK_SET);
  3489     return r;
  3490  }
  3491  #endif
  3492  
  3493  int      stbi_tga_test_memory      (stbi_uc const *buffer, int len)
  3494  {
  3495     stbi s;
  3496     start_mem(&s, buffer, len);
  3497     return tga_test(&s);
  3498  }
  3499  
  3500  static stbi_uc *tga_load(stbi *s, int *x, int *y, int *comp, int req_comp)
  3501  {
  3502     //   read in the TGA header stuff
  3503     int tga_offset = get8u(s);
  3504     int tga_indexed = get8u(s);
  3505     int tga_image_type = get8u(s);
  3506     int tga_is_RLE = 0;
  3507     int tga_palette_start = get16le(s);
  3508     int tga_palette_len = get16le(s);
  3509     int tga_palette_bits = get8u(s);
  3510     int tga_x_origin = get16le(s);
  3511     int tga_y_origin = get16le(s);
  3512     int tga_width = get16le(s);
  3513     int tga_height = get16le(s);
  3514     int tga_bits_per_pixel = get8u(s);
  3515     int tga_inverted = get8u(s);
  3516     //   image data
  3517     unsigned char *tga_data;
  3518     unsigned char *tga_palette = NULL;
  3519     int i, j;
  3520     unsigned char raw_data[4];
  3521     unsigned char trans_data[4];
  3522     int RLE_count = 0;
  3523     int RLE_repeating = 0;
  3524     int read_next_pixel = 1;
  3525  
  3526     //   do a tiny bit of precessing
  3527     if ( tga_image_type >= 8 )
  3528     {
  3529        tga_image_type -= 8;
  3530        tga_is_RLE = 1;
  3531     }
  3532     /* int tga_alpha_bits = tga_inverted & 15; */
  3533     tga_inverted = 1 - ((tga_inverted >> 5) & 1);
  3534  
  3535     //   error check
  3536     if ( //(tga_indexed) ||
  3537        (tga_width < 1) || (tga_height < 1) ||
  3538        (tga_image_type < 1) || (tga_image_type > 3) ||
  3539        ((tga_bits_per_pixel != 8) && (tga_bits_per_pixel != 16) &&
  3540        (tga_bits_per_pixel != 24) && (tga_bits_per_pixel != 32))
  3541        )
  3542     {
  3543        return NULL;
  3544     }
  3545  
  3546     //   If I'm paletted, then I'll use the number of bits from the palette
  3547     if ( tga_indexed )
  3548     {
  3549        tga_bits_per_pixel = tga_palette_bits;
  3550     }
  3551  
  3552     //   tga info
  3553     *x = tga_width;
  3554     *y = tga_height;
  3555     if ( (req_comp < 1) || (req_comp > 4) )
  3556     {
  3557        //   just use whatever the file was
  3558        req_comp = tga_bits_per_pixel / 8;
  3559        *comp = req_comp;
  3560     } else
  3561     {
  3562        //   force a new number of components
  3563        *comp = tga_bits_per_pixel/8;
  3564     }
  3565     tga_data = (unsigned char*)malloc( tga_width * tga_height * req_comp );
  3566  
  3567     //   skip to the data's starting position (offset usually = 0)
  3568     skip(s, tga_offset );
  3569     //   do I need to load a palette?
  3570     if ( tga_indexed )
  3571     {
  3572        //   any data to skip? (offset usually = 0)
  3573        skip(s, tga_palette_start );
  3574        //   load the palette
  3575        tga_palette = (unsigned char*)malloc( tga_palette_len * tga_palette_bits / 8 );
  3576        if (!getn(s, tga_palette, tga_palette_len * tga_palette_bits / 8 ))
  3577           return NULL;
  3578     }
  3579     //   load the data
  3580     trans_data[0] = trans_data[1] = trans_data[2] = trans_data[3] = 0;
  3581     for (i=0; i < tga_width * tga_height; ++i)
  3582     {
  3583        //   if I'm in RLE mode, do I need to get a RLE chunk?
  3584        if ( tga_is_RLE )
  3585        {
  3586           if ( RLE_count == 0 )
  3587           {
  3588              //   yep, get the next byte as a RLE command
  3589              int RLE_cmd = get8u(s);
  3590              RLE_count = 1 + (RLE_cmd & 127);
  3591              RLE_repeating = RLE_cmd >> 7;
  3592              read_next_pixel = 1;
  3593           } else if ( !RLE_repeating )
  3594           {
  3595              read_next_pixel = 1;
  3596           }
  3597        } else
  3598        {
  3599           read_next_pixel = 1;
  3600        }
  3601        //   OK, if I need to read a pixel, do it now
  3602        if ( read_next_pixel )
  3603        {
  3604           //   load however much data we did have
  3605           if ( tga_indexed )
  3606           {
  3607              //   read in 1 byte, then perform the lookup
  3608              int pal_idx = get8u(s);
  3609              if ( pal_idx >= tga_palette_len )
  3610              {
  3611                 //   invalid index
  3612                 pal_idx = 0;
  3613              }
  3614              pal_idx *= tga_bits_per_pixel / 8;
  3615              for (j = 0; j*8 < tga_bits_per_pixel; ++j)
  3616              {
  3617                 raw_data[j] = tga_palette[pal_idx+j];
  3618              }
  3619           } else
  3620           {
  3621              //   read in the data raw
  3622              for (j = 0; j*8 < tga_bits_per_pixel; ++j)
  3623              {
  3624                 raw_data[j] = get8u(s);
  3625              }
  3626           }
  3627           //   convert raw to the intermediate format
  3628           switch (tga_bits_per_pixel)
  3629           {
  3630           case 8:
  3631              //   Luminous => RGBA
  3632              trans_data[0] = raw_data[0];
  3633              trans_data[1] = raw_data[0];
  3634              trans_data[2] = raw_data[0];
  3635              trans_data[3] = 255;
  3636              break;
  3637           case 16:
  3638              //   Luminous,Alpha => RGBA
  3639              trans_data[0] = raw_data[0];
  3640              trans_data[1] = raw_data[0];
  3641              trans_data[2] = raw_data[0];
  3642              trans_data[3] = raw_data[1];
  3643              break;
  3644           case 24:
  3645              //   BGR => RGBA
  3646              trans_data[0] = raw_data[2];
  3647              trans_data[1] = raw_data[1];
  3648              trans_data[2] = raw_data[0];
  3649              trans_data[3] = 255;
  3650              break;
  3651           case 32:
  3652              //   BGRA => RGBA
  3653              trans_data[0] = raw_data[2];
  3654              trans_data[1] = raw_data[1];
  3655              trans_data[2] = raw_data[0];
  3656              trans_data[3] = raw_data[3];
  3657              break;
  3658           }
  3659           //   clear the reading flag for the next pixel
  3660           read_next_pixel = 0;
  3661        } // end of reading a pixel
  3662        //   convert to final format
  3663        switch (req_comp)
  3664        {
  3665        case 1:
  3666           //   RGBA => Luminance
  3667           tga_data[i*req_comp+0] = compute_y(trans_data[0],trans_data[1],trans_data[2]);
  3668           break;
  3669        case 2:
  3670           //   RGBA => Luminance,Alpha
  3671           tga_data[i*req_comp+0] = compute_y(trans_data[0],trans_data[1],trans_data[2]);
  3672           tga_data[i*req_comp+1] = trans_data[3];
  3673           break;
  3674        case 3:
  3675           //   RGBA => RGB
  3676           tga_data[i*req_comp+0] = trans_data[0];
  3677           tga_data[i*req_comp+1] = trans_data[1];
  3678           tga_data[i*req_comp+2] = trans_data[2];
  3679           break;
  3680        case 4:
  3681           //   RGBA => RGBA
  3682           tga_data[i*req_comp+0] = trans_data[0];
  3683           tga_data[i*req_comp+1] = trans_data[1];
  3684           tga_data[i*req_comp+2] = trans_data[2];
  3685           tga_data[i*req_comp+3] = trans_data[3];
  3686           break;
  3687        }
  3688        //   in case we're in RLE mode, keep counting down
  3689        --RLE_count;
  3690     }
  3691     //   do I need to invert the image?
  3692     if ( tga_inverted )
  3693     {
  3694        for (j = 0; j*2 < tga_height; ++j)
  3695        {
  3696           int index1 = j * tga_width * req_comp;
  3697           int index2 = (tga_height - 1 - j) * tga_width * req_comp;
  3698           for (i = tga_width * req_comp; i > 0; --i)
  3699           {
  3700              unsigned char temp = tga_data[index1];
  3701              tga_data[index1] = tga_data[index2];
  3702              tga_data[index2] = temp;
  3703              ++index1;
  3704              ++index2;
  3705           }
  3706        }
  3707     }
  3708     //   clear my palette, if I had one
  3709     if ( tga_palette != NULL )
  3710     {
  3711        free( tga_palette );
  3712     }
  3713     //   the things I do to get rid of an error message, and yet keep
  3714     //   Microsoft's C compilers happy... [8^(
  3715     tga_palette_start = tga_palette_len = tga_palette_bits =
  3716           tga_x_origin = tga_y_origin = 0;
  3717     //   OK, done
  3718     return tga_data;
  3719  }
  3720  
  3721  #ifndef STBI_NO_STDIO
  3722  stbi_uc *stbi_tga_load             (char const *filename,           int *x, int *y, int *comp, int req_comp)
  3723  {
  3724     stbi_uc *data;
  3725     FILE *f = fopen(filename, "rb");
  3726     if (!f) return NULL;
  3727     data = stbi_tga_load_from_file(f, x,y,comp,req_comp);
  3728     fclose(f);
  3729     return data;
  3730  }
  3731  
  3732  stbi_uc *stbi_tga_load_from_file   (FILE *f,                  int *x, int *y, int *comp, int req_comp)
  3733  {
  3734     stbi s;
  3735     start_file(&s, f);
  3736     return tga_load(&s, x,y,comp,req_comp);
  3737  }
  3738  #endif
  3739  
  3740  stbi_uc *stbi_tga_load_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp)
  3741  {
  3742     stbi s;
  3743     start_mem(&s, buffer, len);
  3744     return tga_load(&s, x,y,comp,req_comp);
  3745  }
  3746  
  3747  
  3748  // *************************************************************************************************
  3749  // Photoshop PSD loader -- PD by Thatcher Ulrich, integration by Nicolas Schulz, tweaked by STB
  3750  
  3751  static int psd_test(stbi *s)
  3752  {
  3753     if (get32(s) != 0x38425053) return 0;   // "8BPS"
  3754     else return 1;
  3755  }
  3756  
  3757  #ifndef STBI_NO_STDIO
  3758  int stbi_psd_test_file(FILE *f)
  3759  {
  3760     stbi s;
  3761     int r,n = ftell(f);
  3762     start_file(&s, f);
  3763     r = psd_test(&s);
  3764     fseek(f,n,SEEK_SET);
  3765     return r;
  3766  }
  3767  #endif
  3768  
  3769  int stbi_psd_test_memory(stbi_uc const *buffer, int len)
  3770  {
  3771     stbi s;
  3772     start_mem(&s, buffer, len);
  3773     return psd_test(&s);
  3774  }
  3775  
  3776  static stbi_uc *psd_load(stbi *s, int *x, int *y, int *comp, int req_comp)
  3777  {
  3778     int   pixelCount;
  3779     int channelCount, compression;
  3780     int channel, i, count, len;
  3781     int w,h;
  3782     uint8 *out;
  3783  
  3784     // Check identifier
  3785     if (get32(s) != 0x38425053)   // "8BPS"
  3786        return epuc("not PSD", "Corrupt PSD image");
  3787  
  3788     // Check file type version.
  3789     if (get16(s) != 1)
  3790        return epuc("wrong version", "Unsupported version of PSD image");
  3791  
  3792     // Skip 6 reserved bytes.
  3793     skip(s, 6 );
  3794  
  3795     // Read the number of channels (R, G, B, A, etc).
  3796     channelCount = get16(s);
  3797     if (channelCount < 0 || channelCount > 16)
  3798        return epuc("wrong channel count", "Unsupported number of channels in PSD image");
  3799  
  3800     // Read the rows and columns of the image.
  3801     h = get32(s);
  3802     w = get32(s);
  3803  
  3804     // Make sure the depth is 8 bits.
  3805     if (get16(s) != 8)
  3806        return epuc("unsupported bit depth", "PSD bit depth is not 8 bit");
  3807  
  3808     // Make sure the color mode is RGB.
  3809     // Valid options are:
  3810     //   0: Bitmap
  3811     //   1: Grayscale
  3812     //   2: Indexed color
  3813     //   3: RGB color
  3814     //   4: CMYK color
  3815     //   7: Multichannel
  3816     //   8: Duotone
  3817     //   9: Lab color
  3818     if (get16(s) != 3)
  3819        return epuc("wrong color format", "PSD is not in RGB color format");
  3820  
  3821     // Skip the Mode Data.  (It's the palette for indexed color; other info for other modes.)
  3822     skip(s,get32(s) );
  3823  
  3824     // Skip the image resources.  (resolution, pen tool paths, etc)
  3825     skip(s, get32(s) );
  3826  
  3827     // Skip the reserved data.
  3828     skip(s, get32(s) );
  3829  
  3830     // Find out if the data is compressed.
  3831     // Known values:
  3832     //   0: no compression
  3833     //   1: RLE compressed
  3834     compression = get16(s);
  3835     if (compression > 1)
  3836        return epuc("bad compression", "PSD has an unknown compression format");
  3837  
  3838     // Create the destination image.
  3839     out = (stbi_uc *) malloc(4 * w*h);
  3840     if (!out) return epuc("outofmem", "Out of memory");
  3841     pixelCount = w*h;
  3842  
  3843     // Initialize the data to zero.
  3844     //memset( out, 0, pixelCount * 4 );
  3845  
  3846     // Finally, the image data.
  3847     if (compression) {
  3848        // RLE as used by .PSD and .TIFF
  3849        // Loop until you get the number of unpacked bytes you are expecting:
  3850        //     Read the next source byte into n.
  3851        //     If n is between 0 and 127 inclusive, copy the next n+1 bytes literally.
  3852        //     Else if n is between -127 and -1 inclusive, copy the next byte -n+1 times.
  3853        //     Else if n is 128, noop.
  3854        // Endloop
  3855  
  3856        // The RLE-compressed data is preceeded by a 2-byte data count for each row in the data,
  3857        // which we're going to just skip.
  3858        skip(s, h * channelCount * 2 );
  3859  
  3860        // Read the RLE data by channel.
  3861        for (channel = 0; channel < 4; channel++) {
  3862           uint8 *p;
  3863  
  3864           p = out+channel;
  3865           if (channel >= channelCount) {
  3866              // Fill this channel with default data.
  3867              for (i = 0; i < pixelCount; i++) *p = (channel == 3 ? 255 : 0), p += 4;
  3868           } else {
  3869              // Read the RLE data.
  3870              count = 0;
  3871              while (count < pixelCount) {
  3872                 len = get8(s);
  3873                 if (len == 128) {
  3874                    // No-op.
  3875                 } else if (len < 128) {
  3876                    // Copy next len+1 bytes literally.
  3877                    len++;
  3878                    count += len;
  3879                    while (len) {
  3880                       *p = get8u(s);
  3881                       p += 4;
  3882                       len--;
  3883                    }
  3884                 } else if (len > 128) {
  3885                    uint8   val;
  3886                    // Next -len+1 bytes in the dest are replicated from next source byte.
  3887                    // (Interpret len as a negative 8-bit int.)
  3888                    len ^= 0x0FF;
  3889                    len += 2;
  3890                    val = get8u(s);
  3891                    count += len;
  3892                    while (len) {
  3893                       *p = val;
  3894                       p += 4;
  3895                       len--;
  3896                    }
  3897                 }
  3898              }
  3899           }
  3900        }
  3901  
  3902     } else {
  3903        // We're at the raw image data.  It's each channel in order (Red, Green, Blue, Alpha, ...)
  3904        // where each channel consists of an 8-bit value for each pixel in the image.
  3905  
  3906        // Read the data by channel.
  3907        for (channel = 0; channel < 4; channel++) {
  3908           uint8 *p;
  3909  
  3910           p = out + channel;
  3911           if (channel > channelCount) {
  3912              // Fill this channel with default data.
  3913              for (i = 0; i < pixelCount; i++) *p = channel == 3 ? 255 : 0, p += 4;
  3914           } else {
  3915              // Read the data.
  3916              for (i = 0; i < pixelCount; i++)
  3917                 *p = get8u(s), p += 4;
  3918           }
  3919        }
  3920     }
  3921  
  3922     if (req_comp && req_comp != 4) {
  3923        out = convert_format(out, 4, req_comp, w, h);
  3924        if (out == NULL) return out; // convert_format frees input on failure
  3925     }
  3926  
  3927     if (comp) *comp = channelCount;
  3928     *y = h;
  3929     *x = w;
  3930  
  3931     return out;
  3932  }
  3933  
  3934  #ifndef STBI_NO_STDIO
  3935  stbi_uc *stbi_psd_load(char const *filename, int *x, int *y, int *comp, int req_comp)
  3936  {
  3937     stbi_uc *data;
  3938     FILE *f = fopen(filename, "rb");
  3939     if (!f) return NULL;
  3940     data = stbi_psd_load_from_file(f, x,y,comp,req_comp);
  3941     fclose(f);
  3942     return data;
  3943  }
  3944  
  3945  stbi_uc *stbi_psd_load_from_file(FILE *f, int *x, int *y, int *comp, int req_comp)
  3946  {
  3947     stbi s;
  3948     start_file(&s, f);
  3949     return psd_load(&s, x,y,comp,req_comp);
  3950  }
  3951  #endif
  3952  
  3953  stbi_uc *stbi_psd_load_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp)
  3954  {
  3955     stbi s;
  3956     start_mem(&s, buffer, len);
  3957     return psd_load(&s, x,y,comp,req_comp);
  3958  }
  3959  
  3960  // *************************************************************************************************
  3961  // Softimage PIC loader
  3962  // by Tom Seddon
  3963  //
  3964  // See http://softimage.wiki.softimage.com/index.php/INFO:_PIC_file_format
  3965  // See http://ozviz.wasp.uwa.edu.au/~pbourke/dataformats/softimagepic/
  3966  
  3967  static int pic_is4(stbi *s,const char *str)
  3968  {
  3969     int i;
  3970     for (i=0; i<4; ++i)
  3971        if (get8(s) != (stbi_uc)str[i])
  3972           return 0;
  3973  
  3974     return 1;
  3975  }
  3976  
  3977  static int pic_test(stbi *s)
  3978  {
  3979     int i;
  3980  
  3981     if (!pic_is4(s,"\x53\x80\xF6\x34"))
  3982        return 0;
  3983  
  3984     for(i=0;i<84;++i)
  3985        get8(s);
  3986  
  3987     if (!pic_is4(s,"PICT"))
  3988        return 0;
  3989  
  3990     return 1;
  3991  }
  3992  
  3993  typedef struct
  3994  {
  3995     stbi_uc size,type,channel;
  3996  } pic_packet_t;
  3997  
  3998  static stbi_uc *pic_readval(stbi *s, int channel, stbi_uc *dest)
  3999  {
  4000     int mask=0x80, i;
  4001  
  4002     for (i=0; i<4; ++i, mask>>=1) {
  4003        if (channel & mask) {
  4004           if (at_eof(s)) return epuc("bad file","PIC file too short");
  4005           dest[i]=get8u(s);
  4006        }
  4007     }
  4008  
  4009     return dest;
  4010  }
  4011  
  4012  static void pic_copyval(int channel,stbi_uc *dest,const stbi_uc *src)
  4013  {
  4014     int mask=0x80,i;
  4015  
  4016     for (i=0;i<4; ++i, mask>>=1)
  4017        if (channel&mask)
  4018           dest[i]=src[i];
  4019  }
  4020  
  4021  static stbi_uc *pic_load2(stbi *s,int width,int height,int *comp, stbi_uc *result)
  4022  {
  4023     int act_comp=0,num_packets=0,y,chained;
  4024     pic_packet_t packets[10];
  4025  
  4026     // this will (should...) cater for even some bizarre stuff like having data
  4027      // for the same channel in multiple packets.
  4028     do {
  4029        pic_packet_t *packet;
  4030  
  4031        if (num_packets==sizeof(packets)/sizeof(packets[0]))
  4032           return epuc("bad format","too many packets");
  4033  
  4034        packet = &packets[num_packets++];
  4035  
  4036        chained = get8(s);
  4037        packet->size    = get8u(s);
  4038        packet->type    = get8u(s);
  4039        packet->channel = get8u(s);
  4040  
  4041        act_comp |= packet->channel;
  4042  
  4043        if (at_eof(s))          return epuc("bad file","file too short (reading packets)");
  4044        if (packet->size != 8)  return epuc("bad format","packet isn't 8bpp");
  4045     } while (chained);
  4046  
  4047     *comp = (act_comp & 0x10 ? 4 : 3); // has alpha channel?
  4048  
  4049     for(y=0; y<height; ++y) {
  4050        int packet_idx;
  4051  
  4052        for(packet_idx=0; packet_idx < num_packets; ++packet_idx) {
  4053           pic_packet_t *packet = &packets[packet_idx];
  4054           stbi_uc *dest = result+y*width*4;
  4055  
  4056           switch (packet->type) {
  4057              default:
  4058                 return epuc("bad format","packet has bad compression type");
  4059  
  4060              case 0: {//uncompressed
  4061                 int x;
  4062  
  4063                 for(x=0;x<width;++x, dest+=4)
  4064                    if (!pic_readval(s,packet->channel,dest))
  4065                       return 0;
  4066                 break;
  4067              }
  4068  
  4069              case 1://Pure RLE
  4070                 {
  4071                    int left=width, i;
  4072  
  4073                    while (left>0) {
  4074                       stbi_uc count,value[4];
  4075  
  4076                       count=get8u(s);
  4077                       if (at_eof(s))   return epuc("bad file","file too short (pure read count)");
  4078  
  4079                       if (count > left)
  4080                          count = (uint8) left;
  4081  
  4082                       if (!pic_readval(s,packet->channel,value))  return 0;
  4083  
  4084                       for(i=0; i<count; ++i,dest+=4)
  4085                          pic_copyval(packet->channel,dest,value);
  4086                       left -= count;
  4087                    }
  4088                 }
  4089                 break;
  4090  
  4091              case 2: {//Mixed RLE
  4092                 int left=width;
  4093                 while (left>0) {
  4094                    int count = get8(s), i;
  4095                    if (at_eof(s))  return epuc("bad file","file too short (mixed read count)");
  4096  
  4097                    if (count >= 128) { // Repeated
  4098                       stbi_uc value[4];
  4099                       int i;
  4100  
  4101                       if (count==128)
  4102                          count = get16(s);
  4103                       else
  4104                          count -= 127;
  4105                       if (count > left)
  4106                          return epuc("bad file","scanline overrun");
  4107  
  4108                       if (!pic_readval(s,packet->channel,value))
  4109                          return 0;
  4110  
  4111                       for(i=0;i<count;++i, dest += 4)
  4112                          pic_copyval(packet->channel,dest,value);
  4113                    } else { // Raw
  4114                       ++count;
  4115                       if (count>left) return epuc("bad file","scanline overrun");
  4116  
  4117                       for(i=0;i<count;++i, dest+=4)
  4118                          if (!pic_readval(s,packet->channel,dest))
  4119                             return 0;
  4120                    }
  4121                    left-=count;
  4122                 }
  4123                 break;
  4124              }
  4125           }
  4126        }
  4127     }
  4128  
  4129     return result;
  4130  }
  4131  
  4132  static stbi_uc *pic_load(stbi *s,int *px,int *py,int *comp,int req_comp)
  4133  {
  4134     stbi_uc *result;
  4135     int i, x,y;
  4136  
  4137     for (i=0; i<92; ++i)
  4138        get8(s);
  4139  
  4140     x = get16(s);
  4141     y = get16(s);
  4142     if (at_eof(s))  return epuc("bad file","file too short (pic header)");
  4143     if ((1 << 28) / x < y) return epuc("too large", "Image too large to decode");
  4144  
  4145     get32(s); //skip `ratio'
  4146     get16(s); //skip `fields'
  4147     get16(s); //skip `pad'
  4148  
  4149     // intermediate buffer is RGBA
  4150     result = (stbi_uc *) malloc(x*y*4);
  4151     memset(result, 0xff, x*y*4);
  4152  
  4153     if (!pic_load2(s,x,y,comp, result)) {
  4154        free(result);
  4155        result=0;
  4156     }
  4157     *px = x;
  4158     *py = y;
  4159     if (req_comp == 0) req_comp = *comp;
  4160     result=convert_format(result,4,req_comp,x,y);
  4161  
  4162     return result;
  4163  }
  4164  
  4165  int stbi_pic_test_memory(stbi_uc const *buffer, int len)
  4166  {
  4167     stbi s;
  4168     start_mem(&s,buffer,len);
  4169     return pic_test(&s);
  4170  }
  4171  
  4172  stbi_uc *stbi_pic_load_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp)
  4173  {
  4174     stbi s;
  4175     start_mem(&s,buffer,len);
  4176     return pic_load(&s,x,y,comp,req_comp);
  4177  }
  4178  
  4179  #ifndef STBI_NO_STDIO
  4180  int stbi_pic_test_file(FILE *f)
  4181  {
  4182     int result;
  4183     long l = ftell(f);
  4184     stbi s;
  4185     start_file(&s,f);
  4186     result = pic_test(&s);
  4187     fseek(f,l,SEEK_SET);
  4188     return result;
  4189  }
  4190  
  4191  stbi_uc *stbi_pic_load(char const *filename,int *x, int *y, int *comp, int req_comp)
  4192  {
  4193     stbi_uc *result;
  4194     FILE *f=fopen(filename,"rb");
  4195     if (!f) return 0;
  4196     result = stbi_pic_load_from_file(f,x,y,comp,req_comp);
  4197     fclose(f);
  4198     return result;
  4199  }
  4200  
  4201  stbi_uc *stbi_pic_load_from_file(FILE *f,int *x, int *y, int *comp, int req_comp)
  4202  {
  4203     stbi s;
  4204     start_file(&s,f);
  4205     return pic_load(&s,x,y,comp,req_comp);
  4206  }
  4207  #endif
  4208  
  4209  // *************************************************************************************************
  4210  // GIF loader -- public domain by Jean-Marc Lienher -- simplified/shrunk by stb
  4211  typedef struct stbi_gif_lzw_struct {
  4212     int16 prefix;
  4213     uint8 first;
  4214     uint8 suffix;
  4215  } stbi_gif_lzw;
  4216  
  4217  typedef struct stbi_gif_struct
  4218  {
  4219     int w,h;
  4220     stbi_uc *out;                 // output buffer (always 4 components)
  4221     int flags, bgindex, ratio, transparent, eflags;
  4222     uint8  pal[256][4];
  4223     uint8 lpal[256][4];
  4224     stbi_gif_lzw codes[4096];
  4225     uint8 *color_table;
  4226     int parse, step;
  4227     int lflags;
  4228     int start_x, start_y;
  4229     int max_x, max_y;
  4230     int cur_x, cur_y;
  4231     int line_size;
  4232  } stbi_gif;
  4233  
  4234  static int gif_test(stbi *s)
  4235  {
  4236     int sz;
  4237     if (get8(s) != 'G' || get8(s) != 'I' || get8(s) != 'F' || get8(s) != '8') return 0;
  4238     sz = get8(s);
  4239     if (sz != '9' && sz != '7') return 0;
  4240     if (get8(s) != 'a') return 0;
  4241     return 1;
  4242  }
  4243  
  4244  #ifndef STBI_NO_STDIO
  4245  int      stbi_gif_test_file        (FILE *f)
  4246  {
  4247     stbi s;
  4248     int r,n = ftell(f);
  4249     start_file(&s,f);
  4250     r = gif_test(&s);
  4251     fseek(f,n,SEEK_SET);
  4252     return r;
  4253  }
  4254  #endif
  4255  
  4256  int      stbi_gif_test_memory      (stbi_uc const *buffer, int len)
  4257  {
  4258     stbi s;
  4259     start_mem(&s, buffer, len);
  4260     return gif_test(&s);
  4261  }
  4262  
  4263  static void stbi_gif_parse_colortable(stbi *s, uint8 pal[256][4], int num_entries, int transp)
  4264  {
  4265     int i;
  4266     for (i=0; i < num_entries; ++i) {
  4267        pal[i][2] = get8u(s);
  4268        pal[i][1] = get8u(s);
  4269        pal[i][0] = get8u(s);
  4270        pal[i][3] = transp ? 0 : 255;
  4271     }
  4272  }
  4273  
  4274  static int stbi_gif_header(stbi *s, stbi_gif *g, int *comp, int is_info)
  4275  {
  4276     uint8 version;
  4277     if (get8(s) != 'G' || get8(s) != 'I' || get8(s) != 'F' || get8(s) != '8')
  4278        return e("not GIF", "Corrupt GIF");
  4279  
  4280     version = get8u(s);
  4281     if (version != '7' && version != '9')    return e("not GIF", "Corrupt GIF");
  4282     if (get8(s) != 'a')                      return e("not GIF", "Corrupt GIF");
  4283  
  4284     failure_reason = "";
  4285     g->w = get16le(s);
  4286     g->h = get16le(s);
  4287     g->flags = get8(s);
  4288     g->bgindex = get8(s);
  4289     g->ratio = get8(s);
  4290     g->transparent = -1;
  4291  
  4292     if (comp != 0) *comp = 4;  // can't actually tell whether it's 3 or 4 until we parse the comments
  4293  
  4294     if (is_info) return 1;
  4295  
  4296     if (g->flags & 0x80)
  4297        stbi_gif_parse_colortable(s,g->pal, 2 << (g->flags & 7), -1);
  4298  
  4299     return 1;
  4300  }
  4301  
  4302  static int stbi_gif_info_raw(stbi *s, int *x, int *y, int *comp)
  4303  {
  4304     stbi_gif g;
  4305     if (!stbi_gif_header(s, &g, comp, 1)) return 0;
  4306     if (x) *x = g.w;
  4307     if (y) *y = g.h;
  4308     return 1;
  4309  }
  4310  
  4311  static void stbi_out_gif_code(stbi_gif *g, uint16 code)
  4312  {
  4313     uint8 *p, *c;
  4314  
  4315     // recurse to decode the prefixes, since the linked-list is backwards,
  4316     // and working backwards through an interleaved image would be nasty
  4317     if (g->codes[code].prefix >= 0)
  4318        stbi_out_gif_code(g, g->codes[code].prefix);
  4319  
  4320     if (g->cur_y >= g->max_y) return;
  4321  
  4322     p = &g->out[g->cur_x + g->cur_y];
  4323     c = &g->color_table[g->codes[code].suffix * 4];
  4324  
  4325     if (c[3] >= 128) {
  4326        p[0] = c[2];
  4327        p[1] = c[1];
  4328        p[2] = c[0];
  4329        p[3] = c[3];
  4330     }
  4331     g->cur_x += 4;
  4332  
  4333     if (g->cur_x >= g->max_x) {
  4334        g->cur_x = g->start_x;
  4335        g->cur_y += g->step;
  4336  
  4337        while (g->cur_y >= g->max_y && g->parse > 0) {
  4338           g->step = (1 << g->parse) * g->line_size;
  4339           g->cur_y = g->start_y + (g->step >> 1);
  4340           --g->parse;
  4341        }
  4342     }
  4343  }
  4344  
  4345  static uint8 *stbi_process_gif_raster(stbi *s, stbi_gif *g)
  4346  {
  4347     uint8 lzw_cs;
  4348     int32 len, code;
  4349     uint32 first;
  4350     int32 codesize, codemask, avail, oldcode, bits, valid_bits, clear;
  4351     stbi_gif_lzw *p;
  4352  
  4353     lzw_cs = get8u(s);
  4354     clear = 1 << lzw_cs;
  4355     first = 1;
  4356     codesize = lzw_cs + 1;
  4357     codemask = (1 << codesize) - 1;
  4358     bits = 0;
  4359     valid_bits = 0;
  4360     for (code = 0; code < clear; code++) {
  4361        g->codes[code].prefix = -1;
  4362        g->codes[code].first = (uint8) code;
  4363        g->codes[code].suffix = (uint8) code;
  4364     }
  4365  
  4366     // support no starting clear code
  4367     avail = clear+2;
  4368     oldcode = -1;
  4369  
  4370     len = 0;
  4371     for(;;) {
  4372        if (valid_bits < codesize) {
  4373           if (len == 0) {
  4374              len = get8(s); // start new block
  4375              if (len == 0)
  4376                 return g->out;
  4377           }
  4378           --len;
  4379           bits |= (int32) get8(s) << valid_bits;
  4380           valid_bits += 8;
  4381        } else {
  4382           int32 code = bits & codemask;
  4383           bits >>= codesize;
  4384           valid_bits -= codesize;
  4385           // @OPTIMIZE: is there some way we can accelerate the non-clear path?
  4386           if (code == clear) {  // clear code
  4387              codesize = lzw_cs + 1;
  4388              codemask = (1 << codesize) - 1;
  4389              avail = clear + 2;
  4390              oldcode = -1;
  4391              first = 0;
  4392           } else if (code == clear + 1) { // end of stream code
  4393              skip(s, len);
  4394              while ((len = get8(s)) > 0)
  4395                 skip(s,len);
  4396              return g->out;
  4397           } else if (code <= avail) {
  4398              if (first) return epuc("no clear code", "Corrupt GIF");
  4399  
  4400              if (oldcode >= 0) {
  4401                 p = &g->codes[avail++];
  4402                 if (avail > 4096)        return epuc("too many codes", "Corrupt GIF");
  4403                 p->prefix = (int16) oldcode;
  4404                 p->first = g->codes[oldcode].first;
  4405                 p->suffix = (code == avail) ? p->first : g->codes[code].first;
  4406              } else if (code == avail)
  4407                 return epuc("illegal code in raster", "Corrupt GIF");
  4408  
  4409              stbi_out_gif_code(g, (uint16) code);
  4410  
  4411              if ((avail & codemask) == 0 && avail <= 0x0FFF) {
  4412                 codesize++;
  4413                 codemask = (1 << codesize) - 1;
  4414              }
  4415  
  4416              oldcode = code;
  4417           } else {
  4418              return epuc("illegal code in raster", "Corrupt GIF");
  4419           }
  4420        }
  4421     }
  4422  }
  4423  
  4424  static void stbi_fill_gif_background(stbi_gif *g)
  4425  {
  4426     int i;
  4427     uint8 *c = g->pal[g->bgindex];
  4428     // @OPTIMIZE: write a dword at a time
  4429     for (i = 0; i < g->w * g->h * 4; i += 4) {
  4430        uint8 *p  = &g->out[i];
  4431        p[0] = c[2];
  4432        p[1] = c[1];
  4433        p[2] = c[0];
  4434        p[3] = c[3];
  4435     }
  4436  }
  4437  
  4438  // this function is designed to support animated gifs, although stb_image doesn't support it
  4439  static uint8 *stbi_gif_load_next(stbi *s, stbi_gif *g, int *comp, int req_comp)
  4440  {
  4441     int i;
  4442     uint8 *old_out = 0;
  4443  
  4444     if (g->out == 0) {
  4445        if (!stbi_gif_header(s, g, comp,0))     return 0; // failure_reason set by stbi_gif_header
  4446        g->out = (uint8 *) malloc(4 * g->w * g->h);
  4447        if (g->out == 0)                      return epuc("outofmem", "Out of memory");
  4448        stbi_fill_gif_background(g);
  4449     } else {
  4450        // animated-gif-only path
  4451        if (((g->eflags & 0x1C) >> 2) == 3) {
  4452           old_out = g->out;
  4453           g->out = (uint8 *) malloc(4 * g->w * g->h);
  4454           if (g->out == 0)                   return epuc("outofmem", "Out of memory");
  4455           memcpy(g->out, old_out, g->w*g->h*4);
  4456        }
  4457     }
  4458  
  4459     for (;;) {
  4460        switch (get8(s)) {
  4461           case 0x2C: /* Image Descriptor */
  4462           {
  4463              int32 x, y, w, h;
  4464              uint8 *o;
  4465  
  4466              x = get16le(s);
  4467              y = get16le(s);
  4468              w = get16le(s);
  4469              h = get16le(s);
  4470              if (((x + w) > (g->w)) || ((y + h) > (g->h)))
  4471                 return epuc("bad Image Descriptor", "Corrupt GIF");
  4472  
  4473              g->line_size = g->w * 4;
  4474              g->start_x = x * 4;
  4475              g->start_y = y * g->line_size;
  4476              g->max_x   = g->start_x + w * 4;
  4477              g->max_y   = g->start_y + h * g->line_size;
  4478              g->cur_x   = g->start_x;
  4479              g->cur_y   = g->start_y;
  4480  
  4481              g->lflags = get8(s);
  4482  
  4483              if (g->lflags & 0x40) {
  4484                 g->step = 8 * g->line_size; // first interlaced spacing
  4485                 g->parse = 3;
  4486              } else {
  4487                 g->step = g->line_size;
  4488                 g->parse = 0;
  4489              }
  4490  
  4491              if (g->lflags & 0x80) {
  4492                 stbi_gif_parse_colortable(s,g->lpal, 2 << (g->lflags & 7), g->eflags & 0x01 ? g->transparent : -1);
  4493                 g->color_table = (uint8 *) g->lpal;
  4494              } else if (g->flags & 0x80) {
  4495                 for (i=0; i < 256; ++i)  // @OPTIMIZE: reset only the previous transparent
  4496                    g->pal[i][3] = 255;
  4497                 if (g->transparent >= 0 && (g->eflags & 0x01))
  4498                    g->pal[g->transparent][3] = 0;
  4499                 g->color_table = (uint8 *) g->pal;
  4500              } else
  4501                 return epuc("missing color table", "Corrupt GIF");
  4502  
  4503              o = stbi_process_gif_raster(s, g);
  4504              if (o == NULL) return NULL;
  4505  
  4506              if (req_comp && req_comp != 4)
  4507                 o = convert_format(o, 4, req_comp, g->w, g->h);
  4508              return o;
  4509           }
  4510  
  4511           case 0x21: // Comment Extension.
  4512           {
  4513              int len;
  4514              if (get8(s) == 0xF9) { // Graphic Control Extension.
  4515                 len = get8(s);
  4516                 if (len == 4) {
  4517                    g->eflags = get8(s);
  4518                    get16le(s); // delay
  4519                    g->transparent = get8(s);
  4520                 } else {
  4521                    skip(s, len);
  4522                    break;
  4523                 }
  4524              }
  4525              while ((len = get8(s)) != 0)
  4526                 skip(s, len);
  4527              break;
  4528           }
  4529  
  4530           case 0x3B: // gif stream termination code
  4531              return (uint8 *) 1;
  4532  
  4533           default:
  4534              return epuc("unknown code", "Corrupt GIF");
  4535        }
  4536     }
  4537  }
  4538  
  4539  #ifndef STBI_NO_STDIO
  4540  stbi_uc *stbi_gif_load             (char const *filename,           int *x, int *y, int *comp, int req_comp)
  4541  {
  4542     uint8 *data;
  4543     FILE *f = fopen(filename, "rb");
  4544     if (!f) return NULL;
  4545     data = stbi_gif_load_from_file(f, x,y,comp,req_comp);
  4546     fclose(f);
  4547     return data;
  4548  }
  4549  
  4550  stbi_uc *stbi_gif_load_from_file   (FILE *f, int *x, int *y, int *comp, int req_comp)
  4551  {
  4552     uint8 *u = 0;
  4553     stbi s;
  4554     stbi_gif g={0};
  4555     start_file(&s, f);
  4556  
  4557     u = stbi_gif_load_next(&s, &g, comp, req_comp);
  4558     if (u == (void *) 1) u = 0;  // end of animated gif marker
  4559     if (u) {
  4560        *x = g.w;
  4561        *y = g.h;
  4562     }
  4563  
  4564     return u;
  4565  }
  4566  #endif
  4567  
  4568  stbi_uc *stbi_gif_load_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp)
  4569  {
  4570     uint8 *u = 0;
  4571     stbi s;
  4572     stbi_gif g={0};
  4573     start_mem(&s, buffer, len);
  4574     u = stbi_gif_load_next(&s, &g, comp, req_comp);
  4575     if (u == (void *) 1) u = 0;  // end of animated gif marker
  4576     if (u) {
  4577        *x = g.w;
  4578        *y = g.h;
  4579     }
  4580     return u;
  4581  }
  4582  
  4583  #ifndef STBI_NO_STDIO
  4584  int      stbi_gif_info             (char const *filename,           int *x, int *y, int *comp)
  4585  {
  4586     int res;
  4587     FILE *f = fopen(filename, "rb");
  4588     if (!f) return 0;
  4589     res = stbi_gif_info_from_file(f, x, y, comp);
  4590     fclose(f);
  4591     return res;
  4592  }
  4593  
  4594  int stbi_gif_info_from_file(FILE *f, int *x, int *y, int *comp)
  4595  {
  4596     stbi s;
  4597     int res;
  4598     long n = ftell(f);
  4599     start_file(&s, f);
  4600     res = stbi_gif_info_raw(&s, x, y, comp);
  4601     fseek(f, n, SEEK_SET);
  4602     return res;
  4603  }
  4604  #endif // !STBI_NO_STDIO
  4605  
  4606  int stbi_gif_info_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp)
  4607  {
  4608     stbi s;
  4609     start_mem(&s, buffer, len);
  4610     return stbi_gif_info_raw(&s, x, y, comp);
  4611  }
  4612  
  4613  
  4614  
  4615  
  4616  // *************************************************************************************************
  4617  // Radiance RGBE HDR loader
  4618  // originally by Nicolas Schulz
  4619  #ifndef STBI_NO_HDR
  4620  static int hdr_test(stbi *s)
  4621  {
  4622     const char *signature = "#?RADIANCE\n";
  4623     int i;
  4624     for (i=0; signature[i]; ++i)
  4625        if (get8(s) != signature[i])
  4626           return 0;
  4627     return 1;
  4628  }
  4629  
  4630  int stbi_hdr_test_memory(stbi_uc const *buffer, int len)
  4631  {
  4632     stbi s;
  4633     start_mem(&s, buffer, len);
  4634     return hdr_test(&s);
  4635  }
  4636  
  4637  #ifndef STBI_NO_STDIO
  4638  int stbi_hdr_test_file(FILE *f)
  4639  {
  4640     stbi s;
  4641     int r,n = ftell(f);
  4642     start_file(&s, f);
  4643     r = hdr_test(&s);
  4644     fseek(f,n,SEEK_SET);
  4645     return r;
  4646  }
  4647  #endif
  4648  
  4649  #define HDR_BUFLEN  1024
  4650  static char *hdr_gettoken(stbi *z, char *buffer)
  4651  {
  4652     int len=0;
  4653     char c = '\0';
  4654  
  4655     c = (char) get8(z);
  4656  
  4657     while (!at_eof(z) && c != '\n') {
  4658        buffer[len++] = c;
  4659        if (len == HDR_BUFLEN-1) {
  4660           // flush to end of line
  4661           while (!at_eof(z) && get8(z) != '\n')
  4662              ;
  4663           break;
  4664        }
  4665        c = (char) get8(z);
  4666     }
  4667  
  4668     buffer[len] = 0;
  4669     return buffer;
  4670  }
  4671  
  4672  static void hdr_convert(float *output, stbi_uc *input, int req_comp)
  4673  {
  4674     if ( input[3] != 0 ) {
  4675        float f1;
  4676        // Exponent
  4677        f1 = (float) ldexp(1.0f, input[3] - (int)(128 + 8));
  4678        if (req_comp <= 2)
  4679           output[0] = (input[0] + input[1] + input[2]) * f1 / 3;
  4680        else {
  4681           output[0] = input[0] * f1;
  4682           output[1] = input[1] * f1;
  4683           output[2] = input[2] * f1;
  4684        }
  4685        if (req_comp == 2) output[1] = 1;
  4686        if (req_comp == 4) output[3] = 1;
  4687     } else {
  4688        switch (req_comp) {
  4689           case 4: output[3] = 1; /* fallthrough */
  4690           case 3: output[0] = output[1] = output[2] = 0;
  4691                   break;
  4692           case 2: output[1] = 1; /* fallthrough */
  4693           case 1: output[0] = 0;
  4694                   break;
  4695        }
  4696     }
  4697  }
  4698  
  4699  
  4700  static float *hdr_load(stbi *s, int *x, int *y, int *comp, int req_comp)
  4701  {
  4702     char buffer[HDR_BUFLEN];
  4703     char *token;
  4704     int valid = 0;
  4705     int width, height;
  4706     stbi_uc *scanline;
  4707     float *hdr_data;
  4708     int len;
  4709     unsigned char count, value;
  4710     int i, j, k, c1,c2, z;
  4711  
  4712  
  4713     // Check identifier
  4714     if (strcmp(hdr_gettoken(s,buffer), "#?RADIANCE") != 0)
  4715        return epf("not HDR", "Corrupt HDR image");
  4716  
  4717     // Parse header
  4718     for(;;) {
  4719        token = hdr_gettoken(s,buffer);
  4720        if (token[0] == 0) break;
  4721        if (strcmp(token, "FORMAT=32-bit_rle_rgbe") == 0) valid = 1;
  4722     }
  4723  
  4724     if (!valid)    return epf("unsupported format", "Unsupported HDR format");
  4725  
  4726     // Parse width and height
  4727     // can't use sscanf() if we're not using stdio!
  4728     token = hdr_gettoken(s,buffer);
  4729     if (strncmp(token, "-Y ", 3))  return epf("unsupported data layout", "Unsupported HDR format");
  4730     token += 3;
  4731     height = strtol(token, &token, 10);
  4732     while (*token == ' ') ++token;
  4733     if (strncmp(token, "+X ", 3))  return epf("unsupported data layout", "Unsupported HDR format");
  4734     token += 3;
  4735     width = strtol(token, NULL, 10);
  4736  
  4737     *x = width;
  4738     *y = height;
  4739  
  4740     *comp = 3;
  4741     if (req_comp == 0) req_comp = 3;
  4742  
  4743     // Read data
  4744     hdr_data = (float *) malloc(height * width * req_comp * sizeof(float));
  4745  
  4746     // Load image data
  4747     // image data is stored as some number of sca
  4748     if ( width < 8 || width >= 32768) {
  4749        // Read flat data
  4750        for (j=0; j < height; ++j) {
  4751           for (i=0; i < width; ++i) {
  4752              stbi_uc rgbe[4];
  4753             main_decode_loop:
  4754              getn(s, rgbe, 4);
  4755              hdr_convert(hdr_data + j * width * req_comp + i * req_comp, rgbe, req_comp);
  4756           }
  4757        }
  4758     } else {
  4759        // Read RLE-encoded data
  4760        scanline = NULL;
  4761  
  4762        for (j = 0; j < height; ++j) {
  4763           c1 = get8(s);
  4764           c2 = get8(s);
  4765           len = get8(s);
  4766           if (c1 != 2 || c2 != 2 || (len & 0x80)) {
  4767              // not run-length encoded, so we have to actually use THIS data as a decoded
  4768              // pixel (note this can't be a valid pixel--one of RGB must be >= 128)
  4769              uint8 rgbe[4];
  4770              rgbe[0] = (uint8) c1;
  4771              rgbe[1] = (uint8) c2;
  4772              rgbe[2] = (uint8) len;
  4773              rgbe[3] = (uint8) get8u(s);
  4774              hdr_convert(hdr_data, rgbe, req_comp);
  4775              i = 1;
  4776              j = 0;
  4777              free(scanline);
  4778              goto main_decode_loop; // yes, this makes no sense
  4779           }
  4780           len <<= 8;
  4781           len |= get8(s);
  4782           if (len != width) { free(hdr_data); free(scanline); return epf("invalid decoded scanline length", "corrupt HDR"); }
  4783           if (scanline == NULL) scanline = (stbi_uc *) malloc(width * 4);
  4784  
  4785           for (k = 0; k < 4; ++k) {
  4786              i = 0;
  4787              while (i < width) {
  4788                 count = get8u(s);
  4789                 if (count > 128) {
  4790                    // Run
  4791                    value = get8u(s);
  4792                    count -= 128;
  4793                    for (z = 0; z < count; ++z)
  4794                       scanline[i++ * 4 + k] = value;
  4795                 } else {
  4796                    // Dump
  4797                    for (z = 0; z < count; ++z)
  4798                       scanline[i++ * 4 + k] = get8u(s);
  4799                 }
  4800              }
  4801           }
  4802           for (i=0; i < width; ++i)
  4803              hdr_convert(hdr_data+(j*width + i)*req_comp, scanline + i*4, req_comp);
  4804        }
  4805        free(scanline);
  4806     }
  4807  
  4808     return hdr_data;
  4809  }
  4810  
  4811  #ifndef STBI_NO_STDIO
  4812  float *stbi_hdr_load_from_file(FILE *f, int *x, int *y, int *comp, int req_comp)
  4813  {
  4814     stbi s;
  4815     start_file(&s,f);
  4816     return hdr_load(&s,x,y,comp,req_comp);
  4817  }
  4818  #endif
  4819  
  4820  float *stbi_hdr_load_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp)
  4821  {
  4822     stbi s;
  4823     start_mem(&s,buffer, len);
  4824     return hdr_load(&s,x,y,comp,req_comp);
  4825  }
  4826  
  4827  #endif // STBI_NO_HDR
  4828  
  4829  
  4830  #ifndef STBI_NO_STDIO
  4831  int stbi_info(char const *filename, int *x, int *y, int *comp)
  4832  {
  4833      FILE *f = fopen(filename, "rb");
  4834      int result;
  4835      if (!f) return e("can't fopen", "Unable to open file");
  4836      result = stbi_info_from_file(f, x, y, comp);
  4837      fclose(f);
  4838      return result;
  4839  }
  4840  
  4841  int stbi_info_from_file(FILE *f, int *x, int *y, int *comp)
  4842  {
  4843     if (stbi_jpeg_info_from_file(f, x, y, comp))
  4844         return 1;
  4845     if (stbi_png_info_from_file(f, x, y, comp))
  4846         return 1;
  4847     if (stbi_gif_info_from_file(f, x, y, comp))
  4848         return 1;
  4849     // @TODO: stbi_bmp_info_from_file
  4850     // @TODO: stbi_psd_info_from_file
  4851     #ifndef STBI_NO_HDR
  4852     // @TODO: stbi_hdr_info_from_file
  4853     #endif
  4854     // test tga last because it's a crappy test!
  4855     if (stbi_tga_info_from_file(f, x, y, comp))
  4856         return 1;
  4857     return e("unknown image type", "Image not of any known type, or corrupt");
  4858  }
  4859  #endif // !STBI_NO_STDIO
  4860  
  4861  int stbi_info_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp)
  4862  {
  4863     if (stbi_jpeg_info_from_memory(buffer, len, x, y, comp))
  4864         return 1;
  4865     if (stbi_png_info_from_memory(buffer, len, x, y, comp))
  4866         return 1;
  4867     if (stbi_gif_info_from_memory(buffer, len, x, y, comp))
  4868         return 1;
  4869     // @TODO: stbi_bmp_info_from_memory
  4870     // @TODO: stbi_psd_info_from_memory
  4871     #ifndef STBI_NO_HDR
  4872     // @TODO: stbi_hdr_info_from_memory
  4873     #endif
  4874     // test tga last because it's a crappy test!
  4875     if (stbi_tga_info_from_memory(buffer, len, x, y, comp))
  4876         return 1;
  4877     return e("unknown image type", "Image not of any known type, or corrupt");
  4878  }
  4879  
  4880  /////////////////////// write image ///////////////////////
  4881  
  4882  #ifndef STBI_NO_WRITE
  4883  
  4884  static void write8(FILE *f, int x) { uint8 z = (uint8) x; fwrite(&z,1,1,f); }
  4885  
  4886  static void writefv(FILE *f, char *fmt, va_list v)
  4887  {
  4888     while (*fmt) {
  4889        switch (*fmt++) {
  4890           case ' ': break;
  4891           case '1': { uint8 x = (uint8)va_arg(v, int); write8(f,x); break; }
  4892           case '2': { int16 x = (int16)va_arg(v, int); write8(f,x); write8(f,x>>8); break; }
  4893           case '4': { int32 x = va_arg(v, int); write8(f,x); write8(f,x>>8); write8(f,x>>16); write8(f,x>>24); break; }
  4894           default:
  4895              assert(0);
  4896              va_end(v);
  4897              return;
  4898        }
  4899     }
  4900  }
  4901  
  4902  static void writef(FILE *f, char *fmt, ...)
  4903  {
  4904     va_list v;
  4905     va_start(v, fmt);
  4906     writefv(f,fmt,v);
  4907     va_end(v);
  4908  }
  4909  
  4910  static void write_pixels(FILE *f, int rgb_dir, int vdir, int x, int y, int comp, void *data, int write_alpha, int scanline_pad)
  4911  {
  4912     uint8 bg[3] = { 255, 0, 255}, px[3];
  4913     uint32 zero = 0;
  4914     int i,j,k, j_end;
  4915  
  4916     if (vdir < 0)
  4917        j_end = -1, j = y-1;
  4918     else
  4919        j_end =  y, j = 0;
  4920  
  4921     for (; j != j_end; j += vdir) {
  4922        for (i=0; i < x; ++i) {
  4923           uint8 *d = (uint8 *) data + (j*x+i)*comp;
  4924           if (write_alpha < 0)
  4925              fwrite(&d[comp-1], 1, 1, f);
  4926           switch (comp) {
  4927              case 1:
  4928              case 2: writef(f, (char*)"111", d[0],d[0],d[0]);
  4929                      break;
  4930              case 4:
  4931                 if (!write_alpha) {
  4932                    for (k=0; k < 3; ++k)
  4933                       px[k] = bg[k] + ((d[k] - bg[k]) * d[3])/255;
  4934                    writef(f, (char*)"111", px[1-rgb_dir],px[1],px[1+rgb_dir]);
  4935                    break;
  4936                 }
  4937                 /* FALLTHROUGH */
  4938              case 3:
  4939                 writef(f, (char*)"111", d[1-rgb_dir],d[1],d[1+rgb_dir]);
  4940                 break;
  4941           }
  4942           if (write_alpha > 0)
  4943              fwrite(&d[comp-1], 1, 1, f);
  4944        }
  4945        fwrite(&zero,scanline_pad,1,f);
  4946     }
  4947  }
  4948  
  4949  static int outfile(char const *filename, int rgb_dir, int vdir, int x, int y, int comp, void *data, int alpha, int pad, char *fmt, ...)
  4950  {
  4951     FILE *f = fopen(filename, "wb");
  4952     if (f) {
  4953        va_list v;
  4954        va_start(v, fmt);
  4955        writefv(f, fmt, v);
  4956        va_end(v);
  4957        write_pixels(f,rgb_dir,vdir,x,y,comp,data,alpha,pad);
  4958        fclose(f);
  4959     }
  4960     return f != NULL;
  4961  }
  4962  
  4963  int stbi_write_bmp(char const *filename, int x, int y, int comp, void *data)
  4964  {
  4965     int pad = (-x*3) & 3;
  4966     return outfile(filename,-1,-1,x,y,comp,data,0,pad,
  4967             (char*)"11 4 22 4" "4 44 22 444444",
  4968             'B', 'M', 14+40+(x*3+pad)*y, 0,0, 14+40,  // file header
  4969              40, x,y, 1,24, 0,0,0,0,0,0);             // bitmap header
  4970  }
  4971  
  4972  int stbi_write_tga(char const *filename, int x, int y, int comp, void *data)
  4973  {
  4974     int has_alpha = !(comp & 1);
  4975     return outfile(filename, -1,-1, x, y, comp, data, has_alpha, 0,
  4976                    (char*)"111 221 2222 11", 0,0,2, 0,0,0, 0,0,x,y, 24+8*has_alpha, 8*has_alpha);
  4977  }
  4978  
  4979  #endif // STBI_NO_WRITE
  4980  
  4981  #endif // STBI_HEADER_FILE_ONLY
  4982  
  4983  /*
  4984     revision history:
  4985        1.29 (2010-08-16) various warning fixes from Aurelien Pocheville
  4986        1.28 (2010-08-01) fix bug in GIF palette transparency (SpartanJ)
  4987        1.27 (2010-08-01)
  4988               cast-to-uint8 to fix warnings
  4989        1.26 (2010-07-24)
  4990               fix bug in file buffering for PNG reported by SpartanJ
  4991        1.25 (2010-07-17)
  4992               refix trans_data warning (Won Chun)
  4993        1.24 (2010-07-12)
  4994               perf improvements reading from files on platforms with lock-heavy fgetc()
  4995               minor perf improvements for jpeg
  4996               deprecated type-specific functions so we'll get feedback if they're needed
  4997               attempt to fix trans_data warning (Won Chun)
  4998        1.23   fixed bug in iPhone support
  4999        1.22 (2010-07-10)
  5000               removed image *writing* support
  5001               removed image *writing* support
  5002               stbi_info support from Jetro Lauha
  5003               GIF support from Jean-Marc Lienher
  5004               iPhone PNG-extensions from James Brown
  5005               warning-fixes from Nicolas Schulz and Janez Zemva (i.e. Janez (U+017D)emva)
  5006        1.21   fix use of 'uint8' in header (reported by jon blow)
  5007        1.20   added support for Softimage PIC, by Tom Seddon
  5008        1.19   bug in interlaced PNG corruption check (found by ryg)
  5009        1.18 2008-08-02
  5010               fix a threading bug (local mutable static)
  5011        1.17   support interlaced PNG
  5012        1.16   major bugfix - convert_format converted one too many pixels
  5013        1.15   initialize some fields for thread safety
  5014        1.14   fix threadsafe conversion bug
  5015               header-file-only version (#define STBI_HEADER_FILE_ONLY before including)
  5016        1.13   threadsafe
  5017        1.12   const qualifiers in the API
  5018        1.11   Support installable IDCT, colorspace conversion routines
  5019        1.10   Fixes for 64-bit (don't use "unsigned long")
  5020               optimized upsampling by Fabian "ryg" Giesen
  5021        1.09   Fix format-conversion for PSD code (bad global variables!)
  5022        1.08   Thatcher Ulrich's PSD code integrated by Nicolas Schulz
  5023        1.07   attempt to fix C++ warning/errors again
  5024        1.06   attempt to fix C++ warning/errors again
  5025        1.05   fix TGA loading to return correct *comp and use good luminance calc
  5026        1.04   default float alpha is 1, not 255; use 'void *' for stbi_image_free
  5027        1.03   bugfixes to STBI_NO_STDIO, STBI_NO_HDR
  5028        1.02   support for (subset of) HDR files, float interface for preferred access to them
  5029        1.01   fix bug: possible bug in handling right-side up bmps... not sure
  5030               fix bug: the stbi_bmp_load() and stbi_tga_load() functions didn't work at all
  5031        1.00   interface to zlib that skips zlib header
  5032        0.99   correct handling of alpha in palette
  5033        0.98   TGA loader by lonesock; dynamically add loaders (untested)
  5034        0.97   jpeg errors on too large a file; also catch another malloc failure
  5035        0.96   fix detection of invalid v value - particleman@mollyrocket forum
  5036        0.95   during header scan, seek to markers in case of padding
  5037        0.94   STBI_NO_STDIO to disable stdio usage; rename all #defines the same
  5038        0.93   handle jpegtran output; verbose errors
  5039        0.92   read 4,8,16,24,32-bit BMP files of several formats
  5040        0.91   output 24-bit Windows 3.0 BMP files
  5041        0.90   fix a few more warnings; bump version number to approach 1.0
  5042        0.61   bugfixes due to Marc LeBlanc, Christopher Lloyd
  5043        0.60   fix compiling as c++
  5044        0.59   fix warnings: merge Dave Moore's -Wall fixes
  5045        0.58   fix bug: zlib uncompressed mode len/nlen was wrong endian
  5046        0.57   fix bug: jpg last huffman symbol before marker was >9 bits but less
  5047                        than 16 available
  5048        0.56   fix bug: zlib uncompressed mode len vs. nlen
  5049        0.55   fix bug: restart_interval not initialized to 0
  5050        0.54   allow NULL for 'int *comp'
  5051        0.53   fix bug in png 3->4; speedup png decoding
  5052        0.52   png handles req_comp=3,4 directly; minor cleanup; jpeg comments
  5053        0.51   obey req_comp requests, 1-component jpegs return as 1-component,
  5054               on 'test' only check type, not whether we support this variant
  5055  */