/*
 * 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)
{
    FILE *fp = fopen(file_name, "wb");
    if (!fp)
        abort_("[write_png_file] File %s could not be opened for writing", file_name);

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

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

    if (setjmp(png_jmpbuf(png_ptr)))
        abort_("[write_png_file] Error during writing bytes");

    png_write_image(png_ptr, row_pointers);

    if (setjmp(png_jmpbuf(png_ptr)))
        abort_("[write_png_file] Error during end of write");

    png_write_end(png_ptr, NULL);

    for (y=0; y<height; y++)
        free(row_pointers[y]);
    free(row_pointers);

    fclose(fp);
}

void write_pixel(int px, int py, png_byte cr, png_byte cg, png_byte cb) {
    if (px < 0 || px >= width || py < 0 || py >= height) return; 
    png_byte* row = row_pointers[py];
    png_byte* ptr = &(row[px*3]);
    ptr[0] = cr;
    ptr[1] = cg;
    ptr[2] = cb;
}

void get_pixel(int px, int py, png_byte *cr, png_byte *cg, png_byte *cb) {
    if (px < 0 || px >= width || py < 0 || py >= height) return;
    png_byte* row = row_pointers[py];
    png_byte* ptr = &(row[px*3]);
    *cr = ptr[0]; 
    *cg = ptr[1]; 
    *cb = ptr[2];
}

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 (i1 > i2) {
        int tmp = i1; i1 = i2; i2 = tmp;
        tmp = j1; j1 = j2; j2 = tmp;
    }

    if( i2>i1 && j2>=j1 && j2-j1<=i2-i1) {
        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) {
        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) {
        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) {
        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 if (i1 == i2 && j1 == j2) {
        write_pixel(i1, j1, cr, cg, cb);
    }
}

void draw_polygon(int points[][2], int num_points, png_byte r, png_byte g, png_byte b) {
    for (int i = 0; i < num_points; i++) {
        int next = (i + 1) % num_points;
        bresenham(points[i][0], points[i][1], points[next][0], points[next][1], r, g, b);
    }
}

void draw_circle(int xc, int yc, int radius, png_byte r, png_byte g, png_byte b) {
    int px = 0;
    int py = radius;
    int d = 3 - 2 * radius;

    while (py >= px) {
        write_pixel(xc + px, yc + py, r, g, b);
        write_pixel(xc - px, yc + py, r, g, b);
        write_pixel(xc + px, yc - py, r, g, b);
        write_pixel(xc - px, yc - py, r, g, b);
        write_pixel(xc + py, yc + px, r, g, b);
        write_pixel(xc - py, yc + px, r, g, b);
        write_pixel(xc + py, yc - px, r, g, b);
        write_pixel(xc - py, yc - px, r, g, b);
        
        px++;
        if (d > 0) {
            py--;
            d = d + 4 * (px - py) + 10;
        } else {
            d = d + 4 * px + 6;
        }
    }
}

typedef struct { int x, y; } Point;

int colors_match(png_byte r1, png_byte g1, png_byte b1, png_byte r2, png_byte g2, png_byte b2) {
    return (r1 == r2 && g1 == g2 && b1 == b2);
}

void flood_fill(int start_x, int start_y, png_byte fill_r, png_byte fill_g, png_byte fill_b) {
    png_byte target_r, target_g, target_b;
    get_pixel(start_x, start_y, &target_r, &target_g, &target_b);

    if (colors_match(target_r, target_g, target_b, fill_r, fill_g, fill_b)) return;

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

    stack[++top] = (Point){start_x, start_y};

    while (top >= 0) {
        Point p = stack[top--];
        int px = p.x, py = p.y;

        if (px < 0 || px >= width || py < 0 || py >= height) continue;

        png_byte cr, cg, cb;
        get_pixel(px, py, &cr, &cg, &cb);

        if (colors_match(cr, cg, cb, target_r, target_g, target_b)) {
            write_pixel(px, py, fill_r, fill_g, fill_b);
            stack[++top] = (Point){px + 1, py};
            stack[++top] = (Point){px - 1, py};
            stack[++top] = (Point){px, py + 1};
            stack[++top] = (Point){px, py - 1};
        }
    }
    free(stack);
}

void process_file(void)
{
    for (y = 0; y < height; y++) {
        for (x = 0; x < width; x++) {
            write_pixel(x, y, 255, 255, 255);
        }
    }

    int poly_K[11][2] = {
        {105, 200}, {155, 200}, {155, 280}, {215, 200}, {275, 200},
        {185, 300}, {275, 400}, {215, 400}, {155, 320}, {155, 400}, {105, 400}
    };
    int poly_B_outer[9][2] = {
        {325, 200}, {445, 200}, {485, 230}, {485, 280}, {445, 300},
        {495, 320}, {495, 370}, {445, 400}, {325, 400}
    };
    int poly_B_hole1[6][2] = {
        {375, 240}, {425, 240}, {435, 250}, {435, 270}, {425, 280}, {375, 280}
    };
    int poly_B_hole2[6][2] = {
        {375, 320}, {435, 320}, {445, 330}, {445, 360}, {435, 370}, {375, 370}
    };

    draw_polygon(poly_K, 11, 0, 0, 0);
    draw_polygon(poly_B_outer, 9, 0, 0, 0);
    draw_polygon(poly_B_hole1, 6, 0, 0, 0);
    draw_polygon(poly_B_hole2, 6, 0, 0, 0);

    draw_circle(300, 300, 240, 0, 0, 0);

    flood_fill(5, 5, 152, 251, 152);
    flood_fill(300, 300, 30, 30, 30); 
    flood_fill(80, 300, 30, 30, 30); 
    flood_fill(125, 300, 0, 150, 0); 
    flood_fill(345, 250, 0, 150, 0); 
    flood_fill(345, 350, 0, 150, 0); 
    flood_fill(400, 260, 30, 30, 30);
    flood_fill(400, 345, 30, 30, 30);
}

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

    return 0;
}