Add .hdr file writing support
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by baldurk · committed by baldurk · parent 2572f31 · Browse files · 1 file·+160 added−1 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+160 added−1 removed
- other:+160 added−1 removed
Provenance
- Commit
fb8eabd- Tree
6903a37- Author
baldurk <baldurk@baldurk.org>- Committer
baldurk <baldurk@baldurk.org>- Parent
2572f31
1 file changed+160 added−1 removed
modified:stb_image_write.h+160 added-1 removed
22 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 61 #ifndef INCLUDE_STB_IMAGE_WRITE_H62 #define INCLUDE_STB_IMAGE_WRITE_H4 unmodified lines67 68 extern int stbi_write_png(char const *filename, int w, int h, int comp, const void *data, int stride_in_bytes);69 extern int stbi_write_bmp(char const *filename, int w, int h, int comp, const void *data);70 extern int stbi_write_tga(char const *filename, int w, int h, int comp, const void *data);71+extern int stbi_write_hdr(char const *filename, int w, int h, int comp, const void *data);72 73 #ifdef __cplusplus74 }75 #endif114 unmodified lines190 return outfile(filename, -1,-1, x, y, comp, (void *) data, has_alpha, 0,191 "111 221 2222 11", 0,0,format, 0,0,0, 0,0,x,y, (colorbytes+has_alpha)*8, has_alpha*8);192 }193 194+// *************************************************************************************************195+// Radiance RGBE HDR writer196+// originally by Baldur Karlsson197+#define stbiw__max(a, b) ((a) > (b) ? (a) : (b))198+199+void stbiw__linear_to_rgbe(unsigned char *rgbe, float *linear)200+{201+ int exponent;202+ float maxcomp = stbiw__max(linear[0], stbiw__max(linear[1], linear[2]));203+204+ if (maxcomp < 1e-32) {205+ rgbe[0] = rgbe[1] = rgbe[2] = rgbe[3] = 0;206+ } else {207+ maxcomp = (float) frexp(maxcomp, &exponent) * 256.0f/maxcomp;208+209+ rgbe[0] = (unsigned char)(linear[0] * maxcomp);210+ rgbe[1] = (unsigned char)(linear[1] * maxcomp);211+ rgbe[2] = (unsigned char)(linear[2] * maxcomp);212+ rgbe[3] = (unsigned char)(exponent + 128);213+ }214+}215+216+void stbiw__write_rle_data(FILE *f, int length, unsigned char databyte)217+{218+ unsigned char lengthbyte = 0x80 | (unsigned char)(length & 0x7f);219+ fwrite(&lengthbyte, 1, 1, f);220+ fwrite(&databyte, 1, 1, f);221+}222+223+void stbiw__write_nonrle_data(FILE *f, int length, unsigned char *data)224+{225+ unsigned char lengthbyte = (unsigned char )(length & 0xff);226+ fwrite(&lengthbyte, 1, 1, f);227+ fwrite(data, length, 1, f);228+}229+230+void stbiw__write_hdr_scanline(FILE *f, int width, int comp, unsigned char *scratch, float *scanline)231+{232+ unsigned char scanlineheader[4] = { 2, 2, 0, 0 };233+ unsigned char rgbe[4];234+ float linear[3];235+ int x;236+237+ scanlineheader[2] = (width&0xff00)>>8;238+ scanlineheader[3] = (width&0x00ff);239+240+ /* skip RLE for images too small or large */241+ if (width < 8 || width >= 32768) {242+ for (x=0; x < width; x++) {243+ switch (comp) {244+ case 4: /* fallthrough */245+ case 3: linear[2] = scanline[x*comp + 2];246+ linear[1] = scanline[x*comp + 1];247+ linear[0] = scanline[x*comp + 0];248+ break;249+ case 2: /* fallthrough */250+ case 1: linear[0] = linear[1] = linear[2] = scanline[x*comp + 0];251+ break;252+ }253+ stbiw__linear_to_rgbe(rgbe, linear);254+ fwrite(rgbe, 4, 1, f);255+ }256+ } else {257+ /* encode into scratch buffer */258+ for (x=0; x < width; x++) {259+ switch(comp) {260+ case 4: /* fallthrough */261+ case 3: linear[2] = scanline[x*comp + 2];262+ linear[1] = scanline[x*comp + 1];263+ linear[0] = scanline[x*comp + 0];264+ break;265+ case 2: /* fallthrough */266+ case 1: linear[0] = linear[1] = linear[2] = scanline[x*comp + 0];267+ break;268+ }269+ stbiw__linear_to_rgbe(rgbe, linear);270+ scratch[x + width*0] = rgbe[0];271+ scratch[x + width*1] = rgbe[1];272+ scratch[x + width*2] = rgbe[2];273+ scratch[x + width*3] = rgbe[3];274+ }275+276+ fwrite(scanlineheader, 4, 1, f);277+278+ /* RLE each component separately */279+ for (x=0; x < 4; x++) {280+ unsigned char *comp = &scratch[width*x];281+ int runstart = 0, head = 0, rlerun = 0;282+283+ while (head < width) {284+ head++;285+286+ if (head - runstart == 127 && rlerun == 1) {287+ // max length RLE run288+ stbiw__write_rle_data(f, head - runstart, comp[runstart]);289+ rlerun = 0;290+ runstart = head;291+ } else if (head - runstart == 128 && rlerun == 0) {292+ // max length non-RLE run293+ stbiw__write_nonrle_data(f, head - runstart, comp+runstart);294+ rlerun = 0;295+ runstart = head;296+ } else if (comp[head] != comp[head-1] && rlerun == 1) {297+ // end of RLE run298+ stbiw__write_rle_data(f, head - runstart, comp[runstart]);299+ rlerun = 0;300+ runstart = head;301+ } else {302+ // continue accumulating RLE run303+ if (rlerun == 1) continue;304+305+ // see if we can start an RLE run, at least 3 bytes same306+ if (rlerun == 0 && head - runstart >= 2307+ && comp[head] == comp[head-1]308+ && comp[head] == comp[head-2]) {309+ // found a run. Flush non-run (if there is anything) and then start an RLE run310+ if (head - runstart > 2) {311+ stbiw__write_nonrle_data(f, head-2 - runstart, comp+runstart);312+ }313+314+ rlerun = 1;315+ runstart = head-2;316+ }317+ }318+ }319+320+ // flush remaining sequence (if any)321+ if (rlerun == 1) stbiw__write_rle_data(f, head - runstart, comp[runstart]);322+ else if (head - runstart > 0) stbiw__write_nonrle_data(f, head - runstart, comp+runstart);323+ }324+ }325+}326+327+int stbi_write_hdr(char const *filename, int x, int y, int comp, const void *data)328+{329+ int i;330+ FILE *f;331+ unsigned char *scratch;332+ if (y <= 0 || x <= 0) return 0;333+ f = fopen(filename, "wb");334+ if (f) {335+ float *scanline = (float *)data;336+ /* Each component is stored separately. Allocate scratch space for full output scanline. */337+ scratch = (unsigned char *) malloc(x*4); if (!scanline) { fclose(f); return 0; }338+ fprintf(f, "#?RADIANCE\n# Written by stb_image_write.h\nFORMAT=32-bit_rle_rgbe\n");339+ fprintf(f, "EXPOSURE= 1.0000000000000\n\n-Y %d +X %d\n", y, x);340+ for(i=0; i < y; i++) stbiw__write_hdr_scanline(f, x, comp, scratch, scanline + comp*i*x);341+ free(scratch);342+ fclose(f);343+ }344+ return f != NULL;345+}346+347 // stretchy buffer; stbiw__sbpush() == vector<>::push_back() -- stbiw__sbcount() == vector<>::size()348 #define stbiw__sbraw(a) ((int *) (a) - 2)349 #define stbiw__sbm(a) stbiw__sbraw(a)[0]350 #define stbiw__sbn(a) stbiw__sbraw(a)[1]330 unmodified lines