All pastes #211288 Raw Edit

perm2

public text v1 · immutable
#211288 ·published 2006-10-20 07:21 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>

#ifndef png_jmpbuf
#  define png_jmpbuf(png_ptr)   ((png_ptr)->jmpbuf)
#endif

int x, y;

int width, height;
png_byte color_type;
png_byte bit_depth;
png_uint_32 rowbytes;

png_structp png_ptr;
png_infop info_ptr;
png_bytep * row_pointers;

int answer[3000];
int c =0;
int chars = 0;
int i =0;

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_set_packing(png_ptr);

png_read_info(png_ptr, info_ptr);

width = info_ptr->width;
printf("width: %d ",width);
height = info_ptr->height;
printf("height: %d\n",height);
color_type = info_ptr->color_type;
printf("%d\n",color_type);
bit_depth = info_ptr->bit_depth;
printf("%d\n",bit_depth);

rowbytes = info_ptr->rowbytes;
printf("rowbytes: %d\n", rowbytes);

/* 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(width);

png_read_image(png_ptr, row_pointers);

fclose(fp);
}

void process_file(void)
{
	if (setjmp(png_jmpbuf(png_ptr))) printf("setjmp() failed!\n");
	if (info_ptr->bit_depth == 1) printf("It is indeed a 1 bit image!\n");

	for (y=0; y<height; y++) {
		png_byte* row = row_pointers[y];
		for (x=0; x<width; x++) {
			png_byte* ptr = &(row[x]);
			if(ptr[0]){
				printf("O");
				//printf("answer[%d]-%d",chars,(x+(y*100)));
				answer[chars] = (x+(y*100));
				chars++;
			}else{
				printf("#");
			}
		}
		printf("\n");
	}

}

int answercode()
{
	FILE *fp=fopen("out","w+b");
	if (!fp) return 1;
	for(c=0;c<chars;c++)
	{
		if(c==0 || c>answer[c-1])
		{
			printf("%d", answer[c]);
			fprintf(fp,"%c",answer[c]);
		}else{
			printf("%d", answer[c]-answer[c-1]);
			fprintf(fp,"%c", answer[c]-answer[c-1]);
		}
	}
	printf("\n");
	fclose(fp);
	return 0;
}

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

	return 0;
}