1 /* ScummVM - Graphic Adventure Engine
2 *
3 * ScummVM is the legal property of its developers, whose names
4 * are too numerous to list here. Please refer to the COPYRIGHT
5 * file distributed with this source distribution.
6 *
7 * This program 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 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program 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 this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 *
21 */
22
23 #include "bladerunner/image.h"
24
25 #include "bladerunner/bladerunner.h"
26 #include "bladerunner/decompress_lcw.h"
27
28 #include "common/rect.h"
29
30 namespace BladeRunner {
31
Image(BladeRunnerEngine * vm)32 Image::Image(BladeRunnerEngine *vm) {
33 _vm = vm;
34 }
35
~Image()36 Image::~Image() {
37 _surface.free();
38 }
39
open(const Common::String & name)40 bool Image::open(const Common::String &name) {
41 Common::SeekableReadStream *stream = _vm->getResourceStream(name);
42 if (!stream) {
43 warning("Image::open failed to open '%s'\n", name.c_str());
44 return false;
45 }
46
47 char tag[4] = { 0 };
48 stream->read(tag, 3);
49 uint32 width = stream->readUint32LE();
50 uint32 height = stream->readUint32LE();
51
52 // Enforce a reasonable limit
53 assert(width < 8000 && height < 8000);
54
55 uint32 bufSize = stream->size();
56 uint8 *buf = new uint8[bufSize];
57 stream->read(buf, bufSize);
58
59 uint32 dataSize = 2 * width * height;
60 void *data = malloc(dataSize);
61 assert(data);
62
63 if (strcmp(tag, "LZO") == 0) {
64 warning("LZO image decompression is not implemented");
65 } else if (strcmp(tag, "LCW") == 0) {
66 decompress_lcw(buf, bufSize, (uint8 *)data, dataSize);
67 #ifdef SCUMM_BIG_ENDIAN
68 // As the compression is working with 8-bit data, on big-endian architectures we have to switch order of bytes in uncompressed data
69 uint8 *rawData = (uint8 *)data;
70 for (size_t i = 0; i < dataSize - 1; i += 2) {
71 SWAP(rawData[i], rawData[i + 1]);
72 }
73 #endif
74 }
75
76 _surface.init(width, height, 2*width, data, gameDataPixelFormat());
77 _surface.convertToInPlace(screenPixelFormat());
78
79 delete[] buf;
80 delete stream;
81
82 return true;
83 }
84
copyToSurface(Graphics::Surface * dst) const85 void Image::copyToSurface(Graphics::Surface *dst) const {
86 dst->copyRectToSurface(_surface, 0, 0, Common::Rect(_surface.w, _surface.h));
87 }
88
89 } // End of namespace BladeRunner
90