
#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 writePixel(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; // R
    ptr[1] = cg;   // G
    ptr[2] = cb;   // B
}

void drawLineBresenham(int x0, int y0, int x1, int y1) {
    //if zeby moc rysowac linie w lewo (takie ulatwienie)
    if (x0 > x1) {
        int tempX = x0;
        x0 = x1;
        x1 = tempX;

        int tempY = y0;
        y0 = y1;
        y1 = tempY;
    }

    int m, b, y, P, x;

    if(x1 >= x0 && y1 >= y0 && (y1 - y0) <= (x1 - x0)){
        printf("przypadek 1\n");
        m = 2 * (y1 - y0);
        b = 0;
        writePixel(x0, y0, 0, 0, 0);
        y = y0;
        P = x1 - x0;

        for(int i = x0 + 1; i <= x1; i++){
            b = b + m;
            if(b > P){
                y = y + 1;
                b = b - 2 * P;
            }
            writePixel(i, y, 0, 0, 0);
        }
    }

    else if(x1 >= x0 && y0 >= y1 && (y0 - y1) <= (x1 - x0)){
        printf("przypadek 2\n");
        m = 2 * (y0 - y1);
        b = 0;
        writePixel(x0, y0, 0, 0, 0);
        y = y0;
        P = x1 - x0;

        for(int i = x0 + 1; i <= x1; i++){
            b = b + m;
            if(b > P){
                y = y - 1;
                b = b - 2 * P;
            }
            writePixel(i, y, 0, 0, 0);
        }
    }

    else if(y1 >= y0 && x1 >= x0 && (x1 - x0) <= (y1 - y0)){
        printf("przypadek 3\n");
        m = 2 * (x1 - x0);
        b = 0;
        writePixel(x0, y0, 0, 0, 0);
        x = x0;
        P = y1 - y0;

        for(int i = y0 + 1; i <= y1; i++){
            b = b + m;
            if(b > P){
                x = x + 1;
                b = b - 2 * P;
            }
            writePixel(x, i, 0, 0, 0);
        }
    }

    else if(y0 >= y1 && x1 >= x0 && (x1 - x0) <= (y0 - y1)){
        printf("przypadek 4\n");
        m = 2 * (x1 - x0);
        b = 0;
        writePixel(x0, y0, 0, 0, 0);
        x = x0;
        P = y0 - y1;

        for(int i = y0 - 1; i >= y1; i--){
            b = b + m;
            if(b > P){
                x = x + 1;
                b = b - 2 * P;
            }
            writePixel(x, i, 0, 0, 0);
        }
    }
    else {
        printf("ERROR");
    }
}

void drawCirclePart(int cx, int cy, int r, int sx, int sy, png_byte cr, png_byte cg, png_byte cb) {
    int i = 0;
    int j = r;
    int f = 5 - 4 * r;

    //Uzupelnienie pixeli koła tam gdzie zaczyna rysowac

    writePixel(cx + sx * i, cy + sy * j, cr, cg, cb);
    writePixel(cx + sx * j, cy + sy * i, 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;

        writePixel(cx + sx * i, cy + sy * j, cr, cg, cb);
        writePixel(cx + sx * j, cy + sy * i, cr, cg, cb);
    }
}

void drawFullCircle(int cx, int cy, int r, png_byte cr, png_byte cg, png_byte cb) {
    drawCirclePart(cx, cy, r,  1,  1, cr, cg, cb);
    drawCirclePart(cx, cy, r, -1,  1, cr, cg, cb);
    drawCirclePart(cx, cy, r,  1, -1, cr, cg, cb);
    drawCirclePart(cx, cy, r, -1, -1, cr, cg, cb);
}

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

// Funkcja sprawdzajaca czy piksel jest wewnatrz obrazu i czy ma kolor tla
int isUnpainted(int x, int y, png_byte target_r, png_byte target_g, png_byte target_b) {
    if (x < 0 || x >= width || y < 0 || y >= height) {
        return 0;
    }

    png_byte* row = row_pointers[y];
    png_byte* ptr = &(row[x * 3]);

    if (ptr[0] == target_r && ptr[1] == target_g && ptr[2] == target_b) {
        return 1;
    }

    return 0;
}

void Fill(int start_x, int start_y, png_byte fill_r, png_byte fill_g, png_byte fill_b) {
    if (start_x < 0 || start_x >= width || start_y < 0 || start_y >= height) {
        return;
    }

    png_byte* start_row = row_pointers[start_y];
    png_byte* start_ptr = &(start_row[start_x * 3]);
    png_byte target_r = start_ptr[0];
    png_byte target_g = start_ptr[1];
    png_byte target_b = start_ptr[2];

    if (target_r == fill_r && target_g == fill_g && target_b == fill_b) {
        return;
    }

    int max_stack_size = width * height;
    Point* stack = (Point*)malloc(max_stack_size * sizeof(Point));

    int stack_size = 0;

    writePixel(start_x, start_y, fill_r, fill_g, fill_b);
    stack[stack_size].x = start_x;
    stack[stack_size].y = start_y;
    stack_size++;

    while (stack_size > 0) {
        stack_size--;
        int cx = stack[stack_size].x;
        int cy = stack[stack_size].y;

        if (isUnpainted(cx - 1, cy, target_r, target_g, target_b)) {
            writePixel(cx - 1, cy, fill_r, fill_g, fill_b);
            stack[stack_size].x = cx - 1;
            stack[stack_size].y = cy;
            stack_size++;
        }

        if (isUnpainted(cx, cy - 1, target_r, target_g, target_b)) {
            writePixel(cx, cy - 1, fill_r, fill_g, fill_b);
            stack[stack_size].x = cx;
            stack[stack_size].y = cy - 1;
            stack_size++;
        }

        if (isUnpainted(cx + 1, cy, target_r, target_g, target_b)) {
            writePixel(cx + 1, cy, fill_r, fill_g, fill_b);
            stack[stack_size].x = cx + 1;
            stack[stack_size].y = cy;
            stack_size++;
        }

        if (isUnpainted(cx, cy + 1, target_r, target_g, target_b)) {
            writePixel(cx, cy + 1, fill_r, fill_g, fill_b);
            stack[stack_size].x = cx;
            stack[stack_size].y = cy + 1;
            stack_size++;
        }
    }
}

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

	drawLineBresenham(225, 175, 275, 225);
    drawLineBresenham(275, 225, 275, 275);
    drawLineBresenham(275, 275, 225, 225);
    drawLineBresenham(225, 225, 225, 275);
    drawLineBresenham(225, 275, 275, 300);
    drawLineBresenham(275, 300, 275, 350);
    drawLineBresenham(275, 350, 225, 375);
    drawLineBresenham(225, 375, 175, 350);
    drawLineBresenham(175, 350, 175, 300);
    drawLineBresenham(175, 300, 225, 325);
    drawLineBresenham(225, 325, 225, 300);
    drawLineBresenham(225, 300, 175, 275);
    drawLineBresenham(175, 275, 175, 225);
    drawLineBresenham(175, 225, 225, 175);

    drawLineBresenham(300, 175, 325, 175);
    drawLineBresenham(325, 175, 325, 250);
    drawLineBresenham(325, 250, 375, 175);
    drawLineBresenham(375, 175, 400, 175);
    drawLineBresenham(400, 175, 337, 275);
    drawLineBresenham(337, 275, 400, 375);
    drawLineBresenham(400, 375, 375, 375);
    drawLineBresenham(375, 375, 325, 300);
    drawLineBresenham(325, 300, 325, 375);
    drawLineBresenham(325, 375, 300, 375);
    drawLineBresenham(300, 375, 300, 175);

    drawFullCircle(300, 300, 200, 0, 0, 0);

    Fill(200, 300, 255, 31, 35);
    Fill(225, 180, 34, 255, 31);
    Fill(305, 180, 251, 31, 255);
}


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

        return 0;
}
