tweak baldurk's hdr implementation
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 · parent 258fb15 · Browse files · 1 file·+39 added−20 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+39 added−20 removed
- other:+39 added−20 removed
Provenance
- Commit
edd5e67- Tree
51be6e5- Author
Sean Barrett <sean2@nothings.org>- Committer
Sean Barrett <sean2@nothings.org>- Parent
258fb15
1 file changed+39 added−20 removed
modified:stb_image_write.h+39 added-20 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 be37 unmodified lines55 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 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);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 102 unmodified lines204 }205 206 // *************************************************************************************************207 // Radiance RGBE HDR writer196-// originally by Baldur Karlsson208+// by Baldur Karlsson209 #define stbiw__max(a, b) ((a) > (b) ? (a) : (b))210 211 void stbiw__linear_to_rgbe(unsigned char *rgbe, float *linear)212 {2 unmodified lines215 216 if (maxcomp < 1e-32) {217 rgbe[0] = rgbe[1] = rgbe[2] = rgbe[3] = 0;218 } else {207- maxcomp = (float) frexp(maxcomp, &exponent) * 256.0f/maxcomp;219+ float normalize = (float) frexp(maxcomp, &exponent) * 256.0f/maxcomp;220 221 rgbe[0] = (unsigned char)(linear[0] * maxcomp);222 rgbe[1] = (unsigned char)(linear[1] * maxcomp);223 rgbe[2] = (unsigned char)(linear[2] * maxcomp);3 unmodified lines227 228 void stbiw__write_rle_data(FILE *f, int length, unsigned char databyte)229 {230 unsigned char lengthbyte = 0x80 | (unsigned char)(length & 0x7f);231+ assert(length <= 0x7f);232 fwrite(&lengthbyte, 1, 1, f);233 fwrite(&databyte, 1, 1, f);234 }235 236 void stbiw__write_nonrle_data(FILE *f, int length, unsigned char *data)237 {238 unsigned char lengthbyte = (unsigned char )(length & 0xff);239+ assert(length <= 0x7f);240 fwrite(&lengthbyte, 1, 1, f);241 fwrite(data, length, 1, f);242 }243 230-void stbiw__write_hdr_scanline(FILE *f, int width, int comp, unsigned char *scratch, float *scanline)244+void stbiw__write_hdr_scanline(FILE *f, int width, int comp, unsigned char *scratch, const float *scanline)245 {246 unsigned char scanlineheader[4] = { 2, 2, 0, 0 };247 unsigned char rgbe[4];248 float linear[3];88 unmodified lines337 }338 }339 }340 327-int stbi_write_hdr(char const *filename, int x, int y, int comp, const void *data)341+int stbi_write_hdr(char const *filename, int x, int y, int comp, const float *data)342 {343 int i;344 FILE *f;331- unsigned char *scratch;332- if (y <= 0 || x <= 0) return 0;345+ if (y <= 0 || x <= 0 || data == NULL) return 0;346 f = fopen(filename, "wb");347 if (f) {335- float *scanline = (float *)data;348 /* 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);349+ unsigned char *scratch = (unsigned char *) malloc(x*4);350+ fprintf(f, "#?RADIANCE\n# Written by stb_image_write.h\nFORMAT=32-bit_rle_rgbe\n" );351+ fprintf(f, "EXPOSURE= 1.0000000000000\n\n-Y %d +X %d\n" , y, x);352+ for(i=0; i < y; i++)353+ stbiw__write_hdr_scanline(f, x, comp, scratch, data + comp*i*x);354 free(scratch);355 fclose(f);356 }357 return f != NULL;358 }359 360+/////////////////////////////////////////////////////////361+// PNG362+363 // stretchy buffer; stbiw__sbpush() == vector<>::push_back() -- stbiw__sbcount() == vector<>::size()364 #define stbiw__sbraw(a) ((int *) (a) - 2)365 #define stbiw__sbm(a) stbiw__sbraw(a)[0]366 #define stbiw__sbn(a) stbiw__sbraw(a)[1]314 unmodified lines681 #endif // STB_IMAGE_WRITE_IMPLEMENTATION682 683 /* Revision history684 685+ 0.96 (2015-01-17)686+ add HDR output687+ fix monochrome BMP688 0.95 (2014-08-17)689 add monochrome TGA output690 0.94 (2014-05-31)691 rename private functions to avoid conflicts with stb_image.h8 unmodified lines