Browse Source

TgaImporter: support for grayscale images, updated error messages.

The messages now print fully qualified function name.
pull/34/head
Vladimír Vondruš 13 years ago
parent
commit
cceba12569
  1. 71
      src/Plugins/TgaImporter/Test/TgaImporterTest.cpp
  2. 64
      src/Plugins/TgaImporter/TgaImporter.cpp
  3. 6
      src/Plugins/TgaImporter/TgaImporter.h

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

@ -38,20 +38,28 @@ class TgaImporterTest: public Corrade::TestSuite::Tester {
void openInexistent(); void openInexistent();
void openShort(); void openShort();
void paletted(); void paletted();
void nonRgb(); void compressed();
void bits16();
void bits24(); void colorBits16();
void bits32(); void colorBits24();
void colorBits32();
void grayscaleBits8();
void grayscaleBits16();
}; };
TgaImporterTest::TgaImporterTest() { TgaImporterTest::TgaImporterTest() {
addTests({&TgaImporterTest::openInexistent, addTests({&TgaImporterTest::openInexistent,
&TgaImporterTest::openShort, &TgaImporterTest::openShort,
&TgaImporterTest::paletted, &TgaImporterTest::paletted,
&TgaImporterTest::nonRgb, &TgaImporterTest::compressed,
&TgaImporterTest::bits16,
&TgaImporterTest::bits24, &TgaImporterTest::colorBits16,
&TgaImporterTest::bits32}); &TgaImporterTest::colorBits24,
&TgaImporterTest::colorBits32,
&TgaImporterTest::grayscaleBits8,
&TgaImporterTest::grayscaleBits16});
} }
void TgaImporterTest::openInexistent() { void TgaImporterTest::openInexistent() {
@ -60,7 +68,7 @@ void TgaImporterTest::openInexistent() {
TgaImporter importer; TgaImporter importer;
CORRADE_VERIFY(!importer.openFile("inexistent.file")); CORRADE_VERIFY(!importer.openFile("inexistent.file"));
CORRADE_COMPARE(debug.str(), "TgaImporter: cannot open file inexistent.file\n"); CORRADE_COMPARE(debug.str(), "Trade::TgaImporter::TgaImporter::openFile(): cannot open file inexistent.file\n");
} }
void TgaImporterTest::openShort() { void TgaImporterTest::openShort() {
@ -71,7 +79,7 @@ void TgaImporterTest::openShort() {
std::ostringstream debug; std::ostringstream debug;
Error::setOutput(&debug); Error::setOutput(&debug);
CORRADE_VERIFY(!importer.image2D(0)); CORRADE_VERIFY(!importer.image2D(0));
CORRADE_COMPARE(debug.str(), "TgaImporter: the file is too short: 17 bytes\n"); CORRADE_COMPARE(debug.str(), "Trade::TgaImporter::TgaImporter::image2D(): the file is too short: 17 bytes\n");
} }
void TgaImporterTest::paletted() { void TgaImporterTest::paletted() {
@ -82,10 +90,10 @@ void TgaImporterTest::paletted() {
std::ostringstream debug; std::ostringstream debug;
Error::setOutput(&debug); Error::setOutput(&debug);
CORRADE_VERIFY(!importer.image2D(0)); CORRADE_VERIFY(!importer.image2D(0));
CORRADE_COMPARE(debug.str(), "TgaImporter: paletted files are not supported\n"); CORRADE_COMPARE(debug.str(), "Trade::TgaImporter::TgaImporter::image2D(): paletted files are not supported\n");
} }
void TgaImporterTest::nonRgb() { void TgaImporterTest::compressed() {
TgaImporter importer; TgaImporter importer;
const char data[] = { 0, 0, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; const char data[] = { 0, 0, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
CORRADE_VERIFY(importer.openData(data)); CORRADE_VERIFY(importer.openData(data));
@ -93,10 +101,10 @@ void TgaImporterTest::nonRgb() {
std::ostringstream debug; std::ostringstream debug;
Error::setOutput(&debug); Error::setOutput(&debug);
CORRADE_VERIFY(!importer.image2D(0)); CORRADE_VERIFY(!importer.image2D(0));
CORRADE_COMPARE(debug.str(), "TgaImporter: non-RGB files are not supported\n"); CORRADE_COMPARE(debug.str(), "Trade::TgaImporter::TgaImporter::image2D(): compressed files are not supported\n");
} }
void TgaImporterTest::bits16() { void TgaImporterTest::colorBits16() {
TgaImporter importer; TgaImporter importer;
const char data[] = { 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 0 }; const char data[] = { 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 0 };
CORRADE_VERIFY(importer.openData(data)); CORRADE_VERIFY(importer.openData(data));
@ -104,10 +112,10 @@ void TgaImporterTest::bits16() {
std::ostringstream debug; std::ostringstream debug;
Error::setOutput(&debug); Error::setOutput(&debug);
CORRADE_VERIFY(!importer.image2D(0)); CORRADE_VERIFY(!importer.image2D(0));
CORRADE_COMPARE(debug.str(), "TgaImporter: unsupported bits-per-pixel: 16\n"); CORRADE_COMPARE(debug.str(), "Trade::TgaImporter::TgaImporter::image2D(): unsupported color bits-per-pixel: 16\n");
} }
void TgaImporterTest::bits24() { void TgaImporterTest::colorBits24() {
TgaImporter importer; TgaImporter importer;
const char data[] = { const char data[] = {
0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 3, 0, 24, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 3, 0, 24, 0,
@ -140,7 +148,7 @@ void TgaImporterTest::bits24() {
delete image; delete image;
} }
void TgaImporterTest::bits32() { void TgaImporterTest::colorBits32() {
TgaImporter importer; TgaImporter importer;
const char data[] = { const char data[] = {
0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 3, 0, 32, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 3, 0, 32, 0,
@ -173,6 +181,35 @@ void TgaImporterTest::bits32() {
delete image; delete image;
} }
void TgaImporterTest::grayscaleBits8() {
TgaImporter importer;
const char data[] = {
0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 3, 0, 8, 0,
1, 2,
3, 4,
5, 6
};
CORRADE_VERIFY(importer.openData(data));
Trade::ImageData2D* image = importer.image2D(0);
CORRADE_VERIFY(image);
CORRADE_COMPARE(image->format(), Trade::ImageData2D::Format::Red);
CORRADE_COMPARE(image->size(), Vector2i(2, 3));
CORRADE_COMPARE(image->type(), Trade::ImageData2D::Type::UnsignedByte);
CORRADE_COMPARE(std::string(static_cast<const char*>(image->data()), 2*3), std::string(data + 18, 2*3));
}
void TgaImporterTest::grayscaleBits16() {
TgaImporter importer;
const char data[] = { 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 0 };
CORRADE_VERIFY(importer.openData(data));
std::ostringstream debug;
Error::setOutput(&debug);
CORRADE_VERIFY(!importer.image2D(0));
CORRADE_COMPARE(debug.str(), "Trade::TgaImporter::TgaImporter::image2D(): unsupported grayscale bits-per-pixel: 16\n");
}
}}}} }}}}
CORRADE_TEST_MAIN(Magnum::Trade::TgaImporter::Test::TgaImporterTest) CORRADE_TEST_MAIN(Magnum::Trade::TgaImporter::Test::TgaImporterTest)

64
src/Plugins/TgaImporter/TgaImporter.cpp

@ -63,7 +63,7 @@ bool TgaImporter::TgaImporter::openFile(const std::string& filename) {
in = new std::ifstream(filename.c_str()); in = new std::ifstream(filename.c_str());
if(in->good()) return true; if(in->good()) return true;
Error() << "TgaImporter: cannot open file" << filename; Error() << "Trade::TgaImporter::TgaImporter::openFile(): cannot open file" << filename;
close(); close();
return false; return false;
} }
@ -86,7 +86,7 @@ ImageData2D* TgaImporter::image2D(UnsignedInt id) {
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(Header))) { if(filesize < std::streampos(sizeof(Header))) {
Error() << "TgaImporter: the file is too short:" << filesize << "bytes"; Error() << "Trade::TgaImporter::TgaImporter::image2D(): the file is too short:" << filesize << "bytes";
return nullptr; return nullptr;
} }
@ -97,35 +97,47 @@ ImageData2D* TgaImporter::image2D(UnsignedInt id) {
header.width = Endianness::littleEndian(header.width); header.width = Endianness::littleEndian(header.width);
header.height = Endianness::littleEndian(header.height); header.height = Endianness::littleEndian(header.height);
/* Image format */
ImageData2D::Format format;
if(header.colorMapType != 0) { if(header.colorMapType != 0) {
Error() << "TgaImporter: paletted files are not supported"; Error() << "Trade::TgaImporter::TgaImporter::image2D(): paletted files are not supported";
return nullptr;
}
if(header.imageType != 2) {
Error() << "TgaImporter: non-RGB files are not supported";
return nullptr; return nullptr;
} }
ImageData2D::Format format; /* Color */
switch(header.bpp) { if(header.imageType == 2) {
case 24: switch(header.bpp) {
#ifndef MAGNUM_TARGET_GLES case 24:
format = ImageData2D::Format::BGR; #ifndef MAGNUM_TARGET_GLES
#else format = ImageData2D::Format::BGR;
format = ImageData2D::Format::RGB; #else
#endif format = ImageData2D::Format::RGB;
break; #endif
case 32: break;
#ifndef MAGNUM_TARGET_GLES case 32:
format = ImageData2D::Format::BGRA; #ifndef MAGNUM_TARGET_GLES
#else format = ImageData2D::Format::BGRA;
format = ImageData2D::Format::RGBA; #else
#endif format = ImageData2D::Format::RGBA;
break; #endif
default: break;
Error() << "TgaImporter: unsupported bits-per-pixel:" << header.bpp; default:
Error() << "Trade::TgaImporter::TgaImporter::image2D(): unsupported color bits-per-pixel:" << header.bpp;
return nullptr;
}
/* Grayscale */
} else if(header.imageType == 3) {
format = ImageData2D::Format::Red;
if(header.bpp != 8) {
Error() << "Trade::TgaImporter::TgaImporter::image2D(): unsupported grayscale bits-per-pixel:" << header.bpp;
return nullptr; return nullptr;
}
/* Compressed files */
} else {
Error() << "Trade::TgaImporter::TgaImporter::image2D(): compressed files are not supported";
return nullptr;
} }
std::size_t size = header.width*header.height*header.bpp/8; std::size_t size = header.width*header.height*header.bpp/8;

6
src/Plugins/TgaImporter/TgaImporter.h

@ -46,7 +46,11 @@
namespace Magnum { namespace Trade { namespace TgaImporter { namespace Magnum { namespace Trade { namespace TgaImporter {
/** @brief TGA importer plugin */ /**
@brief TGA image importer
Supports uncompressed BGR, BGRA or grayscale images with 8 bits per channel.
*/
class MAGNUM_TGAIMPORTER_EXPORT TgaImporter: public AbstractImporter { class MAGNUM_TGAIMPORTER_EXPORT TgaImporter: public AbstractImporter {
public: public:
/** @brief Default constructor */ /** @brief Default constructor */

Loading…
Cancel
Save