Skip to content

Commit

Permalink
Texture loader: added option to premultiply color channels by alpha
Browse files Browse the repository at this point in the history
  • Loading branch information
TheMostDiligent committed Sep 1, 2023
1 parent cb6a888 commit 7bcd2b1
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 0 deletions.
8 changes: 8 additions & 0 deletions TextureLoader/interface/Image.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,14 @@ struct ImageLoadInfo
{
/// Image file format
IMAGE_FILE_FORMAT Format DEFAULT_INITIALIZER(IMAGE_FILE_FORMAT_UNKNOWN);

/// Whether to premultiply RGB channels by alpha
bool PermultiplyAlpha DEFAULT_INITIALIZER(false);

/// Whether the image is stored in sRGB format
///
/// \note This flag is only used if PermultiplyAlpha is true.
bool IsSRGB DEFAULT_INITIALIZER(false);
};
typedef struct ImageLoadInfo ImageLoadInfo;

Expand Down
3 changes: 3 additions & 0 deletions TextureLoader/interface/TextureLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ struct TextureLoadInfo
/// Flag indicating that the image should be flipped vertically
Bool FlipVertically DEFAULT_VALUE(False);

/// Flag indicating that RGB channels should be premultiplied by alpha
Bool PermultiplyAlpha DEFAULT_VALUE(False);

/// Texture format
TEXTURE_FORMAT Format DEFAULT_VALUE(TEX_FORMAT_UNKNOWN);

Expand Down
14 changes: 14 additions & 0 deletions TextureLoader/src/Image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
#include "GraphicsAccessories.hpp"
#include "BasicFileStream.hpp"
#include "StringTools.hpp"
#include "TextureUtilities.h"

namespace Diligent
{
Expand Down Expand Up @@ -259,6 +260,19 @@ Image::Image(IReferenceCounters* pRefCounters,
{
LOG_ERROR_MESSAGE("Unknown image format.");
}

if (LoadInfo.PermultiplyAlpha && m_Desc.NumComponents == 4)
{
PremultiplyAlphaAttribs PremultAttribs;
PremultAttribs.Width = m_Desc.Width;
PremultAttribs.Height = m_Desc.Height;
PremultAttribs.ComponentType = m_Desc.ComponentType;
PremultAttribs.ComponentCount = m_Desc.NumComponents;
PremultAttribs.Stride = m_Desc.RowStride;
PremultAttribs.pPixels = m_pData->GetDataPtr();
PremultAttribs.IsSRGB = LoadInfo.IsSRGB;
PremultiplyAlpha(PremultAttribs);
}
}

void Image::CreateFromDataBlob(IDataBlob* pFileData,
Expand Down
2 changes: 2 additions & 0 deletions TextureLoader/src/TextureLoaderImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ TextureLoaderImpl::TextureLoaderImpl(IReferenceCounters* pRefCounters,
{
m_pDataBlob = DataBlobImpl::Create(DataSize, pData);
}
ImgLoadInfo.IsSRGB = TexLoadInfo.IsSRGB;
ImgLoadInfo.PermultiplyAlpha = TexLoadInfo.PermultiplyAlpha;
Image::CreateFromDataBlob(m_pDataBlob, ImgLoadInfo, &m_pImage);
LoadFromImage(TexLoadInfo);
m_pDataBlob.Release();
Expand Down

0 comments on commit 7bcd2b1

Please sign in to comment.