57 static void Flush(png_structp png_ptr);
61 return (_val << 8) + _val;
66 const int num_to_check = 8;
67 if (
size < num_to_check) {
72 #if (PNG_LIBPNG_VER_MAJOR == 1) && (PNG_LIBPNG_VER_MINOR == 2)
74 ok = !png_sig_cmp((png_bytep)mem, 0, num_to_check);
76 ok = !png_sig_cmp(mem, 0, num_to_check);
81 static void Flush(png_structp png_ptr)
88 ImBuf *ibuf = (
ImBuf *)png_get_io_ptr(png_ptr);
104 if (length <= rs->
size - rs->
seek) {
111 printf(
"Reached EOF while decoding PNG\n");
112 longjmp(png_jmpbuf(png_ptr), 1);
131 unsigned char *pixels =
NULL;
132 unsigned char *
from, *to;
133 unsigned short *pixels16 =
NULL, *to16;
134 float *from_float, from_straight[4];
135 png_bytepp row_pointers =
NULL;
136 int i, bytesperpixel, color_type = PNG_COLOR_TYPE_GRAY;
149 compression = compression < 0 ? 0 : (compression > 9 ? 9 : compression);
162 filepath =
"<memory>";
165 bytesperpixel = (ibuf->
planes + 7) >> 3;
166 if ((bytesperpixel > 4) || (bytesperpixel == 2)) {
168 "imb_savepng: Unsupported bytes per pixel: %d for file: '%s'\n", bytesperpixel, filepath);
172 png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,
NULL,
NULL,
NULL);
173 if (png_ptr ==
NULL) {
174 printf(
"imb_savepng: Cannot png_create_write_struct for file: '%s'\n", filepath);
178 info_ptr = png_create_info_struct(png_ptr);
179 if (info_ptr ==
NULL) {
180 png_destroy_write_struct(&png_ptr, (png_infopp)
NULL);
181 printf(
"imb_savepng: Cannot png_create_info_struct for file: '%s'\n", filepath);
186 num_bytes = ((size_t)ibuf->
x) * ibuf->
y * bytesperpixel;
188 pixels16 =
MEM_mallocN(num_bytes *
sizeof(
unsigned short),
"png 16bit pixels");
191 pixels =
MEM_mallocN(num_bytes *
sizeof(
unsigned char),
"png 8bit pixels");
193 if (pixels ==
NULL && pixels16 ==
NULL) {
195 "imb_savepng: Cannot allocate pixels array of %dx%d, %d bytes per pixel for file: "
204 row_pointers = (png_bytepp)
MEM_mallocN(ibuf->
y *
sizeof(png_bytep),
"row_pointers");
205 if (row_pointers ==
NULL) {
206 printf(
"imb_savepng: Cannot allocate row-pointers array for file '%s'\n", filepath);
209 if ((pixels ==
NULL && pixels16 ==
NULL) || (row_pointers ==
NULL) ||
210 setjmp(png_jmpbuf(png_ptr))) {
212 png_destroy_write_struct(&png_ptr, &info_ptr);
234 switch (bytesperpixel) {
236 color_type = PNG_COLOR_TYPE_RGBA;
239 if (channels_in_float == 4) {
240 for (i = ibuf->
x * ibuf->
y; i > 0; i--) {
242 to16[0] =
ftoshort(chanel_colormanage_cb(from_straight[0]));
243 to16[1] =
ftoshort(chanel_colormanage_cb(from_straight[1]));
244 to16[2] =
ftoshort(chanel_colormanage_cb(from_straight[2]));
245 to16[3] =
ftoshort(chanel_colormanage_cb(from_straight[3]));
250 else if (channels_in_float == 3) {
251 for (i = ibuf->
x * ibuf->
y; i > 0; i--) {
252 to16[0] =
ftoshort(chanel_colormanage_cb(from_float[0]));
253 to16[1] =
ftoshort(chanel_colormanage_cb(from_float[1]));
254 to16[2] =
ftoshort(chanel_colormanage_cb(from_float[2]));
261 for (i = ibuf->
x * ibuf->
y; i > 0; i--) {
262 to16[0] =
ftoshort(chanel_colormanage_cb(from_float[0]));
263 to16[2] = to16[1] = to16[0];
271 for (i = ibuf->
x * ibuf->
y; i > 0; i--) {
282 for (i = ibuf->
x * ibuf->
y; i > 0; i--) {
293 color_type = PNG_COLOR_TYPE_RGB;
296 if (channels_in_float == 4) {
297 for (i = ibuf->
x * ibuf->
y; i > 0; i--) {
299 to16[0] =
ftoshort(chanel_colormanage_cb(from_straight[0]));
300 to16[1] =
ftoshort(chanel_colormanage_cb(from_straight[1]));
301 to16[2] =
ftoshort(chanel_colormanage_cb(from_straight[2]));
306 else if (channels_in_float == 3) {
307 for (i = ibuf->
x * ibuf->
y; i > 0; i--) {
308 to16[0] =
ftoshort(chanel_colormanage_cb(from_float[0]));
309 to16[1] =
ftoshort(chanel_colormanage_cb(from_float[1]));
310 to16[2] =
ftoshort(chanel_colormanage_cb(from_float[2]));
316 for (i = ibuf->
x * ibuf->
y; i > 0; i--) {
317 to16[0] =
ftoshort(chanel_colormanage_cb(from_float[0]));
318 to16[2] = to16[1] = to16[0];
325 for (i = ibuf->
x * ibuf->
y; i > 0; i--) {
335 for (i = ibuf->
x * ibuf->
y; i > 0; i--) {
345 color_type = PNG_COLOR_TYPE_GRAY;
349 if (channels_in_float == 4) {
350 for (i = ibuf->
x * ibuf->
y; i > 0; i--) {
352 rgb[0] = chanel_colormanage_cb(from_straight[0]);
353 rgb[1] = chanel_colormanage_cb(from_straight[1]);
354 rgb[2] = chanel_colormanage_cb(from_straight[2]);
360 else if (channels_in_float == 3) {
361 for (i = ibuf->
x * ibuf->
y; i > 0; i--) {
362 rgb[0] = chanel_colormanage_cb(from_float[0]);
363 rgb[1] = chanel_colormanage_cb(from_float[1]);
364 rgb[2] = chanel_colormanage_cb(from_float[2]);
371 for (i = ibuf->
x * ibuf->
y; i > 0; i--) {
372 to16[0] =
ftoshort(chanel_colormanage_cb(from_float[0]));
379 for (i = ibuf->
x * ibuf->
y; i > 0; i--) {
387 for (i = ibuf->
x * ibuf->
y; i > 0; i--) {
406 png_destroy_write_struct(&png_ptr, &info_ptr);
414 printf(
"imb_savepng: Cannot open file for writing: '%s'\n", filepath);
417 png_init_io(png_ptr, fp);
421 png_set_filter(png_ptr,
423 PNG_FILTER_NONE | PNG_FILTER_VALUE_NONE | PNG_FILTER_SUB | PNG_FILTER_VALUE_SUB |
424 PNG_FILTER_UP | PNG_FILTER_VALUE_UP | PNG_FILTER_AVG | PNG_FILTER_VALUE_AVG |
425 PNG_FILTER_PAETH | PNG_FILTER_VALUE_PAETH | PNG_ALL_FILTERS);
428 png_set_compression_level(png_ptr, compression);
431 png_set_IHDR(png_ptr,
438 PNG_COMPRESSION_TYPE_DEFAULT,
439 PNG_FILTER_TYPE_DEFAULT);
454 metadata =
MEM_callocN(num_text *
sizeof(png_text),
"png_metadata");
458 metadata[num_text].compression = PNG_TEXT_COMPRESSION_NONE;
459 metadata[num_text].key = prop->
name;
465 png_set_text(png_ptr, info_ptr, metadata, num_text);
469 if (ibuf->
ppm[0] > 0.0 && ibuf->
ppm[1] > 0.0) {
470 png_set_pHYs(png_ptr,
472 (
unsigned int)(ibuf->
ppm[0] + 0.5),
473 (
unsigned int)(ibuf->
ppm[1] + 0.5),
474 PNG_RESOLUTION_METER);
478 png_write_info(png_ptr, info_ptr);
480 #ifdef __LITTLE_ENDIAN__
481 png_set_swap(png_ptr);
486 for (i = 0; i < ibuf->
y; i++) {
487 row_pointers[ibuf->
y - 1 - i] = (png_bytep)((
unsigned short *)pixels16 +
488 (((size_t)i) * ibuf->
x) * bytesperpixel);
492 for (i = 0; i < ibuf->
y; i++) {
493 row_pointers[ibuf->
y - 1 - i] = (png_bytep)((
unsigned char *)pixels +
494 (((size_t)i) * ibuf->
x) * bytesperpixel *
495 sizeof(
unsigned char));
500 png_write_image(png_ptr, row_pointers);
503 png_write_end(png_ptr, info_ptr);
513 png_destroy_write_struct(&png_ptr, &info_ptr);
532 fprintf(stderr,
"libpng warning: %s\n", message);
537 fprintf(stderr,
"libpng error: %s\n", message);
545 unsigned char *pixels =
NULL;
546 unsigned short *pixels16 =
NULL;
547 png_bytepp row_pointers =
NULL;
549 int bit_depth, color_type;
552 unsigned char *
from, *to;
553 unsigned short *from16;
564 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,
NULL,
NULL,
NULL);
565 if (png_ptr ==
NULL) {
566 printf(
"Cannot png_create_read_struct\n");
572 info_ptr = png_create_info_struct(png_ptr);
573 if (info_ptr ==
NULL) {
574 png_destroy_read_struct(&png_ptr, (png_infopp)
NULL, (png_infopp)
NULL);
575 printf(
"Cannot png_create_info_struct\n");
583 png_set_read_fn(png_ptr, (
void *)&ps,
ReadData);
585 if (setjmp(png_jmpbuf(png_ptr))) {
587 png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)
NULL);
605 png_read_info(png_ptr, info_ptr);
608 channels = png_get_channels(png_ptr, info_ptr);
610 switch (color_type) {
611 case PNG_COLOR_TYPE_RGB:
612 case PNG_COLOR_TYPE_RGB_ALPHA:
614 case PNG_COLOR_TYPE_PALETTE:
615 png_set_palette_to_rgb(png_ptr);
616 if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) {
623 case PNG_COLOR_TYPE_GRAY:
624 case PNG_COLOR_TYPE_GRAY_ALPHA:
626 png_set_expand(png_ptr);
628 if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) {
635 printf(
"PNG format not supported\n");
636 longjmp(png_jmpbuf(png_ptr), 1);
643 if (bit_depth == 16) {
647 if (png_get_valid(png_ptr, info_ptr, PNG_INFO_pHYs)) {
649 png_uint_32 xres, yres;
651 if (png_get_pHYs(png_ptr, info_ptr, &xres, &yres, &unit_type)) {
652 if (unit_type == PNG_RESOLUTION_METER) {
660 printf(
"Couldn't allocate memory for PNG image\n");
664 if (bit_depth == 16) {
666 png_set_swap(png_ptr);
670 printf(
"Cannot allocate pixels array\n");
671 longjmp(png_jmpbuf(png_ptr), 1);
675 row_pointers = (png_bytepp)
MEM_mallocN((
size_t)ibuf->
y *
sizeof(png_uint_16p),
677 if (row_pointers ==
NULL) {
678 printf(
"Cannot allocate row-pointers array\n");
679 longjmp(png_jmpbuf(png_ptr), 1);
683 for (
size_t i = 0; i < ibuf->
y; i++) {
684 row_pointers[ibuf->
y - 1 - i] = (png_bytep)((png_uint_16 *)pixels16 +
688 png_read_image(png_ptr, row_pointers);
697 for (
size_t i = (
size_t)ibuf->
x * (
size_t)ibuf->
y; i > 0; i--) {
698 to_float[0] = from16[0] / 65535.0;
699 to_float[1] = from16[1] / 65535.0;
700 to_float[2] = from16[2] / 65535.0;
701 to_float[3] = from16[3] / 65535.0;
707 for (
size_t i = (
size_t)ibuf->
x * (
size_t)ibuf->
y; i > 0; i--) {
708 to_float[0] = from16[0] / 65535.0;
709 to_float[1] = from16[1] / 65535.0;
710 to_float[2] = from16[2] / 65535.0;
717 for (
size_t i = (
size_t)ibuf->
x * (
size_t)ibuf->
y; i > 0; i--) {
718 to_float[0] = to_float[1] = to_float[2] = from16[0] / 65535.0;
719 to_float[3] = from16[1] / 65535.0;
725 for (
size_t i = (
size_t)ibuf->
x * (
size_t)ibuf->
y; i > 0; i--) {
726 to_float[0] = to_float[1] = to_float[2] = from16[0] / 65535.0;
739 printf(
"Cannot allocate pixels array\n");
740 longjmp(png_jmpbuf(png_ptr), 1);
744 row_pointers = (png_bytepp)
MEM_mallocN((
size_t)ibuf->
y *
sizeof(png_bytep),
"row_pointers");
745 if (row_pointers ==
NULL) {
746 printf(
"Cannot allocate row-pointers array\n");
747 longjmp(png_jmpbuf(png_ptr), 1);
751 for (
int i = 0; i < ibuf->
y; i++) {
752 row_pointers[ibuf->
y - 1 - i] = (png_bytep)(
753 (
unsigned char *)pixels + (((size_t)i) * ibuf->
x) *
channels *
sizeof(
unsigned char));
756 png_read_image(png_ptr, row_pointers);
760 to = (
unsigned char *)ibuf->
rect;
765 for (
size_t i = (
size_t)ibuf->
x * (size_t)ibuf->
y; i > 0; i--) {
775 for (
size_t i = (
size_t)ibuf->
x * (size_t)ibuf->
y; i > 0; i--) {
785 for (
size_t i = (
size_t)ibuf->
x * (size_t)ibuf->
y; i > 0; i--) {
786 to[0] = to[1] = to[2] =
from[0];
793 for (
size_t i = (
size_t)ibuf->
x * (size_t)ibuf->
y; i > 0; i--) {
794 to[0] = to[1] = to[2] =
from[0];
804 png_text *text_chunks;
805 int count = png_get_text(png_ptr, info_ptr, &text_chunks,
NULL);
807 for (
int i = 0; i <
count; i++) {
813 png_read_end(png_ptr, info_ptr);
826 png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)
NULL);
typedef float(TangentPoint)[2]
File and directory operations.
FILE * BLI_fopen(const char *filename, const char *mode) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
MINLINE void premul_to_straight_v4_v4(float straight[4], const float premul[4])
float linearrgb_to_srgb(float c)
ID and Library types, which are fundamental for sdna.
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint GLsizei width
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei height
Header file for allocimbuf.c.
BLI_INLINE float IMB_colormanagement_get_luminance(const float rgb[3])
@ COLOR_ROLE_DEFAULT_BYTE
struct ImBuf * IMB_allocImBuf(unsigned int x, unsigned int y, unsigned char planes, unsigned int flags)
void IMB_freeImBuf(struct ImBuf *ibuf)
void * imb_alloc_pixels(unsigned int x, unsigned int y, unsigned int channels, size_t typesize, const char *name)
bool imb_addrectfloatImBuf(struct ImBuf *ibuf)
bool imb_addrectImBuf(struct ImBuf *ibuf)
Contains defines and structs used throughout the imbuf module.
@ IMB_COLORMANAGE_IS_DATA
Read Guarded memory(de)allocation.
bool imb_enlargeencodedbufferImBuf(ImBuf *ibuf)
bool imb_addencodedbufferImBuf(ImBuf *ibuf)
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
SIMD_FORCE_INLINE btScalar length(const btQuaternion &q)
Return the length of a quaternion.
void colorspace_set_default_role(char *colorspace, int size, int role)
void(* MEM_freeN)(void *vmemh)
void *(* MEM_callocN)(size_t len, const char *str)
void *(* MEM_mallocN)(size_t len, const char *str)
MINLINE unsigned short unit_float_to_ushort_clamp(float val)
static void ReadData(png_structp png_ptr, png_bytep data, png_size_t length)
struct PNGReadStruct PNGReadStruct
static void imb_png_error(png_structp UNUSED(png_ptr), png_const_charp message)
bool imb_savepng(struct ImBuf *ibuf, const char *filepath, int flags)
ImBuf * imb_loadpng(const unsigned char *mem, size_t size, int flags, char colorspace[IM_MAX_SPACE])
static void imb_png_warning(png_structp UNUSED(png_ptr), png_const_charp message)
static float channel_colormanage_noop(float value)
BLI_INLINE unsigned short UPSAMPLE_8_TO_16(const unsigned char _val)
BLI_INLINE unsigned short ftoshort(float val)
static void WriteData(png_structp png_ptr, png_bytep data, png_size_t length)
bool imb_is_a_png(const unsigned char *mem, size_t size)
static void Flush(png_structp png_ptr)
struct IDProperty * metadata
ImbFormatOptions foptions
unsigned char * encodedbuffer
unsigned int encodedbuffersize
struct ColorSpace * float_colorspace
const unsigned char * data