2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6 % FFFFF IIIII TTTTT SSSSS %
13 % Read/Write Flexible Image Transport System Images. %
20 % Copyright 1999-2017 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 % https://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 "MagickCore/studio.h"
43 #include "MagickCore/attribute.h"
44 #include "MagickCore/blob.h"
45 #include "MagickCore/blob-private.h"
46 #include "MagickCore/cache.h"
47 #include "MagickCore/color-private.h"
48 #include "MagickCore/colorspace.h"
49 #include "MagickCore/colorspace-private.h"
50 #include "MagickCore/constitute.h"
51 #include "MagickCore/exception.h"
52 #include "MagickCore/exception-private.h"
53 #include "MagickCore/image.h"
54 #include "MagickCore/image-private.h"
55 #include "MagickCore/list.h"
56 #include "MagickCore/magick.h"
57 #include "MagickCore/memory_.h"
58 #include "MagickCore/module.h"
59 #include "MagickCore/monitor.h"
60 #include "MagickCore/monitor-private.h"
61 #include "MagickCore/pixel-accessor.h"
62 #include "MagickCore/property.h"
63 #include "MagickCore/quantum-private.h"
64 #include "MagickCore/static.h"
65 #include "MagickCore/statistic.h"
66 #include "MagickCore/string_.h"
67 #include "MagickCore/string-private.h"
68 #include "MagickCore/module.h"
73 #define FITSBlocksize 2880UL
78 static MagickBooleanType
79 WriteFITSImage(const ImageInfo *,Image *,ExceptionInfo *);
82 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
90 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
92 % IsFITS() returns MagickTrue if the image format type, identified by the
93 % magick string, is FITS.
95 % The format of the IsFITS method is:
97 % MagickBooleanType IsFITS(const unsigned char *magick,const size_t length)
99 % A description of each parameter follows:
101 % o magick: compare image format pattern against these bytes.
103 % o length: Specifies the length of the magick string.
106 static MagickBooleanType IsFITS(const unsigned char *magick,const size_t length)
110 if (LocaleNCompare((const char *) magick,"IT0",3) == 0)
112 if (LocaleNCompare((const char *) magick,"SIMPLE",6) == 0)
118 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
122 % R e a d F I T S I m a g e %
126 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
128 % ReadFITSImage() reads a FITS image file and returns it. It allocates the
129 % memory necessary for the new Image structure and returns a pointer to the
132 % The format of the ReadFITSImage method is:
134 % Image *ReadFITSImage(const ImageInfo *image_info,
135 % ExceptionInfo *exception)
137 % A description of each parameter follows:
139 % o image_info: the image info.
141 % o exception: return any errors or warnings in this structure.
145 static inline double GetFITSPixel(Image *image,int bits_per_pixel)
147 switch (image->depth >> 3)
150 return((double) ReadBlobByte(image));
152 return((double) ((short) ReadBlobShort(image)));
155 if (bits_per_pixel > 0)
156 return((double) ReadBlobSignedLong(image));
157 return((double) ReadBlobFloat(image));
161 if (bits_per_pixel > 0)
162 return((double) ((MagickOffsetType) ReadBlobLongLong(image)));
167 return(ReadBlobDouble(image));
170 static MagickOffsetType GetFITSPixelExtrema(Image *image,
171 const int bits_per_pixel,double *minima,double *maxima)
182 register MagickOffsetType
185 offset=TellBlob(image);
188 number_pixels=(MagickSizeType) image->columns*image->rows;
189 *minima=GetFITSPixel(image,bits_per_pixel);
191 for (i=1; i < (MagickOffsetType) number_pixels; i++)
193 pixel=GetFITSPixel(image,bits_per_pixel);
199 return(SeekBlob(image,offset,SEEK_SET));
202 static inline double GetFITSPixelRange(const size_t depth)
204 return((double) ((MagickOffsetType) GetQuantumRange(depth)));
207 static void SetFITSUnsignedPixels(const size_t length,
208 const size_t bits_per_pixel,const EndianType endian,unsigned char *pixels)
213 if (endian != MSBEndian)
214 pixels+=(bits_per_pixel >> 3)-1;
215 for (i=0; i < (ssize_t) length; i++)
218 pixels+=bits_per_pixel >> 3;
222 static Image *ReadFITSImage(const ImageInfo *image_info,
223 ExceptionInfo *exception)
225 typedef struct _FITSInfo
251 property[MagickPathExtent],
288 assert(image_info != (const ImageInfo *) NULL);
289 assert(image_info->signature == MagickCoreSignature);
290 if (image_info->debug != MagickFalse)
291 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
292 image_info->filename);
293 assert(exception != (ExceptionInfo *) NULL);
294 assert(exception->signature == MagickCoreSignature);
295 image=AcquireImage(image_info,exception);
296 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
297 if (status == MagickFalse)
299 image=DestroyImageList(image);
300 return((Image *) NULL);
303 Initialize image header.
305 (void) ResetMagickMemory(&fits_info,0,sizeof(fits_info));
306 fits_info.extend=MagickFalse;
307 fits_info.simple=MagickFalse;
308 fits_info.bits_per_pixel=8;
311 fits_info.number_planes=1;
312 fits_info.min_data=0.0;
313 fits_info.max_data=0.0;
316 fits_info.endian=MSBEndian;
320 for (comment=(char *) NULL; EOFBlob(image) == MagickFalse; )
322 for ( ; EOFBlob(image) == MagickFalse; )
327 count=ReadBlob(image,8,(unsigned char *) keyword);
330 for (i=0; i < 8; i++)
332 if (isspace((int) ((unsigned char) keyword[i])) != 0)
334 keyword[i]=tolower((int) ((unsigned char) keyword[i]));
337 count=ReadBlob(image,72,(unsigned char *) value);
345 while (isspace((int) ((unsigned char) *p)) != 0)
348 if (LocaleCompare(keyword,"end") == 0)
350 if (LocaleCompare(keyword,"extend") == 0)
351 fits_info.extend=(*p == 'T') || (*p == 't') ? MagickTrue : MagickFalse;
352 if (LocaleCompare(keyword,"simple") == 0)
353 fits_info.simple=(*p == 'T') || (*p == 't') ? MagickTrue : MagickFalse;
354 if (LocaleCompare(keyword,"bitpix") == 0)
355 fits_info.bits_per_pixel=StringToLong(p);
356 if (LocaleCompare(keyword,"naxis") == 0)
357 fits_info.number_axes=StringToLong(p);
358 if (LocaleCompare(keyword,"naxis1") == 0)
359 fits_info.columns=StringToLong(p);
360 if (LocaleCompare(keyword,"naxis2") == 0)
361 fits_info.rows=StringToLong(p);
362 if (LocaleCompare(keyword,"naxis3") == 0)
363 fits_info.number_planes=StringToLong(p);
364 if (LocaleCompare(keyword,"datamax") == 0)
365 fits_info.max_data=StringToDouble(p,(char **) NULL);
366 if (LocaleCompare(keyword,"datamin") == 0)
367 fits_info.min_data=StringToDouble(p,(char **) NULL);
368 if (LocaleCompare(keyword,"bzero") == 0)
369 fits_info.zero=StringToDouble(p,(char **) NULL);
370 if (LocaleCompare(keyword,"bscale") == 0)
371 fits_info.scale=StringToDouble(p,(char **) NULL);
372 if (LocaleCompare(keyword,"comment") == 0)
374 if (comment == (char *) NULL)
375 comment=ConstantString(p);
377 (void) ConcatenateString(&comment,p);
379 if (LocaleCompare(keyword,"xendian") == 0)
381 if (LocaleNCompare(p,"big",3) == 0)
382 fits_info.endian=MSBEndian;
384 fits_info.endian=LSBEndian;
386 (void) FormatLocaleString(property,MagickPathExtent,"fits:%s",keyword);
387 (void) SetImageProperty(image,property,p,exception);
390 while (((TellBlob(image) % FITSBlocksize) != 0) && (c != EOF))
391 c=ReadBlobByte(image);
392 if (fits_info.extend == MagickFalse)
394 if ((fits_info.bits_per_pixel != 8) && (fits_info.bits_per_pixel != 16) &&
395 (fits_info.bits_per_pixel != 32) && (fits_info.bits_per_pixel != 64) &&
396 (fits_info.bits_per_pixel != -32) && (fits_info.bits_per_pixel != -64))
397 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
398 number_pixels=(MagickSizeType) fits_info.columns*fits_info.rows;
399 if ((fits_info.simple != MagickFalse) && (fits_info.number_axes >= 1) &&
400 (fits_info.number_axes <= 4) && (number_pixels != 0))
404 Verify that required image information is defined.
406 if (comment != (char *) NULL)
408 (void) SetImageProperty(image,"comment",comment,exception);
409 comment=DestroyString(comment);
411 if (EOFBlob(image) != MagickFalse)
412 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
414 number_pixels=(MagickSizeType) fits_info.columns*fits_info.rows;
415 if ((fits_info.simple == MagickFalse) || (fits_info.number_axes < 1) ||
416 (fits_info.number_axes > 4) || (number_pixels == 0))
417 ThrowReaderException(CorruptImageError,"ImageTypeNotSupported");
418 for (scene=0; scene < (ssize_t) fits_info.number_planes; scene++)
420 image->columns=(size_t) fits_info.columns;
421 image->rows=(size_t) fits_info.rows;
422 image->depth=(size_t) (fits_info.bits_per_pixel < 0 ? -1 : 1)*
423 fits_info.bits_per_pixel;
424 image->endian=fits_info.endian;
425 image->scene=(size_t) scene;
426 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
427 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
429 status=SetImageExtent(image,image->columns,image->rows,exception);
430 if (status == MagickFalse)
431 return(DestroyImageList(image));
433 Initialize image structure.
435 (void) SetImageColorspace(image,GRAYColorspace,exception);
436 if ((fits_info.min_data == 0.0) && (fits_info.max_data == 0.0))
438 if ((fits_info.bits_per_pixel == -32) ||
439 (fits_info.bits_per_pixel == -64))
440 (void) GetFITSPixelExtrema(image,fits_info.bits_per_pixel,
441 &fits_info.min_data,&fits_info.max_data);
443 fits_info.max_data=GetFITSPixelRange((size_t)
444 fits_info.bits_per_pixel);
447 fits_info.max_data=GetFITSPixelRange((size_t) fits_info.bits_per_pixel);
449 Convert FITS pixels to pixel packets.
451 scale=QuantumRange/(fits_info.max_data-fits_info.min_data);
452 for (y=(ssize_t) image->rows-1; y >= 0; y--)
454 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
455 if (q == (Quantum *) NULL)
457 for (x=0; x < (ssize_t) image->columns; x++)
459 pixel=GetFITSPixel(image,fits_info.bits_per_pixel);
460 if ((image->depth == 16) || (image->depth == 32) ||
461 (image->depth == 64))
462 SetFITSUnsignedPixels(1,image->depth,image->endian,
463 (unsigned char *) &pixel);
464 SetPixelGray(image,ClampToQuantum(scale*(fits_info.scale*(pixel-
465 fits_info.min_data)+fits_info.zero)),q);
466 q+=GetPixelChannels(image);
468 if (SyncAuthenticPixels(image,exception) == MagickFalse)
470 if (image->previous == (Image *) NULL)
472 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
474 if (status == MagickFalse)
478 if (EOFBlob(image) != MagickFalse)
480 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
485 Proceed to next image.
487 if (image_info->number_scenes != 0)
488 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
490 if (scene < (ssize_t) (fits_info.number_planes-1))
493 Allocate next image structure.
495 AcquireNextImage(image_info,image,exception);
496 if (GetNextImageInList(image) == (Image *) NULL)
498 image=DestroyImageList(image);
499 return((Image *) NULL);
501 image=SyncNextImageInList(image);
502 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
504 if (status == MagickFalse)
508 (void) CloseBlob(image);
509 return(GetFirstImageInList(image));
513 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
517 % R e g i s t e r F I T S I m a g e %
521 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
523 % RegisterFITSImage() adds attributes for the FITS image format to
524 % the list of supported formats. The attributes include the image format
525 % tag, a method to read and/or write the format, whether the format
526 % supports the saving of more than one frame to the same file or blob,
527 % whether the format supports native in-memory I/O, and a brief
528 % description of the format.
530 % The format of the RegisterFITSImage method is:
532 % size_t RegisterFITSImage(void)
535 ModuleExport size_t RegisterFITSImage(void)
540 entry=AcquireMagickInfo("FITS","FITS","Flexible Image Transport System");
541 entry->decoder=(DecodeImageHandler *) ReadFITSImage;
542 entry->encoder=(EncodeImageHandler *) WriteFITSImage;
543 entry->magick=(IsImageFormatHandler *) IsFITS;
544 entry->flags^=CoderAdjoinFlag;
545 entry->flags|=CoderDecoderSeekableStreamFlag;
546 (void) RegisterMagickInfo(entry);
547 entry=AcquireMagickInfo("FITS","FTS","Flexible Image Transport System");
548 entry->decoder=(DecodeImageHandler *) ReadFITSImage;
549 entry->encoder=(EncodeImageHandler *) WriteFITSImage;
550 entry->magick=(IsImageFormatHandler *) IsFITS;
551 entry->flags^=CoderAdjoinFlag;
552 entry->flags|=CoderDecoderSeekableStreamFlag;
553 (void) RegisterMagickInfo(entry);
554 return(MagickImageCoderSignature);
558 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
562 % U n r e g i s t e r F I T S I m a g e %
566 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
568 % UnregisterFITSImage() removes format registrations made by the
569 % FITS module from the list of supported formats.
571 % The format of the UnregisterFITSImage method is:
573 % UnregisterFITSImage(void)
576 ModuleExport void UnregisterFITSImage(void)
578 (void) UnregisterMagickInfo("FITS");
579 (void) UnregisterMagickInfo("FTS");
583 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
587 % W r i t e F I T S I m a g e %
591 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
593 % WriteFITSImage() writes a Flexible Image Transport System image to a
594 % file as gray scale intensities [0..255].
596 % The format of the WriteFITSImage method is:
598 % MagickBooleanType WriteFITSImage(const ImageInfo *image_info,
599 % Image *image,ExceptionInfo *exception)
601 % A description of each parameter follows.
603 % o image_info: the image info.
605 % o image: The image.
607 % o exception: return any errors or warnings in this structure.
610 static MagickBooleanType WriteFITSImage(const ImageInfo *image_info,
611 Image *image,ExceptionInfo *exception)
615 header[FITSBlocksize],
624 register const Quantum
639 Open output image file.
641 assert(image_info != (const ImageInfo *) NULL);
642 assert(image_info->signature == MagickCoreSignature);
643 assert(image != (Image *) NULL);
644 assert(image->signature == MagickCoreSignature);
645 if (image->debug != MagickFalse)
646 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
647 assert(exception != (ExceptionInfo *) NULL);
648 assert(exception->signature == MagickCoreSignature);
649 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
650 if (status == MagickFalse)
652 (void) TransformImageColorspace(image,sRGBColorspace,exception);
654 Allocate image memory.
656 fits_info=(char *) AcquireQuantumMemory(FITSBlocksize,sizeof(*fits_info));
657 if (fits_info == (char *) NULL)
658 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
659 (void) ResetMagickMemory(fits_info,' ',FITSBlocksize*sizeof(*fits_info));
661 Initialize image header.
663 image->depth=GetImageQuantumDepth(image,MagickFalse);
664 image->endian=MSBEndian;
665 quantum_info=AcquireQuantumInfo(image_info,image);
666 if (quantum_info == (QuantumInfo *) NULL)
667 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
669 (void) FormatLocaleString(header,FITSBlocksize,
671 (void) strncpy(fits_info+offset,header,strlen(header));
673 (void) FormatLocaleString(header,FITSBlocksize,"BITPIX = %10ld",
674 (long) ((quantum_info->format == FloatingPointQuantumFormat ? -1 : 1)*
676 (void) strncpy(fits_info+offset,header,strlen(header));
678 (void) FormatLocaleString(header,FITSBlocksize,"NAXIS = %10lu",
679 SetImageGray(image,exception) != MagickFalse ? 2UL : 3UL);
680 (void) strncpy(fits_info+offset,header,strlen(header));
682 (void) FormatLocaleString(header,FITSBlocksize,"NAXIS1 = %10lu",
683 (unsigned long) image->columns);
684 (void) strncpy(fits_info+offset,header,strlen(header));
686 (void) FormatLocaleString(header,FITSBlocksize,"NAXIS2 = %10lu",
687 (unsigned long) image->rows);
688 (void) strncpy(fits_info+offset,header,strlen(header));
690 if (SetImageGray(image,exception) == MagickFalse)
692 (void) FormatLocaleString(header,FITSBlocksize,
693 "NAXIS3 = %10lu",3UL);
694 (void) strncpy(fits_info+offset,header,strlen(header));
697 (void) FormatLocaleString(header,FITSBlocksize,"BSCALE = %E",1.0);
698 (void) strncpy(fits_info+offset,header,strlen(header));
700 (void) FormatLocaleString(header,FITSBlocksize,"BZERO = %E",
701 image->depth > 8 ? GetFITSPixelRange(image->depth)/2.0 : 0.0);
702 (void) strncpy(fits_info+offset,header,strlen(header));
704 (void) FormatLocaleString(header,FITSBlocksize,"DATAMAX = %E",
705 1.0*((MagickOffsetType) GetQuantumRange(image->depth)));
706 (void) strncpy(fits_info+offset,header,strlen(header));
708 (void) FormatLocaleString(header,FITSBlocksize,"DATAMIN = %E",0.0);
709 (void) strncpy(fits_info+offset,header,strlen(header));
711 if (image->endian == LSBEndian)
713 (void) FormatLocaleString(header,FITSBlocksize,"XENDIAN = 'SMALL'");
714 (void) strncpy(fits_info+offset,header,strlen(header));
717 url=GetMagickHomeURL();
718 (void) FormatLocaleString(header,FITSBlocksize,"HISTORY %.72s",url);
719 url=DestroyString(url);
720 (void) strncpy(fits_info+offset,header,strlen(header));
722 (void) strncpy(header,"END",FITSBlocksize);
723 (void) strncpy(fits_info+offset,header,strlen(header));
725 (void) WriteBlob(image,FITSBlocksize,(unsigned char *) fits_info);
727 Convert image to fits scale PseudoColor class.
729 pixels=(unsigned char *) GetQuantumPixels(quantum_info);
730 if (SetImageGray(image,exception) != MagickFalse)
732 length=GetQuantumExtent(image,quantum_info,GrayQuantum);
733 for (y=(ssize_t) image->rows-1; y >= 0; y--)
735 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
736 if (p == (const Quantum *) NULL)
738 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
739 GrayQuantum,pixels,exception);
740 if (image->depth == 16)
741 SetFITSUnsignedPixels(image->columns,image->depth,image->endian,
743 if (((image->depth == 32) || (image->depth == 64)) &&
744 (quantum_info->format != FloatingPointQuantumFormat))
745 SetFITSUnsignedPixels(image->columns,image->depth,image->endian,
747 count=WriteBlob(image,length,pixels);
748 if (count != (ssize_t) length)
750 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
752 if (status == MagickFalse)
758 length=GetQuantumExtent(image,quantum_info,RedQuantum);
759 for (y=(ssize_t) image->rows-1; y >= 0; y--)
761 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
762 if (p == (const Quantum *) NULL)
764 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
765 RedQuantum,pixels,exception);
766 if (image->depth == 16)
767 SetFITSUnsignedPixels(image->columns,image->depth,image->endian,
769 if (((image->depth == 32) || (image->depth == 64)) &&
770 (quantum_info->format != FloatingPointQuantumFormat))
771 SetFITSUnsignedPixels(image->columns,image->depth,image->endian,
773 count=WriteBlob(image,length,pixels);
774 if (count != (ssize_t) length)
776 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
778 if (status == MagickFalse)
781 length=GetQuantumExtent(image,quantum_info,GreenQuantum);
782 for (y=(ssize_t) image->rows-1; y >= 0; y--)
784 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
785 if (p == (const Quantum *) NULL)
787 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
788 GreenQuantum,pixels,exception);
789 if (image->depth == 16)
790 SetFITSUnsignedPixels(image->columns,image->depth,image->endian,
792 if (((image->depth == 32) || (image->depth == 64)) &&
793 (quantum_info->format != FloatingPointQuantumFormat))
794 SetFITSUnsignedPixels(image->columns,image->depth,image->endian,
796 count=WriteBlob(image,length,pixels);
797 if (count != (ssize_t) length)
799 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
801 if (status == MagickFalse)
804 length=GetQuantumExtent(image,quantum_info,BlueQuantum);
805 for (y=(ssize_t) image->rows-1; y >= 0; y--)
807 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
808 if (p == (const Quantum *) NULL)
810 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
811 BlueQuantum,pixels,exception);
812 if (image->depth == 16)
813 SetFITSUnsignedPixels(image->columns,image->depth,image->endian,
815 if (((image->depth == 32) || (image->depth == 64)) &&
816 (quantum_info->format != FloatingPointQuantumFormat))
817 SetFITSUnsignedPixels(image->columns,image->depth,image->endian,
819 count=WriteBlob(image,length,pixels);
820 if (count != (ssize_t) length)
822 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
824 if (status == MagickFalse)
828 quantum_info=DestroyQuantumInfo(quantum_info);
829 length=(size_t) (FITSBlocksize-TellBlob(image) % FITSBlocksize);
832 (void) ResetMagickMemory(fits_info,0,length*sizeof(*fits_info));
833 (void) WriteBlob(image,length,(unsigned char *) fits_info);
835 fits_info=DestroyString(fits_info);
836 (void) CloseBlob(image);