diff --git a/DesktopEditor/raster/PICT/PICFile.cpp b/DesktopEditor/raster/PICT/PICFile.cpp index beb3b6727f..cf04ccf633 100644 --- a/DesktopEditor/raster/PICT/PICFile.cpp +++ b/DesktopEditor/raster/PICT/PICFile.cpp @@ -195,7 +195,10 @@ bool CPictFile::DecodeData() bool is_pix_data = false; if (!m_oImgData.m_pPixelData) + { m_oImgData.m_pPixelData = (BYTE*) malloc(m_oImgData.m_nWidth * m_oImgData.m_nHeight * 4); + memset(m_oImgData.m_pPixelData, 255, m_oImgData.m_nWidth * m_oImgData.m_nHeight * 4); + } for (int code = 0; feof(m_pFile) == 0; ) { @@ -231,9 +234,8 @@ bool CPictFile::DecodeData() return false; if (((frame.GetLeft() & 0x8000) != 0) || ((frame.GetTop() & 0x8000) != 0)) break; - m_oImgData.m_nHeight = frame.Height; - m_oImgData.m_nWidth = frame.Width; - m_oImgData.m_pPixelData = (BYTE*)realloc(m_oImgData.m_pPixelData, m_oImgData.m_nHeight * m_oImgData.m_nWidth * 4); + + m_oImgData.Realloc(frame.Width, frame.Height); break; } case 0x11: @@ -924,9 +926,7 @@ bool CPictFile::DecodeData() { if (m_oImgData.m_nHeight <= tile_image.m_nHeight && m_oImgData.m_nWidth <= tile_image.m_nWidth) { - m_oImgData.m_nHeight = tile_image.m_nHeight; - m_oImgData.m_nWidth = tile_image.m_nWidth; - m_oImgData.m_pPixelData = (BYTE*)realloc(m_oImgData.m_pPixelData, m_oImgData.m_nHeight * m_oImgData.m_nWidth * 4); + m_oImgData.Realloc(tile_image.m_nWidth, tile_image.m_nHeight, false); memcpy(m_oImgData.m_pPixelData, tile_image.m_pPixelData, m_oImgData.m_nHeight * m_oImgData.m_nWidth * 4); } else @@ -1091,7 +1091,10 @@ void CPictFile::SetImageAlpha(Image* img, const BYTE alpha) bool status = true; if (!img->m_pPixelData) + { img->m_pPixelData = (BYTE*)malloc(4 * img->m_nHeight * img->m_nWidth); + memset(img->m_pPixelData, 255, 4 * img->m_nHeight * img->m_nWidth); + } img->m_eAlphaTrait = BlendPixelTrait; img->m_pChannelMap[AlphaPixelChannel].traits = UpdatePixelTrait; diff --git a/DesktopEditor/raster/PICT/PICFile.h b/DesktopEditor/raster/PICT/PICFile.h index 41ca86d822..5466dd2bd2 100644 --- a/DesktopEditor/raster/PICT/PICFile.h +++ b/DesktopEditor/raster/PICT/PICFile.h @@ -157,12 +157,56 @@ struct Image if (other.m_pColormap) { m_pColormap = new PixelInfo[m_nColors + 1]; - memcpy(m_pColormap, other.m_pColormap, m_nColors + 1); + memcpy(m_pColormap, other.m_pColormap, (m_nColors + 1) * sizeof(PixelInfo)); } - memcpy(m_pChannelMap, other.m_pChannelMap, 65); + memcpy(m_pChannelMap, other.m_pChannelMap, 65 * sizeof(PixelChannelMap)); return *this; } + + void Realloc(const size_t& w, const size_t& h, const bool& isCopy = true) + { + size_t oldSize = 4 * m_nWidth * m_nHeight; + size_t newSize = 4 * w * h; + + m_nWidth = w; + m_nHeight = h; + + if (0 == newSize) + { + if (m_pPixelData) + free(m_pPixelData); + m_pPixelData = nullptr; + return; + } + + if (oldSize == newSize) + return; + + BYTE* oldPixels = m_pPixelData; + if (oldSize > newSize || !m_pPixelData) + { + m_pPixelData = (BYTE*)malloc(newSize); + if (isCopy && oldPixels) + memcpy(m_pPixelData, oldPixels, newSize); + + if (oldPixels) + free(oldPixels); + } + else + { + m_pPixelData = (BYTE*)realloc(oldPixels, newSize); + if (NULL == m_pPixelData) + { + m_pPixelData = (BYTE*)malloc(newSize); + if (isCopy && oldPixels) + memcpy(m_pPixelData, oldPixels, oldSize); + + if (oldPixels) + free(oldPixels); + } + } + } }; class CPictFile @@ -221,8 +265,8 @@ private: std::wstring m_wsFontName{}; - Aggplus::Point m_oPenPoint{}; - Aggplus::Point m_oTextPoint{}; + Aggplus::Point m_oPenPoint{0, 0}; + Aggplus::Point m_oTextPoint{0, 0}; Aggplus::Rect m_oLastRect{}; Aggplus::Rect m_oLastRoundRect{};