Use wxScopedArray<> instead of wxScopeGuard in wxTGAHandler

Using smart array class is better than using ad hoc scope guards for
freeing memory in any case, but in this particular case it also helps to
avoid g++ 7 -Wnoexcept-type warnings due to type of free() changing to
become "void(*)() noexcept" in C++17.
This commit is contained in:
Vadim Zeitlin 2017-09-17 18:15:38 +02:00
parent dcf95e1cdc
commit 5db73ec9b1

View File

@ -29,7 +29,7 @@
#include "wx/imagtga.h"
#include "wx/log.h"
#include "wx/scopeguard.h"
#include "wx/scopedarray.h"
// ----------------------------------------------------------------------------
// constants
@ -235,15 +235,13 @@ int ReadTGA(wxImage* image, wxInputStream& stream)
const unsigned long imageSize = width * height * pixelSize;
unsigned char *imageData = (unsigned char* )malloc(imageSize);
wxScopedArray<unsigned char> imageData(imageSize);
if (!imageData)
{
return wxTGA_MEMERR;
}
wxON_BLOCK_EXIT1(free, imageData);
unsigned char *dst = image->GetData();
unsigned char* alpha = NULL;
@ -258,19 +256,22 @@ int ReadTGA(wxImage* image, wxInputStream& stream)
if (stream.SeekI(offset, wxFromStart) == wxInvalidOffset)
return wxTGA_INVFORMAT;
unsigned char *palette = NULL;
wxScopedArray<unsigned char> palette;
// Load a palette if we have one.
if (colorType == wxTGA_MAPPED)
{
unsigned char buf[3];
{
wxScopedArray<unsigned char> paletteTmp(paletteLength*3);
palette.swap(paletteTmp);
}
palette = (unsigned char *) malloc(paletteLength * 3);
unsigned char buf[3];
for (unsigned int i = 0; i < paletteLength; i++)
{
stream.Read(buf, 3);
Palette_SetRGB(palette, paletteLength, i, buf[2], buf[1], buf[0]);
Palette_SetRGB(palette.get(), paletteLength, i, buf[2], buf[1], buf[0]);
}
#if wxUSE_PALETTE
@ -281,8 +282,6 @@ int ReadTGA(wxImage* image, wxInputStream& stream)
}
wxON_BLOCK_EXIT1(free, palette);
// Handle the various TGA formats we support.
switch (imageType)
@ -297,14 +296,14 @@ int ReadTGA(wxImage* image, wxInputStream& stream)
// No compression read the data directly to imageData.
stream.Read(imageData, imageSize);
stream.Read(imageData.get(), imageSize);
// If orientation == 0, then the image is stored upside down.
// We need to store it right side up.
if (orientation == 0)
{
FlipTGA(imageData, width, height, pixelSize);
FlipTGA(imageData.get(), width, height, pixelSize);
}
// Handle the different pixel depths.
@ -317,7 +316,7 @@ int ReadTGA(wxImage* image, wxInputStream& stream)
{
for (unsigned long index = 0; index < imageSize; index += pixelSize)
{
Palette_GetRGB(palette, paletteLength,
Palette_GetRGB(palette.get(), paletteLength,
imageData[index], &r, &g, &b);
*(dst++) = r;
@ -333,7 +332,7 @@ int ReadTGA(wxImage* image, wxInputStream& stream)
{
for (unsigned long index = 0; index < imageSize; index += pixelSize)
{
Palette_GetRGB(palette, paletteLength,
Palette_GetRGB(palette.get(), paletteLength,
imageData[index], &r, &g, &b);
*(dst++) = r;
@ -356,14 +355,14 @@ int ReadTGA(wxImage* image, wxInputStream& stream)
{
// No compression read the data directly to imageData.
stream.Read(imageData, imageSize);
stream.Read(imageData.get(), imageSize);
// If orientation == 0, then the image is stored upside down.
// We need to store it right side up.
if (orientation == 0)
{
FlipTGA(imageData, width, height, pixelSize);
FlipTGA(imageData.get(), width, height, pixelSize);
}
// Handle the different pixel depths.
@ -434,14 +433,14 @@ int ReadTGA(wxImage* image, wxInputStream& stream)
{
// No compression read the data directly to imageData.
stream.Read(imageData, imageSize);
stream.Read(imageData.get(), imageSize);
// If orientation == 0, then the image is stored upside down.
// We need to store it right side up.
if (orientation == 0)
{
FlipTGA(imageData, width, height, pixelSize);
FlipTGA(imageData.get(), width, height, pixelSize);
}
// Handle the different pixel depths.
@ -491,7 +490,7 @@ int ReadTGA(wxImage* image, wxInputStream& stream)
// Decode the RLE data.
int rc = DecodeRLE(imageData, imageSize, pixelSize, stream);
int rc = DecodeRLE(imageData.get(), imageSize, pixelSize, stream);
if ( rc != wxTGA_OK )
return rc;
@ -500,7 +499,7 @@ int ReadTGA(wxImage* image, wxInputStream& stream)
if (orientation == 0)
{
FlipTGA(imageData, width, height, pixelSize);
FlipTGA(imageData.get(), width, height, pixelSize);
}
// Handle the different pixel depths.
@ -513,7 +512,7 @@ int ReadTGA(wxImage* image, wxInputStream& stream)
{
for (unsigned long index = 0; index < imageSize; index += pixelSize)
{
Palette_GetRGB(palette, paletteLength,
Palette_GetRGB(palette.get(), paletteLength,
imageData[index], &r, &g, &b);
*(dst++) = r;
@ -529,7 +528,7 @@ int ReadTGA(wxImage* image, wxInputStream& stream)
{
for (unsigned long index = 0; index < imageSize; index += pixelSize)
{
Palette_GetRGB(palette, paletteLength,
Palette_GetRGB(palette.get(), paletteLength,
imageData[index], &r, &g, &b);
*(dst++) = r;
@ -552,7 +551,7 @@ int ReadTGA(wxImage* image, wxInputStream& stream)
{
// Decode the RLE data.
int rc = DecodeRLE(imageData, imageSize, pixelSize, stream);
int rc = DecodeRLE(imageData.get(), imageSize, pixelSize, stream);
if ( rc != wxTGA_OK )
return rc;
@ -561,7 +560,7 @@ int ReadTGA(wxImage* image, wxInputStream& stream)
if (orientation == 0)
{
FlipTGA(imageData, width, height, pixelSize);
FlipTGA(imageData.get(), width, height, pixelSize);
}
// Handle the different pixel depths.
@ -632,7 +631,7 @@ int ReadTGA(wxImage* image, wxInputStream& stream)
{
// Decode the RLE data.
int rc = DecodeRLE(imageData, imageSize, pixelSize, stream);
int rc = DecodeRLE(imageData.get(), imageSize, pixelSize, stream);
if ( rc != wxTGA_OK )
return rc;
@ -641,7 +640,7 @@ int ReadTGA(wxImage* image, wxInputStream& stream)
if (orientation == 0)
{
FlipTGA(imageData, width, height, pixelSize);
FlipTGA(imageData.get(), width, height, pixelSize);
}
// Handle the different pixel depths.
@ -695,14 +694,12 @@ int SaveTGA(const wxImage& image, wxOutputStream *stream)
unsigned bytesPerPixel = 3 + (hasAlpha ? 1 : 0);
wxSize size = image.GetSize();
size_t scanlineSize = size.x * bytesPerPixel;
unsigned char *scanlineData = (unsigned char *) malloc(scanlineSize);
wxScopedArray<unsigned char> scanlineData(scanlineSize);
if (!scanlineData)
{
return wxTGA_MEMERR;
}
wxON_BLOCK_EXIT1(free, scanlineData);
// Compose and write the TGA header
unsigned char hdr[HDR_SIZE];
(void) memset(&hdr, 0, HDR_SIZE);
@ -735,7 +732,7 @@ int SaveTGA(const wxImage& image, wxOutputStream *stream)
unsigned char *alpha = image.GetAlpha();
for (int y = 0; y < size.y; ++y)
{
unsigned char *dst = scanlineData;
unsigned char *dst = scanlineData.get();
for (int x = 0; x < size.x; ++x)
{
dst[0] = src[2];
@ -748,7 +745,7 @@ int SaveTGA(const wxImage& image, wxOutputStream *stream)
src += 3;
dst += bytesPerPixel;
}
if ( !stream->Write(scanlineData, scanlineSize) )
if ( !stream->Write(scanlineData.get(), scanlineSize) )
{
return wxTGA_IOERR;
}