Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/modules/dds/image_saver_dds.cpp
10277 views
1
/**************************************************************************/
2
/* image_saver_dds.cpp */
3
/**************************************************************************/
4
/* This file is part of: */
5
/* GODOT ENGINE */
6
/* https://godotengine.org */
7
/**************************************************************************/
8
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10
/* */
11
/* Permission is hereby granted, free of charge, to any person obtaining */
12
/* a copy of this software and associated documentation files (the */
13
/* "Software"), to deal in the Software without restriction, including */
14
/* without limitation the rights to use, copy, modify, merge, publish, */
15
/* distribute, sublicense, and/or sell copies of the Software, and to */
16
/* permit persons to whom the Software is furnished to do so, subject to */
17
/* the following conditions: */
18
/* */
19
/* The above copyright notice and this permission notice shall be */
20
/* included in all copies or substantial portions of the Software. */
21
/* */
22
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29
/**************************************************************************/
30
31
#include "image_saver_dds.h"
32
33
#include "dds_enums.h"
34
35
#include "core/io/file_access.h"
36
#include "core/io/stream_peer.h"
37
38
Error save_dds(const String &p_path, const Ref<Image> &p_img) {
39
Vector<uint8_t> buffer = save_dds_buffer(p_img);
40
41
Ref<FileAccess> file = FileAccess::open(p_path, FileAccess::WRITE);
42
if (file.is_null()) {
43
return ERR_CANT_CREATE;
44
}
45
46
file->store_buffer(buffer.ptr(), buffer.size());
47
48
return OK;
49
}
50
51
enum DDSFormatType {
52
DDFT_BITMASK,
53
DDFT_FOURCC,
54
DDFT_DXGI,
55
};
56
57
DDSFormatType _dds_format_get_type(DDSFormat p_format) {
58
switch (p_format) {
59
case DDS_DXT1:
60
case DDS_DXT3:
61
case DDS_DXT5:
62
case DDS_ATI1:
63
case DDS_ATI2:
64
case DDS_R16F:
65
case DDS_RG16F:
66
case DDS_RGBA16F:
67
case DDS_R32F:
68
case DDS_RG32F:
69
case DDS_RGBA32F:
70
return DDFT_FOURCC;
71
72
case DDS_BC6S:
73
case DDS_BC6U:
74
case DDS_BC7:
75
case DDS_RGB9E5:
76
case DDS_RGB32F:
77
return DDFT_DXGI;
78
79
default:
80
return DDFT_BITMASK;
81
}
82
}
83
84
DDSFormat _image_format_to_dds_format(Image::Format p_image_format) {
85
switch (p_image_format) {
86
case Image::FORMAT_RGBAF: {
87
return DDS_RGBA32F;
88
}
89
case Image::FORMAT_RGBF: {
90
return DDS_RGB32F;
91
}
92
case Image::FORMAT_RGBAH: {
93
return DDS_RGBA16F;
94
}
95
case Image::FORMAT_RGF: {
96
return DDS_RG32F;
97
}
98
case Image::FORMAT_RGBA8: {
99
return DDS_RGBA8;
100
}
101
case Image::FORMAT_RGH: {
102
return DDS_RG16F;
103
}
104
case Image::FORMAT_RF: {
105
return DDS_R32F;
106
}
107
case Image::FORMAT_L8:
108
case Image::FORMAT_R8: {
109
return DDS_LUMINANCE;
110
}
111
case Image::FORMAT_RH: {
112
return DDS_R16F;
113
}
114
case Image::FORMAT_LA8:
115
case Image::FORMAT_RG8: {
116
return DDS_LUMINANCE_ALPHA;
117
}
118
case Image::FORMAT_RGBA4444: {
119
return DDS_BGRA4;
120
}
121
case Image::FORMAT_RGB565: {
122
return DDS_BGR565;
123
}
124
case Image::FORMAT_RGBE9995: {
125
return DDS_RGB9E5;
126
}
127
case Image::FORMAT_DXT1: {
128
return DDS_DXT1;
129
}
130
case Image::FORMAT_DXT3: {
131
return DDS_DXT3;
132
}
133
case Image::FORMAT_DXT5: {
134
return DDS_DXT5;
135
}
136
case Image::FORMAT_RGTC_R: {
137
return DDS_ATI1;
138
}
139
case Image::FORMAT_RGTC_RG: {
140
return DDS_ATI2;
141
}
142
case Image::FORMAT_RGB8: {
143
return DDS_RGB8;
144
}
145
case Image::FORMAT_BPTC_RGBFU: {
146
return DDS_BC6U;
147
}
148
case Image::FORMAT_BPTC_RGBF: {
149
return DDS_BC6S;
150
}
151
case Image::FORMAT_BPTC_RGBA: {
152
return DDS_BC7;
153
}
154
default: {
155
return DDS_MAX;
156
}
157
}
158
}
159
160
uint32_t _image_format_to_fourcc_format(Image::Format p_format) {
161
switch (p_format) {
162
case Image::FORMAT_DXT1:
163
return DDFCC_DXT1;
164
case Image::FORMAT_DXT3:
165
return DDFCC_DXT3;
166
case Image::FORMAT_DXT5:
167
return DDFCC_DXT5;
168
case Image::FORMAT_RGTC_R:
169
return DDFCC_ATI1;
170
case Image::FORMAT_RGTC_RG:
171
return DDFCC_ATI2;
172
case Image::FORMAT_RF:
173
return DDFCC_R32F;
174
case Image::FORMAT_RGF:
175
return DDFCC_RG32F;
176
case Image::FORMAT_RGBAF:
177
return DDFCC_RGBA32F;
178
case Image::FORMAT_RH:
179
return DDFCC_R16F;
180
case Image::FORMAT_RGH:
181
return DDFCC_RG16F;
182
case Image::FORMAT_RGBAH:
183
return DDFCC_RGBA16F;
184
185
default:
186
return 0;
187
}
188
}
189
190
uint32_t _image_format_to_dxgi_format(Image::Format p_format) {
191
switch (p_format) {
192
case Image::FORMAT_DXT1:
193
return DXGI_BC1_UNORM;
194
case Image::FORMAT_DXT3:
195
return DXGI_BC2_UNORM;
196
case Image::FORMAT_DXT5:
197
return DXGI_BC3_UNORM;
198
case Image::FORMAT_RGTC_R:
199
return DXGI_BC4_UNORM;
200
case Image::FORMAT_RGTC_RG:
201
return DXGI_BC5_UNORM;
202
case Image::FORMAT_BPTC_RGBFU:
203
return DXGI_BC6H_UF16;
204
case Image::FORMAT_BPTC_RGBF:
205
return DXGI_BC6H_SF16;
206
case Image::FORMAT_BPTC_RGBA:
207
return DXGI_BC7_UNORM;
208
case Image::FORMAT_RF:
209
return DXGI_R32_FLOAT;
210
case Image::FORMAT_RGF:
211
return DXGI_R32G32_FLOAT;
212
case Image::FORMAT_RGBF:
213
return DXGI_R32G32B32_FLOAT;
214
case Image::FORMAT_RGBAF:
215
return DXGI_R32G32B32A32_FLOAT;
216
case Image::FORMAT_RH:
217
return DXGI_R16_FLOAT;
218
case Image::FORMAT_RGH:
219
return DXGI_R16G16_FLOAT;
220
case Image::FORMAT_RGBAH:
221
return DXGI_R16G16B16A16_FLOAT;
222
case Image::FORMAT_RGBE9995:
223
return DXGI_R9G9B9E5;
224
225
default:
226
return 0;
227
}
228
}
229
230
void _get_dds_pixel_bitmask(Image::Format p_format, uint32_t &r_bit_count, uint32_t &r_red_mask, uint32_t &r_green_mask, uint32_t &r_blue_mask, uint32_t &r_alpha_mask) {
231
switch (p_format) {
232
case Image::FORMAT_R8:
233
case Image::FORMAT_L8: {
234
r_bit_count = 8;
235
r_red_mask = 0xff;
236
r_green_mask = 0;
237
r_blue_mask = 0;
238
r_alpha_mask = 0;
239
} break;
240
case Image::FORMAT_RG8:
241
case Image::FORMAT_LA8: {
242
r_bit_count = 16;
243
r_red_mask = 0xff;
244
r_green_mask = 0;
245
r_blue_mask = 0;
246
r_alpha_mask = 0xff00;
247
} break;
248
case Image::FORMAT_RGB8: {
249
// BGR8
250
r_bit_count = 24;
251
r_red_mask = 0xff0000;
252
r_green_mask = 0xff00;
253
r_blue_mask = 0xff;
254
r_alpha_mask = 0;
255
} break;
256
case Image::FORMAT_RGBA8: {
257
r_bit_count = 32;
258
r_red_mask = 0xff;
259
r_green_mask = 0xff00;
260
r_blue_mask = 0xff0000;
261
r_alpha_mask = 0xff000000;
262
} break;
263
case Image::FORMAT_RGBA4444: {
264
// BGRA4444
265
r_bit_count = 16;
266
r_red_mask = 0xf00;
267
r_green_mask = 0xf0;
268
r_blue_mask = 0xf;
269
r_alpha_mask = 0xf000;
270
} break;
271
case Image::FORMAT_RGB565: {
272
// BGR565
273
r_bit_count = 16;
274
r_red_mask = 0xf800;
275
r_green_mask = 0x7e0;
276
r_blue_mask = 0x1f;
277
r_alpha_mask = 0;
278
} break;
279
280
default: {
281
r_bit_count = 0;
282
r_red_mask = 0;
283
r_green_mask = 0;
284
r_blue_mask = 0;
285
r_alpha_mask = 0;
286
} break;
287
}
288
}
289
290
Vector<uint8_t> save_dds_buffer(const Ref<Image> &p_img) {
291
Ref<StreamPeerBuffer> stream_buffer;
292
stream_buffer.instantiate();
293
294
Ref<Image> image = p_img;
295
296
stream_buffer->put_32(DDS_MAGIC);
297
stream_buffer->put_32(DDS_HEADER_SIZE);
298
299
uint32_t flags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT | DDSD_PITCH | DDSD_LINEARSIZE;
300
301
if (image->has_mipmaps()) {
302
flags |= DDSD_MIPMAPCOUNT;
303
}
304
305
stream_buffer->put_32(flags);
306
307
uint32_t height = image->get_height();
308
stream_buffer->put_32(height);
309
310
uint32_t width = image->get_width();
311
stream_buffer->put_32(width);
312
313
DDSFormat dds_format = _image_format_to_dds_format(image->get_format());
314
const DDSFormatInfo &info = dds_format_info[dds_format];
315
316
uint32_t depth = 1; // Default depth for 2D textures
317
318
uint32_t pitch;
319
if (info.compressed) {
320
pitch = ((MAX(info.divisor, width) + info.divisor - 1) / info.divisor) * ((MAX(info.divisor, height) + info.divisor - 1) / info.divisor) * info.block_size;
321
} else {
322
pitch = width * info.block_size;
323
}
324
325
stream_buffer->put_32(pitch);
326
stream_buffer->put_32(depth);
327
328
uint32_t mipmaps = image->get_mipmap_count() + 1;
329
stream_buffer->put_32(mipmaps);
330
331
uint32_t reserved = 0;
332
for (int i = 0; i < 11; i++) {
333
stream_buffer->put_32(reserved);
334
}
335
336
stream_buffer->put_32(DDS_PIXELFORMAT_SIZE);
337
338
uint32_t pf_flags = 0;
339
340
DDSFormatType format_type = _dds_format_get_type(dds_format);
341
342
if (format_type == DDFT_BITMASK) {
343
pf_flags = DDPF_RGB;
344
345
if (image->get_format() == Image::FORMAT_LA8 || image->get_format() == Image::FORMAT_RG8 || image->get_format() == Image::FORMAT_RGBA8 || image->get_format() == Image::FORMAT_RGBA4444) {
346
pf_flags |= DDPF_ALPHAPIXELS;
347
}
348
} else {
349
pf_flags = DDPF_FOURCC;
350
}
351
352
stream_buffer->put_32(pf_flags);
353
354
bool needs_pixeldata_swap = false;
355
356
if (format_type == DDFT_BITMASK) {
357
// Uncompressed bitmasked.
358
stream_buffer->put_32(0); // FourCC
359
360
uint32_t bit_count, r_mask, g_mask, b_mask, a_mask;
361
_get_dds_pixel_bitmask(image->get_format(), bit_count, r_mask, g_mask, b_mask, a_mask);
362
363
stream_buffer->put_32(bit_count);
364
stream_buffer->put_32(r_mask);
365
stream_buffer->put_32(g_mask);
366
stream_buffer->put_32(b_mask);
367
stream_buffer->put_32(a_mask);
368
369
if (image->get_format() == Image::FORMAT_RGBA4444 || image->get_format() == Image::FORMAT_RGB8) {
370
needs_pixeldata_swap = true;
371
}
372
} else if (format_type == DDFT_FOURCC) {
373
// FourCC.
374
uint32_t fourcc = _image_format_to_fourcc_format(image->get_format());
375
stream_buffer->put_32(fourcc);
376
377
stream_buffer->put_32(0); // Bit count
378
stream_buffer->put_32(0); // R Bitmask
379
stream_buffer->put_32(0); // G Bitmask
380
stream_buffer->put_32(0); // B Bitmask
381
stream_buffer->put_32(0); // A Bitmask
382
} else {
383
// DXGI format and DX10 header.
384
stream_buffer->put_32(DDFCC_DX10);
385
386
stream_buffer->put_32(0); // Bit count
387
stream_buffer->put_32(0); // R Bitmask
388
stream_buffer->put_32(0); // G Bitmask
389
stream_buffer->put_32(0); // B Bitmask
390
stream_buffer->put_32(0); // A Bitmask
391
}
392
393
uint32_t caps1 = info.compressed ? DDSD_LINEARSIZE : DDSD_PITCH;
394
stream_buffer->put_32(caps1);
395
396
stream_buffer->put_32(0); // Caps2
397
stream_buffer->put_32(0); // Caps3
398
stream_buffer->put_32(0); // Caps4
399
stream_buffer->put_32(0); // Reserved 2
400
401
if (format_type == DDFT_DXGI) {
402
// DX10 header.
403
uint32_t dxgi_format = _image_format_to_dxgi_format(image->get_format());
404
stream_buffer->put_32(dxgi_format);
405
stream_buffer->put_32(DX10D_2D);
406
stream_buffer->put_32(0); // Misc flags 1
407
stream_buffer->put_32(1); // Array size
408
stream_buffer->put_32(0); // Misc flags 2
409
}
410
411
for (uint32_t mip_i = 0; mip_i < mipmaps; mip_i++) {
412
uint32_t mip_width = MAX(1u, width >> mip_i);
413
uint32_t mip_height = MAX(1u, height >> mip_i);
414
415
uint32_t expected_size = 0;
416
if (info.compressed) {
417
uint32_t blocks_x = (mip_width + info.divisor - 1) / info.divisor;
418
uint32_t blocks_y = (mip_height + info.divisor - 1) / info.divisor;
419
expected_size = blocks_x * blocks_y * info.block_size;
420
} else {
421
expected_size = mip_width * mip_height * info.block_size;
422
}
423
424
if (needs_pixeldata_swap) {
425
// The image's channels need to be swapped.
426
Ref<Image> mip_image = image->get_image_from_mipmap(mip_i);
427
Vector<uint8_t> data = mip_image->get_data();
428
429
ERR_FAIL_COND_V_MSG(data.size() != expected_size, Vector<uint8_t>(),
430
"Image data size mismatch for mipmap level " + itos(mip_i) +
431
". Expected size: " + itos(expected_size) + ", actual size: " + itos(data.size()) + ".");
432
433
if (mip_image->get_format() == Image::FORMAT_RGBA4444) {
434
// RGBA4 to BGRA4
435
const int64_t data_size = data.size();
436
uint8_t *wb = data.ptrw();
437
438
for (int64_t data_i = 0; data_i < data_size; data_i += 2) {
439
uint8_t ar = wb[data_i + 0];
440
uint8_t gb = wb[data_i + 1];
441
442
wb[data_i + 1] = ((ar & 0x0F) << 4) | ((gb & 0xF0) >> 4);
443
wb[data_i + 0] = ((ar & 0xF0) >> 4) | ((gb & 0x0F) << 4);
444
}
445
} else if (mip_image->get_format() == Image::FORMAT_RGB8) {
446
// RGB8 to BGR8
447
const int64_t data_size = data.size();
448
uint8_t *wb = data.ptrw();
449
450
for (int64_t data_i = 0; data_i < data_size; data_i += 3) {
451
SWAP(wb[data_i], wb[data_i + 2]);
452
}
453
}
454
455
stream_buffer->put_data(data.ptr(), data.size());
456
} else {
457
int64_t ofs, size;
458
459
image->get_mipmap_offset_and_size(mip_i, ofs, size);
460
461
ERR_FAIL_COND_V_MSG(size != expected_size, Vector<uint8_t>(),
462
"Image data size mismatch for mipmap level " + itos(mip_i) +
463
". Expected size: " + itos(expected_size) + ", actual size: " + itos(size) + ".");
464
465
stream_buffer->put_data(image->ptr() + ofs, size);
466
}
467
}
468
469
return stream_buffer->get_data_array();
470
}
471
472