blob: 8f7829a5a9c9e88d3b4dc27338884803e13d3582 [file] [log] [blame]
cristy3ed852e2009-09-05 21:47:34 +00001/*
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% %
cristy16af1cb2009-12-11 21:38:29 +000020% Copyright 1999-2010 ImageMagick Studio LLC, a non-profit organization %
cristy3ed852e2009-09-05 21:47:34 +000021% 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
39/*
40 Include declarations.
41*/
42#include "magick/studio.h"
cristy5a2ca482009-10-14 18:24:56 +000043#include "magick/attribute.h"
cristy3ed852e2009-09-05 21:47:34 +000044#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"
cristyf2f27272009-12-17 14:48:46 +000057#include "magick/module.h"
cristy3ed852e2009-09-05 21:47:34 +000058#include "magick/monitor.h"
59#include "magick/monitor-private.h"
60#include "magick/pixel-private.h"
61#include "magick/pixel-private.h"
cristy5a2ca482009-10-14 18:24:56 +000062#include "magick/property.h"
cristy3ed852e2009-09-05 21:47:34 +000063#include "magick/static.h"
64#include "magick/statistic.h"
65#include "magick/string_.h"
cristyf2f27272009-12-17 14:48:46 +000066#include "magick/string-private.h"
cristy3ed852e2009-09-05 21:47:34 +000067#include "magick/module.h"
68
69/*
70 Forward declarations.
71*/
72#define FITSBlocksize 2880UL
73
74/*
75 Forward declarations.
76*/
77static MagickBooleanType
78 WriteFITSImage(const ImageInfo *,Image *);
79
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*/
105static 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
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
144static 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) ((long) 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
169static 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
199static inline double GetFITSPixelRange(const unsigned long depth)
200{
201 return((double) ((MagickOffsetType) GetQuantumRange(depth)));
202}
203
204static void SetFITSUnsignedPixels(const size_t length,
205 const unsigned long bits_per_pixel,unsigned char *pixels)
206{
207 register long
208 i;
209
210 for (i=0; i < (long) length; i++)
211 {
212 *pixels^=0x80;
213 pixels+=bits_per_pixel >> 3;
214 }
215}
216
217static 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 long
263 scene,
264 y;
265
266 MagickBooleanType
267 status;
268
269 MagickSizeType
270 number_pixels;
271
272 register long
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;
cristy466802f2010-02-24 14:18:37 +0000332 keyword[i]=tolower((int) ((unsigned char) keyword[i]));
cristy3ed852e2009-09-05 21:47:34 +0000333 }
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)
cristyf2f27272009-12-17 14:48:46 +0000353 fits_info.bits_per_pixel=StringToLong(p);
cristy3ed852e2009-09-05 21:47:34 +0000354 if (LocaleCompare(keyword,"naxis") == 0)
cristyf2f27272009-12-17 14:48:46 +0000355 fits_info.number_axes=StringToLong(p);
cristy3ed852e2009-09-05 21:47:34 +0000356 if (LocaleCompare(keyword,"naxis1") == 0)
cristyf2f27272009-12-17 14:48:46 +0000357 fits_info.columns=StringToLong(p);
cristy3ed852e2009-09-05 21:47:34 +0000358 if (LocaleCompare(keyword,"naxis2") == 0)
cristyf2f27272009-12-17 14:48:46 +0000359 fits_info.rows=StringToLong(p);
cristy3ed852e2009-09-05 21:47:34 +0000360 if (LocaleCompare(keyword,"naxis3") == 0)
cristyf2f27272009-12-17 14:48:46 +0000361 fits_info.number_planes=StringToLong(p);
cristy3ed852e2009-09-05 21:47:34 +0000362 if (LocaleCompare(keyword,"datamax") == 0)
cristyf2f27272009-12-17 14:48:46 +0000363 fits_info.max_data=StringToDouble(p);
cristy3ed852e2009-09-05 21:47:34 +0000364 if (LocaleCompare(keyword,"datamin") == 0)
cristyf2f27272009-12-17 14:48:46 +0000365 fits_info.min_data=StringToDouble(p);
cristy3ed852e2009-09-05 21:47:34 +0000366 if (LocaleCompare(keyword,"bzero") == 0)
cristyf2f27272009-12-17 14:48:46 +0000367 fits_info.zero=StringToDouble(p);
cristy3ed852e2009-09-05 21:47:34 +0000368 if (LocaleCompare(keyword,"bscale") == 0)
cristyf2f27272009-12-17 14:48:46 +0000369 fits_info.scale=StringToDouble(p);
cristy3ed852e2009-09-05 21:47:34 +0000370 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 < (long) fits_info.number_planes; scene++)
413 {
414 image->columns=(unsigned long) fits_info.columns;
415 image->rows=(unsigned long) fits_info.rows;
416 image->depth=(unsigned long) (fits_info.bits_per_pixel < 0 ? -1 : 1)*
417 fits_info.bits_per_pixel;
418 image->endian=fits_info.endian;
419 image->scene=(unsigned long) scene;
420 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
421 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
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((unsigned long)
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=(long) 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 < (long) image->columns; x++)
446 {
447 pixel=GetFITSPixel(image,fits_info.bits_per_pixel);
cristyce70c172010-01-07 17:15:30 +0000448 q->red=(Quantum) ClampToQuantum(scale*(fits_info.scale*(pixel-
cristy3ed852e2009-09-05 21:47:34 +0000449 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,y,image->rows);
459 if (status == MagickFalse)
460 break;
461 }
462 }
463 if (EOFBlob(image) != MagickFalse)
464 {
465 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
466 image->filename);
467 break;
468 }
469 /*
470 Proceed to next image.
471 */
472 if (image_info->number_scenes != 0)
473 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
474 break;
475 if (scene < (long) (fits_info.number_planes-1))
476 {
477 /*
478 Allocate next image structure.
479 */
480 AcquireNextImage(image_info,image);
481 if (GetNextImageInList(image) == (Image *) NULL)
482 {
483 image=DestroyImageList(image);
484 return((Image *) NULL);
485 }
486 image=SyncNextImageInList(image);
487 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
488 GetBlobSize(image));
489 if (status == MagickFalse)
490 break;
491 }
492 }
493 (void) CloseBlob(image);
494 return(GetFirstImageInList(image));
495}
496
497/*
498%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
499% %
500% %
501% %
502% R e g i s t e r F I T S I m a g e %
503% %
504% %
505% %
506%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
507%
508% RegisterFITSImage() adds attributes for the FITS image format to
509% the list of supported formats. The attributes include the image format
510% tag, a method to read and/or write the format, whether the format
511% supports the saving of more than one frame to the same file or blob,
512% whether the format supports native in-memory I/O, and a brief
513% description of the format.
514%
515% The format of the RegisterFITSImage method is:
516%
517% unsigned long RegisterFITSImage(void)
518%
519*/
520ModuleExport unsigned long RegisterFITSImage(void)
521{
522 MagickInfo
523 *entry;
524
525 entry=SetMagickInfo("FITS");
526 entry->decoder=(DecodeImageHandler *) ReadFITSImage;
527 entry->encoder=(EncodeImageHandler *) WriteFITSImage;
528 entry->magick=(IsImageFormatHandler *) IsFITS;
529 entry->adjoin=MagickFalse;
530 entry->seekable_stream=MagickTrue;
531 entry->description=ConstantString("Flexible Image Transport System");
532 entry->module=ConstantString("FITS");
533 (void) RegisterMagickInfo(entry);
534 entry=SetMagickInfo("FTS");
535 entry->decoder=(DecodeImageHandler *) ReadFITSImage;
536 entry->encoder=(EncodeImageHandler *) WriteFITSImage;
537 entry->magick=(IsImageFormatHandler *) IsFITS;
538 entry->adjoin=MagickFalse;
539 entry->seekable_stream=MagickTrue;
540 entry->description=ConstantString("Flexible Image Transport System");
541 entry->module=ConstantString("FTS");
542 (void) RegisterMagickInfo(entry);
543 return(MagickImageCoderSignature);
544}
545
546/*
547%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
548% %
549% %
550% %
551% U n r e g i s t e r F I T S I m a g e %
552% %
553% %
554% %
555%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
556%
557% UnregisterFITSImage() removes format registrations made by the
558% FITS module from the list of supported formats.
559%
560% The format of the UnregisterFITSImage method is:
561%
562% UnregisterFITSImage(void)
563%
564*/
565ModuleExport void UnregisterFITSImage(void)
566{
567 (void) UnregisterMagickInfo("FITS");
568 (void) UnregisterMagickInfo("FTS");
569}
570
571/*
572%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
573% %
574% %
575% %
576% W r i t e F I T S I m a g e %
577% %
578% %
579% %
580%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
581%
582% WriteFITSImage() writes a Flexible Image Transport System image to a
583% file as gray scale intensities [0..255].
584%
585% The format of the WriteFITSImage method is:
586%
587% MagickBooleanType WriteFITSImage(const ImageInfo *image_info,
588% Image *image)
589%
590% A description of each parameter follows.
591%
592% o image_info: the image info.
593%
594% o image: The image.
595%
596*/
597static MagickBooleanType WriteFITSImage(const ImageInfo *image_info,
598 Image *image)
599{
600 char
601 header[FITSBlocksize],
602 *fits_info;
603
604 long
605 y;
606
607 MagickBooleanType
608 status;
609
610 QuantumInfo
611 *quantum_info;
612
613 register const PixelPacket
614 *p;
615
616 size_t
617 length;
618
619 ssize_t
620 count,
621 offset;
622
623 unsigned char
624 *pixels;
625
626 /*
627 Open output image file.
628 */
629 assert(image_info != (const ImageInfo *) NULL);
630 assert(image_info->signature == MagickSignature);
631 assert(image != (Image *) NULL);
632 assert(image->signature == MagickSignature);
633 if (image->debug != MagickFalse)
634 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
635 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
636 if (status == MagickFalse)
637 return(status);
638 if (image->colorspace != RGBColorspace)
639 (void) TransformImageColorspace(image,RGBColorspace);
640 /*
641 Allocate image memory.
642 */
643 fits_info=(char *) AcquireQuantumMemory(FITSBlocksize,sizeof(*fits_info));
644 if (fits_info == (char *) NULL)
645 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
646 (void) ResetMagickMemory(fits_info,' ',FITSBlocksize*sizeof(*fits_info));
647 /*
648 Initialize image header.
649 */
650 image->depth=GetImageQuantumDepth(image,MagickFalse);
651 quantum_info=AcquireQuantumInfo((const ImageInfo *) NULL,image);
652 if (quantum_info == (QuantumInfo *) NULL)
653 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
654 offset=0;
655 (void) FormatMagickString(header,FITSBlocksize,
656 "SIMPLE = T");
657 (void) strncpy(fits_info+offset,header,strlen(header));
658 offset+=80;
659 (void) FormatMagickString(header,FITSBlocksize,"BITPIX = %10ld",
660 (quantum_info->format == FloatingPointQuantumFormat ? -1 : 1)*image->depth);
661 (void) strncpy(fits_info+offset,header,strlen(header));
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 image->columns);
669 (void) strncpy(fits_info+offset,header,strlen(header));
670 offset+=80;
671 (void) FormatMagickString(header,FITSBlocksize,"NAXIS2 = %10lu",
672 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((unsigned long *) 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=(long) 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,y,image->rows);
724 if (status == MagickFalse)
725 break;
726 }
727 quantum_info=DestroyQuantumInfo(quantum_info);
728 length=(size_t) (FITSBlocksize-TellBlob(image) % FITSBlocksize);
729 if (length != 0)
730 {
731 (void) ResetMagickMemory(fits_info,0,length*sizeof(*fits_info));
732 (void) WriteBlob(image,length,(unsigned char *) fits_info);
733 }
734 fits_info=DestroyString(fits_info);
735 (void) CloseBlob(image);
736 return(MagickTrue);
737}