Add support for premultiplied alpha. Patch by Charles Nicholson.
This commit is contained in:
parent
6b933c4f62
commit
307c8b99ee
@ -478,6 +478,11 @@ bool Compressor::Private::initMipmap(Mipmap & mipmap, const InputOptions::Privat
|
|||||||
// Convert linear float image to fixed image ready for compression.
|
// Convert linear float image to fixed image ready for compression.
|
||||||
mipmap.toFixedImage(inputOptions);
|
mipmap.toFixedImage(inputOptions);
|
||||||
|
|
||||||
|
if (inputOptions.premultiplyAlpha)
|
||||||
|
{
|
||||||
|
premultiplyAlphaMipmap(mipmap, inputOptions);
|
||||||
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -582,6 +587,29 @@ void Compressor::Private::scaleMipmap(Mipmap & mipmap, const InputOptions::Priva
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void Compressor::Private::premultiplyAlphaMipmap(Mipmap & mipmap, const InputOptions::Private & inputOptions) const
|
||||||
|
{
|
||||||
|
nvDebugCheck(mipmap.asFixedImage() != NULL);
|
||||||
|
|
||||||
|
Image * image = mipmap.asMutableFixedImage();
|
||||||
|
|
||||||
|
const uint w = image->width();
|
||||||
|
const uint h = image->height();
|
||||||
|
|
||||||
|
const uint count = w * h;
|
||||||
|
|
||||||
|
for (uint i = 0; i < count; ++i)
|
||||||
|
{
|
||||||
|
Color32 c = image->pixel(i);
|
||||||
|
|
||||||
|
c.r = (uint(c.r) * uint(c.a)) >> 8;
|
||||||
|
c.g = (uint(c.g) * uint(c.a)) >> 8;
|
||||||
|
c.b = (uint(c.b) * uint(c.a)) >> 8;
|
||||||
|
|
||||||
|
image->pixel(i) = c;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Process an input image: Convert to normal map, normalize, or convert to linear space.
|
// Process an input image: Convert to normal map, normalize, or convert to linear space.
|
||||||
void Compressor::Private::processInputImage(Mipmap & mipmap, const InputOptions::Private & inputOptions) const
|
void Compressor::Private::processInputImage(Mipmap & mipmap, const InputOptions::Private & inputOptions) const
|
||||||
{
|
{
|
||||||
|
@ -58,6 +58,7 @@ namespace nvtt
|
|||||||
|
|
||||||
void downsampleMipmap(Mipmap & mipmap, const InputOptions::Private & inputOptions) const;
|
void downsampleMipmap(Mipmap & mipmap, const InputOptions::Private & inputOptions) const;
|
||||||
void scaleMipmap(Mipmap & mipmap, const InputOptions::Private & inputOptions, uint w, uint h, uint d) const;
|
void scaleMipmap(Mipmap & mipmap, const InputOptions::Private & inputOptions, uint w, uint h, uint d) const;
|
||||||
|
void premultiplyAlphaMipmap(Mipmap & mipmap, const InputOptions::Private & inputOptions) const;
|
||||||
void processInputImage(Mipmap & mipmap, const InputOptions::Private & inputOptions) const;
|
void processInputImage(Mipmap & mipmap, const InputOptions::Private & inputOptions) const;
|
||||||
void quantizeMipmap(Mipmap & mipmap, const CompressionOptions::Private & compressionOptions) const;
|
void quantizeMipmap(Mipmap & mipmap, const CompressionOptions::Private & compressionOptions) const;
|
||||||
bool compressMipmap(const Mipmap & mipmap, const CompressionOptions::Private & compressionOptions, const OutputOptions::Private & outputOptions) const;
|
bool compressMipmap(const Mipmap & mipmap, const CompressionOptions::Private & compressionOptions, const OutputOptions::Private & outputOptions) const;
|
||||||
|
@ -118,6 +118,8 @@ void InputOptions::reset()
|
|||||||
|
|
||||||
m.maxExtent = 0;
|
m.maxExtent = 0;
|
||||||
m.roundMode = RoundMode_None;
|
m.roundMode = RoundMode_None;
|
||||||
|
|
||||||
|
m.premultiplyAlpha = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -329,6 +331,10 @@ void InputOptions::setRoundMode(RoundMode mode)
|
|||||||
m.roundMode = mode;
|
m.roundMode = mode;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void InputOptions::setPremultiplyAlpha(bool b)
|
||||||
|
{
|
||||||
|
m.premultiplyAlpha = b;
|
||||||
|
}
|
||||||
|
|
||||||
void InputOptions::Private::computeTargetExtents() const
|
void InputOptions::Private::computeTargetExtents() const
|
||||||
{
|
{
|
||||||
|
@ -78,6 +78,8 @@ namespace nvtt
|
|||||||
uint maxExtent;
|
uint maxExtent;
|
||||||
RoundMode roundMode;
|
RoundMode roundMode;
|
||||||
|
|
||||||
|
bool premultiplyAlpha;
|
||||||
|
|
||||||
// @@ These are computed in nvtt::compress, so they should be mutable or stored elsewhere...
|
// @@ These are computed in nvtt::compress, so they should be mutable or stored elsewhere...
|
||||||
mutable uint targetWidth;
|
mutable uint targetWidth;
|
||||||
mutable uint targetHeight;
|
mutable uint targetHeight;
|
||||||
|
@ -227,6 +227,9 @@ namespace nvtt
|
|||||||
// Set resizing options.
|
// Set resizing options.
|
||||||
NVTT_API void setMaxExtents(int d);
|
NVTT_API void setMaxExtents(int d);
|
||||||
NVTT_API void setRoundMode(RoundMode mode);
|
NVTT_API void setRoundMode(RoundMode mode);
|
||||||
|
|
||||||
|
// Set whether or not to premultiply color by alpha
|
||||||
|
NVTT_API void setPremultiplyAlpha(bool b);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
@ -140,6 +140,7 @@ int main(int argc, char *argv[])
|
|||||||
bool silent = false;
|
bool silent = false;
|
||||||
bool bc1n = false;
|
bool bc1n = false;
|
||||||
nvtt::Format format = nvtt::Format_BC1;
|
nvtt::Format format = nvtt::Format_BC1;
|
||||||
|
bool premultiplyAlpha = false;
|
||||||
|
|
||||||
const char * externalCompressor = NULL;
|
const char * externalCompressor = NULL;
|
||||||
|
|
||||||
@ -173,6 +174,10 @@ int main(int argc, char *argv[])
|
|||||||
{
|
{
|
||||||
noMipmaps = true;
|
noMipmaps = true;
|
||||||
}
|
}
|
||||||
|
else if (strcmp("-premula", argv[i]) == 0)
|
||||||
|
{
|
||||||
|
premultiplyAlpha = true;
|
||||||
|
}
|
||||||
|
|
||||||
// Compression options.
|
// Compression options.
|
||||||
else if (strcmp("-fast", argv[i]) == 0)
|
else if (strcmp("-fast", argv[i]) == 0)
|
||||||
@ -266,7 +271,8 @@ int main(int argc, char *argv[])
|
|||||||
printf(" -tonormal\tConvert input to normal map.\n");
|
printf(" -tonormal\tConvert input to normal map.\n");
|
||||||
printf(" -clamp \tClamp wrapping mode (default).\n");
|
printf(" -clamp \tClamp wrapping mode (default).\n");
|
||||||
printf(" -repeat \tRepeat wrapping mode.\n");
|
printf(" -repeat \tRepeat wrapping mode.\n");
|
||||||
printf(" -nomips \tDisable mipmap generation.\n\n");
|
printf(" -nomips \tDisable mipmap generation.\n");
|
||||||
|
printf(" -premula \tPremultiply alpha into color channel.\n\n");
|
||||||
|
|
||||||
printf("Compression options:\n");
|
printf("Compression options:\n");
|
||||||
printf(" -fast \tFast compression.\n");
|
printf(" -fast \tFast compression.\n");
|
||||||
@ -373,6 +379,11 @@ int main(int argc, char *argv[])
|
|||||||
inputOptions.setMipmapGeneration(false);
|
inputOptions.setMipmapGeneration(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (premultiplyAlpha)
|
||||||
|
{
|
||||||
|
inputOptions.setPremultiplyAlpha(true);
|
||||||
|
inputOptions.setAlphaMode(nvtt::AlphaMode_Premultiplied);
|
||||||
|
}
|
||||||
|
|
||||||
nvtt::CompressionOptions compressionOptions;
|
nvtt::CompressionOptions compressionOptions;
|
||||||
compressionOptions.setFormat(format);
|
compressionOptions.setFormat(format);
|
||||||
|
Loading…
Reference in New Issue
Block a user