aboutsummaryrefslogtreecommitdiff
path: root/graphics/imagedec.cpp
blob: 5d9eac3f4bf5e3ffb803740c853a17d832900abc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/* ScummVM - Scumm Interpreter
 * Copyright (C) 2006 The ScummVM project
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * $URL$
 * $Id$
 */

#include "graphics/imagedec.h"

#include "common/system.h"
#include "common/file.h"

namespace Graphics {
//
// BMP Decoder
//
class BMPDecoder : public ImageDecoder {
public:
	BMPDecoder() {}
	virtual ~BMPDecoder() {}

	bool decodeable(Common::SeekableReadStream &stream);
	Surface *decodeImage(Common::SeekableReadStream &stream);

	struct BitmapHeader {
		uint16 type;
		uint32 size;
		uint16 res1;
		uint16 res2;
		uint32 imageOffset;
	};

	struct InfoHeader {
		uint32 size;
		uint32 width;
		uint32 height;
		uint16 planes;
		uint16 bitsPerPixel;
		uint32 compression;
		uint32 imageSize;
		uint32 pixelsPerMeterX;
		uint32 pixelsPerMeterY;
		uint32 colorsUsed;
		uint32 colorsImportant;
	};
};

bool BMPDecoder::decodeable(Common::SeekableReadStream &stream) {
	BitmapHeader header;
	stream.seek(0);
	header.type = stream.readUint16BE();
	header.size = stream.readUint32LE();

	// TODO: maybe improve this detection
	if (header.size == 0 || header.type != 'BM')
		return false;

	return true;
}

Surface *BMPDecoder::decodeImage(Common::SeekableReadStream &stream) {
	if (!decodeable(stream)) {
		return 0;
	}

	BitmapHeader header;
	InfoHeader info;

	stream.seek(0);
	header.type = stream.readUint16BE();
	header.size = stream.readUint32LE();
	header.res1 = stream.readUint16LE();
	header.res2 = stream.readUint16LE();
	header.imageOffset = stream.readUint32LE();

	if (header.size == 0 || header.type != 'BM') {
		stream.seek(0);
		return 0;
	}

	info.size = stream.readUint32LE();
	info.width = stream.readUint32LE();
	info.height = stream.readUint32LE();
	info.planes = stream.readUint16LE();
	info.bitsPerPixel = stream.readUint16LE();
	info.compression = stream.readUint32LE();
	info.imageSize = stream.readUint32LE();
	info.pixelsPerMeterX = stream.readUint32LE();
	info.pixelsPerMeterY = stream.readUint32LE();
	info.colorsUsed = stream.readUint32LE();
	info.colorsImportant = stream.readUint32LE();

	stream.seek(header.imageOffset);

	if (info.bitsPerPixel != 24) {
		stream.seek(0);
		return 0;
	}

	uint8 r = 0, g = 0, b = 0;
	Surface *newSurf = new Surface;
	assert(newSurf);
	newSurf->create(info.width, info.height, sizeof(OverlayColor));
	assert(newSurf->pixels);
	OverlayColor *curPixel = (OverlayColor*)newSurf->pixels + (newSurf->h-1) * newSurf->w;
	int pitchAdd = info.width % 4;
	for (int i = 0; i < newSurf->h; ++i) {
		for (int i2 = 0; i2 < newSurf->w; ++i2) {
			b = stream.readByte();
			g = stream.readByte();
			r = stream.readByte();
			*curPixel = g_system->RGBToColor(r, g, b);
			++curPixel;
		}
		stream.seek(pitchAdd, SEEK_CUR);
		curPixel -= newSurf->w*2;
	}

	stream.seek(0);
	return newSurf;
}

#pragma mark -

Surface *ImageDecoder::loadFile(const Common::String &name) {
	Surface *newSurf = 0;

	Common::File imageFile;
	if (imageFile.open(name)) {
		newSurf = loadFile(imageFile);
	}

	return newSurf;
}

Surface *ImageDecoder::loadFile(Common::SeekableReadStream &stream) {
	// TODO: implement support for bzipped memory

	// FIXME: this is not a very nice solution but it should work
	// for the moment, we should use a different way to get all
	// decoders
	static BMPDecoder bmpDecoder;
	static ImageDecoder *decoderList[] = {
		&bmpDecoder,			// for uncompressed .BMP files
		0
	};

	ImageDecoder *decoder = 0;
	for (int i = 0; decoderList[i] != 0; ++i) {
		if (decoderList[i]->decodeable(stream)) {
			decoder = decoderList[i];
			break;
		}
	}

	if (!decoder)
		return 0;

	return decoder->decodeImage(stream);
}
} // end of namespace Graphics