2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read/Write Microsoft Direct Draw Surface Image Format %
22 % Copyright 1999-2013 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/blob.h"
46 #include "MagickCore/blob-private.h"
47 #include "MagickCore/cache.h"
48 #include "MagickCore/colorspace.h"
49 #include "MagickCore/exception.h"
50 #include "MagickCore/exception-private.h"
51 #include "MagickCore/image.h"
52 #include "MagickCore/image-private.h"
53 #include "MagickCore/list.h"
54 #include "MagickCore/log.h"
55 #include "MagickCore/magick.h"
56 #include "MagickCore/memory_.h"
57 #include "MagickCore/monitor.h"
58 #include "MagickCore/monitor-private.h"
59 #include "MagickCore/profile.h"
60 #include "MagickCore/quantum-private.h"
61 #include "MagickCore/static.h"
62 #include "MagickCore/string_.h"
63 #include "MagickCore/module.h"
64 #include "MagickCore/transform.h"
65 #include "MagickCore/studio.h"
66 #include "MagickCore/blob.h"
67 #include "MagickCore/blob-private.h"
68 #include "MagickCore/colorspace.h"
69 #include "MagickCore/colorspace-private.h"
70 #include "MagickCore/exception.h"
71 #include "MagickCore/exception-private.h"
72 #include "MagickCore/compress.h"
73 #include "MagickCore/image.h"
74 #include "MagickCore/image-private.h"
75 #include "MagickCore/list.h"
76 #include "MagickCore/magick.h"
77 #include "MagickCore/memory_.h"
78 #include "MagickCore/monitor.h"
79 #include "MagickCore/monitor-private.h"
80 #include "MagickCore/option.h"
81 #include "MagickCore/pixel-accessor.h"
82 #include "MagickCore/quantum.h"
83 #include "MagickCore/static.h"
84 #include "MagickCore/string_.h"
85 #include "MagickCore/string-private.h"
90 #define DDSD_CAPS 0x00000001
91 #define DDSD_HEIGHT 0x00000002
92 #define DDSD_WIDTH 0x00000004
93 #define DDSD_PITCH 0x00000008
94 #define DDSD_PIXELFORMAT 0x00001000
95 #define DDSD_MIPMAPCOUNT 0x00020000
96 #define DDSD_LINEARSIZE 0x00080000
97 #define DDSD_DEPTH 0x00800000
99 #define DDPF_ALPHAPIXELS 0x00000001
100 #define DDPF_FOURCC 0x00000004
101 #define DDPF_RGB 0x00000040
103 #define FOURCC_DXT1 0x31545844
104 #define FOURCC_DXT3 0x33545844
105 #define FOURCC_DXT5 0x35545844
107 #define DDSCAPS_COMPLEX 0x00000008
108 #define DDSCAPS_TEXTURE 0x00001000
109 #define DDSCAPS_MIPMAP 0x00400000
111 #define DDSCAPS2_CUBEMAP 0x00000200
112 #define DDSCAPS2_CUBEMAP_POSITIVEX 0x00000400
113 #define DDSCAPS2_CUBEMAP_NEGATIVEX 0x00000800
114 #define DDSCAPS2_CUBEMAP_POSITIVEY 0x00001000
115 #define DDSCAPS2_CUBEMAP_NEGATIVEY 0x00002000
116 #define DDSCAPS2_CUBEMAP_POSITIVEZ 0x00004000
117 #define DDSCAPS2_CUBEMAP_NEGATIVEZ 0x00008000
118 #define DDSCAPS2_VOLUME 0x00200000
121 #define SIZE_MAX ((size_t) -1)
125 Structure declarations.
127 typedef struct _DDSPixelFormat
139 typedef struct _DDSInfo
155 typedef struct _DDSColors
164 typedef struct _DDSVector4
173 typedef struct _DDSVector3
181 typedef struct _DDSSourceBlock
189 typedef struct _DDSSingleColourLookup
191 DDSSourceBlock sources[2];
192 } DDSSingleColourLookup;
194 typedef MagickBooleanType
195 DDSDecoder(Image *, DDSInfo *, ExceptionInfo *);
197 static const DDSSingleColourLookup DDSLookup_5_4[] =
199 { { { 0, 0, 0 }, { 0, 0, 0 } } },
200 { { { 0, 0, 1 }, { 0, 1, 1 } } },
201 { { { 0, 0, 2 }, { 0, 1, 0 } } },
202 { { { 0, 0, 3 }, { 0, 1, 1 } } },
203 { { { 0, 0, 4 }, { 0, 2, 1 } } },
204 { { { 1, 0, 3 }, { 0, 2, 0 } } },
205 { { { 1, 0, 2 }, { 0, 2, 1 } } },
206 { { { 1, 0, 1 }, { 0, 3, 1 } } },
207 { { { 1, 0, 0 }, { 0, 3, 0 } } },
208 { { { 1, 0, 1 }, { 1, 2, 1 } } },
209 { { { 1, 0, 2 }, { 1, 2, 0 } } },
210 { { { 1, 0, 3 }, { 0, 4, 0 } } },
211 { { { 1, 0, 4 }, { 0, 5, 1 } } },
212 { { { 2, 0, 3 }, { 0, 5, 0 } } },
213 { { { 2, 0, 2 }, { 0, 5, 1 } } },
214 { { { 2, 0, 1 }, { 0, 6, 1 } } },
215 { { { 2, 0, 0 }, { 0, 6, 0 } } },
216 { { { 2, 0, 1 }, { 2, 3, 1 } } },
217 { { { 2, 0, 2 }, { 2, 3, 0 } } },
218 { { { 2, 0, 3 }, { 0, 7, 0 } } },
219 { { { 2, 0, 4 }, { 1, 6, 1 } } },
220 { { { 3, 0, 3 }, { 1, 6, 0 } } },
221 { { { 3, 0, 2 }, { 0, 8, 0 } } },
222 { { { 3, 0, 1 }, { 0, 9, 1 } } },
223 { { { 3, 0, 0 }, { 0, 9, 0 } } },
224 { { { 3, 0, 1 }, { 0, 9, 1 } } },
225 { { { 3, 0, 2 }, { 0, 10, 1 } } },
226 { { { 3, 0, 3 }, { 0, 10, 0 } } },
227 { { { 3, 0, 4 }, { 2, 7, 1 } } },
228 { { { 4, 0, 4 }, { 2, 7, 0 } } },
229 { { { 4, 0, 3 }, { 0, 11, 0 } } },
230 { { { 4, 0, 2 }, { 1, 10, 1 } } },
231 { { { 4, 0, 1 }, { 1, 10, 0 } } },
232 { { { 4, 0, 0 }, { 0, 12, 0 } } },
233 { { { 4, 0, 1 }, { 0, 13, 1 } } },
234 { { { 4, 0, 2 }, { 0, 13, 0 } } },
235 { { { 4, 0, 3 }, { 0, 13, 1 } } },
236 { { { 4, 0, 4 }, { 0, 14, 1 } } },
237 { { { 5, 0, 3 }, { 0, 14, 0 } } },
238 { { { 5, 0, 2 }, { 2, 11, 1 } } },
239 { { { 5, 0, 1 }, { 2, 11, 0 } } },
240 { { { 5, 0, 0 }, { 0, 15, 0 } } },
241 { { { 5, 0, 1 }, { 1, 14, 1 } } },
242 { { { 5, 0, 2 }, { 1, 14, 0 } } },
243 { { { 5, 0, 3 }, { 0, 16, 0 } } },
244 { { { 5, 0, 4 }, { 0, 17, 1 } } },
245 { { { 6, 0, 3 }, { 0, 17, 0 } } },
246 { { { 6, 0, 2 }, { 0, 17, 1 } } },
247 { { { 6, 0, 1 }, { 0, 18, 1 } } },
248 { { { 6, 0, 0 }, { 0, 18, 0 } } },
249 { { { 6, 0, 1 }, { 2, 15, 1 } } },
250 { { { 6, 0, 2 }, { 2, 15, 0 } } },
251 { { { 6, 0, 3 }, { 0, 19, 0 } } },
252 { { { 6, 0, 4 }, { 1, 18, 1 } } },
253 { { { 7, 0, 3 }, { 1, 18, 0 } } },
254 { { { 7, 0, 2 }, { 0, 20, 0 } } },
255 { { { 7, 0, 1 }, { 0, 21, 1 } } },
256 { { { 7, 0, 0 }, { 0, 21, 0 } } },
257 { { { 7, 0, 1 }, { 0, 21, 1 } } },
258 { { { 7, 0, 2 }, { 0, 22, 1 } } },
259 { { { 7, 0, 3 }, { 0, 22, 0 } } },
260 { { { 7, 0, 4 }, { 2, 19, 1 } } },
261 { { { 8, 0, 4 }, { 2, 19, 0 } } },
262 { { { 8, 0, 3 }, { 0, 23, 0 } } },
263 { { { 8, 0, 2 }, { 1, 22, 1 } } },
264 { { { 8, 0, 1 }, { 1, 22, 0 } } },
265 { { { 8, 0, 0 }, { 0, 24, 0 } } },
266 { { { 8, 0, 1 }, { 0, 25, 1 } } },
267 { { { 8, 0, 2 }, { 0, 25, 0 } } },
268 { { { 8, 0, 3 }, { 0, 25, 1 } } },
269 { { { 8, 0, 4 }, { 0, 26, 1 } } },
270 { { { 9, 0, 3 }, { 0, 26, 0 } } },
271 { { { 9, 0, 2 }, { 2, 23, 1 } } },
272 { { { 9, 0, 1 }, { 2, 23, 0 } } },
273 { { { 9, 0, 0 }, { 0, 27, 0 } } },
274 { { { 9, 0, 1 }, { 1, 26, 1 } } },
275 { { { 9, 0, 2 }, { 1, 26, 0 } } },
276 { { { 9, 0, 3 }, { 0, 28, 0 } } },
277 { { { 9, 0, 4 }, { 0, 29, 1 } } },
278 { { { 10, 0, 3 }, { 0, 29, 0 } } },
279 { { { 10, 0, 2 }, { 0, 29, 1 } } },
280 { { { 10, 0, 1 }, { 0, 30, 1 } } },
281 { { { 10, 0, 0 }, { 0, 30, 0 } } },
282 { { { 10, 0, 1 }, { 2, 27, 1 } } },
283 { { { 10, 0, 2 }, { 2, 27, 0 } } },
284 { { { 10, 0, 3 }, { 0, 31, 0 } } },
285 { { { 10, 0, 4 }, { 1, 30, 1 } } },
286 { { { 11, 0, 3 }, { 1, 30, 0 } } },
287 { { { 11, 0, 2 }, { 4, 24, 0 } } },
288 { { { 11, 0, 1 }, { 1, 31, 1 } } },
289 { { { 11, 0, 0 }, { 1, 31, 0 } } },
290 { { { 11, 0, 1 }, { 1, 31, 1 } } },
291 { { { 11, 0, 2 }, { 2, 30, 1 } } },
292 { { { 11, 0, 3 }, { 2, 30, 0 } } },
293 { { { 11, 0, 4 }, { 2, 31, 1 } } },
294 { { { 12, 0, 4 }, { 2, 31, 0 } } },
295 { { { 12, 0, 3 }, { 4, 27, 0 } } },
296 { { { 12, 0, 2 }, { 3, 30, 1 } } },
297 { { { 12, 0, 1 }, { 3, 30, 0 } } },
298 { { { 12, 0, 0 }, { 4, 28, 0 } } },
299 { { { 12, 0, 1 }, { 3, 31, 1 } } },
300 { { { 12, 0, 2 }, { 3, 31, 0 } } },
301 { { { 12, 0, 3 }, { 3, 31, 1 } } },
302 { { { 12, 0, 4 }, { 4, 30, 1 } } },
303 { { { 13, 0, 3 }, { 4, 30, 0 } } },
304 { { { 13, 0, 2 }, { 6, 27, 1 } } },
305 { { { 13, 0, 1 }, { 6, 27, 0 } } },
306 { { { 13, 0, 0 }, { 4, 31, 0 } } },
307 { { { 13, 0, 1 }, { 5, 30, 1 } } },
308 { { { 13, 0, 2 }, { 5, 30, 0 } } },
309 { { { 13, 0, 3 }, { 8, 24, 0 } } },
310 { { { 13, 0, 4 }, { 5, 31, 1 } } },
311 { { { 14, 0, 3 }, { 5, 31, 0 } } },
312 { { { 14, 0, 2 }, { 5, 31, 1 } } },
313 { { { 14, 0, 1 }, { 6, 30, 1 } } },
314 { { { 14, 0, 0 }, { 6, 30, 0 } } },
315 { { { 14, 0, 1 }, { 6, 31, 1 } } },
316 { { { 14, 0, 2 }, { 6, 31, 0 } } },
317 { { { 14, 0, 3 }, { 8, 27, 0 } } },
318 { { { 14, 0, 4 }, { 7, 30, 1 } } },
319 { { { 15, 0, 3 }, { 7, 30, 0 } } },
320 { { { 15, 0, 2 }, { 8, 28, 0 } } },
321 { { { 15, 0, 1 }, { 7, 31, 1 } } },
322 { { { 15, 0, 0 }, { 7, 31, 0 } } },
323 { { { 15, 0, 1 }, { 7, 31, 1 } } },
324 { { { 15, 0, 2 }, { 8, 30, 1 } } },
325 { { { 15, 0, 3 }, { 8, 30, 0 } } },
326 { { { 15, 0, 4 }, { 10, 27, 1 } } },
327 { { { 16, 0, 4 }, { 10, 27, 0 } } },
328 { { { 16, 0, 3 }, { 8, 31, 0 } } },
329 { { { 16, 0, 2 }, { 9, 30, 1 } } },
330 { { { 16, 0, 1 }, { 9, 30, 0 } } },
331 { { { 16, 0, 0 }, { 12, 24, 0 } } },
332 { { { 16, 0, 1 }, { 9, 31, 1 } } },
333 { { { 16, 0, 2 }, { 9, 31, 0 } } },
334 { { { 16, 0, 3 }, { 9, 31, 1 } } },
335 { { { 16, 0, 4 }, { 10, 30, 1 } } },
336 { { { 17, 0, 3 }, { 10, 30, 0 } } },
337 { { { 17, 0, 2 }, { 10, 31, 1 } } },
338 { { { 17, 0, 1 }, { 10, 31, 0 } } },
339 { { { 17, 0, 0 }, { 12, 27, 0 } } },
340 { { { 17, 0, 1 }, { 11, 30, 1 } } },
341 { { { 17, 0, 2 }, { 11, 30, 0 } } },
342 { { { 17, 0, 3 }, { 12, 28, 0 } } },
343 { { { 17, 0, 4 }, { 11, 31, 1 } } },
344 { { { 18, 0, 3 }, { 11, 31, 0 } } },
345 { { { 18, 0, 2 }, { 11, 31, 1 } } },
346 { { { 18, 0, 1 }, { 12, 30, 1 } } },
347 { { { 18, 0, 0 }, { 12, 30, 0 } } },
348 { { { 18, 0, 1 }, { 14, 27, 1 } } },
349 { { { 18, 0, 2 }, { 14, 27, 0 } } },
350 { { { 18, 0, 3 }, { 12, 31, 0 } } },
351 { { { 18, 0, 4 }, { 13, 30, 1 } } },
352 { { { 19, 0, 3 }, { 13, 30, 0 } } },
353 { { { 19, 0, 2 }, { 16, 24, 0 } } },
354 { { { 19, 0, 1 }, { 13, 31, 1 } } },
355 { { { 19, 0, 0 }, { 13, 31, 0 } } },
356 { { { 19, 0, 1 }, { 13, 31, 1 } } },
357 { { { 19, 0, 2 }, { 14, 30, 1 } } },
358 { { { 19, 0, 3 }, { 14, 30, 0 } } },
359 { { { 19, 0, 4 }, { 14, 31, 1 } } },
360 { { { 20, 0, 4 }, { 14, 31, 0 } } },
361 { { { 20, 0, 3 }, { 16, 27, 0 } } },
362 { { { 20, 0, 2 }, { 15, 30, 1 } } },
363 { { { 20, 0, 1 }, { 15, 30, 0 } } },
364 { { { 20, 0, 0 }, { 16, 28, 0 } } },
365 { { { 20, 0, 1 }, { 15, 31, 1 } } },
366 { { { 20, 0, 2 }, { 15, 31, 0 } } },
367 { { { 20, 0, 3 }, { 15, 31, 1 } } },
368 { { { 20, 0, 4 }, { 16, 30, 1 } } },
369 { { { 21, 0, 3 }, { 16, 30, 0 } } },
370 { { { 21, 0, 2 }, { 18, 27, 1 } } },
371 { { { 21, 0, 1 }, { 18, 27, 0 } } },
372 { { { 21, 0, 0 }, { 16, 31, 0 } } },
373 { { { 21, 0, 1 }, { 17, 30, 1 } } },
374 { { { 21, 0, 2 }, { 17, 30, 0 } } },
375 { { { 21, 0, 3 }, { 20, 24, 0 } } },
376 { { { 21, 0, 4 }, { 17, 31, 1 } } },
377 { { { 22, 0, 3 }, { 17, 31, 0 } } },
378 { { { 22, 0, 2 }, { 17, 31, 1 } } },
379 { { { 22, 0, 1 }, { 18, 30, 1 } } },
380 { { { 22, 0, 0 }, { 18, 30, 0 } } },
381 { { { 22, 0, 1 }, { 18, 31, 1 } } },
382 { { { 22, 0, 2 }, { 18, 31, 0 } } },
383 { { { 22, 0, 3 }, { 20, 27, 0 } } },
384 { { { 22, 0, 4 }, { 19, 30, 1 } } },
385 { { { 23, 0, 3 }, { 19, 30, 0 } } },
386 { { { 23, 0, 2 }, { 20, 28, 0 } } },
387 { { { 23, 0, 1 }, { 19, 31, 1 } } },
388 { { { 23, 0, 0 }, { 19, 31, 0 } } },
389 { { { 23, 0, 1 }, { 19, 31, 1 } } },
390 { { { 23, 0, 2 }, { 20, 30, 1 } } },
391 { { { 23, 0, 3 }, { 20, 30, 0 } } },
392 { { { 23, 0, 4 }, { 22, 27, 1 } } },
393 { { { 24, 0, 4 }, { 22, 27, 0 } } },
394 { { { 24, 0, 3 }, { 20, 31, 0 } } },
395 { { { 24, 0, 2 }, { 21, 30, 1 } } },
396 { { { 24, 0, 1 }, { 21, 30, 0 } } },
397 { { { 24, 0, 0 }, { 24, 24, 0 } } },
398 { { { 24, 0, 1 }, { 21, 31, 1 } } },
399 { { { 24, 0, 2 }, { 21, 31, 0 } } },
400 { { { 24, 0, 3 }, { 21, 31, 1 } } },
401 { { { 24, 0, 4 }, { 22, 30, 1 } } },
402 { { { 25, 0, 3 }, { 22, 30, 0 } } },
403 { { { 25, 0, 2 }, { 22, 31, 1 } } },
404 { { { 25, 0, 1 }, { 22, 31, 0 } } },
405 { { { 25, 0, 0 }, { 24, 27, 0 } } },
406 { { { 25, 0, 1 }, { 23, 30, 1 } } },
407 { { { 25, 0, 2 }, { 23, 30, 0 } } },
408 { { { 25, 0, 3 }, { 24, 28, 0 } } },
409 { { { 25, 0, 4 }, { 23, 31, 1 } } },
410 { { { 26, 0, 3 }, { 23, 31, 0 } } },
411 { { { 26, 0, 2 }, { 23, 31, 1 } } },
412 { { { 26, 0, 1 }, { 24, 30, 1 } } },
413 { { { 26, 0, 0 }, { 24, 30, 0 } } },
414 { { { 26, 0, 1 }, { 26, 27, 1 } } },
415 { { { 26, 0, 2 }, { 26, 27, 0 } } },
416 { { { 26, 0, 3 }, { 24, 31, 0 } } },
417 { { { 26, 0, 4 }, { 25, 30, 1 } } },
418 { { { 27, 0, 3 }, { 25, 30, 0 } } },
419 { { { 27, 0, 2 }, { 28, 24, 0 } } },
420 { { { 27, 0, 1 }, { 25, 31, 1 } } },
421 { { { 27, 0, 0 }, { 25, 31, 0 } } },
422 { { { 27, 0, 1 }, { 25, 31, 1 } } },
423 { { { 27, 0, 2 }, { 26, 30, 1 } } },
424 { { { 27, 0, 3 }, { 26, 30, 0 } } },
425 { { { 27, 0, 4 }, { 26, 31, 1 } } },
426 { { { 28, 0, 4 }, { 26, 31, 0 } } },
427 { { { 28, 0, 3 }, { 28, 27, 0 } } },
428 { { { 28, 0, 2 }, { 27, 30, 1 } } },
429 { { { 28, 0, 1 }, { 27, 30, 0 } } },
430 { { { 28, 0, 0 }, { 28, 28, 0 } } },
431 { { { 28, 0, 1 }, { 27, 31, 1 } } },
432 { { { 28, 0, 2 }, { 27, 31, 0 } } },
433 { { { 28, 0, 3 }, { 27, 31, 1 } } },
434 { { { 28, 0, 4 }, { 28, 30, 1 } } },
435 { { { 29, 0, 3 }, { 28, 30, 0 } } },
436 { { { 29, 0, 2 }, { 30, 27, 1 } } },
437 { { { 29, 0, 1 }, { 30, 27, 0 } } },
438 { { { 29, 0, 0 }, { 28, 31, 0 } } },
439 { { { 29, 0, 1 }, { 29, 30, 1 } } },
440 { { { 29, 0, 2 }, { 29, 30, 0 } } },
441 { { { 29, 0, 3 }, { 29, 30, 1 } } },
442 { { { 29, 0, 4 }, { 29, 31, 1 } } },
443 { { { 30, 0, 3 }, { 29, 31, 0 } } },
444 { { { 30, 0, 2 }, { 29, 31, 1 } } },
445 { { { 30, 0, 1 }, { 30, 30, 1 } } },
446 { { { 30, 0, 0 }, { 30, 30, 0 } } },
447 { { { 30, 0, 1 }, { 30, 31, 1 } } },
448 { { { 30, 0, 2 }, { 30, 31, 0 } } },
449 { { { 30, 0, 3 }, { 30, 31, 1 } } },
450 { { { 30, 0, 4 }, { 31, 30, 1 } } },
451 { { { 31, 0, 3 }, { 31, 30, 0 } } },
452 { { { 31, 0, 2 }, { 31, 30, 1 } } },
453 { { { 31, 0, 1 }, { 31, 31, 1 } } },
454 { { { 31, 0, 0 }, { 31, 31, 0 } } }
457 static const DDSSingleColourLookup DDSLookup_6_4[] =
459 { { { 0, 0, 0 }, { 0, 0, 0 } } },
460 { { { 0, 0, 1 }, { 0, 1, 0 } } },
461 { { { 0, 0, 2 }, { 0, 2, 0 } } },
462 { { { 1, 0, 1 }, { 0, 3, 1 } } },
463 { { { 1, 0, 0 }, { 0, 3, 0 } } },
464 { { { 1, 0, 1 }, { 0, 4, 0 } } },
465 { { { 1, 0, 2 }, { 0, 5, 0 } } },
466 { { { 2, 0, 1 }, { 0, 6, 1 } } },
467 { { { 2, 0, 0 }, { 0, 6, 0 } } },
468 { { { 2, 0, 1 }, { 0, 7, 0 } } },
469 { { { 2, 0, 2 }, { 0, 8, 0 } } },
470 { { { 3, 0, 1 }, { 0, 9, 1 } } },
471 { { { 3, 0, 0 }, { 0, 9, 0 } } },
472 { { { 3, 0, 1 }, { 0, 10, 0 } } },
473 { { { 3, 0, 2 }, { 0, 11, 0 } } },
474 { { { 4, 0, 1 }, { 0, 12, 1 } } },
475 { { { 4, 0, 0 }, { 0, 12, 0 } } },
476 { { { 4, 0, 1 }, { 0, 13, 0 } } },
477 { { { 4, 0, 2 }, { 0, 14, 0 } } },
478 { { { 5, 0, 1 }, { 0, 15, 1 } } },
479 { { { 5, 0, 0 }, { 0, 15, 0 } } },
480 { { { 5, 0, 1 }, { 0, 16, 0 } } },
481 { { { 5, 0, 2 }, { 1, 15, 0 } } },
482 { { { 6, 0, 1 }, { 0, 17, 0 } } },
483 { { { 6, 0, 0 }, { 0, 18, 0 } } },
484 { { { 6, 0, 1 }, { 0, 19, 0 } } },
485 { { { 6, 0, 2 }, { 3, 14, 0 } } },
486 { { { 7, 0, 1 }, { 0, 20, 0 } } },
487 { { { 7, 0, 0 }, { 0, 21, 0 } } },
488 { { { 7, 0, 1 }, { 0, 22, 0 } } },
489 { { { 7, 0, 2 }, { 4, 15, 0 } } },
490 { { { 8, 0, 1 }, { 0, 23, 0 } } },
491 { { { 8, 0, 0 }, { 0, 24, 0 } } },
492 { { { 8, 0, 1 }, { 0, 25, 0 } } },
493 { { { 8, 0, 2 }, { 6, 14, 0 } } },
494 { { { 9, 0, 1 }, { 0, 26, 0 } } },
495 { { { 9, 0, 0 }, { 0, 27, 0 } } },
496 { { { 9, 0, 1 }, { 0, 28, 0 } } },
497 { { { 9, 0, 2 }, { 7, 15, 0 } } },
498 { { { 10, 0, 1 }, { 0, 29, 0 } } },
499 { { { 10, 0, 0 }, { 0, 30, 0 } } },
500 { { { 10, 0, 1 }, { 0, 31, 0 } } },
501 { { { 10, 0, 2 }, { 9, 14, 0 } } },
502 { { { 11, 0, 1 }, { 0, 32, 0 } } },
503 { { { 11, 0, 0 }, { 0, 33, 0 } } },
504 { { { 11, 0, 1 }, { 2, 30, 0 } } },
505 { { { 11, 0, 2 }, { 0, 34, 0 } } },
506 { { { 12, 0, 1 }, { 0, 35, 0 } } },
507 { { { 12, 0, 0 }, { 0, 36, 0 } } },
508 { { { 12, 0, 1 }, { 3, 31, 0 } } },
509 { { { 12, 0, 2 }, { 0, 37, 0 } } },
510 { { { 13, 0, 1 }, { 0, 38, 0 } } },
511 { { { 13, 0, 0 }, { 0, 39, 0 } } },
512 { { { 13, 0, 1 }, { 5, 30, 0 } } },
513 { { { 13, 0, 2 }, { 0, 40, 0 } } },
514 { { { 14, 0, 1 }, { 0, 41, 0 } } },
515 { { { 14, 0, 0 }, { 0, 42, 0 } } },
516 { { { 14, 0, 1 }, { 6, 31, 0 } } },
517 { { { 14, 0, 2 }, { 0, 43, 0 } } },
518 { { { 15, 0, 1 }, { 0, 44, 0 } } },
519 { { { 15, 0, 0 }, { 0, 45, 0 } } },
520 { { { 15, 0, 1 }, { 8, 30, 0 } } },
521 { { { 15, 0, 2 }, { 0, 46, 0 } } },
522 { { { 16, 0, 2 }, { 0, 47, 0 } } },
523 { { { 16, 0, 1 }, { 1, 46, 0 } } },
524 { { { 16, 0, 0 }, { 0, 48, 0 } } },
525 { { { 16, 0, 1 }, { 0, 49, 0 } } },
526 { { { 16, 0, 2 }, { 0, 50, 0 } } },
527 { { { 17, 0, 1 }, { 2, 47, 0 } } },
528 { { { 17, 0, 0 }, { 0, 51, 0 } } },
529 { { { 17, 0, 1 }, { 0, 52, 0 } } },
530 { { { 17, 0, 2 }, { 0, 53, 0 } } },
531 { { { 18, 0, 1 }, { 4, 46, 0 } } },
532 { { { 18, 0, 0 }, { 0, 54, 0 } } },
533 { { { 18, 0, 1 }, { 0, 55, 0 } } },
534 { { { 18, 0, 2 }, { 0, 56, 0 } } },
535 { { { 19, 0, 1 }, { 5, 47, 0 } } },
536 { { { 19, 0, 0 }, { 0, 57, 0 } } },
537 { { { 19, 0, 1 }, { 0, 58, 0 } } },
538 { { { 19, 0, 2 }, { 0, 59, 0 } } },
539 { { { 20, 0, 1 }, { 7, 46, 0 } } },
540 { { { 20, 0, 0 }, { 0, 60, 0 } } },
541 { { { 20, 0, 1 }, { 0, 61, 0 } } },
542 { { { 20, 0, 2 }, { 0, 62, 0 } } },
543 { { { 21, 0, 1 }, { 8, 47, 0 } } },
544 { { { 21, 0, 0 }, { 0, 63, 0 } } },
545 { { { 21, 0, 1 }, { 1, 62, 0 } } },
546 { { { 21, 0, 2 }, { 1, 63, 0 } } },
547 { { { 22, 0, 1 }, { 10, 46, 0 } } },
548 { { { 22, 0, 0 }, { 2, 62, 0 } } },
549 { { { 22, 0, 1 }, { 2, 63, 0 } } },
550 { { { 22, 0, 2 }, { 3, 62, 0 } } },
551 { { { 23, 0, 1 }, { 11, 47, 0 } } },
552 { { { 23, 0, 0 }, { 3, 63, 0 } } },
553 { { { 23, 0, 1 }, { 4, 62, 0 } } },
554 { { { 23, 0, 2 }, { 4, 63, 0 } } },
555 { { { 24, 0, 1 }, { 13, 46, 0 } } },
556 { { { 24, 0, 0 }, { 5, 62, 0 } } },
557 { { { 24, 0, 1 }, { 5, 63, 0 } } },
558 { { { 24, 0, 2 }, { 6, 62, 0 } } },
559 { { { 25, 0, 1 }, { 14, 47, 0 } } },
560 { { { 25, 0, 0 }, { 6, 63, 0 } } },
561 { { { 25, 0, 1 }, { 7, 62, 0 } } },
562 { { { 25, 0, 2 }, { 7, 63, 0 } } },
563 { { { 26, 0, 1 }, { 16, 45, 0 } } },
564 { { { 26, 0, 0 }, { 8, 62, 0 } } },
565 { { { 26, 0, 1 }, { 8, 63, 0 } } },
566 { { { 26, 0, 2 }, { 9, 62, 0 } } },
567 { { { 27, 0, 1 }, { 16, 48, 0 } } },
568 { { { 27, 0, 0 }, { 9, 63, 0 } } },
569 { { { 27, 0, 1 }, { 10, 62, 0 } } },
570 { { { 27, 0, 2 }, { 10, 63, 0 } } },
571 { { { 28, 0, 1 }, { 16, 51, 0 } } },
572 { { { 28, 0, 0 }, { 11, 62, 0 } } },
573 { { { 28, 0, 1 }, { 11, 63, 0 } } },
574 { { { 28, 0, 2 }, { 12, 62, 0 } } },
575 { { { 29, 0, 1 }, { 16, 54, 0 } } },
576 { { { 29, 0, 0 }, { 12, 63, 0 } } },
577 { { { 29, 0, 1 }, { 13, 62, 0 } } },
578 { { { 29, 0, 2 }, { 13, 63, 0 } } },
579 { { { 30, 0, 1 }, { 16, 57, 0 } } },
580 { { { 30, 0, 0 }, { 14, 62, 0 } } },
581 { { { 30, 0, 1 }, { 14, 63, 0 } } },
582 { { { 30, 0, 2 }, { 15, 62, 0 } } },
583 { { { 31, 0, 1 }, { 16, 60, 0 } } },
584 { { { 31, 0, 0 }, { 15, 63, 0 } } },
585 { { { 31, 0, 1 }, { 24, 46, 0 } } },
586 { { { 31, 0, 2 }, { 16, 62, 0 } } },
587 { { { 32, 0, 2 }, { 16, 63, 0 } } },
588 { { { 32, 0, 1 }, { 17, 62, 0 } } },
589 { { { 32, 0, 0 }, { 25, 47, 0 } } },
590 { { { 32, 0, 1 }, { 17, 63, 0 } } },
591 { { { 32, 0, 2 }, { 18, 62, 0 } } },
592 { { { 33, 0, 1 }, { 18, 63, 0 } } },
593 { { { 33, 0, 0 }, { 27, 46, 0 } } },
594 { { { 33, 0, 1 }, { 19, 62, 0 } } },
595 { { { 33, 0, 2 }, { 19, 63, 0 } } },
596 { { { 34, 0, 1 }, { 20, 62, 0 } } },
597 { { { 34, 0, 0 }, { 28, 47, 0 } } },
598 { { { 34, 0, 1 }, { 20, 63, 0 } } },
599 { { { 34, 0, 2 }, { 21, 62, 0 } } },
600 { { { 35, 0, 1 }, { 21, 63, 0 } } },
601 { { { 35, 0, 0 }, { 30, 46, 0 } } },
602 { { { 35, 0, 1 }, { 22, 62, 0 } } },
603 { { { 35, 0, 2 }, { 22, 63, 0 } } },
604 { { { 36, 0, 1 }, { 23, 62, 0 } } },
605 { { { 36, 0, 0 }, { 31, 47, 0 } } },
606 { { { 36, 0, 1 }, { 23, 63, 0 } } },
607 { { { 36, 0, 2 }, { 24, 62, 0 } } },
608 { { { 37, 0, 1 }, { 24, 63, 0 } } },
609 { { { 37, 0, 0 }, { 32, 47, 0 } } },
610 { { { 37, 0, 1 }, { 25, 62, 0 } } },
611 { { { 37, 0, 2 }, { 25, 63, 0 } } },
612 { { { 38, 0, 1 }, { 26, 62, 0 } } },
613 { { { 38, 0, 0 }, { 32, 50, 0 } } },
614 { { { 38, 0, 1 }, { 26, 63, 0 } } },
615 { { { 38, 0, 2 }, { 27, 62, 0 } } },
616 { { { 39, 0, 1 }, { 27, 63, 0 } } },
617 { { { 39, 0, 0 }, { 32, 53, 0 } } },
618 { { { 39, 0, 1 }, { 28, 62, 0 } } },
619 { { { 39, 0, 2 }, { 28, 63, 0 } } },
620 { { { 40, 0, 1 }, { 29, 62, 0 } } },
621 { { { 40, 0, 0 }, { 32, 56, 0 } } },
622 { { { 40, 0, 1 }, { 29, 63, 0 } } },
623 { { { 40, 0, 2 }, { 30, 62, 0 } } },
624 { { { 41, 0, 1 }, { 30, 63, 0 } } },
625 { { { 41, 0, 0 }, { 32, 59, 0 } } },
626 { { { 41, 0, 1 }, { 31, 62, 0 } } },
627 { { { 41, 0, 2 }, { 31, 63, 0 } } },
628 { { { 42, 0, 1 }, { 32, 61, 0 } } },
629 { { { 42, 0, 0 }, { 32, 62, 0 } } },
630 { { { 42, 0, 1 }, { 32, 63, 0 } } },
631 { { { 42, 0, 2 }, { 41, 46, 0 } } },
632 { { { 43, 0, 1 }, { 33, 62, 0 } } },
633 { { { 43, 0, 0 }, { 33, 63, 0 } } },
634 { { { 43, 0, 1 }, { 34, 62, 0 } } },
635 { { { 43, 0, 2 }, { 42, 47, 0 } } },
636 { { { 44, 0, 1 }, { 34, 63, 0 } } },
637 { { { 44, 0, 0 }, { 35, 62, 0 } } },
638 { { { 44, 0, 1 }, { 35, 63, 0 } } },
639 { { { 44, 0, 2 }, { 44, 46, 0 } } },
640 { { { 45, 0, 1 }, { 36, 62, 0 } } },
641 { { { 45, 0, 0 }, { 36, 63, 0 } } },
642 { { { 45, 0, 1 }, { 37, 62, 0 } } },
643 { { { 45, 0, 2 }, { 45, 47, 0 } } },
644 { { { 46, 0, 1 }, { 37, 63, 0 } } },
645 { { { 46, 0, 0 }, { 38, 62, 0 } } },
646 { { { 46, 0, 1 }, { 38, 63, 0 } } },
647 { { { 46, 0, 2 }, { 47, 46, 0 } } },
648 { { { 47, 0, 1 }, { 39, 62, 0 } } },
649 { { { 47, 0, 0 }, { 39, 63, 0 } } },
650 { { { 47, 0, 1 }, { 40, 62, 0 } } },
651 { { { 47, 0, 2 }, { 48, 46, 0 } } },
652 { { { 48, 0, 2 }, { 40, 63, 0 } } },
653 { { { 48, 0, 1 }, { 41, 62, 0 } } },
654 { { { 48, 0, 0 }, { 41, 63, 0 } } },
655 { { { 48, 0, 1 }, { 48, 49, 0 } } },
656 { { { 48, 0, 2 }, { 42, 62, 0 } } },
657 { { { 49, 0, 1 }, { 42, 63, 0 } } },
658 { { { 49, 0, 0 }, { 43, 62, 0 } } },
659 { { { 49, 0, 1 }, { 48, 52, 0 } } },
660 { { { 49, 0, 2 }, { 43, 63, 0 } } },
661 { { { 50, 0, 1 }, { 44, 62, 0 } } },
662 { { { 50, 0, 0 }, { 44, 63, 0 } } },
663 { { { 50, 0, 1 }, { 48, 55, 0 } } },
664 { { { 50, 0, 2 }, { 45, 62, 0 } } },
665 { { { 51, 0, 1 }, { 45, 63, 0 } } },
666 { { { 51, 0, 0 }, { 46, 62, 0 } } },
667 { { { 51, 0, 1 }, { 48, 58, 0 } } },
668 { { { 51, 0, 2 }, { 46, 63, 0 } } },
669 { { { 52, 0, 1 }, { 47, 62, 0 } } },
670 { { { 52, 0, 0 }, { 47, 63, 0 } } },
671 { { { 52, 0, 1 }, { 48, 61, 0 } } },
672 { { { 52, 0, 2 }, { 48, 62, 0 } } },
673 { { { 53, 0, 1 }, { 56, 47, 0 } } },
674 { { { 53, 0, 0 }, { 48, 63, 0 } } },
675 { { { 53, 0, 1 }, { 49, 62, 0 } } },
676 { { { 53, 0, 2 }, { 49, 63, 0 } } },
677 { { { 54, 0, 1 }, { 58, 46, 0 } } },
678 { { { 54, 0, 0 }, { 50, 62, 0 } } },
679 { { { 54, 0, 1 }, { 50, 63, 0 } } },
680 { { { 54, 0, 2 }, { 51, 62, 0 } } },
681 { { { 55, 0, 1 }, { 59, 47, 0 } } },
682 { { { 55, 0, 0 }, { 51, 63, 0 } } },
683 { { { 55, 0, 1 }, { 52, 62, 0 } } },
684 { { { 55, 0, 2 }, { 52, 63, 0 } } },
685 { { { 56, 0, 1 }, { 61, 46, 0 } } },
686 { { { 56, 0, 0 }, { 53, 62, 0 } } },
687 { { { 56, 0, 1 }, { 53, 63, 0 } } },
688 { { { 56, 0, 2 }, { 54, 62, 0 } } },
689 { { { 57, 0, 1 }, { 62, 47, 0 } } },
690 { { { 57, 0, 0 }, { 54, 63, 0 } } },
691 { { { 57, 0, 1 }, { 55, 62, 0 } } },
692 { { { 57, 0, 2 }, { 55, 63, 0 } } },
693 { { { 58, 0, 1 }, { 56, 62, 1 } } },
694 { { { 58, 0, 0 }, { 56, 62, 0 } } },
695 { { { 58, 0, 1 }, { 56, 63, 0 } } },
696 { { { 58, 0, 2 }, { 57, 62, 0 } } },
697 { { { 59, 0, 1 }, { 57, 63, 1 } } },
698 { { { 59, 0, 0 }, { 57, 63, 0 } } },
699 { { { 59, 0, 1 }, { 58, 62, 0 } } },
700 { { { 59, 0, 2 }, { 58, 63, 0 } } },
701 { { { 60, 0, 1 }, { 59, 62, 1 } } },
702 { { { 60, 0, 0 }, { 59, 62, 0 } } },
703 { { { 60, 0, 1 }, { 59, 63, 0 } } },
704 { { { 60, 0, 2 }, { 60, 62, 0 } } },
705 { { { 61, 0, 1 }, { 60, 63, 1 } } },
706 { { { 61, 0, 0 }, { 60, 63, 0 } } },
707 { { { 61, 0, 1 }, { 61, 62, 0 } } },
708 { { { 61, 0, 2 }, { 61, 63, 0 } } },
709 { { { 62, 0, 1 }, { 62, 62, 1 } } },
710 { { { 62, 0, 0 }, { 62, 62, 0 } } },
711 { { { 62, 0, 1 }, { 62, 63, 0 } } },
712 { { { 62, 0, 2 }, { 63, 62, 0 } } },
713 { { { 63, 0, 1 }, { 63, 63, 1 } } },
714 { { { 63, 0, 0 }, { 63, 63, 0 } } }
717 static const DDSSingleColourLookup*
728 #define C565_r(x) (((x) & 0xF800) >> 11)
729 #define C565_g(x) (((x) & 0x07E0) >> 5)
730 #define C565_b(x) ((x) & 0x001F)
732 #define C565_red(x) ( (C565_r(x) << 3 | C565_r(x) >> 2))
733 #define C565_green(x) ( (C565_g(x) << 2 | C565_g(x) >> 4))
734 #define C565_blue(x) ( (C565_b(x) << 3 | C565_b(x) >> 2))
736 #define DIV2(x) ((x) > 1 ? ((x) >> 1) : 1)
738 #define FixRange(min, max, steps) \
741 if (max - min < steps) \
742 max = Min(min + steps, 255); \
743 if (max - min < steps) \
744 min = Max(min - steps, 0)
746 #define Dot(left, right) (left.x*right.x) + (left.y*right.y) + (left.z*right.z)
748 #define VectorInit(vector, value) vector.x = vector.y = vector.z = vector.w \
750 #define VectorInit3(vector, value) vector.x = vector.y = vector.z = value
755 static MagickBooleanType
756 ConstructOrdering(const size_t, const DDSVector4 *, const DDSVector3,
757 DDSVector4 *, DDSVector4 *, unsigned char *, size_t);
759 static MagickBooleanType
760 ReadDDSInfo(Image *, DDSInfo *);
763 CalculateColors(unsigned short, unsigned short,
764 DDSColors *, MagickBooleanType);
766 static MagickBooleanType
767 ReadDXT1(Image *, DDSInfo *, ExceptionInfo *);
769 static MagickBooleanType
770 ReadDXT3(Image *, DDSInfo *, ExceptionInfo *);
772 static MagickBooleanType
773 ReadDXT5(Image *, DDSInfo *, ExceptionInfo *);
775 static MagickBooleanType
776 ReadUncompressedRGB(Image *, DDSInfo *, ExceptionInfo *);
778 static MagickBooleanType
779 ReadUncompressedRGBA(Image *, DDSInfo *, ExceptionInfo *);
782 RemapIndices(const ssize_t *, const unsigned char *, unsigned char *);
785 SkipDXTMipmaps(Image *, DDSInfo *, int);
788 SkipRGBMipmaps(Image *, DDSInfo *, int);
791 MagickBooleanType WriteDDSImage(const ImageInfo *, Image *, ExceptionInfo *);
794 WriteDDSInfo(Image *, const size_t, const size_t, const size_t);
797 WriteFourCC(Image *, const size_t, const MagickBooleanType,
798 const MagickBooleanType, ExceptionInfo *);
801 WriteImageData(Image *, const size_t, const size_t, const MagickBooleanType,
802 const MagickBooleanType, ExceptionInfo *);
805 WriteIndices(Image *, const DDSVector3, const DDSVector3, unsigned char *);
807 static MagickBooleanType
808 WriteMipmaps(Image *, const size_t, const size_t, const size_t,
809 const MagickBooleanType, const MagickBooleanType, ExceptionInfo *);
812 WriteSingleColorFit(Image *, const DDSVector4 *, const ssize_t *);
815 WriteUncompressed(Image *, ExceptionInfo *);
817 static inline size_t Max(size_t one, size_t two)
824 static inline float MaxF(float one, float two)
831 static inline size_t Min(size_t one, size_t two)
838 static inline float MinF(float one, float two)
845 static inline void VectorAdd(const DDSVector4 left, const DDSVector4 right,
846 DDSVector4 *destination)
848 destination->x = left.x + right.x;
849 destination->y = left.y + right.y;
850 destination->z = left.z + right.z;
851 destination->w = left.w + right.w;
854 static inline void VectorClamp(DDSVector4 *value)
856 value->x = MinF(1.0f,MaxF(0.0f,value->x));
857 value->y = MinF(1.0f,MaxF(0.0f,value->y));
858 value->z = MinF(1.0f,MaxF(0.0f,value->z));
859 value->w = MinF(1.0f,MaxF(0.0f,value->w));
862 static inline void VectorClamp3(DDSVector3 *value)
864 value->x = MinF(1.0f,MaxF(0.0f,value->x));
865 value->y = MinF(1.0f,MaxF(0.0f,value->y));
866 value->z = MinF(1.0f,MaxF(0.0f,value->z));
869 static inline void VectorCopy43(const DDSVector4 source,
870 DDSVector3 *destination)
872 destination->x = source.x;
873 destination->y = source.y;
874 destination->z = source.z;
877 static inline void VectorCopy44(const DDSVector4 source,
878 DDSVector4 *destination)
880 destination->x = source.x;
881 destination->y = source.y;
882 destination->z = source.z;
883 destination->w = source.w;
886 static inline void VectorNegativeMultiplySubtract(const DDSVector4 a,
887 const DDSVector4 b, const DDSVector4 c, DDSVector4 *destination)
889 destination->x = c.x - (a.x * b.x);
890 destination->y = c.y - (a.y * b.y);
891 destination->z = c.z - (a.z * b.z);
892 destination->w = c.w - (a.w * b.w);
895 static inline void VectorMultiply(const DDSVector4 left,
896 const DDSVector4 right, DDSVector4 *destination)
898 destination->x = left.x * right.x;
899 destination->y = left.y * right.y;
900 destination->z = left.z * right.z;
901 destination->w = left.w * right.w;
904 static inline void VectorMultiply3(const DDSVector3 left,
905 const DDSVector3 right, DDSVector3 *destination)
907 destination->x = left.x * right.x;
908 destination->y = left.y * right.y;
909 destination->z = left.z * right.z;
912 static inline void VectorMultiplyAdd(const DDSVector4 a, const DDSVector4 b,
913 const DDSVector4 c, DDSVector4 *destination)
915 destination->x = (a.x * b.x) + c.x;
916 destination->y = (a.y * b.y) + c.y;
917 destination->z = (a.z * b.z) + c.z;
918 destination->w = (a.w * b.w) + c.w;
921 static inline void VectorMultiplyAdd3(const DDSVector3 a, const DDSVector3 b,
922 const DDSVector3 c, DDSVector3 *destination)
924 destination->x = (a.x * b.x) + c.x;
925 destination->y = (a.y * b.y) + c.y;
926 destination->z = (a.z * b.z) + c.z;
929 static inline void VectorReciprocal(const DDSVector4 value,
930 DDSVector4 *destination)
932 destination->x = 1.0f / value.x;
933 destination->y = 1.0f / value.y;
934 destination->z = 1.0f / value.z;
935 destination->w = 1.0f / value.w;
938 static inline void VectorSubtract(const DDSVector4 left,
939 const DDSVector4 right, DDSVector4 *destination)
941 destination->x = left.x - right.x;
942 destination->y = left.y - right.y;
943 destination->z = left.z - right.z;
944 destination->w = left.w - right.w;
947 static inline void VectorSubtract3(const DDSVector3 left,
948 const DDSVector3 right, DDSVector3 *destination)
950 destination->x = left.x - right.x;
951 destination->y = left.y - right.y;
952 destination->z = left.z - right.z;
955 static inline void VectorTruncate(DDSVector4 *value)
957 value->x = value->x > 0.0f ? floor(value->x) : ceil(value->x);
958 value->y = value->y > 0.0f ? floor(value->y) : ceil(value->y);
959 value->z = value->z > 0.0f ? floor(value->z) : ceil(value->z);
960 value->w = value->w > 0.0f ? floor(value->w) : ceil(value->w);
963 static inline void VectorTruncate3(DDSVector3 *value)
965 value->x = value->x > 0.0f ? floor(value->x) : ceil(value->x);
966 value->y = value->y > 0.0f ? floor(value->y) : ceil(value->y);
967 value->z = value->z > 0.0f ? floor(value->z) : ceil(value->z);
970 static void CalculateColors(unsigned short c0, unsigned short c1,
971 DDSColors *c, MagickBooleanType ignoreAlpha)
973 c->a[0] = c->a[1] = c->a[2] = c->a[3] = 0;
975 c->r[0] = (unsigned char) C565_red(c0);
976 c->g[0] = (unsigned char) C565_green(c0);
977 c->b[0] = (unsigned char) C565_blue(c0);
979 c->r[1] = (unsigned char) C565_red(c1);
980 c->g[1] = (unsigned char) C565_green(c1);
981 c->b[1] = (unsigned char) C565_blue(c1);
983 if (ignoreAlpha == MagickTrue || c0 > c1)
985 c->r[2] = (unsigned char) ((2 * c->r[0] + c->r[1]) / 3);
986 c->g[2] = (unsigned char) ((2 * c->g[0] + c->g[1]) / 3);
987 c->b[2] = (unsigned char) ((2 * c->b[0] + c->b[1]) / 3);
989 c->r[3] = (unsigned char) ((c->r[0] + 2 * c->r[1]) / 3);
990 c->g[3] = (unsigned char) ((c->g[0] + 2 * c->g[1]) / 3);
991 c->b[3] = (unsigned char) ((c->b[0] + 2 * c->b[1]) / 3);
995 c->r[2] = (unsigned char) ((c->r[0] + c->r[1]) / 2);
996 c->g[2] = (unsigned char) ((c->g[0] + c->g[1]) / 2);
997 c->b[2] = (unsigned char) ((c->b[0] + c->b[1]) / 2);
999 c->r[3] = c->g[3] = c->b[3] = 0;
1004 static size_t CompressAlpha(const size_t min, const size_t max,
1005 const size_t steps, const ssize_t *alphas, unsigned char* indices)
1020 codes[0] = (unsigned char) min;
1021 codes[1] = (unsigned char) max;
1025 for (i=1; i < steps; i++)
1026 codes[i+1] = (unsigned char) (((steps-i)*min + i*max) / steps);
1029 for (i=0; i<16; i++)
1031 if (alphas[i] == -1)
1045 dist = value - (size_t)codes[j];
1055 indices[i] = (unsigned char)index;
1062 static void CompressClusterFit(const size_t count,
1063 const DDSVector4 *points, const ssize_t *map, const DDSVector3 principle,
1064 const DDSVector4 metric, DDSVector3 *start, DDSVector3* end,
1065 unsigned char *indices)
1082 twothirds_twothirds2,
1104 VectorInit(half,0.5f);
1105 VectorInit(two,2.0f);
1107 VectorInit(onethird_onethird2,1.0f/3.0f);
1108 onethird_onethird2.w = 1.0f/9.0f;
1109 VectorInit(twothirds_twothirds2,2.0f/3.0f);
1110 twothirds_twothirds2.w = 4.0f/9.0f;
1111 VectorInit(twonineths,2.0f/9.0f);
1118 gridrcp.x = 1.0f/31.0f;
1119 gridrcp.y = 1.0f/63.0f;
1120 gridrcp.z = 1.0f/31.0f;
1123 ConstructOrdering(count,points,principle,pointsWeights,&xSumwSum,order,0);
1125 for (iterationIndex = 0;;)
1127 VectorInit(part0,0.0f);
1128 for (i=0; i < count; i++)
1130 VectorInit(part1,0.0f);
1135 VectorCopy44(pointsWeights[0],&part2);
1140 VectorInit(part2,0.0f);
1161 VectorSubtract(xSumwSum,part2,&part3);
1162 VectorSubtract(part3,part1,&part3);
1163 VectorSubtract(part3,part0,&part3);
1165 VectorMultiplyAdd(part1,twothirds_twothirds2,part0,&alphax_sum);
1166 VectorMultiplyAdd(part2,onethird_onethird2,alphax_sum,&alphax_sum);
1167 VectorInit(alpha2_sum,alphax_sum.w);
1169 VectorMultiplyAdd(part2,twothirds_twothirds2,part3,&betax_sum);
1170 VectorMultiplyAdd(part1,onethird_onethird2,betax_sum,&betax_sum);
1171 VectorInit(beta2_sum,betax_sum.w);
1173 VectorAdd(part1,part2,&alphabeta_sum);
1174 VectorInit(alphabeta_sum,alphabeta_sum.w);
1175 VectorMultiply(twonineths,alphabeta_sum,&alphabeta_sum);
1177 VectorMultiply(alpha2_sum,beta2_sum,&factor);
1178 VectorNegativeMultiplySubtract(alphabeta_sum,alphabeta_sum,factor,
1180 VectorReciprocal(factor,&factor);
1182 VectorMultiply(alphax_sum,beta2_sum,&a);
1183 VectorNegativeMultiplySubtract(betax_sum,alphabeta_sum,a,&a);
1184 VectorMultiply(a,factor,&a);
1186 VectorMultiply(betax_sum,alpha2_sum,&b);
1187 VectorNegativeMultiplySubtract(alphax_sum,alphabeta_sum,b,&b);
1188 VectorMultiply(b,factor,&b);
1191 VectorMultiplyAdd(grid,a,half,&a);
1193 VectorMultiply(a,gridrcp,&a);
1196 VectorMultiplyAdd(grid,b,half,&b);
1198 VectorMultiply(b,gridrcp,&b);
1200 VectorMultiply(b,b,&e1);
1201 VectorMultiply(e1,beta2_sum,&e1);
1202 VectorMultiply(a,a,&e2);
1203 VectorMultiplyAdd(e2,alpha2_sum,e1,&e1);
1205 VectorMultiply(a,b,&e2);
1206 VectorMultiply(e2,alphabeta_sum,&e2);
1207 VectorNegativeMultiplySubtract(a,alphax_sum,e2,&e2);
1208 VectorNegativeMultiplySubtract(b,betax_sum,e2,&e2);
1209 VectorMultiplyAdd(two,e2,e1,&e2);
1210 VectorMultiply(e2,metric,&e2);
1212 error = e2.x + e2.y + e2.z;
1214 if (error < bestError)
1216 VectorCopy43(a,start);
1217 VectorCopy43(b,end);
1222 bestIteration = iterationIndex;
1228 VectorAdd(pointsWeights[k],part2,&part2);
1235 VectorAdd(pointsWeights[j],part1,&part1);
1239 VectorAdd(pointsWeights[i],part0,&part0);
1242 if (bestIteration != iterationIndex)
1246 if (iterationIndex == 8)
1249 VectorSubtract3(*end,*start,&axis);
1250 if (ConstructOrdering(count,points,axis,pointsWeights,&xSumwSum,order,
1251 iterationIndex) == MagickFalse)
1255 o = order + (16*bestIteration);
1257 for (i=0; i < besti; i++)
1258 unordered[o[i]] = 0;
1259 for (i=besti; i < bestj; i++)
1260 unordered[o[i]] = 2;
1261 for (i=bestj; i < bestk; i++)
1262 unordered[o[i]] = 3;
1263 for (i=bestk; i < count; i++)
1264 unordered[o[i]] = 1;
1266 RemapIndices(map,unordered,indices);
1269 static void CompressRangeFit(const size_t count,
1270 const DDSVector4* points, const ssize_t *map, const DDSVector3 principle,
1271 const DDSVector4 metric, DDSVector3 *start, DDSVector3 *end,
1272 unsigned char *indices)
1298 VectorInit3(half,0.5f);
1304 gridrcp.x = 1.0f/31.0f;
1305 gridrcp.y = 1.0f/63.0f;
1306 gridrcp.z = 1.0f/31.0f;
1310 VectorCopy43(points[0],start);
1311 VectorCopy43(points[0],end);
1313 min = max = Dot(points[0],principle);
1314 for (i=1; i < count; i++)
1316 val = Dot(points[i],principle);
1319 VectorCopy43(points[i],start);
1324 VectorCopy43(points[i],end);
1330 VectorClamp3(start);
1331 VectorMultiplyAdd3(grid,*start,half,start);
1332 VectorTruncate3(start);
1333 VectorMultiply3(*start,gridrcp,start);
1336 VectorMultiplyAdd3(grid,*end,half,end);
1337 VectorTruncate3(end);
1338 VectorMultiply3(*end,gridrcp,end);
1342 codes[2].x = (start->x * (2.0f/3.0f)) + (end->x * (1.0f/3.0f));
1343 codes[2].y = (start->y * (2.0f/3.0f)) + (end->y * (1.0f/3.0f));
1344 codes[2].z = (start->z * (2.0f/3.0f)) + (end->z * (1.0f/3.0f));
1345 codes[3].x = (start->x * (1.0f/3.0f)) + (end->x * (2.0f/3.0f));
1346 codes[3].y = (start->y * (1.0f/3.0f)) + (end->y * (2.0f/3.0f));
1347 codes[3].z = (start->z * (1.0f/3.0f)) + (end->z * (2.0f/3.0f));
1349 for (i=0; i < count; i++)
1353 for (j=0; j < 4; j++)
1355 dist.x = (points[i].x - codes[j].x) * metric.x;
1356 dist.y = (points[i].y - codes[j].y) * metric.y;
1357 dist.z = (points[i].z - codes[j].z) * metric.z;
1367 closest[i] = (unsigned char) bestj;
1370 RemapIndices(map, closest, indices);
1373 static void ComputeEndPoints(const DDSSingleColourLookup *lookup[],
1374 const unsigned char *color, DDSVector3 *start, DDSVector3 *end,
1375 unsigned char *index)
1382 maxError = SIZE_MAX;
1384 for (i=0; i < 2; i++)
1386 const DDSSourceBlock*
1392 for (c=0; c < 3; c++)
1394 sources[c] = &lookup[c][color[c]].sources[i];
1395 error += ((size_t) sources[c]->error) * ((size_t) sources[c]->error);
1398 if (error > maxError)
1401 start->x = (float) sources[0]->start / 31.0f;
1402 start->y = (float) sources[1]->start / 63.0f;
1403 start->z = (float) sources[2]->start / 31.0f;
1405 end->x = (float) sources[0]->end / 31.0f;
1406 end->y = (float) sources[1]->end / 63.0f;
1407 end->z = (float) sources[2]->end / 31.0f;
1409 *index = (unsigned char) (2*i);
1414 static void ComputePrincipleComponent(const float *covariance,
1415 DDSVector3 *principle)
1426 row0.x = covariance[0];
1427 row0.y = covariance[1];
1428 row0.z = covariance[2];
1431 row1.x = covariance[1];
1432 row1.y = covariance[3];
1433 row1.z = covariance[4];
1436 row2.x = covariance[2];
1437 row2.y = covariance[4];
1438 row2.z = covariance[5];
1443 for (i=0; i < 8; i++)
1456 w.x = (row1.x * v.y) + w.x;
1457 w.y = (row1.y * v.y) + w.y;
1458 w.z = (row1.z * v.y) + w.z;
1459 w.w = (row1.w * v.y) + w.w;
1461 w.x = (row2.x * v.z) + w.x;
1462 w.y = (row2.y * v.z) + w.y;
1463 w.z = (row2.z * v.z) + w.z;
1464 w.w = (row2.w * v.z) + w.w;
1466 a = 1.0f / MaxF(w.x,MaxF(w.y,w.z));
1474 VectorCopy43(v,principle);
1477 static void ComputeWeightedCovariance(const size_t count,
1478 const DDSVector4 *points, float *covariance)
1490 VectorInit3(centroid,0.0f);
1492 for (i=0; i < count; i++)
1494 total += points[i].w;
1495 centroid.x += (points[i].x * points[i].w);
1496 centroid.y += (points[i].y * points[i].w);
1497 centroid.z += (points[i].z * points[i].w);
1500 if( total > 1.192092896e-07F)
1502 centroid.x /= total;
1503 centroid.y /= total;
1504 centroid.z /= total;
1507 for (i=0; i < 6; i++)
1508 covariance[i] = 0.0f;
1510 for (i = 0; i < count; i++)
1516 a.x = points[i].x - centroid.x;
1517 a.y = points[i].y - centroid.y;
1518 a.z = points[i].z - centroid.z;
1520 b.x = points[i].w * a.x;
1521 b.y = points[i].w * a.y;
1522 b.z = points[i].w * a.z;
1524 covariance[0] += a.x*b.x;
1525 covariance[1] += a.x*b.y;
1526 covariance[2] += a.x*b.z;
1527 covariance[3] += a.y*b.y;
1528 covariance[4] += a.y*b.z;
1529 covariance[5] += a.z*b.z;
1533 static MagickBooleanType ConstructOrdering(const size_t count,
1534 const DDSVector4 *points, const DDSVector3 axis, DDSVector4 *pointsWeights,
1535 DDSVector4 *xSumwSum, unsigned char *order, size_t iteration)
1552 o = order + (16*iteration);
1554 for (i=0; i < count; i++)
1556 dps[i] = Dot(points[i],axis);
1557 o[i] = (unsigned char)i;
1560 for (i=0; i < count; i++)
1562 for (j=i; j > 0 && dps[j] < dps[j - 1]; j--)
1565 dps[j] = dps[j - 1];
1574 for (i=0; i < iteration; i++)
1582 for (j=0; j < count; j++)
1591 if (same == MagickTrue)
1600 for (i=0; i < count; i++)
1607 v.x = points[j].w * points[j].x;
1608 v.y = points[j].w * points[j].y;
1609 v.z = points[j].w * points[j].z;
1610 v.w = points[j].w * 1.0f;
1612 VectorCopy44(v,&pointsWeights[i]);
1613 VectorAdd(*xSumwSum,v,xSumwSum);
1620 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1628 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1630 % IsDDS() returns MagickTrue if the image format type, identified by the
1631 % magick string, is DDS.
1633 % The format of the IsDDS method is:
1635 % MagickBooleanType IsDDS(const unsigned char *magick,const size_t length)
1637 % A description of each parameter follows:
1639 % o magick: compare image format pattern against these bytes.
1641 % o length: Specifies the length of the magick string.
1644 static MagickBooleanType IsDDS(const unsigned char *magick, const size_t length)
1647 return(MagickFalse);
1648 if (LocaleNCompare((char *) magick,"DDS ", 4) == 0)
1650 return(MagickFalse);
1653 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1657 % R e a d D D S I m a g e %
1661 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1663 % ReadDDSImage() reads a DirectDraw Surface image file and returns it. It
1664 % allocates the memory necessary for the new Image structure and returns a
1665 % pointer to the new image.
1667 % The format of the ReadDDSImage method is:
1669 % Image *ReadDDSImage(const ImageInfo *image_info,ExceptionInfo *exception)
1671 % A description of each parameter follows:
1673 % o image_info: The image info.
1675 % o exception: return any errors or warnings in this structure.
1679 static Image *ReadDDSImage(const ImageInfo *image_info,ExceptionInfo *exception)
1686 cubemap = MagickFalse,
1687 volume = MagickFalse;
1707 assert(image_info != (const ImageInfo *) NULL);
1708 assert(image_info->signature == MagickSignature);
1709 if (image_info->debug != MagickFalse)
1710 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
1711 image_info->filename);
1712 assert(exception != (ExceptionInfo *) NULL);
1713 assert(exception->signature == MagickSignature);
1714 image=AcquireImage(image_info,exception);
1715 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
1716 if (status == MagickFalse)
1718 image=DestroyImageList(image);
1719 return((Image *) NULL);
1723 Initialize image structure.
1725 if (ReadDDSInfo(image, &dds_info) != MagickTrue) {
1726 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1729 if (dds_info.ddscaps2 & DDSCAPS2_CUBEMAP)
1730 cubemap = MagickTrue;
1732 if (dds_info.ddscaps2 & DDSCAPS2_VOLUME && dds_info.depth > 0)
1733 volume = MagickTrue;
1735 (void) SeekBlob(image, 128, SEEK_SET);
1738 Determine pixel format
1740 if (dds_info.pixelformat.flags & DDPF_RGB)
1742 compression = NoCompression;
1743 if (dds_info.pixelformat.flags & DDPF_ALPHAPIXELS)
1745 alpha_trait = BlendPixelTrait;
1746 decoder = ReadUncompressedRGBA;
1750 alpha_trait = UndefinedPixelTrait;
1751 decoder = ReadUncompressedRGB;
1754 else if (dds_info.pixelformat.flags & DDPF_FOURCC)
1756 switch (dds_info.pixelformat.fourcc)
1760 alpha_trait = UndefinedPixelTrait;
1761 compression = DXT1Compression;
1768 alpha_trait = BlendPixelTrait;
1769 compression = DXT3Compression;
1776 alpha_trait = BlendPixelTrait;
1777 compression = DXT5Compression;
1784 /* Unknown FOURCC */
1785 ThrowReaderException(CorruptImageError, "ImageTypeNotSupported");
1791 /* Neither compressed nor uncompressed... thus unsupported */
1792 ThrowReaderException(CorruptImageError, "ImageTypeNotSupported");
1799 Determine number of faces defined in the cubemap
1802 if (dds_info.ddscaps2 & DDSCAPS2_CUBEMAP_POSITIVEX) num_images++;
1803 if (dds_info.ddscaps2 & DDSCAPS2_CUBEMAP_NEGATIVEX) num_images++;
1804 if (dds_info.ddscaps2 & DDSCAPS2_CUBEMAP_POSITIVEY) num_images++;
1805 if (dds_info.ddscaps2 & DDSCAPS2_CUBEMAP_NEGATIVEY) num_images++;
1806 if (dds_info.ddscaps2 & DDSCAPS2_CUBEMAP_POSITIVEZ) num_images++;
1807 if (dds_info.ddscaps2 & DDSCAPS2_CUBEMAP_NEGATIVEZ) num_images++;
1811 num_images = dds_info.depth;
1813 for (n = 0; n < num_images; n++)
1817 /* Start a new image */
1818 AcquireNextImage(image_info,image,exception);
1819 if (GetNextImageInList(image) == (Image *) NULL)
1821 image = DestroyImageList(image);
1822 return((Image *) NULL);
1824 image=SyncNextImageInList(image);
1827 image->alpha_trait=alpha_trait;
1828 image->compression = compression;
1829 image->columns = dds_info.width;
1830 image->rows = dds_info.height;
1831 image->storage_class = DirectClass;
1832 image->endian = LSBEndian;
1834 if (image_info->ping != MagickFalse)
1836 (void) CloseBlob(image);
1837 return(GetFirstImageInList(image));
1840 if ((decoder)(image, &dds_info, exception) != MagickTrue)
1842 (void) CloseBlob(image);
1843 return(GetFirstImageInList(image));
1847 if (EOFBlob(image) != MagickFalse)
1848 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
1851 (void) CloseBlob(image);
1852 return(GetFirstImageInList(image));
1855 static MagickBooleanType ReadDDSInfo(Image *image, DDSInfo *dds_info)
1861 /* Seek to start of header */
1862 (void) SeekBlob(image, 4, SEEK_SET);
1864 /* Check header field */
1865 hdr_size = ReadBlobLSBLong(image);
1866 if (hdr_size != 124)
1869 /* Fill in DDS info struct */
1870 dds_info->flags = ReadBlobLSBLong(image);
1872 /* Check required flags */
1873 required=(size_t) (DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT);
1874 if ((dds_info->flags & required) != required)
1877 dds_info->height = ReadBlobLSBLong(image);
1878 dds_info->width = ReadBlobLSBLong(image);
1879 dds_info->pitchOrLinearSize = ReadBlobLSBLong(image);
1880 dds_info->depth = ReadBlobLSBLong(image);
1881 dds_info->mipmapcount = ReadBlobLSBLong(image);
1883 (void) SeekBlob(image, 44, SEEK_CUR); /* reserved region of 11 DWORDs */
1885 /* Read pixel format structure */
1886 hdr_size = ReadBlobLSBLong(image);
1890 dds_info->pixelformat.flags = ReadBlobLSBLong(image);
1891 dds_info->pixelformat.fourcc = ReadBlobLSBLong(image);
1892 dds_info->pixelformat.rgb_bitcount = ReadBlobLSBLong(image);
1893 dds_info->pixelformat.r_bitmask = ReadBlobLSBLong(image);
1894 dds_info->pixelformat.g_bitmask = ReadBlobLSBLong(image);
1895 dds_info->pixelformat.b_bitmask = ReadBlobLSBLong(image);
1896 dds_info->pixelformat.alpha_bitmask = ReadBlobLSBLong(image);
1898 dds_info->ddscaps1 = ReadBlobLSBLong(image);
1899 dds_info->ddscaps2 = ReadBlobLSBLong(image);
1900 (void) SeekBlob(image, 12, SEEK_CUR); /* 3 reserved DWORDs */
1905 static MagickBooleanType ReadDXT1(Image *image, DDSInfo *dds_info,
1906 ExceptionInfo *exception)
1932 for (y = 0; y < (ssize_t) dds_info->height; y += 4)
1934 for (x = 0; x < (ssize_t) dds_info->width; x += 4)
1936 /* Get 4x4 patch of pixels to write on */
1937 q = QueueAuthenticPixels(image, x, y, Min(4, dds_info->width - x),
1938 Min(4, dds_info->height - y),exception);
1940 if (q == (Quantum *) NULL)
1943 /* Read 8 bytes of data from the image */
1944 c0 = ReadBlobLSBShort(image);
1945 c1 = ReadBlobLSBShort(image);
1946 bits = ReadBlobLSBLong(image);
1948 CalculateColors(c0, c1, &colors, MagickFalse);
1950 /* Write the pixels */
1951 for (j = 0; j < 4; j++)
1953 for (i = 0; i < 4; i++)
1955 if ((x + i) < (ssize_t) dds_info->width &&
1956 (y + j) < (ssize_t) dds_info->height)
1958 code = (unsigned char) ((bits >> ((j*4+i)*2)) & 0x3);
1959 SetPixelRed(image,ScaleCharToQuantum(colors.r[code]),q);
1960 SetPixelGreen(image,ScaleCharToQuantum(colors.g[code]),q);
1961 SetPixelBlue(image,ScaleCharToQuantum(colors.b[code]),q);
1962 SetPixelAlpha(image,ScaleCharToQuantum(colors.a[code]),q);
1963 if (colors.a[code] && (image->alpha_trait != BlendPixelTrait))
1964 image->alpha_trait=BlendPixelTrait; /* Correct matte */
1965 q+=GetPixelChannels(image);
1970 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1975 SkipDXTMipmaps(image, dds_info, 8);
1980 static MagickBooleanType ReadDXT3(Image *image, DDSInfo *dds_info,
1981 ExceptionInfo *exception)
2010 for (y = 0; y < (ssize_t) dds_info->height; y += 4)
2012 for (x = 0; x < (ssize_t) dds_info->width; x += 4)
2014 /* Get 4x4 patch of pixels to write on */
2015 q = QueueAuthenticPixels(image, x, y, Min(4, dds_info->width - x),
2016 Min(4, dds_info->height - y),exception);
2018 if (q == (Quantum *) NULL)
2021 /* Read alpha values (8 bytes) */
2022 a0 = ReadBlobLSBLong(image);
2023 a1 = ReadBlobLSBLong(image);
2025 /* Read 8 bytes of data from the image */
2026 c0 = ReadBlobLSBShort(image);
2027 c1 = ReadBlobLSBShort(image);
2028 bits = ReadBlobLSBLong(image);
2030 CalculateColors(c0, c1, &colors, MagickTrue);
2032 /* Write the pixels */
2033 for (j = 0; j < 4; j++)
2035 for (i = 0; i < 4; i++)
2037 if ((x + i) < (ssize_t) dds_info->width && (y + j) < (ssize_t) dds_info->height)
2039 code = (bits >> ((4*j+i)*2)) & 0x3;
2040 SetPixelRed(image,ScaleCharToQuantum(colors.r[code]),q);
2041 SetPixelGreen(image,ScaleCharToQuantum(colors.g[code]),q);
2042 SetPixelBlue(image,ScaleCharToQuantum(colors.b[code]),q);
2044 Extract alpha value: multiply 0..15 by 17 to get range 0..255
2047 alpha = 17U * (unsigned char) ((a0 >> (4*(4*j+i))) & 0xf);
2049 alpha = 17U * (unsigned char) ((a1 >> (4*(4*(j-2)+i))) & 0xf);
2050 SetPixelAlpha(image,ScaleCharToQuantum((unsigned char) alpha),q);
2051 q+=GetPixelChannels(image);
2056 if (SyncAuthenticPixels(image,exception) == MagickFalse)
2061 SkipDXTMipmaps(image, dds_info, 16);
2066 static MagickBooleanType ReadDXT5(Image *image, DDSInfo *dds_info,
2067 ExceptionInfo *exception)
2100 for (y = 0; y < (ssize_t) dds_info->height; y += 4)
2102 for (x = 0; x < (ssize_t) dds_info->width; x += 4)
2104 /* Get 4x4 patch of pixels to write on */
2105 q = QueueAuthenticPixels(image, x, y, Min(4, dds_info->width - x),
2106 Min(4, dds_info->height - y),exception);
2108 if (q == (Quantum *) NULL)
2111 /* Read alpha values (8 bytes) */
2112 a0 = (unsigned char) ReadBlobByte(image);
2113 a1 = (unsigned char) ReadBlobByte(image);
2115 alpha_bits = (MagickSizeType)ReadBlobLSBLong(image);
2116 alpha_bits = alpha_bits | ((MagickSizeType)ReadBlobLSBShort(image) << 32);
2118 /* Read 8 bytes of data from the image */
2119 c0 = ReadBlobLSBShort(image);
2120 c1 = ReadBlobLSBShort(image);
2121 bits = ReadBlobLSBLong(image);
2123 CalculateColors(c0, c1, &colors, MagickTrue);
2125 /* Write the pixels */
2126 for (j = 0; j < 4; j++)
2128 for (i = 0; i < 4; i++)
2130 if ((x + i) < (ssize_t) dds_info->width &&
2131 (y + j) < (ssize_t) dds_info->height)
2133 code = (bits >> ((4*j+i)*2)) & 0x3;
2134 SetPixelRed(image,ScaleCharToQuantum(colors.r[code]),q);
2135 SetPixelGreen(image,ScaleCharToQuantum(colors.g[code]),q);
2136 SetPixelBlue(image,ScaleCharToQuantum(colors.b[code]),q);
2137 /* Extract alpha value */
2138 alpha_code = (size_t) (alpha_bits >> (3*(4*j+i))) & 0x7;
2139 if (alpha_code == 0)
2141 else if (alpha_code == 1)
2144 alpha = ((8-alpha_code) * a0 + (alpha_code-1) * a1) / 7;
2145 else if (alpha_code == 6)
2147 else if (alpha_code == 7)
2150 alpha = (((6-alpha_code) * a0 + (alpha_code-1) * a1) / 5);
2151 SetPixelAlpha(image,ScaleCharToQuantum((unsigned char) alpha),q);
2152 q+=GetPixelChannels(image);
2157 if (SyncAuthenticPixels(image,exception) == MagickFalse)
2162 SkipDXTMipmaps(image, dds_info, 16);
2167 static MagickBooleanType ReadUncompressedRGB(Image *image, DDSInfo *dds_info,
2168 ExceptionInfo *exception)
2176 for (y = 0; y < (ssize_t) dds_info->height; y++)
2178 q = QueueAuthenticPixels(image, 0, y, dds_info->width, 1,exception);
2180 if (q == (Quantum *) NULL)
2183 for (x = 0; x < (ssize_t) dds_info->width; x++)
2185 SetPixelBlue(image,ScaleCharToQuantum((unsigned char)
2186 ReadBlobByte(image)),q);
2187 SetPixelGreen(image,ScaleCharToQuantum((unsigned char)
2188 ReadBlobByte(image)),q);
2189 SetPixelRed(image,ScaleCharToQuantum((unsigned char)
2190 ReadBlobByte(image)),q);
2191 if (dds_info->pixelformat.rgb_bitcount == 32)
2192 (void) ReadBlobByte(image);
2193 q+=GetPixelChannels(image);
2196 if (SyncAuthenticPixels(image,exception) == MagickFalse)
2200 SkipRGBMipmaps(image, dds_info, 3);
2205 static MagickBooleanType ReadUncompressedRGBA(Image *image, DDSInfo *dds_info,
2206 ExceptionInfo *exception)
2214 for (y = 0; y < (ssize_t) dds_info->height; y++)
2216 q = QueueAuthenticPixels(image, 0, y, dds_info->width, 1,exception);
2218 if (q == (Quantum *) NULL)
2221 for (x = 0; x < (ssize_t) dds_info->width; x++)
2223 SetPixelBlue(image,ScaleCharToQuantum((unsigned char)
2224 ReadBlobByte(image)),q);
2225 SetPixelGreen(image,ScaleCharToQuantum((unsigned char)
2226 ReadBlobByte(image)),q);
2227 SetPixelRed(image,ScaleCharToQuantum((unsigned char)
2228 ReadBlobByte(image)),q);
2229 SetPixelAlpha(image,ScaleCharToQuantum((unsigned char)
2230 ReadBlobByte(image)),q);
2231 q+=GetPixelChannels(image);
2234 if (SyncAuthenticPixels(image,exception) == MagickFalse)
2238 SkipRGBMipmaps(image, dds_info, 4);
2244 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2248 % R e g i s t e r D D S I m a g e %
2252 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2254 % RegisterDDSImage() adds attributes for the DDS image format to
2255 % the list of supported formats. The attributes include the image format
2256 % tag, a method to read and/or write the format, whether the format
2257 % supports the saving of more than one frame to the same file or blob,
2258 % whether the format supports native in-memory I/O, and a brief
2259 % description of the format.
2261 % The format of the RegisterDDSImage method is:
2263 % RegisterDDSImage(void)
2266 ModuleExport size_t RegisterDDSImage(void)
2271 entry = SetMagickInfo("DDS");
2272 entry->decoder = (DecodeImageHandler *) ReadDDSImage;
2273 entry->encoder = (EncodeImageHandler *) WriteDDSImage;
2274 entry->magick = (IsImageFormatHandler *) IsDDS;
2275 entry->seekable_stream=MagickTrue;
2276 entry->description = ConstantString("Microsoft DirectDraw Surface");
2277 entry->module = ConstantString("DDS");
2278 (void) RegisterMagickInfo(entry);
2279 entry = SetMagickInfo("DXT1");
2280 entry->decoder = (DecodeImageHandler *) ReadDDSImage;
2281 entry->encoder = (EncodeImageHandler *) WriteDDSImage;
2282 entry->magick = (IsImageFormatHandler *) IsDDS;
2283 entry->seekable_stream=MagickTrue;
2284 entry->description = ConstantString("Microsoft DirectDraw Surface");
2285 entry->module = ConstantString("DDS");
2286 (void) RegisterMagickInfo(entry);
2287 entry = SetMagickInfo("DXT5");
2288 entry->decoder = (DecodeImageHandler *) ReadDDSImage;
2289 entry->encoder = (EncodeImageHandler *) WriteDDSImage;
2290 entry->magick = (IsImageFormatHandler *) IsDDS;
2291 entry->seekable_stream=MagickTrue;
2292 entry->description = ConstantString("Microsoft DirectDraw Surface");
2293 entry->module = ConstantString("DDS");
2294 (void) RegisterMagickInfo(entry);
2295 return(MagickImageCoderSignature);
2298 static void RemapIndices(const ssize_t *map, const unsigned char *source,
2299 unsigned char *target)
2304 for (i = 0; i < 16; i++)
2309 target[i] = source[map[i]];
2314 Skip the mipmap images for compressed (DXTn) dds files
2316 static void SkipDXTMipmaps(Image *image, DDSInfo *dds_info, int texel_size)
2329 Only skip mipmaps for textures and cube maps
2331 if (dds_info->ddscaps1 & DDSCAPS_MIPMAP
2332 && (dds_info->ddscaps1 & DDSCAPS_TEXTURE
2333 || dds_info->ddscaps2 & DDSCAPS2_CUBEMAP))
2335 w = DIV2(dds_info->width);
2336 h = DIV2(dds_info->height);
2339 Mipmapcount includes the main image, so start from one
2341 for (i = 1; (i < (ssize_t) dds_info->mipmapcount) && w && h; i++)
2343 offset = (MagickOffsetType) ((w + 3) / 4) * ((h + 3) / 4) * texel_size;
2344 (void) SeekBlob(image, offset, SEEK_CUR);
2353 Skip the mipmap images for uncompressed (RGB or RGBA) dds files
2355 static void SkipRGBMipmaps(Image *image, DDSInfo *dds_info, int pixel_size)
2368 Only skip mipmaps for textures and cube maps
2370 if (dds_info->ddscaps1 & DDSCAPS_MIPMAP
2371 && (dds_info->ddscaps1 & DDSCAPS_TEXTURE
2372 || dds_info->ddscaps2 & DDSCAPS2_CUBEMAP))
2374 w = DIV2(dds_info->width);
2375 h = DIV2(dds_info->height);
2378 Mipmapcount includes the main image, so start from one
2380 for (i=1; (i < (ssize_t) dds_info->mipmapcount) && w && h; i++)
2382 offset = (MagickOffsetType) w * h * pixel_size;
2383 (void) SeekBlob(image, offset, SEEK_CUR);
2392 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2396 % U n r e g i s t e r D D S I m a g e %
2400 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2402 % UnregisterDDSImage() removes format registrations made by the
2403 % DDS module from the list of supported formats.
2405 % The format of the UnregisterDDSImage method is:
2407 % UnregisterDDSImage(void)
2410 ModuleExport void UnregisterDDSImage(void)
2412 (void) UnregisterMagickInfo("DDS");
2413 (void) UnregisterMagickInfo("DXT1");
2414 (void) UnregisterMagickInfo("DXT5");
2417 static void WriteAlphas(Image *image, const ssize_t *alphas, size_t min5,
2418 size_t max5, size_t min7, size_t max7)
2432 FixRange(min5,max5,5);
2433 err5 = CompressAlpha(min5,max5,5,alphas,indices5);
2435 FixRange(min7,max7,7);
2436 err7 = CompressAlpha(min7,max7,7,alphas,indices7);
2440 for (i=0; i < 16; i++)
2445 index = indices7[i];
2448 else if (index == 1)
2451 indices5[i] = 9 - index;
2458 (void) WriteBlobByte(image,(unsigned char) min5);
2459 (void) WriteBlobByte(image,(unsigned char) max5);
2461 for(i=0; i < 2; i++)
2466 for (j=0; j < 8; j++)
2468 size_t index = (size_t) indices5[j + i*8];
2469 value |= ( index << 3*j );
2472 for (j=0; j < 3; j++)
2474 size_t byte = (value >> 8*j) & 0xff;
2475 (void) WriteBlobByte(image,(unsigned char) byte);
2480 static void WriteCompressed(Image *image, const size_t count,
2481 DDSVector4 *points, const ssize_t *map, const MagickBooleanType clusterFit)
2497 VectorInit(metric,1.0f);
2498 VectorInit3(start,0.0f);
2499 VectorInit3(end,0.0f);
2501 ComputeWeightedCovariance(count,points,covariance);
2502 ComputePrincipleComponent(covariance,&principle);
2504 if (clusterFit == MagickFalse || count == 0)
2505 CompressRangeFit(count,points,map,principle,metric,&start,&end,indices);
2507 CompressClusterFit(count,points,map,principle,metric,&start,&end,indices);
2509 WriteIndices(image,start,end,indices);
2513 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2517 % W r i t e D D S I m a g e %
2521 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2523 % WriteDDSImage() writes a DirectDraw Surface image file in the DXT5 format.
2525 % The format of the WriteBMPImage method is:
2527 % MagickBooleanType WriteDDSImage(const ImageInfo *image_info,Image *image)
2529 % A description of each parameter follows.
2531 % o image_info: the image info.
2533 % o image: The image.
2536 static MagickBooleanType WriteDDSImage(const ImageInfo *image_info,
2537 Image *image, ExceptionInfo *exception)
2555 assert(image_info != (const ImageInfo *) NULL);
2556 assert(image_info->signature == MagickSignature);
2557 assert(image != (Image *) NULL);
2558 assert(image->signature == MagickSignature);
2559 if (image->debug != MagickFalse)
2560 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2561 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
2562 if (status == MagickFalse)
2564 if (IssRGBCompatibleColorspace(image->colorspace) == MagickFalse)
2565 (void) TransformImageColorspace(image,sRGBColorspace,exception);
2567 pixelFormat=DDPF_FOURCC;
2568 compression=FOURCC_DXT5;
2570 if (image->alpha_trait != BlendPixelTrait)
2571 compression=FOURCC_DXT1;
2573 if (LocaleCompare(image_info->magick,"dxt1") == 0)
2574 compression=FOURCC_DXT1;
2576 option=GetImageOption(image_info,"dds:compression");
2577 if (option != (char *) NULL)
2579 if (LocaleCompare(option,"dxt1") == 0)
2580 compression=FOURCC_DXT1;
2581 if (LocaleCompare(option,"none") == 0)
2582 pixelFormat=DDPF_RGB;
2585 clusterFit=MagickFalse;
2586 weightByAlpha=MagickFalse;
2588 if (pixelFormat == DDPF_FOURCC)
2590 option=GetImageOption(image_info,"dds:cluster-fit");
2591 if (option != (char *) NULL && LocaleCompare(option,"true") == 0)
2593 clusterFit=MagickFalse;
2594 if (compression != FOURCC_DXT1)
2596 option=GetImageOption(image_info,"dds:weight-by-alpha");
2597 if (option != (char *) NULL && LocaleCompare(option,"true") == 0)
2598 weightByAlpha=MagickTrue;
2605 if ((image->columns & (image->columns - 1)) == 0 &&
2606 (image->rows & (image->rows - 1)) == 0)
2608 option=GetImageOption(image_info,"dds:mipmaps");
2609 if (option != (char *) NULL)
2610 maxMipmaps=StringToUnsignedLong(option);
2612 if (maxMipmaps != 0)
2614 columns=image->columns;
2616 while (columns != 1 && rows != 1 && mipmaps != maxMipmaps)
2618 columns=DIV2(columns);
2625 WriteDDSInfo(image,pixelFormat,compression,mipmaps);
2627 WriteImageData(image,pixelFormat,compression,clusterFit,weightByAlpha,
2630 if (mipmaps > 0 && WriteMipmaps(image,pixelFormat,compression,mipmaps,
2631 clusterFit,weightByAlpha,exception) == MagickFalse)
2632 return(MagickFalse);
2634 (void) CloseBlob(image);
2638 static void WriteDDSInfo(Image *image, const size_t pixelFormat,
2639 const size_t compression, const size_t mipmaps)
2649 flags=(unsigned int) (DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT |
2650 DDSD_PIXELFORMAT | DDSD_LINEARSIZE);
2651 caps=(unsigned int) DDSCAPS_TEXTURE;
2652 format=(unsigned int) pixelFormat;
2656 flags=flags | (unsigned int) DDSD_MIPMAPCOUNT;
2657 caps=caps | (unsigned int) (DDSCAPS_MIPMAP | DDSCAPS_COMPLEX);
2660 if (format != DDPF_FOURCC && image->alpha_trait == BlendPixelTrait)
2661 format=format | DDPF_ALPHAPIXELS;
2663 (void) WriteBlob(image,4,(unsigned char *) "DDS ");
2664 (void) WriteBlobLSBLong(image,124);
2665 (void) WriteBlobLSBLong(image,flags);
2666 (void) WriteBlobLSBLong(image,image->rows);
2667 (void) WriteBlobLSBLong(image,image->columns);
2669 if (compression == FOURCC_DXT1)
2670 (void) WriteBlobLSBLong(image,
2671 (unsigned int) (Max(1,(image->columns+3)/4) * 8));
2673 (void) WriteBlobLSBLong(image,
2674 (unsigned int) (Max(1,(image->columns+3)/4) * 16));
2676 (void) WriteBlobLSBLong(image,0x00);
2677 (void) WriteBlobLSBLong(image,(unsigned int) mipmaps+1);
2678 (void) WriteBlob(image,44,(unsigned char *) "IMAGEMAGICK");
2680 (void) WriteBlobLSBLong(image,32);
2681 (void) WriteBlobLSBLong(image,format);
2683 if (pixelFormat == DDPF_FOURCC)
2685 (void) WriteBlobLSBLong(image,(unsigned int) compression);
2686 for(i=0;i < 5;i++) // bitcount / masks
2687 (void) WriteBlobLSBLong(image,0x00);
2691 (void) WriteBlobLSBLong(image,0x00);
2692 if (image->alpha_trait == BlendPixelTrait)
2694 (void) WriteBlobLSBLong(image,32);
2695 (void) WriteBlobLSBLong(image,0xff0000);
2696 (void) WriteBlobLSBLong(image,0xff00);
2697 (void) WriteBlobLSBLong(image,0xff);
2698 (void) WriteBlobLSBLong(image,0xff000000);
2702 (void) WriteBlobLSBLong(image,24);
2703 (void) WriteBlobLSBLong(image,0xff);
2704 (void) WriteBlobLSBLong(image,0x00);
2705 (void) WriteBlobLSBLong(image,0x00);
2706 (void) WriteBlobLSBLong(image,0x00);
2710 (void) WriteBlobLSBLong(image,caps);
2711 for(i=0;i < 4;i++) // ddscaps2 + reserved region
2712 (void) WriteBlobLSBLong(image,0x00);
2715 static void WriteFourCC(Image *image, const size_t compression,
2716 const MagickBooleanType clusterFit, const MagickBooleanType weightByAlpha,
2717 ExceptionInfo *exception)
2728 register const Quantum
2731 for (y=0; y < (ssize_t) image->rows; y+=4)
2733 for (x=0; x < (ssize_t) image->columns; x+=4)
2758 if (x + columns >= image->columns)
2759 columns = image->columns - x;
2761 if (y + rows >= image->rows)
2762 rows = image->rows - y;
2764 p=GetVirtualPixels(image,x,y,columns,rows,exception);
2766 for (i=0; i<16; i++)
2772 for (by=0; by < rows; by++)
2774 for (bx=0; bx < columns; bx++)
2776 if (compression == FOURCC_DXT5)
2777 alpha = ScaleQuantumToChar(GetPixelAlpha(image,p));
2781 alphas[4*by + bx] = (size_t)alpha;
2783 point.x = (float)ScaleQuantumToChar(GetPixelRed(image,p)) / 255.0f;
2784 point.y = (float)ScaleQuantumToChar(GetPixelGreen(image,p)) / 255.0f;
2785 point.z = (float)ScaleQuantumToChar(GetPixelBlue(image,p)) / 255.0f;
2786 point.w = weightByAlpha ? (float)(alpha + 1) / 256.0f : 1.0f;
2787 p+=GetPixelChannels(image);
2789 match = MagickFalse;
2790 for (i=0; i < count; i++)
2792 if ((points[i].x == point.x) &&
2793 (points[i].y == point.y) &&
2794 (points[i].z == point.z) &&
2795 (alpha >= 128 || compression == FOURCC_DXT5))
2797 points[i].w += point.w;
2804 if (match == MagickTrue)
2807 points[count].x = point.x;
2808 points[count].y = point.y;
2809 points[count].z = point.z;
2810 points[count].w = point.w;
2811 map[4*by + bx] = count;
2814 if (compression == FOURCC_DXT5)
2820 if (alpha != 0 && alpha < min5)
2822 if (alpha != 255 && alpha > max5)
2828 for (i=0; i < count; i++)
2829 points[i].w = sqrt(points[i].w);
2831 if (compression == FOURCC_DXT5)
2832 WriteAlphas(image,alphas,min5,max5,min7,max7);
2835 WriteSingleColorFit(image,points,map);
2837 WriteCompressed(image,count,points,map,clusterFit);
2842 static void WriteImageData(Image *image, const size_t pixelFormat,
2843 const size_t compression,const MagickBooleanType clusterFit,
2844 const MagickBooleanType weightByAlpha, ExceptionInfo *exception)
2846 if (pixelFormat == DDPF_FOURCC)
2847 WriteFourCC(image,compression,clusterFit,weightByAlpha,exception);
2849 WriteUncompressed(image,exception);
2852 static inline size_t ClampToLimit(const float value, const size_t limit)
2855 result = (int) (value + 0.5f);
2864 static inline size_t ColorTo565(const DDSVector3 point)
2866 size_t r = ClampToLimit(31.0f*point.x,31);
2867 size_t g = ClampToLimit(63.0f*point.y,63);
2868 size_t b = ClampToLimit(31.0f*point.z,31);
2870 return (r << 11) | (g << 5) | b;
2873 static void WriteIndices(Image *image, const DDSVector3 start,
2874 const DDSVector3 end, unsigned char *indices)
2889 a = ColorTo565(start);
2890 b = ColorTo565(end);
2892 for (i=0; i<16; i++)
2895 remapped[i] = (indices[i] ^ 0x1) & 0x3;
2899 remapped[i] = indices[i];
2905 (void) WriteBlobByte(image,(unsigned char) (a & 0xff));
2906 (void) WriteBlobByte(image,(unsigned char) (a >> 8));
2907 (void) WriteBlobByte(image,(unsigned char) (b & 0xff));
2908 (void) WriteBlobByte(image,(unsigned char) (b >> 8));
2912 ind = remapped + 4*i;
2913 (void) WriteBlobByte(image,ind[0] | (ind[1] << 2) | (ind[2] << 4) |
2918 static MagickBooleanType WriteMipmaps(Image *image, const size_t pixelFormat,
2919 const size_t compression, const size_t mipmaps,
2920 const MagickBooleanType clusterFit, const MagickBooleanType weightByAlpha,
2921 ExceptionInfo *exception)
2933 columns = image->columns;
2936 for (i=0; i<mipmaps; i++)
2938 resize_image = ResizeImage(image,columns/2,rows/2,TriangleFilter,
2941 if (resize_image == (Image *) NULL)
2942 return(MagickFalse);
2944 DestroyBlob(resize_image);
2945 resize_image->blob=ReferenceBlob(image->blob);
2947 WriteImageData(resize_image,pixelFormat,compression,weightByAlpha,
2948 clusterFit,exception);
2950 resize_image=DestroyImage(resize_image);
2952 columns = DIV2(columns);
2959 static void WriteSingleColorFit(Image *image, const DDSVector4 *points,
2971 color[0] = ClampToLimit(255.0f*points->x,255);
2972 color[1] = ClampToLimit(255.0f*points->y,255);
2973 color[2] = ClampToLimit(255.0f*points->z,255);
2975 ComputeEndPoints(DDS_LOOKUP,color,&start,&end,&index);
2977 RemapIndices(map,&index,indices);
2978 WriteIndices(image,start,end,indices);
2981 static void WriteUncompressed(Image *image, ExceptionInfo *exception)
2983 register const Quantum
2992 for (y=0; y < (ssize_t) image->rows; y++)
2994 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
2996 for (x=0; x < (ssize_t) image->columns; x++)
2998 (void) WriteBlobByte(image,ScaleQuantumToChar(GetPixelBlue(image,p)));
2999 (void) WriteBlobByte(image,ScaleQuantumToChar(GetPixelGreen(image,p)));
3000 (void) WriteBlobByte(image,ScaleQuantumToChar(GetPixelRed(image,p)));
3001 if (image->alpha_trait == BlendPixelTrait)
3002 (void) WriteBlobByte(image,ScaleQuantumToChar(GetPixelAlpha(image,p)));
3003 p+=GetPixelChannels(image);