cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 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 | % % |
cristy | 1454be7 | 2011-12-19 01:52:48 +0000 | [diff] [blame] | 20 | % Copyright 1999-2012 ImageMagick Studio LLC, a non-profit organization % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 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 | |
| 39 | /* |
| 40 | Include declarations. |
| 41 | */ |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 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" |
cristy | 510d06a | 2011-07-06 23:43:54 +0000 | [diff] [blame] | 49 | #include "MagickCore/colorspace-private.h" |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 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" |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 69 | |
| 70 | /* |
| 71 | Forward declarations. |
| 72 | */ |
| 73 | #define FITSBlocksize 2880UL |
| 74 | |
| 75 | /* |
| 76 | Forward declarations. |
| 77 | */ |
| 78 | static MagickBooleanType |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 79 | WriteFITSImage(const ImageInfo *,Image *,ExceptionInfo *); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 80 | |
| 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 | |
| 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) |
cristy | 6cff05d | 2010-09-02 11:22:46 +0000 | [diff] [blame] | 156 | return((double) ((int) ReadBlobLong(image))); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 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 void GetFITSPixelExtrema(Image *image,const int bits_per_pixel, |
| 171 | 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 | number_pixels=(MagickSizeType) image->columns*image->rows; |
| 187 | *minima=GetFITSPixel(image,bits_per_pixel); |
| 188 | *maxima=(*minima); |
| 189 | for (i=1; i < (MagickOffsetType) number_pixels; i++) |
| 190 | { |
| 191 | pixel=GetFITSPixel(image,bits_per_pixel); |
| 192 | if (pixel < *minima) |
| 193 | *minima=pixel; |
| 194 | if (pixel > *maxima) |
| 195 | *maxima=pixel; |
| 196 | } |
| 197 | (void) SeekBlob(image,offset,SEEK_SET); |
| 198 | } |
| 199 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 200 | static inline double GetFITSPixelRange(const size_t depth) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 201 | { |
| 202 | return((double) ((MagickOffsetType) GetQuantumRange(depth))); |
| 203 | } |
| 204 | |
| 205 | static void SetFITSUnsignedPixels(const size_t length, |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 206 | const size_t bits_per_pixel,unsigned char *pixels) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 207 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 208 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 209 | i; |
| 210 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 211 | for (i=0; i < (ssize_t) length; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 212 | { |
| 213 | *pixels^=0x80; |
| 214 | pixels+=bits_per_pixel >> 3; |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | static Image *ReadFITSImage(const ImageInfo *image_info, |
| 219 | ExceptionInfo *exception) |
| 220 | { |
| 221 | typedef struct _FITSInfo |
| 222 | { |
| 223 | MagickBooleanType |
| 224 | extend, |
| 225 | simple; |
| 226 | |
| 227 | int |
| 228 | bits_per_pixel, |
| 229 | columns, |
| 230 | rows, |
| 231 | number_axes, |
| 232 | number_planes; |
| 233 | |
| 234 | double |
| 235 | min_data, |
| 236 | max_data, |
| 237 | zero, |
| 238 | scale; |
| 239 | |
| 240 | EndianType |
| 241 | endian; |
| 242 | } FITSInfo; |
| 243 | |
| 244 | char |
| 245 | *comment, |
| 246 | keyword[9], |
| 247 | property[MaxTextExtent], |
| 248 | value[73]; |
| 249 | |
| 250 | double |
| 251 | pixel, |
| 252 | scale; |
| 253 | |
| 254 | FITSInfo |
| 255 | fits_info; |
| 256 | |
| 257 | Image |
| 258 | *image; |
| 259 | |
| 260 | int |
| 261 | c; |
| 262 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 263 | MagickBooleanType |
| 264 | status; |
| 265 | |
| 266 | MagickSizeType |
| 267 | number_pixels; |
| 268 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 269 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 270 | i, |
| 271 | x; |
| 272 | |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 273 | register Quantum |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 274 | *q; |
| 275 | |
| 276 | ssize_t |
cristy | 202de44 | 2011-04-24 18:19:07 +0000 | [diff] [blame] | 277 | count, |
| 278 | scene, |
| 279 | y; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 280 | |
| 281 | /* |
| 282 | Open image file. |
| 283 | */ |
| 284 | assert(image_info != (const ImageInfo *) NULL); |
| 285 | assert(image_info->signature == MagickSignature); |
| 286 | if (image_info->debug != MagickFalse) |
| 287 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s", |
| 288 | image_info->filename); |
| 289 | assert(exception != (ExceptionInfo *) NULL); |
| 290 | assert(exception->signature == MagickSignature); |
cristy | 9950d57 | 2011-10-01 18:22:35 +0000 | [diff] [blame] | 291 | image=AcquireImage(image_info,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 292 | status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception); |
| 293 | if (status == MagickFalse) |
| 294 | { |
| 295 | image=DestroyImageList(image); |
| 296 | return((Image *) NULL); |
| 297 | } |
| 298 | /* |
| 299 | Initialize image header. |
| 300 | */ |
| 301 | (void) ResetMagickMemory(&fits_info,0,sizeof(fits_info)); |
| 302 | fits_info.extend=MagickFalse; |
| 303 | fits_info.simple=MagickFalse; |
| 304 | fits_info.bits_per_pixel=8; |
| 305 | fits_info.columns=1; |
| 306 | fits_info.rows=1; |
| 307 | fits_info.rows=1; |
| 308 | fits_info.number_planes=1; |
| 309 | fits_info.min_data=0.0; |
| 310 | fits_info.max_data=0.0; |
| 311 | fits_info.zero=0.0; |
| 312 | fits_info.scale=1.0; |
| 313 | fits_info.endian=MSBEndian; |
| 314 | /* |
| 315 | Decode image header. |
| 316 | */ |
| 317 | for (comment=(char *) NULL; EOFBlob(image) == MagickFalse; ) |
| 318 | { |
| 319 | for ( ; EOFBlob(image) == MagickFalse; ) |
| 320 | { |
| 321 | register char |
| 322 | *p; |
| 323 | |
| 324 | count=ReadBlob(image,8,(unsigned char *) keyword); |
| 325 | if (count != 8) |
| 326 | break; |
| 327 | for (i=0; i < 8; i++) |
| 328 | { |
| 329 | if (isspace((int) ((unsigned char) keyword[i])) != 0) |
| 330 | break; |
cristy | 466802f | 2010-02-24 14:18:37 +0000 | [diff] [blame] | 331 | keyword[i]=tolower((int) ((unsigned char) keyword[i])); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 332 | } |
| 333 | keyword[i]='\0'; |
| 334 | count=ReadBlob(image,72,(unsigned char *) value); |
| 335 | if (count != 72) |
| 336 | break; |
| 337 | value[72]='\0'; |
| 338 | p=value; |
| 339 | if (*p == '=') |
| 340 | { |
| 341 | p+=2; |
| 342 | while (isspace((int) ((unsigned char) *p)) != 0) |
| 343 | p++; |
| 344 | } |
| 345 | if (LocaleCompare(keyword,"end") == 0) |
| 346 | break; |
| 347 | if (LocaleCompare(keyword,"extend") == 0) |
| 348 | fits_info.extend=(*p == 'T') || (*p == 't') ? MagickTrue : MagickFalse; |
| 349 | if (LocaleCompare(keyword,"simple") == 0) |
| 350 | fits_info.simple=(*p == 'T') || (*p == 't') ? MagickTrue : MagickFalse; |
| 351 | if (LocaleCompare(keyword,"bitpix") == 0) |
cristy | f2f2727 | 2009-12-17 14:48:46 +0000 | [diff] [blame] | 352 | fits_info.bits_per_pixel=StringToLong(p); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 353 | if (LocaleCompare(keyword,"naxis") == 0) |
cristy | f2f2727 | 2009-12-17 14:48:46 +0000 | [diff] [blame] | 354 | fits_info.number_axes=StringToLong(p); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 355 | if (LocaleCompare(keyword,"naxis1") == 0) |
cristy | f2f2727 | 2009-12-17 14:48:46 +0000 | [diff] [blame] | 356 | fits_info.columns=StringToLong(p); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 357 | if (LocaleCompare(keyword,"naxis2") == 0) |
cristy | f2f2727 | 2009-12-17 14:48:46 +0000 | [diff] [blame] | 358 | fits_info.rows=StringToLong(p); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 359 | if (LocaleCompare(keyword,"naxis3") == 0) |
cristy | f2f2727 | 2009-12-17 14:48:46 +0000 | [diff] [blame] | 360 | fits_info.number_planes=StringToLong(p); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 361 | if (LocaleCompare(keyword,"datamax") == 0) |
cristy | dbdd0e3 | 2011-11-04 23:29:40 +0000 | [diff] [blame] | 362 | fits_info.max_data=StringToDouble(p,(char **) NULL); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 363 | if (LocaleCompare(keyword,"datamin") == 0) |
cristy | dbdd0e3 | 2011-11-04 23:29:40 +0000 | [diff] [blame] | 364 | fits_info.min_data=StringToDouble(p,(char **) NULL); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 365 | if (LocaleCompare(keyword,"bzero") == 0) |
cristy | dbdd0e3 | 2011-11-04 23:29:40 +0000 | [diff] [blame] | 366 | fits_info.zero=StringToDouble(p,(char **) NULL); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 367 | if (LocaleCompare(keyword,"bscale") == 0) |
cristy | dbdd0e3 | 2011-11-04 23:29:40 +0000 | [diff] [blame] | 368 | fits_info.scale=StringToDouble(p,(char **) NULL); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 369 | if (LocaleCompare(keyword,"comment") == 0) |
| 370 | { |
| 371 | if (comment == (char *) NULL) |
| 372 | comment=ConstantString(p); |
| 373 | else |
| 374 | (void) ConcatenateString(&comment,p); |
| 375 | } |
| 376 | if (LocaleCompare(keyword,"xendian") == 0) |
| 377 | { |
| 378 | if (LocaleNCompare(p,"big",3) == 0) |
| 379 | fits_info.endian=MSBEndian; |
| 380 | else |
| 381 | fits_info.endian=LSBEndian; |
| 382 | } |
cristy | b51dff5 | 2011-05-19 16:55:47 +0000 | [diff] [blame] | 383 | (void) FormatLocaleString(property,MaxTextExtent,"fits:%s",keyword); |
cristy | d15e659 | 2011-10-15 00:13:06 +0000 | [diff] [blame] | 384 | (void) SetImageProperty(image,property,p,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 385 | } |
| 386 | c=0; |
| 387 | while (((TellBlob(image) % FITSBlocksize) != 0) && (c != EOF)) |
| 388 | c=ReadBlobByte(image); |
| 389 | if (fits_info.extend == MagickFalse) |
| 390 | break; |
| 391 | number_pixels=(MagickSizeType) fits_info.columns*fits_info.rows; |
| 392 | if ((fits_info.simple != MagickFalse) && (fits_info.number_axes >= 1) && |
| 393 | (fits_info.number_axes <= 4) && (number_pixels != 0)) |
| 394 | break; |
| 395 | } |
| 396 | /* |
| 397 | Verify that required image information is defined. |
| 398 | */ |
| 399 | if (comment != (char *) NULL) |
| 400 | { |
cristy | d15e659 | 2011-10-15 00:13:06 +0000 | [diff] [blame] | 401 | (void) SetImageProperty(image,"comment",comment,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 402 | comment=DestroyString(comment); |
| 403 | } |
| 404 | if (EOFBlob(image) != MagickFalse) |
| 405 | ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile", |
| 406 | image->filename); |
| 407 | number_pixels=(MagickSizeType) fits_info.columns*fits_info.rows; |
| 408 | if ((fits_info.simple == MagickFalse) || (fits_info.number_axes < 1) || |
| 409 | (fits_info.number_axes > 4) || (number_pixels == 0)) |
| 410 | ThrowReaderException(CorruptImageError,"ImageTypeNotSupported"); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 411 | for (scene=0; scene < (ssize_t) fits_info.number_planes; scene++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 412 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 413 | image->columns=(size_t) fits_info.columns; |
| 414 | image->rows=(size_t) fits_info.rows; |
| 415 | image->depth=(size_t) (fits_info.bits_per_pixel < 0 ? -1 : 1)* |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 416 | fits_info.bits_per_pixel; |
| 417 | image->endian=fits_info.endian; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 418 | image->scene=(size_t) scene; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 419 | if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0)) |
| 420 | if (image->scene >= (image_info->scene+image_info->number_scenes-1)) |
| 421 | break; |
| 422 | /* |
| 423 | Initialize image structure. |
| 424 | */ |
cristy | b2c3f40 | 2012-05-13 18:05:12 +0000 | [diff] [blame] | 425 | SetImageColorspace(image,GRAYColorspace,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 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)) |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 429 | fits_info.max_data=GetFITSPixelRange((size_t) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 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); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 440 | for (y=(ssize_t) image->rows-1; y >= 0; y--) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 441 | { |
| 442 | q=QueueAuthenticPixels(image,0,y,image->columns,1,exception); |
cristy | acd2ed2 | 2011-08-30 01:44:23 +0000 | [diff] [blame] | 443 | if (q == (Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 444 | break; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 445 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 446 | { |
| 447 | pixel=GetFITSPixel(image,fits_info.bits_per_pixel); |
cristy | 1214288 | 2012-06-17 00:52:29 +0000 | [diff] [blame] | 448 | SetPixelGray(image,ClampToQuantum(scale*(fits_info.scale*(pixel- |
| 449 | fits_info.min_data)+fits_info.zero)),q); |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 450 | q+=GetPixelChannels(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 451 | } |
| 452 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
| 453 | break; |
| 454 | if (image->previous == (Image *) NULL) |
| 455 | { |
cristy | cee9711 | 2010-05-28 00:44:52 +0000 | [diff] [blame] | 456 | status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y, |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 457 | image->rows); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 458 | if (status == MagickFalse) |
| 459 | break; |
| 460 | } |
| 461 | } |
| 462 | if (EOFBlob(image) != MagickFalse) |
| 463 | { |
| 464 | ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile", |
| 465 | image->filename); |
| 466 | break; |
| 467 | } |
| 468 | /* |
| 469 | Proceed to next image. |
| 470 | */ |
| 471 | if (image_info->number_scenes != 0) |
| 472 | if (image->scene >= (image_info->scene+image_info->number_scenes-1)) |
| 473 | break; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 474 | if (scene < (ssize_t) (fits_info.number_planes-1)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 475 | { |
| 476 | /* |
| 477 | Allocate next image structure. |
| 478 | */ |
cristy | 9950d57 | 2011-10-01 18:22:35 +0000 | [diff] [blame] | 479 | AcquireNextImage(image_info,image,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 480 | if (GetNextImageInList(image) == (Image *) NULL) |
| 481 | { |
| 482 | image=DestroyImageList(image); |
| 483 | return((Image *) NULL); |
| 484 | } |
| 485 | image=SyncNextImageInList(image); |
| 486 | status=SetImageProgress(image,LoadImagesTag,TellBlob(image), |
| 487 | GetBlobSize(image)); |
| 488 | if (status == MagickFalse) |
| 489 | break; |
| 490 | } |
| 491 | } |
| 492 | (void) CloseBlob(image); |
| 493 | return(GetFirstImageInList(image)); |
| 494 | } |
| 495 | |
| 496 | /* |
| 497 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 498 | % % |
| 499 | % % |
| 500 | % % |
| 501 | % R e g i s t e r F I T S I m a g e % |
| 502 | % % |
| 503 | % % |
| 504 | % % |
| 505 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 506 | % |
| 507 | % RegisterFITSImage() adds attributes for the FITS image format to |
| 508 | % the list of supported formats. The attributes include the image format |
| 509 | % tag, a method to read and/or write the format, whether the format |
| 510 | % supports the saving of more than one frame to the same file or blob, |
| 511 | % whether the format supports native in-memory I/O, and a brief |
| 512 | % description of the format. |
| 513 | % |
| 514 | % The format of the RegisterFITSImage method is: |
| 515 | % |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 516 | % size_t RegisterFITSImage(void) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 517 | % |
| 518 | */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 519 | ModuleExport size_t RegisterFITSImage(void) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 520 | { |
| 521 | MagickInfo |
| 522 | *entry; |
| 523 | |
| 524 | entry=SetMagickInfo("FITS"); |
| 525 | entry->decoder=(DecodeImageHandler *) ReadFITSImage; |
| 526 | entry->encoder=(EncodeImageHandler *) WriteFITSImage; |
| 527 | entry->magick=(IsImageFormatHandler *) IsFITS; |
| 528 | entry->adjoin=MagickFalse; |
| 529 | entry->seekable_stream=MagickTrue; |
| 530 | entry->description=ConstantString("Flexible Image Transport System"); |
| 531 | entry->module=ConstantString("FITS"); |
| 532 | (void) RegisterMagickInfo(entry); |
| 533 | entry=SetMagickInfo("FTS"); |
| 534 | entry->decoder=(DecodeImageHandler *) ReadFITSImage; |
| 535 | entry->encoder=(EncodeImageHandler *) WriteFITSImage; |
| 536 | entry->magick=(IsImageFormatHandler *) IsFITS; |
| 537 | entry->adjoin=MagickFalse; |
| 538 | entry->seekable_stream=MagickTrue; |
| 539 | entry->description=ConstantString("Flexible Image Transport System"); |
| 540 | entry->module=ConstantString("FTS"); |
| 541 | (void) RegisterMagickInfo(entry); |
| 542 | return(MagickImageCoderSignature); |
| 543 | } |
| 544 | |
| 545 | /* |
| 546 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 547 | % % |
| 548 | % % |
| 549 | % % |
| 550 | % U n r e g i s t e r F I T S I m a g e % |
| 551 | % % |
| 552 | % % |
| 553 | % % |
| 554 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 555 | % |
| 556 | % UnregisterFITSImage() removes format registrations made by the |
| 557 | % FITS module from the list of supported formats. |
| 558 | % |
| 559 | % The format of the UnregisterFITSImage method is: |
| 560 | % |
| 561 | % UnregisterFITSImage(void) |
| 562 | % |
| 563 | */ |
| 564 | ModuleExport void UnregisterFITSImage(void) |
| 565 | { |
| 566 | (void) UnregisterMagickInfo("FITS"); |
| 567 | (void) UnregisterMagickInfo("FTS"); |
| 568 | } |
| 569 | |
| 570 | /* |
| 571 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 572 | % % |
| 573 | % % |
| 574 | % % |
| 575 | % W r i t e F I T S I m a g e % |
| 576 | % % |
| 577 | % % |
| 578 | % % |
| 579 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 580 | % |
| 581 | % WriteFITSImage() writes a Flexible Image Transport System image to a |
| 582 | % file as gray scale intensities [0..255]. |
| 583 | % |
| 584 | % The format of the WriteFITSImage method is: |
| 585 | % |
| 586 | % MagickBooleanType WriteFITSImage(const ImageInfo *image_info, |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 587 | % Image *image,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 588 | % |
| 589 | % A description of each parameter follows. |
| 590 | % |
| 591 | % o image_info: the image info. |
| 592 | % |
| 593 | % o image: The image. |
| 594 | % |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 595 | % o exception: return any errors or warnings in this structure. |
| 596 | % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 597 | */ |
| 598 | static MagickBooleanType WriteFITSImage(const ImageInfo *image_info, |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 599 | Image *image,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 600 | { |
| 601 | char |
| 602 | header[FITSBlocksize], |
| 603 | *fits_info; |
| 604 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 605 | MagickBooleanType |
| 606 | status; |
| 607 | |
| 608 | QuantumInfo |
| 609 | *quantum_info; |
| 610 | |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 611 | register const Quantum |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 612 | *p; |
| 613 | |
| 614 | size_t |
| 615 | length; |
| 616 | |
| 617 | ssize_t |
| 618 | count, |
cristy | 2692d59 | 2010-11-01 18:07:27 +0000 | [diff] [blame] | 619 | offset, |
| 620 | y; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 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); |
cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 634 | assert(exception != (ExceptionInfo *) NULL); |
| 635 | assert(exception->signature == MagickSignature); |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 636 | status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 637 | if (status == MagickFalse) |
| 638 | return(status); |
cristy | 3d9f5ba | 2012-06-26 13:37:31 +0000 | [diff] [blame] | 639 | if (IssRGBCompatibleColorspace(image->colorspace) == MagickFalse) |
cristy | d9ecd04 | 2012-06-17 18:26:12 +0000 | [diff] [blame] | 640 | (void) TransformImageColorspace(image,sRGBColorspace,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 641 | /* |
| 642 | Allocate image memory. |
| 643 | */ |
| 644 | fits_info=(char *) AcquireQuantumMemory(FITSBlocksize,sizeof(*fits_info)); |
| 645 | if (fits_info == (char *) NULL) |
| 646 | ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed"); |
| 647 | (void) ResetMagickMemory(fits_info,' ',FITSBlocksize*sizeof(*fits_info)); |
| 648 | /* |
| 649 | Initialize image header. |
| 650 | */ |
| 651 | image->depth=GetImageQuantumDepth(image,MagickFalse); |
| 652 | quantum_info=AcquireQuantumInfo((const ImageInfo *) NULL,image); |
| 653 | if (quantum_info == (QuantumInfo *) NULL) |
| 654 | ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed"); |
| 655 | offset=0; |
cristy | b51dff5 | 2011-05-19 16:55:47 +0000 | [diff] [blame] | 656 | (void) FormatLocaleString(header,FITSBlocksize, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 657 | "SIMPLE = T"); |
| 658 | (void) strncpy(fits_info+offset,header,strlen(header)); |
| 659 | offset+=80; |
cristy | b51dff5 | 2011-05-19 16:55:47 +0000 | [diff] [blame] | 660 | (void) FormatLocaleString(header,FITSBlocksize,"BITPIX = %10ld", |
cristy | f2faecf | 2010-05-28 19:19:36 +0000 | [diff] [blame] | 661 | (long) (quantum_info->format == FloatingPointQuantumFormat ? -1 : 1)* |
| 662 | image->depth); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 663 | (void) strncpy(fits_info+offset,header,strlen(header)); |
| 664 | offset+=80; |
cristy | b51dff5 | 2011-05-19 16:55:47 +0000 | [diff] [blame] | 665 | (void) FormatLocaleString(header,FITSBlocksize,"NAXIS = %10lu", |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 666 | IsImageGray(image,exception) != MagickFalse ? 2UL : 3UL); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 667 | (void) strncpy(fits_info+offset,header,strlen(header)); |
| 668 | offset+=80; |
cristy | b51dff5 | 2011-05-19 16:55:47 +0000 | [diff] [blame] | 669 | (void) FormatLocaleString(header,FITSBlocksize,"NAXIS1 = %10lu", |
cristy | f2faecf | 2010-05-28 19:19:36 +0000 | [diff] [blame] | 670 | (unsigned long) image->columns); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 671 | (void) strncpy(fits_info+offset,header,strlen(header)); |
| 672 | offset+=80; |
cristy | b51dff5 | 2011-05-19 16:55:47 +0000 | [diff] [blame] | 673 | (void) FormatLocaleString(header,FITSBlocksize,"NAXIS2 = %10lu", |
cristy | f2faecf | 2010-05-28 19:19:36 +0000 | [diff] [blame] | 674 | (unsigned long) image->rows); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 675 | (void) strncpy(fits_info+offset,header,strlen(header)); |
| 676 | offset+=80; |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 677 | if (IsImageGray(image,exception) == MagickFalse) |
cristy | fcbcae8 | 2011-04-22 12:53:27 +0000 | [diff] [blame] | 678 | { |
cristy | b51dff5 | 2011-05-19 16:55:47 +0000 | [diff] [blame] | 679 | (void) FormatLocaleString(header,FITSBlocksize, |
cristy | fcbcae8 | 2011-04-22 12:53:27 +0000 | [diff] [blame] | 680 | "NAXIS3 = %10lu",3UL); |
| 681 | (void) strncpy(fits_info+offset,header,strlen(header)); |
| 682 | offset+=80; |
| 683 | } |
cristy | b51dff5 | 2011-05-19 16:55:47 +0000 | [diff] [blame] | 684 | (void) FormatLocaleString(header,FITSBlocksize,"BSCALE = %E",1.0); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 685 | (void) strncpy(fits_info+offset,header,strlen(header)); |
| 686 | offset+=80; |
cristy | b51dff5 | 2011-05-19 16:55:47 +0000 | [diff] [blame] | 687 | (void) FormatLocaleString(header,FITSBlocksize,"BZERO = %E", |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 688 | image->depth > 8 ? GetFITSPixelRange(image->depth) : 0.0); |
| 689 | (void) strncpy(fits_info+offset,header,strlen(header)); |
| 690 | offset+=80; |
cristy | b51dff5 | 2011-05-19 16:55:47 +0000 | [diff] [blame] | 691 | (void) FormatLocaleString(header,FITSBlocksize,"DATAMAX = %E", |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 692 | 1.0*((MagickOffsetType) GetQuantumRange(image->depth))); |
| 693 | (void) strncpy(fits_info+offset,header,strlen(header)); |
| 694 | offset+=80; |
cristy | b51dff5 | 2011-05-19 16:55:47 +0000 | [diff] [blame] | 695 | (void) FormatLocaleString(header,FITSBlocksize,"DATAMIN = %E",0.0); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 696 | (void) strncpy(fits_info+offset,header,strlen(header)); |
| 697 | offset+=80; |
| 698 | if (image->endian == LSBEndian) |
| 699 | { |
cristy | b51dff5 | 2011-05-19 16:55:47 +0000 | [diff] [blame] | 700 | (void) FormatLocaleString(header,FITSBlocksize,"XENDIAN = 'SMALL'"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 701 | (void) strncpy(fits_info+offset,header,strlen(header)); |
| 702 | offset+=80; |
| 703 | } |
cristy | b51dff5 | 2011-05-19 16:55:47 +0000 | [diff] [blame] | 704 | (void) FormatLocaleString(header,FITSBlocksize,"HISTORY %.72s", |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 705 | GetMagickVersion((size_t *) NULL)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 706 | (void) strncpy(fits_info+offset,header,strlen(header)); |
| 707 | offset+=80; |
| 708 | (void) strncpy(header,"END",FITSBlocksize); |
| 709 | (void) strncpy(fits_info+offset,header,strlen(header)); |
| 710 | offset+=80; |
| 711 | (void) WriteBlob(image,FITSBlocksize,(unsigned char *) fits_info); |
| 712 | /* |
| 713 | Convert image to fits scale PseudoColor class. |
| 714 | */ |
| 715 | pixels=GetQuantumPixels(quantum_info); |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 716 | if (IsImageGray(image,exception) != MagickFalse) |
cristy | fcbcae8 | 2011-04-22 12:53:27 +0000 | [diff] [blame] | 717 | { |
| 718 | length=GetQuantumExtent(image,quantum_info,GrayQuantum); |
| 719 | for (y=(ssize_t) image->rows-1; y >= 0; y--) |
| 720 | { |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 721 | p=GetVirtualPixels(image,0,y,image->columns,1,exception); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 722 | if (p == (const Quantum *) NULL) |
cristy | fcbcae8 | 2011-04-22 12:53:27 +0000 | [diff] [blame] | 723 | break; |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 724 | length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info, |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 725 | GrayQuantum,pixels,exception); |
cristy | fcbcae8 | 2011-04-22 12:53:27 +0000 | [diff] [blame] | 726 | if (image->depth == 16) |
| 727 | SetFITSUnsignedPixels(image->columns,image->depth,pixels); |
| 728 | if (((image->depth == 32) || (image->depth == 64)) && |
| 729 | (quantum_info->format != FloatingPointQuantumFormat)) |
| 730 | SetFITSUnsignedPixels(image->columns,image->depth,pixels); |
| 731 | count=WriteBlob(image,length,pixels); |
| 732 | if (count != (ssize_t) length) |
| 733 | break; |
| 734 | status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y, |
| 735 | image->rows); |
| 736 | if (status == MagickFalse) |
| 737 | break; |
| 738 | } |
| 739 | } |
| 740 | else |
| 741 | { |
| 742 | length=GetQuantumExtent(image,quantum_info,RedQuantum); |
| 743 | for (y=(ssize_t) image->rows-1; y >= 0; y--) |
| 744 | { |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 745 | p=GetVirtualPixels(image,0,y,image->columns,1,exception); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 746 | if (p == (const Quantum *) NULL) |
cristy | fcbcae8 | 2011-04-22 12:53:27 +0000 | [diff] [blame] | 747 | break; |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 748 | length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info, |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 749 | RedQuantum,pixels,exception); |
cristy | fcbcae8 | 2011-04-22 12:53:27 +0000 | [diff] [blame] | 750 | if (image->depth == 16) |
| 751 | SetFITSUnsignedPixels(image->columns,image->depth,pixels); |
| 752 | if (((image->depth == 32) || (image->depth == 64)) && |
| 753 | (quantum_info->format != FloatingPointQuantumFormat)) |
| 754 | SetFITSUnsignedPixels(image->columns,image->depth,pixels); |
| 755 | count=WriteBlob(image,length,pixels); |
| 756 | if (count != (ssize_t) length) |
| 757 | break; |
| 758 | status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y, |
| 759 | image->rows); |
| 760 | if (status == MagickFalse) |
| 761 | break; |
| 762 | } |
| 763 | length=GetQuantumExtent(image,quantum_info,GreenQuantum); |
| 764 | for (y=(ssize_t) image->rows-1; y >= 0; y--) |
| 765 | { |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 766 | p=GetVirtualPixels(image,0,y,image->columns,1,exception); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 767 | if (p == (const Quantum *) NULL) |
cristy | fcbcae8 | 2011-04-22 12:53:27 +0000 | [diff] [blame] | 768 | break; |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 769 | length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info, |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 770 | GreenQuantum,pixels,exception); |
cristy | fcbcae8 | 2011-04-22 12:53:27 +0000 | [diff] [blame] | 771 | if (image->depth == 16) |
| 772 | SetFITSUnsignedPixels(image->columns,image->depth,pixels); |
| 773 | if (((image->depth == 32) || (image->depth == 64)) && |
| 774 | (quantum_info->format != FloatingPointQuantumFormat)) |
| 775 | SetFITSUnsignedPixels(image->columns,image->depth,pixels); |
| 776 | count=WriteBlob(image,length,pixels); |
| 777 | if (count != (ssize_t) length) |
| 778 | break; |
| 779 | status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y, |
| 780 | image->rows); |
| 781 | if (status == MagickFalse) |
| 782 | break; |
| 783 | } |
| 784 | length=GetQuantumExtent(image,quantum_info,BlueQuantum); |
| 785 | for (y=(ssize_t) image->rows-1; y >= 0; y--) |
| 786 | { |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 787 | p=GetVirtualPixels(image,0,y,image->columns,1,exception); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 788 | if (p == (const Quantum *) NULL) |
cristy | fcbcae8 | 2011-04-22 12:53:27 +0000 | [diff] [blame] | 789 | break; |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 790 | length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info, |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 791 | BlueQuantum,pixels,exception); |
cristy | fcbcae8 | 2011-04-22 12:53:27 +0000 | [diff] [blame] | 792 | if (image->depth == 16) |
| 793 | SetFITSUnsignedPixels(image->columns,image->depth,pixels); |
| 794 | if (((image->depth == 32) || (image->depth == 64)) && |
| 795 | (quantum_info->format != FloatingPointQuantumFormat)) |
| 796 | SetFITSUnsignedPixels(image->columns,image->depth,pixels); |
| 797 | count=WriteBlob(image,length,pixels); |
| 798 | if (count != (ssize_t) length) |
| 799 | break; |
| 800 | status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y, |
| 801 | image->rows); |
| 802 | if (status == MagickFalse) |
| 803 | break; |
| 804 | } |
| 805 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 806 | quantum_info=DestroyQuantumInfo(quantum_info); |
| 807 | length=(size_t) (FITSBlocksize-TellBlob(image) % FITSBlocksize); |
| 808 | if (length != 0) |
| 809 | { |
| 810 | (void) ResetMagickMemory(fits_info,0,length*sizeof(*fits_info)); |
| 811 | (void) WriteBlob(image,length,(unsigned char *) fits_info); |
| 812 | } |
| 813 | fits_info=DestroyString(fits_info); |
| 814 | (void) CloseBlob(image); |
| 815 | return(MagickTrue); |
| 816 | } |