]> granicus.if.org Git - imagemagick/blob - coders/dds.c
(no commit message)
[imagemagick] / coders / dds.c
1 /*
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 %                                                                             %
20 %  Copyright 1999-2011 ImageMagick Studio LLC, a non-profit organization      %
21 %  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 \f
39 /*
40   Include declarations.
41 */
42 #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"
81 \f
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 */
118 typedef struct _DDSPixelFormat
119 {
120   size_t
121     flags,
122     fourcc,
123     rgb_bitcount,
124     r_bitmask,
125     g_bitmask,
126     b_bitmask,
127     alpha_bitmask;
128 } DDSPixelFormat;
129
130 typedef struct _DDSInfo
131 {
132   size_t
133     flags,
134     height,
135     width,
136     pitchOrLinearSize,
137     depth,
138     mipmapcount,
139     ddscaps1,
140     ddscaps2;
141   
142   DDSPixelFormat
143     pixelformat;
144 } DDSInfo;
145
146 typedef struct _DDSColors
147 {
148   unsigned char
149     r[4],
150     g[4],
151     b[4],
152     a[4];
153 } DDSColors;
154
155 typedef MagickBooleanType
156   DDSDecoder(Image *,DDSInfo *);
157
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 */
174 static MagickBooleanType
175   ReadDDSInfo(Image *image, DDSInfo *dds_info);
176
177 static void
178   CalculateColors(unsigned short c0, unsigned short c1,
179     DDSColors *c, MagickBooleanType ignoreAlpha);
180
181 static MagickBooleanType
182   ReadDXT1(Image *image, DDSInfo *dds_info);
183
184 static MagickBooleanType
185   ReadDXT3(Image *image, DDSInfo *dds_info);
186
187 static MagickBooleanType
188   ReadDXT5(Image *image, DDSInfo *dds_info);
189
190 static MagickBooleanType
191   ReadUncompressedRGB(Image *image, DDSInfo *dds_info);
192
193 static MagickBooleanType
194   ReadUncompressedRGBA(Image *image, DDSInfo *dds_info);
195
196 static void
197   SkipDXTMipmaps(Image *image, DDSInfo *dds_info, int texel_size);
198
199 static void
200   SkipRGBMipmaps(Image *image, DDSInfo *dds_info, int pixel_size);
201 \f
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
229 static inline size_t Min(size_t one, size_t two)
230 {
231   if (one < two)
232     return one;
233   return two;
234 }
235
236 static 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
251     dds_info;
252   
253   DDSDecoder
254     *decoder;
255   
256   size_t
257     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);
269   image=AcquireImage(image_info);
270   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) {
281     ThrowReaderException(CorruptImageError,"ImproperImageHeader");
282   }
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 */
373         AcquireNextImage(image_info,image);
374         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     
395     if ((decoder)(image, &dds_info) != MagickTrue)
396       {
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
410 static MagickBooleanType ReadDDSInfo(Image *image, DDSInfo *dds_info)
411 {
412   size_t
413     hdr_size,
414     required;
415   
416   /* 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 */
428   required=(size_t) (DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT);
429   if ((dds_info->flags & required) != required)
430     return MagickFalse;
431   
432   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);
437   
438   (void) SeekBlob(image, 44, SEEK_CUR);   /* reserved region of 11 DWORDs */
439   
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   
453   dds_info->ddscaps1 = ReadBlobLSBLong(image);
454   dds_info->ddscaps2 = ReadBlobLSBLong(image);
455   (void) SeekBlob(image, 12, SEEK_CUR); /* 3 reserved DWORDs */
456   
457   return MagickTrue;
458 }
459
460 static 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
494 static MagickBooleanType ReadDXT1(Image *image, DDSInfo *dds_info)
495 {
496   DDSColors
497     colors;
498
499   ExceptionInfo
500     *exception;
501
502   register Quantum
503     *q;
504   
505   register ssize_t
506     i,
507     x;
508   
509   size_t
510     bits;
511
512   ssize_t
513     j,
514     y;
515   
516   unsigned char
517     code;
518   
519   unsigned short
520     c0,
521     c1;
522   
523   exception=(&image->exception);
524   for (y = 0; y < (ssize_t) dds_info->height; y += 4)
525   {
526     for (x = 0; x < (ssize_t) dds_info->width; x += 4)
527     {
528       /* Get 4x4 patch of pixels to write on */
529       q = QueueAuthenticPixels(image, x, y, Min(4, dds_info->width - x),
530         Min(4, dds_info->height - y),exception);
531       
532       if (q == (const Quantum *) NULL)
533         return MagickFalse;
534       
535       /* Read 8 bytes of data from the image */
536       c0 = ReadBlobLSBShort(image);
537       c1 = ReadBlobLSBShort(image);
538       bits = ReadBlobLSBLong(image);
539       
540       CalculateColors(c0, c1, &colors, MagickFalse);
541       
542       /* Write the pixels */
543       for (j = 0; j < 4; j++)
544       {
545         for (i = 0; i < 4; i++)
546         {
547           if ((x + i) < (ssize_t) dds_info->width &&
548               (y + j) < (ssize_t) dds_info->height)
549             {
550               code = (unsigned char) ((bits >> ((j*4+i)*2)) & 0x3);
551               SetPixelRed(image,ScaleCharToQuantum(colors.r[code]),q);
552               SetPixelGreen(image,ScaleCharToQuantum(colors.g[code]),q);
553               SetPixelBlue(image,ScaleCharToQuantum(colors.b[code]),q);
554               SetPixelAlpha(image,ScaleCharToQuantum(colors.a[code]),q);
555               if (colors.a[code] && (image->matte == MagickFalse))
556                 image->matte=MagickTrue;  /* Correct matte */
557               q+=GetPixelChannels(image);
558             }
559         }
560       }
561       
562       if (SyncAuthenticPixels(image,exception) == MagickFalse)
563         return MagickFalse;
564     }
565   }
566   
567   SkipDXTMipmaps(image, dds_info, 8);
568   
569   return MagickTrue;
570 }
571
572 static MagickBooleanType ReadDXT3(Image *image, DDSInfo *dds_info)
573 {
574   DDSColors
575     colors;
576   
577   ExceptionInfo
578     *exception;
579
580   register Quantum
581     *q;
582   
583   register ssize_t
584     i,
585     x;
586   
587   unsigned char
588     alpha;
589   
590   size_t
591     a0,
592     a1,
593     bits,
594     code;
595
596   ssize_t
597     j,
598     y;
599
600   unsigned short
601     c0,
602     c1;
603   
604   exception=(&image->exception);
605   for (y = 0; y < (ssize_t) dds_info->height; y += 4)
606   {
607     for (x = 0; x < (ssize_t) dds_info->width; x += 4)
608     {
609       /* Get 4x4 patch of pixels to write on */
610       q = QueueAuthenticPixels(image, x, y, Min(4, dds_info->width - x),
611                          Min(4, dds_info->height - y),exception);
612       
613       if (q == (Quantum *) NULL)
614         return MagickFalse;
615       
616       /* Read alpha values (8 bytes) */
617       a0 = ReadBlobLSBLong(image);
618       a1 = ReadBlobLSBLong(image);
619       
620       /* Read 8 bytes of data from the image */
621       c0 = ReadBlobLSBShort(image);
622       c1 = ReadBlobLSBShort(image);
623       bits = ReadBlobLSBLong(image);
624       
625       CalculateColors(c0, c1, &colors, MagickTrue);
626       
627       /* Write the pixels */
628       for (j = 0; j < 4; j++)
629       {
630         for (i = 0; i < 4; i++)
631         {
632           if ((x + i) < (ssize_t) dds_info->width && (y + j) < (ssize_t) dds_info->height)
633             {
634               code = (bits >> ((4*j+i)*2)) & 0x3;
635               SetPixelRed(image,ScaleCharToQuantum(colors.r[code]),q);
636               SetPixelGreen(image,ScaleCharToQuantum(colors.g[code]),q);
637               SetPixelBlue(image,ScaleCharToQuantum(colors.b[code]),q);
638               /*
639                 Extract alpha value: multiply 0..15 by 17 to get range 0..255
640               */
641               if (j < 2)
642                 alpha = 17U * (unsigned char) ((a0 >> (4*(4*j+i))) & 0xf);
643               else
644                 alpha = 17U * (unsigned char) ((a1 >> (4*(4*(j-2)+i))) & 0xf);
645               SetPixelAlpha(image,ScaleCharToQuantum((unsigned char) alpha),q);
646               q+=GetPixelChannels(image);
647             }
648         }
649       }
650       
651       if (SyncAuthenticPixels(image,exception) == MagickFalse)
652         return MagickFalse;
653     }
654   }
655   
656   SkipDXTMipmaps(image, dds_info, 16);
657   
658   return MagickTrue;
659 }
660
661 static MagickBooleanType ReadDXT5(Image *image, DDSInfo *dds_info)
662 {
663   DDSColors
664     colors;
665   
666   ExceptionInfo
667     *exception;
668
669   MagickSizeType
670     alpha_bits;
671   
672   register Quantum
673     *q;
674   
675   register ssize_t
676     i,
677     x;
678
679   unsigned char
680     a0,
681     a1;
682   
683   size_t
684     alpha,
685     bits,
686     code,
687     alpha_code;
688
689   ssize_t
690     j,
691     y;
692
693   unsigned short
694     c0,
695     c1;
696   
697   exception=(&image->exception);
698   for (y = 0; y < (ssize_t) dds_info->height; y += 4)
699   {
700     for (x = 0; x < (ssize_t) dds_info->width; x += 4)
701     {
702       /* Get 4x4 patch of pixels to write on */
703       q = QueueAuthenticPixels(image, x, y, Min(4, dds_info->width - x),
704                          Min(4, dds_info->height - y),exception);
705       
706       if (q == (const Quantum *) NULL)
707         return MagickFalse;
708       
709       /* Read alpha values (8 bytes) */
710       a0 = (unsigned char) ReadBlobByte(image);
711       a1 = (unsigned char) ReadBlobByte(image);
712       
713       alpha_bits = (MagickSizeType)ReadBlobLSBLong(image)
714                  | ((MagickSizeType)ReadBlobLSBShort(image) << 32);
715       
716       /* Read 8 bytes of data from the image */
717       c0 = ReadBlobLSBShort(image);
718       c1 = ReadBlobLSBShort(image);
719       bits = ReadBlobLSBLong(image);
720       
721       CalculateColors(c0, c1, &colors, MagickTrue);
722       
723       /* Write the pixels */
724       for (j = 0; j < 4; j++)
725       {
726         for (i = 0; i < 4; i++)
727         {
728           if ((x + i) < (ssize_t) dds_info->width &&
729               (y + j) < (ssize_t) dds_info->height)
730             {
731               code = (bits >> ((4*j+i)*2)) & 0x3;
732               SetPixelRed(image,ScaleCharToQuantum(colors.r[code]),q);
733               SetPixelGreen(image,ScaleCharToQuantum(colors.g[code]),q);
734               SetPixelBlue(image,ScaleCharToQuantum(colors.b[code]),q);
735               /* Extract alpha value */
736               alpha_code = (size_t) (alpha_bits >> (3*(4*j+i))) & 0x7;
737               if (alpha_code == 0)
738                 alpha = a0;
739               else if (alpha_code == 1)
740                 alpha = a1;
741               else if (a0 > a1)
742                 alpha = ((8-alpha_code) * a0 + (alpha_code-1) * a1) / 7;
743               else if (alpha_code == 6)
744                 alpha = alpha_code;
745               else if (alpha_code == 7)
746                 alpha = 255;
747               else
748                 alpha = (((6-alpha_code) * a0 + (alpha_code-1) * a1) / 5);
749               SetPixelAlpha(image,ScaleCharToQuantum((unsigned char) alpha),q);
750               q+=GetPixelChannels(image);
751             }
752         }
753       }
754       
755       if (SyncAuthenticPixels(image,exception) == MagickFalse)
756         return MagickFalse;
757     }
758   }
759   
760   SkipDXTMipmaps(image, dds_info, 16);
761   
762   return MagickTrue;
763 }
764
765 static MagickBooleanType ReadUncompressedRGB(Image *image, DDSInfo *dds_info)
766 {
767   ExceptionInfo
768     *exception;
769
770   ssize_t
771     x, y;
772   
773   register Quantum
774     *q;
775   
776   exception=(&image->exception);
777   for (y = 0; y < (ssize_t) dds_info->height; y++)
778   {
779     q = QueueAuthenticPixels(image, 0, y, dds_info->width, 1,exception);
780     
781     if (q == (Quantum *) NULL)
782       return MagickFalse;
783     
784     for (x = 0; x < (ssize_t) dds_info->width; x++)
785     {
786       SetPixelBlue(image,ScaleCharToQuantum((unsigned char)
787         ReadBlobByte(image)),q);
788       SetPixelGreen(image,ScaleCharToQuantum((unsigned char)
789         ReadBlobByte(image)),q);
790       SetPixelRed(image,ScaleCharToQuantum((unsigned char)
791         ReadBlobByte(image)),q);
792       if (dds_info->pixelformat.rgb_bitcount == 32)
793         (void) ReadBlobByte(image);
794       q+=GetPixelChannels(image);
795     }
796     
797     if (SyncAuthenticPixels(image,exception) == MagickFalse)
798       return MagickFalse;
799   }
800   
801   SkipRGBMipmaps(image, dds_info, 3);
802   
803   return MagickTrue;
804 }
805
806 static MagickBooleanType ReadUncompressedRGBA(Image *image, DDSInfo *dds_info)
807 {
808   ExceptionInfo
809     *exception;
810
811   ssize_t
812     x, y;
813   
814   register Quantum
815     *q;
816   
817   exception=(&image->exception);
818   for (y = 0; y < (ssize_t) dds_info->height; y++)
819   {
820     q = QueueAuthenticPixels(image, 0, y, dds_info->width, 1,exception);
821     
822     if (q == (Quantum *) NULL)
823       return MagickFalse;
824     
825     for (x = 0; x < (ssize_t) dds_info->width; x++)
826     {
827       SetPixelBlue(image,ScaleCharToQuantum((unsigned char)
828         ReadBlobByte(image)),q);
829       SetPixelGreen(image,ScaleCharToQuantum((unsigned char)
830         ReadBlobByte(image)),q);
831       SetPixelRed(image,ScaleCharToQuantum((unsigned char)
832         ReadBlobByte(image)),q);
833       SetPixelAlpha(image,ScaleCharToQuantum((unsigned char)
834         ReadBlobByte(image)),q);
835       q+=GetPixelChannels(image);
836     }
837     
838     if (SyncAuthenticPixels(image,exception) == MagickFalse)
839       return MagickFalse;
840   }
841   
842   SkipRGBMipmaps(image, dds_info, 4);
843   
844   return MagickTrue;
845 }
846
847 /*
848   Skip the mipmap images for compressed (DXTn) dds files
849 */
850 static void SkipDXTMipmaps(Image *image, DDSInfo *dds_info, int texel_size)
851 {
852   MagickOffsetType
853     offset;
854
855   register ssize_t
856     i;
857
858   size_t
859     h,
860     w;
861   
862   /*
863     Only skip mipmaps for textures and cube maps
864   */
865   if (dds_info->ddscaps1 & DDSCAPS_MIPMAP
866       && (dds_info->ddscaps1 & DDSCAPS_TEXTURE
867           || dds_info->ddscaps2 & DDSCAPS2_CUBEMAP))
868     {
869       w = DIV2(dds_info->width);
870       h = DIV2(dds_info->height);
871       
872       /*
873         Mipmapcount includes the main image, so start from one
874       */
875       for (i = 1; (i < (ssize_t) dds_info->mipmapcount) && w && h; i++)
876       {
877         offset = (MagickOffsetType) ((w + 3) / 4) * ((h + 3) / 4) * texel_size;
878         (void) SeekBlob(image, offset, SEEK_CUR);
879         
880         w = DIV2(w);
881         h = DIV2(h);
882       }
883     }
884 }
885
886 /*
887   Skip the mipmap images for uncompressed (RGB or RGBA) dds files
888 */
889 static void SkipRGBMipmaps(Image *image, DDSInfo *dds_info, int pixel_size)
890 {
891   MagickOffsetType
892     offset;
893   
894   register ssize_t
895     i;
896
897   size_t
898     h,
899     w;
900
901   /*
902     Only skip mipmaps for textures and cube maps
903   */
904   if (dds_info->ddscaps1 & DDSCAPS_MIPMAP
905       && (dds_info->ddscaps1 & DDSCAPS_TEXTURE
906           || dds_info->ddscaps2 & DDSCAPS2_CUBEMAP))
907     {
908       w = DIV2(dds_info->width);
909       h = DIV2(dds_info->height);
910       
911       /*
912         Mipmapcount includes the main image, so start from one
913       */
914       for (i=1; (i < (ssize_t) dds_info->mipmapcount) && w && h; i++)
915       {
916         offset = (MagickOffsetType) w * h * pixel_size;
917         (void) SeekBlob(image, offset, SEEK_CUR);
918         
919         w = DIV2(w);
920         h = DIV2(h);
921       }
922     }
923 }
924 \f
925 /*
926 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
927 %                                                                             %
928 %                                                                             %
929 %                                                                             %
930 %   I s D D S                                                                 %
931 %                                                                             %
932 %                                                                             %
933 %                                                                             %
934 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
935 %
936 %  IsDDS() returns MagickTrue if the image format type, identified by the
937 %  magick string, is DDS.
938 %
939 %  The format of the IsDDS method is:
940 %
941 %      MagickBooleanType IsDDS(const unsigned char *magick,const size_t length)
942 %
943 %  A description of each parameter follows:
944 %
945 %    o magick: compare image format pattern against these bytes.
946 %
947 %    o length: Specifies the length of the magick string.
948 %
949 */
950 static MagickBooleanType IsDDS(const unsigned char *magick,const size_t length)
951 {
952   if (length < 4)
953     return(MagickFalse);
954   if (LocaleNCompare((char *) magick,"DDS ", 4) == 0)
955     return(MagickTrue);
956   return(MagickFalse);
957 }
958 \f
959 /*
960 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
961 %                                                                             %
962 %                                                                             %
963 %                                                                             %
964 %   R e g i s t e r D D S I m a g e                                           %
965 %                                                                             %
966 %                                                                             %
967 %                                                                             %
968 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
969 %
970 %  RegisterDDSImage() adds attributes for the DDS image format to
971 %  the list of supported formats.  The attributes include the image format
972 %  tag, a method to read and/or write the format, whether the format
973 %  supports the saving of more than one frame to the same file or blob,
974 %  whether the format supports native in-memory I/O, and a brief
975 %  description of the format.
976 %
977 %  The format of the RegisterDDSImage method is:
978 %
979 %      RegisterDDSImage(void)
980 %
981 */
982 ModuleExport size_t RegisterDDSImage(void)
983 {
984   MagickInfo
985     *entry;
986
987   entry = SetMagickInfo("DDS");
988   entry->decoder = (DecodeImageHandler *) ReadDDSImage;
989   entry->magick = (IsImageFormatHandler *) IsDDS;
990   entry->seekable_stream=MagickTrue;
991   entry->description = ConstantString("Microsoft DirectDraw Surface");
992   entry->module = ConstantString("DDS");
993   (void) RegisterMagickInfo(entry);
994   return(MagickImageCoderSignature);
995 }
996 \f
997
998 /*
999 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1000 %                                                                             %
1001 %                                                                             %
1002 %                                                                             %
1003 %   U n r e g i s t e r D D S I m a g e                                       %
1004 %                                                                             %
1005 %                                                                             %
1006 %                                                                             %
1007 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1008 %
1009 %  UnregisterDDSImage() removes format registrations made by the
1010 %  DDS module from the list of supported formats.
1011 %
1012 %  The format of the UnregisterDDSImage method is:
1013 %
1014 %      UnregisterDDSImage(void)
1015 %
1016 */
1017 ModuleExport void UnregisterDDSImage(void)
1018 {
1019   (void) UnregisterMagickInfo("DDS");
1020 }
1021