Add CImageInfo::DataSize convenience function

To calculate the size of the image data based on its width, height and pixel size.
This commit is contained in:
Robert Müller 2024-03-24 14:21:08 +01:00
parent 1147d28007
commit 8218dbb6f8
5 changed files with 9 additions and 6 deletions

View file

@ -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)

View file

@ -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<uint8_t *>(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;

View file

@ -121,9 +121,7 @@ static CImageInfo ColorGroupToImage(const std::array<ColorRGBA, NumTiles> &aColo
Image.m_Width = NumTilesRow * TileSize;
Image.m_Height = NumTilesColumn * TileSize;
Image.m_Format = CImageInfo::FORMAT_RGBA;
uint8_t *pData = static_cast<uint8_t *>(malloc(static_cast<size_t>(Image.m_Width) * Image.m_Height * 4 * sizeof(uint8_t)));
Image.m_pData = pData;
Image.m_pData = static_cast<uint8_t *>(malloc(Image.DataSize()));
for(int y = 0; y < NumTilesColumn; y++)
{

View file

@ -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;

View file

@ -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;
}