40 if ( ! (*pixels_ptr = malloc(
sizeof(
void *) * rows))) {
41 fprintf(stderr,
"ERROR > allocing pixel buffer rows.\n");
46 for (i = 0; i < rows; i++) {
47 if ( ! ((*pixels_ptr)[i] = malloc(
sizeof(
PNGZ_Pixel) * cols))) {
48 fprintf(stderr,
"ERROR > allocing pixel buffer cols.\n");
53 free ((*pixels_ptr)[i]);
68int PNGZ_AllocBytes(uint8_t*** bytes_ptr,
const unsigned rows,
const unsigned cols) {
72 if ( ! (*bytes_ptr = malloc(
sizeof(
void *) * rows))) {
73 fprintf(stderr,
"ERROR > allocing byte buffer rows.\n");
78 for (i = 0; i < rows; i++) {
79 if ( ! ((*bytes_ptr)[i] = malloc(
sizeof(uint8_t) * cols))) {
80 fprintf(stderr,
"ERROR > allocing byte buffer cols.\n");
85 free ((*bytes_ptr)[i]);
105 for (i = 0; i < rows; i++) {
110 fprintf(stderr,
"ERROR > freeing pixel buffer cols.\n");
121 fprintf(stderr,
"ERROR > freeing pixel buffer rows.\n");
140 for (i = 0; i < rows; i++) {
145 fprintf(stderr,
"ERROR > freeing raw buffer cols.\n");
155 fprintf(stderr,
"ERROR > freeing raw buffer rows.\n");
184 uint8_t** bytes_src,
PNGZ_Pixel** pixels_dest,
185 const unsigned rows,
const unsigned cols
191 fprintf(stderr,
"ERROR > unpacking pixels from null source.\n");
196 fprintf(stderr,
"ERROR > unpacking pixels to null destination.\n");
200 for (r = 0; r < rows; r++)
201 for (c = 0; c < cols; c++) {
202 p.
r = bytes_src[r][c * 4];
203 p.
g = bytes_src[r][(c * 4) + 1];
204 p.
b = bytes_src[r][(c * 4) + 2];
205 p.
a = bytes_src[r][(c * 4) + 3];
206 pixels_dest[r][c] = p;
224 PNGZ_Pixel** pixels_src, uint8_t** bytes_dest,
225 const unsigned rows,
const unsigned cols
230 fprintf(stderr,
"ERROR > unpacking pixels from null source.\n");
235 fprintf(stderr,
"ERROR > unpacking pixels to null destination.\n");
239 for (r = 0; r < rows; r++) {
240 for (c = 0; c < cols; c++) {
241 bytes_dest[r][c * 4] = pixels_src[r][c].
r;
242 bytes_dest[r][(c * 4) + 1] = pixels_src[r][c].g;
243 bytes_dest[r][(c * 4) + 2] = pixels_src[r][c].b;
244 bytes_dest[r][(c * 4) + 3] = pixels_src[r][c].a;
261 fprintf(stderr,
"ERROR > copying from null source.\n");
265 if (!z_dest || !(z_dest->
pixels)) {
266 fprintf(stderr,
"ERROR > copying to null destination.\n");
270 for (r = 0; r < z_src.
height && r < z_dest->
height; r++) {
271 for (c = 0; c < z_src.
width && c < z_dest->
width; c++) {
298 unsigned width, height;
299 uint8_t bit_depth, color_type;
306 fprintf(stderr,
"pngz loading png at '%s'\n", z->
path);
308 if( ! (fp = fopen(z->
path,
"rb"))) {
309 fprintf(stderr,
"ERROR > opening file.\n");
313 if ( ! (png = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL))) {
314 fprintf(stderr,
"ERROR > creating read struct.\n");
318 if ( ! (info = png_create_info_struct(png))) {
319 fprintf(stderr,
"ERROR > creating png info.\n");
323 if (setjmp(png_jmpbuf(png))) {
324 fprintf(stderr,
"ERROR > io failed.\n");
329 png_init_io(png, fp);
330 png_read_info(png, info);
331 width = png_get_image_width(png, info);
332 height = png_get_image_height(png, info);
333 bit_depth = png_get_bit_depth(png, info);
334 color_type = png_get_color_type(png, info);
336 if (color_type == PNG_COLOR_TYPE_PALETTE) {
337 png_set_palette_to_rgb(png);
339 if (bit_depth == 16) {
340 png_set_strip_16(png);
341 }
else if (bit_depth < 8) {
342 if (color_type == PNG_COLOR_TYPE_GRAY) {
343 png_set_expand_gray_1_2_4_to_8(png);
348 if (png_get_valid(png, info, PNG_INFO_tRNS)){
349 png_set_tRNS_to_alpha(png);
352 color_type != PNG_COLOR_TYPE_RGBA ||
353 color_type != PNG_COLOR_TYPE_GRAY_ALPHA
355 png_set_filler(png, 0xFF, PNG_FILLER_AFTER);
358 color_type != PNG_COLOR_TYPE_GRAY ||
359 color_type != PNG_COLOR_TYPE_GRAY_ALPHA
361 png_set_gray_to_rgb(png);
363 png_read_update_info(png, info);
368 png_read_image(png, byte_ptrs);
372 png_destroy_read_struct(&png, &info, NULL);
404 if ( ! (fp = fopen(path,
"wb"))) {
405 fprintf(stderr,
"ERROR > opening file.\n");
409 if ( ! (png = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL))) {
410 fprintf(stderr,
"ERROR > creating write struct.\n");
414 if ( ! (info = png_create_info_struct(png))) {
415 fprintf(stderr,
"ERROR > creating info struct.\n");
419 if (setjmp(png_jmpbuf(png))) {
420 fprintf(stderr,
"ERROR > io failed.\n");
425 png_init_io(png, fp);
433 PNG_COMPRESSION_TYPE_DEFAULT,
434 PNG_FILTER_TYPE_DEFAULT
436 png_write_info(png, info);
439 png_write_image(png, bytes);
440 png_write_end(png, NULL);
443 png_destroy_write_struct(&png, &info);
458 printf(
" rows x cols : %d x %d\n", z.
height, z.
width);
460 printf(
" pixels : loaded\n");
462 printf(
" pixels : empty\n");
477 "PIXEL [ #%02X%02X%02X%02X ]\n",
484static void print_indent(
const int indent) {
486 for (i = 0; i < indent; i++) {
500 print_indent(indent);
502 print_indent(indent);
503 printf(
" row x col : %d x %d\n", z.
height, z.
width);
504 print_indent(indent);
506 printf(
" pixels : loaded\n");
508 printf(
" pixels : empty\n");
510 print_indent(indent);
524 print_indent(indent);
526 "PIXEL [ #%02X%02X%02X%02X ]\n",
int PNGZ_PrintPIxelIndent(const PNGZ_Pixel p, const int indent)
print out a single pixels rgba values.
int PNGZ_Load(PNGZ_Image *z)
load a pngz object into memory
int PNGZ_AllocPixels(PNGZ_Pixel ***pixels_ptr, const unsigned rows, const unsigned cols)
allocate a pixel buffer.
int PNGZ_BytesToPixels(uint8_t **bytes_src, PNGZ_Pixel **pixels_dest, const unsigned rows, const unsigned cols)
pack bytes into pixels.
int PNGZ_PrintImage(const PNGZ_Image z)
print a pngz ztruct contents.
int PNGZ_FreePixels(PNGZ_Pixel **pixels, const unsigned rows)
safely free pixel buffer.
int PNGZ_PrintPixel(const PNGZ_Pixel p)
print out a single pixels rgba values in hex.
int PNGZ_Copy(const PNGZ_Image z_src, PNGZ_Image *z_dest)
copies the pixel data from one pngz to another.
int PNGZ_AllocBytes(uint8_t ***bytes_ptr, const unsigned rows, const unsigned cols)
allocate raw pixel byte buffer.
int PNGZ_PixelsToBytes(PNGZ_Pixel **pixels_src, uint8_t **bytes_dest, const unsigned rows, const unsigned cols)
unpack pixels into raw byte ptrs.
int PNGZ_LoadFrom(PNGZ_Image *z, const char *path)
load a pngz from a path directly passed into the call, just a wrapper for standard pngz_load()
int PNGZ_PrintImageIndent(const PNGZ_Image z, const int indent)
print a pngz ztruct contents.
int PNGZ_Save(const PNGZ_Image z)
write a png back out to file, wrapper for pngz_save_as()
int PNGZ_Free(PNGZ_Image *z)
free a pngz structs pixels.
int PNGZ_SaveAs(const PNGZ_Image z, const char *path)
write a png back out to file with a new name
int PNGZ_FreeBytes(uint8_t **bytes, const unsigned rows)
safely free pixel buffer.
easy png based image structure.
PNGZ_Pixel ** pixels
[height][width] pixel buffer (Y, X)
unsigned height
png height in pixels (Y domain)
const char * path
default path to load from and save to
unsigned width
png width in pixels (X domain)