]> granicus.if.org Git - imagemagick/blobdiff - coders/fits.c
...
[imagemagick] / coders / fits.c
index 3ab401609ebb3c9d2cca1101bf2535ff0bd7f6dd..2e86048267d58c333acf3592fc3019f2adf7e505 100644 (file)
 %            Read/Write Flexible Image Transport System Images.               %
 %                                                                             %
 %                              Software Design                                %
-%                                John Cristy                                  %
+%                                   Cristy                                    %
 %                                 July 1992                                   %
 %                                                                             %
 %                                                                             %
-%  Copyright 1999-2013 ImageMagick Studio LLC, a non-profit organization      %
+%  Copyright 1999-2017 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  %
@@ -153,7 +153,7 @@ static inline double GetFITSPixel(Image *image,int bits_per_pixel)
     case 4:
     {
       if (bits_per_pixel > 0)
-        return((double) ((int) ReadBlobLong(image)));
+        return((double) ReadBlobSignedLong(image));
       return((double) ReadBlobFloat(image));
     }
     case 8:
@@ -167,8 +167,8 @@ static inline double GetFITSPixel(Image *image,int bits_per_pixel)
   return(ReadBlobDouble(image));
 }
 
-static void GetFITSPixelExtrema(Image *image,const int bits_per_pixel,
-  double *minima,double *maxima)
+static MagickOffsetType GetFITSPixelExtrema(Image *image,
+  const int bits_per_pixel,double *minima,double *maxima)
 {
   double
     pixel;
@@ -183,6 +183,8 @@ static void GetFITSPixelExtrema(Image *image,const int bits_per_pixel,
     i;
 
   offset=TellBlob(image);
+  if (offset == -1)
+    return(-1);
   number_pixels=(MagickSizeType) image->columns*image->rows;
   *minima=GetFITSPixel(image,bits_per_pixel);
   *maxima=(*minima);
@@ -194,7 +196,7 @@ static void GetFITSPixelExtrema(Image *image,const int bits_per_pixel,
     if (pixel > *maxima)
       *maxima=pixel;
   }
-  (void) SeekBlob(image,offset,SEEK_SET);
+  return(SeekBlob(image,offset,SEEK_SET));
 }
 
 static inline double GetFITSPixelRange(const size_t depth)
@@ -246,7 +248,7 @@ static Image *ReadFITSImage(const ImageInfo *image_info,
   char
     *comment,
     keyword[9],
-    property[MaxTextExtent],
+    property[MagickPathExtent],
     value[73];
 
   double
@@ -284,12 +286,12 @@ static Image *ReadFITSImage(const ImageInfo *image_info,
     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)
@@ -306,7 +308,6 @@ static Image *ReadFITSImage(const ImageInfo *image_info,
   fits_info.bits_per_pixel=8;
   fits_info.columns=1;
   fits_info.rows=1;
-  fits_info.rows=1;
   fits_info.number_planes=1;
   fits_info.min_data=0.0;
   fits_info.max_data=0.0;
@@ -334,9 +335,9 @@ static Image *ReadFITSImage(const ImageInfo *image_info,
       }
       keyword[i]='\0';
       count=ReadBlob(image,72,(unsigned char *) value);
+      value[72]='\0';
       if (count != 72)
         break;
-      value[72]='\0';
       p=value;
       if (*p == '=')
         {
@@ -382,7 +383,7 @@ static Image *ReadFITSImage(const ImageInfo *image_info,
           else
             fits_info.endian=LSBEndian;
         }
-      (void) FormatLocaleString(property,MaxTextExtent,"fits:%s",keyword);
+      (void) FormatLocaleString(property,MagickPathExtent,"fits:%s",keyword);
       (void) SetImageProperty(image,property,p,exception);
     }
     c=0;
@@ -390,6 +391,10 @@ static Image *ReadFITSImage(const ImageInfo *image_info,
       c=ReadBlobByte(image);
     if (fits_info.extend == MagickFalse)
       break;
+    if ((fits_info.bits_per_pixel != 8) && (fits_info.bits_per_pixel != 16) &&
+        (fits_info.bits_per_pixel != 32) && (fits_info.bits_per_pixel != 64) &&
+        (fits_info.bits_per_pixel != -32) && (fits_info.bits_per_pixel != -64))
+      ThrowReaderException(CorruptImageError,"ImproperImageHeader");
     number_pixels=(MagickSizeType) fits_info.columns*fits_info.rows;
     if ((fits_info.simple != MagickFalse) && (fits_info.number_axes >= 1) &&
         (fits_info.number_axes <= 4) && (number_pixels != 0))
@@ -421,14 +426,18 @@ static Image *ReadFITSImage(const ImageInfo *image_info,
     if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
       if (image->scene >= (image_info->scene+image_info->number_scenes-1))
         break;
+    status=SetImageExtent(image,image->columns,image->rows,exception);
+    if (status == MagickFalse)
+      return(DestroyImageList(image));
     /*
       Initialize image structure.
     */
     (void) SetImageColorspace(image,GRAYColorspace,exception);
     if ((fits_info.min_data == 0.0) && (fits_info.max_data == 0.0))
       {
-        if ((fits_info.bits_per_pixel == -32) || (fits_info.max_data == -64))
-          GetFITSPixelExtrema(image,fits_info.bits_per_pixel,
+        if ((fits_info.bits_per_pixel == -32) ||
+            (fits_info.bits_per_pixel == -64))
+          (void) GetFITSPixelExtrema(image,fits_info.bits_per_pixel,
             &fits_info.min_data,&fits_info.max_data);
         else
           fits_info.max_data=GetFITSPixelRange((size_t)
@@ -528,23 +537,19 @@ ModuleExport size_t RegisterFITSImage(void)
   MagickInfo
     *entry;
 
-  entry=SetMagickInfo("FITS");
+  entry=AcquireMagickInfo("FITS","FITS","Flexible Image Transport System");
   entry->decoder=(DecodeImageHandler *) ReadFITSImage;
   entry->encoder=(EncodeImageHandler *) WriteFITSImage;
   entry->magick=(IsImageFormatHandler *) IsFITS;
-  entry->adjoin=MagickFalse;
-  entry->seekable_stream=MagickTrue;
-  entry->description=ConstantString("Flexible Image Transport System");
-  entry->module=ConstantString("FITS");
+  entry->flags^=CoderAdjoinFlag;
+  entry->flags|=CoderDecoderSeekableStreamFlag;
   (void) RegisterMagickInfo(entry);
-  entry=SetMagickInfo("FTS");
+  entry=AcquireMagickInfo("FITS","FTS","Flexible Image Transport System");
   entry->decoder=(DecodeImageHandler *) ReadFITSImage;
   entry->encoder=(EncodeImageHandler *) WriteFITSImage;
   entry->magick=(IsImageFormatHandler *) IsFITS;
-  entry->adjoin=MagickFalse;
-  entry->seekable_stream=MagickTrue;
-  entry->description=ConstantString("Flexible Image Transport System");
-  entry->module=ConstantString("FTS");
+  entry->flags^=CoderAdjoinFlag;
+  entry->flags|=CoderDecoderSeekableStreamFlag;
   (void) RegisterMagickInfo(entry);
   return(MagickImageCoderSignature);
 }
@@ -606,8 +611,9 @@ static MagickBooleanType WriteFITSImage(const ImageInfo *image_info,
   Image *image,ExceptionInfo *exception)
 {
   char
+    *fits_info,
     header[FITSBlocksize],
-    *fits_info;
+    *url;
 
   MagickBooleanType
     status;
@@ -633,18 +639,17 @@ static MagickBooleanType WriteFITSImage(const ImageInfo *image_info,
     Open output image file.
   */
   assert(image_info != (const ImageInfo *) NULL);
-  assert(image_info->signature == MagickSignature);
+  assert(image_info->signature == MagickCoreSignature);
   assert(image != (Image *) NULL);
-  assert(image->signature == MagickSignature);
+  assert(image->signature == MagickCoreSignature);
   if (image->debug != MagickFalse)
     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
   assert(exception != (ExceptionInfo *) NULL);
-  assert(exception->signature == MagickSignature);
+  assert(exception->signature == MagickCoreSignature);
   status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
   if (status == MagickFalse)
     return(status);
-  if (IssRGBCompatibleColorspace(image->colorspace) == MagickFalse)
-    (void) TransformImageColorspace(image,sRGBColorspace,exception);
+  (void) TransformImageColorspace(image,sRGBColorspace,exception);
   /*
     Allocate image memory.
   */
@@ -657,7 +662,7 @@ static MagickBooleanType WriteFITSImage(const ImageInfo *image_info,
   */
   image->depth=GetImageQuantumDepth(image,MagickFalse);
   image->endian=MSBEndian;
-  quantum_info=AcquireQuantumInfo((const ImageInfo *) NULL,image);
+  quantum_info=AcquireQuantumInfo(image_info,image);
   if (quantum_info == (QuantumInfo *) NULL)
     ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
   offset=0;
@@ -666,12 +671,12 @@ static MagickBooleanType WriteFITSImage(const ImageInfo *image_info,
   (void) strncpy(fits_info+offset,header,strlen(header));
   offset+=80;
   (void) FormatLocaleString(header,FITSBlocksize,"BITPIX  =           %10ld",
-    (long) (quantum_info->format == FloatingPointQuantumFormat ? -1 : 1)*
-    image->depth);
+    (long) ((quantum_info->format == FloatingPointQuantumFormat ? -1 : 1)*
+    image->depth));
   (void) strncpy(fits_info+offset,header,strlen(header));
   offset+=80;
   (void) FormatLocaleString(header,FITSBlocksize,"NAXIS   =           %10lu",
-    IsImageGray(image,exception) != MagickFalse ? 2UL : 3UL);
+    SetImageGray(image,exception) != MagickFalse ? 2UL : 3UL);
   (void) strncpy(fits_info+offset,header,strlen(header));
   offset+=80;
   (void) FormatLocaleString(header,FITSBlocksize,"NAXIS1  =           %10lu",
@@ -682,7 +687,7 @@ static MagickBooleanType WriteFITSImage(const ImageInfo *image_info,
     (unsigned long) image->rows);
   (void) strncpy(fits_info+offset,header,strlen(header));
   offset+=80;
-  if (IsImageGray(image,exception) == MagickFalse)
+  if (SetImageGray(image,exception) == MagickFalse)
     {
       (void) FormatLocaleString(header,FITSBlocksize,
         "NAXIS3  =           %10lu",3UL);
@@ -709,8 +714,9 @@ static MagickBooleanType WriteFITSImage(const ImageInfo *image_info,
       (void) strncpy(fits_info+offset,header,strlen(header));
       offset+=80;
     }
-  (void) FormatLocaleString(header,FITSBlocksize,"HISTORY %.72s",
-    GetMagickVersion((size_t *) NULL));
+  url=GetMagickHomeURL();
+  (void) FormatLocaleString(header,FITSBlocksize,"HISTORY %.72s",url);
+  url=DestroyString(url);
   (void) strncpy(fits_info+offset,header,strlen(header));
   offset+=80;
   (void) strncpy(header,"END",FITSBlocksize);
@@ -720,8 +726,8 @@ static MagickBooleanType WriteFITSImage(const ImageInfo *image_info,
   /*
     Convert image to fits scale PseudoColor class.
   */
-  pixels=GetQuantumPixels(quantum_info);
-  if (IsImageGray(image,exception) != MagickFalse)
+  pixels=(unsigned char *) GetQuantumPixels(quantum_info);
+  if (SetImageGray(image,exception) != MagickFalse)
     {
       length=GetQuantumExtent(image,quantum_info,GrayQuantum);
       for (y=(ssize_t) image->rows-1; y >= 0; y--)