xoreos  0.0.5
decoder.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 <cassert>
26 
27 #include "src/common/scopedptr.h"
28 #include "src/common/util.h"
29 #include "src/common/error.h"
31 
32 #include "src/graphics/graphics.h"
33 
38 
39 namespace Graphics {
40 
41 ImageDecoder::MipMap::MipMap(const ImageDecoder *i) : width(0), height(0), size(0), image(i) {
42 }
43 
45  width(mipMap.width), height(mipMap.height), size(mipMap.size), image(i) {
46 
47  data.reset(new byte[size]);
48 
49  std::memcpy(data.get(), mipMap.data.get(), size);
50 }
51 
53 }
54 
56  SWAP(width , right.width );
57  SWAP(height, right.height);
58  SWAP(size , right.size );
59  SWAP(image , right.image );
60 
61  data.swap(right.data);
62 }
63 
64 void ImageDecoder::MipMap::getPixel(int x, int y, float &r, float &g, float &b, float &a) const {
65  getPixel(y * width + x, r, g, b, a);
66 }
67 
68 void ImageDecoder::MipMap::getPixel(int n, float &r, float &g, float &b, float &a) const {
69  assert(n < (int)size);
70  assert(image);
71 
72  if (image->getFormat() == kPixelFormatRGB) {
73  r = data[n * 3 + 0] / 255.0f;
74  g = data[n * 3 + 1] / 255.0f;
75  b = data[n * 3 + 2] / 255.0f;
76  a = 1.0f;
77  } else if (image->getFormat() == kPixelFormatBGR) {
78  r = data[n * 3 + 2] / 255.0f;
79  g = data[n * 3 + 1] / 255.0f;
80  b = data[n * 3 + 0] / 255.0f;
81  a = 1.0f;
82  } else if (image->getFormat() == kPixelFormatRGBA) {
83  r = data[n * 4 + 0] / 255.0f;
84  g = data[n * 4 + 1] / 255.0f;
85  b = data[n * 4 + 2] / 255.0f;
86  a = data[n * 4 + 3] / 255.0f;
87  } else if (image->getFormat() == kPixelFormatBGRA) {
88  r = data[n * 4 + 2] / 255.0f;
89  g = data[n * 4 + 1] / 255.0f;
90  b = data[n * 4 + 0] / 255.0f;
91  a = data[n * 4 + 3] / 255.0f;
92  } else
93  throw Common::Exception("ImageDecoder::MipMap::getPixel(): Unsupported pixel format %d",
94  image->getFormat());
95 }
96 
97 void ImageDecoder::MipMap::setPixel(int x, int y, float r, float g, float b, float a) {
98  getPixel(y * width + x, r, g, b, a);
99 }
100 
101 void ImageDecoder::MipMap::setPixel(int n, float r, float g, float b, float a) {
102  assert(n < (int)size);
103  assert(image);
104 
105  if (image->getFormat() == kPixelFormatRGB) {
106  data[n * 3 + 0] = CLIP(r, 0.0f, 1.0f) * 255.0f;
107  data[n * 3 + 1] = CLIP(g, 0.0f, 1.0f) * 255.0f;
108  data[n * 3 + 2] = CLIP(b, 0.0f, 1.0f) * 255.0f;
109  } else if (image->getFormat() == kPixelFormatBGR) {
110  data[n * 3 + 2] = CLIP(r, 0.0f, 1.0f) * 255.0f;
111  data[n * 3 + 1] = CLIP(g, 0.0f, 1.0f) * 255.0f;
112  data[n * 3 + 0] = CLIP(b, 0.0f, 1.0f) * 255.0f;
113  } else if (image->getFormat() == kPixelFormatRGBA) {
114  data[n * 4 + 0] = CLIP(r, 0.0f, 1.0f) * 255.0f;
115  data[n * 4 + 1] = CLIP(g, 0.0f, 1.0f) * 255.0f;
116  data[n * 4 + 2] = CLIP(b, 0.0f, 1.0f) * 255.0f;
117  data[n * 4 + 3] = CLIP(a, 0.0f, 1.0f) * 255.0f;
118  } else if (image->getFormat() == kPixelFormatBGRA) {
119  data[n * 4 + 2] = CLIP(r, 0.0f, 1.0f) * 255.0f;
120  data[n * 4 + 1] = CLIP(g, 0.0f, 1.0f) * 255.0f;
121  data[n * 4 + 0] = CLIP(b, 0.0f, 1.0f) * 255.0f;
122  data[n * 4 + 3] = CLIP(a, 0.0f, 1.0f) * 255.0f;
123  } else
124  throw Common::Exception("ImageDecoder::MipMap::setPixel(): Unsupported pixel format %d",
125  image->getFormat());
126 }
127 
128 
131  _layerCount(1), _isCubeMap(false) {
132 
133 }
134 
136  *this = image;
137 }
138 
140 }
141 
143  _compressed = image._compressed;
144  _hasAlpha = image._hasAlpha;
145 
146  _format = image._format;
147  _formatRaw = image._formatRaw;
148  _dataType = image._dataType;
149 
150  _layerCount = image._layerCount;
151  _isCubeMap = image._isCubeMap;
152 
153  _txi = image._txi;
154 
155  _mipMaps.clear();
156  _mipMaps.reserve(image._mipMaps.size());
157 
158  for (MipMaps::const_iterator m = image._mipMaps.begin(); m != image._mipMaps.end(); ++m)
159  _mipMaps.push_back(new MipMap(**m));
160 
161  return *this;
162 }
163 
164 const TXI &ImageDecoder::getTXI() const {
165  return _txi;
166 }
167 
169  return _compressed;
170 }
171 
173  return _hasAlpha;
174 }
175 
177  return _format;
178 }
179 
181  return _formatRaw;
182 }
183 
185  return _dataType;
186 }
187 
189  assert((_mipMaps.size() % _layerCount) == 0);
190 
191  return _mipMaps.size() / _layerCount;
192 }
193 
195  return _layerCount;
196 }
197 
199  assert(!_isCubeMap || (_layerCount == 6));
200 
201  return _isCubeMap;
202 }
203 
204 const ImageDecoder::MipMap &ImageDecoder::getMipMap(size_t mipMap, size_t layer) const {
205  assert(layer < _layerCount);
206  assert((_mipMaps.size() % _layerCount) == 0);
207 
208  const size_t index = layer * getMipMapCount() + mipMap;
209 
210  assert(index < _mipMaps.size());
211 
212  return *_mipMaps[index];
213 }
214 
215 void ImageDecoder::decompress(MipMap &out, const MipMap &in, PixelFormatRaw format) {
216  if ((format != kPixelFormatDXT1) &&
217  (format != kPixelFormatDXT3) &&
218  (format != kPixelFormatDXT5))
219  throw Common::Exception("Unknown compressed format %d", format);
220 
221  /* The DXT algorithms work on 4x4 pixel blocks. Textures smaller than one
222  * block will be padded, but larger textures need to be correctly aligned. */
223  if (!hasValidDimensions(format, in.width, in.height))
224  throw Common::Exception("Invalid dimensions (%dx%d) for format %d", in.width, in.height, format);
225 
226  out.width = in.width;
227  out.height = in.height;
228  out.size = out.width * out.height * 4;
229 
230  out.data.reset(new byte[out.size]);
231 
233 
234  if (format == kPixelFormatDXT1)
235  decompressDXT1(out.data.get(), *stream, out.width, out.height, out.width * 4);
236  else if (format == kPixelFormatDXT3)
237  decompressDXT3(out.data.get(), *stream, out.width, out.height, out.width * 4);
238  else if (format == kPixelFormatDXT5)
239  decompressDXT5(out.data.get(), *stream, out.width, out.height, out.width * 4);
240 }
241 
243  if (!_compressed)
244  return;
245 
246  for (MipMaps::iterator m = _mipMaps.begin(); m != _mipMaps.end(); ++m) {
247  MipMap decompressed(this);
248 
249  decompress(decompressed, **m, _formatRaw);
250 
251  decompressed.swap(**m);
252  }
253 
257  _compressed = false;
258 }
259 
260 bool ImageDecoder::dumpTGA(const Common::UString &fileName) const {
261  if (_mipMaps.size() < 1)
262  return false;
263 
264  if (!_compressed) {
265  Graphics::dumpTGA(fileName, this);
266  return true;
267  }
268 
269  ImageDecoder image(*this);
270  image.decompress();
271 
272  Graphics::dumpTGA(fileName, &image);
273  return true;
274 }
275 
276 } // End of namespace Graphics
PixelFormat getFormat() const
Return the image data&#39;s general format.
Definition: decoder.cpp:176
Generic image decoder interface.
The global graphics manager.
void getPixel(int x, int y, float &r, float &g, float &b, float &a) const
Get the color values of the pixel at this position.
Definition: decoder.cpp:64
A class holding an UTF-8 string.
Definition: ustring.h:48
void reset(PointerType o=0)
Resets the pointer with the new value.
Definition: scopedptr.h:87
A simple TGA image dumper.
static bool hasValidDimensions(PixelFormatRaw format, int32 width, int32 height)
Are these image dimensions valid for this format?
Definition: util.h:73
MipMap(const ImageDecoder *i=0)
Definition: decoder.cpp:41
Common::ScopedArray< byte > data
The mip map&#39;s data.
Definition: decoder.h:56
bool _isCubeMap
Is this image a cube map? A cube map always needs to have 6 layers!
Definition: decoder.h:129
Implementing the reading stream interfaces for plain memory blocks.
const TXI & getTXI() const
Return the texture information TXI, which may be embedded in the image.
Definition: decoder.cpp:164
PixelDataType
Definition: types.h:65
PixelFormatRaw
Definition: types.h:55
bool isCubeMap() const
Is this image a cube map?
Definition: decoder.cpp:198
PixelDataType _dataType
Definition: decoder.h:124
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 decompress()
Manually decompress the texture image data.
Definition: decoder.cpp:242
bool dumpTGA(const Common::UString &fileName) const
Dump the image into a TGA.
Definition: decoder.cpp:260
void dumpTGA(const Common::UString &fileName, const ImageDecoder *image)
Dump image into a TGA file.
Definition: dumptga.cpp:95
Basic exceptions to throw.
void decompressDXT3(byte *dest, Common::SeekableReadStream &src, uint32 width, uint32 height, uint32 pitch)
Definition: s3tc.cpp:114
virtual ~ImageDecoder()
Definition: decoder.cpp:139
void swap(MipMap &right)
Definition: decoder.cpp:55
PixelFormat _format
Definition: decoder.h:122
Utility templates and functions.
void setPixel(int x, int y, float r, float g, float b, float a)
Set the color values of the pixel at this position.
Definition: decoder.cpp:97
void decompressDXT1(byte *dest, Common::SeekableReadStream &src, uint32 width, uint32 height, uint32 pitch)
Definition: s3tc.cpp:64
size_t _layerCount
Number of layers in this image.
Definition: decoder.h:127
PixelFormatRaw getFormatRaw() const
Return the image data&#39;s raw format.
Definition: decoder.cpp:180
PixelFormatRaw _formatRaw
Definition: decoder.h:123
void decompressDXT5(byte *dest, Common::SeekableReadStream &src, uint32 width, uint32 height, uint32 pitch)
Definition: s3tc.cpp:165
const MipMap & getMipMap(size_t mipMap, size_t layer=0) const
Return a mip map.
Definition: decoder.cpp:204
Simple memory based &#39;stream&#39;, which implements the ReadStream interface for a plain memory block...
Definition: memreadstream.h:66
Image related utility functions.
StackException Exception
Definition: error.h:59
A scoped plain pointer, allowing pointer-y access and normal deletion.
Definition: scopedptr.h:120
const ImageDecoder * image
The image the mip map belongs to.
Definition: decoder.h:58
PixelDataType getDataType() const
Return the image data pixel&#39;s type.
Definition: decoder.cpp:184
PointerType get() const
Returns the plain pointer value.
Definition: scopedptr.h:96
int width
The mip map&#39;s width.
Definition: decoder.h:52
ImageDecoder & operator=(const ImageDecoder &image)
Definition: decoder.cpp:142
Manual S3TC DXTn decompression methods.
Texture information.
Definition: txi.h:40
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
T CLIP(T v, T amin, T amax)
Definition: util.h:72
uint32 size
The mip map&#39;s size in bytes.
Definition: decoder.h:54
bool hasAlpha() const
Does the image data have alpha? .
Definition: decoder.cpp:172
bool isCompressed() const
Is the image data compressed?
Definition: decoder.cpp:168
uint8 byte
Definition: types.h:209
void SWAP(T &a, T &b)
Template method which swaps the values of its two parameters.
Definition: util.h:78