/*
* =====================================================================================
*
* 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;
};
#include <limits.h>
signed char sclamp(signed int i)
{
if (i < SCHAR_MIN)
return SCHAR_MIN;
if (i > SCHAR_MAX)
return SCHAR_MAX;
return i;
}
unsigned char uclamp(unsigned int i)
{
if (i > 2*CHAR_MAX)
return 2*CHAR_MAX;
return i;
}
// unsigned char
void imageFromTree(Node *n, int s, int w, int h, unsigned char *image, int x1, int y1)
{
if (w == 1 && h == 1) {
image[s*y1+x1] = uclamp(n->avg);
return;
}
if (n->nodeA && n->nodeB) {
if (w >= h) {
imageFromTree(n->nodeA, s, w/2, h, image, x1, y1);
imageFromTree(n->nodeB, s, w/2, h, image, x1+w/2, y1);
} else {
imageFromTree(n->nodeA, s, w, h/2, image, x1, y1);
imageFromTree(n->nodeB, s, w, h/2, image, x1, y1+h/2);
}
}
//n->avg = (n->nodeA->avg + n->nodeB->avg) / 2;
}
//void outputTree(FILE *f, Node *n, Node *parent, bool write)
//void readTree(FILE *f, Node *n, Node *parent, int w, int h, bool write)
void readTree(FILE *f, Node *n, Node *parent, bool write, int d)
{
if (write) {
//fread(&a, 4, 1, f);
//signed char a = (signed char)A;
//printf("%i,", a);
if (parent) {
signed char b;
fread(&b, 1, 1, f);
signed int a = b;
n->avg = ((signed int)parent->avg - (signed int)a);
} else {
unsigned char b;
fread(&b, 1, 1, f);
signed int a = b;
n->avg = a;
}
} else {
signed int a = parent->nodeA->avg;
n->avg = ((signed int)((signed int)parent->avg)*2 - (signed int)a);
}
if (d > 1) {
n->nodeA = new Node;
n->nodeB = new Node;
readTree(f, n->nodeA, n, true, d/2);
readTree(f, n->nodeB, n, false, d/2);
} else {
n->nodeA = 0;
n->nodeB = 0;
}
/*
if (w == 1 && h == 1) {
n->nodeA = 0;
n->nodeB = 0;
} else {
if (w >= h) {
readTree(f, n->nodeA, n, w/2, h, true);
readTree(f, n->nodeB, n, w/2, h, false);
} else {
readTree(f, n->nodeA, n, w, h/2, true);
readTree(f, n->nodeB, n, w, h/2, false);
}
}
*/
}
void dumpImage(int s, unsigned char *image, int depth)
{
QImage outputImage(s, s, QImage::Format_RGB888);
for (int y = 0; y < s; y++) {
for (int x = 0; x < s; x++) {
unsigned char p = image[y*s+x];
unsigned int p2 = p;
p2 = (p2 << 16) | (p2 << 8) | p2;
outputImage.setPixel(x, y, p2);
}
}
outputImage.save(QString("output%1.png").arg(depth));
}
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");
Node headNode;
FILE *f = fopen("blah.out", "r");
//readTree(f, &headNode, 0, 512, 512, true);
readTree(f, &headNode, 0, true, 512*512);
fclose(f);
printf("read tree\n");
for (int depth = 0; depth < 10; depth++) {
int s = 512 >> depth;
memset(image, 0, size);
imageFromTree(&headNode, s, s, s, image, 0, 0);
dumpImage(s, image, depth);
}
return 0;//app.exec();
}