Add isRGBA flag to Heif

This commit is contained in:
Prokhorov Kirill
2025-09-16 12:34:55 +03:00
parent 836a488376
commit 054128d981
3 changed files with 17 additions and 17 deletions

View File

@ -458,7 +458,7 @@ bool CBgraFrame::OpenFile(const std::wstring& strFileName, unsigned int nFileTyp
#if CXIMAGE_SUPPORT_HEIF #if CXIMAGE_SUPPORT_HEIF
if (CXIMAGE_FORMAT_HEIF == m_nFileType) if (CXIMAGE_FORMAT_HEIF == m_nFileType)
{ {
return NSHeif::CHeifFile::Open(this, strFileName); return NSHeif::CHeifFile::Open(this, strFileName, m_bIsRGBA);
} }
#endif #endif
@ -548,7 +548,7 @@ bool CBgraFrame::Decode(BYTE* pBuffer, int nSize, unsigned int nFileType)
#if CXIMAGE_SUPPORT_HEIF #if CXIMAGE_SUPPORT_HEIF
if (CXIMAGE_FORMAT_HEIF == m_nFileType) if (CXIMAGE_FORMAT_HEIF == m_nFileType)
{ {
return NSHeif::CHeifFile::Open(this, pBuffer, nSize); return NSHeif::CHeifFile::Open(this, pBuffer, nSize, m_bIsRGBA);
} }
#endif #endif
@ -584,7 +584,7 @@ bool CBgraFrame::SaveFile(const std::wstring& strFileName, unsigned int nFileTyp
#if CXIMAGE_SUPPORT_HEIF #if CXIMAGE_SUPPORT_HEIF
if (CXIMAGE_FORMAT_HEIF == nFileType) if (CXIMAGE_FORMAT_HEIF == nFileType)
{ {
return NSHeif::CHeifFile::Save(m_pData, m_lWidth, m_lHeight, m_lStride, strFileName); return NSHeif::CHeifFile::Save(m_pData, m_lWidth, m_lHeight, m_lStride, strFileName, m_bIsRGBA);
} }
#endif #endif

View File

@ -31,25 +31,25 @@ namespace NSHeif {
return !IsError(heif_context_read_from_memory_without_copy(ctx, buffer, size, nullptr)); return !IsError(heif_context_read_from_memory_without_copy(ctx, buffer, size, nullptr));
} }
bool CHeifFile::Open(CBgraFrame *frame, const std::wstring& fileName) bool CHeifFile::Open(CBgraFrame *frame, const std::wstring& fileName, bool isRGBA)
{ {
heif_context* ctx = heif_context_alloc(); heif_context* ctx = heif_context_alloc();
defer(heif_context_free(ctx);); defer(heif_context_free(ctx););
if (IsError(heif_context_read_from_file(ctx, m_oConverter.fromUnicode(fileName, "UTF-8").c_str(), nullptr))) if (IsError(heif_context_read_from_file(ctx, m_oConverter.fromUnicode(fileName, "UTF-8").c_str(), nullptr)))
return false; return false;
return Decode(ctx, frame); return Decode(ctx, frame, isRGBA);
} }
bool CHeifFile::Open(CBgraFrame *frame, BYTE* buffer, DWORD size) bool CHeifFile::Open(CBgraFrame *frame, BYTE* buffer, DWORD size, bool isRGBA)
{ {
heif_context* ctx = heif_context_alloc(); heif_context* ctx = heif_context_alloc();
defer(heif_context_free(ctx);); defer(heif_context_free(ctx););
if (IsError(heif_context_read_from_memory_without_copy(ctx, buffer, size, nullptr))) if (IsError(heif_context_read_from_memory_without_copy(ctx, buffer, size, nullptr)))
return false; return false;
return Decode(ctx, frame); return Decode(ctx, frame, isRGBA);
} }
bool CHeifFile::Save(const BYTE* source, int width, int height, int sourceStride, const std::wstring& dstPath) bool CHeifFile::Save(const BYTE* source, int width, int height, int sourceStride, const std::wstring& dstPath, bool isRGBA)
{ {
if (!source) if (!source)
return false; return false;
@ -74,9 +74,9 @@ namespace NSHeif {
const BYTE* row = source + (height - i - 1) * (sourceStride < 0 ? -sourceStride : sourceStride); const BYTE* row = source + (height - i - 1) * (sourceStride < 0 ? -sourceStride : sourceStride);
for (size_t j = 0; j < width; ++j) for (size_t j = 0; j < width; ++j)
{ {
data[(i * width + j) * 3 + 0] = row[(width - j - 1) * 4 + 2]; data[(i * width + j) * 3 + (isRGBA ? 0 : 2)] = row[(width - j - 1) * 4 + 0];
data[(i * width + j) * 3 + 1] = row[(width - j - 1) * 4 + 1]; data[(i * width + j) * 3 + 1] = row[(width - j - 1) * 4 + 1];
data[(i * width + j) * 3 + 2] = row[(width - j - 1) * 4 + 0]; data[(i * width + j) * 3 + (isRGBA ? 2 : 0)] = row[(width - j - 1) * 4 + 2];
} }
} }
@ -103,7 +103,7 @@ namespace NSHeif {
return err.code != heif_error_Ok; return err.code != heif_error_Ok;
} }
inline bool CHeifFile::Decode(heif_context* ctx, CBgraFrame* frame) inline bool CHeifFile::Decode(heif_context* ctx, CBgraFrame* frame, bool isRGBA)
{ {
heif_image_handle* handle; heif_image_handle* handle;
defer(heif_image_handle_release(handle);); defer(heif_image_handle_release(handle););
@ -142,9 +142,9 @@ namespace NSHeif {
const BYTE* row_B = source_B + i * stride_B; const BYTE* row_B = source_B + i * stride_B;
for (size_t j = 0; j < width; ++j) for (size_t j = 0; j < width; ++j)
{ {
data[(i * width + j) * 4 + 0] = row_B[j]; data[(i * width + j) * 4 + (isRGBA ? 0 : 2)] = row_R[j];
data[(i * width + j) * 4 + 1] = row_G[j]; data[(i * width + j) * 4 + 1] = row_G[j];
data[(i * width + j) * 4 + 2] = row_R[j]; data[(i * width + j) * 4 + (isRGBA ? 2 : 0)] = row_B[j];
data[(i * width + j) * 4 + 3] = 255; data[(i * width + j) * 4 + 3] = 255;
} }
} }

View File

@ -10,13 +10,13 @@ namespace NSHeif {
static bool isHeif(const std::wstring& fileName); static bool isHeif(const std::wstring& fileName);
static bool isHeif(BYTE* buffer, DWORD size); static bool isHeif(BYTE* buffer, DWORD size);
static bool Open(CBgraFrame* frame, const std::wstring& fileName); static bool Open(CBgraFrame* frame, const std::wstring& fileName, bool isRGBA);
static bool Open(CBgraFrame* frame, BYTE* buffer, DWORD size); static bool Open(CBgraFrame* frame, BYTE* buffer, DWORD size, bool isRGBA);
static bool Save(const BYTE* source, int width, int height, int sourceStride, const std::wstring& dstPath); static bool Save(const BYTE* source, int width, int height, int sourceStride, const std::wstring& dstPath, bool isRGBA);
private: private:
static bool IsError(heif_error err); static bool IsError(heif_error err);
static bool Decode(heif_context* ctx, CBgraFrame* frame); static bool Decode(heif_context* ctx, CBgraFrame* frame, bool isRGBA);
static NSUnicodeConverter::CUnicodeConverter m_oConverter; static NSUnicodeConverter::CUnicodeConverter m_oConverter;
}; };
} }