tweak baldurk's hdr implementation

pull/67/merge
Sean Barrett 2015-01-17 07:46:22 -08:00
parent 258fb15340
commit edd5e6700d
1 changed files with 39 additions and 20 deletions

View File

@ -1,16 +1,16 @@
/* stb_image_write - v0.95 - public domain - http://nothings.org/stb/stb_image_write.h /* stb_image_write - v0.96 - public domain - http://nothings.org/stb/stb_image_write.h
writes out PNG/BMP/TGA images to C stdio - Sean Barrett 2010 writes out PNG/BMP/TGA images to C stdio - Sean Barrett 2010
no warranty implied; use at your own risk no warranty implied; use at your own risk
Before including, Before #including,
#define STB_IMAGE_WRITE_IMPLEMENTATION #define STB_IMAGE_WRITE_IMPLEMENTATION
in the file that you want to have the implementation. in the file that you want to have the implementation.
Will probably not work correctly with strict-aliasing optimizations.
Will probably not work correctly with strict-aliasing optimizations.
ABOUT: ABOUT:
@ -56,6 +56,17 @@ USAGE:
HDR expects linear float data. Since the format is always 32-bit rgb(e) HDR expects linear float data. Since the format is always 32-bit rgb(e)
data, alpha (if provided) is discarded, and for monochrome data it is data, alpha (if provided) is discarded, and for monochrome data it is
replicated across all three channels. replicated across all three channels.
CREDITS:
PNG/BMP/TGA
Sean Barrett
HDR
Baldur Karlsson
TGA monochrome:
Jean-Sebastien Guay
Bugfixes:
Chribba@github
*/ */
#ifndef INCLUDE_STB_IMAGE_WRITE_H #ifndef INCLUDE_STB_IMAGE_WRITE_H
@ -65,10 +76,10 @@ USAGE:
extern "C" { extern "C" {
#endif #endif
extern int stbi_write_png(char const *filename, int w, int h, int comp, const void *data, int stride_in_bytes); extern int stbi_write_png(char const *filename, int w, int h, int comp, const void *data, int stride_in_bytes);
extern int stbi_write_bmp(char const *filename, int w, int h, int comp, const void *data); extern int stbi_write_bmp(char const *filename, int w, int h, int comp, const void *data);
extern int stbi_write_tga(char const *filename, int w, int h, int comp, const void *data); extern int stbi_write_tga(char const *filename, int w, int h, int comp, const void *data);
extern int stbi_write_hdr(char const *filename, int w, int h, int comp, const void *data); extern int stbi_write_hdr(char const *filename, int w, int h, int comp, const float *data);
#ifdef __cplusplus #ifdef __cplusplus
} }
@ -83,6 +94,7 @@ extern int stbi_write_hdr(char const *filename, int w, int h, int comp, const vo
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <assert.h> #include <assert.h>
#include <math.h>
typedef unsigned int stbiw_uint32; typedef unsigned int stbiw_uint32;
typedef int stb_image_write_test[sizeof(stbiw_uint32)==4 ? 1 : -1]; typedef int stb_image_write_test[sizeof(stbiw_uint32)==4 ? 1 : -1];
@ -193,7 +205,7 @@ int stbi_write_tga(char const *filename, int x, int y, int comp, const void *dat
// ************************************************************************************************* // *************************************************************************************************
// Radiance RGBE HDR writer // Radiance RGBE HDR writer
// originally by Baldur Karlsson // by Baldur Karlsson
#define stbiw__max(a, b) ((a) > (b) ? (a) : (b)) #define stbiw__max(a, b) ((a) > (b) ? (a) : (b))
void stbiw__linear_to_rgbe(unsigned char *rgbe, float *linear) void stbiw__linear_to_rgbe(unsigned char *rgbe, float *linear)
@ -204,7 +216,7 @@ void stbiw__linear_to_rgbe(unsigned char *rgbe, float *linear)
if (maxcomp < 1e-32) { if (maxcomp < 1e-32) {
rgbe[0] = rgbe[1] = rgbe[2] = rgbe[3] = 0; rgbe[0] = rgbe[1] = rgbe[2] = rgbe[3] = 0;
} else { } else {
maxcomp = (float) frexp(maxcomp, &exponent) * 256.0f/maxcomp; float normalize = (float) frexp(maxcomp, &exponent) * 256.0f/maxcomp;
rgbe[0] = (unsigned char)(linear[0] * maxcomp); rgbe[0] = (unsigned char)(linear[0] * maxcomp);
rgbe[1] = (unsigned char)(linear[1] * maxcomp); rgbe[1] = (unsigned char)(linear[1] * maxcomp);
@ -216,6 +228,7 @@ void stbiw__linear_to_rgbe(unsigned char *rgbe, float *linear)
void stbiw__write_rle_data(FILE *f, int length, unsigned char databyte) void stbiw__write_rle_data(FILE *f, int length, unsigned char databyte)
{ {
unsigned char lengthbyte = 0x80 | (unsigned char)(length & 0x7f); unsigned char lengthbyte = 0x80 | (unsigned char)(length & 0x7f);
assert(length <= 0x7f);
fwrite(&lengthbyte, 1, 1, f); fwrite(&lengthbyte, 1, 1, f);
fwrite(&databyte, 1, 1, f); fwrite(&databyte, 1, 1, f);
} }
@ -223,11 +236,12 @@ void stbiw__write_rle_data(FILE *f, int length, unsigned char databyte)
void stbiw__write_nonrle_data(FILE *f, int length, unsigned char *data) void stbiw__write_nonrle_data(FILE *f, int length, unsigned char *data)
{ {
unsigned char lengthbyte = (unsigned char )(length & 0xff); unsigned char lengthbyte = (unsigned char )(length & 0xff);
assert(length <= 0x7f);
fwrite(&lengthbyte, 1, 1, f); fwrite(&lengthbyte, 1, 1, f);
fwrite(data, length, 1, f); fwrite(data, length, 1, f);
} }
void stbiw__write_hdr_scanline(FILE *f, int width, int comp, unsigned char *scratch, float *scanline) void stbiw__write_hdr_scanline(FILE *f, int width, int comp, unsigned char *scratch, const float *scanline)
{ {
unsigned char scanlineheader[4] = { 2, 2, 0, 0 }; unsigned char scanlineheader[4] = { 2, 2, 0, 0 };
unsigned char rgbe[4]; unsigned char rgbe[4];
@ -324,26 +338,28 @@ void stbiw__write_hdr_scanline(FILE *f, int width, int comp, unsigned char *scra
} }
} }
int stbi_write_hdr(char const *filename, int x, int y, int comp, const void *data) int stbi_write_hdr(char const *filename, int x, int y, int comp, const float *data)
{ {
int i; int i;
FILE *f; FILE *f;
unsigned char *scratch; if (y <= 0 || x <= 0 || data == NULL) return 0;
if (y <= 0 || x <= 0) return 0;
f = fopen(filename, "wb"); f = fopen(filename, "wb");
if (f) { if (f) {
float *scanline = (float *)data;
/* Each component is stored separately. Allocate scratch space for full output scanline. */ /* Each component is stored separately. Allocate scratch space for full output scanline. */
scratch = (unsigned char *) malloc(x*4); if (!scanline) { fclose(f); return 0; } unsigned char *scratch = (unsigned char *) malloc(x*4);
fprintf(f, "#?RADIANCE\n# Written by stb_image_write.h\nFORMAT=32-bit_rle_rgbe\n"); fprintf(f, "#?RADIANCE\n# Written by stb_image_write.h\nFORMAT=32-bit_rle_rgbe\n" );
fprintf(f, "EXPOSURE= 1.0000000000000\n\n-Y %d +X %d\n", y, x); fprintf(f, "EXPOSURE= 1.0000000000000\n\n-Y %d +X %d\n" , y, x);
for(i=0; i < y; i++) stbiw__write_hdr_scanline(f, x, comp, scratch, scanline + comp*i*x); for(i=0; i < y; i++)
stbiw__write_hdr_scanline(f, x, comp, scratch, data + comp*i*x);
free(scratch); free(scratch);
fclose(f); fclose(f);
} }
return f != NULL; return f != NULL;
} }
/////////////////////////////////////////////////////////
// PNG
// stretchy buffer; stbiw__sbpush() == vector<>::push_back() -- stbiw__sbcount() == vector<>::size() // stretchy buffer; stbiw__sbpush() == vector<>::push_back() -- stbiw__sbcount() == vector<>::size()
#define stbiw__sbraw(a) ((int *) (a) - 2) #define stbiw__sbraw(a) ((int *) (a) - 2)
#define stbiw__sbm(a) stbiw__sbraw(a)[0] #define stbiw__sbm(a) stbiw__sbraw(a)[0]
@ -666,6 +682,9 @@ int stbi_write_png(char const *filename, int x, int y, int comp, const void *dat
/* Revision history /* Revision history
0.96 (2015-01-17)
add HDR output
fix monochrome BMP
0.95 (2014-08-17) 0.95 (2014-08-17)
add monochrome TGA output add monochrome TGA output
0.94 (2014-05-31) 0.94 (2014-05-31)