/*
 * 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();
}

typedef struct
{
    int x;
    int y;
} Vertex;

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 * 3 * sizeof(png_byte));
}

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 x, int y, png_byte cr, png_byte cg, png_byte cb)
{
    if (x < 0 || x >= width || y < 0 || y >= height)
        return;

    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 (i1 > i2)
    {
        int tempI = i1;
        i1 = i2;
        i2 = tempI;

        int tempJ = j1;
        j1 = j2;
        j2 = tempJ;
    }

    int m, b, j, P, i;

    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 = -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 byc nie moze!\n");
    }
}

void draw_closed_shape(Vertex pts[], int n, png_byte cr, png_byte cg, png_byte cb)
{
    int i;
    for (i = 0; i < n; i++)
    {
        Vertex a = pts[i];
        Vertex b = pts[(i + 1) % n];
        bresenham(a.x, a.y, b.x, b.y, cr, cg, cb);
    }
}

void circle_pixels(int is, int js, int i, int j, png_byte cr, png_byte cg, png_byte cb)
{
    write_pixel(is + i, js + j, cr, cg, cb);
    write_pixel(is + j, js + i, cr, cg, cb);
    write_pixel(is + i, js - j, cr, cg, cb);
    write_pixel(is + j, js - i, cr, cg, cb);

    write_pixel(is - i, js + j, cr, cg, cb);
    write_pixel(is - j, js + i, cr, cg, cb);
    write_pixel(is - i, js - j, cr, cg, cb);
    write_pixel(is - j, js - i, cr, cg, cb);
}

void circle(int is, int js, int R, png_byte cr, png_byte cg, png_byte cb)
{
    int i, j, f;

    i = 0;
    j = R;
    f = 5 - 4 * R;

    circle_pixels(is, js, i, j, 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;
        circle_pixels(is, js, i, j, cr, cg, cb);
    }
}

int inside(int i, int j)
{
    return i >= 0 && i < width && j >= 0 && j < height;
}

int pixel_is_color(int i, int j, png_byte r, png_byte g, png_byte b)
{
    png_byte *ptr = &(row_pointers[j][i * 3]);
    return ptr[0] == r && ptr[1] == g && ptr[2] == b;
}

void flood_fill4(int i, int j, png_byte nr, png_byte ng, png_byte nb)
{
    typedef struct
    {
        int i;
        int j;
    } Point;

    png_byte *ptr;
    png_byte sr, sg, sb;
    Point *stack;
    int max_points;
    int top;

    if (!inside(i, j))
        return;

    ptr = &(row_pointers[j][i * 3]);
    sr = ptr[0];
    sg = ptr[1];
    sb = ptr[2];

    if (sr == nr && sg == ng && sb == nb)
        return;

    max_points = width * height;
    stack = (Point *)malloc(sizeof(Point) * max_points);
    if (!stack)
        abort_("not enoguh ememory flood fil");

    top = 0;
    stack[top++] = (Point){i, j};

    while (top > 0)
    {
        Point p = stack[--top];

        if (!inside(p.i, p.j))
            continue;

        if (!pixel_is_color(p.i, p.j, sr, sg, sb))
            continue;

        write_pixel(p.i, p.j, nr, ng, nb);

        if (top + 4 >= max_points)
        {
            free(stack);
            abort_("overflow");
        }

        stack[top++] = (Point){p.i - 1, p.j};
        stack[top++] = (Point){p.i, p.j - 1};
        stack[top++] = (Point){p.i + 1, p.j};
        stack[top++] = (Point){p.i, p.j + 1};
    }

    free(stack);
}

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] = 235;
            ptr[1] = 0;
            ptr[2] = 0;
        }
    }

    circle(300, 300, 290, 20, 20, 20);

    bresenham(100, 130, 230, 130, 20, 20, 20);
    bresenham(230, 130, 290, 180, 20, 20, 20);
    bresenham(290, 180, 290, 255, 20, 20, 20);
    bresenham(290, 255, 240, 305, 20, 20, 20);
    bresenham(240, 305, 175, 305, 20, 20, 20);
    bresenham(175, 305, 305, 480, 20, 20, 20);
    bresenham(305, 480, 240, 480, 20, 20, 20);
    bresenham(240, 480, 160, 340, 20, 20, 20);
    bresenham(160, 340, 150, 340, 20, 20, 20);
    bresenham(150, 340, 150, 480, 20, 20, 20);
    bresenham(150, 480, 100, 480, 20, 20, 20);
    bresenham(100, 480, 100, 130, 20, 20, 20);
    bresenham(180, 185, 235, 185, 20, 20, 20);
    bresenham(235, 185, 255, 205, 20, 20, 20);
    bresenham(255, 205, 255, 245, 20, 20, 20);
    bresenham(255, 245, 235, 265, 20, 20, 20);
    bresenham(235, 265, 180, 265, 20, 20, 20);
    bresenham(180, 265, 180, 185, 20, 20, 20);
    bresenham(330, 130, 385, 130, 20, 20, 20);
    bresenham(385, 130, 385, 245, 20, 20, 20);
    bresenham(385, 245, 465, 130, 20, 20, 20);
    bresenham(465, 130, 520, 130, 20, 20, 20);
    bresenham(520, 130, 430, 285, 20, 20, 20);
    bresenham(430, 285, 520, 480, 20, 20, 20);
    bresenham(520, 480, 465, 480, 20, 20, 20);
    bresenham(465, 480, 385, 340, 20, 20, 20);
    bresenham(385, 340, 385, 480, 20, 20, 20);
    bresenham(385, 480, 330, 480, 20, 20, 20);
    bresenham(330, 480, 330, 130, 20, 20, 20);
    flood_fill4(300, 300, 210, 255, 245);
    flood_fill4(120, 200, 80, 160, 255);
    flood_fill4(210, 220, 210, 255, 245);
    flood_fill4(360, 220, 80, 160, 255);
}

int main(int argc, char **argv)
{
    create_png_file();
    process_file();
    write_png_file(OUT_FILE);

    return 0;
}