xoreos  0.0.5
nbfs.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/scopedptr.h"
28 #include "src/common/util.h"
29 #include "src/common/error.h"
30 #include "src/common/readstream.h"
31 
33 
34 namespace Graphics {
35 
37  uint32 width, uint32 height) {
38 
39  load(nbfs, nbfp, width, height);
40 }
41 
43 }
44 
46  uint32 width, uint32 height) {
47 
48  try {
49  if (nbfs.size() != (width * height))
50  throw Common::Exception("Dimensions mismatch (%u * %u != %u)", width, height, (uint)nbfs.size());
51 
52  if ((width >= 0x8000) || (height >= 0x8000))
53  throw Common::Exception("Invalid dimensions of %ux%u", width, height);
54 
55  if (nbfp.size() > 512)
56  throw Common::Exception("Too much palette data (%u bytes)", (uint)nbfp.size());
57 
59  readImage(nbfs, palette.get(), width, height);
60 
61  } catch (Common::Exception &e) {
62  e.add("Failed reading NBFS file");
63  throw;
64  }
65 }
66 
68  Common::ScopedArray<byte> palette(new byte[768]);
69  std::memset(palette.get(), 0, 768);
70 
71  const size_t count = MIN<size_t>(nbfp.size() / 2, 256) * 3;
72  for (size_t i = 0; i < count; i += 3) {
73  const uint16 color = nbfp.readUint16LE();
74 
75  palette[i + 0] = ((color >> 10) & 0x1F) << 3;
76  palette[i + 1] = ((color >> 5) & 0x1F) << 3;
77  palette[i + 2] = ( color & 0x1F) << 3;
78  }
79 
80  return palette.release();
81 }
82 
83 void NBFS::readImage(Common::SeekableReadStream &nbfs, const byte *palette,
84  uint32 width, uint32 height) {
85 
89 
90  _mipMaps.push_back(new MipMap);
91 
92  _mipMaps.back()->width = width;
93  _mipMaps.back()->height = height;
94  _mipMaps.back()->size = width * height * 4;
95 
96  _mipMaps.back()->data.reset(new byte[_mipMaps.back()->size]);
97 
98  bool is0Transp = (palette[0] == 0xF8) && (palette[1] == 0x00) && (palette[2] == 0xF8);
99 
100  byte *data = _mipMaps.back()->data.get();
101  for (uint32 i = 0; i < (width * height); i++, data += 4) {
102  uint8 pixel = nbfs.readByte();
103 
104  data[0] = palette[pixel * 3 + 0];
105  data[1] = palette[pixel * 3 + 1];
106  data[2] = palette[pixel * 3 + 2];
107  data[3] = ((pixel == 0) && is0Transp) ? 0x00 : 0xFF;
108  }
109 }
110 
111 } // End of namespace Graphics
uint16 readUint16LE()
Read an unsigned 16-bit word stored in little endian (LSB first) order from the stream and return it...
Definition: readstream.h:122
void add(const char *s,...) GCC_PRINTF(2
Definition: error.cpp:58
PointerType release()
Returns the plain pointer value and releases ScopedPtr.
Definition: scopedptr.h:103
void load(Common::SeekableReadStream &nbfs, Common::SeekableReadStream &nbfp, uint32 width, uint32 height)
Definition: nbfs.cpp:45
uint8_t uint8
Definition: types.h:200
PixelDataType _dataType
Definition: decoder.h:124
NBFS(Common::SeekableReadStream &nbfs, Common::SeekableReadStream &nbfp, uint32 width, uint32 height)
NBFS are raw paletted images and need a palette, width and height.
Definition: nbfs.cpp:36
Exception that provides a stack of explanations.
Definition: error.h:36
A simple scoped smart pointer template.
Basic exceptions to throw.
uint16_t uint16
Definition: types.h:202
PixelFormat _format
Definition: decoder.h:122
Utility templates and functions.
PixelFormatRaw _formatRaw
Definition: decoder.h:123
StackException Exception
Definition: error.h:59
virtual size_t size() const =0
Obtains the total size of the stream, measured in bytes.
Basic reading stream interfaces.
PointerType get() const
Returns the plain pointer value.
Definition: scopedptr.h:96
uint32_t uint32
Definition: types.h:204
const byte * readPalette(Common::SeekableReadStream &nbfp)
Definition: nbfs.cpp:67
Nitro Basic File Screen, a simple raw Nintendo DS image.
Interface for a seekable & readable data stream.
Definition: readstream.h:265
byte readByte()
Read an unsigned byte from the stream and return it.
Definition: readstream.h:92
uint8 byte
Definition: types.h:209
unsigned int uint
Definition: types.h:211
void readImage(Common::SeekableReadStream &nbfs, const byte *palette, uint32 width, uint32 height)
Definition: nbfs.cpp:83