/*
 * 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 x1, int y1, int x2, int y2,
               png_byte cr, png_byte cg, png_byte cb)
{
    int dx = abs(x2 - x1);
    int dy = abs(y2 - y1);

    int sx = (x1 < x2) ? 1 : -1;
    int sy = (y1 < y2) ? 1 : -1;

    int err = dx - dy;

    while (1)
    {
        write_pixel(x1, y1, cr, cg, cb);

        if (x1 == x2 && y1 == y2)
            break;

        int e2 = 2 * err;

        if (e2 > -dy)
        {
            err -= dy;
            x1 += sx;
        }

        if (e2 < dx)
        {
            err += dx;
            y1 += sy;
        }
    }
}

void draw_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);

        x++;

        if (d > 0)
        {
            y--;
            d = d + 4 * (x - y) + 10;
        }
        else
        {
            d = d + 4 * x + 6;
        }
    }
}

png_byte* get_pixel(int x, int y) {
    return &(row_pointers[y][x * 3]);
}

typedef struct {
    int x, y;
} Point;

void floodFill(int x, int y,
               png_byte fr, png_byte fg, png_byte fb)
{
    png_byte* start = get_pixel(x, y);

    png_byte target_r = start[0];
    png_byte target_g = start[1];
    png_byte target_b = start[2];

    Point stack[100000];
    int top = 0;

    write_pixel(x, y, fr, fg, fb);
    stack[top++] = (Point){x, y};

    while (top != 0)
    {
        Point p = stack[--top];

        if (p.x - 1 >= 0) {
            png_byte* pix = get_pixel(p.x - 1, p.y);
            if (pix[0] == target_r &&
                pix[1] == target_g &&
                pix[2] == target_b)
            {
                write_pixel(p.x - 1, p.y, fr, fg, fb);
                stack[top++] = (Point){p.x - 1, p.y};
            }
        }
        if (p.y - 1 >= 0) {
            png_byte* pix = get_pixel(p.x, p.y - 1);
            if (pix[0] == target_r &&
                pix[1] == target_g &&
                pix[2] == target_b)
            {
                write_pixel(p.x, p.y - 1, fr, fg, fb);
                stack[top++] = (Point){p.x, p.y - 1};
            }
        }

        if (p.x + 1 < width) {
            png_byte* pix = get_pixel(p.x + 1, p.y);
            if (pix[0] == target_r &&
                pix[1] == target_g &&
                pix[2] == target_b)
            {
                write_pixel(p.x + 1, p.y, fr, fg, fb);
                stack[top++] = (Point){p.x + 1, p.y};
            }
        }

        if (p.y + 1 < height) {
            png_byte* pix = get_pixel(p.x, p.y + 1);
            if (pix[0] == target_r &&
                pix[1] == target_g &&
                pix[2] == target_b)
            {
                write_pixel(p.x, p.y + 1, fr, fg, fb);
                stack[top++] = (Point){p.x, p.y + 1};
            }
        }
    }
}






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] = 128;
			ptr[1] = 128;
            ptr[2] = 128;

		}
	}

    //V
	bresenham(120, 150, 200, 450, 0, 0, 0);
    bresenham(200, 450, 280, 150, 0, 0, 0);
    bresenham(280, 150, 240, 150, 0, 0, 0);
    bresenham(240, 150, 200, 350, 0, 0, 0);
    bresenham(200, 350, 160, 150, 0, 0, 0);
    bresenham(160, 150, 120, 150, 0, 0, 0);

    //Z
    bresenham(320, 150, 500, 150, 0, 0, 0);
    bresenham(500, 150, 500, 210, 0, 0, 0);
    bresenham(500, 210, 380, 390, 0, 0, 0);
    bresenham(380, 390, 500, 390, 0, 0, 0);
    bresenham(500, 390, 500, 450, 0, 0, 0);
    bresenham(500, 450, 320, 450, 0, 0, 0);
    bresenham(320, 450, 320, 390, 0, 0, 0);
    bresenham(320, 390, 440, 210, 0, 0, 0);
    bresenham(440, 210, 320, 210, 0, 0, 0);
    bresenham(320, 210, 320, 150, 0, 0, 0);

    //kolo
    draw_circle(310, 300, 260, 0, 0, 0);

    //kolor
    floodFill(260, 200, 0, 255, 0);
    floodFill(400, 300, 0, 255, 0);
    floodFill(310, 300, 255, 215, 0);
}


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

        return 0;
}
