2007-05-17 00:11:38 +00:00
|
|
|
// Copyright NVIDIA Corporation 2007 -- Ignacio Castano <icastano@nvidia.com>
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person
|
|
|
|
// obtaining a copy of this software and associated documentation
|
|
|
|
// files (the "Software"), to deal in the Software without
|
|
|
|
// restriction, including without limitation the rights to use,
|
|
|
|
// copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
// copies of the Software, and to permit persons to whom the
|
|
|
|
// Software is furnished to do so, subject to the following
|
|
|
|
// conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be
|
|
|
|
// included in all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
|
|
|
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|
|
|
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|
|
|
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|
|
|
// OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
|
|
|
|
#include <nvcore/Debug.h>
|
2007-07-17 09:16:28 +00:00
|
|
|
|
2007-05-17 00:11:38 +00:00
|
|
|
#include <nvimage/Image.h>
|
2007-10-08 00:56:58 +00:00
|
|
|
#include <nvimage/PixelFormat.h>
|
2007-07-17 09:16:28 +00:00
|
|
|
#include <nvmath/Color.h>
|
2007-05-17 00:11:38 +00:00
|
|
|
|
|
|
|
#include "CompressRGB.h"
|
|
|
|
#include "CompressionOptions.h"
|
2007-12-17 03:51:34 +00:00
|
|
|
#include "OutputOptions.h"
|
2007-05-17 00:11:38 +00:00
|
|
|
|
|
|
|
using namespace nv;
|
|
|
|
using namespace nvtt;
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
|
|
|
inline uint computePitch(uint w, uint bitsize)
|
|
|
|
{
|
|
|
|
uint p = w * ((bitsize + 7) / 8);
|
|
|
|
|
|
|
|
// Align to 32 bits.
|
|
|
|
return ((p + 3) / 4) * 4;
|
|
|
|
}
|
|
|
|
|
2007-10-10 23:59:08 +00:00
|
|
|
inline void convert_to_a8r8g8b8(const void * src, void * dst, uint w)
|
2007-05-17 00:11:38 +00:00
|
|
|
{
|
|
|
|
memcpy(dst, src, 4 * w);
|
|
|
|
}
|
|
|
|
|
2007-10-10 23:59:08 +00:00
|
|
|
inline void convert_to_x8r8g8b8(const void * src, void * dst, uint w)
|
2007-05-17 00:11:38 +00:00
|
|
|
{
|
2007-07-17 09:16:28 +00:00
|
|
|
memcpy(dst, src, 4 * w);
|
2007-05-17 00:11:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
|
|
|
|
// Pixel format converter.
|
2007-12-17 03:51:34 +00:00
|
|
|
void nv::compressRGB(const Image * image, const OutputOptions::Private & outputOptions, const CompressionOptions::Private & compressionOptions)
|
2007-05-17 00:11:38 +00:00
|
|
|
{
|
|
|
|
nvCheck(image != NULL);
|
|
|
|
|
|
|
|
const uint w = image->width();
|
|
|
|
const uint h = image->height();
|
|
|
|
|
2007-07-17 09:16:28 +00:00
|
|
|
const uint bitCount = compressionOptions.bitcount;
|
|
|
|
nvCheck(bitCount == 8 || bitCount == 16 || bitCount == 24 || bitCount == 32);
|
|
|
|
|
|
|
|
const uint byteCount = bitCount / 8;
|
|
|
|
|
|
|
|
const uint rmask = compressionOptions.rmask;
|
2007-05-17 00:11:38 +00:00
|
|
|
uint rshift, rsize;
|
2007-10-08 00:56:58 +00:00
|
|
|
PixelFormat::maskShiftAndSize(rmask, &rshift, &rsize);
|
2007-05-17 00:11:38 +00:00
|
|
|
|
2007-07-17 09:16:28 +00:00
|
|
|
const uint gmask = compressionOptions.gmask;
|
2007-05-17 00:11:38 +00:00
|
|
|
uint gshift, gsize;
|
2007-10-08 00:56:58 +00:00
|
|
|
PixelFormat::maskShiftAndSize(gmask, &gshift, &gsize);
|
2007-05-17 00:11:38 +00:00
|
|
|
|
2007-07-17 09:16:28 +00:00
|
|
|
const uint bmask = compressionOptions.bmask;
|
2007-05-17 00:11:38 +00:00
|
|
|
uint bshift, bsize;
|
2007-10-08 00:56:58 +00:00
|
|
|
PixelFormat::maskShiftAndSize(bmask, &bshift, &bsize);
|
2007-05-17 00:11:38 +00:00
|
|
|
|
2007-07-17 09:16:28 +00:00
|
|
|
const uint amask = compressionOptions.amask;
|
2007-05-17 00:11:38 +00:00
|
|
|
uint ashift, asize;
|
2007-10-08 00:56:58 +00:00
|
|
|
PixelFormat::maskShiftAndSize(amask, &ashift, &asize);
|
2007-05-17 00:11:38 +00:00
|
|
|
|
|
|
|
// Determine pitch.
|
|
|
|
uint pitch = computePitch(w, compressionOptions.bitcount);
|
|
|
|
|
2007-07-17 10:00:48 +00:00
|
|
|
uint8 * dst = (uint8 *)mem::malloc(pitch + 4);
|
2007-05-17 00:11:38 +00:00
|
|
|
|
|
|
|
for (uint y = 0; y < h; y++)
|
|
|
|
{
|
|
|
|
const Color32 * src = image->scanline(y);
|
|
|
|
|
2007-07-17 09:16:28 +00:00
|
|
|
if (bitCount == 32 && rmask == 0xFF0000 && gmask == 0xFF00 && bmask == 0xFF && amask == 0xFF000000)
|
2007-05-17 00:11:38 +00:00
|
|
|
{
|
2007-07-17 09:16:28 +00:00
|
|
|
convert_to_a8r8g8b8(src, dst, w);
|
2007-05-17 00:11:38 +00:00
|
|
|
}
|
2007-07-17 09:16:28 +00:00
|
|
|
else if (bitCount == 32 && rmask == 0xFF0000 && gmask == 0xFF00 && bmask == 0xFF && amask == 0)
|
2007-05-17 00:11:38 +00:00
|
|
|
{
|
2007-07-17 09:16:28 +00:00
|
|
|
convert_to_x8r8g8b8(src, dst, w);
|
2007-05-17 00:11:38 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2007-07-17 09:16:28 +00:00
|
|
|
// Generic pixel format conversion.
|
|
|
|
for (uint x = 0; x < w; x++)
|
|
|
|
{
|
|
|
|
uint c = 0;
|
2007-10-08 00:56:58 +00:00
|
|
|
c |= PixelFormat::convert(src[x].r, 8, rsize) << rshift;
|
|
|
|
c |= PixelFormat::convert(src[x].g, 8, gsize) << gshift;
|
|
|
|
c |= PixelFormat::convert(src[x].b, 8, bsize) << bshift;
|
|
|
|
c |= PixelFormat::convert(src[x].a, 8, asize) << ashift;
|
2007-07-17 09:16:28 +00:00
|
|
|
|
2008-03-11 21:22:54 +00:00
|
|
|
// Output one byte at a time.
|
2007-10-10 17:06:42 +00:00
|
|
|
for (uint i = 0; i < byteCount; i++)
|
2007-10-08 00:56:58 +00:00
|
|
|
{
|
2008-03-11 21:22:54 +00:00
|
|
|
*(dst + x * byteCount + i) = (c >> (i * 8)) & 0xFF;
|
2007-10-08 00:56:58 +00:00
|
|
|
}
|
2007-07-17 09:16:28 +00:00
|
|
|
}
|
2008-03-11 21:22:54 +00:00
|
|
|
|
|
|
|
// Zero padding.
|
|
|
|
for (uint x = w; x < pitch; x++)
|
|
|
|
{
|
|
|
|
*(dst + x) = 0;
|
|
|
|
}
|
2007-05-17 00:11:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (outputOptions.outputHandler != NULL)
|
|
|
|
{
|
|
|
|
outputOptions.outputHandler->writeData(dst, pitch);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-07-17 09:16:28 +00:00
|
|
|
mem::free(dst);
|
2007-05-17 00:11:38 +00:00
|
|
|
}
|
|
|
|
|