]> granicus.if.org Git - imagemagick/blobdiff - coders/dcm.c
...
[imagemagick] / coders / dcm.c
index af469b0b460ba841ca606f9ebfb828652e404fed..94e8e6a8b1ce89f1dbc99aa29f9ba1f2ca468793 100644 (file)
 %                                 July 1992                                   %
 %                                                                             %
 %                                                                             %
-%  Copyright 1999-2014 ImageMagick Studio LLC, a non-profit organization      %
+%  Copyright 1999-2018 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  %
 %  obtain a copy of the License at                                            %
 %                                                                             %
-%    http://www.imagemagick.org/script/license.php                            %
+%    https://www.imagemagick.org/script/license.php                           %
 %                                                                             %
 %  Unless required by applicable law or agreed to in writing, software        %
 %  distributed under the License is distributed on an "AS IS" BASIS,          %
@@ -76,7 +76,7 @@
 
 typedef struct _DicomInfo
 {
-  unsigned short
+  const unsigned short
     group,
     element;
 
@@ -2675,20 +2675,33 @@ static MagickBooleanType IsDCM(const unsigned char *magick,const size_t length)
 %    o exception: return any errors or warnings in this structure.
 %
 */
-
-static inline size_t MagickMax(const size_t x,const size_t y)
+typedef struct _DCMInfo
 {
-  if (x > y)
-    return(x);
-  return(y);
-}
+  MagickBooleanType
+    polarity;
 
-static inline size_t MagickMin(const size_t x,const size_t y)
-{
-  if (x < y)
-    return(x);
-  return(y);
-}
+  Quantum
+    *scale;
+
+  size_t
+    bits_allocated,
+    bytes_per_pixel,
+    depth,
+    mask,
+    max_value,
+    samples_per_pixel,
+    signed_data,
+    significant_bits;
+
+  MagickBooleanType
+    rescale;
+
+  double
+    rescale_intercept,
+    rescale_slope,
+    window_center,
+    window_width;
+} DCMInfo;
 
 typedef struct _DCMStreamInfo
 {
@@ -2760,7 +2773,7 @@ static int ReadDCMByte(DCMStreamInfo *stream_info,Image *image)
   return(ReadBlobByte(image));
 }
 
-static unsigned short ReadDCMLSBShort(DCMStreamInfo *stream_info,Image *image)
+static unsigned short ReadDCMShort(DCMStreamInfo *stream_info,Image *image)
 {
   int
     shift;
@@ -2776,28 +2789,209 @@ static unsigned short ReadDCMLSBShort(DCMStreamInfo *stream_info,Image *image)
   return(value);
 }
 
-static unsigned short ReadDCMMSBShort(DCMStreamInfo *stream_info,Image *image)
+static signed short ReadDCMSignedShort(DCMStreamInfo *stream_info,Image *image)
+{
+  union
+  {
+    unsigned short
+      unsigned_value;
+
+    signed short
+      signed_value;
+  } quantum;
+
+  quantum.unsigned_value=ReadDCMShort(stream_info,image);
+  return(quantum.signed_value);
+}
+
+static MagickBooleanType ReadDCMPixels(Image *image,DCMInfo *info,
+  DCMStreamInfo *stream_info,MagickBooleanType first_segment,
+  ExceptionInfo *exception)
 {
   int
-    shift;
-  unsigned short
-    value;
+    byte,
+    index;
 
-  if (image->compression != RLECompression)
-    return(ReadBlobMSBShort(image));
-  shift=image->depth < 16 ? 4 : 8;
-  value=(ReadDCMByte(stream_info,image) << shift) | (unsigned short)
-    ReadDCMByte(stream_info,image);
-  return(value);
+  MagickBooleanType
+    status;
+
+  PixelPacket
+    pixel;
+
+  register ssize_t
+    i,
+    x;
+
+  register Quantum
+    *q;
+
+  ssize_t
+    y;
+
+  /*
+    Convert DCM Medical image to pixel packets.
+  */
+  byte=0;
+  i=0;
+  status=MagickTrue;
+  (void) ResetMagickMemory(&pixel,0,sizeof(pixel));
+  for (y=0; y < (ssize_t) image->rows; y++)
+  {
+    q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
+    if (q == (Quantum *) NULL)
+      break;
+    for (x=0; x < (ssize_t) image->columns; x++)
+    {
+      if (info->samples_per_pixel == 1)
+        {
+          int
+            pixel_value;
+
+          if (info->bytes_per_pixel == 1)
+            pixel_value=info->polarity != MagickFalse ?
+              ((int) info->max_value-ReadDCMByte(stream_info,image)) :
+              ReadDCMByte(stream_info,image);
+          else
+            if ((info->bits_allocated != 12) || (info->significant_bits != 12))
+              {
+                if (info->signed_data)
+                  pixel_value=ReadDCMSignedShort(stream_info,image);
+                else
+                  pixel_value=(int) ReadDCMShort(stream_info,image);
+                if (info->polarity != MagickFalse)
+                  pixel_value=(int)info->max_value-pixel_value;
+              }
+            else
+              {
+                if ((i & 0x01) != 0)
+                  pixel_value=(ReadDCMByte(stream_info,image) << 8) |
+                    byte;
+                else
+                  {
+                    pixel_value=ReadDCMSignedShort(stream_info,image);
+                    byte=(int) (pixel_value & 0x0f);
+                    pixel_value>>=4;
+                  }
+                i++;
+              }
+          if (info->signed_data == 1)
+            pixel_value-=32767;
+          index=pixel_value;
+          if (info->rescale != MagickFalse)
+            {
+              double
+                scaled_value;
+
+              scaled_value=pixel_value*info->rescale_slope+
+                info->rescale_intercept;
+              index=(int) scaled_value;
+              if (info->window_width != 0)
+                {
+                  double
+                    window_max,
+                    window_min;
+
+                  window_min=ceil(info->window_center-
+                    (info->window_width-1.0)/2.0-0.5);
+                  window_max=floor(info->window_center+
+                    (info->window_width-1.0)/2.0+0.5);
+                  if (scaled_value <= window_min)
+                    index=0;
+                  else
+                    if (scaled_value > window_max)
+                      index=(int) info->max_value;
+                    else
+                      index=(int) (info->max_value*(((scaled_value-
+                        info->window_center-0.5)/(info->window_width-1))+0.5));
+                }
+            }
+          index&=info->mask;
+          index=(int) ConstrainColormapIndex(image,(ssize_t) index,exception);
+          if (first_segment)
+            SetPixelIndex(image,(Quantum) index,q);
+          else
+            SetPixelIndex(image,(Quantum) (((size_t) index) |
+              (((size_t) GetPixelIndex(image,q)) << 8)),q);
+          pixel.red=(unsigned int) image->colormap[index].red;
+          pixel.green=(unsigned int) image->colormap[index].green;
+          pixel.blue=(unsigned int) image->colormap[index].blue;
+        }
+      else
+        {
+          if (info->bytes_per_pixel == 1)
+            {
+              pixel.red=(unsigned int) ReadDCMByte(stream_info,image);
+              pixel.green=(unsigned int) ReadDCMByte(stream_info,image);
+              pixel.blue=(unsigned int) ReadDCMByte(stream_info,image);
+            }
+          else
+            {
+              pixel.red=ReadDCMShort(stream_info,image);
+              pixel.green=ReadDCMShort(stream_info,image);
+              pixel.blue=ReadDCMShort(stream_info,image);
+            }
+          pixel.red&=info->mask;
+          pixel.green&=info->mask;
+          pixel.blue&=info->mask;
+          if (info->scale != (Quantum *) NULL)
+            {
+              if ((MagickSizeType) pixel.red <= GetQuantumRange(info->depth))
+                pixel.red=info->scale[pixel.red];
+              if ((MagickSizeType) pixel.green <= GetQuantumRange(info->depth))
+                pixel.green=info->scale[pixel.green];
+              if ((MagickSizeType) pixel.blue <= GetQuantumRange(info->depth))
+                pixel.blue=info->scale[pixel.blue];
+            }
+        }
+      if (first_segment != MagickFalse)
+        {
+          SetPixelRed(image,(Quantum) pixel.red,q);
+          SetPixelGreen(image,(Quantum) pixel.green,q);
+          SetPixelBlue(image,(Quantum) pixel.blue,q);
+        }
+      else
+        {
+          SetPixelRed(image,(Quantum) (((size_t) pixel.red) |
+            (((size_t) GetPixelRed(image,q)) << 8)),q);
+          SetPixelGreen(image,(Quantum) (((size_t) pixel.green) |
+            (((size_t) GetPixelGreen(image,q)) << 8)),q);
+          SetPixelBlue(image,(Quantum) (((size_t) pixel.blue) |
+            (((size_t) GetPixelBlue(image,q)) << 8)),q);
+        }
+      q+=GetPixelChannels(image);
+    }
+    if (SyncAuthenticPixels(image,exception) == MagickFalse)
+      break;
+    if (image->previous == (Image *) NULL)
+      {
+        status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
+          image->rows);
+        if (status == MagickFalse)
+          break;
+      }
+  }
+  return(status);
 }
 
 static Image *ReadDCMImage(const ImageInfo *image_info,ExceptionInfo *exception)
 {
+#define ThrowDCMException(exception,message) \
+{ \
+  if (data != (unsigned char *) NULL) \
+    data=(unsigned char *) RelinquishMagickMemory(data); \
+  if (stream_info != (DCMStreamInfo *) NULL) \
+    stream_info=(DCMStreamInfo *) RelinquishMagickMemory(stream_info); \
+  ThrowReaderException((exception),(message)); \
+}
+
   char
-    explicit_vr[MaxTextExtent],
-    implicit_vr[MaxTextExtent],
-    magick[MaxTextExtent],
-    photometric[MaxTextExtent];
+    explicit_vr[MagickPathExtent],
+    implicit_vr[MagickPathExtent],
+    magick[MagickPathExtent],
+    photometric[MagickPathExtent];
+
+  DCMInfo
+    info;
 
   DCMStreamInfo
     *stream_info;
@@ -2810,71 +3004,53 @@ static Image *ReadDCMImage(const ImageInfo *image_info,ExceptionInfo *exception)
     datum,
     *greenmap,
     *graymap,
-    index,
     *redmap;
 
   MagickBooleanType
     explicit_file,
     explicit_retry,
-    polarity,
     sequence,
     use_explicit;
 
   MagickOffsetType
     offset;
 
-  Quantum
-    *scale;
-
-  register ssize_t
-    i,
-    x;
-
-  register Quantum
-    *q;
-
   register unsigned char
     *p;
 
+  register ssize_t
+    i;
+
   size_t
-    bits_allocated,
-    bytes_per_pixel,
     colors,
-    depth,
     height,
     length,
-    mask,
-    max_value,
     number_scenes,
     quantum,
-    samples_per_pixel,
-    signed_data,
-    significant_bits,
     status,
-    width,
-    window_width;
+    width;
 
   ssize_t
     count,
-    element,
-    group,
-    scene,
-    window_center,
-    y;
+    scene;
 
   unsigned char
     *data;
 
+  unsigned short
+    group,
+    element;
+
   /*
     Open image file.
   */
   assert(image_info != (const ImageInfo *) NULL);
-  assert(image_info->signature == MagickSignature);
+  assert(image_info->signature == MagickCoreSignature);
   if (image_info->debug != MagickFalse)
     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
       image_info->filename);
   assert(exception != (ExceptionInfo *) NULL);
-  assert(exception->signature == MagickSignature);
+  assert(exception->signature == MagickCoreSignature);
   image=AcquireImage(image_info,exception);
   status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
   if (status == MagickFalse)
@@ -2883,32 +3059,45 @@ static Image *ReadDCMImage(const ImageInfo *image_info,ExceptionInfo *exception)
       return((Image *) NULL);
     }
   image->depth=8UL;
+  image->endian=LSBEndian;
   /*
     Read DCM preamble.
   */
+  data=(unsigned char *) NULL;
   stream_info=(DCMStreamInfo *) AcquireMagickMemory(sizeof(*stream_info));
   if (stream_info == (DCMStreamInfo *) NULL)
-    ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
+    ThrowDCMException(ResourceLimitError,"MemoryAllocationFailed");
   (void) ResetMagickMemory(stream_info,0,sizeof(*stream_info));
   count=ReadBlob(image,128,(unsigned char *) magick);
   if (count != 128)
-    ThrowReaderException(CorruptImageError,"ImproperImageHeader");
+    ThrowDCMException(CorruptImageError,"ImproperImageHeader");
   count=ReadBlob(image,4,(unsigned char *) magick);
   if ((count != 4) || (LocaleNCompare(magick,"DICM",4) != 0))
     {
       offset=SeekBlob(image,0L,SEEK_SET);
       if (offset < 0)
-        ThrowReaderException(CorruptImageError,"ImproperImageHeader");
+        ThrowDCMException(CorruptImageError,"ImproperImageHeader");
     }
   /*
     Read DCM Medical image.
   */
-  (void) CopyMagickString(photometric,"MONOCHROME1 ",MaxTextExtent);
-  bits_allocated=8;
-  bytes_per_pixel=1;
-  polarity=MagickFalse;
+  (void) CopyMagickString(photometric,"MONOCHROME1 ",MagickPathExtent);
+  info.polarity=MagickFalse;
+  info.scale=(Quantum *) NULL;
+  info.bits_allocated=8;
+  info.bytes_per_pixel=1;
+  info.depth=8;
+  info.mask=0xffff;
+  info.max_value=255UL;
+  info.samples_per_pixel=1;
+  info.signed_data=(~0UL);
+  info.significant_bits=0;
+  info.rescale=MagickFalse;
+  info.rescale_intercept=0.0;
+  info.rescale_slope=1.0;
+  info.window_center=0.0;
+  info.window_width=0.0;
   data=(unsigned char *) NULL;
-  depth=8;
   element=0;
   explicit_vr[2]='\0';
   explicit_file=MagickFalse;
@@ -2918,19 +3107,11 @@ static Image *ReadDCMImage(const ImageInfo *image_info,ExceptionInfo *exception)
   bluemap=(int *) NULL;
   graymap=(int *) NULL;
   height=0;
-  max_value=255UL;
-  mask=0xffff;
   number_scenes=1;
-  samples_per_pixel=1;
-  scale=(Quantum *) NULL;
   sequence=MagickFalse;
-  signed_data=(~0UL);
-  significant_bits=0;
   use_explicit=MagickFalse;
   explicit_retry = MagickFalse;
   width=0;
-  window_center=0;
-  window_width=0;
   for (group=0; (group != 0x7FE0) || (element != 0x0010) ||
                 (sequence != MagickFalse); )
   {
@@ -2938,18 +3119,24 @@ static Image *ReadDCMImage(const ImageInfo *image_info,ExceptionInfo *exception)
       Read a group.
     */
     image->offset=(ssize_t) TellBlob(image);
-    group=(ssize_t) ReadBlobLSBShort(image);
-    element=(ssize_t) ReadBlobLSBShort(image);
+    group=ReadBlobLSBShort(image);
+    element=ReadBlobLSBShort(image);
+    if ((group != 0x0002) && (image->endian == MSBEndian))
+      {
+        group=(unsigned short) ((group << 8) | ((group >> 8) & 0xFF));
+        element=(unsigned short) ((element << 8) | ((element >> 8) & 0xFF));
+      }
     quantum=0;
     /*
       Find corresponding VR for this group and element.
     */
     for (i=0; dicom_info[i].group < 0xffff; i++)
-      if ((group == (ssize_t) dicom_info[i].group) &&
-          (element == (ssize_t) dicom_info[i].element))
+      if ((group == dicom_info[i].group) && (element == dicom_info[i].element))
         break;
-    (void) CopyMagickString(implicit_vr,dicom_info[i].vr,MaxTextExtent);
+    (void) CopyMagickString(implicit_vr,dicom_info[i].vr,MagickPathExtent);
     count=ReadBlob(image,2,(unsigned char *) explicit_vr);
+    if (count != 2)
+      ThrowDCMException(CorruptImageError,"ImproperImageHeader");
     /*
       Check for "explicitness", but meta-file headers always explicit.
     */
@@ -2959,13 +3146,13 @@ static Image *ReadDCMImage(const ImageInfo *image_info,ExceptionInfo *exception)
         MagickTrue : MagickFalse;
     use_explicit=((group == 0x0002) && (explicit_retry == MagickFalse)) ||
       (explicit_file != MagickFalse) ? MagickTrue : MagickFalse;
-    if ((use_explicit != MagickFalse) && (strcmp(implicit_vr,"xs") == 0))
-      (void) CopyMagickString(implicit_vr,explicit_vr,MaxTextExtent);
-    if ((use_explicit == MagickFalse) || (strcmp(implicit_vr,"!!") == 0))
+    if ((use_explicit != MagickFalse) && (strncmp(implicit_vr,"xs",2) == 0))
+      (void) CopyMagickString(implicit_vr,explicit_vr,MagickPathExtent);
+    if ((use_explicit == MagickFalse) || (strncmp(implicit_vr,"!!",2) == 0))
       {
         offset=SeekBlob(image,(MagickOffsetType) -2,SEEK_CUR);
         if (offset < 0)
-          ThrowReaderException(CorruptImageError,"ImproperImageHeader");
+          ThrowDCMException(CorruptImageError,"ImproperImageHeader");
         quantum=4;
       }
     else
@@ -2974,9 +3161,10 @@ static Image *ReadDCMImage(const ImageInfo *image_info,ExceptionInfo *exception)
           Assume explicit type.
         */
         quantum=2;
-        if ((strcmp(explicit_vr,"OB") == 0) ||
-            (strcmp(explicit_vr,"UN") == 0) ||
-            (strcmp(explicit_vr,"OW") == 0) || (strcmp(explicit_vr,"SQ") == 0))
+        if ((strncmp(explicit_vr,"OB",2) == 0) ||
+            (strncmp(explicit_vr,"UN",2) == 0) ||
+            (strncmp(explicit_vr,"OW",2) == 0) ||
+            (strncmp(explicit_vr,"SQ",2) == 0))
           {
             (void) ReadBlobLSBShort(image);
             quantum=4;
@@ -2984,24 +3172,34 @@ static Image *ReadDCMImage(const ImageInfo *image_info,ExceptionInfo *exception)
       }
     datum=0;
     if (quantum == 4)
-      datum=(int) ReadBlobLSBLong(image);
+      {
+        if (group == 0x0002)
+          datum=ReadBlobLSBSignedLong(image);
+        else
+          datum=ReadBlobSignedLong(image);
+      }
     else
       if (quantum == 2)
-        datum=(int) ReadBlobLSBShort(image);
+        {
+          if (group == 0x0002)
+            datum=ReadBlobLSBSignedShort(image);
+          else
+            datum=ReadBlobSignedShort(image);
+        }
     quantum=0;
     length=1;
     if (datum != 0)
       {
-        if ((strcmp(implicit_vr,"SS") == 0) ||
-            (strcmp(implicit_vr,"US") == 0))
+        if ((strncmp(implicit_vr,"SS",2) == 0) ||
+            (strncmp(implicit_vr,"US",2) == 0))
           quantum=2;
         else
-          if ((strcmp(implicit_vr,"UL") == 0) ||
-              (strcmp(implicit_vr,"SL") == 0) ||
-              (strcmp(implicit_vr,"FL") == 0))
+          if ((strncmp(implicit_vr,"UL",2) == 0) ||
+              (strncmp(implicit_vr,"SL",2) == 0) ||
+              (strncmp(implicit_vr,"FL",2) == 0))
             quantum=4;
           else
-            if (strcmp(implicit_vr,"FD") != 0)
+            if (strncmp(implicit_vr,"FD",2) != 0)
               quantum=1;
             else
               quantum=8;
@@ -3024,12 +3222,12 @@ static Image *ReadDCMImage(const ImageInfo *image_info,ExceptionInfo *exception)
         if (use_explicit == MagickFalse)
           explicit_vr[0]='\0';
         for (i=0; dicom_info[i].description != (char *) NULL; i++)
-          if ((group == (ssize_t) dicom_info[i].group) &&
-              (element == (ssize_t) dicom_info[i].element))
+          if ((group == dicom_info[i].group) &&
+              (element == dicom_info[i].element))
             break;
         (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);
+          (unsigned long) image->offset,(long) length,implicit_vr,explicit_vr,
+          (unsigned long) group,(unsigned long) element);
         if (dicom_info[i].description != (char *) NULL)
           (void) FormatLocaleFile(stdout," %s",dicom_info[i].description);
         (void) FormatLocaleFile(stdout,": ");
@@ -3045,23 +3243,34 @@ static Image *ReadDCMImage(const ImageInfo *image_info,ExceptionInfo *exception)
     */
     data=(unsigned char *) NULL;
     if ((length == 1) && (quantum == 1))
-      datum=(int) ReadBlobByte(image);
+      datum=ReadBlobByte(image);
     else
       if ((length == 1) && (quantum == 2))
-        datum=(int) ReadBlobLSBShort(image);
+        {
+          if (group == 0x0002)
+            datum=ReadBlobLSBSignedShort(image);
+          else
+            datum=ReadBlobSignedShort(image);
+        }
       else
         if ((length == 1) && (quantum == 4))
-          datum=(int) ReadBlobLSBLong(image);
+          {
+            if (group == 0x0002)
+              datum=ReadBlobLSBSignedLong(image);
+            else
+              datum=ReadBlobSignedLong(image);
+          }
         else
           if ((quantum != 0) && (length != 0))
             {
-              data=(unsigned char *) NULL;
+              if (length > GetBlobSize(image))
+                ThrowDCMException(CorruptImageError,
+                  "InsufficientImageDataInFile");
               if (~length >= 1)
                 data=(unsigned char *) AcquireQuantumMemory(length+1,quantum*
                   sizeof(*data));
               if (data == (unsigned char *) NULL)
-                ThrowReaderException(ResourceLimitError,
-                  "MemoryAllocationFailed");
+                ThrowDCMException(ResourceLimitError,"MemoryAllocationFailed");
               count=ReadBlob(image,(size_t) quantum*length,data);
               if (count != (ssize_t) (quantum*length))
                 {
@@ -3069,7 +3278,7 @@ static Image *ReadDCMImage(const ImageInfo *image_info,ExceptionInfo *exception)
                     (void) FormatLocaleFile(stdout,"count=%d quantum=%d "
                       "length=%d group=%d\n",(int) count,(int) quantum,(int)
                       length,(int) group);
-                   ThrowReaderException(CorruptImageError,
+                   ThrowDCMException(CorruptImageError,
                      "InsufficientImageDataInFile");
                 }
               data[length*quantum]='\0';
@@ -3080,16 +3289,19 @@ static Image *ReadDCMImage(const ImageInfo *image_info,ExceptionInfo *exception)
                 sequence=MagickTrue;
                 continue;
               }
-
-    if (((group << 16) | element) == 0xFFFEE0DD)
+    if ((unsigned int) ((group << 16) | element) == 0xFFFEE0DD)
       {
+        if (data != (unsigned char *) NULL)
+          data=(unsigned char *) RelinquishMagickMemory(data);
         sequence=MagickFalse;
         continue;
       }
-
     if (sequence != MagickFalse)
-      continue;
-
+      {
+        if (data != (unsigned char *) NULL)
+          data=(unsigned char *) RelinquishMagickMemory(data);
+        continue;
+      }
     switch (group)
     {
       case 0x0002:
@@ -3099,7 +3311,7 @@ static Image *ReadDCMImage(const ImageInfo *image_info,ExceptionInfo *exception)
           case 0x0010:
           {
             char
-              transfer_syntax[MaxTextExtent];
+              transfer_syntax[MagickPathExtent];
 
             /*
               Transfer Syntax.
@@ -3118,19 +3330,26 @@ static Image *ReadDCMImage(const ImageInfo *image_info,ExceptionInfo *exception)
             *transfer_syntax='\0';
             if (data != (unsigned char *) NULL)
               (void) CopyMagickString(transfer_syntax,(char *) data,
-                MaxTextExtent);
+                MagickPathExtent);
             if (image_info->verbose != MagickFalse)
               (void) FormatLocaleFile(stdout,"transfer_syntax=%s\n",
-                (const char*) transfer_syntax);
+                (const char *) transfer_syntax);
             if (strncmp(transfer_syntax,"1.2.840.10008.1.2",17) == 0)
               {
                 int
                   subtype,
                   type;
 
-                type=0;
+                type=1;
                 subtype=0;
-                (void) sscanf(transfer_syntax+17,".%d.%d",&type,&subtype);
+                if (strlen(transfer_syntax) > 17)
+                  {
+                    count=(ssize_t) sscanf(transfer_syntax+17,".%d.%d",&type,
+                      &subtype);
+                    if (count < 1)
+                      ThrowDCMException(CorruptImageError,
+                        "ImproperImageHeader");
+                  }
                 switch (type)
                 {
                   case 1:
@@ -3177,7 +3396,7 @@ static Image *ReadDCMImage(const ImageInfo *image_info,ExceptionInfo *exception)
             /*
               Samples per pixel.
             */
-            samples_per_pixel=datum;
+            info.samples_per_pixel=(size_t) datum;
             break;
           }
           case 0x0004:
@@ -3185,10 +3404,12 @@ static Image *ReadDCMImage(const ImageInfo *image_info,ExceptionInfo *exception)
             /*
               Photometric interpretation.
             */
-            for (i=0; i < (ssize_t) MagickMin(length,MaxTextExtent-1); i++)
+            if (data == (unsigned char *) NULL)
+              break;
+            for (i=0; i < (ssize_t) MagickMin(length,MagickPathExtent-1); i++)
               photometric[i]=(char) data[i];
             photometric[i]='\0';
-            polarity=LocaleCompare(photometric,"MONOCHROME1 ") == 0 ?
+            info.polarity=LocaleCompare(photometric,"MONOCHROME1 ") == 0 ?
               MagickTrue : MagickFalse;
             break;
           }
@@ -3206,6 +3427,8 @@ static Image *ReadDCMImage(const ImageInfo *image_info,ExceptionInfo *exception)
             /*
               Number of frames.
             */
+            if (data == (unsigned char *) NULL)
+              break;
             number_scenes=StringToUnsignedLong((char *) data);
             break;
           }
@@ -3214,7 +3437,7 @@ static Image *ReadDCMImage(const ImageInfo *image_info,ExceptionInfo *exception)
             /*
               Image rows.
             */
-            height=datum;
+            height=(size_t) datum;
             break;
           }
           case 0x0011:
@@ -3222,7 +3445,7 @@ static Image *ReadDCMImage(const ImageInfo *image_info,ExceptionInfo *exception)
             /*
               Image columns.
             */
-            width=datum;
+            width=(size_t) datum;
             break;
           }
           case 0x0100:
@@ -3230,14 +3453,15 @@ static Image *ReadDCMImage(const ImageInfo *image_info,ExceptionInfo *exception)
             /*
               Bits allocated.
             */
-            bits_allocated=datum;
-            bytes_per_pixel=1;
+            info.bits_allocated=(size_t) datum;
+            info.bytes_per_pixel=1;
             if (datum > 8)
-              bytes_per_pixel=2;
-            depth=bits_allocated;
-            if (depth > 32)
-              ThrowReaderException(CorruptImageError,"ImproperImageHeader");
-            max_value=(1UL << bits_allocated)-1;
+              info.bytes_per_pixel=2;
+            info.depth=info.bits_allocated;
+            if (info.depth > 32)
+              ThrowDCMException(CorruptImageError,"ImproperImageHeader");
+            info.max_value=(1UL << info.bits_allocated)-1;
+            image->depth=info.depth;
             break;
           }
           case 0x0101:
@@ -3245,15 +3469,16 @@ static Image *ReadDCMImage(const ImageInfo *image_info,ExceptionInfo *exception)
             /*
               Bits stored.
             */
-            significant_bits=datum;
-            bytes_per_pixel=1;
-            if (significant_bits > 8)
-              bytes_per_pixel=2;
-            depth=significant_bits;
-            if (depth > 32)
-              ThrowReaderException(CorruptImageError,"ImproperImageHeader");
-            max_value=(1UL << significant_bits)-1;
-            mask=(size_t) GetQuantumRange(significant_bits);
+            info.significant_bits=(size_t) datum;
+            info.bytes_per_pixel=1;
+            if (info.significant_bits > 8)
+              info.bytes_per_pixel=2;
+            info.depth=info.significant_bits;
+            if (info.depth > 32)
+              ThrowDCMException(CorruptImageError,"ImproperImageHeader");
+            info.max_value=(1UL << info.significant_bits)-1;
+            info.mask=(size_t) GetQuantumRange(info.significant_bits);
+            image->depth=info.depth;
             break;
           }
           case 0x0102:
@@ -3268,7 +3493,7 @@ static Image *ReadDCMImage(const ImageInfo *image_info,ExceptionInfo *exception)
             /*
               Pixel representation.
             */
-            signed_data=datum;
+            info.signed_data=(size_t) datum;
             break;
           }
           case 0x1050:
@@ -3277,7 +3502,7 @@ static Image *ReadDCMImage(const ImageInfo *image_info,ExceptionInfo *exception)
               Visible pixel range: center.
             */
             if (data != (unsigned char *) NULL)
-              window_center=StringToLong((char *) data);
+              info.window_center=StringToDouble((char *) data, (char **) NULL);
             break;
           }
           case 0x1051:
@@ -3286,7 +3511,26 @@ static Image *ReadDCMImage(const ImageInfo *image_info,ExceptionInfo *exception)
               Visible pixel range: width.
             */
             if (data != (unsigned char *) NULL)
-              window_width=StringToUnsignedLong((char *) data);
+              info.window_width=StringToDouble((char *) data, (char **) NULL);
+            break;
+          }
+          case 0x1052:
+          {
+            /*
+              Rescale intercept
+            */
+            if (data != (unsigned char *) NULL)
+              info.rescale_intercept=StringToDouble((char *) data,
+                (char **) NULL);
+            break;
+          }
+          case 0x1053:
+          {
+            /*
+              Rescale slope
+            */
+            if (data != (unsigned char *) NULL)
+              info.rescale_slope=StringToDouble((char *) data, (char **) NULL);
             break;
           }
           case 0x1200:
@@ -3297,14 +3541,14 @@ static Image *ReadDCMImage(const ImageInfo *image_info,ExceptionInfo *exception)
             */
             if (data == (unsigned char *) NULL)
               break;
-            colors=(size_t) (length/bytes_per_pixel);
+            colors=(size_t) (length/info.bytes_per_pixel);
             datum=(int) colors;
             graymap=(int *) AcquireQuantumMemory((size_t) colors,
               sizeof(*graymap));
             if (graymap == (int *) NULL)
-              ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
+              ThrowDCMException(ResourceLimitError,"MemoryAllocationFailed");
             for (i=0; i < (ssize_t) colors; i++)
-              if (bytes_per_pixel == 1)
+              if (info.bytes_per_pixel == 1)
                 graymap[i]=(int) data[i];
               else
                 graymap[i]=(int) ((short *) data)[i];
@@ -3325,7 +3569,7 @@ static Image *ReadDCMImage(const ImageInfo *image_info,ExceptionInfo *exception)
             redmap=(int *) AcquireQuantumMemory((size_t) colors,
               sizeof(*redmap));
             if (redmap == (int *) NULL)
-              ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
+              ThrowDCMException(ResourceLimitError,"MemoryAllocationFailed");
             p=data;
             for (i=0; i < (ssize_t) colors; i++)
             {
@@ -3353,7 +3597,7 @@ static Image *ReadDCMImage(const ImageInfo *image_info,ExceptionInfo *exception)
             greenmap=(int *) AcquireQuantumMemory((size_t) colors,
               sizeof(*greenmap));
             if (greenmap == (int *) NULL)
-              ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
+              ThrowDCMException(ResourceLimitError,"MemoryAllocationFailed");
             p=data;
             for (i=0; i < (ssize_t) colors; i++)
             {
@@ -3381,12 +3625,13 @@ static Image *ReadDCMImage(const ImageInfo *image_info,ExceptionInfo *exception)
             bluemap=(int *) AcquireQuantumMemory((size_t) colors,
               sizeof(*bluemap));
             if (bluemap == (int *) NULL)
-              ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
+              ThrowDCMException(ResourceLimitError,"MemoryAllocationFailed");
             p=data;
             for (i=0; i < (ssize_t) colors; i++)
             {
               if (image->endian == MSBEndian)
-                index=(unsigned short) ((*p << 8) | *(p+1)); else
+                index=(unsigned short) ((*p << 8) | *(p+1));
+              else
                 index=(unsigned short) (*p | (*(p+1) << 8));
               bluemap[i]=(int) index;
               p+=2;
@@ -3405,8 +3650,8 @@ static Image *ReadDCMImage(const ImageInfo *image_info,ExceptionInfo *exception)
           case 0x0020:
           {
             if ((data != (unsigned char *) NULL) &&
-                (strncmp((char*) data,"INVERSE", 7) == 0))
-              polarity=MagickTrue;
+                (strncmp((char *) data,"INVERSE",7) == 0))
+              info.polarity=MagickTrue;
             break;
           }
           default:
@@ -3423,8 +3668,8 @@ static Image *ReadDCMImage(const ImageInfo *image_info,ExceptionInfo *exception)
           *attribute;
 
         for (i=0; dicom_info[i].description != (char *) NULL; i++)
-          if ((group == (ssize_t) dicom_info[i].group) &&
-              (element == (ssize_t) dicom_info[i].element))
+          if ((group == dicom_info[i].group) &&
+              (element == dicom_info[i].element))
             break;
         if (dicom_info[i].description != (char *) NULL)
           {
@@ -3436,7 +3681,8 @@ 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,exception);
+                (void) SetImageProperty(image,attribute,(char *) data,
+                  exception);
               }
             attribute=DestroyString(attribute);
           }
@@ -3482,11 +3728,11 @@ static Image *ReadDCMImage(const ImageInfo *image_info,ExceptionInfo *exception)
       }
   }
   if ((width == 0) || (height == 0))
-    ThrowReaderException(CorruptImageError,"ImproperImageHeader");
+    ThrowDCMException(CorruptImageError,"ImproperImageHeader");
   image->columns=(size_t) width;
   image->rows=(size_t) height;
-  if (signed_data == 0xffff)
-    signed_data=(size_t) (significant_bits == 16 ? 1 : 0);
+  if (info.signed_data == 0xffff)
+    info.signed_data=(size_t) (info.significant_bits == 16 ? 1 : 0);
   if ((image->compression == JPEGCompression) ||
       (image->compression == JPEG2000Compression))
     {
@@ -3499,32 +3745,22 @@ static Image *ReadDCMImage(const ImageInfo *image_info,ExceptionInfo *exception)
       int
         c;
 
-      size_t
-        length;
-
-      unsigned int
-        tag;
-
       /*
         Read offset table.
       */
       for (i=0; i < (ssize_t) stream_info->remaining; i++)
         (void) ReadBlobByte(image);
-      tag=(ReadBlobLSBShort(image) << 16) | ReadBlobLSBShort(image);
-      (void) tag;
+      (void)((ReadBlobLSBShort(image) << 16) | ReadBlobLSBShort(image));
       length=(size_t) ReadBlobLSBLong(image);
       stream_info->offset_count=length >> 2;
       if (stream_info->offset_count != 0)
         {
-          MagickOffsetType
-            offset;
-
           stream_info->offsets=(ssize_t *) AcquireQuantumMemory(
             stream_info->offset_count,sizeof(*stream_info->offsets));
           if (stream_info->offsets == (ssize_t *) NULL)
-            ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
+            ThrowDCMException(ResourceLimitError,"MemoryAllocationFailed");
           for (i=0; i < (ssize_t) stream_info->offset_count; i++)
-            stream_info->offsets[i]=(int) ReadBlobLSBLong(image);
+            stream_info->offsets[i]=(ssize_t) ReadBlobLSBSignedLong(image);
           offset=TellBlob(image);
           for (i=0; i < (ssize_t) stream_info->offset_count; i++)
             stream_info->offsets[i]+=offset;
@@ -3538,7 +3774,7 @@ static Image *ReadDCMImage(const ImageInfo *image_info,ExceptionInfo *exception)
       for (scene=0; scene < (ssize_t) number_scenes; scene++)
       {
         char
-          filename[MaxTextExtent];
+          filename[MagickPathExtent];
 
         const char
           *property;
@@ -3555,41 +3791,39 @@ static Image *ReadDCMImage(const ImageInfo *image_info,ExceptionInfo *exception)
         unsigned int
           tag;
 
+        tag=(ReadBlobLSBShort(image) << 16) | ReadBlobLSBShort(image);
+        length=(size_t) ReadBlobLSBLong(image);
+        if (tag == 0xFFFEE0DD)
+          break; /* sequence delimiter tag */
+        if (tag != 0xFFFEE000)
+          ThrowDCMException(CorruptImageError,"ImproperImageHeader");
         file=(FILE *) NULL;
         unique_file=AcquireUniqueFileResource(filename);
         if (unique_file != -1)
           file=fdopen(unique_file,"wb");
-        if ((unique_file == -1) || (file == (FILE *) NULL))
+        if (file == (FILE *) NULL)
           {
+            (void) RelinquishUniqueFileResource(filename);
             ThrowFileException(exception,FileOpenError,
               "UnableToCreateTemporaryFile",filename);
             break;
           }
-        tag=(ReadBlobLSBShort(image) << 16) | ReadBlobLSBShort(image);
-        length=(size_t) ReadBlobLSBLong(image);
-        if (tag == 0xFFFEE0DD)
-          {
-            (void) fclose(file);
-            break; /* sequence delimiter tag */
-          }
-        if (tag != 0xFFFEE000)
-          {
-            (void) fclose(file);
-            ThrowReaderException(CorruptImageError,"ImproperImageHeader");
-          }
         for ( ; length != 0; length--)
         {
           c=ReadBlobByte(image);
           if (c == EOF)
-            ThrowFileException(exception,CorruptImageError,
-              "UnexpectedEndOfFile",image->filename);
+            {
+              ThrowFileException(exception,CorruptImageError,
+                "UnexpectedEndOfFile",image->filename);
+              break;
+            }
           (void) fputc(c,file);
         }
         (void) fclose(file);
-        (void) FormatLocaleString(read_info->filename,MaxTextExtent,
+        (void) FormatLocaleString(read_info->filename,MagickPathExtent,
           "jpeg:%s",filename);
         if (image->compression == JPEG2000Compression)
-          (void) FormatLocaleString(read_info->filename,MaxTextExtent,
+          (void) FormatLocaleString(read_info->filename,MagickPathExtent,
             "j2k:%s",filename);
         jpeg_image=ReadImage(read_info,exception);
         if (jpeg_image != (Image *) NULL)
@@ -3610,30 +3844,24 @@ static Image *ReadDCMImage(const ImageInfo *image_info,ExceptionInfo *exception)
       image=DestroyImage(image);
       return(GetFirstImageInList(images));
     }
-  if (depth != (1UL*MAGICKCORE_QUANTUM_DEPTH))
+  if (info.depth != (1UL*MAGICKCORE_QUANTUM_DEPTH))
     {
       QuantumAny
         range;
 
-      size_t
-        length;
-
       /*
         Compute pixel scaling table.
       */
-      length=(size_t) (GetQuantumRange(depth)+1);
-      scale=(Quantum *) AcquireQuantumMemory(length,sizeof(*scale));
-      if (scale == (Quantum *) NULL)
-        ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
-      range=GetQuantumRange(depth);
-      for (i=0; i < (ssize_t) (GetQuantumRange(depth)+1); i++)
-        scale[i]=ScaleAnyToQuantum((size_t) i,range);
+      length=(size_t) (GetQuantumRange(info.depth)+1);
+      info.scale=(Quantum *) AcquireQuantumMemory(length,sizeof(*info.scale));
+      if (info.scale == (Quantum *) NULL)
+        ThrowDCMException(ResourceLimitError,"MemoryAllocationFailed");
+      range=GetQuantumRange(info.depth);
+      for (i=0; i <= (ssize_t) GetQuantumRange(info.depth); i++)
+        info.scale[i]=ScaleAnyToQuantum((size_t) i,range);
     }
   if (image->compression == RLECompression)
     {
-      size_t
-        length;
-
       unsigned int
         tag;
 
@@ -3648,16 +3876,13 @@ static Image *ReadDCMImage(const ImageInfo *image_info,ExceptionInfo *exception)
       stream_info->offset_count=length >> 2;
       if (stream_info->offset_count != 0)
         {
-          MagickOffsetType
-            offset;
-
           stream_info->offsets=(ssize_t *) AcquireQuantumMemory(
             stream_info->offset_count,sizeof(*stream_info->offsets));
           if (stream_info->offsets == (ssize_t *) NULL)
-            ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
+            ThrowDCMException(ResourceLimitError,"MemoryAllocationFailed");
           for (i=0; i < (ssize_t) stream_info->offset_count; i++)
-            stream_info->offsets[i]=(int) ReadBlobLSBLong(image);
-          offset=TellBlob(image);
+            stream_info->offsets[i]=(ssize_t) ReadBlobLSBSignedLong(image);
+          offset=TellBlob(image)+8;
           for (i=0; i < (ssize_t) stream_info->offset_count; i++)
             stream_info->offsets[i]+=offset;
         }
@@ -3668,51 +3893,62 @@ static Image *ReadDCMImage(const ImageInfo *image_info,ExceptionInfo *exception)
       break;
     image->columns=(size_t) width;
     image->rows=(size_t) height;
-    image->depth=depth;
+    image->depth=info.depth;
+    status=SetImageExtent(image,image->columns,image->rows,exception);
+    if (status == MagickFalse)
+      break;
     image->colorspace=RGBColorspace;
-    if ((image->colormap == (PixelInfo *) NULL) && (samples_per_pixel == 1))
+    if ((image->colormap == (PixelInfo *) NULL) &&
+        (info.samples_per_pixel == 1))
       {
+        int
+          index;
+
         size_t
           one;
 
         one=1;
         if (colors == 0)
-          colors=one << depth;
-        if (AcquireImageColormap(image,one << depth,exception) == MagickFalse)
-          ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
+          colors=one << info.depth;
+        if (AcquireImageColormap(image,colors,exception) == MagickFalse)
+          ThrowDCMException(ResourceLimitError,"MemoryAllocationFailed");
         if (redmap != (int *) NULL)
           for (i=0; i < (ssize_t) colors; i++)
           {
             index=redmap[i];
-            if ((scale != (Quantum *) NULL) && (index <= (int) max_value))
-              index=(int) scale[index];
-            image->colormap[i].red=index;
+            if ((info.scale != (Quantum *) NULL) &&
+                (index <= (int) info.max_value))
+              index=(int) info.scale[index];
+            image->colormap[i].red=(MagickRealType) index;
           }
         if (greenmap != (int *) NULL)
           for (i=0; i < (ssize_t) colors; i++)
           {
             index=greenmap[i];
-            if ((scale != (Quantum *) NULL) && (index <= (int) max_value))
-              index=(int) scale[index];
-            image->colormap[i].green=index;
+            if ((info.scale != (Quantum *) NULL) &&
+                (index <= (int) info.max_value))
+              index=(int) info.scale[index];
+            image->colormap[i].green=(MagickRealType) index;
           }
         if (bluemap != (int *) NULL)
           for (i=0; i < (ssize_t) colors; i++)
           {
             index=bluemap[i];
-            if ((scale != (Quantum *) NULL) && (index <= (int) max_value))
-              index=(int) scale[index];
-            image->colormap[i].blue=index;
+            if ((info.scale != (Quantum *) NULL) &&
+                (index <= (int) info.max_value))
+              index=(int) info.scale[index];
+            image->colormap[i].blue=(MagickRealType) index;
           }
         if (graymap != (int *) NULL)
           for (i=0; i < (ssize_t) colors; i++)
           {
             index=graymap[i];
-            if ((scale != (Quantum *) NULL) && (index <= (int) max_value))
-              index=(int) scale[index];
-            image->colormap[i].red=index;
-            image->colormap[i].green=index;
-            image->colormap[i].blue=index;
+            if ((info.scale != (Quantum *) NULL) &&
+                (index <= (int) info.max_value))
+              index=(int) info.scale[index];
+            image->colormap[i].red=(MagickRealType) index;
+            image->colormap[i].green=(MagickRealType) index;
+            image->colormap[i].blue=(MagickRealType) index;
           }
       }
     if (image->compression == RLECompression)
@@ -3729,24 +3965,36 @@ static Image *ReadDCMImage(const ImageInfo *image_info,ExceptionInfo *exception)
         stream_info->remaining=(size_t) ReadBlobLSBLong(image);
         if ((tag != 0xFFFEE000) || (stream_info->remaining <= 64) ||
             (EOFBlob(image) != MagickFalse))
-          ThrowReaderException(CorruptImageError,"ImproperImageHeader");
+          ThrowDCMException(CorruptImageError,"ImproperImageHeader");
         stream_info->count=0;
         stream_info->segment_count=ReadBlobLSBLong(image);
+        for (i=0; i < 15; i++)
+          stream_info->segments[i]=(ssize_t) ReadBlobLSBSignedLong(image);
+        stream_info->remaining-=64;
         if (stream_info->segment_count > 1)
           {
-            bytes_per_pixel=1;
-            depth=8;
+            info.bytes_per_pixel=1;
+            info.depth=8;
+            if (stream_info->offset_count > 0)
+              (void) SeekBlob(image,(MagickOffsetType) stream_info->offsets[0]+
+                stream_info->segments[0],SEEK_SET);
           }
-        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))
+    if ((info.samples_per_pixel > 1) && (image->interlace == PlaneInterlace))
       {
+        register ssize_t
+          x;
+
+        register Quantum
+          *q;
+
+        ssize_t
+          y;
+
         /*
           Convert Planar RGB DCM Medical image to pixel packets.
         */
-        for (i=0; i < (ssize_t) samples_per_pixel; i++)
+        for (i=0; i < (ssize_t) info.samples_per_pixel; i++)
         {
           for (y=0; y < (ssize_t) image->rows; y++)
           {
@@ -3803,289 +4051,46 @@ static Image *ReadDCMImage(const ImageInfo *image_info,ExceptionInfo *exception)
         const char
           *option;
 
-        int
-          byte;
-
-        PixelPacket
-          pixel;
-
         /*
           Convert DCM Medical image to pixel packets.
         */
-        byte=0;
-        i=0;
-        if ((window_center != 0) && (window_width == 0))
-          window_width=(size_t) window_center;
         option=GetImageOption(image_info,"dcm:display-range");
         if (option != (const char *) NULL)
           {
             if (LocaleCompare(option,"reset") == 0)
-              window_width=0;
+              info.window_width=0;
           }
-        (void) ResetMagickMemory(&pixel,0,sizeof(pixel));
-        for (y=0; y < (ssize_t) image->rows; y++)
-        {
-          q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
-          if (q == (Quantum *) NULL)
-            break;
-          for (x=0; x < (ssize_t) image->columns; x++)
+        option=GetImageOption(image_info,"dcm:window");
+        if (option != (char *) NULL)
           {
-            if (samples_per_pixel == 1)
-              {
-                int
-                  pixel_value;
+            GeometryInfo
+              geometry_info;
 
-                if (bytes_per_pixel == 1)
-                  pixel_value=polarity != MagickFalse ?
-                    ((int) max_value-ReadDCMByte(stream_info,image)) :
-                    ReadDCMByte(stream_info,image);
-                else
-                  if ((bits_allocated != 12) || (significant_bits != 12))
-                    {
-                      if (image->endian == MSBEndian)
-                        pixel_value=(int) (polarity != MagickFalse ? (max_value-
-                          ReadDCMMSBShort(stream_info,image)) :
-                          ReadDCMMSBShort(stream_info,image));
-                      else
-                        pixel_value=(int) (polarity != MagickFalse ? (max_value-
-                          ReadDCMLSBShort(stream_info,image)) :
-                          ReadDCMLSBShort(stream_info,image));
-                    }
-                  else
-                    {
-                      if ((i & 0x01) != 0)
-                        pixel_value=(ReadDCMByte(stream_info,image) << 8) |
-                          byte;
-                      else
-                        {
-                          if (image->endian == MSBEndian)
-                            pixel_value=(int) ReadDCMMSBShort(stream_info,
-                              image);
-                          else
-                            pixel_value=(int) ReadDCMLSBShort(stream_info,
-                              image);
-                          byte=(int) (pixel_value & 0x0f);
-                          pixel_value>>=4;
-                        }
-                      i++;
-                    }
-                index=pixel_value;
-                if (window_width == 0)
-                  {
-                    if (signed_data == 1)
-                      index=pixel_value-32767;
-                  }
-                else
-                  {
-                    ssize_t
-                      window_max,
-                      window_min;
+            MagickStatusType
+              flags;
 
-                    window_min=(ssize_t) ceil(window_center-(window_width-1)/
-                      2.0-0.5);
-                    window_max=(ssize_t) floor(window_center+(window_width-1)/
-                      2.0+0.5);
-                    if ((ssize_t) pixel_value <= window_min)
-                      index=0;
-                    else
-                      if ((ssize_t) pixel_value > window_max)
-                        index=(int) max_value;
-                      else
-                        index=(int) (max_value*(((pixel_value-window_center-
-                          0.5)/(window_width-1))+0.5));
-                  }
-                index&=mask;
-                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;
-              }
-            else
-              {
-                if (bytes_per_pixel == 1)
-                  {
-                    pixel.red=(size_t) ReadDCMByte(stream_info,image);
-                    pixel.green=(size_t) ReadDCMByte(stream_info,image);
-                    pixel.blue=(size_t) ReadDCMByte(stream_info,image);
-                  }
-                else
-                  {
-                    if (image->endian == MSBEndian)
-                      {
-                        pixel.red=ReadDCMMSBShort(stream_info,image);
-                        pixel.green=ReadDCMMSBShort(stream_info,image);
-                        pixel.blue=ReadDCMMSBShort(stream_info,image);
-                      }
-                    else
-                      {
-                        pixel.red=ReadDCMLSBShort(stream_info,image);
-                        pixel.green=ReadDCMLSBShort(stream_info,image);
-                        pixel.blue=ReadDCMLSBShort(stream_info,image);
-                      }
-                  }
-                pixel.red&=mask;
-                pixel.green&=mask;
-                pixel.blue&=mask;
-                if (scale != (Quantum *) NULL)
-                  {
-                    pixel.red=scale[pixel.red];
-                    pixel.green=scale[pixel.green];
-                    pixel.blue=scale[pixel.blue];
-                  }
-              }
-            SetPixelRed(image,pixel.red,q);
-            SetPixelGreen(image,pixel.green,q);
-            SetPixelBlue(image,pixel.blue,q);
-            q+=GetPixelChannels(image);
+            flags=ParseGeometry(option,&geometry_info);
+            if (flags & RhoValue)
+              info.window_center=geometry_info.rho;
+            if (flags & SigmaValue)
+              info.window_width=geometry_info.sigma;
+            info.rescale=MagickTrue;
           }
-          if (SyncAuthenticPixels(image,exception) == MagickFalse)
-            break;
-          if (image->previous == (Image *) NULL)
-            {
-              status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
-                image->rows);
-              if (status == MagickFalse)
-                break;
-            }
-        }
-        if (stream_info->segment_count > 1)
-          for (y=0; y < (ssize_t) image->rows; y++)
+        option=GetImageOption(image_info,"dcm:rescale");
+        if (option != (char *) NULL)
+          info.rescale=IsStringTrue(option);
+        if ((info.window_center != 0) && (info.window_width == 0))
+          info.window_width=info.window_center;
+        status=ReadDCMPixels(image,&info,stream_info,MagickTrue,exception);
+        if ((status != MagickFalse) && (stream_info->segment_count > 1))
           {
-            q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
-            if (q == (Quantum *) NULL)
-              break;
-            for (x=0; x < (ssize_t) image->columns; x++)
-            {
-              if (samples_per_pixel == 1)
-                {
-                  int
-                    pixel_value;
-
-                  if (bytes_per_pixel == 1)
-                    pixel_value=polarity != MagickFalse ?
-                      ((int) max_value-ReadDCMByte(stream_info,image)) :
-                      ReadDCMByte(stream_info,image);
-                  else
-                    if ((bits_allocated != 12) || (significant_bits != 12))
-                      {
-                        if (image->endian == MSBEndian)
-                          pixel_value=(int) (polarity != MagickFalse ?
-                            (max_value-ReadDCMMSBShort(stream_info,image)) :
-                            ReadDCMMSBShort(stream_info,image));
-                        else
-                          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
-                      {
-                        if ((i & 0x01) != 0)
-                          pixel_value=(ReadDCMByte(stream_info,image) << 8) |
-                            byte;
-                        else
-                          {
-                            if (image->endian == MSBEndian)
-                              pixel_value=(int) ReadDCMMSBShort(stream_info,
-                                image);
-                            else
-                              pixel_value=(int) ReadDCMLSBShort(stream_info,
-                                image);
-                            byte=(int) (pixel_value & 0x0f);
-                            pixel_value>>=4;
-                          }
-                        i++;
-                      }
-                  index=pixel_value;
-                  if (window_width == 0)
-                    {
-                      if (signed_data == 1)
-                        index=pixel_value-32767;
-                    }
-                  else
-                    {
-                      ssize_t
-                        window_max,
-                        window_min;
-
-                      window_min=(ssize_t) ceil(window_center-(window_width-1)/
-                        2.0-0.5);
-                      window_max=(ssize_t) floor(window_center+(window_width-1)/
-                        2.0+0.5);
-                      if ((ssize_t) pixel_value <= window_min)
-                        index=0;
-                      else
-                        if ((ssize_t) pixel_value > window_max)
-                          index=(int) max_value;
-                        else
-                          index=(int) (max_value*(((pixel_value-window_center-
-                            0.5)/(window_width-1))+0.5));
-                    }
-                  index&=mask;
-                  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;
-                }
-              else
-                {
-                  if (bytes_per_pixel == 1)
-                    {
-                      pixel.red=(size_t) ReadDCMByte(stream_info,image);
-                      pixel.green=(size_t) ReadDCMByte(stream_info,image);
-                      pixel.blue=(size_t) ReadDCMByte(stream_info,image);
-                    }
-                  else
-                    {
-                      if (image->endian == MSBEndian)
-                        {
-                          pixel.red=ReadDCMMSBShort(stream_info,image);
-                          pixel.green=ReadDCMMSBShort(stream_info,image);
-                          pixel.blue=ReadDCMMSBShort(stream_info,image);
-                        }
-                      else
-                        {
-                          pixel.red=ReadDCMLSBShort(stream_info,image);
-                          pixel.green=ReadDCMLSBShort(stream_info,image);
-                          pixel.blue=ReadDCMLSBShort(stream_info,image);
-                        }
-                    }
-                  pixel.red&=mask;
-                  pixel.green&=mask;
-                  pixel.blue&=mask;
-                  if (scale != (Quantum *) NULL)
-                    {
-                      pixel.red=scale[pixel.red];
-                      pixel.green=scale[pixel.green];
-                      pixel.blue=scale[pixel.blue];
-                    }
-                }
-              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;
-            if (image->previous == (Image *) NULL)
-              {
-                status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
-                  image->rows);
-                if (status == MagickFalse)
-                  break;
-              }
+            if (stream_info->offset_count > 0)
+              (void) SeekBlob(image,(MagickOffsetType) stream_info->offsets[0]+
+                stream_info->segments[1],SEEK_SET);
+            (void) ReadDCMPixels(image,&info,stream_info,MagickFalse,exception);
           }
       }
-    if (IsImageGray(image,exception) != MagickFalse)
+    if (SetImageGray(image,exception) != MagickFalse)
       (void) SetImageColorspace(image,GRAYColorspace,exception);
     if (EOFBlob(image) != MagickFalse)
       {
@@ -4124,8 +4129,8 @@ static Image *ReadDCMImage(const ImageInfo *image_info,ExceptionInfo *exception)
     stream_info->offsets=(ssize_t *)
       RelinquishMagickMemory(stream_info->offsets);
   stream_info=(DCMStreamInfo *) RelinquishMagickMemory(stream_info);
-  if (scale != (Quantum *) NULL)
-    scale=(Quantum *) RelinquishMagickMemory(scale);
+  if (info.scale != (Quantum *) NULL)
+    info.scale=(Quantum *) RelinquishMagickMemory(info.scale);
   if (graymap != (int *) NULL)
     graymap=(int *) RelinquishMagickMemory(graymap);
   if (bluemap != (int *) NULL)
@@ -4176,15 +4181,13 @@ ModuleExport size_t RegisterDCMImage(void)
       "and supplement 61 which adds JPEG-2000 encoding."
     };
 
-  entry=SetMagickInfo("DCM");
+  entry=AcquireMagickInfo("DCM","DCM",
+    "Digital Imaging and Communications in Medicine image");
   entry->decoder=(DecodeImageHandler *) ReadDCMImage;
   entry->magick=(IsImageFormatHandler *) IsDCM;
-  entry->adjoin=MagickFalse;
-  entry->seekable_stream=MagickTrue;
-  entry->description=ConstantString(
-    "Digital Imaging and Communications in Medicine image");
+  entry->flags^=CoderAdjoinFlag;
+  entry->flags|=CoderDecoderSeekableStreamFlag;
   entry->note=ConstantString(DCMNote);
-  entry->module=ConstantString("DCM");
   (void) RegisterMagickInfo(entry);
   return(MagickImageCoderSignature);
 }