2004-08-16 23:43:12 +08:00
|
|
|
|
// dxtctool.cpp: implementation of DXTC Tools functions.
|
|
|
|
|
//
|
|
|
|
|
// Copyright (C) 2002 Tanguy Fautr<74>.
|
|
|
|
|
// For conditions of distribution and use,
|
|
|
|
|
// see copyright notice in dxtctool.h
|
|
|
|
|
//
|
|
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
#include "dxtctool.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace dxtc_tool {
|
|
|
|
|
|
2004-08-18 03:42:45 +08:00
|
|
|
|
const size_t dxtc_pixels::BSIZE_DXT1 = 8;
|
|
|
|
|
const size_t dxtc_pixels::BSIZE_DXT3 = 16;
|
|
|
|
|
const size_t dxtc_pixels::BSIZE_DXT5 = 16;
|
|
|
|
|
const size_t dxtc_pixels::BSIZE_ALPHA_DXT3 = 8;
|
|
|
|
|
const size_t dxtc_pixels::BSIZE_ALPHA_DXT5 = 8;
|
2004-08-16 23:43:12 +08:00
|
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
|
// Construction/Destruction
|
|
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
|
// Members Functions
|
|
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
bool dxtc_pixels::OpenGLSize() const
|
|
|
|
|
{
|
2004-08-18 03:42:45 +08:00
|
|
|
|
size_t Width = m_Width;
|
|
|
|
|
size_t Height = m_Height;
|
|
|
|
|
// size_t TotalTrueBits = 0;
|
2004-08-16 23:43:12 +08:00
|
|
|
|
|
2004-08-18 03:42:45 +08:00
|
|
|
|
if ((Width == 0) || (Height == 0))
|
|
|
|
|
return false;
|
2004-08-16 23:43:12 +08:00
|
|
|
|
|
2004-08-18 03:42:45 +08:00
|
|
|
|
for (; (Width % 2) == 0; Width /= 2);
|
|
|
|
|
for (; (Height % 2) == 0; Height /= 2);
|
2004-08-16 23:43:12 +08:00
|
|
|
|
|
2004-08-18 03:42:45 +08:00
|
|
|
|
if ((Width != 1) || (Height != 1))
|
|
|
|
|
return false;
|
|
|
|
|
else
|
|
|
|
|
return true;
|
2004-08-16 23:43:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool dxtc_pixels::VFlip() const
|
|
|
|
|
{
|
2004-08-18 03:42:45 +08:00
|
|
|
|
// Check that the given dimentions are 2^x, 2^y
|
|
|
|
|
if (! OpenGLSize())
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
// Check that the given format are supported
|
|
|
|
|
if (! SupportedFormat())
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
// Nothing to do if Height == 1
|
|
|
|
|
if (m_Height == 1)
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
if (DXT1())
|
|
|
|
|
VFlip_DXT1();
|
|
|
|
|
else if (DXT3())
|
|
|
|
|
VFlip_DXT3();
|
|
|
|
|
else if (DXT5())
|
|
|
|
|
VFlip_DXT5();
|
|
|
|
|
else
|
|
|
|
|
return false; // We should never get there
|
|
|
|
|
|
|
|
|
|
return true;
|
2004-08-16 23:43:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void dxtc_pixels::VFlip_DXT1() const
|
|
|
|
|
{
|
2004-08-18 03:42:45 +08:00
|
|
|
|
// const size_t Size = ((m_Width + 3) / 4) * ((m_Height + 3) / 4) * BSIZE_DXT1;
|
|
|
|
|
__int8 * const pPixels = (__int8 * const) m_pPixels;
|
|
|
|
|
|
|
|
|
|
if (m_Height == 2)
|
|
|
|
|
for (size_t j = 0; j < (m_Width + 3) / 4; ++j)
|
|
|
|
|
BVF_Color_H2(pPixels + j * BSIZE_DXT1);
|
|
|
|
|
|
|
|
|
|
if (m_Height == 4)
|
|
|
|
|
for (size_t j = 0; j < (m_Width + 3) / 4; ++j)
|
|
|
|
|
BVF_Color_H4(pPixels + j * BSIZE_DXT1);
|
|
|
|
|
|
|
|
|
|
if (m_Height > 4)
|
|
|
|
|
for (size_t i = 0; i < ((m_Height + 7) / 8); ++i)
|
|
|
|
|
for (size_t j = 0; j < (m_Width + 3) / 4; ++j) {
|
|
|
|
|
const size_t TargetRow = ((m_Height + 3) / 4) - (i + 1);
|
|
|
|
|
BVF_Color(GetBlock(i, j, BSIZE_DXT1), GetBlock(TargetRow, j, BSIZE_DXT1));
|
|
|
|
|
}
|
2004-08-16 23:43:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void dxtc_pixels::VFlip_DXT3() const
|
|
|
|
|
{
|
2004-08-18 03:42:45 +08:00
|
|
|
|
// const size_t Size = ((m_Width + 3) / 4) * ((m_Height + 3) / 4) * BSIZE_DXT3;
|
|
|
|
|
// __int8 * const pPixels = (__int8 * const) m_pPixels;
|
|
|
|
|
|
|
|
|
|
if (m_Height == 2)
|
|
|
|
|
for (size_t j = 0; j < (m_Width + 3) / 4; ++j) {
|
|
|
|
|
BVF_Alpha_DXT3_H2(((__int8 * const) m_pPixels) + (j * BSIZE_DXT3));
|
|
|
|
|
BVF_Color_H2(((__int8 * const) m_pPixels) + (j * BSIZE_DXT3 + BSIZE_ALPHA_DXT3));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (m_Height == 4)
|
|
|
|
|
for (size_t j = 0; j < (m_Width + 3) / 4; ++j) {
|
|
|
|
|
BVF_Alpha_DXT3_H4(((__int8 * const) m_pPixels) + (j * BSIZE_DXT3));
|
|
|
|
|
BVF_Color_H4(((__int8 * const) m_pPixels) + (j * BSIZE_DXT3 + BSIZE_ALPHA_DXT3));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (m_Height > 4)
|
|
|
|
|
for (size_t i = 0; i < ((m_Height + 7) / 8); ++i)
|
|
|
|
|
for (size_t j = 0; j < (m_Width + 3) / 4; ++j) {
|
|
|
|
|
const size_t TargetRow = ((m_Height + 3) / 4) - (i + 1);
|
|
|
|
|
BVF_Alpha_DXT3(GetBlock(i, j, BSIZE_DXT3), GetBlock(TargetRow, j, BSIZE_DXT3));
|
|
|
|
|
BVF_Color(((__int8 * const) GetBlock(i, j, BSIZE_DXT3)) + BSIZE_ALPHA_DXT3,
|
|
|
|
|
((__int8 * const) GetBlock(TargetRow, j, BSIZE_DXT3)) + BSIZE_ALPHA_DXT3);
|
|
|
|
|
}
|
2004-08-16 23:43:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void dxtc_pixels::VFlip_DXT5() const
|
|
|
|
|
{
|
2004-08-18 03:42:45 +08:00
|
|
|
|
// const size_t Size = ((m_Width + 3) / 4) * ((m_Height + 3) / 4) * BSIZE_DXT5;
|
|
|
|
|
// __int8 * const pPixels = (__int8 * const) m_pPixels;
|
|
|
|
|
|
|
|
|
|
if (m_Height == 2)
|
|
|
|
|
for (size_t j = 0; j < (m_Width + 3) / 4; ++j) {
|
|
|
|
|
BVF_Alpha_DXT5_H2(((__int8 * const) m_pPixels) + (j * BSIZE_DXT5));
|
|
|
|
|
BVF_Color_H2(((__int8 * const) m_pPixels) + (j * BSIZE_DXT5 + BSIZE_ALPHA_DXT5));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (m_Height == 4)
|
|
|
|
|
for (size_t j = 0; j < (m_Width + 3) / 4; ++j) {
|
|
|
|
|
BVF_Alpha_DXT5_H4(((__int8 * const) m_pPixels) + (j * BSIZE_DXT5));
|
|
|
|
|
BVF_Color_H4(((__int8 * const) m_pPixels) + (j * BSIZE_DXT5 + BSIZE_ALPHA_DXT5));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (m_Height > 4)
|
|
|
|
|
for (size_t i = 0; i < ((m_Height + 7) / 8); ++i)
|
|
|
|
|
for (size_t j = 0; j < (m_Width + 3) / 4; ++j) {
|
|
|
|
|
const size_t TargetRow = ((m_Height + 3) / 4) - (i + 1);
|
|
|
|
|
BVF_Alpha_DXT5(GetBlock(i, j, BSIZE_DXT5), GetBlock(TargetRow, j, BSIZE_DXT5));
|
|
|
|
|
BVF_Color(((__int8 * const) GetBlock(i, j, BSIZE_DXT5)) + BSIZE_ALPHA_DXT5,
|
|
|
|
|
((__int8 * const) GetBlock(TargetRow, j, BSIZE_DXT5)) + BSIZE_ALPHA_DXT5);
|
|
|
|
|
}
|
2004-08-16 23:43:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}; // namespace dxtc_tool
|