/*
 * 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>
#include <math.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 * 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 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 bresenham(int x0, int y0, int x1, int y1, png_byte cr, png_byte cg, png_byte cb){
    int dx = abs(x1 - x0), sx = x0 < x1 ? 1 : -1;
    int dy = -abs(y1 - y0), sy = y0 < y1 ? 1 : -1;
    int err = dx + dy, e2;
    for (;;) {
        write_pixel(x0, y0, cr, cg, cb);
        if (x0 == x1 && y0 == y1) break;
        e2 = 2 * err;
        if (e2 >= dy) { err += dy; x0 += sx; }
        if (e2 <= dx) { err += dx; y0 += sy; }
    }
}

void draw_scaled_line(int x1, int y1, int x2, int y2, png_byte r, png_byte g, png_byte b) {
    bresenham(x1*4 + 100, y1*4 + 100, x2*4 + 100, y2*4 + 100, r, g, b);
}

void draw_ellipse(int xc, int yc, int rx, int ry, png_byte cr, png_byte cg, png_byte cb) {
    long long rx2 = (long long)rx * rx;
    long long ry2 = (long long)ry * ry;
    long long tworx2 = 2 * rx2;
    long long twory2 = 2 * ry2;
    long long p;
    long long x = 0;
    long long y = ry;
    long long px = 0;
    long long py = tworx2 * y;

    p = (long long)(ry2 - (rx2 * ry) + (0.25 * rx2));
    while (px < py) {
        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);
        x++;
        px += twory2;
        if (p < 0) p += ry2 + px;
        else { y--; py -= tworx2; p += ry2 + px - py; }
    }

    p = (long long)(ry2 * (x + 0.5) * (x + 0.5) + rx2 * (y - 1) * (y - 1) - rx2 * ry2);
    while (y >= 0) {
        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);
        y--;
        py -= tworx2;
        if (p > 0) p += rx2 - py;
        else { x++; px += twory2; p += rx2 - py + px; }
    }
}

int match_color(int px, int py, png_byte r, png_byte g, png_byte b) {
    if (px < 0 || px >= width || py < 0 || py >= height) return 0;
    png_byte* row = row_pointers[py];
    png_byte* ptr = &(row[px*3]);
    return (ptr[0] == r && ptr[1] == g && ptr[2] == b);
}

void flood_fill(int start_x, int start_y, png_byte fill_r, png_byte fill_g, png_byte fill_b) {
    if (start_x < 0 || start_x >= width || start_y < 0 || start_y >= height) return;
    png_byte* ptr = &(row_pointers[start_y][start_x*3]);
    png_byte target_r = ptr[0];
    png_byte target_g = ptr[1];
    png_byte target_b = ptr[2];

    if (target_r == fill_r && target_g == fill_g && target_b == fill_b) return;

    int max_stack = width * height * 4;
    int *stack_x = malloc(max_stack * sizeof(int));
    int *stack_y = malloc(max_stack * sizeof(int));
    int top = 0;

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

    while (top > 0) {
        top--;
        int cx = stack_x[top];
        int cy = stack_y[top];

        if (cx < 0 || cx >= width || cy < 0 || cy >= height) continue;

        if (match_color(cx, cy, target_r, target_g, target_b)) {
            write_pixel(cx, cy, fill_r, fill_g, fill_b);
            
            if (top + 4 >= max_stack) break; 

            stack_x[top] = cx + 1; stack_y[top] = cy; top++;
            stack_x[top] = cx - 1; stack_y[top] = cy; top++;
            stack_x[top] = cx; stack_y[top] = cy + 1; top++;
            stack_x[top] = cx; stack_y[top] = cy - 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]);
            ptr[0] = 200; 
            ptr[1] = 200;
            ptr[2] = 200;
        }
    }

    png_byte r = 0, g = 0, b = 0;

    // --- RD ---
    
    // R
    draw_scaled_line(10, 10, 10, 90, r, g, b);
    draw_scaled_line(10, 90, 20, 90, r, g, b);
    draw_scaled_line(20, 90, 20, 50, r, g, b);
    draw_scaled_line(20, 50, 35, 90, r, g, b);
    draw_scaled_line(35, 90, 45, 90, r, g, b);
    draw_scaled_line(45, 90, 30, 50, r, g, b);
    draw_scaled_line(30, 50, 45, 45, r, g, b);
    draw_scaled_line(45, 45, 45, 15, r, g, b);
    draw_scaled_line(45, 15, 20, 10, r, g, b);
    draw_scaled_line(20, 10, 10, 10, r, g, b);

    // Otwór w R
    draw_scaled_line(20, 20, 35, 25, r, g, b);
    draw_scaled_line(35, 25, 35, 35, r, g, b);
    draw_scaled_line(35, 35, 20, 40, r, g, b);
    draw_scaled_line(20, 40, 20, 20, r, g, b);

    // D
    draw_scaled_line(60, 10, 60, 90, r, g, b);
    draw_scaled_line(60, 90, 75, 90, r, g, b);
    draw_scaled_line(75, 90, 95, 70, r, g, b);
    draw_scaled_line(95, 70, 95, 30, r, g, b);
    draw_scaled_line(95, 30, 75, 10, r, g, b);
    draw_scaled_line(75, 10, 60, 10, r, g, b);

    // Otwór w D
    draw_scaled_line(70, 20, 70, 80, r, g, b);
    draw_scaled_line(70, 80, 75, 80, r, g, b); 
    draw_scaled_line(75, 80, 85, 65, r, g, b); 
    draw_scaled_line(85, 65, 85, 35, r, g, b);
    draw_scaled_line(85, 35, 75, 20, r, g, b); 
    draw_scaled_line(75, 20, 70, 20, r, g, b);

    // --- okrąg ---
    int circle_cx = 52 * 4 + 100;
    int circle_cy = 50 * 4 + 100;
    int circle_r  = 55 * 4;
    draw_ellipse(circle_cx, circle_cy, circle_r, circle_r, r, g, b);

    // --- Wypełnianie ---
    // R
    flood_fill(15 * 4 + 100, 70 * 4 + 100, 255, 192, 203); 
    
    // D
    flood_fill(65 * 4 + 100, 50 * 4 + 100,  255, 192, 203); 

    // okrąg
    flood_fill(52 * 4 + 100, 5 * 4 + 100, 85, 141, 65); 
}

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