]> granicus.if.org Git - imagemagick/blob - coders/fits.c
Horizon validity (anti-aliased) added to Plane2Cylinder
[imagemagick] / coders / fits.c
1 /*
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %                                                                             %
4 %                                                                             %
5 %                                                                             %
6 %                        FFFFF  IIIII  TTTTT  SSSSS                           %
7 %                        F        I      T    SS                              %
8 %                        FFF      I      T     SSS                            %
9 %                        F        I      T       SS                           %
10 %                        F      IIIII    T    SSSSS                           %
11 %                                                                             %
12 %                                                                             %
13 %            Read/Write Flexible Image Transport System Images.               %
14 %                                                                             %
15 %                              Software Design                                %
16 %                                John Cristy                                  %
17 %                                 July 1992                                   %
18 %                                                                             %
19 %                                                                             %
20 %  Copyright 1999-2011 ImageMagick Studio LLC, a non-profit organization      %
21 %  dedicated to making software imaging solutions freely available.           %
22 %                                                                             %
23 %  You may not use this file except in compliance with the License.  You may  %
24 %  obtain a copy of the License at                                            %
25 %                                                                             %
26 %    http://www.imagemagick.org/script/license.php                            %
27 %                                                                             %
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.                                             %
33 %                                                                             %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35 %
36 %
37 */
38 \f
39 /*
40   Include declarations.
41 */
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"
68 \f
69 /*
70   Forward declarations.
71 */
72 #define FITSBlocksize  2880UL
73 \f
74 /*
75   Forward declarations.
76 */
77 static MagickBooleanType
78   WriteFITSImage(const ImageInfo *,Image *);
79 \f
80 /*
81 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
82 %                                                                             %
83 %                                                                             %
84 %                                                                             %
85 %   I s F I T S                                                               %
86 %                                                                             %
87 %                                                                             %
88 %                                                                             %
89 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
90 %
91 %  IsFITS() returns MagickTrue if the image format type, identified by the
92 %  magick string, is FITS.
93 %
94 %  The format of the IsFITS method is:
95 %
96 %      MagickBooleanType IsFITS(const unsigned char *magick,const size_t length)
97 %
98 %  A description of each parameter follows:
99 %
100 %    o magick: compare image format pattern against these bytes.
101 %
102 %    o length: Specifies the length of the magick string.
103 %
104 */
105 static MagickBooleanType IsFITS(const unsigned char *magick,const size_t length)
106 {
107   if (length < 6)
108     return(MagickFalse);
109   if (LocaleNCompare((const char *) magick,"IT0",3) == 0)
110     return(MagickTrue);
111   if (LocaleNCompare((const char *) magick,"SIMPLE",6) == 0)
112     return(MagickTrue);
113   return(MagickFalse);
114 }
115 \f
116 /*
117 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
118 %                                                                             %
119 %                                                                             %
120 %                                                                             %
121 %   R e a d F I T S I m a g e                                                 %
122 %                                                                             %
123 %                                                                             %
124 %                                                                             %
125 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
126 %
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
129 %  new image.
130 %
131 %  The format of the ReadFITSImage method is:
132 %
133 %      Image *ReadFITSImage(const ImageInfo *image_info,
134 %        ExceptionInfo *exception)
135 %
136 %  A description of each parameter follows:
137 %
138 %    o image_info: the image info.
139 %
140 %    o exception: return any errors or warnings in this structure.
141 %
142 */
143
144 static inline double GetFITSPixel(Image *image,int bits_per_pixel)
145 {
146   switch (image->depth >> 3)
147   {
148     case 1:
149       return((double) ReadBlobByte(image));
150     case 2:
151       return((double) ((short) ReadBlobShort(image)));
152     case 4:
153     {
154       if (bits_per_pixel > 0)
155         return((double) ((int) ReadBlobLong(image)));
156       return((double) ReadBlobFloat(image));
157     }
158     case 8:
159     {
160       if (bits_per_pixel > 0)
161         return((double) ((MagickOffsetType) ReadBlobLongLong(image)));
162     }
163     default:
164       break;
165   }
166   return(ReadBlobDouble(image));
167 }
168
169 static void GetFITSPixelExtrema(Image *image,const int bits_per_pixel,
170   double *minima,double *maxima)
171 {
172   double
173     pixel;
174
175   MagickOffsetType
176     offset;
177
178   MagickSizeType
179     number_pixels;
180
181   register MagickOffsetType
182     i;
183
184   offset=TellBlob(image);
185   number_pixels=(MagickSizeType) image->columns*image->rows;
186   *minima=GetFITSPixel(image,bits_per_pixel);
187   *maxima=(*minima);
188   for (i=1; i < (MagickOffsetType) number_pixels; i++)
189   {
190     pixel=GetFITSPixel(image,bits_per_pixel);
191     if (pixel < *minima)
192       *minima=pixel;
193     if (pixel > *maxima)
194       *maxima=pixel;
195   }
196   (void) SeekBlob(image,offset,SEEK_SET);
197 }
198
199 static inline double GetFITSPixelRange(const size_t depth)
200 {
201   return((double) ((MagickOffsetType) GetQuantumRange(depth)));
202 }
203
204 static void SetFITSUnsignedPixels(const size_t length,
205   const size_t bits_per_pixel,unsigned char *pixels)
206 {
207   register ssize_t
208     i;
209
210   for (i=0; i < (ssize_t) length; i++)
211   {
212     *pixels^=0x80;
213     pixels+=bits_per_pixel >> 3;
214   }
215 }
216
217 static Image *ReadFITSImage(const ImageInfo *image_info,
218   ExceptionInfo *exception)
219 {
220   typedef struct _FITSInfo
221   {
222     MagickBooleanType
223       extend,
224       simple;
225
226     int
227       bits_per_pixel,
228       columns,
229       rows,
230       number_axes,
231       number_planes;
232
233     double
234       min_data,
235       max_data,
236       zero,
237       scale;
238
239     EndianType
240       endian;
241   } FITSInfo;
242
243   char
244     *comment,
245     keyword[9],
246     property[MaxTextExtent],
247     value[73];
248
249   double
250     pixel,
251     scale;
252
253   FITSInfo
254     fits_info;
255
256   Image
257     *image;
258
259   int
260     c;
261
262   MagickBooleanType
263     status;
264
265   MagickSizeType
266     number_pixels;
267
268   register ssize_t
269     i,
270     x;
271
272   register PixelPacket
273     *q;
274
275   ssize_t
276     count,
277     scene,
278     y;
279
280   /*
281     Open image file.
282   */
283   assert(image_info != (const ImageInfo *) NULL);
284   assert(image_info->signature == MagickSignature);
285   if (image_info->debug != MagickFalse)
286     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
287       image_info->filename);
288   assert(exception != (ExceptionInfo *) NULL);
289   assert(exception->signature == MagickSignature);
290   image=AcquireImage(image_info);
291   status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
292   if (status == MagickFalse)
293     {
294       image=DestroyImageList(image);
295       return((Image *) NULL);
296     }
297   /*
298     Initialize image header.
299   */
300   (void) ResetMagickMemory(&fits_info,0,sizeof(fits_info));
301   fits_info.extend=MagickFalse;
302   fits_info.simple=MagickFalse;
303   fits_info.bits_per_pixel=8;
304   fits_info.columns=1;
305   fits_info.rows=1;
306   fits_info.rows=1;
307   fits_info.number_planes=1;
308   fits_info.min_data=0.0;
309   fits_info.max_data=0.0;
310   fits_info.zero=0.0;
311   fits_info.scale=1.0;
312   fits_info.endian=MSBEndian;
313   /*
314     Decode image header.
315   */
316   for (comment=(char *) NULL; EOFBlob(image) == MagickFalse; )
317   {
318     for ( ; EOFBlob(image) == MagickFalse; )
319     {
320       register char
321         *p;
322
323       count=ReadBlob(image,8,(unsigned char *) keyword);
324       if (count != 8)
325         break;
326       for (i=0; i < 8; i++)
327       {
328         if (isspace((int) ((unsigned char) keyword[i])) != 0)
329           break;
330         keyword[i]=tolower((int) ((unsigned char) keyword[i]));
331       }
332       keyword[i]='\0';
333       count=ReadBlob(image,72,(unsigned char *) value);
334       if (count != 72)
335         break;
336       value[72]='\0';
337       p=value;
338       if (*p == '=')
339         {
340           p+=2;
341           while (isspace((int) ((unsigned char) *p)) != 0)
342             p++;
343         }
344       if (LocaleCompare(keyword,"end") == 0)
345         break;
346       if (LocaleCompare(keyword,"extend") == 0)
347         fits_info.extend=(*p == 'T') || (*p == 't') ? MagickTrue : MagickFalse;
348       if (LocaleCompare(keyword,"simple") == 0)
349         fits_info.simple=(*p == 'T') || (*p == 't') ? MagickTrue : MagickFalse;
350       if (LocaleCompare(keyword,"bitpix") == 0)
351         fits_info.bits_per_pixel=StringToLong(p);
352       if (LocaleCompare(keyword,"naxis") == 0)
353         fits_info.number_axes=StringToLong(p);
354       if (LocaleCompare(keyword,"naxis1") == 0)
355         fits_info.columns=StringToLong(p);
356       if (LocaleCompare(keyword,"naxis2") == 0)
357         fits_info.rows=StringToLong(p);
358       if (LocaleCompare(keyword,"naxis3") == 0)
359         fits_info.number_planes=StringToLong(p);
360       if (LocaleCompare(keyword,"datamax") == 0)
361         fits_info.max_data=InterpretLocaleValue(p,(char **) NULL);
362       if (LocaleCompare(keyword,"datamin") == 0)
363         fits_info.min_data=InterpretLocaleValue(p,(char **) NULL);
364       if (LocaleCompare(keyword,"bzero") == 0)
365         fits_info.zero=InterpretLocaleValue(p,(char **) NULL);
366       if (LocaleCompare(keyword,"bscale") == 0)
367         fits_info.scale=InterpretLocaleValue(p,(char **) NULL);
368       if (LocaleCompare(keyword,"comment") == 0)
369         {
370           if (comment == (char *) NULL)
371             comment=ConstantString(p);
372           else
373             (void) ConcatenateString(&comment,p);
374         }
375       if (LocaleCompare(keyword,"xendian") == 0)
376         {
377           if (LocaleNCompare(p,"big",3) == 0)
378             fits_info.endian=MSBEndian;
379           else
380             fits_info.endian=LSBEndian;
381         }
382       (void) FormatLocaleString(property,MaxTextExtent,"fits:%s",keyword);
383       (void) SetImageProperty(image,property,p);
384     }
385     c=0;
386     while (((TellBlob(image) % FITSBlocksize) != 0) && (c != EOF))
387       c=ReadBlobByte(image);
388     if (fits_info.extend == MagickFalse)
389       break;
390     number_pixels=(MagickSizeType) fits_info.columns*fits_info.rows;
391     if ((fits_info.simple != MagickFalse) && (fits_info.number_axes >= 1) &&
392         (fits_info.number_axes <= 4) && (number_pixels != 0))
393       break;
394   }
395   /*
396     Verify that required image information is defined.
397   */
398   if (comment != (char *) NULL)
399     {
400       (void) SetImageProperty(image,"comment",comment);
401       comment=DestroyString(comment);
402     }
403   if (EOFBlob(image) != MagickFalse)
404     ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
405       image->filename);
406   number_pixels=(MagickSizeType) fits_info.columns*fits_info.rows;
407   if ((fits_info.simple == MagickFalse) || (fits_info.number_axes < 1) ||
408       (fits_info.number_axes > 4) || (number_pixels == 0))
409     ThrowReaderException(CorruptImageError,"ImageTypeNotSupported");
410   for (scene=0; scene < (ssize_t) fits_info.number_planes; scene++)
411   {
412     image->columns=(size_t) fits_info.columns;
413     image->rows=(size_t) fits_info.rows;
414     image->depth=(size_t) (fits_info.bits_per_pixel < 0 ? -1 : 1)*
415       fits_info.bits_per_pixel;
416     image->endian=fits_info.endian;
417     image->scene=(size_t) scene;
418     if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
419       if (image->scene >= (image_info->scene+image_info->number_scenes-1))
420         break;
421     /*
422       Initialize image structure.
423     */
424     if ((fits_info.min_data != 0.0) || (fits_info.max_data != 0.0))
425       {
426         if ((fits_info.bits_per_pixel != 0) && (fits_info.max_data == 0.0))
427           fits_info.max_data=GetFITSPixelRange((size_t)
428             fits_info.bits_per_pixel);
429       }
430     else
431       GetFITSPixelExtrema(image,fits_info.bits_per_pixel,&fits_info.min_data,
432         &fits_info.max_data);
433     /*
434       Convert FITS pixels to pixel packets.
435     */
436     scale=(double) QuantumRange/(fits_info.scale*(fits_info.max_data-
437       fits_info.min_data)+fits_info.zero);
438     for (y=(ssize_t) image->rows-1; y >= 0; y--)
439     {
440       q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
441       if (q == (PixelPacket *) NULL)
442         break;
443       for (x=0; x < (ssize_t) image->columns; x++)
444       {
445         pixel=GetFITSPixel(image,fits_info.bits_per_pixel);
446         SetRedPixelComponent(q,ClampToQuantum(scale*(fits_info.scale*(pixel-
447           fits_info.min_data)+fits_info.zero)));
448         SetGreenPixelComponent(q,GetRedPixelComponent(q));
449         SetBluePixelComponent(q,GetRedPixelComponent(q));
450         q++;
451       }
452       if (SyncAuthenticPixels(image,exception) == MagickFalse)
453         break;
454       if (image->previous == (Image *) NULL)
455         {
456           status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
457                 image->rows);
458           if (status == MagickFalse)
459             break;
460         }
461     }
462     if (EOFBlob(image) != MagickFalse)
463       {
464         ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
465           image->filename);
466         break;
467       }
468     /*
469       Proceed to next image.
470     */
471     if (image_info->number_scenes != 0)
472       if (image->scene >= (image_info->scene+image_info->number_scenes-1))
473         break;
474     if (scene < (ssize_t) (fits_info.number_planes-1))
475       {
476         /*
477           Allocate next image structure.
478         */
479         AcquireNextImage(image_info,image);
480         if (GetNextImageInList(image) == (Image *) NULL)
481           {
482             image=DestroyImageList(image);
483             return((Image *) NULL);
484           }
485         image=SyncNextImageInList(image);
486         status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
487           GetBlobSize(image));
488         if (status == MagickFalse)
489           break;
490       }
491   }
492   (void) CloseBlob(image);
493   return(GetFirstImageInList(image));
494 }
495 \f
496 /*
497 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
498 %                                                                             %
499 %                                                                             %
500 %                                                                             %
501 %   R e g i s t e r F I T S I m a g e                                         %
502 %                                                                             %
503 %                                                                             %
504 %                                                                             %
505 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
506 %
507 %  RegisterFITSImage() adds attributes for the FITS image format to
508 %  the list of supported formats.  The attributes include the image format
509 %  tag, a method to read and/or write the format, whether the format
510 %  supports the saving of more than one frame to the same file or blob,
511 %  whether the format supports native in-memory I/O, and a brief
512 %  description of the format.
513 %
514 %  The format of the RegisterFITSImage method is:
515 %
516 %      size_t RegisterFITSImage(void)
517 %
518 */
519 ModuleExport size_t RegisterFITSImage(void)
520 {
521   MagickInfo
522     *entry;
523
524   entry=SetMagickInfo("FITS");
525   entry->decoder=(DecodeImageHandler *) ReadFITSImage;
526   entry->encoder=(EncodeImageHandler *) WriteFITSImage;
527   entry->magick=(IsImageFormatHandler *) IsFITS;
528   entry->adjoin=MagickFalse;
529   entry->seekable_stream=MagickTrue;
530   entry->description=ConstantString("Flexible Image Transport System");
531   entry->module=ConstantString("FITS");
532   (void) RegisterMagickInfo(entry);
533   entry=SetMagickInfo("FTS");
534   entry->decoder=(DecodeImageHandler *) ReadFITSImage;
535   entry->encoder=(EncodeImageHandler *) WriteFITSImage;
536   entry->magick=(IsImageFormatHandler *) IsFITS;
537   entry->adjoin=MagickFalse;
538   entry->seekable_stream=MagickTrue;
539   entry->description=ConstantString("Flexible Image Transport System");
540   entry->module=ConstantString("FTS");
541   (void) RegisterMagickInfo(entry);
542   return(MagickImageCoderSignature);
543 }
544 \f
545 /*
546 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
547 %                                                                             %
548 %                                                                             %
549 %                                                                             %
550 %   U n r e g i s t e r F I T S I m a g e                                     %
551 %                                                                             %
552 %                                                                             %
553 %                                                                             %
554 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
555 %
556 %  UnregisterFITSImage() removes format registrations made by the
557 %  FITS module from the list of supported formats.
558 %
559 %  The format of the UnregisterFITSImage method is:
560 %
561 %      UnregisterFITSImage(void)
562 %
563 */
564 ModuleExport void UnregisterFITSImage(void)
565 {
566   (void) UnregisterMagickInfo("FITS");
567   (void) UnregisterMagickInfo("FTS");
568 }
569 \f
570 /*
571 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
572 %                                                                             %
573 %                                                                             %
574 %                                                                             %
575 %   W r i t e F I T S I m a g e                                               %
576 %                                                                             %
577 %                                                                             %
578 %                                                                             %
579 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
580 %
581 %  WriteFITSImage() writes a Flexible Image Transport System image to a
582 %  file as gray scale intensities [0..255].
583 %
584 %  The format of the WriteFITSImage method is:
585 %
586 %      MagickBooleanType WriteFITSImage(const ImageInfo *image_info,
587 %        Image *image)
588 %
589 %  A description of each parameter follows.
590 %
591 %    o image_info: the image info.
592 %
593 %    o image:  The image.
594 %
595 */
596 static MagickBooleanType WriteFITSImage(const ImageInfo *image_info,
597   Image *image)
598 {
599   char
600     header[FITSBlocksize],
601     *fits_info;
602
603   MagickBooleanType
604     status;
605
606   QuantumInfo
607     *quantum_info;
608
609   register const PixelPacket
610     *p;
611
612   size_t
613     length;
614
615   ssize_t
616     count,
617     offset,
618     y;
619
620   unsigned char
621     *pixels;
622
623   /*
624     Open output image file.
625   */
626   assert(image_info != (const ImageInfo *) NULL);
627   assert(image_info->signature == MagickSignature);
628   assert(image != (Image *) NULL);
629   assert(image->signature == MagickSignature);
630   if (image->debug != MagickFalse)
631     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
632   status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
633   if (status == MagickFalse)
634     return(status);
635   if (image->colorspace != RGBColorspace)
636     (void) TransformImageColorspace(image,RGBColorspace);
637   /*
638     Allocate image memory.
639   */
640   fits_info=(char *) AcquireQuantumMemory(FITSBlocksize,sizeof(*fits_info));
641   if (fits_info == (char *) NULL)
642     ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
643   (void) ResetMagickMemory(fits_info,' ',FITSBlocksize*sizeof(*fits_info));
644   /*
645     Initialize image header.
646   */
647   image->depth=GetImageQuantumDepth(image,MagickFalse);
648   quantum_info=AcquireQuantumInfo((const ImageInfo *) NULL,image);
649   if (quantum_info == (QuantumInfo *) NULL)
650     ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
651   offset=0;
652   (void) FormatLocaleString(header,FITSBlocksize,
653     "SIMPLE  =                    T");
654   (void) strncpy(fits_info+offset,header,strlen(header));
655   offset+=80;
656   (void) FormatLocaleString(header,FITSBlocksize,"BITPIX  =           %10ld",
657     (long) (quantum_info->format == FloatingPointQuantumFormat ? -1 : 1)*
658     image->depth);
659   (void) strncpy(fits_info+offset,header,strlen(header));
660   offset+=80;
661   (void) FormatLocaleString(header,FITSBlocksize,"NAXIS   =           %10lu",
662     IsGrayImage(image,&image->exception) != MagickFalse ? 2UL : 3UL);
663   (void) strncpy(fits_info+offset,header,strlen(header));
664   offset+=80;
665   (void) FormatLocaleString(header,FITSBlocksize,"NAXIS1  =           %10lu",
666     (unsigned long) image->columns);
667   (void) strncpy(fits_info+offset,header,strlen(header));
668   offset+=80;
669   (void) FormatLocaleString(header,FITSBlocksize,"NAXIS2  =           %10lu",
670     (unsigned long) image->rows);
671   (void) strncpy(fits_info+offset,header,strlen(header));
672   offset+=80;
673   if (IsGrayImage(image,&image->exception) == MagickFalse)
674     {
675       (void) FormatLocaleString(header,FITSBlocksize,
676         "NAXIS3  =           %10lu",3UL);
677       (void) strncpy(fits_info+offset,header,strlen(header));
678       offset+=80;
679     }
680   (void) FormatLocaleString(header,FITSBlocksize,"BSCALE  =         %E",1.0);
681   (void) strncpy(fits_info+offset,header,strlen(header));
682   offset+=80;
683   (void) FormatLocaleString(header,FITSBlocksize,"BZERO   =         %E",
684     image->depth > 8 ? GetFITSPixelRange(image->depth) : 0.0);
685   (void) strncpy(fits_info+offset,header,strlen(header));
686   offset+=80;
687   (void) FormatLocaleString(header,FITSBlocksize,"DATAMAX =         %E",
688     1.0*((MagickOffsetType) GetQuantumRange(image->depth)));
689   (void) strncpy(fits_info+offset,header,strlen(header));
690   offset+=80;
691   (void) FormatLocaleString(header,FITSBlocksize,"DATAMIN =         %E",0.0);
692   (void) strncpy(fits_info+offset,header,strlen(header));
693   offset+=80;
694   if (image->endian == LSBEndian)
695     {
696       (void) FormatLocaleString(header,FITSBlocksize,"XENDIAN = 'SMALL'");
697       (void) strncpy(fits_info+offset,header,strlen(header));
698       offset+=80;
699     }
700   (void) FormatLocaleString(header,FITSBlocksize,"HISTORY %.72s",
701     GetMagickVersion((size_t *) NULL));
702   (void) strncpy(fits_info+offset,header,strlen(header));
703   offset+=80;
704   (void) strncpy(header,"END",FITSBlocksize);
705   (void) strncpy(fits_info+offset,header,strlen(header));
706   offset+=80;
707   (void) WriteBlob(image,FITSBlocksize,(unsigned char *) fits_info);
708   /*
709     Convert image to fits scale PseudoColor class.
710   */
711   pixels=GetQuantumPixels(quantum_info);
712   if (IsGrayImage(image,&image->exception) != MagickFalse)
713     {
714       length=GetQuantumExtent(image,quantum_info,GrayQuantum);
715       for (y=(ssize_t) image->rows-1; y >= 0; y--)
716       {
717         p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
718         if (p == (const PixelPacket *) NULL)
719           break;
720         length=ExportQuantumPixels(image,(const CacheView *) NULL,quantum_info,
721           GrayQuantum,pixels,&image->exception);
722         if (image->depth == 16)
723           SetFITSUnsignedPixels(image->columns,image->depth,pixels);
724         if (((image->depth == 32) || (image->depth == 64)) &&
725             (quantum_info->format != FloatingPointQuantumFormat))
726           SetFITSUnsignedPixels(image->columns,image->depth,pixels);
727         count=WriteBlob(image,length,pixels);
728         if (count != (ssize_t) length)
729           break;
730         status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
731           image->rows);
732         if (status == MagickFalse)
733           break;
734       }
735     }
736   else
737     {
738       length=GetQuantumExtent(image,quantum_info,RedQuantum);
739       for (y=(ssize_t) image->rows-1; y >= 0; y--)
740       {
741         p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
742         if (p == (const PixelPacket *) NULL)
743           break;
744         length=ExportQuantumPixels(image,(const CacheView *) NULL,quantum_info,
745           RedQuantum,pixels,&image->exception);
746         if (image->depth == 16)
747           SetFITSUnsignedPixels(image->columns,image->depth,pixels);
748         if (((image->depth == 32) || (image->depth == 64)) &&
749             (quantum_info->format != FloatingPointQuantumFormat))
750           SetFITSUnsignedPixels(image->columns,image->depth,pixels);
751         count=WriteBlob(image,length,pixels);
752         if (count != (ssize_t) length)
753           break;
754         status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
755           image->rows);
756         if (status == MagickFalse)
757           break;
758       }
759       length=GetQuantumExtent(image,quantum_info,GreenQuantum);
760       for (y=(ssize_t) image->rows-1; y >= 0; y--)
761       {
762         p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
763         if (p == (const PixelPacket *) NULL)
764           break;
765         length=ExportQuantumPixels(image,(const CacheView *) NULL,quantum_info,
766           GreenQuantum,pixels,&image->exception);
767         if (image->depth == 16)
768           SetFITSUnsignedPixels(image->columns,image->depth,pixels);
769         if (((image->depth == 32) || (image->depth == 64)) &&
770             (quantum_info->format != FloatingPointQuantumFormat))
771           SetFITSUnsignedPixels(image->columns,image->depth,pixels);
772         count=WriteBlob(image,length,pixels);
773         if (count != (ssize_t) length)
774           break;
775         status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
776           image->rows);
777         if (status == MagickFalse)
778           break;
779       }
780       length=GetQuantumExtent(image,quantum_info,BlueQuantum);
781       for (y=(ssize_t) image->rows-1; y >= 0; y--)
782       {
783         p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
784         if (p == (const PixelPacket *) NULL)
785           break;
786         length=ExportQuantumPixels(image,(const CacheView *) NULL,quantum_info,
787           BlueQuantum,pixels,&image->exception);
788         if (image->depth == 16)
789           SetFITSUnsignedPixels(image->columns,image->depth,pixels);
790         if (((image->depth == 32) || (image->depth == 64)) &&
791             (quantum_info->format != FloatingPointQuantumFormat))
792           SetFITSUnsignedPixels(image->columns,image->depth,pixels);
793         count=WriteBlob(image,length,pixels);
794         if (count != (ssize_t) length)
795           break;
796         status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
797           image->rows);
798         if (status == MagickFalse)
799           break;
800       }
801     }
802   quantum_info=DestroyQuantumInfo(quantum_info);
803   length=(size_t) (FITSBlocksize-TellBlob(image) % FITSBlocksize);
804   if (length != 0)
805     {
806       (void) ResetMagickMemory(fits_info,0,length*sizeof(*fits_info));
807       (void) WriteBlob(image,length,(unsigned char *) fits_info);
808     }
809   fits_info=DestroyString(fits_info);
810   (void) CloseBlob(image);
811   return(MagickTrue);
812 }