Browse Source

TgaImporter, TgaImageConverter: adapted to Magnum changes.

It looks like I forgot to `delete` three times in the tests. It just
proves that the previous API was flawed (or unusable for people spoilt
with RAII like me).
pull/34/head
Vladimír Vondruš 13 years ago
parent
commit
b84dc709f8
  1. 2
      src/Plugins/TgaImageConverter/Test/TgaImageConverterTest.cpp
  2. 12
      src/Plugins/TgaImporter/Test/TgaImporterTest.cpp
  3. 14
      src/Plugins/TgaImporter/TgaImporter.cpp
  4. 2
      src/Plugins/TgaImporter/TgaImporter.h

2
src/Plugins/TgaImageConverter/Test/TgaImageConverterTest.cpp

@ -94,7 +94,7 @@ void TgaImageConverterTest::data() {
TgaImporter importer; TgaImporter importer;
CORRADE_VERIFY(importer.openData(data)); CORRADE_VERIFY(importer.openData(data));
Trade::ImageData2D* converted = importer.image2D(0); std::optional<Trade::ImageData2D> converted = importer.image2D(0);
CORRADE_VERIFY(converted); CORRADE_VERIFY(converted);
CORRADE_COMPARE(converted->size(), Vector2i(2, 3)); CORRADE_COMPARE(converted->size(), Vector2i(2, 3));

12
src/Plugins/TgaImporter/Test/TgaImporterTest.cpp

@ -142,7 +142,7 @@ void TgaImporterTest::colorBits24() {
#endif #endif
CORRADE_VERIFY(importer.openData(data)); CORRADE_VERIFY(importer.openData(data));
Trade::ImageData2D* image = importer.image2D(0); std::optional<Trade::ImageData2D> image = importer.image2D(0);
CORRADE_VERIFY(image); CORRADE_VERIFY(image);
#ifndef MAGNUM_TARGET_GLES #ifndef MAGNUM_TARGET_GLES
CORRADE_COMPARE(image->format(), ImageFormat::BGR); CORRADE_COMPARE(image->format(), ImageFormat::BGR);
@ -152,8 +152,6 @@ void TgaImporterTest::colorBits24() {
CORRADE_COMPARE(image->size(), Vector2i(2, 3)); CORRADE_COMPARE(image->size(), Vector2i(2, 3));
CORRADE_COMPARE(image->type(), ImageType::UnsignedByte); CORRADE_COMPARE(image->type(), ImageType::UnsignedByte);
CORRADE_COMPARE(std::string(reinterpret_cast<const char*>(image->data()), 2*3*3), std::string(pixels, 2*3*3)); CORRADE_COMPARE(std::string(reinterpret_cast<const char*>(image->data()), 2*3*3), std::string(pixels, 2*3*3));
delete image;
} }
void TgaImporterTest::colorBits32() { void TgaImporterTest::colorBits32() {
@ -175,7 +173,7 @@ void TgaImporterTest::colorBits32() {
#endif #endif
CORRADE_VERIFY(importer.openData(data)); CORRADE_VERIFY(importer.openData(data));
Trade::ImageData2D* image = importer.image2D(0); std::optional<Trade::ImageData2D> image = importer.image2D(0);
CORRADE_VERIFY(image); CORRADE_VERIFY(image);
#ifndef MAGNUM_TARGET_GLES #ifndef MAGNUM_TARGET_GLES
CORRADE_COMPARE(image->format(), ImageFormat::BGRA); CORRADE_COMPARE(image->format(), ImageFormat::BGRA);
@ -185,8 +183,6 @@ void TgaImporterTest::colorBits32() {
CORRADE_COMPARE(image->size(), Vector2i(2, 3)); CORRADE_COMPARE(image->size(), Vector2i(2, 3));
CORRADE_COMPARE(image->type(), ImageType::UnsignedByte); CORRADE_COMPARE(image->type(), ImageType::UnsignedByte);
CORRADE_COMPARE(std::string(reinterpret_cast<const char*>(image->data()), 2*3*3), std::string(pixels, 2*3*3)); CORRADE_COMPARE(std::string(reinterpret_cast<const char*>(image->data()), 2*3*3), std::string(pixels, 2*3*3));
delete image;
} }
void TgaImporterTest::grayscaleBits8() { void TgaImporterTest::grayscaleBits8() {
@ -199,7 +195,7 @@ void TgaImporterTest::grayscaleBits8() {
}; };
CORRADE_VERIFY(importer.openData(data)); CORRADE_VERIFY(importer.openData(data));
Trade::ImageData2D* image = importer.image2D(0); std::optional<Trade::ImageData2D> image = importer.image2D(0);
CORRADE_VERIFY(image); CORRADE_VERIFY(image);
CORRADE_COMPARE(image->format(), ImageFormat::Red); CORRADE_COMPARE(image->format(), ImageFormat::Red);
CORRADE_COMPARE(image->size(), Vector2i(2, 3)); CORRADE_COMPARE(image->size(), Vector2i(2, 3));
@ -229,7 +225,7 @@ void TgaImporterTest::file() {
}; };
CORRADE_VERIFY(importer.openFile(Utility::Directory::join(TGAIMPORTER_TEST_DIR, "file.tga"))); CORRADE_VERIFY(importer.openFile(Utility::Directory::join(TGAIMPORTER_TEST_DIR, "file.tga")));
Trade::ImageData2D* image = importer.image2D(0); std::optional<Trade::ImageData2D> image = importer.image2D(0);
CORRADE_VERIFY(image); CORRADE_VERIFY(image);
CORRADE_COMPARE(image->format(), ImageFormat::Red); CORRADE_COMPARE(image->format(), ImageFormat::Red);
CORRADE_COMPARE(image->size(), Vector2i(2, 3)); CORRADE_COMPARE(image->size(), Vector2i(2, 3));

14
src/Plugins/TgaImporter/TgaImporter.cpp

@ -71,14 +71,14 @@ void TgaImporter::doClose() {
UnsignedInt TgaImporter::doImage2DCount() const { return 1; } UnsignedInt TgaImporter::doImage2DCount() const { return 1; }
ImageData2D* TgaImporter::doImage2D(UnsignedInt) { std::optional<ImageData2D> TgaImporter::doImage2D(UnsignedInt) {
/* Check if the file is long enough */ /* Check if the file is long enough */
in->seekg(0, std::istream::end); in->seekg(0, std::istream::end);
std::streampos filesize = in->tellg(); std::streampos filesize = in->tellg();
in->seekg(0, std::istream::beg); in->seekg(0, std::istream::beg);
if(filesize < std::streampos(sizeof(TgaHeader))) { if(filesize < std::streampos(sizeof(TgaHeader))) {
Error() << "Trade::TgaImporter::image2D(): the file is too short:" << filesize << "bytes"; Error() << "Trade::TgaImporter::image2D(): the file is too short:" << filesize << "bytes";
return nullptr; return std::nullopt;
} }
TgaHeader header; TgaHeader header;
@ -92,7 +92,7 @@ ImageData2D* TgaImporter::doImage2D(UnsignedInt) {
ImageFormat format; ImageFormat format;
if(header.colorMapType != 0) { if(header.colorMapType != 0) {
Error() << "Trade::TgaImporter::image2D(): paletted files are not supported"; Error() << "Trade::TgaImporter::image2D(): paletted files are not supported";
return nullptr; return std::nullopt;
} }
/* Color */ /* Color */
@ -114,7 +114,7 @@ ImageData2D* TgaImporter::doImage2D(UnsignedInt) {
break; break;
default: default:
Error() << "Trade::TgaImporter::image2D(): unsupported color bits-per-pixel:" << header.bpp; Error() << "Trade::TgaImporter::image2D(): unsupported color bits-per-pixel:" << header.bpp;
return nullptr; return std::nullopt;
} }
/* Grayscale */ /* Grayscale */
@ -127,13 +127,13 @@ ImageData2D* TgaImporter::doImage2D(UnsignedInt) {
#endif #endif
if(header.bpp != 8) { if(header.bpp != 8) {
Error() << "Trade::TgaImporter::image2D(): unsupported grayscale bits-per-pixel:" << header.bpp; Error() << "Trade::TgaImporter::image2D(): unsupported grayscale bits-per-pixel:" << header.bpp;
return nullptr; return std::nullopt;
} }
/* Compressed files */ /* Compressed files */
} else { } else {
Error() << "Trade::TgaImporter::image2D(): unsupported (compressed?) image type:" << header.imageType; Error() << "Trade::TgaImporter::image2D(): unsupported (compressed?) image type:" << header.imageType;
return nullptr; return std::nullopt;
} }
const std::size_t dataSize = header.width*header.height*header.bpp/8; const std::size_t dataSize = header.width*header.height*header.bpp/8;
@ -154,7 +154,7 @@ ImageData2D* TgaImporter::doImage2D(UnsignedInt) {
} }
#endif #endif
return new ImageData2D(format, ImageType::UnsignedByte, size, data); return ImageData2D(format, ImageType::UnsignedByte, size, data);
} }
}} }}

2
src/Plugins/TgaImporter/TgaImporter.h

@ -73,7 +73,7 @@ class MAGNUM_TRADE_TGAIMPORTER_EXPORT TgaImporter: public AbstractImporter {
void MAGNUM_TRADE_TGAIMPORTER_LOCAL doOpenFile(const std::string& filename) override; void MAGNUM_TRADE_TGAIMPORTER_LOCAL doOpenFile(const std::string& filename) override;
void MAGNUM_TRADE_TGAIMPORTER_LOCAL doClose() override; void MAGNUM_TRADE_TGAIMPORTER_LOCAL doClose() override;
UnsignedInt MAGNUM_TRADE_TGAIMPORTER_LOCAL doImage2DCount() const override; UnsignedInt MAGNUM_TRADE_TGAIMPORTER_LOCAL doImage2DCount() const override;
ImageData2D MAGNUM_TRADE_TGAIMPORTER_LOCAL * doImage2D(UnsignedInt id) override; std::optional<ImageData2D> MAGNUM_TRADE_TGAIMPORTER_LOCAL doImage2D(UnsignedInt id) override;
std::istream* in; std::istream* in;
}; };

Loading…
Cancel
Save