Skip to content

Commit

Permalink
GRAPHICS: Use ScopedArray for the ImageDecoder MipMap data
Browse files Browse the repository at this point in the history
  • Loading branch information
DrMcCoy committed Oct 26, 2016
1 parent 9cc7d45 commit 2c3e7c5
Show file tree
Hide file tree
Showing 16 changed files with 60 additions and 62 deletions.
2 changes: 1 addition & 1 deletion src/graphics/aurora/pltfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ void PLTFile::getColorRows(byte rows[4 * 256 * kLayerMAX], const uint8 colors[kL
const uint8 row = palette->getMipMap(0).height - 1 - colors[i];

// Copy the whole row into the buffer
memcpy(rows, palette->getMipMap(0).data + (row * 4 * 256), 4 * 256);
memcpy(rows, palette->getMipMap(0).data.get() + (row * 4 * 256), 4 * 256);

} catch (...) {
// On error set to pink (while honoring intensity), for high debug visibility
Expand Down
4 changes: 2 additions & 2 deletions src/graphics/aurora/texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -223,10 +223,10 @@ void Texture::setMipMapData(GLenum target, size_t layer, size_t mipMap) {

if (_image->isCompressed()) {
glCompressedTexImage2D(target, mipMap, _image->getFormatRaw(),
m.width, m.height, 0, m.size, m.data);
m.width, m.height, 0, m.size, m.data.get());
} else {
glTexImage2D(target, mipMap, _image->getFormatRaw(),
m.width, m.height, 0, _image->getFormat(), _image->getDataType(), m.data);
m.width, m.height, 0, _image->getFormat(), _image->getDataType(), m.data.get());
}
}

Expand Down
7 changes: 3 additions & 4 deletions src/graphics/images/cbgt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,9 +228,8 @@ void CBGT::createImage(uint32 width, uint32 height) {
_mipMaps.back()->height = height;
_mipMaps.back()->size = width * height * 4;

_mipMaps.back()->data = new byte[_mipMaps.back()->size];
byte *data = _mipMaps.back()->data;
std::memset(data, 0, _mipMaps.back()->size);
_mipMaps.back()->data.reset(new byte[_mipMaps.back()->size]);
std::memset(_mipMaps.back()->data.get(), 0, _mipMaps.back()->size);
}

void CBGT::drawImage(ReadContext &ctx) {
Expand Down Expand Up @@ -262,7 +261,7 @@ void CBGT::drawImage(ReadContext &ctx) {
const uint32 tilesX = cellWidth / tileWidth;
const uint32 tilesY = cellHeight / tileHeight;

byte *data = _mipMaps.back()->data;
byte *data = _mipMaps.back()->data.get();
for (size_t i = 0; i < ctx.cells.size(); i++) {
Common::SeekableReadStream *cell = ctx.cells[i];
if (!cell)
Expand Down
8 changes: 3 additions & 5 deletions src/graphics/images/dds.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,6 @@ void DDS::readStandardHeader(Common::SeekableReadStream &dds, DataType &dataType

setSize(*mipMap);

mipMap->data = 0;

width >>= 1;
height >>= 1;

Expand Down Expand Up @@ -220,11 +218,11 @@ void DDS::setSize(MipMap &mipMap) {

void DDS::readData(Common::SeekableReadStream &dds, DataType dataType) {
for (std::vector<MipMap *>::iterator mipMap = _mipMaps.begin(); mipMap != _mipMaps.end(); ++mipMap) {
(*mipMap)->data = new byte[(*mipMap)->size];
(*mipMap)->data.reset(new byte[(*mipMap)->size]);

if (dataType == kDataType4444) {

byte *data = (*mipMap)->data;
byte *data = (*mipMap)->data.get();
for (uint32 i = 0; i < (uint32)((*mipMap)->width * (*mipMap)->height); i++, data += 4) {
const uint16 pixel = dds.readUint16LE();

Expand All @@ -235,7 +233,7 @@ void DDS::readData(Common::SeekableReadStream &dds, DataType dataType) {
}

} else if (dataType == kDataTypeDirect)
if (dds.read((*mipMap)->data, (*mipMap)->size) != (*mipMap)->size)
if (dds.read((*mipMap)->data.get(), (*mipMap)->size) != (*mipMap)->size)
throw Common::Exception(Common::kReadError);

}
Expand Down
23 changes: 12 additions & 11 deletions src/graphics/images/decoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,27 +38,27 @@

namespace Graphics {

ImageDecoder::MipMap::MipMap(const ImageDecoder *i) : width(0), height(0), size(0), data(0), image(i) {
ImageDecoder::MipMap::MipMap(const ImageDecoder *i) : width(0), height(0), size(0), image(i) {
}

ImageDecoder::MipMap::MipMap(const MipMap &mipMap, const ImageDecoder *i) :
width(mipMap.width), height(mipMap.height), size(mipMap.size), data(0), image(i) {
width(mipMap.width), height(mipMap.height), size(mipMap.size), image(i) {

data = new byte[size];
data.reset(new byte[size]);

std::memcpy(data, mipMap.data, size);
std::memcpy(data.get(), mipMap.data.get(), size);
}

ImageDecoder::MipMap::~MipMap() {
delete[] data;
}

void ImageDecoder::MipMap::swap(MipMap &right) {
SWAP(width , right.width );
SWAP(height, right.height);
SWAP(size , right.size );
SWAP(data , right.data );
SWAP(image , right.image );

data.swap(right.data);
}

void ImageDecoder::MipMap::getPixel(int x, int y, float &r, float &g, float &b, float &a) const {
Expand Down Expand Up @@ -234,16 +234,17 @@ void ImageDecoder::decompress(MipMap &out, const MipMap &in, PixelFormatRaw form
out.width = in.width;
out.height = in.height;
out.size = out.width * out.height * 4;
out.data = new byte[out.size];

Common::ScopedPtr<Common::MemoryReadStream> stream(new Common::MemoryReadStream(in.data, in.size));
out.data.reset(new byte[out.size]);

Common::ScopedPtr<Common::MemoryReadStream> stream(new Common::MemoryReadStream(in.data.get(), in.size));

if (format == kPixelFormatDXT1)
decompressDXT1(out.data, *stream, out.width, out.height, out.width * 4);
decompressDXT1(out.data.get(), *stream, out.width, out.height, out.width * 4);
else if (format == kPixelFormatDXT3)
decompressDXT3(out.data, *stream, out.width, out.height, out.width * 4);
decompressDXT3(out.data.get(), *stream, out.width, out.height, out.width * 4);
else if (format == kPixelFormatDXT5)
decompressDXT5(out.data, *stream, out.width, out.height, out.width * 4);
decompressDXT5(out.data.get(), *stream, out.width, out.height, out.width * 4);
}

void ImageDecoder::decompress() {
Expand Down
4 changes: 3 additions & 1 deletion src/graphics/images/decoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include <boost/noncopyable.hpp>

#include "src/common/types.h"
#include "src/common/scopedptr.h"

#include "src/graphics/types.h"

Expand All @@ -50,7 +51,8 @@ class ImageDecoder : boost::noncopyable {
int width; ///< The mip map's width.
int height; ///< The mip map's height.
uint32 size; ///< The mip map's size in bytes.
byte *data; ///< The mip map's data.

Common::ScopedArray<byte> data; ///< The mip map's data.

const ImageDecoder *image; ///< The image the mip map belongs to.

Expand Down
2 changes: 1 addition & 1 deletion src/graphics/images/dumptga.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ static Common::WriteStream *openTGA(const Common::UString &fileName, int width,
}

static void writeMipMap(Common::WriteStream &stream, const ImageDecoder::MipMap &mipMap, PixelFormat format) {
const byte *data = mipMap.data;
const byte *data = mipMap.data.get();

uint32 count = mipMap.width * mipMap.height;
while (count-- > 0)
Expand Down
4 changes: 2 additions & 2 deletions src/graphics/images/nbfs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,11 @@ void NBFS::readImage(Common::SeekableReadStream &nbfs, const byte *palette,
_mipMaps.back()->height = height;
_mipMaps.back()->size = width * height * 4;

_mipMaps.back()->data = new byte[_mipMaps.back()->size];
_mipMaps.back()->data.reset(new byte[_mipMaps.back()->size]);

bool is0Transp = (palette[0] == 0xF8) && (palette[1] == 0x00) && (palette[2] == 0xF8);

byte *data = _mipMaps.back()->data;
byte *data = _mipMaps.back()->data.get();
for (uint32 i = 0; i < (width * height); i++, data += 4) {
uint8 pixel = nbfs.readByte();

Expand Down
4 changes: 2 additions & 2 deletions src/graphics/images/ncgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -261,8 +261,8 @@ void NCGR::draw(ReadContext &ctx) {
_mipMaps.back()->height = imageHeight;
_mipMaps.back()->size = imageWidth * imageHeight * 4;

_mipMaps.back()->data = new byte[_mipMaps.back()->size];
byte *data = _mipMaps.back()->data;
_mipMaps.back()->data.reset(new byte[_mipMaps.back()->size]);
byte *data = _mipMaps.back()->data.get();

const bool is0Transp = (ctx.pal[0] == 0xF8) && (ctx.pal[1] == 0x00) && (ctx.pal[2] == 0xF8);

Expand Down
6 changes: 3 additions & 3 deletions src/graphics/images/sbm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ void SBM::readData(Common::SeekableReadStream &sbm) {
_mipMaps[0]->height = NEXTPOWER2((uint32) rowCount * 32);
_mipMaps[0]->size = _mipMaps[0]->width * _mipMaps[0]->height * 4;

_mipMaps[0]->data = new byte[_mipMaps[0]->size];
_mipMaps[0]->data.reset(new byte[_mipMaps[0]->size]);

// SBM data consists of character sized 32 * 32 pixels, with 2 bits per pixel.
// 4 characters each are on top of each other, occupying the same x/y
Expand All @@ -80,7 +80,7 @@ void SBM::readData(Common::SeekableReadStream &sbm) {
int masks [4] = { 0x03, 0x0C, 0x30, 0xC0 };
int shifts[4] = { 0, 2, 4, 6 };

byte *data = _mipMaps[0]->data;
byte *data = _mipMaps[0]->data.get();
byte buffer[1024];
for (size_t c = 0; c < rowCount; c++) {

Expand All @@ -103,7 +103,7 @@ void SBM::readData(Common::SeekableReadStream &sbm) {
}
}

byte *dataEnd = _mipMaps[0]->data + _mipMaps[0]->size;
byte *dataEnd = _mipMaps[0]->data.get() + _mipMaps[0]->size;
std::memset(data, 0, dataEnd - data);
}

Expand Down
10 changes: 5 additions & 5 deletions src/graphics/images/surface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ Surface::Surface(int width, int height) {
_mipMaps[0]->height = height;
_mipMaps[0]->size = _mipMaps[0]->width * _mipMaps[0]->height * 4;

_mipMaps[0]->data = new byte[_mipMaps[0]->size];
_mipMaps[0]->data.reset(new byte[_mipMaps[0]->size]);
}

Surface::~Surface() {
Expand All @@ -59,20 +59,20 @@ int Surface::getHeight() const {
}

byte *Surface::getData() {
return _mipMaps[0]->data;
return _mipMaps[0]->data.get();
}

const byte *Surface::getData() const {
return _mipMaps[0]->data;
return _mipMaps[0]->data.get();
}

void Surface::fill(byte r, byte g, byte b, byte a) {
if ((r == g) && (r == b) && (r == a)) {
std::memset(_mipMaps[0]->data, r, _mipMaps[0]->size);
std::memset(_mipMaps[0]->data.get(), r, _mipMaps[0]->size);
return;
}

byte *data = _mipMaps[0]->data;
byte *data = _mipMaps[0]->data.get();
uint32 size = _mipMaps[0]->size / 4;
while (size-- > 0) {
*data++ = b;
Expand Down
14 changes: 7 additions & 7 deletions src/graphics/images/tga.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ void TGA::readData(Common::SeekableReadStream &tga, ImageType imageType, byte pi
else if (_format == kPixelFormatBGRA)
_mipMaps[i]->size *= 4;

_mipMaps[i]->data = new byte[_mipMaps[i]->size];
_mipMaps[i]->data.reset(new byte[_mipMaps[i]->size]);

if (imageType == kImageTypeTrueColor) {
if (pixelDepth == 16) {
Expand All @@ -196,7 +196,7 @@ void TGA::readData(Common::SeekableReadStream &tga, ImageType imageType, byte pi
// Hopefully Sonic is the only game that needs 16bpp TGAs.

uint16 count = _mipMaps[i]->width * _mipMaps[i]->height;
byte *dst = _mipMaps[i]->data;
byte *dst = _mipMaps[i]->data.get();

while (count--) {
uint16 pixel = tga.readUint16LE();
Expand All @@ -208,16 +208,16 @@ void TGA::readData(Common::SeekableReadStream &tga, ImageType imageType, byte pi
}
} else {
// Read it in raw
tga.read(_mipMaps[i]->data, _mipMaps[i]->size);
tga.read(_mipMaps[i]->data.get(), _mipMaps[i]->size);
}
} else {
readRLE(tga, pixelDepth, i);
}
} else if (imageType == kImageTypeBW) {
_mipMaps[i]->size = _mipMaps[i]->width * _mipMaps[i]->height * 4;
_mipMaps[i]->data = new byte[_mipMaps[i]->size];
_mipMaps[i]->data.reset(new byte[_mipMaps[i]->size]);

byte *data = _mipMaps[i]->data;
byte *data = _mipMaps[i]->data.get();
uint32 count = _mipMaps[i]->width * _mipMaps[i]->height;

while (count-- > 0) {
Expand All @@ -233,15 +233,15 @@ void TGA::readData(Common::SeekableReadStream &tga, ImageType imageType, byte pi

// Bit 5 of imageDesc set means the origin in upper-left corner
if (imageDesc & 0x20)
flipVertically(_mipMaps[i]->data, _mipMaps[i]->width, _mipMaps[i]->height, pixelDepth / 8);
flipVertically(_mipMaps[i]->data.get(), _mipMaps[i]->width, _mipMaps[i]->height, pixelDepth / 8);
}
}

void TGA::readRLE(Common::SeekableReadStream &tga, byte pixelDepth, size_t layer) {
if (pixelDepth != 24 && pixelDepth != 32)
throw Common::Exception("Unhandled RLE depth %d", pixelDepth);

byte *data = _mipMaps[layer]->data;
byte *data = _mipMaps[layer]->data.get();
uint32 count = _mipMaps[layer]->width * _mipMaps[layer]->height;

while (count > 0) {
Expand Down
18 changes: 8 additions & 10 deletions src/graphics/images/tpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,6 @@ void TPC::readHeader(Common::SeekableReadStream &tpc, byte &encoding) {

mipMap->size = MAX<uint32>(layerSize, minDataSize);

mipMap->data = 0;

const size_t mipMapDataSize = getDataSize(_formatRaw, mipMap->width, mipMap->height);

// Wouldn't fit
Expand Down Expand Up @@ -273,29 +271,29 @@ void TPC::readData(Common::SeekableReadStream &tpc, byte encoding) {
const bool widthPOT = ((*mipMap)->width & ((*mipMap)->width - 1)) == 0;
const bool swizzled = (encoding == kEncodingSwizzledBGRA) && widthPOT;

(*mipMap)->data = new byte[(*mipMap)->size];
(*mipMap)->data.reset(new byte[(*mipMap)->size]);

if (swizzled) {
std::vector<byte> tmp((*mipMap)->size);

if (tpc.read(&tmp[0], (*mipMap)->size) != (*mipMap)->size)
throw Common::Exception(Common::kReadError);

deSwizzle((*mipMap)->data, &tmp[0], (*mipMap)->width, (*mipMap)->height);
deSwizzle((*mipMap)->data.get(), &tmp[0], (*mipMap)->width, (*mipMap)->height);

} else {
if (tpc.read((*mipMap)->data, (*mipMap)->size) != (*mipMap)->size)
if (tpc.read((*mipMap)->data.get(), (*mipMap)->size) != (*mipMap)->size)
throw Common::Exception(Common::kReadError);

// Unpacking 8bpp grayscale data into RGB
if (encoding == kEncodingGray) {
Common::ScopedArray<byte> dataGray((*mipMap)->data);
Common::ScopedArray<byte> dataGray((*mipMap)->data.release());

(*mipMap)->size = (*mipMap)->width * (*mipMap)->height * 3;
(*mipMap)->data = new byte[(*mipMap)->size];
(*mipMap)->data.reset(new byte[(*mipMap)->size]);

for (int i = 0; i < ((*mipMap)->width * (*mipMap)->height); i++)
std::memset((*mipMap)->data + i * 3, dataGray[i], 3);
std::memset((*mipMap)->data.get() + i * 3, dataGray[i], 3);
}
}

Expand Down Expand Up @@ -356,7 +354,7 @@ void TPC::fixupCubeMap() {
MipMap &mipMap0 = *_mipMaps[index0];
MipMap &mipMap1 = *_mipMaps[index1];

SWAP(mipMap0.data, mipMap1.data);
mipMap0.data.swap(mipMap1.data);
}

const int bpp = (_formatRaw == kPixelFormatRGB8) ? 3 : ((_formatRaw == kPixelFormatRGBA8) ? 4 : 0);
Expand All @@ -373,7 +371,7 @@ void TPC::fixupCubeMap() {

static const int rotation[6] = { 1, 3, 0, 2, 2, 0 };

rotate90(mipMap.data, mipMap.width, mipMap.height, bpp, rotation[i]);
rotate90(mipMap.data.get(), mipMap.width, mipMap.height, bpp, rotation[i]);
}
}

Expand Down
7 changes: 3 additions & 4 deletions src/graphics/images/txb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,6 @@ void TXB::readHeader(Common::SeekableReadStream &txb, bool &needDeSwizzle, uint3
break;

mipMap->size = MAX<uint32>(mipMapSize, minDataSize);
mipMap->data = 0;

const size_t mipMapDataSize = getDataSize(_formatRaw, mipMap->width, mipMap->height);

Expand Down Expand Up @@ -198,18 +197,18 @@ void TXB::readData(Common::SeekableReadStream &txb, bool needDeSwizzle) {
const bool widthPOT = ((*mipMap)->width & ((*mipMap)->width - 1)) == 0;
const bool swizzled = needDeSwizzle && widthPOT;

(*mipMap)->data = new byte[(*mipMap)->size];
(*mipMap)->data.reset(new byte[(*mipMap)->size]);

if (swizzled) {
std::vector<byte> tmp((*mipMap)->size);

if (txb.read(&tmp[0], (*mipMap)->size) != (*mipMap)->size)
throw Common::Exception(Common::kReadError);

deSwizzle((*mipMap)->data, &tmp[0], (*mipMap)->width, (*mipMap)->height);
deSwizzle((*mipMap)->data.get(), &tmp[0], (*mipMap)->width, (*mipMap)->height);

} else {
if (txb.read((*mipMap)->data, (*mipMap)->size) != (*mipMap)->size)
if (txb.read((*mipMap)->data.get(), (*mipMap)->size) != (*mipMap)->size)
throw Common::Exception(Common::kReadError);
}

Expand Down

0 comments on commit 2c3e7c5

Please sign in to comment.