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

        write_pixel(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;
            }
            write_pixel(i,j,cr,cg,cb);
        }
        printf("przypadek 1\n");
    }
    else if(i2>i1 && -j2>=-j1 && -j2+j1<=i2-i1){
        m = 2*(-j2 + j1);
        b = 0;

        write_pixel(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;
            }
            write_pixel(i,j,cr,cg,cb);
        }
        printf("przypadek 2\n");
    }
    else if(j2>j1 && i2>=i1 && i2-i1<=j2-j1){
        m = 2*(i2 - i1);
        b = 0;

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

        j = i1;
        P = j2 - j1;

        for(i = j1 + 1; i <= j2; i++){
            b = b + m;
            if(b > P){
                j = j + 1;
                b = b - 2*P;
            }
            write_pixel(j, i, cr, cg, cb);
        }
        printf("przypadek 3\n");
    }
    else if(j2 < j1 && i2 >= i1 && i2 - i1 <= j1 - j2){
        m = 2*(i2 - i1);
        b = 0;

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

        j = i1;
        P = j1 - j2;

        for(i = j1 - 1; i >= j2; i--){
            b = b + m;
            if(b > P){
                j = j + 1;
                b = b - 2*P;
            }
            write_pixel(j, i, cr, cg, cb);
        }
        printf("przypadek 4\n");
    }
    else {
        printf("Tego wyjsc nie moze!\n");
    }
}
void draw_circle(int cx, int cy, 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(cx + x, cy + y, cr, cg, cb);
        write_pixel(cx - x, cy + y, cr, cg, cb);
        write_pixel(cx + x, cy - y, cr, cg, cb);
        write_pixel(cx - x, cy - y, cr, cg, cb);

        write_pixel(cx + y, cy + x, cr, cg, cb);
        write_pixel(cx - y, cy + x, cr, cg, cb);
        write_pixel(cx + y, cy - x, cr, cg, cb);
        write_pixel(cx - y, cy - x, cr, cg, cb);

        x++;

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

void fill_circle(int cx, int cy, int r,
                 png_byte fr, png_byte fg, png_byte fb)
{
    for (int y = -r; y <= r; y++)
    {
        for (int x = -r; x <= r; x++)
        {
            if (x*x + y*y <= r*r)
            {
                write_pixel(cx + x, cy + y, fr, fg, fb);
            }
        }
    }
}

void flood_fill(int x, int y, png_byte fr, png_byte fg, png_byte fb)
{
    if (x < 0 || x >= width || y < 0 || y >= height) return;

    png_byte* row = row_pointers[y];
    png_byte* ptr = &(row[x*3]);
    png_byte tr = ptr[0];
    png_byte tg = ptr[1];
    png_byte tb = ptr[2];

    if (tr == fr && tg == fg && tb == fb) return;

    int max_size = width * height * 4;
    int* stack_x = (int*)malloc(max_size * sizeof(int));
    int* stack_y = (int*)malloc(max_size * sizeof(int));
    int stack_size = 0;

    stack_x[stack_size] = x;
    stack_y[stack_size] = y;
    stack_size++;

    while (stack_size > 0)
    {
        stack_size--;
        int cx = stack_x[stack_size];
        int cy = stack_y[stack_size];

        if (cx < 0 || cx >= width || cy < 0 || cy >= height) continue;

        png_byte* r_row = row_pointers[cy];
        png_byte* r_ptr = &(r_row[cx*3]);

        if (r_ptr[0] == tr && r_ptr[1] == tg && r_ptr[2] == tb)
        {
            r_ptr[0] = fr;
            r_ptr[1] = fg;
            r_ptr[2] = fb;

            stack_x[stack_size] = cx + 1; stack_y[stack_size] = cy; stack_size++;
            stack_x[stack_size] = cx - 1; stack_y[stack_size] = cy; stack_size++;
            stack_x[stack_size] = cx; stack_y[stack_size] = cy + 1; stack_size++;
            stack_x[stack_size] = cx; stack_y[stack_size] = cy - 1; stack_size++;
        }
    }
    free(stack_x);
    free(stack_y);
}

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

	int tx = 100;
    int ty = 100;

    fill_circle(200, 160, 120, 255, 0, 0);
    draw_circle(200, 160, 120, 0, 0, 0);

    bresenham(20+tx,10+ty, 20+tx,90+ty, 0,255,0);
    bresenham(20+tx,90+ty, 55+tx,90+ty, 0,255,0);
    bresenham(55+tx,90+ty, 70+tx,80+ty, 0,255,0);
    bresenham(55+tx,55+ty, 70+tx,65+ty, 0,255,0);

    bresenham(55+tx,55+ty, 70+tx,45+ty, 0,255,0);
    bresenham(70+tx,20+ty, 70+tx,45+ty, 0,255,0);
    bresenham(70+tx,65+ty, 70+tx,80+ty, 0,255,0);
    bresenham(55+tx,10+ty, 70+tx,20+ty, 0,255,0);
    bresenham(20+tx,10+ty, 55+tx,10+ty, 0,255,0);
    bresenham(30+tx,20+ty, 50+tx,20+ty, 0,255,0);
    bresenham(30+tx,20+ty, 30+tx,40+ty, 0,255,0);
    bresenham(50+tx,20+ty, 60+tx,30+ty, 0,255,0);
    bresenham(30+tx,40+ty, 50+tx,40+ty, 0,255,0);
    bresenham(50+tx,40+ty, 60+tx,30+ty, 0,255,0);
    bresenham(30+tx,60+ty, 50+tx,60+ty, 0,255,0);
    bresenham(30+tx,60+ty, 30+tx,80+ty, 0,255,0);
    bresenham(50+tx,60+ty, 60+tx,70+ty, 0,255,0);
    bresenham(30+tx,80+ty, 50+tx,80+ty, 0,255,0);
    bresenham(50+tx,80+ty, 60+tx,70+ty, 0,255,0);

    bresenham(140+tx, 10+ty, 180+tx, 10+ty, 0,255,0);
    bresenham(180+tx, 10+ty, 180+tx, 25+ty, 0,255,0);
    bresenham(155+tx, 25+ty, 180+tx, 25+ty, 0,255,0);
    bresenham(155+tx, 25+ty, 155+tx, 45+ty, 0,255,0);
    bresenham(155+tx, 45+ty, 180+tx, 45+ty, 0,255,0);
    bresenham(180+tx, 45+ty, 180+tx, 95+ty, 0,255,0);
    bresenham(140+tx, 95+ty, 180+tx, 95+ty, 0,255,0);
    bresenham(140+tx, 80+ty, 140+tx, 95+ty, 0,255,0);
    bresenham(140+tx, 80+ty, 165+tx, 80+ty, 0,255,0);
    bresenham(165+tx, 60+ty, 165+tx, 80+ty, 0,255,0);
    bresenham(140+tx, 60+ty, 165+tx, 60+ty, 0,255,0);
    bresenham(140+tx, 10+ty, 140+tx, 60+ty, 0,255,0);

    flood_fill(125, 150, 0, 0, 255);
    flood_fill(260, 115, 0, 0, 255);


}


int main(int argc, char **argv)
{


	create_png_file();
	process_file();
	write_png_file(OUT_FILE);

        return 0;
}
