/*
 * 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);
}


//write_pixel

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) {
        png_byte* row = row_pointers[y];
        png_byte* ptr = &(row[x*3]);
        ptr[0] = cr;
        ptr[1] = cg;
        ptr[2] = cb;
    }
}

// Algorytm Bresenhama 
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) {
        bresenham(i2, j2, i1, j1, cr, cg, cb);
        return;
    }

   
    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) {
            int start_j = j1 < j2 ? j1 : j2;
            int end_j = j1 > j2 ? j1 : j2;
            for (j = start_j; j <= end_j; j++) write_pixel(i1, j, cr, cg, cb);
        } else if (j1 == j2) {
            for (i = i1; i <= i2; i++) write_pixel(i, j1, cr, cg, cb);
        }
    }
}


// Osiem symetrii okręgu

void osiem_symetrii(int srodek_i, int srodek_j, int i, int j, png_byte cr, png_byte cg, png_byte cb) {
    write_pixel(srodek_i + i, srodek_j + j, cr, cg, cb);
    write_pixel(srodek_i - i, srodek_j + j, cr, cg, cb);
    write_pixel(srodek_i + i, srodek_j - j, cr, cg, cb);
    write_pixel(srodek_i - i, srodek_j - j, cr, cg, cb);
    write_pixel(srodek_i + j, srodek_j + i, cr, cg, cb);
    write_pixel(srodek_i - j, srodek_j + i, cr, cg, cb);
    write_pixel(srodek_i + j, srodek_j - i, cr, cg, cb);
    write_pixel(srodek_i - j, srodek_j - i, cr, cg, cb);
}


// Algorytm rasteryzacji okręgu

void rasteryzuj_okrag(int srodek_i, int srodek_j, int R, png_byte cr, png_byte cg, png_byte cb) {
    int i = 0;
    int j = R;
    int f = 5 - 4 * R;

    osiem_symetrii(srodek_i, srodek_j, 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;
        osiem_symetrii(srodek_i, srodek_j, i, j, cr, cg, cb);
    }
}


// Wypełnianie kolorem 

int stos_x[600 * 600];
int stos_y[600 * 600];

void flood_fill_4(int start_x, int start_y, png_byte cr, png_byte cg, png_byte cb) {
    int top = 0;
    
  
    png_byte* r_start = &(row_pointers[start_y][start_x*3]);
    png_byte tlo_r = r_start[0];
    png_byte tlo_g = r_start[1];
    png_byte tlo_b = r_start[2];

    
    if (tlo_r == cr && tlo_g == cg && tlo_b == cb) return;

   
    stos_x[top] = start_x;
    stos_y[top] = start_y;
    top++;

    while (top > 0) {
        top--; 
        int cx = stos_x[top];
        int cy = stos_y[top];

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

        png_byte* ptr = &(row_pointers[cy][cx*3]);
        
     
        if (ptr[0] == tlo_r && ptr[1] == tlo_g && ptr[2] == tlo_b) {
            ptr[0] = cr;
            ptr[1] = cg;
            ptr[2] = cb;

            stos_x[top] = cx + 1; stos_y[top] = cy; top++; // Prawo
            stos_x[top] = cx - 1; stos_y[top] = cy; top++; // Lewo
            stos_x[top] = cx; stos_y[top] = cy + 1; top++; // Dół
            stos_x[top] = cx; stos_y[top] = cy - 1; top++; // Góra
        }
    }
}


// Główna funkcja procesująca plik

void process_file(void)
{
    // KROK 0: Czyszczenie tła na biało
    for (y=0; y<height; y++) {
        for (x=0; x<width; x++) {
            write_pixel(x, y, 255, 255, 255);
        }
    }

    // KROK 1: Rysowanie okręgu 
    // Środek na (300, 300), promień 270, czarny kolor (0,0,0)
    rasteryzuj_okrag(300, 300, 270, 0, 0, 0);

    // KROK 2: Rysowanie inicjałów 
    
    // Rysowanie zarysu litery P
    bresenham(135, 420, 135, 180, 0, 0, 0);
    bresenham(135, 180, 225, 180, 0, 0, 0);
    bresenham(225, 180, 300, 240, 0, 0, 0);
    bresenham(300, 240, 225, 300, 0, 0, 0);
    bresenham(225, 300, 180, 300, 0, 0, 0);
    bresenham(180, 300, 180, 420, 0, 0, 0);
    bresenham(180, 420, 135, 420, 0, 0, 0);
    
    // Rysowanie dziurki w literze P
    bresenham(180, 225, 225, 225, 0, 0, 0);
    bresenham(225, 225, 250, 240, 0, 0, 0);
    bresenham(250, 240, 225, 255, 0, 0, 0);
    bresenham(225, 255, 180, 255, 0, 0, 0);
    bresenham(180, 255, 180, 225, 0, 0, 0);

    // Rysowanie zarysu litery C
    bresenham(540, 210, 420, 180, 0, 0, 0);
    bresenham(420, 180, 300, 300, 0, 0, 0);
    bresenham(300, 300, 420, 420, 0, 0, 0);
    bresenham(420, 420, 540, 390, 0, 0, 0);
    bresenham(540, 390, 510, 366, 0, 0, 0);
    bresenham(510, 366, 420, 390, 0, 0, 0);
    bresenham(420, 390, 345, 300, 0, 0, 0);
    bresenham(345, 300, 420, 210, 0, 0, 0);
    bresenham(420, 210, 510, 234, 0, 0, 0);
    bresenham(510, 234, 540, 210, 0, 0, 0);

    // KROK 3: Wypełnianie kolorem 
    
    //  Magenta (255, 0, 255)
    flood_fill_4(10, 10, 255, 0, 255);
    
    //  Zielony (0, 255, 0)
    flood_fill_4(300, 50, 0, 255, 0);
    
    //  Cyjanowyd(0, 255, 255)
    flood_fill_4(150, 350, 0, 255, 255);
    
    //Żółty (255, 255, 0)
    flood_fill_4(200, 240, 255, 255, 0);
    
    // Wypełniamy literę C kolorem Cyjanowy (0, 255, 255)
  
    flood_fill_4(320, 300, 0, 255, 255);
}
int main(int argc, char **argv)
{
    create_png_file();
    process_file();
    write_png_file(OUT_FILE);

    return 0;
}

