/*
 * 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)
{
    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 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 (i1 > i2) {
        int tmp;

        tmp = i1;
        i1 = i2;
        i2 = tmp;

        tmp = j1;
        j1 = j2;
        j2 = tmp;
    }

    if (i1 == i2) {
        if (j1 > j2) {
            int tmp = j1;
            j1 = j2;
            j2 = tmp;
        }

        for (j = j1; j <= j2; j++) {
            write_pixel(i1, j, cr, cg, cb);
        }

        return;
    }

    if (i2 > i1 && j2 >= j1 && j2 - j1 <= i2 - i1) {
        printf("przypadek 1\n");

        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 && j1 - j2 <= i2 - i1) {
        printf("przypadek 2\n");

        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) {
        printf("przypadek 3\n");

        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 <= j1 - j2) {
        printf("przypadek 4\n");

        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);
        }
    }
}

void circle(int xc, int yc, int r,
            png_byte cr, png_byte cg, png_byte cb)
{
    int x = 0;
    int y = r;
    int d = 3 - 2 * r;

    while (y >= x) {
        write_pixel(xc + x, yc + y, cr, cg, cb);
        write_pixel(xc - x, yc + y, cr, cg, cb);
        write_pixel(xc + x, yc - y, cr, cg, cb);
        write_pixel(xc - x, yc - y, cr, cg, cb);

        write_pixel(xc + y, yc + x, cr, cg, cb);
        write_pixel(xc - y, yc + x, cr, cg, cb);
        write_pixel(xc + y, yc - x, cr, cg, cb);
        write_pixel(xc - y, yc - x, cr, cg, cb);

        if (d < 0) {
            d = d + 4 * x + 6;
        } else {
            d = d + 4 * (x - y) + 10;
            y--;
        }

        x++;
    }
}

int same_color(int x, int y, png_byte r, png_byte g, png_byte b)
{
    png_byte* row = row_pointers[y];
    png_byte* ptr = &(row[x * 3]);

    return ptr[0] == r && ptr[1] == g && ptr[2] == b;
}

typedef struct {
    int x;
    int y;
} Point;

void flood_fill(int start_x, int start_y,
                png_byte old_r, png_byte old_g, png_byte old_b,
                png_byte new_r, png_byte new_g, png_byte new_b)
{
    Point *stack = malloc(width * height * sizeof(Point));
    int top = 0;

    if (stack == NULL) {
        return;
    }

    if (old_r == new_r && old_g == new_g && old_b == new_b) {
        free(stack);
        return;
    }

    stack[top].x = start_x;
    stack[top].y = start_y;
    top++;

    while (top > 0) {
        Point p;
        int x;
        int y;

        top--;
        p = stack[top];

        x = p.x;
        y = p.y;

        if (x < 0 || x >= width || y < 0 || y >= height) {
            continue;
        }

        if (!same_color(x, y, old_r, old_g, old_b)) {
            continue;
        }

        write_pixel(x, y, new_r, new_g, new_b);

        stack[top].x = x + 1;
        stack[top].y = y;
        top++;

        stack[top].x = x - 1;
        stack[top].y = y;
        top++;

        stack[top].x = x;
        stack[top].y = y + 1;
        top++;

        stack[top].x = x;
        stack[top].y = y - 1;
        top++;
    }

    free(stack);
}

void draw_initials(void)
{
    bresenham(135, 400, 135, 150, 0, 0, 0);
    bresenham(135, 150, 175, 150, 0, 0, 0);
    bresenham(175, 150, 225, 270, 0, 0, 0);
    bresenham(225, 270, 275, 150, 0, 0, 0);
    bresenham(275, 150, 315, 150, 0, 0, 0);
    bresenham(315, 150, 315, 400, 0, 0, 0);
    bresenham(315, 400, 275, 400, 0, 0, 0);
    bresenham(275, 400, 275, 240, 0, 0, 0);
    bresenham(275, 240, 225, 350, 0, 0, 0);
    bresenham(225, 350, 175, 240, 0, 0, 0);
    bresenham(175, 240, 175, 400, 0, 0, 0);
    bresenham(175, 400, 135, 400, 0, 0, 0);

    bresenham(455, 150, 365, 150, 0, 0, 0);
    bresenham(365, 150, 345, 250, 0, 0, 0);
    bresenham(345, 250, 435, 280, 0, 0, 0);
    bresenham(435, 280, 455, 380, 0, 0, 0);
    bresenham(455, 380, 355, 400, 0, 0, 0);
    bresenham(355, 400, 345, 360, 0, 0, 0);
    bresenham(345, 360, 405, 350, 0, 0, 0);
    bresenham(405, 350, 395, 310, 0, 0, 0);
    bresenham(395, 310, 315, 280, 0, 0, 0);
    bresenham(315, 280, 335, 180, 0, 0, 0);
    bresenham(335, 180, 445, 190, 0, 0, 0);
    bresenham(445, 190, 455, 150, 0, 0, 0);
}

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] = 255;
            ptr[2] = 255;
        }
    }

    draw_initials();
    circle(300, 285, 230, 0, 0, 0);

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

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

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


}

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

    return 0;
}
