1 /****************************************************************************
2 * MeshLab                                                           o o     *
3 * A versatile mesh processing toolbox                             o     o   *
4 *                                                                _   O  _   *
5 * Copyright(C) 2005-2008                                           \/)\/    *
6 * Visual Computing Lab                                            /\/|      *
7 * ISTI - Italian National Research Council                           |      *
8 *                                                                    \      *
9 * All rights reserved.                                                      *
10 *                                                                           *
11 * This program is free software; you can redistribute it and/or modify      *
12 * it under the terms of the GNU General Public License as published by      *
13 * the Free Software Foundation; either version 2 of the License, or         *
14 * (at your option) any later version.                                       *
15 *                                                                           *
16 * This program is distributed in the hope that it will be useful,           *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of            *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
19 * GNU General Public License (http://www.gnu.org/licenses/gpl.txt)          *
20 * for more details.                                                         *
21 *                                                                           *
22 ****************************************************************************/
23 
24 #ifndef RFX_DDS_H_
25 #define RFX_DDS_H_
26 
27 #include <QFile>
28 #include <GL/glew.h>
29 #include "rfx_textureloader.h"
30 
31 /*
32  * bunch of declarations and defines for the DDS file format
33  */
34 struct DDPIXELFORMAT {
35 	unsigned int dwSize;
36 	unsigned int dwFlags;
37 	unsigned int dwFourCC;
38 	unsigned int dwRGBBitCount;
39 	unsigned int dwRBitMask;
40 	unsigned int dwGBitMask;
41 	unsigned int dwBBitMask;
42 	unsigned int dwAlphaBitMask;
43 };
44 
45 struct DDSCAPS2 {
46 	unsigned int dwCaps1;
47 	unsigned int dwCaps2;
48 	unsigned int dwReserved[2];
49 };
50 
51 // DDS Header as found in MS reference:
52 // http://msdn.microsoft.com/en-us/library/bb943981(VS.85).aspx
53 struct DDSHeader {
54 	unsigned int dwMagic;
55 	unsigned int dwSize;
56 	unsigned int dwFlags;
57 	unsigned int dwHeight;
58 	unsigned int dwWidth;
59 	unsigned int dwPitchOrLinearSize;
60 	unsigned int dwDepth;
61 	unsigned int dwMipMapCount;
62 	unsigned int dwReserved1[11];
63 	DDPIXELFORMAT ddpfPixelFormat;
64 	DDSCAPS2 ddsCaps;
65 	unsigned int dwReserved2;
66 };
67 
68 //  DDSHeader.dwFlags
69 #define DDSD_CAPS                   0x00000001
70 #define DDSD_HEIGHT                 0x00000002
71 #define DDSD_WIDTH                  0x00000004
72 #define DDSD_PITCH                  0x00000008
73 #define DDSD_PIXELFORMAT            0x00001000
74 #define DDSD_MIPMAPCOUNT            0x00020000
75 #define DDSD_LINEARSIZE             0x00080000
76 #define DDSD_DEPTH                  0x00800000
77 
78 //  DDSHeader.ddpfPixelFormat.dwFlags
79 #define DDPF_ALPHAPIXELS            0x00000001
80 #define DDPF_ALPHA                  0x00000002
81 #define DDPF_FOURCC                 0x00000004
82 #define DDPF_INDEXED                0x00000020
83 #define DDPF_RGB                    0x00000040
84 #define DDPF_COMPRESSED             0x00000080
85 #define DDPF_LUMINANCE              0x00020000
86 
87 //  DDSHeader.ddpfPixelFormat.dwFourCC
88 #define FOURCC_DXT1                 0x31545844
89 #define FOURCC_DXT3                 0x33545844
90 #define FOURCC_DXT5                 0x35545844
91 
92 //  DDSHeader.ddsCaps.dwCaps1
93 #define DDSCAPS_COMPLEX             0x00000008
94 #define DDSCAPS_TEXTURE             0x00001000
95 #define DDSCAPS_MIPMAP              0x00400000
96 
97 //  DDSHeader.ddsCaps.dwCaps2
98 #define DDSCAPS2_CUBEMAP            0x00000200
99 #define DDSCAPS2_CUBEMAP_POSITIVEX  0x00000400
100 #define DDSCAPS2_CUBEMAP_NEGATIVEX  0x00000800
101 #define DDSCAPS2_CUBEMAP_POSITIVEY  0x00001000
102 #define DDSCAPS2_CUBEMAP_NEGATIVEY  0x00002000
103 #define DDSCAPS2_CUBEMAP_POSITIVEZ  0x00004000
104 #define DDSCAPS2_CUBEMAP_NEGATIVEZ  0x00008000
105 #define DDSCAPS2_VOLUME             0x00200000
106 
107 
108 
109 /*
110  * RfxDDSPlugin Loader
111  */
112 class RfxDDSPlugin : public RfxTextureLoaderPlugin
113 {
114 public:
RfxDDSPlugin()115 	RfxDDSPlugin() {}
~RfxDDSPlugin()116 	virtual ~RfxDDSPlugin() {}
117 	virtual QList<QByteArray> supportedFormats();
118 	virtual GLuint Load(const QString&, QList<RfxState*>&);
119 	virtual ImageInfo LoadAsQImage(const QString &f);
PluginName()120 	virtual const QString PluginName() { return QString("DDS Plugin"); }
121 
122 private:
123 	bool ValidateHeader(DDSHeader&);
124 	bool GetOGLFormat(DDSHeader&);
125 	bool DXT1CheckAlpha(unsigned char*, int);
126 	int ComputeImageSize();
127 	unsigned char* LoadImageData(const QString&);
128 	void flipImg(char *image, int width, int height, int depth, int size);
129 	void swap(void *byte1, void *byte2, int size);
130 
131 	struct DXTColBlock {
132 		short col0;
133 		short col1;
134 		char row[4];
135 	};
136 
137 	struct DXT3AlphaBlock {
138 		short row[4];
139 	};
140 
141 	struct DXT5AlphaBlock {
142 		char alpha0;
143 		char alpha1;
144 		char row[6];
145 	};
146 
147 	void flip_blocks_dxtc1(DXTColBlock *line, int numBlocks);
148 	void flip_blocks_dxtc3(DXTColBlock *line, int numBlocks);
149 	void flip_blocks_dxtc5(DXTColBlock *line, int numBlocks);
150 	void flip_dxt5_alpha(DXT5AlphaBlock *block);
151 
152 	GLuint tex;
153 	int texTarget;
154 	int texFormat;
155 	bool isCompressed;
156 	bool isCubemap;
157 	bool isVolume;
158 	int width;
159 	int height;
160 	int depth;
161 	int mipCount;
162 	int components;
163 };
164 
165 REGISTER_PLUGIN(RfxDDSPlugin)
166 
167 #endif /* RFX_DDS_H_ */
168