-
-
Notifications
You must be signed in to change notification settings - Fork 24.6k
Expand file tree
/
Copy pathimage_saver_dds.cpp
More file actions
520 lines (459 loc) · 14 KB
/
image_saver_dds.cpp
File metadata and controls
520 lines (459 loc) · 14 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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
/**************************************************************************/
/* image_saver_dds.cpp */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#include "image_saver_dds.h"
#include "dds_enums.h"
#include "core/io/file_access.h"
#include "core/io/stream_peer.h"
Error save_dds(const String &p_path, const Ref<Image> &p_img) {
Vector<uint8_t> buffer = save_dds_buffer(p_img);
Ref<FileAccess> file = FileAccess::open(p_path, FileAccess::WRITE);
if (file.is_null()) {
return ERR_CANT_CREATE;
}
file->store_buffer(buffer.ptr(), buffer.size());
return OK;
}
enum DDSFormatType {
DDFT_BITMASK,
DDFT_FOURCC,
DDFT_DXGI,
};
DDSFormatType _dds_format_get_type(DDSFormat p_format) {
switch (p_format) {
case DDS_DXT1:
case DDS_DXT3:
case DDS_DXT5:
case DDS_ATI1:
case DDS_ATI2:
case DDS_R16F:
case DDS_RG16F:
case DDS_RGBA16F:
case DDS_R32F:
case DDS_RG32F:
case DDS_RGBA32F:
case DDS_RGBA16:
return DDFT_FOURCC;
case DDS_BC6S:
case DDS_BC6U:
case DDS_BC7:
case DDS_RGB9E5:
case DDS_RGB32F:
case DDS_R16I:
case DDS_RG16I:
case DDS_RGBA16I:
return DDFT_DXGI;
default:
return DDFT_BITMASK;
}
}
DDSFormat _image_format_to_dds_format(Image::Format p_image_format) {
switch (p_image_format) {
case Image::FORMAT_RGBAF: {
return DDS_RGBA32F;
}
case Image::FORMAT_RGBF: {
return DDS_RGB32F;
}
case Image::FORMAT_RGBAH: {
return DDS_RGBA16F;
}
case Image::FORMAT_RGF: {
return DDS_RG32F;
}
case Image::FORMAT_RGBA8: {
return DDS_RGBA8;
}
case Image::FORMAT_RGH: {
return DDS_RG16F;
}
case Image::FORMAT_RF: {
return DDS_R32F;
}
case Image::FORMAT_L8:
case Image::FORMAT_R8: {
return DDS_LUMINANCE;
}
case Image::FORMAT_RH: {
return DDS_R16F;
}
case Image::FORMAT_LA8:
case Image::FORMAT_RG8: {
return DDS_LUMINANCE_ALPHA;
}
case Image::FORMAT_RGBA4444: {
return DDS_BGRA4;
}
case Image::FORMAT_RGB565: {
return DDS_BGR565;
}
case Image::FORMAT_RGBE9995: {
return DDS_RGB9E5;
}
case Image::FORMAT_DXT1: {
return DDS_DXT1;
}
case Image::FORMAT_DXT3: {
return DDS_DXT3;
}
case Image::FORMAT_DXT5: {
return DDS_DXT5;
}
case Image::FORMAT_RGTC_R: {
return DDS_ATI1;
}
case Image::FORMAT_RGTC_RG: {
return DDS_ATI2;
}
case Image::FORMAT_RGB8: {
return DDS_RGB8;
}
case Image::FORMAT_BPTC_RGBFU: {
return DDS_BC6U;
}
case Image::FORMAT_BPTC_RGBF: {
return DDS_BC6S;
}
case Image::FORMAT_BPTC_RGBA: {
return DDS_BC7;
}
case Image::FORMAT_R16: {
return DDS_R16;
}
case Image::FORMAT_RG16: {
return DDS_RG16;
}
case Image::FORMAT_RGBA16: {
return DDS_RGBA16;
}
case Image::FORMAT_R16I: {
return DDS_R16I;
}
case Image::FORMAT_RG16I: {
return DDS_RG16I;
}
case Image::FORMAT_RGBA16I: {
return DDS_RGBA16I;
}
default: {
return DDS_MAX;
}
}
}
uint32_t _image_format_to_fourcc_format(Image::Format p_format) {
switch (p_format) {
case Image::FORMAT_DXT1:
return DDFCC_DXT1;
case Image::FORMAT_DXT3:
return DDFCC_DXT3;
case Image::FORMAT_DXT5:
return DDFCC_DXT5;
case Image::FORMAT_RGTC_R:
return DDFCC_ATI1;
case Image::FORMAT_RGTC_RG:
return DDFCC_ATI2;
case Image::FORMAT_RF:
return DDFCC_R32F;
case Image::FORMAT_RGF:
return DDFCC_RG32F;
case Image::FORMAT_RGBAF:
return DDFCC_RGBA32F;
case Image::FORMAT_RH:
return DDFCC_R16F;
case Image::FORMAT_RGH:
return DDFCC_RG16F;
case Image::FORMAT_RGBAH:
return DDFCC_RGBA16F;
case Image::FORMAT_RGBA16:
return DDFCC_RGBA16;
default:
return 0;
}
}
uint32_t _image_format_to_dxgi_format(Image::Format p_format) {
switch (p_format) {
case Image::FORMAT_DXT1:
return DXGI_BC1_UNORM;
case Image::FORMAT_DXT3:
return DXGI_BC2_UNORM;
case Image::FORMAT_DXT5:
return DXGI_BC3_UNORM;
case Image::FORMAT_RGTC_R:
return DXGI_BC4_UNORM;
case Image::FORMAT_RGTC_RG:
return DXGI_BC5_UNORM;
case Image::FORMAT_BPTC_RGBFU:
return DXGI_BC6H_UF16;
case Image::FORMAT_BPTC_RGBF:
return DXGI_BC6H_SF16;
case Image::FORMAT_BPTC_RGBA:
return DXGI_BC7_UNORM;
case Image::FORMAT_RF:
return DXGI_R32_FLOAT;
case Image::FORMAT_RGF:
return DXGI_R32G32_FLOAT;
case Image::FORMAT_RGBF:
return DXGI_R32G32B32_FLOAT;
case Image::FORMAT_RGBAF:
return DXGI_R32G32B32A32_FLOAT;
case Image::FORMAT_RH:
return DXGI_R16_FLOAT;
case Image::FORMAT_RGH:
return DXGI_R16G16_FLOAT;
case Image::FORMAT_RGBAH:
return DXGI_R16G16B16A16_FLOAT;
case Image::FORMAT_RGBE9995:
return DXGI_R9G9B9E5;
case Image::FORMAT_R16:
return DXGI_R16_UNORM;
case Image::FORMAT_RG16:
return DXGI_R16G16_UNORM;
case Image::FORMAT_RGBA16:
return DXGI_R16G16B16A16_UNORM;
case Image::FORMAT_R16I:
return DXGI_R16_UINT;
case Image::FORMAT_RG16I:
return DXGI_R16G16_UINT;
case Image::FORMAT_RGBA16I:
return DXGI_R16G16B16A16_UINT;
default:
return 0;
}
}
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) {
switch (p_format) {
case Image::FORMAT_R8:
case Image::FORMAT_L8: {
r_bit_count = 8;
r_red_mask = 0xff;
r_green_mask = 0;
r_blue_mask = 0;
r_alpha_mask = 0;
} break;
case Image::FORMAT_RG8:
case Image::FORMAT_LA8: {
r_bit_count = 16;
r_red_mask = 0xff;
r_green_mask = 0;
r_blue_mask = 0;
r_alpha_mask = 0xff00;
} break;
case Image::FORMAT_RGB8: {
// BGR8
r_bit_count = 24;
r_red_mask = 0xff0000;
r_green_mask = 0xff00;
r_blue_mask = 0xff;
r_alpha_mask = 0;
} break;
case Image::FORMAT_RGBA8: {
r_bit_count = 32;
r_red_mask = 0xff;
r_green_mask = 0xff00;
r_blue_mask = 0xff0000;
r_alpha_mask = 0xff000000;
} break;
case Image::FORMAT_RGBA4444: {
// BGRA4444
r_bit_count = 16;
r_red_mask = 0xf00;
r_green_mask = 0xf0;
r_blue_mask = 0xf;
r_alpha_mask = 0xf000;
} break;
case Image::FORMAT_RGB565: {
// BGR565
r_bit_count = 16;
r_red_mask = 0xf800;
r_green_mask = 0x7e0;
r_blue_mask = 0x1f;
r_alpha_mask = 0;
} break;
case Image::FORMAT_R16: {
r_bit_count = 16;
r_red_mask = 0xffff;
r_green_mask = 0;
r_blue_mask = 0;
r_alpha_mask = 0;
} break;
case Image::FORMAT_RG16: {
r_bit_count = 32;
r_red_mask = 0xffff;
r_green_mask = 0xffff0000;
r_blue_mask = 0;
r_alpha_mask = 0;
} break;
default: {
r_bit_count = 0;
r_red_mask = 0;
r_green_mask = 0;
r_blue_mask = 0;
r_alpha_mask = 0;
} break;
}
}
Vector<uint8_t> save_dds_buffer(const Ref<Image> &p_img) {
Ref<StreamPeerBuffer> stream_buffer;
stream_buffer.instantiate();
Ref<Image> image = p_img;
stream_buffer->put_32(DDS_MAGIC);
stream_buffer->put_32(DDS_HEADER_SIZE);
uint32_t flags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT | DDSD_PITCH | DDSD_LINEARSIZE;
if (image->has_mipmaps()) {
flags |= DDSD_MIPMAPCOUNT;
}
stream_buffer->put_32(flags);
uint32_t height = image->get_height();
stream_buffer->put_32(height);
uint32_t width = image->get_width();
stream_buffer->put_32(width);
DDSFormat dds_format = _image_format_to_dds_format(image->get_format());
const DDSFormatInfo &info = dds_format_info[dds_format];
uint32_t depth = 1; // Default depth for 2D textures
uint32_t pitch;
if (info.compressed) {
pitch = ((MAX(info.divisor, width) + info.divisor - 1) / info.divisor) * ((MAX(info.divisor, height) + info.divisor - 1) / info.divisor) * info.block_size;
} else {
pitch = width * info.block_size;
}
stream_buffer->put_32(pitch);
stream_buffer->put_32(depth);
uint32_t mipmaps = image->get_mipmap_count() + 1;
stream_buffer->put_32(mipmaps);
uint32_t reserved = 0;
for (int i = 0; i < 11; i++) {
stream_buffer->put_32(reserved);
}
stream_buffer->put_32(DDS_PIXELFORMAT_SIZE);
uint32_t pf_flags = 0;
DDSFormatType format_type = _dds_format_get_type(dds_format);
if (format_type == DDFT_BITMASK) {
pf_flags = DDPF_RGB;
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) {
pf_flags |= DDPF_ALPHAPIXELS;
}
} else {
pf_flags = DDPF_FOURCC;
}
stream_buffer->put_32(pf_flags);
bool needs_pixeldata_swap = false;
if (format_type == DDFT_BITMASK) {
// Uncompressed bitmasked.
stream_buffer->put_32(0); // FourCC
uint32_t bit_count, r_mask, g_mask, b_mask, a_mask;
_get_dds_pixel_bitmask(image->get_format(), bit_count, r_mask, g_mask, b_mask, a_mask);
stream_buffer->put_32(bit_count);
stream_buffer->put_32(r_mask);
stream_buffer->put_32(g_mask);
stream_buffer->put_32(b_mask);
stream_buffer->put_32(a_mask);
if (image->get_format() == Image::FORMAT_RGBA4444 || image->get_format() == Image::FORMAT_RGB8) {
needs_pixeldata_swap = true;
}
} else if (format_type == DDFT_FOURCC) {
// FourCC.
uint32_t fourcc = _image_format_to_fourcc_format(image->get_format());
stream_buffer->put_32(fourcc);
stream_buffer->put_32(0); // Bit count
stream_buffer->put_32(0); // R Bitmask
stream_buffer->put_32(0); // G Bitmask
stream_buffer->put_32(0); // B Bitmask
stream_buffer->put_32(0); // A Bitmask
} else {
// DXGI format and DX10 header.
stream_buffer->put_32(DDFCC_DX10);
stream_buffer->put_32(0); // Bit count
stream_buffer->put_32(0); // R Bitmask
stream_buffer->put_32(0); // G Bitmask
stream_buffer->put_32(0); // B Bitmask
stream_buffer->put_32(0); // A Bitmask
}
uint32_t caps1 = info.compressed ? DDSD_LINEARSIZE : DDSD_PITCH;
stream_buffer->put_32(caps1);
stream_buffer->put_32(0); // Caps2
stream_buffer->put_32(0); // Caps3
stream_buffer->put_32(0); // Caps4
stream_buffer->put_32(0); // Reserved 2
if (format_type == DDFT_DXGI) {
// DX10 header.
uint32_t dxgi_format = _image_format_to_dxgi_format(image->get_format());
stream_buffer->put_32(dxgi_format);
stream_buffer->put_32(DX10D_2D);
stream_buffer->put_32(0); // Misc flags 1
stream_buffer->put_32(1); // Array size
stream_buffer->put_32(0); // Misc flags 2
}
for (uint32_t mip_i = 0; mip_i < mipmaps; mip_i++) {
uint32_t mip_width = MAX(1u, width >> mip_i);
uint32_t mip_height = MAX(1u, height >> mip_i);
uint32_t expected_size = 0;
if (info.compressed) {
uint32_t blocks_x = (mip_width + info.divisor - 1) / info.divisor;
uint32_t blocks_y = (mip_height + info.divisor - 1) / info.divisor;
expected_size = blocks_x * blocks_y * info.block_size;
} else {
expected_size = mip_width * mip_height * info.block_size;
}
if (needs_pixeldata_swap) {
// The image's channels need to be swapped.
Ref<Image> mip_image = image->get_image_from_mipmap(mip_i);
Vector<uint8_t> data = mip_image->get_data();
ERR_FAIL_COND_V_MSG(data.size() != expected_size, Vector<uint8_t>(),
"Image data size mismatch for mipmap level " + itos(mip_i) +
". Expected size: " + itos(expected_size) + ", actual size: " + itos(data.size()) + ".");
if (mip_image->get_format() == Image::FORMAT_RGBA4444) {
// RGBA4 to BGRA4
const int64_t data_size = data.size();
uint8_t *wb = data.ptrw();
for (int64_t data_i = 0; data_i < data_size; data_i += 2) {
uint8_t ar = wb[data_i + 0];
uint8_t gb = wb[data_i + 1];
wb[data_i + 1] = ((ar & 0x0F) << 4) | ((gb & 0xF0) >> 4);
wb[data_i + 0] = ((ar & 0xF0) >> 4) | ((gb & 0x0F) << 4);
}
} else if (mip_image->get_format() == Image::FORMAT_RGB8) {
// RGB8 to BGR8
const int64_t data_size = data.size();
uint8_t *wb = data.ptrw();
for (int64_t data_i = 0; data_i < data_size; data_i += 3) {
SWAP(wb[data_i], wb[data_i + 2]);
}
}
stream_buffer->put_data(data.ptr(), data.size());
} else {
int64_t ofs, size;
image->get_mipmap_offset_and_size(mip_i, ofs, size);
ERR_FAIL_COND_V_MSG(size != expected_size, Vector<uint8_t>(),
"Image data size mismatch for mipmap level " + itos(mip_i) +
". Expected size: " + itos(expected_size) + ", actual size: " + itos(size) + ".");
stream_buffer->put_data(image->ptr() + ofs, size);
}
}
return stream_buffer->get_data_array();
}