nvidia-texture-tools/src/nvtt/nvtt_wrapper.cpp

285 lines
8.5 KiB
C++
Raw Normal View History

#include <stdio.h> //For NULL...
2007-11-30 21:15:57 +00:00
2008-01-18 08:49:57 +00:00
#include "nvtt.h"
#include "nvtt_wrapper.h"
2007-11-30 21:15:57 +00:00
#include "OutputOptions.h"
namespace nvttCWrap
{
//Simple class to re-route calls to the C++ interfaces to C functions
struct HandlerProxy : public nvtt::ErrorHandler, public nvtt::OutputHandler
{
public:
HandlerProxy()
{
errorFunc = NULL;
outputFunc = NULL;
imageFunc = NULL;
}
virtual void error(nvtt::Error e)
{
if(errorFunc != NULL)
errorFunc((NvttError)e);
}
virtual void beginImage(int size, int width, int height, int depth, int face, int miplevel)
{
if(imageFunc != NULL)
imageFunc(size, width, height, depth, face, miplevel);
}
virtual bool writeData(const void * data, int size)
{
if(outputFunc != NULL)
return (outputFunc(data, size) != NVTT_False);
else
return true; //Just say we succeed anyway... despite nothing being done, in case the user only set the beginImage() func.
}
nvttErrorHandler errorFunc;
nvttOutputHandler outputFunc;
nvttImageHandler imageFunc;
};
}
2007-11-30 21:15:57 +00:00
// InputOptions class.
2008-01-18 08:49:57 +00:00
NvttInputOptions * nvttCreateInputOptions()
2007-11-30 21:15:57 +00:00
{
2008-01-18 08:49:57 +00:00
return new nvtt::InputOptions();
2007-11-30 21:15:57 +00:00
}
2008-01-18 08:49:57 +00:00
void nvttDestroyInputOptions(NvttInputOptions * inputOptions)
2007-11-30 21:15:57 +00:00
{
2008-01-18 08:49:57 +00:00
delete inputOptions;
2007-11-30 21:15:57 +00:00
}
2008-01-18 08:49:57 +00:00
void nvttSetInputOptionsTextureLayout(NvttInputOptions * inputOptions, NvttTextureType type, int w, int h, int d)
2007-11-30 21:15:57 +00:00
{
2008-01-18 08:49:57 +00:00
inputOptions->setTextureLayout((nvtt::TextureType)type, w, h, d);
2007-11-30 21:15:57 +00:00
}
2008-01-18 08:49:57 +00:00
void nvttResetInputOptionsTextureLayout(NvttInputOptions * inputOptions)
2007-11-30 21:15:57 +00:00
{
2008-01-18 08:49:57 +00:00
inputOptions->resetTextureLayout();
2007-11-30 21:15:57 +00:00
}
2008-01-18 08:49:57 +00:00
NvttBoolean nvttSetInputOptionsMipmapData(NvttInputOptions * inputOptions, const void * data, int w, int h, int d, int face, int mipmap)
2007-11-30 21:15:57 +00:00
{
2008-01-18 08:49:57 +00:00
return (NvttBoolean)inputOptions->setMipmapData(data, w, h, d, face, mipmap);
2007-11-30 21:15:57 +00:00
}
void nvttSetInputOptionsFormat(NvttInputOptions * inputOptions, NvttInputFormat format)
{
inputOptions->setFormat((nvtt::InputFormat)format);
}
void nvttSetInputOptionsAlphaMode(NvttInputOptions * inputOptions, NvttAlphaMode alphaMode)
{
inputOptions->setAlphaMode((nvtt::AlphaMode)alphaMode);
}
void nvttSetInputOptionsGamma(NvttInputOptions * inputOptions, float inputGamma, float outputGamma)
{
inputOptions->setGamma(inputGamma, outputGamma);
}
void nvttSetInputOptionsWrapMode(NvttInputOptions * inputOptions, NvttWrapMode mode)
{
inputOptions->setWrapMode((nvtt::WrapMode)mode);
}
void nvttSetInputOptionsMipmapFilter(NvttInputOptions * inputOptions, NvttMipmapFilter filter)
{
inputOptions->setMipmapFilter((nvtt::MipmapFilter)filter);
}
void nvttSetInputOptionsMipmapGeneration(NvttInputOptions * inputOptions, NvttBoolean enabled, int maxLevel)
{
inputOptions->setMipmapGeneration(enabled != NVTT_False, maxLevel);
}
void nvttSetInputOptionsKaiserParameters(NvttInputOptions * inputOptions, float width, float alpha, float stretch)
{
inputOptions->setKaiserParameters(width, alpha, stretch);
}
void nvttSetInputOptionsNormalMap(NvttInputOptions * inputOptions, NvttBoolean b)
{
inputOptions->setNormalMap(b != NVTT_False);
}
void nvttSetInputOptionsConvertToNormalMap(NvttInputOptions * inputOptions, NvttBoolean convert)
{
inputOptions->setConvertToNormalMap(convert != NVTT_False);
}
void nvttSetInputOptionsHeightEvaluation(NvttInputOptions * inputOptions, float redScale, float greenScale, float blueScale, float alphaScale)
{
inputOptions->setHeightEvaluation(redScale, greenScale, blueScale, alphaScale);
}
void nvttSetInputOptionsNormalFilter(NvttInputOptions * inputOptions, float small, float medium, float big, float large)
{
inputOptions->setNormalFilter(small, medium, big, large);
}
void nvttSetInputOptionsNormalizeMipmaps(NvttInputOptions * inputOptions, NvttBoolean b)
{
inputOptions->setNormalizeMipmaps(b != NVTT_False);
}
void nvttSetInputOptionsColorTransform(NvttInputOptions * inputOptions, NvttColorTransform t)
{
inputOptions->setColorTransform((nvtt::ColorTransform)t);
}
void nvttSetInputOptionsLinearTransfrom(NvttInputOptions * inputOptions, int channel, float w0, float w1, float w2, float w3)
{
inputOptions->setLinearTransform(channel, w0, w1, w2, w3);
}
void nvttSetInputOptionsMaxExtents(NvttInputOptions * inputOptions, int dim)
{
inputOptions->setMaxExtents(dim);
}
void nvttSetInputOptionsRoundMode(NvttInputOptions * inputOptions, NvttRoundMode mode)
{
inputOptions->setRoundMode((nvtt::RoundMode)mode);
}
2007-11-30 21:15:57 +00:00
// CompressionOptions class.
2008-01-18 08:49:57 +00:00
NvttCompressionOptions * nvttCreateCompressionOptions()
2007-11-30 21:15:57 +00:00
{
2008-01-18 08:49:57 +00:00
return new nvtt::CompressionOptions();
2007-11-30 21:15:57 +00:00
}
2008-01-18 08:49:57 +00:00
void nvttDestroyCompressionOptions(NvttCompressionOptions * compressionOptions)
2007-11-30 21:15:57 +00:00
{
2008-01-18 08:49:57 +00:00
delete compressionOptions;
2007-11-30 21:15:57 +00:00
}
2008-01-18 08:49:57 +00:00
void nvttSetCompressionOptionsFormat(NvttCompressionOptions * compressionOptions, NvttFormat format)
2007-11-30 21:15:57 +00:00
{
2008-01-18 08:49:57 +00:00
compressionOptions->setFormat((nvtt::Format)format);
2007-11-30 21:15:57 +00:00
}
2008-01-18 08:49:57 +00:00
void nvttSetCompressionOptionsQuality(NvttCompressionOptions * compressionOptions, NvttQuality quality)
2007-11-30 21:15:57 +00:00
{
2008-01-18 08:49:57 +00:00
compressionOptions->setQuality((nvtt::Quality)quality);
2007-11-30 21:15:57 +00:00
}
void nvttSetCompressionOptionsColorWeights(NvttCompressionOptions * compressionOptions, float red, float green, float blue, float alpha)
{
compressionOptions->setColorWeights(red, green, blue, alpha);
}
2008-01-18 08:49:57 +00:00
void nvttSetCompressionOptionsPixelFormat(NvttCompressionOptions * compressionOptions, unsigned int bitcount, unsigned int rmask, unsigned int gmask, unsigned int bmask, unsigned int amask)
2007-11-30 21:15:57 +00:00
{
2008-01-18 08:49:57 +00:00
compressionOptions->setPixelFormat(bitcount, rmask, gmask, bmask, amask);
2007-11-30 21:15:57 +00:00
}
void nvttSetCompressionOptionsQuantization(NvttCompressionOptions * compressionOptions, NvttBoolean colorDithering, NvttBoolean alphaDithering, NvttBoolean binaryAlpha, int alphaThreshold)
{
compressionOptions->setQuantization(colorDithering != NVTT_False, alphaDithering != NVTT_False, binaryAlpha != NVTT_False, alphaThreshold);
}
// OutputOptions class.
2008-01-18 08:49:57 +00:00
NvttOutputOptions * nvttCreateOutputOptions()
2007-11-30 21:15:57 +00:00
{
nvtt::OutputOptions * outputOptions = new nvtt::OutputOptions();
nvttCWrap::HandlerProxy * handlerProxy = new nvttCWrap::HandlerProxy();
outputOptions->m.cWrapperProxy = handlerProxy;
return outputOptions;
2007-11-30 21:15:57 +00:00
}
2008-01-18 08:49:57 +00:00
void nvttDestroyOutputOptions(NvttOutputOptions * outputOptions)
2007-11-30 21:15:57 +00:00
{
nvttCWrap::HandlerProxy * handlerProxy = (nvttCWrap::HandlerProxy *)outputOptions->m.cWrapperProxy;
2008-01-18 08:49:57 +00:00
delete outputOptions;
delete handlerProxy;
2007-11-30 21:15:57 +00:00
}
2008-01-18 08:49:57 +00:00
void nvttSetOutputOptionsFileName(NvttOutputOptions * outputOptions, const char * fileName)
2007-11-30 21:15:57 +00:00
{
2008-01-18 08:49:57 +00:00
outputOptions->setFileName(fileName);
2007-11-30 21:15:57 +00:00
}
void nvttSetOutputOptionsOutputHeader(NvttOutputOptions * outputOptions, NvttBoolean b)
{
outputOptions->setOutputHeader(b != NVTT_False);
}
void nvttSetOutputOptionsErrorHandler(NvttOutputOptions * outputOptions, nvttErrorHandler errorHandler)
{
nvttCWrap::HandlerProxy * handlerProxy = (nvttCWrap::HandlerProxy *)outputOptions->m.cWrapperProxy;
handlerProxy->errorFunc = errorHandler;
if(errorHandler == NULL)
outputOptions->setErrorHandler(NULL);
else
outputOptions->setErrorHandler(handlerProxy);
}
void nvttSetOutputOptionsOutputHandler(NvttOutputOptions * outputOptions, nvttOutputHandler outputHandler, nvttImageHandler imageHandler)
{
nvttCWrap::HandlerProxy * handlerProxy = (nvttCWrap::HandlerProxy *)outputOptions->m.cWrapperProxy;
handlerProxy->outputFunc = outputHandler;
handlerProxy->imageFunc = imageHandler;
if((outputHandler == NULL) && (imageHandler == NULL))
outputOptions->setOutputHandler(NULL);
else
outputOptions->setOutputHandler(handlerProxy);
}
2007-11-30 21:15:57 +00:00
// Compressor class.
NvttCompressor * nvttCreateCompressor()
{
return new nvtt::Compressor();
}
void nvttDestroyCompressor(NvttCompressor * compressor)
{
delete compressor;
}
NvttBoolean nvttCompress(const NvttCompressor * compressor, const NvttInputOptions * inputOptions, const NvttCompressionOptions * compressionOptions, const NvttOutputOptions * outputOptions)
2007-11-30 21:15:57 +00:00
{
return (NvttBoolean)compressor->process(*inputOptions, *compressionOptions, *outputOptions);
2007-11-30 21:15:57 +00:00
}
int nvttEstimateSize(const NvttCompressor * compressor, const NvttInputOptions * inputOptions, const NvttCompressionOptions * compressionOptions)
2007-11-30 21:15:57 +00:00
{
return compressor->estimateSize(*inputOptions, *compressionOptions);
2007-11-30 21:15:57 +00:00
}
void nvttEnableCudaCompression(NvttCompressor * compressor, NvttBoolean enable)
{
compressor->enableCudaAcceleration(enable != NVTT_False);
}
// Global functions.
const char * nvttErrorString(NvttError e)
{
return nvtt::errorString((nvtt::Error)e);
}
unsigned int nvttVersion()
{
return nvtt::version();
}