All pastes #207709 Raw Edit

rip

public text v1 · immutable
#207709 ·published 2006-10-18 04:06 UTC
rendered paste body
/*
 * read png pixels. determine pattern.
 * simple...
 */

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#define PNG_DEBUG 3
#include <png.h>

int x, y;

int width, height;
png_byte color_type;
png_byte bit_depth;

png_structp png_ptr;
png_infop info_ptr;
int number_of_passes;
png_bytep * row_pointers;

void read_png_file()
{
char header[8];	// 8 is the maximum size that can be checked

/* open file and test for it being a png */
FILE *fp = fopen("PNG.png", "rb");
if (!fp) printf("Failed to open image file!\n");
fread(header, 1, 8, fp);
if (png_sig_cmp(header, 0, 8)) printf("Not a PNG file!\n");


/* initialize stuff */
png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);

if (!png_ptr) printf("Could not create read struct!\n");

info_ptr = png_create_info_struct(png_ptr);
if (!info_ptr) printf("Could not create info struct!\n");

if (setjmp(png_jmpbuf(png_ptr))) printf("setjmp() failed!\n");

png_init_io(png_ptr, fp);
png_set_sig_bytes(png_ptr, 8);

png_read_info(png_ptr, info_ptr);

width = info_ptr->width;
height = info_ptr->height;
color_type = info_ptr->color_type;
bit_depth = info_ptr->bit_depth;

number_of_passes = png_set_interlace_handling(png_ptr);
png_read_update_info(png_ptr, info_ptr);

/* read file */
if (setjmp(png_jmpbuf(png_ptr))) printf("Failed to read image!\n");

row_pointers = (png_bytep*) malloc(sizeof(png_bytep) * height);
for (y=0; y<height; y++)
	row_pointers[y] = (png_byte*) malloc(info_ptr->rowbytes);

png_read_image(png_ptr, row_pointers);

fclose(fp);
}


void process_file(void)
{
	if (info_ptr->color_type != PNG_COLOR_TYPE_RGBA) printf("Unexpected color type - %d", info_ptr->color_type);

	for (y=0; y<1; y++) {
		png_byte* row = row_pointers[y];
		for (x=0; x<width; x++) {
			png_byte* ptr = &(row[x*4]);
			printf("Pixel at position [ %d - %d ] has the following RGBA values: %d - %d - %d - %d\n", x, y, ptr[0], ptr[1], ptr[2], ptr[3]);
		}
	}

}


int main(int argc, char **argv)
{
	read_png_file();
	process_file();

	return 0;
}