summaryrefslogtreecommitdiffstats
path: root/kfile-plugins/dds/kfile_dds.cpp
blob: c392685febce95b855fe1a3943d350693be4ab7c (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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/* This file is part of the KDE project
 * Copyright (C) 2002 Ignacio Casta�o <[email protected]>
 *
 * 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 version 2.
 *
 * 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; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 *
 */

#include <config.h>
#include "kfile_dds.h"

#include <kprocess.h>
#include <klocale.h>
#include <kgenericfactory.h>
#include <kstringvalidator.h>
#include <kdebug.h>

#include <tqdict.h>
#include <tqvalidator.h>
#include <tqcstring.h>
#include <tqfile.h>
#include <tqdatetime.h>


typedef KGenericFactory<KDdsPlugin> DdsFactory;

typedef TQ_UINT32 uint;
typedef TQ_UINT16 ushort;
typedef TQ_UINT8 uchar;

namespace {	// Private.

#if !defined(MAKEFOURCC)
#	define MAKEFOURCC(ch0, ch1, ch2, ch3) \
		(uint(uchar(ch0)) | (uint(uchar(ch1)) << 8) | \
		(uint(uchar(ch2)) << 16) | (uint(uchar(ch3)) << 24 ))
#endif

	static const uint FOURCC_DDS = MAKEFOURCC('D', 'D', 'S', ' ');
	static const uint FOURCC_DXT1 = MAKEFOURCC('D', 'X', 'T', '1');
	static const uint FOURCC_DXT2 = MAKEFOURCC('D', 'X', 'T', '2');
	static const uint FOURCC_DXT3 = MAKEFOURCC('D', 'X', 'T', '3');
	static const uint FOURCC_DXT4 = MAKEFOURCC('D', 'X', 'T', '4');
	static const uint FOURCC_DXT5 = MAKEFOURCC('D', 'X', 'T', '5');
	static const uint FOURCC_RXGB = MAKEFOURCC('R', 'X', 'G', 'B');

	static const uint DDSD_CAPS = 0x00000001l;
	static const uint DDSD_PIXELFORMAT = 0x00001000l;
	static const uint DDSD_WIDTH = 0x00000004l;
	static const uint DDSD_HEIGHT = 0x00000002l;
	static const uint DDSD_PITCH = 0x00000008l;

	static const uint DDSCAPS_TEXTURE = 0x00001000l;
	static const uint DDSCAPS2_VOLUME = 0x00200000l;
	static const uint DDSCAPS2_CUBEMAP = 0x00000200l;

	static const uint DDPF_RGB = 0x00000040l;
 	static const uint DDPF_FOURCC = 0x00000004l;
 	static const uint DDPF_ALPHAPIXELS = 0x00000001l;

	enum DDSType {
		DDS_A8R8G8B8 = 0,
		DDS_A1R5G5B5 = 1,
		DDS_A4R4G4B4 = 2,
		DDS_R8G8B8 = 3,
		DDS_R5G6B5 = 4,
		DDS_DXT1 = 5,
		DDS_DXT2 = 6,
		DDS_DXT3 = 7,
		DDS_DXT4 = 8,
		DDS_DXT5 = 9,
		DDS_RXGB = 10,
		DDS_UNKNOWN
	};


	struct DDSPixelFormat {
		uint size;
		uint flags;
		uint fourcc;
		uint bitcount;
		uint rmask;
		uint gmask;
		uint bmask;
		uint amask;
	};

	TQDataStream & operator>> ( TQDataStream & s, DDSPixelFormat & pf )
	{
		s >> pf.size;
		s >> pf.flags;
		s >> pf.fourcc;
		s >> pf.bitcount;
		s >> pf.rmask;
		s >> pf.gmask;
		s >> pf.bmask;
		s >> pf.amask;
		return s;
	}

	struct DDSCaps {
		uint caps1;
		uint caps2;
		uint caps3;
		uint caps4;
	};

	TQDataStream & operator>> ( TQDataStream & s, DDSCaps & caps )
	{
		s >> caps.caps1;
		s >> caps.caps2;
		s >> caps.caps3;
		s >> caps.caps4;
		return s;
	}

	struct DDSHeader {
		uint size;
		uint flags;
		uint height;
		uint width;
		uint pitch;
		uint depth;
		uint mipmapcount;
		uint reserved[11];
		DDSPixelFormat pf;
		DDSCaps caps;
		uint notused;
	};

	TQDataStream & operator>> ( TQDataStream & s, DDSHeader & header )
	{
		s >> header.size;
		s >> header.flags;
		s >> header.height;
		s >> header.width;
		s >> header.pitch;
		s >> header.depth;
		s >> header.mipmapcount;
		for( int i = 0; i < 11; i++ ) {
			s >> header.reserved[i];
		}
		s >> header.pf;
		s >> header.caps;
		s >> header.notused;
		return s;
	}

	static bool IsValid( const DDSHeader & header )
	{
		if( header.size != 124 ) {
			return false;
		}
		const uint required = (DDSD_WIDTH|DDSD_HEIGHT|DDSD_CAPS|DDSD_PIXELFORMAT);
		if( (header.flags & required) != required ) {
			return false;
		}
		if( header.pf.size != 32 ) {
			return false;
		}
		if( !(header.caps.caps1 & DDSCAPS_TEXTURE) ) {
			return false;
		}
		return true;
	}
	
} // namespace



K_EXPORT_COMPONENT_FACTORY(kfile_dds, DdsFactory( "kfile_dds" ))

// Constructor, init mime type info.
KDdsPlugin::KDdsPlugin(TQObject *parent, const char *name, const TQStringList &args) : 
	KFilePlugin(parent, name, args)
{
    KFileMimeTypeInfo * info = addMimeTypeInfo( "image/x-dds" );

    KFileMimeTypeInfo::GroupInfo * group = 0L;

    group = addGroupInfo(info, "Technical", i18n("Technical Details"));

    KFileMimeTypeInfo::ItemInfo * item;

    item = addItemInfo(group, "Dimensions", i18n("Dimensions"), TQVariant::Size);
    setHint(item, KFileMimeTypeInfo::Size);
    setUnit(item, KFileMimeTypeInfo::Pixels);

	item = addItemInfo(group, "Depth", i18n("Depth"), TQVariant::Int);
	setUnit(item, KFileMimeTypeInfo::Pixels);
	
    item = addItemInfo(group, "BitDepth", i18n("Bit Depth"), TQVariant::Int);
    setUnit(item, KFileMimeTypeInfo::BitsPerPixel);

	addItemInfo(group, "MipmapCount", i18n("Mipmap Count"), TQVariant::Int);
	
	addItemInfo(group, "Type", i18n("Type"), TQVariant::String);
    addItemInfo(group, "ColorMode", i18n("Color Mode"), TQVariant::String);
    addItemInfo(group, "Compression", i18n("Compression"), TQVariant::String);
}

// Read mime type info.
bool KDdsPlugin::readInfo( KFileMetaInfo& info, uint /*what*/)
{
	TQFile file(info.path());

	if (!file.open(IO_ReadOnly)) {
		kdDebug(7034) << "Couldn't open " << TQFile::encodeName(info.path()).data() << endl;
		return false;
	}

	TQDataStream s(&file);
	s.setByteOrder(TQDataStream::LittleEndian);

	// Validate header.
	uint fourcc;
	s >> fourcc;
	if( fourcc != FOURCC_DDS ) {
		kdDebug(7034) << TQFile::encodeName(info.path()).data() << " is not a DDS file." << endl;
		return false;
	}

	// Read image header.
	DDSHeader header;
	s >> header;

	// Check image file format.
	if( s.atEnd() || !IsValid( header ) ) {
		kdDebug(7034) << TQFile::encodeName(info.path()).data() << " is not a valid DDS file." << endl;
		return false;
	}

	// Set file info.
	KFileMetaInfoGroup group = appendGroup(info, "Technical");
 	appendItem(group, "Dimensions", TQSize(header.width, header.height));
	appendItem(group, "MipmapCount", header.mipmapcount);
	
	// Set file type.
	if( header.caps.caps2 & DDSCAPS2_CUBEMAP ) {
		appendItem(group, "Type", i18n("Cube Map Texture"));
	}
	else if( header.caps.caps2 & DDSCAPS2_VOLUME ) {
		appendItem(group, "Type", i18n("Volume Texture"));
		appendItem(group, "Depth", header.depth);
	}
	else {
		appendItem(group, "Type", i18n("2D Texture"));
	}

	// Set file color depth and compression.
	if( header.pf.flags & DDPF_RGB ) {
		appendItem(group, "BitDepth", header.pf.bitcount);
		appendItem(group, "Compression", i18n("Uncompressed"));
		if( header.pf.flags & DDPF_ALPHAPIXELS ) {
			appendItem(group, "ColorMode", "RGB/Alpha");
		}
		else {
			appendItem(group, "ColorMode", "RGB");
		}
	}
	else if( header.pf.flags & DDPF_FOURCC ) {
		switch( header.pf.fourcc ) {
			case FOURCC_DXT1:
				appendItem(group, "BitDepth", 4);
				appendItem(group, "Compression", "DXT1");
				appendItem(group, "ColorMode", "RGB");
				break;
			case FOURCC_DXT2:
				appendItem(group, "BitDepth", 16);
				appendItem(group, "Compression", "DXT2");
				appendItem(group, "ColorMode", "RGB/Alpha");
				break;
			case FOURCC_DXT3:
				appendItem(group, "BitDepth", 16);
				appendItem(group, "Compression", "DXT3");
				appendItem(group, "ColorMode", "RGB/Alpha");
				break;
			case FOURCC_DXT4:
				appendItem(group, "BitDepth", 16);
				appendItem(group, "Compression", "DXT4");
				appendItem(group, "ColorMode", "RGB/Alpha");
				break;
			case FOURCC_DXT5:
				appendItem(group, "BitDepth", 16);
				appendItem(group, "Compression", "DXT5");
				appendItem(group, "ColorMode", "RGB/Alpha");
				break;
			case FOURCC_RXGB:
				appendItem(group, "BitDepth", 16);
				appendItem(group, "Compression", "RXGB");
				appendItem(group, "ColorMode", "RGB");
				break;
			default:
				appendItem(group, "Compression", "Unknown");
				break;
		}
	}
	else {
		appendItem(group, "Compression", "Unknown");
	}

    return true;
}

#include "kfile_dds.moc"