Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/ImageConverter/ImageConverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,16 @@ namespace
return false;
}

const auto loadResult = image::LoadIwi(file);
auto loadResult = image::LoadIwi(file);
if (!loadResult)
return false;

auto texture(std::move(loadResult->m_texture));

auto convertedTexture = ConvertTextureForDdsFileOutputIfNecessary(texture.get());
if (convertedTexture)
texture = std::move(convertedTexture);

auto outPath = iwiPath;
outPath.replace_extension(".dds");

Expand Down
54 changes: 50 additions & 4 deletions src/ModMan/Asset/Image/DynamicAssetsImage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

#include "Context/ModManContext.h"
#include "Game/CommonAsset.h"
#include "Image/Compression/ImageDecompressor.h"
#include "Image/DdsWriter.h"
#include "Image/ImageToCommonConverter.h"
#include "Image/TextureConverter.h"
#include "Pool/XAssetInfo.h"
#include "SearchPath/SearchPaths.h"
#include "Utils/Logging/Log.h"
#include "Utils/StringUtils.h"

Expand Down Expand Up @@ -44,6 +45,26 @@ namespace
return false;
}

std::optional<ImageFormatId> NeedsConversionToWebGlSupportedFormat(const ImageFormatId imageFormatId)
{
static std::unordered_map<ImageFormatId, ImageFormatId> unsupportedFormatMap{
{ImageFormatId::BC4, ImageFormatId::B8_G8_R8 },
{ImageFormatId::BC5, ImageFormatId::B8_G8_R8 },
{ImageFormatId::B8_G8_R8_X8, ImageFormatId::B8_G8_R8 },
{ImageFormatId::R8_G8_B8, ImageFormatId::B8_G8_R8 },
{ImageFormatId::R8_G8_B8_A8, ImageFormatId::B8_G8_R8_A8},
{ImageFormatId::A8, ImageFormatId::B8_G8_R8 },
{ImageFormatId::R8, ImageFormatId::B8_G8_R8 },
{ImageFormatId::R8_A8, ImageFormatId::B8_G8_R8 },
};

const auto entry = unsupportedFormatMap.find(imageFormatId);
if (entry != unsupportedFormatMap.end())
return entry->second;

return std::nullopt;
}

void ImageDds(const webwindowed::dynamic_asset_request& request, webwindowed::dynamic_asset_response& response)
{
const auto imageName = request.get_query("name");
Expand All @@ -68,8 +89,8 @@ namespace
assert(zone);

const auto gameName = GameId_Names[std::to_underlying(zone->m_game_id)];
const auto converter = ToCommonConverter::GetForGame(zone->m_game_id);
if (!converter)
const auto toCommonConverter = ToCommonConverter::GetForGame(zone->m_game_id);
if (!toCommonConverter)
{
con::error("No image converter for game {}", gameName);
response.send_response(500);
Expand All @@ -79,7 +100,7 @@ namespace
std::unique_ptr<Texture> texture;
{
const auto searchPaths = ModManContext::Get().m_fast_file.GetSearchPaths();
texture = converter->Convert(*image, searchPaths.Data());
texture = toCommonConverter->Convert(*image, searchPaths.Data());
if (!texture)
{
con::warn("Failed to convert image {} of zone {}", *imageName, *zoneName);
Expand All @@ -88,6 +109,31 @@ namespace
}
}

const auto* originalTextureFormat = texture->GetFormat();
const auto originalTextureFormatId = originalTextureFormat->GetId();
const auto targetFormatId = NeedsConversionToWebGlSupportedFormat(originalTextureFormatId);
if (targetFormatId)
{
const auto* targetFormat = ImageFormat::GetImageFormatById(*targetFormatId);
if (originalTextureFormat->GetType() == ImageFormatType::BLOCK_COMPRESSED)
{
auto* textureDecompressor = ImageDecompressor::GetDecompressorForFormat(originalTextureFormatId);
assert(textureDecompressor);

if (textureDecompressor)
texture = textureDecompressor->Decompress(*texture, targetFormat);
}
else
{
TextureConverter converter(texture.get(), targetFormat);
auto newTexture = converter.Convert();
assert(newTexture);

if (newTexture)
texture = std::move(newTexture);
}
}

std::ostringstream ss;
DdsWriter output;
output.DumpImage(ss, texture.get());
Expand Down
448 changes: 448 additions & 0 deletions src/ObjImage/Image/Compression/DecompressorBc1.cpp

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions src/ObjImage/Image/Compression/DecompressorBc1.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#pragma once

#include "ImageDecompressor.h"

namespace image
{
class DecompressorBc1 final : public ImageDecompressor
{
public:
std::unique_ptr<Texture> Decompress(const Texture& input, const ImageFormat* targetFormat) override;
};
} // namespace image
Loading