Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/modules/dds/image_saver_dds.cpp
11352 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
case DDS_RGBA16:
71
return DDFT_FOURCC;
72
73
case DDS_BC6S:
74
case DDS_BC6U:
75
case DDS_BC7:
76
case DDS_RGB9E5:
77
case DDS_RGB32F:
78
case DDS_R16I:
79
case DDS_RG16I:
80
case DDS_RGBA16I:
81
return DDFT_DXGI;
82
83
default:
84
return DDFT_BITMASK;
85
}
86
}
87
88
DDSFormat _image_format_to_dds_format(Image::Format p_image_format) {
89
switch (p_image_format) {
90
case Image::FORMAT_RGBAF: {
91
return DDS_RGBA32F;
92
}
93
case Image::FORMAT_RGBF: {
94
return DDS_RGB32F;
95
}
96
case Image::FORMAT_RGBAH: {
97
return DDS_RGBA16F;
98
}
99
case Image::FORMAT_RGF: {
100
return DDS_RG32F;
101
}
102
case Image::FORMAT_RGBA8: {
103
return DDS_RGBA8;
104
}
105
case Image::FORMAT_RGH: {
106
return DDS_RG16F;
107
}
108
case Image::FORMAT_RF: {
109
return DDS_R32F;
110
}
111
case Image::FORMAT_L8:
112
case Image::FORMAT_R8: {
113
return DDS_LUMINANCE;
114
}
115
case Image::FORMAT_RH: {
116
return DDS_R16F;
117
}
118
case Image::FORMAT_LA8:
119
case Image::FORMAT_RG8: {
120
return DDS_LUMINANCE_ALPHA;
121
}
122
case Image::FORMAT_RGBA4444: {
123
return DDS_BGRA4;
124
}
125
case Image::FORMAT_RGB565: {
126
return DDS_BGR565;
127
}
128
case Image::FORMAT_RGBE9995: {
129
return DDS_RGB9E5;
130
}
131
case Image::FORMAT_DXT1: {
132
return DDS_DXT1;
133
}
134
case Image::FORMAT_DXT3: {
135
return DDS_DXT3;
136
}
137
case Image::FORMAT_DXT5: {
138
return DDS_DXT5;
139
}
140
case Image::FORMAT_RGTC_R: {
141
return DDS_ATI1;
142
}
143
case Image::FORMAT_RGTC_RG: {
144
return DDS_ATI2;
145
}
146
case Image::FORMAT_RGB8: {
147
return DDS_RGB8;
148
}
149
case Image::FORMAT_BPTC_RGBFU: {
150
return DDS_BC6U;
151
}
152
case Image::FORMAT_BPTC_RGBF: {
153
return DDS_BC6S;
154
}
155
case Image::FORMAT_BPTC_RGBA: {
156
return DDS_BC7;
157
}
158
case Image::FORMAT_R16: {
159
return DDS_R16;
160
}
161
case Image::FORMAT_RG16: {
162
return DDS_RG16;
163
}
164
case Image::FORMAT_RGBA16: {
165
return DDS_RGBA16;
166
}
167
case Image::FORMAT_R16I: {
168
return DDS_R16I;
169
}
170
case Image::FORMAT_RG16I: {
171
return DDS_RG16I;
172
}
173
case Image::FORMAT_RGBA16I: {
174
return DDS_RGBA16I;
175
}
176
default: {
177
return DDS_MAX;
178
}
179
}
180
}
181
182
uint32_t _image_format_to_fourcc_format(Image::Format p_format) {
183
switch (p_format) {
184
case Image::FORMAT_DXT1:
185
return DDFCC_DXT1;
186
case Image::FORMAT_DXT3:
187
return DDFCC_DXT3;
188
case Image::FORMAT_DXT5:
189
return DDFCC_DXT5;
190
case Image::FORMAT_RGTC_R:
191
return DDFCC_ATI1;
192
case Image::FORMAT_RGTC_RG:
193
return DDFCC_ATI2;
194
case Image::FORMAT_RF:
195
return DDFCC_R32F;
196
case Image::FORMAT_RGF:
197
return DDFCC_RG32F;
198
case Image::FORMAT_RGBAF:
199
return DDFCC_RGBA32F;
200
case Image::FORMAT_RH:
201
return DDFCC_R16F;
202
case Image::FORMAT_RGH:
203
return DDFCC_RG16F;
204
case Image::FORMAT_RGBAH:
205
return DDFCC_RGBA16F;
206
case Image::FORMAT_RGBA16:
207
return DDFCC_RGBA16;
208
209
default:
210
return 0;
211
}
212
}
213
214
uint32_t _image_format_to_dxgi_format(Image::Format p_format) {
215
switch (p_format) {
216
case Image::FORMAT_DXT1:
217
return DXGI_BC1_UNORM;
218
case Image::FORMAT_DXT3:
219
return DXGI_BC2_UNORM;
220
case Image::FORMAT_DXT5:
221
return DXGI_BC3_UNORM;
222
case Image::FORMAT_RGTC_R:
223
return DXGI_BC4_UNORM;
224
case Image::FORMAT_RGTC_RG:
225
return DXGI_BC5_UNORM;
226
case Image::FORMAT_BPTC_RGBFU:
227
return DXGI_BC6H_UF16;
228
case Image::FORMAT_BPTC_RGBF:
229
return DXGI_BC6H_SF16;
230
case Image::FORMAT_BPTC_RGBA:
231
return DXGI_BC7_UNORM;
232
case Image::FORMAT_RF:
233
return DXGI_R32_FLOAT;
234
case Image::FORMAT_RGF:
235
return DXGI_R32G32_FLOAT;
236
case Image::FORMAT_RGBF:
237
return DXGI_R32G32B32_FLOAT;
238
case Image::FORMAT_RGBAF:
239
return DXGI_R32G32B32A32_FLOAT;
240
case Image::FORMAT_RH:
241
return DXGI_R16_FLOAT;
242
case Image::FORMAT_RGH:
243
return DXGI_R16G16_FLOAT;
244
case Image::FORMAT_RGBAH:
245
return DXGI_R16G16B16A16_FLOAT;
246
case Image::FORMAT_RGBE9995:
247
return DXGI_R9G9B9E5;
248
case Image::FORMAT_R16:
249
return DXGI_R16_UNORM;
250
case Image::FORMAT_RG16:
251
return DXGI_R16G16_UNORM;
252
case Image::FORMAT_RGBA16:
253
return DXGI_R16G16B16A16_UNORM;
254
case Image::FORMAT_R16I:
255
return DXGI_R16_UINT;
256
case Image::FORMAT_RG16I:
257
return DXGI_R16G16_UINT;
258
case Image::FORMAT_RGBA16I:
259
return DXGI_R16G16B16A16_UINT;
260
261
default:
262
return 0;
263
}
264
}
265
266
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) {
267
switch (p_format) {
268
case Image::FORMAT_R8:
269
case Image::FORMAT_L8: {
270
r_bit_count = 8;
271
r_red_mask = 0xff;
272
r_green_mask = 0;
273
r_blue_mask = 0;
274
r_alpha_mask = 0;
275
} break;
276
case Image::FORMAT_RG8:
277
case Image::FORMAT_LA8: {
278
r_bit_count = 16;
279
r_red_mask = 0xff;
280
r_green_mask = 0;
281
r_blue_mask = 0;
282
r_alpha_mask = 0xff00;
283
} break;
284
case Image::FORMAT_RGB8: {
285
// BGR8
286
r_bit_count = 24;
287
r_red_mask = 0xff0000;
288
r_green_mask = 0xff00;
289
r_blue_mask = 0xff;
290
r_alpha_mask = 0;
291
} break;
292
case Image::FORMAT_RGBA8: {
293
r_bit_count = 32;
294
r_red_mask = 0xff;
295
r_green_mask = 0xff00;
296
r_blue_mask = 0xff0000;
297
r_alpha_mask = 0xff000000;
298
} break;
299
case Image::FORMAT_RGBA4444: {
300
// BGRA4444
301
r_bit_count = 16;
302
r_red_mask = 0xf00;
303
r_green_mask = 0xf0;
304
r_blue_mask = 0xf;
305
r_alpha_mask = 0xf000;
306
} break;
307
case Image::FORMAT_RGB565: {
308
// BGR565
309
r_bit_count = 16;
310
r_red_mask = 0xf800;
311
r_green_mask = 0x7e0;
312
r_blue_mask = 0x1f;
313
r_alpha_mask = 0;
314
} break;
315
case Image::FORMAT_R16: {
316
r_bit_count = 16;
317
r_red_mask = 0xffff;
318
r_green_mask = 0;
319
r_blue_mask = 0;
320
r_alpha_mask = 0;
321
} break;
322
case Image::FORMAT_RG16: {
323
r_bit_count = 32;
324
r_red_mask = 0xffff;
325
r_green_mask = 0xffff0000;
326
r_blue_mask = 0;
327
r_alpha_mask = 0;
328
} break;
329
330
default: {
331
r_bit_count = 0;
332
r_red_mask = 0;
333
r_green_mask = 0;
334
r_blue_mask = 0;
335
r_alpha_mask = 0;
336
} break;
337
}
338
}
339
340
Vector<uint8_t> save_dds_buffer(const Ref<Image> &p_img) {
341
Ref<StreamPeerBuffer> stream_buffer;
342
stream_buffer.instantiate();
343
344
Ref<Image> image = p_img;
345
346
stream_buffer->put_32(DDS_MAGIC);
347
stream_buffer->put_32(DDS_HEADER_SIZE);
348
349
uint32_t flags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT | DDSD_PITCH | DDSD_LINEARSIZE;
350
351
if (image->has_mipmaps()) {
352
flags |= DDSD_MIPMAPCOUNT;
353
}
354
355
stream_buffer->put_32(flags);
356
357
uint32_t height = image->get_height();
358
stream_buffer->put_32(height);
359
360
uint32_t width = image->get_width();
361
stream_buffer->put_32(width);
362
363
DDSFormat dds_format = _image_format_to_dds_format(image->get_format());
364
const DDSFormatInfo &info = dds_format_info[dds_format];
365
366
uint32_t depth = 1; // Default depth for 2D textures
367
368
uint32_t pitch;
369
if (info.compressed) {
370
pitch = ((MAX(info.divisor, width) + info.divisor - 1) / info.divisor) * ((MAX(info.divisor, height) + info.divisor - 1) / info.divisor) * info.block_size;
371
} else {
372
pitch = width * info.block_size;
373
}
374
375
stream_buffer->put_32(pitch);
376
stream_buffer->put_32(depth);
377
378
uint32_t mipmaps = image->get_mipmap_count() + 1;
379
stream_buffer->put_32(mipmaps);
380
381
uint32_t reserved = 0;
382
for (int i = 0; i < 11; i++) {
383
stream_buffer->put_32(reserved);
384
}
385
386
stream_buffer->put_32(DDS_PIXELFORMAT_SIZE);
387
388
uint32_t pf_flags = 0;
389
390
DDSFormatType format_type = _dds_format_get_type(dds_format);
391
392
if (format_type == DDFT_BITMASK) {
393
pf_flags = DDPF_RGB;
394
395
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) {
396
pf_flags |= DDPF_ALPHAPIXELS;
397
}
398
} else {
399
pf_flags = DDPF_FOURCC;
400
}
401
402
stream_buffer->put_32(pf_flags);
403
404
bool needs_pixeldata_swap = false;
405
406
if (format_type == DDFT_BITMASK) {
407
// Uncompressed bitmasked.
408
stream_buffer->put_32(0); // FourCC
409
410
uint32_t bit_count, r_mask, g_mask, b_mask, a_mask;
411
_get_dds_pixel_bitmask(image->get_format(), bit_count, r_mask, g_mask, b_mask, a_mask);
412
413
stream_buffer->put_32(bit_count);
414
stream_buffer->put_32(r_mask);
415
stream_buffer->put_32(g_mask);
416
stream_buffer->put_32(b_mask);
417
stream_buffer->put_32(a_mask);
418
419
if (image->get_format() == Image::FORMAT_RGBA4444 || image->get_format() == Image::FORMAT_RGB8) {
420
needs_pixeldata_swap = true;
421
}
422
} else if (format_type == DDFT_FOURCC) {
423
// FourCC.
424
uint32_t fourcc = _image_format_to_fourcc_format(image->get_format());
425
stream_buffer->put_32(fourcc);
426
427
stream_buffer->put_32(0); // Bit count
428
stream_buffer->put_32(0); // R Bitmask
429
stream_buffer->put_32(0); // G Bitmask
430
stream_buffer->put_32(0); // B Bitmask
431
stream_buffer->put_32(0); // A Bitmask
432
} else {
433
// DXGI format and DX10 header.
434
stream_buffer->put_32(DDFCC_DX10);
435
436
stream_buffer->put_32(0); // Bit count
437
stream_buffer->put_32(0); // R Bitmask
438
stream_buffer->put_32(0); // G Bitmask
439
stream_buffer->put_32(0); // B Bitmask
440
stream_buffer->put_32(0); // A Bitmask
441
}
442
443
uint32_t caps1 = info.compressed ? DDSD_LINEARSIZE : DDSD_PITCH;
444
stream_buffer->put_32(caps1);
445
446
stream_buffer->put_32(0); // Caps2
447
stream_buffer->put_32(0); // Caps3
448
stream_buffer->put_32(0); // Caps4
449
stream_buffer->put_32(0); // Reserved 2
450
451
if (format_type == DDFT_DXGI) {
452
// DX10 header.
453
uint32_t dxgi_format = _image_format_to_dxgi_format(image->get_format());
454
stream_buffer->put_32(dxgi_format);
455
stream_buffer->put_32(DX10D_2D);
456
stream_buffer->put_32(0); // Misc flags 1
457
stream_buffer->put_32(1); // Array size
458
stream_buffer->put_32(0); // Misc flags 2
459
}
460
461
for (uint32_t mip_i = 0; mip_i < mipmaps; mip_i++) {
462
uint32_t mip_width = MAX(1u, width >> mip_i);
463
uint32_t mip_height = MAX(1u, height >> mip_i);
464
465
uint32_t expected_size = 0;
466
if (info.compressed) {
467
uint32_t blocks_x = (mip_width + info.divisor - 1) / info.divisor;
468
uint32_t blocks_y = (mip_height + info.divisor - 1) / info.divisor;
469
expected_size = blocks_x * blocks_y * info.block_size;
470
} else {
471
expected_size = mip_width * mip_height * info.block_size;
472
}
473
474
if (needs_pixeldata_swap) {
475
// The image's channels need to be swapped.
476
Ref<Image> mip_image = image->get_image_from_mipmap(mip_i);
477
Vector<uint8_t> data = mip_image->get_data();
478
479
ERR_FAIL_COND_V_MSG(data.size() != expected_size, Vector<uint8_t>(),
480
"Image data size mismatch for mipmap level " + itos(mip_i) +
481
". Expected size: " + itos(expected_size) + ", actual size: " + itos(data.size()) + ".");
482
483
if (mip_image->get_format() == Image::FORMAT_RGBA4444) {
484
// RGBA4 to BGRA4
485
const int64_t data_size = data.size();
486
uint8_t *wb = data.ptrw();
487
488
for (int64_t data_i = 0; data_i < data_size; data_i += 2) {
489
uint8_t ar = wb[data_i + 0];
490
uint8_t gb = wb[data_i + 1];
491
492
wb[data_i + 1] = ((ar & 0x0F) << 4) | ((gb & 0xF0) >> 4);
493
wb[data_i + 0] = ((ar & 0xF0) >> 4) | ((gb & 0x0F) << 4);
494
}
495
} else if (mip_image->get_format() == Image::FORMAT_RGB8) {
496
// RGB8 to BGR8
497
const int64_t data_size = data.size();
498
uint8_t *wb = data.ptrw();
499
500
for (int64_t data_i = 0; data_i < data_size; data_i += 3) {
501
SWAP(wb[data_i], wb[data_i + 2]);
502
}
503
}
504
505
stream_buffer->put_data(data.ptr(), data.size());
506
} else {
507
int64_t ofs, size;
508
509
image->get_mipmap_offset_and_size(mip_i, ofs, size);
510
511
ERR_FAIL_COND_V_MSG(size != expected_size, Vector<uint8_t>(),
512
"Image data size mismatch for mipmap level " + itos(mip_i) +
513
". Expected size: " + itos(expected_size) + ", actual size: " + itos(size) + ".");
514
515
stream_buffer->put_data(image->ptr() + ofs, size);
516
}
517
}
518
519
return stream_buffer->get_data_array();
520
}
521
522