File: dds.cpp

package info (click to toggle)
scummvm 2.9.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 450,580 kB
  • sloc: cpp: 4,299,825; asm: 28,322; python: 12,901; sh: 11,302; java: 9,289; xml: 7,895; perl: 2,639; ansic: 2,465; yacc: 1,670; javascript: 1,020; makefile: 933; lex: 578; awk: 275; objc: 82; sed: 11; php: 1
file content (198 lines) | stat: -rw-r--r-- 5,776 bytes parent folder | download | duplicates (3)
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * 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 3 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, see <http://www.gnu.org/licenses/>.
 *
 */

#include "engines/stark/formats/dds.h"

#include "common/textconsole.h"

namespace Stark {
namespace Formats {

// Based on xoreos' DDS code

static const uint32 kDDSID  = MKTAG('D', 'D', 'S', ' ');

static const uint32 kHeaderFlagsHasMipMaps = 0x00020000;

static const uint32 kPixelFlagsHasAlpha  = 0x00000001;
static const uint32 kPixelFlagsHasFourCC = 0x00000004;
static const uint32 kPixelFlagsIsIndexed = 0x00000020;
static const uint32 kPixelFlagsIsRGB     = 0x00000040;

DDS::~DDS() {
	for (uint i = 0; i < _mipmaps.size(); i++) {
		_mipmaps[i].free();
	}
}

bool DDS::load(Common::SeekableReadStream &dds, const Common::String &name) {
	assert(_mipmaps.empty());

	_name = name;

	if (!readHeader(dds)) {
		return false;
	}

	return readData(dds);
}

const DDS::MipMaps &DDS::getMipMaps() const {
	return _mipmaps;
}

bool DDS::readHeader(Common::SeekableReadStream &dds) {
	// We found the FourCC of a standard DDS
	uint32 magic = dds.readUint32BE();
	if (magic != kDDSID) {
		warning("Invalid DDS magic number: %d for %s", magic, _name.c_str());
		return false;
	}

	// All DDS header should be 124 bytes (+ 4 for the FourCC)
	uint32 headerSize = dds.readUint32LE();
	if (headerSize != 124) {
		warning("Invalid DDS header size: %d for %s", headerSize, _name.c_str());
		return false;
	}

	// DDS features
	uint32 flags = dds.readUint32LE();

	// Image dimensions
	uint32 height = dds.readUint32LE();
	uint32 width  = dds.readUint32LE();

	if ((width >= 0x8000) || (height >= 0x8000)) {
		warning("Unsupported DDS image dimensions (%ux%u) for %s", width, height, _name.c_str());
		return false;
	}

	dds.skip(4 + 4); // Pitch + Depth
	//uint32 pitchOrLineSize = dds.readUint32LE();
	//uint32 depth           = dds.readUint32LE();
	uint32 mipMapCount     = dds.readUint32LE();

	// DDS doesn't provide any mip maps, only one full-size image
	if ((flags & kHeaderFlagsHasMipMaps) == 0) {
		mipMapCount = 1;
	}

	dds.skip(44); // Reserved

	// Read the pixel data format
	DDSPixelFormat format;
	format.size     = dds.readUint32LE();
	format.flags    = dds.readUint32LE();
	format.fourCC   = dds.readUint32BE();
	format.bitCount = dds.readUint32LE();
	format.rBitMask = dds.readUint32LE();
	format.gBitMask = dds.readUint32LE();
	format.bBitMask = dds.readUint32LE();
	format.aBitMask = dds.readUint32LE();

	// Detect which specific format it describes
	if (!detectFormat(format)) {
		return false;
	}

	dds.skip(16 + 4); // DDCAPS2 + Reserved

	_mipmaps.resize(mipMapCount);
	for (uint32 i = 0; i < mipMapCount; i++) {
		_mipmaps[i].create(width, height, _format);

		width  >>= 1;
		height >>= 1;
	}

	return true;
}

bool DDS::readData(Common::SeekableReadStream &dds) {
	for (uint i = 0; i < _mipmaps.size(); i++) {
		Graphics::Surface &mipmap = _mipmaps[i];

		uint32 size = mipmap.pitch * mipmap.h;
		uint32 readSize = dds.read(mipmap.getPixels(), size);

		if (readSize != size) {
			warning("Inconsistent read size in DDS file: %d, expected %d for %s level %d",
			        readSize, size, _name.c_str(), i);
			return false;
		}
	}

	return true;
}

bool DDS::detectFormat(const DDSPixelFormat &format) {
	if (format.flags & kPixelFlagsHasFourCC) {
		warning("Unsupported DDS feature: FourCC pixel format %d for %s", format.fourCC, _name.c_str());
		return false;
	}

	if (format.flags & kPixelFlagsIsIndexed) {
		warning("Unsupported DDS feature: Indexed %d-bits pixel format for %s", format.bitCount, _name.c_str());
		return false;
	}

	if (!(format.flags & kPixelFlagsIsRGB)) {
		warning("Only RGB DDS files are supported for %s", _name.c_str());
		return false;
	}

	if (format.bitCount != 24 && format.bitCount != 32) {
		warning("Only 24-bits and 32-bits DDS files are supported for %s", _name.c_str());
		return false;
	}

	if ((format.flags & kPixelFlagsHasAlpha) &&
	           (format.bitCount == 32) &&
	           (format.rBitMask == 0x00FF0000) && (format.gBitMask == 0x0000FF00) &&
	           (format.bBitMask == 0x000000FF) && (format.aBitMask == 0xFF000000)) {
#ifdef SCUMM_BIG_ENDIAN
		_format = Graphics::PixelFormat(4, 8, 8, 8, 8, 24, 0, 8, 16);
#else
		_format = Graphics::PixelFormat(4, 8, 8, 8, 8, 16, 8, 0, 24);
#endif
		return true;
	} else if (!(format.flags & kPixelFlagsHasAlpha) &&
	           (format.bitCount == 24) &&
	           (format.rBitMask == 0x00FF0000) && (format.gBitMask == 0x0000FF00) &&
	           (format.bBitMask == 0x000000FF)) {
#ifdef SCUMM_BIG_ENDIAN
		_format = Graphics::PixelFormat(3, 8, 8, 8, 0, 0, 8, 16, 0);
#else
		_format = Graphics::PixelFormat(3, 8, 8, 8, 0, 16, 8, 0, 0);
#endif
		return true;
	} else {
		warning("Unsupported pixel format (%X, %X, %d, %X, %X, %X, %X) for %s",
		        format.flags, format.fourCC, format.bitCount,
		        format.rBitMask, format.gBitMask, format.bBitMask, format.aBitMask,
		        _name.c_str());
		return false;
	}
}

} // End of namespace Formats
} // End of namespace Stark