Merge branch 'work2'
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Sean Barrett · committed by Sean Barrett · parents 8a0a882, 00b1797 · Browse files · 1 file·+66 added−34 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+66 added−34 removed
- other:+66 added−34 removed
Provenance
- Commit
6dc6304- Tree
679349d- Author
Sean Barrett <sean2@nothings.org>- Committer
Sean Barrett <sean2@nothings.org>- Parent 1
8a0a882- Parent 2
00b1797
1 file changed+66 added−34 removed
modified:stb_image_write.h+66 added-34 removed
1-/* stb_image_write - v0.97 - public domain - http://nothings.org/stb/stb_image_write.h1+/* stb_image_write - v0.98 - public domain - http://nothings.org/stb/stb_image_write.h2 writes out PNG/BMP/TGA images to C stdio - Sean Barrett 20103 no warranty implied; use at your own risk4 5 2 unmodified lines8 #define STB_IMAGE_WRITE_IMPLEMENTATION9 10 in the file that you want to have the implementation.11 12-12 Will probably not work correctly with strict-aliasing optimizations.13 14 ABOUT:15 4 unmodified lines20 written by a decent optimizing implementation. This library is designed21 for source code compactness and simplicitly, not optimal image file size22 or run-time performance.23 24+BUILDING:25+26+ You can #define STBIW_ASSERT(x) before the #include to avoid using assert.h.27+ You can #define STBIW_MALLOC(), STBIW_REALLOC(), and STBIW_FREE() to replace28+ malloc,realloc,free.29+ You can define STBIW_MEMMOVE() to replace memmove()30+31 USAGE:32 33 There are four functions, one for each image file format:34 2 unmodified lines37 int stbi_write_tga(char const *filename, int w, int h, int comp, const void *data);38 int stbi_write_hdr(char const *filename, int w, int h, int comp, const void *data);39 40 Each function returns 0 on failure and non-0 on success.35- 41+42 The functions create an image file defined by the parameters. The image43 is a rectangle of pixels stored from left-to-right, top-to-bottom.44 Each pixel contains 'comp' channels of data stored interleaved with 8-bits45 per channel, in the following order: 1=Y, 2=YA, 3=RGB, 4=RGBA. (Y is4 unmodified lines50 51 PNG creates output files with the same number of components as the input.52 The BMP format expands Y to RGB in the file format and does not53 output alpha.48- 54+55 PNG supports writing rectangles of data even when the bytes storing rows of56 data are not consecutive in memory (e.g. sub-rectangles of a larger image),57 by supplying the stride between the beginning of adjacent rows. The other58 formats do not. (Thus you cannot write a native-format BMP through the BMP11 unmodified lines70 HDR71 Baldur Karlsson72 TGA monochrome:73 Jean-Sebastien Guay68- Bugfixes:69- Chribba@github74+ misc enhancements:75+ Tim Kelsey76+ bugfixes:77+ github:Chribba78 */79 80 #ifndef INCLUDE_STB_IMAGE_WRITE_H81 #define INCLUDE_STB_IMAGE_WRITE_H18 unmodified lines100 #include <stdarg.h>101 #include <stdlib.h>102 #include <stdio.h>103 #include <string.h>96-#include <assert.h>104 #include <math.h>105 106+#if defined(STBIW_MALLOC) && defined(STBIW_FREE) && defined(STBIW_REALLOC)107+// ok108+#elif !defined(STBIW_MALLOC) && !defined(STBIW_FREE) && !defined(STBIW_REALLOC)109+// ok110+#else111+#error "Must define all or none of STBIW_MALLOC, STBIW_FREE, and STBIW_REALLOC."112+#endif113+114+#ifndef STBIW_MALLOC115+#define STBIW_MALLOC(sz) malloc(sz)116+#define STBIW_REALLOC(p,sz) realloc(p,sz)117+#define STBIW_FREE(p) free(p)118+#endif119+#ifndef STBIW_MEMMOVE120+#define STBIW_MEMMOVE(a,b,sz) memmove(a,b,sz)121+#endif122+123+124+#ifndef STBIW_ASSERT125+#include <assert.h>126+#define STBIW_ASSERT(x) assert(x)127+#endif128+129 typedef unsigned int stbiw_uint32;130 typedef int stb_image_write_test[sizeof(stbiw_uint32)==4 ? 1 : -1];131 132 static void writefv(FILE *f, const char *fmt, va_list v)9 unmodified lines142 b[0]=(unsigned char)x; b[1]=(unsigned char)(x>>8);143 b[2]=(unsigned char)(x>>16); b[3]=(unsigned char)(x>>24);144 fwrite(b,4,1,f); break; }145 default:116- assert(0);146+ STBIW_ASSERT(0);147 return;148 }149 }150 }13 unmodified lines164 165 if (y <= 0)166 return;167 138- if (vdir < 0) 168+ if (vdir < 0)169 j_end = -1, j = y-1;170 else171 j_end = y, j = 0;172 88 unmodified lines261 262 void stbiw__write_run_data(FILE *f, int length, unsigned char databyte)263 {264 unsigned char lengthbyte = (unsigned char) (length+128);235- assert(length+128 <= 255);265+ STBIW_ASSERT(length+128 <= 255);266 fwrite(&lengthbyte, 1, 1, f);267 fwrite(&databyte, 1, 1, f);268 }269 270 void stbiw__write_dump_data(FILE *f, int length, unsigned char *data)271 {272 unsigned char lengthbyte = (unsigned char )(length & 0xff);243- assert(length <= 128); // inconsistent with spec but consistent with official code273+ STBIW_ASSERT(length <= 128); // inconsistent with spec but consistent with official code274 fwrite(&lengthbyte, 1, 1, f);275 fwrite(data, length, 1, f);276 }277 48 unmodified lines326 327 /* RLE each component separately */328 for (c=0; c < 4; c++) {329 unsigned char *comp = &scratch[width*c];300- int runstart = 0, head = 0, rlerun = 0;330 331 x = 0;332 while (x < width) {333 // find first run15 unmodified lines349 // if there's a run, output it350 if (r+2 < width) { // same test as what we break out of in search loop, so only true if we break'd351 // find next byte after run352 while (r < width && comp[r] == comp[x])324- ++r; 353+ ++r;354 // output run up to r355 while (x < r) {356 int len = r-x;357 if (len > 127) len = 127;13 unmodified lines371 if (y <= 0 || x <= 0 || data == NULL) return 0;372 f = fopen(filename, "wb");373 if (f) {374 /* Each component is stored separately. Allocate scratch space for full output scanline. */346- unsigned char *scratch = (unsigned char *) malloc(x*4);375+ unsigned char *scratch = (unsigned char *) STBIW_MALLOC(x*4);376 fprintf(f, "#?RADIANCE\n# Written by stb_image_write.h\nFORMAT=32-bit_rle_rgbe\n" );377 fprintf(f, "EXPOSURE= 1.0000000000000\n\n-Y %d +X %d\n" , y, x);378 for(i=0; i < y; i++)379 stbiw__write_hdr_scanline(f, x, comp, scratch, data + comp*i*x);351- free(scratch);380+ STBIW_FREE(scratch);381 fclose(f);382 }383 return f != NULL;384 }11 unmodified lines396 #define stbiw__sbgrow(a,n) stbiw__sbgrowf((void **) &(a), (n), sizeof(*(a)))397 398 #define stbiw__sbpush(a, v) (stbiw__sbmaybegrow(a,1), (a)[stbiw__sbn(a)++] = (v))399 #define stbiw__sbcount(a) ((a) ? stbiw__sbn(a) : 0)371-#define stbiw__sbfree(a) ((a) ? free(stbiw__sbraw(a)),0 : 0)400+#define stbiw__sbfree(a) ((a) ? STBIW_FREE(stbiw__sbraw(a)),0 : 0)401 402 static void *stbiw__sbgrowf(void **arr, int increment, int itemsize)403 {404 int m = *arr ? 2*stbiw__sbm(*arr)+increment : increment+1;376- void *p = realloc(*arr ? stbiw__sbraw(*arr) : 0, itemsize * m + sizeof(int)*2);377- assert(p);405+ void *p = STBIW_REALLOC(*arr ? stbiw__sbraw(*arr) : 0, itemsize * m + sizeof(int)*2);406+ STBIW_ASSERT(p);407 if (p) {408 if (!*arr) ((int *) p)[1] = 0;409 *arr = (void *) ((int *) p + 2);410 stbiw__sbm(*arr) = m;76 unmodified lines487 hash_table[i] = NULL;488 489 i=0;490 while (i < data_len-3) {462- // hash next 3 bytes of data to be compressed 491+ // hash next 3 bytes of data to be compressed492 int h = stbiw__zhash(data+i)&(stbiw__ZHASH-1), best=3;493 unsigned char *bestloc = 0;494 unsigned char **hlist = hash_table[h];495 int n = stbiw__sbcount(hlist);4 unmodified lines500 }501 }502 // when hash table entry is too long, delete half the entries503 if (hash_table[h] && stbiw__sbn(hash_table[h]) == 2*quality) {475- memcpy(hash_table[h], hash_table[h]+quality, sizeof(hash_table[h][0])*quality);504+ STBIW_MEMMOVE(hash_table[h], hash_table[h]+quality, sizeof(hash_table[h][0])*quality);505 stbiw__sbn(hash_table[h]) = quality;506 }507 stbiw__sbpush(hash_table[h],data+i);508 14 unmodified lines523 }524 525 if (bestloc) {526 int d = (int) (data+i - bestloc); // distance back498- assert(d <= 32767 && best <= 258);527+ STBIW_ASSERT(d <= 32767 && best <= 258);528 for (j=0; best > lengthc[j+1]-1; ++j);529 stbiw__zlib_huff(j+257);530 if (lengtheb[j]) stbiw__zlib_add(best - lengthc[j], lengtheb[j]);531 for (j=0; d > distc[j+1]-1; ++j);32 unmodified lines564 stbiw__sbpush(out, (unsigned char) s1);565 }566 *out_len = stbiw__sbn(out);567 // make returned pointer freeable539- memmove(stbiw__sbraw(out), out, *out_len);568+ STBIW_MEMMOVE(stbiw__sbraw(out), out, *out_len);569 return (unsigned char *) stbiw__sbraw(out);570 }571 572 unsigned int stbiw__crc32(unsigned char *buffer, int len)38 unmodified lines611 612 if (stride_bytes == 0)613 stride_bytes = x * n;614 586- filt = (unsigned char *) malloc((x*n+1) * y); if (!filt) return 0;587- line_buffer = (signed char *) malloc(x * n); if (!line_buffer) { free(filt); return 0; }615+ filt = (unsigned char *) STBIW_MALLOC((x*n+1) * y); if (!filt) return 0;616+ line_buffer = (signed char *) STBIW_MALLOC(x * n); if (!line_buffer) { STBIW_FREE(filt); return 0; }617 for (j=0; j < y; ++j) {618 static int mapping[] = { 0,1,2,3,4 };619 static int firstmap[] = { 0,1,0,5,6 };620 int *mymap = j ? mapping : firstmap;30 unmodified lines651 }652 }653 // when we get here, best contains the filter type, and line_buffer contains the data654 filt[j*(x*n+1)] = (unsigned char) best;626- memcpy(filt+j*(x*n+1)+1, line_buffer, x*n);655+ STBIW_MEMMOVE(filt+j*(x*n+1)+1, line_buffer, x*n);656 }628- free(line_buffer);657+ STBIW_FREE(line_buffer);658 zlib = stbi_zlib_compress(filt, y*( x*n+1), &zlen, 8); // increase 8 to get smaller but use more memory630- free(filt);659+ STBIW_FREE(filt);660 if (!zlib) return 0;661 662 // each tag requires 12 bytes of overhead634- out = (unsigned char *) malloc(8 + 12+13 + 12+zlen + 12); 663+ out = (unsigned char *) STBIW_MALLOC(8 + 12+13 + 12+zlen + 12);664 if (!out) return 0;665 *out_len = 8 + 12+13 + 12+zlen + 12;666 667 o=out;639- memcpy(o,sig,8); o+= 8;668+ STBIW_MEMMOVE(o,sig,8); o+= 8;669 stbiw__wp32(o, 13); // header length670 stbiw__wptag(o, "IHDR");671 stbiw__wp32(o, x);672 stbiw__wp32(o, y);5 unmodified lines678 stbiw__wpcrc(&o,13);679 680 stbiw__wp32(o, zlen);681 stbiw__wptag(o, "IDAT");653- memcpy(o, zlib, zlen); o += zlen; free(zlib);682+ STBIW_MEMMOVE(o, zlib, zlen);683+ o += zlen;684+ STBIW_FREE(zlib);685 stbiw__wpcrc(&o, zlen);686 687 stbiw__wp32(o,0);688 stbiw__wptag(o, "IEND");689 stbiw__wpcrc(&o,0);690 660- assert(o == out + *out_len);691+ STBIW_ASSERT(o == out + *out_len);692 693 return out;694 }695 3 unmodified lines699 int len;700 unsigned char *png = stbi_write_png_to_mem((unsigned char *) data, stride_bytes, x, y, comp, &len);701 if (!png) return 0;702 f = fopen(filename, "wb");672- if (!f) { free(png); return 0; }703+ if (!f) { STBIW_FREE(png); return 0; }704 fwrite(png, 1, len, f);705 fclose(f);675- free(png);706+ STBIW_FREE(png);707 return 1;708 }709 #endif // STB_IMAGE_WRITE_IMPLEMENTATION710 711 /* Revision history681-712+ 0.98 (2015-04-08)713+ added STBIW_MALLOC, STBIW_ASSERT etc714 0.97 (2015-01-18)715 fixed HDR asserts, rewrote HDR rle logic716 0.96 (2015-01-17)717 add HDR output13 unmodified lines