Skip to content
Myr

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

1 file+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 risk456Before including,6 Before #including,78 #define STB_IMAGE_WRITE_IMPLEMENTATION8 #define STB_IMAGE_WRITE_IMPLEMENTATION910in the file that you want to have the implementation.10 in the file that you want to have the implementation.1112Will probably not work correctly with strict-aliasing optimizations.1213 Will probably not work correctly with strict-aliasing optimizations.1415ABOUT:1617 This header file is a library for writing images to C stdio. It could be
37 unmodified lines
5556 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.5960CREDITS:6162 PNG/BMP/TGA63 Sean Barrett64 HDR65 Baldur Karlsson66 TGA monochrome:67 Jean-Sebastien Guay68 Bugfixes:69 Chribba@github70*/7172#ifndef INCLUDE_STB_IMAGE_WRITE_H73#define INCLUDE_STB_IMAGE_WRITE_H
1 unmodified line
75#ifdef __cplusplus76extern "C" {77#endif7868extern int stbi_write_png(char const *filename, int w, int h, int comp, const void *data, int stride_in_bytes);69extern int stbi_write_bmp(char const *filename, int w, int h, int comp, const void *data);70extern int stbi_write_tga(char const *filename, int w, int h, int comp, const void *data);71extern int stbi_write_hdr(char const *filename, int w, int h, int comp, const void *data);79extern int stbi_write_png(char const *filename, int w, int h, int comp, const void *data, int stride_in_bytes);80extern int stbi_write_bmp(char const *filename, int w, int h, int comp, const void *data);81extern int stbi_write_tga(char const *filename, int w, int h, int comp, const void *data);82extern int stbi_write_hdr(char const *filename, int w, int h, int comp, const float *data);8384#ifdef __cplusplus85}86#endif
6 unmodified lines
93#include <stdlib.h>94#include <stdio.h>95#include <string.h>96#include <assert.h>97#include <math.h>9899typedef unsigned int stbiw_uint32;100typedef int stb_image_write_test[sizeof(stbiw_uint32)==4 ? 1 : -1];101
102 unmodified lines
204}205206// *************************************************************************************************207// Radiance RGBE HDR writer196// originally by Baldur Karlsson208// by Baldur Karlsson209#define stbiw__max(a, b) ((a) > (b) ? (a) : (b))210211void stbiw__linear_to_rgbe(unsigned char *rgbe, float *linear)212{
2 unmodified lines
215216 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;220221 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 lines
227228void 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}235236void 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}243230void stbiw__write_hdr_scanline(FILE *f, int width, int comp, unsigned char *scratch, float *scanline)244void 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 lines
337 }338 }339}340327int stbi_write_hdr(char const *filename, int x, int y, int comp, const void *data)341int 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}359360/////////////////////////////////////////////////////////361// PNG362363// 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 lines
681#endif // STB_IMAGE_WRITE_IMPLEMENTATION682683/* Revision history684685 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.h
8 unmodified lines