/*
 * 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 put_pixel(int px, int py, int r, int g, int b)
{
    if (px < 0 || px >= width || py < 0 || py >= height)
        return;

    png_byte* row = row_pointers[py];
    png_byte* ptr = &(row[px * 3]);

    ptr[0] = r;
    ptr[1] = g;
    ptr[2] = b;
}

void draw_line(int x0, int y0, int x1, int y1, int r, int g, int b)
{
    int dx = abs(x1 - x0);
    int dy = abs(y1 - y0);
    int sx = x0 < x1 ? 1 : -1;
    int sy = y0 < y1 ? 1 : -1;
    int err = dx - dy;

    while (1)
    {
        put_pixel(x0, y0, r, g, b);

        if (x0 == x1 && y0 == y1)
            break;

        int e2 = 2 * err;

        if (e2 > -dy)
        {
            err -= dy;
            x0 += sx;
        }

        if (e2 < dx)
        {
            err += dx;
            y0 += sy;
        }
    }
}
void draw_circle(int xc, int yc, int r, int R, int G, int B)
{
    int x = 0;
    int y = r;
    int d = 3 - 2 * r;

    while (y >= x)
    {
        put_pixel(xc + x, yc + y, R, G, B);
        put_pixel(xc - x, yc + y, R, G, B);
        put_pixel(xc + x, yc - y, R, G, B);
        put_pixel(xc - x, yc - y, R, G, B);

        put_pixel(xc + y, yc + x, R, G, B);
        put_pixel(xc - y, yc + x, R, G, B);
        put_pixel(xc + y, yc - x, R, G, B);
        put_pixel(xc - y, yc - x, R, G, B);

        x++;

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

void flood_fill(int start_x, int start_y, int r, int g, int b, int target_r, int target_g, int target_b)
{
    int max = width * height;
    int *stack_x = malloc(sizeof(int) * max);
    int *stack_y = malloc(sizeof(int) * max);
    int top = 0;

    stack_x[top] = start_x;
    stack_y[top] = start_y;
    top++;

    while (top > 0)
    {
        top--;
        int x = stack_x[top];
        int y = stack_y[top];

        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] != target_r || ptr[1] != target_g || ptr[2] != target_b)
            continue;

        ptr[0] = r;
        ptr[1] = g;
        ptr[2] = b;

        if (top + 4 < max)
        {
            stack_x[top] = x + 1; stack_y[top] = y; top++;
            stack_x[top] = x - 1; stack_y[top] = y; top++;
            stack_x[top] = x; stack_y[top] = y + 1; top++;
            stack_x[top] = x; stack_y[top] = y - 1; top++;
        }
    }

    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]);
			//kolor tła

		put_pixel(x, y, 150,0,0);
		}
	}
			//Kontur M
draw_line(80, 420, 80, 180, 0, 0, 0);
draw_line(80, 180, 125, 180, 0, 0, 0);
draw_line(125, 180, 190, 310, 0, 0, 0);
draw_line(190, 310, 255, 180, 0, 0, 0);
draw_line(255, 180, 300, 180, 0, 0, 0);
draw_line(300, 180, 300, 420, 0, 0, 0);
draw_line(300, 420, 255, 420, 0, 0, 0);
draw_line(255, 420, 255, 270, 0, 0, 0);
draw_line(255, 270, 190, 400, 0, 0, 0);
draw_line(190, 400, 125, 270, 0, 0, 0);
draw_line(125, 270, 125, 420, 0, 0, 0);
draw_line(125, 420, 80, 420, 0, 0, 0);
// Kontur K 
draw_line(330, 180, 330, 420, 0, 0, 0);
draw_line(330, 420, 375, 420, 0, 0, 0);
draw_line(375, 420, 375, 330, 0, 0, 0);
draw_line(375, 330, 445, 420, 0, 0, 0);
draw_line(445, 420, 500, 420, 0, 0, 0);
draw_line(500, 420, 410, 300, 0, 0, 0);
draw_line(410, 300, 500, 180, 0, 0, 0);
draw_line(500, 180, 445, 180, 0, 0, 0);
draw_line(445, 180, 375, 270, 0, 0, 0);
draw_line(375, 270, 375, 180, 0, 0, 0);
draw_line(375, 180, 330, 180, 0, 0, 0);
// kolo
draw_circle(300, 300, 260, 0, 0, 0);
draw_circle(300, 300, 259, 0, 0, 0);
draw_circle(300, 300, 258, 0, 0, 0);
draw_circle(300, 300, 257, 0, 0, 0);
// wypelnianie kola 
flood_fill(300, 100, 45, 0, 0, 150, 0, 0);

// wypelnianie liter 
flood_fill(100, 300, 0, 0, 0, 150, 0, 0);
flood_fill(350, 300, 0, 0, 0, 150, 0, 0);
flood_fill(430, 250, 0, 0, 0, 150, 0, 0);
flood_fill(430, 370, 0, 0, 0, 150, 0, 0);

}



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

        return 0;
}
