/*
 * 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)
{

    if (i1 > i2) {
        int temp_i = i1;
        int temp_j = j1;
        i1 = i2;
        j1 = j2;
        i2 = temp_i;
        j2 = temp_j;
    }
    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;
        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);
        }
    }
    else if(i2>i1 && -j2>=-j1 && -j2+j1<=i2-i1)
    {
        printf("przypadek 2\n");
        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);
        }
    }
    else if(j2>j1 && i2>=i1 && i2-i1<=j2-j1)
    {
        printf("przypadek 3\n");
        m = 2 * (i2 - i1);
        b = 0;
        write_pixel(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;
            }
            write_pixel(i, j, cr, cg, cb);
        }

    }
    else if(-j2>-j1 && i2>=i1 && i2-i1<=-j2+j1)
    {
        printf("przypadek 4\n");
        m = 2 * (i2 - i1);
        b = 0;
        write_pixel(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;
            }
            write_pixel(i, j, cr, cg, cb);
        }
    }
    else{
        printf("nigdy tego nie zobacze\n");
    }
}

void draw_circle(int xm, int ym, int R, png_byte cr, png_byte cg, png_byte cb) {
    int i = 0;
    int j = R;
    int f = 5 - 4 * R;

    void write_8_pixels(int xm, int ym, int i, int j) {
        write_pixel(xm + i, ym + j, cr, cg, cb);
        write_pixel(xm - i, ym + j, cr, cg, cb);
        write_pixel(xm + i, ym - j, cr, cg, cb);
        write_pixel(xm - i, ym - j, cr, cg, cb);
        write_pixel(xm + j, ym + i, cr, cg, cb);
        write_pixel(xm - j, ym + i, cr, cg, cb);
        write_pixel(xm + j, ym - i, cr, cg, cb);
        write_pixel(xm - j, ym - i, cr, cg, cb);
    }

    write_8_pixels(xm, ym, i, j);

    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;

        write_8_pixels(xm, ym, i, j);
    }
}

int is_color(int x, int y, png_byte r, png_byte g, png_byte b) {
    if (x < 0 || x >= width || y < 0 || y >= height) return 0;
    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, y;
} Point;

void flood_fill(int start_x, int start_y, png_byte tr, png_byte tg, png_byte tb,
                png_byte fr, png_byte fg, png_byte fb) {

    // Jeśli punkt startowy już ma kolor docelowy, nie rób nic
    if (is_color(start_x, start_y, fr, fg, fb)) return;
    // Jeśli punkt startowy nie ma koloru, który chcemy zastąpić, nie rób nic
    if (!is_color(start_x, start_y, tr, tg, tb)) return;

    // Tworzymy duży stos na punkty (dla 600x600 wystarczy szerokosc * wysokosc)
    Point *stack = (Point*)malloc(width * height * sizeof(Point));
    int top = -1;

    // Dodaj pierwszy punkt (S <- S + (i,j))
    stack[++top] = (Point){start_x, start_y};
    write_pixel(start_x, start_y, fr, fg, fb);

    while (top >= 0) {
        Point p = stack[top--]; // Pobierz ze stosu

        // Sprawdź 4 sąsiadów (czterospójny obszar)
        int dx[] = {0, 0, 1, -1};
        int dy[] = {1, -1, 0, 0};

        for (int i = 0; i < 4; i++) {
            int nx = p.x + dx[i];
            int ny = p.y + dy[i];

            // Jeśli sąsiad mieści się w granicach i ma kolor tła (tr, tg, tb)
            if (nx >= 0 && nx < width && ny >= 0 && ny < height && is_color(nx, ny, tr, tg, tb)) {
                write_pixel(nx, ny, fr, fg, fb); // Zamaluj
                stack[++top] = (Point){nx, ny};  // Dodaj na stos
            }
        }
    }
    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] = 0;
			ptr[1] = ptr[2] = 255;
		}
	}
	int ox = 170;
    int oy = 200;
	png_byte r = 0, g = 0, b = 0;
	draw_circle(300, 300, 200, r, g, b);

    // LITERA B
    bresenham(10+ox, 20+oy, 10+ox, 170+oy, r, g, b);
    bresenham(10+ox, 20+oy, 70+ox, 20+oy, r, g, b);
    // Górny brzuszek
    bresenham(70+ox, 20+oy, 100+ox, 40+oy, r, g, b);
    bresenham(100+ox, 40+oy, 110+ox, 60+oy, r, g, b);
    bresenham(110+ox, 60+oy, 110+ox, 80+oy, r, g, b);
    bresenham(110+ox, 80+oy, 100+ox, 90+oy, r, g, b);
    bresenham(100+ox, 90+oy, 70+ox, 95+oy, r, g, b);
    // Dolny brzuszek
    bresenham(70+ox, 95+oy, 105+ox, 100+oy, r, g, b);
    bresenham(105+ox, 100+oy, 120+ox, 120+oy, r, g, b);
    bresenham(120+ox, 120+oy, 120+ox, 145+oy, r, g, b);
    bresenham(120+ox, 145+oy, 105+ox, 165+oy, r, g, b);
    bresenham(105+ox, 165+oy, 70+ox, 170+oy, r, g, b);

    bresenham(70+ox, 170+oy, 10+ox, 170+oy, r, g, b);

    // Górna dziura
    bresenham(35+ox, 45+oy, 75+ox, 45+oy, r, g, b);
    bresenham(75+ox, 45+oy, 85+ox, 55+oy, r, g, b);
    bresenham(85+ox, 55+oy, 85+ox, 65+oy, r, g, b);
    bresenham(85+ox, 65+oy, 75+ox, 70+oy, r, g, b);
    bresenham(75+ox, 70+oy, 35+ox, 70+oy, r, g, b);
    bresenham(35+ox, 70+oy, 35+ox, 45+oy, r, g, b);

    // Dolna dziura
    bresenham(35+ox, 115+oy, 80+ox, 115+oy, r, g, b);
    bresenham(80+ox, 115+oy, 90+ox, 125+oy, r, g, b);
    bresenham(90+ox, 125+oy, 90+ox, 135+oy, r, g, b);
    bresenham(90+ox, 135+oy, 80+ox, 145+oy, r, g, b);
    bresenham(80+ox, 145+oy, 35+ox, 145+oy, r, g, b);
    bresenham(35+ox, 145+oy, 35+ox, 115+oy, r, g, b);
    // LITERA M
    bresenham(130+ox, 20+oy, 130+ox, 170+oy, r, g, b);
    bresenham(130+ox, 170+oy, 150+ox, 170+oy, r, g, b);
    bresenham(150+ox, 170+oy, 150+ox, 80+oy, r, g, b);
    bresenham(150+ox, 80+oy, 180+ox, 140+oy, r, g, b);
    bresenham(180+ox, 140+oy, 200+ox, 140+oy, r, g, b);
    bresenham(200+ox, 140+oy, 230+ox, 80+oy, r, g, b);
    bresenham(230+ox, 80+oy, 230+ox, 170+oy, r, g, b);
    bresenham(230+ox, 170+oy, 250+ox, 170+oy, r, g, b);
    bresenham(250+ox, 170+oy, 250+ox, 20+oy, r, g, b);
    bresenham(250+ox, 20+oy, 230+ox, 20+oy, r, g, b);
    bresenham(230+ox, 20+oy, 190+ox, 110+oy, r, g, b);
    bresenham(190+ox, 110+oy, 150+ox, 20+oy, r, g, b);
    bresenham(150+ox, 20+oy, 130+ox, 20+oy, r, g, b);

    flood_fill(150, 300, 0, 255, 255, 255, 255, 0);

    flood_fill(300, 300, 0, 255, 255, 120, 255, 120);

    flood_fill(ox + 140, oy + 100, 0, 255, 255, 255, 0, 0);

    flood_fill(ox + 20, oy + 100, 0, 255, 255, 0, 0, 255);

    flood_fill(ox + 50, oy + 60, 0, 255, 255, 255, 255, 255);
    flood_fill(ox + 50, oy + 130, 0, 255, 255, 255, 255, 255);
}


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

        return 0;
}
