32#include <glib/gstdio.h>
46 png->
f = g_fopen(filename,
"rb");
50#define NUM_BYTES_CHECK (8)
62 png->
png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
74 png_destroy_read_struct(&png->
png_ptr, NULL, NULL);
78 if(setjmp(png_jmpbuf(png->
png_ptr)))
99 if(png->
color_type == PNG_COLOR_TYPE_PALETTE) png_set_palette_to_rgb(png->
png_ptr);
104 png_set_expand_gray_1_2_4_to_8(png->
png_ptr);
113 png_set_gray_to_rgb(png->
png_ptr);
122#undef NUM_BYTES_CHECK
130 if(setjmp(png_jmpbuf(png->
png_ptr)))
137 png_bytep *row_pointers = malloc(
sizeof(png_bytep) * png->
height);
139 png_bytep row_pointer = (png_bytep)
out;
142 for(
int y = 0; y < png->
height; y++)
144 row_pointers[y] = row_pointer + (size_t)y * rowbytes;
147 png_read_image(png->
png_ptr, row_pointers);
161 const char *ext = filename + strlen(filename);
162 while(*ext !=
'.' && ext > filename) ext--;
180 img->
dsc.
bpp = 4 *
sizeof(float);
183 img->
flags &= ~DT_IMAGE_RAW;
184 img->
flags &= ~DT_IMAGE_S_RAW;
185 img->
flags &= ~DT_IMAGE_HDR;
201 fprintf(stderr,
"[png_open] could not alloc full buffer for image `%s'\n", img->
filename);
213 fprintf(stderr,
"[png_open] could not alloc intermediate buffer for image `%s'\n", img->
filename);
220 fprintf(stderr,
"[png_open] could not read image `%s'\n", img->
filename);
224 for(
size_t j = 0; j <
height; j++)
228 for(
int k = 0;
k < 3;
k++)
229 mipbuf[4 * (j *
width +
i) +
k] = buf[3 * (j *
width +
i) +
k] * (1.0f / 255.0f);
232 for(
int k = 0;
k < 3;
k++)
233 mipbuf[4 * (j *
width +
i) +
k] = (256.0f * buf[2 * (3 * (j *
width +
i) +
k)]
234 + buf[2 * (3 * (j *
width +
i) +
k) + 1]) * (1.0f / 65535.0f);
246 int compression_type;
249#if PNG_LIBPNG_VER >= 10500
255 if(!(filename && *filename &&
out))
return 0;
259#ifdef PNG_iCCP_SUPPORTED
261 && png_get_iCCP(image.
png_ptr, image.
info_ptr, &
name, &compression_type, &profile, &proflen) != 0)
263 *
out = (uint8_t *)g_malloc(proflen);
264 memcpy(*
out, profile, proflen);
typedef void((*dt_cache_allocate_t)(void *userdata, dt_cache_entry_t *entry))
const dt_colormatrix_t dt_aligned_pixel_t out
int dt_exif_read(dt_image_t *img, const char *path)
What a photograph says about itself: the EXIF, IPTC and XMP tags a camera and a cataloguer write,...
@ DT_IMAGEIO_FILE_CORRUPTED
int read_image(dt_imageio_png_t *png, void *out)
int read_header(const char *filename, dt_imageio_png_t *png)
int dt_imageio_png_read_profile(const char *filename, uint8_t **out)
dt_imageio_retval_t dt_imageio_open_png(dt_image_t *img, const char *filename, dt_mipmap_buffer_t *mbuf)
float *const restrict const size_t k
#define IS_NULL_PTR(p)
C is way too permissive with !=, == and if(var) checks, which can mean too many things depending on w...
#define dt_free(ptr)
g_free() ptr and set it to NULL, skipping both if it is already NULL.
void * dt_mipmap_cache_alloc(dt_mipmap_buffer_t *buf, const dt_image_t *img)
#define dt_pixelpipe_cache_alloc_align_cache(size, id)
#define dt_pixelpipe_cache_free_align(mem)
char filename[DT_MAX_FILENAME_LEN]
dt_iop_buffer_type_t datatype