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