41 pixels = malloc(
sizeof(
void *) * rows);
43 fprintf(stderr,
"ERROR > allocing pixel buffer rows.\n");
48 for (
unsigned i = 0; i < rows; i++) {
49 pixels[i] = malloc(
sizeof(
pixel) * cols);
51 fprintf(stderr,
"ERROR > allocing pixel buffer cols.\n");
69 unsigned char** bytes = malloc(
sizeof(
void *) * rows);
71 fprintf(stderr,
"ERROR > allocing raw buffer rows.\n");
75 for (
unsigned i = 0; i < rows; i++) {
76 bytes[i] = malloc(
sizeof(
unsigned char) * cols);
78 fprintf(stderr,
"ERROR > allocing raw buffer cols.\n");
96 for (
unsigned i = 0; i < rows; i++) {
101 fprintf(stderr,
"ERROR > freeing pixel buffer cols.\n");
110 fprintf(stderr,
"ERROR > freeing pixel buffer rows.\n");
128 for (
unsigned i = 0; i < rows; i++) {
133 fprintf(stderr,
"ERROR > freeing raw buffer cols.\n");
142 fprintf(stderr,
"ERROR > freeing raw buffer rows.\n");
171 unsigned char** bytes_src,
pixel** pixels_dest,
172 unsigned rows,
unsigned cols
174 for (
unsigned r = 0; r < rows; r++) {
175 for (
unsigned c = 0; c < cols; c++) {
177 .
r = bytes_src[r][c * 4],
178 .g = bytes_src[r][(c * 4) + 1],
179 .b = bytes_src[r][(c * 4) + 2],
180 .a = bytes_src[r][(c * 4) + 3]
182 pixels_dest[r][c] = p;
198 pixel** pixels_src,
unsigned char** bytes_dest,
199 unsigned rows,
unsigned cols
201 for (
unsigned r = 0; r < rows; r++) {
202 for (
unsigned c = 0; c < cols; c++) {
203 bytes_dest[r][c * 4] = pixels_src[r][c].
r;
204 bytes_dest[r][(c * 4) + 1] = pixels_src[r][c].g;
205 bytes_dest[r][(c * 4) + 2] = pixels_src[r][c].b;
206 bytes_dest[r][(c * 4) + 3] = pixels_src[r][c].a;
238 fprintf(stderr,
"pngz loading png at '%s'\n", z->
path);
243 if(!(fp = fopen(z->
path,
"rb"))) {
244 fprintf(stderr,
"ERROR > opening file.\n");
249 if (!(png = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL))) {
250 fprintf(stderr,
"ERROR > creating read struct.\n");
255 if (!(info = png_create_info_struct(png))) {
256 fprintf(stderr,
"ERROR > creating png info.\n");
260 if (setjmp(png_jmpbuf(png))) {
261 fprintf(stderr,
"ERROR > io failed.\n");
267 png_init_io(png, fp);
268 png_read_info(png, info);
269 unsigned width = png_get_image_width(png, info);
270 unsigned height = png_get_image_height(png, info);
271 unsigned char bit_depth = png_get_bit_depth(png, info);
272 unsigned char color_type = png_get_color_type(png, info);
275 if (color_type == PNG_COLOR_TYPE_PALETTE) {
276 png_set_palette_to_rgb(png);
278 if (bit_depth == 16) {
279 png_set_strip_16(png);
282 if (color_type == PNG_COLOR_TYPE_GRAY) {
283 png_set_expand_gray_1_2_4_to_8(png);
288 if (png_get_valid(png, info, PNG_INFO_tRNS)){
289 png_set_tRNS_to_alpha(png);
292 color_type != PNG_COLOR_TYPE_RGBA ||
293 color_type != PNG_COLOR_TYPE_GRAY_ALPHA
295 png_set_filler(png, 0xFF, PNG_FILLER_AFTER);
298 color_type != PNG_COLOR_TYPE_GRAY ||
299 color_type != PNG_COLOR_TYPE_GRAY_ALPHA
301 png_set_gray_to_rgb(png);
303 png_read_update_info(png, info);
309 png_read_image(png, byte_ptrs);
313 png_destroy_read_struct(&png, &info, NULL);
348 if (!(fp = fopen(path,
"wb"))) {
349 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");
371 png_init_io(png, fp);
379 PNG_COMPRESSION_TYPE_DEFAULT,
380 PNG_FILTER_TYPE_DEFAULT
382 png_write_info(png, info);
385 png_write_image(png, bytes);
386 png_write_end(png, NULL);
390 png_destroy_write_struct(&png, &info);
405 printf(
" rows x cols : %d x %d\n", z.
rows, z.
cols);
407 printf(
" pixels : loaded\n");
409 printf(
" pixels : empty\n");
424 "PIXEL [ #%02X%02X%02X%02X ]\n",
432 for (
int i = 0; i < indent; i++) {
449 printf(
" row x col : %d x %d\n", z.
height, z.
width);
452 printf(
" pixels : loaded\n");
454 printf(
" pixels : empty\n");
472 "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 indirectly calls the 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
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 pixels with bytes.
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
pixel ** pixels
[height][width] pixel buffer