|
|
@ -1,7 +1,6 @@ |
|
|
|
/* stb_image_write - v0.98 - public domain - http://nothings.org/stb/stb_image_write.h |
|
|
|
writes out PNG/BMP/TGA images to C stdio - Sean Barrett 2010 |
|
|
|
no warranty implied; use at your own risk |
|
|
|
|
|
|
|
/* stb_image_write - v1.01 - public domain - http://nothings.org/stb/stb_image_write.h |
|
|
|
writes out PNG/BMP/TGA images to C stdio - Sean Barrett 2010-2015 |
|
|
|
no warranty implied; use at your own risk |
|
|
|
|
|
|
|
Before #including, |
|
|
|
|
|
|
@ -18,7 +17,7 @@ ABOUT: |
|
|
|
|
|
|
|
The PNG output is not optimal; it is 20-50% larger than the file |
|
|
|
written by a decent optimizing implementation. This library is designed |
|
|
|
for source code compactness and simplicitly, not optimal image file size |
|
|
|
for source code compactness and simplicity, not optimal image file size |
|
|
|
or run-time performance. |
|
|
|
|
|
|
|
BUILDING: |
|
|
@ -35,7 +34,22 @@ USAGE: |
|
|
|
int stbi_write_png(char const *filename, int w, int h, int comp, const void *data, int stride_in_bytes); |
|
|
|
int stbi_write_bmp(char const *filename, int w, int h, int comp, const void *data); |
|
|
|
int stbi_write_tga(char const *filename, int w, int h, int comp, const void *data); |
|
|
|
int stbi_write_hdr(char const *filename, int w, int h, int comp, const void *data); |
|
|
|
int stbi_write_hdr(char const *filename, int w, int h, int comp, const float *data); |
|
|
|
|
|
|
|
There are also four equivalent functions that use an arbitrary write function. You are |
|
|
|
expected to open/close your file-equivalent before and after calling these: |
|
|
|
|
|
|
|
int stbi_write_png_to_func(stbi_write_func *func, void *context, int w, int h, int comp, const void *data, int stride_in_bytes); |
|
|
|
int stbi_write_bmp_to_func(stbi_write_func *func, void *context, int w, int h, int comp, const void *data); |
|
|
|
int stbi_write_tga_to_func(stbi_write_func *func, void *context, int w, int h, int comp, const void *data); |
|
|
|
int stbi_write_hdr_to_func(stbi_write_func *func, void *context, int w, int h, int comp, const float *data); |
|
|
|
|
|
|
|
where the callback is: |
|
|
|
void stbi_write_func(void *context, void *data, int size); |
|
|
|
|
|
|
|
You can define STBI_WRITE_NO_STDIO to disable the file variant of these |
|
|
|
functions, so the library will not use stdio.h at all. However, this will |
|
|
|
also disable HDR writing, because it requires stdio for formatted output. |
|
|
|
|
|
|
|
Each function returns 0 on failure and non-0 on success. |
|
|
|
|
|
|
@ -63,6 +77,9 @@ USAGE: |
|
|
|
data, alpha (if provided) is discarded, and for monochrome data it is |
|
|
|
replicated across all three channels. |
|
|
|
|
|
|
|
TGA supports RLE or non-RLE compressed data. To use non-RLE-compressed |
|
|
|
data, set the global variable 'stbi_write_tga_with_rle' to 0. |
|
|
|
|
|
|
|
CREDITS: |
|
|
|
|
|
|
|
PNG/BMP/TGA |
|
|
@ -73,8 +90,25 @@ CREDITS: |
|
|
|
Jean-Sebastien Guay |
|
|
|
misc enhancements: |
|
|
|
Tim Kelsey |
|
|
|
TGA RLE |
|
|
|
Alan Hickman |
|
|
|
initial file IO callback implementation |
|
|
|
Emmanuel Julien |
|
|
|
bugfixes: |
|
|
|
github:Chribba |
|
|
|
Guillaume Chereau |
|
|
|
github:jry2 |
|
|
|
github:romigrou |
|
|
|
Sergio Gonzalez |
|
|
|
Jonas Karlsson |
|
|
|
Filip Wasil |
|
|
|
|
|
|
|
LICENSE |
|
|
|
|
|
|
|
This software is in the public domain. Where that dedication is not |
|
|
|
recognized, you are granted a perpetual, irrevocable license to copy, |
|
|
|
distribute, and modify this file as you see fit. |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
#ifndef INCLUDE_STB_IMAGE_WRITE_H |
|
|
@ -84,10 +118,26 @@ CREDITS: |
|
|
|
extern "C" { |
|
|
|
#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_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_hdr(char const *filename, int w, int h, int comp, const float *data); |
|
|
|
#ifdef STB_IMAGE_WRITE_STATIC |
|
|
|
#define STBIWDEF static |
|
|
|
#else |
|
|
|
#define STBIWDEF extern |
|
|
|
extern int stbi_write_tga_with_rle; |
|
|
|
#endif |
|
|
|
|
|
|
|
#ifndef STBI_WRITE_NO_STDIO |
|
|
|
STBIWDEF int stbi_write_png(char const *filename, int w, int h, int comp, const void *data, int stride_in_bytes); |
|
|
|
STBIWDEF int stbi_write_bmp(char const *filename, int w, int h, int comp, const void *data); |
|
|
|
STBIWDEF int stbi_write_tga(char const *filename, int w, int h, int comp, const void *data); |
|
|
|
STBIWDEF int stbi_write_hdr(char const *filename, int w, int h, int comp, const float *data); |
|
|
|
#endif |
|
|
|
|
|
|
|
typedef void stbi_write_func(void *context, void *data, int size); |
|
|
|
|
|
|
|
STBIWDEF int stbi_write_png_to_func(stbi_write_func *func, void *context, int w, int h, int comp, const void *data, int stride_in_bytes); |
|
|
|
STBIWDEF int stbi_write_bmp_to_func(stbi_write_func *func, void *context, int w, int h, int comp, const void *data); |
|
|
|
STBIWDEF int stbi_write_tga_to_func(stbi_write_func *func, void *context, int w, int h, int comp, const void *data); |
|
|
|
STBIWDEF int stbi_write_hdr_to_func(stbi_write_func *func, void *context, int w, int h, int comp, const float *data); |
|
|
|
|
|
|
|
#ifdef __cplusplus |
|
|
|
} |
|
|
@ -97,25 +147,43 @@ extern int stbi_write_hdr(char const *filename, int w, int h, int comp, const fl |
|
|
|
|
|
|
|
#ifdef STB_IMAGE_WRITE_IMPLEMENTATION |
|
|
|
|
|
|
|
#ifdef _WIN32 |
|
|
|
#ifndef _CRT_SECURE_NO_WARNINGS |
|
|
|
#define _CRT_SECURE_NO_WARNINGS |
|
|
|
#endif |
|
|
|
#ifndef _CRT_NONSTDC_NO_DEPRECATE |
|
|
|
#define _CRT_NONSTDC_NO_DEPRECATE |
|
|
|
#endif |
|
|
|
#endif |
|
|
|
|
|
|
|
#ifndef STBI_WRITE_NO_STDIO |
|
|
|
#include <stdio.h> |
|
|
|
#endif // STBI_WRITE_NO_STDIO |
|
|
|
|
|
|
|
#include <stdarg.h> |
|
|
|
#include <stdlib.h> |
|
|
|
#include <stdio.h> |
|
|
|
#include <string.h> |
|
|
|
#include <math.h> |
|
|
|
|
|
|
|
#if defined(STBIW_MALLOC) && defined(STBIW_FREE) && defined(STBIW_REALLOC) |
|
|
|
#if defined(STBIW_MALLOC) && defined(STBIW_FREE) && (defined(STBIW_REALLOC) || defined(STBIW_REALLOC_SIZED)) |
|
|
|
// ok |
|
|
|
#elif !defined(STBIW_MALLOC) && !defined(STBIW_FREE) && !defined(STBIW_REALLOC) |
|
|
|
#elif !defined(STBIW_MALLOC) && !defined(STBIW_FREE) && !defined(STBIW_REALLOC) && !defined(STBIW_REALLOC_SIZED) |
|
|
|
// ok |
|
|
|
#else |
|
|
|
#error "Must define all or none of STBIW_MALLOC, STBIW_FREE, and STBIW_REALLOC." |
|
|
|
#error "Must define all or none of STBIW_MALLOC, STBIW_FREE, and STBIW_REALLOC (or STBIW_REALLOC_SIZED)." |
|
|
|
#endif |
|
|
|
|
|
|
|
#ifndef STBIW_MALLOC |
|
|
|
#define STBIW_MALLOC(sz) malloc(sz) |
|
|
|
#define STBIW_REALLOC(p,sz) realloc(p,sz) |
|
|
|
#define STBIW_FREE(p) free(p) |
|
|
|
#define STBIW_MALLOC(sz) malloc(sz) |
|
|
|
#define STBIW_REALLOC(p,newsz) realloc(p,newsz) |
|
|
|
#define STBIW_FREE(p) free(p) |
|
|
|
#endif |
|
|
|
|
|
|
|
#ifndef STBIW_REALLOC_SIZED |
|
|
|
#define STBIW_REALLOC_SIZED(p,oldsz,newsz) STBIW_REALLOC(p,newsz) |
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
#ifndef STBIW_MEMMOVE |
|
|
|
#define STBIW_MEMMOVE(a,b,sz) memmove(a,b,sz) |
|
|
|
#endif |
|
|
@ -126,22 +194,73 @@ extern int stbi_write_hdr(char const *filename, int w, int h, int comp, const fl |
|
|
|
#define STBIW_ASSERT(x) assert(x) |
|
|
|
#endif |
|
|
|
|
|
|
|
#define STBIW_UCHAR(x) (unsigned char) ((x) & 0xff) |
|
|
|
|
|
|
|
typedef struct |
|
|
|
{ |
|
|
|
stbi_write_func *func; |
|
|
|
void *context; |
|
|
|
} stbi__write_context; |
|
|
|
|
|
|
|
// initialize a callback-based context |
|
|
|
static void stbi__start_write_callbacks(stbi__write_context *s, stbi_write_func *c, void *context) |
|
|
|
{ |
|
|
|
s->func = c; |
|
|
|
s->context = context; |
|
|
|
} |
|
|
|
|
|
|
|
#ifndef STBI_WRITE_NO_STDIO |
|
|
|
|
|
|
|
static void stbi__stdio_write(void *context, void *data, int size) |
|
|
|
{ |
|
|
|
fwrite(data,1,size,(FILE*) context); |
|
|
|
} |
|
|
|
|
|
|
|
static int stbi__start_write_file(stbi__write_context *s, const char *filename) |
|
|
|
{ |
|
|
|
FILE *f = fopen(filename, "wb"); |
|
|
|
stbi__start_write_callbacks(s, stbi__stdio_write, (void *) f); |
|
|
|
return f != NULL; |
|
|
|
} |
|
|
|
|
|
|
|
static void stbi__end_write_file(stbi__write_context *s) |
|
|
|
{ |
|
|
|
fclose((FILE *)s->context); |
|
|
|
} |
|
|
|
|
|
|
|
#endif // !STBI_WRITE_NO_STDIO |
|
|
|
|
|
|
|
typedef unsigned int stbiw_uint32; |
|
|
|
typedef int stb_image_write_test[sizeof(stbiw_uint32)==4 ? 1 : -1]; |
|
|
|
|
|
|
|
static void writefv(FILE *f, const char *fmt, va_list v) |
|
|
|
#ifdef STB_IMAGE_WRITE_STATIC |
|
|
|
static int stbi_write_tga_with_rle = 1; |
|
|
|
#else |
|
|
|
int stbi_write_tga_with_rle = 1; |
|
|
|
#endif |
|
|
|
|
|
|
|
static void stbiw__writefv(stbi__write_context *s, const char *fmt, va_list v) |
|
|
|
{ |
|
|
|
while (*fmt) { |
|
|
|
switch (*fmt++) { |
|
|
|
case ' ': break; |
|
|
|
case '1': { unsigned char x = (unsigned char) va_arg(v, int); fputc(x,f); break; } |
|
|
|
case '2': { int x = va_arg(v,int); unsigned char b[2]; |
|
|
|
b[0] = (unsigned char) x; b[1] = (unsigned char) (x>>8); |
|
|
|
fwrite(b,2,1,f); break; } |
|
|
|
case '4': { stbiw_uint32 x = va_arg(v,int); unsigned char b[4]; |
|
|
|
b[0]=(unsigned char)x; b[1]=(unsigned char)(x>>8); |
|
|
|
b[2]=(unsigned char)(x>>16); b[3]=(unsigned char)(x>>24); |
|
|
|
fwrite(b,4,1,f); break; } |
|
|
|
case '1': { unsigned char x = STBIW_UCHAR(va_arg(v, int)); |
|
|
|
s->func(s->context,&x,1); |
|
|
|
break; } |
|
|
|
case '2': { int x = va_arg(v,int); |
|
|
|
unsigned char b[2]; |
|
|
|
b[0] = STBIW_UCHAR(x); |
|
|
|
b[1] = STBIW_UCHAR(x>>8); |
|
|
|
s->func(s->context,b,2); |
|
|
|
break; } |
|
|
|
case '4': { stbiw_uint32 x = va_arg(v,int); |
|
|
|
unsigned char b[4]; |
|
|
|
b[0]=STBIW_UCHAR(x); |
|
|
|
b[1]=STBIW_UCHAR(x>>8); |
|
|
|
b[2]=STBIW_UCHAR(x>>16); |
|
|
|
b[3]=STBIW_UCHAR(x>>24); |
|
|
|
s->func(s->context,b,4); |
|
|
|
break; } |
|
|
|
default: |
|
|
|
STBIW_ASSERT(0); |
|
|
|
return; |
|
|
@ -149,18 +268,60 @@ static void writefv(FILE *f, const char *fmt, va_list v) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
static void write3(FILE *f, unsigned char a, unsigned char b, unsigned char c) |
|
|
|
static void stbiw__writef(stbi__write_context *s, const char *fmt, ...) |
|
|
|
{ |
|
|
|
va_list v; |
|
|
|
va_start(v, fmt); |
|
|
|
stbiw__writefv(s, fmt, v); |
|
|
|
va_end(v); |
|
|
|
} |
|
|
|
|
|
|
|
static void stbiw__write3(stbi__write_context *s, unsigned char a, unsigned char b, unsigned char c) |
|
|
|
{ |
|
|
|
unsigned char arr[3]; |
|
|
|
arr[0] = a, arr[1] = b, arr[2] = c; |
|
|
|
fwrite(arr, 3, 1, f); |
|
|
|
s->func(s->context, arr, 3); |
|
|
|
} |
|
|
|
|
|
|
|
static void write_pixels(FILE *f, int rgb_dir, int vdir, int x, int y, int comp, void *data, int write_alpha, int scanline_pad, int expand_mono) |
|
|
|
static void stbiw__write_pixel(stbi__write_context *s, int rgb_dir, int comp, int write_alpha, int expand_mono, unsigned char *d) |
|
|
|
{ |
|
|
|
unsigned char bg[3] = { 255, 0, 255}, px[3]; |
|
|
|
int k; |
|
|
|
|
|
|
|
if (write_alpha < 0) |
|
|
|
s->func(s->context, &d[comp - 1], 1); |
|
|
|
|
|
|
|
switch (comp) { |
|
|
|
case 1: |
|
|
|
s->func(s->context,d,1); |
|
|
|
break; |
|
|
|
case 2: |
|
|
|
if (expand_mono) |
|
|
|
stbiw__write3(s, d[0], d[0], d[0]); // monochrome bmp |
|
|
|
else |
|
|
|
s->func(s->context, d, 1); // monochrome TGA |
|
|
|
break; |
|
|
|
case 4: |
|
|
|
if (!write_alpha) { |
|
|
|
// composite against pink background |
|
|
|
for (k = 0; k < 3; ++k) |
|
|
|
px[k] = bg[k] + ((d[k] - bg[k]) * d[3]) / 255; |
|
|
|
stbiw__write3(s, px[1 - rgb_dir], px[1], px[1 + rgb_dir]); |
|
|
|
break; |
|
|
|
} |
|
|
|
/* FALLTHROUGH */ |
|
|
|
case 3: |
|
|
|
stbiw__write3(s, d[1 - rgb_dir], d[1], d[1 + rgb_dir]); |
|
|
|
break; |
|
|
|
} |
|
|
|
if (write_alpha > 0) |
|
|
|
s->func(s->context, &d[comp - 1], 1); |
|
|
|
} |
|
|
|
|
|
|
|
static void stbiw__write_pixels(stbi__write_context *s, int rgb_dir, int vdir, int x, int y, int comp, void *data, int write_alpha, int scanline_pad, int expand_mono) |
|
|
|
{ |
|
|
|
stbiw_uint32 zero = 0; |
|
|
|
int i,j,k, j_end; |
|
|
|
int i,j, j_end; |
|
|
|
|
|
|
|
if (y <= 0) |
|
|
|
return; |
|
|
@ -173,73 +334,148 @@ static void write_pixels(FILE *f, int rgb_dir, int vdir, int x, int y, int comp, |
|
|
|
for (; j != j_end; j += vdir) { |
|
|
|
for (i=0; i < x; ++i) { |
|
|
|
unsigned char *d = (unsigned char *) data + (j*x+i)*comp; |
|
|
|
if (write_alpha < 0) |
|
|
|
fwrite(&d[comp-1], 1, 1, f); |
|
|
|
switch (comp) { |
|
|
|
case 1: fwrite(d, 1, 1, f); |
|
|
|
break; |
|
|
|
case 2: if (expand_mono) |
|
|
|
write3(f, d[0],d[0],d[0]); // monochrome bmp |
|
|
|
else |
|
|
|
fwrite(d, 1, 1, f); // monochrome TGA |
|
|
|
break; |
|
|
|
case 4: |
|
|
|
if (!write_alpha) { |
|
|
|
// composite against pink background |
|
|
|
for (k=0; k < 3; ++k) |
|
|
|
px[k] = bg[k] + ((d[k] - bg[k]) * d[3])/255; |
|
|
|
write3(f, px[1-rgb_dir],px[1],px[1+rgb_dir]); |
|
|
|
break; |
|
|
|
} |
|
|
|
/* FALLTHROUGH */ |
|
|
|
case 3: |
|
|
|
write3(f, d[1-rgb_dir],d[1],d[1+rgb_dir]); |
|
|
|
break; |
|
|
|
} |
|
|
|
if (write_alpha > 0) |
|
|
|
fwrite(&d[comp-1], 1, 1, f); |
|
|
|
stbiw__write_pixel(s, rgb_dir, comp, write_alpha, expand_mono, d); |
|
|
|
} |
|
|
|
fwrite(&zero,scanline_pad,1,f); |
|
|
|
s->func(s->context, &zero, scanline_pad); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
static int outfile(kt">char const *filename, int rgb_dir, int vdir, int x, int y, int comp, int expand_mono, void *data, int alpha, int pad, const char *fmt, ...) |
|
|
|
static int stbiw__outfile(stbi__write_context *s, int rgb_dir, int vdir, int x, int y, int comp, int expand_mono, void *data, int alpha, int pad, const char *fmt, ...) |
|
|
|
{ |
|
|
|
FILE *f; |
|
|
|
if (y < 0 || x < 0) return 0; |
|
|
|
f = fopen(filename, "wb"); |
|
|
|
if (f) { |
|
|
|
if (y < 0 || x < 0) { |
|
|
|
return 0; |
|
|
|
} else { |
|
|
|
va_list v; |
|
|
|
va_start(v, fmt); |
|
|
|
writefv(f, fmt, v); |
|
|
|
stbiw__writefv(s, fmt, v); |
|
|
|
va_end(v); |
|
|
|
write_pixels(f,rgb_dir,vdir,x,y,comp,data,alpha,pad,expand_mono); |
|
|
|
n">fclose(f); |
|
|
|
stbiw__write_pixels(s,rgb_dir,vdir,x,y,comp,data,alpha,pad, expand_mono); |
|
|
|
return 1; |
|
|
|
} |
|
|
|
return f != NULL; |
|
|
|
} |
|
|
|
|
|
|
|
int stbi_write_bmp(char const *filename, int x, int y, int comp, const void *data) |
|
|
|
static int stbi_write_bmp_core(stbi__write_context *s, int x, int y, int comp, const void *data) |
|
|
|
{ |
|
|
|
int pad = (-x*3) & 3; |
|
|
|
return outfile(filename,-1,-1,x,y,comp,1,(void *) data,0,pad, |
|
|
|
return stbiw__outfile(s,-1,-1,x,y,comp,1,(void *) data,0,pad, |
|
|
|
"11 4 22 4" "4 44 22 444444", |
|
|
|
'B', 'M', 14+40+(x*3+pad)*y, 0,0, 14+40, // file header |
|
|
|
40, x,y, 1,24, 0,0,0,0,0,0); // bitmap header |
|
|
|
} |
|
|
|
|
|
|
|
int stbi_write_tga(char const *filename, int x, int y, int comp, const void *data) |
|
|
|
STBIWDEF int stbi_write_bmp_to_func(stbi_write_func *func, void *context, int x, int y, int comp, const void *data) |
|
|
|
{ |
|
|
|
stbi__write_context s; |
|
|
|
stbi__start_write_callbacks(&s, func, context); |
|
|
|
return stbi_write_bmp_core(&s, x, y, comp, data); |
|
|
|
} |
|
|
|
|
|
|
|
#ifndef STBI_WRITE_NO_STDIO |
|
|
|
STBIWDEF int stbi_write_bmp(char const *filename, int x, int y, int comp, const void *data) |
|
|
|
{ |
|
|
|
stbi__write_context s; |
|
|
|
if (stbi__start_write_file(&s,filename)) { |
|
|
|
int r = stbi_write_bmp_core(&s, x, y, comp, data); |
|
|
|
stbi__end_write_file(&s); |
|
|
|
return r; |
|
|
|
} else |
|
|
|
return 0; |
|
|
|
} |
|
|
|
#endif //!STBI_WRITE_NO_STDIO |
|
|
|
|
|
|
|
static int stbi_write_tga_core(stbi__write_context *s, int x, int y, int comp, void *data) |
|
|
|
{ |
|
|
|
int has_alpha = (comp == 2 || comp == 4); |
|
|
|
int colorbytes = has_alpha ? comp-1 : comp; |
|
|
|
int format = colorbytes < 2 ? 3 : 2; // 3 color channels (RGB/RGBA) = 2, 1 color channel (Y/YA) = 3 |
|
|
|
return outfile(filename, -1,-1, x, y, comp, 0, (void *) data, has_alpha, 0, |
|
|
|
"111 221 2222 11", 0,0,format, 0,0,0, 0,0,x,y, (colorbytes+has_alpha)*8, has_alpha*8); |
|
|
|
|
|
|
|
if (y < 0 || x < 0) |
|
|
|
return 0; |
|
|
|
|
|
|
|
if (!stbi_write_tga_with_rle) { |
|
|
|
return stbiw__outfile(s, -1, -1, x, y, comp, 0, (void *) data, has_alpha, 0, |
|
|
|
"111 221 2222 11", 0, 0, format, 0, 0, 0, 0, 0, x, y, (colorbytes + has_alpha) * 8, has_alpha * 8); |
|
|
|
} else { |
|
|
|
int i,j,k; |
|
|
|
|
|
|
|
stbiw__writef(s, "111 221 2222 11", 0,0,format+8, 0,0,0, 0,0,x,y, (colorbytes + has_alpha) * 8, has_alpha * 8); |
|
|
|
|
|
|
|
for (j = y - 1; j >= 0; --j) { |
|
|
|
unsigned char *row = (unsigned char *) data + j * x * comp; |
|
|
|
int len; |
|
|
|
|
|
|
|
for (i = 0; i < x; i += len) { |
|
|
|
unsigned char *begin = row + i * comp; |
|
|
|
int diff = 1; |
|
|
|
len = 1; |
|
|
|
|
|
|
|
if (i < x - 1) { |
|
|
|
++len; |
|
|
|
diff = memcmp(begin, row + (i + 1) * comp, comp); |
|
|
|
if (diff) { |
|
|
|
const unsigned char *prev = begin; |
|
|
|
for (k = i + 2; k < x && len < 128; ++k) { |
|
|
|
if (memcmp(prev, row + k * comp, comp)) { |
|
|
|
prev += comp; |
|
|
|
++len; |
|
|
|
} else { |
|
|
|
--len; |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
} else { |
|
|
|
for (k = i + 2; k < x && len < 128; ++k) { |
|
|
|
if (!memcmp(begin, row + k * comp, comp)) { |
|
|
|
++len; |
|
|
|
} else { |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
if (diff) { |
|
|
|
unsigned char header = STBIW_UCHAR(len - 1); |
|
|
|
s->func(s->context, &header, 1); |
|
|
|
for (k = 0; k < len; ++k) { |
|
|
|
stbiw__write_pixel(s, -1, comp, has_alpha, 0, begin + k * comp); |
|
|
|
} |
|
|
|
} else { |
|
|
|
unsigned char header = STBIW_UCHAR(len - 129); |
|
|
|
s->func(s->context, &header, 1); |
|
|
|
stbiw__write_pixel(s, -1, comp, has_alpha, 0, begin); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
return 1; |
|
|
|
} |
|
|
|
|
|
|
|
int stbi_write_tga_to_func(stbi_write_func *func, void *context, int x, int y, int comp, const void *data) |
|
|
|
{ |
|
|
|
stbi__write_context s; |
|
|
|
stbi__start_write_callbacks(&s, func, context); |
|
|
|
return stbi_write_tga_core(&s, x, y, comp, (void *) data); |
|
|
|
} |
|
|
|
|
|
|
|
#ifndef STBI_WRITE_NO_STDIO |
|
|
|
int stbi_write_tga(char const *filename, int x, int y, int comp, const void *data) |
|
|
|
{ |
|
|
|
stbi__write_context s; |
|
|
|
if (stbi__start_write_file(&s,filename)) { |
|
|
|
int r = stbi_write_tga_core(&s, x, y, comp, (void *) data); |
|
|
|
stbi__end_write_file(&s); |
|
|
|
return r; |
|
|
|
} else |
|
|
|
return 0; |
|
|
|
} |
|
|
|
#endif |
|
|
|
|
|
|
|
// ************************************************************************************************* |
|
|
|
// Radiance RGBE HDR writer |
|
|
|
// by Baldur Karlsson |
|
|
|
#ifndef STBI_WRITE_NO_STDIO |
|
|
|
|
|
|
|
#define stbiw__max(a, b) ((a) > (b) ? (a) : (b)) |
|
|
|
|
|
|
|
void stbiw__linear_to_rgbe(unsigned char *rgbe, float *linear) |
|
|
@ -247,7 +483,7 @@ void stbiw__linear_to_rgbe(unsigned char *rgbe, float *linear) |
|
|
|
int exponent; |
|
|
|
float maxcomp = stbiw__max(linear[0], stbiw__max(linear[1], linear[2])); |
|
|
|
|
|
|
|
if (maxcomp < 1e-32) { |
|
|
|
if (maxcomp < 1e-32n">f) { |
|
|
|
rgbe[0] = rgbe[1] = rgbe[2] = rgbe[3] = 0; |
|
|
|
} else { |
|
|
|
float normalize = (float) frexp(maxcomp, &exponent) * 256.0f/maxcomp; |
|
|
@ -259,23 +495,23 @@ void stbiw__linear_to_rgbe(unsigned char *rgbe, float *linear) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
void stbiw__write_run_data(FILE *f, int length, unsigned char databyte) |
|
|
|
void stbiw__write_run_data(stbi__write_context *s, int length, unsigned char databyte) |
|
|
|
{ |
|
|
|
unsigned char lengthbyte = p">(unsigned char) (length+128); |
|
|
|
unsigned char lengthbyte = n">STBIW_UCHAR(length+128); |
|
|
|
STBIW_ASSERT(length+128 <= 255); |
|
|
|
fwrite(&lengthbyte, 1, 1, f); |
|
|
|
fwrite(&databyte, 1, 1, f); |
|
|
|
s->func(s->context, &lengthbyte, 1); |
|
|
|
s->func(s->context, &databyte, 1); |
|
|
|
} |
|
|
|
|
|
|
|
void stbiw__write_dump_data(FILE *f, int length, unsigned char *data) |
|
|
|
void stbiw__write_dump_data(stbi__write_context *s, int length, unsigned char *data) |
|
|
|
{ |
|
|
|
unsigned char lengthbyte = p">(unsigned char )(length & 0xff); |
|
|
|
unsigned char lengthbyte = n">STBIW_UCHAR(length); |
|
|
|
STBIW_ASSERT(length <= 128); // inconsistent with spec but consistent with official code |
|
|
|
fwrite(&lengthbyte, 1, 1, f); |
|
|
|
fwrite(data, length, 1, f); |
|
|
|
s->func(s->context, &lengthbyte, 1); |
|
|
|
s->func(s->context, data, length); |
|
|
|
} |
|
|
|
|
|
|
|
void stbiw__write_hdr_scanline(FILE *f, int width, int comp, unsigned char *scratch, const float *scanline) |
|
|
|
void stbiw__write_hdr_scanline(stbi__write_context *s, int width, int ncomp, unsigned char *scratch, float *scanline) |
|
|
|
{ |
|
|
|
unsigned char scanlineheader[4] = { 2, 2, 0, 0 }; |
|
|
|
unsigned char rgbe[4]; |
|
|
@ -288,31 +524,31 @@ void stbiw__write_hdr_scanline(FILE *f, int width, int comp, unsigned char *scra |
|
|
|
/* skip RLE for images too small or large */ |
|
|
|
if (width < 8 || width >= 32768) { |
|
|
|
for (x=0; x < width; x++) { |
|
|
|
switch (comp) { |
|
|
|
switch (ncomp) { |
|
|
|
case 4: /* fallthrough */ |
|
|
|
case 3: linear[2] = scanline[x*comp + 2]; |
|
|
|
linear[1] = scanline[x*comp + 1]; |
|
|
|
linear[0] = scanline[x*comp + 0]; |
|
|
|
case 3: linear[2] = scanline[x*ncomp + 2]; |
|
|
|
linear[1] = scanline[x*ncomp + 1]; |
|
|
|
linear[0] = scanline[x*ncomp + 0]; |
|
|
|
break; |
|
|
|
case 2: /* fallthrough */ |
|
|
|
case 1: linear[0] = linear[1] = linear[2] = scanline[x*comp + 0]; |
|
|
|
default: |
|
|
|
linear[0] = linear[1] = linear[2] = scanline[x*ncomp + 0]; |
|
|
|
break; |
|
|
|
} |
|
|
|
stbiw__linear_to_rgbe(rgbe, linear); |
|
|
|
fwrite(rgbe, 4, 1, f); |
|
|
|
s->func(s->context, rgbe, 4); |
|
|
|
} |
|
|
|
} else { |
|
|
|
int c,r; |
|
|
|
/* encode into scratch buffer */ |
|
|
|
for (x=0; x < width; x++) { |
|
|
|
switch(comp) { |
|
|
|
switch(ncomp) { |
|
|
|
case 4: /* fallthrough */ |
|
|
|
case 3: linear[2] = scanline[x*comp + 2]; |
|
|
|
linear[1] = scanline[x*comp + 1]; |
|
|
|
linear[0] = scanline[x*comp + 0]; |
|
|
|
case 3: linear[2] = scanline[x*ncomp + 2]; |
|
|
|
linear[1] = scanline[x*ncomp + 1]; |
|
|
|
linear[0] = scanline[x*ncomp + 0]; |
|
|
|
break; |
|
|
|
case 2: /* fallthrough */ |
|
|
|
case 1: linear[0] = linear[1] = linear[2] = scanline[x*comp + 0]; |
|
|
|
default: |
|
|
|
linear[0] = linear[1] = linear[2] = scanline[x*ncomp + 0]; |
|
|
|
break; |
|
|
|
} |
|
|
|
stbiw__linear_to_rgbe(rgbe, linear); |
|
|
@ -322,7 +558,7 @@ void stbiw__write_hdr_scanline(FILE *f, int width, int comp, unsigned char *scra |
|
|
|
scratch[x + width*3] = rgbe[3]; |
|
|
|
} |
|
|
|
|
|
|
|
fwrite(scanlineheader, 4, 1, f); |
|
|
|
s->func(s->context, scanlineheader, 4); |
|
|
|
|
|
|
|
/* RLE each component separately */ |
|
|
|
for (c=0; c < 4; c++) { |
|
|
@ -343,7 +579,7 @@ void stbiw__write_hdr_scanline(FILE *f, int width, int comp, unsigned char *scra |
|
|
|
while (x < r) { |
|
|
|
int len = r-x; |
|
|
|
if (len > 128) len = 128; |
|
|
|
stbiw__write_dump_data(f, len, &comp[x]); |
|
|
|
stbiw__write_dump_data(s, len, &comp[x]); |
|
|
|
x += len; |
|
|
|
} |
|
|
|
// if there's a run, output it |
|
|
@ -355,7 +591,7 @@ void stbiw__write_hdr_scanline(FILE *f, int width, int comp, unsigned char *scra |
|
|
|
while (x < r) { |
|
|
|
int len = r-x; |
|
|
|
if (len > 127) len = 127; |
|
|
|
stbiw__write_run_data(f, len, comp[x]); |
|
|
|
stbiw__write_run_data(s, len, comp[x]); |
|
|
|
x += len; |
|
|
|
} |
|
|
|
} |
|
|
@ -364,27 +600,52 @@ 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 float *data) |
|
|
|
static int stbi_write_hdr_core(stbi__write_context *s, int x, int y, int comp, float *data) |
|
|
|
{ |
|
|
|
int i; |
|
|
|
FILE *f; |
|
|
|
if (y <= 0 || x <= 0 || data == NULL) return 0; |
|
|
|
f = fopen(filename, "wb"); |
|
|
|
if (f) { |
|
|
|
/* Each component is stored separately. Allocate scratch space for full output scanline. */ |
|
|
|
if (y <= 0 || x <= 0 || data == NULL) |
|
|
|
return 0; |
|
|
|
else { |
|
|
|
// Each component is stored separately. Allocate scratch space for full output scanline. |
|
|
|
unsigned char *scratch = (unsigned char *) STBIW_MALLOC(x*4); |
|
|
|
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); |
|
|
|
int i, len; |
|
|
|
char buffer[128]; |
|
|
|
char header[] = "#?RADIANCE\n# Written by stb_image_write.h\nFORMAT=32-bit_rle_rgbe\n"; |
|
|
|
s->func(s->context, header, sizeof(header)-1); |
|
|
|
|
|
|
|
len = sprintf(buffer, "EXPOSURE= 1.0000000000000\n\n-Y %d +X %d\n", y, x); |
|
|
|
s->func(s->context, buffer, len); |
|
|
|
|
|
|
|
for(i=0; i < y; i++) |
|
|
|
stbiw__write_hdr_scanline(f, x, comp, scratch, data + comp*i*x); |
|
|
|
stbiw__write_hdr_scanline(s, x, comp, scratch, data + comp*i*x); |
|
|
|
STBIW_FREE(scratch); |
|
|
|
fclose(f); |
|
|
|
k">return 1; |
|
|
|
} |
|
|
|
return f != NULL; |
|
|
|
} |
|
|
|
|
|
|
|
///////////////////////////////////////////////////////// |
|
|
|
// PNG |
|
|
|
int stbi_write_hdr_to_func(stbi_write_func *func, void *context, int x, int y, int comp, const float *data) |
|
|
|
{ |
|
|
|
stbi__write_context s; |
|
|
|
stbi__start_write_callbacks(&s, func, context); |
|
|
|
return stbi_write_hdr_core(&s, x, y, comp, (float *) data); |
|
|
|
} |
|
|
|
|
|
|
|
int stbi_write_hdr(char const *filename, int x, int y, int comp, const float *data) |
|
|
|
{ |
|
|
|
stbi__write_context s; |
|
|
|
if (stbi__start_write_file(&s,filename)) { |
|
|
|
int r = stbi_write_hdr_core(&s, x, y, comp, (float *) data); |
|
|
|
stbi__end_write_file(&s); |
|
|
|
return r; |
|
|
|
} else |
|
|
|
return 0; |
|
|
|
} |
|
|
|
#endif // STBI_WRITE_NO_STDIO |
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////// |
|
|
|
// |
|
|
|
// PNG writer |
|
|
|
// |
|
|
|
|
|
|
|
// stretchy buffer; stbiw__sbpush() == vector<>::push_back() -- stbiw__sbcount() == vector<>::size() |
|
|
|
#define stbiw__sbraw(a) ((int *) (a) - 2) |
|
|
@ -402,7 +663,7 @@ int stbi_write_hdr(char const *filename, int x, int y, int comp, const float *da |
|
|
|
static void *stbiw__sbgrowf(void **arr, int increment, int itemsize) |
|
|
|
{ |
|
|
|
int m = *arr ? 2*stbiw__sbm(*arr)+increment : increment+1; |
|
|
|
void *p = STBIW_REALLOC(*arr ? stbiw__sbraw(*arr) : 0, itemsize * m + sizeof(int)*2); |
|
|
|
void *p = STBIW_REALLOC_SIZED(*arr ? stbiw__sbraw(*arr) : 0, *arr ? (stbiw__sbm(*arr)*itemsize + sizeof(int)*2) : 0, itemsize * m + sizeof(int)*2); |
|
|
|
STBIW_ASSERT(p); |
|
|
|
if (p) { |
|
|
|
if (!*arr) ((int *) p)[1] = 0; |
|
|
@ -415,7 +676,7 @@ static void *stbiw__sbgrowf(void **arr, int increment, int itemsize) |
|
|
|
static unsigned char *stbiw__zlib_flushf(unsigned char *data, unsigned int *bitbuffer, int *bitcount) |
|
|
|
{ |
|
|
|
while (*bitcount >= 8) { |
|
|
|
stbiw__sbpush(data, p">(unsigned char) *bitbuffer); |
|
|
|
stbiw__sbpush(data, n">STBIW_UCHAR(*bitbuffer)); |
|
|
|
*bitbuffer >>= 8; |
|
|
|
*bitcount -= 8; |
|
|
|
} |
|
|
@ -550,18 +811,19 @@ unsigned char * stbi_zlib_compress(unsigned char *data, int data_len, int *out_l |
|
|
|
|
|
|
|
{ |
|
|
|
// compute adler32 on input |
|
|
|
unsigned int i=0, s1=1, s2=0, blocklen = data_len % 5552; |
|
|
|
int j=0; |
|
|
|
unsigned int s1=1, s2=0; |
|
|
|
int blocklen = (int) (data_len % 5552); |
|
|
|
j=0; |
|
|
|
while (j < data_len) { |
|
|
|
for (i=0; i < blocklen; ++i) s1 += data[j+i], s2 += s1; |
|
|
|
s1 %= 65521, s2 %= 65521; |
|
|
|
j += blocklen; |
|
|
|
blocklen = 5552; |
|
|
|
} |
|
|
|
stbiw__sbpush(out, p">(unsigned char) (s2 >> 8)); |
|
|
|
stbiw__sbpush(out, p">(unsigned char) s2); |
|
|
|
stbiw__sbpush(out, p">(unsigned char) (s1 >> 8)); |
|
|
|
stbiw__sbpush(out, p">(unsigned char) s1); |
|
|
|
stbiw__sbpush(out, n">STBIW_UCHAR(s2 >> 8)); |
|
|
|
stbiw__sbpush(out, n">STBIW_UCHAR(s2)); |
|
|
|
stbiw__sbpush(out, n">STBIW_UCHAR(s1 >> 8)); |
|
|
|
stbiw__sbpush(out, n">STBIW_UCHAR(s1)); |
|
|
|
} |
|
|
|
*out_len = stbiw__sbn(out); |
|
|
|
// make returned pointer freeable |
|
|
@ -569,21 +831,52 @@ unsigned char * stbi_zlib_compress(unsigned char *data, int data_len, int *out_l |
|
|
|
return (unsigned char *) stbiw__sbraw(out); |
|
|
|
} |
|
|
|
|
|
|
|
unsigned int stbiw__crc32(unsigned char *buffer, int len) |
|
|
|
static unsigned int stbiw__crc32(unsigned char *buffer, int len) |
|
|
|
{ |
|
|
|
static unsigned int crc_table[256]; |
|
|
|
static unsigned int crc_table[256] = |
|
|
|
{ |
|
|
|
0x00000000, 0x77073096, 0xEE0E612C, 0x990951BA, 0x076DC419, 0x706AF48F, 0xE963A535, 0x9E6495A3, |
|
|
|
0x0eDB8832, 0x79DCB8A4, 0xE0D5E91E, 0x97D2D988, 0x09B64C2B, 0x7EB17CBD, 0xE7B82D07, 0x90BF1D91, |
|
|
|
0x1DB71064, 0x6AB020F2, 0xF3B97148, 0x84BE41DE, 0x1ADAD47D, 0x6DDDE4EB, 0xF4D4B551, 0x83D385C7, |
|
|
|
0x136C9856, 0x646BA8C0, 0xFD62F97A, 0x8A65C9EC, 0x14015C4F, 0x63066CD9, 0xFA0F3D63, 0x8D080DF5, |
|
|
|
0x3B6E20C8, 0x4C69105E, 0xD56041E4, 0xA2677172, 0x3C03E4D1, 0x4B04D447, 0xD20D85FD, 0xA50AB56B, |
|
|
|
0x35B5A8FA, 0x42B2986C, 0xDBBBC9D6, 0xACBCF940, 0x32D86CE3, 0x45DF5C75, 0xDCD60DCF, 0xABD13D59, |
|
|
|
0x26D930AC, 0x51DE003A, 0xC8D75180, 0xBFD06116, 0x21B4F4B5, 0x56B3C423, 0xCFBA9599, 0xB8BDA50F, |
|
|
|
0x2802B89E, 0x5F058808, 0xC60CD9B2, 0xB10BE924, 0x2F6F7C87, 0x58684C11, 0xC1611DAB, 0xB6662D3D, |
|
|
|
0x76DC4190, 0x01DB7106, 0x98D220BC, 0xEFD5102A, 0x71B18589, 0x06B6B51F, 0x9FBFE4A5, 0xE8B8D433, |
|
|
|
0x7807C9A2, 0x0F00F934, 0x9609A88E, 0xE10E9818, 0x7F6A0DBB, 0x086D3D2D, 0x91646C97, 0xE6635C01, |
|
|
|
0x6B6B51F4, 0x1C6C6162, 0x856530D8, 0xF262004E, 0x6C0695ED, 0x1B01A57B, 0x8208F4C1, 0xF50FC457, |
|
|
|
0x65B0D9C6, 0x12B7E950, 0x8BBEB8EA, 0xFCB9887C, 0x62DD1DDF, 0x15DA2D49, 0x8CD37CF3, 0xFBD44C65, |
|
|
|
0x4DB26158, 0x3AB551CE, 0xA3BC0074, 0xD4BB30E2, 0x4ADFA541, 0x3DD895D7, 0xA4D1C46D, 0xD3D6F4FB, |
|
|
|
0x4369E96A, 0x346ED9FC, 0xAD678846, 0xDA60B8D0, 0x44042D73, 0x33031DE5, 0xAA0A4C5F, 0xDD0D7CC9, |
|
|
|
0x5005713C, 0x270241AA, 0xBE0B1010, 0xC90C2086, 0x5768B525, 0x206F85B3, 0xB966D409, 0xCE61E49F, |
|
|
|
0x5EDEF90E, 0x29D9C998, 0xB0D09822, 0xC7D7A8B4, 0x59B33D17, 0x2EB40D81, 0xB7BD5C3B, 0xC0BA6CAD, |
|
|
|
0xEDB88320, 0x9ABFB3B6, 0x03B6E20C, 0x74B1D29A, 0xEAD54739, 0x9DD277AF, 0x04DB2615, 0x73DC1683, |
|
|
|
0xE3630B12, 0x94643B84, 0x0D6D6A3E, 0x7A6A5AA8, 0xE40ECF0B, 0x9309FF9D, 0x0A00AE27, 0x7D079EB1, |
|
|
|
0xF00F9344, 0x8708A3D2, 0x1E01F268, 0x6906C2FE, 0xF762575D, 0x806567CB, 0x196C3671, 0x6E6B06E7, |
|
|
|
0xFED41B76, 0x89D32BE0, 0x10DA7A5A, 0x67DD4ACC, 0xF9B9DF6F, 0x8EBEEFF9, 0x17B7BE43, 0x60B08ED5, |
|
|
|
0xD6D6A3E8, 0xA1D1937E, 0x38D8C2C4, 0x4FDFF252, 0xD1BB67F1, 0xA6BC5767, 0x3FB506DD, 0x48B2364B, |
|
|
|
0xD80D2BDA, 0xAF0A1B4C, 0x36034AF6, 0x41047A60, 0xDF60EFC3, 0xA867DF55, 0x316E8EEF, 0x4669BE79, |
|
|
|
0xCB61B38C, 0xBC66831A, 0x256FD2A0, 0x5268E236, 0xCC0C7795, 0xBB0B4703, 0x220216B9, 0x5505262F, |
|
|
|
0xC5BA3BBE, 0xB2BD0B28, 0x2BB45A92, 0x5CB36A04, 0xC2D7FFA7, 0xB5D0CF31, 0x2CD99E8B, 0x5BDEAE1D, |
|
|
|
0x9B64C2B0, 0xEC63F226, 0x756AA39C, 0x026D930A, 0x9C0906A9, 0xEB0E363F, 0x72076785, 0x05005713, |
|
|
|
0x95BF4A82, 0xE2B87A14, 0x7BB12BAE, 0x0CB61B38, 0x92D28E9B, 0xE5D5BE0D, 0x7CDCEFB7, 0x0BDBDF21, |
|
|
|
0x86D3D2D4, 0xF1D4E242, 0x68DDB3F8, 0x1FDA836E, 0x81BE16CD, 0xF6B9265B, 0x6FB077E1, 0x18B74777, |
|
|
|
0x88085AE6, 0xFF0F6A70, 0x66063BCA, 0x11010B5C, 0x8F659EFF, 0xF862AE69, 0x616BFFD3, 0x166CCF45, |
|
|
|
0xA00AE278, 0xD70DD2EE, 0x4E048354, 0x3903B3C2, 0xA7672661, 0xD06016F7, 0x4969474D, 0x3E6E77DB, |
|
|
|
0xAED16A4A, 0xD9D65ADC, 0x40DF0B66, 0x37D83BF0, 0xA9BCAE53, 0xDEBB9EC5, 0x47B2CF7F, 0x30B5FFE9, |
|
|
|
0xBDBDF21C, 0xCABAC28A, 0x53B39330, 0x24B4A3A6, 0xBAD03605, 0xCDD70693, 0x54DE5729, 0x23D967BF, |
|
|
|
0xB3667A2E, 0xC4614AB8, 0x5D681B02, 0x2A6F2B94, 0xB40BBE37, 0xC30C8EA1, 0x5A05DF1B, 0x2D02EF8D |
|
|
|
}; |
|
|
|
|
|
|
|
unsigned int crc = ~0u; |
|
|
|
int i,j; |
|
|
|
if (crc_table[1] == 0) |
|
|
|
for(i=0; i < 256; i++) |
|
|
|
for (crc_table[i]=i, j=0; j < 8; ++j) |
|
|
|
crc_table[i] = (crc_table[i] >> 1) ^ (crc_table[i] & 1 ? 0xedb88320 : 0); |
|
|
|
int i; |
|
|
|
for (i=0; i < len; ++i) |
|
|
|
crc = (crc >> 8) ^ crc_table[buffer[i] ^ (crc & 0xff)]; |
|
|
|
return ~crc; |
|
|
|
} |
|
|
|
|
|
|
|
#define stbiw__wpng4(o,a,b,c,d) ((o)[0]=(unsigned char)(a),(o)[1]=(unsigned char)(b),(o)[2]=(unsigned char)(c),(o)[3]=(unsigned char)(d),(o)+=4) |
|
|
|
#define stbiw__wpng4(o,a,b,c,d) ((o)[0]=STBIW_UCHAR(a),(o)[1]=STBIW_UCHAR(b),(o)[2]=STBIW_UCHAR(c),(o)[3]=STBIW_UCHAR(d),(o)+=4) |
|
|
|
#define stbiw__wp32(data,v) stbiw__wpng4(data, (v)>>24,(v)>>16,(v)>>8,(v)); |
|
|
|
#define stbiw__wptag(data,s) stbiw__wpng4(data, s[0],s[1],s[2],s[3]) |
|
|
|
|
|
|
@ -596,9 +889,9 @@ static void stbiw__wpcrc(unsigned char **data, int len) |
|
|
|
static unsigned char stbiw__paeth(int a, int b, int c) |
|
|
|
{ |
|
|
|
int p = a + b - c, pa = abs(p-a), pb = abs(p-b), pc = abs(p-c); |
|
|
|
if (pa <= pb && pa <= pc) return p">(unsigned char) a; |
|
|
|
if (pb <= pc) return p">(unsigned char) b; |
|
|
|
return p">(unsigned char) c; |
|
|
|
if (pa <= pb && pa <= pc) return n">STBIW_UCHAR(a); |
|
|
|
if (pb <= pc) return n">STBIW_UCHAR(b); |
|
|
|
return nf">STBIW_UCHAR(c); |
|
|
|
} |
|
|
|
|
|
|
|
unsigned char *stbi_write_png_to_mem(unsigned char *pixels, int stride_bytes, int x, int y, int n, int *out_len) |
|
|
@ -671,7 +964,7 @@ unsigned char *stbi_write_png_to_mem(unsigned char *pixels, int stride_bytes, in |
|
|
|
stbiw__wp32(o, x); |
|
|
|
stbiw__wp32(o, y); |
|
|
|
*o++ = 8; |
|
|
|
*o++ = p">(unsigned char) ctype[n]; |
|
|
|
*o++ = n">STBIW_UCHAR(ctype[n]); |
|
|
|
*o++ = 0; |
|
|
|
*o++ = 0; |
|
|
|
*o++ = 0; |
|
|
@ -693,12 +986,13 @@ unsigned char *stbi_write_png_to_mem(unsigned char *pixels, int stride_bytes, in |
|
|
|
return out; |
|
|
|
} |
|
|
|
|
|
|
|
int stbi_write_png(char const *filename, int x, int y, int comp, const void *data, int stride_bytes) |
|
|
|
#ifndef STBI_WRITE_NO_STDIO |
|
|
|
STBIWDEF int stbi_write_png(char const *filename, int x, int y, int comp, const void *data, int stride_bytes) |
|
|
|
{ |
|
|
|
FILE *f; |
|
|
|
int len; |
|
|
|
unsigned char *png = stbi_write_png_to_mem((unsigned char *) data, stride_bytes, x, y, comp, &len); |
|
|
|
if (o">!png) return 0; |
|
|
|
if (n">png == NULL) return 0; |
|
|
|
f = fopen(filename, "wb"); |
|
|
|
if (!f) { STBIW_FREE(png); return 0; } |
|
|
|
fwrite(png, 1, len, f); |
|
|
@ -706,9 +1000,29 @@ int stbi_write_png(char const *filename, int x, int y, int comp, const void *dat |
|
|
|
STBIW_FREE(png); |
|
|
|
return 1; |
|
|
|
} |
|
|
|
#endif |
|
|
|
|
|
|
|
STBIWDEF int stbi_write_png_to_func(stbi_write_func *func, void *context, int x, int y, int comp, const void *data, int stride_bytes) |
|
|
|
{ |
|
|
|
int len; |
|
|
|
unsigned char *png = stbi_write_png_to_mem((unsigned char *) data, stride_bytes, x, y, comp, &len); |
|
|
|
if (png == NULL) return 0; |
|
|
|
func(context, png, len); |
|
|
|
STBIW_FREE(png); |
|
|
|
return 1; |
|
|
|
} |
|
|
|
|
|
|
|
#endif // STB_IMAGE_WRITE_IMPLEMENTATION |
|
|
|
|
|
|
|
/* Revision history |
|
|
|
1.01 (2016-01-16) |
|
|
|
STBIW_REALLOC_SIZED: support allocators with no realloc support |
|
|
|
avoid race-condition in crc initialization |
|
|
|
minor compile issues |
|
|
|
1.00 (2015-09-14) |
|
|
|
installable file IO function |
|
|
|
0.99 (2015-09-13) |
|
|
|
warning fixes; TGA rle support |
|
|
|
0.98 (2015-04-08) |
|
|
|
added STBIW_MALLOC, STBIW_ASSERT etc |
|
|
|
0.97 (2015-01-18) |
|
|
|