/*
 * =====================================================================================
 *
 *       Filename:  build-table.cpp
 *
 *    Description:  
 *
 *        Version:  1.0
 *        Created:  03/04/2011 22:11:22
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  John Ryland (jryland), jryland@xiaofrog.com
 *        Company:  InvertedLogic
 *
 * =====================================================================================
 */
#include <QApplication>
#include <QPixmap>
#include <QImage>
#include <stdio.h>


typedef struct Node {
    signed int avg;
    Node *nodeA;
    Node *nodeB;
};


// unsigned char 
void buildTree(Node *n, int w, int h, unsigned char *image, int x1, int y1)
{
    if (w == 1 && h == 1) {
        n->avg = image[512*y1+x1];
        n->nodeA = 0;
        n->nodeB = 0;
        //return n->avg;
        return;
    }
    n->nodeA = new Node;
    n->nodeB = new Node;
    //unsigned int a, b;
    if (w >= h) {
        buildTree(n->nodeA, w/2, h, image, x1, y1);
        buildTree(n->nodeB, w/2, h, image, x1+w/2, y1);
    } else {
        buildTree(n->nodeA, w, h/2, image, x1, y1);
        buildTree(n->nodeB, w, h/2, image, x1, y1+h/2);
    }
    n->avg = (((signed int)n->nodeA->avg + (signed int)n->nodeB->avg + 1) / 2);
    //return n->avg;
}


void outputTree(FILE *f, Node *n, Node *parent, bool write)
{
    if (write) {
        signed int a;
        if (parent) {
            a = ((signed int)parent->avg - (signed int)n->avg);
        } else {
            a = n->avg;
        }
        //printf("%i,", a);
        //fwrite(&a, 4, 1, f);
        if (a > 127)
            printf("%i,", a);
        if (a < -127)
            printf("%i,", a);
            
        signed char b = a;
        fwrite(&b, 1, 1, f);
    }
    if (n->nodeA && n->nodeB) {
        outputTree(f, n->nodeA, n, true);
        outputTree(f, n->nodeB, n, false);
    }
}


int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

printf("starting\n");

    const int size = 512*512;
    unsigned char *image = (unsigned char *)malloc(size);
printf("alloced mem\n");
    memset(image, 0, size);
printf("zeroed mem\n");

    QImage pix( QString("test01.png") );
    int w = pix.width();
    int h = pix.height();

    for (int y = 0; y < h; y++) {
        for (int x = 0; x < w; x++) {
            image[y*w+x] = pix.pixel(x,y);
        }
    }

    Node headNode;
    //unsigned char avg = 
    buildTree(&headNode, 512, 512, image, 0, 0);
    printf("average = %i\n", headNode.avg);

    FILE *f = fopen("blah.out", "w");
printf("opened output\n");
    outputTree(f, &headNode, 0, true);
/*
    for (int i = 0; i < size; i++) {
        unsigned char p = image[i];
        fwrite(&i, 1, 1, f);
    }
*/
printf("written output\n");
    fclose(f);
printf("closed output\n");

    QImage outputImage(512, 512, QImage::Format_RGB888);
    for (int y = 0; y < h; y++) {
        for (int x = 0; x < w; x++) {
            unsigned char p = image[y*w+x];
            unsigned int p2 = p;
            p2 = (p2 << 16) | (p2 << 8) | p2;
            outputImage.setPixel(x, y, p2);
        }
    }
    outputImage.save("output.png");

    return 0;//app.exec();
}



