STBI_MALLOC etc.
Before adoption: this commit came in with the history the project adopted, so no landing records it.
some documentation cleanup
authored by Sean Barrett · committed by Sean Barrett · parent bd6b78f · Browse files · 2 files·+111 added−78 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+111 added−78 removed
- tests:+8 added−0 removed
- other:+103 added−78 removed
Provenance
- Commit
ba5e333- Tree
f9addb3- Author
Sean Barrett <sean2@nothings.org>- Committer
Sean Barrett <sean2@nothings.org>- Parent
bd6b78f
2 files changed+111 added−78 removed
modified:tests/test_cpp_compilation.cpp+8 added
6 unmodified lines7 #define STB_IMAGE_IMPLEMENTATION8 #define STB_HERRINGBONE_WANG_TILE_IMPLEMENTATION9 #define STB_RECT_PACK_IMPLEMENTATION10 11+#define STBI_MALLOC my_malloc12+#define STBI_FREE my_free13+#define STBI_REALLOC my_realloc14+15+void *my_malloc(size_t) { return 0; }16+void *my_realloc(void *, size_t) { return 0; }17+void my_free(void *) { }18+19 #include "stb_image.h"20 #include "stb_rect_pack.h"21 #include "stb_truetype.h"22 #include "stb_image_write.h"10 unmodified linesmodified:stb_image.h+103 added-78 removed
130 unmodified lines131 //132 // Limitations:133 // - no jpeg progressive support134 // - no 16-bit-per-channel PNG135-// - no 12-bit-per-channel jpeg136-// - no delayed line count (jpeg) -- IJG doesn't support either135+// - no 12-bit-per-channel JPEG136 // - no 1-bit BMP137 // - GIF always returns *comp=4138 //140-// Basic usage (see HDR discussion below):139+// Basic usage (see HDR discussion below for HDR usage):140 // int x,y,n;141 // unsigned char *data = stbi_load(filename, &x, &y, &n, 0);142 // // ... process data if not NULL ... 143 // // ... x = width, y = height, n = # 8-bit components per pixel ...7 unmodified lines151 // int *comp -- outputs # of image components in image file152 // int req_comp -- if non-zero, # of image components requested in result153 //154 // The return value from an image loader is an 'unsigned char *' which points156-// to the pixel data. The pixel data consists of *y scanlines of *x pixels,155+// to the pixel data, or NULL on an allocation failure or if the image is156+// corrupt or invalid. The pixel data consists of *y scanlines of *x pixels,157 // with each pixel consisting of N interleaved 8-bit components; the first158 // pixel pointed to is top-left-most in the image. There is no padding between159 // image scanlines or between pixels, regardless of format. The number of160 // components N is 'req_comp' if req_comp is non-zero, or *comp otherwise.161 // If req_comp is non-zero, *comp has the number of components that _would_162 // have been output otherwise. E.g. if you set req_comp to 4, you will always163-// get RGBA output, but you can check *comp to easily see if it's opaque.163+// get RGBA output, but you can check *comp to see if it's trivially opaque164+// because e.g. there were only 3 channels in the source image.165 //166 // An output image with N components has the following components interleaved167 // in this order in each pixel:168 //13 unmodified lines182 // Paletted PNG, BMP, GIF, and PIC images are automatically depalettized.183 //184 // ===========================================================================185 //185-// iPhone PNG support:186+// I/O callbacks187 //187-// By default we convert iphone-formatted PNGs back to RGB; nominally they188-// would silently load as BGR, except the existing code should have just189-// failed on such iPhone PNGs. But you can disable this conversion by190-// by calling stbi_convert_iphone_png_to_rgb(0), in which case191-// you will always just get the native iphone "format" through.188+// I/O callbacks allow you to read from arbitrary sources, like packaged189+// files or some other source. Data read from callbacks are processed190+// through a small internal buffer (currently 128 bytes) to try to reduce191+// overhead. 192 //193-// Call stbi_set_unpremultiply_on_load(1) as well to force a divide per194-// pixel to remove any premultiplied alpha *only* if the image file explicitly195-// says there's premultiplied data (currently only happens in iPhone images,196-// and only if iPhone convert-to-rgb processing is on).193+// The three functions you must define are "read" (reads some bytes of data),194+// "skip" (skips some bytes of data), "eof" (reports if the stream is at the end).195 //196 // ===========================================================================197 //198+// SIMD support199+//200+// The JPEG decoder will automatically use SIMD kernels on x86 platforms201+// where supported.202+//203+// (The old do-it-yourself SIMD API is no longer supported in the current204+// code.)205+//206+// The code will automatically detect if the required SIMD instructions are207+// available, and fall back to the generic C version where they're not.208+//209+// The output of the JPEG decoder is slightly different from versions where210+// SIMD support was introduced (that is, for versions before 1.49). The211+// difference is only +-1 in the 8-bit RGB channels, and only on a small212+// fraction of pixels. You can force the pre-1.49 behavior by defining213+// STBI_JPEG_OLD, but this will disable some of the SIMD decoding path214+// and hence cost some performance.215+//216+// If for some reason you do not want to use any of SIMD code, or if217+// you have issues compiling it, you can disable it entirely by218+// defining STBI_NO_SIMD.219+//220+// ===========================================================================221+//222 // HDR image support (disable by defining STBI_NO_HDR)223 //224 // stb_image now supports loading HDR images in general, and currently225 // the Radiance .HDR file format, although the support is provided28 unmodified lines254 // stbi_is_hdr(char *filename);255 //256 // ===========================================================================257 //236-// I/O callbacks258+// iPhone PNG support:259 //238-// I/O callbacks allow you to read from arbitrary sources, like packaged239-// files or some other source. Data read from callbacks are processed240-// through a small internal buffer (currently 128 bytes) to try to reduce241-// overhead. 260+// By default we convert iphone-formatted PNGs back to RGB, even though261+// they are internally encoded differently. You can disable this conversion262+// by by calling stbi_convert_iphone_png_to_rgb(0), in which case263+// you will always just get the native iphone "format" through (which264+// is BGR stored in RGB).265 //243-// The three functions you must define are "read" (reads some bytes of data),244-// "skip" (skips some bytes of data), "eof" (reports if the stream is at the end).266+// Call stbi_set_unpremultiply_on_load(1) as well to force a divide per267+// pixel to remove any premultiplied alpha *only* if the image file explicitly268+// says there's premultiplied data (currently only happens in iPhone images,269+// and only if iPhone convert-to-rgb processing is on).270 //246-// ===========================================================================247-//248-// SIMD support249-//250-// The JPEG decoder will automatically use SIMD kernels on x86 platforms251-// where supported.252-//253-// (The old do-it-yourself SIMD API is no longer supported in the current254-// code.)255-//256-// The code will automatically detect if the required SIMD instructions are257-// available, and fall back to the generic C version where they're not.258-//259-// The supplied kernels are designed to produce results that are bit-identical260-// to the C versions. Nevertheless, if you want to disable this functionality,261-// define STBI_NO_SIMD.271 272 273 #ifndef STBI_NO_STDIO274 #include <stdio.h>125 unmodified lines400 #endif // STBI_INCLUDE_STB_IMAGE_H401 402 #ifdef STB_IMAGE_IMPLEMENTATION403 404+#include <stdarg.h>405+#include <stddef.h> // ptrdiff_t on osx406+#include <stdlib.h>407+#include <string.h>408+409 #ifndef STBI_NO_HDR410 #include <math.h> // ldexp397-#include <string.h> // strcmp, strtok411 #endif412 413 #ifndef STBI_NO_STDIO414 #include <stdio.h>415 #endif403-#include <stdlib.h>404-#include <string.h>416+417 #ifndef STBI_ASSERT418 #include <assert.h>419 #define STBI_ASSERT(x) assert(x)420 #endif409-#include <stdarg.h>410-#include <stddef.h> // ptrdiff_t on osx421 422+423 #ifndef _MSC_VER424 #ifdef __cplusplus425 #define stbi_inline inline426 #else35 unmodified lines462 #else463 #define stbi_lrot(x,y) (((x) << (y)) | ((x) >> (32 - (y))))464 #endif465 466+#if defined(STBI_MALLOC) && defined(STBI_FREE) && defined(STBI_REALLOC)467+// ok468+#elif !defined(STBI_MALLOC) && !defined(STBI_FREE) && !defined(STBI_REALLOC)469+// ok470+#else471+#error "Must define all or none of STBI_MALLOC, STBI_FREE, and STBI_REALLOC."472+#endif473+474+#ifndef STBI_MALLOC475+#define STBI_MALLOC(sz) malloc(sz)476+#define STBI_REALLOC(p,sz) realloc(p,sz)477+#define STBI_FREE(p) free(p)478+#endif479+480 #if !defined(STBI_NO_SIMD) && (defined(__x86_64__) || defined(_M_X64) || defined(__i386) || defined(_M_IX86))481 #define STBI_SSE2482 #include <emmintrin.h>483 173 unmodified lines657 }658 659 static void *stbi__malloc(size_t size)660 {636- return malloc(size);661+ return STBI_MALLOC(size);662 }663 664 // stbi__err - error665 // stbi__errpf - error returning pointer to float11 unmodified lines677 #define stbi__errpuc(x,y) ((unsigned char *) (stbi__err(x,y)?NULL:NULL))678 679 STBIDEF void stbi_image_free(void *retval_from_stbi_load)680 {656- free(retval_from_stbi_load);681+ STBI_FREE(retval_from_stbi_load);682 }683 684 #ifndef STBI_NO_HDR685 static float *stbi__ldr_to_hdr(stbi_uc *data, int x, int y, int comp);326 unmodified lines1012 STBI_ASSERT(req_comp >= 1 && req_comp <= 4);1013 1014 good = (unsigned char *) stbi__malloc(req_comp * x * y);1015 if (good == NULL) {991- free(data);1016+ STBI_FREE(data);1017 return stbi__errpuc("outofmem", "Out of memory");1018 }1019 1020 for (j=0; j < (int) y; ++j) {21 unmodified lines1042 }1043 #undef CASE1044 }1045 1021- free(data);1046+ STBI_FREE(data);1047 return good;1048 }1049 1050 #ifndef STBI_NO_HDR1051 static float *stbi__ldr_to_hdr(stbi_uc *data, int x, int y, int comp)1052 {1053 int i,k,n;1054 float *output = (float *) stbi__malloc(x * y * comp * sizeof(float));1030- if (output == NULL) { free(data); return stbi__errpf("outofmem", "Out of memory"); }1055+ if (output == NULL) { STBI_FREE(data); return stbi__errpf("outofmem", "Out of memory"); }1056 // compute number of non-alpha components1057 if (comp & 1) n = comp; else n = comp-1;1058 for (i=0; i < x*y; ++i) {1059 for (k=0; k < n; ++k) {1060 output[i*comp + k] = (float) (pow(data[i*comp+k]/255.0f, stbi__l2h_gamma) * stbi__l2h_scale);1061 }1062 if (k < comp) output[i*comp + k] = data[i*comp+k]/255.0f;1063 }1039- free(data);1064+ STBI_FREE(data);1065 return output;1066 }1067 1068 #define stbi__float2int(x) ((int) (x))1069 static stbi_uc *stbi__hdr_to_ldr(float *data, int x, int y, int comp)1070 {1071 int i,k,n;1072 stbi_uc *output = (stbi_uc *) stbi__malloc(x * y * comp);1048- if (output == NULL) { free(data); return stbi__errpuc("outofmem", "Out of memory"); }1073+ if (output == NULL) { STBI_FREE(data); return stbi__errpuc("outofmem", "Out of memory"); }1074 // compute number of non-alpha components1075 if (comp & 1) n = comp; else n = comp-1;1076 for (i=0; i < x*y; ++i) {1077 for (k=0; k < n; ++k) {8 unmodified lines1086 if (z > 255) z = 255;1087 output[i*comp + k] = (stbi_uc) stbi__float2int(z);1088 }1089 }1065- free(data);1090+ STBI_FREE(data);1091 return output;1092 }1093 #endif1094 847 unmodified lines1942 z->img_comp[i].h2 = z->img_mcu_y * z->img_comp[i].v * 8;1943 z->img_comp[i].raw_data = stbi__malloc(z->img_comp[i].w2 * z->img_comp[i].h2+15);1944 if (z->img_comp[i].raw_data == NULL) {1945 for(--i; i >= 0; --i) {1921- free(z->img_comp[i].raw_data);1946+ STBI_FREE(z->img_comp[i].raw_data);1947 z->img_comp[i].data = NULL;1948 }1949 return stbi__err("outofmem", "Out of memory");1950 }406 unmodified lines2357 {2358 int i;2359 for (i=0; i < j->s->img_n; ++i) {2360 if (j->img_comp[i].raw_data) {2336- free(j->img_comp[i].raw_data);2361+ STBI_FREE(j->img_comp[i].raw_data);2362 j->img_comp[i].raw_data = NULL;2363 j->img_comp[i].data = NULL;2364 }2365 if (j->img_comp[i].linebuf) {2341- free(j->img_comp[i].linebuf);2366+ STBI_FREE(j->img_comp[i].linebuf);2367 j->img_comp[i].linebuf = NULL;2368 }2369 }2370 }315 unmodified lines2686 cur = (int) (z->zout - z->zout_start);2687 limit = (int) (z->zout_end - z->zout_start);2688 while (cur + n > limit)2689 limit *= 2;2665- q = (char *) realloc(z->zout_start, limit);2690+ q = (char *) STBI_REALLOC(z->zout_start, limit);2691 if (q == NULL) return stbi__err("outofmem", "Out of memory");2692 z->zout_start = q;2693 z->zout = q + cur;2694 z->zout_end = q + limit;206 unmodified lines2901 if (stbi__do_zlib(&a, p, initial_size, 1, 1)) {2902 if (outlen) *outlen = (int) (a.zout - a.zout_start);2903 return a.zout_start;2904 } else {2880- free(a.zout_start);2905+ STBI_FREE(a.zout_start);2906 return NULL;2907 }2908 }2909 12 unmodified lines2922 if (stbi__do_zlib(&a, p, initial_size, 1, parse_header)) {2923 if (outlen) *outlen = (int) (a.zout - a.zout_start);2924 return a.zout_start;2925 } else {2901- free(a.zout_start);2926+ STBI_FREE(a.zout_start);2927 return NULL;2928 }2929 }2930 18 unmodified lines2949 if (stbi__do_zlib(&a, p, 16384, 1, 0)) {2950 if (outlen) *outlen = (int) (a.zout - a.zout_start);2951 return a.zout_start;2952 } else {2928- free(a.zout_start);2953+ STBI_FREE(a.zout_start);2954 return NULL;2955 }2956 }2957 283 unmodified lines3241 y = (a->s->img_y - yorig[p] + yspc[p]-1) / yspc[p];3242 if (x && y) {3243 stbi__uint32 img_len = ((((a->s->img_n * x * depth) + 7) >> 3) + 1) * y;3244 if (!stbi__create_png_image_raw(a, image_data, image_data_len, out_n, x, y, depth, color)) {3220- free(final);3245+ STBI_FREE(final);3246 return 0;3247 }3248 for (j=0; j < y; ++j) {3249 for (i=0; i < x; ++i) {2 unmodified lines3252 memcpy(final + out_y*a->s->img_x*out_n + out_x*out_n,3253 a->out + (j*x+i)*out_n, out_n);3254 }3255 }3231- free(a->out);3256+ STBI_FREE(a->out);3257 image_data += img_len;3258 image_data_len -= img_len;3259 }3260 }55 unmodified lines3316 p[3] = palette[n+3];3317 p += 4;3318 }3319 }3295- free(a->out);3320+ STBI_FREE(a->out);3321 a->out = temp_out;3322 3323 STBI_NOTUSED(len);3324 150 unmodified lines3475 stbi_uc *p;3476 if (idata_limit == 0) idata_limit = c.length > 4096 ? c.length : 4096;3477 while (ioff + c.length > idata_limit)3478 idata_limit *= 2;3454- p = (stbi_uc *) realloc(z->idata, idata_limit); if (p == NULL) return stbi__err("outofmem", "Out of memory");3479+ p = (stbi_uc *) STBI_REALLOC(z->idata, idata_limit); if (p == NULL) return stbi__err("outofmem", "Out of memory");3480 z->idata = p;3481 }3482 if (!stbi__getn(s, z->idata+ioff,c.length)) return stbi__err("outofdata","Corrupt PNG");3483 ioff += c.length;8 unmodified lines3492 // initial guess for decoded data size to avoid unnecessary reallocs3493 raw_len = s->img_x * s->img_y * s->img_n /* pixels */ + s->img_y /* filter mode per row */;3494 z->expanded = (stbi_uc *) stbi_zlib_decode_malloc_guesssize_headerflag((char *) z->idata, ioff, raw_len, (int *) &raw_len, !is_iphone);3495 if (z->expanded == NULL) return 0; // zlib should set error3471- free(z->idata); z->idata = NULL;3496+ STBI_FREE(z->idata); z->idata = NULL;3497 if ((req_comp == s->img_n+1 && req_comp != 3 && !pal_img_n) || has_trans)3498 s->img_out_n = s->img_n+1;3499 else3500 s->img_out_n = s->img_n;9 unmodified lines3510 if (req_comp >= 3) s->img_out_n = req_comp;3511 if (!stbi__expand_png_palette(z, palette, pal_len, s->img_out_n))3512 return 0;3513 }3489- free(z->expanded); z->expanded = NULL;3514+ STBI_FREE(z->expanded); z->expanded = NULL;3515 return 1;3516 }3517 3518 default:33 unmodified lines3552 *x = p->s->img_x;3553 *y = p->s->img_y;3554 if (n) *n = p->s->img_out_n;3555 }3531- free(p->out); p->out = NULL;3532- free(p->expanded); p->expanded = NULL;3533- free(p->idata); p->idata = NULL;3556+ STBI_FREE(p->out); p->out = NULL;3557+ STBI_FREE(p->expanded); p->expanded = NULL;3558+ STBI_FREE(p->idata); p->idata = NULL;3559 3560 return result;3561 }3562 193 unmodified lines3756 out = (stbi_uc *) stbi__malloc(target * s->img_x * s->img_y);3757 if (!out) return stbi__errpuc("outofmem", "Out of memory");3758 if (bpp < 16) {3759 int z=0;3735- if (psize == 0 || psize > 256) { free(out); return stbi__errpuc("invalid", "Corrupt BMP"); }3760+ if (psize == 0 || psize > 256) { STBI_FREE(out); return stbi__errpuc("invalid", "Corrupt BMP"); }3761 for (i=0; i < psize; ++i) {3762 pal[i][2] = stbi__get8(s);3763 pal[i][1] = stbi__get8(s);3764 pal[i][0] = stbi__get8(s);2 unmodified lines3767 }3768 stbi__skip(s, offset - 14 - hsz - psize * (hsz == 12 ? 3 : 4));3769 if (bpp == 4) width = (s->img_x + 1) >> 1;3770 else if (bpp == 8) width = s->img_x;3746- else { free(out); return stbi__errpuc("bad bpp", "Corrupt BMP"); }3771+ else { STBI_FREE(out); return stbi__errpuc("bad bpp", "Corrupt BMP"); }3772 pad = (-width)&3;3773 for (j=0; j < (int) s->img_y; ++j) {3774 for (i=0; i < (int) s->img_x; i += 2) {3775 int v=stbi__get8(s),v2=0;29 unmodified lines3805 if (mb == 0xff && mg == 0xff00 && mr == 0x00ff0000 && ma == 0xff000000)3806 easy = 2;3807 }3808 if (!easy) {3784- if (!mr || !mg || !mb) { free(out); return stbi__errpuc("bad masks", "Corrupt BMP"); }3809+ if (!mr || !mg || !mb) { STBI_FREE(out); return stbi__errpuc("bad masks", "Corrupt BMP"); }3810 // right shift amt to put high bit in position #73811 rshift = stbi__high_bit(mr)-7; rcount = stbi__bitcount(mr);3812 gshift = stbi__high_bit(mg)-7; gcount = stbi__bitcount(mg);3813 bshift = stbi__high_bit(mb)-7; bcount = stbi__bitcount(mb);188 unmodified lines4002 stbi__skip(s, tga_palette_start );4003 // load the palette4004 tga_palette = (unsigned char*)stbi__malloc( tga_palette_len * tga_palette_bits / 8 );4005 if (!tga_palette) {3981- free(tga_data);4006+ STBI_FREE(tga_data);4007 return stbi__errpuc("outofmem", "Out of memory");4008 }4009 if (!stbi__getn(s, tga_palette, tga_palette_len * tga_palette_bits / 8 )) {3985- free(tga_data);3986- free(tga_palette);4010+ STBI_FREE(tga_data);4011+ STBI_FREE(tga_palette);4012 return stbi__errpuc("bad palette", "Corrupt TGA");4013 }4014 }4015 // load the data73 unmodified lines4089 }4090 // clear my palette, if I had one4091 if ( tga_palette != NULL )4092 {4068- free( tga_palette );4093+ STBI_FREE( tga_palette );4094 }4095 }4096 4097 // swap RGB382 unmodified lines4480 result = (stbi_uc *) stbi__malloc(x*y*4);4481 memset(result, 0xff, x*y*4);4482 4483 if (!stbi__pic_load_core(s,x,y,comp, result)) {4459- free(result);4484+ STBI_FREE(result);4485 result=0;4486 }4487 *px = x;4488 *py = y;501 unmodified lines4990 rgbe[3] = (stbi_uc) stbi__get8(s);4991 stbi__hdr_convert(hdr_data, rgbe, req_comp);4992 i = 1;4993 j = 0;4969- free(scanline);4994+ STBI_FREE(scanline);4995 goto main_decode_loop; // yes, this makes no sense4996 }4997 len <<= 8;4998 len |= stbi__get8(s);4974- if (len != width) { free(hdr_data); free(scanline); return stbi__errpf("invalid decoded scanline length", "corrupt HDR"); }4999+ if (len != width) { STBI_FREE(hdr_data); STBI_FREE(scanline); return stbi__errpf("invalid decoded scanline length", "corrupt HDR"); }5000 if (scanline == NULL) scanline = (stbi_uc *) stbi__malloc(width * 4);5001 5002 for (k = 0; k < 4; ++k) {5003 i = 0;14 unmodified lines5018 }5019 for (i=0; i < width; ++i)5020 stbi__hdr_convert(hdr_data+(j*width + i)*req_comp, scanline + i*4, req_comp);5021 }4997- free(scanline);5022+ STBI_FREE(scanline);5023 }5024 5025 return hdr_data;5026 }334 unmodified lines