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 openShort();
void paletted();
void nonRgb();
void bits16();
void bits24();
void bits32();
void compressed();
void colorBits16();
void colorBits24();
void colorBits32();
void grayscaleBits8();
void grayscaleBits16();
};
TgaImporterTest::TgaImporterTest() {
addTests({&TgaImporterTest::openInexistent,
&TgaImporterTest::openShort,
&TgaImporterTest::paletted,
&TgaImporterTest::nonRgb,
&TgaImporterTest::bits16,
&TgaImporterTest::bits24,
&TgaImporterTest::bits32});
&TgaImporterTest::compressed,
&TgaImporterTest::colorBits16,
&TgaImporterTest::colorBits24,
&TgaImporterTest::colorBits32,
&TgaImporterTest::grayscaleBits8,
&TgaImporterTest::grayscaleBits16});
}
void TgaImporterTest::openInexistent() {
@ -60,7 +68,7 @@ void TgaImporterTest::openInexistent() {
TgaImporter importer;
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() {
@ -71,7 +79,7 @@ void TgaImporterTest::openShort() {
std::ostringstream debug;
Error::setOutput(&debug);
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() {
@ -82,10 +90,10 @@ void TgaImporterTest::paletted() {
std::ostringstream debug;
Error::setOutput(&debug);
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;
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));
@ -93,10 +101,10 @@ void TgaImporterTest::nonRgb() {
std::ostringstream debug;
Error::setOutput(&debug);
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;
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));
@ -104,10 +112,10 @@ void TgaImporterTest::bits16() {
std::ostringstream debug;
Error::setOutput(&debug);
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;
const char data[] = {
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;
}
void TgaImporterTest::bits32() {
void TgaImporterTest::colorBits32() {
TgaImporter importer;
const char data[] = {
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;
}
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)

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());
if(in->good()) return true;
Error() << "TgaImporter: cannot open file" << filename;
Error() << "Trade::TgaImporter::TgaImporter::openFile(): cannot open file" << filename;
close();
return false;
}
@ -86,7 +86,7 @@ ImageData2D* TgaImporter::image2D(UnsignedInt id) {
std::streampos filesize = in->tellg();
in->seekg(0, std::istream::beg);
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;
}
@ -97,35 +97,47 @@ ImageData2D* TgaImporter::image2D(UnsignedInt id) {
header.width = Endianness::littleEndian(header.width);
header.height = Endianness::littleEndian(header.height);
/* Image format */
ImageData2D::Format format;
if(header.colorMapType != 0) {
Error() << "TgaImporter: paletted files are not supported";
return nullptr;
}
if(header.imageType != 2) {
Error() << "TgaImporter: non-RGB files are not supported";
Error() << "Trade::TgaImporter::TgaImporter::image2D(): paletted files are not supported";
return nullptr;
}
ImageData2D::Format format;
switch(header.bpp) {
case 24:
#ifndef MAGNUM_TARGET_GLES
format = ImageData2D::Format::BGR;
#else
format = ImageData2D::Format::RGB;
#endif
break;
case 32:
#ifndef MAGNUM_TARGET_GLES
format = ImageData2D::Format::BGRA;
#else
format = ImageData2D::Format::RGBA;
#endif
break;
default:
Error() << "TgaImporter: unsupported bits-per-pixel:" << header.bpp;
/* Color */
if(header.imageType == 2) {
switch(header.bpp) {
case 24:
#ifndef MAGNUM_TARGET_GLES
format = ImageData2D::Format::BGR;
#else
format = ImageData2D::Format::RGB;
#endif
break;
case 32:
#ifndef MAGNUM_TARGET_GLES
format = ImageData2D::Format::BGRA;
#else
format = ImageData2D::Format::RGBA;
#endif
break;
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;
}
/* 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;

6
src/Plugins/TgaImporter/TgaImporter.h

@ -46,7 +46,11 @@
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 {
public:
/** @brief Default constructor */

Loading…
Cancel
Save