]> granicus.if.org Git - imagemagick/blobdiff - coders/dcm.c
Added new coder to take a screen shot of a monitor in Windows.
[imagemagick] / coders / dcm.c
index d78dd1b00ad48fc48cc96efa564d41e87c362484..8202eb30ca25c977bec3337f0f335d2aa6e50942 100644 (file)
 %                          Read DICOM Image Format                            %
 %                                                                             %
 %                              Software Design                                %
-%                                John Cristy                                  %
+%                                   Cristy                                    %
 %                                 July 1992                                   %
 %                                                                             %
 %                                                                             %
-%  Copyright 1999-2011 ImageMagick Studio LLC, a non-profit organization      %
+%  Copyright 1999-2014 ImageMagick Studio LLC, a non-profit organization      %
 %  dedicated to making software imaging solutions freely available.           %
 %                                                                             %
 %  You may not use this file except in compliance with the License.  You may  %
 /*
   Include declarations.
 */
-#include "magick/studio.h"
-#include "magick/property.h"
-#include "magick/blob.h"
-#include "magick/blob-private.h"
-#include "magick/cache.h"
-#include "magick/color.h"
-#include "magick/color-private.h"
-#include "magick/colormap.h"
-#include "magick/colormap-private.h"
-#include "magick/constitute.h"
-#include "magick/enhance.h"
-#include "magick/exception.h"
-#include "magick/exception-private.h"
-#include "magick/image.h"
-#include "magick/image-private.h"
-#include "magick/list.h"
-#include "magick/magick.h"
-#include "magick/memory_.h"
-#include "magick/monitor.h"
-#include "magick/monitor-private.h"
-#include "magick/option.h"
-#include "magick/resource_.h"
-#include "magick/quantum-private.h"
-#include "magick/static.h"
-#include "magick/string_.h"
-#include "magick/string-private.h"
-#include "magick/module.h"
+#include "MagickCore/studio.h"
+#include "MagickCore/artifact.h"
+#include "MagickCore/attribute.h"
+#include "MagickCore/blob.h"
+#include "MagickCore/blob-private.h"
+#include "MagickCore/cache.h"
+#include "MagickCore/color.h"
+#include "MagickCore/color-private.h"
+#include "MagickCore/colormap.h"
+#include "MagickCore/colormap-private.h"
+#include "MagickCore/constitute.h"
+#include "MagickCore/enhance.h"
+#include "MagickCore/exception.h"
+#include "MagickCore/exception-private.h"
+#include "MagickCore/image.h"
+#include "MagickCore/image-private.h"
+#include "MagickCore/list.h"
+#include "MagickCore/magick.h"
+#include "MagickCore/memory_.h"
+#include "MagickCore/monitor.h"
+#include "MagickCore/monitor-private.h"
+#include "MagickCore/option.h"
+#include "MagickCore/pixel-accessor.h"
+#include "MagickCore/property.h"
+#include "MagickCore/resource_.h"
+#include "MagickCore/quantum-private.h"
+#include "MagickCore/static.h"
+#include "MagickCore/string_.h"
+#include "MagickCore/string-private.h"
+#include "MagickCore/module.h"
 \f
 /*
   Dicom medical image declarations.
@@ -2812,9 +2815,10 @@ static Image *ReadDCMImage(const ImageInfo *image_info,ExceptionInfo *exception)
 
   MagickBooleanType
     explicit_file,
-    use_explicit,
     explicit_retry,
-    polarity;
+    polarity,
+    sequence,
+    use_explicit;
 
   MagickOffsetType
     offset;
@@ -2822,14 +2826,11 @@ static Image *ReadDCMImage(const ImageInfo *image_info,ExceptionInfo *exception)
   Quantum
     *scale;
 
-  register IndexPacket
-    *indexes;
-
   register ssize_t
     i,
     x;
 
-  register PixelPacket
+  register Quantum
     *q;
 
   register unsigned char
@@ -2839,6 +2840,7 @@ static Image *ReadDCMImage(const ImageInfo *image_info,ExceptionInfo *exception)
     bits_allocated,
     bytes_per_pixel,
     colors,
+    depth,
     height,
     length,
     mask,
@@ -2860,7 +2862,6 @@ static Image *ReadDCMImage(const ImageInfo *image_info,ExceptionInfo *exception)
     window_center,
     y;
 
-
   unsigned char
     *data;
 
@@ -2874,7 +2875,7 @@ static Image *ReadDCMImage(const ImageInfo *image_info,ExceptionInfo *exception)
       image_info->filename);
   assert(exception != (ExceptionInfo *) NULL);
   assert(exception->signature == MagickSignature);
-  image=AcquireImage(image_info);
+  image=AcquireImage(image_info,exception);
   status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
   if (status == MagickFalse)
     {
@@ -2907,6 +2908,7 @@ static Image *ReadDCMImage(const ImageInfo *image_info,ExceptionInfo *exception)
   bytes_per_pixel=1;
   polarity=MagickFalse;
   data=(unsigned char *) NULL;
+  depth=8;
   element=0;
   explicit_vr[2]='\0';
   explicit_file=MagickFalse;
@@ -2921,6 +2923,7 @@ static Image *ReadDCMImage(const ImageInfo *image_info,ExceptionInfo *exception)
   number_scenes=1;
   samples_per_pixel=1;
   scale=(Quantum *) NULL;
+  sequence=MagickFalse;
   signed_data=(~0UL);
   significant_bits=0;
   use_explicit=MagickFalse;
@@ -2928,7 +2931,8 @@ static Image *ReadDCMImage(const ImageInfo *image_info,ExceptionInfo *exception)
   width=0;
   window_center=0;
   window_width=0;
-  for (group=0; (group != 0x7FE0) || (element != 0x0010); )
+  for (group=0; (group != 0x7FE0) || (element != 0x0010) ||
+                (sequence != MagickFalse); )
   {
     /*
       Read a group.
@@ -2950,9 +2954,9 @@ static Image *ReadDCMImage(const ImageInfo *image_info,ExceptionInfo *exception)
       Check for "explicitness", but meta-file headers always explicit.
     */
     if ((explicit_file == MagickFalse) && (group != 0x0002))
-      explicit_file=(isupper((int) *explicit_vr) != MagickFalse) &&
-        (isupper((int) *(explicit_vr+1)) != MagickFalse) ? MagickTrue :
-        MagickFalse;
+      explicit_file=(isupper((unsigned char) *explicit_vr) != MagickFalse) &&
+        (isupper((unsigned char) *(explicit_vr+1)) != MagickFalse) ?
+        MagickTrue : MagickFalse;
     use_explicit=((group == 0x0002) && (explicit_retry == MagickFalse)) ||
       (explicit_file != MagickFalse) ? MagickTrue : MagickFalse;
     if ((use_explicit != MagickFalse) && (strcmp(implicit_vr,"xs") == 0))
@@ -3023,17 +3027,17 @@ static Image *ReadDCMImage(const ImageInfo *image_info,ExceptionInfo *exception)
           if ((group == (ssize_t) dicom_info[i].group) &&
               (element == (ssize_t) dicom_info[i].element))
             break;
-        (void) fprintf(stdout,"0x%04lX %4ld %s-%s (0x%04lx,0x%04lx)",
+        (void) FormatLocaleFile(stdout,"0x%04lX %4ld %s-%s (0x%04lx,0x%04lx)",
           (unsigned long) image->offset,(long) length,implicit_vr,
           explicit_vr,(unsigned long) group,(unsigned long) element);
         if (dicom_info[i].description != (char *) NULL)
-          (void) fprintf(stdout," %s",dicom_info[i].description);
-        (void) fprintf(stdout,": ");
+          (void) FormatLocaleFile(stdout," %s",dicom_info[i].description);
+        (void) FormatLocaleFile(stdout,": ");
       }
-    if ((group == 0x7FE0) && (element == 0x0010))
+    if ((sequence == MagickFalse) && (group == 0x7FE0) && (element == 0x0010))
       {
         if (image_info->verbose != MagickFalse)
-          (void) fprintf(stdout,"\n");
+          (void) FormatLocaleFile(stdout,"\n");
         break;
       }
     /*
@@ -3061,14 +3065,30 @@ static Image *ReadDCMImage(const ImageInfo *image_info,ExceptionInfo *exception)
               count=ReadBlob(image,(size_t) quantum*length,data);
               if (count != (ssize_t) (quantum*length))
                 {
-                  (void) fprintf(stdout,"count=%d quantum=%d length=%d "
-                    "group=%d\n",(int) count,(int) quantum,(int) length,(int)
-                    group);
+                  (void) FormatLocaleFile(stdout,"count=%d quantum=%d "
+                    "length=%d group=%d\n",(int) count,(int) quantum,(int)
+                    length,(int) group);
                    ThrowReaderException(CorruptImageError,
                      "InsufficientImageDataInFile");
                 }
               data[length*quantum]='\0';
             }
+          else
+            if ((unsigned int) datum == 0xFFFFFFFFU)
+              {
+                sequence=MagickTrue;
+                continue;
+              }
+
+    if (((group << 16) | element) == 0xFFFEE0DD)
+      {
+        sequence=MagickFalse;
+        continue;
+      }
+
+    if (sequence != MagickFalse)
+      continue;
+
     switch (group)
     {
       case 0x0002:
@@ -3090,7 +3110,7 @@ static Image *ReadDCMImage(const ImageInfo *image_info,ExceptionInfo *exception)
                 group=0;
                 element=0;
                 if (image_info->verbose != MagickFalse)
-                  (void) fprintf(stdout,
+                  (void) FormatLocaleFile(stdout,
                     "Corrupted image - trying explicit format\n");
                 break;
               }
@@ -3099,8 +3119,8 @@ static Image *ReadDCMImage(const ImageInfo *image_info,ExceptionInfo *exception)
               (void) CopyMagickString(transfer_syntax,(char *) data,
                 MaxTextExtent);
             if (image_info->verbose != MagickFalse)
-              (void) fprintf(stdout,"transfer_syntax=%s\n",(const char*)
-                transfer_syntax);
+              (void) FormatLocaleFile(stdout,"transfer_syntax=%s\n",
+                (const char*) transfer_syntax);
             if (strncmp(transfer_syntax,"1.2.840.10008.1.2",17) == 0)
               {
                 int
@@ -3213,8 +3233,8 @@ static Image *ReadDCMImage(const ImageInfo *image_info,ExceptionInfo *exception)
             bytes_per_pixel=1;
             if (datum > 8)
               bytes_per_pixel=2;
-            image->depth=bits_allocated;
-            if (image->depth > 32)
+            depth=bits_allocated;
+            if (depth > 32)
               ThrowReaderException(CorruptImageError,"ImproperImageHeader");
             max_value=(1UL << bits_allocated)-1;
             break;
@@ -3228,8 +3248,8 @@ static Image *ReadDCMImage(const ImageInfo *image_info,ExceptionInfo *exception)
             bytes_per_pixel=1;
             if (significant_bits > 8)
               bytes_per_pixel=2;
-            image->depth=significant_bits;
-            if (image->depth > 32)
+            depth=significant_bits;
+            if (depth > 32)
               ThrowReaderException(CorruptImageError,"ImproperImageHeader");
             max_value=(1UL << significant_bits)-1;
             mask=(size_t) GetQuantumRange(significant_bits);
@@ -3308,7 +3328,7 @@ static Image *ReadDCMImage(const ImageInfo *image_info,ExceptionInfo *exception)
             p=data;
             for (i=0; i < (ssize_t) colors; i++)
             {
-              if (image->endian != LSBEndian)
+              if (image->endian == MSBEndian)
                 index=(unsigned short) ((*p << 8) | *(p+1));
               else
                 index=(unsigned short) (*p | (*(p+1) << 8));
@@ -3336,7 +3356,7 @@ static Image *ReadDCMImage(const ImageInfo *image_info,ExceptionInfo *exception)
             p=data;
             for (i=0; i < (ssize_t) colors; i++)
             {
-              if (image->endian != LSBEndian)
+              if (image->endian == MSBEndian)
                 index=(unsigned short) ((*p << 8) | *(p+1));
               else
                 index=(unsigned short) (*p | (*(p+1) << 8));
@@ -3364,15 +3384,16 @@ static Image *ReadDCMImage(const ImageInfo *image_info,ExceptionInfo *exception)
             p=data;
             for (i=0; i < (ssize_t) colors; i++)
             {
-              if (image->endian != LSBEndian)
-                index=(unsigned short) ((*p << 8) | *(p+1));
-              else
+              if (image->endian == MSBEndian)
+                index=(unsigned short) ((*p << 8) | *(p+1)); else
                 index=(unsigned short) (*p | (*(p+1) << 8));
               bluemap[i]=(int) index;
               p+=2;
             }
             break;
           }
+          default:
+            break;
         }
         break;
       }
@@ -3412,14 +3433,14 @@ static Image *ReadDCMImage(const ImageInfo *image_info,ExceptionInfo *exception)
         if ((i == (ssize_t) length) || (length > 4))
           {
             (void) SubstituteString(&attribute," ","");
-            (void) SetImageProperty(image,attribute,(char *) data);
+            (void) SetImageProperty(image,attribute,(char *) data,exception);
           }
         attribute=DestroyString(attribute);
       }
     if (image_info->verbose != MagickFalse)
       {
         if (data == (unsigned char *) NULL)
-          (void) fprintf(stdout,"%d\n",datum);
+          (void) FormatLocaleFile(stdout,"%d\n",datum);
         else
           {
             /*
@@ -3436,15 +3457,15 @@ static Image *ReadDCMImage(const ImageInfo *image_info,ExceptionInfo *exception)
                 datum=0;
                 for (j=(ssize_t) length-1; j >= 0; j--)
                   datum=(256*datum+data[j]);
-                (void) fprintf(stdout,"%d",datum);
+                (void) FormatLocaleFile(stdout,"%d",datum);
               }
             else
               for (i=0; i < (ssize_t) length; i++)
                 if (isprint((int) data[i]) != MagickFalse)
-                  (void) fprintf(stdout,"%c",data[i]);
+                  (void) FormatLocaleFile(stdout,"%c",data[i]);
                 else
-                  (void) fprintf(stdout,"%c",'.');
-            (void) fprintf(stdout,"\n");
+                  (void) FormatLocaleFile(stdout,"%c",'.');
+            (void) FormatLocaleFile(stdout,"\n");
           }
       }
     if (data != (unsigned char *) NULL)
@@ -3555,11 +3576,11 @@ static Image *ReadDCMImage(const ImageInfo *image_info,ExceptionInfo *exception)
           (void) fputc(c,file);
         }
         (void) fclose(file);
-        (void) FormatMagickString(read_info->filename,MaxTextExtent,
+        (void) FormatLocaleString(read_info->filename,MaxTextExtent,
           "jpeg:%s",filename);
         if (image->compression == JPEG2000Compression)
-          (void) FormatMagickString(read_info->filename,MaxTextExtent,
-            "jp2:%s",filename);
+          (void) FormatLocaleString(read_info->filename,MaxTextExtent,
+            "j2k:%s",filename);
         jpeg_image=ReadImage(read_info,exception);
         if (jpeg_image != (Image *) NULL)
           {
@@ -3568,7 +3589,7 @@ static Image *ReadDCMImage(const ImageInfo *image_info,ExceptionInfo *exception)
             while (property != (const char *) NULL)
             {
               (void) SetImageProperty(jpeg_image,property,
-                GetImageProperty(image,property));
+                GetImageProperty(image,property,exception),exception);
               property=GetNextImageProperty(image);
             }
             AppendImageToList(&images,jpeg_image);
@@ -3579,7 +3600,7 @@ static Image *ReadDCMImage(const ImageInfo *image_info,ExceptionInfo *exception)
       image=DestroyImage(image);
       return(GetFirstImageInList(images));
     }
-  if (image->depth != (1UL*MAGICKCORE_QUANTUM_DEPTH))
+  if (depth != (1UL*MAGICKCORE_QUANTUM_DEPTH))
     {
       QuantumAny
         range;
@@ -3590,12 +3611,12 @@ static Image *ReadDCMImage(const ImageInfo *image_info,ExceptionInfo *exception)
       /*
         Compute pixel scaling table.
       */
-      length=(size_t) (GetQuantumRange(image->depth)+1);
+      length=(size_t) (GetQuantumRange(depth)+1);
       scale=(Quantum *) AcquireQuantumMemory(length,sizeof(*scale));
       if (scale == (Quantum *) NULL)
         ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
-      range=GetQuantumRange(image->depth);
-      for (i=0; i < (ssize_t) (GetQuantumRange(image->depth)+1); i++)
+      range=GetQuantumRange(depth);
+      for (i=0; i < (ssize_t) (GetQuantumRange(depth)+1); i++)
         scale[i]=ScaleAnyToQuantum((size_t) i,range);
     }
   if (image->compression == RLECompression)
@@ -3635,43 +3656,19 @@ static Image *ReadDCMImage(const ImageInfo *image_info,ExceptionInfo *exception)
   {
     if (image_info->ping != MagickFalse)
       break;
-    if (image->compression == RLECompression)
-      {
-        unsigned int
-          tag;
-
-        /*
-          Read RLE segment table.
-        */
-        for (i=0; i < (ssize_t) stream_info->remaining; i++)
-          (void) ReadBlobByte(image);
-        tag=(ReadBlobLSBShort(image) << 16) | ReadBlobLSBShort(image);
-        stream_info->remaining=(size_t) ReadBlobLSBLong(image);
-        if ((tag != 0xFFFEE000) || (stream_info->remaining <= 64) ||
-            (EOFBlob(image) == MagickTrue))
-          ThrowReaderException(CorruptImageError,"ImproperImageHeader");
-        stream_info->count=0;
-        stream_info->segment_count=ReadBlobLSBLong(image);
-        if (stream_info->segment_count > 1)
-          {
-            bytes_per_pixel=1;
-            image->depth=8;
-          }
-        for (i=0; i < 15; i++)
-          stream_info->segments[i]=(int) ReadBlobLSBLong(image);
-        stream_info->remaining-=64;
-      }
     image->columns=(size_t) width;
     image->rows=(size_t) height;
-    if ((image->colormap == (PixelPacket *) NULL) && (samples_per_pixel == 1))
+    image->depth=depth;
+    image->colorspace=RGBColorspace;
+    if ((image->colormap == (PixelInfo *) NULL) && (samples_per_pixel == 1))
       {
         size_t
           one;
 
         one=1;
         if (colors == 0)
-          colors=one << image->depth;
-        if (AcquireImageColormap(image,colors) == MagickFalse)
+          colors=one << depth;
+        if (AcquireImageColormap(image,one << depth,exception) == MagickFalse)
           ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
         if (redmap != (int *) NULL)
           for (i=0; i < (ssize_t) colors; i++)
@@ -3708,6 +3705,32 @@ static Image *ReadDCMImage(const ImageInfo *image_info,ExceptionInfo *exception)
             image->colormap[i].blue=index;
           }
       }
+    if (image->compression == RLECompression)
+      {
+        unsigned int
+          tag;
+
+        /*
+          Read RLE segment table.
+        */
+        for (i=0; i < (ssize_t) stream_info->remaining; i++)
+          (void) ReadBlobByte(image);
+        tag=(ReadBlobLSBShort(image) << 16) | ReadBlobLSBShort(image);
+        stream_info->remaining=(size_t) ReadBlobLSBLong(image);
+        if ((tag != 0xFFFEE000) || (stream_info->remaining <= 64) ||
+            (EOFBlob(image) != MagickFalse))
+          ThrowReaderException(CorruptImageError,"ImproperImageHeader");
+        stream_info->count=0;
+        stream_info->segment_count=ReadBlobLSBLong(image);
+        if (stream_info->segment_count > 1)
+          {
+            bytes_per_pixel=1;
+            depth=8;
+          }
+        for (i=0; i < 15; i++)
+          stream_info->segments[i]=(int) ReadBlobLSBLong(image);
+        stream_info->remaining-=64;
+      }
     if ((samples_per_pixel > 1) && (image->interlace == PlaneInterlace))
       {
         /*
@@ -3718,7 +3741,7 @@ static Image *ReadDCMImage(const ImageInfo *image_info,ExceptionInfo *exception)
           for (y=0; y < (ssize_t) image->rows; y++)
           {
             q=GetAuthenticPixels(image,0,y,image->columns,1,exception);
-            if (q == (PixelPacket *) NULL)
+            if (q == (Quantum *) NULL)
               break;
             for (x=0; x < (ssize_t) image->columns; x++)
             {
@@ -3726,33 +3749,32 @@ static Image *ReadDCMImage(const ImageInfo *image_info,ExceptionInfo *exception)
               {
                 case 0:
                 {
-                  SetRedPixelComponent(q,ScaleCharToQuantum((unsigned char)
-                    ReadDCMByte(stream_info,image)));
+                  SetPixelRed(image,ScaleCharToQuantum((unsigned char)
+                    ReadDCMByte(stream_info,image)),q);
                   break;
                 }
                 case 1:
                 {
-                  SetGreenPixelComponent(q,ScaleCharToQuantum((unsigned char)
-                    ReadDCMByte(stream_info,image)));
+                  SetPixelGreen(image,ScaleCharToQuantum((unsigned char)
+                    ReadDCMByte(stream_info,image)),q);
                   break;
                 }
                 case 2:
                 {
-                  SetBluePixelComponent(q,ScaleCharToQuantum((unsigned char)
-                    ReadDCMByte(stream_info,image)));
+                  SetPixelBlue(image,ScaleCharToQuantum((unsigned char)
+                    ReadDCMByte(stream_info,image)),q);
                   break;
                 }
                 case 3:
                 {
-                  SetOpacityPixelComponent(q,(QuantumRange-
-                    ScaleCharToQuantum((unsigned char) ReadDCMByte(stream_info,
-                    image))));
+                  SetPixelAlpha(image,ScaleCharToQuantum((unsigned char)
+                    ReadDCMByte(stream_info,image)),q);
                   break;
                 }
                 default:
                   break;
               }
-              q++;
+              q+=GetPixelChannels(image);
             }
             if (SyncAuthenticPixels(image,exception) == MagickFalse)
               break;
@@ -3774,7 +3796,7 @@ static Image *ReadDCMImage(const ImageInfo *image_info,ExceptionInfo *exception)
         int
           byte;
 
-        LongPixelPacket
+        PixelPacket
           pixel;
 
         /*
@@ -3794,9 +3816,8 @@ static Image *ReadDCMImage(const ImageInfo *image_info,ExceptionInfo *exception)
         for (y=0; y < (ssize_t) image->rows; y++)
         {
           q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
-          if (q == (PixelPacket *) NULL)
+          if (q == (Quantum *) NULL)
             break;
-          indexes=GetAuthenticIndexQueue(image);
           for (x=0; x < (ssize_t) image->columns; x++)
           {
             if (samples_per_pixel == 1)
@@ -3819,8 +3840,6 @@ static Image *ReadDCMImage(const ImageInfo *image_info,ExceptionInfo *exception)
                         pixel_value=(int) (polarity != MagickFalse ? (max_value-
                           ReadDCMLSBShort(stream_info,image)) :
                           ReadDCMLSBShort(stream_info,image));
-                      if (signed_data == 1)
-                        pixel_value=((signed short) pixel_value);
                     }
                   else
                     {
@@ -3866,8 +3885,9 @@ static Image *ReadDCMImage(const ImageInfo *image_info,ExceptionInfo *exception)
                           0.5)/(window_width-1))+0.5));
                   }
                 index&=mask;
-                index=(int) ConstrainColormapIndex(image,(size_t) index);
-                SetIndexPixelComponent(indexes+x,index);
+                index=(int) ConstrainColormapIndex(image,(size_t) index,
+                  exception);
+                SetPixelIndex(image,index,q);
                 pixel.red=1UL*image->colormap[index].red;
                 pixel.green=1UL*image->colormap[index].green;
                 pixel.blue=1UL*image->colormap[index].blue;
@@ -3905,10 +3925,10 @@ static Image *ReadDCMImage(const ImageInfo *image_info,ExceptionInfo *exception)
                     pixel.blue=scale[pixel.blue];
                   }
               }
-            SetRedPixelComponent(q,pixel.red);
-            SetGreenPixelComponent(q,pixel.green);
-            SetBluePixelComponent(q,pixel.blue);
-            q++;
+            SetPixelRed(image,pixel.red,q);
+            SetPixelGreen(image,pixel.green,q);
+            SetPixelBlue(image,pixel.blue,q);
+            q+=GetPixelChannels(image);
           }
           if (SyncAuthenticPixels(image,exception) == MagickFalse)
             break;
@@ -3924,9 +3944,8 @@ static Image *ReadDCMImage(const ImageInfo *image_info,ExceptionInfo *exception)
           for (y=0; y < (ssize_t) image->rows; y++)
           {
             q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
-            if (q == (PixelPacket *) NULL)
+            if (q == (Quantum *) NULL)
               break;
-            indexes=GetAuthenticIndexQueue(image);
             for (x=0; x < (ssize_t) image->columns; x++)
             {
               if (samples_per_pixel == 1)
@@ -3996,8 +4015,10 @@ static Image *ReadDCMImage(const ImageInfo *image_info,ExceptionInfo *exception)
                             0.5)/(window_width-1))+0.5));
                     }
                   index&=mask;
-                  index=(int) ConstrainColormapIndex(image,(size_t) index);
-                  SetIndexPixelComponent(indexes+x,index);
+                  index=(int) ConstrainColormapIndex(image,(size_t) index,
+                    exception);
+                  SetPixelIndex(image,(((size_t) GetPixelIndex(image,q)) |
+                    (((size_t) index) << 8)),q);
                   pixel.red=1UL*image->colormap[index].red;
                   pixel.green=1UL*image->colormap[index].green;
                   pixel.blue=1UL*image->colormap[index].blue;
@@ -4035,13 +4056,13 @@ static Image *ReadDCMImage(const ImageInfo *image_info,ExceptionInfo *exception)
                       pixel.blue=scale[pixel.blue];
                     }
                 }
-              SetRedPixelComponent(q,(((size_t) q->red) |
-                (((size_t) pixel.red) << 8)));
-              SetGreenPixelComponent(q,(((size_t) q->green) |
-                (((size_t) pixel.green) << 8)));
-              SetBluePixelComponent(q,(((size_t) q->green) |
-                (((size_t) pixel.blue) << 8)));
-              q++;
+              SetPixelRed(image,(((size_t) GetPixelRed(image,q)) |
+                (((size_t) pixel.red) << 8)),q);
+              SetPixelGreen(image,(((size_t) GetPixelGreen(image,q)) |
+                (((size_t) pixel.green) << 8)),q);
+              SetPixelBlue(image,(((size_t) GetPixelBlue(image,q)) |
+                (((size_t) pixel.blue) << 8)),q);
+              q+=GetPixelChannels(image);
             }
             if (SyncAuthenticPixels(image,exception) == MagickFalse)
               break;
@@ -4054,6 +4075,8 @@ static Image *ReadDCMImage(const ImageInfo *image_info,ExceptionInfo *exception)
               }
           }
       }
+    if (IsImageGray(image,exception) != MagickFalse)
+      (void) SetImageColorspace(image,GRAYColorspace,exception);
     if (EOFBlob(image) != MagickFalse)
       {
         ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
@@ -4071,7 +4094,7 @@ static Image *ReadDCMImage(const ImageInfo *image_info,ExceptionInfo *exception)
         /*
           Allocate next image structure.
         */
-        AcquireNextImage(image_info,image);
+        AcquireNextImage(image_info,image,exception);
         if (GetNextImageInList(image) == (Image *) NULL)
           {
             image=DestroyImageList(image);