Merge branch 'test'
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 f392ec7, edd5e67 · Browse files · 1 file·+187 added−9 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+187 added−9 removed
- other:+187 added−9 removed
Provenance
- Commit
d91cbdf- Tree
0b6b4c3- Author
Sean Barrett <sean2@nothings.org>- Committer
Sean Barrett <sean2@nothings.org>- Parent 1
f392ec7- Parent 2
edd5e67
1 file changed+187 added−9 removed
modified:stb_image_write.h+187 added-9 removed
1-/* stb_image_write - v0.95 - public domain - http://nothings.org/stb/stb_image_write.h1+/* stb_image_write - v0.96 - 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 6-Before including,6+ Before #including,7 8- #define STB_IMAGE_WRITE_IMPLEMENTATION8+ #define STB_IMAGE_WRITE_IMPLEMENTATION9 10-in the file that you want to have the implementation.10+ in the file that you want to have the implementation.11 12-Will probably not work correctly with strict-aliasing optimizations.12 13+ Will probably not work correctly with strict-aliasing optimizations.14 15 ABOUT:16 17 This header file is a library for writing images to C stdio. It could be5 unmodified lines23 or run-time performance.24 25 USAGE:26 27- There are three functions, one for each image file format:27+ There are four functions, one for each image file format:28 29 int stbi_write_png(char const *filename, int w, int h, int comp, const void *data, int stride_in_bytes);30 int stbi_write_bmp(char const *filename, int w, int h, int comp, const void *data);31 int stbi_write_tga(char const *filename, int w, int h, int comp, const void *data);32+ int stbi_write_hdr(char const *filename, int w, int h, int comp, const void *data);33 34 Each function returns 0 on failure and non-0 on success.35 36 The functions create an image file defined by the parameters. The image14 unmodified lines51 by supplying the stride between the beginning of adjacent rows. The other52 formats do not. (Thus you cannot write a native-format BMP through the BMP53 writer, both because it is in BGR order and because it may have padding54 at the end of the line.)55+56+ HDR expects linear float data. Since the format is always 32-bit rgb(e)57+ data, alpha (if provided) is discarded, and for monochrome data it is58+ replicated across all three channels.59+60+CREDITS:61+62+ PNG/BMP/TGA63+ Sean Barrett64+ HDR65+ Baldur Karlsson66+ TGA monochrome:67+ Jean-Sebastien Guay68+ Bugfixes:69+ Chribba@github70 */71 72 #ifndef INCLUDE_STB_IMAGE_WRITE_H73 #define INCLUDE_STB_IMAGE_WRITE_H1 unmodified line75 #ifdef __cplusplus76 extern "C" {77 #endif78 63-extern int stbi_write_png(char const *filename, int w, int h, int comp, const void *data, int stride_in_bytes);64-extern int stbi_write_bmp(char const *filename, int w, int h, int comp, const void *data);65-extern int stbi_write_tga(char const *filename, int w, int h, int comp, const void *data);79+extern int stbi_write_png(char const *filename, int w, int h, int comp, const void *data, int stride_in_bytes);80+extern int stbi_write_bmp(char const *filename, int w, int h, int comp, const void *data);81+extern int stbi_write_tga(char const *filename, int w, int h, int comp, const void *data);82+extern int stbi_write_hdr(char const *filename, int w, int h, int comp, const float *data);83 84 #ifdef __cplusplus85 }86 #endif6 unmodified lines93 #include <stdlib.h>94 #include <stdio.h>95 #include <string.h>96 #include <assert.h>97+#include <math.h>98 99 typedef unsigned int stbiw_uint32;100 typedef int stb_image_write_test[sizeof(stbiw_uint32)==4 ? 1 : -1];101 104 unmodified lines206 return outfile(filename, -1,-1, x, y, comp, 0, (void *) data, has_alpha, 0,207 "111 221 2222 11", 0,0,format, 0,0,0, 0,0,x,y, (colorbytes+has_alpha)*8, has_alpha*8);208 }209 210+// *************************************************************************************************211+// Radiance RGBE HDR writer212+// by Baldur Karlsson213+#define stbiw__max(a, b) ((a) > (b) ? (a) : (b))214+215+void stbiw__linear_to_rgbe(unsigned char *rgbe, float *linear)216+{217+ int exponent;218+ float maxcomp = stbiw__max(linear[0], stbiw__max(linear[1], linear[2]));219+220+ if (maxcomp < 1e-32) {221+ rgbe[0] = rgbe[1] = rgbe[2] = rgbe[3] = 0;222+ } else {223+ float normalize = (float) frexp(maxcomp, &exponent) * 256.0f/maxcomp;224+225+ rgbe[0] = (unsigned char)(linear[0] * maxcomp);226+ rgbe[1] = (unsigned char)(linear[1] * maxcomp);227+ rgbe[2] = (unsigned char)(linear[2] * maxcomp);228+ rgbe[3] = (unsigned char)(exponent + 128);229+ }230+}231+232+void stbiw__write_rle_data(FILE *f, int length, unsigned char databyte)233+{234+ unsigned char lengthbyte = 0x80 | (unsigned char)(length & 0x7f);235+ assert(length <= 0x7f);236+ fwrite(&lengthbyte, 1, 1, f);237+ fwrite(&databyte, 1, 1, f);238+}239+240+void stbiw__write_nonrle_data(FILE *f, int length, unsigned char *data)241+{242+ unsigned char lengthbyte = (unsigned char )(length & 0xff);243+ assert(length <= 0x7f);244+ fwrite(&lengthbyte, 1, 1, f);245+ fwrite(data, length, 1, f);246+}247+248+void stbiw__write_hdr_scanline(FILE *f, int width, int comp, unsigned char *scratch, const float *scanline)249+{250+ unsigned char scanlineheader[4] = { 2, 2, 0, 0 };251+ unsigned char rgbe[4];252+ float linear[3];253+ int x;254+255+ scanlineheader[2] = (width&0xff00)>>8;256+ scanlineheader[3] = (width&0x00ff);257+258+ /* skip RLE for images too small or large */259+ if (width < 8 || width >= 32768) {260+ for (x=0; x < width; x++) {261+ switch (comp) {262+ case 4: /* fallthrough */263+ case 3: linear[2] = scanline[x*comp + 2];264+ linear[1] = scanline[x*comp + 1];265+ linear[0] = scanline[x*comp + 0];266+ break;267+ case 2: /* fallthrough */268+ case 1: linear[0] = linear[1] = linear[2] = scanline[x*comp + 0];269+ break;270+ }271+ stbiw__linear_to_rgbe(rgbe, linear);272+ fwrite(rgbe, 4, 1, f);273+ }274+ } else {275+ /* encode into scratch buffer */276+ for (x=0; x < width; x++) {277+ switch(comp) {278+ case 4: /* fallthrough */279+ case 3: linear[2] = scanline[x*comp + 2];280+ linear[1] = scanline[x*comp + 1];281+ linear[0] = scanline[x*comp + 0];282+ break;283+ case 2: /* fallthrough */284+ case 1: linear[0] = linear[1] = linear[2] = scanline[x*comp + 0];285+ break;286+ }287+ stbiw__linear_to_rgbe(rgbe, linear);288+ scratch[x + width*0] = rgbe[0];289+ scratch[x + width*1] = rgbe[1];290+ scratch[x + width*2] = rgbe[2];291+ scratch[x + width*3] = rgbe[3];292+ }293+294+ fwrite(scanlineheader, 4, 1, f);295+296+ /* RLE each component separately */297+ for (x=0; x < 4; x++) {298+ unsigned char *comp = &scratch[width*x];299+ int runstart = 0, head = 0, rlerun = 0;300+301+ while (head < width) {302+ head++;303+304+ if (head - runstart == 127 && rlerun == 1) {305+ // max length RLE run306+ stbiw__write_rle_data(f, head - runstart, comp[runstart]);307+ rlerun = 0;308+ runstart = head;309+ } else if (head - runstart == 128 && rlerun == 0) {310+ // max length non-RLE run311+ stbiw__write_nonrle_data(f, head - runstart, comp+runstart);312+ rlerun = 0;313+ runstart = head;314+ } else if (comp[head] != comp[head-1] && rlerun == 1) {315+ // end of RLE run316+ stbiw__write_rle_data(f, head - runstart, comp[runstart]);317+ rlerun = 0;318+ runstart = head;319+ } else {320+ // continue accumulating RLE run321+ if (rlerun == 1) continue;322+323+ // see if we can start an RLE run, at least 3 bytes same324+ if (rlerun == 0 && head - runstart >= 2325+ && comp[head] == comp[head-1]326+ && comp[head] == comp[head-2]) {327+ // found a run. Flush non-run (if there is anything) and then start an RLE run328+ if (head - runstart > 2) {329+ stbiw__write_nonrle_data(f, head-2 - runstart, comp+runstart);330+ }331+332+ rlerun = 1;333+ runstart = head-2;334+ }335+ }336+ }337+338+ // flush remaining sequence (if any)339+ if (rlerun == 1) stbiw__write_rle_data(f, head - runstart, comp[runstart]);340+ else if (head - runstart > 0) stbiw__write_nonrle_data(f, head - runstart, comp+runstart);341+ }342+ }343+}344+345+int stbi_write_hdr(char const *filename, int x, int y, int comp, const float *data)346+{347+ int i;348+ FILE *f;349+ if (y <= 0 || x <= 0 || data == NULL) return 0;350+ f = fopen(filename, "wb");351+ if (f) {352+ /* Each component is stored separately. Allocate scratch space for full output scanline. */353+ unsigned char *scratch = (unsigned char *) malloc(x*4);354+ fprintf(f, "#?RADIANCE\n# Written by stb_image_write.h\nFORMAT=32-bit_rle_rgbe\n" );355+ fprintf(f, "EXPOSURE= 1.0000000000000\n\n-Y %d +X %d\n" , y, x);356+ for(i=0; i < y; i++)357+ stbiw__write_hdr_scanline(f, x, comp, scratch, data + comp*i*x);358+ free(scratch);359+ fclose(f);360+ }361+ return f != NULL;362+}363+364+/////////////////////////////////////////////////////////365+// PNG366+367 // stretchy buffer; stbiw__sbpush() == vector<>::push_back() -- stbiw__sbcount() == vector<>::size()368 #define stbiw__sbraw(a) ((int *) (a) - 2)369 #define stbiw__sbm(a) stbiw__sbraw(a)[0]370 #define stbiw__sbn(a) stbiw__sbraw(a)[1]314 unmodified lines685 #endif // STB_IMAGE_WRITE_IMPLEMENTATION686 687 /* Revision history688 689+ 0.96 (2015-01-17)690+ add HDR output691+ fix monochrome BMP692 0.95 (2014-08-17)693 add monochrome TGA output694 0.94 (2014-05-31)695 rename private functions to avoid conflicts with stb_image.h8 unmodified lines