/*
* =====================================================================================
*
* 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, double depth)
{
if (write) {
signed char a;
static signed char previous_a;
if (parent) {
a = (((signed int)parent->avg - (signed int)n->avg));// / (depth);
//if (a < 32 && a > -32)//127)
//printf("%i(%i,%i),", a, (signed char)n->avg, (signed int)parent->avg + (signed int)n->avg);
// printf("%i,", a);//, parent->avg);//(signed char)n->avg, (signed int)parent->avg + (signed int)n->avg);
signed char b = previous_a;
previous_a = a;
//a -= b;
//a = (signed int)n->avg;
printf("%i,", a);//, parent->avg);//(signed char)n->avg, (signed int)parent->avg + (signed int)n->avg);
} else {
a = n->avg;
previous_a = 0;
}
/*
unsigned char shft = 0;
if (a > 16 || a < -16)
shft = 1;
if (a > 32 || a < -32)
shft = 2;
if (a > 64 || a < -64)
shft = 1;
a >>= shft;
a <<= 1;
a |= shft;
*/
/*
if (a > 32)//127)
printf("%i,", a);
if (a < -32)//-127)
printf("%i,", a);
//if ( depth <= 16 ) {
//printf("%i,", a);
//fwrite(&a, 4, 1, f);
if (a > 32)//127)
printf("%i,", a);
if (a < -32)//-127)
printf("%i,", a);
//}
*/
signed char b = a;
fwrite(&b, 1, 1, f);
}
if (n->nodeA && n->nodeB) {
outputTree(f, n->nodeA, n, true, depth + 1);
outputTree(f, n->nodeB, n, false, depth + 1);
}
}
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, 0);
/*
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();
}