42 pixels = malloc(
sizeof(
void *) * rows);
44 fprintf(stderr,
"ERROR > allocing pixel buffer rows.\n");
49 for (i = 0; i < rows; i++) {
50 pixels[i] = malloc(
sizeof(
pixel) * cols);
52 fprintf(stderr,
"ERROR > allocing pixel buffer cols.\n");
69 unsigned char** bytes;
72 bytes = malloc(
sizeof(
void *) * rows);
74 fprintf(stderr,
"ERROR > allocing raw buffer rows.\n");
79 for (i = 0; i < rows; i++) {
80 bytes[i] = malloc(
sizeof(
unsigned char) * cols);
82 fprintf(stderr,
"ERROR > allocing raw buffer cols.\n");
102 for (i = 0; i < rows; i++) {
107 fprintf(stderr,
"ERROR > freeing pixel buffer cols.\n");
117 fprintf(stderr,
"ERROR > freeing pixel buffer rows.\n");
136 for (i = 0; i < rows; i++) {
141 fprintf(stderr,
"ERROR > freeing raw buffer cols.\n");
151 fprintf(stderr,
"ERROR > freeing raw buffer rows.\n");
180 unsigned char** bytes_src,
pixel** pixels_dest,
181 unsigned rows,
unsigned cols
185 for (r = 0; r < rows; r++) {
186 for (c = 0; c < cols; c++) {
187 p.
r = bytes_src[r][c * 4];
188 p.
g = bytes_src[r][(c * 4) + 1];
189 p.
b = bytes_src[r][(c * 4) + 2];
190 p.
a = bytes_src[r][(c * 4) + 3];
191 pixels_dest[r][c] = p;
207 pixel** pixels_src,
unsigned char** bytes_dest,
208 unsigned rows,
unsigned cols
211 for (r = 0; r < rows; r++) {
212 for (c = 0; c < cols; c++) {
213 bytes_dest[r][c * 4] = pixels_src[r][c].
r;
214 bytes_dest[r][(c * 4) + 1] = pixels_src[r][c].g;
215 bytes_dest[r][(c * 4) + 2] = pixels_src[r][c].b;
216 bytes_dest[r][(c * 4) + 3] = pixels_src[r][c].a;
242 unsigned width, height;
243 unsigned char bit_depth, color_type;
244 unsigned char** byte_ptrs;
250 fprintf(stderr,
"pngz loading png at '%s'\n", z->
path);
252 if(!(fp = fopen(z->
path,
"rb"))) {
253 fprintf(stderr,
"ERROR > opening file.\n");
257 if (!(png = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL))) {
258 fprintf(stderr,
"ERROR > creating read struct.\n");
262 if (!(info = png_create_info_struct(png))) {
263 fprintf(stderr,
"ERROR > creating png info.\n");
267 if (setjmp(png_jmpbuf(png))) {
268 fprintf(stderr,
"ERROR > io failed.\n");
273 png_init_io(png, fp);
274 png_read_info(png, info);
275 width = png_get_image_width(png, info);
276 height = png_get_image_height(png, info);
277 bit_depth = png_get_bit_depth(png, info);
278 color_type = png_get_color_type(png, info);
280 if (color_type == PNG_COLOR_TYPE_PALETTE) {
281 png_set_palette_to_rgb(png);
283 if (bit_depth == 16) {
284 png_set_strip_16(png);
287 if (color_type == PNG_COLOR_TYPE_GRAY) {
288 png_set_expand_gray_1_2_4_to_8(png);
293 if (png_get_valid(png, info, PNG_INFO_tRNS)){
294 png_set_tRNS_to_alpha(png);
297 color_type != PNG_COLOR_TYPE_RGBA ||
298 color_type != PNG_COLOR_TYPE_GRAY_ALPHA
300 png_set_filler(png, 0xFF, PNG_FILLER_AFTER);
303 color_type != PNG_COLOR_TYPE_GRAY ||
304 color_type != PNG_COLOR_TYPE_GRAY_ALPHA
306 png_set_gray_to_rgb(png);
308 png_read_update_info(png, info);
313 png_read_image(png, byte_ptrs);
317 png_destroy_read_struct(&png, &info, NULL);
344 unsigned char** bytes;
349 if (!(fp = fopen(path,
"wb"))) {
350 fprintf(stderr,
"ERROR > opening file.\n");
354 if (!(png = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL))) {
355 fprintf(stderr,
"ERROR > creating write struct.\n");
359 if (!(info = png_create_info_struct(png))) {
360 fprintf(stderr,
"ERROR > creating info struct.\n");
364 if (setjmp(png_jmpbuf(png))) {
365 fprintf(stderr,
"ERROR > io failed.\n");
370 png_init_io(png, fp);
378 PNG_COMPRESSION_TYPE_DEFAULT,
379 PNG_FILTER_TYPE_DEFAULT
381 png_write_info(png, info);
384 png_write_image(png, bytes);
385 png_write_end(png, NULL);
388 png_destroy_write_struct(&png, &info);
403 printf(
" rows x cols : %d x %d\n", z.
height, z.
width);
405 printf(
" pixels : loaded\n");
407 printf(
" pixels : empty\n");
422 "PIXEL [ #%02X%02X%02X%02X ]\n",
431 for (i = 0; i < indent; i++) {
448 printf(
" row x col : %d x %d\n", z.
height, z.
width);
451 printf(
" pixels : loaded\n");
453 printf(
" pixels : empty\n");
471 "PIXEL [ #%02X%02X%02X%02X ]\n",
void print_indent(int indent)
int pngz_unpack_pixels(pixel **pixels_src, unsigned char **bytes_dest, unsigned rows, unsigned cols)
unpack pixels into raw byte ptrs.
int pngz_load(pngz *z)
load a pngz object into memory
int pngz_free_bytes(unsigned char **bytes, unsigned rows)
safely free pixel buffer.
unsigned char ** pngz_alloc_bytes(unsigned rows, unsigned cols)
allocate raw pixel byte buffer.
int pngz_load_from(pngz *z, char *path)
load a pngz from a path directly passed into the call, just a wrapper for standard pngz_load()
void pngz_print_pixel_indent(pixel p, int indent)
print out a single pixels rgba values.
int pngz_save(pngz z)
write a png back out to file, wrapper for pngz_save_as()
void pngz_print_indent(pngz z, int indent)
print a pngz ztruct contents.
int pngz_free(pngz *z)
free a pngz structs pixels.
void pngz_print_pixel(pixel p)
print out a single pixels rgba values in hex.
int pngz_free_pixels(pixel **pixels, unsigned rows)
safely free pixel buffer.
pixel ** pngz_alloc_pixels(unsigned rows, unsigned cols)
allocate a pixel buffer.
int pngz_pack_pixels(unsigned char **bytes_src, pixel **pixels_dest, unsigned rows, unsigned cols)
pack bytes into pixels.
void pngz_print(pngz z)
print a pngz ztruct contents.
int pngz_save_as(pngz z, char *path)
write a png back out to file with a new name
unsigned char r
channel values
char * path
default path to load from and save to
unsigned height
png height in pixels (Y domain)
pixel ** pixels
[height][width] pixel buffer (Y, X)
unsigned width
png width in pixels (X domain)