//-----------------------------------------------------------------------------
// Fraktaly v pocitacove grafice
// Ukazkovy priklad cislo 15.1
// Autor: Pavel Tisnovsky
//
// Vykresleni Mandelbrotovy mnoziny s moznosti zmeny pohledu na mnozinu a
// zmeny pouzite barvove palety.
// Pixmapu s vykreslenym fraktalem je mozne ulozit do souboru ve formatu TGA
// pomoci klavesove zkratky [S].
// Ukonceni aplikace se provede klavesou [Esc] nebo klavesou [Q].
//-----------------------------------------------------------------------------

#ifdef __BORLANDC__
#include <windows.h>
#endif

#include <GL/glut.h>                            // hlavickovy soubor funkci GLUTu a OpenGL
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

#define WINDOW_TITLE    "Fraktaly 15.1"         // titulek okna
#define WINDOW_WIDTH    512                     // pocatecni velikost okna
#define WINDOW_HEIGHT   384
#define FILE_NAME       "fraktaly151.tga"       // jmeno souboru pro ulozeni pixmapy

#define PIXMAP_WIDTH    512                     // sirka pixmapy
#define PIXMAP_HEIGHT   384                     // vyska pixmapy

typedef struct {                                // struktura popisujici pixmapu
    unsigned int width;
    unsigned int height;
    unsigned char *pixels;
} pixmap;

pixmap *pix;

int    palette=0;                               // barvova paleta
int    maxiter=128;                             // maximalni pocet iteraci
int    rf=1, gf=1, bf=1;                        // priznaky barvovych slozek
double scale=124.04;                            // meritko fraktalu
double xpos=-0.014;                             // posun fraktalu
double ypos=-0.714;                             // posun fraktalu

// sirka a vyska zakladniho obrazce v komplexni rovine
#define WIDTH   2.0
#define HEIGHT  1.5



//-----------------------------------------------------------------------------
// Funkce pro vytvoreni pixmapy o zadane velikosti
//-----------------------------------------------------------------------------
pixmap * createPixmap(const unsigned int width, const unsigned int height)
{
    pixmap *p=(pixmap *)malloc(sizeof(pixmap)); // alokace struktury pixmapy
    if (!p) return NULL;
    p->width=width;                             // naplneni struktury
    p->height=height;
    p->pixels=(unsigned char *)malloc(3*width*height);
    if (!p->pixels) {                           // alokace pole pro pixely
        free(p);                                // alokace se nepovedla
        return NULL;
    }
    else {
        memset(p->pixels, 0, 3*width*height);   // smazani pixmapy
    }
    return p;
}



//-----------------------------------------------------------------------------
// Funkce pro zruseni pixmapy
//-----------------------------------------------------------------------------
void destroyPixmap(pixmap *p)
{
    if (p->pixels) free(p->pixels);             // uvolnit vlastni pixmapu
    if (p) free(p);                             // i okolni strukturu
}



//-----------------------------------------------------------------------------
// Vymazani pixmapy
//-----------------------------------------------------------------------------
void clearPixmap(const pixmap *p)
{
    if (!p) return;
    if (!p->pixels) return;
    memset(p->pixels, 0, 3*p->width*p->height);
}



//-----------------------------------------------------------------------------
// Funkce pro vykresleni pixmapy do barvoveho bufferu
//-----------------------------------------------------------------------------
void drawPixmap(const pixmap *p)
{
    if (!p || !p->pixels) return;
    glDrawPixels(                               // vykresleni pixmapy
            p->width, p->height,                // sirka a vyska pixmapy
            GL_RGB,                             // format dat pixelu
            GL_UNSIGNED_BYTE,                   // datovy typ kazde barevne slozky
            p->pixels);                         // ukazatel na pamet s barvami pixelu
}



//-----------------------------------------------------------------------------
// Ulozeni pixmapy do externiho souboru
//-----------------------------------------------------------------------------
void savePixmap(const pixmap *p, const char *fileName)
{
    FILE *fout;
    unsigned int i, j, k;
    unsigned char tgaHeader[18]={               // hlavicka formatu typu TGA
                        0x00,                   // typ hlavicky TGA
                        0x00,                   // nepouzivame paletu
                        0x02,                   // typ obrazku je RGB TrueColor
                        0x00, 0x00,             // delka palety je nulova
                        0x00, 0x00, 0x00,       // pozice v palete nas nezajima
                        0x00, 0x00, 0x00, 0x00, // obrazek je umisteny na pozici [0, 0]
                        0x00, 0x00, 0x00, 0x00, // sirka a vyska obrazku (dva byty na polozku)
                        0x18,                   // format je 24 bitu na pixel
                        0x20                    // orientace bitmapy v obrazku
    };
    if (!p || !p->pixels) return;
    memcpy(tgaHeader+12, &(p->width), 2);       // do hlavicky TGA zapsat velikost obrazku
    memcpy(tgaHeader+14, &(p->height), 2);
    fout=fopen(fileName, "wb");
    if (fout) {
        fwrite(tgaHeader, 18, 1, fout);         // zapsat hlavicku TGA do souboru
        for (j=p->height; j; j--) {             // radky zapisovat v opacnem poradi
            unsigned int yoff=(j-1)*3*p->width; // y-ovy offset v poli
            unsigned int xoff=0;                // x-ovy offset v poli
            for (i=0; i<p->width; i++) {        // pro kazdy pixel na radku
                for (k=0; k<3; k++) {           // prohodit barvy RGB na BGR
                    fputc(p->pixels[xoff+yoff+2-k], fout); // a zapsat do souboru
                }
                xoff+=3;                        // posun na dalsi pixel
            }
        }
        fclose(fout);
    }
}



//-----------------------------------------------------------------------------
// Zmena barvy pixelu na zadanych souradnicich
//-----------------------------------------------------------------------------
void putpixel(pixmap *p,
              const unsigned int x,             // pozice pixelu v pixmape
              const unsigned int y,
              const unsigned char r,            // barva pixelu
              const unsigned char g,
              const unsigned char b)
{
    int pos;
    // zde se vyuziva zkraceneho vyhodnoceni vyrazu - pokud plati !p, nic se dale netestuje
    if (!p || !p->pixels || x>=p->width || y>=p->height) return;
    pos=3*(x+y*p->width);
    p->pixels[pos++]=r;                         // nastaveni barvy pixelu
    p->pixels[pos++]=g;
    p->pixels[pos]=b;
}



//-----------------------------------------------------------------------------
// Funkce pro vypocet pozice rohu vykreslovaneho obrazku ze zadaneho stredu
// a meritka
//-----------------------------------------------------------------------------
void calcCorner(double xpos, double ypos, double scale,
                double *xmin,  double *ymin,  double *xmax, double *ymax)
{
    *xmin=xpos-WIDTH/scale;
    *ymin=ypos-HEIGHT/scale;
    *xmax=xpos+WIDTH/scale;
    *ymax=ypos+HEIGHT/scale;
}



//-----------------------------------------------------------------------------
// Vypocet barvy pixelu podle zvolene barvove palety
//-----------------------------------------------------------------------------
void mapPalette(int palette, int iter, char *r, char *g, char *b)
{
    char (*p)[256][3];                  // ukazatel na vybranou paletu
    static char blues[256][3]={
        {  0,   0,   0}, {  0,   0,   0}, {  0,   0,   4}, {  0,   0,  12},
        {  0,   0,  16}, {  0,   0,  24}, {  0,   0,  32}, {  0,   0,  36},
        {  0,   0,  44}, {  0,   0,  48}, {  0,   0,  56}, {  0,   0,  64},
        {  0,   0,  68}, {  0,   0,  76}, {  0,   0,  80}, {  0,   0,  88},
        {  0,   0,  96}, {  0,   0, 100}, {  0,   0, 108}, {  0,   0, 116},
        {  0,   0, 120}, {  0,   0, 128}, {  0,   0, 132}, {  0,   0, 140},
        {  0,   0, 148}, {  0,   0, 152}, {  0,   0, 160}, {  0,   0, 164},
        {  0,   0, 172}, {  0,   0, 180}, {  0,   0, 184}, {  0,   0, 192},
        {  0,   0, 200}, {  0,   4, 200}, {  0,  12, 200}, {  0,  16, 204},
        {  0,  24, 204}, {  0,  28, 208}, {  0,  36, 208}, {  0,  40, 208},
        {  0,  48, 212}, {  0,  56, 212}, {  0,  60, 216}, {  0,  68, 216},
        {  0,  72, 216}, {  0,  80, 220}, {  0,  84, 220}, {  0,  92, 224},
        {  0, 100, 224}, {  0, 104, 224}, {  0, 112, 228}, {  0, 116, 228},
        {  0, 124, 232}, {  0, 128, 232}, {  0, 136, 232}, {  0, 140, 236},
        {  0, 148, 236}, {  0, 156, 240}, {  0, 160, 240}, {  0, 168, 240},
        {  0, 172, 244}, {  0, 180, 244}, {  0, 184, 248}, {  0, 192, 248},
        {  0, 200, 252}, {  4, 200, 252}, { 12, 200, 252}, { 20, 204, 252},
        { 28, 204, 252}, { 36, 208, 252}, { 44, 208, 252}, { 52, 208, 252},
        { 60, 212, 252}, { 68, 212, 252}, { 76, 216, 252}, { 84, 216, 252},
        { 92, 216, 252}, {100, 220, 252}, {108, 220, 252}, {116, 224, 252},
        {124, 224, 252}, {132, 224, 252}, {140, 228, 252}, {148, 228, 252},
        {156, 232, 252}, {164, 232, 252}, {172, 232, 252}, {180, 236, 252},
        {188, 236, 252}, {196, 240, 252}, {204, 240, 252}, {212, 240, 252},
        {220, 244, 252}, {228, 244, 252}, {236, 248, 252}, {244, 248, 252},
        {252, 252, 252}, {248, 252, 252}, {244, 252, 252}, {240, 252, 252},
        {232, 252, 252}, {228, 252, 252}, {224, 252, 252}, {216, 252, 252},
        {212, 252, 252}, {208, 252, 252}, {200, 252, 252}, {196, 252, 252},
        {192, 252, 252}, {184, 252, 252}, {180, 252, 252}, {176, 252, 252},
        {168, 252, 252}, {164, 252, 252}, {160, 252, 252}, {156, 252, 252},
        {148, 252, 252}, {144, 252, 252}, {140, 252, 252}, {132, 252, 252},
        {128, 252, 252}, {124, 252, 252}, {116, 252, 252}, {112, 252, 252},
        {108, 252, 252}, {100, 252, 252}, { 96, 252, 252}, { 92, 252, 252},
        { 84, 252, 252}, { 80, 252, 252}, { 76, 252, 252}, { 72, 252, 252},
        { 64, 252, 252}, { 60, 252, 252}, { 56, 252, 252}, { 48, 252, 252},
        { 44, 252, 252}, { 40, 252, 252}, { 32, 252, 252}, { 28, 252, 252},
        { 24, 252, 252}, { 16, 252, 252}, { 12, 252, 252}, {  8, 252, 252},
        {  0, 252, 252}, {  0, 248, 252}, {  0, 244, 252}, {  0, 240, 252},
        {  0, 232, 252}, {  0, 228, 252}, {  0, 224, 252}, {  0, 216, 252},
        {  0, 212, 252}, {  0, 208, 252}, {  0, 200, 252}, {  0, 196, 252},
        {  0, 192, 252}, {  0, 184, 252}, {  0, 180, 252}, {  0, 176, 252},
        {  0, 168, 252}, {  0, 164, 252}, {  0, 160, 252}, {  0, 156, 252},
        {  0, 148, 252}, {  0, 144, 252}, {  0, 140, 252}, {  0, 132, 252},
        {  0, 128, 252}, {  0, 124, 252}, {  0, 116, 252}, {  0, 112, 252},
        {  0, 108, 252}, {  0, 100, 252}, {  0,  96, 252}, {  0,  92, 252},
        {  0,  84, 252}, {  0,  80, 252}, {  0,  76, 252}, {  0,  72, 252},
        {  0,  64, 252}, {  0,  60, 252}, {  0,  56, 252}, {  0,  48, 252},
        {  0,  44, 252}, {  0,  40, 252}, {  0,  32, 252}, {  0,  28, 252},
        {  0,  24, 252}, {  0,  16, 252}, {  0,  12, 252}, {  0,   8, 252},
        {  0,   0, 252}, {  0,   0, 248}, {  0,   0, 244}, {  0,   0, 240},
        {  0,   0, 236}, {  0,   0, 232}, {  0,   0, 228}, {  0,   0, 224},
        {  0,   0, 220}, {  0,   0, 216}, {  0,   0, 212}, {  0,   0, 208},
        {  0,   0, 204}, {  0,   0, 200}, {  0,   0, 196}, {  0,   0, 192},
        {  0,   0, 188}, {  0,   0, 184}, {  0,   0, 180}, {  0,   0, 176},
        {  0,   0, 172}, {  0,   0, 168}, {  0,   0, 164}, {  0,   0, 160},
        {  0,   0, 156}, {  0,   0, 152}, {  0,   0, 148}, {  0,   0, 144},
        {  0,   0, 140}, {  0,   0, 136}, {  0,   0, 132}, {  0,   0, 128},
        {  0,   0, 124}, {  0,   0, 120}, {  0,   0, 116}, {  0,   0, 112},
        {  0,   0, 108}, {  0,   0, 104}, {  0,   0, 100}, {  0,   0,  96},
        {  0,   0,  92}, {  0,   0,  88}, {  0,   0,  84}, {  0,   0,  80},
        {  0,   0,  76}, {  0,   0,  72}, {  0,   0,  68}, {  0,   0,  64},
        {  0,   0,  60}, {  0,   0,  56}, {  0,   0,  52}, {  0,   0,  48},
        {  0,   0,  44}, {  0,   0,  40}, {  0,   0,  36}, {  0,   0,  32},
        {  0,   0,  28}, {  0,   0,  24}, {  0,   0,  20}, {  0,   0,  16},
        {  0,   0,  12}, {  0,   0,   8}, {  0,   0,   0}, {  0,   0,   0}
    };
    static char gold[256][3]={
        {  0,   0,   0}, {252, 252, 128}, {252, 252, 128}, {252, 248, 124},
        {252, 248, 124}, {252, 244, 120}, {248, 244, 120}, {248, 240, 116},
        {248, 240, 112}, {248, 236, 112}, {244, 236, 108}, {244, 232, 108},
        {244, 232, 104}, {244, 228, 104}, {240, 228, 100}, {240, 224,  96},
        {240, 224,  96}, {240, 220,  92}, {236, 220,  92}, {236, 216,  88},
        {236, 216,  84}, {236, 212,  84}, {236, 212,  80}, {232, 208,  80},
        {232, 208,  76}, {232, 204,  76}, {232, 204,  72}, {228, 200,  68},
        {228, 200,  68}, {228, 196,  64}, {228, 196,  64}, {224, 192,  60},
        {224, 192,  56}, {224, 188,  56}, {224, 188,  52}, {220, 184,  52},
        {220, 184,  48}, {220, 180,  48}, {220, 180,  44}, {220, 176,  40},
        {216, 176,  40}, {216, 172,  36}, {216, 172,  36}, {216, 168,  32},
        {212, 168,  28}, {212, 164,  28}, {212, 164,  24}, {212, 160,  24},
        {208, 160,  20}, {208, 156,  20}, {208, 156,  16}, {208, 152,  12},
        {204, 152,  12}, {204, 148,   8}, {204, 148,   8}, {204, 144,   4},
        {200, 140,   0}, {196, 136,   0}, {196, 136,   0}, {196, 136,   0},
        {196, 136,   0}, {192, 132,   0}, {192, 132,   0}, {192, 132,   0},
        {192, 132,   0}, {188, 128,   0}, {188, 128,   0}, {188, 128,   0},
        {188, 128,   0}, {184, 124,   0}, {184, 124,   0}, {184, 124,   0},
        {184, 124,   0}, {180, 120,   0}, {180, 120,   0}, {180, 120,   0},
        {180, 120,   0}, {176, 116,   0}, {176, 116,   0}, {176, 116,   0},
        {176, 116,   0}, {172, 112,   0}, {172, 112,   0}, {172, 112,   0},
        {172, 112,   0}, {168, 108,   0}, {168, 108,   0}, {168, 108,   0},
        {168, 108,   0}, {164, 104,   0}, {164, 104,   0}, {164, 104,   0},
        {164, 104,   0}, {160, 100,   0}, {160, 100,   0}, {160, 100,   0},
        {160, 100,   0}, {156,  96,   0}, {156,  96,   0}, {156,  96,   0},
        {156,  96,   0}, {152,  92,   0}, {152,  92,   0}, {152,  92,   0},
        {152,  92,   0}, {148,  88,   0}, {148,  88,   0}, {148,  88,   0},
        {148,  88,   0}, {144,  84,   0}, {144,  84,   0}, {144,  84,   0},
        {144,  84,   0}, {140,  80,   0}, {140,  80,   0}, {140,  80,   0},
        {140,  80,   0}, {136,  76,   0}, {136,  76,   0}, {136,  76,   0},
        {136,  76,   0}, {132,  72,   0}, {132,  72,   0}, {132,  72,   0},
        {132,  72,   0}, {128,  68,   0}, {128,  68,   0}, {128,  68,   0},
        {128,  68,   0}, {124,  64,   0}, {124,  64,   0}, {124,  64,   0},
        {124,  64,   0}, {120,  60,   0}, {120,  60,   0}, {120,  60,   0},
        {120,  60,   0}, {116,  56,   0}, {116,  56,   0}, {116,  56,   0},
        {116,  56,   0}, {112,  52,   0}, {112,  52,   0}, {112,  52,   0},
        {112,  52,   0}, {108,  48,   0}, {108,  48,   0}, {108,  48,   0},
        {108,  48,   0}, {104,  44,   0}, {104,  44,   0}, {104,  44,   0},
        {104,  44,   0}, {100,  40,   0}, {100,  40,   0}, {100,  40,   0},
        {100,  40,   0}, { 96,  36,   0}, { 96,  36,   0}, { 96,  36,   0},
        { 96,  36,   0}, { 92,  32,   0}, { 92,  32,   0}, { 92,  32,   0},
        { 92,  32,   0}, { 88,  28,   0}, { 88,  28,   0}, { 88,  28,   0},
        { 88,  28,   0}, { 84,  24,   0}, { 84,  24,   0}, { 84,  24,   0},
        { 84,  24,   0}, { 80,  20,   0}, { 80,  20,   0}, { 80,  20,   0},
        { 80,  20,   0}, { 76,  16,   0}, { 76,  16,   0}, { 76,  16,   0},
        { 76,  16,   0}, { 72,  12,   0}, { 72,  12,   0}, { 72,  12,   0},
        { 72,  12,   0}, { 68,   8,   0}, { 68,   8,   0}, { 68,   8,   0},
        { 68,   8,   0}, { 64,   4,   0}, { 64,   4,   0}, { 64,   4,   0},
        { 64,   4,   0}, { 60,   0,   0}, { 60,   0,   0}, { 60,   0,   0},
        { 60,   0,   0}, { 56,   0,   0}, { 56,   0,   0}, { 56,   0,   0},
        { 56,   0,   0}, { 52,   0,   0}, { 52,   0,   0}, { 52,   0,   0},
        { 52,   0,   0}, { 48,   0,   0}, { 48,   0,   0}, { 48,   0,   0},
        { 48,   0,   0}, { 44,   0,   0}, { 44,   0,   0}, { 44,   0,   0},
        { 44,   0,   0}, { 40,   0,   0}, { 40,   0,   0}, { 40,   0,   0},
        { 40,   0,   0}, { 36,   0,   0}, { 36,   0,   0}, { 36,   0,   0},
        { 36,   0,   0}, { 32,   0,   0}, { 32,   0,   0}, { 32,   0,   0},
        { 32,   0,   0}, { 28,   0,   0}, { 28,   0,   0}, { 28,   0,   0},
        { 28,   0,   0}, { 24,   0,   0}, { 24,   0,   0}, { 24,   0,   0},
        { 24,   0,   0}, { 20,   0,   0}, { 20,   0,   0}, { 20,   0,   0},
        { 20,   0,   0}, { 16,   0,   0}, { 16,   0,   0}, { 16,   0,   0},
        { 16,   0,   0}, { 12,   0,   0}, { 12,   0,   0}, { 12,   0,   0},
        { 12,   0,   0}, {  8,   0,   0}, {  8,   0,   0}, {  8,   0,   0},
        {  8,   0,   0}, {  4,   0,   0}, {  4,   0,   0}, {  4,   0,   0},
        {  4,   0,   0}, {  0,   0,   0}, {  0,   0,   0}, {  0,   0,   0}
    };
    static char greens[256][3]={
        {  0,   0,   0}, {  0,   0,   0}, {  0,   4,   0}, {  0,  12,   0},
        {  0,  16,   0}, {  0,  24,   0}, {  0,  32,   0}, {  0,  36,   0},
        {  0,  44,   0}, {  0,  48,   0}, {  0,  56,   0}, {  0,  64,   0},
        {  0,  68,   0}, {  0,  76,   0}, {  0,  80,   0}, {  0,  88,   0},
        {  0,  96,   0}, {  0, 100,   0}, {  0, 108,   0}, {  0, 116,   0},
        {  0, 120,   0}, {  0, 128,   0}, {  0, 132,   0}, {  0, 140,   0},
        {  0, 148,   0}, {  0, 152,   0}, {  0, 160,   0}, {  0, 164,   0},
        {  0, 172,   0}, {  0, 180,   0}, {  0, 184,   0}, {  0, 192,   0},
        {  0, 200,   0}, {  4, 200,   0}, { 12, 200,   0}, { 16, 204,   0},
        { 24, 204,   0}, { 28, 208,   0}, { 36, 208,   0}, { 40, 208,   0},
        { 48, 212,   0}, { 56, 212,   0}, { 60, 216,   0}, { 68, 216,   0},
        { 72, 216,   0}, { 80, 220,   0}, { 84, 220,   0}, { 92, 224,   0},
        {100, 224,   0}, {104, 224,   0}, {112, 228,   0}, {116, 228,   0},
        {124, 232,   0}, {128, 232,   0}, {136, 232,   0}, {140, 236,   0},
        {148, 236,   0}, {156, 240,   0}, {160, 240,   0}, {168, 240,   0},
        {172, 244,   0}, {180, 244,   0}, {184, 248,   0}, {192, 248,   0},
        {200, 252,   0}, {200, 252,   4}, {200, 252,  12}, {204, 252,  20},
        {204, 252,  28}, {208, 252,  36}, {208, 252,  44}, {208, 252,  52},
        {212, 252,  60}, {212, 252,  68}, {216, 252,  76}, {216, 252,  84},
        {216, 252,  92}, {220, 252, 100}, {220, 252, 108}, {224, 252, 116},
        {224, 252, 124}, {224, 252, 132}, {228, 252, 140}, {228, 252, 148},
        {232, 252, 156}, {232, 252, 164}, {232, 252, 172}, {236, 252, 180},
        {236, 252, 188}, {240, 252, 196}, {240, 252, 204}, {240, 252, 212},
        {244, 252, 220}, {244, 252, 228}, {248, 252, 236}, {248, 252, 244},
        {252, 252, 252}, {252, 252, 248}, {252, 252, 244}, {252, 252, 240},
        {252, 252, 232}, {252, 252, 228}, {252, 252, 224}, {252, 252, 216},
        {252, 252, 212}, {252, 252, 208}, {252, 252, 200}, {252, 252, 196},
        {252, 252, 192}, {252, 252, 184}, {252, 252, 180}, {252, 252, 176},
        {252, 252, 168}, {252, 252, 164}, {252, 252, 160}, {252, 252, 156},
        {252, 252, 148}, {252, 252, 144}, {252, 252, 140}, {252, 252, 132},
        {252, 252, 128}, {252, 252, 124}, {252, 252, 116}, {252, 252, 112},
        {252, 252, 108}, {252, 252, 100}, {252, 252,  96}, {252, 252,  92},
        {252, 252,  84}, {252, 252,  80}, {252, 252,  76}, {252, 252,  72},
        {252, 252,  64}, {252, 252,  60}, {252, 252,  56}, {252, 252,  48},
        {252, 252,  44}, {252, 252,  40}, {252, 252,  32}, {252, 252,  28},
        {252, 252,  24}, {252, 252,  16}, {252, 252,  12}, {252, 252,   8},
        {252, 252,   0}, {248, 252,   0}, {244, 252,   0}, {240, 252,   0},
        {232, 252,   0}, {228, 252,   0}, {224, 252,   0}, {216, 252,   0},
        {212, 252,   0}, {208, 252,   0}, {200, 252,   0}, {196, 252,   0},
        {192, 252,   0}, {184, 252,   0}, {180, 252,   0}, {176, 252,   0},
        {168, 252,   0}, {164, 252,   0}, {160, 252,   0}, {156, 252,   0},
        {148, 252,   0}, {144, 252,   0}, {140, 252,   0}, {132, 252,   0},
        {128, 252,   0}, {124, 252,   0}, {116, 252,   0}, {112, 252,   0},
        {108, 252,   0}, {100, 252,   0}, { 96, 252,   0}, { 92, 252,   0},
        { 84, 252,   0}, { 80, 252,   0}, { 76, 252,   0}, { 72, 252,   0},
        { 64, 252,   0}, { 60, 252,   0}, { 56, 252,   0}, { 48, 252,   0},
        { 44, 252,   0}, { 40, 252,   0}, { 32, 252,   0}, { 28, 252,   0},
        { 24, 252,   0}, { 16, 252,   0}, { 12, 252,   0}, {  8, 252,   0},
        {  0, 252,   0}, {  0, 248,   0}, {  0, 244,   0}, {  0, 240,   0},
        {  0, 236,   0}, {  0, 232,   0}, {  0, 228,   0}, {  0, 224,   0},
        {  0, 220,   0}, {  0, 216,   0}, {  0, 212,   0}, {  0, 208,   0},
        {  0, 204,   0}, {  0, 200,   0}, {  0, 196,   0}, {  0, 192,   0},
        {  0, 188,   0}, {  0, 184,   0}, {  0, 180,   0}, {  0, 176,   0},
        {  0, 172,   0}, {  0, 168,   0}, {  0, 164,   0}, {  0, 160,   0},
        {  0, 156,   0}, {  0, 152,   0}, {  0, 148,   0}, {  0, 144,   0},
        {  0, 140,   0}, {  0, 136,   0}, {  0, 132,   0}, {  0, 128,   0},
        {  0, 124,   0}, {  0, 120,   0}, {  0, 116,   0}, {  0, 112,   0},
        {  0, 108,   0}, {  0, 104,   0}, {  0, 100,   0}, {  0,  96,   0},
        {  0,  92,   0}, {  0,  88,   0}, {  0,  84,   0}, {  0,  80,   0},
        {  0,  76,   0}, {  0,  72,   0}, {  0,  68,   0}, {  0,  64,   0},
        {  0,  60,   0}, {  0,  56,   0}, {  0,  52,   0}, {  0,  48,   0},
        {  0,  44,   0}, {  0,  40,   0}, {  0,  36,   0}, {  0,  32,   0},
        {  0,  28,   0}, {  0,  24,   0}, {  0,  20,   0}, {  0,  16,   0},
        {  0,  12,   0}, {  0,   8,   0}, {  0,   0,   0}, {  0,   0,   0}
    };
    static char ice[256][3]={
        {  0,   0,   0}, {  0,   0,   0}, {  0,   0,   8}, {  0,   0,  16},
        {  0,   0,  24}, {  0,   0,  32}, {  0,   0,  40}, {  0,   0,  48},
        {  0,   0,  56}, {  0,   0,  64}, {  0,   0,  72}, {  0,   0,  80},
        {  0,   0,  88}, {  0,   0,  96}, {  0,   0, 104}, {  0,   0, 112},
        {  0,   0, 120}, {  0,   0, 128}, {  0,   0, 136}, {  0,   0, 144},
        {  0,   0, 152}, {  0,   0, 160}, {  0,   0, 168}, {  0,   0, 176},
        {  0,   0, 184}, {  0,   0, 192}, {  0,   0, 200}, {  0,   0, 208},
        {  0,   0, 216}, {  0,   0, 224}, {  0,   0, 232}, {  0,   0, 240},
        {  0,   0, 252}, {  0,   4, 252}, {  0,  12, 252}, {  0,  20, 252},
        {  0,  28, 252}, {  0,  36, 252}, {  0,  44, 252}, {  0,  52, 252},
        {  0,  60, 252}, {  0,  68, 252}, {  0,  76, 252}, {  0,  84, 252},
        {  0,  92, 252}, {  0, 100, 252}, {  0, 108, 252}, {  0, 116, 252},
        {  0, 124, 252}, {  0, 132, 252}, {  0, 140, 252}, {  0, 148, 252},
        {  0, 156, 252}, {  0, 164, 252}, {  0, 172, 252}, {  0, 180, 252},
        {  0, 188, 252}, {  0, 196, 252}, {  0, 204, 252}, {  0, 212, 252},
        {  0, 220, 252}, {  0, 228, 252}, {  0, 236, 252}, {  0, 244, 252},
        {  0, 252, 252}, {  0, 252, 252}, {  0, 248, 252}, {  0, 244, 252},
        {  0, 240, 252}, {  0, 236, 252}, {  0, 232, 252}, {  0, 228, 252},
        {  0, 224, 252}, {  0, 220, 252}, {  0, 216, 252}, {  0, 212, 252},
        {  0, 208, 252}, {  0, 204, 252}, {  0, 200, 252}, {  0, 196, 252},
        {  0, 192, 252}, {  0, 188, 252}, {  0, 184, 252}, {  0, 180, 252},
        {  0, 176, 252}, {  0, 172, 252}, {  0, 168, 252}, {  0, 164, 252},
        {  0, 160, 252}, {  0, 156, 252}, {  0, 152, 252}, {  0, 148, 252},
        {  0, 144, 252}, {  0, 140, 252}, {  0, 136, 252}, {  0, 132, 252},
        {  0, 128, 252}, {  0, 124, 252}, {  0, 120, 252}, {  0, 116, 252},
        {  0, 112, 252}, {  0, 108, 252}, {  0, 104, 252}, {  0, 100, 252},
        {  0,  96, 252}, {  0,  92, 252}, {  0,  88, 252}, {  0,  84, 252},
        {  0,  80, 252}, {  0,  76, 252}, {  0,  72, 252}, {  0,  68, 252},
        {  0,  64, 252}, {  0,  60, 252}, {  0,  56, 252}, {  0,  52, 252},
        {  0,  48, 252}, {  0,  44, 252}, {  0,  40, 252}, {  0,  36, 252},
        {  0,  32, 252}, {  0,  28, 252}, {  0,  24, 252}, {  0,  20, 252},
        {  0,  16, 252}, {  0,  12, 252}, {  0,   8, 252}, {  0,   4, 252},
        {  0,   0, 252}, {  0,   0, 252}, {  0,  12, 252}, {  0,  28, 252},
        {  0,  44, 252}, {  0,  60, 252}, {  0,  76, 252}, {  0,  92, 252},
        {  0, 108, 252}, {  0, 124, 252}, {  0, 140, 252}, {  0, 156, 252},
        {  0, 172, 252}, {  0, 188, 252}, {  0, 204, 252}, {  0, 220, 252},
        {  0, 236, 252}, {  0, 232, 252}, {  4, 228, 252}, {  4, 224, 252},
        {  8, 220, 252}, { 12, 212, 252}, { 12, 208, 252}, { 16, 204, 252},
        { 20, 200, 252}, { 20, 192, 252}, { 24, 188, 252}, { 28, 184, 252},
        { 28, 180, 252}, { 32, 176, 252}, { 36, 168, 252}, { 36, 164, 252},
        { 40, 160, 252}, { 44, 156, 252}, { 44, 148, 252}, { 48, 144, 252},
        { 52, 140, 252}, { 52, 136, 252}, { 56, 128, 252}, { 60, 124, 252},
        { 60, 120, 252}, { 64, 116, 252}, { 68, 112, 252}, { 68, 104, 252},
        { 72, 100, 252}, { 76,  96, 252}, { 76,  92, 252}, { 80,  84, 252},
        { 84,  80, 252}, { 84,  76, 252}, { 88,  72, 252}, { 92,  64, 252},
        { 92,  60, 252}, { 96,  56, 252}, {100,  52, 252}, {100,  48, 252},
        {104,  40, 252}, {108,  36, 252}, {108,  32, 252}, {112,  28, 252},
        {116,  20, 252}, {116,  16, 252}, {120,  12, 252}, {124,   8, 252},
        {128,   0, 252}, {128,   0, 252}, {124,   0, 248}, {124,   0, 244},
        {120,   0, 240}, {120,   0, 236}, {116,   0, 232}, {116,   0, 228},
        {112,   0, 224}, {112,   0, 220}, {108,   0, 216}, {108,   0, 212},
        {104,   0, 208}, {104,   0, 204}, {100,   0, 200}, {100,   0, 196},
        { 96,   0, 192}, { 96,   0, 188}, { 92,   0, 184}, { 92,   0, 180},
        { 88,   0, 176}, { 88,   0, 172}, { 84,   0, 168}, { 84,   0, 164},
        { 80,   0, 160}, { 80,   0, 156}, { 76,   0, 152}, { 76,   0, 148},
        { 72,   0, 144}, { 72,   0, 140}, { 68,   0, 136}, { 68,   0, 132},
        { 64,   0, 128}, { 64,   0, 124}, { 60,   0, 120}, { 60,   0, 116},
        { 56,   0, 112}, { 56,   0, 108}, { 52,   0, 104}, { 52,   0, 100},
        { 48,   0,  96}, { 48,   0,  92}, { 44,   0,  88}, { 44,   0,  84},
        { 40,   0,  80}, { 40,   0,  76}, { 36,   0,  72}, { 36,   0,  68},
        { 32,   0,  64}, { 32,   0,  60}, { 28,   0,  56}, { 28,   0,  52},
        { 24,   0,  48}, { 24,   0,  44}, { 20,   0,  40}, { 20,   0,  36},
        { 16,   0,  32}, { 16,   0,  28}, { 12,   0,  24}, { 12,   0,  20},
        {  8,   0,  16}, {  8,   0,  12}, {  4,   0,   8}, {  4,   0,   4}
    };
    static char juteblue[256][3]={
        {  0,   0,   0}, {144, 152, 208}, {140, 148, 204}, {136, 144, 204},
        {132, 140, 200}, {128, 136, 200}, {124, 132, 196}, {120, 128, 196},
        {116, 124, 192}, {112, 120, 192}, {108, 116, 188}, {104, 112, 188},
        { 96, 108, 184}, { 92, 104, 184}, { 88, 100, 180}, { 84,  96, 180},
        { 80,  92, 176}, { 76,  88, 176}, { 72,  84, 172}, { 68,  80, 172},
        { 64,  76, 168}, { 60,  72, 168}, { 56,  68, 164}, { 52,  64, 164},
        { 44,  56, 160}, { 44,  56, 160}, { 44,  56, 156}, { 44,  56, 156},
        { 44,  56, 152}, { 44,  56, 148}, { 44,  56, 148}, { 44,  56, 144},
        { 44,  52, 140}, { 44,  52, 140}, { 44,  52, 136}, { 44,  52, 136},
        { 44,  52, 132}, { 44,  52, 128}, { 44,  52, 128}, { 44,  52, 124},
        { 44,  48, 120}, { 44,  48, 120}, { 44,  48, 116}, { 44,  48, 116},
        { 44,  48, 112}, { 44,  48, 108}, { 44,  48, 108}, { 44,  48, 104},
        { 44,  44, 100}, { 44,  44, 100}, { 44,  44,  96}, { 44,  44,  96},
        { 44,  44,  92}, { 44,  44,  88}, { 44,  44,  88}, { 44,  44,  84},
        { 40,  40,  80}, { 48,  52,  80}, { 56,  64,  76}, { 68,  76,  76},
        { 76,  88,  72}, { 84, 100,  72}, { 96, 112,  68}, {104, 124,  68},
        {116, 136,  64}, {124, 148,  64}, {132, 160,  60}, {144, 172,  60},
        {152, 184,  56}, {160, 196,  56}, {172, 208,  52}, {180, 220,  52},
        {192, 232,  48}, {184, 220,  48}, {176, 208,  52}, {164, 196,  52},
        {156, 184,  56}, {148, 172,  56}, {136, 160,  60}, {128, 148,  60},
        {116, 136,  64}, {108, 124,  64}, {100, 112,  68}, { 88, 100,  68},
        { 80,  88,  72}, { 72,  76,  72}, { 60,  64,  76}, { 52,  52,  76},
        { 40,  40,  80}, { 52,  40,  76}, { 64,  36,  72}, { 76,  36,  68},
        { 92,  32,  60}, {104,  28,  56}, {116,  28,  52}, {132,  24,  48},
        {144,  20,  40}, {156,  20,  36}, {172,  16,  32}, {184,  16,  28},
        {196,  12,  20}, {212,   8,  16}, {224,   8,  12}, {236,   4,   8},
        {252,   0,   0}, {252,  12,  12}, {252,  28,  28}, {252,  44,  44},
        {252,  60,  60}, {252,  76,  76}, {252,  92,  92}, {252, 108, 108},
        {252, 124, 124}, {252, 140, 140}, {252, 156, 156}, {252, 172, 172},
        {252, 188, 188}, {252, 204, 204}, {252, 220, 220}, {252, 236, 236},
        {252, 252, 252}, {248, 236, 240}, {240, 216, 228}, {236, 200, 216},
        {228, 180, 204}, {220, 164, 192}, {216, 144, 180}, {208, 128, 168},
        {200, 108, 156}, {196,  92, 144}, {188,  72, 132}, {180,  56, 120},
        {176,  36, 108}, {168,  20,  96}, {160,   0,  80}, {164,   4,  80},
        {172,   8,  76}, {176,  16,  72}, {184,  20,  72}, {188,  24,  68},
        {196,  32,  64}, {200,  36,  64}, {208,  40,  60}, {212,  48,  56},
        {220,  52,  56}, {224,  56,  52}, {232,  64,  48}, {236,  68,  48},
        {244,  72,  44}, {252,  80,  40}, {252,  88,  44}, {252, 100,  48},
        {252, 112,  52}, {252, 120,  60}, {252, 132,  64}, {252, 144,  68},
        {252, 152,  72}, {252, 164,  80}, {252, 176,  84}, {252, 184,  88},
        {252, 196,  92}, {252, 208, 100}, {252, 216, 104}, {252, 228, 108},
        {252, 240, 112}, {252, 252, 120}, {240, 240, 120}, {228, 228, 120},
        {212, 216, 124}, {200, 204, 124}, {188, 192, 128}, {172, 176, 128},
        {160, 164, 132}, {144, 152, 132}, {132, 140, 132}, {120, 128, 136},
        {104, 112, 136}, { 92, 100, 140}, { 80,  88, 140}, { 64,  76, 144},
        { 52,  64, 144}, { 36,  48, 148}, { 40,  52, 152}, { 44,  56, 160},
        { 48,  60, 160}, { 52,  64, 160}, { 56,  68, 164}, { 60,  72, 164},
        { 64,  76, 168}, { 68,  80, 168}, { 72,  84, 172}, { 76,  88, 172},
        { 80,  92, 176}, { 84,  96, 176}, { 88, 100, 180}, { 96, 104, 180},
        {100, 108, 184}, {104, 112, 184}, {108, 116, 188}, {112, 120, 188},
        {116, 124, 192}, {120, 128, 192}, {124, 132, 196}, {128, 136, 196},
        {132, 140, 200}, {136, 144, 200}, {140, 148, 204}, {148, 152, 204},
        {152, 156, 204}, {156, 160, 208}, {160, 164, 208}, {164, 168, 212},
        {168, 172, 212}, {172, 176, 216}, {176, 180, 216}, {180, 184, 220},
        {184, 188, 220}, {188, 192, 224}, {192, 196, 224}, {200, 200, 228},
        {204, 204, 228}, {208, 208, 232}, {212, 212, 232}, {216, 216, 236},
        {220, 220, 236}, {224, 224, 240}, {228, 228, 240}, {232, 232, 244},
        {236, 236, 244}, {240, 240, 248}, {244, 244, 248}, {252, 252, 252},
        {248, 248, 252}, {244, 244, 252}, {240, 240, 248}, {236, 236, 248},
        {232, 232, 244}, {228, 228, 244}, {224, 224, 240}, {220, 220, 240},
        {216, 216, 236}, {212, 212, 236}, {208, 208, 232}, {200, 204, 232},
        {196, 200, 228}, {192, 196, 228}, {188, 192, 224}, {184, 188, 224},
        {180, 184, 220}, {176, 180, 220}, {172, 176, 216}, {168, 172, 216},
        {164, 168, 212}, {160, 164, 212}, {156, 160, 208}, {148, 156, 208}
    };
    static char jutemap[256][3]={
        {  0,   0,   0}, { 88,  88, 184}, { 96,  92, 192}, {104, 100, 192},
        {112, 112, 196}, {124, 120, 200}, {132, 132, 204}, {144, 140, 208},
        {152, 152, 212}, {164, 160, 216}, {172, 172, 220}, {180, 180, 224},
        {192, 192, 228}, {200, 200, 232}, {212, 212, 236}, {220, 220, 240},
        {232, 232, 244}, {240, 240, 248}, {252, 252, 252}, {252, 240, 244},
        {252, 224, 232}, {252, 208, 224}, {252, 192, 212}, {252, 176, 204},
        {252, 160, 192}, {252, 144, 184}, {252, 128, 172}, {252, 112, 164},
        {252,  96, 152}, {252,  80, 144}, {252,  64, 132}, {252,  48, 124},
        {252,  32, 112}, {252,  16, 104}, {252,   0,  92}, {236,   0,  88},
        {220,   0,  80}, {204,   0,  76}, {188,   0,  68}, {168,   0,  64},
        {152,   0,  56}, {136,   0,  52}, {120,   0,  44}, {104,   0,  40},
        { 84,   0,  32}, { 68,   0,  28}, { 52,   0,  20}, { 36,   0,  16},
        { 20,   0,   8}, {  0,   0,   0}, {  0,   8,   4}, {  4,  16,   8},
        {  8,  24,  16}, { 12,  32,  20}, { 16,  44,  28}, { 20,  52,  32},
        { 24,  60,  36}, { 28,  68,  44}, { 32,  76,  48}, { 36,  88,  56},
        { 40,  96,  60}, { 44, 104,  64}, { 48, 112,  72}, { 52, 120,  76},
        { 56, 132,  84}, { 48, 136,  84}, { 40, 144,  80}, { 52, 148,  88},
        { 68, 156, 100}, { 80, 164, 112}, { 96, 168, 124}, {108, 176, 136},
        {124, 184, 144}, {136, 192, 156}, {152, 196, 168}, {164, 204, 180},
        {180, 212, 192}, {192, 220, 200}, {208, 224, 212}, {220, 232, 224},
        {236, 240, 236}, {252, 248, 248}, {252, 252, 252}, {252, 248, 252},
        {252, 244, 252}, {248, 236, 248}, {248, 232, 248}, {244, 228, 244},
        {244, 220, 244}, {240, 216, 240}, {240, 212, 240}, {236, 204, 236},
        {236, 200, 236}, {232, 196, 232}, {232, 188, 232}, {228, 184, 228},
        {228, 180, 228}, {224, 172, 224}, {224, 168, 224}, {224, 164, 224},
        {220, 156, 220}, {220, 152, 220}, {216, 148, 216}, {216, 140, 216},
        {212, 136, 212}, {212, 132, 212}, {208, 124, 208}, {208, 120, 208},
        {204, 116, 204}, {204, 108, 204}, {200, 104, 200}, {200, 100, 200},
        {196,  92, 196}, {196,  88, 196}, {192,  80, 192}, {192,  88, 188},
        {192, 100, 184}, {192, 112, 176}, {196, 120, 172}, {196, 132, 164},
        {196, 144, 160}, {200, 152, 152}, {200, 164, 148}, {200, 176, 144},
        {204, 184, 136}, {204, 196, 132}, {204, 208, 124}, {208, 216, 120},
        {208, 228, 112}, {208, 240, 108}, {212, 252, 100}, {208, 244, 100},
        {204, 232, 100}, {200, 224, 100}, {192, 212,  96}, {188, 200,  96},
        {184, 192,  96}, {176, 180,  96}, {172, 168,  92}, {168, 160,  92},
        {164, 148,  92}, {156, 140,  92}, {152, 128,  88}, {148, 116,  88},
        {140, 108,  88}, {136,  96,  88}, {132,  84,  84}, {128,  76,  84},
        {120,  64,  84}, {116,  56,  84}, {112,  44,  80}, {104,  32,  80},
        {100,  24,  80}, { 96,  12,  80}, { 88,   0,  76}, { 88,   0,  80},
        { 84,   0,  88}, { 80,   0,  96}, { 76,   0, 104}, { 72,   0, 112},
        { 68,   0, 120}, { 64,   0, 124}, { 60,   0, 132}, { 56,   0, 140},
        { 52,   0, 148}, { 48,   0, 156}, { 44,   0, 164}, { 44,   0, 168},
        { 40,   0, 176}, { 36,   0, 184}, { 32,   0, 192}, { 28,   0, 200},
        { 24,   0, 208}, { 20,   0, 212}, { 16,   0, 220}, { 12,   0, 228},
        {  0,   0, 236}, {  4,   0, 244}, {  0,   0, 252}, {  0,   4, 252},
        {  4,  12, 252}, {  8,  20, 252}, { 12,  28, 252}, { 16,  36, 252},
        { 20,  44, 252}, { 20,  52, 252}, { 24,  60, 252}, { 28,  68, 252},
        { 32,  76, 252}, { 36,  84, 252}, { 40,  92, 252}, { 40, 100, 252},
        { 44, 108, 252}, { 48, 116, 252}, { 52, 120, 252}, { 56, 128, 252},
        { 60, 136, 252}, { 60, 144, 252}, { 64, 152, 252}, { 68, 160, 252},
        { 72, 168, 252}, { 76, 176, 252}, { 80, 184, 252}, { 80, 192, 252},
        { 84, 200, 252}, { 88, 208, 252}, { 92, 216, 252}, { 96, 224, 252},
        {100, 232, 252}, {100, 228, 248}, { 96, 224, 244}, { 92, 216, 240},
        { 88, 212, 236}, { 88, 204, 232}, { 84, 200, 228}, { 80, 192, 220},
        { 76, 188, 216}, { 76, 180, 212}, { 72, 176, 208}, { 68, 168, 204},
        { 64, 164, 200}, { 64, 156, 196}, { 60, 152, 188}, { 56, 144, 184},
        { 52, 140, 180}, { 52, 132, 176}, { 48, 128, 172}, { 44, 120, 168},
        { 40, 116, 160}, { 40, 108, 156}, { 36, 104, 152}, { 32,  96, 148},
        { 28,  92, 144}, { 28,  84, 140}, { 24,  80, 136}, { 20,  72, 128},
        { 16,  68, 124}, { 16,  60, 120}, { 12,  56, 116}, {  8,  48, 112},
        {  4,  44, 108}, {  0,  36, 100}, {  4,  36, 104}, { 12,  40, 108},
        { 16,  44, 116}, { 24,  48, 120}, { 28,  52, 128}, { 36,  56, 132},
        { 40,  60, 140}, { 48,  64, 144}, { 52,  64, 148}, { 60,  68, 156},
        { 64,  72, 160}, { 72,  76, 168}, { 76,  80, 172}, { 84,  84, 180}
    };
    static char jutes[256][3]={
        {  0,   0,   0}, {  0,  72,   0}, {  0, 108,   0}, {  0, 144,   0},
        {  0, 180,   0}, {  0, 216,   0}, {  0, 252,   0}, {  0, 224,   0},
        {  0, 196,   0}, {  0, 168,   0}, {  0, 140,   0}, {  0, 112,   0},
        {  0,  84,   0}, {  0,  56,   0}, {  0,  28,   0}, {  0,   0,   0},
        {  0,  36,  36}, {  0,  72,  72}, {  0, 108, 108}, {  0, 144, 144},
        {  0, 180, 180}, {  0, 216, 216}, {  0, 252, 252}, {  0, 224, 224},
        {  0, 196, 196}, {  0, 168, 168}, {  0, 140, 140}, {  0, 112, 112},
        {  0,  84,  84}, {  0,  56,  56}, {  0,  28,  28}, {  0,   0,   0},
        { 36,  36,   0}, { 72,  72,   0}, {108, 108,   0}, {144, 144,   0},
        {180, 180,   0}, {216, 216,   0}, {252, 252,   0}, {224, 224,   0},
        {196, 196,   0}, {168, 168,   0}, {140, 140,   0}, {112, 112,   0},
        { 84,  84,   0}, { 56,  56,   0}, { 28,  28,   0}, {  0,   0,   0},
        { 36,   0,  36}, { 72,   0,  72}, {108,   0, 108}, {144,   0, 144},
        {180,   0, 180}, {216,   0, 216}, {252,   0, 252}, {224,   0, 224},
        {196,   0, 196}, {168,   0, 168}, {140,   0, 140}, {112,   0, 112},
        { 84,   0,  84}, { 56,   0,  56}, { 28,   0,  28}, {  0,   0,   0},
        { 36,  36,   0}, { 72,  72,   0}, {108, 108,   0}, {144, 144,   0},
        {180, 180,   0}, {216, 216,   0}, {252, 252,   0}, {224, 224,   0},
        {196, 196,   0}, {168, 168,   0}, {140, 140,   0}, {112, 112,   0},
        { 84,  84,   0}, { 56,  56,   0}, { 28,  28,   0}, {  0,   0,   0},
        {  0,  36,  36}, {  0,  72,  72}, {  0, 108, 108}, {  0, 144, 144},
        {  0, 180, 180}, {  0, 216, 216}, {  0, 252, 252}, {  0, 224, 224},
        {  0, 196, 196}, {  0, 168, 168}, {  0, 140, 140}, {  0, 112, 112},
        {  0,  84,  84}, {  0,  56,  56}, {  0,  28,  28}, {  0,   0,   0},
        {  0,  36,   0}, {  0,  72,   0}, {  0, 108,   0}, {  0, 144,   0},
        {  0, 180,   0}, {  0, 216,   0}, {  0, 252,   0}, {  0, 224,   0},
        {  0, 196,   0}, {  0, 168,   0}, {  0, 140,   0}, {  0, 112,   0},
        {  0,  84,   0}, {  0,  56,   0}, {  0,  28,   0}, {  0,   0,   4},
        { 16,   8,  32}, { 32,  16,  64}, { 48,  24, 100}, { 68,  32, 132},
        { 84,  40, 168}, {100,  48, 200}, {120,  56, 236}, {108,  52, 212},
        { 96,  44, 184}, { 80,  40, 160}, { 68,  32, 132}, { 56,  28, 108},
        { 40,  20,  80}, { 28,  16,  56}, { 16,   8,  28}, {  0,   0,   0},
        { 28,  12,  12}, { 56,  28,  28}, { 84,  40,  40}, {116,  56,  56},
        {144,  68,  68}, {172,  84,  84}, {204, 100, 100}, {184,  92,  92},
        {160,  80,  80}, {136,  68,  68}, {116,  56,  56}, { 92,  48,  48},
        { 68,  36,  36}, { 48,  24,  24}, { 24,  12,  12}, {  0,   0,   0},
        {  0,   0,  40}, {  0,   0,  80}, {  0,   0, 120}, {  0,   0, 160},
        { 40,  40, 148}, { 80,  80, 136}, {120, 120, 120}, {108, 108, 108},
        { 96,  96,  96}, { 80,  80,  80}, { 68,  68,  68}, { 56,  56,  56},
        { 40,  40,  40}, { 28,  28,  28}, { 16,  16,  16}, {  0,   0,   0},
        {  0,   0,  48}, {  0,   0, 100}, {  0,   0, 148}, {  0,   0, 200},
        { 52,  52, 188}, {104, 104, 176}, {160, 160, 160}, {144, 144, 144},
        {128, 128, 128}, {108, 108, 108}, { 92,  92,  92}, { 72,  72,  72},
        { 56,  56,  56}, { 36,  36,  36}, { 20,  20,  20}, {  0,   0,   0},
        {  0,   0,  60}, {  0,   0, 120}, {  0,   0, 180}, {  0,   0, 240},
        { 64,  64, 228}, {132, 132, 216}, {200, 200, 200}, {180, 180, 180},
        {156, 156, 156}, {136, 136, 136}, {112, 112, 112}, { 92,  92,  92},
        { 68,  68,  68}, { 48,  48,  48}, { 24,  24,  24}, {  0,   0,   0},
        {  0,  28,  60}, {  0,  60, 124}, {  0,  88, 188}, {  0, 120, 252},
        { 80, 160, 248}, {160, 200, 244}, {240, 240, 240}, {216, 216, 216},
        {188, 188, 188}, {160, 160, 160}, {136, 136, 136}, {108, 108, 108},
        { 80,  80,  80}, { 56,  56,  56}, { 28,  28,  28}, {  0,   0,   0},
        {  0,  40,  60}, {  0,  80, 124}, {  0, 120, 188}, {  0, 160, 252},
        { 84, 188, 252}, {168, 220, 248}, {252, 252, 244}, {224, 224, 224},
        {192, 192, 192}, {160, 160, 160}, {128, 128, 128}, { 96,  96,  96},
        { 64,  64,  64}, { 32,  32,  32}, {  0,   0,   0}, {  0,   0,  36},
        {  0,   0,  72}, {  0,   0, 108}, {  0,   0, 144}, {  0,   0, 180},
        {  0,   0, 216}, {  0,   0, 252}, {  0,   0, 224}, {  0,   0, 196},
        {  0,   0, 168}, {  0,   0, 140}, {  0,   0, 112}, {  0,   0,  84},
        {  0,   0,  56}, {  0,   0,  28}, {  0,   0,   0}, { 36,   0,   0},
        { 72,   0,   0}, {108,   0,   0}, {144,   0,   0}, {180,   0,   0},
        {216,   0,   0}, {252,   0,   0}, {224,   0,   0}, {196,   0,   0},
        {168,   0,   0}, {140,   0,   0}, {112,   0,   0}, { 84,   0,   0},
        { 56,   0,   0}, { 28,   0,   0}, {  0,   0,   0}, {  0,  36,   0}
    };
    static char mandmap[256][3]={
        {  0,   0,   0}, {224, 224, 224}, {216, 216, 216}, {208, 208, 208},
        {200, 200, 200}, {192, 192, 192}, {184, 184, 184}, {176, 176, 176},
        {168, 168, 168}, {160, 160, 160}, {152, 152, 152}, {144, 144, 144},
        {136, 136, 136}, {128, 128, 128}, {120, 120, 120}, {112, 112, 112},
        {104, 104, 104}, { 96,  96,  96}, { 88,  88,  88}, { 80,  80,  80},
        { 72,  72,  72}, { 64,  64,  64}, { 56,  56,  56}, { 48,  48,  56},
        { 40,  40,  56}, { 32,  32,  56}, { 24,  24,  56}, { 16,  16,  56},
        {  8,   8,  56}, {  0,   0,  60}, {  0,   0,  64}, {  0,   0,  72},
        {  0,   0,  80}, {  0,   0,  88}, {  0,   0,  96}, {  0,   0, 104},
        {  0,   0, 108}, {  0,   0, 116}, {  0,   0, 124}, {  0,   0, 132},
        {  0,   0, 140}, {  0,   0, 148}, {  0,   0, 156}, {  0,   0, 160},
        {  0,   0, 168}, {  0,   0, 176}, {  0,   0, 184}, {  0,   0, 192},
        {  0,   0, 200}, {  0,   0, 204}, {  0,   0, 212}, {  0,   0, 220},
        {  0,   0, 228}, {  0,   0, 236}, {  0,   0, 244}, {  0,   0, 252},
        {  0,   4, 252}, {  4,  12, 252}, {  8,  20, 252}, { 12,  28, 252},
        { 16,  36, 252}, { 20,  44, 252}, { 20,  52, 252}, { 24,  60, 252},
        { 28,  68, 252}, { 32,  76, 252}, { 36,  84, 252}, { 40,  92, 252},
        { 40, 100, 252}, { 44, 108, 252}, { 48, 116, 252}, { 52, 120, 252},
        { 56, 128, 252}, { 60, 136, 252}, { 60, 144, 252}, { 64, 152, 252},
        { 68, 160, 252}, { 72, 168, 252}, { 76, 176, 252}, { 80, 184, 252},
        { 80, 192, 252}, { 84, 200, 252}, { 88, 208, 252}, { 92, 216, 252},
        { 96, 224, 252}, {100, 232, 252}, {100, 228, 248}, { 96, 224, 244},
        { 92, 216, 240}, { 88, 212, 236}, { 88, 204, 232}, { 84, 200, 228},
        { 80, 192, 220}, { 76, 188, 216}, { 76, 180, 212}, { 72, 176, 208},
        { 68, 168, 204}, { 64, 164, 200}, { 64, 156, 196}, { 60, 152, 188},
        { 56, 144, 184}, { 52, 140, 180}, { 52, 132, 176}, { 48, 128, 172},
        { 44, 120, 168}, { 40, 116, 160}, { 40, 108, 156}, { 36, 104, 152},
        { 32,  96, 148}, { 28,  92, 144}, { 28,  84, 140}, { 24,  80, 136},
        { 20,  72, 128}, { 16,  68, 124}, { 16,  60, 120}, { 12,  56, 116},
        {  8,  48, 112}, {  4,  44, 108}, {  0,  36, 100}, {  4,  36, 104},
        { 12,  40, 108}, { 16,  44, 116}, { 24,  48, 120}, { 28,  52, 128},
        { 36,  56, 132}, { 40,  60, 140}, { 48,  64, 144}, { 52,  64, 148},
        { 60,  68, 156}, { 64,  72, 160}, { 72,  76, 168}, { 76,  80, 172},
        { 84,  84, 180}, { 88,  88, 184}, { 96,  92, 192}, {104, 100, 192},
        {112, 112, 196}, {124, 120, 200}, {132, 132, 204}, {144, 140, 208},
        {152, 152, 212}, {164, 160, 216}, {172, 172, 220}, {180, 180, 224},
        {192, 192, 228}, {200, 200, 232}, {212, 212, 236}, {220, 220, 240},
        {232, 232, 244}, {240, 240, 248}, {252, 252, 252}, {252, 240, 244},
        {252, 224, 232}, {252, 208, 224}, {252, 192, 212}, {252, 176, 204},
        {252, 160, 192}, {252, 144, 184}, {252, 128, 172}, {252, 112, 164},
        {252,  96, 152}, {252,  80, 144}, {252,  64, 132}, {252,  48, 124},
        {252,  32, 112}, {252,  16, 104}, {252,   0,  92}, {236,   0,  88},
        {228,   0,  88}, {216,   4,  84}, {204,   4,  80}, {192,   8,  76},
        {180,   8,  76}, {168,  12,  72}, {156,  16,  68}, {144,  16,  64},
        {132,  20,  60}, {124,  20,  60}, {112,  24,  56}, {100,  24,  52},
        { 88,  28,  48}, { 76,  32,  44}, { 64,  32,  44}, { 52,  36,  40},
        { 40,  36,  36}, { 28,  40,  32}, { 16,  44,  28}, { 20,  52,  32},
        { 24,  60,  36}, { 28,  68,  44}, { 32,  76,  48}, { 36,  88,  56},
        { 40,  96,  60}, { 44, 104,  64}, { 48, 112,  72}, { 52, 120,  76},
        { 56, 132,  84}, { 48, 136,  84}, { 40, 144,  80}, { 52, 148,  88},
        { 68, 156, 100}, { 80, 164, 112}, { 96, 168, 124}, {108, 176, 136},
        {124, 184, 144}, {136, 192, 156}, {152, 196, 168}, {164, 204, 180},
        {180, 212, 192}, {192, 220, 200}, {208, 224, 212}, {220, 232, 224},
        {236, 240, 236}, {252, 248, 248}, {252, 252, 252}, {252, 252, 240},
        {252, 252, 228}, {252, 252, 216}, {248, 248, 204}, {248, 248, 192},
        {248, 248, 180}, {248, 248, 164}, {244, 244, 152}, {244, 244, 140},
        {244, 244, 128}, {244, 244, 116}, {240, 240, 104}, {240, 240,  92},
        {240, 240,  76}, {240, 240,  64}, {236, 236,  52}, {236, 236,  40},
        {236, 236,  28}, {236, 236,  16}, {232, 232,   0}, {232, 232,  12},
        {232, 232,  28}, {232, 232,  40}, {236, 236,  56}, {236, 236,  68},
        {236, 236,  84}, {236, 236,  96}, {240, 240, 112}, {240, 240, 124},
        {240, 240, 140}, {244, 244, 152}, {244, 244, 168}, {244, 244, 180},
        {244, 244, 196}, {248, 248, 208}, {248, 248, 224}, {248, 248, 236},
        {252, 252, 252}, {248, 248, 248}, {240, 240, 240}, {232, 232, 232}
    };
    static char phong[256][3]={
        {  0,   0,   0}, {  0,   0,   0}, {  0,   0,   4}, {  0,   0,   4},
        {  0,   0,   8}, {  0,   0,  12}, {  0,   0,  12}, {  0,   0,  16},
        {  0,   0,  20}, {  0,   0,  20}, {  0,   0,  24}, {  0,   0,  28},
        {  0,   0,  28}, {  0,   0,  32}, {  0,   0,  36}, {  0,   0,  36},
        {  0,   0,  40}, {  0,   0,  44}, {  0,   0,  44}, {  0,   0,  48},
        {  0,   0,  52}, {  0,   0,  52}, {  0,   0,  56}, {  0,   0,  60},
        {  0,   0,  60}, {  0,   0,  64}, {  0,   0,  68}, {  0,   0,  68},
        {  0,   0,  72}, {  0,   0,  76}, {  0,   0,  76}, {  0,   0,  80},
        {  0,   0,  84}, {  0,   0,  84}, {  0,   0,  88}, {  0,   0,  92},
        {  0,   0,  92}, {  0,   0,  96}, {  0,   0, 100}, {  0,   0, 100},
        {  0,   0, 104}, {  0,   0, 108}, {  0,   0, 108}, {  0,   0, 112},
        {  0,   0, 116}, {  0,   0, 116}, {  0,   0, 120}, {  0,   0, 124},
        {  0,   0, 124}, {  0,   0, 128}, {  0,   0, 132}, {  0,   0, 132},
        {  0,   0, 136}, {  0,   0, 140}, {  0,   0, 140}, {  0,   0, 144},
        {  0,   0, 148}, {  0,   0, 148}, {  0,   0, 152}, {  0,   0, 156},
        {  0,   0, 156}, {  0,   0, 160}, {  0,   0, 164}, {  0,   0, 168},
        {  0,   4, 168}, {  0,   8, 168}, {  0,  12, 168}, {  0,  20, 168},
        {  0,  24, 168}, {  0,  28, 168}, {  0,  36, 168}, {  0,  40, 168},
        {  0,  44, 168}, {  0,  52, 168}, {  0,  56, 168}, {  0,  60, 168},
        {  0,  68, 168}, {  0,  72, 168}, {  0,  76, 168}, {  0,  84, 168},
        {  0,  88, 168}, {  0,  92, 168}, {  0,  96, 168}, {  0, 104, 168},
        {  0, 108, 168}, {  0, 112, 168}, {  0, 120, 168}, {  0, 124, 168},
        {  0, 128, 168}, {  0, 136, 168}, {  0, 140, 168}, {  0, 144, 168},
        {  0, 152, 168}, {  0, 156, 168}, {  0, 160, 168}, {  0, 168, 168},
        {  0, 168, 168}, {  4, 172, 172}, {  4, 172, 172}, {  8, 176, 176},
        { 12, 180, 180}, { 12, 180, 180}, { 16, 184, 184}, { 20, 188, 188},
        { 20, 188, 188}, { 24, 192, 192}, { 28, 196, 196}, { 28, 196, 196},
        { 32, 200, 200}, { 36, 204, 204}, { 36, 204, 204}, { 40, 208, 208},
        { 44, 212, 212}, { 44, 212, 212}, { 48, 216, 216}, { 52, 220, 220},
        { 52, 220, 220}, { 56, 224, 224}, { 60, 228, 228}, { 60, 228, 228},
        { 64, 232, 232}, { 68, 236, 236}, { 68, 236, 236}, { 72, 240, 240},
        { 76, 244, 244}, { 76, 244, 244}, { 80, 248, 248}, { 84, 252, 252},
        { 84, 252, 252}, { 88, 252, 248}, { 88, 252, 248}, { 92, 252, 244},
        { 96, 252, 240}, { 96, 252, 240}, {100, 252, 236}, {104, 252, 232},
        {104, 252, 232}, {108, 252, 228}, {112, 252, 224}, {112, 252, 224},
        {116, 252, 220}, {120, 252, 216}, {120, 252, 216}, {124, 252, 212},
        {128, 252, 208}, {128, 252, 208}, {132, 252, 204}, {136, 252, 200},
        {136, 252, 200}, {140, 252, 196}, {144, 252, 192}, {144, 252, 192},
        {148, 252, 188}, {152, 252, 184}, {152, 252, 184}, {156, 252, 180},
        {160, 252, 176}, {160, 252, 176}, {164, 252, 172}, {168, 252, 168},
        {168, 252, 168}, {172, 252, 164}, {172, 252, 164}, {176, 252, 160},
        {180, 252, 156}, {180, 252, 156}, {184, 252, 152}, {188, 252, 148},
        {188, 252, 148}, {192, 252, 144}, {196, 252, 140}, {196, 252, 140},
        {200, 252, 136}, {204, 252, 132}, {204, 252, 132}, {208, 252, 128},
        {212, 252, 124}, {212, 252, 124}, {216, 252, 120}, {220, 252, 116},
        {220, 252, 116}, {224, 252, 112}, {228, 252, 108}, {228, 252, 108},
        {232, 252, 104}, {236, 252, 100}, {236, 252, 100}, {240, 252,  96},
        {244, 252,  92}, {244, 252,  92}, {248, 252,  88}, {252, 252,  84},
        {248, 248,  84}, {240, 240,  88}, {232, 232,  88}, {224, 224,  92},
        {216, 216,  96}, {208, 208,  96}, {200, 200, 100}, {192, 192, 104},
        {184, 184, 104}, {176, 176, 108}, {168, 168, 112}, {160, 160, 112},
        {152, 152, 116}, {144, 144, 120}, {136, 136, 120}, {128, 128, 124},
        {120, 120, 128}, {112, 112, 128}, {104, 104, 132}, { 96,  96, 136},
        { 88,  88, 136}, { 80,  80, 140}, { 72,  72, 144}, { 64,  64, 144},
        { 56,  56, 148}, { 48,  48, 152}, { 40,  40, 152}, { 32,  32, 156},
        { 24,  24, 160}, { 16,  16, 160}, {  8,   8, 164}, {  0,   0, 168},
        {  0,   0, 164}, {  0,   0, 160}, {  0,   0, 156}, {  0,   0, 148},
        {  0,   0, 144}, {  0,   0, 140}, {  0,   0, 132}, {  0,   0, 128},
        {  0,   0, 124}, {  0,   0, 116}, {  0,   0, 112}, {  0,   0, 108},
        {  0,   0, 100}, {  0,   0,  96}, {  0,   0,  92}, {  0,   0,  84},
        {  0,   0,  80}, {  0,   0,  76}, {  0,   0,  72}, {  0,   0,  64},
        {  0,   0,  60}, {  0,   0,  56}, {  0,   0,  48}, {  0,   0,  44},
        {  0,   0,  40}, {  0,   0,  32}, {  0,   0,  28}, {  0,   0,  24},
        {  0,   0,  16}, {  0,   0,  12}, {  0,   0,   8}, {  0,   0,   0}
    };
    static char rose[256][3]={
        {  0,   0,   0}, {124, 192, 208}, {124, 196, 208}, {124, 204, 208},
        {124, 212, 208}, {128, 220, 212}, {128, 192, 204}, {132, 180, 196},
        {136, 168, 188}, {140, 156, 184}, {144, 144, 176}, {148, 132, 168},
        {152, 120, 160}, {156, 108, 156}, {160,  96, 148}, {164,  84, 140},
        {168,  72, 136}, {172,  60, 128}, {176,  48, 120}, {180,  32, 112},
        {172,  48, 112}, {164,  48, 104}, {156,  48,  92}, {152,  48,  84},
        {144,  48,  72}, {136,  48,  64}, {132,  48,  56}, {124,  48,  44},
        {116,  48,  36}, {112,  48,  24}, {104,  48,  16}, { 96,  52,   4},
        {116,  48,  36}, {116,  48,  36}, {116,  48,  36}, {112,  44,  36},
        {112,  44,  36}, {112,  44,  32}, {112,  44,  32}, {108,  40,  32},
        {108,  40,  32}, {108,  40,  32}, {104,  36,  28}, {104,  36,  28},
        {104,  36,  28}, {104,  36,  28}, {100,  32,  28}, {100,  32,  28},
        {100,  32,  24}, {100,  32,  24}, { 96,  28,  24}, { 96,  28,  24},
        { 96,  28,  24}, { 92,  24,  20}, { 92,  24,  20}, { 92,  24,  20},
        { 92,  24,  20}, { 88,  20,  20}, { 88,  20,  20}, { 88,  20,  16},
        { 88,  20,  16}, { 84,  16,  16}, { 84,  16,  16}, { 84,  16,  16},
        { 80,  12,  12}, { 80,  12,  12}, { 80,  12,  12}, { 80,  12,  12},
        { 76,   8,  12}, { 76,   8,  12}, { 76,   8,   8}, { 76,   8,   8},
        { 72,   4,   8}, { 72,   4,   8}, { 72,   4,   8}, { 68,   0,   4},
        { 72,   4,   8}, { 76,   4,   8}, { 76,   4,  12}, { 80,   4,  12},
        { 80,   4,  16}, { 84,   4,  16}, { 84,   4,  20}, { 88,   4,  20},
        { 88,   4,  24}, { 92,   4,  24}, { 96,   4,  28}, { 96,   4,  28},
        {100,   4,  32}, {100,   4,  32}, {104,   4,  36}, {104,   4,  36},
        {108,   4,  40}, {108,   4,  40}, {112,   4,  44}, {112,   4,  44},
        {116,   4,  48}, {120,   8,  48}, {120,   8,  52}, {124,   8,  52},
        {124,   8,  56}, {128,   8,  56}, {128,   8,  60}, {132,   8,  60},
        {132,   8,  64}, {136,   8,  64}, {136,   8,  68}, {140,   8,  68},
        {144,   8,  72}, {144,   8,  72}, {148,   8,  72}, {148,   8,  76},
        {152,   8,  76}, {152,   8,  80}, {156,   8,  80}, {156,   8,  84},
        {160,   8,  84}, {160,   8,  88}, {164,   8,  88}, {168,  12,  92},
        {168,  12,  92}, {172,  12,  96}, {172,  12,  96}, {176,  12, 100},
        {176,  12, 100}, {180,  12, 104}, {180,  12, 104}, {184,  12, 108},
        {184,  12, 108}, {188,  12, 112}, {192,  12, 112}, {192,  12, 116},
        {196,  12, 116}, {196,  12, 120}, {200,  12, 120}, {200,  12, 124},
        {204,  12, 124}, {204,  12, 128}, {208,  12, 128}, {208,  12, 132},
        {212,  12, 132}, {216,  16, 136}, {208,  16, 124}, {208,  24, 120},
        {208,  32, 116}, {212,  36, 112}, {212,  44, 108}, {212,  52, 104},
        {212,  60, 100}, {216,  64,  96}, {216,  72,  92}, {216,  80,  88},
        {216,  88,  84}, {220,  92,  80}, {220, 100,  76}, {220, 108,  68},
        {220, 116,  64}, {224, 120,  60}, {224, 128,  56}, {224, 136,  52},
        {224, 144,  48}, {228, 148,  44}, {228, 156,  40}, {228, 164,  36},
        {228, 172,  32}, {232, 176,  28}, {232, 184,  24}, {232, 192,  20},
        {236, 200,  12}, {236, 200,  12}, {236, 200,  16}, {232, 200,  16},
        {232, 200,  20}, {228, 200,  20}, {228, 200,  24}, {224, 196,  24},
        {224, 196,  28}, {224, 196,  28}, {220, 196,  32}, {220, 196,  32},
        {216, 196,  36}, {216, 192,  36}, {212, 192,  40}, {212, 192,  40},
        {208, 192,  44}, {208, 192,  44}, {208, 192,  48}, {204, 188,  48},
        {204, 188,  52}, {200, 188,  52}, {200, 188,  56}, {196, 188,  56},
        {196, 188,  60}, {192, 184,  60}, {192, 184,  64}, {192, 184,  64},
        {188, 184,  68}, {188, 184,  68}, {184, 184,  72}, {184, 184,  72},
        {180, 180,  76}, {180, 180,  76}, {180, 180,  80}, {176, 180,  80},
        {176, 180,  84}, {172, 180,  84}, {172, 176,  88}, {168, 176,  88},
        {168, 176,  92}, {164, 176,  92}, {164, 176,  96}, {164, 176,  96},
        {160, 172, 100}, {160, 172, 100}, {156, 172, 104}, {156, 172, 104},
        {152, 172, 108}, {152, 172, 108}, {148, 168, 112}, {148, 168, 112},
        {148, 168, 116}, {144, 168, 116}, {144, 168, 120}, {140, 168, 120},
        {140, 168, 124}, {136, 164, 124}, {136, 164, 128}, {136, 164, 128},
        {132, 164, 132}, {132, 164, 132}, {128, 164, 136}, {128, 160, 136},
        {124, 160, 140}, {124, 160, 140}, {120, 160, 144}, {120, 160, 144},
        {120, 160, 148}, {116, 156, 148}, {116, 156, 152}, {112,  72, 208},
        {112,  80, 208}, {112,  88, 208}, {116,  92, 208}, {116, 100, 208},
        {116, 108, 208}, {116, 116, 208}, {116, 120, 208}, {116, 128, 208},
        {120, 136, 208}, {120, 140, 208}, {120, 148, 208}, {120, 156, 208},
        {120, 164, 208}, {120, 168, 208}, {124, 176, 208}, {124, 184, 208}
    };
    switch (palette) {                  // vyber barvove palety
        case 0: p=&blues;       break;
        case 1: p=&gold;        break;
        case 2: p=&greens;      break;
        case 3: p=&ice;         break;
        case 4: p=&juteblue;    break;
        case 5: p=&jutemap;     break;
        case 6: p=&jutes;       break;
        case 7: p=&mandmap;     break;
        case 8: p=&phong;       break;
        case 9: p=&rose;        break;
        default:p=&blues;       break;
    }
    iter=iter & 255;                    // upravit pocet iteraci do rozsahu 0..255
    *r=(*p)[iter][0];                   // vyber a predani barvovych slozek
    *g=(*p)[iter][1];
    *b=(*p)[iter][2];
}



//-----------------------------------------------------------------------------
// Prekresleni Mandelbrotovy mnoziny
//-----------------------------------------------------------------------------
void recalcMandelbrot( pixmap *pix,             // pixmapa pro vykreslovani
                  int    maxiter,               // maximalni pocet iteraci
                  double scale,                 // meritko obrazce
                  double xpos,                  // posun obrazce
                  double ypos,
                  int    palette,               // barvova paleta
                  int    rf, int gf, int bf)    // priznaky barvovych slozek
{
    double zx, zy, zx2, zy2;                    // slozky komplexni promenne Z a Z^2
    double cx, cy;                              // slozky komplexni konstanty C
    double cx0, cy0;
    double xmin, ymin, xmax, ymax;              // rohy vykreslovaneho obrazce v komplexni
                                                // rovine
    int    x, y;                                // pocitadla sloupcu a radku v pixmape
    int    iter;                                // pocitadlo iteraci
    unsigned char r, g, b;

    calcCorner(xpos, ypos, scale, &xmin, &ymin, &xmax, &ymax);
    cy0=ymin;
    for (y=0; y<pix->height; y++) {             // pro vsechny radky v pixmape
        cx0=xmin;
        for (x=0; x<pix->width; x++) {          // pro vsechny pixely na radku
            cx=cx0;                             // nastavit pocatecni hodnotu Z(0)
            cy=cy0;
            zx=zy=0.0;                          // nastaveni nuloveho orbitu
            for (iter=0; iter<maxiter; iter++) {// iteracni smycka
                zx2=zx*zx;                      // zkraceny vypocet druhe mocniny slozek Z
                zy2=zy*zy;
                if (zx2+zy2>4.0) break;         // kontrola prekroceni meze divergence
                zy=2.0*zx*zy+cy;                // vypocet Z(n+1)
                zx=zx2-zy2+cx;
            }
            if (iter==maxiter)                  // pixely uvnitr Mandelbrotovy
                r=g=b=0;                        // mnoziny jsou cerne
            else                                // vypocet barev podle poctu iteraci
                mapPalette(palette, iter, &r, &g, &b);
            r=r*rf;                             // uzivatelem rizene vynulovani
            g=g*gf;                             // vybranych barvovych slozek
            b=b*bf;
            putpixel(pix, x, y, r, g, b);
            cx0+=(xmax-xmin)/pix->width;        // posun na dalsi bod na radku
        }
        cy0+=(ymax-ymin)/pix->height;           // posun na dalsi radek
    }
}



//-----------------------------------------------------------------------------
// Vykresleni retezce na obrazovku
//-----------------------------------------------------------------------------
void drawString(const int x, const int y,                    // umisteni retezce
                const float r, const float g, const float b, // barva pisma
                char *str)                                   // ukazatel na retezec
{
    char *c;
    glColor3f(r, g, b);
    glRasterPos2i(x, y);
    for (c=str; *c!=0; c++) {
        glutBitmapCharacter(GLUT_BITMAP_9_BY_15, *c);
    }
}



//-----------------------------------------------------------------------------
// Vypis informaci o vykreslovanem fraktalu
//-----------------------------------------------------------------------------
void drawInfo(int maxiter, double xpos, double ypos, double scale, int palette, int r, int g, int b)
{
    char str[100];
    char *palNames[10]={
        "blues",
        "gold",
        "greens",
        "ice",
        "juteblue",
        "jutemap",
        "jutes",
        "mandmap",
        "phong",
        "rose",
    };
    sprintf(str, "[R][G][B]  colors = %d %d %d", r, g, b);
    drawString(11, 79, 0.0, 0.0, 0.0, str);     // stinovani
    drawString(10, 80, 0.6, 1.0, 0.6, str);
    sprintf(str, "[0]-[9]    palette = %s", palNames[palette]);
    drawString(11, 63, 0.0, 0.0, 0.0, str);
    drawString(10, 64, 0.6, 1.0, 0.6, str);
    sprintf(str, "[<][>]     maxiter = %d", maxiter);
    drawString(11, 47, 0.0, 0.0, 0.0, str);
    drawString(10, 48, 0.6, 1.0, 0.6, str);
    sprintf(str, "[PgUp][PgDn]  scale = %4.2f ", scale);
    drawString(11, 31, 0.0, 0.0, 0.0, str);
    drawString(10, 32, 0.4, 1.0, 0.8, str);
    sprintf(str, "[Arrows]  pan = %5.3f, %5.3f", xpos, ypos);
    drawString(11, 15, 0.0, 0.0, 0.0, str);
    drawString(10, 16, 0.2, 1.0, 1.0, str);
}



//-----------------------------------------------------------------------------
// Funkce volana pro inicializaci vykreslovani
//-----------------------------------------------------------------------------
void onInit(void)
{
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);       // barva pozadi
    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);      // mod ulozeni pixelu

    pix=createPixmap(PIXMAP_WIDTH, PIXMAP_HEIGHT);
}



//-----------------------------------------------------------------------------
// Nastaveni souradneho systemu v zavislosti na velikosti okna
//-----------------------------------------------------------------------------
void onResize(int w, int h)                     // argumenty w a h reprezentuji novou velikost okna
{
    glViewport(0, 0, w, h);                     // viditelna oblast pres cele okno
    glMatrixMode(GL_PROJECTION);                // zacatek modifikace projekcni matice
    glLoadIdentity();                           // vymazani projekcni matice (=identita)
    glOrtho(0, w, 0, h, -1, 1);                 // mapovani abstraktnich souradnic do souradnic okna
}



//-----------------------------------------------------------------------------
// Tato callback funkce je zavolana pri kazdem prekresleni okna
//-----------------------------------------------------------------------------
void onDisplay(void)
{
    glClear(GL_COLOR_BUFFER_BIT);               // vymazani vsech bitovych rovin barvoveho bufferu
    glDrawBuffer(GL_BACK);                      // pixmapa se bude kreslit do zadniho barvoveho bufferu
    glRasterPos2i(0, 0);                        // nastaveni souradnic leveho spodniho rohu pixmapy
    clearPixmap(pix);                           // vymazani pracovni pixmapy (nastaveni pixelu na cernou barvu)
    recalcMandelbrot(pix, maxiter, scale, xpos, ypos, palette, rf, gf, bf);// prepocet fraktalu
    drawPixmap(pix);                            // vykresleni pixmapy
    drawInfo(maxiter, xpos, ypos, scale, palette, rf, gf, bf);
    glFlush();                                  // provedeni a vykresleni vsech zmen
    glutSwapBuffers();
}



//-----------------------------------------------------------------------------
// Tato callback funkce je zavolana pri stlaceni ASCII klavesy
//-----------------------------------------------------------------------------
#ifdef __BORLANDC__
#pragma option -w-par
#endif
void onKeyboard(unsigned char key, int x, int y)
{
    key=(key>='A' && key<='Z') ? key-'A'+'a': key;
    if (key>='0' && key<='9') { palette=key-'0'; glutPostRedisplay(); }       // nastaveni barvove palety
    switch (key) {
        case 27:                                                              // pokud byla stlacena klavesa ESC, konec programu
        case 'q': exit(0);                                             break; // totez co klavesa ESC
        case 's': savePixmap(pix, FILE_NAME);                          break; // ulozeni pixmapy
        case '<': if (maxiter>10)   maxiter-=10; glutPostRedisplay();  break;
        case ',': if (maxiter>1)    maxiter--;   glutPostRedisplay();  break; // zmena poctu iteraci
        case '>': if (maxiter<246)  maxiter+=10; glutPostRedisplay();  break;
        case '.': if (maxiter<255)  maxiter++;   glutPostRedisplay();  break;
        case 'r': rf=!rf;                        glutPostRedisplay();  break; // priznaky barvovych slozek
        case 'g': gf=!gf;                        glutPostRedisplay();  break;
        case 'b': bf=!bf;                        glutPostRedisplay();  break;
        default:                                                       break;
    }
}
#ifdef __BORLANDC__
#pragma option -w+par
#endif



//-----------------------------------------------------------------------------
// Tato callback funkce je zavolana pri stlaceni non-ASCII klavesy
//-----------------------------------------------------------------------------
#ifdef __BORLANDC__
#pragma option -w-par
#endif
void onSpecial(int key, int x, int y)
{
    // posun fraktalu a zmena meritka
    switch (key) {
        case GLUT_KEY_LEFT:      xpos-=0.05/scale; glutPostRedisplay();           break;
        case GLUT_KEY_RIGHT:     xpos+=0.05/scale; glutPostRedisplay();           break;
        case GLUT_KEY_UP:        ypos+=0.05/scale; glutPostRedisplay();           break;
        case GLUT_KEY_DOWN:      ypos-=0.05/scale; glutPostRedisplay();           break;
        case GLUT_KEY_PAGE_UP:   scale*=1.1; glutPostRedisplay();                 break;
        case GLUT_KEY_PAGE_DOWN: if (scale>1) scale/=1.1; glutPostRedisplay();    break;
        default:                                                                  break;
    }
}
#ifdef __BORLANDC__
#pragma option -w+par
#endif



//-----------------------------------------------------------------------------
// Hlavni funkce konzolove aplikace
//-----------------------------------------------------------------------------
int main(int argc, char **argv)
{
    glutInit(&argc, argv);                      // inicializace knihovny GLUT
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
    glutCreateWindow(WINDOW_TITLE);             // vytvoreni okna pro kresleni
    glutReshapeWindow(WINDOW_WIDTH, WINDOW_HEIGHT);// zmena velikosti okna
    glutPositionWindow(100, 100);               // pozice leveho horniho rohu okna
    glutDisplayFunc(onDisplay);                 // registrace funkce volane pri prekreslovani okna
    glutReshapeFunc(onResize);                  // registrace funkce volane pri zmene velikosti okna
    glutKeyboardFunc(onKeyboard);               // registrace funkce volane pri stlaceni klavesy
    glutSpecialFunc(onSpecial);                 // registrace funkce volane pri stlaceni specialni klavesy
    onInit();                                   // inicializace vykreslovani
    glutMainLoop();                             // nekonecna smycka, kde se volaji zaregistrovane funkce
    return 0;                                   // navratova hodnota vracena operacnimu systemu
}



//-----------------------------------------------------------------------------
// finito
//-----------------------------------------------------------------------------