Skip to content
Myr

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

1 file+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 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
5 unmodified lines
23 or run-time performance.2425USAGE:2627 There are three functions, one for each image file format:27 There are four functions, one for each image file format:2829 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);3334 Each function returns 0 on failure and non-0 on success.35 36 The functions create an image file defined by the parameters. The image
14 unmodified lines
51 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.)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#endif7863extern int stbi_write_png(char const *filename, int w, int h, int comp, const void *data, int stride_in_bytes);64extern int stbi_write_bmp(char const *filename, int w, int h, int comp, const void *data);65extern int stbi_write_tga(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
104 unmodified lines
206 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}209210// *************************************************************************************************211// Radiance RGBE HDR writer212// by Baldur Karlsson213#define stbiw__max(a, b) ((a) > (b) ? (a) : (b))214215void 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]));219220 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;224225 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}231232void 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}239240void 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}247248void 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;254255 scanlineheader[2] = (width&0xff00)>>8;256 scanlineheader[3] = (width&0x00ff);257258 /* 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 }293294 fwrite(scanlineheader, 4, 1, f);295296 /* 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;300301 while (head < width) {302 head++;303304 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;322323 // 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 }331332 rlerun = 1;333 runstart = head-2;334 }335 }336 }337338 // 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}344345int 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}363364/////////////////////////////////////////////////////////365// PNG366367// 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 lines
685#endif // STB_IMAGE_WRITE_IMPLEMENTATION686687/* Revision history688689 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.h
8 unmodified lines