Skip to content

Commit

Permalink
Add FlipY option to Image::ConvertImageData
Browse files Browse the repository at this point in the history
  • Loading branch information
thearperson committed Jun 7, 2024
1 parent 5941d3d commit 51a053b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
4 changes: 3 additions & 1 deletion TextureLoader/interface/Image.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ struct Image : public ObjectBase<IObject>
bool KeepAlpha = false;
const void* pData = nullptr;
Uint32 Stride = 0;
bool FlipY = false;
IMAGE_FILE_FORMAT FileFormat = IMAGE_FILE_FORMAT_JPEG;
int JpegQuality = 95;
};
Expand All @@ -155,7 +156,8 @@ struct Image : public ObjectBase<IObject>
Uint32 Stride,
TEXTURE_FORMAT SrcFormat,
TEXTURE_FORMAT DstFormat,
bool KeepAlpha);
bool KeepAlpha,
bool FlipY);

static IMAGE_FILE_FORMAT GetFileFormat(const Uint8* pData, size_t Size, const char* FilePath = nullptr);

Expand Down
10 changes: 6 additions & 4 deletions TextureLoader/src/Image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,8 @@ std::vector<Uint8> Image::ConvertImageData(Uint32 Width,
Uint32 Stride,
TEXTURE_FORMAT SrcFormat,
TEXTURE_FORMAT DstFormat,
bool KeepAlpha)
bool KeepAlpha,
bool FlipY)
{
const auto& SrcFmtAttribs = GetTextureFormatAttribs(SrcFormat);
const auto& DstFmtAttribs = GetTextureFormatAttribs(DstFormat);
Expand All @@ -433,8 +434,9 @@ std::vector<Uint8> Image::ConvertImageData(Uint32 Width,
{
for (Uint32 c = 0; c < NumDstComponents; ++c)
{
size_t jSrc = FlipY ? Height - j - 1 : j;
ConvertedData[j * Width * NumDstComponents + i * NumDstComponents + DstOffsets[c]] =
pData[j * Stride + i * SrcFmtAttribs.NumComponents + SrcOffsets[c]];
pData[jSrc * Stride + i * SrcFmtAttribs.NumComponents + SrcOffsets[c]];
}
}
}
Expand All @@ -448,7 +450,7 @@ void Image::Encode(const EncodeInfo& Info, IDataBlob** ppEncodedData)
auto pEncodedData = DataBlobImpl::Create();
if (Info.FileFormat == IMAGE_FILE_FORMAT_JPEG)
{
auto RGBData = ConvertImageData(Info.Width, Info.Height, reinterpret_cast<const Uint8*>(Info.pData), Info.Stride, Info.TexFormat, TEX_FORMAT_RGBA8_UNORM, false);
auto RGBData = ConvertImageData(Info.Width, Info.Height, reinterpret_cast<const Uint8*>(Info.pData), Info.Stride, Info.TexFormat, TEX_FORMAT_RGBA8_UNORM, false, Info.FlipY);

auto Res = EncodeJpeg(RGBData.data(), Info.Width, Info.Height, Info.JpegQuality, pEncodedData.RawPtr());
if (Res != ENCODE_JPEG_RESULT_OK)
Expand All @@ -461,7 +463,7 @@ void Image::Encode(const EncodeInfo& Info, IDataBlob** ppEncodedData)
std::vector<Uint8> ConvertedData;
if (!((Info.TexFormat == TEX_FORMAT_RGBA8_UNORM || Info.TexFormat == TEX_FORMAT_RGBA8_UNORM_SRGB) && Info.KeepAlpha))
{
ConvertedData = ConvertImageData(Info.Width, Info.Height, reinterpret_cast<const Uint8*>(Info.pData), Info.Stride, Info.TexFormat, TEX_FORMAT_RGBA8_UNORM, Info.KeepAlpha);
ConvertedData = ConvertImageData(Info.Width, Info.Height, reinterpret_cast<const Uint8*>(Info.pData), Info.Stride, Info.TexFormat, TEX_FORMAT_RGBA8_UNORM, Info.KeepAlpha, Info.FlipY);
pData = ConvertedData.data();
Stride = Info.Width * (Info.KeepAlpha ? 4 : 3);
}
Expand Down

0 comments on commit 51a053b

Please sign in to comment.