/*
 * Copyright 2002-2008 Guillaume Cottenceau, 2015 Aleksander Denisiuk
 *
 * This software may be freely redistributed under the terms
 * of the X11 license.
 *
 */

#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>

#define PNG_DEBUG 3
#include <png.h>

#define OUT_FILE "initials.png"
#define WIDTH 600
#define HEIGHT 600
#define COLOR_TYPE PNG_COLOR_TYPE_RGB
#define BIT_DEPTH 8

typedef struct {
	int i;
	int j; 
} Point;

void write_pixel(int i, int j, int color[]);
void draw_line(int i1, int j1, int i2, int j2, int color[]);
void draw_shape(Point points[], int size, int color[]);
void write_circle_points(int cx, int cy, int i, int j, int color[]);
void draw_circle(int cx, int cy, int R, int color[]);
void fill(int i, int j, int target_color[],int new_color[]);


void abort_(const char * s, ...)
{
	va_list args;
	va_start(args, s);
	vfprintf(stderr, s, args);
	fprintf(stderr, "\n");
	va_end(args);
	abort();
}

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 create_png_file()
{
	width = WIDTH;
	height = HEIGHT;
        bit_depth = BIT_DEPTH;
        color_type = COLOR_TYPE;

	row_pointers = (png_bytep*) malloc(sizeof(png_bytep) * height);
	for (y=0; y<height; y++)
		row_pointers[y] = (png_byte*) malloc(width*bit_depth*3);
}


void write_png_file(char* file_name)
{
	/* create file */
	FILE *fp = fopen(file_name, "wb");
	if (!fp)
		abort_("[write_png_file] File %s could not be opened for writing", file_name);


	/* initialize stuff */
	png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
	
	if (!png_ptr)
		abort_("[write_png_file] png_create_write_struct failed");

	info_ptr = png_create_info_struct(png_ptr);
	if (!info_ptr)
		abort_("[write_png_file] png_create_info_struct failed");

	if (setjmp(png_jmpbuf(png_ptr)))
		abort_("[write_png_file] Error during init_io");

	png_init_io(png_ptr, fp);


	/* write header */
	if (setjmp(png_jmpbuf(png_ptr)))
		abort_("[write_png_file] Error during writing header");

	png_set_IHDR(png_ptr, info_ptr, width, height,
		     bit_depth, color_type, PNG_INTERLACE_NONE,
		     PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);

	png_write_info(png_ptr, info_ptr);


	/* write bytes */
	if (setjmp(png_jmpbuf(png_ptr)))
		abort_("[write_png_file] Error during writing bytes");

	png_write_image(png_ptr, row_pointers);


	/* end write */
	if (setjmp(png_jmpbuf(png_ptr)))
		abort_("[write_png_file] Error during end of write");

	png_write_end(png_ptr, NULL);

    /* cleanup heap allocation */
	for (y=0; y<height; y++)
		free(row_pointers[y]);
	free(row_pointers);

        fclose(fp);
}

void process_file(void)
{
	int background_color[3] = {253, 231, 76};

	for (y = 0; y < height; y++) {
		png_byte* row = row_pointers[y];
		for (x=0; x<width; x++) {
			png_byte* ptr = &(row[x*3]);
			ptr[0] = background_color[0];
			ptr[1] = background_color[1];
			ptr[2] = background_color[2];
		}
	}

	int circle_color[3] = {251, 177, 60};

	draw_circle(WIDTH / 2, HEIGHT / 2, 295, circle_color);
	fill(300, 300, background_color, circle_color);

	int initial_outline[3] = {33, 118, 174};
	int initial_fill[3] ={87, 184, 255};

	Point M1[] = {
		(Point){50, 150},
		(Point){50, 450},
		(Point){100, 450},
		(Point){100, 225},
		(Point){162, 325},
		(Point){163, 325},
		(Point){225, 225},
		(Point){225, 450},
		(Point){275, 450},
		(Point){275, 150},
		(Point){225, 150},
		(Point){163, 250},
		(Point){162, 250},
		(Point){100, 150}
	};

	Point M2[] = {
		(Point){325, 150},
		(Point){325, 450},
		(Point){375, 450},
		(Point){375, 225},
		(Point){437, 325},
		(Point){438, 325},
		(Point){500, 225},
		(Point){500, 450},
		(Point){550, 450},
		(Point){550, 150},
		(Point){500, 150},
		(Point){438, 250},
		(Point){437, 250},
		(Point){375, 150}
	};

	draw_shape(M1, sizeof(M1)/sizeof(M1[0]), initial_outline);
	draw_shape(M2, sizeof(M2)/sizeof(M2[0]), initial_outline);

	fill(51, 151, circle_color, initial_fill);
	fill(326, 151, circle_color, initial_fill);
}

void write_pixel(int i, int j, int color[]) {
	if (i < 0 || i >= width || j < 0 || j >= height) return;

	png_byte* row = row_pointers[j];
	png_byte* ptr = &(row[i*3]);
	ptr[0] = color[0];
	ptr[1] = color[1];
	ptr[2] = color[2];
}

void draw_line(int i1, int j1, int i2, int j2, int color[]) {
	int di = abs(i2 - i1);
    int dj = abs(j2 - j1);
    int si = (i1 < i2) ? 1 : -1;
    int sj = (j1 < j2) ? 1 : -1;
    int err = di - dj;

    while (1) {
        write_pixel(i1, j1, color);
        if (i1 == i2 && j1 == j2) break;

        int e2 = 2 * err;
        if (e2 > -dj) { err -= dj; i1 += si; }
        if (e2 <  di) { err += di; j1 += sj; }
    }
}

void draw_shape(Point points[], int size, int color[]) {
	for (int i = 0; i < size - 1; i++) {
		Point point_A = points[i];
		Point point_B = points[i+1];

		draw_line(point_A.i, point_A.j, point_B.i, point_B.j, color);
	}
	draw_line(points[size - 1].i, points[size - 1].j, points[0].i, points[0].j, color);
}

void write_circle_points(int cx, int cy, int i, int j, int color[]) {
	write_pixel(cx + i, cy + j, color);
	write_pixel(cx - i, cy + j, color);
	write_pixel(cx + i, cy - j, color);
	write_pixel(cx - i, cy - j, color);
	write_pixel(cx + j, cy + i, color);
	write_pixel(cx - j, cy + i, color);
	write_pixel(cx + j, cy - i, color);
	write_pixel(cx - j, cy - i, color);
}

void draw_circle(int cx, int cy, int R, int color[]) {
	int i = 0;
	int j = R;
	int f = 5 - 4 * R;
	write_circle_points(cx, cy, i, j, color);

	while (i < j) {
		if (f > 0) {
			f = f + 8 * i - 8 * j + 20;
			j--;
		} else  {
			f = f + 8 * i + 12;
		}
		i++;
		write_circle_points(cx, cy, i, j, color);
	}
}

void fill(int start_i, int start_j, int target_color[], int new_color[]) {
	if (target_color[0] == new_color[0] &&
        target_color[1] == new_color[1] &&
        target_color[2] == new_color[2]) return;

	int max = HEIGHT * WIDTH * 4;
    int head = 0;
	int tail = 0;

    Point* queue = malloc(max * sizeof(Point));
    if (!queue) abort_("");

	Point start;
    start.i = start_i;
    start.j = start_j;
    queue[tail] = start;
    tail++;

    while (head < tail) {
		Point p = queue[head];
		head++;

		int i = p.i;
        int j = p.j;

		if (i < 0 || i >= width || j < 0 || j >= height) {
            continue;
        }

        png_byte* row = row_pointers[j];
        png_byte* ptr = &(row[i * 3]);

        if (ptr[0] != target_color[0] ||
            ptr[1] != target_color[1] ||
            ptr[2] != target_color[2]) continue;

        ptr[0] = new_color[0];
        ptr[1] = new_color[1];
        ptr[2] = new_color[2];

		Point p1, p2, p3, p4;

		p1.i = i - 1; 
		p1.j = j;

		p2.i = i + 1;
		p2.j = j;

		p3.i = i;
		p3.j = j - 1;

		p4.i = i;
		p4.j = j + 1;

		queue[tail++] = p1;
		queue[tail++] = p2;
		queue[tail++] = p3;
		queue[tail++] = p4;
	}

free(queue);
}

int main(int argc, char **argv) {
	create_png_file();
	process_file();
	write_png_file(OUT_FILE);

    return 0;
}
