2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read/Write Microsoft Direct Draw Surface Image Format %
22 % Copyright 1999-2015 ImageMagick Studio LLC, a non-profit organization %
23 % dedicated to making software imaging solutions freely available. %
25 % You may not use this file except in compliance with the License. You may %
26 % obtain a copy of the License at %
28 % http://www.imagemagick.org/script/license.php %
30 % Unless required by applicable law or agreed to in writing, software %
31 % distributed under the License is distributed on an "AS IS" BASIS, %
32 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
33 % See the License for the specific language governing permissions and %
34 % limitations under the License. %
36 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
44 #include "MagickCore/studio.h"
45 #include "MagickCore/attribute.h"
46 #include "MagickCore/blob.h"
47 #include "MagickCore/blob-private.h"
48 #include "MagickCore/cache.h"
49 #include "MagickCore/colorspace.h"
50 #include "MagickCore/colorspace-private.h"
51 #include "MagickCore/exception.h"
52 #include "MagickCore/exception-private.h"
53 #include "MagickCore/image.h"
54 #include "MagickCore/image-private.h"
55 #include "MagickCore/list.h"
56 #include "MagickCore/log.h"
57 #include "MagickCore/magick.h"
58 #include "MagickCore/memory_.h"
59 #include "MagickCore/monitor.h"
60 #include "MagickCore/monitor-private.h"
61 #include "MagickCore/option.h"
62 #include "MagickCore/pixel-accessor.h"
63 #include "MagickCore/profile.h"
64 #include "MagickCore/quantum.h"
65 #include "MagickCore/quantum-private.h"
66 #include "MagickCore/resource_.h"
67 #include "MagickCore/static.h"
68 #include "MagickCore/string_.h"
69 #include "MagickCore/string-private.h"
70 #include "MagickCore/module.h"
71 #include "MagickCore/transform.h"
76 #define DDSD_CAPS 0x00000001
77 #define DDSD_HEIGHT 0x00000002
78 #define DDSD_WIDTH 0x00000004
79 #define DDSD_PITCH 0x00000008
80 #define DDSD_PIXELFORMAT 0x00001000
81 #define DDSD_MIPMAPCOUNT 0x00020000
82 #define DDSD_LINEARSIZE 0x00080000
83 #define DDSD_DEPTH 0x00800000
85 #define DDPF_ALPHAPIXELS 0x00000001
86 #define DDPF_FOURCC 0x00000004
87 #define DDPF_RGB 0x00000040
88 #define DDPF_LUMINANCE 0x00020000
90 #define FOURCC_DXT1 0x31545844
91 #define FOURCC_DXT3 0x33545844
92 #define FOURCC_DXT5 0x35545844
94 #define DDSCAPS_COMPLEX 0x00000008
95 #define DDSCAPS_TEXTURE 0x00001000
96 #define DDSCAPS_MIPMAP 0x00400000
98 #define DDSCAPS2_CUBEMAP 0x00000200
99 #define DDSCAPS2_CUBEMAP_POSITIVEX 0x00000400
100 #define DDSCAPS2_CUBEMAP_NEGATIVEX 0x00000800
101 #define DDSCAPS2_CUBEMAP_POSITIVEY 0x00001000
102 #define DDSCAPS2_CUBEMAP_NEGATIVEY 0x00002000
103 #define DDSCAPS2_CUBEMAP_POSITIVEZ 0x00004000
104 #define DDSCAPS2_CUBEMAP_NEGATIVEZ 0x00008000
105 #define DDSCAPS2_VOLUME 0x00200000
108 #define SIZE_MAX ((size_t) -1)
112 Structure declarations.
114 typedef struct _DDSPixelFormat
126 typedef struct _DDSInfo
142 typedef struct _DDSColors
151 typedef struct _DDSVector4
160 typedef struct _DDSVector3
168 typedef struct _DDSSourceBlock
176 typedef struct _DDSSingleColourLookup
178 DDSSourceBlock sources[2];
179 } DDSSingleColourLookup;
181 typedef MagickBooleanType
182 DDSDecoder(Image *, DDSInfo *, ExceptionInfo *);
184 static const DDSSingleColourLookup DDSLookup_5_4[] =
186 { { { 0, 0, 0 }, { 0, 0, 0 } } },
187 { { { 0, 0, 1 }, { 0, 1, 1 } } },
188 { { { 0, 0, 2 }, { 0, 1, 0 } } },
189 { { { 0, 0, 3 }, { 0, 1, 1 } } },
190 { { { 0, 0, 4 }, { 0, 2, 1 } } },
191 { { { 1, 0, 3 }, { 0, 2, 0 } } },
192 { { { 1, 0, 2 }, { 0, 2, 1 } } },
193 { { { 1, 0, 1 }, { 0, 3, 1 } } },
194 { { { 1, 0, 0 }, { 0, 3, 0 } } },
195 { { { 1, 0, 1 }, { 1, 2, 1 } } },
196 { { { 1, 0, 2 }, { 1, 2, 0 } } },
197 { { { 1, 0, 3 }, { 0, 4, 0 } } },
198 { { { 1, 0, 4 }, { 0, 5, 1 } } },
199 { { { 2, 0, 3 }, { 0, 5, 0 } } },
200 { { { 2, 0, 2 }, { 0, 5, 1 } } },
201 { { { 2, 0, 1 }, { 0, 6, 1 } } },
202 { { { 2, 0, 0 }, { 0, 6, 0 } } },
203 { { { 2, 0, 1 }, { 2, 3, 1 } } },
204 { { { 2, 0, 2 }, { 2, 3, 0 } } },
205 { { { 2, 0, 3 }, { 0, 7, 0 } } },
206 { { { 2, 0, 4 }, { 1, 6, 1 } } },
207 { { { 3, 0, 3 }, { 1, 6, 0 } } },
208 { { { 3, 0, 2 }, { 0, 8, 0 } } },
209 { { { 3, 0, 1 }, { 0, 9, 1 } } },
210 { { { 3, 0, 0 }, { 0, 9, 0 } } },
211 { { { 3, 0, 1 }, { 0, 9, 1 } } },
212 { { { 3, 0, 2 }, { 0, 10, 1 } } },
213 { { { 3, 0, 3 }, { 0, 10, 0 } } },
214 { { { 3, 0, 4 }, { 2, 7, 1 } } },
215 { { { 4, 0, 4 }, { 2, 7, 0 } } },
216 { { { 4, 0, 3 }, { 0, 11, 0 } } },
217 { { { 4, 0, 2 }, { 1, 10, 1 } } },
218 { { { 4, 0, 1 }, { 1, 10, 0 } } },
219 { { { 4, 0, 0 }, { 0, 12, 0 } } },
220 { { { 4, 0, 1 }, { 0, 13, 1 } } },
221 { { { 4, 0, 2 }, { 0, 13, 0 } } },
222 { { { 4, 0, 3 }, { 0, 13, 1 } } },
223 { { { 4, 0, 4 }, { 0, 14, 1 } } },
224 { { { 5, 0, 3 }, { 0, 14, 0 } } },
225 { { { 5, 0, 2 }, { 2, 11, 1 } } },
226 { { { 5, 0, 1 }, { 2, 11, 0 } } },
227 { { { 5, 0, 0 }, { 0, 15, 0 } } },
228 { { { 5, 0, 1 }, { 1, 14, 1 } } },
229 { { { 5, 0, 2 }, { 1, 14, 0 } } },
230 { { { 5, 0, 3 }, { 0, 16, 0 } } },
231 { { { 5, 0, 4 }, { 0, 17, 1 } } },
232 { { { 6, 0, 3 }, { 0, 17, 0 } } },
233 { { { 6, 0, 2 }, { 0, 17, 1 } } },
234 { { { 6, 0, 1 }, { 0, 18, 1 } } },
235 { { { 6, 0, 0 }, { 0, 18, 0 } } },
236 { { { 6, 0, 1 }, { 2, 15, 1 } } },
237 { { { 6, 0, 2 }, { 2, 15, 0 } } },
238 { { { 6, 0, 3 }, { 0, 19, 0 } } },
239 { { { 6, 0, 4 }, { 1, 18, 1 } } },
240 { { { 7, 0, 3 }, { 1, 18, 0 } } },
241 { { { 7, 0, 2 }, { 0, 20, 0 } } },
242 { { { 7, 0, 1 }, { 0, 21, 1 } } },
243 { { { 7, 0, 0 }, { 0, 21, 0 } } },
244 { { { 7, 0, 1 }, { 0, 21, 1 } } },
245 { { { 7, 0, 2 }, { 0, 22, 1 } } },
246 { { { 7, 0, 3 }, { 0, 22, 0 } } },
247 { { { 7, 0, 4 }, { 2, 19, 1 } } },
248 { { { 8, 0, 4 }, { 2, 19, 0 } } },
249 { { { 8, 0, 3 }, { 0, 23, 0 } } },
250 { { { 8, 0, 2 }, { 1, 22, 1 } } },
251 { { { 8, 0, 1 }, { 1, 22, 0 } } },
252 { { { 8, 0, 0 }, { 0, 24, 0 } } },
253 { { { 8, 0, 1 }, { 0, 25, 1 } } },
254 { { { 8, 0, 2 }, { 0, 25, 0 } } },
255 { { { 8, 0, 3 }, { 0, 25, 1 } } },
256 { { { 8, 0, 4 }, { 0, 26, 1 } } },
257 { { { 9, 0, 3 }, { 0, 26, 0 } } },
258 { { { 9, 0, 2 }, { 2, 23, 1 } } },
259 { { { 9, 0, 1 }, { 2, 23, 0 } } },
260 { { { 9, 0, 0 }, { 0, 27, 0 } } },
261 { { { 9, 0, 1 }, { 1, 26, 1 } } },
262 { { { 9, 0, 2 }, { 1, 26, 0 } } },
263 { { { 9, 0, 3 }, { 0, 28, 0 } } },
264 { { { 9, 0, 4 }, { 0, 29, 1 } } },
265 { { { 10, 0, 3 }, { 0, 29, 0 } } },
266 { { { 10, 0, 2 }, { 0, 29, 1 } } },
267 { { { 10, 0, 1 }, { 0, 30, 1 } } },
268 { { { 10, 0, 0 }, { 0, 30, 0 } } },
269 { { { 10, 0, 1 }, { 2, 27, 1 } } },
270 { { { 10, 0, 2 }, { 2, 27, 0 } } },
271 { { { 10, 0, 3 }, { 0, 31, 0 } } },
272 { { { 10, 0, 4 }, { 1, 30, 1 } } },
273 { { { 11, 0, 3 }, { 1, 30, 0 } } },
274 { { { 11, 0, 2 }, { 4, 24, 0 } } },
275 { { { 11, 0, 1 }, { 1, 31, 1 } } },
276 { { { 11, 0, 0 }, { 1, 31, 0 } } },
277 { { { 11, 0, 1 }, { 1, 31, 1 } } },
278 { { { 11, 0, 2 }, { 2, 30, 1 } } },
279 { { { 11, 0, 3 }, { 2, 30, 0 } } },
280 { { { 11, 0, 4 }, { 2, 31, 1 } } },
281 { { { 12, 0, 4 }, { 2, 31, 0 } } },
282 { { { 12, 0, 3 }, { 4, 27, 0 } } },
283 { { { 12, 0, 2 }, { 3, 30, 1 } } },
284 { { { 12, 0, 1 }, { 3, 30, 0 } } },
285 { { { 12, 0, 0 }, { 4, 28, 0 } } },
286 { { { 12, 0, 1 }, { 3, 31, 1 } } },
287 { { { 12, 0, 2 }, { 3, 31, 0 } } },
288 { { { 12, 0, 3 }, { 3, 31, 1 } } },
289 { { { 12, 0, 4 }, { 4, 30, 1 } } },
290 { { { 13, 0, 3 }, { 4, 30, 0 } } },
291 { { { 13, 0, 2 }, { 6, 27, 1 } } },
292 { { { 13, 0, 1 }, { 6, 27, 0 } } },
293 { { { 13, 0, 0 }, { 4, 31, 0 } } },
294 { { { 13, 0, 1 }, { 5, 30, 1 } } },
295 { { { 13, 0, 2 }, { 5, 30, 0 } } },
296 { { { 13, 0, 3 }, { 8, 24, 0 } } },
297 { { { 13, 0, 4 }, { 5, 31, 1 } } },
298 { { { 14, 0, 3 }, { 5, 31, 0 } } },
299 { { { 14, 0, 2 }, { 5, 31, 1 } } },
300 { { { 14, 0, 1 }, { 6, 30, 1 } } },
301 { { { 14, 0, 0 }, { 6, 30, 0 } } },
302 { { { 14, 0, 1 }, { 6, 31, 1 } } },
303 { { { 14, 0, 2 }, { 6, 31, 0 } } },
304 { { { 14, 0, 3 }, { 8, 27, 0 } } },
305 { { { 14, 0, 4 }, { 7, 30, 1 } } },
306 { { { 15, 0, 3 }, { 7, 30, 0 } } },
307 { { { 15, 0, 2 }, { 8, 28, 0 } } },
308 { { { 15, 0, 1 }, { 7, 31, 1 } } },
309 { { { 15, 0, 0 }, { 7, 31, 0 } } },
310 { { { 15, 0, 1 }, { 7, 31, 1 } } },
311 { { { 15, 0, 2 }, { 8, 30, 1 } } },
312 { { { 15, 0, 3 }, { 8, 30, 0 } } },
313 { { { 15, 0, 4 }, { 10, 27, 1 } } },
314 { { { 16, 0, 4 }, { 10, 27, 0 } } },
315 { { { 16, 0, 3 }, { 8, 31, 0 } } },
316 { { { 16, 0, 2 }, { 9, 30, 1 } } },
317 { { { 16, 0, 1 }, { 9, 30, 0 } } },
318 { { { 16, 0, 0 }, { 12, 24, 0 } } },
319 { { { 16, 0, 1 }, { 9, 31, 1 } } },
320 { { { 16, 0, 2 }, { 9, 31, 0 } } },
321 { { { 16, 0, 3 }, { 9, 31, 1 } } },
322 { { { 16, 0, 4 }, { 10, 30, 1 } } },
323 { { { 17, 0, 3 }, { 10, 30, 0 } } },
324 { { { 17, 0, 2 }, { 10, 31, 1 } } },
325 { { { 17, 0, 1 }, { 10, 31, 0 } } },
326 { { { 17, 0, 0 }, { 12, 27, 0 } } },
327 { { { 17, 0, 1 }, { 11, 30, 1 } } },
328 { { { 17, 0, 2 }, { 11, 30, 0 } } },
329 { { { 17, 0, 3 }, { 12, 28, 0 } } },
330 { { { 17, 0, 4 }, { 11, 31, 1 } } },
331 { { { 18, 0, 3 }, { 11, 31, 0 } } },
332 { { { 18, 0, 2 }, { 11, 31, 1 } } },
333 { { { 18, 0, 1 }, { 12, 30, 1 } } },
334 { { { 18, 0, 0 }, { 12, 30, 0 } } },
335 { { { 18, 0, 1 }, { 14, 27, 1 } } },
336 { { { 18, 0, 2 }, { 14, 27, 0 } } },
337 { { { 18, 0, 3 }, { 12, 31, 0 } } },
338 { { { 18, 0, 4 }, { 13, 30, 1 } } },
339 { { { 19, 0, 3 }, { 13, 30, 0 } } },
340 { { { 19, 0, 2 }, { 16, 24, 0 } } },
341 { { { 19, 0, 1 }, { 13, 31, 1 } } },
342 { { { 19, 0, 0 }, { 13, 31, 0 } } },
343 { { { 19, 0, 1 }, { 13, 31, 1 } } },
344 { { { 19, 0, 2 }, { 14, 30, 1 } } },
345 { { { 19, 0, 3 }, { 14, 30, 0 } } },
346 { { { 19, 0, 4 }, { 14, 31, 1 } } },
347 { { { 20, 0, 4 }, { 14, 31, 0 } } },
348 { { { 20, 0, 3 }, { 16, 27, 0 } } },
349 { { { 20, 0, 2 }, { 15, 30, 1 } } },
350 { { { 20, 0, 1 }, { 15, 30, 0 } } },
351 { { { 20, 0, 0 }, { 16, 28, 0 } } },
352 { { { 20, 0, 1 }, { 15, 31, 1 } } },
353 { { { 20, 0, 2 }, { 15, 31, 0 } } },
354 { { { 20, 0, 3 }, { 15, 31, 1 } } },
355 { { { 20, 0, 4 }, { 16, 30, 1 } } },
356 { { { 21, 0, 3 }, { 16, 30, 0 } } },
357 { { { 21, 0, 2 }, { 18, 27, 1 } } },
358 { { { 21, 0, 1 }, { 18, 27, 0 } } },
359 { { { 21, 0, 0 }, { 16, 31, 0 } } },
360 { { { 21, 0, 1 }, { 17, 30, 1 } } },
361 { { { 21, 0, 2 }, { 17, 30, 0 } } },
362 { { { 21, 0, 3 }, { 20, 24, 0 } } },
363 { { { 21, 0, 4 }, { 17, 31, 1 } } },
364 { { { 22, 0, 3 }, { 17, 31, 0 } } },
365 { { { 22, 0, 2 }, { 17, 31, 1 } } },
366 { { { 22, 0, 1 }, { 18, 30, 1 } } },
367 { { { 22, 0, 0 }, { 18, 30, 0 } } },
368 { { { 22, 0, 1 }, { 18, 31, 1 } } },
369 { { { 22, 0, 2 }, { 18, 31, 0 } } },
370 { { { 22, 0, 3 }, { 20, 27, 0 } } },
371 { { { 22, 0, 4 }, { 19, 30, 1 } } },
372 { { { 23, 0, 3 }, { 19, 30, 0 } } },
373 { { { 23, 0, 2 }, { 20, 28, 0 } } },
374 { { { 23, 0, 1 }, { 19, 31, 1 } } },
375 { { { 23, 0, 0 }, { 19, 31, 0 } } },
376 { { { 23, 0, 1 }, { 19, 31, 1 } } },
377 { { { 23, 0, 2 }, { 20, 30, 1 } } },
378 { { { 23, 0, 3 }, { 20, 30, 0 } } },
379 { { { 23, 0, 4 }, { 22, 27, 1 } } },
380 { { { 24, 0, 4 }, { 22, 27, 0 } } },
381 { { { 24, 0, 3 }, { 20, 31, 0 } } },
382 { { { 24, 0, 2 }, { 21, 30, 1 } } },
383 { { { 24, 0, 1 }, { 21, 30, 0 } } },
384 { { { 24, 0, 0 }, { 24, 24, 0 } } },
385 { { { 24, 0, 1 }, { 21, 31, 1 } } },
386 { { { 24, 0, 2 }, { 21, 31, 0 } } },
387 { { { 24, 0, 3 }, { 21, 31, 1 } } },
388 { { { 24, 0, 4 }, { 22, 30, 1 } } },
389 { { { 25, 0, 3 }, { 22, 30, 0 } } },
390 { { { 25, 0, 2 }, { 22, 31, 1 } } },
391 { { { 25, 0, 1 }, { 22, 31, 0 } } },
392 { { { 25, 0, 0 }, { 24, 27, 0 } } },
393 { { { 25, 0, 1 }, { 23, 30, 1 } } },
394 { { { 25, 0, 2 }, { 23, 30, 0 } } },
395 { { { 25, 0, 3 }, { 24, 28, 0 } } },
396 { { { 25, 0, 4 }, { 23, 31, 1 } } },
397 { { { 26, 0, 3 }, { 23, 31, 0 } } },
398 { { { 26, 0, 2 }, { 23, 31, 1 } } },
399 { { { 26, 0, 1 }, { 24, 30, 1 } } },
400 { { { 26, 0, 0 }, { 24, 30, 0 } } },
401 { { { 26, 0, 1 }, { 26, 27, 1 } } },
402 { { { 26, 0, 2 }, { 26, 27, 0 } } },
403 { { { 26, 0, 3 }, { 24, 31, 0 } } },
404 { { { 26, 0, 4 }, { 25, 30, 1 } } },
405 { { { 27, 0, 3 }, { 25, 30, 0 } } },
406 { { { 27, 0, 2 }, { 28, 24, 0 } } },
407 { { { 27, 0, 1 }, { 25, 31, 1 } } },
408 { { { 27, 0, 0 }, { 25, 31, 0 } } },
409 { { { 27, 0, 1 }, { 25, 31, 1 } } },
410 { { { 27, 0, 2 }, { 26, 30, 1 } } },
411 { { { 27, 0, 3 }, { 26, 30, 0 } } },
412 { { { 27, 0, 4 }, { 26, 31, 1 } } },
413 { { { 28, 0, 4 }, { 26, 31, 0 } } },
414 { { { 28, 0, 3 }, { 28, 27, 0 } } },
415 { { { 28, 0, 2 }, { 27, 30, 1 } } },
416 { { { 28, 0, 1 }, { 27, 30, 0 } } },
417 { { { 28, 0, 0 }, { 28, 28, 0 } } },
418 { { { 28, 0, 1 }, { 27, 31, 1 } } },
419 { { { 28, 0, 2 }, { 27, 31, 0 } } },
420 { { { 28, 0, 3 }, { 27, 31, 1 } } },
421 { { { 28, 0, 4 }, { 28, 30, 1 } } },
422 { { { 29, 0, 3 }, { 28, 30, 0 } } },
423 { { { 29, 0, 2 }, { 30, 27, 1 } } },
424 { { { 29, 0, 1 }, { 30, 27, 0 } } },
425 { { { 29, 0, 0 }, { 28, 31, 0 } } },
426 { { { 29, 0, 1 }, { 29, 30, 1 } } },
427 { { { 29, 0, 2 }, { 29, 30, 0 } } },
428 { { { 29, 0, 3 }, { 29, 30, 1 } } },
429 { { { 29, 0, 4 }, { 29, 31, 1 } } },
430 { { { 30, 0, 3 }, { 29, 31, 0 } } },
431 { { { 30, 0, 2 }, { 29, 31, 1 } } },
432 { { { 30, 0, 1 }, { 30, 30, 1 } } },
433 { { { 30, 0, 0 }, { 30, 30, 0 } } },
434 { { { 30, 0, 1 }, { 30, 31, 1 } } },
435 { { { 30, 0, 2 }, { 30, 31, 0 } } },
436 { { { 30, 0, 3 }, { 30, 31, 1 } } },
437 { { { 30, 0, 4 }, { 31, 30, 1 } } },
438 { { { 31, 0, 3 }, { 31, 30, 0 } } },
439 { { { 31, 0, 2 }, { 31, 30, 1 } } },
440 { { { 31, 0, 1 }, { 31, 31, 1 } } },
441 { { { 31, 0, 0 }, { 31, 31, 0 } } }
444 static const DDSSingleColourLookup DDSLookup_6_4[] =
446 { { { 0, 0, 0 }, { 0, 0, 0 } } },
447 { { { 0, 0, 1 }, { 0, 1, 0 } } },
448 { { { 0, 0, 2 }, { 0, 2, 0 } } },
449 { { { 1, 0, 1 }, { 0, 3, 1 } } },
450 { { { 1, 0, 0 }, { 0, 3, 0 } } },
451 { { { 1, 0, 1 }, { 0, 4, 0 } } },
452 { { { 1, 0, 2 }, { 0, 5, 0 } } },
453 { { { 2, 0, 1 }, { 0, 6, 1 } } },
454 { { { 2, 0, 0 }, { 0, 6, 0 } } },
455 { { { 2, 0, 1 }, { 0, 7, 0 } } },
456 { { { 2, 0, 2 }, { 0, 8, 0 } } },
457 { { { 3, 0, 1 }, { 0, 9, 1 } } },
458 { { { 3, 0, 0 }, { 0, 9, 0 } } },
459 { { { 3, 0, 1 }, { 0, 10, 0 } } },
460 { { { 3, 0, 2 }, { 0, 11, 0 } } },
461 { { { 4, 0, 1 }, { 0, 12, 1 } } },
462 { { { 4, 0, 0 }, { 0, 12, 0 } } },
463 { { { 4, 0, 1 }, { 0, 13, 0 } } },
464 { { { 4, 0, 2 }, { 0, 14, 0 } } },
465 { { { 5, 0, 1 }, { 0, 15, 1 } } },
466 { { { 5, 0, 0 }, { 0, 15, 0 } } },
467 { { { 5, 0, 1 }, { 0, 16, 0 } } },
468 { { { 5, 0, 2 }, { 1, 15, 0 } } },
469 { { { 6, 0, 1 }, { 0, 17, 0 } } },
470 { { { 6, 0, 0 }, { 0, 18, 0 } } },
471 { { { 6, 0, 1 }, { 0, 19, 0 } } },
472 { { { 6, 0, 2 }, { 3, 14, 0 } } },
473 { { { 7, 0, 1 }, { 0, 20, 0 } } },
474 { { { 7, 0, 0 }, { 0, 21, 0 } } },
475 { { { 7, 0, 1 }, { 0, 22, 0 } } },
476 { { { 7, 0, 2 }, { 4, 15, 0 } } },
477 { { { 8, 0, 1 }, { 0, 23, 0 } } },
478 { { { 8, 0, 0 }, { 0, 24, 0 } } },
479 { { { 8, 0, 1 }, { 0, 25, 0 } } },
480 { { { 8, 0, 2 }, { 6, 14, 0 } } },
481 { { { 9, 0, 1 }, { 0, 26, 0 } } },
482 { { { 9, 0, 0 }, { 0, 27, 0 } } },
483 { { { 9, 0, 1 }, { 0, 28, 0 } } },
484 { { { 9, 0, 2 }, { 7, 15, 0 } } },
485 { { { 10, 0, 1 }, { 0, 29, 0 } } },
486 { { { 10, 0, 0 }, { 0, 30, 0 } } },
487 { { { 10, 0, 1 }, { 0, 31, 0 } } },
488 { { { 10, 0, 2 }, { 9, 14, 0 } } },
489 { { { 11, 0, 1 }, { 0, 32, 0 } } },
490 { { { 11, 0, 0 }, { 0, 33, 0 } } },
491 { { { 11, 0, 1 }, { 2, 30, 0 } } },
492 { { { 11, 0, 2 }, { 0, 34, 0 } } },
493 { { { 12, 0, 1 }, { 0, 35, 0 } } },
494 { { { 12, 0, 0 }, { 0, 36, 0 } } },
495 { { { 12, 0, 1 }, { 3, 31, 0 } } },
496 { { { 12, 0, 2 }, { 0, 37, 0 } } },
497 { { { 13, 0, 1 }, { 0, 38, 0 } } },
498 { { { 13, 0, 0 }, { 0, 39, 0 } } },
499 { { { 13, 0, 1 }, { 5, 30, 0 } } },
500 { { { 13, 0, 2 }, { 0, 40, 0 } } },
501 { { { 14, 0, 1 }, { 0, 41, 0 } } },
502 { { { 14, 0, 0 }, { 0, 42, 0 } } },
503 { { { 14, 0, 1 }, { 6, 31, 0 } } },
504 { { { 14, 0, 2 }, { 0, 43, 0 } } },
505 { { { 15, 0, 1 }, { 0, 44, 0 } } },
506 { { { 15, 0, 0 }, { 0, 45, 0 } } },
507 { { { 15, 0, 1 }, { 8, 30, 0 } } },
508 { { { 15, 0, 2 }, { 0, 46, 0 } } },
509 { { { 16, 0, 2 }, { 0, 47, 0 } } },
510 { { { 16, 0, 1 }, { 1, 46, 0 } } },
511 { { { 16, 0, 0 }, { 0, 48, 0 } } },
512 { { { 16, 0, 1 }, { 0, 49, 0 } } },
513 { { { 16, 0, 2 }, { 0, 50, 0 } } },
514 { { { 17, 0, 1 }, { 2, 47, 0 } } },
515 { { { 17, 0, 0 }, { 0, 51, 0 } } },
516 { { { 17, 0, 1 }, { 0, 52, 0 } } },
517 { { { 17, 0, 2 }, { 0, 53, 0 } } },
518 { { { 18, 0, 1 }, { 4, 46, 0 } } },
519 { { { 18, 0, 0 }, { 0, 54, 0 } } },
520 { { { 18, 0, 1 }, { 0, 55, 0 } } },
521 { { { 18, 0, 2 }, { 0, 56, 0 } } },
522 { { { 19, 0, 1 }, { 5, 47, 0 } } },
523 { { { 19, 0, 0 }, { 0, 57, 0 } } },
524 { { { 19, 0, 1 }, { 0, 58, 0 } } },
525 { { { 19, 0, 2 }, { 0, 59, 0 } } },
526 { { { 20, 0, 1 }, { 7, 46, 0 } } },
527 { { { 20, 0, 0 }, { 0, 60, 0 } } },
528 { { { 20, 0, 1 }, { 0, 61, 0 } } },
529 { { { 20, 0, 2 }, { 0, 62, 0 } } },
530 { { { 21, 0, 1 }, { 8, 47, 0 } } },
531 { { { 21, 0, 0 }, { 0, 63, 0 } } },
532 { { { 21, 0, 1 }, { 1, 62, 0 } } },
533 { { { 21, 0, 2 }, { 1, 63, 0 } } },
534 { { { 22, 0, 1 }, { 10, 46, 0 } } },
535 { { { 22, 0, 0 }, { 2, 62, 0 } } },
536 { { { 22, 0, 1 }, { 2, 63, 0 } } },
537 { { { 22, 0, 2 }, { 3, 62, 0 } } },
538 { { { 23, 0, 1 }, { 11, 47, 0 } } },
539 { { { 23, 0, 0 }, { 3, 63, 0 } } },
540 { { { 23, 0, 1 }, { 4, 62, 0 } } },
541 { { { 23, 0, 2 }, { 4, 63, 0 } } },
542 { { { 24, 0, 1 }, { 13, 46, 0 } } },
543 { { { 24, 0, 0 }, { 5, 62, 0 } } },
544 { { { 24, 0, 1 }, { 5, 63, 0 } } },
545 { { { 24, 0, 2 }, { 6, 62, 0 } } },
546 { { { 25, 0, 1 }, { 14, 47, 0 } } },
547 { { { 25, 0, 0 }, { 6, 63, 0 } } },
548 { { { 25, 0, 1 }, { 7, 62, 0 } } },
549 { { { 25, 0, 2 }, { 7, 63, 0 } } },
550 { { { 26, 0, 1 }, { 16, 45, 0 } } },
551 { { { 26, 0, 0 }, { 8, 62, 0 } } },
552 { { { 26, 0, 1 }, { 8, 63, 0 } } },
553 { { { 26, 0, 2 }, { 9, 62, 0 } } },
554 { { { 27, 0, 1 }, { 16, 48, 0 } } },
555 { { { 27, 0, 0 }, { 9, 63, 0 } } },
556 { { { 27, 0, 1 }, { 10, 62, 0 } } },
557 { { { 27, 0, 2 }, { 10, 63, 0 } } },
558 { { { 28, 0, 1 }, { 16, 51, 0 } } },
559 { { { 28, 0, 0 }, { 11, 62, 0 } } },
560 { { { 28, 0, 1 }, { 11, 63, 0 } } },
561 { { { 28, 0, 2 }, { 12, 62, 0 } } },
562 { { { 29, 0, 1 }, { 16, 54, 0 } } },
563 { { { 29, 0, 0 }, { 12, 63, 0 } } },
564 { { { 29, 0, 1 }, { 13, 62, 0 } } },
565 { { { 29, 0, 2 }, { 13, 63, 0 } } },
566 { { { 30, 0, 1 }, { 16, 57, 0 } } },
567 { { { 30, 0, 0 }, { 14, 62, 0 } } },
568 { { { 30, 0, 1 }, { 14, 63, 0 } } },
569 { { { 30, 0, 2 }, { 15, 62, 0 } } },
570 { { { 31, 0, 1 }, { 16, 60, 0 } } },
571 { { { 31, 0, 0 }, { 15, 63, 0 } } },
572 { { { 31, 0, 1 }, { 24, 46, 0 } } },
573 { { { 31, 0, 2 }, { 16, 62, 0 } } },
574 { { { 32, 0, 2 }, { 16, 63, 0 } } },
575 { { { 32, 0, 1 }, { 17, 62, 0 } } },
576 { { { 32, 0, 0 }, { 25, 47, 0 } } },
577 { { { 32, 0, 1 }, { 17, 63, 0 } } },
578 { { { 32, 0, 2 }, { 18, 62, 0 } } },
579 { { { 33, 0, 1 }, { 18, 63, 0 } } },
580 { { { 33, 0, 0 }, { 27, 46, 0 } } },
581 { { { 33, 0, 1 }, { 19, 62, 0 } } },
582 { { { 33, 0, 2 }, { 19, 63, 0 } } },
583 { { { 34, 0, 1 }, { 20, 62, 0 } } },
584 { { { 34, 0, 0 }, { 28, 47, 0 } } },
585 { { { 34, 0, 1 }, { 20, 63, 0 } } },
586 { { { 34, 0, 2 }, { 21, 62, 0 } } },
587 { { { 35, 0, 1 }, { 21, 63, 0 } } },
588 { { { 35, 0, 0 }, { 30, 46, 0 } } },
589 { { { 35, 0, 1 }, { 22, 62, 0 } } },
590 { { { 35, 0, 2 }, { 22, 63, 0 } } },
591 { { { 36, 0, 1 }, { 23, 62, 0 } } },
592 { { { 36, 0, 0 }, { 31, 47, 0 } } },
593 { { { 36, 0, 1 }, { 23, 63, 0 } } },
594 { { { 36, 0, 2 }, { 24, 62, 0 } } },
595 { { { 37, 0, 1 }, { 24, 63, 0 } } },
596 { { { 37, 0, 0 }, { 32, 47, 0 } } },
597 { { { 37, 0, 1 }, { 25, 62, 0 } } },
598 { { { 37, 0, 2 }, { 25, 63, 0 } } },
599 { { { 38, 0, 1 }, { 26, 62, 0 } } },
600 { { { 38, 0, 0 }, { 32, 50, 0 } } },
601 { { { 38, 0, 1 }, { 26, 63, 0 } } },
602 { { { 38, 0, 2 }, { 27, 62, 0 } } },
603 { { { 39, 0, 1 }, { 27, 63, 0 } } },
604 { { { 39, 0, 0 }, { 32, 53, 0 } } },
605 { { { 39, 0, 1 }, { 28, 62, 0 } } },
606 { { { 39, 0, 2 }, { 28, 63, 0 } } },
607 { { { 40, 0, 1 }, { 29, 62, 0 } } },
608 { { { 40, 0, 0 }, { 32, 56, 0 } } },
609 { { { 40, 0, 1 }, { 29, 63, 0 } } },
610 { { { 40, 0, 2 }, { 30, 62, 0 } } },
611 { { { 41, 0, 1 }, { 30, 63, 0 } } },
612 { { { 41, 0, 0 }, { 32, 59, 0 } } },
613 { { { 41, 0, 1 }, { 31, 62, 0 } } },
614 { { { 41, 0, 2 }, { 31, 63, 0 } } },
615 { { { 42, 0, 1 }, { 32, 61, 0 } } },
616 { { { 42, 0, 0 }, { 32, 62, 0 } } },
617 { { { 42, 0, 1 }, { 32, 63, 0 } } },
618 { { { 42, 0, 2 }, { 41, 46, 0 } } },
619 { { { 43, 0, 1 }, { 33, 62, 0 } } },
620 { { { 43, 0, 0 }, { 33, 63, 0 } } },
621 { { { 43, 0, 1 }, { 34, 62, 0 } } },
622 { { { 43, 0, 2 }, { 42, 47, 0 } } },
623 { { { 44, 0, 1 }, { 34, 63, 0 } } },
624 { { { 44, 0, 0 }, { 35, 62, 0 } } },
625 { { { 44, 0, 1 }, { 35, 63, 0 } } },
626 { { { 44, 0, 2 }, { 44, 46, 0 } } },
627 { { { 45, 0, 1 }, { 36, 62, 0 } } },
628 { { { 45, 0, 0 }, { 36, 63, 0 } } },
629 { { { 45, 0, 1 }, { 37, 62, 0 } } },
630 { { { 45, 0, 2 }, { 45, 47, 0 } } },
631 { { { 46, 0, 1 }, { 37, 63, 0 } } },
632 { { { 46, 0, 0 }, { 38, 62, 0 } } },
633 { { { 46, 0, 1 }, { 38, 63, 0 } } },
634 { { { 46, 0, 2 }, { 47, 46, 0 } } },
635 { { { 47, 0, 1 }, { 39, 62, 0 } } },
636 { { { 47, 0, 0 }, { 39, 63, 0 } } },
637 { { { 47, 0, 1 }, { 40, 62, 0 } } },
638 { { { 47, 0, 2 }, { 48, 46, 0 } } },
639 { { { 48, 0, 2 }, { 40, 63, 0 } } },
640 { { { 48, 0, 1 }, { 41, 62, 0 } } },
641 { { { 48, 0, 0 }, { 41, 63, 0 } } },
642 { { { 48, 0, 1 }, { 48, 49, 0 } } },
643 { { { 48, 0, 2 }, { 42, 62, 0 } } },
644 { { { 49, 0, 1 }, { 42, 63, 0 } } },
645 { { { 49, 0, 0 }, { 43, 62, 0 } } },
646 { { { 49, 0, 1 }, { 48, 52, 0 } } },
647 { { { 49, 0, 2 }, { 43, 63, 0 } } },
648 { { { 50, 0, 1 }, { 44, 62, 0 } } },
649 { { { 50, 0, 0 }, { 44, 63, 0 } } },
650 { { { 50, 0, 1 }, { 48, 55, 0 } } },
651 { { { 50, 0, 2 }, { 45, 62, 0 } } },
652 { { { 51, 0, 1 }, { 45, 63, 0 } } },
653 { { { 51, 0, 0 }, { 46, 62, 0 } } },
654 { { { 51, 0, 1 }, { 48, 58, 0 } } },
655 { { { 51, 0, 2 }, { 46, 63, 0 } } },
656 { { { 52, 0, 1 }, { 47, 62, 0 } } },
657 { { { 52, 0, 0 }, { 47, 63, 0 } } },
658 { { { 52, 0, 1 }, { 48, 61, 0 } } },
659 { { { 52, 0, 2 }, { 48, 62, 0 } } },
660 { { { 53, 0, 1 }, { 56, 47, 0 } } },
661 { { { 53, 0, 0 }, { 48, 63, 0 } } },
662 { { { 53, 0, 1 }, { 49, 62, 0 } } },
663 { { { 53, 0, 2 }, { 49, 63, 0 } } },
664 { { { 54, 0, 1 }, { 58, 46, 0 } } },
665 { { { 54, 0, 0 }, { 50, 62, 0 } } },
666 { { { 54, 0, 1 }, { 50, 63, 0 } } },
667 { { { 54, 0, 2 }, { 51, 62, 0 } } },
668 { { { 55, 0, 1 }, { 59, 47, 0 } } },
669 { { { 55, 0, 0 }, { 51, 63, 0 } } },
670 { { { 55, 0, 1 }, { 52, 62, 0 } } },
671 { { { 55, 0, 2 }, { 52, 63, 0 } } },
672 { { { 56, 0, 1 }, { 61, 46, 0 } } },
673 { { { 56, 0, 0 }, { 53, 62, 0 } } },
674 { { { 56, 0, 1 }, { 53, 63, 0 } } },
675 { { { 56, 0, 2 }, { 54, 62, 0 } } },
676 { { { 57, 0, 1 }, { 62, 47, 0 } } },
677 { { { 57, 0, 0 }, { 54, 63, 0 } } },
678 { { { 57, 0, 1 }, { 55, 62, 0 } } },
679 { { { 57, 0, 2 }, { 55, 63, 0 } } },
680 { { { 58, 0, 1 }, { 56, 62, 1 } } },
681 { { { 58, 0, 0 }, { 56, 62, 0 } } },
682 { { { 58, 0, 1 }, { 56, 63, 0 } } },
683 { { { 58, 0, 2 }, { 57, 62, 0 } } },
684 { { { 59, 0, 1 }, { 57, 63, 1 } } },
685 { { { 59, 0, 0 }, { 57, 63, 0 } } },
686 { { { 59, 0, 1 }, { 58, 62, 0 } } },
687 { { { 59, 0, 2 }, { 58, 63, 0 } } },
688 { { { 60, 0, 1 }, { 59, 62, 1 } } },
689 { { { 60, 0, 0 }, { 59, 62, 0 } } },
690 { { { 60, 0, 1 }, { 59, 63, 0 } } },
691 { { { 60, 0, 2 }, { 60, 62, 0 } } },
692 { { { 61, 0, 1 }, { 60, 63, 1 } } },
693 { { { 61, 0, 0 }, { 60, 63, 0 } } },
694 { { { 61, 0, 1 }, { 61, 62, 0 } } },
695 { { { 61, 0, 2 }, { 61, 63, 0 } } },
696 { { { 62, 0, 1 }, { 62, 62, 1 } } },
697 { { { 62, 0, 0 }, { 62, 62, 0 } } },
698 { { { 62, 0, 1 }, { 62, 63, 0 } } },
699 { { { 62, 0, 2 }, { 63, 62, 0 } } },
700 { { { 63, 0, 1 }, { 63, 63, 1 } } },
701 { { { 63, 0, 0 }, { 63, 63, 0 } } }
704 static const DDSSingleColourLookup*
715 #define C565_r(x) (((x) & 0xF800) >> 11)
716 #define C565_g(x) (((x) & 0x07E0) >> 5)
717 #define C565_b(x) ((x) & 0x001F)
719 #define C565_red(x) ( (C565_r(x) << 3 | C565_r(x) >> 2))
720 #define C565_green(x) ( (C565_g(x) << 2 | C565_g(x) >> 4))
721 #define C565_blue(x) ( (C565_b(x) << 3 | C565_b(x) >> 2))
723 #define DIV2(x) ((x) > 1 ? ((x) >> 1) : 1)
725 #define FixRange(min, max, steps) \
728 if (max - min < steps) \
729 max = Min(min + steps, 255); \
730 if (max - min < steps) \
731 min = Max(min - steps, 0)
733 #define Dot(left, right) (left.x*right.x) + (left.y*right.y) + (left.z*right.z)
735 #define VectorInit(vector, value) vector.x = vector.y = vector.z = vector.w \
737 #define VectorInit3(vector, value) vector.x = vector.y = vector.z = value
739 #define IsBitMask(mask, r, g, b, a) (mask.r_bitmask == r && mask.g_bitmask == \
740 g && mask.b_bitmask == b && mask.alpha_bitmask == a)
745 static MagickBooleanType
746 ConstructOrdering(const size_t, const DDSVector4 *, const DDSVector3,
747 DDSVector4 *, DDSVector4 *, unsigned char *, size_t);
749 static MagickBooleanType
750 ReadDDSInfo(Image *, DDSInfo *);
753 CalculateColors(unsigned short, unsigned short,
754 DDSColors *, MagickBooleanType);
756 static MagickBooleanType
757 ReadDXT1(Image *, DDSInfo *, ExceptionInfo *);
759 static MagickBooleanType
760 ReadDXT3(Image *, DDSInfo *, ExceptionInfo *);
762 static MagickBooleanType
763 ReadDXT5(Image *, DDSInfo *, ExceptionInfo *);
765 static MagickBooleanType
766 ReadUncompressedRGB(Image *, DDSInfo *, ExceptionInfo *);
768 static MagickBooleanType
769 ReadUncompressedRGBA(Image *, DDSInfo *, ExceptionInfo *);
772 RemapIndices(const ssize_t *, const unsigned char *, unsigned char *);
775 SkipDXTMipmaps(Image *, DDSInfo *, int);
778 SkipRGBMipmaps(Image *, DDSInfo *, int);
781 MagickBooleanType WriteDDSImage(const ImageInfo *, Image *, ExceptionInfo *);
784 WriteDDSInfo(Image *, const size_t, const size_t, const size_t);
787 WriteFourCC(Image *, const size_t, const MagickBooleanType,
788 const MagickBooleanType, ExceptionInfo *);
791 WriteImageData(Image *, const size_t, const size_t, const MagickBooleanType,
792 const MagickBooleanType, ExceptionInfo *);
795 WriteIndices(Image *, const DDSVector3, const DDSVector3, unsigned char *);
797 static MagickBooleanType
798 WriteMipmaps(Image *, const size_t, const size_t, const size_t,
799 const MagickBooleanType, const MagickBooleanType, ExceptionInfo *);
802 WriteSingleColorFit(Image *, const DDSVector4 *, const ssize_t *);
805 WriteUncompressed(Image *, ExceptionInfo *);
807 static inline size_t Max(size_t one, size_t two)
814 static inline float MaxF(float one, float two)
821 static inline size_t Min(size_t one, size_t two)
828 static inline float MinF(float one, float two)
835 static inline void VectorAdd(const DDSVector4 left, const DDSVector4 right,
836 DDSVector4 *destination)
838 destination->x = left.x + right.x;
839 destination->y = left.y + right.y;
840 destination->z = left.z + right.z;
841 destination->w = left.w + right.w;
844 static inline void VectorClamp(DDSVector4 *value)
846 value->x = MinF(1.0f,MaxF(0.0f,value->x));
847 value->y = MinF(1.0f,MaxF(0.0f,value->y));
848 value->z = MinF(1.0f,MaxF(0.0f,value->z));
849 value->w = MinF(1.0f,MaxF(0.0f,value->w));
852 static inline void VectorClamp3(DDSVector3 *value)
854 value->x = MinF(1.0f,MaxF(0.0f,value->x));
855 value->y = MinF(1.0f,MaxF(0.0f,value->y));
856 value->z = MinF(1.0f,MaxF(0.0f,value->z));
859 static inline void VectorCopy43(const DDSVector4 source,
860 DDSVector3 *destination)
862 destination->x = source.x;
863 destination->y = source.y;
864 destination->z = source.z;
867 static inline void VectorCopy44(const DDSVector4 source,
868 DDSVector4 *destination)
870 destination->x = source.x;
871 destination->y = source.y;
872 destination->z = source.z;
873 destination->w = source.w;
876 static inline void VectorNegativeMultiplySubtract(const DDSVector4 a,
877 const DDSVector4 b, const DDSVector4 c, DDSVector4 *destination)
879 destination->x = c.x - (a.x * b.x);
880 destination->y = c.y - (a.y * b.y);
881 destination->z = c.z - (a.z * b.z);
882 destination->w = c.w - (a.w * b.w);
885 static inline void VectorMultiply(const DDSVector4 left,
886 const DDSVector4 right, DDSVector4 *destination)
888 destination->x = left.x * right.x;
889 destination->y = left.y * right.y;
890 destination->z = left.z * right.z;
891 destination->w = left.w * right.w;
894 static inline void VectorMultiply3(const DDSVector3 left,
895 const DDSVector3 right, DDSVector3 *destination)
897 destination->x = left.x * right.x;
898 destination->y = left.y * right.y;
899 destination->z = left.z * right.z;
902 static inline void VectorMultiplyAdd(const DDSVector4 a, const DDSVector4 b,
903 const DDSVector4 c, DDSVector4 *destination)
905 destination->x = (a.x * b.x) + c.x;
906 destination->y = (a.y * b.y) + c.y;
907 destination->z = (a.z * b.z) + c.z;
908 destination->w = (a.w * b.w) + c.w;
911 static inline void VectorMultiplyAdd3(const DDSVector3 a, const DDSVector3 b,
912 const DDSVector3 c, DDSVector3 *destination)
914 destination->x = (a.x * b.x) + c.x;
915 destination->y = (a.y * b.y) + c.y;
916 destination->z = (a.z * b.z) + c.z;
919 static inline void VectorReciprocal(const DDSVector4 value,
920 DDSVector4 *destination)
922 destination->x = 1.0f / value.x;
923 destination->y = 1.0f / value.y;
924 destination->z = 1.0f / value.z;
925 destination->w = 1.0f / value.w;
928 static inline void VectorSubtract(const DDSVector4 left,
929 const DDSVector4 right, DDSVector4 *destination)
931 destination->x = left.x - right.x;
932 destination->y = left.y - right.y;
933 destination->z = left.z - right.z;
934 destination->w = left.w - right.w;
937 static inline void VectorSubtract3(const DDSVector3 left,
938 const DDSVector3 right, DDSVector3 *destination)
940 destination->x = left.x - right.x;
941 destination->y = left.y - right.y;
942 destination->z = left.z - right.z;
945 static inline void VectorTruncate(DDSVector4 *value)
947 value->x = value->x > 0.0f ? floor(value->x) : ceil(value->x);
948 value->y = value->y > 0.0f ? floor(value->y) : ceil(value->y);
949 value->z = value->z > 0.0f ? floor(value->z) : ceil(value->z);
950 value->w = value->w > 0.0f ? floor(value->w) : ceil(value->w);
953 static inline void VectorTruncate3(DDSVector3 *value)
955 value->x = value->x > 0.0f ? floor(value->x) : ceil(value->x);
956 value->y = value->y > 0.0f ? floor(value->y) : ceil(value->y);
957 value->z = value->z > 0.0f ? floor(value->z) : ceil(value->z);
960 static void CalculateColors(unsigned short c0, unsigned short c1,
961 DDSColors *c, MagickBooleanType ignoreAlpha)
963 c->a[0] = c->a[1] = c->a[2] = c->a[3] = 0;
965 c->r[0] = (unsigned char) C565_red(c0);
966 c->g[0] = (unsigned char) C565_green(c0);
967 c->b[0] = (unsigned char) C565_blue(c0);
969 c->r[1] = (unsigned char) C565_red(c1);
970 c->g[1] = (unsigned char) C565_green(c1);
971 c->b[1] = (unsigned char) C565_blue(c1);
973 if (ignoreAlpha != MagickFalse || c0 > c1)
975 c->r[2] = (unsigned char) ((2 * c->r[0] + c->r[1]) / 3);
976 c->g[2] = (unsigned char) ((2 * c->g[0] + c->g[1]) / 3);
977 c->b[2] = (unsigned char) ((2 * c->b[0] + c->b[1]) / 3);
979 c->r[3] = (unsigned char) ((c->r[0] + 2 * c->r[1]) / 3);
980 c->g[3] = (unsigned char) ((c->g[0] + 2 * c->g[1]) / 3);
981 c->b[3] = (unsigned char) ((c->b[0] + 2 * c->b[1]) / 3);
985 c->r[2] = (unsigned char) ((c->r[0] + c->r[1]) / 2);
986 c->g[2] = (unsigned char) ((c->g[0] + c->g[1]) / 2);
987 c->b[2] = (unsigned char) ((c->b[0] + c->b[1]) / 2);
989 c->r[3] = c->g[3] = c->b[3] = 0;
994 static size_t CompressAlpha(const size_t min, const size_t max,
995 const size_t steps, const ssize_t *alphas, unsigned char* indices)
1010 codes[0] = (unsigned char) min;
1011 codes[1] = (unsigned char) max;
1015 for (i=1; i < (ssize_t) steps; i++)
1016 codes[i+1] = (unsigned char) (((steps-i)*min + i*max) / steps);
1019 for (i=0; i<16; i++)
1021 if (alphas[i] == -1)
1035 dist = value - (size_t)codes[j];
1045 indices[i] = (unsigned char)index;
1052 static void CompressClusterFit(const size_t count,
1053 const DDSVector4 *points, const ssize_t *map, const DDSVector3 principle,
1054 const DDSVector4 metric, DDSVector3 *start, DDSVector3* end,
1055 unsigned char *indices)
1068 twothirds_twothirds2,
1089 VectorInit(half,0.5f);
1090 VectorInit(two,2.0f);
1092 VectorInit(onethird_onethird2,1.0f/3.0f);
1093 onethird_onethird2.w = 1.0f/9.0f;
1094 VectorInit(twothirds_twothirds2,2.0f/3.0f);
1095 twothirds_twothirds2.w = 4.0f/9.0f;
1096 VectorInit(twonineths,2.0f/9.0f);
1103 gridrcp.x = 1.0f/31.0f;
1104 gridrcp.y = 1.0f/63.0f;
1105 gridrcp.z = 1.0f/31.0f;
1113 ConstructOrdering(count,points,principle,pointsWeights,&xSumwSum,order,0);
1115 for (iterationIndex = 0;;)
1117 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1118 #pragma omp parallel for schedule(dynamic,1) \
1119 num_threads(GetMagickResourceLimit(ThreadResource))
1121 for (i=0; i < (ssize_t) count; i++)
1134 VectorInit(part0,0.0f);
1135 for(ii=0; ii < (size_t) i; ii++)
1136 VectorAdd(pointsWeights[ii],part0,&part0);
1138 VectorInit(part1,0.0f);
1139 for (j=(size_t) i;;)
1143 VectorCopy44(pointsWeights[0],&part2);
1148 VectorInit(part2,0.0f);
1170 VectorSubtract(xSumwSum,part2,&part3);
1171 VectorSubtract(part3,part1,&part3);
1172 VectorSubtract(part3,part0,&part3);
1174 VectorMultiplyAdd(part1,twothirds_twothirds2,part0,&alphax_sum);
1175 VectorMultiplyAdd(part2,onethird_onethird2,alphax_sum,&alphax_sum);
1176 VectorInit(alpha2_sum,alphax_sum.w);
1178 VectorMultiplyAdd(part2,twothirds_twothirds2,part3,&betax_sum);
1179 VectorMultiplyAdd(part1,onethird_onethird2,betax_sum,&betax_sum);
1180 VectorInit(beta2_sum,betax_sum.w);
1182 VectorAdd(part1,part2,&alphabeta_sum);
1183 VectorInit(alphabeta_sum,alphabeta_sum.w);
1184 VectorMultiply(twonineths,alphabeta_sum,&alphabeta_sum);
1186 VectorMultiply(alpha2_sum,beta2_sum,&factor);
1187 VectorNegativeMultiplySubtract(alphabeta_sum,alphabeta_sum,factor,
1189 VectorReciprocal(factor,&factor);
1191 VectorMultiply(alphax_sum,beta2_sum,&a);
1192 VectorNegativeMultiplySubtract(betax_sum,alphabeta_sum,a,&a);
1193 VectorMultiply(a,factor,&a);
1195 VectorMultiply(betax_sum,alpha2_sum,&b);
1196 VectorNegativeMultiplySubtract(alphax_sum,alphabeta_sum,b,&b);
1197 VectorMultiply(b,factor,&b);
1200 VectorMultiplyAdd(grid,a,half,&a);
1202 VectorMultiply(a,gridrcp,&a);
1205 VectorMultiplyAdd(grid,b,half,&b);
1207 VectorMultiply(b,gridrcp,&b);
1209 VectorMultiply(b,b,&e1);
1210 VectorMultiply(e1,beta2_sum,&e1);
1211 VectorMultiply(a,a,&e2);
1212 VectorMultiplyAdd(e2,alpha2_sum,e1,&e1);
1214 VectorMultiply(a,b,&e2);
1215 VectorMultiply(e2,alphabeta_sum,&e2);
1216 VectorNegativeMultiplySubtract(a,alphax_sum,e2,&e2);
1217 VectorNegativeMultiplySubtract(b,betax_sum,e2,&e2);
1218 VectorMultiplyAdd(two,e2,e1,&e2);
1219 VectorMultiply(e2,metric,&e2);
1221 error = e2.x + e2.y + e2.z;
1223 if (error < bestError)
1225 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1226 #pragma omp critical (DDS_CompressClusterFit)
1229 if (error < bestError)
1231 VectorCopy43(a,start);
1232 VectorCopy43(b,end);
1237 bestIteration = iterationIndex;
1245 VectorAdd(pointsWeights[k],part2,&part2);
1252 VectorAdd(pointsWeights[j],part1,&part1);
1257 if (bestIteration != iterationIndex)
1261 if (iterationIndex == 8)
1264 VectorSubtract3(*end,*start,&axis);
1265 if (ConstructOrdering(count,points,axis,pointsWeights,&xSumwSum,order,
1266 iterationIndex) == MagickFalse)
1270 o = order + (16*bestIteration);
1272 for (i=0; i < besti; i++)
1273 unordered[o[i]] = 0;
1274 for (i=besti; i < bestj; i++)
1275 unordered[o[i]] = 2;
1276 for (i=bestj; i < bestk; i++)
1277 unordered[o[i]] = 3;
1278 for (i=bestk; i < count; i++)
1279 unordered[o[i]] = 1;
1281 RemapIndices(map,unordered,indices);
1284 static void CompressRangeFit(const size_t count,
1285 const DDSVector4* points, const ssize_t *map, const DDSVector3 principle,
1286 const DDSVector4 metric, DDSVector3 *start, DDSVector3 *end,
1287 unsigned char *indices)
1313 VectorInit3(half,0.5f);
1319 gridrcp.x = 1.0f/31.0f;
1320 gridrcp.y = 1.0f/63.0f;
1321 gridrcp.z = 1.0f/31.0f;
1325 VectorCopy43(points[0],start);
1326 VectorCopy43(points[0],end);
1328 min = max = Dot(points[0],principle);
1329 for (i=1; i < (ssize_t) count; i++)
1331 val = Dot(points[i],principle);
1334 VectorCopy43(points[i],start);
1339 VectorCopy43(points[i],end);
1345 VectorClamp3(start);
1346 VectorMultiplyAdd3(grid,*start,half,start);
1347 VectorTruncate3(start);
1348 VectorMultiply3(*start,gridrcp,start);
1351 VectorMultiplyAdd3(grid,*end,half,end);
1352 VectorTruncate3(end);
1353 VectorMultiply3(*end,gridrcp,end);
1357 codes[2].x = (start->x * (2.0f/3.0f)) + (end->x * (1.0f/3.0f));
1358 codes[2].y = (start->y * (2.0f/3.0f)) + (end->y * (1.0f/3.0f));
1359 codes[2].z = (start->z * (2.0f/3.0f)) + (end->z * (1.0f/3.0f));
1360 codes[3].x = (start->x * (1.0f/3.0f)) + (end->x * (2.0f/3.0f));
1361 codes[3].y = (start->y * (1.0f/3.0f)) + (end->y * (2.0f/3.0f));
1362 codes[3].z = (start->z * (1.0f/3.0f)) + (end->z * (2.0f/3.0f));
1364 for (i=0; i < (ssize_t) count; i++)
1368 for (j=0; j < 4; j++)
1370 dist.x = (points[i].x - codes[j].x) * metric.x;
1371 dist.y = (points[i].y - codes[j].y) * metric.y;
1372 dist.z = (points[i].z - codes[j].z) * metric.z;
1382 closest[i] = (unsigned char) bestj;
1385 RemapIndices(map, closest, indices);
1388 static void ComputeEndPoints(const DDSSingleColourLookup *lookup[],
1389 const unsigned char *color, DDSVector3 *start, DDSVector3 *end,
1390 unsigned char *index)
1397 maxError = SIZE_MAX;
1399 for (i=0; i < 2; i++)
1401 const DDSSourceBlock*
1407 for (c=0; c < 3; c++)
1409 sources[c] = &lookup[c][color[c]].sources[i];
1410 error += ((size_t) sources[c]->error) * ((size_t) sources[c]->error);
1413 if (error > maxError)
1416 start->x = (float) sources[0]->start / 31.0f;
1417 start->y = (float) sources[1]->start / 63.0f;
1418 start->z = (float) sources[2]->start / 31.0f;
1420 end->x = (float) sources[0]->end / 31.0f;
1421 end->y = (float) sources[1]->end / 63.0f;
1422 end->z = (float) sources[2]->end / 31.0f;
1424 *index = (unsigned char) (2*i);
1429 static void ComputePrincipleComponent(const float *covariance,
1430 DDSVector3 *principle)
1441 row0.x = covariance[0];
1442 row0.y = covariance[1];
1443 row0.z = covariance[2];
1446 row1.x = covariance[1];
1447 row1.y = covariance[3];
1448 row1.z = covariance[4];
1451 row2.x = covariance[2];
1452 row2.y = covariance[4];
1453 row2.z = covariance[5];
1458 for (i=0; i < 8; i++)
1471 w.x = (row1.x * v.y) + w.x;
1472 w.y = (row1.y * v.y) + w.y;
1473 w.z = (row1.z * v.y) + w.z;
1474 w.w = (row1.w * v.y) + w.w;
1476 w.x = (row2.x * v.z) + w.x;
1477 w.y = (row2.y * v.z) + w.y;
1478 w.z = (row2.z * v.z) + w.z;
1479 w.w = (row2.w * v.z) + w.w;
1481 a = 1.0f / MaxF(w.x,MaxF(w.y,w.z));
1489 VectorCopy43(v,principle);
1492 static void ComputeWeightedCovariance(const size_t count,
1493 const DDSVector4 *points, float *covariance)
1505 VectorInit3(centroid,0.0f);
1507 for (i=0; i < count; i++)
1509 total += points[i].w;
1510 centroid.x += (points[i].x * points[i].w);
1511 centroid.y += (points[i].y * points[i].w);
1512 centroid.z += (points[i].z * points[i].w);
1515 if( total > 1.192092896e-07F)
1517 centroid.x /= total;
1518 centroid.y /= total;
1519 centroid.z /= total;
1522 for (i=0; i < 6; i++)
1523 covariance[i] = 0.0f;
1525 for (i = 0; i < count; i++)
1531 a.x = points[i].x - centroid.x;
1532 a.y = points[i].y - centroid.y;
1533 a.z = points[i].z - centroid.z;
1535 b.x = points[i].w * a.x;
1536 b.y = points[i].w * a.y;
1537 b.z = points[i].w * a.z;
1539 covariance[0] += a.x*b.x;
1540 covariance[1] += a.x*b.y;
1541 covariance[2] += a.x*b.z;
1542 covariance[3] += a.y*b.y;
1543 covariance[4] += a.y*b.z;
1544 covariance[5] += a.z*b.z;
1548 static MagickBooleanType ConstructOrdering(const size_t count,
1549 const DDSVector4 *points, const DDSVector3 axis, DDSVector4 *pointsWeights,
1550 DDSVector4 *xSumwSum, unsigned char *order, size_t iteration)
1567 o = order + (16*iteration);
1569 for (i=0; i < (ssize_t) count; i++)
1571 dps[i] = Dot(points[i],axis);
1572 o[i] = (unsigned char)i;
1575 for (i=0; i < (ssize_t) count; i++)
1577 for (j=i; j > 0 && dps[j] < dps[j - 1]; j--)
1580 dps[j] = dps[j - 1];
1589 for (i=0; i < (ssize_t) iteration; i++)
1597 for (j=0; j < count; j++)
1606 if (same != MagickFalse)
1615 for (i=0; i < (ssize_t) count; i++)
1622 v.x = points[j].w * points[j].x;
1623 v.y = points[j].w * points[j].y;
1624 v.z = points[j].w * points[j].z;
1625 v.w = points[j].w * 1.0f;
1627 VectorCopy44(v,&pointsWeights[i]);
1628 VectorAdd(*xSumwSum,v,xSumwSum);
1635 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1643 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1645 % IsDDS() returns MagickTrue if the image format type, identified by the
1646 % magick string, is DDS.
1648 % The format of the IsDDS method is:
1650 % MagickBooleanType IsDDS(const unsigned char *magick,const size_t length)
1652 % A description of each parameter follows:
1654 % o magick: compare image format pattern against these bytes.
1656 % o length: Specifies the length of the magick string.
1659 static MagickBooleanType IsDDS(const unsigned char *magick, const size_t length)
1662 return(MagickFalse);
1663 if (LocaleNCompare((char *) magick,"DDS ", 4) == 0)
1665 return(MagickFalse);
1668 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1672 % R e a d D D S I m a g e %
1676 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1678 % ReadDDSImage() reads a DirectDraw Surface image file and returns it. It
1679 % allocates the memory necessary for the new Image structure and returns a
1680 % pointer to the new image.
1682 % The format of the ReadDDSImage method is:
1684 % Image *ReadDDSImage(const ImageInfo *image_info,ExceptionInfo *exception)
1686 % A description of each parameter follows:
1688 % o image_info: The image info.
1690 % o exception: return any errors or warnings in this structure.
1694 static Image *ReadDDSImage(const ImageInfo *image_info,ExceptionInfo *exception)
1701 cubemap = MagickFalse,
1702 volume = MagickFalse;
1723 assert(image_info != (const ImageInfo *) NULL);
1724 assert(image_info->signature == MagickSignature);
1725 if (image_info->debug != MagickFalse)
1726 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
1727 image_info->filename);
1728 assert(exception != (ExceptionInfo *) NULL);
1729 assert(exception->signature == MagickSignature);
1730 image=AcquireImage(image_info,exception);
1731 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
1732 if (status == MagickFalse)
1734 image=DestroyImageList(image);
1735 return((Image *) NULL);
1739 Initialize image structure.
1741 if (ReadDDSInfo(image, &dds_info) != MagickTrue) {
1742 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1745 if (dds_info.ddscaps2 & DDSCAPS2_CUBEMAP)
1746 cubemap = MagickTrue;
1748 if (dds_info.ddscaps2 & DDSCAPS2_VOLUME && dds_info.depth > 0)
1749 volume = MagickTrue;
1751 (void) SeekBlob(image, 128, SEEK_SET);
1754 Determine pixel format
1756 if (dds_info.pixelformat.flags & DDPF_RGB)
1758 compression = NoCompression;
1759 if (dds_info.pixelformat.flags & DDPF_ALPHAPIXELS)
1761 alpha_trait = BlendPixelTrait;
1762 decoder = ReadUncompressedRGBA;
1766 alpha_trait = UndefinedPixelTrait;
1767 decoder = ReadUncompressedRGB;
1770 else if (dds_info.pixelformat.flags & DDPF_LUMINANCE)
1772 compression = NoCompression;
1773 if (dds_info.pixelformat.flags & DDPF_ALPHAPIXELS)
1775 /* Not sure how to handle this */
1776 ThrowReaderException(CorruptImageError, "ImageTypeNotSupported");
1780 alpha_trait = UndefinedPixelTrait;
1781 decoder = ReadUncompressedRGB;
1784 else if (dds_info.pixelformat.flags & DDPF_FOURCC)
1786 switch (dds_info.pixelformat.fourcc)
1790 alpha_trait = UndefinedPixelTrait;
1791 compression = DXT1Compression;
1797 alpha_trait = BlendPixelTrait;
1798 compression = DXT3Compression;
1804 alpha_trait = BlendPixelTrait;
1805 compression = DXT5Compression;
1811 /* Unknown FOURCC */
1812 ThrowReaderException(CorruptImageError, "ImageTypeNotSupported");
1818 /* Neither compressed nor uncompressed... thus unsupported */
1819 ThrowReaderException(CorruptImageError, "ImageTypeNotSupported");
1826 Determine number of faces defined in the cubemap
1829 if (dds_info.ddscaps2 & DDSCAPS2_CUBEMAP_POSITIVEX) num_images++;
1830 if (dds_info.ddscaps2 & DDSCAPS2_CUBEMAP_NEGATIVEX) num_images++;
1831 if (dds_info.ddscaps2 & DDSCAPS2_CUBEMAP_POSITIVEY) num_images++;
1832 if (dds_info.ddscaps2 & DDSCAPS2_CUBEMAP_NEGATIVEY) num_images++;
1833 if (dds_info.ddscaps2 & DDSCAPS2_CUBEMAP_POSITIVEZ) num_images++;
1834 if (dds_info.ddscaps2 & DDSCAPS2_CUBEMAP_NEGATIVEZ) num_images++;
1838 num_images = dds_info.depth;
1840 for (n = 0; n < num_images; n++)
1844 /* Start a new image */
1845 AcquireNextImage(image_info,image,exception);
1846 if (GetNextImageInList(image) == (Image *) NULL)
1847 return(DestroyImageList(image));
1848 image=SyncNextImageInList(image);
1851 image->alpha_trait=alpha_trait;
1852 image->compression = compression;
1853 image->columns = dds_info.width;
1854 image->rows = dds_info.height;
1855 image->storage_class = DirectClass;
1856 image->endian = LSBEndian;
1858 if (image_info->ping != MagickFalse)
1860 (void) CloseBlob(image);
1861 return(GetFirstImageInList(image));
1863 status=SetImageExtent(image,image->columns,image->rows,exception);
1864 if (status == MagickFalse)
1865 return(DestroyImageList(image));
1866 if ((decoder)(image, &dds_info, exception) != MagickTrue)
1868 (void) CloseBlob(image);
1869 return(GetFirstImageInList(image));
1873 if (EOFBlob(image) != MagickFalse)
1874 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
1877 (void) CloseBlob(image);
1878 return(GetFirstImageInList(image));
1881 static MagickBooleanType ReadDDSInfo(Image *image, DDSInfo *dds_info)
1887 /* Seek to start of header */
1888 (void) SeekBlob(image, 4, SEEK_SET);
1890 /* Check header field */
1891 hdr_size = ReadBlobLSBLong(image);
1892 if (hdr_size != 124)
1895 /* Fill in DDS info struct */
1896 dds_info->flags = ReadBlobLSBLong(image);
1898 /* Check required flags */
1899 required=(size_t) (DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT);
1900 if ((dds_info->flags & required) != required)
1903 dds_info->height = ReadBlobLSBLong(image);
1904 dds_info->width = ReadBlobLSBLong(image);
1905 dds_info->pitchOrLinearSize = ReadBlobLSBLong(image);
1906 dds_info->depth = ReadBlobLSBLong(image);
1907 dds_info->mipmapcount = ReadBlobLSBLong(image);
1909 (void) SeekBlob(image, 44, SEEK_CUR); /* reserved region of 11 DWORDs */
1911 /* Read pixel format structure */
1912 hdr_size = ReadBlobLSBLong(image);
1916 dds_info->pixelformat.flags = ReadBlobLSBLong(image);
1917 dds_info->pixelformat.fourcc = ReadBlobLSBLong(image);
1918 dds_info->pixelformat.rgb_bitcount = ReadBlobLSBLong(image);
1919 dds_info->pixelformat.r_bitmask = ReadBlobLSBLong(image);
1920 dds_info->pixelformat.g_bitmask = ReadBlobLSBLong(image);
1921 dds_info->pixelformat.b_bitmask = ReadBlobLSBLong(image);
1922 dds_info->pixelformat.alpha_bitmask = ReadBlobLSBLong(image);
1924 dds_info->ddscaps1 = ReadBlobLSBLong(image);
1925 dds_info->ddscaps2 = ReadBlobLSBLong(image);
1926 (void) SeekBlob(image, 12, SEEK_CUR); /* 3 reserved DWORDs */
1931 static MagickBooleanType ReadDXT1(Image *image, DDSInfo *dds_info,
1932 ExceptionInfo *exception)
1958 for (y = 0; y < (ssize_t) dds_info->height; y += 4)
1960 for (x = 0; x < (ssize_t) dds_info->width; x += 4)
1962 /* Get 4x4 patch of pixels to write on */
1963 q = QueueAuthenticPixels(image, x, y, Min(4, dds_info->width - x),
1964 Min(4, dds_info->height - y),exception);
1966 if (q == (Quantum *) NULL)
1969 /* Read 8 bytes of data from the image */
1970 c0 = ReadBlobLSBShort(image);
1971 c1 = ReadBlobLSBShort(image);
1972 bits = ReadBlobLSBLong(image);
1974 CalculateColors(c0, c1, &colors, MagickFalse);
1976 /* Write the pixels */
1977 for (j = 0; j < 4; j++)
1979 for (i = 0; i < 4; i++)
1981 if ((x + i) < (ssize_t) dds_info->width &&
1982 (y + j) < (ssize_t) dds_info->height)
1984 code = (unsigned char) ((bits >> ((j*4+i)*2)) & 0x3);
1985 SetPixelRed(image,ScaleCharToQuantum(colors.r[code]),q);
1986 SetPixelGreen(image,ScaleCharToQuantum(colors.g[code]),q);
1987 SetPixelBlue(image,ScaleCharToQuantum(colors.b[code]),q);
1988 SetPixelAlpha(image,ScaleCharToQuantum(colors.a[code]),q);
1989 if (colors.a[code] && (image->alpha_trait == UndefinedPixelTrait))
1990 image->alpha_trait=BlendPixelTrait; /* Correct matte */
1991 q+=GetPixelChannels(image);
1996 if (SyncAuthenticPixels(image,exception) == MagickFalse)
2001 SkipDXTMipmaps(image, dds_info, 8);
2006 static MagickBooleanType ReadDXT3(Image *image, DDSInfo *dds_info,
2007 ExceptionInfo *exception)
2036 for (y = 0; y < (ssize_t) dds_info->height; y += 4)
2038 for (x = 0; x < (ssize_t) dds_info->width; x += 4)
2040 /* Get 4x4 patch of pixels to write on */
2041 q = QueueAuthenticPixels(image, x, y, Min(4, dds_info->width - x),
2042 Min(4, dds_info->height - y),exception);
2044 if (q == (Quantum *) NULL)
2047 /* Read alpha values (8 bytes) */
2048 a0 = ReadBlobLSBLong(image);
2049 a1 = ReadBlobLSBLong(image);
2051 /* Read 8 bytes of data from the image */
2052 c0 = ReadBlobLSBShort(image);
2053 c1 = ReadBlobLSBShort(image);
2054 bits = ReadBlobLSBLong(image);
2056 CalculateColors(c0, c1, &colors, MagickTrue);
2058 /* Write the pixels */
2059 for (j = 0; j < 4; j++)
2061 for (i = 0; i < 4; i++)
2063 if ((x + i) < (ssize_t) dds_info->width && (y + j) < (ssize_t) dds_info->height)
2065 code = (bits >> ((4*j+i)*2)) & 0x3;
2066 SetPixelRed(image,ScaleCharToQuantum(colors.r[code]),q);
2067 SetPixelGreen(image,ScaleCharToQuantum(colors.g[code]),q);
2068 SetPixelBlue(image,ScaleCharToQuantum(colors.b[code]),q);
2070 Extract alpha value: multiply 0..15 by 17 to get range 0..255
2073 alpha = 17U * (unsigned char) ((a0 >> (4*(4*j+i))) & 0xf);
2075 alpha = 17U * (unsigned char) ((a1 >> (4*(4*(j-2)+i))) & 0xf);
2076 SetPixelAlpha(image,ScaleCharToQuantum((unsigned char) alpha),q);
2077 q+=GetPixelChannels(image);
2082 if (SyncAuthenticPixels(image,exception) == MagickFalse)
2087 SkipDXTMipmaps(image, dds_info, 16);
2092 static MagickBooleanType ReadDXT5(Image *image, DDSInfo *dds_info,
2093 ExceptionInfo *exception)
2126 for (y = 0; y < (ssize_t) dds_info->height; y += 4)
2128 for (x = 0; x < (ssize_t) dds_info->width; x += 4)
2130 /* Get 4x4 patch of pixels to write on */
2131 q = QueueAuthenticPixels(image, x, y, Min(4, dds_info->width - x),
2132 Min(4, dds_info->height - y),exception);
2134 if (q == (Quantum *) NULL)
2137 /* Read alpha values (8 bytes) */
2138 a0 = (unsigned char) ReadBlobByte(image);
2139 a1 = (unsigned char) ReadBlobByte(image);
2141 alpha_bits = (MagickSizeType)ReadBlobLSBLong(image);
2142 alpha_bits = alpha_bits | ((MagickSizeType)ReadBlobLSBShort(image) << 32);
2144 /* Read 8 bytes of data from the image */
2145 c0 = ReadBlobLSBShort(image);
2146 c1 = ReadBlobLSBShort(image);
2147 bits = ReadBlobLSBLong(image);
2149 CalculateColors(c0, c1, &colors, MagickTrue);
2151 /* Write the pixels */
2152 for (j = 0; j < 4; j++)
2154 for (i = 0; i < 4; i++)
2156 if ((x + i) < (ssize_t) dds_info->width &&
2157 (y + j) < (ssize_t) dds_info->height)
2159 code = (bits >> ((4*j+i)*2)) & 0x3;
2160 SetPixelRed(image,ScaleCharToQuantum(colors.r[code]),q);
2161 SetPixelGreen(image,ScaleCharToQuantum(colors.g[code]),q);
2162 SetPixelBlue(image,ScaleCharToQuantum(colors.b[code]),q);
2163 /* Extract alpha value */
2164 alpha_code = (size_t) (alpha_bits >> (3*(4*j+i))) & 0x7;
2165 if (alpha_code == 0)
2167 else if (alpha_code == 1)
2170 alpha = ((8-alpha_code) * a0 + (alpha_code-1) * a1) / 7;
2171 else if (alpha_code == 6)
2173 else if (alpha_code == 7)
2176 alpha = (((6-alpha_code) * a0 + (alpha_code-1) * a1) / 5);
2177 SetPixelAlpha(image,ScaleCharToQuantum((unsigned char) alpha),q);
2178 q+=GetPixelChannels(image);
2183 if (SyncAuthenticPixels(image,exception) == MagickFalse)
2188 SkipDXTMipmaps(image, dds_info, 16);
2193 static MagickBooleanType ReadUncompressedRGB(Image *image, DDSInfo *dds_info,
2194 ExceptionInfo *exception)
2205 if (dds_info->pixelformat.rgb_bitcount == 8)
2206 (void) SetImageType(image,GrayscaleType,exception);
2207 else if (dds_info->pixelformat.rgb_bitcount == 16 && !IsBitMask(
2208 dds_info->pixelformat,0xf800,0x07e0,0x001f,0x0000))
2209 ThrowBinaryException(CorruptImageError,"ImageTypeNotSupported",
2212 for (y = 0; y < (ssize_t) dds_info->height; y++)
2214 q = QueueAuthenticPixels(image, 0, y, dds_info->width, 1,exception);
2216 if (q == (Quantum *) NULL)
2219 for (x = 0; x < (ssize_t) dds_info->width; x++)
2221 if (dds_info->pixelformat.rgb_bitcount == 8)
2222 SetPixelGray(image,ScaleCharToQuantum(ReadBlobByte(image)),q);
2223 else if (dds_info->pixelformat.rgb_bitcount == 16)
2225 color=ReadBlobShort(image);
2226 SetPixelRed(image,ScaleCharToQuantum((unsigned char)
2227 (((color >> 11)/31.0)*255)),q);
2228 SetPixelGreen(image,ScaleCharToQuantum((unsigned char)
2229 ((((unsigned short)(color << 5) >> 10)/63.0)*255)),q);
2230 SetPixelBlue(image,ScaleCharToQuantum((unsigned char)
2231 ((((unsigned short)(color << 11) >> 11)/31.0)*255)),q);
2235 SetPixelBlue(image,ScaleCharToQuantum((unsigned char)
2236 ReadBlobByte(image)),q);
2237 SetPixelGreen(image,ScaleCharToQuantum((unsigned char)
2238 ReadBlobByte(image)),q);
2239 SetPixelRed(image,ScaleCharToQuantum((unsigned char)
2240 ReadBlobByte(image)),q);
2241 if (dds_info->pixelformat.rgb_bitcount == 32)
2242 (void) ReadBlobByte(image);
2244 q+=GetPixelChannels(image);
2247 if (SyncAuthenticPixels(image,exception) == MagickFalse)
2251 SkipRGBMipmaps(image, dds_info, 3);
2256 static MagickBooleanType ReadUncompressedRGBA(Image *image, DDSInfo *dds_info,
2257 ExceptionInfo *exception)
2271 if (dds_info->pixelformat.rgb_bitcount == 16)
2273 if (IsBitMask(dds_info->pixelformat,0x7c00,0x03e0,0x001f,0x8000))
2275 else if (IsBitMask(dds_info->pixelformat,0x00ff,0x00ff,0x00ff,0xff00))
2278 (void) SetImageType(image,GrayscaleMatteType,exception);
2280 else if (IsBitMask(dds_info->pixelformat,0x0f00,0x00f0,0x000f,0xf000))
2283 ThrowBinaryException(CorruptImageError,"ImageTypeNotSupported",
2287 for (y = 0; y < (ssize_t) dds_info->height; y++)
2289 q = QueueAuthenticPixels(image, 0, y, dds_info->width, 1,exception);
2291 if (q == (Quantum *) NULL)
2294 for (x = 0; x < (ssize_t) dds_info->width; x++)
2296 if (dds_info->pixelformat.rgb_bitcount == 16)
2298 color=ReadBlobShort(image);
2301 SetPixelAlpha(image,(color & (1 << 15)) ? QuantumRange : 0,q);
2302 SetPixelRed(image,ScaleCharToQuantum((unsigned char)
2303 ((((unsigned short)(color << 1) >> 11)/31.0)*255)),q);
2304 SetPixelGreen(image,ScaleCharToQuantum((unsigned char)
2305 ((((unsigned short)(color << 6) >> 11)/31.0)*255)),q);
2306 SetPixelBlue(image,ScaleCharToQuantum((unsigned char)
2307 ((((unsigned short)(color << 11) >> 11)/31.0)*255)),q);
2309 else if (alphaBits == 2)
2311 SetPixelAlpha(image,ScaleCharToQuantum((unsigned char)
2313 SetPixelGray(image,ScaleCharToQuantum((unsigned char)color),q);
2317 SetPixelAlpha(image,ScaleCharToQuantum((unsigned char)
2318 (((color >> 12)/15.0)*255)),q);
2319 SetPixelRed(image,ScaleCharToQuantum((unsigned char)
2320 ((((unsigned short)(color << 4) >> 12)/15.0)*255)),q);
2321 SetPixelGreen(image,ScaleCharToQuantum((unsigned char)
2322 ((((unsigned short)(color << 8) >> 12)/15.0)*255)),q);
2323 SetPixelBlue(image,ScaleCharToQuantum((unsigned char)
2324 ((((unsigned short)(color << 12) >> 12)/15.0)*255)),q);
2329 SetPixelBlue(image,ScaleCharToQuantum((unsigned char)
2330 ReadBlobByte(image)),q);
2331 SetPixelGreen(image,ScaleCharToQuantum((unsigned char)
2332 ReadBlobByte(image)),q);
2333 SetPixelRed(image,ScaleCharToQuantum((unsigned char)
2334 ReadBlobByte(image)),q);
2335 SetPixelAlpha(image,ScaleCharToQuantum((unsigned char)
2336 ReadBlobByte(image)),q);
2338 q+=GetPixelChannels(image);
2341 if (SyncAuthenticPixels(image,exception) == MagickFalse)
2345 SkipRGBMipmaps(image, dds_info, 4);
2351 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2355 % R e g i s t e r D D S I m a g e %
2359 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2361 % RegisterDDSImage() adds attributes for the DDS image format to
2362 % the list of supported formats. The attributes include the image format
2363 % tag, a method to read and/or write the format, whether the format
2364 % supports the saving of more than one frame to the same file or blob,
2365 % whether the format supports native in-memory I/O, and a brief
2366 % description of the format.
2368 % The format of the RegisterDDSImage method is:
2370 % RegisterDDSImage(void)
2373 ModuleExport size_t RegisterDDSImage(void)
2378 entry = SetMagickInfo("DDS");
2379 entry->decoder = (DecodeImageHandler *) ReadDDSImage;
2380 entry->encoder = (EncodeImageHandler *) WriteDDSImage;
2381 entry->magick = (IsImageFormatHandler *) IsDDS;
2382 entry->seekable_stream=MagickTrue;
2383 entry->description = ConstantString("Microsoft DirectDraw Surface");
2384 entry->module = ConstantString("DDS");
2385 (void) RegisterMagickInfo(entry);
2386 entry = SetMagickInfo("DXT1");
2387 entry->decoder = (DecodeImageHandler *) ReadDDSImage;
2388 entry->encoder = (EncodeImageHandler *) WriteDDSImage;
2389 entry->magick = (IsImageFormatHandler *) IsDDS;
2390 entry->seekable_stream=MagickTrue;
2391 entry->description = ConstantString("Microsoft DirectDraw Surface");
2392 entry->module = ConstantString("DDS");
2393 (void) RegisterMagickInfo(entry);
2394 entry = SetMagickInfo("DXT5");
2395 entry->decoder = (DecodeImageHandler *) ReadDDSImage;
2396 entry->encoder = (EncodeImageHandler *) WriteDDSImage;
2397 entry->magick = (IsImageFormatHandler *) IsDDS;
2398 entry->seekable_stream=MagickTrue;
2399 entry->description = ConstantString("Microsoft DirectDraw Surface");
2400 entry->module = ConstantString("DDS");
2401 (void) RegisterMagickInfo(entry);
2402 return(MagickImageCoderSignature);
2405 static void RemapIndices(const ssize_t *map, const unsigned char *source,
2406 unsigned char *target)
2411 for (i = 0; i < 16; i++)
2416 target[i] = source[map[i]];
2421 Skip the mipmap images for compressed (DXTn) dds files
2423 static void SkipDXTMipmaps(Image *image, DDSInfo *dds_info, int texel_size)
2436 Only skip mipmaps for textures and cube maps
2438 if (dds_info->ddscaps1 & DDSCAPS_MIPMAP
2439 && (dds_info->ddscaps1 & DDSCAPS_TEXTURE
2440 || dds_info->ddscaps2 & DDSCAPS2_CUBEMAP))
2442 w = DIV2(dds_info->width);
2443 h = DIV2(dds_info->height);
2446 Mipmapcount includes the main image, so start from one
2448 for (i = 1; (i < (ssize_t) dds_info->mipmapcount) && w && h; i++)
2450 offset = (MagickOffsetType) ((w + 3) / 4) * ((h + 3) / 4) * texel_size;
2451 (void) SeekBlob(image, offset, SEEK_CUR);
2460 Skip the mipmap images for uncompressed (RGB or RGBA) dds files
2462 static void SkipRGBMipmaps(Image *image, DDSInfo *dds_info, int pixel_size)
2475 Only skip mipmaps for textures and cube maps
2477 if (dds_info->ddscaps1 & DDSCAPS_MIPMAP
2478 && (dds_info->ddscaps1 & DDSCAPS_TEXTURE
2479 || dds_info->ddscaps2 & DDSCAPS2_CUBEMAP))
2481 w = DIV2(dds_info->width);
2482 h = DIV2(dds_info->height);
2485 Mipmapcount includes the main image, so start from one
2487 for (i=1; (i < (ssize_t) dds_info->mipmapcount) && w && h; i++)
2489 offset = (MagickOffsetType) w * h * pixel_size;
2490 (void) SeekBlob(image, offset, SEEK_CUR);
2499 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2503 % U n r e g i s t e r D D S I m a g e %
2507 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2509 % UnregisterDDSImage() removes format registrations made by the
2510 % DDS module from the list of supported formats.
2512 % The format of the UnregisterDDSImage method is:
2514 % UnregisterDDSImage(void)
2517 ModuleExport void UnregisterDDSImage(void)
2519 (void) UnregisterMagickInfo("DDS");
2520 (void) UnregisterMagickInfo("DXT1");
2521 (void) UnregisterMagickInfo("DXT5");
2524 static void WriteAlphas(Image *image, const ssize_t *alphas, size_t min5,
2525 size_t max5, size_t min7, size_t max7)
2539 FixRange(min5,max5,5);
2540 err5 = CompressAlpha(min5,max5,5,alphas,indices5);
2542 FixRange(min7,max7,7);
2543 err7 = CompressAlpha(min7,max7,7,alphas,indices7);
2547 for (i=0; i < 16; i++)
2552 index = indices7[i];
2555 else if (index == 1)
2558 indices5[i] = 9 - index;
2565 (void) WriteBlobByte(image,(unsigned char) min5);
2566 (void) WriteBlobByte(image,(unsigned char) max5);
2568 for(i=0; i < 2; i++)
2573 for (j=0; j < 8; j++)
2575 size_t index = (size_t) indices5[j + i*8];
2576 value |= ( index << 3*j );
2579 for (j=0; j < 3; j++)
2581 size_t byte = (value >> 8*j) & 0xff;
2582 (void) WriteBlobByte(image,(unsigned char) byte);
2587 static void WriteCompressed(Image *image, const size_t count,
2588 DDSVector4 *points, const ssize_t *map, const MagickBooleanType clusterFit)
2604 VectorInit(metric,1.0f);
2605 VectorInit3(start,0.0f);
2606 VectorInit3(end,0.0f);
2608 ComputeWeightedCovariance(count,points,covariance);
2609 ComputePrincipleComponent(covariance,&principle);
2611 if (clusterFit == MagickFalse || count == 0)
2612 CompressRangeFit(count,points,map,principle,metric,&start,&end,indices);
2614 CompressClusterFit(count,points,map,principle,metric,&start,&end,indices);
2616 WriteIndices(image,start,end,indices);
2620 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2624 % W r i t e D D S I m a g e %
2628 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2630 % WriteDDSImage() writes a DirectDraw Surface image file in the DXT5 format.
2632 % The format of the WriteBMPImage method is:
2634 % MagickBooleanType WriteDDSImage(const ImageInfo *image_info,Image *image)
2636 % A description of each parameter follows.
2638 % o image_info: the image info.
2640 % o image: The image.
2643 static MagickBooleanType WriteDDSImage(const ImageInfo *image_info,
2644 Image *image, ExceptionInfo *exception)
2662 assert(image_info != (const ImageInfo *) NULL);
2663 assert(image_info->signature == MagickSignature);
2664 assert(image != (Image *) NULL);
2665 assert(image->signature == MagickSignature);
2666 if (image->debug != MagickFalse)
2667 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2668 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
2669 if (status == MagickFalse)
2671 (void) TransformImageColorspace(image,sRGBColorspace,exception);
2672 pixelFormat=DDPF_FOURCC;
2673 compression=FOURCC_DXT5;
2675 if (image->alpha_trait == UndefinedPixelTrait)
2676 compression=FOURCC_DXT1;
2678 if (LocaleCompare(image_info->magick,"dxt1") == 0)
2679 compression=FOURCC_DXT1;
2681 option=GetImageOption(image_info,"dds:compression");
2682 if (option != (char *) NULL)
2684 if (LocaleCompare(option,"dxt1") == 0)
2685 compression=FOURCC_DXT1;
2686 if (LocaleCompare(option,"none") == 0)
2687 pixelFormat=DDPF_RGB;
2690 clusterFit=MagickFalse;
2691 weightByAlpha=MagickFalse;
2693 if (pixelFormat == DDPF_FOURCC)
2695 option=GetImageOption(image_info,"dds:cluster-fit");
2696 if (option != (char *) NULL && LocaleCompare(option,"true") == 0)
2698 clusterFit=MagickTrue;
2699 if (compression != FOURCC_DXT1)
2701 option=GetImageOption(image_info,"dds:weight-by-alpha");
2702 if (option != (char *) NULL && LocaleCompare(option,"true") == 0)
2703 weightByAlpha=MagickTrue;
2708 maxMipmaps=SIZE_MAX;
2710 if ((image->columns & (image->columns - 1)) == 0 &&
2711 (image->rows & (image->rows - 1)) == 0)
2713 option=GetImageOption(image_info,"dds:mipmaps");
2714 if (option != (char *) NULL)
2715 maxMipmaps=StringToUnsignedLong(option);
2717 if (maxMipmaps != 0)
2719 columns=image->columns;
2721 while (columns != 1 && rows != 1 && mipmaps != maxMipmaps)
2723 columns=DIV2(columns);
2730 WriteDDSInfo(image,pixelFormat,compression,mipmaps);
2732 WriteImageData(image,pixelFormat,compression,clusterFit,weightByAlpha,
2735 if (mipmaps > 0 && WriteMipmaps(image,pixelFormat,compression,mipmaps,
2736 clusterFit,weightByAlpha,exception) == MagickFalse)
2737 return(MagickFalse);
2739 (void) CloseBlob(image);
2743 static void WriteDDSInfo(Image *image, const size_t pixelFormat,
2744 const size_t compression, const size_t mipmaps)
2747 software[MaxTextExtent];
2757 flags=(unsigned int) (DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT |
2758 DDSD_PIXELFORMAT | DDSD_LINEARSIZE);
2759 caps=(unsigned int) DDSCAPS_TEXTURE;
2760 format=(unsigned int) pixelFormat;
2764 flags=flags | (unsigned int) DDSD_MIPMAPCOUNT;
2765 caps=caps | (unsigned int) (DDSCAPS_MIPMAP | DDSCAPS_COMPLEX);
2768 if (format != DDPF_FOURCC && image->alpha_trait != UndefinedPixelTrait)
2769 format=format | DDPF_ALPHAPIXELS;
2771 (void) WriteBlob(image,4,(unsigned char *) "DDS ");
2772 (void) WriteBlobLSBLong(image,124);
2773 (void) WriteBlobLSBLong(image,flags);
2774 (void) WriteBlobLSBLong(image,(unsigned int) image->rows);
2775 (void) WriteBlobLSBLong(image,(unsigned int) image->columns);
2777 if (compression == FOURCC_DXT1)
2778 (void) WriteBlobLSBLong(image,
2779 (unsigned int) (Max(1,(image->columns+3)/4) * 8));
2781 (void) WriteBlobLSBLong(image,
2782 (unsigned int) (Max(1,(image->columns+3)/4) * 16));
2784 (void) WriteBlobLSBLong(image,0x00);
2785 (void) WriteBlobLSBLong(image,(unsigned int) mipmaps+1);
2786 (void) ResetMagickMemory(software,0,sizeof(software));
2787 (void) strcpy(software,"IMAGEMAGICK");
2788 (void) WriteBlob(image,44,(unsigned char *) software);
2790 (void) WriteBlobLSBLong(image,32);
2791 (void) WriteBlobLSBLong(image,format);
2793 if (pixelFormat == DDPF_FOURCC)
2795 (void) WriteBlobLSBLong(image,(unsigned int) compression);
2796 for(i=0;i < 5;i++) // bitcount / masks
2797 (void) WriteBlobLSBLong(image,0x00);
2801 (void) WriteBlobLSBLong(image,0x00);
2802 if (image->alpha_trait != UndefinedPixelTrait)
2804 (void) WriteBlobLSBLong(image,32);
2805 (void) WriteBlobLSBLong(image,0xff0000);
2806 (void) WriteBlobLSBLong(image,0xff00);
2807 (void) WriteBlobLSBLong(image,0xff);
2808 (void) WriteBlobLSBLong(image,0xff000000);
2812 (void) WriteBlobLSBLong(image,24);
2813 (void) WriteBlobLSBLong(image,0xff);
2814 (void) WriteBlobLSBLong(image,0x00);
2815 (void) WriteBlobLSBLong(image,0x00);
2816 (void) WriteBlobLSBLong(image,0x00);
2820 (void) WriteBlobLSBLong(image,caps);
2821 for(i=0;i < 4;i++) // ddscaps2 + reserved region
2822 (void) WriteBlobLSBLong(image,0x00);
2825 static void WriteFourCC(Image *image, const size_t compression,
2826 const MagickBooleanType clusterFit, const MagickBooleanType weightByAlpha,
2827 ExceptionInfo *exception)
2838 register const Quantum
2841 for (y=0; y < (ssize_t) image->rows; y+=4)
2843 for (x=0; x < (ssize_t) image->columns; x+=4)
2868 if (x + columns >= image->columns)
2869 columns = image->columns - x;
2871 if (y + rows >= image->rows)
2872 rows = image->rows - y;
2874 p=GetVirtualPixels(image,x,y,columns,rows,exception);
2875 if (p == (const Quantum *) NULL)
2878 for (i=0; i<16; i++)
2884 for (by=0; by < (ssize_t) rows; by++)
2886 for (bx=0; bx < (ssize_t) columns; bx++)
2888 if (compression == FOURCC_DXT5)
2889 alpha = ScaleQuantumToChar(GetPixelAlpha(image,p));
2893 alphas[4*by + bx] = (size_t)alpha;
2895 point.x = (float)ScaleQuantumToChar(GetPixelRed(image,p)) / 255.0f;
2896 point.y = (float)ScaleQuantumToChar(GetPixelGreen(image,p)) / 255.0f;
2897 point.z = (float)ScaleQuantumToChar(GetPixelBlue(image,p)) / 255.0f;
2898 point.w = weightByAlpha ? (float)(alpha + 1) / 256.0f : 1.0f;
2899 p+=GetPixelChannels(image);
2901 match = MagickFalse;
2902 for (i=0; i < (ssize_t) count; i++)
2904 if ((points[i].x == point.x) &&
2905 (points[i].y == point.y) &&
2906 (points[i].z == point.z) &&
2907 (alpha >= 128 || compression == FOURCC_DXT5))
2909 points[i].w += point.w;
2916 if (match != MagickFalse)
2919 points[count].x = point.x;
2920 points[count].y = point.y;
2921 points[count].z = point.z;
2922 points[count].w = point.w;
2923 map[4*by + bx] = count;
2926 if (compression == FOURCC_DXT5)
2932 if (alpha != 0 && alpha < min5)
2934 if (alpha != 255 && alpha > max5)
2940 for (i=0; i < (ssize_t) count; i++)
2941 points[i].w = sqrt(points[i].w);
2943 if (compression == FOURCC_DXT5)
2944 WriteAlphas(image,alphas,min5,max5,min7,max7);
2947 WriteSingleColorFit(image,points,map);
2949 WriteCompressed(image,count,points,map,clusterFit);
2954 static void WriteImageData(Image *image, const size_t pixelFormat,
2955 const size_t compression,const MagickBooleanType clusterFit,
2956 const MagickBooleanType weightByAlpha, ExceptionInfo *exception)
2958 if (pixelFormat == DDPF_FOURCC)
2959 WriteFourCC(image,compression,clusterFit,weightByAlpha,exception);
2961 WriteUncompressed(image,exception);
2964 static inline size_t ClampToLimit(const float value, const size_t limit)
2967 result = (int) (value + 0.5f);
2976 static inline size_t ColorTo565(const DDSVector3 point)
2978 size_t r = ClampToLimit(31.0f*point.x,31);
2979 size_t g = ClampToLimit(63.0f*point.y,63);
2980 size_t b = ClampToLimit(31.0f*point.z,31);
2982 return (r << 11) | (g << 5) | b;
2985 static void WriteIndices(Image *image, const DDSVector3 start,
2986 const DDSVector3 end, unsigned char *indices)
3001 a = ColorTo565(start);
3002 b = ColorTo565(end);
3004 for (i=0; i<16; i++)
3007 remapped[i] = (indices[i] ^ 0x1) & 0x3;
3011 remapped[i] = indices[i];
3017 (void) WriteBlobByte(image,(unsigned char) (a & 0xff));
3018 (void) WriteBlobByte(image,(unsigned char) (a >> 8));
3019 (void) WriteBlobByte(image,(unsigned char) (b & 0xff));
3020 (void) WriteBlobByte(image,(unsigned char) (b >> 8));
3024 ind = remapped + 4*i;
3025 (void) WriteBlobByte(image,ind[0] | (ind[1] << 2) | (ind[2] << 4) |
3030 static MagickBooleanType WriteMipmaps(Image *image, const size_t pixelFormat,
3031 const size_t compression, const size_t mipmaps,
3032 const MagickBooleanType clusterFit, const MagickBooleanType weightByAlpha,
3033 ExceptionInfo *exception)
3045 columns = image->columns;
3048 for (i=0; i< (ssize_t) mipmaps; i++)
3050 resize_image = ResizeImage(image,columns/2,rows/2,TriangleFilter,
3053 if (resize_image == (Image *) NULL)
3054 return(MagickFalse);
3056 DestroyBlob(resize_image);
3057 resize_image->blob=ReferenceBlob(image->blob);
3059 WriteImageData(resize_image,pixelFormat,compression,weightByAlpha,
3060 clusterFit,exception);
3062 resize_image=DestroyImage(resize_image);
3064 columns = DIV2(columns);
3071 static void WriteSingleColorFit(Image *image, const DDSVector4 *points,
3087 color[0] = (unsigned char) ClampToLimit(255.0f*points->x,255);
3088 color[1] = (unsigned char) ClampToLimit(255.0f*points->y,255);
3089 color[2] = (unsigned char) ClampToLimit(255.0f*points->z,255);
3092 ComputeEndPoints(DDS_LOOKUP,color,&start,&end,&index);
3094 for (i=0; i< 16; i++)
3096 RemapIndices(map,indexes,indices);
3097 WriteIndices(image,start,end,indices);
3100 static void WriteUncompressed(Image *image, ExceptionInfo *exception)
3102 register const Quantum
3111 for (y=0; y < (ssize_t) image->rows; y++)
3113 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
3114 if (p == (const Quantum *) NULL)
3117 for (x=0; x < (ssize_t) image->columns; x++)
3119 (void) WriteBlobByte(image,ScaleQuantumToChar(GetPixelBlue(image,p)));
3120 (void) WriteBlobByte(image,ScaleQuantumToChar(GetPixelGreen(image,p)));
3121 (void) WriteBlobByte(image,ScaleQuantumToChar(GetPixelRed(image,p)));
3122 if (image->alpha_trait != UndefinedPixelTrait)
3123 (void) WriteBlobByte(image,ScaleQuantumToChar(GetPixelAlpha(image,p)));
3124 p+=GetPixelChannels(image);