Create default output handle on setFileName to avoid modifying const argument.

Fix ref counting errors in TexImage.
Format TODO messages.
pull/216/head
castano 15 years ago
parent 78d65e8368
commit c1f9c4df42

@ -62,8 +62,11 @@ namespace nvtt
uint getBitCount() const uint getBitCount() const
{ {
if (bitcount != 0) return bitcount; if (format == Format_RGBA) {
else return rsize + gsize + bsize + asize; if (bitcount != 0) return bitcount;
else return rsize + gsize + bsize + asize;
}
return 0;
} }
}; };

@ -311,8 +311,7 @@ int Compressor::estimateSize(int w, int h, int d, const CompressionOptions & com
const Format format = co.format; const Format format = co.format;
uint bitCount = co.bitcount; uint bitCount = co.getBitCount();
if (format == Format_RGBA && bitCount == 0) bitCount = co.rsize + co.gsize + co.bsize + co.asize;
return computeImageSize(w, h, d, bitCount, format); return computeImageSize(w, h, d, bitCount, format);
} }
@ -325,7 +324,6 @@ TexImage Compressor::createTexImage() const
return *new TexImage(); return *new TexImage();
} }
bool Compressor::outputHeader(const TexImage & tex, int mipmapCount, const CompressionOptions & compressionOptions, const OutputOptions & outputOptions) const bool Compressor::outputHeader(const TexImage & tex, int mipmapCount, const CompressionOptions & compressionOptions, const OutputOptions & outputOptions) const
{ {
m.outputHeader(tex, mipmapCount, compressionOptions.m, outputOptions.m); m.outputHeader(tex, mipmapCount, compressionOptions.m, outputOptions.m);
@ -336,7 +334,6 @@ bool Compressor::compress(const TexImage & tex, const CompressionOptions & compr
// @@ Convert to fixed point and call compress2D for each face. // @@ Convert to fixed point and call compress2D for each face.
} }
/// Estimate the size of compressing the given texture.
int Compressor::estimateSize(const TexImage & tex, const CompressionOptions & compressionOptions) const int Compressor::estimateSize(const TexImage & tex, const CompressionOptions & compressionOptions) const
{ {
const uint w = tex.width(); const uint w = tex.width();
@ -348,8 +345,6 @@ int Compressor::estimateSize(const TexImage & tex, const CompressionOptions & co
} }
bool Compressor::Private::compress(const InputOptions::Private & inputOptions, const CompressionOptions::Private & compressionOptions, const OutputOptions::Private & outputOptions) const bool Compressor::Private::compress(const InputOptions::Private & inputOptions, const CompressionOptions::Private & compressionOptions, const OutputOptions::Private & outputOptions) const
{ {
// Make sure enums match. // Make sure enums match.
@ -358,12 +353,14 @@ bool Compressor::Private::compress(const InputOptions::Private & inputOptions, c
nvStaticCheck(FloatImage::WrapMode_Repeat == (FloatImage::WrapMode)WrapMode_Repeat); nvStaticCheck(FloatImage::WrapMode_Repeat == (FloatImage::WrapMode)WrapMode_Repeat);
// Get output handler. // Get output handler.
if (!outputOptions.openFile()) if (!outputOptions.hasValidOutputHandler())
{ {
if (outputOptions.errorHandler) outputOptions.errorHandler->error(Error_FileOpen); if (outputOptions.errorHandler) outputOptions.errorHandler->error(Error_FileOpen);
return false; return false;
} }
#pragma message(NV_FILE_LINE "TODO: If DefaultOutputHandler, then seek begining of the file.")
inputOptions.computeTargetExtents(); inputOptions.computeTargetExtents();
// Output DDS header. // Output DDS header.
@ -380,8 +377,6 @@ bool Compressor::Private::compress(const InputOptions::Private & inputOptions, c
} }
} }
outputOptions.closeFile();
return true; return true;
} }
@ -630,7 +625,7 @@ bool Compressor::Private::outputHeader(const TexImage & tex, int mipmapCount, co
{ {
if (tex.width() <= 0 || tex.height() <= 0 || tex.depth() <= 0 || mipmapCount <= 0) if (tex.width() <= 0 || tex.height() <= 0 || tex.depth() <= 0 || mipmapCount <= 0)
{ {
#pragma message(NV_FILE_LINE "Set invalid argument error") #pragma message(NV_FILE_LINE "TODO: Set invalid argument error.")
return false; return false;
} }

@ -33,6 +33,9 @@ OutputOptions::OutputOptions() : m(*new OutputOptions::Private())
OutputOptions::~OutputOptions() OutputOptions::~OutputOptions()
{ {
// Cleanup output handler.
setOutputHandler(NULL);
delete &m; delete &m;
} }
@ -50,14 +53,24 @@ void OutputOptions::reset()
/// Set output file name. /// Set output file name.
void OutputOptions::setFileName(const char * fileName) void OutputOptions::setFileName(const char * fileName)
{ {
m.fileName = fileName; m.fileName = fileName; // @@ Do we need to record filename?
m.outputHandler = NULL; m.outputHandler = NULL;
DefaultOutputHandler * oh = new DefaultOutputHandler(fileName);
if (!oh->stream.isError())
{
m.outputHandler = oh;
}
} }
/// Set output handler. /// Set output handler.
void OutputOptions::setOutputHandler(OutputHandler * outputHandler) void OutputOptions::setOutputHandler(OutputHandler * outputHandler)
{ {
m.fileName.reset(); if (!m.fileName.isNull())
{
delete m.outputHandler;
m.fileName.reset();
}
m.outputHandler = outputHandler; m.outputHandler = outputHandler;
} }
@ -80,30 +93,13 @@ void OutputOptions::setContainer(Container container)
} }
bool OutputOptions::Private::openFile() const bool OutputOptions::Private::hasValidOutputHandler() const
{ {
if (!fileName.isNull()) if (!fileName.isNull())
{ {
nvCheck(outputHandler == NULL); return outputHandler != NULL;
DefaultOutputHandler * oh = new DefaultOutputHandler(fileName.str());
if (oh->stream.isError())
{
return false;
}
outputHandler = oh;
} }
return true; return true;
} }
void OutputOptions::Private::closeFile() const
{
if (!fileName.isNull())
{
delete outputHandler;
outputHandler = NULL;
}
}

@ -52,7 +52,7 @@ namespace nvtt
//return !stream.isError(); //return !stream.isError();
return true; return true;
} }
nv::StdOutputStream stream; nv::StdOutputStream stream;
}; };
@ -61,13 +61,12 @@ namespace nvtt
{ {
nv::Path fileName; nv::Path fileName;
mutable OutputHandler * outputHandler; OutputHandler * outputHandler;
ErrorHandler * errorHandler; ErrorHandler * errorHandler;
bool outputHeader; bool outputHeader;
Container container; Container container;
bool openFile() const; bool hasValidOutputHandler() const;
void closeFile() const;
}; };

@ -61,7 +61,8 @@ namespace
} }
} }
#pragma message(NV_FILE_LINE "Move these functions to a common location") #pragma message(NV_FILE_LINE "TODO: Move these functions to a common location.")
static int blockSize(Format format) static int blockSize(Format format)
{ {
if (format == Format_DXT1 || format == Format_DXT1a || format == Format_DXT1n) { if (format == Format_DXT1 || format == Format_DXT1a || format == Format_DXT1n) {
@ -107,20 +108,20 @@ TexImage::TexImage() : m(new TexImage::Private())
TexImage::TexImage(const TexImage & tex) : m(tex.m) TexImage::TexImage(const TexImage & tex) : m(tex.m)
{ {
m->addRef(); if (m != NULL) m->addRef();
} }
TexImage::~TexImage() TexImage::~TexImage()
{ {
m->release(); if (m != NULL) m->release();
m = NULL; m = NULL;
} }
void TexImage::operator=(const TexImage & tex) void TexImage::operator=(const TexImage & tex)
{ {
tex.m->addRef(); if (tex.m != NULL) tex.m->addRef();
if (m != NULL) m->release();
m = tex.m; m = tex.m;
m->release();
} }
void TexImage::detach() void TexImage::detach()
@ -141,6 +142,12 @@ void TexImage::setTextureType(TextureType type)
m->type = type; m->type = type;
// Free images. @@ Use AutoPtr?
foreach (i, m->imageArray)
{
delete m->imageArray[i];
}
if (type == TextureType_2D) if (type == TextureType_2D)
{ {
// @@ Free images. // @@ Free images.
@ -237,7 +244,7 @@ int TexImage::countMipmaps() const
bool TexImage::load(const char * fileName) bool TexImage::load(const char * fileName)
{ {
#pragma message(NV_FILE_LINE "Add support for DDS textures in TexImage::load") #pragma message(NV_FILE_LINE "TODO: Add support for DDS textures in TexImage::load().")
AutoPtr<FloatImage> img(ImageIO::loadFloat(fileName)); AutoPtr<FloatImage> img(ImageIO::loadFloat(fileName));
@ -268,7 +275,7 @@ bool TexImage::save(const char * fileName) const
} }
} }
bool TexImage::setImage2D(InputFormat format, int w, int h, int idx, const void * restrict data) bool TexImage::setImage2D(nvtt::InputFormat format, int w, int h, int idx, const void * restrict data)
{ {
if (idx < 0 || uint(idx) >= m->imageArray.count()) if (idx < 0 || uint(idx) >= m->imageArray.count())
{ {

Loading…
Cancel
Save