Browse Source

TGA importer plugin.

pull/34/head
Vladimír Vondruš 15 years ago
parent
commit
14306f47c5
  1. 2
      src/Plugins/TGAImporter/CMakeLists.txt
  2. 4
      src/Plugins/TGAImporter/TGAImporter.conf
  3. 67
      src/Plugins/TGAImporter/TGAImporter.cpp
  4. 63
      src/Plugins/TGAImporter/TGAImporter.h

2
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})

4
src/Plugins/TGAImporter/TGAImporter.conf

@ -0,0 +1,4 @@
author=Vladimír Vondruš <mosra@centrum.cz>
[metadata]
name=TGA image importer

67
src/Plugins/TGAImporter/TGAImporter.cpp

@ -0,0 +1,67 @@
/*
Copyright © 2010, 2011 Vladimír Vondruš <mosra@centrum.cz>
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<char*>(&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<char*>(buffer), size);
Math::Vector2<GLsizei> dimensions(header.width, header.height);
_image = shared_ptr<Image2D>(new Image2D(dimensions, colorFormat, buffer));
return true;
}
void TGAImporter::close() {
_image = shared_ptr<Image2D>();
}
shared_ptr<Image2D> TGAImporter::image2D(size_t id) {
return _image;
}
}}}

63
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š <mosra@centrum.cz>
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> 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<Image2D> _image;
};
}}}
#endif
Loading…
Cancel
Save