blob: 197be8b052a84ee48801bfbb76aad40c80697c56 [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 %
cristyde984cd2013-12-01 14:49:27 +000016% Cristy %
cristy3ed852e2009-09-05 21:47:34 +000017% July 1992 %
18% %
19% %
Cristy7ce65e72015-12-12 18:03:16 -050020% Copyright 1999-2016 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*/
cristy4c08aed2011-07-01 19:47:50 +000042#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"
cristy510d06a2011-07-06 23:43:54 +000049#include "MagickCore/colorspace-private.h"
cristy4c08aed2011-07-01 19:47:50 +000050#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"
cristy3ed852e2009-09-05 21:47:34 +000069
70/*
71 Forward declarations.
72*/
73#define FITSBlocksize 2880UL
74
75/*
76 Forward declarations.
77*/
78static MagickBooleanType
cristy1e178e72011-08-28 19:44:34 +000079 WriteFITSImage(const ImageInfo *,Image *,ExceptionInfo *);
cristy3ed852e2009-09-05 21:47:34 +000080
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*/
106static 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
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
145static 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)
cristy6cff05d2010-09-02 11:22:46 +0000156 return((double) ((int) ReadBlobLong(image)));
cristy3ed852e2009-09-05 21:47:34 +0000157 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
cristy4d0ca342014-05-01 00:42:09 +0000170static MagickOffsetType GetFITSPixelExtrema(Image *image,
171 const int bits_per_pixel,double *minima,double *maxima)
cristy3ed852e2009-09-05 21:47:34 +0000172{
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);
cristy4d0ca342014-05-01 00:42:09 +0000186 if (offset == -1)
187 return(-1);
cristy3ed852e2009-09-05 21:47:34 +0000188 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 }
cristy4d0ca342014-05-01 00:42:09 +0000199 return(SeekBlob(image,offset,SEEK_SET));
cristy3ed852e2009-09-05 21:47:34 +0000200}
201
cristybb503372010-05-27 20:51:26 +0000202static inline double GetFITSPixelRange(const size_t depth)
cristy3ed852e2009-09-05 21:47:34 +0000203{
204 return((double) ((MagickOffsetType) GetQuantumRange(depth)));
205}
206
207static void SetFITSUnsignedPixels(const size_t length,
cristyf84dc8e2013-02-02 16:46:26 +0000208 const size_t bits_per_pixel,const EndianType endian,unsigned char *pixels)
cristy3ed852e2009-09-05 21:47:34 +0000209{
cristybb503372010-05-27 20:51:26 +0000210 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000211 i;
212
cristyf84dc8e2013-02-02 16:46:26 +0000213 if (endian != MSBEndian)
214 pixels+=(bits_per_pixel >> 3)-1;
cristybb503372010-05-27 20:51:26 +0000215 for (i=0; i < (ssize_t) length; i++)
cristy3ed852e2009-09-05 21:47:34 +0000216 {
217 *pixels^=0x80;
218 pixels+=bits_per_pixel >> 3;
219 }
220}
221
222static 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],
cristy151b66d2015-04-15 10:50:31 +0000251 property[MagickPathExtent],
cristy3ed852e2009-09-05 21:47:34 +0000252 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
cristy3ed852e2009-09-05 21:47:34 +0000267 MagickBooleanType
268 status;
269
270 MagickSizeType
271 number_pixels;
272
cristybb503372010-05-27 20:51:26 +0000273 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000274 i,
275 x;
276
cristy4c08aed2011-07-01 19:47:50 +0000277 register Quantum
cristy3ed852e2009-09-05 21:47:34 +0000278 *q;
279
280 ssize_t
cristy202de442011-04-24 18:19:07 +0000281 count,
282 scene,
283 y;
cristy3ed852e2009-09-05 21:47:34 +0000284
285 /*
286 Open image file.
287 */
288 assert(image_info != (const ImageInfo *) NULL);
cristye1c94d92015-06-28 12:16:33 +0000289 assert(image_info->signature == MagickCoreSignature);
cristy3ed852e2009-09-05 21:47:34 +0000290 if (image_info->debug != MagickFalse)
291 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
292 image_info->filename);
293 assert(exception != (ExceptionInfo *) NULL);
cristye1c94d92015-06-28 12:16:33 +0000294 assert(exception->signature == MagickCoreSignature);
cristy9950d572011-10-01 18:22:35 +0000295 image=AcquireImage(image_info,exception);
cristy3ed852e2009-09-05 21:47:34 +0000296 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) ResetMagickMemory(&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;
cristy3ed852e2009-09-05 21:47:34 +0000311 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;
cristy466802f2010-02-24 14:18:37 +0000334 keyword[i]=tolower((int) ((unsigned char) keyword[i]));
cristy3ed852e2009-09-05 21:47:34 +0000335 }
336 keyword[i]='\0';
337 count=ReadBlob(image,72,(unsigned char *) value);
cristye3216eb2014-04-18 17:10:21 +0000338 value[72]='\0';
cristy3ed852e2009-09-05 21:47:34 +0000339 if (count != 72)
340 break;
cristy3ed852e2009-09-05 21:47:34 +0000341 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)
cristyf2f27272009-12-17 14:48:46 +0000355 fits_info.bits_per_pixel=StringToLong(p);
cristy3ed852e2009-09-05 21:47:34 +0000356 if (LocaleCompare(keyword,"naxis") == 0)
cristyf2f27272009-12-17 14:48:46 +0000357 fits_info.number_axes=StringToLong(p);
cristy3ed852e2009-09-05 21:47:34 +0000358 if (LocaleCompare(keyword,"naxis1") == 0)
cristyf2f27272009-12-17 14:48:46 +0000359 fits_info.columns=StringToLong(p);
cristy3ed852e2009-09-05 21:47:34 +0000360 if (LocaleCompare(keyword,"naxis2") == 0)
cristyf2f27272009-12-17 14:48:46 +0000361 fits_info.rows=StringToLong(p);
cristy3ed852e2009-09-05 21:47:34 +0000362 if (LocaleCompare(keyword,"naxis3") == 0)
cristyf2f27272009-12-17 14:48:46 +0000363 fits_info.number_planes=StringToLong(p);
cristy3ed852e2009-09-05 21:47:34 +0000364 if (LocaleCompare(keyword,"datamax") == 0)
cristydbdd0e32011-11-04 23:29:40 +0000365 fits_info.max_data=StringToDouble(p,(char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +0000366 if (LocaleCompare(keyword,"datamin") == 0)
cristydbdd0e32011-11-04 23:29:40 +0000367 fits_info.min_data=StringToDouble(p,(char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +0000368 if (LocaleCompare(keyword,"bzero") == 0)
cristydbdd0e32011-11-04 23:29:40 +0000369 fits_info.zero=StringToDouble(p,(char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +0000370 if (LocaleCompare(keyword,"bscale") == 0)
cristydbdd0e32011-11-04 23:29:40 +0000371 fits_info.scale=StringToDouble(p,(char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +0000372 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 }
cristy151b66d2015-04-15 10:50:31 +0000386 (void) FormatLocaleString(property,MagickPathExtent,"fits:%s",keyword);
cristyd15e6592011-10-15 00:13:06 +0000387 (void) SetImageProperty(image,property,p,exception);
cristy3ed852e2009-09-05 21:47:34 +0000388 }
389 c=0;
390 while (((TellBlob(image) % FITSBlocksize) != 0) && (c != EOF))
391 c=ReadBlobByte(image);
392 if (fits_info.extend == MagickFalse)
393 break;
cristy11dc2b82015-01-25 00:11:48 +0000394 if ((fits_info.bits_per_pixel != 8) && (fits_info.bits_per_pixel != 16) &&
395 (fits_info.bits_per_pixel != 32) && (fits_info.bits_per_pixel != 64) &&
396 (fits_info.bits_per_pixel != -32) && (fits_info.bits_per_pixel != -64))
397 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
cristy3ed852e2009-09-05 21:47:34 +0000398 number_pixels=(MagickSizeType) fits_info.columns*fits_info.rows;
399 if ((fits_info.simple != MagickFalse) && (fits_info.number_axes >= 1) &&
400 (fits_info.number_axes <= 4) && (number_pixels != 0))
401 break;
402 }
403 /*
404 Verify that required image information is defined.
405 */
406 if (comment != (char *) NULL)
407 {
cristyd15e6592011-10-15 00:13:06 +0000408 (void) SetImageProperty(image,"comment",comment,exception);
cristy3ed852e2009-09-05 21:47:34 +0000409 comment=DestroyString(comment);
410 }
411 if (EOFBlob(image) != MagickFalse)
412 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
413 image->filename);
414 number_pixels=(MagickSizeType) fits_info.columns*fits_info.rows;
415 if ((fits_info.simple == MagickFalse) || (fits_info.number_axes < 1) ||
416 (fits_info.number_axes > 4) || (number_pixels == 0))
417 ThrowReaderException(CorruptImageError,"ImageTypeNotSupported");
cristybb503372010-05-27 20:51:26 +0000418 for (scene=0; scene < (ssize_t) fits_info.number_planes; scene++)
cristy3ed852e2009-09-05 21:47:34 +0000419 {
cristybb503372010-05-27 20:51:26 +0000420 image->columns=(size_t) fits_info.columns;
421 image->rows=(size_t) fits_info.rows;
422 image->depth=(size_t) (fits_info.bits_per_pixel < 0 ? -1 : 1)*
cristy3ed852e2009-09-05 21:47:34 +0000423 fits_info.bits_per_pixel;
424 image->endian=fits_info.endian;
cristybb503372010-05-27 20:51:26 +0000425 image->scene=(size_t) scene;
cristy3ed852e2009-09-05 21:47:34 +0000426 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
427 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
428 break;
cristyacabb842014-12-14 23:36:33 +0000429 status=SetImageExtent(image,image->columns,image->rows,exception);
430 if (status == MagickFalse)
431 return(DestroyImageList(image));
cristy3ed852e2009-09-05 21:47:34 +0000432 /*
433 Initialize image structure.
434 */
cristyd8b39812013-02-02 02:12:57 +0000435 (void) SetImageColorspace(image,GRAYColorspace,exception);
436 if ((fits_info.min_data == 0.0) && (fits_info.max_data == 0.0))
cristy3ed852e2009-09-05 21:47:34 +0000437 {
cristy3f6782c2013-02-06 01:33:50 +0000438 if (fits_info.zero == 0.0)
cristy4d0ca342014-05-01 00:42:09 +0000439 (void) GetFITSPixelExtrema(image,fits_info.bits_per_pixel,
cristyd8b39812013-02-02 02:12:57 +0000440 &fits_info.min_data,&fits_info.max_data);
441 else
cristybb503372010-05-27 20:51:26 +0000442 fits_info.max_data=GetFITSPixelRange((size_t)
cristy3ed852e2009-09-05 21:47:34 +0000443 fits_info.bits_per_pixel);
444 }
445 else
cristyd8b39812013-02-02 02:12:57 +0000446 fits_info.max_data=GetFITSPixelRange((size_t) fits_info.bits_per_pixel);
cristy3ed852e2009-09-05 21:47:34 +0000447 /*
448 Convert FITS pixels to pixel packets.
449 */
cristyd8b39812013-02-02 02:12:57 +0000450 scale=QuantumRange/(fits_info.max_data-fits_info.min_data);
cristybb503372010-05-27 20:51:26 +0000451 for (y=(ssize_t) image->rows-1; y >= 0; y--)
cristy3ed852e2009-09-05 21:47:34 +0000452 {
453 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +0000454 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000455 break;
cristybb503372010-05-27 20:51:26 +0000456 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000457 {
458 pixel=GetFITSPixel(image,fits_info.bits_per_pixel);
cristy163d6782013-01-26 13:54:54 +0000459 if ((image->depth == 16) || (image->depth == 32) ||
cristy9dcadc52013-01-26 13:53:22 +0000460 (image->depth == 64))
cristyf84dc8e2013-02-02 16:46:26 +0000461 SetFITSUnsignedPixels(1,image->depth,image->endian,
462 (unsigned char *) &pixel);
cristy12142882012-06-17 00:52:29 +0000463 SetPixelGray(image,ClampToQuantum(scale*(fits_info.scale*(pixel-
464 fits_info.min_data)+fits_info.zero)),q);
cristyed231572011-07-14 02:18:59 +0000465 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000466 }
467 if (SyncAuthenticPixels(image,exception) == MagickFalse)
468 break;
469 if (image->previous == (Image *) NULL)
470 {
cristycee97112010-05-28 00:44:52 +0000471 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
cristy4c08aed2011-07-01 19:47:50 +0000472 image->rows);
cristy3ed852e2009-09-05 21:47:34 +0000473 if (status == MagickFalse)
474 break;
475 }
476 }
477 if (EOFBlob(image) != MagickFalse)
478 {
479 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
480 image->filename);
481 break;
482 }
483 /*
484 Proceed to next image.
485 */
486 if (image_info->number_scenes != 0)
487 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
488 break;
cristybb503372010-05-27 20:51:26 +0000489 if (scene < (ssize_t) (fits_info.number_planes-1))
cristy3ed852e2009-09-05 21:47:34 +0000490 {
491 /*
492 Allocate next image structure.
493 */
cristy9950d572011-10-01 18:22:35 +0000494 AcquireNextImage(image_info,image,exception);
cristy3ed852e2009-09-05 21:47:34 +0000495 if (GetNextImageInList(image) == (Image *) NULL)
496 {
497 image=DestroyImageList(image);
498 return((Image *) NULL);
499 }
500 image=SyncNextImageInList(image);
501 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
502 GetBlobSize(image));
503 if (status == MagickFalse)
504 break;
505 }
506 }
507 (void) CloseBlob(image);
508 return(GetFirstImageInList(image));
509}
510
511/*
512%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
513% %
514% %
515% %
516% R e g i s t e r F I T S I m a g e %
517% %
518% %
519% %
520%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
521%
522% RegisterFITSImage() adds attributes for the FITS image format to
523% the list of supported formats. The attributes include the image format
524% tag, a method to read and/or write the format, whether the format
525% supports the saving of more than one frame to the same file or blob,
526% whether the format supports native in-memory I/O, and a brief
527% description of the format.
528%
529% The format of the RegisterFITSImage method is:
530%
cristybb503372010-05-27 20:51:26 +0000531% size_t RegisterFITSImage(void)
cristy3ed852e2009-09-05 21:47:34 +0000532%
533*/
cristybb503372010-05-27 20:51:26 +0000534ModuleExport size_t RegisterFITSImage(void)
cristy3ed852e2009-09-05 21:47:34 +0000535{
536 MagickInfo
537 *entry;
538
dirk06b627a2015-04-06 18:59:17 +0000539 entry=AcquireMagickInfo("FITS","FITS","Flexible Image Transport System");
cristy3ed852e2009-09-05 21:47:34 +0000540 entry->decoder=(DecodeImageHandler *) ReadFITSImage;
541 entry->encoder=(EncodeImageHandler *) WriteFITSImage;
542 entry->magick=(IsImageFormatHandler *) IsFITS;
dirk08e9a112015-02-22 01:51:41 +0000543 entry->flags^=CoderAdjoinFlag;
544 entry->flags|=CoderSeekableStreamFlag;
cristy3ed852e2009-09-05 21:47:34 +0000545 (void) RegisterMagickInfo(entry);
dirkcbab2d12015-04-06 19:14:44 +0000546 entry=AcquireMagickInfo("FITS","FTS","Flexible Image Transport System");
cristy3ed852e2009-09-05 21:47:34 +0000547 entry->decoder=(DecodeImageHandler *) ReadFITSImage;
548 entry->encoder=(EncodeImageHandler *) WriteFITSImage;
549 entry->magick=(IsImageFormatHandler *) IsFITS;
dirk08e9a112015-02-22 01:51:41 +0000550 entry->flags^=CoderAdjoinFlag;
551 entry->flags|=CoderSeekableStreamFlag;
cristy3ed852e2009-09-05 21:47:34 +0000552 (void) RegisterMagickInfo(entry);
553 return(MagickImageCoderSignature);
554}
555
556/*
557%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
558% %
559% %
560% %
561% U n r e g i s t e r F I T S I m a g e %
562% %
563% %
564% %
565%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
566%
567% UnregisterFITSImage() removes format registrations made by the
568% FITS module from the list of supported formats.
569%
570% The format of the UnregisterFITSImage method is:
571%
572% UnregisterFITSImage(void)
573%
574*/
575ModuleExport void UnregisterFITSImage(void)
576{
577 (void) UnregisterMagickInfo("FITS");
578 (void) UnregisterMagickInfo("FTS");
579}
580
581/*
582%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
583% %
584% %
585% %
586% W r i t e F I T S I m a g e %
587% %
588% %
589% %
590%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
591%
592% WriteFITSImage() writes a Flexible Image Transport System image to a
593% file as gray scale intensities [0..255].
594%
595% The format of the WriteFITSImage method is:
596%
597% MagickBooleanType WriteFITSImage(const ImageInfo *image_info,
cristy1e178e72011-08-28 19:44:34 +0000598% Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000599%
600% A description of each parameter follows.
601%
602% o image_info: the image info.
603%
604% o image: The image.
605%
cristy1e178e72011-08-28 19:44:34 +0000606% o exception: return any errors or warnings in this structure.
607%
cristy3ed852e2009-09-05 21:47:34 +0000608*/
609static MagickBooleanType WriteFITSImage(const ImageInfo *image_info,
cristy1e178e72011-08-28 19:44:34 +0000610 Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000611{
612 char
613 header[FITSBlocksize],
614 *fits_info;
615
cristy3ed852e2009-09-05 21:47:34 +0000616 MagickBooleanType
617 status;
618
619 QuantumInfo
620 *quantum_info;
621
cristy4c08aed2011-07-01 19:47:50 +0000622 register const Quantum
cristy3ed852e2009-09-05 21:47:34 +0000623 *p;
624
625 size_t
626 length;
627
628 ssize_t
629 count,
cristy2692d592010-11-01 18:07:27 +0000630 offset,
631 y;
cristy3ed852e2009-09-05 21:47:34 +0000632
633 unsigned char
634 *pixels;
635
636 /*
637 Open output image file.
638 */
639 assert(image_info != (const ImageInfo *) NULL);
cristye1c94d92015-06-28 12:16:33 +0000640 assert(image_info->signature == MagickCoreSignature);
cristy3ed852e2009-09-05 21:47:34 +0000641 assert(image != (Image *) NULL);
cristye1c94d92015-06-28 12:16:33 +0000642 assert(image->signature == MagickCoreSignature);
cristy3ed852e2009-09-05 21:47:34 +0000643 if (image->debug != MagickFalse)
644 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy3a37efd2011-08-28 20:31:03 +0000645 assert(exception != (ExceptionInfo *) NULL);
cristye1c94d92015-06-28 12:16:33 +0000646 assert(exception->signature == MagickCoreSignature);
cristy1e178e72011-08-28 19:44:34 +0000647 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
cristy3ed852e2009-09-05 21:47:34 +0000648 if (status == MagickFalse)
649 return(status);
cristyaf8d3912014-02-21 14:50:33 +0000650 (void) TransformImageColorspace(image,sRGBColorspace,exception);
cristy3ed852e2009-09-05 21:47:34 +0000651 /*
652 Allocate image memory.
653 */
654 fits_info=(char *) AcquireQuantumMemory(FITSBlocksize,sizeof(*fits_info));
655 if (fits_info == (char *) NULL)
656 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
657 (void) ResetMagickMemory(fits_info,' ',FITSBlocksize*sizeof(*fits_info));
658 /*
659 Initialize image header.
660 */
661 image->depth=GetImageQuantumDepth(image,MagickFalse);
cristy6ea44a92013-02-03 23:40:10 +0000662 image->endian=MSBEndian;
cristy5f766ef2014-12-14 21:12:47 +0000663 quantum_info=AcquireQuantumInfo(image_info,image);
cristy3ed852e2009-09-05 21:47:34 +0000664 if (quantum_info == (QuantumInfo *) NULL)
665 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
666 offset=0;
cristyb51dff52011-05-19 16:55:47 +0000667 (void) FormatLocaleString(header,FITSBlocksize,
cristy3ed852e2009-09-05 21:47:34 +0000668 "SIMPLE = T");
669 (void) strncpy(fits_info+offset,header,strlen(header));
670 offset+=80;
cristyb51dff52011-05-19 16:55:47 +0000671 (void) FormatLocaleString(header,FITSBlocksize,"BITPIX = %10ld",
cristy67e3eab2014-06-17 00:25:27 +0000672 (long) ((quantum_info->format == FloatingPointQuantumFormat ? -1 : 1)*
673 image->depth));
cristy3ed852e2009-09-05 21:47:34 +0000674 (void) strncpy(fits_info+offset,header,strlen(header));
675 offset+=80;
cristyb51dff52011-05-19 16:55:47 +0000676 (void) FormatLocaleString(header,FITSBlocksize,"NAXIS = %10lu",
dirkf1d85482015-04-06 00:36:00 +0000677 SetImageGray(image,exception) != MagickFalse ? 2UL : 3UL);
cristy3ed852e2009-09-05 21:47:34 +0000678 (void) strncpy(fits_info+offset,header,strlen(header));
679 offset+=80;
cristyb51dff52011-05-19 16:55:47 +0000680 (void) FormatLocaleString(header,FITSBlocksize,"NAXIS1 = %10lu",
cristyf2faecf2010-05-28 19:19:36 +0000681 (unsigned long) image->columns);
cristy3ed852e2009-09-05 21:47:34 +0000682 (void) strncpy(fits_info+offset,header,strlen(header));
683 offset+=80;
cristyb51dff52011-05-19 16:55:47 +0000684 (void) FormatLocaleString(header,FITSBlocksize,"NAXIS2 = %10lu",
cristyf2faecf2010-05-28 19:19:36 +0000685 (unsigned long) image->rows);
cristy3ed852e2009-09-05 21:47:34 +0000686 (void) strncpy(fits_info+offset,header,strlen(header));
687 offset+=80;
dirkf1d85482015-04-06 00:36:00 +0000688 if (SetImageGray(image,exception) == MagickFalse)
cristyfcbcae82011-04-22 12:53:27 +0000689 {
cristyb51dff52011-05-19 16:55:47 +0000690 (void) FormatLocaleString(header,FITSBlocksize,
cristyfcbcae82011-04-22 12:53:27 +0000691 "NAXIS3 = %10lu",3UL);
692 (void) strncpy(fits_info+offset,header,strlen(header));
693 offset+=80;
694 }
cristyb51dff52011-05-19 16:55:47 +0000695 (void) FormatLocaleString(header,FITSBlocksize,"BSCALE = %E",1.0);
cristy3ed852e2009-09-05 21:47:34 +0000696 (void) strncpy(fits_info+offset,header,strlen(header));
697 offset+=80;
cristyb51dff52011-05-19 16:55:47 +0000698 (void) FormatLocaleString(header,FITSBlocksize,"BZERO = %E",
cristy3ec9e8d2013-01-30 16:29:51 +0000699 image->depth > 8 ? GetFITSPixelRange(image->depth)/2.0 : 0.0);
cristy3ed852e2009-09-05 21:47:34 +0000700 (void) strncpy(fits_info+offset,header,strlen(header));
701 offset+=80;
cristyb51dff52011-05-19 16:55:47 +0000702 (void) FormatLocaleString(header,FITSBlocksize,"DATAMAX = %E",
cristy3ed852e2009-09-05 21:47:34 +0000703 1.0*((MagickOffsetType) GetQuantumRange(image->depth)));
704 (void) strncpy(fits_info+offset,header,strlen(header));
705 offset+=80;
cristyb51dff52011-05-19 16:55:47 +0000706 (void) FormatLocaleString(header,FITSBlocksize,"DATAMIN = %E",0.0);
cristy3ed852e2009-09-05 21:47:34 +0000707 (void) strncpy(fits_info+offset,header,strlen(header));
708 offset+=80;
709 if (image->endian == LSBEndian)
710 {
cristyb51dff52011-05-19 16:55:47 +0000711 (void) FormatLocaleString(header,FITSBlocksize,"XENDIAN = 'SMALL'");
cristy3ed852e2009-09-05 21:47:34 +0000712 (void) strncpy(fits_info+offset,header,strlen(header));
713 offset+=80;
714 }
cristyb51dff52011-05-19 16:55:47 +0000715 (void) FormatLocaleString(header,FITSBlocksize,"HISTORY %.72s",
cristybb503372010-05-27 20:51:26 +0000716 GetMagickVersion((size_t *) NULL));
cristy3ed852e2009-09-05 21:47:34 +0000717 (void) strncpy(fits_info+offset,header,strlen(header));
718 offset+=80;
719 (void) strncpy(header,"END",FITSBlocksize);
720 (void) strncpy(fits_info+offset,header,strlen(header));
721 offset+=80;
722 (void) WriteBlob(image,FITSBlocksize,(unsigned char *) fits_info);
723 /*
724 Convert image to fits scale PseudoColor class.
725 */
cristyb3f97ae2015-05-18 12:29:32 +0000726 pixels=(unsigned char *) GetQuantumPixels(quantum_info);
dirkf1d85482015-04-06 00:36:00 +0000727 if (SetImageGray(image,exception) != MagickFalse)
cristyfcbcae82011-04-22 12:53:27 +0000728 {
729 length=GetQuantumExtent(image,quantum_info,GrayQuantum);
730 for (y=(ssize_t) image->rows-1; y >= 0; y--)
731 {
cristy1e178e72011-08-28 19:44:34 +0000732 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +0000733 if (p == (const Quantum *) NULL)
cristyfcbcae82011-04-22 12:53:27 +0000734 break;
cristy4c08aed2011-07-01 19:47:50 +0000735 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
cristy1e178e72011-08-28 19:44:34 +0000736 GrayQuantum,pixels,exception);
cristyfcbcae82011-04-22 12:53:27 +0000737 if (image->depth == 16)
cristyf84dc8e2013-02-02 16:46:26 +0000738 SetFITSUnsignedPixels(image->columns,image->depth,image->endian,
739 pixels);
cristyfcbcae82011-04-22 12:53:27 +0000740 if (((image->depth == 32) || (image->depth == 64)) &&
741 (quantum_info->format != FloatingPointQuantumFormat))
cristyf84dc8e2013-02-02 16:46:26 +0000742 SetFITSUnsignedPixels(image->columns,image->depth,image->endian,
743 pixels);
cristyfcbcae82011-04-22 12:53:27 +0000744 count=WriteBlob(image,length,pixels);
745 if (count != (ssize_t) length)
746 break;
747 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
748 image->rows);
749 if (status == MagickFalse)
750 break;
751 }
752 }
753 else
754 {
755 length=GetQuantumExtent(image,quantum_info,RedQuantum);
756 for (y=(ssize_t) image->rows-1; y >= 0; y--)
757 {
cristy1e178e72011-08-28 19:44:34 +0000758 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +0000759 if (p == (const Quantum *) NULL)
cristyfcbcae82011-04-22 12:53:27 +0000760 break;
cristy4c08aed2011-07-01 19:47:50 +0000761 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
cristy1e178e72011-08-28 19:44:34 +0000762 RedQuantum,pixels,exception);
cristyfcbcae82011-04-22 12:53:27 +0000763 if (image->depth == 16)
cristyf84dc8e2013-02-02 16:46:26 +0000764 SetFITSUnsignedPixels(image->columns,image->depth,image->endian,
765 pixels);
cristyfcbcae82011-04-22 12:53:27 +0000766 if (((image->depth == 32) || (image->depth == 64)) &&
767 (quantum_info->format != FloatingPointQuantumFormat))
cristyf84dc8e2013-02-02 16:46:26 +0000768 SetFITSUnsignedPixels(image->columns,image->depth,image->endian,
769 pixels);
cristyfcbcae82011-04-22 12:53:27 +0000770 count=WriteBlob(image,length,pixels);
771 if (count != (ssize_t) length)
772 break;
773 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
774 image->rows);
775 if (status == MagickFalse)
776 break;
777 }
778 length=GetQuantumExtent(image,quantum_info,GreenQuantum);
779 for (y=(ssize_t) image->rows-1; y >= 0; y--)
780 {
cristy1e178e72011-08-28 19:44:34 +0000781 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +0000782 if (p == (const Quantum *) NULL)
cristyfcbcae82011-04-22 12:53:27 +0000783 break;
cristy4c08aed2011-07-01 19:47:50 +0000784 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
cristy1e178e72011-08-28 19:44:34 +0000785 GreenQuantum,pixels,exception);
cristyfcbcae82011-04-22 12:53:27 +0000786 if (image->depth == 16)
cristyf84dc8e2013-02-02 16:46:26 +0000787 SetFITSUnsignedPixels(image->columns,image->depth,image->endian,
788 pixels);
cristyfcbcae82011-04-22 12:53:27 +0000789 if (((image->depth == 32) || (image->depth == 64)) &&
790 (quantum_info->format != FloatingPointQuantumFormat))
cristyf84dc8e2013-02-02 16:46:26 +0000791 SetFITSUnsignedPixels(image->columns,image->depth,image->endian,
792 pixels);
cristyfcbcae82011-04-22 12:53:27 +0000793 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 length=GetQuantumExtent(image,quantum_info,BlueQuantum);
802 for (y=(ssize_t) image->rows-1; y >= 0; y--)
803 {
cristy1e178e72011-08-28 19:44:34 +0000804 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +0000805 if (p == (const Quantum *) NULL)
cristyfcbcae82011-04-22 12:53:27 +0000806 break;
cristy4c08aed2011-07-01 19:47:50 +0000807 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
cristy1e178e72011-08-28 19:44:34 +0000808 BlueQuantum,pixels,exception);
cristyfcbcae82011-04-22 12:53:27 +0000809 if (image->depth == 16)
cristyf84dc8e2013-02-02 16:46:26 +0000810 SetFITSUnsignedPixels(image->columns,image->depth,image->endian,
811 pixels);
cristyfcbcae82011-04-22 12:53:27 +0000812 if (((image->depth == 32) || (image->depth == 64)) &&
813 (quantum_info->format != FloatingPointQuantumFormat))
cristyf84dc8e2013-02-02 16:46:26 +0000814 SetFITSUnsignedPixels(image->columns,image->depth,image->endian,
815 pixels);
cristyfcbcae82011-04-22 12:53:27 +0000816 count=WriteBlob(image,length,pixels);
817 if (count != (ssize_t) length)
818 break;
819 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
820 image->rows);
821 if (status == MagickFalse)
822 break;
823 }
824 }
cristy3ed852e2009-09-05 21:47:34 +0000825 quantum_info=DestroyQuantumInfo(quantum_info);
826 length=(size_t) (FITSBlocksize-TellBlob(image) % FITSBlocksize);
827 if (length != 0)
828 {
829 (void) ResetMagickMemory(fits_info,0,length*sizeof(*fits_info));
830 (void) WriteBlob(image,length,(unsigned char *) fits_info);
831 }
832 fits_info=DestroyString(fits_info);
833 (void) CloseBlob(image);
834 return(MagickTrue);
835}