blob: 51217d7b31d4b0ff3a1499fbe57c451fe960af4c [file] [log] [blame]
cristy3ed852e2009-09-05 21:47:34 +00001/*
2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3% %
4% %
5% %
6% DDDD DDDD SSSSS %
7% D D D D SS %
8% D D D D SSS %
9% D D D D SS %
10% DDDD DDDD SSSSS %
11% %
12% %
13% Read Microsoft Direct Draw Surface Image Format %
14% %
15% Software Design %
16% Bianca van Schaik %
17% March 2008 %
18% %
19% %
cristy1454be72011-12-19 01:52:48 +000020% Copyright 1999-2012 ImageMagick Studio LLC, a non-profit organization %
cristy3ed852e2009-09-05 21:47:34 +000021% dedicated to making software imaging solutions freely available. %
22% %
23% You may not use this file except in compliance with the License. You may %
24% obtain a copy of the License at %
25% %
26% http://www.imagemagick.org/script/license.php %
27% %
28% Unless required by applicable law or agreed to in writing, software %
29% distributed under the License is distributed on an "AS IS" BASIS, %
30% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31% See the License for the specific language governing permissions and %
32% limitations under the License. %
33% %
34%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35%
36%
37*/
38
39/*
40 Include declarations.
41*/
cristy4c08aed2011-07-01 19:47:50 +000042#include "MagickCore/studio.h"
43#include "MagickCore/blob.h"
44#include "MagickCore/blob-private.h"
45#include "MagickCore/cache.h"
46#include "MagickCore/colorspace.h"
47#include "MagickCore/exception.h"
48#include "MagickCore/exception-private.h"
49#include "MagickCore/image.h"
50#include "MagickCore/image-private.h"
51#include "MagickCore/list.h"
52#include "MagickCore/log.h"
53#include "MagickCore/magick.h"
54#include "MagickCore/memory_.h"
55#include "MagickCore/monitor.h"
56#include "MagickCore/monitor-private.h"
57#include "MagickCore/profile.h"
58#include "MagickCore/quantum-private.h"
59#include "MagickCore/static.h"
60#include "MagickCore/string_.h"
61#include "MagickCore/module.h"
62#include "MagickCore/transform.h"
63#include "MagickCore/studio.h"
64#include "MagickCore/blob.h"
65#include "MagickCore/blob-private.h"
66#include "MagickCore/colorspace.h"
67#include "MagickCore/exception.h"
68#include "MagickCore/exception-private.h"
69#include "MagickCore/compress.h"
70#include "MagickCore/image.h"
71#include "MagickCore/image-private.h"
72#include "MagickCore/list.h"
73#include "MagickCore/magick.h"
74#include "MagickCore/memory_.h"
75#include "MagickCore/monitor.h"
76#include "MagickCore/monitor-private.h"
77#include "MagickCore/pixel-accessor.h"
78#include "MagickCore/quantum.h"
79#include "MagickCore/static.h"
80#include "MagickCore/string_.h"
cristy3ed852e2009-09-05 21:47:34 +000081
82/*
83 Definitions
84*/
85#define DDSD_CAPS 0x00000001
86#define DDSD_HEIGHT 0x00000002
87#define DDSD_WIDTH 0x00000004
88#define DDSD_PITCH 0x00000008
89#define DDSD_PIXELFORMAT 0x00001000
90#define DDSD_MIPMAPCOUNT 0x00020000
91#define DDSD_LINEARSIZE 0x00080000
92#define DDSD_DEPTH 0x00800000
93
94#define DDPF_ALPHAPIXELS 0x00000001
95#define DDPF_FOURCC 0x00000004
96#define DDPF_RGB 0x00000040
97
98#define FOURCC_DXT1 0x31545844
99#define FOURCC_DXT3 0x33545844
100#define FOURCC_DXT5 0x35545844
101
102#define DDSCAPS_COMPLEX 0x00000008
103#define DDSCAPS_TEXTURE 0x00001000
104#define DDSCAPS_MIPMAP 0x00400000
105
106#define DDSCAPS2_CUBEMAP 0x00000200
107#define DDSCAPS2_CUBEMAP_POSITIVEX 0x00000400
108#define DDSCAPS2_CUBEMAP_NEGATIVEX 0x00000800
109#define DDSCAPS2_CUBEMAP_POSITIVEY 0x00001000
110#define DDSCAPS2_CUBEMAP_NEGATIVEY 0x00002000
111#define DDSCAPS2_CUBEMAP_POSITIVEZ 0x00004000
112#define DDSCAPS2_CUBEMAP_NEGATIVEZ 0x00008000
113#define DDSCAPS2_VOLUME 0x00200000
114
115/*
116 Structure declarations.
117*/
118typedef struct _DDSPixelFormat
119{
cristybb503372010-05-27 20:51:26 +0000120 size_t
cristy3ed852e2009-09-05 21:47:34 +0000121 flags,
122 fourcc,
123 rgb_bitcount,
124 r_bitmask,
125 g_bitmask,
126 b_bitmask,
127 alpha_bitmask;
128} DDSPixelFormat;
129
130typedef struct _DDSInfo
131{
cristybb503372010-05-27 20:51:26 +0000132 size_t
cristyc5de6992009-10-06 19:19:48 +0000133 flags,
134 height,
135 width,
136 pitchOrLinearSize,
137 depth,
138 mipmapcount,
cristy3ed852e2009-09-05 21:47:34 +0000139 ddscaps1,
140 ddscaps2;
141
142 DDSPixelFormat
143 pixelformat;
144} DDSInfo;
145
146typedef struct _DDSColors
147{
148 unsigned char
149 r[4],
150 g[4],
151 b[4],
152 a[4];
153} DDSColors;
154
155typedef MagickBooleanType
cristyc82a27b2011-10-21 01:07:16 +0000156 DDSDecoder(Image *,DDSInfo *,ExceptionInfo *);
cristy3ed852e2009-09-05 21:47:34 +0000157
158/*
159 Macros
160*/
161#define C565_r(x) (((x) & 0xF800) >> 11)
162#define C565_g(x) (((x) & 0x07E0) >> 5)
163#define C565_b(x) ((x) & 0x001F)
164
165#define C565_red(x) ( (C565_r(x) << 3 | C565_r(x) >> 2))
166#define C565_green(x) ( (C565_g(x) << 2 | C565_g(x) >> 4))
167#define C565_blue(x) ( (C565_b(x) << 3 | C565_b(x) >> 2))
168
169#define DIV2(x) ((x) > 1 ? ((x) >> 1) : 1)
170
171/*
172 Forward declarations
173*/
174static MagickBooleanType
175 ReadDDSInfo(Image *image, DDSInfo *dds_info);
176
177static void
178 CalculateColors(unsigned short c0, unsigned short c1,
179 DDSColors *c, MagickBooleanType ignoreAlpha);
180
181static MagickBooleanType
cristyc82a27b2011-10-21 01:07:16 +0000182 ReadDXT1(Image *, DDSInfo *,ExceptionInfo *);
cristy3ed852e2009-09-05 21:47:34 +0000183
184static MagickBooleanType
cristyc82a27b2011-10-21 01:07:16 +0000185 ReadDXT3(Image *image, DDSInfo *dds_info,ExceptionInfo *);
cristy3ed852e2009-09-05 21:47:34 +0000186
187static MagickBooleanType
cristyc82a27b2011-10-21 01:07:16 +0000188 ReadDXT5(Image *image, DDSInfo *dds_info,ExceptionInfo *);
cristy3ed852e2009-09-05 21:47:34 +0000189
190static MagickBooleanType
cristyc82a27b2011-10-21 01:07:16 +0000191 ReadUncompressedRGB(Image *image, DDSInfo *dds_info,ExceptionInfo *);
cristy3ed852e2009-09-05 21:47:34 +0000192
193static MagickBooleanType
cristyc82a27b2011-10-21 01:07:16 +0000194 ReadUncompressedRGBA(Image *image, DDSInfo *dds_info,ExceptionInfo *);
cristy3ed852e2009-09-05 21:47:34 +0000195
196static void
197 SkipDXTMipmaps(Image *image, DDSInfo *dds_info, int texel_size);
198
199static void
200 SkipRGBMipmaps(Image *image, DDSInfo *dds_info, int pixel_size);
201
202/*
203%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
204% %
205% %
206% %
207% R e a d D D S I m a g e %
208% %
209% %
210% %
211%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
212%
213% ReadDDSImage() reads a DirectDraw Surface image file and returns it. It
214% allocates the memory necessary for the new Image structure and returns a
215% pointer to the new image.
216%
217% The format of the ReadDDSImage method is:
218%
219% Image *ReadDDSImage(const ImageInfo *image_info,ExceptionInfo *exception)
220%
221% A description of each parameter follows:
222%
223% o image_info: The image info.
224%
225% o exception: return any errors or warnings in this structure.
226%
227*/
228
cristybb503372010-05-27 20:51:26 +0000229static inline size_t Min(size_t one, size_t two)
cristy3ed852e2009-09-05 21:47:34 +0000230{
231 if (one < two)
232 return one;
233 return two;
234}
235
236static Image *ReadDDSImage(const ImageInfo *image_info,ExceptionInfo *exception)
237{
238 Image
239 *image;
240
241 MagickBooleanType
242 status,
243 cubemap = MagickFalse,
244 volume = MagickFalse,
245 matte;
246
247 CompressionType
248 compression;
249
250 DDSInfo
cristyc5de6992009-10-06 19:19:48 +0000251 dds_info;
cristy3ed852e2009-09-05 21:47:34 +0000252
253 DDSDecoder
254 *decoder;
255
cristybb503372010-05-27 20:51:26 +0000256 size_t
cristy3ed852e2009-09-05 21:47:34 +0000257 n, num_images;
258
259 /*
260 Open image file.
261 */
262 assert(image_info != (const ImageInfo *) NULL);
263 assert(image_info->signature == MagickSignature);
264 if (image_info->debug != MagickFalse)
265 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
266 image_info->filename);
267 assert(exception != (ExceptionInfo *) NULL);
268 assert(exception->signature == MagickSignature);
cristy9950d572011-10-01 18:22:35 +0000269 image=AcquireImage(image_info,exception);
cristy3ed852e2009-09-05 21:47:34 +0000270 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
271 if (status == MagickFalse)
272 {
273 image=DestroyImageList(image);
274 return((Image *) NULL);
275 }
276
277 /*
278 Initialize image structure.
279 */
280 if (ReadDDSInfo(image, &dds_info) != MagickTrue) {
cristyc5de6992009-10-06 19:19:48 +0000281 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
cristy3ed852e2009-09-05 21:47:34 +0000282 }
283
284 if (dds_info.ddscaps2 & DDSCAPS2_CUBEMAP)
285 cubemap = MagickTrue;
286
287 if (dds_info.ddscaps2 & DDSCAPS2_VOLUME && dds_info.depth > 0)
288 volume = MagickTrue;
289
290 (void) SeekBlob(image, 128, SEEK_SET);
291
292 /*
293 Determine pixel format
294 */
295 if (dds_info.pixelformat.flags & DDPF_RGB)
296 {
297 compression = NoCompression;
298 if (dds_info.pixelformat.flags & DDPF_ALPHAPIXELS)
299 {
300 matte = MagickTrue;
301 decoder = ReadUncompressedRGBA;
302 }
303 else
304 {
305 matte = MagickTrue;
306 decoder = ReadUncompressedRGB;
307 }
308 }
309 else if (dds_info.pixelformat.flags & DDPF_FOURCC)
310 {
311 switch (dds_info.pixelformat.fourcc)
312 {
313 case FOURCC_DXT1:
314 {
315 matte = MagickFalse;
316 compression = DXT1Compression;
317 decoder = ReadDXT1;
318 break;
319 }
320
321 case FOURCC_DXT3:
322 {
323 matte = MagickTrue;
324 compression = DXT3Compression;
325 decoder = ReadDXT3;
326 break;
327 }
328
329 case FOURCC_DXT5:
330 {
331 matte = MagickTrue;
332 compression = DXT5Compression;
333 decoder = ReadDXT5;
334 break;
335 }
336
337 default:
338 {
339 /* Unknown FOURCC */
340 ThrowReaderException(CorruptImageError, "ImageTypeNotSupported");
341 }
342 }
343 }
344 else
345 {
346 /* Neither compressed nor uncompressed... thus unsupported */
347 ThrowReaderException(CorruptImageError, "ImageTypeNotSupported");
348 }
349
350 num_images = 1;
351 if (cubemap)
352 {
353 /*
354 Determine number of faces defined in the cubemap
355 */
356 num_images = 0;
357 if (dds_info.ddscaps2 & DDSCAPS2_CUBEMAP_POSITIVEX) num_images++;
358 if (dds_info.ddscaps2 & DDSCAPS2_CUBEMAP_NEGATIVEX) num_images++;
359 if (dds_info.ddscaps2 & DDSCAPS2_CUBEMAP_POSITIVEY) num_images++;
360 if (dds_info.ddscaps2 & DDSCAPS2_CUBEMAP_NEGATIVEY) num_images++;
361 if (dds_info.ddscaps2 & DDSCAPS2_CUBEMAP_POSITIVEZ) num_images++;
362 if (dds_info.ddscaps2 & DDSCAPS2_CUBEMAP_NEGATIVEZ) num_images++;
363 }
364
365 if (volume)
366 num_images = dds_info.depth;
367
368 for (n = 0; n < num_images; n++)
369 {
370 if (n != 0)
371 {
372 /* Start a new image */
cristy9950d572011-10-01 18:22:35 +0000373 AcquireNextImage(image_info,image,exception);
cristy3ed852e2009-09-05 21:47:34 +0000374 if (GetNextImageInList(image) == (Image *) NULL)
375 {
376 image = DestroyImageList(image);
377 return((Image *) NULL);
378 }
379 image=SyncNextImageInList(image);
380 }
381
382 image->matte = matte;
383 image->compression = compression;
384 image->columns = dds_info.width;
385 image->rows = dds_info.height;
386 image->storage_class = DirectClass;
387 image->endian = LSBEndian;
388 image->depth = 8;
389 if (image_info->ping != MagickFalse)
390 {
391 (void) CloseBlob(image);
392 return(GetFirstImageInList(image));
393 }
394
cristyc82a27b2011-10-21 01:07:16 +0000395 if ((decoder)(image, &dds_info, exception) != MagickTrue)
cristy3ed852e2009-09-05 21:47:34 +0000396 {
397 (void) CloseBlob(image);
398 return(GetFirstImageInList(image));
399 }
400 }
401
402 if (EOFBlob(image) != MagickFalse)
403 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
404 image->filename);
405
406 (void) CloseBlob(image);
407 return(GetFirstImageInList(image));
408}
409
410static MagickBooleanType ReadDDSInfo(Image *image, DDSInfo *dds_info)
411{
cristybb503372010-05-27 20:51:26 +0000412 size_t
cristy3ed852e2009-09-05 21:47:34 +0000413 hdr_size,
414 required;
cristy3ed852e2009-09-05 21:47:34 +0000415
cristyc5de6992009-10-06 19:19:48 +0000416 /* Seek to start of header */
417 (void) SeekBlob(image, 4, SEEK_SET);
418
419 /* Check header field */
420 hdr_size = ReadBlobLSBLong(image);
421 if (hdr_size != 124)
422 return MagickFalse;
423
424 /* Fill in DDS info struct */
425 dds_info->flags = ReadBlobLSBLong(image);
426
427 /* Check required flags */
cristybb503372010-05-27 20:51:26 +0000428 required=(size_t) (DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT);
cristy3ed852e2009-09-05 21:47:34 +0000429 if ((dds_info->flags & required) != required)
cristyc5de6992009-10-06 19:19:48 +0000430 return MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +0000431
cristyc5de6992009-10-06 19:19:48 +0000432 dds_info->height = ReadBlobLSBLong(image);
433 dds_info->width = ReadBlobLSBLong(image);
434 dds_info->pitchOrLinearSize = ReadBlobLSBLong(image);
435 dds_info->depth = ReadBlobLSBLong(image);
436 dds_info->mipmapcount = ReadBlobLSBLong(image);
cristy3ed852e2009-09-05 21:47:34 +0000437
cristyc5de6992009-10-06 19:19:48 +0000438 (void) SeekBlob(image, 44, SEEK_CUR); /* reserved region of 11 DWORDs */
cristy3ed852e2009-09-05 21:47:34 +0000439
440 /* Read pixel format structure */
441 hdr_size = ReadBlobLSBLong(image);
442 if (hdr_size != 32)
443 return MagickFalse;
444
445 dds_info->pixelformat.flags = ReadBlobLSBLong(image);
446 dds_info->pixelformat.fourcc = ReadBlobLSBLong(image);
447 dds_info->pixelformat.rgb_bitcount = ReadBlobLSBLong(image);
448 dds_info->pixelformat.r_bitmask = ReadBlobLSBLong(image);
449 dds_info->pixelformat.g_bitmask = ReadBlobLSBLong(image);
450 dds_info->pixelformat.b_bitmask = ReadBlobLSBLong(image);
451 dds_info->pixelformat.alpha_bitmask = ReadBlobLSBLong(image);
452
cristyc5de6992009-10-06 19:19:48 +0000453 dds_info->ddscaps1 = ReadBlobLSBLong(image);
454 dds_info->ddscaps2 = ReadBlobLSBLong(image);
455 (void) SeekBlob(image, 12, SEEK_CUR); /* 3 reserved DWORDs */
cristy3ed852e2009-09-05 21:47:34 +0000456
cristyc5de6992009-10-06 19:19:48 +0000457 return MagickTrue;
cristy3ed852e2009-09-05 21:47:34 +0000458}
459
460static void CalculateColors(unsigned short c0, unsigned short c1,
461 DDSColors *c, MagickBooleanType ignoreAlpha)
462{
463 c->a[0] = c->a[1] = c->a[2] = c->a[3] = 0;
464
465 c->r[0] = (unsigned char) C565_red(c0);
466 c->g[0] = (unsigned char) C565_green(c0);
467 c->b[0] = (unsigned char) C565_blue(c0);
468
469 c->r[1] = (unsigned char) C565_red(c1);
470 c->g[1] = (unsigned char) C565_green(c1);
471 c->b[1] = (unsigned char) C565_blue(c1);
472
473 if (ignoreAlpha == MagickTrue || c0 > c1)
474 {
475 c->r[2] = (unsigned char) ((2 * c->r[0] + c->r[1]) / 3);
476 c->g[2] = (unsigned char) ((2 * c->g[0] + c->g[1]) / 3);
477 c->b[2] = (unsigned char) ((2 * c->b[0] + c->b[1]) / 3);
478
479 c->r[3] = (unsigned char) ((c->r[0] + 2 * c->r[1]) / 3);
480 c->g[3] = (unsigned char) ((c->g[0] + 2 * c->g[1]) / 3);
481 c->b[3] = (unsigned char) ((c->b[0] + 2 * c->b[1]) / 3);
482 }
483 else
484 {
485 c->r[2] = (unsigned char) ((c->r[0] + c->r[1]) / 2);
486 c->g[2] = (unsigned char) ((c->g[0] + c->g[1]) / 2);
487 c->b[2] = (unsigned char) ((c->b[0] + c->b[1]) / 2);
488
489 c->r[3] = c->g[3] = c->b[3] = 0;
490 c->a[3] = 255;
491 }
492}
493
cristyc82a27b2011-10-21 01:07:16 +0000494static MagickBooleanType ReadDXT1(Image *image, DDSInfo *dds_info,
495 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000496{
497 DDSColors
498 colors;
499
cristy4c08aed2011-07-01 19:47:50 +0000500 register Quantum
cristy3ed852e2009-09-05 21:47:34 +0000501 *q;
502
cristybb503372010-05-27 20:51:26 +0000503 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000504 i,
505 x;
506
cristyf9a6c462011-04-24 14:01:11 +0000507 size_t
508 bits;
509
510 ssize_t
511 j,
512 y;
513
cristy3ed852e2009-09-05 21:47:34 +0000514 unsigned char
515 code;
516
cristyc5de6992009-10-06 19:19:48 +0000517 unsigned short
cristy3ed852e2009-09-05 21:47:34 +0000518 c0,
519 c1;
cristyc5de6992009-10-06 19:19:48 +0000520
cristybb503372010-05-27 20:51:26 +0000521 for (y = 0; y < (ssize_t) dds_info->height; y += 4)
cristy3ed852e2009-09-05 21:47:34 +0000522 {
cristybb503372010-05-27 20:51:26 +0000523 for (x = 0; x < (ssize_t) dds_info->width; x += 4)
cristy3ed852e2009-09-05 21:47:34 +0000524 {
525 /* Get 4x4 patch of pixels to write on */
526 q = QueueAuthenticPixels(image, x, y, Min(4, dds_info->width - x),
cristyf9a6c462011-04-24 14:01:11 +0000527 Min(4, dds_info->height - y),exception);
cristy3ed852e2009-09-05 21:47:34 +0000528
cristyacd2ed22011-08-30 01:44:23 +0000529 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000530 return MagickFalse;
531
532 /* Read 8 bytes of data from the image */
533 c0 = ReadBlobLSBShort(image);
534 c1 = ReadBlobLSBShort(image);
535 bits = ReadBlobLSBLong(image);
536
537 CalculateColors(c0, c1, &colors, MagickFalse);
538
539 /* Write the pixels */
540 for (j = 0; j < 4; j++)
541 {
542 for (i = 0; i < 4; i++)
543 {
cristy4c08aed2011-07-01 19:47:50 +0000544 if ((x + i) < (ssize_t) dds_info->width &&
545 (y + j) < (ssize_t) dds_info->height)
cristy3ed852e2009-09-05 21:47:34 +0000546 {
547 code = (unsigned char) ((bits >> ((j*4+i)*2)) & 0x3);
cristy4c08aed2011-07-01 19:47:50 +0000548 SetPixelRed(image,ScaleCharToQuantum(colors.r[code]),q);
549 SetPixelGreen(image,ScaleCharToQuantum(colors.g[code]),q);
550 SetPixelBlue(image,ScaleCharToQuantum(colors.b[code]),q);
551 SetPixelAlpha(image,ScaleCharToQuantum(colors.a[code]),q);
552 if (colors.a[code] && (image->matte == MagickFalse))
553 image->matte=MagickTrue; /* Correct matte */
cristyed231572011-07-14 02:18:59 +0000554 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000555 }
556 }
557 }
558
559 if (SyncAuthenticPixels(image,exception) == MagickFalse)
560 return MagickFalse;
561 }
562 }
563
564 SkipDXTMipmaps(image, dds_info, 8);
565
566 return MagickTrue;
567}
568
cristyc82a27b2011-10-21 01:07:16 +0000569static MagickBooleanType ReadDXT3(Image *image, DDSInfo *dds_info,
570 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000571{
572 DDSColors
573 colors;
574
cristy4c08aed2011-07-01 19:47:50 +0000575 register Quantum
cristy3ed852e2009-09-05 21:47:34 +0000576 *q;
577
cristybb503372010-05-27 20:51:26 +0000578 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000579 i,
580 x;
581
582 unsigned char
583 alpha;
584
cristybb503372010-05-27 20:51:26 +0000585 size_t
cristy3ed852e2009-09-05 21:47:34 +0000586 a0,
587 a1,
588 bits,
589 code;
590
cristy4c08aed2011-07-01 19:47:50 +0000591 ssize_t
592 j,
593 y;
594
cristy3ed852e2009-09-05 21:47:34 +0000595 unsigned short
596 c0,
597 c1;
598
cristybb503372010-05-27 20:51:26 +0000599 for (y = 0; y < (ssize_t) dds_info->height; y += 4)
cristy3ed852e2009-09-05 21:47:34 +0000600 {
cristybb503372010-05-27 20:51:26 +0000601 for (x = 0; x < (ssize_t) dds_info->width; x += 4)
cristy3ed852e2009-09-05 21:47:34 +0000602 {
603 /* Get 4x4 patch of pixels to write on */
604 q = QueueAuthenticPixels(image, x, y, Min(4, dds_info->width - x),
605 Min(4, dds_info->height - y),exception);
606
cristy4c08aed2011-07-01 19:47:50 +0000607 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000608 return MagickFalse;
609
610 /* Read alpha values (8 bytes) */
611 a0 = ReadBlobLSBLong(image);
612 a1 = ReadBlobLSBLong(image);
613
614 /* Read 8 bytes of data from the image */
615 c0 = ReadBlobLSBShort(image);
616 c1 = ReadBlobLSBShort(image);
617 bits = ReadBlobLSBLong(image);
618
619 CalculateColors(c0, c1, &colors, MagickTrue);
620
621 /* Write the pixels */
622 for (j = 0; j < 4; j++)
623 {
624 for (i = 0; i < 4; i++)
625 {
cristybb503372010-05-27 20:51:26 +0000626 if ((x + i) < (ssize_t) dds_info->width && (y + j) < (ssize_t) dds_info->height)
cristy3ed852e2009-09-05 21:47:34 +0000627 {
628 code = (bits >> ((4*j+i)*2)) & 0x3;
cristy4c08aed2011-07-01 19:47:50 +0000629 SetPixelRed(image,ScaleCharToQuantum(colors.r[code]),q);
630 SetPixelGreen(image,ScaleCharToQuantum(colors.g[code]),q);
631 SetPixelBlue(image,ScaleCharToQuantum(colors.b[code]),q);
cristy3ed852e2009-09-05 21:47:34 +0000632 /*
633 Extract alpha value: multiply 0..15 by 17 to get range 0..255
634 */
635 if (j < 2)
636 alpha = 17U * (unsigned char) ((a0 >> (4*(4*j+i))) & 0xf);
637 else
638 alpha = 17U * (unsigned char) ((a1 >> (4*(4*(j-2)+i))) & 0xf);
cristy4c08aed2011-07-01 19:47:50 +0000639 SetPixelAlpha(image,ScaleCharToQuantum((unsigned char) alpha),q);
cristyed231572011-07-14 02:18:59 +0000640 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000641 }
642 }
643 }
644
645 if (SyncAuthenticPixels(image,exception) == MagickFalse)
646 return MagickFalse;
647 }
648 }
649
650 SkipDXTMipmaps(image, dds_info, 16);
651
652 return MagickTrue;
653}
654
cristyc82a27b2011-10-21 01:07:16 +0000655static MagickBooleanType ReadDXT5(Image *image, DDSInfo *dds_info,
656 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000657{
658 DDSColors
659 colors;
660
cristy3ed852e2009-09-05 21:47:34 +0000661 MagickSizeType
662 alpha_bits;
663
cristy4c08aed2011-07-01 19:47:50 +0000664 register Quantum
cristy3ed852e2009-09-05 21:47:34 +0000665 *q;
666
cristybb503372010-05-27 20:51:26 +0000667 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000668 i,
669 x;
670
671 unsigned char
672 a0,
673 a1;
674
cristybb503372010-05-27 20:51:26 +0000675 size_t
cristy3ed852e2009-09-05 21:47:34 +0000676 alpha,
677 bits,
678 code,
679 alpha_code;
680
cristy4c08aed2011-07-01 19:47:50 +0000681 ssize_t
682 j,
683 y;
684
cristy3ed852e2009-09-05 21:47:34 +0000685 unsigned short
686 c0,
687 c1;
688
cristybb503372010-05-27 20:51:26 +0000689 for (y = 0; y < (ssize_t) dds_info->height; y += 4)
cristy3ed852e2009-09-05 21:47:34 +0000690 {
cristybb503372010-05-27 20:51:26 +0000691 for (x = 0; x < (ssize_t) dds_info->width; x += 4)
cristy3ed852e2009-09-05 21:47:34 +0000692 {
693 /* Get 4x4 patch of pixels to write on */
694 q = QueueAuthenticPixels(image, x, y, Min(4, dds_info->width - x),
695 Min(4, dds_info->height - y),exception);
696
cristyacd2ed22011-08-30 01:44:23 +0000697 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000698 return MagickFalse;
699
700 /* Read alpha values (8 bytes) */
701 a0 = (unsigned char) ReadBlobByte(image);
702 a1 = (unsigned char) ReadBlobByte(image);
703
704 alpha_bits = (MagickSizeType)ReadBlobLSBLong(image)
705 | ((MagickSizeType)ReadBlobLSBShort(image) << 32);
706
707 /* Read 8 bytes of data from the image */
708 c0 = ReadBlobLSBShort(image);
709 c1 = ReadBlobLSBShort(image);
710 bits = ReadBlobLSBLong(image);
711
712 CalculateColors(c0, c1, &colors, MagickTrue);
713
714 /* Write the pixels */
715 for (j = 0; j < 4; j++)
716 {
717 for (i = 0; i < 4; i++)
718 {
cristy4c08aed2011-07-01 19:47:50 +0000719 if ((x + i) < (ssize_t) dds_info->width &&
720 (y + j) < (ssize_t) dds_info->height)
cristy3ed852e2009-09-05 21:47:34 +0000721 {
722 code = (bits >> ((4*j+i)*2)) & 0x3;
cristy4c08aed2011-07-01 19:47:50 +0000723 SetPixelRed(image,ScaleCharToQuantum(colors.r[code]),q);
724 SetPixelGreen(image,ScaleCharToQuantum(colors.g[code]),q);
725 SetPixelBlue(image,ScaleCharToQuantum(colors.b[code]),q);
cristy3ed852e2009-09-05 21:47:34 +0000726 /* Extract alpha value */
cristybb503372010-05-27 20:51:26 +0000727 alpha_code = (size_t) (alpha_bits >> (3*(4*j+i))) & 0x7;
cristy3ed852e2009-09-05 21:47:34 +0000728 if (alpha_code == 0)
729 alpha = a0;
730 else if (alpha_code == 1)
731 alpha = a1;
732 else if (a0 > a1)
733 alpha = ((8-alpha_code) * a0 + (alpha_code-1) * a1) / 7;
734 else if (alpha_code == 6)
735 alpha = alpha_code;
736 else if (alpha_code == 7)
737 alpha = 255;
738 else
739 alpha = (((6-alpha_code) * a0 + (alpha_code-1) * a1) / 5);
cristy4c08aed2011-07-01 19:47:50 +0000740 SetPixelAlpha(image,ScaleCharToQuantum((unsigned char) alpha),q);
cristyed231572011-07-14 02:18:59 +0000741 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000742 }
743 }
744 }
745
746 if (SyncAuthenticPixels(image,exception) == MagickFalse)
747 return MagickFalse;
748 }
749 }
750
751 SkipDXTMipmaps(image, dds_info, 16);
752
753 return MagickTrue;
754}
755
cristyc82a27b2011-10-21 01:07:16 +0000756static MagickBooleanType ReadUncompressedRGB(Image *image, DDSInfo *dds_info,
757 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000758{
cristybb503372010-05-27 20:51:26 +0000759 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000760 x, y;
761
cristy4c08aed2011-07-01 19:47:50 +0000762 register Quantum
cristy3ed852e2009-09-05 21:47:34 +0000763 *q;
764
cristybb503372010-05-27 20:51:26 +0000765 for (y = 0; y < (ssize_t) dds_info->height; y++)
cristy3ed852e2009-09-05 21:47:34 +0000766 {
767 q = QueueAuthenticPixels(image, 0, y, dds_info->width, 1,exception);
768
cristy4c08aed2011-07-01 19:47:50 +0000769 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000770 return MagickFalse;
771
cristybb503372010-05-27 20:51:26 +0000772 for (x = 0; x < (ssize_t) dds_info->width; x++)
cristy3ed852e2009-09-05 21:47:34 +0000773 {
cristy4c08aed2011-07-01 19:47:50 +0000774 SetPixelBlue(image,ScaleCharToQuantum((unsigned char)
775 ReadBlobByte(image)),q);
776 SetPixelGreen(image,ScaleCharToQuantum((unsigned char)
777 ReadBlobByte(image)),q);
778 SetPixelRed(image,ScaleCharToQuantum((unsigned char)
779 ReadBlobByte(image)),q);
cristy6305d192010-06-23 01:07:24 +0000780 if (dds_info->pixelformat.rgb_bitcount == 32)
781 (void) ReadBlobByte(image);
cristyed231572011-07-14 02:18:59 +0000782 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000783 }
784
785 if (SyncAuthenticPixels(image,exception) == MagickFalse)
786 return MagickFalse;
787 }
788
789 SkipRGBMipmaps(image, dds_info, 3);
790
791 return MagickTrue;
792}
793
cristyc82a27b2011-10-21 01:07:16 +0000794static MagickBooleanType ReadUncompressedRGBA(Image *image, DDSInfo *dds_info,
795 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000796{
cristybb503372010-05-27 20:51:26 +0000797 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000798 x, y;
799
cristy4c08aed2011-07-01 19:47:50 +0000800 register Quantum
cristy3ed852e2009-09-05 21:47:34 +0000801 *q;
802
cristybb503372010-05-27 20:51:26 +0000803 for (y = 0; y < (ssize_t) dds_info->height; y++)
cristy3ed852e2009-09-05 21:47:34 +0000804 {
805 q = QueueAuthenticPixels(image, 0, y, dds_info->width, 1,exception);
806
cristy4c08aed2011-07-01 19:47:50 +0000807 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000808 return MagickFalse;
809
cristybb503372010-05-27 20:51:26 +0000810 for (x = 0; x < (ssize_t) dds_info->width; x++)
cristy3ed852e2009-09-05 21:47:34 +0000811 {
cristy4c08aed2011-07-01 19:47:50 +0000812 SetPixelBlue(image,ScaleCharToQuantum((unsigned char)
813 ReadBlobByte(image)),q);
814 SetPixelGreen(image,ScaleCharToQuantum((unsigned char)
815 ReadBlobByte(image)),q);
816 SetPixelRed(image,ScaleCharToQuantum((unsigned char)
817 ReadBlobByte(image)),q);
818 SetPixelAlpha(image,ScaleCharToQuantum((unsigned char)
819 ReadBlobByte(image)),q);
cristyed231572011-07-14 02:18:59 +0000820 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000821 }
822
823 if (SyncAuthenticPixels(image,exception) == MagickFalse)
824 return MagickFalse;
825 }
826
827 SkipRGBMipmaps(image, dds_info, 4);
828
829 return MagickTrue;
830}
831
832/*
833 Skip the mipmap images for compressed (DXTn) dds files
834*/
835static void SkipDXTMipmaps(Image *image, DDSInfo *dds_info, int texel_size)
836{
cristy3ed852e2009-09-05 21:47:34 +0000837 MagickOffsetType
838 offset;
839
cristy4c08aed2011-07-01 19:47:50 +0000840 register ssize_t
841 i;
842
cristybb503372010-05-27 20:51:26 +0000843 size_t
cristy3ed852e2009-09-05 21:47:34 +0000844 h,
845 w;
846
847 /*
848 Only skip mipmaps for textures and cube maps
849 */
850 if (dds_info->ddscaps1 & DDSCAPS_MIPMAP
851 && (dds_info->ddscaps1 & DDSCAPS_TEXTURE
852 || dds_info->ddscaps2 & DDSCAPS2_CUBEMAP))
853 {
854 w = DIV2(dds_info->width);
855 h = DIV2(dds_info->height);
856
857 /*
858 Mipmapcount includes the main image, so start from one
859 */
cristybb503372010-05-27 20:51:26 +0000860 for (i = 1; (i < (ssize_t) dds_info->mipmapcount) && w && h; i++)
cristy3ed852e2009-09-05 21:47:34 +0000861 {
862 offset = (MagickOffsetType) ((w + 3) / 4) * ((h + 3) / 4) * texel_size;
863 (void) SeekBlob(image, offset, SEEK_CUR);
864
865 w = DIV2(w);
866 h = DIV2(h);
867 }
868 }
869}
870
871/*
872 Skip the mipmap images for uncompressed (RGB or RGBA) dds files
873*/
874static void SkipRGBMipmaps(Image *image, DDSInfo *dds_info, int pixel_size)
875{
876 MagickOffsetType
877 offset;
878
cristybb503372010-05-27 20:51:26 +0000879 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000880 i;
881
cristybb503372010-05-27 20:51:26 +0000882 size_t
cristy3ed852e2009-09-05 21:47:34 +0000883 h,
884 w;
885
886 /*
887 Only skip mipmaps for textures and cube maps
888 */
889 if (dds_info->ddscaps1 & DDSCAPS_MIPMAP
890 && (dds_info->ddscaps1 & DDSCAPS_TEXTURE
891 || dds_info->ddscaps2 & DDSCAPS2_CUBEMAP))
892 {
893 w = DIV2(dds_info->width);
894 h = DIV2(dds_info->height);
895
896 /*
897 Mipmapcount includes the main image, so start from one
898 */
cristybb503372010-05-27 20:51:26 +0000899 for (i=1; (i < (ssize_t) dds_info->mipmapcount) && w && h; i++)
cristy3ed852e2009-09-05 21:47:34 +0000900 {
901 offset = (MagickOffsetType) w * h * pixel_size;
902 (void) SeekBlob(image, offset, SEEK_CUR);
903
904 w = DIV2(w);
905 h = DIV2(h);
906 }
907 }
908}
909
910/*
911%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
912% %
913% %
914% %
915% I s D D S %
916% %
917% %
918% %
919%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
920%
921% IsDDS() returns MagickTrue if the image format type, identified by the
922% magick string, is DDS.
923%
924% The format of the IsDDS method is:
925%
926% MagickBooleanType IsDDS(const unsigned char *magick,const size_t length)
927%
928% A description of each parameter follows:
929%
930% o magick: compare image format pattern against these bytes.
931%
932% o length: Specifies the length of the magick string.
933%
934*/
935static MagickBooleanType IsDDS(const unsigned char *magick,const size_t length)
936{
937 if (length < 4)
938 return(MagickFalse);
939 if (LocaleNCompare((char *) magick,"DDS ", 4) == 0)
940 return(MagickTrue);
941 return(MagickFalse);
942}
943
944/*
945%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
946% %
947% %
948% %
949% R e g i s t e r D D S I m a g e %
950% %
951% %
952% %
953%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
954%
955% RegisterDDSImage() adds attributes for the DDS image format to
956% the list of supported formats. The attributes include the image format
957% tag, a method to read and/or write the format, whether the format
958% supports the saving of more than one frame to the same file or blob,
959% whether the format supports native in-memory I/O, and a brief
960% description of the format.
961%
962% The format of the RegisterDDSImage method is:
963%
964% RegisterDDSImage(void)
965%
966*/
cristybb503372010-05-27 20:51:26 +0000967ModuleExport size_t RegisterDDSImage(void)
cristy3ed852e2009-09-05 21:47:34 +0000968{
969 MagickInfo
970 *entry;
971
972 entry = SetMagickInfo("DDS");
973 entry->decoder = (DecodeImageHandler *) ReadDDSImage;
974 entry->magick = (IsImageFormatHandler *) IsDDS;
cristyffaf9782011-04-13 19:50:51 +0000975 entry->seekable_stream=MagickTrue;
cristy3ed852e2009-09-05 21:47:34 +0000976 entry->description = ConstantString("Microsoft DirectDraw Surface");
977 entry->module = ConstantString("DDS");
978 (void) RegisterMagickInfo(entry);
979 return(MagickImageCoderSignature);
980}
981
982
983/*
984%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
985% %
986% %
987% %
988% U n r e g i s t e r D D S I m a g e %
989% %
990% %
991% %
992%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
993%
994% UnregisterDDSImage() removes format registrations made by the
995% DDS module from the list of supported formats.
996%
997% The format of the UnregisterDDSImage method is:
998%
999% UnregisterDDSImage(void)
1000%
1001*/
1002ModuleExport void UnregisterDDSImage(void)
1003{
1004 (void) UnregisterMagickInfo("DDS");
1005}
1006