Add output for error metric

This commit is contained in:
Pavel Krajcevski 2012-09-18 19:00:20 -04:00
parent e4c8708a4a
commit 78da9499ec
8 changed files with 1707 additions and 1572 deletions

File diff suppressed because it is too large Load diff

View file

@ -84,8 +84,17 @@ int main(int argc, char **argv) {
CompressedImage *ci = CompressImage(file, settings);
double PSNR = ComputePSNR(*ci, file);
if(PSNR > 0.0) {
fprintf(stdout, "PSNR: %.3f\n", PSNR);
}
else {
fprintf(stderr, "Error computing PSNR\n");
}
// Cleanup
if(NULL != ci)
delete ci;
return 0;
}

View file

@ -30,6 +30,8 @@ class CompressedImage {
CompressedImage( const CompressedImage &other );
~CompressedImage();
bool DecompressImage(unsigned char *outBuf, unsigned int outBufSz) const;
};
#endif // _COMPRESSED_IMAGE_H_

View file

@ -25,4 +25,6 @@ typedef void (* CompressionFunc)(
unsigned int height
);
extern double ComputePSNR(const CompressedImage &, const ImageFile &);
#endif //_TEX_COMP_H_

View file

@ -2,12 +2,17 @@
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include "BC7Compressor.h"
CompressedImage::CompressedImage()
: m_Width(0)
, m_Height(0)
, m_Format(ECompressionFormat(-1))
, m_Data(0)
, m_DataSz(0)
{ }
CompressedImage::CompressedImage( const CompressedImage &other )
@ -15,6 +20,7 @@ CompressedImage::CompressedImage( const CompressedImage &other )
, m_Height(other.m_Height)
, m_Format(other.m_Format)
, m_Data(0)
, m_DataSz(0)
{
InitData(other.m_Data);
}
@ -29,23 +35,24 @@ CompressedImage::CompressedImage(
, m_Height(height)
, m_Format(format)
, m_Data(0)
, m_DataSz(0)
{
InitData(data);
}
void CompressedImage::InitData(const unsigned char *withData) {
unsigned int dataSz = 0;
m_DataSz = 0;
int uncompDataSz = m_Width * m_Height * 4;
switch(m_Format) {
case eCompressionFormat_DXT1: dataSz = uncompDataSz / 8; break;
case eCompressionFormat_DXT5: dataSz = uncompDataSz / 4; break;
case eCompressionFormat_BPTC: dataSz = uncompDataSz / 4; break;
case eCompressionFormat_DXT1: m_DataSz = uncompDataSz / 8; break;
case eCompressionFormat_DXT5: m_DataSz = uncompDataSz / 4; break;
case eCompressionFormat_BPTC: m_DataSz = uncompDataSz / 4; break;
}
if(dataSz > 0) {
m_Data = new unsigned char[dataSz];
memcpy(m_Data, withData, dataSz);
if(m_DataSz > 0) {
m_Data = new unsigned char[m_DataSz];
memcpy(m_Data, withData, m_DataSz);
}
}
@ -55,3 +62,34 @@ CompressedImage::~CompressedImage() {
m_Data = NULL;
}
}
bool CompressedImage::DecompressImage(unsigned char *outBuf, unsigned int outBufSz) const {
// First make sure that we have enough data
int dataSz = 0;
switch(m_Format) {
case eCompressionFormat_DXT1: dataSz = m_DataSz * 8; break;
case eCompressionFormat_DXT5: dataSz = m_DataSz * 4; break;
case eCompressionFormat_BPTC: dataSz = m_DataSz * 4; break;
}
if(dataSz > outBufSz) {
fprintf(stderr, "Not enough space to store entire decompressed image! "
"Got %d bytes, but need %d!\n", outBufSz, dataSz);
return false;
}
switch(m_Format) {
case eCompressionFormat_BPTC:
BC7C::DecompressImageBC7(m_Data, outBuf, m_Width, m_Height);
break;
default:
const char *errStr = "Have not implemented decompression method.";
fprintf(stderr, "%s\n", errStr);
assert(!errStr);
return false;
}
return true;
}

View file

@ -2,6 +2,7 @@
#include "TexComp.h"
#include "ThreadGroup.h"
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
@ -148,4 +149,57 @@ CompressedImage * CompressImage(
delete [] cmpData;
return outImg;
}
}
template <typename T>
static inline T sad(const T &a, const T &b) {
return (a > b)? a - b : b - a;
}
double ComputePSNR(const CompressedImage &ci, const ImageFile &file) {
unsigned int imageSz = 4 * file.GetWidth() * file.GetHeight();
unsigned char *unCompData = new unsigned char[imageSz];
if(!(ci.DecompressImage(unCompData, imageSz))) {
ReportError("Failed to decompress image.");
return -1.0f;
}
const unsigned char *rawData = file.GetPixels();
const double wr = 1.0;
const double wg = 1.0;
const double wb = 1.0;
double MSE = 0.0;
for(int i = 0; i < imageSz; i+=4) {
const unsigned char *pixelDataRaw = rawData + i;
const unsigned char *pixelDataUncomp = unCompData + i;
double dr = double(sad(pixelDataRaw[0], pixelDataUncomp[0])) * wr;
double dg = double(sad(pixelDataRaw[1], pixelDataUncomp[1])) * wg;
double db = double(sad(pixelDataRaw[2], pixelDataUncomp[2])) * wb;
const double pixelMSE =
(double(dr) * double(dr)) +
(double(dg) * double(dg)) +
(double(db) * double(db));
//fprintf(stderr, "Pixel MSE: %f\n", pixelMSE);
MSE += pixelMSE;
}
MSE /= (double(file.GetWidth()) * double(file.GetHeight()));
double MAXI =
(255.0 * wr) * (255.0 * wr) +
(255.0 * wg) * (255.0 * wg) +
(255.0 * wb) * (255.0 * wb);
double PSNR = 10 * log10(MAXI/MSE);
// Cleanup
delete unCompData;
return PSNR;
}

View file

@ -196,6 +196,11 @@ bool ImageFile::LoadImage(const unsigned char *rawImageData) {
const unsigned int aw = ((m_Width + 3) >> 2) << 2;
const unsigned int ah = ((m_Height + 3) >> 2) << 2;
#ifndef NDEBUG
if(aw != m_Width || ah != m_Height)
fprintf(stderr, "Warning: Image dimension not multiple of four. Space will be filled with black.\n");
#endif
int byteIdx = 0;
for(int i = 0; i < ah; i+=4) {
for(int j = 0; j < aw; j+= 4) {

View file

@ -112,7 +112,7 @@ bool ImageLoaderPNG::ReadData() {
unsigned int byteIdx = 0;
for(int j = 0; j < m_Width; j++) {
m_RedData[rowOffset + j] = rowData[byteIdx++];
}
assert(byteIdx == bpr);