diff --git a/src/Plugins/TGAImporter/CMakeLists.txt b/src/Plugins/TGAImporter/CMakeLists.txt new file mode 100644 index 000000000..79482c1ca --- /dev/null +++ b/src/Plugins/TGAImporter/CMakeLists.txt @@ -0,0 +1,2 @@ +corrade_add_plugin(TGAImporter ${MAGNUM_PLUGINS_IMPORTER_INSTALL_DIR} TGAImporter.conf TGAImporter.cpp) +target_link_libraries(TGAImporter ${MAGNUM_LIBRARY}) diff --git a/src/Plugins/TGAImporter/TGAImporter.conf b/src/Plugins/TGAImporter/TGAImporter.conf new file mode 100644 index 000000000..02be1f4ce --- /dev/null +++ b/src/Plugins/TGAImporter/TGAImporter.conf @@ -0,0 +1,4 @@ +author=Vladimír Vondruš + +[metadata] +name=TGA image importer diff --git a/src/Plugins/TGAImporter/TGAImporter.cpp b/src/Plugins/TGAImporter/TGAImporter.cpp new file mode 100644 index 000000000..f02d7c803 --- /dev/null +++ b/src/Plugins/TGAImporter/TGAImporter.cpp @@ -0,0 +1,67 @@ +/* + Copyright © 2010, 2011 Vladimír Vondruš + + This file is part of Magnum. + + Magnum is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License version 3 + only, as published by the Free Software Foundation. + + Magnum is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License version 3 for more details. +*/ + +#include "TGAImporter.h" + +#include "Math/Vector2.h" +#include "Image.h" + +using namespace std; +using namespace Corrade::Utility; + +PLUGIN_REGISTER(Magnum::Plugins::TGAImporter::TGAImporter, "cz.mosra.magnum.AbstractImporter/0.1") + +namespace Magnum { namespace Plugins { namespace TGAImporter { + +bool TGAImporter::open(std::istream& in) { + if(_image) close(); + if(!in.good()) return false; + + Header header; + in.read(reinterpret_cast(&header), sizeof(Header)); + + ColorFormat colorFormat; + + switch(header.bpp) { + case 24: + colorFormat = ColorFormat::BGR; + break; + case 32: + colorFormat = ColorFormat::BGRA; + break; + default: + Error() << "TGATexture: unsupported bits-per-pixel:" << (int) header.bpp; + return false; + } + + size_t size = header.width*header.height*header.bpp/8; + GLubyte* buffer = new GLubyte[size]; + in.read(reinterpret_cast(buffer), size); + + Math::Vector2 dimensions(header.width, header.height); + + _image = shared_ptr(new Image2D(dimensions, colorFormat, buffer)); + return true; +} + +void TGAImporter::close() { + _image = shared_ptr(); +} + +shared_ptr TGAImporter::image2D(size_t id) { + return _image; +} + +}}} diff --git a/src/Plugins/TGAImporter/TGAImporter.h b/src/Plugins/TGAImporter/TGAImporter.h new file mode 100644 index 000000000..b3507d1d3 --- /dev/null +++ b/src/Plugins/TGAImporter/TGAImporter.h @@ -0,0 +1,63 @@ +#ifndef Magnum_Plugins_TGAImporter_TGAImporter_h +#define Magnum_Plugins_TGAImporter_TGAImporter_h +/* + Copyright © 2010, 2011 Vladimír Vondruš + + This file is part of Magnum. + + Magnum is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License version 3 + only, as published by the Free Software Foundation. + + Magnum is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License version 3 for more details. +*/ + +/** @file + * @brief Class Magnum::TGAImporter + */ + +#include "AbstractImporter.h" + +#include "Magnum.h" + +namespace Magnum { namespace Plugins { namespace TGAImporter { + +class TGAImporter: public AbstractImporter { + public: + TGAImporter(Corrade::PluginManager::AbstractPluginManager* manager = 0, const std::string& plugin = ""): AbstractImporter(manager, plugin) {} + + inline virtual ~TGAImporter() { close(); } + + bool open(std::istream& in); + void close(); + + inline size_t image2DCount() const { return _image ? 1 : 0; } + inline std::shared_ptr image2D(size_t id); + + private: + #pragma pack(1) + struct Header { + GLbyte identsize; /**< @brief Size of ID field that follows header (0) */ + GLbyte colorMapType; /**< @brief 0 = None, 1 = paletted */ + GLbyte imageType; /**< @brief 0 = none, 1 = indexed, 2 = rgb, 3 = grey, +8=rle */ + unsigned short colorMapStart; /**< @brief First color map entry */ + unsigned short colorMapLength; /**< @brief Number of colors */ + unsigned char colorMapBpp; /**< @brief Bits per palette entry */ + unsigned short beginX; /**< @brief Image x origin */ + unsigned short beginY; /**< @brief Image y origin */ + unsigned short width; /**< @brief Image width */ + unsigned short height; /**< @brief Image height */ + GLbyte bpp; /**< @brief Bits per pixel (8 16, 24, 32) */ + GLbyte descriptor; /**< @brief Image descriptor */ + }; + #pragma pack(8) + + std::shared_ptr _image; +}; + +}}} + +#endif