From 8218dbb6f86cc3ad0fd869d9f88ab7155a342b5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20M=C3=BCller?= Date: Sun, 24 Mar 2024 14:21:08 +0100 Subject: [PATCH] Add `CImageInfo::DataSize` convenience function To calculate the size of the image data based on its width, height and pixel size. --- src/engine/graphics.h | 5 +++++ src/game/editor/mapitems/map_io.cpp | 2 +- src/game/editor/tileart.cpp | 4 +--- src/tools/map_convert_07.cpp | 2 +- src/tools/map_replace_image.cpp | 2 +- 5 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/engine/graphics.h b/src/engine/graphics.h index 0c54fac62..221c5c138 100644 --- a/src/engine/graphics.h +++ b/src/engine/graphics.h @@ -118,6 +118,11 @@ public: return PixelSize(m_Format); } + size_t DataSize() const + { + return (size_t)m_Width * m_Height * PixelSize(m_Format); + } + static EImageFormat ImageFormatFromInt(int Format) { if(Format < (int)FORMAT_RGB || Format > (int)FORMAT_SINGLE_COMPONENT) diff --git a/src/game/editor/mapitems/map_io.cpp b/src/game/editor/mapitems/map_io.cpp index 362eb264b..b40a302b2 100644 --- a/src/game/editor/mapitems/map_io.cpp +++ b/src/game/editor/mapitems/map_io.cpp @@ -538,7 +538,7 @@ bool CEditorMap::Load(const char *pFileName, int StorageType, const std::functio // copy image data void *pData = DataFile.GetData(pItem->m_ImageData); - const size_t DataSize = (size_t)pImg->m_Width * pImg->m_Height * CImageInfo::PixelSize(Format); + const size_t DataSize = pImg->DataSize(); pImg->m_pData = static_cast(malloc(DataSize)); mem_copy(pImg->m_pData, pData, DataSize); int TextureLoadFlag = m_pEditor->Graphics()->Uses2DTextureArrays() ? IGraphics::TEXLOAD_TO_2D_ARRAY_TEXTURE : IGraphics::TEXLOAD_TO_3D_TEXTURE; diff --git a/src/game/editor/tileart.cpp b/src/game/editor/tileart.cpp index 816e3d93c..96072fadc 100644 --- a/src/game/editor/tileart.cpp +++ b/src/game/editor/tileart.cpp @@ -121,9 +121,7 @@ static CImageInfo ColorGroupToImage(const std::array &aColo Image.m_Width = NumTilesRow * TileSize; Image.m_Height = NumTilesColumn * TileSize; Image.m_Format = CImageInfo::FORMAT_RGBA; - - uint8_t *pData = static_cast(malloc(static_cast(Image.m_Width) * Image.m_Height * 4 * sizeof(uint8_t))); - Image.m_pData = pData; + Image.m_pData = static_cast(malloc(Image.DataSize())); for(int y = 0; y < NumTilesColumn; y++) { diff --git a/src/tools/map_convert_07.cpp b/src/tools/map_convert_07.cpp index dc82731c6..440cf5557 100644 --- a/src/tools/map_convert_07.cpp +++ b/src/tools/map_convert_07.cpp @@ -137,7 +137,7 @@ void *ReplaceImageItem(int Index, CMapItemImage *pImgItem, CMapItemImage *pNewIm pNewImgItem->m_ImageData = g_NextDataItemId++; g_apNewData[g_Index] = ImgInfo.m_pData; - g_aNewDataSize[g_Index] = (size_t)ImgInfo.m_Width * ImgInfo.m_Height * ImgInfo.PixelSize(); + g_aNewDataSize[g_Index] = ImgInfo.DataSize(); g_Index++; return (void *)pNewImgItem; diff --git a/src/tools/map_replace_image.cpp b/src/tools/map_replace_image.cpp index 6e3d35066..ae7db30b9 100644 --- a/src/tools/map_replace_image.cpp +++ b/src/tools/map_replace_image.cpp @@ -105,7 +105,7 @@ void *ReplaceImageItem(int Index, CMapItemImage *pImgItem, const char *pImgName, IStorage::StripPathAndExtension(pImgFile, g_aNewName, sizeof(g_aNewName)); g_NewDataId = pImgItem->m_ImageData; g_pNewData = ImgInfo.m_pData; - g_NewDataSize = (size_t)ImgInfo.m_Width * ImgInfo.m_Height * ImgInfo.PixelSize(); + g_NewDataSize = ImgInfo.DataSize(); return (void *)pNewImgItem; }