mirror of https://github.com/mosra/magnum.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
92 lines
2.6 KiB
92 lines
2.6 KiB
|
15 years ago
|
/*
|
||
|
15 years ago
|
Copyright © 2010, 2011, 2012 Vladimír Vondruš <mosra@centrum.cz>
|
||
|
15 years ago
|
|
||
|
|
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.
|
||
|
|
*/
|
||
|
|
|
||
|
14 years ago
|
#include "TgaImporter.h"
|
||
|
15 years ago
|
|
||
|
15 years ago
|
#include "Utility/Endianness.h"
|
||
|
15 years ago
|
#include "Math/Vector2.h"
|
||
|
14 years ago
|
#include "Trade/ImageData.h"
|
||
|
15 years ago
|
|
||
|
|
using namespace std;
|
||
|
|
using namespace Corrade::Utility;
|
||
|
|
|
||
|
14 years ago
|
PLUGIN_REGISTER(Magnum::Trade::TgaImporter::TgaImporter, "cz.mosra.magnum.Trade.AbstractImporter/0.1")
|
||
|
15 years ago
|
|
||
|
14 years ago
|
namespace Magnum { namespace Trade { namespace TgaImporter {
|
||
|
15 years ago
|
|
||
|
14 years ago
|
static_assert(sizeof(TgaImporter::Header) == 18, "TgaImporter: header size is not 18 bytes");
|
||
|
15 years ago
|
|
||
|
14 years ago
|
bool TgaImporter::TgaImporter::open(const string& filename) {
|
||
|
15 years ago
|
ifstream in(filename.c_str());
|
||
|
|
bool status = open(in);
|
||
|
|
in.close();
|
||
|
|
return status;
|
||
|
|
}
|
||
|
|
|
||
|
14 years ago
|
bool TgaImporter::open(std::istream& in) {
|
||
|
15 years ago
|
if(_image) close();
|
||
|
|
if(!in.good()) return false;
|
||
|
|
|
||
|
|
Header header;
|
||
|
|
in.read(reinterpret_cast<char*>(&header), sizeof(Header));
|
||
|
|
|
||
|
15 years ago
|
/* Convert to machine endian */
|
||
|
|
header.width = Endianness::littleEndian<unsigned short>(header.width);
|
||
|
|
header.height = Endianness::littleEndian<unsigned short>(header.height);
|
||
|
15 years ago
|
|
||
|
15 years ago
|
if(header.colorMapType != 0) {
|
||
|
14 years ago
|
Error() << "TgaImporter: paletted files are not supported";
|
||
|
15 years ago
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
if(header.imageType != 2) {
|
||
|
14 years ago
|
Error() << "TgaImporter: non-RGB files are not supported";
|
||
|
15 years ago
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
14 years ago
|
AbstractTexture::ColorFormat colorFormat;
|
||
|
15 years ago
|
switch(header.bpp) {
|
||
|
|
case 24:
|
||
|
14 years ago
|
colorFormat = AbstractTexture::ColorFormat::BGR;
|
||
|
15 years ago
|
break;
|
||
|
|
case 32:
|
||
|
14 years ago
|
colorFormat = AbstractTexture::ColorFormat::BGRA;
|
||
|
15 years ago
|
break;
|
||
|
|
default:
|
||
|
14 years ago
|
Error() << "TgaImporter: unsupported bits-per-pixel:" << (int) header.bpp;
|
||
|
15 years ago
|
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);
|
||
|
|
|
||
|
14 years ago
|
_image = new ImageData2D(colorFormat, dimensions, buffer);
|
||
|
15 years ago
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
14 years ago
|
void TgaImporter::close() {
|
||
|
14 years ago
|
delete _image;
|
||
|
|
_image = nullptr;
|
||
|
15 years ago
|
}
|
||
|
|
|
||
|
14 years ago
|
ImageData2D* TgaImporter::image2D(size_t id) {
|
||
|
15 years ago
|
return _image;
|
||
|
|
}
|
||
|
|
|
||
|
|
}}}
|