/*
 * 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>
#include <math.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 writePixel(int x, int y, png_byte cr, png_byte cg, png_byte cb)
{
    if (x < 0 || x >= WIDTH || y < 0 || y >= HEIGHT) return;

    png_byte* row = row_pointers[y];
    png_byte* ptr = &(row[x*3]);
    ptr[0] = cr;
    ptr[1] = cg;
    ptr[2] = cb;
}

void bresenhamLine(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)) )
    {
        printf("Przypadek 1\n");
        m = 2 * (j2 - j1);
        b = 0;
        writePixel(i1, j1, cr, cg, cb);
        j = j1;
        P = i2 - i1;
        for(i = i1 + 1; i <= i2; i++)
        {
            b = b + m;
            if(b > P)
            {
                j = j + 1;
                b = b - 2 * P;
            }
            writePixel(i, j, cr, cg, cb);
        }
    }
    else if ( (i2 > i1) && (j2 < j1) && ((j1 - j2) <= (i2 - i1)) )
    {
        printf("Przypadek 2\n");
        m = 2 * (j1 - j2);
        b = 0;
        writePixel(i1, j1, cr, cg, cb);
        j = j1;
        P = i2 - i1;
        for(i = i1 + 1; i <= i2; i++)
        {
            b = b + m;
            if(b > P)
            {
                j = j - 1;
                b = b - 2 * P;
            }
            writePixel(i, j, cr, cg, cb);
        }
    }
    else if ( (i2 < i1) && (j2 >= j1) && ((j2 - j1) <= (i1 - i2)) )
    {
        printf("Przypadek 3\n");
        m = 2 * (j2 - j1);
        b = 0;
        writePixel(i1, j1, cr, cg, cb);
        j = j1;
        P = i1 - i2;
        for(i = i1 - 1; i >= i2; i--)
        {
            b = b + m;
            if(b > P)
            {
                j = j + 1;
                b = b - 2 * P;
            }
            writePixel(i, j, cr, cg, cb);
        }
    }
    else if ( (i2 < i1) && (j2 < j1) && ((j1 - j2) <= (i1 - i2)) )
    {
        printf("Przypadek 4\n");
        m = 2 * (j1 - j2);
        b = 0;
        writePixel(i1, j1, cr, cg, cb);
        j = j1;
        P = i1 - i2;
        for(i = i1 - 1; i >= i2; i--)
        {
            b = b + m;
            if(b > P)
            {
                j = j - 1;
                b = b - 2 * P;
            }
            writePixel(i, j, cr, cg, cb);
        }
    }
    else if ( (j2 > j1) && (i2 >= i1) && ((i2 - i1) < (j2 - j1)) )
    {
        printf("Przypadek 5\n");
        m = 2 * (i2 - i1);
        b = 0;
        writePixel(i1, j1, cr, cg, cb);
        i = i1;
        P = j2 - j1;
        for(j = j1 + 1; j <= j2; j++)
        {
            b = b + m;
            if(b > P)
            {
                i = i + 1;
                b = b - 2 * P;
            }
            writePixel(i, j, cr, cg, cb);
        }
    }
    else if ( (j2 > j1) && (i2 < i1) && ((i1 - i2) < (j2 - j1)) )
    {
        printf("Przypadek 6\n");
        m = 2 * (i1 - i2);
        b = 0;
        writePixel(i1, j1, cr, cg, cb);
        i = i1;
        P = j2 - j1;
        for(j = j1 + 1; j <= j2; j++)
        {
            b = b + m;
            if(b > P)
            {
                i = i - 1;
                b = b - 2 * P;
            }
            writePixel(i, j, cr, cg, cb);
        }
    }
    else if ( (j2 < j1) && (i2 >= i1) && ((i2 - i1) < (j1 - j2)) )
    {
        printf("Przypadek 7\n");
        m = 2 * (i2 - i1);
        b = 0;
        writePixel(i1, j1, cr, cg, cb);
        i = i1;
        P = j1 - j2;
        for(j = j1 - 1; j >= j2; j--)
        {
            b = b + m;
            if(b > P)
            {
                i = i + 1;
                b = b - 2 * P;
            }
            writePixel(i, j, cr, cg, cb);
        }
    }
    else if ( (j2 < j1) && (i2 < i1) && ((i1 - i2) < (j1 - j2)) )
    {
        printf("Przypadek 8\n");
        m = 2 * (i1 - i2);
        b = 0;
        writePixel(i1, j1, cr, cg, cb);
        i = i1;
        P = j1 - j2;
        for(j = j1 - 1; j >= j2; j--)
        {
            b = b + m;
            if(b > P)
            {
                i = i - 1;
                b = b - 2 * P;
            }
            writePixel(i, j, cr, cg, cb);
        }
    }
    else
    {
        printf("Nigdy te nie zobacze chyba, ze sie popsulo\n");
    }
}

void writeCirclePixel(int xc, int yc, int dx, int dy, png_byte cr, png_byte cg, png_byte cb)
{
    writePixel(xc + dx, yc + dy, cr, cg, cb);
    writePixel(xc - dx, yc + dy, cr, cg, cb);
    writePixel(xc + dx, yc - dy, cr, cg, cb);
    writePixel(xc - dx, yc - dy, cr, cg, cb);

    writePixel(xc + dy, yc + dx, cr, cg, cb);
    writePixel(xc - dy, yc + dx, cr, cg, cb);
    writePixel(xc + dy, yc - dx, cr, cg, cb);
    writePixel(xc - dy, yc - dx, cr, cg, cb);
}
void bresenhamCircle(int x, int y, int R, png_byte cr, png_byte cg, png_byte cb)
{
    if(R > WIDTH/2 || R > HEIGHT/2)
    {
        printf("Za duzy radius okregu");
    }

    int i = 0;
    int j = R;
    int f = 5 - 4 * R;

    writeCirclePixel(x, y, i, j, cr, cg, cb);

    while(i < j)
    {
        if(f > 0)
        {
            f = f + 8*i - 8*j + 20;
            j = j - 1;
        }
        else
        {
            f = f + 8*i + 12;
        }
        i = i + 1;
        writeCirclePixel(x, y, i, j, cr, cg, cb);
    }
}

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

int niezamalowany(int i, int j, png_byte bg_r, png_byte bg_g, png_byte bg_b)
{
    if (i < 0 || i >= WIDTH || j < 0 || j >= HEIGHT) return 0;

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

    return (ptr[0] == bg_r && ptr[1] == bg_g && ptr[2] == bg_b);
}


void wypelnij_obszar(int i_start, int j_start, png_byte cr, png_byte cg, png_byte cb)
{
    if (i_start < 0 || i_start >= WIDTH || j_start < 0 || j_start >= HEIGHT) return;
    png_byte* row_start = row_pointers[j_start];
    png_byte bg_r = row_start[i_start*3];
    png_byte bg_g = row_start[i_start*3 + 1];
    png_byte bg_b = row_start[i_start*3 + 2];

    if (bg_r == cr && bg_g == cg && bg_b == cb) return;

    Punkt* S = (Punkt*)malloc(WIDTH * HEIGHT * sizeof(Punkt));
    int S_top = 0;

    writePixel(i_start, j_start, cr, cg, cb);
    S[S_top].i = i_start;
    S[S_top].j = j_start;
    S_top++;

    while(S_top > 0) {
        S_top--;
        int i = S[S_top].i;
        int j = S[S_top].j;

        if(niezamalowany(i-1, j, bg_r, bg_g, bg_b)) {
            writePixel(i-1, j, cr, cg, cb);
            S[S_top].i = i-1;
            S[S_top].j = j;
            S_top++;
        }

        if(niezamalowany(i, j-1, bg_r, bg_g, bg_b)) {
            writePixel(i, j-1, cr, cg, cb);
            S[S_top].i = i;
            S[S_top].j = j-1;
            S_top++;
        }

        if(niezamalowany(i+1, j, bg_r, bg_g, bg_b)) {
            writePixel(i+1, j, cr, cg, cb);
            S[S_top].i = i+1;
            S[S_top].j = j;
            S_top++;
        }
        if(niezamalowany(i, j+1, bg_r, bg_g, bg_b)) {
            writePixel(i, j+1, cr, cg, cb);
            S[S_top].i = i;
            S[S_top].j = j+1;
            S_top++;
        }
    }

    free(S);
}

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] = 0;
			ptr[1] = ptr[2] = 255;
		}
	}
	png_byte r = 0, g = 180, b = 0;
    //W
    bresenhamLine(100, 200, 150, 450, r, g, b);
    bresenhamLine(150, 450, 200, 350, r, g, b);
    bresenhamLine(200, 350, 250, 450, r, g, b);
    bresenhamLine(250, 450, 300, 200, r, g, b);
    bresenhamLine(300, 200, 250, 200, r, g, b);
    bresenhamLine(250, 200, 235, 350, r, g, b);
    bresenhamLine(235, 350, 200, 300, r, g, b);
    bresenhamLine(200, 300, 165, 350, r, g, b);
    bresenhamLine(165, 350, 150, 200, r, g, b);
    bresenhamLine(150, 200, 100, 200, r, g, b);
    bresenhamLine(350, 200, 350, 450, r, g, b);
    bresenhamLine(350, 450, 450, 450, r, g, b);
    bresenhamLine(450, 450, 500, 420, r, g, b);
    bresenhamLine(500, 420, 500, 355, r, g, b);
    bresenhamLine(500, 355, 450, 325, r, g, b);
    bresenhamLine(450, 325, 500, 295, r, g, b);
    bresenhamLine(500, 295, 500, 230, r, g, b);
    bresenhamLine(500, 230, 450, 200, r, g, b);
    bresenhamLine(450, 200, 350, 200, r, g, b);
    bresenhamLine(385, 235, 425, 235, r, g, b);
    bresenhamLine(425, 235, 465, 250, r, g, b);
    bresenhamLine(465, 250, 465, 285, r, g, b);
    bresenhamLine(465, 285, 425, 300, r, g, b);
    bresenhamLine(425, 300, 385, 300, r, g, b);
    bresenhamLine(385, 300, 385, 235, r, g, b);
    bresenhamLine(385, 355, 425, 355, r, g, b);
    bresenhamLine(425, 355, 465, 370, r, g, b);
    bresenhamLine(465, 370, 465, 405, r, g, b);
    bresenhamLine(465, 405, 425, 420, r, g, b);
    bresenhamLine(425, 420, 385, 420, r, g, b);
    bresenhamLine(385, 420, 385, 355, r, g, b);

    bresenhamCircle(300, 300, 250, 155, 0, 155);

    wypelnij_obszar(10, 10, 200, 200, 200);
    wypelnij_obszar(405, 260, 255, 0, 0);
    wypelnij_obszar(405, 380, 255, 255, 0);
    wypelnij_obszar(300, 300, 255, 128, 0);
    wypelnij_obszar(200, 320, 0, 0, 255);
}

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

        return 0;
}
