#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){
        printf("przypadek 1\n"); //odcinek idzie w prawo i w dol (mniej niz 45 stopni)
        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"); //odcinek idzie w prawo i w gore (mniej niz 45 stopni)
        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"); //odcinek idzie w prawo i w dol (wiecej niz 45 stopni)
        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"); //odcinek idzie w prawo i w gore (wiecej niz 45 stopni)
		        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{
        printf("Tego nie moze byc!!:(\n");

    }

}

void draw_circle(int cx, int cy, int R, png_byte cr, png_byte cg, png_byte cb) {
    int i = 0;
    int j = R;
    int f = 5 - 4 * R;

    void draw8(int cx, int cy, int i, int j) {
        write_pixel(cx + i, cy + j, cr, cg, cb);
        write_pixel(cx - i, cy + j, cr, cg, cb);
        write_pixel(cx + i, cy - j, cr, cg, cb);
        write_pixel(cx - i, cy - j, cr, cg, cb);
        write_pixel(cx + j, cy + i, cr, cg, cb);
        write_pixel(cx - j, cy + i, cr, cg, cb);
        write_pixel(cx + j, cy - i, cr, cg, cb);
        write_pixel(cx - j, cy - i, cr, cg, cb);
    }

    draw8(cx, cy, 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;
        draw8(cx, cy, i, j);
    }
}

void fill(int i, int j, png_byte nr, png_byte ng, png_byte nb) {
    png_byte* row = row_pointers[j];
    png_byte* ptr = &(row[i*3]);

    // Jesli niezamalowany
    if (ptr[0] == 0 && ptr[1] == 255 && ptr[2] == 255) {
        write_pixel(i, j, nr, ng, nb);

        fill(i - 1, j, nr, ng, nb);
        fill(i, j - 1, nr, ng, nb);
        fill(i + 1, j, nr, ng, nb);
        fill(i, j + 1, nr, ng, nb);
    }
}

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;
		}
	}
	
	// KOLOR DARK BLUE
    png_byte r = 0, g = 0, b = 139;

	// !!!!!!!!!!!!!! LITERA 'O'!!!!!!!!!!!!!!!!!!

    // M 50 10 + translate(20,40) = (70, 50)
    // L 75 40 + translate(20,40) = (95, 80)
    bresenham(70, 50, 95, 80, r, g, b);//70 w prawo, 50 w dol do 95 prawo, 80 dol
    bresenham(95, 80, 95, 110, r, g, b);
    bresenham(70, 140, 95, 110, r, g, b); 
    bresenham(45, 110, 70, 140, r, g, b); 
    bresenham(45, 80, 45, 110, r, g, b); 
    bresenham(45, 80, 70, 50, r, g, b);

    bresenham(55, 80, 70, 60, r, g, b); 
    bresenham(55, 80, 55, 110, r, g, b); 
    bresenham(55, 110, 70, 130, r, g, b); 
    bresenham(70, 130, 85, 110, r, g, b); 
    bresenham(85, 80, 85, 110, r, g, b); 
    bresenham(70, 60, 85, 80, r, g, b);
    
    
    // !!!!!!!!!!!!!!!! LITERA 'L' !!!!!!!!!!!!!!!!!!!
    bresenham(110, 50, 110, 140, r, g, b);
    bresenham(110, 140, 160, 140, r, g, b);
    bresenham(160, 130, 160, 140, r, g, b);
    bresenham(120, 130, 160, 130, r, g, b);
    bresenham(120, 50, 120, 130, r, g, b);
    bresenham(110, 50, 120, 50, r, g, b);
    
    
    // ????????????????? OKRAG ???????????????????????
    draw_circle(100, 100, 80, r, g, b);
    
	// $$$$$$$$$$$$$$$ ZAMALOWYWANIE $$$$$$$$$$$$$$
    // Kolo na zielono
    fill(30, 100, 0, 255, 0); 
    fill(70, 80, 0, 255, 0); //w srodku 'O'
    // 'O' na pomaranczowo
    fill(50, 100, 255, 165, 0); 
    // 'L' na zolto
    fill(115, 100, 255, 255, 0);
}

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

    return 0;
}
