2004-09-17 17:25:59 +02:00
|
|
|
#include "patch.h"
|
2004-10-10 19:36:43 +02:00
|
|
|
#ifdef WIN32
|
|
|
|
#include "minilzo.108/minilzo.h"
|
|
|
|
#else
|
2004-10-10 19:19:42 +02:00
|
|
|
#include <lzo1x.h>
|
2004-10-10 19:36:43 +02:00
|
|
|
#endif
|
2004-10-10 19:19:42 +02:00
|
|
|
#include <iostream>
|
|
|
|
using namespace std;
|
2004-09-17 17:25:59 +02:00
|
|
|
using namespace nxs;
|
|
|
|
|
2004-10-10 19:36:43 +02:00
|
|
|
#ifdef WIN32
|
|
|
|
static double wrkmem[LZO1X_1_MEM_COMPRESS/sizeof(double) +1];
|
|
|
|
#else
|
2004-10-10 19:19:42 +02:00
|
|
|
static double wrkmem[LZO1X_999_MEM_COMPRESS/sizeof(double) +1];
|
2004-10-10 19:36:43 +02:00
|
|
|
#endif
|
2004-10-10 19:19:42 +02:00
|
|
|
|
2004-10-15 13:41:03 +02:00
|
|
|
|
2004-10-01 18:54:57 +02:00
|
|
|
void pad(unsigned int &size) {
|
|
|
|
while(size&0x3) size++;
|
|
|
|
}
|
2004-09-17 17:25:59 +02:00
|
|
|
|
2004-10-11 18:04:18 +02:00
|
|
|
void shuffle(float *buffer, unsigned int size, unsigned int stride) {
|
|
|
|
float *tmp = new float[size];
|
|
|
|
unsigned int count = 0;
|
|
|
|
|
|
|
|
unsigned int nelem = size/stride;
|
|
|
|
for(unsigned int s = 0; s < stride; s++) {
|
|
|
|
float *ptr = buffer + s;
|
|
|
|
for(unsigned int i = 0; i < nelem; i++) {
|
|
|
|
tmp[count++] = *ptr;
|
|
|
|
ptr += stride;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
memcpy(buffer, tmp, size * sizeof(float));
|
|
|
|
delete []tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
void unshuffle(float *buffer, unsigned int size, unsigned int stride) {
|
|
|
|
float *tmp = new float[size];
|
|
|
|
|
|
|
|
unsigned int count = 0;
|
|
|
|
unsigned int nelem = size/stride;
|
|
|
|
for(unsigned int s = 0; s < stride; s++) {
|
|
|
|
float *ptr = tmp + s;
|
|
|
|
for(unsigned int i = 0; i < nelem; i++) {
|
|
|
|
*ptr = buffer[count++];
|
|
|
|
ptr += stride;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
memcpy(buffer, tmp, size * sizeof(float));
|
|
|
|
delete []tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
void subtract(float *buffer, unsigned int size) {
|
|
|
|
float p = buffer[0];
|
|
|
|
float q;
|
|
|
|
for(unsigned int i = 1; i < size; i++) {
|
|
|
|
q = buffer[i];
|
|
|
|
buffer[i] -= p;
|
|
|
|
p = q;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void unsubtract(float *buffer, unsigned int size) {
|
|
|
|
for(unsigned int i = 1; i < size; i++)
|
|
|
|
buffer[i] += buffer[i-1];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-10-08 17:12:04 +02:00
|
|
|
Patch::Patch(Signature signature, char *s,
|
2004-09-17 17:25:59 +02:00
|
|
|
unsigned short nvert, unsigned short nface):
|
|
|
|
start(s) {
|
|
|
|
Init(signature, nvert, nface);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Patch::Init(Signature signature,
|
|
|
|
unsigned short nvert, unsigned short nface) {
|
|
|
|
nv = nvert;
|
|
|
|
nf = nface;
|
|
|
|
|
2004-09-30 02:27:42 +02:00
|
|
|
if(signature & NXS_FACES)
|
2004-09-17 17:25:59 +02:00
|
|
|
vstart = (float *)(((char *)start) + nf * sizeof(unsigned short) * 3);
|
2004-09-30 02:27:42 +02:00
|
|
|
else if(signature & NXS_STRIP)
|
2004-09-17 17:25:59 +02:00
|
|
|
vstart = (float *)(((char *)start) + nf * sizeof(unsigned short));
|
|
|
|
else
|
|
|
|
vstart = (float *)start;
|
|
|
|
|
|
|
|
//align memory
|
|
|
|
if(((int)vstart) & 0x2) vstart = (float *)(((char *)vstart) + 2);
|
|
|
|
|
2004-10-01 18:54:57 +02:00
|
|
|
cstart = nv * 3;
|
|
|
|
if(signature & NXS_COLORS)
|
|
|
|
nstart = cstart + nv;
|
|
|
|
else
|
|
|
|
nstart = cstart;
|
|
|
|
|
|
|
|
if(signature & NXS_NORMALS_SHORT)
|
|
|
|
tstart = nstart + nv * 2;
|
|
|
|
else if(signature & NXS_NORMALS_FLOAT)
|
|
|
|
tstart = nstart + nv * 3;
|
|
|
|
else
|
|
|
|
tstart = nstart;
|
|
|
|
|
|
|
|
if(signature & NXS_TEXTURES_SHORT)
|
|
|
|
dstart = tstart + nv;
|
|
|
|
else if(signature & NXS_TEXTURES_FLOAT)
|
|
|
|
dstart = tstart + nv;
|
|
|
|
else
|
|
|
|
dstart = tstart;
|
2004-09-17 17:25:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int Patch::ChunkSize(Signature signature,
|
|
|
|
unsigned short nvert,
|
2004-10-08 17:12:04 +02:00
|
|
|
unsigned short nface,
|
|
|
|
unsigned int chunk_size) {
|
2004-09-17 17:25:59 +02:00
|
|
|
unsigned int size = ByteSize(signature, nvert, nface);
|
2004-10-08 17:12:04 +02:00
|
|
|
size = (size/chunk_size + 1);
|
2004-09-17 17:25:59 +02:00
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int Patch::ByteSize(Signature signature,
|
|
|
|
unsigned short nvert,
|
|
|
|
unsigned short nface) {
|
|
|
|
unsigned int size = 0;
|
2004-09-30 02:27:42 +02:00
|
|
|
if(signature & NXS_STRIP)
|
2004-09-17 17:25:59 +02:00
|
|
|
size += nface * sizeof(unsigned short);
|
2004-09-30 02:27:42 +02:00
|
|
|
else if(signature & NXS_FACES)
|
2004-09-17 17:25:59 +02:00
|
|
|
size += nface * 3 * sizeof(unsigned short);
|
|
|
|
|
|
|
|
//memory alignment
|
2004-10-01 18:54:57 +02:00
|
|
|
pad(size);
|
2004-09-17 17:25:59 +02:00
|
|
|
|
|
|
|
size += nvert * sizeof(vcg::Point3f);
|
|
|
|
|
2004-09-30 02:27:42 +02:00
|
|
|
if(signature & NXS_COLORS)
|
2004-09-17 17:25:59 +02:00
|
|
|
size += nvert * sizeof(unsigned int);
|
|
|
|
|
2004-09-30 02:27:42 +02:00
|
|
|
if(signature & NXS_NORMALS_SHORT)
|
2004-09-17 17:25:59 +02:00
|
|
|
size += nvert * 4 * sizeof(short);
|
|
|
|
|
2004-09-30 02:27:42 +02:00
|
|
|
if(signature & NXS_NORMALS_FLOAT)
|
2004-09-17 17:25:59 +02:00
|
|
|
size += nvert * 3 * sizeof(float);
|
|
|
|
|
2004-09-30 02:27:42 +02:00
|
|
|
if(signature & NXS_TEXTURES_SHORT)
|
2004-09-17 17:25:59 +02:00
|
|
|
size += nvert * 2 * sizeof(short);
|
|
|
|
|
2004-09-30 02:27:42 +02:00
|
|
|
if(signature & NXS_TEXTURES_FLOAT)
|
2004-09-17 17:25:59 +02:00
|
|
|
size += nvert * 2 * sizeof(float);
|
|
|
|
|
2004-09-30 02:27:42 +02:00
|
|
|
if(signature & NXS_DATA8)
|
2004-09-17 17:25:59 +02:00
|
|
|
size += nvert * sizeof(char);
|
2004-10-01 18:54:57 +02:00
|
|
|
pad(size);
|
2004-09-30 02:27:42 +02:00
|
|
|
if(signature & NXS_DATA16)
|
2004-09-17 17:25:59 +02:00
|
|
|
size += nvert * 2 * sizeof(char);
|
2004-10-01 18:54:57 +02:00
|
|
|
pad(size);
|
2004-09-30 02:27:42 +02:00
|
|
|
if(signature & NXS_DATA32)
|
2004-09-17 17:25:59 +02:00
|
|
|
size += nvert * 4 * sizeof(char);
|
2004-09-30 02:27:42 +02:00
|
|
|
if(signature & NXS_DATA64)
|
2004-09-17 17:25:59 +02:00
|
|
|
size += nvert * 8 * sizeof(char);
|
|
|
|
|
|
|
|
|
|
|
|
//this condition should really rarely happen but helps save space
|
|
|
|
//during construction
|
|
|
|
if(size < nface * 3 * sizeof(unsigned int))
|
|
|
|
size = nface * 3 * sizeof(unsigned int);
|
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
2004-10-10 19:19:42 +02:00
|
|
|
|
|
|
|
|
|
|
|
char *Patch::Compress(unsigned int ram_size, unsigned int &size) {
|
2004-10-10 19:36:43 +02:00
|
|
|
|
2004-10-11 18:04:18 +02:00
|
|
|
//lets use differences
|
|
|
|
// shuffle((float *)VertBegin(), nv * 3, 3);
|
|
|
|
// subtract((float *)VertBegin(), nv * 3);
|
2004-10-10 19:36:43 +02:00
|
|
|
|
2004-10-10 19:19:42 +02:00
|
|
|
//TODO use OVERLAP and test speed
|
|
|
|
//TODO fill chunk padding with zeroes?
|
|
|
|
//TODO compress only used memory!
|
|
|
|
size = ram_size + ram_size/64 + 23;
|
|
|
|
char *buffer = new char[size];
|
2004-10-10 19:36:43 +02:00
|
|
|
#ifdef WIN32
|
|
|
|
lzo1x_1_compress(((unsigned char *)start), ram_size,
|
2004-10-10 19:19:42 +02:00
|
|
|
(unsigned char *)buffer + sizeof(int), &size,
|
|
|
|
(char *)wrkmem);
|
2004-10-10 19:36:43 +02:00
|
|
|
#else
|
2004-10-15 13:41:03 +02:00
|
|
|
lzo1x_999_compress(((unsigned char *)start), ram_size,
|
|
|
|
(unsigned char *)buffer + sizeof(int), &size,
|
2004-10-10 19:36:43 +02:00
|
|
|
(char *)wrkmem);
|
2004-10-15 13:41:03 +02:00
|
|
|
|
|
|
|
lzo1x_optimize((unsigned char *)buffer + sizeof(int), size,
|
|
|
|
((unsigned char *)start), &ram_size,
|
|
|
|
NULL);
|
2004-10-10 19:36:43 +02:00
|
|
|
#endif
|
|
|
|
|
2004-10-10 19:19:42 +02:00
|
|
|
*(int *)buffer = size;
|
|
|
|
size += sizeof(int);
|
|
|
|
|
|
|
|
// memcpy(buffer, start, ram_size);
|
|
|
|
// size = ram_size;
|
2004-10-15 13:41:03 +02:00
|
|
|
|
2004-10-10 19:19:42 +02:00
|
|
|
return buffer;
|
2004-10-10 19:36:43 +02:00
|
|
|
|
2004-10-10 19:19:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Patch::Decompress(unsigned int ram_size, void *src, unsigned int src_sz) {
|
2004-10-10 19:36:43 +02:00
|
|
|
|
2004-10-10 19:19:42 +02:00
|
|
|
unsigned int size = *(int *)src;
|
|
|
|
assert(size < src_sz + sizeof(int));
|
|
|
|
unsigned int dst_size = ram_size;
|
2004-10-10 19:36:43 +02:00
|
|
|
|
2004-10-10 19:19:42 +02:00
|
|
|
int ret = lzo1x_decompress_safe(((unsigned char *)src) + sizeof(int), size,
|
|
|
|
(unsigned char *)start, &dst_size, 0);
|
|
|
|
if(ret != 0) {
|
|
|
|
cerr << "Ret from decompress: " << ret << endl;
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
assert(dst_size == ram_size);
|
|
|
|
//TODO add 3 to start... so we can use asm_fast decompressor
|
2004-10-11 18:04:18 +02:00
|
|
|
|
|
|
|
// unsubtract((float *)VertBegin(), nv * 3);
|
|
|
|
// unshuffle((float *)VertBegin(), nv * 3, 3);
|
2004-10-10 19:19:42 +02:00
|
|
|
}
|
|
|
|
|