mirror of
https://github.com/Laupetin/OpenAssetTools.git
synced 2025-06-10 23:08:05 -05:00
refactor: extract image code into single component
This commit is contained in:
@ -3,6 +3,8 @@
|
||||
#include "Game/IW4/IW4.h"
|
||||
#include "Utils/MemoryManager.h"
|
||||
|
||||
#include <istream>
|
||||
|
||||
namespace IW4
|
||||
{
|
||||
bool LoadLeaderboardAsJson(std::istream& stream, LeaderboardDef& leaderboard, MemoryManager* memory);
|
||||
|
@ -3,6 +3,8 @@
|
||||
#include "Game/IW5/IW5.h"
|
||||
#include "Utils/MemoryManager.h"
|
||||
|
||||
#include <istream>
|
||||
|
||||
namespace IW5
|
||||
{
|
||||
bool LoadLeaderboardAsJson(std::istream& stream, LeaderboardDef& leaderboard, MemoryManager* memory);
|
||||
|
@ -1,271 +0,0 @@
|
||||
#include "DdsLoader.h"
|
||||
|
||||
#include "Image/DdsTypes.h"
|
||||
#include "Utils/ClassUtils.h"
|
||||
#include "Utils/FileUtils.h"
|
||||
|
||||
#include <format>
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
|
||||
namespace dds
|
||||
{
|
||||
class DdsLoaderInternal
|
||||
{
|
||||
static constexpr auto DDS_MAGIC = FileUtils::MakeMagic32('D', 'D', 'S', ' ');
|
||||
|
||||
std::istream& m_stream;
|
||||
|
||||
TextureType m_texture_type;
|
||||
bool m_has_mip_maps;
|
||||
size_t m_width;
|
||||
size_t m_height;
|
||||
size_t m_depth;
|
||||
const ImageFormat* m_format;
|
||||
|
||||
_NODISCARD bool ReadMagic() const
|
||||
{
|
||||
uint32_t magic;
|
||||
m_stream.read(reinterpret_cast<char*>(&magic), sizeof(magic));
|
||||
if (m_stream.gcount() != sizeof(magic))
|
||||
{
|
||||
std::cerr << "Failed to read dds data\n";
|
||||
return false;
|
||||
}
|
||||
|
||||
if (magic != DDS_MAGIC)
|
||||
{
|
||||
std::cerr << "Invalid magic for dds\n";
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
_NODISCARD bool ReadDxt10Header()
|
||||
{
|
||||
DDS_HEADER_DXT10 headerDx10{};
|
||||
m_stream.read(reinterpret_cast<char*>(&headerDx10), sizeof(headerDx10));
|
||||
if (m_stream.gcount() != sizeof(headerDx10))
|
||||
{
|
||||
std::cerr << "Failed to read dds data\n";
|
||||
return false;
|
||||
}
|
||||
|
||||
if (headerDx10.resourceDimension == D3D10_RESOURCE_DIMENSION_TEXTURE3D)
|
||||
{
|
||||
m_texture_type = TextureType::T_3D;
|
||||
}
|
||||
else if (headerDx10.resourceDimension == D3D10_RESOURCE_DIMENSION_TEXTURE2D)
|
||||
{
|
||||
if (headerDx10.miscFlag & DDS_RESOURCE_MISC_TEXTURECUBE || headerDx10.arraySize == 6)
|
||||
{
|
||||
m_texture_type = TextureType::T_CUBE;
|
||||
}
|
||||
else
|
||||
{
|
||||
m_texture_type = TextureType::T_2D;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cerr << std::format("Unsupported dds resourceDimension {}\n", static_cast<unsigned>(headerDx10.resourceDimension));
|
||||
return false;
|
||||
}
|
||||
|
||||
for (const auto* imageFormat : ImageFormat::ALL_FORMATS)
|
||||
{
|
||||
if (imageFormat->GetDxgiFormat() == headerDx10.dxgiFormat)
|
||||
{
|
||||
m_format = imageFormat;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
std::cerr << std::format("Unsupported dds dxgi format {}\n", static_cast<unsigned>(headerDx10.dxgiFormat));
|
||||
return false;
|
||||
}
|
||||
|
||||
_NODISCARD bool ReadPixelFormatFourCc(DDS_PIXELFORMAT& pf)
|
||||
{
|
||||
switch (pf.dwFourCC)
|
||||
{
|
||||
case FileUtils::MakeMagic32('D', 'X', 'T', '1'):
|
||||
m_format = &ImageFormat::FORMAT_BC1;
|
||||
return true;
|
||||
|
||||
case FileUtils::MakeMagic32('D', 'X', 'T', '3'):
|
||||
m_format = &ImageFormat::FORMAT_BC2;
|
||||
return true;
|
||||
|
||||
case FileUtils::MakeMagic32('D', 'X', 'T', '5'):
|
||||
m_format = &ImageFormat::FORMAT_BC3;
|
||||
return true;
|
||||
|
||||
case FileUtils::MakeMagic32('D', 'X', '1', '0'):
|
||||
return ReadDxt10Header();
|
||||
|
||||
default:
|
||||
std::cerr << std::format("Unknown dds FourCC {}\n", pf.dwFourCC);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
static void ExtractSizeAndOffsetFromMask(uint32_t mask, unsigned& offset, unsigned& size)
|
||||
{
|
||||
offset = 0;
|
||||
size = 0;
|
||||
|
||||
if (mask == 0)
|
||||
return;
|
||||
|
||||
while ((mask & 1) == 0)
|
||||
{
|
||||
offset++;
|
||||
mask >>= 1;
|
||||
}
|
||||
|
||||
while ((mask & 1) == 1)
|
||||
{
|
||||
size++;
|
||||
mask >>= 1;
|
||||
}
|
||||
}
|
||||
|
||||
_NODISCARD bool ReadPixelFormatUnsigned(DDS_PIXELFORMAT& pf)
|
||||
{
|
||||
unsigned rOffset, rSize, gOffset, gSize, bOffset, bSize, aOffset, aSize;
|
||||
|
||||
ExtractSizeAndOffsetFromMask(pf.dwRBitMask, rOffset, rSize);
|
||||
ExtractSizeAndOffsetFromMask(pf.dwGBitMask, gOffset, gSize);
|
||||
ExtractSizeAndOffsetFromMask(pf.dwBBitMask, bOffset, bSize);
|
||||
ExtractSizeAndOffsetFromMask(pf.dwABitMask, aOffset, aSize);
|
||||
|
||||
for (const auto* imageFormat : ImageFormat::ALL_FORMATS)
|
||||
{
|
||||
if (imageFormat->GetType() != ImageFormatType::UNSIGNED)
|
||||
continue;
|
||||
|
||||
const auto* unsignedImageFormat = dynamic_cast<const ImageFormatUnsigned*>(imageFormat);
|
||||
|
||||
if (unsignedImageFormat->m_r_offset == rOffset && unsignedImageFormat->m_r_size == rSize && unsignedImageFormat->m_g_offset == gOffset
|
||||
&& unsignedImageFormat->m_g_size == gSize && unsignedImageFormat->m_b_offset == bOffset && unsignedImageFormat->m_b_size == bSize
|
||||
&& unsignedImageFormat->m_a_offset == aOffset && unsignedImageFormat->m_a_size == aSize)
|
||||
{
|
||||
m_format = imageFormat;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
std::cerr << std::format(
|
||||
"Failed to find dds pixel format: R={:#x} G={:#x} B={:#x} A={:#x}\n", pf.dwRBitMask, pf.dwGBitMask, pf.dwBBitMask, pf.dwABitMask);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
_NODISCARD bool ReadPixelFormat(DDS_PIXELFORMAT& pf)
|
||||
{
|
||||
if (pf.dwFlags & DDPF_FOURCC)
|
||||
return ReadPixelFormatFourCc(pf);
|
||||
|
||||
return ReadPixelFormatUnsigned(pf);
|
||||
}
|
||||
|
||||
_NODISCARD bool ReadHeader()
|
||||
{
|
||||
DDS_HEADER header{};
|
||||
m_stream.read(reinterpret_cast<char*>(&header), sizeof(header));
|
||||
if (m_stream.gcount() != sizeof(header))
|
||||
{
|
||||
std::cerr << "Failed to read dds data\n";
|
||||
return false;
|
||||
}
|
||||
|
||||
m_width = header.dwWidth;
|
||||
m_height = header.dwHeight;
|
||||
m_depth = header.dwDepth;
|
||||
m_has_mip_maps = (header.dwCaps & DDSCAPS_MIPMAP) != 0 || header.dwMipMapCount > 1;
|
||||
|
||||
if (header.dwCaps2 & DDSCAPS2_CUBEMAP)
|
||||
m_texture_type = TextureType::T_CUBE;
|
||||
else if (header.dwDepth > 1)
|
||||
m_texture_type = TextureType::T_3D;
|
||||
else
|
||||
m_texture_type = TextureType::T_2D;
|
||||
|
||||
return ReadPixelFormat(header.ddspf);
|
||||
}
|
||||
|
||||
_NODISCARD std::unique_ptr<Texture> ReadTextureData() const
|
||||
{
|
||||
std::unique_ptr<Texture> result;
|
||||
|
||||
switch (m_texture_type)
|
||||
{
|
||||
case TextureType::T_2D:
|
||||
result = std::make_unique<Texture2D>(m_format, m_width, m_height, m_has_mip_maps);
|
||||
break;
|
||||
|
||||
case TextureType::T_3D:
|
||||
result = std::make_unique<Texture3D>(m_format, m_width, m_height, m_depth, m_has_mip_maps);
|
||||
break;
|
||||
|
||||
case TextureType::T_CUBE:
|
||||
result = std::make_unique<TextureCube>(m_format, m_width, m_height, m_has_mip_maps);
|
||||
break;
|
||||
|
||||
default:
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
const auto mipMapCount = m_has_mip_maps ? result->GetMipMapCount() : 1;
|
||||
const auto faceCount = m_texture_type == TextureType::T_CUBE ? 6 : 1;
|
||||
|
||||
result->Allocate();
|
||||
|
||||
for (auto mipLevel = 0; mipLevel < mipMapCount; mipLevel++)
|
||||
{
|
||||
const auto mipSize = result->GetSizeOfMipLevel(mipLevel);
|
||||
|
||||
for (auto face = 0; face < faceCount; face++)
|
||||
{
|
||||
m_stream.read(reinterpret_cast<char*>(result->GetBufferForMipLevel(mipLevel, face)), mipSize);
|
||||
|
||||
if (m_stream.gcount() != mipSize)
|
||||
{
|
||||
std::cerr << "Failed to read texture data from dds\n";
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public:
|
||||
explicit DdsLoaderInternal(std::istream& stream)
|
||||
: m_stream(stream),
|
||||
m_texture_type(TextureType::T_2D),
|
||||
m_has_mip_maps(false),
|
||||
m_width(0u),
|
||||
m_height(0u),
|
||||
m_depth(0u),
|
||||
m_format(nullptr)
|
||||
{
|
||||
}
|
||||
|
||||
std::unique_ptr<Texture> LoadDds()
|
||||
{
|
||||
if (!ReadMagic() || !ReadHeader())
|
||||
return nullptr;
|
||||
|
||||
return ReadTextureData();
|
||||
}
|
||||
};
|
||||
|
||||
std::unique_ptr<Texture> LoadDds(std::istream& stream)
|
||||
{
|
||||
DdsLoaderInternal internal(stream);
|
||||
return internal.LoadDds();
|
||||
}
|
||||
} // namespace dds
|
@ -1,10 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include "Image/Texture.h"
|
||||
|
||||
#include <istream>
|
||||
|
||||
namespace dds
|
||||
{
|
||||
std::unique_ptr<Texture> LoadDds(std::istream& stream);
|
||||
}
|
@ -1,104 +0,0 @@
|
||||
#include "Dx12TextureLoader.h"
|
||||
|
||||
#include <cstring>
|
||||
|
||||
Dx12TextureLoader::Dx12TextureLoader()
|
||||
: m_format(oat::DXGI_FORMAT_UNKNOWN),
|
||||
m_type(TextureType::T_2D),
|
||||
m_has_mip_maps(false),
|
||||
m_width(1u),
|
||||
m_height(1u),
|
||||
m_depth(1u)
|
||||
{
|
||||
}
|
||||
|
||||
const ImageFormat* Dx12TextureLoader::GetFormatForDx12Format() const
|
||||
{
|
||||
for (auto i : ImageFormat::ALL_FORMATS)
|
||||
{
|
||||
if (i->GetDxgiFormat() == m_format)
|
||||
return i;
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
Dx12TextureLoader& Dx12TextureLoader::Format(const oat::DXGI_FORMAT format)
|
||||
{
|
||||
m_format = format;
|
||||
return *this;
|
||||
}
|
||||
|
||||
Dx12TextureLoader& Dx12TextureLoader::Type(const TextureType textureType)
|
||||
{
|
||||
m_type = textureType;
|
||||
return *this;
|
||||
}
|
||||
|
||||
Dx12TextureLoader& Dx12TextureLoader::HasMipMaps(const bool hasMipMaps)
|
||||
{
|
||||
m_has_mip_maps = hasMipMaps;
|
||||
return *this;
|
||||
}
|
||||
|
||||
Dx12TextureLoader& Dx12TextureLoader::Width(const size_t width)
|
||||
{
|
||||
m_width = width;
|
||||
return *this;
|
||||
}
|
||||
|
||||
Dx12TextureLoader& Dx12TextureLoader::Height(const size_t height)
|
||||
{
|
||||
m_height = height;
|
||||
return *this;
|
||||
}
|
||||
|
||||
Dx12TextureLoader& Dx12TextureLoader::Depth(const size_t depth)
|
||||
{
|
||||
m_depth = depth;
|
||||
return *this;
|
||||
}
|
||||
|
||||
std::unique_ptr<Texture> Dx12TextureLoader::LoadTexture(const void* data)
|
||||
{
|
||||
const auto* format = GetFormatForDx12Format();
|
||||
|
||||
if (format == nullptr)
|
||||
return nullptr;
|
||||
|
||||
std::unique_ptr<Texture> texture;
|
||||
switch (m_type)
|
||||
{
|
||||
case TextureType::T_2D:
|
||||
texture = std::make_unique<Texture2D>(format, m_width, m_height, m_has_mip_maps);
|
||||
break;
|
||||
|
||||
case TextureType::T_3D:
|
||||
texture = std::make_unique<Texture3D>(format, m_width, m_height, m_depth, m_has_mip_maps);
|
||||
break;
|
||||
|
||||
case TextureType::T_CUBE:
|
||||
texture = std::make_unique<TextureCube>(format, m_width, m_width, m_has_mip_maps);
|
||||
break;
|
||||
|
||||
default:
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
texture->Allocate();
|
||||
const auto mipMapCount = m_has_mip_maps ? texture->GetMipMapCount() : 1;
|
||||
const auto faceCount = m_type == TextureType::T_CUBE ? 6 : 1;
|
||||
const void* currentDataOffset = data;
|
||||
|
||||
for (auto currentMipLevel = 0; currentMipLevel < mipMapCount; currentMipLevel++)
|
||||
{
|
||||
for (auto currentFace = 0; currentFace < faceCount; currentFace++)
|
||||
{
|
||||
const auto mipSize = texture->GetSizeOfMipLevel(currentMipLevel);
|
||||
memcpy(texture->GetBufferForMipLevel(currentMipLevel, currentFace), currentDataOffset, mipSize);
|
||||
currentDataOffset = reinterpret_cast<const void*>(reinterpret_cast<uintptr_t>(currentDataOffset) + mipSize);
|
||||
}
|
||||
}
|
||||
|
||||
return texture;
|
||||
}
|
@ -1,36 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include "Image/DxgiFormat.h"
|
||||
#include "Image/Texture.h"
|
||||
#include "Utils/ClassUtils.h"
|
||||
#include "Utils/MemoryManager.h"
|
||||
|
||||
#include <memory>
|
||||
#include <unordered_map>
|
||||
|
||||
class Dx12TextureLoader
|
||||
{
|
||||
public:
|
||||
Dx12TextureLoader();
|
||||
|
||||
Dx12TextureLoader& Format(oat::DXGI_FORMAT format);
|
||||
Dx12TextureLoader& Type(TextureType textureType);
|
||||
Dx12TextureLoader& HasMipMaps(bool hasMipMaps);
|
||||
Dx12TextureLoader& Width(size_t width);
|
||||
Dx12TextureLoader& Height(size_t height);
|
||||
Dx12TextureLoader& Depth(size_t depth);
|
||||
|
||||
std::unique_ptr<Texture> LoadTexture(const void* data);
|
||||
|
||||
private:
|
||||
_NODISCARD const ImageFormat* GetFormatForDx12Format() const;
|
||||
|
||||
static std::unordered_map<ImageFormatId, ImageFormatId> m_conversion_table;
|
||||
|
||||
oat::DXGI_FORMAT m_format;
|
||||
TextureType m_type;
|
||||
bool m_has_mip_maps;
|
||||
size_t m_width;
|
||||
size_t m_height;
|
||||
size_t m_depth;
|
||||
};
|
@ -1,104 +0,0 @@
|
||||
#include "Dx9TextureLoader.h"
|
||||
|
||||
#include <cstring>
|
||||
|
||||
Dx9TextureLoader::Dx9TextureLoader()
|
||||
: m_format(oat::D3DFMT_UNKNOWN),
|
||||
m_type(TextureType::T_2D),
|
||||
m_has_mip_maps(false),
|
||||
m_width(1u),
|
||||
m_height(1u),
|
||||
m_depth(1u)
|
||||
{
|
||||
}
|
||||
|
||||
const ImageFormat* Dx9TextureLoader::GetFormatForDx9Format() const
|
||||
{
|
||||
for (const auto* i : ImageFormat::ALL_FORMATS)
|
||||
{
|
||||
if (i->GetD3DFormat() == m_format)
|
||||
return i;
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
Dx9TextureLoader& Dx9TextureLoader::Format(const oat::D3DFORMAT format)
|
||||
{
|
||||
m_format = format;
|
||||
return *this;
|
||||
}
|
||||
|
||||
Dx9TextureLoader& Dx9TextureLoader::Type(const TextureType textureType)
|
||||
{
|
||||
m_type = textureType;
|
||||
return *this;
|
||||
}
|
||||
|
||||
Dx9TextureLoader& Dx9TextureLoader::HasMipMaps(const bool hasMipMaps)
|
||||
{
|
||||
m_has_mip_maps = hasMipMaps;
|
||||
return *this;
|
||||
}
|
||||
|
||||
Dx9TextureLoader& Dx9TextureLoader::Width(const size_t width)
|
||||
{
|
||||
m_width = width;
|
||||
return *this;
|
||||
}
|
||||
|
||||
Dx9TextureLoader& Dx9TextureLoader::Height(const size_t height)
|
||||
{
|
||||
m_height = height;
|
||||
return *this;
|
||||
}
|
||||
|
||||
Dx9TextureLoader& Dx9TextureLoader::Depth(const size_t depth)
|
||||
{
|
||||
m_depth = depth;
|
||||
return *this;
|
||||
}
|
||||
|
||||
std::unique_ptr<Texture> Dx9TextureLoader::LoadTexture(const void* data)
|
||||
{
|
||||
const auto* format = GetFormatForDx9Format();
|
||||
|
||||
if (format == nullptr)
|
||||
return nullptr;
|
||||
|
||||
std::unique_ptr<Texture> texture;
|
||||
switch (m_type)
|
||||
{
|
||||
case TextureType::T_2D:
|
||||
texture = std::make_unique<Texture2D>(format, m_width, m_height, m_has_mip_maps);
|
||||
break;
|
||||
|
||||
case TextureType::T_3D:
|
||||
texture = std::make_unique<Texture3D>(format, m_width, m_height, m_depth, m_has_mip_maps);
|
||||
break;
|
||||
|
||||
case TextureType::T_CUBE:
|
||||
texture = std::make_unique<TextureCube>(format, m_width, m_width, m_has_mip_maps);
|
||||
break;
|
||||
|
||||
default:
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
texture->Allocate();
|
||||
const auto mipMapCount = m_has_mip_maps ? texture->GetMipMapCount() : 1;
|
||||
const auto faceCount = m_type == TextureType::T_CUBE ? 6 : 1;
|
||||
const void* currentDataOffset = data;
|
||||
|
||||
for (auto currentMipLevel = 0; currentMipLevel < mipMapCount; currentMipLevel++)
|
||||
{
|
||||
for (auto currentFace = 0; currentFace < faceCount; currentFace++)
|
||||
{
|
||||
const auto mipSize = texture->GetSizeOfMipLevel(currentMipLevel);
|
||||
memcpy(texture->GetBufferForMipLevel(currentMipLevel, currentFace), currentDataOffset, mipSize);
|
||||
currentDataOffset = reinterpret_cast<const void*>(reinterpret_cast<uintptr_t>(currentDataOffset) + mipSize);
|
||||
}
|
||||
}
|
||||
|
||||
return texture;
|
||||
}
|
@ -1,34 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include "Image/D3DFormat.h"
|
||||
#include "Image/Texture.h"
|
||||
#include "Utils/ClassUtils.h"
|
||||
#include "Utils/MemoryManager.h"
|
||||
|
||||
#include <memory>
|
||||
#include <unordered_map>
|
||||
|
||||
class Dx9TextureLoader
|
||||
{
|
||||
public:
|
||||
Dx9TextureLoader();
|
||||
|
||||
Dx9TextureLoader& Format(oat::D3DFORMAT format);
|
||||
Dx9TextureLoader& Type(TextureType textureType);
|
||||
Dx9TextureLoader& HasMipMaps(bool hasMipMaps);
|
||||
Dx9TextureLoader& Width(size_t width);
|
||||
Dx9TextureLoader& Height(size_t height);
|
||||
Dx9TextureLoader& Depth(size_t depth);
|
||||
|
||||
std::unique_ptr<Texture> LoadTexture(const void* data);
|
||||
|
||||
private:
|
||||
_NODISCARD const ImageFormat* GetFormatForDx9Format() const;
|
||||
|
||||
oat::D3DFORMAT m_format;
|
||||
TextureType m_type;
|
||||
bool m_has_mip_maps;
|
||||
size_t m_width;
|
||||
size_t m_height;
|
||||
size_t m_depth;
|
||||
};
|
@ -1,443 +0,0 @@
|
||||
#include "IwiLoader.h"
|
||||
|
||||
#include "Image/IwiTypes.h"
|
||||
|
||||
#include <cassert>
|
||||
#include <format>
|
||||
#include <iostream>
|
||||
#include <type_traits>
|
||||
|
||||
namespace iwi
|
||||
{
|
||||
const ImageFormat* GetFormat6(int8_t format)
|
||||
{
|
||||
switch (static_cast<iwi6::IwiFormat>(format))
|
||||
{
|
||||
case iwi6::IwiFormat::IMG_FORMAT_BITMAP_RGBA:
|
||||
return &ImageFormat::FORMAT_R8_G8_B8_A8;
|
||||
case iwi6::IwiFormat::IMG_FORMAT_BITMAP_RGB:
|
||||
return &ImageFormat::FORMAT_R8_G8_B8;
|
||||
case iwi6::IwiFormat::IMG_FORMAT_BITMAP_ALPHA:
|
||||
return &ImageFormat::FORMAT_A8;
|
||||
case iwi6::IwiFormat::IMG_FORMAT_DXT1:
|
||||
return &ImageFormat::FORMAT_BC1;
|
||||
case iwi6::IwiFormat::IMG_FORMAT_DXT3:
|
||||
return &ImageFormat::FORMAT_BC2;
|
||||
case iwi6::IwiFormat::IMG_FORMAT_DXT5:
|
||||
return &ImageFormat::FORMAT_BC3;
|
||||
case iwi6::IwiFormat::IMG_FORMAT_DXN:
|
||||
return &ImageFormat::FORMAT_BC5;
|
||||
case iwi6::IwiFormat::IMG_FORMAT_BITMAP_LUMINANCE_ALPHA:
|
||||
return &ImageFormat::FORMAT_R8_A8;
|
||||
case iwi6::IwiFormat::IMG_FORMAT_BITMAP_LUMINANCE:
|
||||
return &ImageFormat::FORMAT_R8;
|
||||
case iwi6::IwiFormat::IMG_FORMAT_WAVELET_RGBA: // used
|
||||
case iwi6::IwiFormat::IMG_FORMAT_WAVELET_RGB: // used
|
||||
case iwi6::IwiFormat::IMG_FORMAT_WAVELET_LUMINANCE_ALPHA:
|
||||
case iwi6::IwiFormat::IMG_FORMAT_WAVELET_LUMINANCE:
|
||||
case iwi6::IwiFormat::IMG_FORMAT_WAVELET_ALPHA:
|
||||
std::cerr << std::format("Unsupported IWI format: {}\n", format);
|
||||
break;
|
||||
default:
|
||||
std::cerr << std::format("Unknown IWI format: {}\n", format);
|
||||
break;
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
std::unique_ptr<Texture> LoadIwi6(std::istream& stream)
|
||||
{
|
||||
iwi6::IwiHeader header{};
|
||||
|
||||
stream.read(reinterpret_cast<char*>(&header), sizeof(header));
|
||||
if (stream.gcount() != sizeof(header))
|
||||
return nullptr;
|
||||
|
||||
const auto* format = GetFormat6(header.format);
|
||||
if (format == nullptr)
|
||||
return nullptr;
|
||||
|
||||
auto width = header.dimensions[0];
|
||||
auto height = header.dimensions[1];
|
||||
auto depth = header.dimensions[2];
|
||||
auto hasMipMaps = !(header.flags & iwi6::IwiFlags::IMG_FLAG_NOMIPMAPS);
|
||||
|
||||
std::unique_ptr<Texture> texture;
|
||||
if (header.flags & iwi6::IwiFlags::IMG_FLAG_CUBEMAP)
|
||||
texture = std::make_unique<TextureCube>(format, width, height, hasMipMaps);
|
||||
else if (header.flags & iwi6::IwiFlags::IMG_FLAG_VOLMAP)
|
||||
texture = std::make_unique<Texture3D>(format, width, height, depth, hasMipMaps);
|
||||
else
|
||||
texture = std::make_unique<Texture2D>(format, width, height, hasMipMaps);
|
||||
|
||||
texture->Allocate();
|
||||
|
||||
auto currentFileSize = sizeof(iwi6::IwiHeader) + sizeof(IwiVersion);
|
||||
const auto mipMapCount = hasMipMaps ? texture->GetMipMapCount() : 1;
|
||||
|
||||
for (auto currentMipLevel = mipMapCount - 1; currentMipLevel >= 0; currentMipLevel--)
|
||||
{
|
||||
const auto sizeOfMipLevel = texture->GetSizeOfMipLevel(currentMipLevel) * texture->GetFaceCount();
|
||||
currentFileSize += sizeOfMipLevel;
|
||||
|
||||
if (currentMipLevel < static_cast<int>(std::extent_v<decltype(iwi6::IwiHeader::fileSizeForPicmip)>)
|
||||
&& currentFileSize != header.fileSizeForPicmip[currentMipLevel])
|
||||
{
|
||||
std::cerr << std::format("Iwi has invalid file size for picmip {}\n", currentMipLevel);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
stream.read(reinterpret_cast<char*>(texture->GetBufferForMipLevel(currentMipLevel)), sizeOfMipLevel);
|
||||
if (stream.gcount() != sizeOfMipLevel)
|
||||
{
|
||||
std::cerr << std::format("Unexpected eof of iwi in mip level {}\n", currentMipLevel);
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
return texture;
|
||||
}
|
||||
|
||||
const ImageFormat* GetFormat8(int8_t format)
|
||||
{
|
||||
switch (static_cast<iwi8::IwiFormat>(format))
|
||||
{
|
||||
case iwi8::IwiFormat::IMG_FORMAT_BITMAP_RGBA:
|
||||
return &ImageFormat::FORMAT_R8_G8_B8_A8;
|
||||
case iwi8::IwiFormat::IMG_FORMAT_BITMAP_RGB:
|
||||
return &ImageFormat::FORMAT_R8_G8_B8;
|
||||
case iwi8::IwiFormat::IMG_FORMAT_BITMAP_ALPHA:
|
||||
return &ImageFormat::FORMAT_A8;
|
||||
case iwi8::IwiFormat::IMG_FORMAT_DXT1:
|
||||
return &ImageFormat::FORMAT_BC1;
|
||||
case iwi8::IwiFormat::IMG_FORMAT_DXT3:
|
||||
return &ImageFormat::FORMAT_BC2;
|
||||
case iwi8::IwiFormat::IMG_FORMAT_DXT5:
|
||||
return &ImageFormat::FORMAT_BC3;
|
||||
case iwi8::IwiFormat::IMG_FORMAT_DXN:
|
||||
return &ImageFormat::FORMAT_BC5;
|
||||
case iwi8::IwiFormat::IMG_FORMAT_BITMAP_LUMINANCE_ALPHA:
|
||||
return &ImageFormat::FORMAT_R8_A8;
|
||||
case iwi8::IwiFormat::IMG_FORMAT_BITMAP_LUMINANCE:
|
||||
return &ImageFormat::FORMAT_R8;
|
||||
case iwi8::IwiFormat::IMG_FORMAT_WAVELET_RGBA: // used
|
||||
case iwi8::IwiFormat::IMG_FORMAT_WAVELET_RGB: // used
|
||||
case iwi8::IwiFormat::IMG_FORMAT_WAVELET_LUMINANCE_ALPHA:
|
||||
case iwi8::IwiFormat::IMG_FORMAT_WAVELET_LUMINANCE:
|
||||
case iwi8::IwiFormat::IMG_FORMAT_WAVELET_ALPHA:
|
||||
case iwi8::IwiFormat::IMG_FORMAT_DXT3A_AS_LUMINANCE:
|
||||
case iwi8::IwiFormat::IMG_FORMAT_DXT5A_AS_LUMINANCE:
|
||||
case iwi8::IwiFormat::IMG_FORMAT_DXT3A_AS_ALPHA:
|
||||
case iwi8::IwiFormat::IMG_FORMAT_DXT5A_AS_ALPHA:
|
||||
case iwi8::IwiFormat::IMG_FORMAT_DXT1_AS_LUMINANCE_ALPHA:
|
||||
case iwi8::IwiFormat::IMG_FORMAT_DXN_AS_LUMINANCE_ALPHA:
|
||||
case iwi8::IwiFormat::IMG_FORMAT_DXT1_AS_LUMINANCE:
|
||||
case iwi8::IwiFormat::IMG_FORMAT_DXT1_AS_ALPHA:
|
||||
std::cerr << std::format("Unsupported IWI format: {}\n", format);
|
||||
break;
|
||||
default:
|
||||
std::cerr << std::format("Unknown IWI format: {}\n", format);
|
||||
break;
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
std::unique_ptr<Texture> LoadIwi8(std::istream& stream)
|
||||
{
|
||||
iwi8::IwiHeader header{};
|
||||
|
||||
stream.read(reinterpret_cast<char*>(&header), sizeof(header));
|
||||
if (stream.gcount() != sizeof(header))
|
||||
return nullptr;
|
||||
|
||||
const auto* format = GetFormat8(header.format);
|
||||
if (format == nullptr)
|
||||
return nullptr;
|
||||
|
||||
auto width = header.dimensions[0];
|
||||
auto height = header.dimensions[1];
|
||||
auto depth = header.dimensions[2];
|
||||
auto hasMipMaps = !(header.flags & iwi8::IwiFlags::IMG_FLAG_NOMIPMAPS);
|
||||
|
||||
std::unique_ptr<Texture> texture;
|
||||
if ((header.flags & iwi8::IwiFlags::IMG_FLAG_MAPTYPE_MASK) == iwi8::IwiFlags::IMG_FLAG_MAPTYPE_CUBE)
|
||||
{
|
||||
texture = std::make_unique<TextureCube>(format, width, height, hasMipMaps);
|
||||
}
|
||||
else if ((header.flags & iwi8::IwiFlags::IMG_FLAG_MAPTYPE_MASK) == iwi8::IwiFlags::IMG_FLAG_MAPTYPE_3D)
|
||||
{
|
||||
texture = std::make_unique<Texture3D>(format, width, height, depth, hasMipMaps);
|
||||
}
|
||||
else if ((header.flags & iwi8::IwiFlags::IMG_FLAG_MAPTYPE_MASK) == iwi8::IwiFlags::IMG_FLAG_MAPTYPE_2D)
|
||||
{
|
||||
texture = std::make_unique<Texture2D>(format, width, height, hasMipMaps);
|
||||
}
|
||||
else if ((header.flags & iwi8::IwiFlags::IMG_FLAG_MAPTYPE_MASK) == iwi8::IwiFlags::IMG_FLAG_MAPTYPE_1D)
|
||||
{
|
||||
std::cerr << "Iwi has unsupported map type 1D\n";
|
||||
return nullptr;
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cerr << "Iwi has unsupported map type\n";
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
texture->Allocate();
|
||||
|
||||
auto currentFileSize = sizeof(iwi8::IwiHeader) + sizeof(IwiVersion);
|
||||
const auto mipMapCount = hasMipMaps ? texture->GetMipMapCount() : 1;
|
||||
|
||||
for (auto currentMipLevel = mipMapCount - 1; currentMipLevel >= 0; currentMipLevel--)
|
||||
{
|
||||
const auto sizeOfMipLevel = texture->GetSizeOfMipLevel(currentMipLevel) * texture->GetFaceCount();
|
||||
currentFileSize += sizeOfMipLevel;
|
||||
|
||||
if (currentMipLevel < static_cast<int>(std::extent_v<decltype(iwi8::IwiHeader::fileSizeForPicmip)>)
|
||||
&& currentFileSize != header.fileSizeForPicmip[currentMipLevel])
|
||||
{
|
||||
std::cerr << std::format("Iwi has invalid file size for picmip {}\n", currentMipLevel);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
stream.read(reinterpret_cast<char*>(texture->GetBufferForMipLevel(currentMipLevel)), sizeOfMipLevel);
|
||||
if (stream.gcount() != sizeOfMipLevel)
|
||||
{
|
||||
std::cerr << std::format("Unexpected eof of iwi in mip level {}\n", currentMipLevel);
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
return texture;
|
||||
}
|
||||
|
||||
const ImageFormat* GetFormat13(int8_t format)
|
||||
{
|
||||
switch (static_cast<iwi13::IwiFormat>(format))
|
||||
{
|
||||
case iwi13::IwiFormat::IMG_FORMAT_BITMAP_RGBA:
|
||||
return &ImageFormat::FORMAT_R8_G8_B8_A8;
|
||||
case iwi13::IwiFormat::IMG_FORMAT_BITMAP_RGB:
|
||||
return &ImageFormat::FORMAT_R8_G8_B8;
|
||||
case iwi13::IwiFormat::IMG_FORMAT_BITMAP_ALPHA:
|
||||
return &ImageFormat::FORMAT_A8;
|
||||
case iwi13::IwiFormat::IMG_FORMAT_DXT1:
|
||||
return &ImageFormat::FORMAT_BC1;
|
||||
case iwi13::IwiFormat::IMG_FORMAT_DXT3:
|
||||
return &ImageFormat::FORMAT_BC2;
|
||||
case iwi13::IwiFormat::IMG_FORMAT_DXT5:
|
||||
return &ImageFormat::FORMAT_BC3;
|
||||
case iwi13::IwiFormat::IMG_FORMAT_DXN:
|
||||
return &ImageFormat::FORMAT_BC5;
|
||||
case iwi13::IwiFormat::IMG_FORMAT_BITMAP_LUMINANCE_ALPHA:
|
||||
return &ImageFormat::FORMAT_R8_A8;
|
||||
case iwi13::IwiFormat::IMG_FORMAT_BITMAP_LUMINANCE:
|
||||
return &ImageFormat::FORMAT_R8;
|
||||
case iwi13::IwiFormat::IMG_FORMAT_WAVELET_RGBA: // used
|
||||
case iwi13::IwiFormat::IMG_FORMAT_WAVELET_RGB: // used
|
||||
case iwi13::IwiFormat::IMG_FORMAT_WAVELET_LUMINANCE_ALPHA:
|
||||
case iwi13::IwiFormat::IMG_FORMAT_WAVELET_LUMINANCE:
|
||||
case iwi13::IwiFormat::IMG_FORMAT_WAVELET_ALPHA:
|
||||
case iwi13::IwiFormat::IMG_FORMAT_BITMAP_RGB565:
|
||||
case iwi13::IwiFormat::IMG_FORMAT_BITMAP_RGB5A3:
|
||||
case iwi13::IwiFormat::IMG_FORMAT_BITMAP_C8:
|
||||
case iwi13::IwiFormat::IMG_FORMAT_BITMAP_RGBA8:
|
||||
case iwi13::IwiFormat::IMG_FORMAT_A16B16G16R16F:
|
||||
std::cerr << std::format("Unsupported IWI format: {}\n", format);
|
||||
break;
|
||||
default:
|
||||
std::cerr << std::format("Unknown IWI format: {}\n", format);
|
||||
break;
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
std::unique_ptr<Texture> LoadIwi13(std::istream& stream)
|
||||
{
|
||||
iwi13::IwiHeader header{};
|
||||
|
||||
stream.read(reinterpret_cast<char*>(&header), sizeof(header));
|
||||
if (stream.gcount() != sizeof(header))
|
||||
return nullptr;
|
||||
|
||||
const auto* format = GetFormat6(header.format);
|
||||
if (format == nullptr)
|
||||
return nullptr;
|
||||
|
||||
auto width = header.dimensions[0];
|
||||
auto height = header.dimensions[1];
|
||||
auto depth = header.dimensions[2];
|
||||
auto hasMipMaps = !(header.flags & iwi13::IwiFlags::IMG_FLAG_NOMIPMAPS);
|
||||
|
||||
std::unique_ptr<Texture> texture;
|
||||
if (header.flags & iwi13::IwiFlags::IMG_FLAG_CUBEMAP)
|
||||
texture = std::make_unique<TextureCube>(format, width, height, hasMipMaps);
|
||||
else if (header.flags & iwi13::IwiFlags::IMG_FLAG_VOLMAP)
|
||||
texture = std::make_unique<Texture3D>(format, width, height, depth, hasMipMaps);
|
||||
else
|
||||
texture = std::make_unique<Texture2D>(format, width, height, hasMipMaps);
|
||||
|
||||
texture->Allocate();
|
||||
|
||||
auto currentFileSize = sizeof(iwi13::IwiHeader) + sizeof(IwiVersion);
|
||||
const auto mipMapCount = hasMipMaps ? texture->GetMipMapCount() : 1;
|
||||
|
||||
for (auto currentMipLevel = mipMapCount - 1; currentMipLevel >= 0; currentMipLevel--)
|
||||
{
|
||||
const auto sizeOfMipLevel = texture->GetSizeOfMipLevel(currentMipLevel) * texture->GetFaceCount();
|
||||
currentFileSize += sizeOfMipLevel;
|
||||
|
||||
if (currentMipLevel < static_cast<int>(std::extent_v<decltype(iwi13::IwiHeader::fileSizeForPicmip)>)
|
||||
&& currentFileSize != header.fileSizeForPicmip[currentMipLevel])
|
||||
{
|
||||
std::cerr << std::format("Iwi has invalid file size for picmip {}\n", currentMipLevel);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
stream.read(reinterpret_cast<char*>(texture->GetBufferForMipLevel(currentMipLevel)), sizeOfMipLevel);
|
||||
if (stream.gcount() != sizeOfMipLevel)
|
||||
{
|
||||
std::cerr << std::format("Unexpected eof of iwi in mip level {}\n", currentMipLevel);
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
return texture;
|
||||
}
|
||||
|
||||
const ImageFormat* GetFormat27(int8_t format)
|
||||
{
|
||||
switch (static_cast<iwi27::IwiFormat>(format))
|
||||
{
|
||||
case iwi27::IwiFormat::IMG_FORMAT_BITMAP_RGBA:
|
||||
return &ImageFormat::FORMAT_R8_G8_B8_A8;
|
||||
case iwi27::IwiFormat::IMG_FORMAT_BITMAP_ALPHA:
|
||||
return &ImageFormat::FORMAT_A8;
|
||||
case iwi27::IwiFormat::IMG_FORMAT_DXT1:
|
||||
return &ImageFormat::FORMAT_BC1;
|
||||
case iwi27::IwiFormat::IMG_FORMAT_DXT3:
|
||||
return &ImageFormat::FORMAT_BC2;
|
||||
case iwi27::IwiFormat::IMG_FORMAT_DXT5:
|
||||
return &ImageFormat::FORMAT_BC3;
|
||||
case iwi27::IwiFormat::IMG_FORMAT_DXN:
|
||||
return &ImageFormat::FORMAT_BC5;
|
||||
case iwi27::IwiFormat::IMG_FORMAT_A16B16G16R16F:
|
||||
assert(false); // Unsupported yet
|
||||
return &ImageFormat::FORMAT_R16_G16_B16_A16_FLOAT;
|
||||
case iwi27::IwiFormat::IMG_FORMAT_BITMAP_RGB:
|
||||
return &ImageFormat::FORMAT_R8_G8_B8;
|
||||
case iwi27::IwiFormat::IMG_FORMAT_BITMAP_LUMINANCE_ALPHA:
|
||||
return &ImageFormat::FORMAT_R8_A8;
|
||||
case iwi27::IwiFormat::IMG_FORMAT_BITMAP_LUMINANCE:
|
||||
return &ImageFormat::FORMAT_R8;
|
||||
case iwi27::IwiFormat::IMG_FORMAT_WAVELET_RGBA:
|
||||
case iwi27::IwiFormat::IMG_FORMAT_WAVELET_RGB:
|
||||
case iwi27::IwiFormat::IMG_FORMAT_WAVELET_LUMINANCE_ALPHA:
|
||||
case iwi27::IwiFormat::IMG_FORMAT_WAVELET_LUMINANCE:
|
||||
case iwi27::IwiFormat::IMG_FORMAT_WAVELET_ALPHA:
|
||||
case iwi27::IwiFormat::IMG_FORMAT_BITMAP_RGB565:
|
||||
case iwi27::IwiFormat::IMG_FORMAT_BITMAP_RGB5A3:
|
||||
case iwi27::IwiFormat::IMG_FORMAT_BITMAP_C8:
|
||||
case iwi27::IwiFormat::IMG_FORMAT_BITMAP_RGBA8:
|
||||
std::cerr << std::format("Unsupported IWI format: {}\n", format);
|
||||
break;
|
||||
default:
|
||||
std::cerr << std::format("Unknown IWI format: {}\n", format);
|
||||
break;
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
std::unique_ptr<Texture> LoadIwi27(std::istream& stream)
|
||||
{
|
||||
iwi27::IwiHeader header{};
|
||||
|
||||
stream.read(reinterpret_cast<char*>(&header), sizeof(header));
|
||||
if (stream.gcount() != sizeof(header))
|
||||
return nullptr;
|
||||
|
||||
const auto* format = GetFormat27(header.format);
|
||||
if (format == nullptr)
|
||||
return nullptr;
|
||||
|
||||
auto width = header.dimensions[0];
|
||||
auto height = header.dimensions[1];
|
||||
auto depth = header.dimensions[2];
|
||||
auto hasMipMaps = !(header.flags & iwi27::IwiFlags::IMG_FLAG_NOMIPMAPS);
|
||||
|
||||
std::unique_ptr<Texture> texture;
|
||||
if (header.flags & iwi27::IwiFlags::IMG_FLAG_CUBEMAP)
|
||||
texture = std::make_unique<TextureCube>(format, width, height, hasMipMaps);
|
||||
else if (header.flags & iwi27::IwiFlags::IMG_FLAG_VOLMAP)
|
||||
texture = std::make_unique<Texture3D>(format, width, height, depth, hasMipMaps);
|
||||
else
|
||||
texture = std::make_unique<Texture2D>(format, width, height, hasMipMaps);
|
||||
|
||||
texture->Allocate();
|
||||
|
||||
auto currentFileSize = sizeof(iwi27::IwiHeader) + sizeof(IwiVersion);
|
||||
const auto mipMapCount = hasMipMaps ? texture->GetMipMapCount() : 1;
|
||||
|
||||
for (auto currentMipLevel = mipMapCount - 1; currentMipLevel >= 0; currentMipLevel--)
|
||||
{
|
||||
const auto sizeOfMipLevel = texture->GetSizeOfMipLevel(currentMipLevel) * texture->GetFaceCount();
|
||||
currentFileSize += sizeOfMipLevel;
|
||||
|
||||
if (currentMipLevel < static_cast<int>(std::extent_v<decltype(iwi27::IwiHeader::fileSizeForPicmip)>)
|
||||
&& currentFileSize != header.fileSizeForPicmip[currentMipLevel])
|
||||
{
|
||||
std::cerr << std::format("Iwi has invalid file size for picmip {}\n", currentMipLevel);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
stream.read(reinterpret_cast<char*>(texture->GetBufferForMipLevel(currentMipLevel)), sizeOfMipLevel);
|
||||
if (stream.gcount() != sizeOfMipLevel)
|
||||
{
|
||||
std::cerr << std::format("Unexpected eof of iwi in mip level {}\n", currentMipLevel);
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
return texture;
|
||||
}
|
||||
|
||||
std::unique_ptr<Texture> LoadIwi(std::istream& stream)
|
||||
{
|
||||
IwiVersion iwiVersion{};
|
||||
|
||||
stream.read(reinterpret_cast<char*>(&iwiVersion), sizeof(iwiVersion));
|
||||
if (stream.gcount() != sizeof(iwiVersion))
|
||||
return nullptr;
|
||||
|
||||
if (iwiVersion.tag[0] != 'I' || iwiVersion.tag[1] != 'W' || iwiVersion.tag[2] != 'i')
|
||||
{
|
||||
std::cerr << "Invalid IWI magic\n";
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
switch (iwiVersion.version)
|
||||
{
|
||||
case 6:
|
||||
return LoadIwi6(stream);
|
||||
|
||||
case 8:
|
||||
return LoadIwi8(stream);
|
||||
|
||||
case 13:
|
||||
return LoadIwi13(stream);
|
||||
|
||||
case 27:
|
||||
return LoadIwi27(stream);
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
std::cerr << std::format("Unknown IWI version {}\n", iwiVersion.version);
|
||||
return nullptr;
|
||||
}
|
||||
} // namespace iwi
|
@ -1,11 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include "Image/Texture.h"
|
||||
|
||||
#include <istream>
|
||||
#include <memory>
|
||||
|
||||
namespace iwi
|
||||
{
|
||||
std::unique_ptr<Texture> LoadIwi(std::istream& stream);
|
||||
}; // namespace iwi
|
Reference in New Issue
Block a user