]> 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-2013 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 *,ExceptionInfo *);
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 *, DDSInfo *,ExceptionInfo *);
183
184 static MagickBooleanType
185   ReadDXT3(Image *image, DDSInfo *dds_info,ExceptionInfo *);
186
187 static MagickBooleanType
188   ReadDXT5(Image *image, DDSInfo *dds_info,ExceptionInfo *);
189
190 static MagickBooleanType
191   ReadUncompressedRGB(Image *image, DDSInfo *dds_info,ExceptionInfo *);
192
193 static MagickBooleanType
194   ReadUncompressedRGBA(Image *image, DDSInfo *dds_info,ExceptionInfo *);
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
246   CompressionType
247     compression;
248
249   DDSInfo
250     dds_info;
251   
252   DDSDecoder
253     *decoder;
254   
255   PixelTrait
256     alpha_trait;
257   
258   size_t
259     n, num_images;
260   
261   /*
262     Open image file.
263   */
264   assert(image_info != (const ImageInfo *) NULL);
265   assert(image_info->signature == MagickSignature);
266   if (image_info->debug != MagickFalse)
267     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
268       image_info->filename);
269   assert(exception != (ExceptionInfo *) NULL);
270   assert(exception->signature == MagickSignature);
271   image=AcquireImage(image_info,exception);
272   status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
273   if (status == MagickFalse)
274     {
275       image=DestroyImageList(image);
276       return((Image *) NULL);
277     }
278   
279   /*
280     Initialize image structure.
281   */
282   if (ReadDDSInfo(image, &dds_info) != MagickTrue) {
283     ThrowReaderException(CorruptImageError,"ImproperImageHeader");
284   }
285   
286   if (dds_info.ddscaps2 & DDSCAPS2_CUBEMAP)
287     cubemap = MagickTrue;
288   
289   if (dds_info.ddscaps2 & DDSCAPS2_VOLUME && dds_info.depth > 0)
290     volume = MagickTrue;
291   
292   (void) SeekBlob(image, 128, SEEK_SET);
293
294   /*
295     Determine pixel format
296   */
297   if (dds_info.pixelformat.flags & DDPF_RGB)
298     {
299       compression = NoCompression;
300       if (dds_info.pixelformat.flags & DDPF_ALPHAPIXELS)
301         {
302           alpha_trait = BlendPixelTrait;
303           decoder = ReadUncompressedRGBA;
304         }
305       else
306         {
307           alpha_trait = UndefinedPixelTrait;
308           decoder = ReadUncompressedRGB;
309         }
310     }
311   else if (dds_info.pixelformat.flags & DDPF_FOURCC)
312     {
313       switch (dds_info.pixelformat.fourcc)
314       {
315         case FOURCC_DXT1:
316         {
317           alpha_trait = UndefinedPixelTrait;
318           compression = DXT1Compression;
319           decoder = ReadDXT1;
320           break;
321         }
322         
323         case FOURCC_DXT3:
324         {
325           alpha_trait = BlendPixelTrait;
326           compression = DXT3Compression;
327           decoder = ReadDXT3;
328           break;
329         }
330         
331         case FOURCC_DXT5:
332         {
333           alpha_trait = BlendPixelTrait;
334           compression = DXT5Compression;
335           decoder = ReadDXT5;
336           break;
337         }
338         
339         default:
340         {
341           /* Unknown FOURCC */
342           ThrowReaderException(CorruptImageError, "ImageTypeNotSupported");
343         }
344       }
345     }
346   else
347     {
348       /* Neither compressed nor uncompressed... thus unsupported */
349       ThrowReaderException(CorruptImageError, "ImageTypeNotSupported");
350     }
351   
352   num_images = 1;
353   if (cubemap)
354     {
355       /*
356         Determine number of faces defined in the cubemap
357       */
358       num_images = 0;
359       if (dds_info.ddscaps2 & DDSCAPS2_CUBEMAP_POSITIVEX) num_images++;
360       if (dds_info.ddscaps2 & DDSCAPS2_CUBEMAP_NEGATIVEX) num_images++;
361       if (dds_info.ddscaps2 & DDSCAPS2_CUBEMAP_POSITIVEY) num_images++;
362       if (dds_info.ddscaps2 & DDSCAPS2_CUBEMAP_NEGATIVEY) num_images++;
363       if (dds_info.ddscaps2 & DDSCAPS2_CUBEMAP_POSITIVEZ) num_images++;
364       if (dds_info.ddscaps2 & DDSCAPS2_CUBEMAP_NEGATIVEZ) num_images++;
365     }
366   
367   if (volume)
368     num_images = dds_info.depth;
369   
370   for (n = 0; n < num_images; n++)
371   {
372     if (n != 0)
373       {
374         /* Start a new image */
375         AcquireNextImage(image_info,image,exception);
376         if (GetNextImageInList(image) == (Image *) NULL)
377           {
378             image = DestroyImageList(image);
379             return((Image *) NULL);
380           }
381         image=SyncNextImageInList(image);
382       }
383     
384     image->alpha_trait=alpha_trait;
385     image->compression = compression;
386     image->columns = dds_info.width;
387     image->rows = dds_info.height;
388     image->storage_class = DirectClass;
389     image->endian = LSBEndian;
390     image->depth = 8;
391     if (image_info->ping != MagickFalse)
392       {
393         (void) CloseBlob(image);
394         return(GetFirstImageInList(image));
395       }
396     
397     if ((decoder)(image, &dds_info, exception) != MagickTrue)
398       {
399         (void) CloseBlob(image);
400         return(GetFirstImageInList(image));
401       }
402   }
403   
404   if (EOFBlob(image) != MagickFalse)
405     ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
406       image->filename);
407   
408   (void) CloseBlob(image);
409   return(GetFirstImageInList(image));
410 }
411
412 static MagickBooleanType ReadDDSInfo(Image *image, DDSInfo *dds_info)
413 {
414   size_t
415     hdr_size,
416     required;
417   
418   /* Seek to start of header */
419   (void) SeekBlob(image, 4, SEEK_SET);
420   
421   /* Check header field */
422   hdr_size = ReadBlobLSBLong(image);
423   if (hdr_size != 124)
424     return MagickFalse;
425   
426   /* Fill in DDS info struct */
427   dds_info->flags = ReadBlobLSBLong(image);
428   
429   /* Check required flags */
430   required=(size_t) (DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT);
431   if ((dds_info->flags & required) != required)
432     return MagickFalse;
433   
434   dds_info->height = ReadBlobLSBLong(image);
435   dds_info->width = ReadBlobLSBLong(image);
436   dds_info->pitchOrLinearSize = ReadBlobLSBLong(image);
437   dds_info->depth = ReadBlobLSBLong(image);
438   dds_info->mipmapcount = ReadBlobLSBLong(image);
439   
440   (void) SeekBlob(image, 44, SEEK_CUR);   /* reserved region of 11 DWORDs */
441   
442   /* Read pixel format structure */
443   hdr_size = ReadBlobLSBLong(image);
444   if (hdr_size != 32)
445     return MagickFalse;
446   
447   dds_info->pixelformat.flags = ReadBlobLSBLong(image);
448   dds_info->pixelformat.fourcc = ReadBlobLSBLong(image);
449   dds_info->pixelformat.rgb_bitcount = ReadBlobLSBLong(image);
450   dds_info->pixelformat.r_bitmask = ReadBlobLSBLong(image);
451   dds_info->pixelformat.g_bitmask = ReadBlobLSBLong(image);
452   dds_info->pixelformat.b_bitmask = ReadBlobLSBLong(image);
453   dds_info->pixelformat.alpha_bitmask = ReadBlobLSBLong(image);
454   
455   dds_info->ddscaps1 = ReadBlobLSBLong(image);
456   dds_info->ddscaps2 = ReadBlobLSBLong(image);
457   (void) SeekBlob(image, 12, SEEK_CUR); /* 3 reserved DWORDs */
458   
459   return MagickTrue;
460 }
461
462 static void CalculateColors(unsigned short c0, unsigned short c1,
463   DDSColors *c, MagickBooleanType ignoreAlpha)
464 {
465   c->a[0] = c->a[1] = c->a[2] = c->a[3] = 0;
466   
467   c->r[0] = (unsigned char) C565_red(c0);
468   c->g[0] = (unsigned char) C565_green(c0);
469   c->b[0] = (unsigned char) C565_blue(c0);
470   
471   c->r[1] = (unsigned char) C565_red(c1);
472   c->g[1] = (unsigned char) C565_green(c1);
473   c->b[1] = (unsigned char) C565_blue(c1);
474   
475   if (ignoreAlpha == MagickTrue || c0 > c1)
476     {
477       c->r[2] = (unsigned char) ((2 * c->r[0] + c->r[1]) / 3);
478       c->g[2] = (unsigned char) ((2 * c->g[0] + c->g[1]) / 3);
479       c->b[2] = (unsigned char) ((2 * c->b[0] + c->b[1]) / 3);
480       
481       c->r[3] = (unsigned char) ((c->r[0] + 2 * c->r[1]) / 3);
482       c->g[3] = (unsigned char) ((c->g[0] + 2 * c->g[1]) / 3);
483       c->b[3] = (unsigned char) ((c->b[0] + 2 * c->b[1]) / 3);
484     }
485   else
486     {
487       c->r[2] = (unsigned char) ((c->r[0] + c->r[1]) / 2);
488       c->g[2] = (unsigned char) ((c->g[0] + c->g[1]) / 2);
489       c->b[2] = (unsigned char) ((c->b[0] + c->b[1]) / 2);
490       
491       c->r[3] = c->g[3] = c->b[3] = 0;
492       c->a[3] = 255;
493     }
494 }
495
496 static MagickBooleanType ReadDXT1(Image *image, DDSInfo *dds_info,
497   ExceptionInfo *exception)
498 {
499   DDSColors
500     colors;
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   for (y = 0; y < (ssize_t) dds_info->height; y += 4)
524   {
525     for (x = 0; x < (ssize_t) dds_info->width; x += 4)
526     {
527       /* Get 4x4 patch of pixels to write on */
528       q = QueueAuthenticPixels(image, x, y, Min(4, dds_info->width - x),
529         Min(4, dds_info->height - y),exception);
530       
531       if (q == (Quantum *) NULL)
532         return MagickFalse;
533       
534       /* Read 8 bytes of data from the image */
535       c0 = ReadBlobLSBShort(image);
536       c1 = ReadBlobLSBShort(image);
537       bits = ReadBlobLSBLong(image);
538       
539       CalculateColors(c0, c1, &colors, MagickFalse);
540       
541       /* Write the pixels */
542       for (j = 0; j < 4; j++)
543       {
544         for (i = 0; i < 4; i++)
545         {
546           if ((x + i) < (ssize_t) dds_info->width &&
547               (y + j) < (ssize_t) dds_info->height)
548             {
549               code = (unsigned char) ((bits >> ((j*4+i)*2)) & 0x3);
550               SetPixelRed(image,ScaleCharToQuantum(colors.r[code]),q);
551               SetPixelGreen(image,ScaleCharToQuantum(colors.g[code]),q);
552               SetPixelBlue(image,ScaleCharToQuantum(colors.b[code]),q);
553               SetPixelAlpha(image,ScaleCharToQuantum(colors.a[code]),q);
554               if (colors.a[code] && (image->alpha_trait != BlendPixelTrait))
555                 image->alpha_trait=BlendPixelTrait;  /* Correct matte */
556               q+=GetPixelChannels(image);
557             }
558         }
559       }
560       
561       if (SyncAuthenticPixels(image,exception) == MagickFalse)
562         return MagickFalse;
563     }
564   }
565   
566   SkipDXTMipmaps(image, dds_info, 8);
567   
568   return MagickTrue;
569 }
570
571 static MagickBooleanType ReadDXT3(Image *image, DDSInfo *dds_info,
572   ExceptionInfo *exception)
573 {
574   DDSColors
575     colors;
576   
577   register Quantum
578     *q;
579   
580   register ssize_t
581     i,
582     x;
583   
584   unsigned char
585     alpha;
586   
587   size_t
588     a0,
589     a1,
590     bits,
591     code;
592
593   ssize_t
594     j,
595     y;
596
597   unsigned short
598     c0,
599     c1;
600   
601   for (y = 0; y < (ssize_t) dds_info->height; y += 4)
602   {
603     for (x = 0; x < (ssize_t) dds_info->width; x += 4)
604     {
605       /* Get 4x4 patch of pixels to write on */
606       q = QueueAuthenticPixels(image, x, y, Min(4, dds_info->width - x),
607                          Min(4, dds_info->height - y),exception);
608       
609       if (q == (Quantum *) NULL)
610         return MagickFalse;
611       
612       /* Read alpha values (8 bytes) */
613       a0 = ReadBlobLSBLong(image);
614       a1 = ReadBlobLSBLong(image);
615       
616       /* Read 8 bytes of data from the image */
617       c0 = ReadBlobLSBShort(image);
618       c1 = ReadBlobLSBShort(image);
619       bits = ReadBlobLSBLong(image);
620       
621       CalculateColors(c0, c1, &colors, MagickTrue);
622       
623       /* Write the pixels */
624       for (j = 0; j < 4; j++)
625       {
626         for (i = 0; i < 4; i++)
627         {
628           if ((x + i) < (ssize_t) dds_info->width && (y + j) < (ssize_t) dds_info->height)
629             {
630               code = (bits >> ((4*j+i)*2)) & 0x3;
631               SetPixelRed(image,ScaleCharToQuantum(colors.r[code]),q);
632               SetPixelGreen(image,ScaleCharToQuantum(colors.g[code]),q);
633               SetPixelBlue(image,ScaleCharToQuantum(colors.b[code]),q);
634               /*
635                 Extract alpha value: multiply 0..15 by 17 to get range 0..255
636               */
637               if (j < 2)
638                 alpha = 17U * (unsigned char) ((a0 >> (4*(4*j+i))) & 0xf);
639               else
640                 alpha = 17U * (unsigned char) ((a1 >> (4*(4*(j-2)+i))) & 0xf);
641               SetPixelAlpha(image,ScaleCharToQuantum((unsigned char) alpha),q);
642               q+=GetPixelChannels(image);
643             }
644         }
645       }
646       
647       if (SyncAuthenticPixels(image,exception) == MagickFalse)
648         return MagickFalse;
649     }
650   }
651   
652   SkipDXTMipmaps(image, dds_info, 16);
653   
654   return MagickTrue;
655 }
656
657 static MagickBooleanType ReadDXT5(Image *image, DDSInfo *dds_info,
658   ExceptionInfo *exception)
659 {
660   DDSColors
661     colors;
662   
663   MagickSizeType
664     alpha_bits;
665   
666   register Quantum
667     *q;
668   
669   register ssize_t
670     i,
671     x;
672
673   unsigned char
674     a0,
675     a1;
676   
677   size_t
678     alpha,
679     bits,
680     code,
681     alpha_code;
682
683   ssize_t
684     j,
685     y;
686
687   unsigned short
688     c0,
689     c1;
690   
691   for (y = 0; y < (ssize_t) dds_info->height; y += 4)
692   {
693     for (x = 0; x < (ssize_t) dds_info->width; x += 4)
694     {
695       /* Get 4x4 patch of pixels to write on */
696       q = QueueAuthenticPixels(image, x, y, Min(4, dds_info->width - x),
697                          Min(4, dds_info->height - y),exception);
698       
699       if (q == (Quantum *) NULL)
700         return MagickFalse;
701       
702       /* Read alpha values (8 bytes) */
703       a0 = (unsigned char) ReadBlobByte(image);
704       a1 = (unsigned char) ReadBlobByte(image);
705       
706       alpha_bits = (MagickSizeType)ReadBlobLSBLong(image)
707                  | ((MagickSizeType)ReadBlobLSBShort(image) << 32);
708       
709       /* Read 8 bytes of data from the image */
710       c0 = ReadBlobLSBShort(image);
711       c1 = ReadBlobLSBShort(image);
712       bits = ReadBlobLSBLong(image);
713       
714       CalculateColors(c0, c1, &colors, MagickTrue);
715       
716       /* Write the pixels */
717       for (j = 0; j < 4; j++)
718       {
719         for (i = 0; i < 4; i++)
720         {
721           if ((x + i) < (ssize_t) dds_info->width &&
722               (y + j) < (ssize_t) dds_info->height)
723             {
724               code = (bits >> ((4*j+i)*2)) & 0x3;
725               SetPixelRed(image,ScaleCharToQuantum(colors.r[code]),q);
726               SetPixelGreen(image,ScaleCharToQuantum(colors.g[code]),q);
727               SetPixelBlue(image,ScaleCharToQuantum(colors.b[code]),q);
728               /* Extract alpha value */
729               alpha_code = (size_t) (alpha_bits >> (3*(4*j+i))) & 0x7;
730               if (alpha_code == 0)
731                 alpha = a0;
732               else if (alpha_code == 1)
733                 alpha = a1;
734               else if (a0 > a1)
735                 alpha = ((8-alpha_code) * a0 + (alpha_code-1) * a1) / 7;
736               else if (alpha_code == 6)
737                 alpha = alpha_code;
738               else if (alpha_code == 7)
739                 alpha = 255;
740               else
741                 alpha = (((6-alpha_code) * a0 + (alpha_code-1) * a1) / 5);
742               SetPixelAlpha(image,ScaleCharToQuantum((unsigned char) alpha),q);
743               q+=GetPixelChannels(image);
744             }
745         }
746       }
747       
748       if (SyncAuthenticPixels(image,exception) == MagickFalse)
749         return MagickFalse;
750     }
751   }
752   
753   SkipDXTMipmaps(image, dds_info, 16);
754   
755   return MagickTrue;
756 }
757
758 static MagickBooleanType ReadUncompressedRGB(Image *image, DDSInfo *dds_info,
759   ExceptionInfo *exception)
760 {
761   ssize_t
762     x, y;
763   
764   register Quantum
765     *q;
766   
767   for (y = 0; y < (ssize_t) dds_info->height; y++)
768   {
769     q = QueueAuthenticPixels(image, 0, y, dds_info->width, 1,exception);
770     
771     if (q == (Quantum *) NULL)
772       return MagickFalse;
773     
774     for (x = 0; x < (ssize_t) dds_info->width; x++)
775     {
776       SetPixelBlue(image,ScaleCharToQuantum((unsigned char)
777         ReadBlobByte(image)),q);
778       SetPixelGreen(image,ScaleCharToQuantum((unsigned char)
779         ReadBlobByte(image)),q);
780       SetPixelRed(image,ScaleCharToQuantum((unsigned char)
781         ReadBlobByte(image)),q);
782       if (dds_info->pixelformat.rgb_bitcount == 32)
783         (void) ReadBlobByte(image);
784       q+=GetPixelChannels(image);
785     }
786     
787     if (SyncAuthenticPixels(image,exception) == MagickFalse)
788       return MagickFalse;
789   }
790   
791   SkipRGBMipmaps(image, dds_info, 3);
792   
793   return MagickTrue;
794 }
795
796 static MagickBooleanType ReadUncompressedRGBA(Image *image, DDSInfo *dds_info,
797   ExceptionInfo *exception)
798 {
799   ssize_t
800     x, y;
801   
802   register Quantum
803     *q;
804   
805   for (y = 0; y < (ssize_t) dds_info->height; y++)
806   {
807     q = QueueAuthenticPixels(image, 0, y, dds_info->width, 1,exception);
808     
809     if (q == (Quantum *) NULL)
810       return MagickFalse;
811     
812     for (x = 0; x < (ssize_t) dds_info->width; x++)
813     {
814       SetPixelBlue(image,ScaleCharToQuantum((unsigned char)
815         ReadBlobByte(image)),q);
816       SetPixelGreen(image,ScaleCharToQuantum((unsigned char)
817         ReadBlobByte(image)),q);
818       SetPixelRed(image,ScaleCharToQuantum((unsigned char)
819         ReadBlobByte(image)),q);
820       SetPixelAlpha(image,ScaleCharToQuantum((unsigned char)
821         ReadBlobByte(image)),q);
822       q+=GetPixelChannels(image);
823     }
824     
825     if (SyncAuthenticPixels(image,exception) == MagickFalse)
826       return MagickFalse;
827   }
828   
829   SkipRGBMipmaps(image, dds_info, 4);
830   
831   return MagickTrue;
832 }
833
834 /*
835   Skip the mipmap images for compressed (DXTn) dds files
836 */
837 static void SkipDXTMipmaps(Image *image, DDSInfo *dds_info, int texel_size)
838 {
839   MagickOffsetType
840     offset;
841
842   register ssize_t
843     i;
844
845   size_t
846     h,
847     w;
848   
849   /*
850     Only skip mipmaps for textures and cube maps
851   */
852   if (dds_info->ddscaps1 & DDSCAPS_MIPMAP
853       && (dds_info->ddscaps1 & DDSCAPS_TEXTURE
854           || dds_info->ddscaps2 & DDSCAPS2_CUBEMAP))
855     {
856       w = DIV2(dds_info->width);
857       h = DIV2(dds_info->height);
858       
859       /*
860         Mipmapcount includes the main image, so start from one
861       */
862       for (i = 1; (i < (ssize_t) dds_info->mipmapcount) && w && h; i++)
863       {
864         offset = (MagickOffsetType) ((w + 3) / 4) * ((h + 3) / 4) * texel_size;
865         (void) SeekBlob(image, offset, SEEK_CUR);
866         
867         w = DIV2(w);
868         h = DIV2(h);
869       }
870     }
871 }
872
873 /*
874   Skip the mipmap images for uncompressed (RGB or RGBA) dds files
875 */
876 static void SkipRGBMipmaps(Image *image, DDSInfo *dds_info, int pixel_size)
877 {
878   MagickOffsetType
879     offset;
880   
881   register ssize_t
882     i;
883
884   size_t
885     h,
886     w;
887
888   /*
889     Only skip mipmaps for textures and cube maps
890   */
891   if (dds_info->ddscaps1 & DDSCAPS_MIPMAP
892       && (dds_info->ddscaps1 & DDSCAPS_TEXTURE
893           || dds_info->ddscaps2 & DDSCAPS2_CUBEMAP))
894     {
895       w = DIV2(dds_info->width);
896       h = DIV2(dds_info->height);
897       
898       /*
899         Mipmapcount includes the main image, so start from one
900       */
901       for (i=1; (i < (ssize_t) dds_info->mipmapcount) && w && h; i++)
902       {
903         offset = (MagickOffsetType) w * h * pixel_size;
904         (void) SeekBlob(image, offset, SEEK_CUR);
905         
906         w = DIV2(w);
907         h = DIV2(h);
908       }
909     }
910 }
911 \f
912 /*
913 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
914 %                                                                             %
915 %                                                                             %
916 %                                                                             %
917 %   I s D D S                                                                 %
918 %                                                                             %
919 %                                                                             %
920 %                                                                             %
921 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
922 %
923 %  IsDDS() returns MagickTrue if the image format type, identified by the
924 %  magick string, is DDS.
925 %
926 %  The format of the IsDDS method is:
927 %
928 %      MagickBooleanType IsDDS(const unsigned char *magick,const size_t length)
929 %
930 %  A description of each parameter follows:
931 %
932 %    o magick: compare image format pattern against these bytes.
933 %
934 %    o length: Specifies the length of the magick string.
935 %
936 */
937 static MagickBooleanType IsDDS(const unsigned char *magick,const size_t length)
938 {
939   if (length < 4)
940     return(MagickFalse);
941   if (LocaleNCompare((char *) magick,"DDS ", 4) == 0)
942     return(MagickTrue);
943   return(MagickFalse);
944 }
945 \f
946 /*
947 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
948 %                                                                             %
949 %                                                                             %
950 %                                                                             %
951 %   R e g i s t e r D D S I m a g e                                           %
952 %                                                                             %
953 %                                                                             %
954 %                                                                             %
955 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
956 %
957 %  RegisterDDSImage() adds attributes for the DDS image format to
958 %  the list of supported formats.  The attributes include the image format
959 %  tag, a method to read and/or write the format, whether the format
960 %  supports the saving of more than one frame to the same file or blob,
961 %  whether the format supports native in-memory I/O, and a brief
962 %  description of the format.
963 %
964 %  The format of the RegisterDDSImage method is:
965 %
966 %      RegisterDDSImage(void)
967 %
968 */
969 ModuleExport size_t RegisterDDSImage(void)
970 {
971   MagickInfo
972     *entry;
973
974   entry = SetMagickInfo("DDS");
975   entry->decoder = (DecodeImageHandler *) ReadDDSImage;
976   entry->magick = (IsImageFormatHandler *) IsDDS;
977   entry->seekable_stream=MagickTrue;
978   entry->description = ConstantString("Microsoft DirectDraw Surface");
979   entry->module = ConstantString("DDS");
980   (void) RegisterMagickInfo(entry);
981   return(MagickImageCoderSignature);
982 }
983 \f
984
985 /*
986 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
987 %                                                                             %
988 %                                                                             %
989 %                                                                             %
990 %   U n r e g i s t e r D D S I m a g e                                       %
991 %                                                                             %
992 %                                                                             %
993 %                                                                             %
994 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
995 %
996 %  UnregisterDDSImage() removes format registrations made by the
997 %  DDS module from the list of supported formats.
998 %
999 %  The format of the UnregisterDDSImage method is:
1000 %
1001 %      UnregisterDDSImage(void)
1002 %
1003 */
1004 ModuleExport void UnregisterDDSImage(void)
1005 {
1006   (void) UnregisterMagickInfo("DDS");
1007 }
1008