Do not mix tabs and spaces.

This commit is contained in:
castano 2008-11-22 00:14:05 +00:00
parent 321f320bfb
commit e1916d43c8

View File

@ -41,37 +41,38 @@
using namespace nv; using namespace nv;
static const char * s_fileNames[] = { static const char * s_fileNames[] = {
"kodim01.png", "kodim01.png",
"kodim02.png", "kodim02.png",
"kodim03.png", "kodim03.png",
"kodim04.png", "kodim04.png",
"kodim05.png", "kodim05.png",
"kodim06.png", "kodim06.png",
"kodim07.png", "kodim07.png",
"kodim08.png", "kodim08.png",
"kodim09.png", "kodim09.png",
"kodim10.png", "kodim10.png",
"kodim11.png", "kodim11.png",
"kodim12.png", "kodim12.png",
"kodim13.png", "kodim13.png",
"kodim14.png", "kodim14.png",
"kodim15.png", "kodim15.png",
"kodim16.png", "kodim16.png",
"kodim17.png", "kodim17.png",
"kodim18.png", "kodim18.png",
"kodim19.png", "kodim19.png",
"kodim20.png", "kodim20.png",
"kodim21.png", "kodim21.png",
"kodim22.png", "kodim22.png",
"kodim23.png", "kodim23.png",
"kodim24.png", "kodim24.png",
"clegg.tif", "clegg.png",
"frymire.tif", "frymire.png",
"lena.tif", "lena.png",
"monarch.tif", "monarch.png",
"sail.tif", "peppers.png",
"serrano.tif", "sail.png",
"tulips.tif", "serrano.png",
"tulips.png",
}; };
const int s_fileCount = sizeof(s_fileNames)/sizeof(s_fileNames[0]); const int s_fileCount = sizeof(s_fileNames)/sizeof(s_fileNames[0]);
@ -79,18 +80,18 @@ const int s_fileCount = sizeof(s_fileNames)/sizeof(s_fileNames[0]);
struct MyOutputHandler : public nvtt::OutputHandler struct MyOutputHandler : public nvtt::OutputHandler
{ {
MyOutputHandler() : m_data(NULL), m_ptr(NULL) {} MyOutputHandler() : m_data(NULL), m_ptr(NULL) {}
~MyOutputHandler() ~MyOutputHandler()
{ {
free(m_data); free(m_data);
} }
virtual void beginImage(int size, int width, int height, int depth, int face, int miplevel) virtual void beginImage(int size, int width, int height, int depth, int face, int miplevel)
{ {
m_size = size; m_size = size;
m_width = width; m_width = width;
m_height = height; m_height = height;
free(m_data); free(m_data);
m_data = (unsigned char *)malloc(size); m_data = (unsigned char *)malloc(size);
m_ptr = m_data; m_ptr = m_data;
} }
@ -101,83 +102,83 @@ struct MyOutputHandler : public nvtt::OutputHandler
return true; return true;
} }
Image * decompress(nvtt::Format format) Image * decompress(nvtt::Format format)
{ {
int bw = (m_width + 3) / 4; int bw = (m_width + 3) / 4;
int bh = (m_height + 3) / 4; int bh = (m_height + 3) / 4;
AutoPtr<Image> img( new Image() ); AutoPtr<Image> img( new Image() );
img->allocate(m_width, m_height); img->allocate(m_width, m_height);
if (format == nvtt::Format_BC1) if (format == nvtt::Format_BC1)
{ {
BlockDXT1 * block = (BlockDXT1 *)m_data; BlockDXT1 * block = (BlockDXT1 *)m_data;
for (int y = 0; y < bh; y++) for (int y = 0; y < bh; y++)
{ {
for (int x = 0; x < bw; x++) for (int x = 0; x < bw; x++)
{ {
ColorBlock colors; ColorBlock colors;
block->decodeBlock(&colors); block->decodeBlock(&colors);
for (int yy = 0; yy < 4; yy++) for (int yy = 0; yy < 4; yy++)
{ {
for (int xx = 0; xx < 4; xx++) for (int xx = 0; xx < 4; xx++)
{ {
Color32 c = colors.color(xx, yy); Color32 c = colors.color(xx, yy);
if (x * 4 + xx < m_width && y * 4 + yy < m_height) if (x * 4 + xx < m_width && y * 4 + yy < m_height)
{ {
img->pixel(x * 4 + xx, y * 4 + yy) = c; img->pixel(x * 4 + xx, y * 4 + yy) = c;
} }
} }
} }
block++; block++;
} }
} }
} }
return img.release(); return img.release();
} }
int m_size; int m_size;
int m_width; int m_width;
int m_height; int m_height;
unsigned char * m_data; unsigned char * m_data;
unsigned char * m_ptr; unsigned char * m_ptr;
}; };
float rmsError(const Image * a, const Image * b) float rmsError(const Image * a, const Image * b)
{ {
nvCheck(a != NULL); nvCheck(a != NULL);
nvCheck(b != NULL); nvCheck(b != NULL);
nvCheck(a->width() == b->width()); nvCheck(a->width() == b->width());
nvCheck(a->height() == b->height()); nvCheck(a->height() == b->height());
float mse = 0; float mse = 0;
const uint count = a->width() * a->height(); const uint count = a->width() * a->height();
for (uint i = 0; i < count; i++) for (uint i = 0; i < count; i++)
{ {
Color32 c0 = a->pixel(i); Color32 c0 = a->pixel(i);
Color32 c1 = b->pixel(i); Color32 c1 = b->pixel(i);
int r = c0.r - c1.r; int r = c0.r - c1.r;
int g = c0.g - c1.g; int g = c0.g - c1.g;
int b = c0.b - c1.b; int b = c0.b - c1.b;
//int a = c0.a - c1.a; //int a = c0.a - c1.a;
mse += r * r; mse += r * r;
mse += g * g; mse += g * g;
mse += b * b; mse += b * b;
} }
mse /= count; mse /= count;
return sqrtf(mse); return sqrtf(mse);
} }
@ -229,46 +230,46 @@ int main(int argc, char *argv[])
return 1; return 1;
} }
nvtt::InputOptions inputOptions; nvtt::InputOptions inputOptions;
inputOptions.setMipmapGeneration(false); inputOptions.setMipmapGeneration(false);
nvtt::CompressionOptions compressionOptions; nvtt::CompressionOptions compressionOptions;
compressionOptions.setFormat(nvtt::Format_BC1); compressionOptions.setFormat(nvtt::Format_BC1);
if (fast) if (fast)
{ {
compressionOptions.setQuality(nvtt::Quality_Fastest); compressionOptions.setQuality(nvtt::Quality_Fastest);
} }
else else
{ {
compressionOptions.setQuality(nvtt::Quality_Production); compressionOptions.setQuality(nvtt::Quality_Production);
} }
nvtt::OutputOptions outputOptions; nvtt::OutputOptions outputOptions;
outputOptions.setOutputHeader(false); outputOptions.setOutputHeader(false);
MyOutputHandler outputHandler; MyOutputHandler outputHandler;
outputOptions.setOutputHandler(&outputHandler); outputOptions.setOutputHandler(&outputHandler);
nvtt::Compressor compressor; nvtt::Compressor compressor;
compressor.enableCudaAcceleration(!nocuda); compressor.enableCudaAcceleration(!nocuda);
float totalTime = 0; float totalTime = 0;
float totalRMS = 0; float totalRMS = 0;
for (int i = 0; i < s_fileCount; i++) for (int i = 0; i < s_fileCount; i++)
{ {
AutoPtr<Image> img( new Image() ); AutoPtr<Image> img( new Image() );
if (!img->load(s_fileNames[i])) if (!img->load(s_fileNames[i]))
{ {
printf("Input image '%s' not found.\n", s_fileNames[i]); printf("Input image '%s' not found.\n", s_fileNames[i]);
return EXIT_FAILURE; return EXIT_FAILURE;
} }
inputOptions.setTextureLayout(nvtt::TextureType_2D, img->width(), img->height()); inputOptions.setTextureLayout(nvtt::TextureType_2D, img->width(), img->height());
inputOptions.setMipmapData(img->pixels(), img->width(), img->height()); inputOptions.setMipmapData(img->pixels(), img->width(), img->height());
printf("Compressing: \t'%s'\n", s_fileNames[i]); printf("Compressing: \t'%s'\n", s_fileNames[i]);
clock_t start = clock(); clock_t start = clock();
@ -278,28 +279,28 @@ int main(int argc, char *argv[])
printf(" Time: \t%.3f sec\n", float(end-start) / CLOCKS_PER_SEC); printf(" Time: \t%.3f sec\n", float(end-start) / CLOCKS_PER_SEC);
totalTime += float(end-start); totalTime += float(end-start);
AutoPtr<Image> img_out( outputHandler.decompress(nvtt::Format_BC1) ); AutoPtr<Image> img_out( outputHandler.decompress(nvtt::Format_BC1) );
Path outputFileName("%s/%s", outPath, s_fileNames[i]); Path outputFileName("%s/%s", outPath, s_fileNames[i]);
outputFileName.stripExtension(); outputFileName.stripExtension();
outputFileName.append(".tga"); outputFileName.append(".tga");
if (!ImageIO::save(outputFileName, img_out.ptr())) if (!ImageIO::save(outputFileName, img_out.ptr()))
{ {
printf("Error saving file '%s'.\n", outputFileName.str()); printf("Error saving file '%s'.\n", outputFileName.str());
} }
float rms = rmsError(img.ptr(), img_out.ptr()); float rms = rmsError(img.ptr(), img_out.ptr());
totalRMS += rms; totalRMS += rms;
printf(" RMS: \t%.4f\n", rms); printf(" RMS: \t%.4f\n", rms);
} }
totalTime /= s_fileCount; totalTime /= s_fileCount;
totalRMS /= s_fileCount; totalRMS /= s_fileCount;
printf("Average Results:\n"); printf("Average Results:\n");
printf(" Time: \t%.3f sec\n", totalTime / CLOCKS_PER_SEC); printf(" Time: \t%.3f sec\n", totalTime / CLOCKS_PER_SEC);
printf(" RMS: \t%.4f\n", totalRMS); printf(" RMS: \t%.4f\n", totalRMS);
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }