mirror of
https://github.com/zeldaret/oot.git
synced 2025-01-15 12:47:04 +00:00
515ebdce9d
* git subrepo pull --force tools/ZAPD subrepo: subdir: "tools/ZAPD" merged: "769f5702a" upstream: origin: "https://github.com/zeldaret/ZAPD.git" branch: "master" commit: "769f5702a" git-subrepo: version: "0.4.3" origin: "???" commit: "???" * Add `libpng` to readme * Remove `-ifp` since it doesn't exists anymore in ZAPD * Remove extra print I added * Add UNK_09 macro and other minor fixes * Simplify PNG rules * simplify gitignore * Update README.md Co-authored-by: Roman971 <32455037+Roman971@users.noreply.github.com> * Update dockerfile * basic instructions for cygwin and mac * git subrepo pull --force tools/ZAPD subrepo: subdir: "tools/ZAPD" merged: "86160be69" upstream: origin: "https://github.com/zeldaret/ZAPD.git" branch: "master" commit: "86160be69" git-subrepo: version: "0.4.3" origin: "???" commit: "???" * Change nanoseconds to seconds in extract_assets.py Co-authored-by: Roman971 <32455037+Roman971@users.noreply.github.com>
885 lines
21 KiB
C++
885 lines
21 KiB
C++
#include "ZTexture.h"
|
|
|
|
#include <cassert>
|
|
#include "BitConverter.h"
|
|
#include "CRC32.h"
|
|
#include "Directory.h"
|
|
#include "File.h"
|
|
#include "Globals.h"
|
|
#include "Path.h"
|
|
|
|
REGISTER_ZFILENODE(Texture, ZTexture);
|
|
|
|
ZTexture::ZTexture(ZFile* nParent) : ZResource(nParent)
|
|
{
|
|
width = 0;
|
|
height = 0;
|
|
|
|
RegisterRequiredAttribute("Width");
|
|
RegisterRequiredAttribute("Height");
|
|
RegisterRequiredAttribute("Format");
|
|
RegisterOptionalAttribute("TlutOffset");
|
|
}
|
|
|
|
void ZTexture::ExtractFromXML(tinyxml2::XMLElement* reader, const std::vector<uint8_t>& nRawData,
|
|
uint32_t nRawDataIndex)
|
|
{
|
|
ZResource::ExtractFromXML(reader, nRawData, nRawDataIndex);
|
|
|
|
auto filepath = Globals::Instance->outputPath / fs::path(name).stem();
|
|
|
|
std::string incStr =
|
|
StringHelper::Sprintf("%s.%s.inc.c", filepath.c_str(), GetExternalExtension().c_str());
|
|
|
|
parent->AddDeclarationIncludeArray(rawDataIndex, incStr, GetRawDataSize(), GetSourceTypeName(),
|
|
name, 0);
|
|
}
|
|
|
|
void ZTexture::FromBinary(const std::vector<uint8_t>& nRawData, uint32_t nRawDataIndex,
|
|
int32_t nWidth, int32_t nHeight, TextureType nType, bool nIsPalette)
|
|
{
|
|
width = nWidth;
|
|
height = nHeight;
|
|
format = nType;
|
|
rawDataIndex = nRawDataIndex;
|
|
isPalette = nIsPalette;
|
|
name = GetDefaultName(parent->GetName());
|
|
outName = name;
|
|
|
|
rawData.assign(nRawData.begin(), nRawData.end());
|
|
|
|
ParseRawData();
|
|
CalcHash();
|
|
}
|
|
|
|
void ZTexture::FromPNG(const fs::path& pngFilePath, TextureType texType)
|
|
{
|
|
format = texType;
|
|
name = StringHelper::Split(Path::GetFileNameWithoutExtension(pngFilePath), ".")[0];
|
|
PrepareRawDataFromFile(pngFilePath);
|
|
}
|
|
|
|
void ZTexture::FromHLTexture(HLTexture* hlTex)
|
|
{
|
|
width = hlTex->width;
|
|
height = hlTex->height;
|
|
format = static_cast<TextureType>(hlTex->type);
|
|
}
|
|
|
|
void ZTexture::ParseXML(tinyxml2::XMLElement* reader)
|
|
{
|
|
ZResource::ParseXML(reader);
|
|
|
|
std::string widthXml = registeredAttributes.at("Width").value;
|
|
std::string heightXml = registeredAttributes.at("Height").value;
|
|
|
|
if (!StringHelper::HasOnlyDigits(widthXml))
|
|
{
|
|
throw std::runtime_error(StringHelper::Sprintf(
|
|
"ZTexture::ParseXML: Error in %s\n"
|
|
"\t Value of 'Width' attribute has non-decimal digits: '%s'.\n",
|
|
name.c_str(), widthXml.c_str()));
|
|
}
|
|
if (!StringHelper::HasOnlyDigits(heightXml))
|
|
{
|
|
throw std::runtime_error(StringHelper::Sprintf(
|
|
"ZTexture::ParseXML: Error in %s\n"
|
|
"\t Value of 'Height' attribute has non-decimal digits: '%s'.\n",
|
|
name.c_str(), heightXml.c_str()));
|
|
}
|
|
|
|
width = StringHelper::StrToL(widthXml);
|
|
height = StringHelper::StrToL(heightXml);
|
|
|
|
std::string formatStr = registeredAttributes.at("Format").value;
|
|
format = GetTextureTypeFromString(formatStr);
|
|
|
|
if (format == TextureType::Error)
|
|
throw std::runtime_error("Format " + formatStr + " is not supported!");
|
|
|
|
const auto& tlutOffsetAttr = registeredAttributes.at("TlutOffset");
|
|
if (tlutOffsetAttr.wasSet)
|
|
{
|
|
switch (format)
|
|
{
|
|
case TextureType::Palette4bpp:
|
|
case TextureType::Palette8bpp:
|
|
tlutOffset = StringHelper::StrToL(tlutOffsetAttr.value, 16);
|
|
break;
|
|
|
|
default:
|
|
throw std::runtime_error(StringHelper::Sprintf(
|
|
"ZTexture::ParseXML: Error in %s\n"
|
|
"\t 'TlutOffset' declared in non color-indexed (ci4 or ci8) texture.\n",
|
|
name.c_str()));
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
void ZTexture::ParseRawData()
|
|
{
|
|
switch (format)
|
|
{
|
|
case TextureType::RGBA16bpp:
|
|
PrepareBitmapRGBA16();
|
|
break;
|
|
case TextureType::RGBA32bpp:
|
|
PrepareBitmapRGBA32();
|
|
break;
|
|
case TextureType::Grayscale4bpp:
|
|
PrepareBitmapGrayscale4();
|
|
break;
|
|
case TextureType::Grayscale8bpp:
|
|
PrepareBitmapGrayscale8();
|
|
break;
|
|
case TextureType::GrayscaleAlpha4bpp:
|
|
PrepareBitmapGrayscaleAlpha4();
|
|
break;
|
|
case TextureType::GrayscaleAlpha8bpp:
|
|
PrepareBitmapGrayscaleAlpha8();
|
|
break;
|
|
case TextureType::GrayscaleAlpha16bpp:
|
|
PrepareBitmapGrayscaleAlpha16();
|
|
break;
|
|
case TextureType::Palette4bpp:
|
|
PrepareBitmapPalette4();
|
|
break;
|
|
case TextureType::Palette8bpp:
|
|
PrepareBitmapPalette8();
|
|
break;
|
|
default:
|
|
throw std::runtime_error("Format is not supported!");
|
|
}
|
|
}
|
|
|
|
void ZTexture::PrepareBitmapRGBA16()
|
|
{
|
|
textureData.InitEmptyRGBImage(width, height, true);
|
|
auto parentRawData = parent->GetRawData();
|
|
for (size_t y = 0; y < height; y++)
|
|
{
|
|
for (size_t x = 0; x < width; x++)
|
|
{
|
|
int32_t pos = rawDataIndex + ((y * width) + x) * 2;
|
|
uint16_t data = parentRawData.at(pos + 1) | (parentRawData.at(pos) << 8);
|
|
uint8_t r = (data & 0xF800) >> 11;
|
|
uint8_t g = (data & 0x07C0) >> 6;
|
|
uint8_t b = (data & 0x003E) >> 1;
|
|
uint8_t alpha = data & 0x01;
|
|
|
|
textureData.SetRGBPixel(y, x, r * 8, g * 8, b * 8, alpha * 255);
|
|
}
|
|
}
|
|
}
|
|
|
|
void ZTexture::PrepareBitmapRGBA32()
|
|
{
|
|
textureData.InitEmptyRGBImage(width, height, true);
|
|
auto parentRawData = parent->GetRawData();
|
|
for (size_t y = 0; y < height; y++)
|
|
{
|
|
for (size_t x = 0; x < width; x++)
|
|
{
|
|
size_t pos = rawDataIndex + ((y * width) + x) * 4;
|
|
uint8_t r = parentRawData.at(pos + 0);
|
|
uint8_t g = parentRawData.at(pos + 1);
|
|
uint8_t b = parentRawData.at(pos + 2);
|
|
uint8_t alpha = parentRawData.at(pos + 3);
|
|
|
|
textureData.SetRGBPixel(y, x, r, g, b, alpha);
|
|
}
|
|
}
|
|
}
|
|
|
|
void ZTexture::PrepareBitmapGrayscale4()
|
|
{
|
|
textureData.InitEmptyRGBImage(width, height, false);
|
|
auto parentRawData = parent->GetRawData();
|
|
for (size_t y = 0; y < height; y++)
|
|
{
|
|
for (size_t x = 0; x < width; x += 2)
|
|
{
|
|
for (uint8_t i = 0; i < 2; i++)
|
|
{
|
|
size_t pos = rawDataIndex + ((y * width) + x) / 2;
|
|
uint8_t grayscale = 0;
|
|
|
|
if (i == 0)
|
|
grayscale = parentRawData.at(pos) & 0xF0;
|
|
else
|
|
grayscale = (parentRawData.at(pos) & 0x0F) << 4;
|
|
|
|
textureData.SetGrayscalePixel(y, x + i, grayscale);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void ZTexture::PrepareBitmapGrayscale8()
|
|
{
|
|
textureData.InitEmptyRGBImage(width, height, false);
|
|
auto parentRawData = parent->GetRawData();
|
|
for (size_t y = 0; y < height; y++)
|
|
{
|
|
for (size_t x = 0; x < width; x++)
|
|
{
|
|
size_t pos = rawDataIndex + ((y * width) + x) * 1;
|
|
|
|
textureData.SetGrayscalePixel(y, x, parentRawData.at(pos));
|
|
}
|
|
}
|
|
}
|
|
|
|
void ZTexture::PrepareBitmapGrayscaleAlpha4()
|
|
{
|
|
textureData.InitEmptyRGBImage(width, height, true);
|
|
auto parentRawData = parent->GetRawData();
|
|
for (size_t y = 0; y < height; y++)
|
|
{
|
|
for (size_t x = 0; x < width; x += 2)
|
|
{
|
|
for (uint16_t i = 0; i < 2; i++)
|
|
{
|
|
size_t pos = rawDataIndex + ((y * width) + x) / 2;
|
|
uint8_t data = 0;
|
|
|
|
if (i == 0)
|
|
data = (parentRawData.at(pos) & 0xF0) >> 4;
|
|
else
|
|
data = parentRawData.at(pos) & 0x0F;
|
|
|
|
uint8_t grayscale = ((data & 0x0E) >> 1) * 32;
|
|
uint8_t alpha = (data & 0x01) * 255;
|
|
|
|
textureData.SetGrayscalePixel(y, x + i, grayscale, alpha);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void ZTexture::PrepareBitmapGrayscaleAlpha8()
|
|
{
|
|
textureData.InitEmptyRGBImage(width, height, true);
|
|
auto parentRawData = parent->GetRawData();
|
|
for (size_t y = 0; y < height; y++)
|
|
{
|
|
for (size_t x = 0; x < width; x++)
|
|
{
|
|
size_t pos = rawDataIndex + ((y * width) + x) * 1;
|
|
uint8_t grayscale = parentRawData.at(pos) & 0xF0;
|
|
uint8_t alpha = (parentRawData.at(pos) & 0x0F) << 4;
|
|
|
|
textureData.SetGrayscalePixel(y, x, grayscale, alpha);
|
|
}
|
|
}
|
|
}
|
|
|
|
void ZTexture::PrepareBitmapGrayscaleAlpha16()
|
|
{
|
|
textureData.InitEmptyRGBImage(width, height, true);
|
|
auto parentRawData = parent->GetRawData();
|
|
for (size_t y = 0; y < height; y++)
|
|
{
|
|
for (size_t x = 0; x < width; x++)
|
|
{
|
|
size_t pos = rawDataIndex + ((y * width) + x) * 2;
|
|
uint8_t grayscale = parentRawData.at(pos + 0);
|
|
uint8_t alpha = parentRawData.at(pos + 1);
|
|
|
|
textureData.SetGrayscalePixel(y, x, grayscale, alpha);
|
|
}
|
|
}
|
|
}
|
|
|
|
void ZTexture::PrepareBitmapPalette4()
|
|
{
|
|
textureData.InitEmptyPaletteImage(width, height);
|
|
auto parentRawData = parent->GetRawData();
|
|
for (size_t y = 0; y < height; y++)
|
|
{
|
|
for (size_t x = 0; x < width; x += 2)
|
|
{
|
|
for (uint16_t i = 0; i < 2; i++)
|
|
{
|
|
size_t pos = rawDataIndex + ((y * width) + x) / 2;
|
|
uint8_t paletteIndex = 0;
|
|
|
|
if (i == 0)
|
|
paletteIndex = (parentRawData.at(pos) & 0xF0) >> 4;
|
|
else
|
|
paletteIndex = (parentRawData.at(pos) & 0x0F);
|
|
|
|
textureData.SetIndexedPixel(y, x + i, paletteIndex, paletteIndex * 16);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void ZTexture::PrepareBitmapPalette8()
|
|
{
|
|
textureData.InitEmptyPaletteImage(width, height);
|
|
auto parentRawData = parent->GetRawData();
|
|
for (size_t y = 0; y < height; y++)
|
|
{
|
|
for (size_t x = 0; x < width; x++)
|
|
{
|
|
size_t pos = rawDataIndex + ((y * width) + x) * 1;
|
|
uint8_t grayscale = parentRawData.at(pos);
|
|
|
|
textureData.SetIndexedPixel(y, x, grayscale, grayscale);
|
|
}
|
|
}
|
|
}
|
|
|
|
void ZTexture::DeclareReferences(const std::string& prefix)
|
|
{
|
|
if (tlutOffset != static_cast<uint32_t>(-1))
|
|
{
|
|
tlut = parent->GetTextureResource(tlutOffset);
|
|
if (tlut == nullptr)
|
|
{
|
|
int32_t tlutDim = 16;
|
|
if (format == TextureType::Palette4bpp)
|
|
tlutDim = 4;
|
|
|
|
auto filepath = Globals::Instance->outputPath / fs::path(name).stem();
|
|
std::string incStr = StringHelper::Sprintf("%s.%s.inc.c", filepath.c_str(),
|
|
GetExternalExtension().c_str());
|
|
|
|
tlut = new ZTexture(parent);
|
|
tlut->FromBinary(rawData, tlutOffset, tlutDim, tlutDim, TextureType::RGBA16bpp, true);
|
|
parent->AddTextureResource(tlutOffset, tlut);
|
|
parent->AddDeclarationIncludeArray(tlutOffset, incStr, tlut->GetRawDataSize(),
|
|
tlut->GetSourceTypeName(), tlut->GetName(), 0);
|
|
}
|
|
else
|
|
{
|
|
tlut->isPalette = true;
|
|
}
|
|
SetTlut(tlut);
|
|
}
|
|
}
|
|
|
|
void ZTexture::PrepareRawDataFromFile(const fs::path& pngFilePath)
|
|
{
|
|
switch (format)
|
|
{
|
|
case TextureType::RGBA16bpp:
|
|
PrepareRawDataRGBA16(pngFilePath);
|
|
break;
|
|
case TextureType::RGBA32bpp:
|
|
PrepareRawDataRGBA32(pngFilePath);
|
|
break;
|
|
case TextureType::Grayscale4bpp:
|
|
PrepareRawDataGrayscale4(pngFilePath);
|
|
break;
|
|
case TextureType::Grayscale8bpp:
|
|
PrepareRawDataGrayscale8(pngFilePath);
|
|
break;
|
|
case TextureType::GrayscaleAlpha4bpp:
|
|
PrepareRawDataGrayscaleAlpha4(pngFilePath);
|
|
break;
|
|
case TextureType::GrayscaleAlpha8bpp:
|
|
PrepareRawDataGrayscaleAlpha8(pngFilePath);
|
|
break;
|
|
case TextureType::GrayscaleAlpha16bpp:
|
|
PrepareRawDataGrayscaleAlpha16(pngFilePath);
|
|
break;
|
|
case TextureType::Palette4bpp:
|
|
PrepareRawDataPalette4(pngFilePath);
|
|
break;
|
|
case TextureType::Palette8bpp:
|
|
PrepareRawDataPalette8(pngFilePath);
|
|
break;
|
|
default:
|
|
throw std::runtime_error("Format is not supported!");
|
|
}
|
|
}
|
|
|
|
void ZTexture::PrepareRawDataRGBA16(const fs::path& rgbaPath)
|
|
{
|
|
textureData.ReadPng(rgbaPath);
|
|
|
|
width = textureData.GetWidth();
|
|
height = textureData.GetHeight();
|
|
|
|
textureDataRaw.clear();
|
|
textureDataRaw.resize(GetRawDataSize());
|
|
for (uint16_t y = 0; y < height; y++)
|
|
{
|
|
for (uint16_t x = 0; x < width; x++)
|
|
{
|
|
size_t pos = ((y * width) + x) * 2;
|
|
RGBAPixel pixel = textureData.GetPixel(y, x);
|
|
|
|
uint8_t r = pixel.r / 8;
|
|
uint8_t g = pixel.g / 8;
|
|
uint8_t b = pixel.b / 8;
|
|
|
|
uint8_t alphaBit = pixel.a != 0;
|
|
|
|
uint16_t data = (r << 11) + (g << 6) + (b << 1) + alphaBit;
|
|
|
|
textureDataRaw[pos + 0] = (data & 0xFF00) >> 8;
|
|
textureDataRaw[pos + 1] = (data & 0x00FF);
|
|
}
|
|
}
|
|
}
|
|
|
|
void ZTexture::PrepareRawDataRGBA32(const fs::path& rgbaPath)
|
|
{
|
|
textureData.ReadPng(rgbaPath);
|
|
|
|
width = textureData.GetWidth();
|
|
height = textureData.GetHeight();
|
|
|
|
textureDataRaw.clear();
|
|
textureDataRaw.resize(GetRawDataSize());
|
|
for (uint16_t y = 0; y < height; y++)
|
|
{
|
|
for (uint16_t x = 0; x < width; x++)
|
|
{
|
|
size_t pos = ((y * width) + x) * 4;
|
|
RGBAPixel pixel = textureData.GetPixel(y, x);
|
|
|
|
textureDataRaw[pos + 0] = pixel.r;
|
|
textureDataRaw[pos + 1] = pixel.g;
|
|
textureDataRaw[pos + 2] = pixel.b;
|
|
textureDataRaw[pos + 3] = pixel.a;
|
|
}
|
|
}
|
|
}
|
|
|
|
void ZTexture::PrepareRawDataGrayscale4(const fs::path& grayPath)
|
|
{
|
|
textureData.ReadPng(grayPath);
|
|
|
|
width = textureData.GetWidth();
|
|
height = textureData.GetHeight();
|
|
|
|
textureDataRaw.clear();
|
|
textureDataRaw.resize(GetRawDataSize());
|
|
for (uint16_t y = 0; y < height; y++)
|
|
{
|
|
for (uint16_t x = 0; x < width; x += 2)
|
|
{
|
|
size_t pos = ((y * width) + x) / 2;
|
|
uint8_t r1 = textureData.GetPixel(y, x).r;
|
|
uint8_t r2 = textureData.GetPixel(y, x + 1).r;
|
|
|
|
textureDataRaw[pos] = (uint8_t)(((r1 / 16) << 4) + (r2 / 16));
|
|
}
|
|
}
|
|
}
|
|
|
|
void ZTexture::PrepareRawDataGrayscale8(const fs::path& grayPath)
|
|
{
|
|
textureData.ReadPng(grayPath);
|
|
|
|
width = textureData.GetWidth();
|
|
height = textureData.GetHeight();
|
|
|
|
textureDataRaw.clear();
|
|
textureDataRaw.resize(GetRawDataSize());
|
|
for (uint16_t y = 0; y < height; y++)
|
|
{
|
|
for (uint16_t x = 0; x < width; x++)
|
|
{
|
|
size_t pos = (y * width) + x;
|
|
RGBAPixel pixel = textureData.GetPixel(y, x);
|
|
textureDataRaw[pos] = pixel.r;
|
|
}
|
|
}
|
|
}
|
|
|
|
void ZTexture::PrepareRawDataGrayscaleAlpha4(const fs::path& grayAlphaPath)
|
|
{
|
|
textureData.ReadPng(grayAlphaPath);
|
|
|
|
width = textureData.GetWidth();
|
|
height = textureData.GetHeight();
|
|
|
|
textureDataRaw.clear();
|
|
textureDataRaw.resize(GetRawDataSize());
|
|
for (uint16_t y = 0; y < height; y++)
|
|
{
|
|
for (uint16_t x = 0; x < width; x += 2)
|
|
{
|
|
size_t pos = ((y * width) + x) / 2;
|
|
uint8_t data = 0;
|
|
|
|
for (uint16_t i = 0; i < 2; i++)
|
|
{
|
|
RGBAPixel pixel = textureData.GetPixel(y, x + i);
|
|
uint8_t cR = pixel.r;
|
|
uint8_t alphaBit = pixel.a != 0;
|
|
|
|
if (i == 0)
|
|
data |= (((cR / 32) << 1) + alphaBit) << 4;
|
|
else
|
|
data |= ((cR / 32) << 1) + alphaBit;
|
|
}
|
|
|
|
textureDataRaw[pos] = data;
|
|
}
|
|
}
|
|
}
|
|
|
|
void ZTexture::PrepareRawDataGrayscaleAlpha8(const fs::path& grayAlphaPath)
|
|
{
|
|
textureData.ReadPng(grayAlphaPath);
|
|
|
|
width = textureData.GetWidth();
|
|
height = textureData.GetHeight();
|
|
|
|
textureDataRaw.clear();
|
|
textureDataRaw.resize(GetRawDataSize());
|
|
for (uint16_t y = 0; y < height; y++)
|
|
{
|
|
for (uint16_t x = 0; x < width; x++)
|
|
{
|
|
size_t pos = ((y * width) + x) * 1;
|
|
RGBAPixel pixel = textureData.GetPixel(y, x);
|
|
|
|
uint8_t r = pixel.r;
|
|
uint8_t a = pixel.a;
|
|
|
|
textureDataRaw[pos] = ((r / 16) << 4) + (a / 16);
|
|
}
|
|
}
|
|
}
|
|
|
|
void ZTexture::PrepareRawDataGrayscaleAlpha16(const fs::path& grayAlphaPath)
|
|
{
|
|
textureData.ReadPng(grayAlphaPath);
|
|
|
|
width = textureData.GetWidth();
|
|
height = textureData.GetHeight();
|
|
|
|
textureDataRaw.clear();
|
|
textureDataRaw.resize(GetRawDataSize());
|
|
for (uint16_t y = 0; y < height; y++)
|
|
{
|
|
for (uint16_t x = 0; x < width; x++)
|
|
{
|
|
size_t pos = ((y * width) + x) * 2;
|
|
RGBAPixel pixel = textureData.GetPixel(y, x);
|
|
|
|
uint8_t cR = pixel.r;
|
|
uint8_t aR = pixel.a;
|
|
|
|
textureDataRaw[pos + 0] = cR;
|
|
textureDataRaw[pos + 1] = aR;
|
|
}
|
|
}
|
|
}
|
|
|
|
void ZTexture::PrepareRawDataPalette4(const fs::path& palPath)
|
|
{
|
|
textureData.ReadPng(palPath);
|
|
|
|
width = textureData.GetWidth();
|
|
height = textureData.GetHeight();
|
|
|
|
textureDataRaw.clear();
|
|
textureDataRaw.resize(GetRawDataSize());
|
|
for (uint16_t y = 0; y < height; y++)
|
|
{
|
|
for (uint16_t x = 0; x < width; x += 2)
|
|
{
|
|
size_t pos = ((y * width) + x) / 2;
|
|
|
|
uint8_t cR1 = textureData.GetIndexedPixel(y, x);
|
|
uint8_t cR2 = textureData.GetIndexedPixel(y, x + 1);
|
|
|
|
textureDataRaw[pos] = (cR1 << 4) | (cR2);
|
|
}
|
|
}
|
|
}
|
|
|
|
void ZTexture::PrepareRawDataPalette8(const fs::path& palPath)
|
|
{
|
|
textureData.ReadPng(palPath);
|
|
|
|
width = textureData.GetWidth();
|
|
height = textureData.GetHeight();
|
|
|
|
textureDataRaw.clear();
|
|
textureDataRaw.resize(GetRawDataSize());
|
|
for (uint16_t y = 0; y < height; y++)
|
|
{
|
|
for (uint16_t x = 0; x < width; x++)
|
|
{
|
|
size_t pos = ((y * width) + x);
|
|
uint8_t cR = textureData.GetIndexedPixel(y, x);
|
|
|
|
textureDataRaw[pos] = cR;
|
|
}
|
|
}
|
|
}
|
|
|
|
float ZTexture::GetPixelMultiplyer() const
|
|
{
|
|
switch (format)
|
|
{
|
|
case TextureType::Grayscale4bpp:
|
|
case TextureType::GrayscaleAlpha4bpp:
|
|
case TextureType::Palette4bpp:
|
|
return 0.5f;
|
|
case TextureType::Grayscale8bpp:
|
|
case TextureType::GrayscaleAlpha8bpp:
|
|
case TextureType::Palette8bpp:
|
|
return 1;
|
|
case TextureType::GrayscaleAlpha16bpp:
|
|
case TextureType::RGBA16bpp:
|
|
return 2;
|
|
case TextureType::RGBA32bpp:
|
|
return 4;
|
|
default:
|
|
return -1;
|
|
}
|
|
}
|
|
|
|
size_t ZTexture::GetRawDataSize() const
|
|
{
|
|
return (width * height * GetPixelMultiplyer());
|
|
}
|
|
|
|
std::string ZTexture::GetIMFmtFromType()
|
|
{
|
|
switch (format)
|
|
{
|
|
case TextureType::RGBA32bpp:
|
|
case TextureType::RGBA16bpp:
|
|
return "G_IM_FMT_RGBA";
|
|
case TextureType::Grayscale4bpp:
|
|
case TextureType::Grayscale8bpp:
|
|
return "G_IM_FMT_I";
|
|
case TextureType::Palette4bpp:
|
|
case TextureType::Palette8bpp:
|
|
return "G_IM_FMT_CI";
|
|
case TextureType::GrayscaleAlpha4bpp:
|
|
case TextureType::GrayscaleAlpha8bpp:
|
|
case TextureType::GrayscaleAlpha16bpp:
|
|
return "G_IM_FMT_IA";
|
|
default:
|
|
return "ERROR";
|
|
}
|
|
}
|
|
|
|
std::string ZTexture::GetIMSizFromType()
|
|
{
|
|
switch (format)
|
|
{
|
|
case TextureType::Grayscale4bpp:
|
|
case TextureType::Palette4bpp:
|
|
case TextureType::GrayscaleAlpha4bpp:
|
|
return "G_IM_SIZ_4b";
|
|
case TextureType::Palette8bpp:
|
|
case TextureType::Grayscale8bpp:
|
|
return "G_IM_SIZ_8b";
|
|
case TextureType::GrayscaleAlpha16bpp:
|
|
case TextureType::RGBA16bpp:
|
|
return "G_IM_SIZ_16b";
|
|
case TextureType::RGBA32bpp:
|
|
return "G_IM_SIZ_32b";
|
|
default:
|
|
return "ERROR";
|
|
}
|
|
}
|
|
|
|
std::string ZTexture::GetDefaultName(const std::string& prefix)
|
|
{
|
|
const char* suffix = "Tex";
|
|
if (isPalette)
|
|
suffix = "TLUT";
|
|
return StringHelper::Sprintf("%s%s_%06X", prefix.c_str(), suffix, rawDataIndex);
|
|
}
|
|
|
|
uint32_t ZTexture::GetWidth() const
|
|
{
|
|
return width;
|
|
}
|
|
|
|
uint32_t ZTexture::GetHeight() const
|
|
{
|
|
return height;
|
|
}
|
|
|
|
void ZTexture::SetDimensions(uint32_t nWidth, uint32_t nHeight)
|
|
{
|
|
width = nWidth;
|
|
height = nHeight;
|
|
ParseRawData();
|
|
}
|
|
|
|
TextureType ZTexture::GetTextureType() const
|
|
{
|
|
return format;
|
|
}
|
|
|
|
void ZTexture::Save(const fs::path& outFolder)
|
|
{
|
|
// Optionally generate text file containing CRC information. This is going to be a one time
|
|
// process for generating the Texture Pool XML.
|
|
if (Globals::Instance->outputCrc)
|
|
{
|
|
File::WriteAllText(Globals::Instance->outputPath / (outName + ".txt"),
|
|
StringHelper::Sprintf("%08lX", hash));
|
|
}
|
|
|
|
auto outPath = GetPoolOutPath(outFolder);
|
|
|
|
if (!Directory::Exists(outPath))
|
|
Directory::CreateDirectory(outPath);
|
|
|
|
auto outFileName = outPath / (outName + "." + GetExternalExtension() + ".png");
|
|
|
|
#ifdef TEXTURE_DEBUG
|
|
printf("Saving PNG: %s\n", outFileName.c_str());
|
|
printf("\t Var name: %s\n", name.c_str());
|
|
if (tlut != nullptr)
|
|
printf("\t TLUT name: %s\n", tlut->name.c_str());
|
|
#endif
|
|
|
|
textureData.WritePng(outFileName);
|
|
|
|
#ifdef TEXTURE_DEBUG
|
|
printf("\n");
|
|
#endif
|
|
}
|
|
|
|
std::string ZTexture::GetBodySourceCode() const
|
|
{
|
|
std::string sourceOutput = "";
|
|
|
|
for (size_t i = 0; i < textureDataRaw.size(); i += 8)
|
|
{
|
|
if (i % 32 == 0)
|
|
sourceOutput += " ";
|
|
|
|
sourceOutput +=
|
|
StringHelper::Sprintf("0x%016llX, ", BitConverter::ToUInt64BE(textureDataRaw, i));
|
|
|
|
if (i % 32 == 24)
|
|
sourceOutput += StringHelper::Sprintf(" // 0x%06X \n", rawDataIndex + ((i / 32) * 32));
|
|
}
|
|
|
|
// Ensure there's always a trailing line feed to prevent dumb warnings.
|
|
// Please don't remove this line, unless you somehow made a way to prevent
|
|
// that warning when building the OoT repo.
|
|
sourceOutput += "\n";
|
|
|
|
return sourceOutput;
|
|
}
|
|
|
|
bool ZTexture::IsExternalResource() const
|
|
{
|
|
return true;
|
|
}
|
|
|
|
ZResourceType ZTexture::GetResourceType() const
|
|
{
|
|
return ZResourceType::Texture;
|
|
}
|
|
|
|
std::string ZTexture::GetSourceTypeName() const
|
|
{
|
|
return "u64";
|
|
}
|
|
|
|
void ZTexture::CalcHash()
|
|
{
|
|
auto parentRawData = parent->GetRawData();
|
|
hash = CRC32B(parentRawData.data() + rawDataIndex, GetRawDataSize());
|
|
}
|
|
|
|
std::string ZTexture::GetExternalExtension() const
|
|
{
|
|
switch (format)
|
|
{
|
|
case TextureType::RGBA32bpp:
|
|
return "rgba32";
|
|
case TextureType::RGBA16bpp:
|
|
return "rgb5a1";
|
|
case TextureType::Grayscale4bpp:
|
|
return "i4";
|
|
case TextureType::Grayscale8bpp:
|
|
return "i8";
|
|
case TextureType::GrayscaleAlpha4bpp:
|
|
return "ia4";
|
|
case TextureType::GrayscaleAlpha8bpp:
|
|
return "ia8";
|
|
case TextureType::GrayscaleAlpha16bpp:
|
|
return "ia16";
|
|
case TextureType::Palette4bpp:
|
|
return "ci4";
|
|
case TextureType::Palette8bpp:
|
|
return "ci8";
|
|
default:
|
|
return "ERROR";
|
|
}
|
|
}
|
|
|
|
fs::path ZTexture::GetPoolOutPath(const fs::path& defaultValue)
|
|
{
|
|
if (Globals::Instance->cfg.texturePool.find(hash) != Globals::Instance->cfg.texturePool.end())
|
|
return Path::GetDirectoryName(Globals::Instance->cfg.texturePool[hash].path.string());
|
|
|
|
return defaultValue;
|
|
}
|
|
|
|
TextureType ZTexture::GetTextureTypeFromString(std::string str)
|
|
{
|
|
TextureType texType = TextureType::Error;
|
|
|
|
if (str == "rgba32")
|
|
texType = TextureType::RGBA32bpp;
|
|
else if (str == "rgb5a1")
|
|
texType = TextureType::RGBA16bpp;
|
|
else if (str == "i4")
|
|
texType = TextureType::Grayscale4bpp;
|
|
else if (str == "i8")
|
|
texType = TextureType::Grayscale8bpp;
|
|
else if (str == "ia4")
|
|
texType = TextureType::GrayscaleAlpha4bpp;
|
|
else if (str == "ia8")
|
|
texType = TextureType::GrayscaleAlpha8bpp;
|
|
else if (str == "ia16")
|
|
texType = TextureType::GrayscaleAlpha16bpp;
|
|
else if (str == "ci4")
|
|
texType = TextureType::Palette4bpp;
|
|
else if (str == "ci8")
|
|
texType = TextureType::Palette8bpp;
|
|
else
|
|
fprintf(stderr, "Encountered Unknown Texture format %s \n", str.c_str());
|
|
return texType;
|
|
}
|
|
|
|
bool ZTexture::IsColorIndexed() const
|
|
{
|
|
switch (format)
|
|
{
|
|
case TextureType::Palette4bpp:
|
|
case TextureType::Palette8bpp:
|
|
return true;
|
|
|
|
default:
|
|
return false;
|
|
}
|
|
}
|
|
|
|
void ZTexture::SetTlut(ZTexture* nTlut)
|
|
{
|
|
assert(IsColorIndexed());
|
|
assert(nTlut->isPalette);
|
|
tlut = nTlut;
|
|
|
|
textureData.SetPalette(tlut->textureData);
|
|
}
|
|
|
|
bool ZTexture::HasTlut() const
|
|
{
|
|
return tlut != nullptr;
|
|
}
|