]> granicus.if.org Git - imagemagick/blobdiff - coders/fits.c
...
[imagemagick] / coders / fits.c
index 0f7de7e0e986a0aa0e73c8a58860ffff4974e4cd..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)
@@ -203,11 +205,13 @@ static inline double GetFITSPixelRange(const size_t depth)
 }
 
 static void SetFITSUnsignedPixels(const size_t length,
-  const size_t bits_per_pixel,unsigned char *pixels)
+  const size_t bits_per_pixel,const EndianType endian,unsigned char *pixels)
 {
   register ssize_t
     i;
 
+  if (endian != MSBEndian)
+    pixels+=(bits_per_pixel >> 3)-1;
   for (i=0; i < (ssize_t) length; i++)
   {
     *pixels^=0x80;
@@ -244,11 +248,10 @@ static Image *ReadFITSImage(const ImageInfo *image_info,
   char
     *comment,
     keyword[9],
-    property[MaxTextExtent],
+    property[MagickPathExtent],
     value[73];
 
   double
-    half_interval,
     pixel,
     scale;
 
@@ -283,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)
@@ -305,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;
@@ -333,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 == '=')
         {
@@ -381,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;
@@ -389,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))
@@ -409,7 +415,6 @@ static Image *ReadFITSImage(const ImageInfo *image_info,
   if ((fits_info.simple == MagickFalse) || (fits_info.number_axes < 1) ||
       (fits_info.number_axes > 4) || (number_pixels == 0))
     ThrowReaderException(CorruptImageError,"ImageTypeNotSupported");
-  half_interval=pow(2.0,(double) image->depth-1);
   for (scene=0; scene < (ssize_t) fits_info.number_planes; scene++)
   {
     image->columns=(size_t) fits_info.columns;
@@ -421,24 +426,29 @@ 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.
     */
-    SetImageColorspace(image,GRAYColorspace,exception);
-    if ((fits_info.min_data != 0.0) || (fits_info.max_data != 0.0))
+    (void) SetImageColorspace(image,GRAYColorspace,exception);
+    if ((fits_info.min_data == 0.0) && (fits_info.max_data == 0.0))
       {
-        if ((fits_info.bits_per_pixel != 0) && (fits_info.max_data == 0.0))
+        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)
             fits_info.bits_per_pixel);
       }
     else
-      GetFITSPixelExtrema(image,fits_info.bits_per_pixel,&fits_info.min_data,
-        &fits_info.max_data);
+      fits_info.max_data=GetFITSPixelRange((size_t) fits_info.bits_per_pixel);
     /*
       Convert FITS pixels to pixel packets.
     */
-    scale=(double) QuantumRange/(fits_info.scale*(fits_info.max_data-
-      fits_info.min_data)+fits_info.zero);
+    scale=QuantumRange/(fits_info.max_data-fits_info.min_data);
     for (y=(ssize_t) image->rows-1; y >= 0; y--)
     {
       q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
@@ -449,7 +459,8 @@ static Image *ReadFITSImage(const ImageInfo *image_info,
         pixel=GetFITSPixel(image,fits_info.bits_per_pixel);
         if ((image->depth == 16) || (image->depth == 32) ||
             (image->depth == 64))
-          pixel-=half_interval;
+          SetFITSUnsignedPixels(1,image->depth,image->endian,
+            (unsigned char *) &pixel);
         SetPixelGray(image,ClampToQuantum(scale*(fits_info.scale*(pixel-
           fits_info.min_data)+fits_info.zero)),q);
         q+=GetPixelChannels(image);
@@ -526,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);
 }
@@ -604,8 +611,9 @@ static MagickBooleanType WriteFITSImage(const ImageInfo *image_info,
   Image *image,ExceptionInfo *exception)
 {
   char
+    *fits_info,
     header[FITSBlocksize],
-    *fits_info;
+    *url;
 
   MagickBooleanType
     status;
@@ -631,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.
   */
@@ -654,7 +661,8 @@ static MagickBooleanType WriteFITSImage(const ImageInfo *image_info,
     Initialize image header.
   */
   image->depth=GetImageQuantumDepth(image,MagickFalse);
-  quantum_info=AcquireQuantumInfo((const ImageInfo *) NULL,image);
+  image->endian=MSBEndian;
+  quantum_info=AcquireQuantumInfo(image_info,image);
   if (quantum_info == (QuantumInfo *) NULL)
     ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
   offset=0;
@@ -663,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",
@@ -679,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);
@@ -690,7 +698,7 @@ static MagickBooleanType WriteFITSImage(const ImageInfo *image_info,
   (void) strncpy(fits_info+offset,header,strlen(header));
   offset+=80;
   (void) FormatLocaleString(header,FITSBlocksize,"BZERO   =         %E",
-    image->depth > 8 ? GetFITSPixelRange(image->depth) : 0.0);
+    image->depth > 8 ? GetFITSPixelRange(image->depth)/2.0 : 0.0);
   (void) strncpy(fits_info+offset,header,strlen(header));
   offset+=80;
   (void) FormatLocaleString(header,FITSBlocksize,"DATAMAX =         %E",
@@ -706,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);
@@ -717,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--)
@@ -729,10 +738,12 @@ static MagickBooleanType WriteFITSImage(const ImageInfo *image_info,
         length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
           GrayQuantum,pixels,exception);
         if (image->depth == 16)
-          SetFITSUnsignedPixels(image->columns,image->depth,pixels);
+          SetFITSUnsignedPixels(image->columns,image->depth,image->endian,
+            pixels);
         if (((image->depth == 32) || (image->depth == 64)) &&
             (quantum_info->format != FloatingPointQuantumFormat))
-          SetFITSUnsignedPixels(image->columns,image->depth,pixels);
+          SetFITSUnsignedPixels(image->columns,image->depth,image->endian,
+            pixels);
         count=WriteBlob(image,length,pixels);
         if (count != (ssize_t) length)
           break;
@@ -753,10 +764,12 @@ static MagickBooleanType WriteFITSImage(const ImageInfo *image_info,
         length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
           RedQuantum,pixels,exception);
         if (image->depth == 16)
-          SetFITSUnsignedPixels(image->columns,image->depth,pixels);
+          SetFITSUnsignedPixels(image->columns,image->depth,image->endian,
+            pixels);
         if (((image->depth == 32) || (image->depth == 64)) &&
             (quantum_info->format != FloatingPointQuantumFormat))
-          SetFITSUnsignedPixels(image->columns,image->depth,pixels);
+          SetFITSUnsignedPixels(image->columns,image->depth,image->endian,
+            pixels);
         count=WriteBlob(image,length,pixels);
         if (count != (ssize_t) length)
           break;
@@ -774,10 +787,12 @@ static MagickBooleanType WriteFITSImage(const ImageInfo *image_info,
         length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
           GreenQuantum,pixels,exception);
         if (image->depth == 16)
-          SetFITSUnsignedPixels(image->columns,image->depth,pixels);
+          SetFITSUnsignedPixels(image->columns,image->depth,image->endian,
+            pixels);
         if (((image->depth == 32) || (image->depth == 64)) &&
             (quantum_info->format != FloatingPointQuantumFormat))
-          SetFITSUnsignedPixels(image->columns,image->depth,pixels);
+          SetFITSUnsignedPixels(image->columns,image->depth,image->endian,
+            pixels);
         count=WriteBlob(image,length,pixels);
         if (count != (ssize_t) length)
           break;
@@ -795,10 +810,12 @@ static MagickBooleanType WriteFITSImage(const ImageInfo *image_info,
         length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
           BlueQuantum,pixels,exception);
         if (image->depth == 16)
-          SetFITSUnsignedPixels(image->columns,image->depth,pixels);
+          SetFITSUnsignedPixels(image->columns,image->depth,image->endian,
+            pixels);
         if (((image->depth == 32) || (image->depth == 64)) &&
             (quantum_info->format != FloatingPointQuantumFormat))
-          SetFITSUnsignedPixels(image->columns,image->depth,pixels);
+          SetFITSUnsignedPixels(image->columns,image->depth,image->endian,
+            pixels);
         count=WriteBlob(image,length,pixels);
         if (count != (ssize_t) length)
           break;