diff --git a/coders/hdr.c b/coders/hdr.c
index 681a088..f75701a 100644
--- a/coders/hdr.c
+++ b/coders/hdr.c
@@ -10,7 +10,7 @@
% H H DDDD R R %
% %
% %
-% Read/Write Radiance HDR Image Format %
+% Read/Write Radiance RGBE Image Format %
% %
% Software Design %
% John Cristy %
@@ -53,9 +53,11 @@
#include "magick/memory_.h"
#include "magick/monitor.h"
#include "magick/monitor-private.h"
+#include "magick/property.h"
#include "magick/quantum-private.h"
#include "magick/static.h"
#include "magick/string_.h"
+#include "magick/string-private.h"
#include "magick/module.h"
/*
@@ -63,13 +65,49 @@
% %
% %
% %
+% I s H D R %
+% %
+% %
+% %
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%
+% IsHDR() returns MagickTrue if the image format type, identified by the
+% magick string, is Radiance RGBE image format.
+%
+% The format of the IsHDR method is:
+%
+% MagickBooleanType IsHDR(const unsigned char *magick,
+% const size_t length)
+%
+% A description of each parameter follows:
+%
+% o magick: compare image format pattern against these bytes.
+%
+% o length: Specifies the length of the magick string.
+%
+*/
+static MagickBooleanType IsHDR(const unsigned char *magick,
+ const size_t length)
+{
+ if (length < 10)
+ return(MagickFalse);
+ if (LocaleNCompare((const char *) magick,"#?RADIANCE",10) == 0)
+ return(MagickTrue);
+ return(MagickFalse);
+}
+
+/*
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+% %
+% %
+% %
% R e a d H D R I m a g e %
% %
% %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
-% ReadHDRImage() reads the Radiance HDR image format and returns it. It
+% ReadHDRImage() reads the Radiance RGBE image format and returns it. It
% allocates the memory necessary for the new Image structure and returns a
% pointer to the new image.
%
@@ -86,14 +124,24 @@
*/
static Image *ReadHDRImage(const ImageInfo *image_info,ExceptionInfo *exception)
{
+ char
+ format[MaxTextExtent],
+ keyword[MaxTextExtent],
+ tag[MaxTextExtent],
+ value[MaxTextExtent];
+
+ double
+ gamma;
+
Image
*image;
- MagickBooleanType
- status;
+ int
+ c;
- register ssize_t
- x;
+ MagickBooleanType
+ status,
+ value_expected;
register PixelPacket
*q;
@@ -101,16 +149,17 @@
register unsigned char
*p;
- size_t
- height,
- length,
- width;
+ register ssize_t
+ i,
+ x;
ssize_t
count,
y;
unsigned char
+ *end,
+ pixel[4],
*pixels;
/*
@@ -131,88 +180,231 @@
return((Image *) NULL);
}
/*
- Read HDR X image.
+ Decode image header.
*/
- width=ReadBlobMSBLong(image);
- height=ReadBlobMSBLong(image);
- if (EOFBlob(image) != MagickFalse)
- ThrowReaderException(CorruptImageError,"ImproperImageHeader");
- if ((width == 0UL) || (height == 0UL))
- ThrowReaderException(CorruptImageError,"ImproperImageHeader");
- do
- {
- /*
- Convert HDR raster image to pixel packets.
- */
- image->columns=width;
- image->rows=height;
- image->depth=8;
- if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
- if (image->scene >= (image_info->scene+image_info->number_scenes-1))
- break;
- pixels=(unsigned char *) AcquireQuantumMemory(image->columns,
- 4*sizeof(*pixels));
- if (pixels == (unsigned char *) NULL)
- ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
- length=(size_t) 4*image->columns;
- for (y=0; y < (ssize_t) image->rows; y++)
+ image->columns=0;
+ image->rows=0;
+ *format='\0';
+ c=ReadBlobByte(image);
+ if (c == EOF)
{
- count=ReadBlob(image,length,pixels);
- if ((size_t) count != length)
- ThrowReaderException(CorruptImageError,"UnableToReadImageData");
- p=pixels;
- q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
- if (q == (PixelPacket *) NULL)
- break;
- for (x=0; x < (ssize_t) image->columns; x++)
- {
- q->opacity=(Quantum) (QuantumRange-ScaleCharToQuantum(*p++));
- q->red=ScaleCharToQuantum(*p++);
- q->green=ScaleCharToQuantum(*p++);
- q->blue=ScaleCharToQuantum(*p++);
- if (q->opacity != OpaqueOpacity)
- image->matte=MagickTrue;
- q++;
- }
- if (SyncAuthenticPixels(image,exception) == MagickFalse)
- break;
- if ((image->previous == (Image *) NULL) &&
- (SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,image->rows) == MagickFalse))
- break;
+ image=DestroyImage(image);
+ return((Image *) NULL);
}
- pixels=(unsigned char *) RelinquishMagickMemory(pixels);
- if (EOFBlob(image) != MagickFalse)
+ while (isgraph(c) && (image->columns == 0) && (image->rows == 0))
+ {
+ if (isalnum(c) == MagickFalse)
+ c=ReadBlobByte(image);
+ else
{
- ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
- image->filename);
- break;
- }
- /*
- Proceed to next image.
- */
- if (image_info->number_scenes != 0)
- if (image->scene >= (image_info->scene+image_info->number_scenes-1))
- break;
- width=ReadBlobMSBLong(image);
- height=ReadBlobMSBLong(image);
- if ((width != 0UL) && (height != 0UL))
- {
+ register char
+ *p;
+
/*
- Allocate next image structure.
+ Determine a keyword and its value.
*/
- AcquireNextImage(image_info,image);
- if (GetNextImageInList(image) == (Image *) NULL)
+ p=keyword;
+ do
+ {
+ if ((size_t) (p-keyword) < (MaxTextExtent-1))
+ *p++=c;
+ c=ReadBlobByte(image);
+ } while (isalnum(c) || (c == '_'));
+ *p='\0';
+ value_expected=MagickFalse;
+ while ((isspace((int) ((unsigned char) c)) != 0) || (c == '='))
+ {
+ if (c == '=')
+ value_expected=MagickTrue;
+ c=ReadBlobByte(image);
+ }
+ if (LocaleCompare(keyword,"Y") == 0)
+ value_expected=MagickTrue;
+ if (value_expected == MagickFalse)
+ continue;
+ p=value;
+ while ((c != '\n') && (c != '\0'))
+ {
+ if ((size_t) (p-value) < (MaxTextExtent-1))
+ *p++=c;
+ c=ReadBlobByte(image);
+ }
+ *p='\0';
+ /*
+ Assign a value to the specified keyword.
+ */
+ switch (*keyword)
+ {
+ case 'F':
+ case 'f':
{
- image=DestroyImageList(image);
- return((Image *) NULL);
+ if (LocaleCompare(keyword,"format") == 0)
+ {
+ (void) CopyMagickString(format,value,MaxTextExtent);
+ break;
+ }
+ (void) FormatMagickString(tag,MaxTextExtent,"hdr:%s",keyword);
+ (void) SetImageProperty(image,tag,value);
+ break;
}
- image=SyncNextImageInList(image);
- status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
- GetBlobSize(image));
- if (status == MagickFalse)
- break;
+ case 'G':
+ case 'g':
+ {
+ if (LocaleCompare(keyword,"gamma") == 0)
+ {
+ image->gamma=StringToDouble(value);
+ break;
+ }
+ (void) FormatMagickString(tag,MaxTextExtent,"hdr:%s",keyword);
+ (void) SetImageProperty(image,tag,value);
+ break;
+ }
+ case 'P':
+ case 'p':
+ {
+ if (LocaleCompare(keyword,"primaries") == 0)
+ {
+ float
+ chromaticity[6],
+ white_point[2];
+
+ (void) sscanf(value,"%g %g %g %g %g %g %g %g",&chromaticity[0],
+ &chromaticity[1],&chromaticity[2],&chromaticity[3],
+ &chromaticity[4],&chromaticity[5],&white_point[0],
+ &white_point[1]);
+ image->chromaticity.red_primary.x=chromaticity[0];
+ image->chromaticity.red_primary.y=chromaticity[1];
+ image->chromaticity.green_primary.x=chromaticity[2];
+ image->chromaticity.green_primary.y=chromaticity[3];
+ image->chromaticity.blue_primary.x=chromaticity[4];
+ image->chromaticity.blue_primary.y=chromaticity[5];
+ image->chromaticity.white_point.x=white_point[0],
+ image->chromaticity.white_point.y=white_point[1];
+ break;
+ }
+ (void) FormatMagickString(tag,MaxTextExtent,"hdr:%s",keyword);
+ (void) SetImageProperty(image,tag,value);
+ break;
+ }
+ case 'Y':
+ case 'y':
+ {
+ if ((LocaleCompare(keyword,"Y") == 0) &&
+ (LocaleCompare(format,"32-bit_rle_rgbe") == 0))
+ {
+ int
+ height,
+ width;
+
+ (void) sscanf(value,"%d +X %d",&height,&width);
+ image->columns=(size_t) width;
+ image->rows=(size_t) height;
+ break;
+ }
+ (void) FormatMagickString(tag,MaxTextExtent,"hdr:%s",keyword);
+ (void) SetImageProperty(image,tag,value);
+ break;
+ }
+ default:
+ {
+ (void) FormatMagickString(tag,MaxTextExtent,"hdr:%s",keyword);
+ (void) SetImageProperty(image,tag,value);
+ break;
+ }
+ }
}
- } while ((width != 0UL) && (height != 0UL));
+ if ((image->columns == 0) && (image->rows == 0))
+ while (isspace((int) ((unsigned char) c)) != 0)
+ c=ReadBlobByte(image);
+ }
+ if (LocaleCompare(format,"32-bit_rle_rgbe") != 0)
+ ThrowReaderException(CorruptImageError,"ImproperImageHeader");
+ if ((image->columns == 0) || (image->rows == 0))
+ ThrowReaderException(CorruptImageError,"NegativeOrZeroImageSize");
+ if (image_info->ping != MagickFalse)
+ {
+ (void) CloseBlob(image);
+ return(GetFirstImageInList(image));
+ }
+ /*
+ Read RGBE pixels.
+ */
+ pixels=(unsigned char *) AcquireQuantumMemory(image->columns,
+ 4*sizeof(*pixels));
+ if (pixels == (unsigned char *) NULL)
+ ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
+ for (y=0; y < (ssize_t) image->rows; y++)
+ {
+ count=ReadBlob(image,4*sizeof(*pixel),pixel);
+ if (count != 4)
+ break;
+ if ((size_t) ((((size_t) pixel[2]) << 8) | pixel[3]) != image->columns)
+ break;
+ p=pixels;
+ for (i=0; i < 4; i++)
+ {
+ end=&pixels[(i+1)*image->columns];
+ while (p < end)
+ {
+ count=ReadBlob(image,2*sizeof(*pixel),pixel);
+ if (count < 1)
+ break;
+ if (pixel[0] > 128)
+ {
+ count=(ssize_t) pixel[0]-128;
+ if ((count == 0) || (count > (ssize_t) (end-p)))
+ break;
+ while (count-- > 0)
+ *p++=pixel[1];
+ }
+ else
+ {
+ count=(ssize_t) pixel[0];
+ if ((count == 0) || (count > (ssize_t) (end-p)))
+ break;
+ *p++=pixel[1];
+ if (--count > 0)
+ {
+ count=ReadBlob(image,(size_t) count*sizeof(*p),p);
+ if (count < 1)
+ break;
+ p+=count;
+ }
+ }
+ }
+ }
+ q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
+ if (q == (PixelPacket *) NULL)
+ break;
+ for (x=0; x < (ssize_t) image->columns; x++)
+ {
+ pixel[0]=pixels[x];
+ pixel[1]=pixels[x+image->columns];
+ pixel[2]=pixels[x+2*image->columns];
+ pixel[3]=pixels[x+3*image->columns];
+ q->red=0;
+ q->green=0;
+ q->blue=0;
+ if (pixel[3] != 0)
+ {
+ gamma=pow(2.0,pixel[3]-(128.0+8.0));
+ q->red=ClampToQuantum(QuantumRange*gamma*pixel[0]);
+ q->green=ClampToQuantum(QuantumRange*gamma*pixel[1]);
+ q->blue=ClampToQuantum(QuantumRange*gamma*pixel[2]);
+ }
+ q++;
+ }
+ if (SyncAuthenticPixels(image,exception) == MagickFalse)
+ break;
+ status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
+ image->rows);
+ if (status == MagickFalse)
+ break;
+ }
+ pixels=(unsigned char *) RelinquishMagickMemory(pixels);
+ if (EOFBlob(image) != MagickFalse)
+ ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
+ image->filename);
(void) CloseBlob(image);
return(GetFirstImageInList(image));
}
@@ -228,7 +420,7 @@
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
-% RegisterHDRImage() adds attributes for the Radiance HDR image format to the
+% RegisterHDRImage() adds attributes for the Radiance RGBE image format to the
% list of supported formats. The attributes include the image format tag, a
% method to read and/or write the format, whether the format supports the
% saving of more than one frame to the same file or blob, whether the format
@@ -246,8 +438,9 @@
entry=SetMagickInfo("HDR");
entry->decoder=(DecodeImageHandler *) ReadHDRImage;
- entry->description=ConstantString("Radiance HDR image format");
+ entry->description=ConstantString("Radiance RGBE image format");
entry->module=ConstantString("HDR");
+ entry->magick=(IsImageFormatHandler *) IsHDR;
(void) RegisterMagickInfo(entry);
return(MagickImageCoderSignature);
}
diff --git a/coders/vicar.c b/coders/vicar.c
index bc5b1f5..5e1b11d 100644
--- a/coders/vicar.c
+++ b/coders/vicar.c
@@ -169,12 +169,12 @@
register PixelPacket
*q;
- ssize_t
- count;
-
size_t
length;
+ ssize_t
+ count;
+
unsigned char
*pixels;
@@ -401,8 +401,7 @@
% stacked together to form image cubes. This method only writes a single
% grayscale plane.
%
-% WriteVICARImage was written contributed by
-% gorelick@esther.la.asu.edu.
+% WriteVICARImage was written contributed by gorelick@esther.la.asu.edu.
%
% The format of the WriteVICARImage method is:
%
diff --git a/magick/magic.c b/magick/magic.c
index f2d7b7a..859d656 100644
--- a/magick/magic.c
+++ b/magick/magic.c
@@ -120,6 +120,8 @@
{ "GIF", 0, MagickString("GIF8") },
{ "GPLT", 0, MagickString("#!/usr/local/bin/gnuplot") },
{ "HDF", 1, MagickString("HDF") },
+ { "HDR", 0, MagickString("#?RADIANCE") },
+ { "HDR", 0, MagickString("VIEW= ") },
{ "HPGL", 0, MagickString("IN;") },
{ "HTML", 1, MagickString("HTML") },
{ "HTML", 1, MagickString("html") },
@@ -168,8 +170,6 @@
{ "PSD", 0, MagickString("8BPS") },
{ "PWP", 0, MagickString("SFW95") },
{ "RAF", 0, MagickString("FUJIFILMCCD-RAW ") },
- { "RAD", 0, MagickString("#?RADIANCE") },
- { "RAD", 0, MagickString("VIEW= ") },
{ "RLE", 0, MagickString("\122\314") },
{ "SCT", 0, MagickString("CT") },
{ "SFW", 0, MagickString("SFW94") },