xoreos  0.0.5
sbm.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 <cstring>
26 
27 #include "src/common/util.h"
28 #include "src/common/readstream.h"
29 #include "src/common/error.h"
30 
33 
34 namespace Graphics {
35 
36 SBM::SBM(Common::SeekableReadStream &sbm, bool deswizzle) {
37  _compressed = false;
38  _hasAlpha = true;
42 
43  load(sbm, deswizzle);
44 }
45 
47 }
48 
49 void SBM::load(Common::SeekableReadStream &sbm, bool deswizzle) {
50  try {
51 
52  readData(sbm, deswizzle);
53 
54  } catch (Common::Exception &e) {
55  e.add("Failed reading SBM file");
56  throw;
57  }
58 }
59 
60 void SBM::readData(Common::SeekableReadStream &sbm, bool deswizzle) {
61  if ((sbm.size() % 1024) != 0)
62  throw Common::Exception("Invalid SBM (%u)", (uint)sbm.size());
63 
64  const size_t rowCount = (sbm.size() / 1024);
65 
66  _mipMaps.push_back(new MipMap(this));
67 
68  _mipMaps[0]->width = 4 * 32;
69  _mipMaps[0]->height = NEXTPOWER2((uint32) rowCount * 32);
70  _mipMaps[0]->size = _mipMaps[0]->width * _mipMaps[0]->height * 4;
71 
72  _mipMaps[0]->data.reset(new byte[_mipMaps[0]->size]);
73 
74  // SBM data consists of character sized 32 * 32 pixels, with 2 bits per pixel.
75  // 4 characters each are on top of each other, occupying the same x/y
76  // coordinates but different planes.
77  // We'll unpack them and draw them next to each other instead.
78 
79  static const int masks [4] = { 0x03, 0x0C, 0x30, 0xC0 };
80  static const int shifts[4] = { 0, 2, 4, 6 };
81 
82  byte *data = _mipMaps[0]->data.get();
83  byte buffer[1024];
84  for (size_t c = 0; c < rowCount; c++) {
85 
86  if (sbm.read(buffer, 1024) != 1024)
88 
89  for (int y = 0; y < 32; y++) {
90  for (int plane = 0; plane < 4; plane++) {
91  for (int x = 0; x < 32; x++) {
92  const uint32 offset = deswizzle ? deSwizzleOffset(x, y, 32, rowCount) : (y * 32 + x);
93 
94  const byte a = ((buffer[offset] & masks[plane]) >> shifts[plane]) * 0x55;
95 
96  *data++ = 0xFF; // B
97  *data++ = 0xFF; // G
98  *data++ = 0xFF; // R
99  *data++ = a; // A
100  }
101  }
102  }
103  }
104 
105  byte *dataEnd = _mipMaps[0]->data.get() + _mipMaps[0]->size;
106  std::memset(data, 0, dataEnd - data);
107 }
108 
109 } // End of namespace Graphics
void load(Common::SeekableReadStream &sbm, bool deswizzle)
Definition: sbm.cpp:49
void add(const char *s,...) GCC_PRINTF(2
Definition: error.cpp:58
static uint32 NEXTPOWER2(uint32 x)
Round up to the next power of 2.
Definition: util.h:84
PixelDataType _dataType
Definition: decoder.h:124
Exception that provides a stack of explanations.
Definition: error.h:36
SBM(Common::SeekableReadStream &sbm, bool deswizzle=false)
Read an SBM image out of a stream.
Definition: sbm.cpp:36
void readData(Common::SeekableReadStream &sbm, bool deswizzle)
Definition: sbm.cpp:60
Basic exceptions to throw.
static uint32 deSwizzleOffset(uint32 x, uint32 y, uint32 width, uint32 height)
De-"swizzle" a texture pixel offset.
Definition: util.h:179
PixelFormat _format
Definition: decoder.h:122
Utility templates and functions.
PixelFormatRaw _formatRaw
Definition: decoder.h:123
virtual size_t read(void *dataPtr, size_t dataSize)=0
Read data from the stream.
Image related utility functions.
StackException Exception
Definition: error.h:59
const Exception kReadError("Read error")
Exception when reading from a stream failed.
Definition: error.h:62
virtual size_t size() const =0
Obtains the total size of the stream, measured in bytes.
Basic reading stream interfaces.
Decoding SBM (font bitmap data).
uint32_t uint32
Definition: types.h:204
Interface for a seekable & readable data stream.
Definition: readstream.h:265
uint8 byte
Definition: types.h:209
unsigned int uint
Definition: types.h:211