Replace FileAPI with c++ streams and std::filesystem

This commit is contained in:
Jan
2021-03-03 14:04:35 +01:00
parent b6b0a57232
commit 1cd06668e0
96 changed files with 1355 additions and 1061 deletions

View File

@ -15,7 +15,7 @@ const std::map<ImageFormatId, ImageFormatId> DDS_CONVERSION_TABLE
class DdsWriterInternal
{
FileAPI::IFile* m_file;
std::ostream& m_stream;
Texture* m_texture;
std::unique_ptr<Texture> m_converted_texture;
bool m_use_dx10_extension;
@ -190,8 +190,8 @@ public:
return ".dds";
}
DdsWriterInternal(FileAPI::IFile* file, Texture* texture)
: m_file(file),
DdsWriterInternal(std::ostream& stream, Texture* texture)
: m_stream(stream),
m_texture(texture),
m_use_dx10_extension(false)
{
@ -204,24 +204,24 @@ public:
DDS_HEADER header{};
PopulateDdsHeader(header);
constexpr uint32_t magic = MakeFourCc('D', 'D', 'S', ' ');
constexpr auto magic = MakeFourCc('D', 'D', 'S', ' ');
m_file->Write(&magic, sizeof magic, 1);
m_file->Write(&header, sizeof header, 1);
m_stream.write(reinterpret_cast<const char*>(&magic), sizeof magic);
m_stream.write(reinterpret_cast<const char*>(&header), sizeof header);
if (m_use_dx10_extension)
{
DDS_HEADER_DXT10 dxt10{};
PopulateDxt10Header(dxt10);
m_file->Write(&dxt10, sizeof dxt10, 1);
m_stream.write(reinterpret_cast<const char*>(&dxt10), sizeof dxt10);
}
const int mipCount = m_texture->HasMipMaps() ? m_texture->GetMipMapCount() : 1;
const auto mipCount = m_texture->HasMipMaps() ? m_texture->GetMipMapCount() : 1;
for (auto mipLevel = 0; mipLevel < mipCount; mipLevel++)
{
const auto* buffer = m_texture->GetBufferForMipLevel(mipLevel);
const auto mipLevelSize = m_texture->GetSizeOfMipLevel(mipLevel) * m_texture->GetFaceCount();
m_file->Write(buffer, 1, mipLevelSize);
m_stream.write(reinterpret_cast<const char*>(buffer), mipLevelSize);
}
}
};
@ -239,8 +239,8 @@ std::string DdsWriter::GetFileExtension()
return DdsWriterInternal::GetFileExtension();
}
void DdsWriter::DumpImage(FileAPI::IFile* file, Texture* texture)
void DdsWriter::DumpImage(std::ostream& stream, Texture* texture)
{
DdsWriterInternal internal(file, texture);
DdsWriterInternal internal(stream, texture);
internal.DumpImage();
}

View File

@ -8,5 +8,5 @@ public:
bool SupportsImageFormat(const ImageFormat * imageFormat) override;
std::string GetFileExtension() override;
void DumpImage(FileAPI::IFile* file, Texture* texture) override;
void DumpImage(std::ostream& stream, Texture* texture) override;
};

View File

@ -1,9 +1,10 @@
#pragma once
#include "Image/Texture.h"
#include "Utils/FileAPI.h"
#include <ostream>
#include <string>
#include "Image/Texture.h"
class IImageWriter
{
public:
@ -11,5 +12,5 @@ public:
virtual bool SupportsImageFormat(const ImageFormat* imageFormat) = 0;
virtual std::string GetFileExtension() = 0;
virtual void DumpImage(FileAPI::IFile* file, Texture* texture) = 0;
virtual void DumpImage(std::ostream& stream, Texture* texture) = 0;
};

View File

@ -1,5 +1,6 @@
#include "IwiWriter27.h"
#include <cassert>
#include <ostream>
using namespace iwi27;
@ -52,7 +53,7 @@ std::string IwiWriter::GetFileExtension()
return ".iwi";
}
void IwiWriter::WriteVersion(FileAPI::IFile* file)
void IwiWriter::WriteVersion(std::ostream& stream)
{
IwiVersion version{};
version.tag[0] = 'I';
@ -60,7 +61,7 @@ void IwiWriter::WriteVersion(FileAPI::IFile* file)
version.tag[2] = 'i';
version.version = 27;
file->Write(&version, sizeof IwiVersion, 1);
stream.write(reinterpret_cast<char*>(&version), sizeof IwiVersion);
}
void IwiWriter::FillHeader2D(IwiHeader* header, Texture2D* texture)
@ -86,12 +87,11 @@ void IwiWriter::FillHeader3D(IwiHeader* header, Texture3D* texture)
header->flags |= IMG_FLAG_VOLMAP;
}
void IwiWriter::DumpImage(FileAPI::IFile* file, Texture* texture)
void IwiWriter::DumpImage(std::ostream& stream, Texture* texture)
{
assert(file != nullptr);
assert(texture != nullptr);
WriteVersion(file);
WriteVersion(stream);
IwiHeader header{};
header.flags = 0;
@ -102,39 +102,44 @@ void IwiWriter::DumpImage(FileAPI::IFile* file, Texture* texture)
if (!texture->HasMipMaps())
header.flags |= IMG_FLAG_NOMIPMAPS;
for (signed char& i : header.maxGlossForMip)
for (auto& i : header.maxGlossForMip)
i = 0;
size_t currentFileSize = sizeof IwiVersion + sizeof IwiHeader;
auto currentFileSize = sizeof IwiVersion + sizeof IwiHeader;
const int textureMipCount = texture->HasMipMaps() ? texture->GetMipMapCount() : 1;
for (int currentMipLevel = textureMipCount - 1; currentMipLevel >= 0; currentMipLevel--)
const auto textureMipCount = texture->HasMipMaps() ? texture->GetMipMapCount() : 1;
for (auto currentMipLevel = textureMipCount - 1; currentMipLevel >= 0; currentMipLevel--)
{
const size_t mipLevelSize = texture->GetSizeOfMipLevel(currentMipLevel) * texture->GetFaceCount();
const auto mipLevelSize = texture->GetSizeOfMipLevel(currentMipLevel) * texture->GetFaceCount();
currentFileSize += mipLevelSize;
if(currentMipLevel < static_cast<int>(_countof(iwi27::IwiHeader::fileSizeForPicmip)))
if (currentMipLevel < static_cast<int>(_countof(iwi27::IwiHeader::fileSizeForPicmip)))
header.fileSizeForPicmip[currentMipLevel] = currentFileSize;
}
if(auto* texture2D = dynamic_cast<Texture2D*>(texture))
if (auto* texture2D = dynamic_cast<Texture2D*>(texture))
{
FillHeader2D(&header, texture2D);
}
else if(auto* textureCube = dynamic_cast<TextureCube*>(texture))
else if (auto* textureCube = dynamic_cast<TextureCube*>(texture))
{
FillHeaderCube(&header, textureCube);
}
else if(auto* texture3D = dynamic_cast<Texture3D*>(texture))
else if (auto* texture3D = dynamic_cast<Texture3D*>(texture))
{
FillHeader3D(&header, texture3D);
}
file->Write(&header, sizeof IwiHeader, 1);
for (int currentMipLevel = textureMipCount - 1; currentMipLevel >= 0; currentMipLevel--)
else
{
const size_t mipLevelSize = texture->GetSizeOfMipLevel(currentMipLevel) * texture->GetFaceCount();
file->Write(texture->GetBufferForMipLevel(currentMipLevel), 1, mipLevelSize);
assert(false);
return;
}
stream.write(reinterpret_cast<char*>(&header), sizeof IwiHeader);
for (auto currentMipLevel = textureMipCount - 1; currentMipLevel >= 0; currentMipLevel--)
{
const auto mipLevelSize = texture->GetSizeOfMipLevel(currentMipLevel) * texture->GetFaceCount();
stream.write(reinterpret_cast<char*>(texture->GetBufferForMipLevel(currentMipLevel)), mipLevelSize);
}
}

View File

@ -9,7 +9,7 @@ namespace iwi27
{
static IwiFormat GetIwiFormatForImageFormat(const ImageFormat* imageFormat);
static void WriteVersion(FileAPI::IFile* file);
static void WriteVersion(std::ostream& stream);
static void FillHeader2D(IwiHeader* header, Texture2D* texture);
static void FillHeaderCube(IwiHeader* header, TextureCube* texture);
static void FillHeader3D(IwiHeader* header, Texture3D* texture);
@ -25,6 +25,6 @@ namespace iwi27
bool SupportsImageFormat(const ImageFormat* imageFormat) override;
std::string GetFileExtension() override;
void DumpImage(FileAPI::IFile* file, Texture* texture) override;
void DumpImage(std::ostream& stream, Texture* texture) override;
};
}

View File

@ -40,7 +40,7 @@ IwiFormat IwiWriter::GetIwiFormatForImageFormat(const ImageFormat* imageFormat)
}
}
void IwiWriter::WriteVersion(FileAPI::IFile* file)
void IwiWriter::WriteVersion(std::ostream& stream)
{
IwiVersion version{};
version.tag[0] = 'I';
@ -48,7 +48,7 @@ void IwiWriter::WriteVersion(FileAPI::IFile* file)
version.tag[2] = 'i';
version.version = 8;
file->Write(&version, sizeof IwiVersion, 1);
stream.write(reinterpret_cast<char*>(&version), sizeof IwiVersion);
}
void IwiWriter::FillHeader2D(IwiHeader* header, Texture2D* texture)
@ -84,12 +84,11 @@ std::string IwiWriter::GetFileExtension()
return ".iwi";
}
void IwiWriter::DumpImage(FileAPI::IFile* file, Texture* texture)
void IwiWriter::DumpImage(std::ostream& stream, Texture* texture)
{
assert(file != nullptr);
assert(texture != nullptr);
WriteVersion(file);
WriteVersion(stream);
IwiHeader header{};
header.flags = 0;
@ -99,12 +98,12 @@ void IwiWriter::DumpImage(FileAPI::IFile* file, Texture* texture)
if (!texture->HasMipMaps())
header.flags |= IMG_FLAG_NOMIPMAPS;
size_t currentFileSize = sizeof IwiVersion + sizeof IwiHeader;
auto currentFileSize = sizeof IwiVersion + sizeof IwiHeader;
const int textureMipCount = texture->HasMipMaps() ? texture->GetMipMapCount() : 1;
for (int currentMipLevel = textureMipCount - 1; currentMipLevel >= 0; currentMipLevel--)
const auto textureMipCount = texture->HasMipMaps() ? texture->GetMipMapCount() : 1;
for (auto currentMipLevel = textureMipCount - 1; currentMipLevel >= 0; currentMipLevel--)
{
const size_t mipLevelSize = texture->GetSizeOfMipLevel(currentMipLevel) * texture->GetFaceCount();
const auto mipLevelSize = texture->GetSizeOfMipLevel(currentMipLevel) * texture->GetFaceCount();
currentFileSize += mipLevelSize;
if (currentMipLevel < static_cast<int>(_countof(iwi27::IwiHeader::fileSizeForPicmip)))
@ -123,12 +122,17 @@ void IwiWriter::DumpImage(FileAPI::IFile* file, Texture* texture)
{
FillHeader3D(&header, texture3D);
}
file->Write(&header, sizeof iwi27::IwiHeader, 1);
for (int currentMipLevel = textureMipCount - 1; currentMipLevel >= 0; currentMipLevel--)
else
{
const size_t mipLevelSize = texture->GetSizeOfMipLevel(currentMipLevel) * texture->GetFaceCount();
file->Write(texture->GetBufferForMipLevel(currentMipLevel), 1, mipLevelSize);
assert(false);
return;
}
stream.write(reinterpret_cast<char*>(&header), sizeof IwiHeader);
for (auto currentMipLevel = textureMipCount - 1; currentMipLevel >= 0; currentMipLevel--)
{
const auto mipLevelSize = texture->GetSizeOfMipLevel(currentMipLevel) * texture->GetFaceCount();
stream.write(reinterpret_cast<char*>(texture->GetBufferForMipLevel(currentMipLevel)), mipLevelSize);
}
}

View File

@ -9,7 +9,7 @@ namespace iwi8
{
static IwiFormat GetIwiFormatForImageFormat(const ImageFormat* imageFormat);
static void WriteVersion(FileAPI::IFile* file);
static void WriteVersion(std::ostream& stream);
static void FillHeader2D(IwiHeader* header, Texture2D* texture);
static void FillHeaderCube(IwiHeader* header, TextureCube* texture);
static void FillHeader3D(IwiHeader* header, Texture3D* texture);
@ -25,6 +25,6 @@ namespace iwi8
bool SupportsImageFormat(const ImageFormat* imageFormat) override;
std::string GetFileExtension() override;
void DumpImage(FileAPI::IFile* file, Texture* texture) override;
void DumpImage(std::ostream& stream, Texture* texture) override;
};
}