2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6 % FFFFF IIIII TTTTT SSSSS %
13 % Read/Write Flexible Image Transport System Images. %
20 % Copyright 1999-2010 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
26 % http://www.imagemagick.org/script/license.php %
28 % Unless required by applicable law or agreed to in writing, software %
29 % distributed under the License is distributed on an "AS IS" BASIS, %
30 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31 % See the License for the specific language governing permissions and %
32 % limitations under the License. %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
42 #include "magick/studio.h"
43 #include "magick/attribute.h"
44 #include "magick/blob.h"
45 #include "magick/blob-private.h"
46 #include "magick/cache.h"
47 #include "magick/color-private.h"
48 #include "magick/colorspace.h"
49 #include "magick/constitute.h"
50 #include "magick/exception.h"
51 #include "magick/exception-private.h"
52 #include "magick/image.h"
53 #include "magick/image-private.h"
54 #include "magick/list.h"
55 #include "magick/magick.h"
56 #include "magick/memory_.h"
57 #include "magick/module.h"
58 #include "magick/monitor.h"
59 #include "magick/monitor-private.h"
60 #include "magick/pixel-private.h"
61 #include "magick/pixel-private.h"
62 #include "magick/property.h"
63 #include "magick/static.h"
64 #include "magick/statistic.h"
65 #include "magick/string_.h"
66 #include "magick/string-private.h"
67 #include "magick/module.h"
72 #define FITSBlocksize 2880UL
77 static MagickBooleanType
78 WriteFITSImage(const ImageInfo *,Image *);
81 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
89 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
91 % IsFITS() returns MagickTrue if the image format type, identified by the
92 % magick string, is FITS.
94 % The format of the IsFITS method is:
96 % MagickBooleanType IsFITS(const unsigned char *magick,const size_t length)
98 % A description of each parameter follows:
100 % o magick: compare image format pattern against these bytes.
102 % o length: Specifies the length of the magick string.
105 static MagickBooleanType IsFITS(const unsigned char *magick,const size_t length)
109 if (LocaleNCompare((const char *) magick,"IT0",3) == 0)
111 if (LocaleNCompare((const char *) magick,"SIMPLE",6) == 0)
117 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
121 % R e a d F I T S I m a g e %
125 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
127 % ReadFITSImage() reads a FITS image file and returns it. It allocates the
128 % memory necessary for the new Image structure and returns a pointer to the
131 % The format of the ReadFITSImage method is:
133 % Image *ReadFITSImage(const ImageInfo *image_info,
134 % ExceptionInfo *exception)
136 % A description of each parameter follows:
138 % o image_info: the image info.
140 % o exception: return any errors or warnings in this structure.
144 static inline double GetFITSPixel(Image *image,int bits_per_pixel)
146 switch (image->depth >> 3)
149 return((double) ReadBlobByte(image));
151 return((double) ((short) ReadBlobShort(image)));
154 if (bits_per_pixel > 0)
155 return((double) ((long) ReadBlobLong(image)));
156 return((double) ReadBlobFloat(image));
160 if (bits_per_pixel > 0)
161 return((double) ((MagickOffsetType) ReadBlobLongLong(image)));
166 return(ReadBlobDouble(image));
169 static void GetFITSPixelExtrema(Image *image,const int bits_per_pixel,
170 double *minima,double *maxima)
181 register MagickOffsetType
184 offset=TellBlob(image);
185 number_pixels=(MagickSizeType) image->columns*image->rows;
186 *minima=GetFITSPixel(image,bits_per_pixel);
188 for (i=1; i < (MagickOffsetType) number_pixels; i++)
190 pixel=GetFITSPixel(image,bits_per_pixel);
196 (void) SeekBlob(image,offset,SEEK_SET);
199 static inline double GetFITSPixelRange(const unsigned long depth)
201 return((double) ((MagickOffsetType) GetQuantumRange(depth)));
204 static void SetFITSUnsignedPixels(const size_t length,
205 const unsigned long bits_per_pixel,unsigned char *pixels)
210 for (i=0; i < (long) length; i++)
213 pixels+=bits_per_pixel >> 3;
217 static Image *ReadFITSImage(const ImageInfo *image_info,
218 ExceptionInfo *exception)
220 typedef struct _FITSInfo
246 property[MaxTextExtent],
285 assert(image_info != (const ImageInfo *) NULL);
286 assert(image_info->signature == MagickSignature);
287 if (image_info->debug != MagickFalse)
288 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
289 image_info->filename);
290 assert(exception != (ExceptionInfo *) NULL);
291 assert(exception->signature == MagickSignature);
292 image=AcquireImage(image_info);
293 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
294 if (status == MagickFalse)
296 image=DestroyImageList(image);
297 return((Image *) NULL);
300 Initialize image header.
302 (void) ResetMagickMemory(&fits_info,0,sizeof(fits_info));
303 fits_info.extend=MagickFalse;
304 fits_info.simple=MagickFalse;
305 fits_info.bits_per_pixel=8;
309 fits_info.number_planes=1;
310 fits_info.min_data=0.0;
311 fits_info.max_data=0.0;
314 fits_info.endian=MSBEndian;
318 for (comment=(char *) NULL; EOFBlob(image) == MagickFalse; )
320 for ( ; EOFBlob(image) == MagickFalse; )
325 count=ReadBlob(image,8,(unsigned char *) keyword);
328 for (i=0; i < 8; i++)
330 if (isspace((int) ((unsigned char) keyword[i])) != 0)
332 keyword[i]=tolower((int) ((unsigned char) keyword[i]));
335 count=ReadBlob(image,72,(unsigned char *) value);
343 while (isspace((int) ((unsigned char) *p)) != 0)
346 if (LocaleCompare(keyword,"end") == 0)
348 if (LocaleCompare(keyword,"extend") == 0)
349 fits_info.extend=(*p == 'T') || (*p == 't') ? MagickTrue : MagickFalse;
350 if (LocaleCompare(keyword,"simple") == 0)
351 fits_info.simple=(*p == 'T') || (*p == 't') ? MagickTrue : MagickFalse;
352 if (LocaleCompare(keyword,"bitpix") == 0)
353 fits_info.bits_per_pixel=StringToLong(p);
354 if (LocaleCompare(keyword,"naxis") == 0)
355 fits_info.number_axes=StringToLong(p);
356 if (LocaleCompare(keyword,"naxis1") == 0)
357 fits_info.columns=StringToLong(p);
358 if (LocaleCompare(keyword,"naxis2") == 0)
359 fits_info.rows=StringToLong(p);
360 if (LocaleCompare(keyword,"naxis3") == 0)
361 fits_info.number_planes=StringToLong(p);
362 if (LocaleCompare(keyword,"datamax") == 0)
363 fits_info.max_data=StringToDouble(p);
364 if (LocaleCompare(keyword,"datamin") == 0)
365 fits_info.min_data=StringToDouble(p);
366 if (LocaleCompare(keyword,"bzero") == 0)
367 fits_info.zero=StringToDouble(p);
368 if (LocaleCompare(keyword,"bscale") == 0)
369 fits_info.scale=StringToDouble(p);
370 if (LocaleCompare(keyword,"comment") == 0)
372 if (comment == (char *) NULL)
373 comment=ConstantString(p);
375 (void) ConcatenateString(&comment,p);
377 if (LocaleCompare(keyword,"xendian") == 0)
379 if (LocaleNCompare(p,"big",3) == 0)
380 fits_info.endian=MSBEndian;
382 fits_info.endian=LSBEndian;
384 (void) FormatMagickString(property,MaxTextExtent,"fits:%s",keyword);
385 (void) SetImageProperty(image,property,p);
388 while (((TellBlob(image) % FITSBlocksize) != 0) && (c != EOF))
389 c=ReadBlobByte(image);
390 if (fits_info.extend == MagickFalse)
392 number_pixels=(MagickSizeType) fits_info.columns*fits_info.rows;
393 if ((fits_info.simple != MagickFalse) && (fits_info.number_axes >= 1) &&
394 (fits_info.number_axes <= 4) && (number_pixels != 0))
398 Verify that required image information is defined.
400 if (comment != (char *) NULL)
402 (void) SetImageProperty(image,"comment",comment);
403 comment=DestroyString(comment);
405 if (EOFBlob(image) != MagickFalse)
406 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
408 number_pixels=(MagickSizeType) fits_info.columns*fits_info.rows;
409 if ((fits_info.simple == MagickFalse) || (fits_info.number_axes < 1) ||
410 (fits_info.number_axes > 4) || (number_pixels == 0))
411 ThrowReaderException(CorruptImageError,"ImageTypeNotSupported");
412 for (scene=0; scene < (long) fits_info.number_planes; scene++)
414 image->columns=(unsigned long) fits_info.columns;
415 image->rows=(unsigned long) fits_info.rows;
416 image->depth=(unsigned long) (fits_info.bits_per_pixel < 0 ? -1 : 1)*
417 fits_info.bits_per_pixel;
418 image->endian=fits_info.endian;
419 image->scene=(unsigned long) scene;
420 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
421 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
424 Initialize image structure.
426 if ((fits_info.min_data != 0.0) || (fits_info.max_data != 0.0))
428 if ((fits_info.bits_per_pixel != 0) && (fits_info.max_data == 0.0))
429 fits_info.max_data=GetFITSPixelRange((unsigned long)
430 fits_info.bits_per_pixel);
433 GetFITSPixelExtrema(image,fits_info.bits_per_pixel,&fits_info.min_data,
434 &fits_info.max_data);
436 Convert FITS pixels to pixel packets.
438 scale=(double) QuantumRange/(fits_info.scale*(fits_info.max_data-
439 fits_info.min_data)+fits_info.zero);
440 for (y=(long) image->rows-1; y >= 0; y--)
442 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
443 if (q == (PixelPacket *) NULL)
445 for (x=0; x < (long) image->columns; x++)
447 pixel=GetFITSPixel(image,fits_info.bits_per_pixel);
448 q->red=(Quantum) ClampToQuantum(scale*(fits_info.scale*(pixel-
449 fits_info.min_data)+fits_info.zero));
454 if (SyncAuthenticPixels(image,exception) == MagickFalse)
456 if (image->previous == (Image *) NULL)
458 status=SetImageProgress(image,LoadImageTag,y,image->rows);
459 if (status == MagickFalse)
463 if (EOFBlob(image) != MagickFalse)
465 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
470 Proceed to next image.
472 if (image_info->number_scenes != 0)
473 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
475 if (scene < (long) (fits_info.number_planes-1))
478 Allocate next image structure.
480 AcquireNextImage(image_info,image);
481 if (GetNextImageInList(image) == (Image *) NULL)
483 image=DestroyImageList(image);
484 return((Image *) NULL);
486 image=SyncNextImageInList(image);
487 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
489 if (status == MagickFalse)
493 (void) CloseBlob(image);
494 return(GetFirstImageInList(image));
498 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
502 % R e g i s t e r F I T S I m a g e %
506 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
508 % RegisterFITSImage() adds attributes for the FITS image format to
509 % the list of supported formats. The attributes include the image format
510 % tag, a method to read and/or write the format, whether the format
511 % supports the saving of more than one frame to the same file or blob,
512 % whether the format supports native in-memory I/O, and a brief
513 % description of the format.
515 % The format of the RegisterFITSImage method is:
517 % unsigned long RegisterFITSImage(void)
520 ModuleExport unsigned long RegisterFITSImage(void)
525 entry=SetMagickInfo("FITS");
526 entry->decoder=(DecodeImageHandler *) ReadFITSImage;
527 entry->encoder=(EncodeImageHandler *) WriteFITSImage;
528 entry->magick=(IsImageFormatHandler *) IsFITS;
529 entry->adjoin=MagickFalse;
530 entry->seekable_stream=MagickTrue;
531 entry->description=ConstantString("Flexible Image Transport System");
532 entry->module=ConstantString("FITS");
533 (void) RegisterMagickInfo(entry);
534 entry=SetMagickInfo("FTS");
535 entry->decoder=(DecodeImageHandler *) ReadFITSImage;
536 entry->encoder=(EncodeImageHandler *) WriteFITSImage;
537 entry->magick=(IsImageFormatHandler *) IsFITS;
538 entry->adjoin=MagickFalse;
539 entry->seekable_stream=MagickTrue;
540 entry->description=ConstantString("Flexible Image Transport System");
541 entry->module=ConstantString("FTS");
542 (void) RegisterMagickInfo(entry);
543 return(MagickImageCoderSignature);
547 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
551 % U n r e g i s t e r F I T S I m a g e %
555 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
557 % UnregisterFITSImage() removes format registrations made by the
558 % FITS module from the list of supported formats.
560 % The format of the UnregisterFITSImage method is:
562 % UnregisterFITSImage(void)
565 ModuleExport void UnregisterFITSImage(void)
567 (void) UnregisterMagickInfo("FITS");
568 (void) UnregisterMagickInfo("FTS");
572 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
576 % W r i t e F I T S I m a g e %
580 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
582 % WriteFITSImage() writes a Flexible Image Transport System image to a
583 % file as gray scale intensities [0..255].
585 % The format of the WriteFITSImage method is:
587 % MagickBooleanType WriteFITSImage(const ImageInfo *image_info,
590 % A description of each parameter follows.
592 % o image_info: the image info.
594 % o image: The image.
597 static MagickBooleanType WriteFITSImage(const ImageInfo *image_info,
601 header[FITSBlocksize],
613 register const PixelPacket
627 Open output image file.
629 assert(image_info != (const ImageInfo *) NULL);
630 assert(image_info->signature == MagickSignature);
631 assert(image != (Image *) NULL);
632 assert(image->signature == MagickSignature);
633 if (image->debug != MagickFalse)
634 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
635 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
636 if (status == MagickFalse)
638 if (image->colorspace != RGBColorspace)
639 (void) TransformImageColorspace(image,RGBColorspace);
641 Allocate image memory.
643 fits_info=(char *) AcquireQuantumMemory(FITSBlocksize,sizeof(*fits_info));
644 if (fits_info == (char *) NULL)
645 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
646 (void) ResetMagickMemory(fits_info,' ',FITSBlocksize*sizeof(*fits_info));
648 Initialize image header.
650 image->depth=GetImageQuantumDepth(image,MagickFalse);
651 quantum_info=AcquireQuantumInfo((const ImageInfo *) NULL,image);
652 if (quantum_info == (QuantumInfo *) NULL)
653 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
655 (void) FormatMagickString(header,FITSBlocksize,
657 (void) strncpy(fits_info+offset,header,strlen(header));
659 (void) FormatMagickString(header,FITSBlocksize,"BITPIX = %10ld",
660 (quantum_info->format == FloatingPointQuantumFormat ? -1 : 1)*image->depth);
661 (void) strncpy(fits_info+offset,header,strlen(header));
663 (void) FormatMagickString(header,FITSBlocksize,"NAXIS = %10lu",
665 (void) strncpy(fits_info+offset,header,strlen(header));
667 (void) FormatMagickString(header,FITSBlocksize,"NAXIS1 = %10lu",
669 (void) strncpy(fits_info+offset,header,strlen(header));
671 (void) FormatMagickString(header,FITSBlocksize,"NAXIS2 = %10lu",
673 (void) strncpy(fits_info+offset,header,strlen(header));
675 (void) FormatMagickString(header,FITSBlocksize,"BSCALE = %E",1.0);
676 (void) strncpy(fits_info+offset,header,strlen(header));
678 (void) FormatMagickString(header,FITSBlocksize,"BZERO = %E",
679 image->depth > 8 ? GetFITSPixelRange(image->depth) : 0.0);
680 (void) strncpy(fits_info+offset,header,strlen(header));
682 (void) FormatMagickString(header,FITSBlocksize,"DATAMAX = %E",
683 1.0*((MagickOffsetType) GetQuantumRange(image->depth)));
684 (void) strncpy(fits_info+offset,header,strlen(header));
686 (void) FormatMagickString(header,FITSBlocksize,"DATAMIN = %E",0.0);
687 (void) strncpy(fits_info+offset,header,strlen(header));
689 if (image->endian == LSBEndian)
691 (void) FormatMagickString(header,FITSBlocksize,"XENDIAN = 'SMALL'");
692 (void) strncpy(fits_info+offset,header,strlen(header));
695 (void) FormatMagickString(header,FITSBlocksize,"HISTORY %.72s",
696 GetMagickVersion((unsigned long *) NULL));
697 (void) strncpy(fits_info+offset,header,strlen(header));
699 (void) strncpy(header,"END",FITSBlocksize);
700 (void) strncpy(fits_info+offset,header,strlen(header));
702 (void) WriteBlob(image,FITSBlocksize,(unsigned char *) fits_info);
704 Convert image to fits scale PseudoColor class.
706 pixels=GetQuantumPixels(quantum_info);
707 length=GetQuantumExtent(image,quantum_info,GrayQuantum);
708 for (y=(long) image->rows-1; y >= 0; y--)
710 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
711 if (p == (const PixelPacket *) NULL)
713 length=ExportQuantumPixels(image,(const CacheView *) NULL,quantum_info,
714 GrayQuantum,pixels,&image->exception);
715 if (image->depth == 16)
716 SetFITSUnsignedPixels(image->columns,image->depth,pixels);
717 if (((image->depth == 32) || (image->depth == 64)) &&
718 (quantum_info->format != FloatingPointQuantumFormat))
719 SetFITSUnsignedPixels(image->columns,image->depth,pixels);
720 count=WriteBlob(image,length,pixels);
721 if (count != (ssize_t) length)
723 status=SetImageProgress(image,SaveImageTag,y,image->rows);
724 if (status == MagickFalse)
727 quantum_info=DestroyQuantumInfo(quantum_info);
728 length=(size_t) (FITSBlocksize-TellBlob(image) % FITSBlocksize);
731 (void) ResetMagickMemory(fits_info,0,length*sizeof(*fits_info));
732 (void) WriteBlob(image,length,(unsigned char *) fits_info);
734 fits_info=DestroyString(fits_info);
735 (void) CloseBlob(image);