cleanups, brought volume file format up to spec

metadata
Wenzel Jakob 2010-09-02 03:25:53 +02:00
parent 575beeb792
commit e0b52ef022
4 changed files with 14 additions and 178 deletions

View File

@ -390,8 +390,6 @@ if sys.platform == 'darwin':
env.Program('src/utils/utils_test', ['src/utils/utils_test.cpp'])
env.Program('src/utils/joinrgb', ['src/utils/joinrgb.cpp'])
env.Program('src/utils/ssalbedo', ['src/utils/ssalbedo.cpp'])
env.Program('src/utils/dumpimage', ['src/utils/dumpimage.cpp'])
env.Program('src/utils/ttest', ['src/utils/ttest.cpp'])
env.Program('src/utils/createvol', ['src/utils/createvol.cpp'])

View File

@ -1,80 +0,0 @@
#include <mitsuba/core/bitmap.h>
#include <mitsuba/core/fstream.h>
#include <iomanip>
using namespace mitsuba;
Bitmap *downsample(const Bitmap *in) {
Bitmap *result = new Bitmap(in->getWidth()/2, in->getHeight()/2, 128);
const float *data = in->getFloatData();
float *out = result->getFloatData();
result->clear();
for (int y=0; y<in->getWidth()/2; ++y) {
for (int x=0; x<in->getWidth()/2; ++x) {
Float value1 = data[(2*x + 2*y * in->getWidth()) * 4];
Float value2 = data[(2*x + (2*y+1) * in->getWidth()) * 4];
Float value3 = data[(2*x+1 + 2*y * in->getWidth()) * 4];
Float value4 = data[(2*x+1 + (2*y+1) * in->getWidth()) * 4];
Float avg = (value1+value2+value3+value4)/4;
out[(x + y * result->getWidth()) * 4 + 0] = avg;
out[(x + y * result->getWidth()) * 4 + 1] = avg;
out[(x + y * result->getWidth()) * 4 + 2] = avg;
out[(x + y * result->getWidth()) * 4 + 3] = 1;
}
}
return result;
}
void dumpImage(const std::string &s1) {
ref<FileStream> stream = new FileStream(s1, FileStream::EReadOnly);
ref<Bitmap> bitmap = new Bitmap(Bitmap::EEXR, stream);
// bitmap = downsample(bitmap);
// bitmap = downsample(bitmap);
stream = new FileStream("downsampled.exr", FileStream::ETruncReadWrite);
bitmap->save(Bitmap::EEXR, stream);
float *data = bitmap->getFloatData();
cout << "A={" << endl;
cout << std::fixed << endl;
cout << std::setprecision(12)<< endl;
for (int y=0; y<bitmap->getWidth(); ++y) {
cout << "\t{";
for (int x=0; x<bitmap->getWidth(); ++x) {
cout << data[(x + y * bitmap->getWidth()) * 4];
if (x+1 < bitmap->getWidth())
cout << ", ";
}
cout << "}";
if (y+1 < bitmap->getHeight())
cout << ",";
cout << endl;
}
cout << "};";
}
int main(int argc, char **argv) {
Class::staticInitialization();
Thread::staticInitialization();
Logger::staticInitialization();
Spectrum::staticInitialization();
try {
if (argc < 2) {
cout << "dumpimage <image.exr>" << endl;
} else {
dumpImage(argv[1]);
}
} catch (const std::exception &e) {
std::cerr << "Caught a critical exeption: " << e.what() << std::endl;
exit(-1);
} catch (...) {
std::cerr << "Caught a critical exeption of unknown type! " << std::endl;
exit(-1);
}
Spectrum::staticShutdown();
Logger::staticShutdown();
Thread::staticShutdown();
Class::staticShutdown();
return 0;
}

View File

@ -1,94 +0,0 @@
#include <mitsuba/core/aabb.h>
#include <mitsuba/core/fstream.h>
using namespace mitsuba;
Float solveSSAlbedo(Float diffAlbedo) {
if (diffAlbedo == 1 || diffAlbedo == 0)
return diffAlbedo;
if (diffAlbedo < 0 || diffAlbedo > 1) {
cout << "Overflow: "<< diffAlbedo << "!" << endl;
diffAlbedo = std::max((Float) 0, std::min((Float) 1, diffAlbedo));
}
Float l = 0, r = 1;
while (true) {
Float m = (l+r)/2,
fm = m/2*(1+std::exp(-4.0/3.0*std::sqrt(3*(1-m))))*std::exp(-std::sqrt(3*(1-m)));
if (fm < diffAlbedo)
l = m;
else
r = m;
if (std::abs(fm-diffAlbedo) < 1e-3)
return m;
}
}
void computeSSAlbedo(const std::string &filename, const std::string target) {
ref<FileStream> stream = new FileStream(filename, FileStream::EReadOnly);
stream->setByteOrder(Stream::ELittleEndian);
int xres = stream->readInt(), yres=stream->readInt(), zres=stream->readInt();
Vector3i res = Vector3i(xres, yres, zres);
int channels = stream->readInt();
size_t nEntries = res.x*res.y*res.z*channels;
Float xmin = stream->readSingle(), ymin = stream->readSingle(), zmin = stream->readSingle();
Float xmax = stream->readSingle(), ymax = stream->readSingle(), zmax = stream->readSingle();
AABB aabb(Point(xmin, ymin, zmin), Point(xmax, ymax, zmax));
SLog(EInfo, "Loading \"%s\": %ix%ix%i (%i channels), %i KiB, %s", filename.c_str(),
res.x, res.y, res.z, channels, nEntries*sizeof(float)/1024,
aabb.toString().c_str());
float *data = new float[nEntries];
stream->read(data, nEntries*sizeof(float));
stream->close();
SLog(EInfo, "Computing single scattering albedo ..");
for (size_t i=0; i<nEntries; ++i)
data[i] = solveSSAlbedo(data[i]);
SLog(EInfo, "Saving \"%s\": %ix%ix%i (%i channels), %i KiB, %s", target.c_str(),
res.x, res.y, res.z, channels, nEntries*sizeof(float)/1024,
aabb.toString().c_str());
ref<FileStream> targetStream = new FileStream(target, FileStream::ETruncReadWrite);
targetStream->setByteOrder(Stream::ELittleEndian);
res.serialize(targetStream);
targetStream->writeInt(channels);
targetStream->writeSingle(xmin); targetStream->writeSingle(ymin); targetStream->writeSingle(zmin);
targetStream->writeSingle(xmax); targetStream->writeSingle(ymax); targetStream->writeSingle(zmax);
targetStream->write(data, nEntries*sizeof(float));
targetStream->close();
}
int main(int argc, char **argv) {
Class::staticInitialization();
Thread::staticInitialization();
Logger::staticInitialization();
Spectrum::staticInitialization();
try {
if (argc != 3) {
cout << "Converts a volume of diffuse color values to " << endl;
cout << "a single scattering albedo volume by numerically" << endl;
cout << "inverting (2.4) in the Jensen et al. BSSRDF paper" << endl;
cout << "Syntax: ssalbedo <source.vol> <target.vol>" << endl;
} else {
computeSSAlbedo(argv[1], argv[2]);
}
} catch (const std::exception &e) {
std::cerr << "Caught a critical exeption: " << e.what() << std::endl;
exit(-1);
} catch (...) {
std::cerr << "Caught a critical exeption of unknown type! " << std::endl;
exit(-1);
}
Spectrum::staticShutdown();
Logger::staticShutdown();
Thread::staticShutdown();
Class::staticShutdown();
return 0;
}

View File

@ -124,6 +124,18 @@ public:
m_filename = filename;
m_fromStream = false;
char header[3];
stream->read(header, 3);
if (header[0] != 'V' || header[1] != 'O' || header[2] != 'L')
Log(EError, "Encountered an invalid volume data file (incorrect header identifier)");
uint8_t version;
stream->read(&version, 1);
if (version != 3)
Log(EError, "Encountered an invalid volume data file (incorrect file version)");
int type = stream->readInt();
if (type != 1)
Log(EError, "Encountered an invalid volume data file (incorrect data type)");
int xres = stream->readInt(), yres=stream->readInt(), zres=stream->readInt();
m_res = Vector3i(xres, yres, zres);
m_channels = stream->readInt();
@ -142,11 +154,11 @@ public:
int fd = open(resolved.c_str(), O_RDONLY);
if (fd == -1)
Log(EError, "Could not open \"%s\"!", m_filename.c_str());
m_mmapSize = (nEntries+10)*sizeof(float);
m_mmapSize = (nEntries+12)*sizeof(float);
m_mmapPtr = mmap(NULL, m_mmapSize, PROT_READ, MAP_SHARED, fd, 0);
if (m_mmapPtr == NULL)
Log(EError, "Could not map \"%s\" to memory!", m_filename.c_str());
m_data = ((float *) m_mmapPtr) + 10;
m_data = ((float *) m_mmapPtr) + 12;
if (close(fd) != 0)
Log(EError, "close(): unable to close file!");
#elif defined(WIN32)