/*
 * 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


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 write_pixel(int x, int y, png_byte cr, png_byte cg, png_byte cb){
    png_byte* row = row_pointers[y];
    png_byte* ptr = &(row[x*3]);
    ptr[0] = cr;
    ptr[1] = cg;
    ptr[2] = cb;
}

void bresenham(int i1, int j1, int i2, int j2, png_byte cr, png_byte cg, png_byte cb){
    int m,b,j,p,i;

    if(i2 > i1 && j2 >= j1 && j2-j1 <= i2-i1){
        m = 2 * (j2 - j1);
        b = 0;
        j = j1;
        p = i2 - i1;

        write_pixel(i1, j1, cr, cg, cb);

        for(i = i1 + 1; i <= i2; i++){
            b = b + m;
            if(b > p){
                j = j + 1;
                b = b - 2 * p;
            }
            write_pixel(i, j, cr, cg, cb);
        }
    }
    else if(i2 > i1 && -j2 >= -j1 && -j2+j1 <= i2-i1){
        m = 2 * (j1 - j2);
        b = 0;
        j = j1;
        p = i2 - i1;

        write_pixel(i1, j1, cr, cg, cb);

        for(i = i1 + 1; i <= i2; i++){
            b = b + m;
            if(b > p){
                j = j - 1;
                b = b - 2 * p;
            }
            write_pixel(i, j, cr, cg, cb);
        }

    }
    else if(j2 > j1 && i2 >= i1 && i2-i1 <= j2-j1){
        m = 2 * (i2 - i1);
        b = 0;
        i = i1;
        p = j2 - j1;

        write_pixel(i1, j1, cr, cg, cb);

        for(j = j1 + 1; j <= j2; j++){
            b = b + m;
            if(b > p){
                i = i + 1;
                b = b - 2 * p;
            }
            write_pixel(i, j, cr, cg, cb);
        }

    }
    else if(-j2 > -j1 && i2 >= i1 && i2-i1 <= -j2+j1){
        m = 2 * (i2 - i1);
        b = 0;
        i = i1;
        p = j1 - j2;

        write_pixel(i1, j1, cr, cg, cb);

        for(j = j1 - 1; j >= j2; j--){
            b = b + m;
            if(b > p){
                i = i + 1;
                b = b - 2 * p;
            }
            write_pixel(i, j, cr, cg, cb);
        }
    }
    else {
        printf("Nigdy tego nie zobacze\n");
    }
}

void draw_circle(int x0, int y0, int radius, png_byte cr, png_byte cg, png_byte cb) {
    int x = 0;
    int y = radius;
    int d = 5 - 4 * radius;

    while (x <= y) {
        write_pixel(x0 + x, y0 + y, cr, cg, cb);
        write_pixel(x0 + y, y0 + x, cr, cg, cb);
        write_pixel(x0 - y, y0 + x, cr, cg, cb);
        write_pixel(x0 - x, y0 + y, cr, cg, cb);
        write_pixel(x0 - x, y0 - y, cr, cg, cb);
        write_pixel(x0 - y, y0 - x, cr, cg, cb);
        write_pixel(x0 + y, y0 - x, cr, cg, cb);
        write_pixel(x0 + x, y0 - y, cr, cg, cb);

        if (d < 0) {
            d += 8 * x + 12;
        } else {
            d += 8 * (x - y) + 20;
            y--;
        }
        x++;
    }
}

typedef struct {
    int x, y;
} Point;

void flood_fill(int x, int y, png_byte target_r, png_byte target_g, png_byte target_b, png_byte fill_r, png_byte fill_g, png_byte fill_b) {

    if (target_r == fill_r && target_g == fill_g && target_b == fill_b) return;

    Point* stack = (Point*)malloc(width * height * sizeof(Point));
    int top = 0;

    stack[top++] = (Point){x, y};

    while (top > 0) {
        Point p = stack[--top];

        if (p.x < 0 || p.x >= width || p.y < 0 || p.y >= height) continue;

        png_byte* ptr = &(row_pointers[p.y][p.x * 3]);

        if (ptr[0] == target_r && ptr[1] == target_g && ptr[2] == target_b) {
            write_pixel(p.x, p.y, fill_r, fill_g, fill_b);

            stack[top++] = (Point){p.x + 1, p.y};
            stack[top++] = (Point){p.x - 1, p.y};
            stack[top++] = (Point){p.x, p.y + 1};
            stack[top++] = (Point){p.x, p.y - 1};
        }
    }
    free(stack);
}


void process_file(void)
{
	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] = 255; ptr[1] = 255; ptr[2] = 255;
        }
    }
	draw_circle(300, 300, 280, 0, 0, 0);

    bresenham(150, 150, 150, 450, 0, 0, 0);
	bresenham(150, 450, 180, 450, 0, 0, 0);
	bresenham(180, 310, 180, 450, 0, 0, 0);
	bresenham(180, 310, 260, 310, 0, 0, 0);
	bresenham(260, 150, 260, 310, 0, 0, 0);
	bresenham(150, 150, 260, 150, 0, 0, 0);
	bresenham(180, 180, 180, 280, 0, 0, 0);
	bresenham(180, 280, 230, 280, 0, 0, 0);
	bresenham(230, 180, 230, 280, 0, 0, 0);
	bresenham(180, 180, 230, 180, 0, 0, 0);

	bresenham(350, 150, 350, 450, 0, 0, 0);
	bresenham(350, 450, 380, 450, 0, 0, 0);
	bresenham(380, 330, 380, 450, 0, 0, 0);
	bresenham(380, 330, 450, 450, 0, 0, 0);
	bresenham(450, 450, 485, 450, 0, 0, 0);
	bresenham(400, 300, 485, 450, 0, 0, 0);
	bresenham(400, 300, 485, 150, 0, 0, 0);
	bresenham(450, 150, 485, 150, 0, 0, 0);
	bresenham(380, 270, 450, 150, 0, 0, 0);
	bresenham(380, 150, 380, 270, 0, 0, 0);
	bresenham(350, 150, 380, 150, 0, 0, 0);

	flood_fill(0, 0, 255, 255, 255, 0, 0, 255);

    flood_fill(300, 300, 255, 255, 255, 0, 255, 0);

    flood_fill(160, 350, 255, 255, 255, 255, 255, 0);
    flood_fill(200, 230, 255, 255, 255, 255, 0, 0);
    flood_fill(365, 300, 255, 255, 255, 255, 255, 0);
}


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

        return 0;
}
