41 static void Flush(png_structp png_ptr);
45 return (_val << 8) + _val;
50 const int num_to_check = 8;
51 if (
size < num_to_check) {
56 #if (PNG_LIBPNG_VER_MAJOR == 1) && (PNG_LIBPNG_VER_MINOR == 2)
58 ok = !png_sig_cmp((png_bytep)mem, 0, num_to_check);
60 ok = !png_sig_cmp(mem, 0, num_to_check);
65 static void Flush(png_structp png_ptr)
72 ImBuf *ibuf = (
ImBuf *)png_get_io_ptr(png_ptr);
95 printf(
"Reached EOF while decoding PNG\n");
96 longjmp(png_jmpbuf(png_ptr), 1);
115 unsigned char *pixels =
NULL;
116 unsigned char *
from, *to;
117 unsigned short *pixels16 =
NULL, *to16;
119 png_bytepp row_pointers =
NULL;
120 int i, bytesperpixel, color_type = PNG_COLOR_TYPE_GRAY;
133 compression = compression < 0 ? 0 : (compression > 9 ? 9 : compression);
146 filepath =
"<memory>";
149 bytesperpixel = (ibuf->
planes + 7) >> 3;
150 if ((bytesperpixel > 4) || (bytesperpixel == 2)) {
152 "imb_savepng: Unsupported bytes per pixel: %d for file: '%s'\n", bytesperpixel, filepath);
156 png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,
NULL,
NULL,
NULL);
157 if (png_ptr ==
NULL) {
158 printf(
"imb_savepng: Cannot png_create_write_struct for file: '%s'\n", filepath);
162 info_ptr = png_create_info_struct(png_ptr);
163 if (info_ptr ==
NULL) {
164 png_destroy_write_struct(&png_ptr, (png_infopp)
NULL);
165 printf(
"imb_savepng: Cannot png_create_info_struct for file: '%s'\n", filepath);
170 num_bytes = ((size_t)ibuf->
x) * ibuf->
y * bytesperpixel;
172 pixels16 =
MEM_mallocN(num_bytes *
sizeof(
unsigned short),
"png 16bit pixels");
175 pixels =
MEM_mallocN(num_bytes *
sizeof(
unsigned char),
"png 8bit pixels");
177 if (pixels ==
NULL && pixels16 ==
NULL) {
179 "imb_savepng: Cannot allocate pixels array of %dx%d, %d bytes per pixel for file: "
188 row_pointers = (png_bytepp)
MEM_mallocN(ibuf->
y *
sizeof(png_bytep),
"row_pointers");
189 if (row_pointers ==
NULL) {
190 printf(
"imb_savepng: Cannot allocate row-pointers array for file '%s'\n", filepath);
193 if ((pixels ==
NULL && pixels16 ==
NULL) || (row_pointers ==
NULL) ||
194 setjmp(png_jmpbuf(png_ptr))) {
196 png_destroy_write_struct(&png_ptr, &info_ptr);
218 switch (bytesperpixel) {
220 color_type = PNG_COLOR_TYPE_RGBA;
223 if (channels_in_float == 4) {
224 for (i = ibuf->
x * ibuf->
y; i > 0; i--) {
226 to16[0] =
ftoshort(chanel_colormanage_cb(from_straight[0]));
227 to16[1] =
ftoshort(chanel_colormanage_cb(from_straight[1]));
228 to16[2] =
ftoshort(chanel_colormanage_cb(from_straight[2]));
229 to16[3] =
ftoshort(chanel_colormanage_cb(from_straight[3]));
234 else if (channels_in_float == 3) {
235 for (i = ibuf->
x * ibuf->
y; i > 0; i--) {
245 for (i = ibuf->
x * ibuf->
y; i > 0; i--) {
247 to16[2] = to16[1] = to16[0];
255 for (i = ibuf->
x * ibuf->
y; i > 0; i--) {
266 for (i = ibuf->
x * ibuf->
y; i > 0; i--) {
277 color_type = PNG_COLOR_TYPE_RGB;
280 if (channels_in_float == 4) {
281 for (i = ibuf->
x * ibuf->
y; i > 0; i--) {
283 to16[0] =
ftoshort(chanel_colormanage_cb(from_straight[0]));
284 to16[1] =
ftoshort(chanel_colormanage_cb(from_straight[1]));
285 to16[2] =
ftoshort(chanel_colormanage_cb(from_straight[2]));
290 else if (channels_in_float == 3) {
291 for (i = ibuf->
x * ibuf->
y; i > 0; i--) {
300 for (i = ibuf->
x * ibuf->
y; i > 0; i--) {
302 to16[2] = to16[1] = to16[0];
309 for (i = ibuf->
x * ibuf->
y; i > 0; i--) {
319 for (i = ibuf->
x * ibuf->
y; i > 0; i--) {
329 color_type = PNG_COLOR_TYPE_GRAY;
333 if (channels_in_float == 4) {
334 for (i = ibuf->
x * ibuf->
y; i > 0; i--) {
336 rgb[0] = chanel_colormanage_cb(from_straight[0]);
337 rgb[1] = chanel_colormanage_cb(from_straight[1]);
338 rgb[2] = chanel_colormanage_cb(from_straight[2]);
344 else if (channels_in_float == 3) {
345 for (i = ibuf->
x * ibuf->
y; i > 0; i--) {
355 for (i = ibuf->
x * ibuf->
y; i > 0; i--) {
363 for (i = ibuf->
x * ibuf->
y; i > 0; i--) {
371 for (i = ibuf->
x * ibuf->
y; i > 0; i--) {
390 png_destroy_write_struct(&png_ptr, &info_ptr);
398 printf(
"imb_savepng: Cannot open file for writing: '%s'\n", filepath);
401 png_init_io(png_ptr, fp);
405 png_set_filter(png_ptr,
407 PNG_FILTER_NONE | PNG_FILTER_VALUE_NONE | PNG_FILTER_SUB | PNG_FILTER_VALUE_SUB |
408 PNG_FILTER_UP | PNG_FILTER_VALUE_UP | PNG_FILTER_AVG | PNG_FILTER_VALUE_AVG |
409 PNG_FILTER_PAETH | PNG_FILTER_VALUE_PAETH | PNG_ALL_FILTERS);
412 png_set_compression_level(png_ptr, compression);
415 png_set_IHDR(png_ptr,
422 PNG_COMPRESSION_TYPE_DEFAULT,
423 PNG_FILTER_TYPE_DEFAULT);
438 metadata =
MEM_callocN(num_text *
sizeof(png_text),
"png_metadata");
442 metadata[num_text].compression = PNG_TEXT_COMPRESSION_NONE;
443 metadata[num_text].key = prop->
name;
449 png_set_text(png_ptr, info_ptr, metadata, num_text);
453 if (ibuf->
ppm[0] > 0.0 && ibuf->
ppm[1] > 0.0) {
454 png_set_pHYs(png_ptr,
456 (
unsigned int)(ibuf->
ppm[0] + 0.5),
457 (
unsigned int)(ibuf->
ppm[1] + 0.5),
458 PNG_RESOLUTION_METER);
462 png_write_info(png_ptr, info_ptr);
464 #ifdef __LITTLE_ENDIAN__
465 png_set_swap(png_ptr);
470 for (i = 0; i < ibuf->
y; i++) {
471 row_pointers[ibuf->
y - 1 - i] = (png_bytep)((
unsigned short *)pixels16 +
472 (((size_t)i) * ibuf->
x) * bytesperpixel);
476 for (i = 0; i < ibuf->
y; i++) {
477 row_pointers[ibuf->
y - 1 - i] = (png_bytep)((
unsigned char *)pixels +
478 (((size_t)i) * ibuf->
x) * bytesperpixel *
479 sizeof(
unsigned char));
484 png_write_image(png_ptr, row_pointers);
487 png_write_end(png_ptr, info_ptr);
497 png_destroy_write_struct(&png_ptr, &info_ptr);
516 fprintf(stderr,
"libpng warning: %s\n", message);
521 fprintf(stderr,
"libpng error: %s\n", message);
529 unsigned char *pixels =
NULL;
530 unsigned short *pixels16 =
NULL;
531 png_bytepp row_pointers =
NULL;
533 int bit_depth, color_type;
536 unsigned char *
from, *to;
537 unsigned short *from16;
548 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,
NULL,
NULL,
NULL);
549 if (png_ptr ==
NULL) {
550 printf(
"Cannot png_create_read_struct\n");
556 info_ptr = png_create_info_struct(png_ptr);
557 if (info_ptr ==
NULL) {
558 png_destroy_read_struct(&png_ptr, (png_infopp)
NULL, (png_infopp)
NULL);
559 printf(
"Cannot png_create_info_struct\n");
567 png_set_read_fn(png_ptr, (
void *)&ps,
ReadData);
569 if (setjmp(png_jmpbuf(png_ptr))) {
571 png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)
NULL);
589 png_read_info(png_ptr, info_ptr);
592 channels = png_get_channels(png_ptr, info_ptr);
594 switch (color_type) {
595 case PNG_COLOR_TYPE_RGB:
596 case PNG_COLOR_TYPE_RGB_ALPHA:
598 case PNG_COLOR_TYPE_PALETTE:
599 png_set_palette_to_rgb(png_ptr);
600 if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) {
607 case PNG_COLOR_TYPE_GRAY:
608 case PNG_COLOR_TYPE_GRAY_ALPHA:
610 png_set_expand(png_ptr);
612 if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) {
619 printf(
"PNG format not supported\n");
620 longjmp(png_jmpbuf(png_ptr), 1);
627 if (bit_depth == 16) {
631 if (png_get_valid(png_ptr, info_ptr, PNG_INFO_pHYs)) {
633 png_uint_32 xres, yres;
635 if (png_get_pHYs(png_ptr, info_ptr, &xres, &yres, &unit_type)) {
636 if (unit_type == PNG_RESOLUTION_METER) {
644 printf(
"Couldn't allocate memory for PNG image\n");
648 if (bit_depth == 16) {
650 png_set_swap(png_ptr);
654 printf(
"Cannot allocate pixels array\n");
655 longjmp(png_jmpbuf(png_ptr), 1);
659 row_pointers = (png_bytepp)
MEM_mallocN((
size_t)ibuf->
y *
sizeof(png_uint_16p),
661 if (row_pointers ==
NULL) {
662 printf(
"Cannot allocate row-pointers array\n");
663 longjmp(png_jmpbuf(png_ptr), 1);
667 for (
size_t i = 0; i < ibuf->
y; i++) {
668 row_pointers[ibuf->
y - 1 - i] = (png_bytep)((png_uint_16 *)pixels16 +
672 png_read_image(png_ptr, row_pointers);
681 for (
size_t i = (
size_t)ibuf->
x * (
size_t)ibuf->
y; i > 0; i--) {
691 for (
size_t i = (
size_t)ibuf->
x * (
size_t)ibuf->
y; i > 0; i--) {
701 for (
size_t i = (
size_t)ibuf->
x * (
size_t)ibuf->
y; i > 0; i--) {
709 for (
size_t i = (
size_t)ibuf->
x * (
size_t)ibuf->
y; i > 0; i--) {
723 printf(
"Cannot allocate pixels array\n");
724 longjmp(png_jmpbuf(png_ptr), 1);
728 row_pointers = (png_bytepp)
MEM_mallocN((
size_t)ibuf->
y *
sizeof(png_bytep),
"row_pointers");
729 if (row_pointers ==
NULL) {
730 printf(
"Cannot allocate row-pointers array\n");
731 longjmp(png_jmpbuf(png_ptr), 1);
735 for (
int i = 0; i < ibuf->
y; i++) {
736 row_pointers[ibuf->
y - 1 - i] = (png_bytep)((
unsigned char *)pixels +
738 sizeof(
unsigned char));
741 png_read_image(png_ptr, row_pointers);
745 to = (
unsigned char *)ibuf->
rect;
750 for (
size_t i = (
size_t)ibuf->
x * (size_t)ibuf->
y; i > 0; i--) {
760 for (
size_t i = (
size_t)ibuf->
x * (size_t)ibuf->
y; i > 0; i--) {
770 for (
size_t i = (
size_t)ibuf->
x * (size_t)ibuf->
y; i > 0; i--) {
771 to[0] = to[1] = to[2] =
from[0];
778 for (
size_t i = (
size_t)ibuf->
x * (size_t)ibuf->
y; i > 0; i--) {
779 to[0] = to[1] = to[2] =
from[0];
789 png_text *text_chunks;
790 int count = png_get_text(png_ptr, info_ptr, &text_chunks,
NULL);
792 for (
int i = 0; i <
count; i++) {
798 png_read_end(png_ptr, info_ptr);
811 png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)
NULL);
typedef float(TangentPoint)[2]
File and directory operations.
FILE * BLI_fopen(const char *filepath, 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 height
_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
Header file for allocimbuf.c.
@ COLOR_ROLE_DEFAULT_BYTE
BLI_INLINE float IMB_colormanagement_get_luminance(const float rgb[3])
struct ImBuf * IMB_allocImBuf(unsigned int x, unsigned int y, unsigned char planes, unsigned int flags)
bool imb_addrectfloatImBuf(struct ImBuf *ibuf, const unsigned int channels)
void * imb_alloc_pixels(unsigned int x, unsigned int y, unsigned int channels, size_t typesize, const char *name)
bool imb_addrectImBuf(struct ImBuf *ibuf)
Contains defines and structs used throughout the imbuf module.
@ IMB_COLORMANAGE_IS_DATA
Read Guarded memory(de)allocation.
Group Output data from inside of a node group A color picker Mix two input colors RGB to Convert a color s luminance to a grayscale value Generate a normal vector and a dot product Bright Control the brightness and contrast of the input color Vector Map an input vectors to used to fine tune the interpolation of the input Camera Retrieve information about the camera and how it relates to the current shading point s position Clamp a value between a minimum and a maximum Vector Perform vector math operation Invert a producing a negative Combine Generate a color from its and blue channels(Deprecated)") DefNode(ShaderNode
bool imb_enlargeencodedbufferImBuf(ImBuf *ibuf)
bool imb_addencodedbufferImBuf(ImBuf *ibuf)
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
void colorspace_set_default_role(char *colorspace, int size, int role)
SyclQueue void void size_t num_bytes void
void IMB_freeImBuf(ImBuf *UNUSED(ibuf))
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)
void to_float(const T &src, float dst[4])
void from_float(const float src[4], T &dst)
T length(const vec_base< T, Size > &a)
static const pxr::TfToken rgb("rgb", pxr::TfToken::Immortal)
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