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