1#ifndef INCLUDE_STB_IMAGE_WRITE_16_H
2#define INCLUDE_STB_IMAGE_WRITE_16_H
8#ifdef STB_IMAGE_WRITE_16_STATIC
9#define STBIW16DEF static
12#define STBIW16DEF extern "C"
14#define STBIW16DEF extern
19#ifndef STB_IMAGE_WRITE_16_STATIC
24#ifndef STBI_WRITE_16_NO_STDIO
30#ifdef STB_IMAGE_WRITE_16_IMPLEMENTATION
33#ifndef _CRT_SECURE_NO_WARNINGS
34#define _CRT_SECURE_NO_WARNINGS
36#ifndef _CRT_NONSTDC_NO_DEPRECATE
37#define _CRT_NONSTDC_NO_DEPRECATE
42#ifndef STBI_WRITE_16_NO_STDIO
51#if defined(STBIW_MALLOC) && defined(STBIW_FREE) && (defined(STBIW_REALLOC) || defined(STBIW_REALLOC_SIZED))
53#elif !defined(STBIW_MALLOC) && !defined(STBIW_FREE) && !defined(STBIW_REALLOC) && !defined(STBIW_REALLOC_SIZED)
56#error "Must define all or none of STBIW_MALLOC, STBIW_FREE, and STBIW_REALLOC (or STBIW_REALLOC_SIZED)."
60#define STBIW_MALLOC(sz) malloc(sz)
61#define STBIW_REALLOC(p,newsz) realloc(p,newsz)
62#define STBIW_FREE(p) free(p)
65#ifndef STBIW_REALLOC_SIZED
66#define STBIW_REALLOC_SIZED(p,oldsz,newsz) STBIW_REALLOC(p,newsz)
71#define STBIW_MEMMOVE(a,b,sz) memmove(a,b,sz)
77#define STBIW_ASSERT(x) assert(x)
80#define STBIW_UCHAR(x) (unsigned char) ((x) & 0xff)
82#ifdef STB_IMAGE_WRITE_STATIC
90STBIW16DEF unsigned char* stbi_write_png16_to_mem(
const unsigned short* pixels,
int stride_bytes,
int x,
int y,
int comp,
int* out_len)
93 int ctype[5] = { -1, 0, 4, 2, 6 };
94 unsigned char sig[8] = { 137, 80, 78, 71, 13, 10, 26, 10 };
95 unsigned char* out, * o, * filt, * zlib, * pixels8;
96 signed char* line_buffer;
98 int bytes_per_pixel = comp * 2;
101 if (stride_bytes == 0)
102 stride_bytes = x * bytes_per_pixel;
104 if (force_filter >= 5) {
109 pixels8 = (
unsigned char*)STBIW_MALLOC((
size_t)y * stride_bytes);
110 if (!pixels8)
return 0;
113 for (
int r = 0;
r < y; ++
r) {
114 const unsigned char* src_bytes = (
const unsigned char*)pixels + (
size_t)
r * stride_bytes;
115 const unsigned short* src = (
const unsigned short*)src_bytes;
116 unsigned char* dst = pixels8 + (size_t)r * stride_bytes;
117 for (
int i = 0; i < x; ++i) {
118 for (
int c = 0; c < comp; ++c) {
119 unsigned short v = src[i * comp + c];
121 dst[(i * bytes_per_pixel) + c * 2 + 0] = STBIW_UCHAR(v >> 8);
122 dst[(i * bytes_per_pixel) + c * 2 + 1] = STBIW_UCHAR(v);
129 filt = (
unsigned char*)STBIW_MALLOC((x * bytes_per_pixel + 1) * y);
if (!filt) { STBIW_FREE(pixels8);
return 0; }
130 line_buffer = (
signed char*)STBIW_MALLOC(x * bytes_per_pixel);
if (!line_buffer) { STBIW_FREE(filt); STBIW_FREE(pixels8);
return 0; }
131 for (j = 0; j < y; ++j) {
133 if (force_filter > -1) {
134 filter_type = force_filter;
135 stbiw__encode_png_line(pixels8, stride_bytes, x, y, j, bytes_per_pixel, force_filter, line_buffer);
138 int best_filter = 0, best_filter_val = 0x7fffffff, est, i;
139 for (filter_type = 0; filter_type < 5; filter_type++) {
140 stbiw__encode_png_line(pixels8, stride_bytes, x, y, j, bytes_per_pixel, filter_type, line_buffer);
142 for (i = 0; i < x * bytes_per_pixel; ++i) {
143 est +=
abs((
signed char)line_buffer[i]);
145 if (est < best_filter_val) {
146 best_filter_val = est;
147 best_filter = filter_type;
150 if (filter_type != best_filter) {
151 stbiw__encode_png_line(pixels8, stride_bytes, x, y, j, bytes_per_pixel, best_filter, line_buffer);
152 filter_type = best_filter;
155 filt[j * (x * bytes_per_pixel + 1)] = (
unsigned char)filter_type;
156 STBIW_MEMMOVE(filt + j * (x * bytes_per_pixel + 1) + 1, line_buffer, x * bytes_per_pixel);
158 STBIW_FREE(line_buffer);
166 out = (
unsigned char*)STBIW_MALLOC(8 + 12 + 13 + 12 + zlen + 12);
167 if (!out) { STBIW_FREE(zlib);
return 0; }
168 *out_len = 8 + 12 + 13 + 12 + zlen + 12;
171 STBIW_MEMMOVE(o, sig, 8); o += 8;
173 stbiw__wptag(o,
"IHDR");
177 *o++ = STBIW_UCHAR(ctype[comp]);
181 stbiw__wpcrc(&o, 13);
183 stbiw__wp32(o, zlen);
184 stbiw__wptag(o,
"IDAT");
185 STBIW_MEMMOVE(o, zlib, zlen);
188 stbiw__wpcrc(&o, zlen);
191 stbiw__wptag(o,
"IEND");
194 STBIW_ASSERT(o == out + *out_len);
202 unsigned char* png = stbi_write_png16_to_mem((
const unsigned short*)data, stride_bytes, x, y, comp, &len);
203 if (png == NULL)
return 0;
204 func(context, png, len);
209#ifndef STBI_WRITE_NO_STDIO
214 unsigned char* png = stbi_write_png16_to_mem((
const unsigned short*)data, stride_bytes, x, y, comp, &len);
215 if (png == NULL)
return 0;
217 f = stbiw__fopen(filename,
"wb");
218 if (!f) { STBIW_FREE(png);
return 0; }
219 fwrite(png, 1, len, f);
__forceinline scalar abs(scalar a)
Definition scalar.h:451
float r
Definition ssaocontext.cc:49
STBIW16DEF int stbi_write_png16(char const *filename, int w, int h, int comp, const void *data, int stride_in_bytes)
int stbi_write_force_png_16_filter
STBIW16DEF int stbi_write_png16_to_func(stbi_write_func *func, void *context, int w, int h, int comp, const void *data, int stride_in_bytes)
#define STBIW16DEF
Definition stb_image_write_16bit.h:14
int stbi_write_png_16_compression_level