xoreos  0.0.5
dumptga.cpp
Go to the documentation of this file.
1 /* xoreos - A reimplementation of BioWare's Aurora engine
2  *
3  * xoreos is the legal property of its developers, whose names
4  * can be found in the AUTHORS file distributed with this source
5  * distribution.
6  *
7  * xoreos is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 3
10  * of the License, or (at your option) any later version.
11  *
12  * xoreos is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with xoreos. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
25 #include <cstdio>
26 
27 #include "src/common/scopedptr.h"
28 #include "src/common/error.h"
29 #include "src/common/ustring.h"
30 #include "src/common/writefile.h"
31 
33 
34 namespace Graphics {
35 
36 static void writePixel(Common::WriteStream &file, const byte *&data, PixelFormat format) {
37  if (format == kPixelFormatRGB) {
38  file.writeByte(data[2]);
39  file.writeByte(data[1]);
40  file.writeByte(data[0]);
41  file.writeByte(0xFF);
42  data += 3;
43  } else if (format == kPixelFormatBGR) {
44  file.writeByte(data[0]);
45  file.writeByte(data[1]);
46  file.writeByte(data[2]);
47  file.writeByte(0xFF);
48  data += 3;
49  } else if (format == kPixelFormatRGBA) {
50  file.writeByte(data[2]);
51  file.writeByte(data[1]);
52  file.writeByte(data[0]);
53  file.writeByte(data[3]);
54  data += 4;
55  } else if (format == kPixelFormatBGRA) {
56  file.writeByte(data[0]);
57  file.writeByte(data[1]);
58  file.writeByte(data[2]);
59  file.writeByte(data[3]);
60  data += 4;
61  } else
62  throw Common::Exception("Unsupported pixel format: %d", (int) format);
63 
64 }
65 
66 static Common::WriteStream *openTGA(const Common::UString &fileName, int width, int height) {
67  Common::WriteFile *file = new Common::WriteFile(fileName);
68 
69  file->writeByte(0); // ID Length
70  file->writeByte(0); // Palette size
71  file->writeByte(2); // Unmapped RGB
72  file->writeUint32LE(0); // Color map
73  file->writeByte(0); // Color map
74  file->writeUint16LE(0); // X
75  file->writeUint16LE(0); // Y
76 
77  file->writeUint16LE(width);
78  file->writeUint16LE(height);
79 
80  file->writeByte(32); // Pixel depths
81 
82  file->writeByte(0);
83 
84  return file;
85 }
86 
87 static void writeMipMap(Common::WriteStream &stream, const ImageDecoder::MipMap &mipMap, PixelFormat format) {
88  const byte *data = mipMap.data.get();
89 
90  uint32 count = mipMap.width * mipMap.height;
91  while (count-- > 0)
92  writePixel(stream, data, format);
93 }
94 
95 void dumpTGA(const Common::UString &fileName, const ImageDecoder *image) {
96  if (!image || (image->getLayerCount() < 1) || (image->getMipMapCount() < 1))
97  throw Common::Exception("No image");
98 
99  int32 width = image->getMipMap(0, 0).width;
100  int32 height = 0;
101 
102  for (size_t i = 0; i < image->getLayerCount(); i++) {
103  const ImageDecoder::MipMap &mipMap = image->getMipMap(0, i);
104 
105  if (mipMap.width != width)
106  throw Common::Exception("dumpTGA(): Unsupported image with variable layer width");
107 
108  height += mipMap.height;
109  }
110 
111  Common::ScopedPtr<Common::WriteStream> file(openTGA(fileName, width, height));
112 
113  for (size_t i = 0; i < image->getLayerCount(); i++)
114  writeMipMap(*file, image->getMipMap(0, i), image->getFormat());
115 
116  file->flush();
117 }
118 
119 } // End of namespace Graphics
PixelFormat getFormat() const
Return the image data&#39;s general format.
Definition: decoder.cpp:176
static void writeMipMap(Common::WriteStream &stream, const ImageDecoder::MipMap &mipMap, PixelFormat format)
Definition: dumptga.cpp:87
Generic image decoder interface.
static void writePixel(Common::WriteStream &file, const byte *&data, PixelFormat format)
Definition: dumptga.cpp:36
virtual void flush()
Commit any buffered data to the underlying channel or storage medium; unbuffered streams can use the ...
Definition: writestream.cpp:69
A class holding an UTF-8 string.
Definition: ustring.h:48
Common::ScopedArray< byte > data
The mip map&#39;s data.
Definition: decoder.h:56
static Common::WriteStream * openTGA(const Common::UString &fileName, int width, int height)
Definition: dumptga.cpp:66
size_t getLayerCount() const
Return the number of layers contained in the image.
Definition: decoder.cpp:194
int height
The mip map&#39;s height.
Definition: decoder.h:53
A simple scoped smart pointer template.
void dumpTGA(const Common::UString &fileName, const ImageDecoder *image)
Dump image into a TGA file.
Definition: dumptga.cpp:95
Basic exceptions to throw.
const MipMap & getMipMap(size_t mipMap, size_t layer=0) const
Return a mip map.
Definition: decoder.cpp:204
void writeUint16LE(uint16 value)
Definition: writestream.h:98
StackException Exception
Definition: error.h:59
A scoped plain pointer, allowing pointer-y access and normal deletion.
Definition: scopedptr.h:120
void writeByte(byte value)
Definition: writestream.h:88
Generic interface for a writable data stream.
Definition: writestream.h:64
Unicode string handling.
PointerType get() const
Returns the plain pointer value.
Definition: scopedptr.h:96
int width
The mip map&#39;s width.
Definition: decoder.h:52
uint32_t uint32
Definition: types.h:204
Implementing the stream writing interfaces for files.
A generic interface for image decoders.
Definition: decoder.h:48
PixelFormat
Definition: types.h:48
size_t getMipMapCount() const
Return the number of mip maps contained in the image.
Definition: decoder.cpp:188
A simple streaming file writing class.
Definition: writefile.h:40
void writeUint32LE(uint32 value)
Definition: writestream.h:104
uint8 byte
Definition: types.h:209
int32_t int32
Definition: types.h:203