Blender V5.0
format_dds.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2009 Google Inc. All rights reserved. (BSD-3-Clause)
2 * SPDX-FileCopyrightText: 2023-2024 Blender Authors (GPL-2.0-or-later).
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later AND BSD-3-Clause */
5
12
13#include <algorithm>
14
16
17#include "IMB_filetype.hh"
18#include "IMB_imbuf_types.hh"
19
20#include "BLI_math_base.h"
21#include "BLI_path_utils.hh"
22#include "BLI_string.h"
23
24OIIO_NAMESPACE_USING
25using namespace blender::imbuf;
26
27static void LoadDXTCImage(ImBuf *ibuf, Filesystem::IOMemReader &mem_reader);
28
30{
31 /* To match historical behavior for DDS file loading, tell OpenImageIO
32 * to process BC5 compressed textures as normal maps. But only do so
33 * if the environment does not already contain a directive that might
34 * say otherwise. */
35 const char *bc5normal = "dds:bc5normal";
36 const char *oiio_env = BLI_getenv("OPENIMAGEIO_OPTIONS");
37 if (!oiio_env || !BLI_strcasestr(oiio_env, bc5normal)) {
38 OIIO::attribute(bc5normal, 1);
39 }
40}
41
42bool imb_is_a_dds(const uchar *mem, size_t size)
43{
44 return imb_oiio_check(mem, size, "dds");
45}
46
47ImBuf *imb_load_dds(const uchar *mem, size_t size, int flags, ImFileColorSpace &r_colorspace)
48{
49 ImageSpec config, spec;
50 ReadContext ctx{mem, size, "dds", IMB_FTYPE_DDS, flags};
51
52 ImBuf *ibuf = imb_oiio_read(ctx, config, r_colorspace, spec);
53
54 /* Load compressed DDS information if available. */
55 if (ibuf && (flags & IB_test) == 0) {
56 Filesystem::IOMemReader mem_reader(cspan<uchar>(mem, size));
57 LoadDXTCImage(ibuf, mem_reader);
58 }
59
60 return ibuf;
61}
62
63/* A function that flips a DXTC block. */
64using FlipBlockFunction = void (*)(uint8_t *block);
65
66/* Flips a full DXT1 block in the y direction. */
67static void FlipDXT1BlockFull(uint8_t *block)
68{
69 /* A DXT1 block layout is:
70 * [0-1] color0.
71 * [2-3] color1.
72 * [4-7] color bitmap, 2 bits per pixel.
73 * So each of the 4-7 bytes represents one line, flipping a block is just
74 * flipping those bytes. */
75 uint8_t tmp = block[4];
76 block[4] = block[7];
77 block[7] = tmp;
78 tmp = block[5];
79 block[5] = block[6];
80 block[6] = tmp;
81}
82
83/* Flips the first 2 lines of a DXT1 block in the y direction. */
84static void FlipDXT1BlockHalf(uint8_t *block)
85{
86 /* See layout above. */
87 uint8_t tmp = block[4];
88 block[4] = block[5];
89 block[5] = tmp;
90}
91
92/* Flips a full DXT3 block in the y direction. */
93static void FlipDXT3BlockFull(uint8_t *block)
94{
95 /* A DXT3 block layout is:
96 * [0-7] alpha bitmap, 4 bits per pixel.
97 * [8-15] a DXT1 block. */
98
99 /* We can flip the alpha bits at the byte level (2 bytes per line). */
100 uint8_t tmp = block[0];
101
102 block[0] = block[6];
103 block[6] = tmp;
104 tmp = block[1];
105 block[1] = block[7];
106 block[7] = tmp;
107 tmp = block[2];
108 block[2] = block[4];
109 block[4] = tmp;
110 tmp = block[3];
111 block[3] = block[5];
112 block[5] = tmp;
113
114 /* And flip the DXT1 block using the above function. */
115 FlipDXT1BlockFull(block + 8);
116}
117
118/* Flips the first 2 lines of a DXT3 block in the y direction. */
119static void FlipDXT3BlockHalf(uint8_t *block)
120{
121 /* See layout above. */
122 uint8_t tmp = block[0];
123
124 block[0] = block[2];
125 block[2] = tmp;
126 tmp = block[1];
127 block[1] = block[3];
128 block[3] = tmp;
129 FlipDXT1BlockHalf(block + 8);
130}
131
132/* Flips a full DXT5 block in the y direction. */
133static void FlipDXT5BlockFull(uint8_t *block)
134{
135 /* A DXT5 block layout is:
136 * [0] alpha0.
137 * [1] alpha1.
138 * [2-7] alpha bitmap, 3 bits per pixel.
139 * [8-15] a DXT1 block. */
140
141 /* The alpha bitmap doesn't easily map lines to bytes, so we have to
142 * interpret it correctly. Extracted from
143 * http://www.opengl.org/registry/specs/EXT/texture_compression_s3tc.txt :
144 *
145 * The 6 "bits" bytes of the block are decoded into one 48-bit integer:
146 *
147 * bits = bits_0 + 256 * (bits_1 + 256 * (bits_2 + 256 * (bits_3 +
148 * 256 * (bits_4 + 256 * bits_5))))
149 *
150 * bits is a 48-bit unsigned-integer, from which a three-bit control code
151 * is extracted for a texel at location (x,y) in the block using:
152 *
153 * code(x,y) = bits[3*(4*y+x)+1..3*(4*y+x)+0]
154 *
155 * where bit 47 is the most significant and bit 0 is the least
156 * significant bit. */
157 uint line_0_1 = block[2] + 256 * (block[3] + 256 * block[4]);
158 uint line_2_3 = block[5] + 256 * (block[6] + 256 * block[7]);
159 /* swap lines 0 and 1 in line_0_1. */
160 uint line_1_0 = ((line_0_1 & 0x000fff) << 12) | ((line_0_1 & 0xfff000) >> 12);
161 /* swap lines 2 and 3 in line_2_3. */
162 uint line_3_2 = ((line_2_3 & 0x000fff) << 12) | ((line_2_3 & 0xfff000) >> 12);
163
164 block[2] = line_3_2 & 0xff;
165 block[3] = (line_3_2 & 0xff00) >> 8;
166 block[4] = (line_3_2 & 0xff0000) >> 16;
167 block[5] = line_1_0 & 0xff;
168 block[6] = (line_1_0 & 0xff00) >> 8;
169 block[7] = (line_1_0 & 0xff0000) >> 16;
170
171 /* And flip the DXT1 block using the above function. */
172 FlipDXT1BlockFull(block + 8);
173}
174
175/* Flips the first 2 lines of a DXT5 block in the y direction. */
176static void FlipDXT5BlockHalf(uint8_t *block)
177{
178 /* See layout above. */
179 uint line_0_1 = block[2] + 256 * (block[3] + 256 * block[4]);
180 uint line_1_0 = ((line_0_1 & 0x000fff) << 12) | ((line_0_1 & 0xfff000) >> 12);
181 block[2] = line_1_0 & 0xff;
182 block[3] = (line_1_0 & 0xff00) >> 8;
183 block[4] = (line_1_0 & 0xff0000) >> 16;
184 FlipDXT1BlockHalf(block + 8);
185}
186
192static void FlipDXTCImage(ImBuf *ibuf)
193{
194 uint32_t width = ibuf->x;
195 uint32_t height = ibuf->y;
196 uint32_t levels = ibuf->dds_data.nummipmaps;
197 int fourcc = ibuf->dds_data.fourcc;
198 uint8_t *data = ibuf->dds_data.data;
199 int data_size = ibuf->dds_data.size;
200
201 uint32_t *num_valid_levels = &ibuf->dds_data.nummipmaps;
202 *num_valid_levels = 0;
203
204 /* Must have valid dimensions. */
205 if (width == 0 || height == 0) {
206 return;
207 }
208 /* Height must be a power-of-two: not because something within DXT/S3TC
209 * needs it. Only because we want to flip the image upside down, by
210 * swapping and flipping block rows, and that in general case (with mipmaps)
211 * is only possible for POT height. */
212 if (!is_power_of_2_i(height)) {
213 return;
214 }
215
216 FlipBlockFunction full_block_function;
217 FlipBlockFunction half_block_function;
218 uint block_bytes = 0;
219
220 switch (fourcc) {
221 case FOURCC_DXT1:
222 full_block_function = FlipDXT1BlockFull;
223 half_block_function = FlipDXT1BlockHalf;
224 block_bytes = 8;
225 break;
226 case FOURCC_DXT3:
227 full_block_function = FlipDXT3BlockFull;
228 half_block_function = FlipDXT3BlockHalf;
229 block_bytes = 16;
230 break;
231 case FOURCC_DXT5:
232 full_block_function = FlipDXT5BlockFull;
233 half_block_function = FlipDXT5BlockHalf;
234 block_bytes = 16;
235 break;
236 default:
237 return;
238 }
239
240 *num_valid_levels = levels;
241
242 uint mip_width = width;
243 uint mip_height = height;
244
245 const uint8_t *data_end = data + data_size;
246
247 for (uint level = 0; level < levels; level++) {
248 uint blocks_per_row = (mip_width + 3) / 4;
249 uint blocks_per_col = (mip_height + 3) / 4;
250 uint blocks = blocks_per_row * blocks_per_col;
251
252 if (data + block_bytes * blocks > data_end) {
253 /* Stop flipping when running out of data to be modified, avoiding possible buffer overrun
254 * on a malformed files. */
255 *num_valid_levels = level;
256 break;
257 }
258
259 if (mip_height == 1) {
260 /* no flip to do, and we're done. */
261 break;
262 }
263 if (mip_height == 2) {
264 /* flip the first 2 lines in each block. */
265 for (uint i = 0; i < blocks_per_row; i++) {
266 half_block_function(data + i * block_bytes);
267 }
268 }
269 else {
270 /* flip each block. */
271 for (uint i = 0; i < blocks; i++) {
272 full_block_function(data + i * block_bytes);
273 }
274
275 /* Swap each block line in the first half of the image with the
276 * corresponding one in the second half.
277 * note that this is a no-op if mip_height is 4. */
278 uint row_bytes = block_bytes * blocks_per_row;
279 uint8_t *temp_line = new uint8_t[row_bytes];
280
281 for (uint y = 0; y < blocks_per_col / 2; y++) {
282 uint8_t *line1 = data + y * row_bytes;
283 uint8_t *line2 = data + (blocks_per_col - y - 1) * row_bytes;
284
285 memcpy(temp_line, line1, row_bytes);
286 memcpy(line1, line2, row_bytes);
287 memcpy(line2, temp_line, row_bytes);
288 }
289
290 delete[] temp_line;
291 }
292
293 /* mip levels are contiguous. */
294 data += block_bytes * blocks;
295 mip_width = std::max(1U, mip_width >> 1);
296 mip_height = std::max(1U, mip_height >> 1);
297 }
298}
299
300static void LoadDXTCImage(ImBuf *ibuf, Filesystem::IOMemReader &mem_reader)
301{
302 /* Reach into memory and pull out the pixel format flags and mipmap counts. This is safe if
303 * we've made it this far. */
304 uint32_t flags = 0;
305 mem_reader.pread(&flags, sizeof(uint32_t), 8);
306 /* NOTE: this is endianness-sensitive. */
307 /* `ibuf->dds_data.nummipmaps` is always expected to be little-endian. */
308 mem_reader.pread(&ibuf->dds_data.nummipmaps, sizeof(uint32_t), 28);
309 mem_reader.pread(&ibuf->dds_data.fourcc, sizeof(uint32_t), 84);
310
311 const uint32_t DDSD_MIPMAPCOUNT = 0x00020000U;
312 if ((flags & DDSD_MIPMAPCOUNT) == 0) {
313 ibuf->dds_data.nummipmaps = 1;
314 }
315
316 /* Load the compressed data. */
317 if (ibuf->dds_data.fourcc != FOURCC_DDS) {
318 uint32_t dds_header_size = 128;
319 if (ibuf->dds_data.fourcc == FOURCC_DX10) {
320 dds_header_size += 20;
321 }
322
323 ibuf->dds_data.size = mem_reader.size() - dds_header_size;
324 ibuf->dds_data.data = (uchar *)malloc(ibuf->dds_data.size);
325 mem_reader.pread(ibuf->dds_data.data, ibuf->dds_data.size, dds_header_size);
327
328 /* Flip compressed image data to match OpenGL convention. */
329 FlipDXTCImage(ibuf);
330 }
331}
MINLINE int is_power_of_2_i(int n)
const char * BLI_getenv(const char *env) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
int char * BLI_strcasestr(const char *s, const char *find) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1
unsigned char uchar
unsigned int uint
@ IMB_FTYPE_DDS
#define FOURCC_DX10
#define FOURCC_DXT5
#define FOURCC_DDS
#define FOURCC_DXT1
@ IB_TAKE_OWNERSHIP
@ IB_test
#define FOURCC_DXT3
BMesh const char void * data
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition btDbvt.cpp:52
static void FlipDXT3BlockFull(uint8_t *block)
Definition format_dds.cc:93
void(*)(uint8_t *block) FlipBlockFunction
Definition format_dds.cc:64
ImBuf * imb_load_dds(const uchar *mem, size_t size, int flags, ImFileColorSpace &r_colorspace)
Definition format_dds.cc:47
static void FlipDXT5BlockHalf(uint8_t *block)
static void FlipDXTCImage(ImBuf *ibuf)
bool imb_is_a_dds(const uchar *mem, size_t size)
Definition format_dds.cc:42
static void LoadDXTCImage(ImBuf *ibuf, Filesystem::IOMemReader &mem_reader)
void imb_init_dds()
Definition format_dds.cc:29
static void FlipDXT1BlockFull(uint8_t *block)
Definition format_dds.cc:67
static void FlipDXT5BlockFull(uint8_t *block)
static void FlipDXT1BlockHalf(uint8_t *block)
Definition format_dds.cc:84
static void FlipDXT3BlockHalf(uint8_t *block)
ImBuf * imb_oiio_read(const ReadContext &ctx, const ImageSpec &config, ImFileColorSpace &r_colorspace, ImageSpec &r_newspec)
bool imb_oiio_check(const uchar *mem, size_t mem_size, const char *file_format)
unsigned int size
unsigned int nummipmaps
ImBufOwnership ownership
unsigned char * data
unsigned int fourcc
DDSData dds_data
i
Definition text_draw.cc:230