/*
 * 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 (i2 < i1) {
        int tmp;
        tmp = i1; i1 = i2; i2 = tmp;
        tmp = j1; j1 = j2; j2 = tmp;
    }
    int m, b, j, P, i;
    if(i2>i1 && j2>=j1 && j2-j1<=i2-i1){
        printf("przypadek1\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("przypadek2\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("przypadek3\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("przypadek4\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{
        printf("tego przypadku nie ma\n");
    }
}

void draw_polygon(int points[][2], int n,
                  png_byte cr, png_byte cg, png_byte cb)
{
    for(int i = 0; i < n; i++){
        int next = (i + 1) % n;
        bresenham(points[i][0], points[i][1],
                  points[next][0], points[next][1],
                  cr, cg, cb);
    }
}

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 (x <= y) {

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

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

        x++;
    }
}

typedef struct {
    int x, y;
} Point;

#define MAX_STACK 100000

void flood_fill_stack(int sx, int sy,
                       png_byte tr, png_byte tg, png_byte tb,
                       png_byte nr, png_byte ng, png_byte nb)
{
    Point stack[MAX_STACK];
    int top = 0;

    // start
    stack[top++] = (Point){sx, sy};

    while (top > 0) {

        Point p = stack[--top];
        int x = p.x;
        int y = p.y;

        if (x < 0 || x >= width || y < 0 || y >= height)
            continue;

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

        if (ptr[0] != tr || ptr[1] != tg || ptr[2] != tb)
            continue;

        ptr[0] = nr;
        ptr[1] = ng;
        ptr[2] = nb;

        stack[top++] = (Point){x - 1, y};
        stack[top++] = (Point){x, y - 1};
        stack[top++] = (Point){x + 1, y};
        stack[top++] = (Point){x, y + 1};
    }
}

void scanline_fill(int poly[][2], int n,
                   png_byte r, png_byte g, png_byte b)
{
    int minY = HEIGHT, maxY = 0;

    for(int i=0;i<n;i++){
        if(poly[i][1] < minY) minY = poly[i][1];
        if(poly[i][1] > maxY) maxY = poly[i][1];
    }

    for(int y=minY; y<=maxY; y++){
        int nodes[1000];
        int nodeCount = 0;

        for(int i=0;i<n;i++){
            int j = (i+1)%n;

            int y1 = poly[i][1];
            int y2 = poly[j][1];
            int x1 = poly[i][0];
            int x2 = poly[j][0];

            if((y1 < y && y2 >= y) || (y2 < y && y1 >= y)){
                int x = x1 + (y - y1) * (x2 - x1) / (y2 - y1);
                nodes[nodeCount++] = x;
            }
        }

        // sort
        for(int i=0;i<nodeCount-1;i++)
            for(int j=i+1;j<nodeCount;j++)
                if(nodes[i] > nodes[j]){
                    int t = nodes[i];
                    nodes[i] = nodes[j];
                    nodes[j] = t;
                }

        // fill between pairs
        for(int i=0;i<nodeCount;i+=2){
            if(i+1 >= nodeCount) break;
            for(int x=nodes[i]; x<nodes[i+1]; x++){
                write_pixel(x, y, r, g, b);
            }
        }
    }
}

void fill_circle(int xc, int yc, int r,
                 png_byte cr, png_byte cg, png_byte cb)
{
    for(int y = -r; y <= r; y++){
        for(int x = -r; x <= r; x++){
            if(x*x + y*y <= r*r){
                write_pixel(xc + x, yc + y, cr, cg, cb);
            }
        }
    }
}

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 M[][2] = {
    {110+110, 190+150},
    {110+110, 110+150},
    {130+110, 110+150},
    {150+110, 150+150},
    {170+110, 110+150},
    {190+110, 110+150},
    {190+110, 190+150},
    {170+110, 190+150},
    {170+110, 160+150},
    {150+110, 180+150},
    {130+110, 160+150},
    {130+110, 190+150}
};

    // L
    int L[][2] = {
    {200+110, 190+150},
    {200+110, 110+150},
    {220+110, 110+150},
    {220+110, 170+150},
    {270+110, 170+150},
    {270+110, 190+150}
};

    // obrys (opcjonalnie)
    draw_circle(300, 300, 140, 0, 100, 0);

    // wypełnienie
    fill_circle(300, 300, 140, 0, 255, 0);
    draw_polygon(M, 12, 0,0,0);
    draw_polygon(L, 6, 0,0,0);

    // WYPEŁNIENIE
    scanline_fill(M, 12, 255,0,255);   // M różowy
    scanline_fill(L, 6, 255,255,0);    // L żółty


}

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

        return 0;
}
