switch memcpy to memmove for simplicity;
Before adoption: this commit came in with the history the project adopted, so no landing records it.
rename all STBI_ config macros to STBIW_ to avoid conflict with stb_image.h
authored by Sean Barrett · committed by Sean Barrett · parent 019236a · Browse files · 1 file·+49 added−43 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+49 added−43 removed
- other:+49 added−43 removed
Provenance
- Commit
00b1797- Tree
6390932- Author
Sean Barrett <sean2@nothings.org>- Committer
Sean Barrett <sean2@nothings.org>- Parent
019236a
1 file changed+49 added−43 removed
modified:stb_image_write.h+49 added-43 removed
7 unmodified lines8 #define STB_IMAGE_WRITE_IMPLEMENTATION9 10 in the file that you want to have the implementation.11 12- You can #define STBI_ASSERT(x) before the #include to avoid using assert.h.13- And #define STBI_MALLOC, STBI_REALLOC, and STBI_FREE to avoid using malloc,realloc,free14- You can optionally define STBI_MEMMOVE and STBI_MEMCPY if the default semantics of15- memmove and memcpy would be incompatible with your choice of malloc et al.16-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 35 unmodified lines70 HDR71 Baldur Karlsson72 TGA monochrome:73 Jean-Sebastien Guay72- Bugfixes:73- Chribba@github74- STBI_MALLOC etc:75- callidus@github74+ misc enhancements:75+ Tim Kelsey76+ bugfixes:77+ github:Chribba78 */79 80 #ifndef INCLUDE_STB_IMAGE_WRITE_H81 #define INCLUDE_STB_IMAGE_WRITE_H20 unmodified lines102 #include <stdio.h>103 #include <string.h>104 #include <math.h>105 104-#if defined(STBI_MALLOC) && defined(STBI_FREE) && defined(STBI_REALLOC)106+#if defined(STBIW_MALLOC) && defined(STBIW_FREE) && defined(STBIW_REALLOC)107 // ok106-#elif !defined(STBI_MALLOC) && !defined(STBI_FREE) && !defined(STBI_REALLOC)108+#elif !defined(STBIW_MALLOC) && !defined(STBIW_FREE) && !defined(STBIW_REALLOC)109 // ok110 #else109-#error "Must define all or none of STBI_MALLOC, STBI_FREE, and STBI_REALLOC."111+#error "Must define all or none of STBIW_MALLOC, STBIW_FREE, and STBIW_REALLOC."112 #endif113 112-#ifndef STBI_MALLOC113-#define STBI_MALLOC(sz) malloc(sz)114-#define STBI_REALLOC(p,sz) realloc(p,sz)115-#define STBI_FREE(p) free(p)116-#define STBI_MEMMOVE(a,b,sz) memmove(a,b,sz)117-#define STBI_MEMCPY(a,b,sz) memcpy(a,b,sz)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+120-#ifndef STBI_ASSERT124+#ifndef STBIW_ASSERT125 #include <assert.h>122-#define STBI_ASSERT(x) assert(x)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];11 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:142- STBI_ASSERT(0);146+ STBIW_ASSERT(0);147 return;148 }149 }150 }110 unmodified lines261 262 void stbiw__write_run_data(FILE *f, int length, unsigned char databyte)263 {264 unsigned char lengthbyte = (unsigned char) (length+128);261- STBI_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);269- STBI_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 93 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. */371- unsigned char *scratch = (unsigned char *) STBI_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);376- STBI_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)396-#define stbiw__sbfree(a) ((a) ? STBI_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;401- void *p = STBI_REALLOC(*arr ? stbiw__sbraw(*arr) : 0, itemsize * m + sizeof(int)*2);402- STBI_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;89 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) {500- STBI_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 back523- STBI_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 freeable564- STBI_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 611- filt = (unsigned char *) STBI_MALLOC((x*n+1) * y); if (!filt) return 0;612- line_buffer = (signed char *) STBI_MALLOC(x * n); if (!line_buffer) { STBI_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;651- STBI_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 }653- STBI_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 memory655- STBI_FREE(filt);659+ STBIW_FREE(filt);660 if (!zlib) return 0;661 662 // each tag requires 12 bytes of overhead659- out = (unsigned char *) STBI_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;664- STBI_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");678- STBI_MEMCPY(o, zlib, zlen); o += zlen; STBI_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 685- STBI_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");697- if (!f) { STBI_FREE(png); return 0; }703+ if (!f) { STBIW_FREE(png); return 0; }704 fwrite(png, 1, len, f);705 fclose(f);700- STBI_FREE(png);706+ STBIW_FREE(png);707 return 1;708 }709 #endif // STB_IMAGE_WRITE_IMPLEMENTATION710 711 /* Revision history712 0.98 (2015-04-08)707- added STBI_MALLOC, STBI_ASSERT etc713+ 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