]> granicus.if.org Git - imagemagick/blob - coders/fits.c
(no commit message)
[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   ssize_t
263     scene,
264     y;
265
266   MagickBooleanType
267     status;
268
269   MagickSizeType
270     number_pixels;
271
272   register ssize_t
273     i,
274     x;
275
276   register PixelPacket
277     *q;
278
279   ssize_t
280     count;
281
282   /*
283     Open image file.
284   */
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)
295     {
296       image=DestroyImageList(image);
297       return((Image *) NULL);
298     }
299   /*
300     Initialize image header.
301   */
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;
306   fits_info.columns=1;
307   fits_info.rows=1;
308   fits_info.rows=1;
309   fits_info.number_planes=1;
310   fits_info.min_data=0.0;
311   fits_info.max_data=0.0;
312   fits_info.zero=0.0;
313   fits_info.scale=1.0;
314   fits_info.endian=MSBEndian;
315   /*
316     Decode image header.
317   */
318   for (comment=(char *) NULL; EOFBlob(image) == MagickFalse; )
319   {
320     for ( ; EOFBlob(image) == MagickFalse; )
321     {
322       register char
323         *p;
324
325       count=ReadBlob(image,8,(unsigned char *) keyword);
326       if (count != 8)
327         break;
328       for (i=0; i < 8; i++)
329       {
330         if (isspace((int) ((unsigned char) keyword[i])) != 0)
331           break;
332         keyword[i]=tolower((int) ((unsigned char) keyword[i]));
333       }
334       keyword[i]='\0';
335       count=ReadBlob(image,72,(unsigned char *) value);
336       if (count != 72)
337         break;
338       value[72]='\0';
339       p=value;
340       if (*p == '=')
341         {
342           p+=2;
343           while (isspace((int) ((unsigned char) *p)) != 0)
344             p++;
345         }
346       if (LocaleCompare(keyword,"end") == 0)
347         break;
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)
371         {
372           if (comment == (char *) NULL)
373             comment=ConstantString(p);
374           else
375             (void) ConcatenateString(&comment,p);
376         }
377       if (LocaleCompare(keyword,"xendian") == 0)
378         {
379           if (LocaleNCompare(p,"big",3) == 0)
380             fits_info.endian=MSBEndian;
381           else
382             fits_info.endian=LSBEndian;
383         }
384       (void) FormatMagickString(property,MaxTextExtent,"fits:%s",keyword);
385       (void) SetImageProperty(image,property,p);
386     }
387     c=0;
388     while (((TellBlob(image) % FITSBlocksize) != 0) && (c != EOF))
389       c=ReadBlobByte(image);
390     if (fits_info.extend == MagickFalse)
391       break;
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))
395       break;
396   }
397   /*
398     Verify that required image information is defined.
399   */
400   if (comment != (char *) NULL)
401     {
402       (void) SetImageProperty(image,"comment",comment);
403       comment=DestroyString(comment);
404     }
405   if (EOFBlob(image) != MagickFalse)
406     ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
407       image->filename);
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 < (ssize_t) fits_info.number_planes; scene++)
413   {
414     image->columns=(size_t) fits_info.columns;
415     image->rows=(size_t) fits_info.rows;
416     image->depth=(size_t) (fits_info.bits_per_pixel < 0 ? -1 : 1)*
417       fits_info.bits_per_pixel;
418     image->endian=fits_info.endian;
419     image->scene=(size_t) scene;
420     if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
421       if (image->scene >= (image_info->scene+image_info->number_scenes-1))
422         break;
423     /*
424       Initialize image structure.
425     */
426     if ((fits_info.min_data != 0.0) || (fits_info.max_data != 0.0))
427       {
428         if ((fits_info.bits_per_pixel != 0) && (fits_info.max_data == 0.0))
429           fits_info.max_data=GetFITSPixelRange((size_t)
430             fits_info.bits_per_pixel);
431       }
432     else
433       GetFITSPixelExtrema(image,fits_info.bits_per_pixel,&fits_info.min_data,
434         &fits_info.max_data);
435     /*
436       Convert FITS pixels to pixel packets.
437     */
438     scale=(double) QuantumRange/(fits_info.scale*(fits_info.max_data-
439       fits_info.min_data)+fits_info.zero);
440     for (y=(ssize_t) image->rows-1; y >= 0; y--)
441     {
442       q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
443       if (q == (PixelPacket *) NULL)
444         break;
445       for (x=0; x < (ssize_t) image->columns; x++)
446       {
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));
450         q->green=q->red;
451         q->blue=q->red;
452         q++;
453       }
454       if (SyncAuthenticPixels(image,exception) == MagickFalse)
455         break;
456       if (image->previous == (Image *) NULL)
457         {
458           status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
459                 image->rows);
460           if (status == MagickFalse)
461             break;
462         }
463     }
464     if (EOFBlob(image) != MagickFalse)
465       {
466         ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
467           image->filename);
468         break;
469       }
470     /*
471       Proceed to next image.
472     */
473     if (image_info->number_scenes != 0)
474       if (image->scene >= (image_info->scene+image_info->number_scenes-1))
475         break;
476     if (scene < (ssize_t) (fits_info.number_planes-1))
477       {
478         /*
479           Allocate next image structure.
480         */
481         AcquireNextImage(image_info,image);
482         if (GetNextImageInList(image) == (Image *) NULL)
483           {
484             image=DestroyImageList(image);
485             return((Image *) NULL);
486           }
487         image=SyncNextImageInList(image);
488         status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
489           GetBlobSize(image));
490         if (status == MagickFalse)
491           break;
492       }
493   }
494   (void) CloseBlob(image);
495   return(GetFirstImageInList(image));
496 }
497 \f
498 /*
499 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
500 %                                                                             %
501 %                                                                             %
502 %                                                                             %
503 %   R e g i s t e r F I T S I m a g e                                         %
504 %                                                                             %
505 %                                                                             %
506 %                                                                             %
507 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
508 %
509 %  RegisterFITSImage() adds attributes for the FITS image format to
510 %  the list of supported formats.  The attributes include the image format
511 %  tag, a method to read and/or write the format, whether the format
512 %  supports the saving of more than one frame to the same file or blob,
513 %  whether the format supports native in-memory I/O, and a brief
514 %  description of the format.
515 %
516 %  The format of the RegisterFITSImage method is:
517 %
518 %      size_t RegisterFITSImage(void)
519 %
520 */
521 ModuleExport size_t RegisterFITSImage(void)
522 {
523   MagickInfo
524     *entry;
525
526   entry=SetMagickInfo("FITS");
527   entry->decoder=(DecodeImageHandler *) ReadFITSImage;
528   entry->encoder=(EncodeImageHandler *) WriteFITSImage;
529   entry->magick=(IsImageFormatHandler *) IsFITS;
530   entry->adjoin=MagickFalse;
531   entry->seekable_stream=MagickTrue;
532   entry->description=ConstantString("Flexible Image Transport System");
533   entry->module=ConstantString("FITS");
534   (void) RegisterMagickInfo(entry);
535   entry=SetMagickInfo("FTS");
536   entry->decoder=(DecodeImageHandler *) ReadFITSImage;
537   entry->encoder=(EncodeImageHandler *) WriteFITSImage;
538   entry->magick=(IsImageFormatHandler *) IsFITS;
539   entry->adjoin=MagickFalse;
540   entry->seekable_stream=MagickTrue;
541   entry->description=ConstantString("Flexible Image Transport System");
542   entry->module=ConstantString("FTS");
543   (void) RegisterMagickInfo(entry);
544   return(MagickImageCoderSignature);
545 }
546 \f
547 /*
548 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
549 %                                                                             %
550 %                                                                             %
551 %                                                                             %
552 %   U n r e g i s t e r F I T S I m a g e                                     %
553 %                                                                             %
554 %                                                                             %
555 %                                                                             %
556 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
557 %
558 %  UnregisterFITSImage() removes format registrations made by the
559 %  FITS module from the list of supported formats.
560 %
561 %  The format of the UnregisterFITSImage method is:
562 %
563 %      UnregisterFITSImage(void)
564 %
565 */
566 ModuleExport void UnregisterFITSImage(void)
567 {
568   (void) UnregisterMagickInfo("FITS");
569   (void) UnregisterMagickInfo("FTS");
570 }
571 \f
572 /*
573 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
574 %                                                                             %
575 %                                                                             %
576 %                                                                             %
577 %   W r i t e F I T S I m a g e                                               %
578 %                                                                             %
579 %                                                                             %
580 %                                                                             %
581 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
582 %
583 %  WriteFITSImage() writes a Flexible Image Transport System image to a
584 %  file as gray scale intensities [0..255].
585 %
586 %  The format of the WriteFITSImage method is:
587 %
588 %      MagickBooleanType WriteFITSImage(const ImageInfo *image_info,
589 %        Image *image)
590 %
591 %  A description of each parameter follows.
592 %
593 %    o image_info: the image info.
594 %
595 %    o image:  The image.
596 %
597 */
598 static MagickBooleanType WriteFITSImage(const ImageInfo *image_info,
599   Image *image)
600 {
601   char
602     header[FITSBlocksize],
603     *fits_info;
604
605   MagickBooleanType
606     status;
607
608   QuantumInfo
609     *quantum_info;
610
611   register const PixelPacket
612     *p;
613
614   size_t
615     length;
616
617   ssize_t
618     count,
619     offset,
620     y;
621
622   unsigned char
623     *pixels;
624
625   /*
626     Open output image file.
627   */
628   assert(image_info != (const ImageInfo *) NULL);
629   assert(image_info->signature == MagickSignature);
630   assert(image != (Image *) NULL);
631   assert(image->signature == MagickSignature);
632   if (image->debug != MagickFalse)
633     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
634   status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
635   if (status == MagickFalse)
636     return(status);
637   if (image->colorspace != RGBColorspace)
638     (void) TransformImageColorspace(image,RGBColorspace);
639   /*
640     Allocate image memory.
641   */
642   fits_info=(char *) AcquireQuantumMemory(FITSBlocksize,sizeof(*fits_info));
643   if (fits_info == (char *) NULL)
644     ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
645   (void) ResetMagickMemory(fits_info,' ',FITSBlocksize*sizeof(*fits_info));
646   /*
647     Initialize image header.
648   */
649   image->depth=GetImageQuantumDepth(image,MagickFalse);
650   quantum_info=AcquireQuantumInfo((const ImageInfo *) NULL,image);
651   if (quantum_info == (QuantumInfo *) NULL)
652     ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
653   offset=0;
654   (void) FormatMagickString(header,FITSBlocksize,
655     "SIMPLE  =                    T");
656   (void) strncpy(fits_info+offset,header,strlen(header));
657   offset+=80;
658   (void) FormatMagickString(header,FITSBlocksize,"BITPIX  =           %10ld",
659     (long) (quantum_info->format == FloatingPointQuantumFormat ? -1 : 1)*
660     image->depth);
661   (void) strncpy(fits_info+offset,header,strlen(header));
662   offset+=80;
663   (void) FormatMagickString(header,FITSBlocksize,"NAXIS   =           %10lu",
664     2UL);
665   (void) strncpy(fits_info+offset,header,strlen(header));
666   offset+=80;
667   (void) FormatMagickString(header,FITSBlocksize,"NAXIS1  =           %10lu",
668     (unsigned long) image->columns);
669   (void) strncpy(fits_info+offset,header,strlen(header));
670   offset+=80;
671   (void) FormatMagickString(header,FITSBlocksize,"NAXIS2  =           %10lu",
672     (unsigned long) image->rows);
673   (void) strncpy(fits_info+offset,header,strlen(header));
674   offset+=80;
675   (void) FormatMagickString(header,FITSBlocksize,"BSCALE  =         %E",1.0);
676   (void) strncpy(fits_info+offset,header,strlen(header));
677   offset+=80;
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));
681   offset+=80;
682   (void) FormatMagickString(header,FITSBlocksize,"DATAMAX =         %E",
683     1.0*((MagickOffsetType) GetQuantumRange(image->depth)));
684   (void) strncpy(fits_info+offset,header,strlen(header));
685   offset+=80;
686   (void) FormatMagickString(header,FITSBlocksize,"DATAMIN =         %E",0.0);
687   (void) strncpy(fits_info+offset,header,strlen(header));
688   offset+=80;
689   if (image->endian == LSBEndian)
690     {
691       (void) FormatMagickString(header,FITSBlocksize,"XENDIAN = 'SMALL'");
692       (void) strncpy(fits_info+offset,header,strlen(header));
693       offset+=80;
694     }
695   (void) FormatMagickString(header,FITSBlocksize,"HISTORY %.72s",
696     GetMagickVersion((size_t *) NULL));
697   (void) strncpy(fits_info+offset,header,strlen(header));
698   offset+=80;
699   (void) strncpy(header,"END",FITSBlocksize);
700   (void) strncpy(fits_info+offset,header,strlen(header));
701   offset+=80;
702   (void) WriteBlob(image,FITSBlocksize,(unsigned char *) fits_info);
703   /*
704     Convert image to fits scale PseudoColor class.
705   */
706   pixels=GetQuantumPixels(quantum_info);
707   length=GetQuantumExtent(image,quantum_info,GrayQuantum);
708   for (y=(ssize_t) image->rows-1; y >= 0; y--)
709   {
710     p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
711     if (p == (const PixelPacket *) NULL)
712       break;
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)
722       break;
723     status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
724       image->rows);
725     if (status == MagickFalse)
726       break;
727   }
728   quantum_info=DestroyQuantumInfo(quantum_info);
729   length=(size_t) (FITSBlocksize-TellBlob(image) % FITSBlocksize);
730   if (length != 0)
731     {
732       (void) ResetMagickMemory(fits_info,0,length*sizeof(*fits_info));
733       (void) WriteBlob(image,length,(unsigned char *) fits_info);
734     }
735   fits_info=DestroyString(fits_info);
736   (void) CloseBlob(image);
737   return(MagickTrue);
738 }