forked from CelestiaProject/Celestia
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdds.cpp
More file actions
314 lines (284 loc) · 9.52 KB
/
Copy pathdds.cpp
File metadata and controls
314 lines (284 loc) · 9.52 KB
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
// dds.cpp
//
// Copyright (C) 2001-present, the Celestia Development Team
// Original version by Chris Laurel <claurel@shatters.net>
//
// 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.
#include <iostream>
#include <fstream>
#include <algorithm>
#include <memory>
#include <celengine/glsupport.h>
#include <celengine/image.h>
#include <celutil/debug.h>
#include <celutil/bytes.h>
#include "dds_decompress.h"
using namespace celestia;
using namespace std;
struct DDPixelFormat
{
uint32_t size;
uint32_t flags;
uint32_t fourCC;
uint32_t bpp;
uint32_t redMask;
uint32_t greenMask;
uint32_t blueMask;
uint32_t alphaMask;
};
struct DDSCaps
{
uint32_t caps;
uint32_t caps2;
uint32_t caps3;
uint32_t caps4;
};
struct DDColorKey
{
uint32_t lowVal;
uint32_t highVal;
};
struct DDSurfaceDesc
{
uint32_t size;
uint32_t flags;
uint32_t height;
uint32_t width;
uint32_t pitch;
uint32_t depth;
uint32_t mipMapLevels;
uint32_t alphaBitDepth;
uint32_t reserved;
uint32_t surface;
DDColorKey ckDestOverlay;
DDColorKey ckDestBlt;
DDColorKey ckSrcOverlay;
DDColorKey ckSrcBlt;
DDPixelFormat format;
DDSCaps caps;
uint32_t textureStage;
};
namespace
{
constexpr uint32_t FourCC(const char* s)
{
return (((uint32_t) s[3] << 24) |
((uint32_t) s[2] << 16) |
((uint32_t) s[1] << 8) |
(uint32_t) s[0]);
}
// decompress a DXTc texture to a RGBA texture, taken from https://github.com/ptitSeb/gl4es
uint32_t* decompressDXTc(uint32_t width, uint32_t height, GLenum format, bool transparent0, ifstream &in)
{
// TODO: check with the size of the input data stream if the stream is in fact decompressed
// alloc memory
auto *pixels = new uint32_t[((width + 3) & ~3) * ((height + 3) & ~3)];
int blocksize = 0;
#define DDS_MAX_BLOCK_SIZE 16
switch (format)
{
case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
blocksize = 8;
break;
case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
blocksize = 16;
break;
}
uint8_t block[DDS_MAX_BLOCK_SIZE]; // enough to hold DXT1/3/5 blocks
for (uint32_t y = 0; y < height; y += 4)
{
for (uint32_t x = 0; x < width; x += 4)
{
if (!in.good())
{
delete[] pixels;
return nullptr;
}
in.read((char*)block, blocksize);
switch(format)
{
case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
DecompressBlockDXT1(x, y, width, block, transparent0, pixels);
break;
case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
DecompressBlockDXT3(x, y, width, block, transparent0, pixels);
break;
case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
DecompressBlockDXT5(x, y, width, block, transparent0, pixels);
break;
}
}
}
return pixels;
}
} // anonymous namespace
Image* LoadDDSImage(const fs::path& filename)
{
ifstream in(filename.string(), ios::in | ios::binary);
if (!in.good())
{
DPRINTF(LOG_LEVEL_ERROR, "Error opening DDS texture file %s.\n", filename);
return nullptr;
}
char header[4];
in.read(header, sizeof header);
if (header[0] != 'D' || header[1] != 'D' ||
header[2] != 'S' || header[3] != ' ')
{
DPRINTF(LOG_LEVEL_ERROR, "DDS texture file %s has bad header.\n", filename);
return nullptr;
}
DDSurfaceDesc ddsd;
in.read(reinterpret_cast<char*>(&ddsd), sizeof ddsd);
LE_TO_CPU_INT32(ddsd.size, ddsd.size);
LE_TO_CPU_INT32(ddsd.pitch, ddsd.pitch);
LE_TO_CPU_INT32(ddsd.width, ddsd.width);
LE_TO_CPU_INT32(ddsd.height, ddsd.height);
LE_TO_CPU_INT32(ddsd.mipMapLevels, ddsd.mipMapLevels);
LE_TO_CPU_INT32(ddsd.format.flags, ddsd.format.flags);
LE_TO_CPU_INT32(ddsd.format.redMask, ddsd.format.redMask);
LE_TO_CPU_INT32(ddsd.format.greenMask, ddsd.format.greenMask);
LE_TO_CPU_INT32(ddsd.format.blueMask, ddsd.format.blueMask);
LE_TO_CPU_INT32(ddsd.format.alphaMask, ddsd.format.alphaMask);
LE_TO_CPU_INT32(ddsd.format.bpp, ddsd.format.bpp);
LE_TO_CPU_INT32(ddsd.format.fourCC, ddsd.format.fourCC);
int format = -1;
if (ddsd.format.fourCC != 0)
{
if (ddsd.format.fourCC == FourCC("DXT1"))
{
format = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
}
else if (ddsd.format.fourCC == FourCC("DXT3"))
{
format = GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
}
else if (ddsd.format.fourCC == FourCC("DXT5"))
{
format = GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
}
else
{
cerr << "Unknown FourCC in DDS file: " << ddsd.format.fourCC;
}
}
else
{
clog << "DDS Format: " << ddsd.format.fourCC << '\n';
if (ddsd.format.bpp == 32)
{
if (ddsd.format.redMask == 0x00ff0000 &&
ddsd.format.greenMask == 0x0000ff00 &&
ddsd.format.blueMask == 0x000000ff &&
ddsd.format.alphaMask == 0xff000000)
{
format = GL_BGRA_EXT;
}
else if (ddsd.format.redMask == 0x000000ff &&
ddsd.format.greenMask == 0x0000ff00 &&
ddsd.format.blueMask == 0x00ff0000 &&
ddsd.format.alphaMask == 0xff000000)
{
format = GL_RGBA;
}
}
else if (ddsd.format.bpp == 24)
{
if (ddsd.format.redMask == 0x000000ff &&
ddsd.format.greenMask == 0x0000ff00 &&
ddsd.format.blueMask == 0x00ff0000)
{
format = GL_RGB;
}
#ifndef GL_ES
else if (ddsd.format.redMask == 0x00ff0000 &&
ddsd.format.greenMask == 0x0000ff00 &&
ddsd.format.blueMask == 0x000000ff)
{
format = GL_BGR;
}
#endif
}
}
if (format == -1)
{
DPRINTF(LOG_LEVEL_ERROR, "Unsupported format for DDS texture file %s.\n", filename);
return nullptr;
}
// Check if the platform supports compressed DTXc textures
if (format == GL_COMPRESSED_RGBA_S3TC_DXT1_EXT ||
format == GL_COMPRESSED_RGBA_S3TC_DXT3_EXT ||
format == GL_COMPRESSED_RGBA_S3TC_DXT5_EXT)
{
if (!gl::EXT_texture_compression_s3tc)
{
// DXTc texture not supported, decompress DXTc to RGB/RGBA
uint32_t *pixels = nullptr;
bool transparent0 = format == GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
if ((ddsd.width & 3) != 0 || (ddsd.height & 3) != 0)
{
uint32_t nw = max(ddsd.width, 4u);
uint32_t nh = max(ddsd.height, 4u);
auto *tmp = decompressDXTc(nw, nh, format, transparent0, in);
if (tmp != nullptr)
{
pixels = new uint32_t[ddsd.width * ddsd.height];
// crop
for (uint32_t y = 0; y < ddsd.height; y++)
memcpy((char *)pixels + y * ddsd.width * 4, (char *)tmp + y * nw * 4, ddsd.width * 4);
delete[] tmp;
}
}
else
{
pixels = decompressDXTc(ddsd.width, ddsd.height, format, transparent0, in);
}
if (pixels == nullptr)
{
DPRINTF(LOG_LEVEL_ERROR, "Failed to decompress DDS texture file %s.\n", filename);
return nullptr;
}
if (transparent0)
{
// Remove the alpha channel for DXT1 since DXT1 textures
// are deemed not to contain alpha values in Celestia
// https://github.com/CelestiaProject/Celestia/pull/1086
char *ptr = reinterpret_cast<char*>(pixels);
uint32_t numberOfPixels = ddsd.width * ddsd.height;
for (uint32_t index = 0; index < numberOfPixels; ++index)
{
memcpy(&ptr[3 * index], &ptr[4 * index], sizeof(char) * 3);
}
}
Image *img = new Image(transparent0 ? PixelFormat::RGB : PixelFormat::RGBA, ddsd.width, ddsd.height);
memcpy(img->getPixels(), pixels, (transparent0 ? 3 : 4) * ddsd.width * ddsd.height);
delete[] pixels;
return img;
}
}
// TODO: Verify that the reported texture size matches the amount of
// data expected.
Image* img = new Image(static_cast<PixelFormat>(format),
(int) ddsd.width,
(int) ddsd.height,
max(ddsd.mipMapLevels, 1u));
in.read(reinterpret_cast<char*>(img->getPixels()), img->getSize());
if (!in.eof() && !in.good())
{
DPRINTF(LOG_LEVEL_ERROR, "Failed reading data from DDS texture file %s.\n", filename);
delete img;
return nullptr;
}
#if 0
cout << "sizeof(ddsd) = " << sizeof(ddsd) << '\n';
cout << "dimensions: " << ddsd.width << 'x' << ddsd.height << '\n';
cout << "mipmap levels: " << ddsd.mipMapLevels << '\n';
cout << "fourCC: " << ddsd.format.fourCC << '\n';
cout << "bpp: " << ddsd.format.bpp << '\n';
#endif
return img;
}