% July 1992 %
% %
% %
-% Copyright 1999-2016 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, %
max_value,
samples_per_pixel,
signed_data,
- significant_bits,
- window_width;
+ significant_bits;
- ssize_t
+ MagickBooleanType
+ rescale;
+
+ double
rescale_intercept,
rescale_slope,
- window_center;
+ window_center,
+ window_width;
} DCMInfo;
typedef struct _DCMStreamInfo
if (info->signed_data)
pixel_value=ReadDCMSignedShort(stream_info,image);
else
- pixel_value=ReadDCMShort(stream_info,image);
+ pixel_value=(int) ReadDCMShort(stream_info,image);
if (info->polarity != MagickFalse)
pixel_value=(int)info->max_value-pixel_value;
}
}
i++;
}
- index=(pixel_value*info->rescale_slope)+info->rescale_intercept;
- if (info->window_width == 0)
+ if (info->signed_data == 1)
+ pixel_value-=32767;
+ index=pixel_value;
+ if (info->rescale != MagickFalse)
{
- if (info->signed_data == 1)
- index-=32767;
- }
- else
- {
- ssize_t
- window_max,
- window_min;
+ double
+ scaled_value;
- window_min=(ssize_t) ceil((double) info->window_center-
- (info->window_width-1.0)/2.0-0.5);
- window_max=(ssize_t) floor((double) info->window_center+
- (info->window_width-1.0)/2.0+0.5);
- if ((ssize_t)index <= window_min)
- index=0;
- else
- if ((ssize_t)index > window_max)
- index=(int) info->max_value;
- else
- index=(int) (info->max_value*(((index-info->window_center-
- 0.5)/(info->window_width-1))+0.5));
+ 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,(size_t) index,exception);
+ index=(int) ConstrainColormapIndex(image,(ssize_t) index,exception);
if (first_segment)
SetPixelIndex(image,(Quantum) index,q);
else
pixel.blue&=info->mask;
if (info->scale != (Quantum *) NULL)
{
- if (pixel.red <= GetQuantumRange(info->depth))
+ if ((MagickSizeType) pixel.red <= GetQuantumRange(info->depth))
pixel.red=info->scale[pixel.red];
- if (pixel.green <= GetQuantumRange(info->depth))
+ if ((MagickSizeType) pixel.green <= GetQuantumRange(info->depth))
pixel.green=info->scale[pixel.green];
- if (pixel.blue <= GetQuantumRange(info->depth))
+ if ((MagickSizeType) pixel.blue <= GetQuantumRange(info->depth))
pixel.blue=info->scale[pixel.blue];
}
}
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[MagickPathExtent],
implicit_vr[MagickPathExtent],
/*
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 ",MagickPathExtent);
+ info.polarity=MagickFalse;
+ info.scale=(Quantum *) NULL;
info.bits_allocated=8;
info.bytes_per_pixel=1;
info.depth=8;
- info.max_value=255UL;
info.mask=0xffff;
- info.polarity=MagickFalse;
- info.rescale_intercept=0;
- info.rescale_slope=1;
+ info.max_value=255UL;
info.samples_per_pixel=1;
- info.scale=(Quantum *) NULL;
info.signed_data=(~0UL);
info.significant_bits=0;
- info.window_center=0;
- info.window_width=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;
element=0;
explicit_vr[2]='\0';
(void) CopyMagickString(implicit_vr,dicom_info[i].vr,MagickPathExtent);
count=ReadBlob(image,2,(unsigned char *) explicit_vr);
if (count != 2)
- ThrowReaderException(CorruptImageError,"ImproperImageHeader");
+ ThrowDCMException(CorruptImageError,"ImproperImageHeader");
/*
Check for "explicitness", but meta-file headers always explicit.
*/
{
offset=SeekBlob(image,(MagickOffsetType) -2,SEEK_CUR);
if (offset < 0)
- ThrowReaderException(CorruptImageError,"ImproperImageHeader");
+ ThrowDCMException(CorruptImageError,"ImproperImageHeader");
quantum=4;
}
else
else
if ((quantum != 0) && (length != 0))
{
+ 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))
{
(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';
sequence=MagickTrue;
continue;
}
-
if ((unsigned int) ((group << 16) | element) == 0xFFFEE0DD)
{
if (data != (unsigned char *) NULL)
sequence=MagickFalse;
continue;
}
-
if (sequence != MagickFalse)
{
if (data != (unsigned char *) NULL)
data=(unsigned char *) RelinquishMagickMemory(data);
continue;
}
-
switch (group)
{
case 0x0002:
count=(ssize_t) sscanf(transfer_syntax+17,".%d.%d",&type,
&subtype);
if (count < 1)
- ThrowReaderException(CorruptImageError,
+ ThrowDCMException(CorruptImageError,
"ImproperImageHeader");
}
switch (type)
info.bytes_per_pixel=2;
info.depth=info.bits_allocated;
if (info.depth > 32)
- ThrowReaderException(CorruptImageError,"ImproperImageHeader");
+ ThrowDCMException(CorruptImageError,"ImproperImageHeader");
info.max_value=(1UL << info.bits_allocated)-1;
image->depth=info.depth;
break;
info.bytes_per_pixel=2;
info.depth=info.significant_bits;
if (info.depth > 32)
- ThrowReaderException(CorruptImageError,"ImproperImageHeader");
+ ThrowDCMException(CorruptImageError,"ImproperImageHeader");
info.max_value=(1UL << info.significant_bits)-1;
info.mask=(size_t) GetQuantumRange(info.significant_bits);
image->depth=info.depth;
Visible pixel range: center.
*/
if (data != (unsigned char *) NULL)
- info.window_center=(ssize_t) StringToLong((char *) data);
+ info.window_center=StringToDouble((char *) data, (char **) NULL);
break;
}
case 0x1051:
Visible pixel range: width.
*/
if (data != (unsigned char *) NULL)
- info.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=(ssize_t) StringToLong((char *) data);
+ info.rescale_intercept=StringToDouble((char *) data,
+ (char **) NULL);
break;
}
case 0x1053:
Rescale slope
*/
if (data != (unsigned char *) NULL)
- info.rescale_slope=(ssize_t) StringToLong((char *) data);
+ info.rescale_slope=StringToDouble((char *) data, (char **) NULL);
break;
}
case 0x1200:
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 (info.bytes_per_pixel == 1)
graymap[i]=(int) data[i];
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++)
{
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++)
{
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 ((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);
}
}
}
if ((width == 0) || (height == 0))
- ThrowReaderException(CorruptImageError,"ImproperImageHeader");
+ ThrowDCMException(CorruptImageError,"ImproperImageHeader");
image->columns=(size_t) width;
image->rows=(size_t) height;
if (info.signed_data == 0xffff)
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]=(ssize_t) ReadBlobLSBSignedLong(image);
offset=TellBlob(image);
if (tag == 0xFFFEE0DD)
break; /* sequence delimiter tag */
if (tag != 0xFFFEE000)
- ThrowReaderException(CorruptImageError,"ImproperImageHeader");
+ ThrowDCMException(CorruptImageError,"ImproperImageHeader");
file=(FILE *) NULL;
unique_file=AcquireUniqueFileResource(filename);
if (unique_file != -1)
QuantumAny
range;
- size_t
- length;
-
/*
Compute pixel scaling table.
*/
length=(size_t) (GetQuantumRange(info.depth)+1);
info.scale=(Quantum *) AcquireQuantumMemory(length,sizeof(*info.scale));
if (info.scale == (Quantum *) NULL)
- ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
+ 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;
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]=(ssize_t) ReadBlobLSBSignedLong(image);
offset=TellBlob(image)+8;
one=1;
if (colors == 0)
colors=one << info.depth;
- if (AcquireImageColormap(image,one << info.depth,exception) == MagickFalse)
- ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
+ if (AcquireImageColormap(image,colors,exception) == MagickFalse)
+ ThrowDCMException(ResourceLimitError,"MemoryAllocationFailed");
if (redmap != (int *) NULL)
for (i=0; i < (ssize_t) colors; i++)
{
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++)
info.bytes_per_pixel=1;
info.depth=8;
if (stream_info->offset_count > 0)
- SeekBlob(image,stream_info->offsets[0]+stream_info->segments[0],
- SEEK_SET);
+ (void) SeekBlob(image,(MagickOffsetType) stream_info->offsets[0]+
+ stream_info->segments[0],SEEK_SET);
}
}
if ((info.samples_per_pixel > 1) && (image->interlace == PlaneInterlace))
/*
Convert DCM Medical image to pixel packets.
*/
- if ((info.window_center != 0) && (info.window_width == 0))
- info.window_width=(size_t) info.window_center;
option=GetImageOption(image_info,"dcm:display-range");
if (option != (const char *) NULL)
{
if (LocaleCompare(option,"reset") == 0)
info.window_width=0;
}
+ option=GetImageOption(image_info,"dcm:window");
+ if (option != (char *) NULL)
+ {
+ GeometryInfo
+ geometry_info;
+
+ MagickStatusType
+ flags;
+
+ 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;
+ }
+ 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))
{
if (stream_info->offset_count > 0)
- SeekBlob(image,stream_info->offsets[0]+stream_info->segments[1],
- SEEK_SET);
- ReadDCMPixels(image,&info,stream_info,MagickFalse,exception);
+ (void) SeekBlob(image,(MagickOffsetType) stream_info->offsets[0]+
+ stream_info->segments[1],SEEK_SET);
+ (void) ReadDCMPixels(image,&info,stream_info,MagickFalse,exception);
}
}
if (SetImageGray(image,exception) != MagickFalse)
entry->decoder=(DecodeImageHandler *) ReadDCMImage;
entry->magick=(IsImageFormatHandler *) IsDCM;
entry->flags^=CoderAdjoinFlag;
- entry->flags|=CoderSeekableStreamFlag;
+ entry->flags|=CoderDecoderSeekableStreamFlag;
entry->note=ConstantString(DCMNote);
(void) RegisterMagickInfo(entry);
return(MagickImageCoderSignature);