Merge branch 'master' into working
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Conflicts: stb_image.h
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
21 files+2596 added−1179 removed
- tests:+0 added−0 removed
- other:+2596 added−1179 removed
Provenance
- Commit
5db03ef- Tree
119f146- Author
Sean Barrett <sean@nothings.org>- Committer
Sean Barrett <sean@nothings.org>- Parent 1
d74530c- Parent 2
094cb31
21 files changed+2596 added−1179 removed
This commit is too large to show whole: its files are listed by path, without their changes.
added:tests/sdf/sdf_test_arial_16.pngnot shown
Its text is not shown, added.
added:tests/sdf/sdf_test_times_16.pngnot shown
Its text is not shown, added.
added:tests/sdf/sdf_test_times_50.pngnot shown
Its text is not shown, added.
added:tests/sdf/sdf_test.cnot shown
Its text is not shown, added.
modified:tests/Makefilenot shown
Its text is not shown, modified.
modified:tests/stb.dspnot shown
Its text is not shown, modified.
modified:tests/test_truetype.cnot shown
Its text is not shown, modified.
modified:tools/README.footer.mdnot shown
Its text is not shown, modified.
modified:README.md+27 added-11 removed
9 unmodified lines10 11 12 library | lastest version | category | LoC | description13 --------------------- | ---- | -------- | --- | --------------------------------14-**[stb_vorbis.c](stb_vorbis.c)** | 1.10 | audio | 5447 | decode ogg vorbis files from file/memory to float/16-bit signed output15-**[stb_image.h](stb_image.h)** | 2.15 | graphics | 7177 | image loading/decoding from file/memory: JPG, PNG, TGA, BMP, PSD, GIF, HDR, PIC16-**[stb_truetype.h](stb_truetype.h)** | 1.15 | graphics | 4061 | parse, decode, and rasterize characters from truetype fonts17-**[stb_image_write.h](stb_image_write.h)** | 1.05 | graphics | 1092 | image writing to disk: PNG, TGA, BMP18-**[stb_image_resize.h](stb_image_resize.h)** | 0.94 | graphics | 2624 | resize images larger/smaller with good quality19-**[stb_rect_pack.h](stb_rect_pack.h)** | 0.11 | graphics | 635 | simple 2D rectangle packer with decent quality20-**[stb_sprintf.h](stb_sprintf.h)** | 1.02 | utility | 1202 | fast sprintf, snprintf for C/C++14+**[stb_vorbis.c](stb_vorbis.c)** | 1.11 | audio | 5449 | decode ogg vorbis files from file/memory to float/16-bit signed output15+**[stb_image.h](stb_image.h)** | 2.16 | graphics | 7187 | image loading/decoding from file/memory: JPG, PNG, TGA, BMP, PSD, GIF, HDR, PIC16+**[stb_truetype.h](stb_truetype.h)** | 1.17 | graphics | 4566 | parse, decode, and rasterize characters from truetype fonts17+**[stb_image_write.h](stb_image_write.h)** | 1.07 | graphics | 1458 | image writing to disk: PNG, TGA, BMP18+**[stb_image_resize.h](stb_image_resize.h)** | 0.95 | graphics | 2627 | resize images larger/smaller with good quality19+**[stb_rect_pack.h](stb_rect_pack.h)** | 0.11 | graphics | 624 | simple 2D rectangle packer with decent quality20+**[stb_sprintf.h](stb_sprintf.h)** | 1.03 | utility | 1812 | fast sprintf, snprintf for C/C++21 **[stretchy_buffer.h](stretchy_buffer.h)** | 1.02 | utility | 257 | typesafe dynamic array for C (i.e. approximation to vector<>), doesn't compile as C++22 **[stb_textedit.h](stb_textedit.h)** | 1.11 | user interface | 1393 | guts of a text editor for games etc implementing them from scratch23 **[stb_voxel_render.h](stb_voxel_render.h)** | 0.85 | 3D graphics | 3803 | Minecraft-esque voxel rendering "engine" with many more features24-**[stb_dxt.h](stb_dxt.h)** | 1.06 | 3D graphics | 687 | Fabian "ryg" Giesen's real-time DXT compressor24+**[stb_dxt.h](stb_dxt.h)** | 1.07 | 3D graphics | 719 | Fabian "ryg" Giesen's real-time DXT compressor25 **[stb_perlin.h](stb_perlin.h)** | 0.3 | 3D graphics | 316 | revised Perlin noise (3D input, 1D output)26 **[stb_easy_font.h](stb_easy_font.h)** | 1.0 | 3D graphics | 303 | quick-and-dirty easy-to-deploy bitmap font for printing frame rate, etc27 **[stb_tilemap_editor.h](stb_tilemap_editor.h)** | 0.38 | game dev | 4172 | embeddable tilemap editor28 **[stb_herringbone_wa...](stb_herringbone_wang_tile.h)** | 0.6 | game dev | 1220 | herringbone Wang tile map generator29 **[stb_c_lexer.h](stb_c_lexer.h)** | 0.09 | parsing | 962 | simplify writing parsers for C-like languages30 **[stb_divide.h](stb_divide.h)** | 0.91 | math | 419 | more useful 32-bit modulus e.g. "euclidean divide"31 **[stb_connected_comp...](stb_connected_components.h)** | 0.95 | misc | 1045 | incrementally compute reachability on grids32-**[stb.h](stb.h)** | 2.29 | misc | 14324 | helper functions for C, mostly redundant in C++; basically author's personal stuff33-**[stb_leakcheck.h](stb_leakcheck.h)** | 0.3 | misc | 165 | quick-and-dirty malloc/free leak-checking32+**[stb.h](stb.h)** | 2.30 | misc | 14328 | helper functions for C, mostly redundant in C++; basically author's personal stuff33+**[stb_leakcheck.h](stb_leakcheck.h)** | 0.4 | misc | 186 | quick-and-dirty malloc/free leak-checking34 35 Total libraries: 20 36-Total lines of C code: 5130436+Total lines of C code: 5284637 38 39 FAQ40 ---16 unmodified lines57 58 No, because it's public domain you can freely relicense it to whatever license your new59 library wants to be.60 61+#### What's the deal with SSE support in GCC-based compilers?62+63+stb_image will either use SSE2 (if you compile with -msse2) or64+will not use any SIMD at all, rather than trying to detect the65+processor at runtime and handle it correctly. As I understand it,66+the approved path in GCC for runtime-detection require67+you to use multiple source files, one for each CPU configuration.68+Because stb_image is a header-file library that compiles in only69+one source file, there's no approved way to build both an70+SSE-enabled and a non-SSE-enabled variation.71+72+While we've tried to work around it, we've had multiple issues over73+the years due to specific versions of gcc breaking what we're doing,74+so we've given up on it. See https://github.com/nothings/stb/issues/28075+and https://github.com/nothings/stb/issues/410 for examples.76+77 #### Some of these libraries seem redundant to existing open source libraries. Are they better somehow?78 79 Generally they're only better in that they're easier to integrate,80 easier to use, and easier to release (single file; good API; no78 unmodified linesmodified:stb_dxt.h+42 added-10 removed
1-// stb_dxt.h - v1.06 - DXT1/DXT5 compressor - public domain1+// stb_dxt.h - v1.07 - DXT1/DXT5 compressor - public domain2 // original by fabian "ryg" giesen - ported to C by stb3 // use '#define STB_DXT_IMPLEMENTATION' before including to create the implementation4 //5 // USAGE:2 unmodified lines8 // A is ignored if you specify alpha=0; you can turn on dithering9 // and "high quality" using mode.10 //11 // version history:12+// v1.07 - bc4; allow not using libc; add STB_DXT_STATIC13 // v1.06 - (stb) fix to known-broken 1.0514 // v1.05 - (stb) support bc5/3dc (Arvids Kokins), use extern "C" in C++ (Pavel Krajcevski)15 // v1.04 - (ryg) default to no rounding bias for lerped colors (as per S3TC/DX10 spec);16 // single color match fix (allow for inexact color interpolation);2 unmodified lines19 // v1.02 - (stb) fix alpha encoding bug20 // v1.01 - (stb) fix bug converting to RGB that messed up quality, thanks ryg & cbloom21 // v1.00 - (stb) first release22 //23+// contributors: 24+// Kevin Schmidt (#defines for "freestanding" compilation)25+// github:ppiastucki (BC4 support)26+// 27 // LICENSE28 //29 // See end of file for license information.30 8 unmodified lines39 #ifdef __cplusplus40 extern "C" {41 #endif42 38-void stb_compress_dxt_block(unsigned char *dest, const unsigned char *src_rgba_four_bytes_per_pixel, int alpha, int mode);39-void stb_compress_bc5_block(unsigned char *dest, const unsigned char *src_rg_two_byte_per_pixel);43+#ifdef STB_DXT_STATIC44+#define STBDDEF static45+#else46+#define STBDDEF extern47+#endif48 49+STBDDEF void stb_compress_dxt_block(unsigned char *dest, const unsigned char *src_rgba_four_bytes_per_pixel, int alpha, int mode);50+STBDDEF void stb_compress_bc4_block(unsigned char *dest, const unsigned char *src_r_one_byte_per_pixel);51+STBDDEF void stb_compress_bc5_block(unsigned char *dest, const unsigned char *src_rg_two_byte_per_pixel);52+53 #ifdef __cplusplus54 }55 #endif56 15 unmodified lines72 // you also see "(a*5 + b*3) / 8" on some old GPU designs.73 // #define STB_DXT_USE_ROUNDING_BIAS74 75 #include <stdlib.h>76+77+#if !defined(STBD_ABS) || !defined(STBI_FABS)78 #include <math.h>65-#include <string.h> // memset79+#endif80 81+#ifndef STBD_ABS82+#define STBD_ABS(i) abs(i)83+#endif84+85+#ifndef STBD_FABS86+#define STBD_FABS(x) fabs(x)87+#endif88+89+#ifndef STBD_MEMSET90+#include <string.h>91+#define STBD_MEMSET memset92+#endif93+94 static unsigned char stb__Expand5[32];95 static unsigned char stb__Expand6[64];96 static unsigned char stb__OMatch5[256][2];97 static unsigned char stb__OMatch6[256][2];55 unmodified lines153 for (mn=0;mn<size;mn++) {154 for (mx=0;mx<size;mx++) {155 int mine = expand[mn];156 int maxe = expand[mx];130- int err = abs(stb__Lerp13(maxe, mine) - i);157+ int err = STBD_ABS(stb__Lerp13(maxe, mine) - i);158 159 // DX10 spec says that interpolation must be within 3% of "correct" result,160 // add this as error term. (normally we'd expect a random distribution of161 // +-1.5% error, but nowhere in the spec does it say that the error has to be162 // unbiased - better safe than sorry).136- err += abs(maxe - mine) * 3 / 100;163+ err += STBD_ABS(maxe - mine) * 3 / 100;164 165 if(err < bestErr)166 { 167 Table[i*2+0] = mx;23 unmodified lines191 // process channels seperately192 for (ch=0; ch<3; ++ch) {193 unsigned char *bp = block+ch, *dp = dest+ch;194 unsigned char *quant = (ch == 1) ? stb__QuantGTab+8 : stb__QuantRBTab+8;168- memset(err, 0, sizeof(err));195+ STBD_MEMSET(err, 0, sizeof(err));196 for(y=0; y<4; ++y) {197 dp[ 0] = quant[bp[ 0] + ((3*ep2[1] + 5*ep2[0]) >> 4)];198 ep1[0] = bp[ 0] - dp[ 0];199 dp[ 4] = quant[bp[ 4] + ((7*ep1[0] + 3*ep2[2] + 5*ep2[1] + ep2[0]) >> 4)];175 unmodified lines375 vfg = g;376 vfb = b;377 }378 352- magn = fabs(vfr);353- if (fabs(vfg) > magn) magn = fabs(vfg);354- if (fabs(vfb) > magn) magn = fabs(vfb);379+ magn = STBD_FABS(vfr);380+ if (STBD_FABS(vfg) > magn) magn = STBD_FABS(vfg);381+ if (STBD_FABS(vfb) > magn) magn = STBD_FABS(vfb);382 383 if(magn < 4.0f) { // too small, default to luminance384 v_r = 299; // JPEG YCbCr luma coefs, scaled by 1000.385 v_g = 587;276 unmodified lines662 663 stb__CompressColorBlock(dest,(unsigned char*) src,mode);664 }665 666+void stb_compress_bc4_block(unsigned char *dest, const unsigned char *src)667+{668+ stb__CompressAlphaBlock(dest,(unsigned char*) src, 1);669+}670+671 void stb_compress_bc5_block(unsigned char *dest, const unsigned char *src)672 {673 stb__CompressAlphaBlock(dest,(unsigned char*) src,2);674 stb__CompressAlphaBlock(dest + 8,(unsigned char*) src+1,2);45 unmodified linesmodified:stb_image_resize.h+8 added-5 removed
1-/* stb_image_resize - v0.94 - public domain image resizing1+/* stb_image_resize - v0.95 - public domain image resizing2 by Jorge L Rodriguez (@VinoBS) - 20143 http://github.com/nothings/stb4 5 Written with emphasis on usability, portability, and efficiency. (No149 unmodified lines155 CONTRIBUTORS156 Jorge L Rodriguez: Implementation157 Sean Barrett: API design, optimizations158 Aras Pranckevicius: bugfix159+ Nathan Reed: warning fixes159- 160+161 REVISIONS162+ 0.95 (2017-07-23) fixed warnings163 0.94 (2017-03-18) fixed warnings164 0.93 (2017-03-03) fixed bug with certain combinations of heights165 0.92 (2017-01-02) fix integer overflow on large (>2GB) images166 0.91 (2016-04-02) fix warnings; fix handling of subpixel regions227 unmodified lines394 #include <math.h>395 396 #ifndef STBIR_MALLOC397 #include <stdlib.h>396-#define STBIR_MALLOC(size,c) malloc(size)397-#define STBIR_FREE(ptr,c) free(ptr)398+// use comma operator to evaluate c, to avoid "unused parameter" warnings399+#define STBIR_MALLOC(size,c) ((void)(c), malloc(size))400+#define STBIR_FREE(ptr,c) ((void)(c), free(ptr))401 #endif402 403 #ifndef _MSC_VER404 #ifdef __cplusplus580 unmodified lines985 m = max - m;986 987 return (m);988 }986- return n; // NOTREACHED989+ // NOTREACHED990 991 default:992 STBIR_ASSERT(!"Unimplemented edge type");993 return 0;1634 unmodified linesmodified:stb_image_write.h+534 added-71 removed
1-/* stb_image_write - v1.05 - public domain - http://nothings.org/stb/stb_image_write.h2- writes out PNG/BMP/TGA images to C stdio - Sean Barrett 2010-20151+/* stb_image_write - v1.08 - public domain - http://nothings.org/stb/stb_image_write.h2+ writes out PNG/BMP/TGA/JPEG/HDR images to C stdio - Sean Barrett 2010-20153 no warranty implied; use at your own risk4 5 Before #including,6 2 unmodified lines9 in the file that you want to have the implementation.10 11 Will probably not work correctly with strict-aliasing optimizations.12 13+ If using a modern Microsoft Compiler, non-safe versions of CRT calls may cause 14+ compilation warnings or even errors. To avoid this, also before #including,15+16+ #define STBI_MSC_SECURE_CRT17+18 ABOUT:19 20 This header file is a library for writing images to C stdio. It could be21 adapted to write to memory or a general streaming interface; let me know.22 23 The PNG output is not optimal; it is 20-50% larger than the file19- written by a decent optimizing implementation. This library is designed20- for source code compactness and simplicity, not optimal image file size21- or run-time performance.24+ written by a decent optimizing implementation; though providing a custom25+ zlib compress function (see STBIW_ZLIB_COMPRESS) can mitigate that.26+ This library is designed for source code compactness and simplicity,27+ not optimal image file size or run-time performance.28 29 BUILDING:30 31 You can #define STBIW_ASSERT(x) before the #include to avoid using assert.h.32 You can #define STBIW_MALLOC(), STBIW_REALLOC(), and STBIW_FREE() to replace33 malloc,realloc,free.28- You can define STBIW_MEMMOVE() to replace memmove()34+ You can #define STBIW_MEMMOVE() to replace memmove()35+ You can #define STBIW_ZLIB_COMPRESS to use a custom zlib-style compress function36+ for PNG compression (instead of the builtin one), it must have the following signature:37+ unsigned char * my_compress(unsigned char *data, int data_len, int *out_len, int quality);38+ The returned data will be freed with STBIW_FREE() (free() by default),39+ so it must be heap allocated with STBIW_MALLOC() (malloc() by default),40 41 USAGE:42 32- There are four functions, one for each image file format:43+ There are five functions, one for each image file format:44 45 int stbi_write_png(char const *filename, int w, int h, int comp, const void *data, int stride_in_bytes);46 int stbi_write_bmp(char const *filename, int w, int h, int comp, const void *data);47 int stbi_write_tga(char const *filename, int w, int h, int comp, const void *data);48 int stbi_write_hdr(char const *filename, int w, int h, int comp, const float *data);49+ int stbi_write_jpg(char const *filename, int w, int h, int comp, const float *data, int quality);50 51+ void stbi_flip_vertically_on_write(int flag); // flag is non-zero to flip data vertically52+39- There are also four equivalent functions that use an arbitrary write function. You are53+ There are also five equivalent functions that use an arbitrary write function. You are54 expected to open/close your file-equivalent before and after calling these:55 56 int stbi_write_png_to_func(stbi_write_func *func, void *context, int w, int h, int comp, const void *data, int stride_in_bytes);57 int stbi_write_bmp_to_func(stbi_write_func *func, void *context, int w, int h, int comp, const void *data);58 int stbi_write_tga_to_func(stbi_write_func *func, void *context, int w, int h, int comp, const void *data);59 int stbi_write_hdr_to_func(stbi_write_func *func, void *context, int w, int h, int comp, const float *data);60+ int stbi_write_jpg_to_func(stbi_write_func *func, void *context, int x, int y, int comp, const void *data, int quality);61 62 where the callback is:63 void stbi_write_func(void *context, void *data, int size);64 65+ You can configure it with these global variables:66+ int stbi_write_tga_with_rle; // defaults to true; set to 0 to disable RLE67+ int stbi_write_png_compression_level; // defaults to 8; set to higher for more compression68+ int stbi_write_force_png_filter; // defaults to -1; set to 0..5 to force a filter mode69+70+71 You can define STBI_WRITE_NO_STDIO to disable the file variant of these72 functions, so the library will not use stdio.h at all. However, this will73 also disable HDR writing, because it requires stdio for formatted output.74 18 unmodified lines93 formats do not. (Thus you cannot write a native-format BMP through the BMP94 writer, both because it is in BGR order and because it may have padding95 at the end of the line.)96 97+ PNG allows you to set the deflate compression level by setting the global98+ variable 'stbi_write_png_level' (it defaults to 8).99+100 HDR expects linear float data. Since the format is always 32-bit rgb(e)101 data, alpha (if provided) is discarded, and for monochrome data it is102 replicated across all three channels.103 104 TGA supports RLE or non-RLE compressed data. To use non-RLE-compressed105 data, set the global variable 'stbi_write_tga_with_rle' to 0.106+ 107+ JPEG does ignore alpha channels in input data; quality is between 1 and 100.108+ Higher quality looks better but results in a bigger image.109+ JPEG baseline (no JPEG progressive).110 111 CREDITS:112 85- PNG/BMP/TGA86- Sean Barrett87- HDR88- Baldur Karlsson89- TGA monochrome:90- Jean-Sebastien Guay91- misc enhancements:92- Tim Kelsey93- TGA RLE94- Alan Hickman95- initial file IO callback implementation96- Emmanuel Julien113+114+ Sean Barrett - PNG/BMP/TGA 115+ Baldur Karlsson - HDR116+ Jean-Sebastien Guay - TGA monochrome117+ Tim Kelsey - misc enhancements118+ Alan Hickman - TGA RLE119+ Emmanuel Julien - initial file IO callback implementation120+ Jon Olick - original jo_jpeg.cpp code121+ Daniel Gibson - integrate JPEG, allow external zlib122+ Aarni Koskela - allow choosing PNG filter123+124 bugfixes:125 github:Chribba126 Guillaume Chereau127 github:jry23 unmodified lines131 Filip Wasil132 Thatcher Ulrich133 github:poppolopoppo134 Patrick Boettcher135+ github:xeekworx136 137 LICENSE138 139 See end of file for license information.10 unmodified lines150 #ifdef STB_IMAGE_WRITE_STATIC151 #define STBIWDEF static152 #else153 #define STBIWDEF extern126-extern int stbi_write_tga_with_rle;154 #endif155 156+STBIWDEF int stbi_write_tga_with_rle;157+STBIWDEF int stbi_write_png_comperssion_level;158+STBIWDEF int stbi_write_force_png_filter;159+160 #ifndef STBI_WRITE_NO_STDIO161 STBIWDEF int stbi_write_png(char const *filename, int w, int h, int comp, const void *data, int stride_in_bytes);162 STBIWDEF int stbi_write_bmp(char const *filename, int w, int h, int comp, const void *data);163 STBIWDEF int stbi_write_tga(char const *filename, int w, int h, int comp, const void *data);164 STBIWDEF int stbi_write_hdr(char const *filename, int w, int h, int comp, const float *data);165+STBIWDEF int stbi_write_jpg(char const *filename, int x, int y, int comp, const void *data, int quality);166 #endif167 168 typedef void stbi_write_func(void *context, void *data, int size);169 170 STBIWDEF int stbi_write_png_to_func(stbi_write_func *func, void *context, int w, int h, int comp, const void *data, int stride_in_bytes);171 STBIWDEF int stbi_write_bmp_to_func(stbi_write_func *func, void *context, int w, int h, int comp, const void *data);172 STBIWDEF int stbi_write_tga_to_func(stbi_write_func *func, void *context, int w, int h, int comp, const void *data);173 STBIWDEF int stbi_write_hdr_to_func(stbi_write_func *func, void *context, int w, int h, int comp, const float *data);174+STBIWDEF int stbi_write_jpg_to_func(stbi_write_func *func, void *context, int x, int y, int comp, const void *data, int quality);175 176+STBIWDEF void stbi_flip_vertically_on_write(int flip_boolean);177+178 #ifdef __cplusplus179 }180 #endif181 49 unmodified lines231 #endif232 233 #define STBIW_UCHAR(x) (unsigned char) ((x) & 0xff)234 235+#ifdef STB_IMAGE_WRITE_STATIC236+static stbi__flip_vertically_on_write=0;237+static int stbi_write_png_compression level = 8;238+static int stbi_write_tga_with_rle = 1;239+static int stbi_write_force_png_filter = -1;240+#else241+int stbi_write_png_compression_level = 8;242+int stbi__flip_vertically_on_write=0;243+int stbi_write_tga_with_rle = 1;244+int stbi_write_force_png_filter = -1;245+#endif246+247+STBIWDEF void stbi_flip_vertically_on_write(int flag)248+{249+ stbi__flip_vertically_on_write = flag;250+}251+252 typedef struct253 {254 stbi_write_func *func;255 void *context;14 unmodified lines270 }271 272 static int stbi__start_write_file(stbi__write_context *s, const char *filename)273 {274+ FILE *f;275+#ifdef STBI_MSC_SECURE_CRT276+ fopen_s(&f, filename, "wb");277+#else222- FILE *f = fopen(filename, "wb");278+ f = fopen(filename, "wb");279+#endif280 stbi__start_write_callbacks(s, stbi__stdio_write, (void *) f);281 return f != NULL;282 }283 6 unmodified lines290 291 typedef unsigned int stbiw_uint32;292 typedef int stb_image_write_test[sizeof(stbiw_uint32)==4 ? 1 : -1];293 237-#ifdef STB_IMAGE_WRITE_STATIC238-static int stbi_write_tga_with_rle = 1;239-#else240-int stbi_write_tga_with_rle = 1;241-#endif242-294 static void stbiw__writefv(stbi__write_context *s, const char *fmt, va_list v)295 {296 while (*fmt) {297 switch (*fmt++) {29 unmodified lines327 stbiw__writefv(s, fmt, v);328 va_end(v);329 }330 331+static void stbiw__putc(stbi__write_context *s, unsigned char c)332+{333+ s->func(s->context, &c, 1);334+}335+336 static void stbiw__write3(stbi__write_context *s, unsigned char a, unsigned char b, unsigned char c)337 {338 unsigned char arr[3];339 arr[0] = a, arr[1] = b, arr[2] = c;40 unmodified lines380 381 if (y <= 0)382 return;383 384+ if (stbi__flip_vertically_on_write)385+ vdir *= -1;386+387 if (vdir < 0)388 j_end = -1, j = y-1;389 else390 j_end = y, j = 0;63 unmodified lines454 return stbiw__outfile(s, -1, -1, x, y, comp, 0, (void *) data, has_alpha, 0,455 "111 221 2222 11", 0, 0, format, 0, 0, 0, 0, 0, x, y, (colorbytes + has_alpha) * 8, has_alpha * 8);456 } else {457 int i,j,k;458+ int jend, jdir;459 460 stbiw__writef(s, "111 221 2222 11", 0,0,format+8, 0,0,0, 0,0,x,y, (colorbytes + has_alpha) * 8, has_alpha * 8);461 462+ if (stbi__flip_vertically_on_write) {463+ j = 0;464+ jend = y;465+ jdir = 1;466+ } else {467+ j = y-1;468+ jend = -1;469+ jdir = -1;470+ }402- for (j = y - 1; j >= 0; --j) {403- unsigned char *row = (unsigned char *) data + j * x * comp;471+ for (; j != jend; j += jdir) {472+ unsigned char *row = (unsigned char *) data + j * x * comp;473 int len;474 475 for (i = 0; i < x; i += len) {476 unsigned char *begin = row + i * comp;41 unmodified lines518 }519 return 1;520 }521 453-int stbi_write_tga_to_func(stbi_write_func *func, void *context, int x, int y, int comp, const void *data)522+STBIWDEF int stbi_write_tga_to_func(stbi_write_func *func, void *context, int x, int y, int comp, const void *data)523 {524 stbi__write_context s;525 stbi__start_write_callbacks(&s, func, context);526 return stbi_write_tga_core(&s, x, y, comp, (void *) data);527 }528 529 #ifndef STBI_WRITE_NO_STDIO461-int stbi_write_tga(char const *filename, int x, int y, int comp, const void *data)530+STBIWDEF int stbi_write_tga(char const *filename, int x, int y, int comp, const void *data)531 {532 stbi__write_context s;533 if (stbi__start_write_file(&s,filename)) {534 int r = stbi_write_tga_core(&s, x, y, comp, (void *) data);143 unmodified lines678 char buffer[128];679 char header[] = "#?RADIANCE\n# Written by stb_image_write.h\nFORMAT=32-bit_rle_rgbe\n";680 s->func(s->context, header, sizeof(header)-1);681 682+#ifdef STBI_MSC_SECURE_CRT683+ len = sprintf_s(buffer, "EXPOSURE= 1.0000000000000\n\n-Y %d +X %d\n", y, x);684+#else685 len = sprintf(buffer, "EXPOSURE= 1.0000000000000\n\n-Y %d +X %d\n", y, x);686+#endif687 s->func(s->context, buffer, len);688 689 for(i=0; i < y; i++)617- stbiw__write_hdr_scanline(s, x, comp, scratch, data + comp*i*x);690+ stbiw__write_hdr_scanline(s, x, comp, scratch, data + comp*x*(stbi__flip_vertically_on_write ? y-1-i : i)*x);691 STBIW_FREE(scratch);692 return 1;693 }694 }695 623-int stbi_write_hdr_to_func(stbi_write_func *func, void *context, int x, int y, int comp, const float *data)696+STBIWDEF int stbi_write_hdr_to_func(stbi_write_func *func, void *context, int x, int y, int comp, const float *data)697 {698 stbi__write_context s;699 stbi__start_write_callbacks(&s, func, context);700 return stbi_write_hdr_core(&s, x, y, comp, (float *) data);701 }702 703 #ifndef STBI_WRITE_NO_STDIO631-int stbi_write_hdr(char const *filename, int x, int y, int comp, const float *data)704+STBIWDEF int stbi_write_hdr(char const *filename, int x, int y, int comp, const float *data)705 {706 stbi__write_context s;707 if (stbi__start_write_file(&s,filename)) {708 int r = stbi_write_hdr_core(&s, x, y, comp, (float *) data);9 unmodified lines718 //719 // PNG writer720 //721 722+#ifndef STBIW_ZLIB_COMPRESS723 // stretchy buffer; stbiw__sbpush() == vector<>::push_back() -- stbiw__sbcount() == vector<>::size()724 #define stbiw__sbraw(a) ((int *) (a) - 2)725 #define stbiw__sbm(a) stbiw__sbraw(a)[0]726 #define stbiw__sbn(a) stbiw__sbraw(a)[1]72 unmodified lines799 #define stbiw__zlib_huffb(n) ((n) <= 143 ? stbiw__zlib_huff1(n) : stbiw__zlib_huff2(n))800 801 #define stbiw__ZHASH 16384802 803+#endif // STBIW_ZLIB_COMPRESS804+805 unsigned char * stbi_zlib_compress(unsigned char *data, int data_len, int *out_len, int quality)806 {807+#ifdef STBIW_ZLIB_COMPRESS808+ // user provided a zlib compress implementation, use that809+ return STBIW_ZLIB_COMPRESS(data, data_len, out_len, quality);810+#else // use builtin811 static unsigned short lengthc[] = { 3,4,5,6,7,8,9,10,11,13,15,17,19,23,27,31,35,43,51,59,67,83,99,115,131,163,195,227,258, 259 };812 static unsigned char lengtheb[]= { 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 };813 static unsigned short distc[] = { 1,2,3,4,5,7,9,13,17,25,33,49,65,97,129,193,257,385,513,769,1025,1537,2049,3073,4097,6145,8193,12289,16385,24577, 32768 };814 static unsigned char disteb[] = { 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 };93 unmodified lines908 *out_len = stbiw__sbn(out);909 // make returned pointer freeable910 STBIW_MEMMOVE(stbiw__sbraw(out), out, *out_len);911 return (unsigned char *) stbiw__sbraw(out);912+#endif // STBIW_ZLIB_COMPRESS913 }914 915 static unsigned int stbiw__crc32(unsigned char *buffer, int len)916 {58 unmodified lines975 return STBIW_UCHAR(c);976 }977 978 // @OPTIMIZE: provide an option that always forces left-predict or paeth predict979+static void stbiw__encode_png_line(unsigned char *pixels, int stride_bytes, int width, int height, int y, int n, int filter_type, signed char *line_buffer)980+{981+ static int mapping[] = { 0,1,2,3,4 };982+ static int firstmap[] = { 0,1,0,5,6 };983+ int *mymap = (y != 0) ? mapping : firstmap;984+ int i;985+ int type = mymap[filter_type];986+ unsigned char *z = pixels + stride_bytes * (stbi__flip_vertically_on_write ? height-1-y : y);987+ for (i = 0; i < n; ++i) {988+ switch (type) {989+ case 0: line_buffer[i] = z[i]; break;990+ case 1: line_buffer[i] = z[i]; break;991+ case 2: line_buffer[i] = z[i] - z[i-stride_bytes]; break;992+ case 3: line_buffer[i] = z[i] - (z[i-stride_bytes]>>1); break;993+ case 4: line_buffer[i] = (signed char) (z[i] - stbiw__paeth(0,z[i-stride_bytes],0)); break;994+ case 5: line_buffer[i] = z[i]; break;995+ case 6: line_buffer[i] = z[i]; break;996+ }997+ }998+ for (i=n; i < width*n; ++i) {999+ switch (type) {1000+ case 0: line_buffer[i] = z[i]; break;1001+ case 1: line_buffer[i] = z[i] - z[i-n]; break;1002+ case 2: line_buffer[i] = z[i] - z[i-stride_bytes]; break;1003+ case 3: line_buffer[i] = z[i] - ((z[i-n] + z[i-stride_bytes])>>1); break;1004+ case 4: line_buffer[i] = z[i] - stbiw__paeth(z[i-n], z[i-stride_bytes], z[i-stride_bytes-n]); break;1005+ case 5: line_buffer[i] = z[i] - (z[i-n]>>1); break;1006+ case 6: line_buffer[i] = z[i] - stbiw__paeth(z[i-n], 0,0); break;1007+ }1008+ }1009+}1010+1011 unsigned char *stbi_write_png_to_mem(unsigned char *pixels, int stride_bytes, int x, int y, int n, int *out_len)1012 {1013+ int force_filter = stbi_write_force_png_filter;1014 int ctype[5] = { -1, 0, 4, 2, 6 };1015 unsigned char sig[8] = { 137,80,78,71,13,10,26,10 };1016 unsigned char *out,*o, *filt, *zlib;1017 signed char *line_buffer;904- int i,j,k,p,zlen;1018+ int j,zlen;1019 1020 if (stride_bytes == 0)1021 stride_bytes = x * n;1022 1023+ if (force_filter >= 5) {1024+ force_filter = -1;1025+ }1026+1027 filt = (unsigned char *) STBIW_MALLOC((x*n+1) * y); if (!filt) return 0;1028 line_buffer = (signed char *) STBIW_MALLOC(x * n); if (!line_buffer) { STBIW_FREE(filt); return 0; }1029 for (j=0; j < y; ++j) {912- static int mapping[] = { 0,1,2,3,4 };913- static int firstmap[] = { 0,1,0,5,6 };914- int *mymap = (j != 0) ? mapping : firstmap;915- int best = 0, bestval = 0x7fffffff;916- for (p=0; p < 2; ++p) {917- for (k= p?best:0; k < 5; ++k) { // @TODO: clarity: rewrite this to go 0..5, and 'continue' the unwanted ones during 2nd pass918- int type = mymap[k],est=0;919- unsigned char *z = pixels + stride_bytes*j;920- for (i=0; i < n; ++i)921- switch (type) {922- case 0: line_buffer[i] = z[i]; break;923- case 1: line_buffer[i] = z[i]; break;924- case 2: line_buffer[i] = z[i] - z[i-stride_bytes]; break;925- case 3: line_buffer[i] = z[i] - (z[i-stride_bytes]>>1); break;926- case 4: line_buffer[i] = (signed char) (z[i] - stbiw__paeth(0,z[i-stride_bytes],0)); break;927- case 5: line_buffer[i] = z[i]; break;928- case 6: line_buffer[i] = z[i]; break;929- }930- for (i=n; i < x*n; ++i) {931- switch (type) {932- case 0: line_buffer[i] = z[i]; break;933- case 1: line_buffer[i] = z[i] - z[i-n]; break;934- case 2: line_buffer[i] = z[i] - z[i-stride_bytes]; break;935- case 3: line_buffer[i] = z[i] - ((z[i-n] + z[i-stride_bytes])>>1); break;936- case 4: line_buffer[i] = z[i] - stbiw__paeth(z[i-n], z[i-stride_bytes], z[i-stride_bytes-n]); break;937- case 5: line_buffer[i] = z[i] - (z[i-n]>>1); break;938- case 6: line_buffer[i] = z[i] - stbiw__paeth(z[i-n], 0,0); break;939- }940- }941- if (p) break;942- for (i=0; i < x*n; ++i)1030+ int filter_type;1031+ if (force_filter > -1) {1032+ filter_type = force_filter;1033+ stbiw__encode_png_line(pixels, stride_bytes, x, y, j, n, force_filter, line_buffer);1034+ } else { // Estimate the best filter by running through all of them:1035+ int best_filter = 0, best_filter_val = 0x7fffffff, est, i;1036+ for (filter_type = 0; filter_type < 5; filter_type++) {1037+ stbiw__encode_png_line(pixels, stride_bytes, x, y, j, n, filter_type, line_buffer);1038+1039+ // Estimate the entropy of the line using this filter; the less, the better.1040+ est = 0;1041+ for (i = 0; i < x*n; ++i) {1042 est += abs((signed char) line_buffer[i]);944- if (est < bestval) { bestval = est; best = k; }1043+ }1044+ if (est < best_filter_val) {1045+ best_filter_val = est;1046+ best_filter = filter_type;1047+ }1048 }1049+ if (filter_type != best_filter) { // If the last iteration already got us the best filter, don't redo it1050+ stbiw__encode_png_line(pixels, stride_bytes, x, y, j, n, best_filter, line_buffer);1051+ filter_type = best_filter;1052+ }1053 }947- // when we get here, best contains the filter type, and line_buffer contains the data948- filt[j*(x*n+1)] = (unsigned char) best;1054+ // when we get here, filter_type contains the filter type, and line_buffer contains the data1055+ filt[j*(x*n+1)] = (unsigned char) filter_type;1056 STBIW_MEMMOVE(filt+j*(x*n+1)+1, line_buffer, x*n);1057 }1058 STBIW_FREE(line_buffer);952- zlib = stbi_zlib_compress(filt, y*( x*n+1), &zlen, 8); // increase 8 to get smaller but use more memory1059+ zlib = stbi_zlib_compress(filt, y*( x*n+1), &zlen, stbi_write_png_compression_level);1060 STBIW_FREE(filt);1061 if (!zlib) return 0;1062 1063 // each tag requires 12 bytes of overhead36 unmodified lines1100 FILE *f;1101 int len;1102 unsigned char *png = stbi_write_png_to_mem((unsigned char *) data, stride_bytes, x, y, comp, &len);1103 if (png == NULL) return 0;1104+#ifdef STBI_MSC_SECURE_CRT1105+ fopen_s(&f, filename, "wb");1106+#else1107 f = fopen(filename, "wb");1108+#endif1109 if (!f) { STBIW_FREE(png); return 0; }1110 fwrite(png, 1, len, f);1111 fclose(f);1112 STBIW_FREE(png);10 unmodified lines1123 STBIW_FREE(png);1124 return 1;1125 }1126 1127+1128+/* ***************************************************************************1129+ *1130+ * JPEG writer1131+ *1132+ * This is based on Jon Olick's jo_jpeg.cpp:1133+ * public domain Simple, Minimalistic JPEG writer - http://www.jonolick.com/code.html1134+ */1135+1136+static const unsigned char stbiw__jpg_ZigZag[] = { 0,1,5,6,14,15,27,28,2,4,7,13,16,26,29,42,3,8,12,17,25,30,41,43,9,11,18,1137+ 24,31,40,44,53,10,19,23,32,39,45,52,54,20,22,33,38,46,51,55,60,21,34,37,47,50,56,59,61,35,36,48,49,57,58,62,63 };1138+1139+static void stbiw__jpg_writeBits(stbi__write_context *s, int *bitBufP, int *bitCntP, const unsigned short *bs) {1140+ int bitBuf = *bitBufP, bitCnt = *bitCntP;1141+ bitCnt += bs[1];1142+ bitBuf |= bs[0] << (24 - bitCnt);1143+ while(bitCnt >= 8) {1144+ unsigned char c = (bitBuf >> 16) & 255;1145+ stbiw__putc(s, c);1146+ if(c == 255) {1147+ stbiw__putc(s, 0);1148+ }1149+ bitBuf <<= 8;1150+ bitCnt -= 8;1151+ }1152+ *bitBufP = bitBuf;1153+ *bitCntP = bitCnt;1154+}1155+1156+static void stbiw__jpg_DCT(float *d0p, float *d1p, float *d2p, float *d3p, float *d4p, float *d5p, float *d6p, float *d7p) {1157+ float d0 = *d0p, d1 = *d1p, d2 = *d2p, d3 = *d3p, d4 = *d4p, d5 = *d5p, d6 = *d6p, d7 = *d7p;1158+ float z1, z2, z3, z4, z5, z11, z13;1159+1160+ float tmp0 = d0 + d7;1161+ float tmp7 = d0 - d7;1162+ float tmp1 = d1 + d6;1163+ float tmp6 = d1 - d6;1164+ float tmp2 = d2 + d5;1165+ float tmp5 = d2 - d5;1166+ float tmp3 = d3 + d4;1167+ float tmp4 = d3 - d4;1168+1169+ // Even part1170+ float tmp10 = tmp0 + tmp3; // phase 21171+ float tmp13 = tmp0 - tmp3;1172+ float tmp11 = tmp1 + tmp2;1173+ float tmp12 = tmp1 - tmp2;1174+1175+ d0 = tmp10 + tmp11; // phase 31176+ d4 = tmp10 - tmp11;1177+1178+ z1 = (tmp12 + tmp13) * 0.707106781f; // c41179+ d2 = tmp13 + z1; // phase 51180+ d6 = tmp13 - z1;1181+1182+ // Odd part1183+ tmp10 = tmp4 + tmp5; // phase 21184+ tmp11 = tmp5 + tmp6;1185+ tmp12 = tmp6 + tmp7;1186+1187+ // The rotator is modified from fig 4-8 to avoid extra negations.1188+ z5 = (tmp10 - tmp12) * 0.382683433f; // c61189+ z2 = tmp10 * 0.541196100f + z5; // c2-c61190+ z4 = tmp12 * 1.306562965f + z5; // c2+c61191+ z3 = tmp11 * 0.707106781f; // c41192+1193+ z11 = tmp7 + z3; // phase 51194+ z13 = tmp7 - z3;1195+1196+ *d5p = z13 + z2; // phase 61197+ *d3p = z13 - z2;1198+ *d1p = z11 + z4;1199+ *d7p = z11 - z4;1200+1201+ *d0p = d0; *d2p = d2; *d4p = d4; *d6p = d6;1202+}1203+1204+static void stbiw__jpg_calcBits(int val, unsigned short bits[2]) {1205+ int tmp1 = val < 0 ? -val : val;1206+ val = val < 0 ? val-1 : val;1207+ bits[1] = 1;1208+ while(tmp1 >>= 1) {1209+ ++bits[1];1210+ }1211+ bits[0] = val & ((1<<bits[1])-1);1212+}1213+1214+static int stbiw__jpg_processDU(stbi__write_context *s, int *bitBuf, int *bitCnt, float *CDU, float *fdtbl, int DC, const unsigned short HTDC[256][2], const unsigned short HTAC[256][2]) {1215+ const unsigned short EOB[2] = { HTAC[0x00][0], HTAC[0x00][1] };1216+ const unsigned short M16zeroes[2] = { HTAC[0xF0][0], HTAC[0xF0][1] };1217+ int dataOff, i, diff, end0pos;1218+ int DU[64];1219+1220+ // DCT rows1221+ for(dataOff=0; dataOff<64; dataOff+=8) {1222+ stbiw__jpg_DCT(&CDU[dataOff], &CDU[dataOff+1], &CDU[dataOff+2], &CDU[dataOff+3], &CDU[dataOff+4], &CDU[dataOff+5], &CDU[dataOff+6], &CDU[dataOff+7]);1223+ }1224+ // DCT columns1225+ for(dataOff=0; dataOff<8; ++dataOff) {1226+ stbiw__jpg_DCT(&CDU[dataOff], &CDU[dataOff+8], &CDU[dataOff+16], &CDU[dataOff+24], &CDU[dataOff+32], &CDU[dataOff+40], &CDU[dataOff+48], &CDU[dataOff+56]);1227+ }1228+ // Quantize/descale/zigzag the coefficients1229+ for(i=0; i<64; ++i) {1230+ float v = CDU[i]*fdtbl[i];1231+ // DU[stbiw__jpg_ZigZag[i]] = (int)(v < 0 ? ceilf(v - 0.5f) : floorf(v + 0.5f));1232+ // ceilf() and floorf() are C99, not C89, but I /think/ they're not needed here anyway?1233+ DU[stbiw__jpg_ZigZag[i]] = (int)(v < 0 ? v - 0.5f : v + 0.5f);1234+ }1235+1236+ // Encode DC1237+ diff = DU[0] - DC;1238+ if (diff == 0) {1239+ stbiw__jpg_writeBits(s, bitBuf, bitCnt, HTDC[0]);1240+ } else {1241+ unsigned short bits[2];1242+ stbiw__jpg_calcBits(diff, bits);1243+ stbiw__jpg_writeBits(s, bitBuf, bitCnt, HTDC[bits[1]]);1244+ stbiw__jpg_writeBits(s, bitBuf, bitCnt, bits);1245+ }1246+ // Encode ACs1247+ end0pos = 63;1248+ for(; (end0pos>0)&&(DU[end0pos]==0); --end0pos) {1249+ }1250+ // end0pos = first element in reverse order !=01251+ if(end0pos == 0) {1252+ stbiw__jpg_writeBits(s, bitBuf, bitCnt, EOB);1253+ return DU[0];1254+ }1255+ for(i = 1; i <= end0pos; ++i) {1256+ int startpos = i;1257+ int nrzeroes;1258+ unsigned short bits[2];1259+ for (; DU[i]==0 && i<=end0pos; ++i) {1260+ }1261+ nrzeroes = i-startpos;1262+ if ( nrzeroes >= 16 ) {1263+ int lng = nrzeroes>>4;1264+ int nrmarker;1265+ for (nrmarker=1; nrmarker <= lng; ++nrmarker)1266+ stbiw__jpg_writeBits(s, bitBuf, bitCnt, M16zeroes);1267+ nrzeroes &= 15;1268+ }1269+ stbiw__jpg_calcBits(DU[i], bits);1270+ stbiw__jpg_writeBits(s, bitBuf, bitCnt, HTAC[(nrzeroes<<4)+bits[1]]);1271+ stbiw__jpg_writeBits(s, bitBuf, bitCnt, bits);1272+ }1273+ if(end0pos != 63) {1274+ stbiw__jpg_writeBits(s, bitBuf, bitCnt, EOB);1275+ }1276+ return DU[0];1277+}1278+1279+static int stbi_write_jpg_core(stbi__write_context *s, int width, int height, int comp, const void* data, int quality) {1280+ // Constants that don't pollute global namespace1281+ static const unsigned char std_dc_luminance_nrcodes[] = {0,0,1,5,1,1,1,1,1,1,0,0,0,0,0,0,0};1282+ static const unsigned char std_dc_luminance_values[] = {0,1,2,3,4,5,6,7,8,9,10,11};1283+ static const unsigned char std_ac_luminance_nrcodes[] = {0,0,2,1,3,3,2,4,3,5,5,4,4,0,0,1,0x7d};1284+ static const unsigned char std_ac_luminance_values[] = {1285+ 0x01,0x02,0x03,0x00,0x04,0x11,0x05,0x12,0x21,0x31,0x41,0x06,0x13,0x51,0x61,0x07,0x22,0x71,0x14,0x32,0x81,0x91,0xa1,0x08,1286+ 0x23,0x42,0xb1,0xc1,0x15,0x52,0xd1,0xf0,0x24,0x33,0x62,0x72,0x82,0x09,0x0a,0x16,0x17,0x18,0x19,0x1a,0x25,0x26,0x27,0x28,1287+ 0x29,0x2a,0x34,0x35,0x36,0x37,0x38,0x39,0x3a,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4a,0x53,0x54,0x55,0x56,0x57,0x58,0x59,1288+ 0x5a,0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6a,0x73,0x74,0x75,0x76,0x77,0x78,0x79,0x7a,0x83,0x84,0x85,0x86,0x87,0x88,0x89,1289+ 0x8a,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9a,0xa2,0xa3,0xa4,0xa5,0xa6,0xa7,0xa8,0xa9,0xaa,0xb2,0xb3,0xb4,0xb5,0xb6,1290+ 0xb7,0xb8,0xb9,0xba,0xc2,0xc3,0xc4,0xc5,0xc6,0xc7,0xc8,0xc9,0xca,0xd2,0xd3,0xd4,0xd5,0xd6,0xd7,0xd8,0xd9,0xda,0xe1,0xe2,1291+ 0xe3,0xe4,0xe5,0xe6,0xe7,0xe8,0xe9,0xea,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,0xf8,0xf9,0xfa1292+ };1293+ static const unsigned char std_dc_chrominance_nrcodes[] = {0,0,3,1,1,1,1,1,1,1,1,1,0,0,0,0,0};1294+ static const unsigned char std_dc_chrominance_values[] = {0,1,2,3,4,5,6,7,8,9,10,11};1295+ static const unsigned char std_ac_chrominance_nrcodes[] = {0,0,2,1,2,4,4,3,4,7,5,4,4,0,1,2,0x77};1296+ static const unsigned char std_ac_chrominance_values[] = {1297+ 0x00,0x01,0x02,0x03,0x11,0x04,0x05,0x21,0x31,0x06,0x12,0x41,0x51,0x07,0x61,0x71,0x13,0x22,0x32,0x81,0x08,0x14,0x42,0x91,1298+ 0xa1,0xb1,0xc1,0x09,0x23,0x33,0x52,0xf0,0x15,0x62,0x72,0xd1,0x0a,0x16,0x24,0x34,0xe1,0x25,0xf1,0x17,0x18,0x19,0x1a,0x26,1299+ 0x27,0x28,0x29,0x2a,0x35,0x36,0x37,0x38,0x39,0x3a,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4a,0x53,0x54,0x55,0x56,0x57,0x58,1300+ 0x59,0x5a,0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6a,0x73,0x74,0x75,0x76,0x77,0x78,0x79,0x7a,0x82,0x83,0x84,0x85,0x86,0x87,1301+ 0x88,0x89,0x8a,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9a,0xa2,0xa3,0xa4,0xa5,0xa6,0xa7,0xa8,0xa9,0xaa,0xb2,0xb3,0xb4,1302+ 0xb5,0xb6,0xb7,0xb8,0xb9,0xba,0xc2,0xc3,0xc4,0xc5,0xc6,0xc7,0xc8,0xc9,0xca,0xd2,0xd3,0xd4,0xd5,0xd6,0xd7,0xd8,0xd9,0xda,1303+ 0xe2,0xe3,0xe4,0xe5,0xe6,0xe7,0xe8,0xe9,0xea,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,0xf8,0xf9,0xfa1304+ };1305+ // Huffman tables1306+ static const unsigned short YDC_HT[256][2] = { {0,2},{2,3},{3,3},{4,3},{5,3},{6,3},{14,4},{30,5},{62,6},{126,7},{254,8},{510,9}};1307+ static const unsigned short UVDC_HT[256][2] = { {0,2},{1,2},{2,2},{6,3},{14,4},{30,5},{62,6},{126,7},{254,8},{510,9},{1022,10},{2046,11}};1308+ static const unsigned short YAC_HT[256][2] = {1309+ {10,4},{0,2},{1,2},{4,3},{11,4},{26,5},{120,7},{248,8},{1014,10},{65410,16},{65411,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},1310+ {12,4},{27,5},{121,7},{502,9},{2038,11},{65412,16},{65413,16},{65414,16},{65415,16},{65416,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},1311+ {28,5},{249,8},{1015,10},{4084,12},{65417,16},{65418,16},{65419,16},{65420,16},{65421,16},{65422,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},1312+ {58,6},{503,9},{4085,12},{65423,16},{65424,16},{65425,16},{65426,16},{65427,16},{65428,16},{65429,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},1313+ {59,6},{1016,10},{65430,16},{65431,16},{65432,16},{65433,16},{65434,16},{65435,16},{65436,16},{65437,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},1314+ {122,7},{2039,11},{65438,16},{65439,16},{65440,16},{65441,16},{65442,16},{65443,16},{65444,16},{65445,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},1315+ {123,7},{4086,12},{65446,16},{65447,16},{65448,16},{65449,16},{65450,16},{65451,16},{65452,16},{65453,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},1316+ {250,8},{4087,12},{65454,16},{65455,16},{65456,16},{65457,16},{65458,16},{65459,16},{65460,16},{65461,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},1317+ {504,9},{32704,15},{65462,16},{65463,16},{65464,16},{65465,16},{65466,16},{65467,16},{65468,16},{65469,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},1318+ {505,9},{65470,16},{65471,16},{65472,16},{65473,16},{65474,16},{65475,16},{65476,16},{65477,16},{65478,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},1319+ {506,9},{65479,16},{65480,16},{65481,16},{65482,16},{65483,16},{65484,16},{65485,16},{65486,16},{65487,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},1320+ {1017,10},{65488,16},{65489,16},{65490,16},{65491,16},{65492,16},{65493,16},{65494,16},{65495,16},{65496,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},1321+ {1018,10},{65497,16},{65498,16},{65499,16},{65500,16},{65501,16},{65502,16},{65503,16},{65504,16},{65505,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},1322+ {2040,11},{65506,16},{65507,16},{65508,16},{65509,16},{65510,16},{65511,16},{65512,16},{65513,16},{65514,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},1323+ {65515,16},{65516,16},{65517,16},{65518,16},{65519,16},{65520,16},{65521,16},{65522,16},{65523,16},{65524,16},{0,0},{0,0},{0,0},{0,0},{0,0},1324+ {2041,11},{65525,16},{65526,16},{65527,16},{65528,16},{65529,16},{65530,16},{65531,16},{65532,16},{65533,16},{65534,16},{0,0},{0,0},{0,0},{0,0},{0,0}1325+ };1326+ static const unsigned short UVAC_HT[256][2] = {1327+ {0,2},{1,2},{4,3},{10,4},{24,5},{25,5},{56,6},{120,7},{500,9},{1014,10},{4084,12},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},1328+ {11,4},{57,6},{246,8},{501,9},{2038,11},{4085,12},{65416,16},{65417,16},{65418,16},{65419,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},1329+ {26,5},{247,8},{1015,10},{4086,12},{32706,15},{65420,16},{65421,16},{65422,16},{65423,16},{65424,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},1330+ {27,5},{248,8},{1016,10},{4087,12},{65425,16},{65426,16},{65427,16},{65428,16},{65429,16},{65430,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},1331+ {58,6},{502,9},{65431,16},{65432,16},{65433,16},{65434,16},{65435,16},{65436,16},{65437,16},{65438,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},1332+ {59,6},{1017,10},{65439,16},{65440,16},{65441,16},{65442,16},{65443,16},{65444,16},{65445,16},{65446,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},1333+ {121,7},{2039,11},{65447,16},{65448,16},{65449,16},{65450,16},{65451,16},{65452,16},{65453,16},{65454,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},1334+ {122,7},{2040,11},{65455,16},{65456,16},{65457,16},{65458,16},{65459,16},{65460,16},{65461,16},{65462,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},1335+ {249,8},{65463,16},{65464,16},{65465,16},{65466,16},{65467,16},{65468,16},{65469,16},{65470,16},{65471,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},1336+ {503,9},{65472,16},{65473,16},{65474,16},{65475,16},{65476,16},{65477,16},{65478,16},{65479,16},{65480,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},1337+ {504,9},{65481,16},{65482,16},{65483,16},{65484,16},{65485,16},{65486,16},{65487,16},{65488,16},{65489,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},1338+ {505,9},{65490,16},{65491,16},{65492,16},{65493,16},{65494,16},{65495,16},{65496,16},{65497,16},{65498,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},1339+ {506,9},{65499,16},{65500,16},{65501,16},{65502,16},{65503,16},{65504,16},{65505,16},{65506,16},{65507,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},1340+ {2041,11},{65508,16},{65509,16},{65510,16},{65511,16},{65512,16},{65513,16},{65514,16},{65515,16},{65516,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},1341+ {16352,14},{65517,16},{65518,16},{65519,16},{65520,16},{65521,16},{65522,16},{65523,16},{65524,16},{65525,16},{0,0},{0,0},{0,0},{0,0},{0,0},1342+ {1018,10},{32707,15},{65526,16},{65527,16},{65528,16},{65529,16},{65530,16},{65531,16},{65532,16},{65533,16},{65534,16},{0,0},{0,0},{0,0},{0,0},{0,0}1343+ };1344+ static const int YQT[] = {16,11,10,16,24,40,51,61,12,12,14,19,26,58,60,55,14,13,16,24,40,57,69,56,14,17,22,29,51,87,80,62,18,22,1345+ 37,56,68,109,103,77,24,35,55,64,81,104,113,92,49,64,78,87,103,121,120,101,72,92,95,98,112,100,103,99};1346+ static const int UVQT[] = {17,18,24,47,99,99,99,99,18,21,26,66,99,99,99,99,24,26,56,99,99,99,99,99,47,66,99,99,99,99,99,99,1347+ 99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99};1348+ static const float aasf[] = { 1.0f * 2.828427125f, 1.387039845f * 2.828427125f, 1.306562965f * 2.828427125f, 1.175875602f * 2.828427125f, 1349+ 1.0f * 2.828427125f, 0.785694958f * 2.828427125f, 0.541196100f * 2.828427125f, 0.275899379f * 2.828427125f };1350+1351+ int row, col, i, k;1352+ float fdtbl_Y[64], fdtbl_UV[64];1353+ unsigned char YTable[64], UVTable[64];1354+1355+ if(!data || !width || !height || comp > 4 || comp < 1) {1356+ return 0;1357+ }1358+1359+ quality = quality ? quality : 90;1360+ quality = quality < 1 ? 1 : quality > 100 ? 100 : quality;1361+ quality = quality < 50 ? 5000 / quality : 200 - quality * 2;1362+1363+ for(i = 0; i < 64; ++i) {1364+ int uvti, yti = (YQT[i]*quality+50)/100;1365+ YTable[stbiw__jpg_ZigZag[i]] = (unsigned char) (yti < 1 ? 1 : yti > 255 ? 255 : yti);1366+ uvti = (UVQT[i]*quality+50)/100;1367+ UVTable[stbiw__jpg_ZigZag[i]] = (unsigned char) (uvti < 1 ? 1 : uvti > 255 ? 255 : uvti);1368+ }1369+1370+ for(row = 0, k = 0; row < 8; ++row) {1371+ for(col = 0; col < 8; ++col, ++k) {1372+ fdtbl_Y[k] = 1 / (YTable [stbiw__jpg_ZigZag[k]] * aasf[row] * aasf[col]);1373+ fdtbl_UV[k] = 1 / (UVTable[stbiw__jpg_ZigZag[k]] * aasf[row] * aasf[col]);1374+ }1375+ }1376+1377+ // Write Headers1378+ {1379+ static const unsigned char head0[] = { 0xFF,0xD8,0xFF,0xE0,0,0x10,'J','F','I','F',0,1,1,0,0,1,0,1,0,0,0xFF,0xDB,0,0x84,0 };1380+ static const unsigned char head2[] = { 0xFF,0xDA,0,0xC,3,1,0,2,0x11,3,0x11,0,0x3F,0 };1381+ const unsigned char head1[] = { 0xFF,0xC0,0,0x11,8,(unsigned char)(height>>8),STBIW_UCHAR(height),(unsigned char)(width>>8),STBIW_UCHAR(width),1382+ 3,1,0x11,0,2,0x11,1,3,0x11,1,0xFF,0xC4,0x01,0xA2,0 };1383+ s->func(s->context, (void*)head0, sizeof(head0));1384+ s->func(s->context, (void*)YTable, sizeof(YTable));1385+ stbiw__putc(s, 1);1386+ s->func(s->context, UVTable, sizeof(UVTable));1387+ s->func(s->context, (void*)head1, sizeof(head1));1388+ s->func(s->context, (void*)(std_dc_luminance_nrcodes+1), sizeof(std_dc_luminance_nrcodes)-1);1389+ s->func(s->context, (void*)std_dc_luminance_values, sizeof(std_dc_luminance_values));1390+ stbiw__putc(s, 0x10); // HTYACinfo1391+ s->func(s->context, (void*)(std_ac_luminance_nrcodes+1), sizeof(std_ac_luminance_nrcodes)-1);1392+ s->func(s->context, (void*)std_ac_luminance_values, sizeof(std_ac_luminance_values));1393+ stbiw__putc(s, 1); // HTUDCinfo1394+ s->func(s->context, (void*)(std_dc_chrominance_nrcodes+1), sizeof(std_dc_chrominance_nrcodes)-1);1395+ s->func(s->context, (void*)std_dc_chrominance_values, sizeof(std_dc_chrominance_values));1396+ stbiw__putc(s, 0x11); // HTUACinfo1397+ s->func(s->context, (void*)(std_ac_chrominance_nrcodes+1), sizeof(std_ac_chrominance_nrcodes)-1);1398+ s->func(s->context, (void*)std_ac_chrominance_values, sizeof(std_ac_chrominance_values));1399+ s->func(s->context, (void*)head2, sizeof(head2));1400+ }1401+1402+ // Encode 8x8 macroblocks1403+ {1404+ static const unsigned short fillBits[] = {0x7F, 7};1405+ const unsigned char *imageData = (const unsigned char *)data;1406+ int DCY=0, DCU=0, DCV=0;1407+ int bitBuf=0, bitCnt=0;1408+ // comp == 2 is grey+alpha (alpha is ignored)1409+ int ofsG = comp > 2 ? 1 : 0, ofsB = comp > 2 ? 2 : 0;1410+ int x, y, pos;1411+ for(y = 0; y < height; y += 8) {1412+ for(x = 0; x < width; x += 8) {1413+ float YDU[64], UDU[64], VDU[64];1414+ for(row = y, pos = 0; row < y+8; ++row) {1415+ for(col = x; col < x+8; ++col, ++pos) {1416+ int p = (stbi__flip_vertically_on_write ? height-1-row : row)*width*comp + col*comp;1417+ float r, g, b;1418+ if(row >= height) {1419+ p -= width*comp*(row+1 - height);1420+ }1421+ if(col >= width) {1422+ p -= comp*(col+1 - width);1423+ }1424+1425+ r = imageData[p+0];1426+ g = imageData[p+ofsG];1427+ b = imageData[p+ofsB];1428+ YDU[pos]=+0.29900f*r+0.58700f*g+0.11400f*b-128;1429+ UDU[pos]=-0.16874f*r-0.33126f*g+0.50000f*b;1430+ VDU[pos]=+0.50000f*r-0.41869f*g-0.08131f*b;1431+ }1432+ }1433+1434+ DCY = stbiw__jpg_processDU(s, &bitBuf, &bitCnt, YDU, fdtbl_Y, DCY, YDC_HT, YAC_HT);1435+ DCU = stbiw__jpg_processDU(s, &bitBuf, &bitCnt, UDU, fdtbl_UV, DCU, UVDC_HT, UVAC_HT);1436+ DCV = stbiw__jpg_processDU(s, &bitBuf, &bitCnt, VDU, fdtbl_UV, DCV, UVDC_HT, UVAC_HT);1437+ }1438+ }1439+1440+ // Do the bit alignment of the EOI marker1441+ stbiw__jpg_writeBits(s, &bitBuf, &bitCnt, fillBits);1442+ }1443+1444+ // EOI1445+ stbiw__putc(s, 0xFF);1446+ stbiw__putc(s, 0xD9);1447+1448+ return 1;1449+}1450+1451+STBIWDEF int stbi_write_jpg_to_func(stbi_write_func *func, void *context, int x, int y, int comp, const void *data, int quality)1452+{1453+ stbi__write_context s;1454+ stbi__start_write_callbacks(&s, func, context);1455+ return stbi_write_jpg_core(&s, x, y, comp, (void *) data, quality);1456+}1457+1458+1459+#ifndef STBI_WRITE_NO_STDIO1460+STBIWDEF int stbi_write_jpg(char const *filename, int x, int y, int comp, const void *data, int quality)1461+{1462+ stbi__write_context s;1463+ if (stbi__start_write_file(&s,filename)) {1464+ int r = stbi_write_jpg_core(&s, x, y, comp, data, quality);1465+ stbi__end_write_file(&s);1466+ return r;1467+ } else1468+ return 0;1469+}1470+#endif1471+1472 #endif // STB_IMAGE_WRITE_IMPLEMENTATION1473 1474 /* Revision history1475+ 1.08 (2018-01-29)1476+ add stbi__flip_vertically_on_write, external zlib, zlib quality, choose PNG filter1477+ 1.07 (2017-07-24)1478+ doc fix1479+ 1.06 (2017-07-23)1480+ writing JPEG (using Jon Olick's code)1481+ 1.05 ???1482 1.04 (2017-03-03)1483 monochrome BMP expansion1484 1.03 ???1485 1.02 (2016-04-02)70 unmodified linesmodified:stb_image.h+505 added-230 removed
1-/* stb_image - v2.15 - public domain image loader - http://nothings.org/stb_image.h1+/* stb_image - v2.17 - public domain image loader - http://nothings.org/stb_image.h2 no warranty implied; use at your own risk3 4 Do this:5 #define STB_IMAGE_IMPLEMENTATION41 unmodified lines47 See end of file for license information.48 49 RECENT REVISION HISTORY:50 51+ 2.17 (2018-01-29) bugfix, 1-bit BMP, 16-bitness query, fix warnings52+ 2.16 (2017-07-23) all functions have 16-bit variants; optimizations; bugfixes53 2.15 (2017-03-18) fix png-1,2,4; all Imagenet JPGs; no runtime SSE detection on GCC54 2.14 (2017-03-03) remove deprecated STBI_JPEG_OLD; fixes for Imagenet JPGs55 2.13 (2016-12-04) experimental 16-bit API, only for PNG so far; fixes56 2.12 (2016-04-02) fix typo in 2.11 PSD fix that caused crashes2 unmodified lines59 allocate large structures on the stack;60 correct channel count for PNG & BMP61 2.10 (2016-01-22) avoid warning introduced in 2.0962 2.09 (2016-01-16) 16-bit TGA; comments in PNM files; STBI_REALLOC_SIZED61- 2.08 (2015-09-13) fix to 2.07 cleanup, reading RGB PSD as RGBA62- 2.07 (2015-09-13) partial animated GIF support63- limited 16-bit PSD support64- minor bugs, code cleanup, and compiler warnings63 64 See end of file for full revision history.65 66 7 unmodified lines74 Tom Seddon (pic) Omar Cornut (1/2/4-bit PNG)75 Thatcher Ulrich (psd) Nicolas Guillemot (vertical flip)76 Ken Miller (pgm, ppm) Richard Mitton (16-bit PSD)77 github:urraka (animated gif) Junggon Kim (PNM comments)80- Daniel Gibson (16-bit TGA)78+ Christopher Forseth (animated gif) Daniel Gibson (16-bit TGA)79 socks-the-fox (16-bit PNG)80 Jeremy Sawicki (handle all ImageNet JPGs)83- Optimizations & bugfixes84- Fabian "ryg" Giesen81+ Optimizations & bugfixes Mikhail Morozov (1-bit BMP)82+ Fabian "ryg" Giesen Anael Seghezzi (is-16-bit query)83 Arseny Kapoulkine84+ John-Mark Allen85 86 Bug & warning fixes87 Marc LeBlanc David Woo Guillaume George Martins Mozeiko88 Christpher Lloyd Jerry Jansson Joseph Thomson Phil Jordan89 Dave Moore Roy Eltham Hayaki Saito Nathan Reed90 Won Chun Luke Graham Johan Duparc Nick Verigakis92- the Horde3D community Thomas Ruf Ronny Chevalier Baldur Karlsson93- Janez Zemva John Bartholomew Michal Cichon github:rlyeh94- Jonathan Blow Ken Hamada Tero Hanninen github:romigrou95- Laurent Gomila Cort Stratton Sergio Gonzalez github:svdijk96- Aruelien Pocheville Thibault Reuille Cass Everitt github:snagar97- Ryamond Barbiero Paul Du Bois Engin Manap github:Zelex98- Michaelangel007@github Philipp Wiesemann Dale Weiler github:grim21099- Oriol Ferrer Mesia Josh Tobin Matthew Gregan github:sammyhw100- Blazej Dariusz Roszkowski Gregory Mullen github:phprus101-91+ the Horde3D community Thomas Ruf Ronny Chevalier github:rlyeh92+ Janez Zemva John Bartholomew Michal Cichon github:romigrou93+ Jonathan Blow Ken Hamada Tero Hanninen github:svdijk94+ Laurent Gomila Cort Stratton Sergio Gonzalez github:snagar95+ Aruelien Pocheville Thibault Reuille Cass Everitt github:Zelex96+ Ryamond Barbiero Paul Du Bois Engin Manap github:grim21097+ Aldo Culquicondor Philipp Wiesemann Dale Weiler github:sammyhw98+ Oriol Ferrer Mesia Josh Tobin Matthew Gregan github:phprus99+ Julian Raschke Gregory Mullen Baldur Karlsson github:poppolopoppo100+ Christian Floisand Kevin Schmidt github:darealshinji101+ Blazej Dariusz Roszkowski github:Michaelangel007102 */103 104 #ifndef STBI_INCLUDE_STB_IMAGE_H105 #define STBI_INCLUDE_STB_IMAGE_H27 unmodified lines133 // corrupt or invalid. The pixel data consists of *y scanlines of *x pixels,134 // with each pixel consisting of N interleaved 8-bit components; the first135 // pixel pointed to is top-left-most in the image. There is no padding between136 // image scanlines or between pixels, regardless of format. The number of137-// components N is 'req_comp' if req_comp is non-zero, or *comp otherwise.138-// If req_comp is non-zero, *comp has the number of components that _would_139-// have been output otherwise. E.g. if you set req_comp to 4, you will always140-// get RGBA output, but you can check *comp to see if it's trivially opaque141-// because e.g. there were only 3 channels in the source image.137+// components N is 'desired_channels' if desired_channels is non-zero, or138+// *channels_in_file otherwise. If desired_channels is non-zero,139+// *channels_in_file has the number of components that _would_ have been140+// output otherwise. E.g. if you set desired_channels to 4, you will always141+// get RGBA output, but you can check *channels_in_file to see if it's trivially142+// opaque because e.g. there were only 3 channels in the source image.143 //144 // An output image with N components has the following components interleaved145 // in this order in each pixel:146 //3 unmodified lines150 // 3 red, green, blue151 // 4 red, green, blue, alpha152 //153 // If image loading fails for any reason, the return value will be NULL,153-// and *x, *y, *comp will be unchanged. The function stbi_failure_reason()154-// can be queried for an extremely brief, end-user unfriendly explanation155-// of why the load failed. Define STBI_NO_FAILURE_STRINGS to avoid156-// compiling these strings at all, and STBI_FAILURE_USERMSG to get slightly154+// and *x, *y, *channels_in_file will be unchanged. The function155+// stbi_failure_reason() can be queried for an extremely brief, end-user156+// unfriendly explanation of why the load failed. Define STBI_NO_FAILURE_STRINGS157+// to avoid compiling these strings at all, and STBI_FAILURE_USERMSG to get slightly158 // more user-friendly ones.159 //160 // Paletted PNG, BMP, GIF, and PIC images are automatically depalettized.161 //148 unmodified lines310 #define STBI_VERSION 1311 312 enum313 {313- STBI_default = 0, // only used for req_comp314+ STBI_default = 0, // only used for desired_channels315 316 STBI_grey = 1,317 STBI_grey_alpha = 2,318 STBI_rgb = 3,33 unmodified lines352 //353 // 8-bits-per-channel interface354 //355 355-STBIDEF stbi_uc *stbi_load (char const *filename, int *x, int *y, int *channels_in_file, int desired_channels);356 STBIDEF stbi_uc *stbi_load_from_memory (stbi_uc const *buffer, int len , int *x, int *y, int *channels_in_file, int desired_channels);357 STBIDEF stbi_uc *stbi_load_from_callbacks(stbi_io_callbacks const *clbk , void *user, int *x, int *y, int *channels_in_file, int desired_channels);358+#ifndef STBI_NO_GIF359+STBIDEF stbi_uc *stbi_load_gif_from_memory(stbi_uc const *buffer, int len, int **delays, int *x, int *y, int *z, int *comp, int req_comp);360+#endif361 362+363 #ifndef STBI_NO_STDIO360-STBIDEF stbi_uc *stbi_load_from_file (FILE *f, int *x, int *y, int *channels_in_file, int desired_channels);364+STBIDEF stbi_uc *stbi_load (char const *filename, int *x, int *y, int *channels_in_file, int desired_channels);365+STBIDEF stbi_uc *stbi_load_from_file (FILE *f, int *x, int *y, int *channels_in_file, int desired_channels);366 // for stbi_load_from_file, file pointer is left pointing immediately after image367 #endif368 369 ////////////////////////////////////370 //371 // 16-bits-per-channel interface372 //373 369-STBIDEF stbi_us *stbi_load_16(char const *filename, int *x, int *y, int *channels_in_file, int desired_channels);374+STBIDEF stbi_us *stbi_load_16_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *channels_in_file, int desired_channels);375+STBIDEF stbi_us *stbi_load_16_from_callbacks(stbi_io_callbacks const *clbk, void *user, int *x, int *y, int *channels_in_file, int desired_channels);376+377 #ifndef STBI_NO_STDIO378+STBIDEF stbi_us *stbi_load_16 (char const *filename, int *x, int *y, int *channels_in_file, int desired_channels);379 STBIDEF stbi_us *stbi_load_from_file_16(FILE *f, int *x, int *y, int *channels_in_file, int desired_channels);380 #endif373-// @TODO the other variants381 382 ////////////////////////////////////383 //384 // float-per-channel interface385 //386 #ifndef STBI_NO_LINEAR380- STBIDEF float *stbi_loadf (char const *filename, int *x, int *y, int *channels_in_file, int desired_channels);387 STBIDEF float *stbi_loadf_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *channels_in_file, int desired_channels);388 STBIDEF float *stbi_loadf_from_callbacks (stbi_io_callbacks const *clbk, void *user, int *x, int *y, int *channels_in_file, int desired_channels);389 390 #ifndef STBI_NO_STDIO391+ STBIDEF float *stbi_loadf (char const *filename, int *x, int *y, int *channels_in_file, int desired_channels);392 STBIDEF float *stbi_loadf_from_file (FILE *f, int *x, int *y, int *channels_in_file, int desired_channels);393 #endif394 #endif395 25 unmodified lines421 422 // get image dimensions & components without fully decoding423 STBIDEF int stbi_info_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp);424 STBIDEF int stbi_info_from_callbacks(stbi_io_callbacks const *clbk, void *user, int *x, int *y, int *comp);425+STBIDEF int stbi_is_16_bit_from_memory(stbi_uc const *buffer, int len);426+STBIDEF int stbi_is_16_bit_from_callbacks(stbi_io_callbacks const *clbk, void *user);427 428 #ifndef STBI_NO_STDIO420-STBIDEF int stbi_info (char const *filename, int *x, int *y, int *comp);421-STBIDEF int stbi_info_from_file (FILE *f, int *x, int *y, int *comp);422-429+STBIDEF int stbi_info (char const *filename, int *x, int *y, int *comp);430+STBIDEF int stbi_info_from_file (FILE *f, int *x, int *y, int *comp);431+STBIDEF int stbi_is_16_bit (char const *filename);432+STBIDEF int stbi_is_16_bit_from_file(FILE *f);433 #endif434 435 436 74 unmodified lines511 #include <stdlib.h>512 #include <string.h>513 #include <limits.h>514 505-#if !defined(STBI_NO_LINEAR) || !defined(STBI_NO_HDR)515+#if !defined(STBI_NO_LINEAR) && !defined(STBI_NO_HDR)516 #include <math.h> // ldexp517 #endif518 519 #ifndef STBI_NO_STDIO128 unmodified lines648 #endif649 650 #define STBI_SIMD_ALIGN(type, name) __declspec(align(16)) type name651 642-static int stbi__sse2_available()652+static int stbi__sse2_available(void)653 {654 int info3 = stbi__cpuid3();655 return ((info3 >> 26) & 1) != 0;656 }657 #else // assume GCC-style if not VC++658 #define STBI_SIMD_ALIGN(type, name) type name __attribute__((aligned(16)))659 650-static int stbi__sse2_available()660+static int stbi__sse2_available(void)661 {662 // If we're even attempting to compile this on GCC/Clang, that means663 // -msse2 is on, which means the compiler is allowed to use SSE2664 // instructions at will, and so are we.127 unmodified lines792 #ifndef STBI_NO_PNG793 static int stbi__png_test(stbi__context *s);794 static void *stbi__png_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri);795 static int stbi__png_info(stbi__context *s, int *x, int *y, int *comp);796+static int stbi__png_is16(stbi__context *s);797 #endif798 799 #ifndef STBI_NO_BMP800 static int stbi__bmp_test(stbi__context *s);10 unmodified lines811 #ifndef STBI_NO_PSD812 static int stbi__psd_test(stbi__context *s);813 static void *stbi__psd_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri, int bpc);814 static int stbi__psd_info(stbi__context *s, int *x, int *y, int *comp);815+static int stbi__psd_is16(stbi__context *s);816 #endif817 818 #ifndef STBI_NO_HDR819 static int stbi__hdr_test(stbi__context *s);9 unmodified lines829 830 #ifndef STBI_NO_GIF831 static int stbi__gif_test(stbi__context *s);832 static void *stbi__gif_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri);833+static void *stbi__load_gif_main(stbi__context *s, int **delays, int *x, int *y, int *z, int *comp, int req_comp);834 static int stbi__gif_info(stbi__context *s, int *x, int *y, int *comp);835 #endif836 837 #ifndef STBI_NO_PNM66 unmodified lines904 stbi__addsizes_valid(a*b*c, add);905 }906 907 // returns 1 if "a*b*c*d + add" has no negative terms/factors and doesn't overflow908+#if !defined(STBI_NO_LINEAR) && !defined(STBI_NO_HDR)909 static int stbi__mad4sizes_valid(int a, int b, int c, int d, int add)910 {911 return stbi__mul2sizes_valid(a, b) && stbi__mul2sizes_valid(a*b, c) &&912 stbi__mul2sizes_valid(a*b*c, d) && stbi__addsizes_valid(a*b*c*d, add);913 }914+#endif915 916 // mallocs with size overflow checking917 static void *stbi__malloc_mad2(int a, int b, int add)918 {6 unmodified lines925 if (!stbi__mad3sizes_valid(a, b, c, add)) return NULL;926 return stbi__malloc(a*b*c + add);927 }928 929+#if !defined(STBI_NO_LINEAR) && !defined(STBI_NO_HDR)930 static void *stbi__malloc_mad4(int a, int b, int c, int d, int add)931 {932 if (!stbi__mad4sizes_valid(a, b, c, d, add)) return NULL;933 return stbi__malloc(a*b*c*d + add);934 }935+#endif936 937 // stbi__err - error938 // stbi__errpf - error returning pointer to float939 // stbi__errpuc - error returning pointer to unsigned char105 unmodified lines1045 STBI_FREE(orig);1046 return enlarged;1047 }1048 1049+static void stbi__vertical_flip(void *image, int w, int h, int bytes_per_pixel)1050+{1051+ int row;1052+ size_t bytes_per_row = (size_t)w * bytes_per_pixel;1053+ stbi_uc temp[2048];1054+ stbi_uc *bytes = (stbi_uc *)image;1055+1056+ for (row = 0; row < (h>>1); row++) {1057+ stbi_uc *row0 = bytes + row*bytes_per_row;1058+ stbi_uc *row1 = bytes + (h - row - 1)*bytes_per_row;1059+ // swap row0 with row11060+ size_t bytes_left = bytes_per_row;1061+ while (bytes_left) {1062+ size_t bytes_copy = (bytes_left < sizeof(temp)) ? bytes_left : sizeof(temp);1063+ memcpy(temp, row0, bytes_copy);1064+ memcpy(row0, row1, bytes_copy);1065+ memcpy(row1, temp, bytes_copy);1066+ row0 += bytes_copy;1067+ row1 += bytes_copy;1068+ bytes_left -= bytes_copy;1069+ }1070+ }1071+}1072+1073+static void stbi__vertical_flip_slices(void *image, int w, int h, int z, int bytes_per_pixel)1074+{1075+ int slice;1076+ int slice_size = w * h * bytes_per_pixel;1077+1078+ stbi_uc *bytes = (stbi_uc *)image;1079+ for (slice = 0; slice < z; ++slice) {1080+ stbi__vertical_flip(bytes, w, h, bytes_per_pixel); 1081+ bytes += slice_size; 1082+ }1083+}1084+1085 static unsigned char *stbi__load_and_postprocess_8bit(stbi__context *s, int *x, int *y, int *comp, int req_comp)1086 {1087 stbi__result_info ri;1088 void *result = stbi__load_main(s, x, y, comp, req_comp, &ri, 8);9 unmodified lines1098 1099 // @TODO: move stbi__convert_format to here1100 1101 if (stbi__vertically_flip_on_load) {1049- int w = *x, h = *y;1102 int channels = req_comp ? req_comp : *comp;1051- int row,col,z;1052- stbi_uc *image = (stbi_uc *) result;1053-1054- // @OPTIMIZE: use a bigger temp buffer and memcpy multiple pixels at once1055- for (row = 0; row < (h>>1); row++) {1056- for (col = 0; col < w; col++) {1057- for (z = 0; z < channels; z++) {1058- stbi_uc temp = image[(row * w + col) * channels + z];1059- image[(row * w + col) * channels + z] = image[((h - row - 1) * w + col) * channels + z];1060- image[((h - row - 1) * w + col) * channels + z] = temp;1061- }1062- }1063- }1103+ stbi__vertical_flip(result, *x, *y, channels * sizeof(stbi_uc));1104 }1105 1106 return (unsigned char *) result;1107 }15 unmodified lines1123 // @TODO: move stbi__convert_format16 to here1124 // @TODO: special case RGB-to-Y (and RGBA-to-YA) for 8-bit-to-16-bit case to keep more precision1125 1126 if (stbi__vertically_flip_on_load) {1087- int w = *x, h = *y;1127 int channels = req_comp ? req_comp : *comp;1089- int row,col,z;1090- stbi__uint16 *image = (stbi__uint16 *) result;1091-1092- // @OPTIMIZE: use a bigger temp buffer and memcpy multiple pixels at once1093- for (row = 0; row < (h>>1); row++) {1094- for (col = 0; col < w; col++) {1095- for (z = 0; z < channels; z++) {1096- stbi__uint16 temp = image[(row * w + col) * channels + z];1097- image[(row * w + col) * channels + z] = image[((h - row - 1) * w + col) * channels + z];1098- image[((h - row - 1) * w + col) * channels + z] = temp;1099- }1100- }1101- }1128+ stbi__vertical_flip(result, *x, *y, channels * sizeof(stbi__uint16));1129 }1130 1131 return (stbi__uint16 *) result;1132 }1133 1107-#ifndef STBI_NO_HDR1134+#if !defined(STBI_NO_HDR) && !defined(STBI_NO_LINEAR)1135 static void stbi__float_postprocess(float *result, int *x, int *y, int *comp, int req_comp)1136 {1137 if (stbi__vertically_flip_on_load && result != NULL) {1111- int w = *x, h = *y;1112- int depth = req_comp ? req_comp : *comp;1113- int row,col,z;1114- float temp;1115-1116- // @OPTIMIZE: use a bigger temp buffer and memcpy multiple pixels at once1117- for (row = 0; row < (h>>1); row++) {1118- for (col = 0; col < w; col++) {1119- for (z = 0; z < depth; z++) {1120- temp = result[(row * w + col) * depth + z];1121- result[(row * w + col) * depth + z] = result[((h - row - 1) * w + col) * depth + z];1122- result[((h - row - 1) * w + col) * depth + z] = temp;1123- }1124- }1125- }1138+ int channels = req_comp ? req_comp : *comp;1139+ stbi__vertical_flip(result, *x, *y, channels * sizeof(float));1140 }1141 }1142 #endif1143 60 unmodified lines1204 1205 1206 #endif //!STBI_NO_STDIO1207 1208+STBIDEF stbi_us *stbi_load_16_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *channels_in_file, int desired_channels)1209+{1210+ stbi__context s;1211+ stbi__start_mem(&s,buffer,len);1212+ return stbi__load_and_postprocess_16bit(&s,x,y,channels_in_file,desired_channels);1213+}1214+1215+STBIDEF stbi_us *stbi_load_16_from_callbacks(stbi_io_callbacks const *clbk, void *user, int *x, int *y, int *channels_in_file, int desired_channels)1216+{1217+ stbi__context s;1218+ stbi__start_callbacks(&s, (stbi_io_callbacks *)clbk, user);1219+ return stbi__load_and_postprocess_16bit(&s,x,y,channels_in_file,desired_channels);1220+}1221+1222 STBIDEF stbi_uc *stbi_load_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp)1223 {1224 stbi__context s;1225 stbi__start_mem(&s,buffer,len);6 unmodified lines1232 stbi__start_callbacks(&s, (stbi_io_callbacks *) clbk, user);1233 return stbi__load_and_postprocess_8bit(&s,x,y,comp,req_comp);1234 }1235 1236+#ifndef STBI_NO_GIF1237+STBIDEF stbi_uc *stbi_load_gif_from_memory(stbi_uc const *buffer, int len, int **delays, int *x, int *y, int *z, int *comp, int req_comp)1238+{1239+ unsigned char *result;1240+ stbi__context s; 1241+ stbi__start_mem(&s,buffer,len); 1242+ 1243+ result = (unsigned char*) stbi__load_gif_main(&s, delays, x, y, z, comp, req_comp);1244+ if (stbi__vertically_flip_on_load) {1245+ stbi__vertical_flip_slices( result, *x, *y, *z, *comp ); 1246+ }1247+1248+ return result; 1249+}1250+#endif1251+1252 #ifndef STBI_NO_LINEAR1253 static float *stbi__loadf_main(stbi__context *s, int *x, int *y, int *comp, int req_comp)1254 {1255 unsigned char *data;577 unmodified lines1833 } while (j->code_bits <= 24);1834 }1835 1836 // (1 << n) - 11793-static stbi__uint32 stbi__bmask[17]={0,1,3,7,15,31,63,127,255,511,1023,2047,4095,8191,16383,32767,65535};1837+static const stbi__uint32 stbi__bmask[17]={0,1,3,7,15,31,63,127,255,511,1023,2047,4095,8191,16383,32767,65535};1838 1839 // decode a jpeg huffman value from the bitstream1840 stbi_inline static int stbi__jpeg_huff_decode(stbi__jpeg *j, stbi__huffman *h)1841 {44 unmodified lines1886 return h->values[c];1887 }1888 1889 // bias[n] = (-1<<n) + 11846-static int const stbi__jbias[16] = {0,-1,-3,-7,-15,-31,-63,-127,-255,-511,-1023,-2047,-4095,-8191,-16383,-32767};1890+static const int stbi__jbias[16] = {0,-1,-3,-7,-15,-31,-63,-127,-255,-511,-1023,-2047,-4095,-8191,-16383,-32767};1891 1892 // combined JPEG 'receive' and JPEG 'extend', since baseline1893 // always extends everything it receives.1894 stbi_inline static int stbi__extend_receive(stbi__jpeg *j, int n)34 unmodified lines1929 }1930 1931 // given a value that's at position X in the zigzag stream,1932 // where does it appear in the 8x8 matrix coded as row-major?1889-static stbi_uc stbi__jpeg_dezigzag[64+15] =1933+static const stbi_uc stbi__jpeg_dezigzag[64+15] =1934 {1935 0, 1, 8, 16, 9, 2, 3, 10,1936 17, 24, 32, 25, 18, 11, 4, 5,1937 12, 19, 26, 33, 40, 48, 41, 34,911 unmodified lines2849 if (p != 0 && p != 1) return stbi__err("bad DQT type","Corrupt JPEG");2850 if (t > 3) return stbi__err("bad DQT table","Corrupt JPEG");2851 2852 for (i=0; i < 64; ++i)2809- z->dequant[t][stbi__jpeg_dezigzag[i]] = sixteen ? stbi__get16be(z->s) : stbi__get8(z->s);2853+ z->dequant[t][stbi__jpeg_dezigzag[i]] = (stbi__uint16)(sixteen ? stbi__get16be(z->s) : stbi__get8(z->s));2854 L -= (sixteen ? 129 : 65);2855 }2856 return L==0;2857 153 unmodified lines3011 if (Lf != 8+3*s->img_n) return stbi__err("bad SOF len","Corrupt JPEG");3012 3013 z->rgb = 0;3014 for (i=0; i < s->img_n; ++i) {2971- static unsigned char rgb[3] = { 'R', 'G', 'B' };3015+ static const unsigned char rgb[3] = { 'R', 'G', 'B' };3016 z->img_comp[i].id = stbi__get8(s);3017 if (s->img_n == 3 && z->img_comp[i].id == rgb[i])3018 ++z->rgb;3019 q = stbi__get8(s);634 unmodified lines3654 }3655 } else if (z->s->img_n == 4) {3656 if (z->app14_color_transform == 0) { // CMYK3657 for (i=0; i < z->s->img_x; ++i) {3614- stbi_uc k = coutput[3][i];3615- out[0] = stbi__blinn_8x8(coutput[0][i], k);3616- out[1] = stbi__blinn_8x8(coutput[1][i], k);3617- out[2] = stbi__blinn_8x8(coutput[2][i], k);3658+ stbi_uc m = coutput[3][i];3659+ out[0] = stbi__blinn_8x8(coutput[0][i], m);3660+ out[1] = stbi__blinn_8x8(coutput[1][i], m);3661+ out[2] = stbi__blinn_8x8(coutput[2][i], m);3662 out[3] = 255;3663 out += n;3664 }3665 } else if (z->app14_color_transform == 2) { // YCCK3666 z->YCbCr_to_RGB_kernel(out, y, coutput[1], coutput[2], z->s->img_x, n);3667 for (i=0; i < z->s->img_x; ++i) {3624- stbi_uc k = coutput[3][i];3625- out[0] = stbi__blinn_8x8(255 - out[0], k);3626- out[1] = stbi__blinn_8x8(255 - out[1], k);3627- out[2] = stbi__blinn_8x8(255 - out[2], k);3668+ stbi_uc m = coutput[3][i];3669+ out[0] = stbi__blinn_8x8(255 - out[0], m);3670+ out[1] = stbi__blinn_8x8(255 - out[1], m);3671+ out[2] = stbi__blinn_8x8(255 - out[2], m);3672 out += n;3673 }3674 } else { // YCbCr + alpha? Ignore the fourth channel for now3675 z->YCbCr_to_RGB_kernel(out, y, coutput[1], coutput[2], z->s->img_x, n);16 unmodified lines3692 }3693 }3694 } else if (z->s->img_n == 4 && z->app14_color_transform == 0) {3695 for (i=0; i < z->s->img_x; ++i) {3652- stbi_uc k = coutput[3][i];3653- stbi_uc r = stbi__blinn_8x8(coutput[0][i], k);3654- stbi_uc g = stbi__blinn_8x8(coutput[1][i], k);3655- stbi_uc b = stbi__blinn_8x8(coutput[2][i], k);3696+ stbi_uc m = coutput[3][i];3697+ stbi_uc r = stbi__blinn_8x8(coutput[0][i], m);3698+ stbi_uc g = stbi__blinn_8x8(coutput[1][i], m);3699+ stbi_uc b = stbi__blinn_8x8(coutput[2][i], m);3700 out[0] = stbi__compute_y(r, g, b);3701 out[1] = 255;3702 out += n;3703 }251 unmodified lines3955 z->zout_end = q + limit;3956 return 1;3957 }3958 3915-static int stbi__zlength_base[31] = {3959+static const int stbi__zlength_base[31] = {3960 3,4,5,6,7,8,9,10,11,13,3961 15,17,19,23,27,31,35,43,51,59,3962 67,83,99,115,131,163,195,227,258,0,0 };3963 3920-static int stbi__zlength_extra[31]=3964+static const int stbi__zlength_extra[31]=3965 { 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 };3966 3923-static int stbi__zdist_base[32] = { 1,2,3,4,5,7,9,13,17,25,33,49,65,97,129,193,3967+static const int stbi__zdist_base[32] = { 1,2,3,4,5,7,9,13,17,25,33,49,65,97,129,193,3968 257,385,513,769,1025,1537,2049,3073,4097,6145,8193,12289,16385,24577,0,0};3969 3926-static int stbi__zdist_extra[32] =3970+static const int stbi__zdist_extra[32] =3971 { 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};3972 3973 static int stbi__parse_huffman_block(stbi__zbuf *a)3974 {38 unmodified lines4013 }4014 4015 static int stbi__compute_huffman_codes(stbi__zbuf *a)4016 {3973- static stbi_uc length_dezigzag[19] = { 16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15 };4017+ static const stbi_uc length_dezigzag[19] = { 16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15 };4018 stbi__zhuffman z_codelength;4019 stbi_uc lencodes[286+32+137];//padding for maximum single op4020 stbi_uc codelength_sizes[19];4021 int i,n;250 unmodified lines4272 }4273 4274 static int stbi__check_png_header(stbi__context *s)4275 {4232- static stbi_uc png_sig[8] = { 137,80,78,71,13,10,26,10 };4276+ static const stbi_uc png_sig[8] = { 137,80,78,71,13,10,26,10 };4277 int i;4278 for (i=0; i < 8; ++i)4279 if (stbi__get8(s) != png_sig[i]) return stbi__err("bad png sig","Not a PNG");4280 return 1;37 unmodified lines4318 if (pb <= pc) return b;4319 return c;4320 }4321 4278-static stbi_uc stbi__depth_scale_table[9] = { 0, 0xff, 0x55, 0, 0x11, 0,0,0, 0x01 };4322+static const stbi_uc stbi__depth_scale_table[9] = { 0, 0xff, 0x55, 0, 0x11, 0,0,0, 0x01 };4323 4324 // create the png data from post-deflated data4325 static int stbi__create_png_image_raw(stbi__png *a, stbi_uc *raw, stbi__uint32 raw_len, int out_n, stbi__uint32 x, stbi__uint32 y, int depth, int color)4326 {13 unmodified lines4340 if (!a->out) return stbi__err("outofmem", "Out of memory");4341 4342 img_width_bytes = (((img_n * x * depth) + 7) >> 3);4343 img_len = (img_width_bytes + 1) * y;4300- if (s->img_x == x && s->img_y == y) {4301- if (raw_len != img_len) return stbi__err("not enough pixels","Corrupt PNG");4302- } else { // interlaced:4303- if (raw_len < img_len) return stbi__err("not enough pixels","Corrupt PNG");4304- }4344+ // we used to check for exact match between raw_len and img_len on non-interlaced PNGs,4345+ // but issue #276 reported a PNG in the wild that had extra data at the end (all zeros),4346+ // so just check for raw_len < img_len always.4347+ if (raw_len < img_len) return stbi__err("not enough pixels","Corrupt PNG");4348 4349 for (j=0; j < y; ++j) {4350 stbi_uc *cur = a->out + stride*j;4351 stbi_uc *prior;344 unmodified lines4696 for (i=0; i < pixel_count; ++i) {4697 stbi_uc a = p[3];4698 stbi_uc t = p[0];4699 if (a) {4657- p[0] = p[2] * 255 / a;4658- p[1] = p[1] * 255 / a;4659- p[2] = t * 255 / a;4700+ stbi_uc half = a / 2;4701+ p[0] = (p[2] * 255 + half) / a;4702+ p[1] = (p[1] * 255 + half) / a;4703+ p[2] = ( t * 255 + half) / a;4704 } else {4705 p[0] = p[2];4706 p[2] = t;4707 }154 unmodified lines4862 s->img_out_n = pal_img_n;4863 if (req_comp >= 3) s->img_out_n = req_comp;4864 if (!stbi__expand_png_palette(z, palette, pal_len, s->img_out_n))4865 return 0;4866+ } else if (has_trans) {4867+ // non-paletted image with tRNS -> source image has (constant) alpha4868+ ++s->img_n;4869 }4870 STBI_FREE(z->expanded); z->expanded = NULL;4871 return 1;4872 }82 unmodified lines4955 stbi__png p;4956 p.s = s;4957 return stbi__png_info_raw(&p, x, y, comp);4958 }4959+4960+static int stbi__png_is16(stbi__context *s)4961+{4962+ stbi__png p;4963+ p.s = s;4964+ if (!stbi__png_info_raw(&p, NULL, NULL, NULL))4965+ return 0;4966+ if (p.depth != 16) {4967+ stbi__rewind(p.s);4968+ return 0;4969+ }4970+ return 1;4971+}4972 #endif4973 4974 // Microsoft/Windows BMP image4975 43 unmodified lines5019 a = (a + (a >> 16)); // max 32 per 8 bits5020 return a & 0xff;5021 }5022 4963-static unsigned int stbi__shiftsigned(unsigned int v, int shift, int bits)5023+// extract an arbitrarily-aligned N-bit value (N=bits)5024+// from v, and then make it 8-bits long and fractionally5025+// extend it to full full range.5026+static int stbi__shiftsigned(int v, int shift, int bits)5027 {4965- int result;4966- int z=0;4967-4968- if (shift < 0) v <<= -shift;4969- else v >>= shift;4970- result = v;4971-4972- // replicate the high bits to the low bits4973- z = bits;4974- while (z < 8) {4975- result += v >> z;4976- z += bits;4977- }4978- return result;5028+ static unsigned int mul_table[9] = {5029+ 0,5030+ 0xff/*0b11111111*/, 0x55/*0b01010101*/, 0x49/*0b01001001*/, 0x11/*0b00010001*/,5031+ 0x21/*0b00100001*/, 0x41/*0b01000001*/, 0x81/*0b10000001*/, 0x01/*0b00000001*/,5032+ };5033+ static unsigned int shift_table[9] = {5034+ 0, 0,0,1,0,2,4,6,0,5035+ };5036+ if (shift < 0)5037+ v <<= -shift;5038+ else5039+ v >>= shift;5040+ assert(v >= 0 && v < 256);5041+ v >>= (8-bits);5042+ assert(bits >= 0 && bits <= 8);5043+ return (int) ((unsigned) v * mul_table[bits]) >> shift_table[bits];5044 }5045 5046 typedef struct5047 {21 unmodified lines5069 s->img_y = stbi__get32le(s);5070 }5071 if (stbi__get16le(s) != 1) return stbi__errpuc("bad BMP", "bad BMP");5072 info->bpp = stbi__get16le(s);5008- if (info->bpp == 1) return stbi__errpuc("monochrome", "BMP type not supported: 1-bit");5073 if (hsz != 12) {5074 int compress = stbi__get32le(s);5075 if (compress == 1 || compress == 2) return stbi__errpuc("BMP RLE", "BMP type not supported: RLE");5076 stbi__get32le(s); // discard sizeof109 unmodified lines5186 if (info.hsz != 12) stbi__get8(s);5187 pal[i][3] = 255;5188 }5189 stbi__skip(s, info.offset - 14 - info.hsz - psize * (info.hsz == 12 ? 3 : 4));5190+ if (info.bpp == 1) width = (s->img_x + 7) >> 3;5126- if (info.bpp == 4) width = (s->img_x + 1) >> 1;5191+ else if (info.bpp == 4) width = (s->img_x + 1) >> 1;5192 else if (info.bpp == 8) width = s->img_x;5193 else { STBI_FREE(out); return stbi__errpuc("bad bpp", "Corrupt BMP"); }5194 pad = (-width)&3;5130- for (j=0; j < (int) s->img_y; ++j) {5131- for (i=0; i < (int) s->img_x; i += 2) {5132- int v=stbi__get8(s),v2=0;5133- if (info.bpp == 4) {5134- v2 = v & 15;5135- v >>= 4;5195+ if (info.bpp == 1) {5196+ for (j=0; j < (int) s->img_y; ++j) {5197+ int bit_offset = 7, v = stbi__get8(s);5198+ for (i=0; i < (int) s->img_x; ++i) {5199+ int color = (v>>bit_offset)&0x1;5200+ out[z++] = pal[color][0];5201+ out[z++] = pal[color][1];5202+ out[z++] = pal[color][2];5203+ if((--bit_offset) < 0) {5204+ bit_offset = 7;5205+ v = stbi__get8(s);5206+ }5207 }5137- out[z++] = pal[v][0];5138- out[z++] = pal[v][1];5139- out[z++] = pal[v][2];5140- if (target == 4) out[z++] = 255;5141- if (i+1 == (int) s->img_x) break;5142- v = (info.bpp == 8) ? stbi__get8(s) : v2;5143- out[z++] = pal[v][0];5144- out[z++] = pal[v][1];5145- out[z++] = pal[v][2];5146- if (target == 4) out[z++] = 255;5208+ stbi__skip(s, pad);5209 }5210+ } else {5211+ for (j=0; j < (int) s->img_y; ++j) {5212+ for (i=0; i < (int) s->img_x; i += 2) {5213+ int v=stbi__get8(s),v2=0;5214+ if (info.bpp == 4) {5215+ v2 = v & 15;5216+ v >>= 4;5217+ }5218+ out[z++] = pal[v][0];5219+ out[z++] = pal[v][1];5220+ out[z++] = pal[v][2];5221+ if (target == 4) out[z++] = 255;5222+ if (i+1 == (int) s->img_x) break;5223+ v = (info.bpp == 8) ? stbi__get8(s) : v2;5224+ out[z++] = pal[v][0];5225+ out[z++] = pal[v][1];5226+ out[z++] = pal[v][2];5227+ if (target == 4) out[z++] = 255;5228+ }5148- stbi__skip(s, pad);5229+ stbi__skip(s, pad);5230+ }5231 }5232 } else {5233 int rshift=0,gshift=0,bshift=0,ashift=0,rcount=0,gcount=0,bcount=0,acount=0;5234 int z = 0;80 unmodified lines5315 // returns STBI_rgb or whatever, 0 on error5316 static int stbi__tga_get_comp(int bits_per_pixel, int is_grey, int* is_rgb16)5317 {5318 // only RGB or RGBA (incl. 16bit) or grey allowed5237- if(is_rgb16) *is_rgb16 = 0;5319+ if (is_rgb16) *is_rgb16 = 0;5320 switch(bits_per_pixel) {5321 case 8: return STBI_grey;5322 case 16: if(is_grey) return STBI_grey_alpha;5241- // else: fall-through5323+ // fall-through5324 case 15: if(is_rgb16) *is_rgb16 = 1;5243- return STBI_rgb;5325+ return STBI_rgb;5326 case 24: // fall-through5327 case 32: return bits_per_pixel/8;5328 default: return 0;5329 }787 unmodified lines6117 6118 typedef struct6119 {6120 int w,h;6039- stbi_uc *out, *old_out; // output buffer (always 4 components)6040- int flags, bgindex, ratio, transparent, eflags, delay;6121+ stbi_uc *out; // output buffer (always 4 components)6122+ stbi_uc *background; // The current "background" as far as a gif is concerned6123+ stbi_uc *history; 6124+ int flags, bgindex, ratio, transparent, eflags;6125 stbi_uc pal[256][4];6126 stbi_uc lpal[256][4];6043- stbi__gif_lzw codes[4096];6127+ stbi__gif_lzw codes[8192];6128 stbi_uc *color_table;6129 int parse, step;6130 int lflags;6131 int start_x, start_y;6132 int max_x, max_y;6133 int cur_x, cur_y;6134 int line_size;6135+ int delay;6136 } stbi__gif;6137 6138 static int stbi__gif_test_raw(stbi__context *s)6139 {67 unmodified lines6207 6208 static void stbi__out_gif_code(stbi__gif *g, stbi__uint16 code)6209 {6210 stbi_uc *p, *c;6211+ int idx; 6212 6213 // recurse to decode the prefixes, since the linked-list is backwards,6214 // and working backwards through an interleaved image would be nasty6215 if (g->codes[code].prefix >= 0)6216 stbi__out_gif_code(g, g->codes[code].prefix);6217 6218 if (g->cur_y >= g->max_y) return;6219 6134- p = &g->out[g->cur_x + g->cur_y];6135- c = &g->color_table[g->codes[code].suffix * 4];6220+ idx = g->cur_x + g->cur_y; 6221+ p = &g->out[idx];6222+ g->history[idx / 4] = 1; 6223 6137- if (c[3] >= 128) {6224+ c = &g->color_table[g->codes[code].suffix * 4];6225+ if (c[3] > 128) { // don't render transparent pixels; 6226 p[0] = c[2];6227 p[1] = c[1];6228 p[2] = c[0];6229 p[3] = c[3];65 unmodified lines6295 while ((len = stbi__get8(s)) > 0)6296 stbi__skip(s,len);6297 return g->out;6298 } else if (code <= avail) {6299+ if (first) {6211- if (first) return stbi__errpuc("no clear code", "Corrupt GIF");6300+ return stbi__errpuc("no clear code", "Corrupt GIF");6301+ }6302 6303 if (oldcode >= 0) {6304 p = &g->codes[avail++];6305+ if (avail > 8192) {6215- if (avail > 4096) return stbi__errpuc("too many codes", "Corrupt GIF");6306+ return stbi__errpuc("too many codes", "Corrupt GIF");6307+ }6308+6309 p->prefix = (stbi__int16) oldcode;6310 p->first = g->codes[oldcode].first;6311 p->suffix = (code == avail) ? p->first : g->codes[code].first;6312 } else if (code == avail)13 unmodified lines6326 }6327 }6328 }6329 6237-static void stbi__fill_gif_background(stbi__gif *g, int x0, int y0, int x1, int y1)6238-{6239- int x, y;6240- stbi_uc *c = g->pal[g->bgindex];6241- for (y = y0; y < y1; y += 4 * g->w) {6242- for (x = x0; x < x1; x += 4) {6243- stbi_uc *p = &g->out[y + x];6244- p[0] = c[2];6245- p[1] = c[1];6246- p[2] = c[0];6247- p[3] = 0;6248- }6249- }6250-}6251-6330 // this function is designed to support animated gifs, although stb_image doesn't support it6331+// two back is the image from two frames ago, used for a very specific disposal format6253-static stbi_uc *stbi__gif_load_next(stbi__context *s, stbi__gif *g, int *comp, int req_comp)6332+static stbi_uc *stbi__gif_load_next(stbi__context *s, stbi__gif *g, int *comp, int req_comp, stbi_uc *two_back)6333 {6255- int i;6256- stbi_uc *prev_out = 0;6334+ int dispose; 6335+ int first_frame; 6336+ int pi; 6337+ int pcount; 6338 6258- if (g->out == 0 && !stbi__gif_header(s, g, comp,0))6259- return 0; // stbi__g_failure_reason set by stbi__gif_header6339+ // on first frame, any non-written pixels get the background colour (non-transparent)6340+ first_frame = 0; 6341+ if (g->out == 0) {6342+ if (!stbi__gif_header(s, g, comp,0)) return 0; // stbi__g_failure_reason set by stbi__gif_header6343+ g->out = (stbi_uc *) stbi__malloc(4 * g->w * g->h);6344+ g->background = (stbi_uc *) stbi__malloc(4 * g->w * g->h); 6345+ g->history = (stbi_uc *) stbi__malloc(g->w * g->h); 6346+ if (g->out == 0) return stbi__errpuc("outofmem", "Out of memory");6347 6261- if (!stbi__mad3sizes_valid(g->w, g->h, 4, 0))6262- return stbi__errpuc("too large", "GIF too large");6348+ // image is treated as "tranparent" at the start - ie, nothing overwrites the current background; 6349+ // background colour is only used for pixels that are not rendered first frame, after that "background"6350+ // color refers to teh color that was there the previous frame. 6351+ memset( g->out, 0x00, 4 * g->w * g->h ); 6352+ memset( g->background, 0x00, 4 * g->w * g->h ); // state of the background (starts transparent)6353+ memset( g->history, 0x00, g->w * g->h ); // pixels that were affected previous frame6354+ first_frame = 1; 6355+ } else {6356+ // second frame - how do we dispoase of the previous one?6357+ dispose = (g->eflags & 0x1C) >> 2; 6358+ pcount = g->w * g->h; 6359 6264- prev_out = g->out;6265- g->out = (stbi_uc *) stbi__malloc_mad3(4, g->w, g->h, 0);6266- if (g->out == 0) return stbi__errpuc("outofmem", "Out of memory");6360+ if ((dispose == 3) && (two_back == 0)) {6361+ dispose = 2; // if I don't have an image to revert back to, default to the old background6362+ }6363 6268- switch ((g->eflags & 0x1C) >> 2) {6269- case 0: // unspecified (also always used on 1st frame)6270- stbi__fill_gif_background(g, 0, 0, 4 * g->w, 4 * g->w * g->h);6271- break;6272- case 1: // do not dispose6273- if (prev_out) memcpy(g->out, prev_out, 4 * g->w * g->h);6274- g->old_out = prev_out;6275- break;6276- case 2: // dispose to background6277- if (prev_out) memcpy(g->out, prev_out, 4 * g->w * g->h);6278- stbi__fill_gif_background(g, g->start_x, g->start_y, g->max_x, g->max_y);6279- break;6280- case 3: // dispose to previous6281- if (g->old_out) {6282- for (i = g->start_y; i < g->max_y; i += 4 * g->w)6283- memcpy(&g->out[i + g->start_x], &g->old_out[i + g->start_x], g->max_x - g->start_x);6364+ if (dispose == 3) { // use previous graphic6365+ for (pi = 0; pi < pcount; ++pi) {6366+ if (g->history[pi]) {6367+ memcpy( &g->out[pi * 4], &two_back[pi * 4], 4 ); 6368+ }6369 }6285- break;6370+ } else if (dispose == 2) { 6371+ // restore what was changed last frame to background before that frame; 6372+ for (pi = 0; pi < pcount; ++pi) {6373+ if (g->history[pi]) {6374+ memcpy( &g->out[pi * 4], &g->background[pi * 4], 4 ); 6375+ }6376+ }6377+ } else {6378+ // This is a non-disposal case eithe way, so just 6379+ // leave the pixels as is, and they will become the new background6380+ // 1: do not dispose6381+ // 0: not specified.6382+ }6383+6384+ // background is what out is after the undoing of the previou frame; 6385+ memcpy( g->background, g->out, 4 * g->w * g->h ); 6386 }6387 6388+ // clear my history; 6389+ memset( g->history, 0x00, g->w * g->h ); // pixels that were affected previous frame6390+6391 for (;;) {6289- switch (stbi__get8(s)) {6392+ int tag = stbi__get8(s); 6393+ switch (tag) {6394 case 0x2C: /* Image Descriptor */6395 {6292- int prev_trans = -1;6396 stbi__int32 x, y, w, h;6397 stbi_uc *o;6398 6399 x = stbi__get16le(s);24 unmodified lines6424 if (g->lflags & 0x80) {6425 stbi__gif_parse_colortable(s,g->lpal, 2 << (g->lflags & 7), g->eflags & 0x01 ? g->transparent : -1);6426 g->color_table = (stbi_uc *) g->lpal;6427 } else if (g->flags & 0x80) {6325- if (g->transparent >= 0 && (g->eflags & 0x01)) {6326- prev_trans = g->pal[g->transparent][3];6327- g->pal[g->transparent][3] = 0;6328- }6428 g->color_table = (stbi_uc *) g->pal;6429 } else6331- return stbi__errpuc("missing color table", "Corrupt GIF");6332-6430+ return stbi__errpuc("missing color table", "Corrupt GIF"); 6431+ 6432 o = stbi__process_gif_raster(s, g);6433 if (o == NULL) return NULL;6434 6336- if (prev_trans != -1)6337- g->pal[g->transparent][3] = (stbi_uc) prev_trans;6435+ // if this was the first frame, 6436+ pcount = g->w * g->h; 6437+ if (first_frame && (g->bgindex > 0)) {6438+ // if first frame, any pixel not drawn to gets the background color6439+ for (pi = 0; pi < pcount; ++pi) {6440+ if (g->history[pi] == 0) {6441+ g->pal[g->bgindex][3] = 255; // just in case it was made transparent, undo that; It will be reset next frame if need be; 6442+ memcpy( &g->out[pi * 4], &g->pal[g->bgindex], 4 ); 6443+ }6444+ }6445+ }6446 6447 return o;6448 }6449 6450 case 0x21: // Comment Extension.6451 {6452 int len;6453+ int ext = stbi__get8(s); 6345- if (stbi__get8(s) == 0xF9) { // Graphic Control Extension.6454+ if (ext == 0xF9) { // Graphic Control Extension.6455 len = stbi__get8(s);6456 if (len == 4) {6457 g->eflags = stbi__get8(s);6349- g->delay = stbi__get16le(s);6350- g->transparent = stbi__get8(s);6458+ g->delay = 10 * stbi__get16le(s); // delay - 1/100th of a second, saving as 1/1000ths.6459+6460+ // unset old transparent6461+ if (g->transparent >= 0) {6462+ g->pal[g->transparent][3] = 255; 6463+ } 6464+ if (g->eflags & 0x01) {6465+ g->transparent = stbi__get8(s);6466+ if (g->transparent >= 0) {6467+ g->pal[g->transparent][3] = 0; 6468+ }6469+ } else {6470+ // don't need transparent6471+ stbi__skip(s, 1); 6472+ g->transparent = -1; 6473+ }6474 } else {6475 stbi__skip(s, len);6476 break;6477 }6355- }6356- while ((len = stbi__get8(s)) != 0)6478+ } 6479+ while ((len = stbi__get8(s)) != 0) {6480 stbi__skip(s, len);6481+ }6482 break;6483 }6484 6485 case 0x3B: // gif stream termination code2 unmodified lines6488 default:6489 return stbi__errpuc("unknown code", "Corrupt GIF");6490 }6491 }6492+}6493 6369- STBI_NOTUSED(req_comp);6494+static void *stbi__load_gif_main(stbi__context *s, int **delays, int *x, int *y, int *z, int *comp, int req_comp)6495+{6496+ if (stbi__gif_test(s)) {6497+ int layers = 0; 6498+ stbi_uc *u = 0;6499+ stbi_uc *out = 0;6500+ stbi_uc *two_back = 0; 6501+ stbi__gif g;6502+ int stride; 6503+ memset(&g, 0, sizeof(g));6504+ if (delays) {6505+ *delays = 0; 6506+ }6507+6508+ do {6509+ u = stbi__gif_load_next(s, &g, comp, req_comp, two_back);6510+ if (u == (stbi_uc *) s) u = 0; // end of animated gif marker6511+6512+ if (u) {6513+ *x = g.w;6514+ *y = g.h;6515+ ++layers; 6516+ stride = g.w * g.h * 4; 6517+ 6518+ if (out) {6519+ out = (stbi_uc*) STBI_REALLOC( out, layers * stride ); 6520+ if (delays) {6521+ *delays = (int*) STBI_REALLOC( *delays, sizeof(int) * layers ); 6522+ }6523+ } else {6524+ out = (stbi_uc*)stbi__malloc( layers * stride ); 6525+ if (delays) {6526+ *delays = (int*) stbi__malloc( layers * sizeof(int) ); 6527+ }6528+ }6529+ memcpy( out + ((layers - 1) * stride), u, stride ); 6530+ if (layers >= 2) {6531+ two_back = out - 2 * stride; 6532+ }6533+6534+ if (delays) {6535+ (*delays)[layers - 1U] = g.delay; 6536+ }6537+ }6538+ } while (u != 0); 6539+6540+ // free temp buffer; 6541+ STBI_FREE(g.out); 6542+ STBI_FREE(g.history); 6543+ STBI_FREE(g.background); 6544+6545+ // do the final conversion after loading everything; 6546+ if (req_comp && req_comp != 4)6547+ out = stbi__convert_format(out, 4, req_comp, layers * g.w, g.h);6548+6549+ *z = layers; 6550+ return out;6551+ } else {6552+ return stbi__errpuc("not GIF", "Image was not as a gif type."); 6553+ }6554 }6555 6556 static void *stbi__gif_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri)6557 {6558 stbi_uc *u = 0;6375- stbi__gif* g = (stbi__gif*) stbi__malloc(sizeof(stbi__gif));6376- memset(g, 0, sizeof(*g));6377- STBI_NOTUSED(ri);6559+ stbi__gif g;6560+ memset(&g, 0, sizeof(g));6561 6379- u = stbi__gif_load_next(s, g, comp, req_comp);6562+ u = stbi__gif_load_next(s, &g, comp, req_comp, 0);6563 if (u == (stbi_uc *) s) u = 0; // end of animated gif marker6564 if (u) {6382- *x = g->w;6383- *y = g->h;6565+ *x = g.w;6566+ *y = g.h;6567+6568+ // moved conversion to after successful load so that the same6569+ // can be done for multiple frames. 6570 if (req_comp && req_comp != 4)6385- u = stbi__convert_format(u, 4, req_comp, g->w, g->h);6571+ u = stbi__convert_format(u, 4, req_comp, g.w, g.h);6572 }6387- else if (g->out)6388- STBI_FREE(g->out);6389- STBI_FREE(g);6573+6574+ // free buffers needed for multiple frame loading; 6575+ STBI_FREE(g.history);6576+ STBI_FREE(g.background); 6577+6578 return u;6579 }6580 6581 static int stbi__gif_info(stbi__context *s, int *x, int *y, int *comp)270 unmodified lines6852 6853 #ifndef STBI_NO_PSD6854 static int stbi__psd_info(stbi__context *s, int *x, int *y, int *comp)6855 {6668- int channelCount, dummy;6856+ int channelCount, dummy, depth;6857 if (!x) x = &dummy;6858 if (!y) y = &dummy;6859 if (!comp) comp = &dummy;6860 if (stbi__get32be(s) != 0x38425053) {11 unmodified lines6872 return 0;6873 }6874 *y = stbi__get32be(s);6875 *x = stbi__get32be(s);6688- if (stbi__get16be(s) != 8) {6876+ depth = stbi__get16be(s);6877+ if (depth != 8 && depth != 16) {6878 stbi__rewind( s );6879 return 0;6880 }6881 if (stbi__get16be(s) != 3) {2 unmodified lines6884 }6885 *comp = 4;6886 return 1;6887 }6888+6889+static int stbi__psd_is16(stbi__context *s)6890+{6891+ int channelCount, dummy, depth;6892+ if (stbi__get32be(s) != 0x38425053) {6893+ stbi__rewind( s );6894+ return 0;6895+ }6896+ if (stbi__get16be(s) != 1) {6897+ stbi__rewind( s );6898+ return 0;6899+ }6900+ stbi__skip(s, 6);6901+ channelCount = stbi__get16be(s);6902+ if (channelCount < 0 || channelCount > 16) {6903+ stbi__rewind( s );6904+ return 0;6905+ }6906+ dummy = stbi__get32be(s);6907+ dummy = stbi__get32be(s);6908+ depth = stbi__get16be(s);6909+ if (depth != 16) {6910+ stbi__rewind( s );6911+ return 0;6912+ }6913+ return 1;6914+}6915 #endif6916 6917 #ifndef STBI_NO_PIC6918 static int stbi__pic_info(stbi__context *s, int *x, int *y, int *comp)222 unmodified lines7141 #endif7142 return stbi__err("unknown image type", "Image not of any known type, or corrupt");7143 }7144 7145+static int stbi__is_16_main(stbi__context *s)7146+{7147+ #ifndef STBI_NO_PNG7148+ if (stbi__png_is16(s)) return 1;7149+ #endif7150+7151+ #ifndef STBI_NO_PSD7152+ if (stbi__psd_is16(s)) return 1;7153+ #endif7154+7155+ return 0;7156+}7157+7158 #ifndef STBI_NO_STDIO7159 STBIDEF int stbi_info(char const *filename, int *x, int *y, int *comp)7160 {7161 FILE *f = stbi__fopen(filename, "rb");13 unmodified lines7175 r = stbi__info_main(&s,x,y,comp);7176 fseek(f,pos,SEEK_SET);7177 return r;7178 }7179+7180+STBIDEF int stbi_is_16_bit(char const *filename)7181+{7182+ FILE *f = stbi__fopen(filename, "rb");7183+ int result;7184+ if (!f) return stbi__err("can't fopen", "Unable to open file");7185+ result = stbi_is_16_bit_from_file(f);7186+ fclose(f);7187+ return result;7188+}7189+7190+STBIDEF int stbi_is_16_bit_from_file(FILE *f)7191+{7192+ int r;7193+ stbi__context s;7194+ long pos = ftell(f);7195+ stbi__start_file(&s, f);7196+ r = stbi__is_16_main(&s);7197+ fseek(f,pos,SEEK_SET);7198+ return r;7199+}7200 #endif // !STBI_NO_STDIO7201 7202 STBIDEF int stbi_info_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp)7203 {8 unmodified lines7212 stbi__start_callbacks(&s, (stbi_io_callbacks *) c, user);7213 return stbi__info_main(&s,x,y,comp);7214 }7215 7216+STBIDEF int stbi_is_16_bit_from_memory(stbi_uc const *buffer, int len)7217+{7218+ stbi__context s;7219+ stbi__start_mem(&s,buffer,len);7220+ return stbi__is_16_main(&s);7221+}7222+7223+STBIDEF int stbi_is_16_bit_from_callbacks(stbi_io_callbacks const *c, void *user)7224+{7225+ stbi__context s;7226+ stbi__start_callbacks(&s, (stbi_io_callbacks *) c, user);7227+ return stbi__is_16_main(&s);7228+}7229+7230 #endif // STB_IMAGE_IMPLEMENTATION7231 7232 /*7233 revision history:7234+ 2.17 (2018-01-29) change sbti__shiftsigned to avoid clang -O2 bug7235+ 1-bit BMP7236+ *_is_16_bit api7237+ avoid warnings7238+ 2.16 (2017-07-23) all functions have 16-bit variants;7239+ STBI_NO_STDIO works again;7240+ compilation fixes;7241+ fix rounding in unpremultiply;7242+ optimize vertical flip;7243+ disable raw_len validation;7244+ documentation fixes7245 2.15 (2017-03-18) fix png-1,2,4 bug; now all Imagenet JPGs decode;7246 warning fixes; disable run-time SSE detection on gcc;7247 uniform handling of optional "return" values;7248 thread-safe initialization of zlib tables205 unmodified linesmodified:stb_leakcheck.h+27 added-6 removed
1-// stb_leakcheck.h - v0.3 - quick & dirty malloc leak-checking - public domain1+// stb_leakcheck.h - v0.4 - quick & dirty malloc leak-checking - public domain2 // LICENSE3 //4 // See end of file.5 15 unmodified lines21 typedef struct malloc_info stb_leakcheck_malloc_info;22 23 struct malloc_info24 {25- char *file;25+ const char *file;26 int line;27 size_t size;28 stb_leakcheck_malloc_info *next,*prev;29 };30 31 static stb_leakcheck_malloc_info *mi_head;32 33-void *stb_leakcheck_malloc(size_t sz, char *file, int line)33+void *stb_leakcheck_malloc(size_t sz, const char *file, int line)34 {35 stb_leakcheck_malloc_info *mi = (stb_leakcheck_malloc_info *) malloc(sz + sizeof(*mi));36 if (mi == NULL) return mi;37 mi->file = file;20 unmodified lines58 mi->prev->next = mi->next;59 if (mi->next)60 mi->next->prev = mi->prev;61 #endif62+ free(mi);63 }64 }65 65-void *stb_leakcheck_realloc(void *ptr, size_t sz, char *file, int line)66+void *stb_leakcheck_realloc(void *ptr, size_t sz, const char *file, int line)67 {68 if (ptr == NULL) {69 return stb_leakcheck_malloc(sz, file, line);70 } else if (sz == 0) {17 unmodified lines88 }89 }90 }91 92+static void stblkck_internal_print(const char *reason, const char *file, int line, size_t size, void *ptr)93+{94+#if (defined(_MSC_VER) && _MSC_VER < 1900) /* 1900=VS 2015 */ || defined(__MINGW32__)95+ // Compilers that use the old MS C runtime library don't have %zd96+ // and the older ones don't even have %lld either... however, the old compilers97+ // without "long long" don't support 64-bit targets either, so here's the98+ // compromise:99+ #if defined(_MSC_VER) && _MSC_VER < 1400 // before VS 2005100+ printf("%-6s: %s (%4d): %8d bytes at %p\n", reason, file, line, (int)size, ptr);101+ #else102+ printf("%-6s: %s (%4d): %8lld bytes at %p\n", reason, file, line, (long long)size, ptr);103+ #endif104+#else105+ // Assume we have %zd on other targets.106+ printf("%-6s: %s (%4d): %zd bytes at %p\n", reason, file, line, size, ptr);107+#endif108+}109+110 void stb_leakcheck_dumpmem(void)111 {112 stb_leakcheck_malloc_info *mi = mi_head;113 while (mi) {114 if ((ptrdiff_t) mi->size >= 0)115+ stblkck_internal_print("LEAKED", mi->file, mi->line, mi->size, mi+1);116 printf("LEAKED: %s (%4d): %8d bytes at %p\n", mi->file, mi->line, (int) mi->size, mi+1);117 mi = mi->next;118 }119 #ifdef STB_LEAKCHECK_SHOWALL120 mi = mi_head;121 while (mi) {122 if ((ptrdiff_t) mi->size < 0)123+ stblkck_internal_print("FREED", mi->file, mi->line, ~mi->size, mi+1);124 printf("FREED : %s (%4d): %8d bytes at %p\n", mi->file, mi->line, (int) ~mi->size, mi+1);125 mi = mi->next;126 }127 #endif6 unmodified lines134 #define malloc(sz) stb_leakcheck_malloc(sz, __FILE__, __LINE__)135 #define free(p) stb_leakcheck_free(p)136 #define realloc(p,sz) stb_leakcheck_realloc(p,sz, __FILE__, __LINE__)137 117-extern void * stb_leakcheck_malloc(size_t sz, char *file, int line);118-extern void * stb_leakcheck_realloc(void *ptr, size_t sz, char *file, int line);138+extern void * stb_leakcheck_malloc(size_t sz, const char *file, int line);139+extern void * stb_leakcheck_realloc(void *ptr, size_t sz, const char *file, int line);140 extern void stb_leakcheck_free(void *ptr);141 extern void stb_leakcheck_dumpmem(void);142 143 #endif // INCLUDE_STB_LEAKCHECK_H43 unmodified linesmodified:stb_rect_pack.h-11 removed
522 unmodified lines523 return 1;524 return (p->w > q->w) ? -1 : (p->w < q->w);525 }526 527-static int rect_width_compare(const void *a, const void *b)528-{529- const stbrp_rect *p = (const stbrp_rect *) a;530- const stbrp_rect *q = (const stbrp_rect *) b;531- if (p->w > q->w)532- return -1;533- if (p->w < q->w)534- return 1;535- return (p->h > q->h) ? -1 : (p->h < q->h);536-}537-527 static int rect_original_order(const void *a, const void *b)528 {529 const stbrp_rect *p = (const stbrp_rect *) a;530 const stbrp_rect *q = (const stbrp_rect *) b;94 unmodified linesmodified:stb_sprintf.h+1402 added-790 removed
1-// stb_sprintf - v1.02 - public domain snprintf() implementation1+// stb_sprintf - v1.03 - public domain snprintf() implementation2 // originally by Jeff Roberts / RAD Game Tools, 2015/10/203 // http://github.com/nothings/stb4 //5 // allowed types: sc uidBboXx p AaGgEef n6 // lengths : h ll j z t I64 I32 I7 //8+// Contributors:9+// Fabian "ryg" Giesen (reformatting)10+//11 // Contributors (bugfixes):12 // github:d2643513 // github:trex7814 // Jari Komppa (SI suffixes)15+// Rohit Nirmal16+// Marcin Wojdyr17 //18 // LICENSE:19 //20 // See end of file for license information.3 unmodified lines24 25 /*26 Single file sprintf replacement.27 23-Originally written by Jeff Roberts at RAD Game Tools - 2015/10/20. 28+Originally written by Jeff Roberts at RAD Game Tools - 2015/10/20.29 Hereby placed in public domain.30 31 This is a full sprintf replacement that supports everything that32 the C runtime sprintfs support, including float/double, 64-bit integers,33 hex floats, field parameters (%*.*d stuff), length reads backs, etc.34 35 Why would you need this if sprintf already exists? Well, first off,36 it's *much* faster (see below). It's also much smaller than the CRT32-versions code-space-wise. We've also added some simple improvements 37+versions code-space-wise. We've also added some simple improvements38 that are super handy (commas in thousands, callbacks at buffer full,34-for example). Finally, the format strings for MSVC and GCC differ 35-for 64-bit integers (among other small things), so this lets you use 39+for example). Finally, the format strings for MSVC and GCC differ40+for 64-bit integers (among other small things), so this lets you use41 the same format strings in cross platform code.42 43 It uses the standard single file trick of being both the header file39-and the source itself. If you just include it normally, you just get 44+and the source itself. If you just include it normally, you just get45 the header file function definitions. To get the code, you include46 it from a C or C++ file and define STB_SPRINTF_IMPLEMENTATION first.47 48 It only uses va_args macros from the C runtime to do it's work. It44-does cast doubles to S64s and shifts and divides U64s, which does 49+does cast doubles to S64s and shifts and divides U64s, which does50 drag in CRT code on most platforms.51 52 It compiles to roughly 8K with float support, and 4K without.53 As a comparison, when using MSVC static libs, calling sprintf drags28 unmodified lines82 doubles with error correction (double-doubles, for ~105 bits of83 precision). This conversion is round-trip perfect - that is, an atof84 of the values output here will give you the bit-exact double back.85 81-One difference is that our insignificant digits will be different than 82-with MSVC or GCC (but they don't match each other either). We also 83-don't attempt to find the minimum length matching float (pre-MSVC15 86+One difference is that our insignificant digits will be different than87+with MSVC or GCC (but they don't match each other either). We also88+don't attempt to find the minimum length matching float (pre-MSVC1589 doesn't either).90 91 If you don't need float or doubles at all, define STB_SPRINTF_NOFLOAT92 and you'll save 4K of code space.6 unmodified lines99 100 EXTRAS:101 =======102 Like some GCCs, for integers and floats, you can use a ' (single quote)98-specifier and commas will be inserted on the thousands: "%'d" on 12345 103+specifier and commas will be inserted on the thousands: "%'d" on 12345104 would print 12,345.105 101-For integers and floats, you can use a "$" specifier and the number 106+For integers and floats, you can use a "$" specifier and the number107 will be converted to float and then divided to get kilo, mega, giga or103-tera and then printed, so "%$d" 1000 is "1.0 k", "%$.2d" 2536000 is 104-"2.53 M", etc. For byte values, use two $:s, like "%$$d" to turn 108+tera and then printed, so "%$d" 1000 is "1.0 k", "%$.2d" 2536000 is109+"2.53 M", etc. For byte values, use two $:s, like "%$$d" to turn110 2536000 to "2.42 Mi". If you prefer JEDEC suffixes to SI ones, use three111 $:s: "%$$$d" -> "2.42 M". To remove the space between the number and the112 suffix, add "_" specifier: "%_$d" -> "2.53M".113 109-In addition to octal and hexadecimal conversions, you can print 114+In addition to octal and hexadecimal conversions, you can print115 integers in binary: "%b" for 256 would print 100.116 117 PERFORMANCE vs MSVC 2008 32-/64-bit (GCC is even slower than MSVC):118 ===================================================================16 unmodified lines135 "...512 char string..." ( 35.0x/32.5x faster!)136 */137 138 #if defined(__has_feature)134- #if __has_feature(address_sanitizer)135- #define STBI__ASAN __attribute__((no_sanitize("address")))136- #endif139+ #if __has_feature(address_sanitizer)140+ #define STBI__ASAN __attribute__((no_sanitize("address")))141+ #endif142 #endif143 #ifndef STBI__ASAN144 #define STBI__ASAN145 #endif10 unmodified lines156 #define STBSP__PUBLICDEF STBI__ASAN157 #endif158 #endif159 155-#include <stdarg.h> // for va_list()160+#include <stdarg.h> // for va_list()161 162 #ifndef STB_SPRINTF_MIN163 #define STB_SPRINTF_MIN 512 // how many characters per callback164 #endif160-typedef char * STBSP_SPRINTFCB( char * buf, void * user, int len );165+typedef char *STBSP_SPRINTFCB(char *buf, void *user, int len);166 167 #ifndef STB_SPRINTF_DECORATE163-#define STB_SPRINTF_DECORATE(name) stbsp_##name // define this before including if you want to change the names168+#define STB_SPRINTF_DECORATE(name) stbsp_##name // define this before including if you want to change the names169 #endif170 166-STBSP__PUBLICDEF int STB_SPRINTF_DECORATE( vsprintf )( char * buf, char const * fmt, va_list va );167-STBSP__PUBLICDEF int STB_SPRINTF_DECORATE( vsnprintf )( char * buf, int count, char const * fmt, va_list va );168-STBSP__PUBLICDEF int STB_SPRINTF_DECORATE( sprintf ) ( char * buf, char const * fmt, ... );169-STBSP__PUBLICDEF int STB_SPRINTF_DECORATE( snprintf )( char * buf, int count, char const * fmt, ... );171+STBSP__PUBLICDEF int STB_SPRINTF_DECORATE(vsprintf)(char *buf, char const *fmt, va_list va);172+STBSP__PUBLICDEF int STB_SPRINTF_DECORATE(vsnprintf)(char *buf, int count, char const *fmt, va_list va);173+STBSP__PUBLICDEF int STB_SPRINTF_DECORATE(sprintf)(char *buf, char const *fmt, ...);174+STBSP__PUBLICDEF int STB_SPRINTF_DECORATE(snprintf)(char *buf, int count, char const *fmt, ...);175 171-STBSP__PUBLICDEF int STB_SPRINTF_DECORATE( vsprintfcb )( STBSP_SPRINTFCB * callback, void * user, char * buf, char const * fmt, va_list va );172-STBSP__PUBLICDEF void STB_SPRINTF_DECORATE( set_separators )( char comma, char period );176+STBSP__PUBLICDEF int STB_SPRINTF_DECORATE(vsprintfcb)(STBSP_SPRINTFCB *callback, void *user, char *buf, char const *fmt, va_list va);177+STBSP__PUBLICDEF void STB_SPRINTF_DECORATE(set_separators)(char comma, char period);178 179 #endif // STB_SPRINTF_H_INCLUDE180 181 #ifdef STB_SPRINTF_IMPLEMENTATION182 178-#include <stdlib.h> // for va_arg()183+#include <stdlib.h> // for va_arg()184 185 #define stbsp__uint32 unsigned int186 #define stbsp__int32 signed int187 5 unmodified lines193 #define stbsp__int64 signed long long194 #endif195 #define stbsp__uint16 unsigned short196 192-#ifndef stbsp__uintptr 197+#ifndef stbsp__uintptr198 #if defined(__ppc64__) || defined(__aarch64__) || defined(_M_X64) || defined(__x86_64__) || defined(__x86_64)199 #define stbsp__uintptr stbsp__uint64200 #else201 #define stbsp__uintptr stbsp__uint32202 #endif203 #endif204 200-#ifndef STB_SPRINTF_MSVC_MODE // used for MSVC2013 and earlier (MSVC2015 matches GCC)201-#if defined(_MSC_VER) && (_MSC_VER<1900)205+#ifndef STB_SPRINTF_MSVC_MODE // used for MSVC2013 and earlier (MSVC2015 matches GCC)206+#if defined(_MSC_VER) && (_MSC_VER < 1900)207 #define STB_SPRINTF_MSVC_MODE208 #endif209 #endif210 206-#ifdef STB_SPRINTF_NOUNALIGNED // define this before inclusion to force stbsp_sprintf to always use aligned accesses211+#ifdef STB_SPRINTF_NOUNALIGNED // define this before inclusion to force stbsp_sprintf to always use aligned accesses212 #define STBSP__UNALIGNED(code)213 #else214 #define STBSP__UNALIGNED(code) code215 #endif216 217 #ifndef STB_SPRINTF_NOFLOAT218 // internal float utility functions214-static stbsp__int32 stbsp__real_to_str( char const * * start, stbsp__uint32 * len, char *out, stbsp__int32 * decimal_pos, double value, stbsp__uint32 frac_digits );215-static stbsp__int32 stbsp__real_to_parts( stbsp__int64 * bits, stbsp__int32 * expo, double value );219+static stbsp__int32 stbsp__real_to_str(char const **start, stbsp__uint32 *len, char *out, stbsp__int32 *decimal_pos, double value, stbsp__uint32 frac_digits);220+static stbsp__int32 stbsp__real_to_parts(stbsp__int64 *bits, stbsp__int32 *expo, double value);221 #define STBSP__SPECIAL 0x7000222 #endif223 219-static char stbsp__period='.';220-static char stbsp__comma=',';221-static char stbsp__digitpair[201]="00010203040506070809101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899";224+static char stbsp__period = '.';225+static char stbsp__comma = ',';226+static char stbsp__digitpair[201] =227+ "0001020304050607080910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576"228+ "7778798081828384858687888990919293949596979899";229 223-STBSP__PUBLICDEF void STB_SPRINTF_DECORATE( set_separators )( char pcomma, char pperiod )230+STBSP__PUBLICDEF void STB_SPRINTF_DECORATE(set_separators)(char pcomma, char pperiod)231 {225- stbsp__period=pperiod;226- stbsp__comma=pcomma;232+ stbsp__period = pperiod;233+ stbsp__comma = pcomma;234 }235 236 #define STBSP__LEFTJUST 1237 #define STBSP__LEADINGPLUS 210 unmodified lines248 #define STBSP__METRIC_JEDEC 4096249 250 static void stbsp__lead_sign(stbsp__uint32 fl, char *sign)251 {245- sign[0] = 0;246- if (fl&STBSP__NEGATIVE) {247- sign[0]=1;248- sign[1]='-';249- } else if (fl&STBSP__LEADINGSPACE) {250- sign[0]=1;251- sign[1]=' ';252- } else if (fl&STBSP__LEADINGPLUS) {253- sign[0]=1;254- sign[1]='+';255- }252+ sign[0] = 0;253+ if (fl & STBSP__NEGATIVE) {254+ sign[0] = 1;255+ sign[1] = '-';256+ } else if (fl & STBSP__LEADINGSPACE) {257+ sign[0] = 1;258+ sign[1] = ' ';259+ } else if (fl & STBSP__LEADINGPLUS) {260+ sign[0] = 1;261+ sign[1] = '+';262+ }263 }257- 258-STBSP__PUBLICDEF int STB_SPRINTF_DECORATE( vsprintfcb )( STBSP_SPRINTFCB * callback, void * user, char * buf, char const * fmt, va_list va )264+265+STBSP__PUBLICDEF int STB_SPRINTF_DECORATE(vsprintfcb)(STBSP_SPRINTFCB *callback, void *user, char *buf, char const *fmt, va_list va)266 {260- static char hex[]="0123456789abcdefxp";261- static char hexu[]="0123456789ABCDEFXP";262- char * bf;263- char const * f;264- int tlen = 0;267+ static char hex[] = "0123456789abcdefxp";268+ static char hexu[] = "0123456789ABCDEFXP";269+ char *bf;270+ char const *f;271+ int tlen = 0;272 266- bf = buf;267- f = fmt;268- for(;;)269- {270- stbsp__int32 fw,pr,tz; stbsp__uint32 fl;273+ bf = buf;274+ f = fmt;275+ for (;;) {276+ stbsp__int32 fw, pr, tz;277+ stbsp__uint32 fl;278 272- // macros for the callback buffer stuff273- #define stbsp__chk_cb_bufL(bytes) { int len = (int)(bf-buf); if ((len+(bytes))>=STB_SPRINTF_MIN) { tlen+=len; if (0==(bf=buf=callback(buf,user,len))) goto done; } }274- #define stbsp__chk_cb_buf(bytes) { if ( callback ) { stbsp__chk_cb_bufL(bytes); } }275- #define stbsp__flush_cb() { stbsp__chk_cb_bufL(STB_SPRINTF_MIN-1); } //flush if there is even one byte in the buffer276- #define stbsp__cb_buf_clamp(cl,v) cl = v; if ( callback ) { int lg = STB_SPRINTF_MIN-(int)(bf-buf); if (cl>lg) cl=lg; }279+ // macros for the callback buffer stuff280+ #define stbsp__chk_cb_bufL(bytes) \281+ { \282+ int len = (int)(bf - buf); \283+ if ((len + (bytes)) >= STB_SPRINTF_MIN) { \284+ tlen += len; \285+ if (0 == (bf = buf = callback(buf, user, len))) \286+ goto done; \287+ } \288+ }289+ #define stbsp__chk_cb_buf(bytes) \290+ { \291+ if (callback) { \292+ stbsp__chk_cb_bufL(bytes); \293+ } \294+ }295+ #define stbsp__flush_cb() \296+ { \297+ stbsp__chk_cb_bufL(STB_SPRINTF_MIN - 1); \298+ } // flush if there is even one byte in the buffer299+ #define stbsp__cb_buf_clamp(cl, v) \300+ cl = v; \301+ if (callback) { \302+ int lg = STB_SPRINTF_MIN - (int)(bf - buf); \303+ if (cl > lg) \304+ cl = lg; \305+ }306 278- // fast copy everything up to the next % (or end of string)279- for(;;)280- { 281- while (((stbsp__uintptr)f)&3)282- {283- schk1: if (f[0]=='%') goto scandd;284- schk2: if (f[0]==0) goto endfmt;285- stbsp__chk_cb_buf(1); *bf++=f[0]; ++f;286- } 287- for(;;)288- { 289- // Check if the next 4 bytes contain %(0x25) or end of string.290- // Using the 'hasless' trick:291- // https://graphics.stanford.edu/~seander/bithacks.html#HasLessInWord292- stbsp__uint32 v,c;293- v=*(stbsp__uint32*)f; c=(~v)&0x80808080;294- if (((v^0x25252525)-0x01010101)&c) goto schk1;295- if ((v-0x01010101)&c) goto schk2; 296- if (callback) if ((STB_SPRINTF_MIN-(int)(bf-buf))<4) goto schk1;297- *(stbsp__uint32*)bf=v; bf+=4; f+=4;307+ // fast copy everything up to the next % (or end of string)308+ for (;;) {309+ while (((stbsp__uintptr)f) & 3) {310+ schk1:311+ if (f[0] == '%')312+ goto scandd;313+ schk2:314+ if (f[0] == 0)315+ goto endfmt;316+ stbsp__chk_cb_buf(1);317+ *bf++ = f[0];318+ ++f;319+ }320+ for (;;) {321+ // Check if the next 4 bytes contain %(0x25) or end of string.322+ // Using the 'hasless' trick:323+ // https://graphics.stanford.edu/~seander/bithacks.html#HasLessInWord324+ stbsp__uint32 v, c;325+ v = *(stbsp__uint32 *)f;326+ c = (~v) & 0x80808080;327+ if (((v ^ 0x25252525) - 0x01010101) & c)328+ goto schk1;329+ if ((v - 0x01010101) & c)330+ goto schk2;331+ if (callback)332+ if ((STB_SPRINTF_MIN - (int)(bf - buf)) < 4)333+ goto schk1;334+ *(stbsp__uint32 *)bf = v;335+ bf += 4;336+ f += 4;337+ }338 }299- } scandd:339+ scandd:340 301- ++f;341+ ++f;342 303- // ok, we have a percent, read the modifiers first304- fw = 0; pr = -1; fl = 0; tz = 0;305- 306- // flags307- for(;;)308- {309- switch(f[0])310- {311- // if we have left justify312- case '-': fl|=STBSP__LEFTJUST; ++f; continue;313- // if we have leading plus314- case '+': fl|=STBSP__LEADINGPLUS; ++f; continue; 315- // if we have leading space316- case ' ': fl|=STBSP__LEADINGSPACE; ++f; continue; 317- // if we have leading 0x318- case '#': fl|=STBSP__LEADING_0X; ++f; continue; 319- // if we have thousand commas320- case '\'': fl|=STBSP__TRIPLET_COMMA; ++f; continue; 321- // if we have kilo marker (none->kilo->kibi->jedec)322- case '$': 323- if (fl&STBSP__METRIC_SUFFIX)324- {325- if (fl&STBSP__METRIC_1024)326- {327- fl|=STBSP__METRIC_JEDEC;328- }329- else330- {331- fl|=STBSP__METRIC_1024;332- }343+ // ok, we have a percent, read the modifiers first344+ fw = 0;345+ pr = -1;346+ fl = 0;347+ tz = 0;348+349+ // flags350+ for (;;) {351+ switch (f[0]) {352+ // if we have left justify353+ case '-':354+ fl |= STBSP__LEFTJUST;355+ ++f;356+ continue;357+ // if we have leading plus358+ case '+':359+ fl |= STBSP__LEADINGPLUS;360+ ++f;361+ continue;362+ // if we have leading space363+ case ' ':364+ fl |= STBSP__LEADINGSPACE;365+ ++f;366+ continue;367+ // if we have leading 0x368+ case '#':369+ fl |= STBSP__LEADING_0X;370+ ++f;371+ continue;372+ // if we have thousand commas373+ case '\'':374+ fl |= STBSP__TRIPLET_COMMA;375+ ++f;376+ continue;377+ // if we have kilo marker (none->kilo->kibi->jedec)378+ case '$':379+ if (fl & STBSP__METRIC_SUFFIX) {380+ if (fl & STBSP__METRIC_1024) {381+ fl |= STBSP__METRIC_JEDEC;382+ } else {383+ fl |= STBSP__METRIC_1024;384+ }385+ } else {386+ fl |= STBSP__METRIC_SUFFIX;387 }334- else335- {336- fl|=STBSP__METRIC_SUFFIX; 388+ ++f;389+ continue;390+ // if we don't want space between metric suffix and number391+ case '_':392+ fl |= STBSP__METRIC_NOSPACE;393+ ++f;394+ continue;395+ // if we have leading zero396+ case '0':397+ fl |= STBSP__LEADINGZERO;398+ ++f;399+ goto flags_done;400+ default: goto flags_done;401+ }402+ }403+ flags_done:404+405+ // get the field width406+ if (f[0] == '*') {407+ fw = va_arg(va, stbsp__uint32);408+ ++f;409+ } else {410+ while ((f[0] >= '0') && (f[0] <= '9')) {411+ fw = fw * 10 + f[0] - '0';412+ f++;413+ }414+ }415+ // get the precision416+ if (f[0] == '.') {417+ ++f;418+ if (f[0] == '*') {419+ pr = va_arg(va, stbsp__uint32);420+ ++f;421+ } else {422+ pr = 0;423+ while ((f[0] >= '0') && (f[0] <= '9')) {424+ pr = pr * 10 + f[0] - '0';425+ f++;426 }338- ++f; continue; 339- // if we don't want space between metric suffix and number340- case '_': fl|=STBSP__METRIC_NOSPACE; ++f; continue;341- // if we have leading zero342- case '0': fl|=STBSP__LEADINGZERO; ++f; goto flags_done; 343- default: goto flags_done;427+ }428 }345- }346- flags_done:347- 348- // get the field width349- if ( f[0] == '*' ) {fw = va_arg(va,stbsp__uint32); ++f;} else { while (( f[0] >= '0' ) && ( f[0] <= '9' )) { fw = fw * 10 + f[0] - '0'; f++; } }350- // get the precision351- if ( f[0]=='.' ) { ++f; if ( f[0] == '*' ) {pr = va_arg(va,stbsp__uint32); ++f;} else { pr = 0; while (( f[0] >= '0' ) && ( f[0] <= '9' )) { pr = pr * 10 + f[0] - '0'; f++; } } } 352- 353- // handle integer size overrides354- switch(f[0])429+430+ // handle integer size overrides431+ switch (f[0]) {355- {432 // are we halfwidth?357- case 'h': fl|=STBSP__HALFWIDTH; ++f; break;433+ case 'h':434+ fl |= STBSP__HALFWIDTH;435+ ++f;436+ break;437 // are we 64-bit (unix style)359- case 'l': ++f; if ( f[0]=='l') { fl|=STBSP__INTMAX; ++f; } break;438+ case 'l':439+ ++f;440+ if (f[0] == 'l') {441+ fl |= STBSP__INTMAX;442+ ++f;443+ }444+ break;445 // are we 64-bit on intmax? (c99)361- case 'j': fl|=STBSP__INTMAX; ++f; break; 446+ case 'j':447+ fl |= STBSP__INTMAX;448+ ++f;449+ break;450 // are we 64-bit on size_t or ptrdiff_t? (c99)363- case 'z': case 't': fl|=((sizeof(char*)==8)?STBSP__INTMAX:0); ++f; break; 451+ case 'z':452+ case 't':453+ fl |= ((sizeof(char *) == 8) ? STBSP__INTMAX : 0);454+ ++f;455+ break;456 // are we 64-bit (msft style)365- case 'I': if ( ( f[1]=='6') && ( f[2]=='4') ) { fl|=STBSP__INTMAX; f+=3; }366- else if ( ( f[1]=='3') && ( f[2]=='2') ) { f+=3; }367- else { fl|=((sizeof(void*)==8)?STBSP__INTMAX:0); ++f; } break;457+ case 'I':458+ if ((f[1] == '6') && (f[2] == '4')) {459+ fl |= STBSP__INTMAX;460+ f += 3;461+ } else if ((f[1] == '3') && (f[2] == '2')) {462+ f += 3;463+ } else {464+ fl |= ((sizeof(void *) == 8) ? STBSP__INTMAX : 0);465+ ++f;466+ }467+ break;468 default: break;369- }469+ }470 371- // handle each replacement372- switch( f[0] )373- {374- #define STBSP__NUMSZ 512 // big enough for e308 (with commas) or e-307 375- char num[STBSP__NUMSZ]; 376- char lead[8]; 377- char tail[8]; 378- char *s;379- char const *h;380- stbsp__uint32 l,n,cs;381- stbsp__uint64 n64;382- #ifndef STB_SPRINTF_NOFLOAT 383- double fv; 384- #endif385- stbsp__int32 dp; char const * sn;471+ // handle each replacement472+ switch (f[0]) {473+ #define STBSP__NUMSZ 512 // big enough for e308 (with commas) or e-307474+ char num[STBSP__NUMSZ];475+ char lead[8];476+ char tail[8];477+ char *s;478+ char const *h;479+ stbsp__uint32 l, n, cs;480+ stbsp__uint64 n64;481+#ifndef STB_SPRINTF_NOFLOAT482+ double fv;483+#endif484+ stbsp__int32 dp;485+ char const *sn;486 487 case 's':388- // get the string389- s = va_arg(va,char*); if (s==0) s = (char*)"null";390- // get the length391- sn = s;392- for(;;)393- { 394- if ((((stbsp__uintptr)sn)&3)==0) break;488+ // get the string489+ s = va_arg(va, char *);490+ if (s == 0)491+ s = (char *)"null";492+ // get the length493+ sn = s;494+ for (;;) {495+ if ((((stbsp__uintptr)sn) & 3) == 0)496+ break;497 lchk:396- if (sn[0]==0) goto ld;397- ++sn;398- }399- n = 0xffffffff;400- if (pr>=0) { n=(stbsp__uint32)(sn-s); if (n>=(stbsp__uint32)pr) goto ld; n=((stbsp__uint32)(pr-n))>>2; }401- while(n) 402- { 403- stbsp__uint32 v=*(stbsp__uint32*)sn;404- if ((v-0x01010101)&(~v)&0x80808080UL) goto lchk; 405- sn+=4; 406- --n;407- }408- goto lchk;409- ld:498+ if (sn[0] == 0)499+ goto ld;500+ ++sn;501+ }502+ n = 0xffffffff;503+ if (pr >= 0) {504+ n = (stbsp__uint32)(sn - s);505+ if (n >= (stbsp__uint32)pr)506+ goto ld;507+ n = ((stbsp__uint32)(pr - n)) >> 2;508+ }509+ while (n) {510+ stbsp__uint32 v = *(stbsp__uint32 *)sn;511+ if ((v - 0x01010101) & (~v) & 0x80808080UL)512+ goto lchk;513+ sn += 4;514+ --n;515+ }516+ goto lchk;517+ ld:518 411- l = (stbsp__uint32) ( sn - s );412- // clamp to precision413- if ( l > (stbsp__uint32)pr ) l = pr;414- lead[0]=0; tail[0]=0; pr = 0; dp = 0; cs = 0;415- // copy the string in416- goto scopy;519+ l = (stbsp__uint32)(sn - s);520+ // clamp to precision521+ if (l > (stbsp__uint32)pr)522+ l = pr;523+ lead[0] = 0;524+ tail[0] = 0;525+ pr = 0;526+ dp = 0;527+ cs = 0;528+ // copy the string in529+ goto scopy;530 531 case 'c': // char419- // get the character420- s = num + STBSP__NUMSZ -1; *s = (char)va_arg(va,int);421- l = 1;422- lead[0]=0; tail[0]=0; pr = 0; dp = 0; cs = 0;423- goto scopy;532+ // get the character533+ s = num + STBSP__NUMSZ - 1;534+ *s = (char)va_arg(va, int);535+ l = 1;536+ lead[0] = 0;537+ tail[0] = 0;538+ pr = 0;539+ dp = 0;540+ cs = 0;541+ goto scopy;542 543 case 'n': // weird write-bytes specifier544+ {426- { int * d = va_arg(va,int*);427- *d = tlen + (int)( bf - buf ); }428- break;545+ int *d = va_arg(va, int *);546+ *d = tlen + (int)(bf - buf);547+ } break;548 549 #ifdef STB_SPRINTF_NOFLOAT431- case 'A': // float432- case 'a': // hex float433- case 'G': // float434- case 'g': // float435- case 'E': // float436- case 'e': // float437- case 'f': // float438- va_arg(va,double); // eat it439- s = (char*)"No float";440- l = 8;441- lead[0]=0; tail[0]=0; pr = 0; dp = 0; cs = 0;442- goto scopy;550+ case 'A': // float551+ case 'a': // hex float552+ case 'G': // float553+ case 'g': // float554+ case 'E': // float555+ case 'e': // float556+ case 'f': // float557+ va_arg(va, double); // eat it558+ s = (char *)"No float";559+ l = 8;560+ lead[0] = 0;561+ tail[0] = 0;562+ pr = 0;563+ dp = 0;564+ cs = 0;565+ goto scopy;566 #else444- case 'A': // float445- h=hexu; 446- goto hexfloat;447-567+ case 'A': // hex float568 case 'a': // hex float449- h=hex;450- hexfloat: 451- fv = va_arg(va,double);452- if (pr==-1) pr=6; // default is 6453- // read the double into a string454- if ( stbsp__real_to_parts( (stbsp__int64*)&n64, &dp, fv ) )455- fl |= STBSP__NEGATIVE;456- 457- s = num+64;569+ h = (f[0] == 'A') ? hexu : hex;570+ fv = va_arg(va, double);571+ if (pr == -1)572+ pr = 6; // default is 6573+ // read the double into a string574+ if (stbsp__real_to_parts((stbsp__int64 *)&n64, &dp, fv))575+ fl |= STBSP__NEGATIVE;576 459- stbsp__lead_sign(fl, lead);577+ s = num + 64;578 461- if (dp==-1023) dp=(n64)?-1022:0; else n64|=(((stbsp__uint64)1)<<52);462- n64<<=(64-56);463- if (pr<15) n64+=((((stbsp__uint64)8)<<56)>>(pr*4));464- // add leading chars465- 466- #ifdef STB_SPRINTF_MSVC_MODE467- *s++='0';*s++='x';468- #else469- lead[1+lead[0]]='0'; lead[2+lead[0]]='x'; lead[0]+=2;470- #endif471- *s++=h[(n64>>60)&15]; n64<<=4;472- if ( pr ) *s++=stbsp__period;473- sn = s;579+ stbsp__lead_sign(fl, lead);580 475- // print the bits476- n = pr; if (n>13) n = 13; if (pr>(stbsp__int32)n) tz=pr-n; pr = 0;477- while(n--) { *s++=h[(n64>>60)&15]; n64<<=4; }581+ if (dp == -1023)582+ dp = (n64) ? -1022 : 0;583+ else584+ n64 |= (((stbsp__uint64)1) << 52);585+ n64 <<= (64 - 56);586+ if (pr < 15)587+ n64 += ((((stbsp__uint64)8) << 56) >> (pr * 4));588+// add leading chars589 479- // print the expo480- tail[1]=h[17];481- if (dp<0) { tail[2]='-'; dp=-dp;} else tail[2]='+';482- n = (dp>=1000)?6:((dp>=100)?5:((dp>=10)?4:3));483- tail[0]=(char)n;484- for(;;) { tail[n]='0'+dp%10; if (n<=3) break; --n; dp/=10; }590+#ifdef STB_SPRINTF_MSVC_MODE591+ *s++ = '0';592+ *s++ = 'x';593+#else594+ lead[1 + lead[0]] = '0';595+ lead[2 + lead[0]] = 'x';596+ lead[0] += 2;597+#endif598+ *s++ = h[(n64 >> 60) & 15];599+ n64 <<= 4;600+ if (pr)601+ *s++ = stbsp__period;602+ sn = s;603 486- dp = (int)(s-sn);487- l = (int)(s-(num+64));488- s = num+64;489- cs = 1 + (3<<24);490- goto scopy;604+ // print the bits605+ n = pr;606+ if (n > 13)607+ n = 13;608+ if (pr > (stbsp__int32)n)609+ tz = pr - n;610+ pr = 0;611+ while (n--) {612+ *s++ = h[(n64 >> 60) & 15];613+ n64 <<= 4;614+ }615 492- case 'G': // float493- h=hexu;494- goto dosmallfloat;616+ // print the expo617+ tail[1] = h[17];618+ if (dp < 0) {619+ tail[2] = '-';620+ dp = -dp;621+ } else622+ tail[2] = '+';623+ n = (dp >= 1000) ? 6 : ((dp >= 100) ? 5 : ((dp >= 10) ? 4 : 3));624+ tail[0] = (char)n;625+ for (;;) {626+ tail[n] = '0' + dp % 10;627+ if (n <= 3)628+ break;629+ --n;630+ dp /= 10;631+ }632 633+ dp = (int)(s - sn);634+ l = (int)(s - (num + 64));635+ s = num + 64;636+ cs = 1 + (3 << 24);637+ goto scopy;638+639+ case 'G': // float640 case 'g': // float497- h=hex;498- dosmallfloat: 499- fv = va_arg(va,double);500- if (pr==-1) pr=6; else if (pr==0) pr = 1; // default is 6501- // read the double into a string502- if ( stbsp__real_to_str( &sn, &l, num, &dp, fv, (pr-1)|0x80000000 ) )503- fl |= STBSP__NEGATIVE;641+ h = (f[0] == 'G') ? hexu : hex;642+ fv = va_arg(va, double);643+ if (pr == -1)644+ pr = 6;645+ else if (pr == 0)646+ pr = 1; // default is 6647+ // read the double into a string648+ if (stbsp__real_to_str(&sn, &l, num, &dp, fv, (pr - 1) | 0x80000000))649+ fl |= STBSP__NEGATIVE;650 505- // clamp the precision and delete extra zeros after clamp506- n = pr;507- if ( l > (stbsp__uint32)pr ) l = pr; while ((l>1)&&(pr)&&(sn[l-1]=='0')) { --pr; --l; }651+ // clamp the precision and delete extra zeros after clamp652+ n = pr;653+ if (l > (stbsp__uint32)pr)654+ l = pr;655+ while ((l > 1) && (pr) && (sn[l - 1] == '0')) {656+ --pr;657+ --l;658+ }659 509- // should we use %e510- if ((dp<=-4)||(dp>(stbsp__int32)n))511- {512- if ( pr > (stbsp__int32)l ) pr = l-1; else if ( pr ) --pr; // when using %e, there is one digit before the decimal513- goto doexpfromg;514- }515- // this is the insane action to get the pr to match %g sematics for %f516- if(dp>0) { pr=(dp<(stbsp__int32)l)?l-dp:0; } else { pr = -dp+((pr>(stbsp__int32)l)?l:pr); }517- goto dofloatfromg;660+ // should we use %e661+ if ((dp <= -4) || (dp > (stbsp__int32)n)) {662+ if (pr > (stbsp__int32)l)663+ pr = l - 1;664+ else if (pr)665+ --pr; // when using %e, there is one digit before the decimal666+ goto doexpfromg;667+ }668+ // this is the insane action to get the pr to match %g sematics for %f669+ if (dp > 0) {670+ pr = (dp < (stbsp__int32)l) ? l - dp : 0;671+ } else {672+ pr = -dp + ((pr > (stbsp__int32)l) ? l : pr);673+ }674+ goto dofloatfromg;675 676 case 'E': // float520- h=hexu; 521- goto doexp;522-677 case 'e': // float524- h=hex;525- doexp: 526- fv = va_arg(va,double);527- if (pr==-1) pr=6; // default is 6528- // read the double into a string529- if ( stbsp__real_to_str( &sn, &l, num, &dp, fv, pr|0x80000000 ) )530- fl |= STBSP__NEGATIVE;531- doexpfromg: 532- tail[0]=0; 533- stbsp__lead_sign(fl, lead);534- if ( dp == STBSP__SPECIAL ) { s=(char*)sn; cs=0; pr=0; goto scopy; }535- s=num+64; 536- // handle leading chars537- *s++=sn[0];678+ h = (f[0] == 'E') ? hexu : hex;679+ fv = va_arg(va, double);680+ if (pr == -1)681+ pr = 6; // default is 6682+ // read the double into a string683+ if (stbsp__real_to_str(&sn, &l, num, &dp, fv, pr | 0x80000000))684+ fl |= STBSP__NEGATIVE;685+ doexpfromg:686+ tail[0] = 0;687+ stbsp__lead_sign(fl, lead);688+ if (dp == STBSP__SPECIAL) {689+ s = (char *)sn;690+ cs = 0;691+ pr = 0;692+ goto scopy;693+ }694+ s = num + 64;695+ // handle leading chars696+ *s++ = sn[0];697 698+ if (pr)539- if (pr) *s++=stbsp__period;699+ *s++ = stbsp__period;700 541- // handle after decimal542- if ((l-1)>(stbsp__uint32)pr) l=pr+1;543- for(n=1;n<l;n++) *s++=sn[n];544- // trailing zeros545- tz = pr-(l-1); pr=0;546- // dump expo547- tail[1]=h[0xe];548- dp -= 1;549- if (dp<0) { tail[2]='-'; dp=-dp;} else tail[2]='+';550- #ifdef STB_SPRINTF_MSVC_MODE551- n = 5;552- #else553- n = (dp>=100)?5:4;554- #endif555- tail[0]=(char)n;556- for(;;) { tail[n]='0'+dp%10; if (n<=3) break; --n; dp/=10; }557- cs = 1 + (3<<24); // how many tens558- goto flt_lead; 701+ // handle after decimal702+ if ((l - 1) > (stbsp__uint32)pr)703+ l = pr + 1;704+ for (n = 1; n < l; n++)705+ *s++ = sn[n];706+ // trailing zeros707+ tz = pr - (l - 1);708+ pr = 0;709+ // dump expo710+ tail[1] = h[0xe];711+ dp -= 1;712+ if (dp < 0) {713+ tail[2] = '-';714+ dp = -dp;715+ } else716+ tail[2] = '+';717+#ifdef STB_SPRINTF_MSVC_MODE718+ n = 5;719+#else720+ n = (dp >= 100) ? 5 : 4;721+#endif722+ tail[0] = (char)n;723+ for (;;) {724+ tail[n] = '0' + dp % 10;725+ if (n <= 3)726+ break;727+ --n;728+ dp /= 10;729+ }730+ cs = 1 + (3 << 24); // how many tens731+ goto flt_lead;732 733 case 'f': // float561- fv = va_arg(va,double);562- doafloat: 563- // do kilos564- if (fl&STBSP__METRIC_SUFFIX) 565- {734+ fv = va_arg(va, double);735+ doafloat:736+ // do kilos737+ if (fl & STBSP__METRIC_SUFFIX) {738 double divisor;567- divisor=1000.0f;568- if (fl&STBSP__METRIC_1024) divisor = 1024.0;569- while(fl<0x4000000) { if ((fv<divisor) && (fv>-divisor)) break; fv/=divisor; fl+=0x1000000; }570- }571- if (pr==-1) pr=6; // default is 6572- // read the double into a string573- if ( stbsp__real_to_str( &sn, &l, num, &dp, fv, pr ) )574- fl |= STBSP__NEGATIVE;575- dofloatfromg:576- tail[0]=0;577- stbsp__lead_sign(fl, lead);578- if ( dp == STBSP__SPECIAL ) { s=(char*)sn; cs=0; pr=0; goto scopy; }579- s=num+64; 739+ divisor = 1000.0f;740+ if (fl & STBSP__METRIC_1024)741+ divisor = 1024.0;742+ while (fl < 0x4000000) {743+ if ((fv < divisor) && (fv > -divisor))744+ break;745+ fv /= divisor;746+ fl += 0x1000000;747+ }748+ }749+ if (pr == -1)750+ pr = 6; // default is 6751+ // read the double into a string752+ if (stbsp__real_to_str(&sn, &l, num, &dp, fv, pr))753+ fl |= STBSP__NEGATIVE;754+ dofloatfromg:755+ tail[0] = 0;756+ stbsp__lead_sign(fl, lead);757+ if (dp == STBSP__SPECIAL) {758+ s = (char *)sn;759+ cs = 0;760+ pr = 0;761+ goto scopy;762+ }763+ s = num + 64;764 581- // handle the three decimal varieties582- if (dp<=0) 583- { 584- stbsp__int32 i;585- // handle 0.000*000xxxx586- *s++='0'; if (pr) *s++=stbsp__period; 587- n=-dp; if((stbsp__int32)n>pr) n=pr; i=n; while(i) { if ((((stbsp__uintptr)s)&3)==0) break; *s++='0'; --i; } while(i>=4) { *(stbsp__uint32*)s=0x30303030; s+=4; i-=4; } while(i) { *s++='0'; --i; }588- if ((stbsp__int32)(l+n)>pr) l=pr-n; i=l; while(i) { *s++=*sn++; --i; }589- tz = pr-(n+l);590- cs = 1 + (3<<24); // how many tens did we write (for commas below)591- }592- else593- {594- cs = (fl&STBSP__TRIPLET_COMMA)?((600-(stbsp__uint32)dp)%3):0;595- if ((stbsp__uint32)dp>=l)596- {597- // handle xxxx000*000.0598- n=0; for(;;) { if ((fl&STBSP__TRIPLET_COMMA) && (++cs==4)) { cs = 0; *s++=stbsp__comma; } else { *s++=sn[n]; ++n; if (n>=l) break; } }599- if (n<(stbsp__uint32)dp)600- {601- n = dp - n;602- if ((fl&STBSP__TRIPLET_COMMA)==0) { while(n) { if ((((stbsp__uintptr)s)&3)==0) break; *s++='0'; --n; } while(n>=4) { *(stbsp__uint32*)s=0x30303030; s+=4; n-=4; } }603- while(n) { if ((fl&STBSP__TRIPLET_COMMA) && (++cs==4)) { cs = 0; *s++=stbsp__comma; } else { *s++='0'; --n; } }765+ // handle the three decimal varieties766+ if (dp <= 0) {767+ stbsp__int32 i;768+ // handle 0.000*000xxxx769+ *s++ = '0';770+ if (pr)771+ *s++ = stbsp__period;772+ n = -dp;773+ if ((stbsp__int32)n > pr)774+ n = pr;775+ i = n;776+ while (i) {777+ if ((((stbsp__uintptr)s) & 3) == 0)778+ break;779+ *s++ = '0';780+ --i;781 }605- cs = (int)(s-(num+64)) + (3<<24); // cs is how many tens606- if (pr) { *s++=stbsp__period; tz=pr;}607- }608- else609- {610- // handle xxxxx.xxxx000*000611- n=0; for(;;) { if ((fl&STBSP__TRIPLET_COMMA) && (++cs==4)) { cs = 0; *s++=stbsp__comma; } else { *s++=sn[n]; ++n; if (n>=(stbsp__uint32)dp) break; } }612- cs = (int)(s-(num+64)) + (3<<24); // cs is how many tens613- if (pr) *s++=stbsp__period;614- if ((l-dp)>(stbsp__uint32)pr) l=pr+dp;615- while(n<l) { *s++=sn[n]; ++n; }616- tz = pr-(l-dp);617- }618- }619- pr = 0;620- 621- // handle k,m,g,t622- if (fl&STBSP__METRIC_SUFFIX) 623- { 782+ while (i >= 4) {783+ *(stbsp__uint32 *)s = 0x30303030;784+ s += 4;785+ i -= 4;786+ }787+ while (i) {788+ *s++ = '0';789+ --i;790+ }791+ if ((stbsp__int32)(l + n) > pr)792+ l = pr - n;793+ i = l;794+ while (i) {795+ *s++ = *sn++;796+ --i;797+ }798+ tz = pr - (n + l);799+ cs = 1 + (3 << 24); // how many tens did we write (for commas below)800+ } else {801+ cs = (fl & STBSP__TRIPLET_COMMA) ? ((600 - (stbsp__uint32)dp) % 3) : 0;802+ if ((stbsp__uint32)dp >= l) {803+ // handle xxxx000*000.0804+ n = 0;805+ for (;;) {806+ if ((fl & STBSP__TRIPLET_COMMA) && (++cs == 4)) {807+ cs = 0;808+ *s++ = stbsp__comma;809+ } else {810+ *s++ = sn[n];811+ ++n;812+ if (n >= l)813+ break;814+ }815+ }816+ if (n < (stbsp__uint32)dp) {817+ n = dp - n;818+ if ((fl & STBSP__TRIPLET_COMMA) == 0) {819+ while (n) {820+ if ((((stbsp__uintptr)s) & 3) == 0)821+ break;822+ *s++ = '0';823+ --n;824+ }825+ while (n >= 4) {826+ *(stbsp__uint32 *)s = 0x30303030;827+ s += 4;828+ n -= 4;829+ }830+ }831+ while (n) {832+ if ((fl & STBSP__TRIPLET_COMMA) && (++cs == 4)) {833+ cs = 0;834+ *s++ = stbsp__comma;835+ } else {836+ *s++ = '0';837+ --n;838+ }839+ }840+ }841+ cs = (int)(s - (num + 64)) + (3 << 24); // cs is how many tens842+ if (pr) {843+ *s++ = stbsp__period;844+ tz = pr;845+ }846+ } else {847+ // handle xxxxx.xxxx000*000848+ n = 0;849+ for (;;) {850+ if ((fl & STBSP__TRIPLET_COMMA) && (++cs == 4)) {851+ cs = 0;852+ *s++ = stbsp__comma;853+ } else {854+ *s++ = sn[n];855+ ++n;856+ if (n >= (stbsp__uint32)dp)857+ break;858+ }859+ }860+ cs = (int)(s - (num + 64)) + (3 << 24); // cs is how many tens861+ if (pr)862+ *s++ = stbsp__period;863+ if ((l - dp) > (stbsp__uint32)pr)864+ l = pr + dp;865+ while (n < l) {866+ *s++ = sn[n];867+ ++n;868+ }869+ tz = pr - (l - dp);870+ }871+ }872+ pr = 0;873+874+ // handle k,m,g,t875+ if (fl & STBSP__METRIC_SUFFIX) {876 char idx;625- idx=1;626- if (fl&STBSP__METRIC_NOSPACE)627- idx=0;628- tail[0]=idx; 629- tail[1]=' '; 630- { 631- if (fl>>24) 632- { // SI kilo is 'k', JEDEC and SI kibits are 'K'.633- if (fl&STBSP__METRIC_1024)634- tail[idx+1]="_KMGT"[fl>>24]; 635- else636- tail[idx+1]="_kMGT"[fl>>24]; 637- idx++;638- // If printing kibits and not in jedec, add the 'i'.639- if (fl&STBSP__METRIC_1024&&!(fl&STBSP__METRIC_JEDEC))640- {641- tail[idx+1]='i';642- idx++;643- }644- tail[0]=idx;645- } 646- } 647- };877+ idx = 1;878+ if (fl & STBSP__METRIC_NOSPACE)879+ idx = 0;880+ tail[0] = idx;881+ tail[1] = ' ';882+ {883+ if (fl >> 24) { // SI kilo is 'k', JEDEC and SI kibits are 'K'.884+ if (fl & STBSP__METRIC_1024)885+ tail[idx + 1] = "_KMGT"[fl >> 24];886+ else887+ tail[idx + 1] = "_kMGT"[fl >> 24];888+ idx++;889+ // If printing kibits and not in jedec, add the 'i'.890+ if (fl & STBSP__METRIC_1024 && !(fl & STBSP__METRIC_JEDEC)) {891+ tail[idx + 1] = 'i';892+ idx++;893+ }894+ tail[0] = idx;895+ }896+ }897+ };898 649- flt_lead:650- // get the length that we copied651- l = (stbsp__uint32) ( s-(num+64) );652- s=num+64; 653- goto scopy;899+ flt_lead:900+ // get the length that we copied901+ l = (stbsp__uint32)(s - (num + 64));902+ s = num + 64;903+ goto scopy;904 #endif905 906 case 'B': // upper binary657- h = hexu;658- goto binary;659-907 case 'b': // lower binary661- h = hex;662- binary:663- lead[0]=0;664- if (fl&STBSP__LEADING_0X) { lead[0]=2;lead[1]='0';lead[2]=h[0xb]; }665- l=(8<<4)|(1<<8);666- goto radixnum;908+ h = (f[0] == 'B') ? hexu : hex;909+ lead[0] = 0;910+ if (fl & STBSP__LEADING_0X) {911+ lead[0] = 2;912+ lead[1] = '0';913+ lead[2] = h[0xb];914+ }915+ l = (8 << 4) | (1 << 8);916+ goto radixnum;917 918 case 'o': // octal669- h = hexu;670- lead[0]=0;671- if (fl&STBSP__LEADING_0X) { lead[0]=1;lead[1]='0'; }672- l=(3<<4)|(3<<8);673- goto radixnum;919+ h = hexu;920+ lead[0] = 0;921+ if (fl & STBSP__LEADING_0X) {922+ lead[0] = 1;923+ lead[1] = '0';924+ }925+ l = (3 << 4) | (3 << 8);926+ goto radixnum;927 928 case 'p': // pointer676- fl |= (sizeof(void*)==8)?STBSP__INTMAX:0;677- pr = sizeof(void*)*2;678- fl &= ~STBSP__LEADINGZERO; // 'p' only prints the pointer with zeros679- // drop through to X680- 681- case 'X': // upper binary682- h = hexu;683- goto dohexb;929+ fl |= (sizeof(void *) == 8) ? STBSP__INTMAX : 0;930+ pr = sizeof(void *) * 2;931+ fl &= ~STBSP__LEADINGZERO; // 'p' only prints the pointer with zeros932+ // fall through - to X933 934+ case 'X': // upper hex935+ case 'x': // lower hex936+ h = (f[0] == 'X') ? hexu : hex;937+ l = (4 << 4) | (4 << 8);938+ lead[0] = 0;685- case 'x': // lower binary686- h = hex; dohexb:687- l=(4<<4)|(4<<8);688- lead[0]=0;689- if (fl&STBSP__LEADING_0X) { lead[0]=2;lead[1]='0';lead[2]=h[16]; }690- radixnum: 691- // get the number692- if ( fl&STBSP__INTMAX )693- n64 = va_arg(va,stbsp__uint64);694- else695- n64 = va_arg(va,stbsp__uint32);939+ if (fl & STBSP__LEADING_0X) {940+ lead[0] = 2;941+ lead[1] = '0';942+ lead[2] = h[16];943+ }944+ radixnum:945+ // get the number946+ if (fl & STBSP__INTMAX)947+ n64 = va_arg(va, stbsp__uint64);948+ else949+ n64 = va_arg(va, stbsp__uint32);950 697- s = num + STBSP__NUMSZ; dp = 0;698- // clear tail, and clear leading if value is zero699- tail[0]=0; if (n64==0) { lead[0]=0; if (pr==0) { l=0; cs = ( ((l>>4)&15)) << 24; goto scopy; } }700- // convert to string701- for(;;) { *--s = h[n64&((1<<(l>>8))-1)]; n64>>=(l>>8); if ( ! ( (n64) || ((stbsp__int32) ( (num+STBSP__NUMSZ) - s ) < pr ) ) ) break; if ( fl&STBSP__TRIPLET_COMMA) { ++l; if ((l&15)==((l>>4)&15)) { l&=~15; *--s=stbsp__comma; } } };702- // get the tens and the comma pos703- cs = (stbsp__uint32) ( (num+STBSP__NUMSZ) - s ) + ( ( ((l>>4)&15)) << 24 );704- // get the length that we copied705- l = (stbsp__uint32) ( (num+STBSP__NUMSZ) - s );706- // copy it707- goto scopy;951+ s = num + STBSP__NUMSZ;952+ dp = 0;953+ // clear tail, and clear leading if value is zero954+ tail[0] = 0;955+ if (n64 == 0) {956+ lead[0] = 0;957+ if (pr == 0) {958+ l = 0;959+ cs = (((l >> 4) & 15)) << 24;960+ goto scopy;961+ }962+ }963+ // convert to string964+ for (;;) {965+ *--s = h[n64 & ((1 << (l >> 8)) - 1)];966+ n64 >>= (l >> 8);967+ if (!((n64) || ((stbsp__int32)((num + STBSP__NUMSZ) - s) < pr)))968+ break;969+ if (fl & STBSP__TRIPLET_COMMA) {970+ ++l;971+ if ((l & 15) == ((l >> 4) & 15)) {972+ l &= ~15;973+ *--s = stbsp__comma;974+ }975+ }976+ };977+ // get the tens and the comma pos978+ cs = (stbsp__uint32)((num + STBSP__NUMSZ) - s) + ((((l >> 4) & 15)) << 24);979+ // get the length that we copied980+ l = (stbsp__uint32)((num + STBSP__NUMSZ) - s);981+ // copy it982+ goto scopy;983 984 case 'u': // unsigned985 case 'i':986 case 'd': // integer712- // get the integer and abs it713- if ( fl&STBSP__INTMAX )714- {715- stbsp__int64 i64 = va_arg(va,stbsp__int64); n64 = (stbsp__uint64)i64; if ((f[0]!='u') && (i64<0)) { n64=(stbsp__uint64)-i64; fl|=STBSP__NEGATIVE; }716- }717- else718- {719- stbsp__int32 i = va_arg(va,stbsp__int32); n64 = (stbsp__uint32)i; if ((f[0]!='u') && (i<0)) { n64=(stbsp__uint32)-i; fl|=STBSP__NEGATIVE; }720- }987+ // get the integer and abs it988+ if (fl & STBSP__INTMAX) {989+ stbsp__int64 i64 = va_arg(va, stbsp__int64);990+ n64 = (stbsp__uint64)i64;991+ if ((f[0] != 'u') && (i64 < 0)) {992+ n64 = (stbsp__uint64)-i64;993+ fl |= STBSP__NEGATIVE;994+ }995+ } else {996+ stbsp__int32 i = va_arg(va, stbsp__int32);997+ n64 = (stbsp__uint32)i;998+ if ((f[0] != 'u') && (i < 0)) {999+ n64 = (stbsp__uint32)-i;1000+ fl |= STBSP__NEGATIVE;1001+ }1002+ }1003 722- #ifndef STB_SPRINTF_NOFLOAT723- if (fl&STBSP__METRIC_SUFFIX) { if (n64<1024) pr=0; else if (pr==-1) pr=1; fv=(double)(stbsp__int64)n64; goto doafloat; } 724- #endif1004+#ifndef STB_SPRINTF_NOFLOAT1005+ if (fl & STBSP__METRIC_SUFFIX) {1006+ if (n64 < 1024)1007+ pr = 0;1008+ else if (pr == -1)1009+ pr = 1;1010+ fv = (double)(stbsp__int64)n64;1011+ goto doafloat;1012+ }1013+#endif1014 726- // convert to string727- s = num+STBSP__NUMSZ; l=0; 728- 729- for(;;)730- {731- // do in 32-bit chunks (avoid lots of 64-bit divides even with constant denominators)732- char * o=s-8;733- if (n64>=100000000) { n = (stbsp__uint32)( n64 % 100000000); n64 /= 100000000; } else {n = (stbsp__uint32)n64; n64 = 0; }734- if((fl&STBSP__TRIPLET_COMMA)==0) { while(n) { s-=2; *(stbsp__uint16*)s=*(stbsp__uint16*)&stbsp__digitpair[(n%100)*2]; n/=100; } }735- while (n) { if ( ( fl&STBSP__TRIPLET_COMMA) && (l++==3) ) { l=0; *--s=stbsp__comma; --o; } else { *--s=(char)(n%10)+'0'; n/=10; } }736- if (n64==0) { if ((s[0]=='0') && (s!=(num+STBSP__NUMSZ))) ++s; break; }737- while (s!=o) if ( ( fl&STBSP__TRIPLET_COMMA) && (l++==3) ) { l=0; *--s=stbsp__comma; --o; } else { *--s='0'; }738- }1015+ // convert to string1016+ s = num + STBSP__NUMSZ;1017+ l = 0;1018 740- tail[0]=0;741- stbsp__lead_sign(fl, lead);1019+ for (;;) {1020+ // do in 32-bit chunks (avoid lots of 64-bit divides even with constant denominators)1021+ char *o = s - 8;1022+ if (n64 >= 100000000) {1023+ n = (stbsp__uint32)(n64 % 100000000);1024+ n64 /= 100000000;1025+ } else {1026+ n = (stbsp__uint32)n64;1027+ n64 = 0;1028+ }1029+ if ((fl & STBSP__TRIPLET_COMMA) == 0) {1030+ do {1031+ s -= 2;1032+ *(stbsp__uint16 *)s = *(stbsp__uint16 *)&stbsp__digitpair[(n % 100) * 2];1033+ n /= 100;1034+ } while (n);1035+ }1036+ while (n) {1037+ if ((fl & STBSP__TRIPLET_COMMA) && (l++ == 3)) {1038+ l = 0;1039+ *--s = stbsp__comma;1040+ --o;1041+ } else {1042+ *--s = (char)(n % 10) + '0';1043+ n /= 10;1044+ }1045+ }1046+ if (n64 == 0) {1047+ if ((s[0] == '0') && (s != (num + STBSP__NUMSZ)))1048+ ++s;1049+ break;1050+ }1051+ while (s != o)1052+ if ((fl & STBSP__TRIPLET_COMMA) && (l++ == 3)) {1053+ l = 0;1054+ *--s = stbsp__comma;1055+ --o;1056+ } else {1057+ *--s = '0';1058+ }1059+ }1060 743- // get the length that we copied744- l = (stbsp__uint32) ( (num+STBSP__NUMSZ) - s ); if ( l == 0 ) { *--s='0'; l = 1; }745- cs = l + (3<<24);746- if (pr<0) pr = 0;1061+ tail[0] = 0;1062+ stbsp__lead_sign(fl, lead);1063 748- scopy: 749- // get fw=leading/trailing space, pr=leading zeros750- if (pr<(stbsp__int32)l) pr = l;751- n = pr + lead[0] + tail[0] + tz;752- if (fw<(stbsp__int32)n) fw = n;753- fw -= n;754- pr -= l;1064+ // get the length that we copied1065+ l = (stbsp__uint32)((num + STBSP__NUMSZ) - s);1066+ if (l == 0) {1067+ *--s = '0';1068+ l = 1;1069+ }1070+ cs = l + (3 << 24);1071+ if (pr < 0)1072+ pr = 0;1073 756- // handle right justify and leading zeros757- if ( (fl&STBSP__LEFTJUST)==0 )758- {759- if (fl&STBSP__LEADINGZERO) // if leading zeros, everything is in pr760- { 761- pr = (fw>pr)?fw:pr;762- fw = 0;763- }764- else765- {766- fl &= ~STBSP__TRIPLET_COMMA; // if no leading zeros, then no commas767- }768- }1074+ scopy:1075+ // get fw=leading/trailing space, pr=leading zeros1076+ if (pr < (stbsp__int32)l)1077+ pr = l;1078+ n = pr + lead[0] + tail[0] + tz;1079+ if (fw < (stbsp__int32)n)1080+ fw = n;1081+ fw -= n;1082+ pr -= l;1083 770- // copy the spaces and/or zeros771- if (fw+pr)772- {773- stbsp__int32 i; stbsp__uint32 c; 1084+ // handle right justify and leading zeros1085+ if ((fl & STBSP__LEFTJUST) == 0) {1086+ if (fl & STBSP__LEADINGZERO) // if leading zeros, everything is in pr1087+ {1088+ pr = (fw > pr) ? fw : pr;1089+ fw = 0;1090+ } else {1091+ fl &= ~STBSP__TRIPLET_COMMA; // if no leading zeros, then no commas1092+ }1093+ }1094 775- // copy leading spaces (or when doing %8.4d stuff)776- if ( (fl&STBSP__LEFTJUST)==0 ) while(fw>0) { stbsp__cb_buf_clamp(i,fw); fw -= i; while(i) { if ((((stbsp__uintptr)bf)&3)==0) break; *bf++=' '; --i; } while(i>=4) { *(stbsp__uint32*)bf=0x20202020; bf+=4; i-=4; } while (i) {*bf++=' '; --i;} stbsp__chk_cb_buf(1); }777- 778- // copy leader779- sn=lead+1; while(lead[0]) { stbsp__cb_buf_clamp(i,lead[0]); lead[0] -= (char)i; while (i) {*bf++=*sn++; --i;} stbsp__chk_cb_buf(1); }780- 781- // copy leading zeros782- c = cs >> 24; cs &= 0xffffff;783- cs = (fl&STBSP__TRIPLET_COMMA)?((stbsp__uint32)(c-((pr+cs)%(c+1)))):0;784- while(pr>0) { stbsp__cb_buf_clamp(i,pr); pr -= i; if((fl&STBSP__TRIPLET_COMMA)==0) { while(i) { if ((((stbsp__uintptr)bf)&3)==0) break; *bf++='0'; --i; } while(i>=4) { *(stbsp__uint32*)bf=0x30303030; bf+=4; i-=4; } } while (i) { if((fl&STBSP__TRIPLET_COMMA) && (cs++==c)) { cs = 0; *bf++=stbsp__comma; } else *bf++='0'; --i; } stbsp__chk_cb_buf(1); }785- }1095+ // copy the spaces and/or zeros1096+ if (fw + pr) {1097+ stbsp__int32 i;1098+ stbsp__uint32 c;1099 787- // copy leader if there is still one788- sn=lead+1; while(lead[0]) { stbsp__int32 i; stbsp__cb_buf_clamp(i,lead[0]); lead[0] -= (char)i; while (i) {*bf++=*sn++; --i;} stbsp__chk_cb_buf(1); }1100+ // copy leading spaces (or when doing %8.4d stuff)1101+ if ((fl & STBSP__LEFTJUST) == 0)1102+ while (fw > 0) {1103+ stbsp__cb_buf_clamp(i, fw);1104+ fw -= i;1105+ while (i) {1106+ if ((((stbsp__uintptr)bf) & 3) == 0)1107+ break;1108+ *bf++ = ' ';1109+ --i;1110+ }1111+ while (i >= 4) {1112+ *(stbsp__uint32 *)bf = 0x20202020;1113+ bf += 4;1114+ i -= 4;1115+ }1116+ while (i) {1117+ *bf++ = ' ';1118+ --i;1119+ }1120+ stbsp__chk_cb_buf(1);1121+ }1122 790- // copy the string791- n = l; while (n) { stbsp__int32 i; stbsp__cb_buf_clamp(i,n); n-=i; STBSP__UNALIGNED( while(i>=4) { *(stbsp__uint32*)bf=*(stbsp__uint32*)s; bf+=4; s+=4; i-=4; } ) while (i) {*bf++=*s++; --i;} stbsp__chk_cb_buf(1); }1123+ // copy leader1124+ sn = lead + 1;1125+ while (lead[0]) {1126+ stbsp__cb_buf_clamp(i, lead[0]);1127+ lead[0] -= (char)i;1128+ while (i) {1129+ *bf++ = *sn++;1130+ --i;1131+ }1132+ stbsp__chk_cb_buf(1);1133+ }1134 793- // copy trailing zeros794- while(tz) { stbsp__int32 i; stbsp__cb_buf_clamp(i,tz); tz -= i; while(i) { if ((((stbsp__uintptr)bf)&3)==0) break; *bf++='0'; --i; } while(i>=4) { *(stbsp__uint32*)bf=0x30303030; bf+=4; i-=4; } while (i) {*bf++='0'; --i;} stbsp__chk_cb_buf(1); }1135+ // copy leading zeros1136+ c = cs >> 24;1137+ cs &= 0xffffff;1138+ cs = (fl & STBSP__TRIPLET_COMMA) ? ((stbsp__uint32)(c - ((pr + cs) % (c + 1)))) : 0;1139+ while (pr > 0) {1140+ stbsp__cb_buf_clamp(i, pr);1141+ pr -= i;1142+ if ((fl & STBSP__TRIPLET_COMMA) == 0) {1143+ while (i) {1144+ if ((((stbsp__uintptr)bf) & 3) == 0)1145+ break;1146+ *bf++ = '0';1147+ --i;1148+ }1149+ while (i >= 4) {1150+ *(stbsp__uint32 *)bf = 0x30303030;1151+ bf += 4;1152+ i -= 4;1153+ }1154+ }1155+ while (i) {1156+ if ((fl & STBSP__TRIPLET_COMMA) && (cs++ == c)) {1157+ cs = 0;1158+ *bf++ = stbsp__comma;1159+ } else1160+ *bf++ = '0';1161+ --i;1162+ }1163+ stbsp__chk_cb_buf(1);1164+ }1165+ }1166 796- // copy tail if there is one797- sn=tail+1; while(tail[0]) { stbsp__int32 i; stbsp__cb_buf_clamp(i,tail[0]); tail[0] -= (char)i; while (i) {*bf++=*sn++; --i;} stbsp__chk_cb_buf(1); }1167+ // copy leader if there is still one1168+ sn = lead + 1;1169+ while (lead[0]) {1170+ stbsp__int32 i;1171+ stbsp__cb_buf_clamp(i, lead[0]);1172+ lead[0] -= (char)i;1173+ while (i) {1174+ *bf++ = *sn++;1175+ --i;1176+ }1177+ stbsp__chk_cb_buf(1);1178+ }1179 799- // handle the left justify800- if (fl&STBSP__LEFTJUST) if (fw>0) { while (fw) { stbsp__int32 i; stbsp__cb_buf_clamp(i,fw); fw-=i; while(i) { if ((((stbsp__uintptr)bf)&3)==0) break; *bf++=' '; --i; } while(i>=4) { *(stbsp__uint32*)bf=0x20202020; bf+=4; i-=4; } while (i--) *bf++=' '; stbsp__chk_cb_buf(1); } }801- break;1180+ // copy the string1181+ n = l;1182+ while (n) {1183+ stbsp__int32 i;1184+ stbsp__cb_buf_clamp(i, n);1185+ n -= i;1186+ STBSP__UNALIGNED(while (i >= 4) {1187+ *(stbsp__uint32 *)bf = *(stbsp__uint32 *)s;1188+ bf += 4;1189+ s += 4;1190+ i -= 4;1191+ })1192+ while (i) {1193+ *bf++ = *s++;1194+ --i;1195+ }1196+ stbsp__chk_cb_buf(1);1197+ }1198 1199+ // copy trailing zeros1200+ while (tz) {1201+ stbsp__int32 i;1202+ stbsp__cb_buf_clamp(i, tz);1203+ tz -= i;1204+ while (i) {1205+ if ((((stbsp__uintptr)bf) & 3) == 0)1206+ break;1207+ *bf++ = '0';1208+ --i;1209+ }1210+ while (i >= 4) {1211+ *(stbsp__uint32 *)bf = 0x30303030;1212+ bf += 4;1213+ i -= 4;1214+ }1215+ while (i) {1216+ *bf++ = '0';1217+ --i;1218+ }1219+ stbsp__chk_cb_buf(1);1220+ }1221+1222+ // copy tail if there is one1223+ sn = tail + 1;1224+ while (tail[0]) {1225+ stbsp__int32 i;1226+ stbsp__cb_buf_clamp(i, tail[0]);1227+ tail[0] -= (char)i;1228+ while (i) {1229+ *bf++ = *sn++;1230+ --i;1231+ }1232+ stbsp__chk_cb_buf(1);1233+ }1234+1235+ // handle the left justify1236+ if (fl & STBSP__LEFTJUST)1237+ if (fw > 0) {1238+ while (fw) {1239+ stbsp__int32 i;1240+ stbsp__cb_buf_clamp(i, fw);1241+ fw -= i;1242+ while (i) {1243+ if ((((stbsp__uintptr)bf) & 3) == 0)1244+ break;1245+ *bf++ = ' ';1246+ --i;1247+ }1248+ while (i >= 4) {1249+ *(stbsp__uint32 *)bf = 0x20202020;1250+ bf += 4;1251+ i -= 4;1252+ }1253+ while (i--)1254+ *bf++ = ' ';1255+ stbsp__chk_cb_buf(1);1256+ }1257+ }1258+ break;1259+1260 default: // unknown, just copy code804- s = num + STBSP__NUMSZ -1; *s = f[0];805- l = 1;806- fw=pr=fl=0;807- lead[0]=0; tail[0]=0; pr = 0; dp = 0; cs = 0;808- goto scopy;809- }810- ++f;811- }812- endfmt:1261+ s = num + STBSP__NUMSZ - 1;1262+ *s = f[0];1263+ l = 1;1264+ fw = fl = 0;1265+ lead[0] = 0;1266+ tail[0] = 0;1267+ pr = 0;1268+ dp = 0;1269+ cs = 0;1270+ goto scopy;1271+ }1272+ ++f;1273+ }1274+endfmt:1275 814- if (!callback) 815- *bf = 0;816- else817- stbsp__flush_cb();818- 819- done:820- return tlen + (int)(bf-buf);1276+ if (!callback)1277+ *bf = 0;1278+ else1279+ stbsp__flush_cb();1280+1281+done:1282+ return tlen + (int)(bf - buf);1283 }1284 1285 // cleanup1286 #undef STBSP__LEFTJUST13 unmodified lines1300 1301 // ============================================================================1302 // wrapper functions1303 842-STBSP__PUBLICDEF int STB_SPRINTF_DECORATE( sprintf )( char * buf, char const * fmt, ... )1304+STBSP__PUBLICDEF int STB_SPRINTF_DECORATE(sprintf)(char *buf, char const *fmt, ...)1305 {844- int result;845- va_list va;846- va_start( va, fmt );847- result = STB_SPRINTF_DECORATE( vsprintfcb )( 0, 0, buf, fmt, va );848- va_end(va);849- return result;1306+ int result;1307+ va_list va;1308+ va_start(va, fmt);1309+ result = STB_SPRINTF_DECORATE(vsprintfcb)(0, 0, buf, fmt, va);1310+ va_end(va);1311+ return result;1312 }1313 852-typedef struct stbsp__context853-{854- char * buf;855- int count;856- char tmp[ STB_SPRINTF_MIN ];1314+typedef struct stbsp__context {1315+ char *buf;1316+ int count;1317+ char tmp[STB_SPRINTF_MIN];1318 } stbsp__context;1319 859-static char * stbsp__clamp_callback( char * buf, void * user, int len )1320+static char *stbsp__clamp_callback(char *buf, void *user, int len)1321 {861- stbsp__context * c = (stbsp__context*)user;1322+ stbsp__context *c = (stbsp__context *)user;1323 863- if ( len > c->count ) len = c->count;1324+ if (len > c->count)1325+ len = c->count;1326 865- if (len)866- {867- if ( buf != c->buf )868- {869- char * s, * d, * se;870- d = c->buf; s = buf; se = buf+len;871- do{ *d++ = *s++; } while (s<se);872- }873- c->buf += len;874- c->count -= len;875- }876- 877- if ( c->count <= 0 ) return 0;878- return ( c->count >= STB_SPRINTF_MIN ) ? c->buf : c->tmp; // go direct into buffer if you can1327+ if (len) {1328+ if (buf != c->buf) {1329+ char *s, *d, *se;1330+ d = c->buf;1331+ s = buf;1332+ se = buf + len;1333+ do {1334+ *d++ = *s++;1335+ } while (s < se);1336+ }1337+ c->buf += len;1338+ c->count -= len;1339+ }1340+1341+ if (c->count <= 0)1342+ return 0;1343+ return (c->count >= STB_SPRINTF_MIN) ? c->buf : c->tmp; // go direct into buffer if you can1344 }1345 881-STBSP__PUBLICDEF int STB_SPRINTF_DECORATE( vsnprintf )( char * buf, int count, char const * fmt, va_list va )1346+STBSP__PUBLICDEF int STB_SPRINTF_DECORATE(vsnprintf)(char *buf, int count, char const *fmt, va_list va)1347 {883- stbsp__context c;884- int l;1348+ stbsp__context c;1349+ int l;1350 886- if ( count == 0 )887- return 0;1351+ if (count == 0)1352+ return 0;1353 889- c.buf = buf;890- c.count = count;1354+ c.buf = buf;1355+ c.count = count;1356 892- STB_SPRINTF_DECORATE( vsprintfcb )( stbsp__clamp_callback, &c, stbsp__clamp_callback(0,&c,0), fmt, va );893- 894- // zero-terminate895- l = (int)( c.buf - buf );896- if ( l >= count ) // should never be greater, only equal (or less) than count897- l = count - 1;898- buf[l] = 0;1357+ STB_SPRINTF_DECORATE(vsprintfcb)(stbsp__clamp_callback, &c, stbsp__clamp_callback(0, &c, 0), fmt, va);1358 1359+ // zero-terminate1360+ l = (int)(c.buf - buf);1361+ if (l >= count) // should never be greater, only equal (or less) than count1362+ l = count - 1;1363+ buf[l] = 0;1364+900- return l;1365+ return l;1366 }1367 903-STBSP__PUBLICDEF int STB_SPRINTF_DECORATE( snprintf )( char * buf, int count, char const * fmt, ... )1368+STBSP__PUBLICDEF int STB_SPRINTF_DECORATE(snprintf)(char *buf, int count, char const *fmt, ...)1369 {905- int result;906- va_list va;907- va_start( va, fmt );1370+ int result;1371+ va_list va;1372+ va_start(va, fmt);1373 909- result = STB_SPRINTF_DECORATE( vsnprintf )( buf, count, fmt, va );910- va_end(va);1374+ result = STB_SPRINTF_DECORATE(vsnprintf)(buf, count, fmt, va);1375+ va_end(va);1376 912- return result;1377+ return result;1378 }1379 915-STBSP__PUBLICDEF int STB_SPRINTF_DECORATE( vsprintf )( char * buf, char const * fmt, va_list va )1380+STBSP__PUBLICDEF int STB_SPRINTF_DECORATE(vsprintf)(char *buf, char const *fmt, va_list va)1381 {917- return STB_SPRINTF_DECORATE( vsprintfcb )( 0, 0, buf, fmt, va );1382+ return STB_SPRINTF_DECORATE(vsprintfcb)(0, 0, buf, fmt, va);1383 }1384 1385 // =======================================================================1386 // low level float utility functions1387 1388 #ifndef STB_SPRINTF_NOFLOAT1389 1390 // copies d to bits w/ strict aliasing (this compiles to nothing on /Ox)926-#define STBSP__COPYFP(dest,src) { int cn; for(cn=0;cn<8;cn++) ((char*)&dest)[cn]=((char*)&src)[cn]; }927- 1391+#define STBSP__COPYFP(dest, src) \1392+ { \1393+ int cn; \1394+ for (cn = 0; cn < 8; cn++) \1395+ ((char *)&dest)[cn] = ((char *)&src)[cn]; \1396+ }1397+1398 // get float info929-static stbsp__int32 stbsp__real_to_parts( stbsp__int64 * bits, stbsp__int32 * expo, double value )1399+static stbsp__int32 stbsp__real_to_parts(stbsp__int64 *bits, stbsp__int32 *expo, double value)1400 {931- double d;932- stbsp__int64 b = 0;1401+ double d;1402+ stbsp__int64 b = 0;1403 934- // load value and round at the frac_digits935- d = value;1404+ // load value and round at the frac_digits1405+ d = value;1406 937- STBSP__COPYFP( b, d );1407+ STBSP__COPYFP(b, d);1408 939- *bits = b & ((((stbsp__uint64)1)<<52)-1);940- *expo = (stbsp__int32) (((b >> 52) & 2047)-1023);941- 942- return (stbsp__int32)(b >> 63);1409+ *bits = b & ((((stbsp__uint64)1) << 52) - 1);1410+ *expo = (stbsp__int32)(((b >> 52) & 2047) - 1023);1411+1412+ return (stbsp__int32)(b >> 63);1413 }1414 945-static double const stbsp__bot[23]={1e+000,1e+001,1e+002,1e+003,1e+004,1e+005,1e+006,1e+007,1e+008,1e+009,1e+010,1e+011,1e+012,1e+013,1e+014,1e+015,1e+016,1e+017,1e+018,1e+019,1e+020,1e+021,1e+022};946-static double const stbsp__negbot[22]={1e-001,1e-002,1e-003,1e-004,1e-005,1e-006,1e-007,1e-008,1e-009,1e-010,1e-011,1e-012,1e-013,1e-014,1e-015,1e-016,1e-017,1e-018,1e-019,1e-020,1e-021,1e-022};947-static double const stbsp__negboterr[22]={-5.551115123125783e-018,-2.0816681711721684e-019,-2.0816681711721686e-020,-4.7921736023859299e-021,-8.1803053914031305e-022,4.5251888174113741e-023,4.5251888174113739e-024,-2.0922560830128471e-025,-6.2281591457779853e-026,-3.6432197315497743e-027,6.0503030718060191e-028,2.0113352370744385e-029,-3.0373745563400371e-030,1.1806906454401013e-032,-7.7705399876661076e-032,2.0902213275965398e-033,-7.1542424054621921e-034,-7.1542424054621926e-035,2.4754073164739869e-036,5.4846728545790429e-037,9.2462547772103625e-038,-4.8596774326570872e-039};948-static double const stbsp__top[13]={1e+023,1e+046,1e+069,1e+092,1e+115,1e+138,1e+161,1e+184,1e+207,1e+230,1e+253,1e+276,1e+299};949-static double const stbsp__negtop[13]={1e-023,1e-046,1e-069,1e-092,1e-115,1e-138,1e-161,1e-184,1e-207,1e-230,1e-253,1e-276,1e-299};950-static double const stbsp__toperr[13]={8388608,6.8601809640529717e+028,-7.253143638152921e+052,-4.3377296974619174e+075,-1.5559416129466825e+098,-3.2841562489204913e+121,-3.7745893248228135e+144,-1.7356668416969134e+167,-3.8893577551088374e+190,-9.9566444326005119e+213,6.3641293062232429e+236,-5.2069140800249813e+259,-5.2504760255204387e+282};951-static double const stbsp__negtoperr[13]={3.9565301985100693e-040,-2.299904345391321e-063,3.6506201437945798e-086,1.1875228833981544e-109,-5.0644902316928607e-132,-6.7156837247865426e-155,-2.812077463003139e-178,-5.7778912386589953e-201,7.4997100559334532e-224,-4.6439668915134491e-247,-6.3691100762962136e-270,-9.436808465446358e-293,8.0970921678014997e-317};1415+static double const stbsp__bot[23] = {1416+ 1e+000, 1e+001, 1e+002, 1e+003, 1e+004, 1e+005, 1e+006, 1e+007, 1e+008, 1e+009, 1e+010, 1e+011,1417+ 1e+012, 1e+013, 1e+014, 1e+015, 1e+016, 1e+017, 1e+018, 1e+019, 1e+020, 1e+021, 1e+0221418+};1419+static double const stbsp__negbot[22] = {1420+ 1e-001, 1e-002, 1e-003, 1e-004, 1e-005, 1e-006, 1e-007, 1e-008, 1e-009, 1e-010, 1e-011,1421+ 1e-012, 1e-013, 1e-014, 1e-015, 1e-016, 1e-017, 1e-018, 1e-019, 1e-020, 1e-021, 1e-0221422+};1423+static double const stbsp__negboterr[22] = {1424+ -5.551115123125783e-018, -2.0816681711721684e-019, -2.0816681711721686e-020, -4.7921736023859299e-021, -8.1803053914031305e-022, 4.5251888174113741e-023,1425+ 4.5251888174113739e-024, -2.0922560830128471e-025, -6.2281591457779853e-026, -3.6432197315497743e-027, 6.0503030718060191e-028, 2.0113352370744385e-029,1426+ -3.0373745563400371e-030, 1.1806906454401013e-032, -7.7705399876661076e-032, 2.0902213275965398e-033, -7.1542424054621921e-034, -7.1542424054621926e-035,1427+ 2.4754073164739869e-036, 5.4846728545790429e-037, 9.2462547772103625e-038, -4.8596774326570872e-0391428+};1429+static double const stbsp__top[13] = {1430+ 1e+023, 1e+046, 1e+069, 1e+092, 1e+115, 1e+138, 1e+161, 1e+184, 1e+207, 1e+230, 1e+253, 1e+276, 1e+2991431+};1432+static double const stbsp__negtop[13] = {1433+ 1e-023, 1e-046, 1e-069, 1e-092, 1e-115, 1e-138, 1e-161, 1e-184, 1e-207, 1e-230, 1e-253, 1e-276, 1e-2991434+};1435+static double const stbsp__toperr[13] = {1436+ 8388608,1437+ 6.8601809640529717e+028,1438+ -7.253143638152921e+052,1439+ -4.3377296974619174e+075,1440+ -1.5559416129466825e+098,1441+ -3.2841562489204913e+121,1442+ -3.7745893248228135e+144,1443+ -1.7356668416969134e+167,1444+ -3.8893577551088374e+190,1445+ -9.9566444326005119e+213,1446+ 6.3641293062232429e+236,1447+ -5.2069140800249813e+259,1448+ -5.2504760255204387e+2821449+};1450+static double const stbsp__negtoperr[13] = {1451+ 3.9565301985100693e-040, -2.299904345391321e-063, 3.6506201437945798e-086, 1.1875228833981544e-109,1452+ -5.0644902316928607e-132, -6.7156837247865426e-155, -2.812077463003139e-178, -5.7778912386589953e-201,1453+ 7.4997100559334532e-224, -4.6439668915134491e-247, -6.3691100762962136e-270, -9.436808465446358e-293,1454+ 8.0970921678014997e-3171455+};1456 953-#if defined(_MSC_VER) && (_MSC_VER<=1200) 954-static stbsp__uint64 const stbsp__powten[20]={1,10,100,1000, 10000,100000,1000000,10000000, 100000000,1000000000,10000000000,100000000000, 1000000000000,10000000000000,100000000000000,1000000000000000, 10000000000000000,100000000000000000,1000000000000000000,10000000000000000000U };1457+#if defined(_MSC_VER) && (_MSC_VER <= 1200)1458+static stbsp__uint64 const stbsp__powten[20] = {1459+ 1,1460+ 10,1461+ 100,1462+ 1000,1463+ 10000,1464+ 100000,1465+ 1000000,1466+ 10000000,1467+ 100000000,1468+ 1000000000,1469+ 10000000000,1470+ 100000000000,1471+ 1000000000000,1472+ 10000000000000,1473+ 100000000000000,1474+ 1000000000000000,1475+ 10000000000000000,1476+ 100000000000000000,1477+ 1000000000000000000,1478+ 10000000000000000000U1479+};1480 #define stbsp__tento19th ((stbsp__uint64)1000000000000000000)1481 #else957-static stbsp__uint64 const stbsp__powten[20]={1,10,100,1000, 10000,100000,1000000,10000000, 100000000,1000000000,10000000000ULL,100000000000ULL, 1000000000000ULL,10000000000000ULL,100000000000000ULL,1000000000000000ULL, 10000000000000000ULL,100000000000000000ULL,1000000000000000000ULL,10000000000000000000ULL };1482+static stbsp__uint64 const stbsp__powten[20] = {1483+ 1,1484+ 10,1485+ 100,1486+ 1000,1487+ 10000,1488+ 100000,1489+ 1000000,1490+ 10000000,1491+ 100000000,1492+ 1000000000,1493+ 10000000000ULL,1494+ 100000000000ULL,1495+ 1000000000000ULL,1496+ 10000000000000ULL,1497+ 100000000000000ULL,1498+ 1000000000000000ULL,1499+ 10000000000000000ULL,1500+ 100000000000000000ULL,1501+ 1000000000000000000ULL,1502+ 10000000000000000000ULL1503+};1504 #define stbsp__tento19th (1000000000000000000ULL)1505 #endif1506 961-#define stbsp__ddmulthi(oh,ol,xh,yh) \962-{ \963- double ahi=0,alo,bhi=0,blo; \964- stbsp__int64 bt; \965- oh = xh * yh; \966- STBSP__COPYFP(bt,xh); bt&=((~(stbsp__uint64)0)<<27); STBSP__COPYFP(ahi,bt); alo = xh-ahi; \967- STBSP__COPYFP(bt,yh); bt&=((~(stbsp__uint64)0)<<27); STBSP__COPYFP(bhi,bt); blo = yh-bhi; \968- ol = ((ahi*bhi-oh)+ahi*blo+alo*bhi)+alo*blo; \969-}1507+#define stbsp__ddmulthi(oh, ol, xh, yh) \1508+ { \1509+ double ahi = 0, alo, bhi = 0, blo; \1510+ stbsp__int64 bt; \1511+ oh = xh * yh; \1512+ STBSP__COPYFP(bt, xh); \1513+ bt &= ((~(stbsp__uint64)0) << 27); \1514+ STBSP__COPYFP(ahi, bt); \1515+ alo = xh - ahi; \1516+ STBSP__COPYFP(bt, yh); \1517+ bt &= ((~(stbsp__uint64)0) << 27); \1518+ STBSP__COPYFP(bhi, bt); \1519+ blo = yh - bhi; \1520+ ol = ((ahi * bhi - oh) + ahi * blo + alo * bhi) + alo * blo; \1521+ }1522 971-#define stbsp__ddtoS64(ob,xh,xl) \972-{ \973- double ahi=0,alo,vh,t;\974- ob = (stbsp__int64)ph;\975- vh=(double)ob;\976- ahi = ( xh - vh );\977- t = ( ahi - xh );\978- alo = (xh-(ahi-t))-(vh+t);\979- ob += (stbsp__int64)(ahi+alo+xl);\980-}1523+#define stbsp__ddtoS64(ob, xh, xl) \1524+ { \1525+ double ahi = 0, alo, vh, t; \1526+ ob = (stbsp__int64)ph; \1527+ vh = (double)ob; \1528+ ahi = (xh - vh); \1529+ t = (ahi - xh); \1530+ alo = (xh - (ahi - t)) - (vh + t); \1531+ ob += (stbsp__int64)(ahi + alo + xl); \1532+ }1533 1534+#define stbsp__ddrenorm(oh, ol) \1535+ { \1536+ double s; \1537+ s = oh + ol; \1538+ ol = ol - (s - oh); \1539+ oh = s; \1540+ }1541 983-#define stbsp__ddrenorm(oh,ol) { double s; s=oh+ol; ol=ol-(s-oh); oh=s; }1542+#define stbsp__ddmultlo(oh, ol, xh, xl, yh, yl) ol = ol + (xh * yl + xl * yh);1543 985-#define stbsp__ddmultlo(oh,ol,xh,xl,yh,yl) \986- ol = ol + ( xh*yl + xl*yh ); \1544+#define stbsp__ddmultlos(oh, ol, xh, yl) ol = ol + (xh * yl);1545 988-#define stbsp__ddmultlos(oh,ol,xh,yl) \989- ol = ol + ( xh*yl ); \990-991-static void stbsp__raise_to_power10( double *ohi, double *olo, double d, stbsp__int32 power ) // power can be -323 to +3501546+static void stbsp__raise_to_power10(double *ohi, double *olo, double d, stbsp__int32 power) // power can be -323 to +3501547 {993- double ph, pl;994- if ((power>=0) && (power<=22))995- {996- stbsp__ddmulthi(ph,pl,d,stbsp__bot[power]);997- }998- else999- {1000- stbsp__int32 e,et,eb;1001- double p2h,p2l;1548+ double ph, pl;1549+ if ((power >= 0) && (power <= 22)) {1550+ stbsp__ddmulthi(ph, pl, d, stbsp__bot[power]);1551+ } else {1552+ stbsp__int32 e, et, eb;1553+ double p2h, p2l;1554 1003- e=power; if (power<0) e=-e; 1004- et = (e*0x2c9)>>14;/* %23 */ if (et>13) et=13; eb = e-(et*23);1555+ e = power;1556+ if (power < 0)1557+ e = -e;1558+ et = (e * 0x2c9) >> 14; /* %23 */1559+ if (et > 13)1560+ et = 13;1561+ eb = e - (et * 23);1562 1006- ph = d; pl = 0.0;1007- if (power<0)1008- {1009- if (eb) { --eb; stbsp__ddmulthi(ph,pl,d,stbsp__negbot[eb]); stbsp__ddmultlos(ph,pl,d,stbsp__negboterr[eb]); }1010- if (et)1011- { 1012- stbsp__ddrenorm(ph,pl);1013- --et; stbsp__ddmulthi(p2h,p2l,ph,stbsp__negtop[et]); stbsp__ddmultlo(p2h,p2l,ph,pl,stbsp__negtop[et],stbsp__negtoperr[et]); ph=p2h;pl=p2l;1563+ ph = d;1564+ pl = 0.0;1565+ if (power < 0) {1566+ if (eb) {1567+ --eb;1568+ stbsp__ddmulthi(ph, pl, d, stbsp__negbot[eb]);1569+ stbsp__ddmultlos(ph, pl, d, stbsp__negboterr[eb]);1570+ }1571+ if (et) {1572+ stbsp__ddrenorm(ph, pl);1573+ --et;1574+ stbsp__ddmulthi(p2h, p2l, ph, stbsp__negtop[et]);1575+ stbsp__ddmultlo(p2h, p2l, ph, pl, stbsp__negtop[et], stbsp__negtoperr[et]);1576+ ph = p2h;1577+ pl = p2l;1578+ }1579+ } else {1580+ if (eb) {1581+ e = eb;1582+ if (eb > 22)1583+ eb = 22;1584+ e -= eb;1585+ stbsp__ddmulthi(ph, pl, d, stbsp__bot[eb]);1586+ if (e) {1587+ stbsp__ddrenorm(ph, pl);1588+ stbsp__ddmulthi(p2h, p2l, ph, stbsp__bot[e]);1589+ stbsp__ddmultlos(p2h, p2l, stbsp__bot[e], pl);1590+ ph = p2h;1591+ pl = p2l;1592+ }1593+ }1594+ if (et) {1595+ stbsp__ddrenorm(ph, pl);1596+ --et;1597+ stbsp__ddmulthi(p2h, p2l, ph, stbsp__top[et]);1598+ stbsp__ddmultlo(p2h, p2l, ph, pl, stbsp__top[et], stbsp__toperr[et]);1599+ ph = p2h;1600+ pl = p2l;1601+ }1602 }1015- }1016- else1017- {1018- if (eb) 1019- { 1020- e = eb; if (eb>22) eb=22; e -= eb;1021- stbsp__ddmulthi(ph,pl,d,stbsp__bot[eb]); 1022- if ( e ) { stbsp__ddrenorm(ph,pl); stbsp__ddmulthi(p2h,p2l,ph,stbsp__bot[e]); stbsp__ddmultlos(p2h,p2l,stbsp__bot[e],pl); ph=p2h;pl=p2l; }1023- }1024- if (et)1025- {1026- stbsp__ddrenorm(ph,pl);1027- --et; stbsp__ddmulthi(p2h,p2l,ph,stbsp__top[et]); stbsp__ddmultlo(p2h,p2l,ph,pl,stbsp__top[et],stbsp__toperr[et]); ph=p2h;pl=p2l;1028- }1029- }1030- }1031- stbsp__ddrenorm(ph,pl);1032- *ohi = ph; *olo = pl;1603+ }1604+ stbsp__ddrenorm(ph, pl);1605+ *ohi = ph;1606+ *olo = pl;1607 }1608 1609 // given a float value, returns the significant bits in bits, and the position of the1610 // decimal point in decimal_pos. +/-INF and NAN are specified by special values1611 // returned in the decimal_pos parameter.1612 // frac_digits is absolute normally, but if you want from first significant digits (got %g and %e), or in 0x800000001039-static stbsp__int32 stbsp__real_to_str( char const * * start, stbsp__uint32 * len, char *out, stbsp__int32 * decimal_pos, double value, stbsp__uint32 frac_digits )1613+static stbsp__int32 stbsp__real_to_str(char const **start, stbsp__uint32 *len, char *out, stbsp__int32 *decimal_pos, double value, stbsp__uint32 frac_digits)1614 {1041- double d;1042- stbsp__int64 bits = 0;1043- stbsp__int32 expo, e, ng, tens;1615+ double d;1616+ stbsp__int64 bits = 0;1617+ stbsp__int32 expo, e, ng, tens;1618 1045- d = value;1046- STBSP__COPYFP(bits,d);1047- expo = (stbsp__int32) ((bits >> 52) & 2047);1048- ng = (stbsp__int32)(bits >> 63);1049- if (ng) d=-d;1619+ d = value;1620+ STBSP__COPYFP(bits, d);1621+ expo = (stbsp__int32)((bits >> 52) & 2047);1622+ ng = (stbsp__int32)(bits >> 63);1623+ if (ng)1624+ d = -d;1625 1051- if ( expo == 2047 ) // is nan or inf?1052- {1053- *start = (bits&((((stbsp__uint64)1)<<52)-1)) ? "NaN" : "Inf";1054- *decimal_pos = STBSP__SPECIAL;1055- *len = 3;1056- return ng;1057- } 1058-1059- if ( expo == 0 ) // is zero or denormal1060- {1061- if ((bits<<1)==0) // do zero1062- {1063- *decimal_pos = 1; 1064- *start = out;1065- out[0] = '0'; *len = 1;1626+ if (expo == 2047) // is nan or inf?1627+ {1628+ *start = (bits & ((((stbsp__uint64)1) << 52) - 1)) ? "NaN" : "Inf";1629+ *decimal_pos = STBSP__SPECIAL;1630+ *len = 3;1631 return ng;1067- }1068- // find the right expo for denormals1069- {1070- stbsp__int64 v = ((stbsp__uint64)1)<<51;1071- while ((bits&v)==0) { --expo; v >>= 1; }1072- }1073- }1632+ }1633 1075- // find the decimal exponent as well as the decimal bits of the value1076- {1077- double ph,pl;1634+ if (expo == 0) // is zero or denormal1635+ {1636+ if ((bits << 1) == 0) // do zero1637+ {1638+ *decimal_pos = 1;1639+ *start = out;1640+ out[0] = '0';1641+ *len = 1;1642+ return ng;1643+ }1644+ // find the right expo for denormals1645+ {1646+ stbsp__int64 v = ((stbsp__uint64)1) << 51;1647+ while ((bits & v) == 0) {1648+ --expo;1649+ v >>= 1;1650+ }1651+ }1652+ }1653 1079- // log10 estimate - very specifically tweaked to hit or undershoot by no more than 1 of log10 of all expos 1..20461080- tens=expo-1023; tens = (tens<0)?((tens*617)/2048):(((tens*1233)/4096)+1);1654+ // find the decimal exponent as well as the decimal bits of the value1655+ {1656+ double ph, pl;1657 1082- // move the significant bits into position and stick them into an int 1083- stbsp__raise_to_power10( &ph, &pl, d, 18-tens );1658+ // log10 estimate - very specifically tweaked to hit or undershoot by no more than 1 of log10 of all expos 1..20461659+ tens = expo - 1023;1660+ tens = (tens < 0) ? ((tens * 617) / 2048) : (((tens * 1233) / 4096) + 1);1661 1085- // get full as much precision from double-double as possible1086- stbsp__ddtoS64( bits, ph,pl );1662+ // move the significant bits into position and stick them into an int1663+ stbsp__raise_to_power10(&ph, &pl, d, 18 - tens);1664 1088- // check if we undershot1089- if ( ((stbsp__uint64)bits) >= stbsp__tento19th ) ++tens; 1090- }1665+ // get full as much precision from double-double as possible1666+ stbsp__ddtoS64(bits, ph, pl);1667 1092- // now do the rounding in integer land1093- frac_digits = ( frac_digits & 0x80000000 ) ? ( (frac_digits&0x7ffffff) + 1 ) : ( tens + frac_digits );1094- if ( ( frac_digits < 24 ) )1095- {1096- stbsp__uint32 dg = 1; if ((stbsp__uint64)bits >= stbsp__powten[9] ) dg=10; while( (stbsp__uint64)bits >= stbsp__powten[dg] ) { ++dg; if (dg==20) goto noround; }1097- if ( frac_digits < dg )1098- {1099- stbsp__uint64 r;1100- // add 0.5 at the right position and round1101- e = dg - frac_digits;1102- if ( (stbsp__uint32)e >= 24 ) goto noround;1103- r = stbsp__powten[e];1104- bits = bits + (r/2);1105- if ( (stbsp__uint64)bits >= stbsp__powten[dg] ) ++tens;1106- bits /= r;1107- } 1108- noround:;1109- }1668+ // check if we undershot1669+ if (((stbsp__uint64)bits) >= stbsp__tento19th)1670+ ++tens;1671+ }1672 1111- // kill long trailing runs of zeros1112- if ( bits )1113- {1114- stbsp__uint32 n;1115- for(;;) { if ( bits<=0xffffffff ) break; if (bits%1000) goto donez; bits/=1000; }1116- n = (stbsp__uint32)bits;1117- while ((n%1000)==0) n/=1000;1118- bits=n;1119- donez:;1120- }1673+ // now do the rounding in integer land1674+ frac_digits = (frac_digits & 0x80000000) ? ((frac_digits & 0x7ffffff) + 1) : (tens + frac_digits);1675+ if ((frac_digits < 24)) {1676+ stbsp__uint32 dg = 1;1677+ if ((stbsp__uint64)bits >= stbsp__powten[9])1678+ dg = 10;1679+ while ((stbsp__uint64)bits >= stbsp__powten[dg]) {1680+ ++dg;1681+ if (dg == 20)1682+ goto noround;1683+ }1684+ if (frac_digits < dg) {1685+ stbsp__uint64 r;1686+ // add 0.5 at the right position and round1687+ e = dg - frac_digits;1688+ if ((stbsp__uint32)e >= 24)1689+ goto noround;1690+ r = stbsp__powten[e];1691+ bits = bits + (r / 2);1692+ if ((stbsp__uint64)bits >= stbsp__powten[dg])1693+ ++tens;1694+ bits /= r;1695+ }1696+ noround:;1697+ }1698 1122- // convert to string1123- out += 64;1124- e = 0; 1125- for(;;)1126- {1127- stbsp__uint32 n;1128- char * o = out-8;1129- // do the conversion in chunks of U32s (avoid most 64-bit divides, worth it, constant denomiators be damned)1130- if (bits>=100000000) { n = (stbsp__uint32)( bits % 100000000); bits /= 100000000; } else {n = (stbsp__uint32)bits; bits = 0; }1131- while(n) { out-=2; *(stbsp__uint16*)out=*(stbsp__uint16*)&stbsp__digitpair[(n%100)*2]; n/=100; e+=2; }1132- if (bits==0) { if ((e) && (out[0]=='0')) { ++out; --e; } break; }1133- while( out!=o ) { *--out ='0'; ++e; }1134- }1135- 1136- *decimal_pos = tens;1137- *start = out;1138- *len = e;1139- return ng;1699+ // kill long trailing runs of zeros1700+ if (bits) {1701+ stbsp__uint32 n;1702+ for (;;) {1703+ if (bits <= 0xffffffff)1704+ break;1705+ if (bits % 1000)1706+ goto donez;1707+ bits /= 1000;1708+ }1709+ n = (stbsp__uint32)bits;1710+ while ((n % 1000) == 0)1711+ n /= 1000;1712+ bits = n;1713+ donez:;1714+ }1715+1716+ // convert to string1717+ out += 64;1718+ e = 0;1719+ for (;;) {1720+ stbsp__uint32 n;1721+ char *o = out - 8;1722+ // do the conversion in chunks of U32s (avoid most 64-bit divides, worth it, constant denomiators be damned)1723+ if (bits >= 100000000) {1724+ n = (stbsp__uint32)(bits % 100000000);1725+ bits /= 100000000;1726+ } else {1727+ n = (stbsp__uint32)bits;1728+ bits = 0;1729+ }1730+ while (n) {1731+ out -= 2;1732+ *(stbsp__uint16 *)out = *(stbsp__uint16 *)&stbsp__digitpair[(n % 100) * 2];1733+ n /= 100;1734+ e += 2;1735+ }1736+ if (bits == 0) {1737+ if ((e) && (out[0] == '0')) {1738+ ++out;1739+ --e;1740+ }1741+ break;1742+ }1743+ while (out != o) {1744+ *--out = '0';1745+ ++e;1746+ }1747+ }1748+1749+ *decimal_pos = tens;1750+ *start = out;1751+ *len = e;1752+ return ng;1753 }1754 1755 #undef stbsp__ddmulthi1756 #undef stbsp__ddrenorm1757 #undef stbsp__ddmultlo1758 #undef stbsp__ddmultlos1146-#undef STBSP__SPECIAL 1759+#undef STBSP__SPECIAL1760 #undef STBSP__COPYFP1148- 1761+1762 #endif // STB_SPRINTF_NOFLOAT1763 1764 // clean up1765 #undef stbsp__uint161153-#undef stbsp__uint32 1154-#undef stbsp__int32 1766+#undef stbsp__uint321767+#undef stbsp__int321768 #undef stbsp__uint641769 #undef stbsp__int641770 #undef STBSP__UNALIGNED1771 1772 #endif // STB_SPRINTF_IMPLEMENTATION1773 1161-1774 /*1775 ------------------------------------------------------------------------------1776 This software is available under 2 licenses -- choose whichever you prefer.1777 ------------------------------------------------------------------------------1778 ALTERNATIVE A - MIT License1779 Copyright (c) 2017 Sean Barrett1168-Permission is hereby granted, free of charge, to any person obtaining a copy of 1169-this software and associated documentation files (the "Software"), to deal in 1170-the Software without restriction, including without limitation the rights to 1171-use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 1172-of the Software, and to permit persons to whom the Software is furnished to do 1780+Permission is hereby granted, free of charge, to any person obtaining a copy of1781+this software and associated documentation files (the "Software"), to deal in1782+the Software without restriction, including without limitation the rights to1783+use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies1784+of the Software, and to permit persons to whom the Software is furnished to do1785 so, subject to the following conditions:1174-The above copyright notice and this permission notice shall be included in all 1786+The above copyright notice and this permission notice shall be included in all1787 copies or substantial portions of the Software.1176-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 1177-IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 1178-FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 1179-AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 1180-LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 1181-OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 1788+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR1789+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,1790+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE1791+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER1792+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,1793+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE1794 SOFTWARE.1795 ------------------------------------------------------------------------------1796 ALTERNATIVE B - Public Domain (www.unlicense.org)1797 This is free and unencumbered software released into the public domain.1186-Anyone is free to copy, modify, publish, use, compile, sell, or distribute this 1187-software, either in source code form or as a compiled binary, for any purpose, 1798+Anyone is free to copy, modify, publish, use, compile, sell, or distribute this1799+software, either in source code form or as a compiled binary, for any purpose,1800 commercial or non-commercial, and by any means.1189-In jurisdictions that recognize copyright laws, the author or authors of this 1190-software dedicate any and all copyright interest in the software to the public 1191-domain. We make this dedication for the benefit of the public at large and to 1192-the detriment of our heirs and successors. We intend this dedication to be an 1193-overt act of relinquishment in perpetuity of all present and future rights to 1801+In jurisdictions that recognize copyright laws, the author or authors of this1802+software dedicate any and all copyright interest in the software to the public1803+domain. We make this dedication for the benefit of the public at large and to1804+the detriment of our heirs and successors. We intend this dedication to be an1805+overt act of relinquishment in perpetuity of all present and future rights to1806 this software under copyright law.1195-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 1196-IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 1197-FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 1198-AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 1199-ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 1807+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR1808+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,1809+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE1810+AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN1811+ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION1812 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.1813 ------------------------------------------------------------------------------1814 */modified:stb_tilemap_editor.hnot shown
Its text is not shown, modified.
modified:stb_truetype.hnot shown
Its text is not shown, modified.
modified:stb_vorbis.cnot shown
Its text is not shown, modified.
modified:stb.h+51 added-45 removed
1-/* stb.h - v2.29 - Sean's Tool Box -- public domain -- http://nothings.org/stb.h1+/* stb.h - v2.31 - Sean's Tool Box -- public domain -- http://nothings.org/stb.h2 no warranty is offered or implied; use this code at your own risk3 4 This is a single header file with a bunch of useful utilities5 for getting stuff done in C/C++.18 unmodified lines24 25 26 Version History27 28+ 2.31 stb_ucharcmp29+ 2.30 MinGW fix30 2.29 attempt to fix use of swprintf()31 2.28 various new functionality32 2.27 test _WIN32 not WIN32 in STB_THREADS33 2.26 various warning & bugfixes152 unmodified lines186 Philipp Wiesemann187 Robert Nix188 r-lyeh189 blackpawn188- Mojofreem@github190+ github:Mojofreem191 Ryan Whitworth192 Vincent Isambart193 Mike Sartain194 Eugene Opalev195 Tim Sjostrand196+ github:infatum197+ Dave Butler (Croepha)198 */199 200+#include <stdarg.h>201+202 #ifndef STB__INCLUDE_STB_H203 #define STB__INCLUDE_STB_H204 205 #define STB_VERSION 11343 unmodified lines1549 STB_EXTERN int (*stb_doublecmp(int offset))(const void *a, const void *b);1550 STB_EXTERN int (*stb_charcmp(int offset))(const void *a, const void *b);1551 1552 #ifdef STB_DEFINE1547-static int stb__intcmpoffset, stb__charcmpoffset, stb__strcmpoffset;1553+static int stb__intcmpoffset, stb__ucharcmpoffset, stb__strcmpoffset;1554 static int stb__floatcmpoffset, stb__doublecmpoffset;1555 1556 int stb__intcmp(const void *a, const void *b)1557 {1 unmodified line1559 const int q = *(const int *) ((const char *) b + stb__intcmpoffset);1560 return p < q ? -1 : p > q;1561 }1562 1557-int stb__charcmp(const void *a, const void *b)1563+int stb__ucharcmp(const void *a, const void *b)1564 {1559- const int p = *(const unsigned char *) ((const char *) a + stb__charcmpoffset);1560- const int q = *(const unsigned char *) ((const char *) b + stb__charcmpoffset);1565+ const int p = *(const unsigned char *) ((const char *) a + stb__ucharcmpoffset);1566+ const int q = *(const unsigned char *) ((const char *) b + stb__ucharcmpoffset);1567 return p < q ? -1 : p > q;1568 }1569 1570 int stb__floatcmp(const void *a, const void *b)29 unmodified lines1600 stb__intcmpoffset = offset;1601 return &stb__intcmp;1602 }1603 1598-int (*stb_charcmp(int offset))(const void *, const void *)1604+int (*stb_ucharcmp(int offset))(const void *, const void *)1605 {1600- stb__charcmpoffset = offset;1601- return &stb__charcmp;1606+ stb__ucharcmpoffset = offset;1607+ return &stb__ucharcmp;1608 }1609 1610 int (*stb_qsort_strcmp(int offset))(const void *, const void *)1611 {17 unmodified lines1629 {1630 stb__doublecmpoffset = offset;1631 return &stb__doublecmp;1632 }1627-1633 #endif1634 1635 //////////////////////////////////////////////////////////////////////////////1636 //158 unmodified lines1795 STB_EXTERN int stb_prefix (char *s, char *t);1796 STB_EXTERN char * stb_strichr(char *s, char t);1797 STB_EXTERN char * stb_stristr(char *s, char *t);1798 STB_EXTERN int stb_prefix_count(char *s, char *t);1794-STB_EXTERN char * stb_plural(int n); // "s" or ""1799+STB_EXTERN const char * stb_plural(int n); // "s" or ""1800 STB_EXTERN size_t stb_strscpy(char *d, const char *s, size_t n);1801 1802 STB_EXTERN char **stb_tokens(char *src, char *delimit, int *count);1803 STB_EXTERN char **stb_tokens_nested(char *src, char *delimit, int *count, char *nest_in, char *nest_out);18 unmodified lines1822 strcpy(d,s);1823 return len + 1;1824 }1825 1821-char *stb_plural(int n)1826+const char *stb_plural(int n)1827 {1828 return n == 1 ? "" : "s";1829 }1830 1531 unmodified lines3362 }3363 3364 unsigned int stb_hashptr(void *p)3365 {3361- unsigned int x = (unsigned int) p;3366+ unsigned int x = (unsigned int)(size_t) p;3367 3368 // typically lacking in low bits and high bits3369 x = stb_rehash(x);3370 x += x << 16;40 unmodified lines3411 3412 if (len <= 0 || q == NULL) return 0;3413 3414 /* Main loop */3410- if (((int) q & 1) == 0) {3415+ if (((int)(size_t) q & 1) == 0) {3416 for (;len > 3; len -= 4) {3417 unsigned int val;3418 hash += stb__get16(q);3419 val = (stb__get16(q+2) << 11);317 unmodified lines3737 static stb_perfect p;3738 static unsigned char (*tables)[256];3739 static char ** sets = NULL;3740 3736- int z = stb_perfect_hash(&p, (int) set);3741+ int z = stb_perfect_hash(&p, (int)(size_t) set);3742 if (z < 0) {3743 int i,k,n,j,f;3744 // special code that means free all existing data3745 if (set == NULL) {10 unmodified lines3756 k = (n+7) >> 3;3757 tables = (unsigned char (*)[256]) realloc(tables, sizeof(*tables) * k);3758 memset(tables, 0, sizeof(*tables) * k);3759 for (i=0; i < stb_arr_len(sets); ++i) {3755- k = stb_perfect_hash(&p, (int) sets[i]);3760+ k = stb_perfect_hash(&p, (int)(size_t) sets[i]);3761 assert(k >= 0);3762 n = k >> 3;3763 f = bit[k&7];3764 for (j=0; !j || sets[i][j]; ++j) {3765 tables[n][(unsigned char) sets[i][j]] |= f;3766 }3767 }3763- z = stb_perfect_hash(&p, (int) set);3768+ z = stb_perfect_hash(&p, (int)(size_t) set);3769 }3770 return tables[z >> 3][(unsigned char) c] & bit[z & 7];3771 }3772 3910 unmodified lines7683 h->count_deletes = 0;7684 h->any_offset = 0;7685 memset(h->table, 0, size * sizeof(h->table[0]));7686 for (i=0; i < old_size; ++i)7682- if (!stb_ps_empty(old_data[i]))7687+ if (!stb_ps_empty((size_t)old_data[i]))7688 stb_ps_add(EncodeHash(h), old_data[i]);7689 return h;7690 }7691 7692 void stb_ps_delete(stb_ps *ps)7693 {7689- switch (3 & (int) ps) {7694+ switch (3 & (int)(size_t) ps) {7695 case STB_ps_direct: break;7696 case STB_ps_bucket: stb_bucket_free(GetBucket(ps)); break;7697 case STB_ps_array : free(GetArray(ps)); break;7698 case STB_ps_hash : free(GetHash(ps)); break;3 unmodified lines7702 stb_ps *stb_ps_copy(stb_ps *ps)7703 {7704 int i;7705 // not a switch: order based on expected performance/power-law distribution7701- switch (3 & (int) ps) {7706+ switch (3 & (int)(size_t) ps) {7707 case STB_ps_direct: return ps;7708 case STB_ps_bucket: {7709 stb_ps_bucket *n = (stb_ps_bucket *) malloc(sizeof(*n));7710 *n = *GetBucket(ps);18 unmodified lines7729 }7730 7731 int stb_ps_find(stb_ps *ps, void *value)7732 {7728- int i, code = 3 & (int) ps;7729- assert((3 & (int) value) == STB_ps_direct);7733+ int i, code = 3 & (int)(size_t) ps;7734+ assert((3 & (int)(size_t) value) == STB_ps_direct);7735 assert(stb_ps_fastlist_valid(value));7736 // not a switch: order based on expected performance/power-law distribution7737 if (code == STB_ps_direct)7738 return value == ps;32 unmodified lines7771 #ifdef STB_DEBUG7772 assert(!stb_ps_find(ps,value));7773 #endif7774 if (value == NULL) return ps; // ignore NULL adds to avoid bad breakage7770- assert((3 & (int) value) == STB_ps_direct);7775+ assert((3 & (int)(size_t) value) == STB_ps_direct);7776 assert(stb_ps_fastlist_valid(value));7777 assert(value != STB_DEL); // STB_DEL is less likely7778 7774- switch (3 & (int) ps) {7779+ switch (3 & (int)(size_t) ps) {7780 case STB_ps_direct:7781 if (ps == NULL) return (stb_ps *) value;7782 return EncodeBucket(stb_bucket_create2(ps,value));7783 40 unmodified lines7824 stb_uint32 hash = stb_hashptr(value);7825 stb_uint32 n = hash & h->mask;7826 void **t = h->table;7827 // find first NULL or STB_DEL entry7823- if (!stb_ps_empty(t[n])) {7828+ if (!stb_ps_empty((size_t)t[n])) {7829 stb_uint32 s = stb_rehash(hash) | 1;7830 do {7831 n = (n + s) & h->mask;7827- } while (!stb_ps_empty(t[n]));7832+ } while (!stb_ps_empty((size_t)t[n]));7833 }7834 if (t[n] == STB_DEL)7835 -- h->count_deletes;7836 t[n] = value;18 unmodified lines7855 {7856 #ifdef STB_DEBUG7857 assert(stb_ps_find(ps, value));7858 #endif7854- assert((3 & (int) value) == STB_ps_direct);7859+ assert((3 & (int)(size_t) value) == STB_ps_direct);7860 if (value == NULL) return ps; // ignore NULL removes to avoid bad breakage7856- switch (3 & (int) ps) {7861+ switch (3 & (int)(size_t) ps) {7862 case STB_ps_direct:7863 return ps == value ? NULL : ps;7864 case STB_ps_bucket: {7865 stb_ps_bucket *b = GetBucket(ps);50 unmodified lines7916 if (h->count < n) {7917 stb_ps_array *a = (stb_ps_array *) malloc(sizeof(*a) + (n-1) * sizeof(a->p[0]));7918 int i,j=0;7919 for (i=0; i < h->size; ++i)7915- if (!stb_ps_empty(t[i]))7920+ if (!stb_ps_empty((size_t)t[i]))7921 a->p[j++] = t[i];7922 assert(j == h->count);7923 a->count = j;7924 free(h);13 unmodified lines7938 7939 stb_ps *stb_ps_remove_any(stb_ps *ps, void **value)7940 {7941 assert(ps != NULL);7937- switch (3 & (int) ps) {7942+ switch (3 & (int)(size_t) ps) {7943 case STB_ps_direct:7944 *value = ps;7945 return NULL;7946 case STB_ps_bucket: {24 unmodified lines7971 case STB_ps_hash: {7972 stb_ps_hash *h = GetHash(ps);7973 void **t = h->table;7974 stb_uint32 n = h->any_offset;7970- while (stb_ps_empty(t[n]))7975+ while (stb_ps_empty((size_t)t[n]))7976 n = (n + 1) & h->mask;7977 *value = t[n];7978 h->any_offset = (n+1) & h->mask;7979 // check if we need to skip down to the previous type12 unmodified lines7992 void ** stb_ps_getlist(stb_ps *ps, int *count)7993 {7994 int i,n=0;7995 void **p = NULL;7991- switch (3 & (int) ps) {7996+ switch (3 & (int)(size_t) ps) {7997 case STB_ps_direct:7998 if (ps == NULL) { *count = 0; return NULL; }7999 p = (void **) malloc(sizeof(*p) * 1);8000 p[0] = ps;17 unmodified lines8018 case STB_ps_hash: {8019 stb_ps_hash *h = GetHash(ps);8020 p = (void **) malloc(sizeof(*p) * h->count);8021 for (i=0; i < h->size; ++i)8017- if (!stb_ps_empty(h->table[i]))8022+ if (!stb_ps_empty((size_t)h->table[i]))8023 p[n++] = h->table[i];8024 break;8025 }8026 }3 unmodified lines8030 8031 int stb_ps_writelist(stb_ps *ps, void **list, int size )8032 {8033 int i,n=0;8029- switch (3 & (int) ps) {8034+ switch (3 & (int)(size_t) ps) {8035 case STB_ps_direct:8036 if (ps == NULL || size <= 0) return 0;8037 list[0] = ps;8038 return 1;13 unmodified lines8052 case STB_ps_hash: {8053 stb_ps_hash *h = GetHash(ps);8054 if (size <= 0) return 0;8055 for (i=0; i < h->count; ++i) {8051- if (!stb_ps_empty(h->table[i])) {8056+ if (!stb_ps_empty((size_t)h->table[i])) {8057 list[n++] = h->table[i];8058 if (n == size) break;8059 }8060 }5 unmodified lines8066 8067 int stb_ps_enum(stb_ps *ps, void *data, int (*func)(void *value, void *data))8068 {8069 int i;8065- switch (3 & (int) ps) {8070+ switch (3 & (int)(size_t) ps) {8071 case STB_ps_direct:8072 if (ps == NULL) return STB_TRUE;8073 return func(ps, data);8074 case STB_ps_bucket: {13 unmodified lines8088 }8089 case STB_ps_hash: {8090 stb_ps_hash *h = GetHash(ps);8091 for (i=0; i < h->count; ++i)8087- if (!stb_ps_empty(h->table[i]))8092+ if (!stb_ps_empty((size_t)h->table[i]))8093 if (!func(h->table[i], data))8094 return STB_FALSE;8095 return STB_TRUE;8096 }2 unmodified lines8099 }8100 8101 int stb_ps_count (stb_ps *ps)8102 {8098- switch (3 & (int) ps) {8103+ switch (3 & (int)(size_t) ps) {8104 case STB_ps_direct:8105 return ps != NULL;8106 case STB_ps_bucket: {8107 stb_ps_bucket *b = GetBucket(ps);15 unmodified lines8123 void ** stb_ps_fastlist(stb_ps *ps, int *count)8124 {8125 static void *storage;8126 8122- switch (3 & (int) ps) {8127+ switch (3 & (int)(size_t) ps) {8128 case STB_ps_direct:8129 if (ps == NULL) { *count = 0; return NULL; }8130 storage = ps;8131 *count = 1;1038 unmodified lines9170 }9171 9172 static void stb__add_edge(stb_matcher *matcher, int from, int to, int type)9173 {9169- stb_nfa_edge z = { type, to };9174+ stb_nfa_edge z = { (stb_int16)type, (stb_uint16)to };9175 if (matcher->nodes[from].out == NULL)9176 stb_arr_malloc((void **) &matcher->nodes[from].out, matcher);9177 stb_arr_push(matcher->nodes[from].out, z);9178 }704 unmodified lines9883 static stb_matcher ** matchers;9884 static char ** regexps;9885 static char ** regexp_cache;9886 static unsigned short *mapping;9882- int z = stb_perfect_hash(&p, (int) regex);9887+ int z = stb_perfect_hash(&p, (int)(size_t) regex);9888 if (z >= 0) {9889 if (strcmp(regex, regexp_cache[(int) mapping[z]])) {9890 int i = mapping[z];9891 stb_matcher_free(matchers[i]);22 unmodified lines9914 stb_perfect_destroy(&p);9915 n = stb_perfect_create(&p, (unsigned int *) (char **) regexps, stb_arr_len(regexps));9916 mapping = (unsigned short *) realloc(mapping, n * sizeof(*mapping));9917 for (i=0; i < stb_arr_len(regexps); ++i)9913- mapping[stb_perfect_hash(&p, (int) regexps[i])] = i;9914- z = stb_perfect_hash(&p, (int) regex);9918+ mapping[stb_perfect_hash(&p, (int)(size_t) regexps[i])] = i;9919+ z = stb_perfect_hash(&p, (int)(size_t) regex);9920 }9921 return stb_matcher_find(matchers[(int) mapping[z]], str);9922 }9923 485 unmodified lines10409 fputc(v, stb__outfile);10410 ++stb__outbytes;10411 }10412 10408-#define stb_out(v) (stb__out ? *stb__out++ = (stb_uchar) (v) : stb__write((stb_uchar) (v)))10413+#define stb_out(v) (stb__out ? (void)(*stb__out++ = (stb_uchar) (v)) : stb__write((stb_uchar) (v)))10414 10415 static void stb_out2(stb_uint v)10416 {10417 stb_out(v >> 8);254 unmodified lines10672 }10673 10674 static void stb_out_backpatch(size_t id, stb_uint value)10675 {10671- stb_uchar data[4] = { value >> 24, value >> 16, value >> 8, value };10676+ 10677+ stb_uchar data[4] = { (stb_uchar)(value >> 24), (stb_uchar)(value >> 16), (stb_uchar)(value >> 8), (stb_uchar)(value) };10678 if (stb__out) {10679 memcpy((void *) id, data, 4);10680 } else {10681 stb_uint where = ftell(stb__outfile);3701 unmodified linesmodified:stretchy_buffer.hnot shown
Its text is not shown, modified.