Skip to content

Instantly share code, notes, and snippets.

@wolfy1339
Created February 15, 2016 00:08
Show Gist options
  • Save wolfy1339/281685be2b3ac3513cab to your computer and use it in GitHub Desktop.
Save wolfy1339/281685be2b3ac3513cab to your computer and use it in GitHub Desktop.
WIP PNG to RGB values
// based off the code from http://zarb.org/~gc/html/libpng.html but simplified/stripped down by Lockheedmartin.
// Conversion to C++ by wolfy1339
#include <unistd.h>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <cstdarg>
#include <png.h>
int x, y, width, height, r, g, b;
png_byte color_type;
png_byte bit_depth;
png_structp png_ptr;
png_infop info_ptr;
int num_passes;
png_bytep* row_pointers;
void exit_error(const char *s,...) {
va_list args;
va_start(args, s);
vfprintf(stderr, s, args);
fprintf(stderr, "\n");
va_end(args);
abort();
}
/*void exit_error(const char *s) {
printf(s);
printf("\n");
abort();
}*/
int main(int argc, char ** argv) {
if (argc == 1) {
exit_error("No filename given");
}
char header[8];
FILE *fp = fopen(argv[1], "rb");
if (!fp) {
exit_error("[reading file] File %s cannot be opened.", argv[1]);
//exit_error("[reading file] File cannot be opened.");
}
fread(header, 1, 8, fp);
if (png_sig_cmp(header, 0, 8))
exit_error("[reading file] File %s is not a PNG file.", argv[1]);
//exit_error("[reading file] File is not a PNG file.");
png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
if (!png_ptr)
exit_error("[reading file] png_create_read_struct failed.");
info_ptr=png_create_info_struct(png_ptr);
if (!info_ptr)
exit_error("[reading file] png_create_info_struct failed.");
if (setjmp(png_jmpbuf(png_ptr)))
exit_error("[reading file] Error during init_io");
png_init_io(png_ptr, fp);
png_set_sig_bytes(png_ptr, 8);
png_read_info(png_ptr, info_ptr);
width = png_get_image_width(png_ptr, info_ptr);
height = png_get_image_height(png_ptr, info_ptr);
color_type = png_get_color_type(png_ptr, info_ptr);
bit_depth = png_get_bit_depth(png_ptr, info_ptr);
num_passes = png_set_interlace_handling(png_ptr);
png_read_update_info(png_ptr, info_ptr);
if (setjmp(png_jmpbuf(png_ptr)))
exit_error("[reading file] Error during read_image");
row_pointers = (png_bytep*) malloc(sizeof(png_bytep) * height);
for (y=0; y < height; y++)
row_pointers[y] = (png_byte*) malloc(png_get_rowbytes(png_ptr, info_ptr));
png_read_image(png_ptr, row_pointers);
fclose(fp);
if (!((color_type == PNG_COLOR_TYPE_RGB)||(color_type == PNG_COLOR_TYPE_RGBA)))
exit_error("[print color info] Image is not RGB or RGBA...");
for (y=0; y < height; y++) {
png_byte* row = row_pointers[y];
for (x=0; x<width; x++) {
if (color_type == PNG_COLOR_TYPE_RGB) {
png_byte *ptr = &(row[x*3]);
printf("Pixel at position (%d,%d) has RGBA values (%d,%d,%d,%d)", x ,y, ptr[0], ptr[1], ptr[2], 0);
}
if (color_type == PNG_COLOR_TYPE_RGBA) {
png_byte *ptr = &(row[x*4]);
printf("Pixel at position (%d,%d) has RGBA values (%d,%d,%d,%d)", x, y, ptr[0], ptr[1], ptr[2], ptr[3]);
}
printf("\n");
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment