cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1 | /* |
| 2 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 3 | % % |
| 4 | % % |
| 5 | % % |
| 6 | % V V IIIII CCCC AAA RRRR % |
| 7 | % V V I C A A R R % |
| 8 | % V V I C AAAAA RRRR % |
| 9 | % V V I C A A R R % |
| 10 | % V IIIII CCCC A A R R % |
| 11 | % % |
| 12 | % % |
| 13 | % Read/Write VICAR Rasterfile Format % |
| 14 | % % |
| 15 | % Software Design % |
cristy | de984cd | 2013-12-01 14:49:27 +0000 | [diff] [blame] | 16 | % Cristy % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 17 | % July 1992 % |
| 18 | % % |
| 19 | % % |
Cristy | f6ff9ea | 2016-12-05 09:53:35 -0500 | [diff] [blame] | 20 | % Copyright 1999-2017 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 | % % |
Cristy | f19d414 | 2017-04-24 11:34:30 -0400 | [diff] [blame^] | 26 | % https://www.imagemagick.org/script/license.php % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 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" |
cristy | d9ecd04 | 2012-06-17 18:26:12 +0000 | [diff] [blame] | 43 | #include "MagickCore/attribute.h" |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 44 | #include "MagickCore/blob.h" |
| 45 | #include "MagickCore/blob-private.h" |
| 46 | #include "MagickCore/cache.h" |
| 47 | #include "MagickCore/colormap.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/quantum-private.h" |
| 62 | #include "MagickCore/quantum-private.h" |
| 63 | #include "MagickCore/static.h" |
| 64 | #include "MagickCore/string_.h" |
| 65 | #include "MagickCore/string-private.h" |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 66 | |
| 67 | /* |
| 68 | Forward declarations. |
| 69 | */ |
| 70 | static MagickBooleanType |
cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 71 | WriteVICARImage(const ImageInfo *,Image *,ExceptionInfo *); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 72 | |
| 73 | /* |
| 74 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 75 | % % |
| 76 | % % |
| 77 | % % |
| 78 | % I s V I C A R % |
| 79 | % % |
| 80 | % % |
| 81 | % % |
| 82 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 83 | % |
| 84 | % IsVICAR() returns MagickTrue if the image format type, identified by the |
| 85 | % magick string, is VICAR. |
| 86 | % |
| 87 | % The format of the IsVICAR method is: |
| 88 | % |
| 89 | % MagickBooleanType IsVICAR(const unsigned char *magick, |
| 90 | % const size_t length) |
| 91 | % |
| 92 | % A description of each parameter follows: |
| 93 | % |
| 94 | % o magick: compare image format pattern against these bytes. |
| 95 | % |
| 96 | % o length: Specifies the length of the magick string. |
| 97 | % |
| 98 | */ |
| 99 | static MagickBooleanType IsVICAR(const unsigned char *magick, |
| 100 | const size_t length) |
| 101 | { |
| 102 | if (length < 14) |
| 103 | return(MagickFalse); |
| 104 | if (LocaleNCompare((const char *) magick,"LBLSIZE",7) == 0) |
| 105 | return(MagickTrue); |
| 106 | if (LocaleNCompare((const char *) magick,"NJPL1I",6) == 0) |
| 107 | return(MagickTrue); |
| 108 | if (LocaleNCompare((const char *) magick,"PDS_VERSION_ID",14) == 0) |
| 109 | return(MagickTrue); |
| 110 | return(MagickFalse); |
| 111 | } |
| 112 | |
| 113 | /* |
| 114 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 115 | % % |
| 116 | % % |
| 117 | % % |
| 118 | % R e a d V I C A R I m a g e % |
| 119 | % % |
| 120 | % % |
| 121 | % % |
| 122 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 123 | % |
| 124 | % ReadVICARImage() reads a VICAR image file and returns it. It |
| 125 | % allocates the memory necessary for the new Image structure and returns a |
| 126 | % pointer to the new image. |
| 127 | % |
| 128 | % The format of the ReadVICARImage method is: |
| 129 | % |
| 130 | % Image *ReadVICARImage(const ImageInfo *image_info, |
| 131 | % ExceptionInfo *exception) |
| 132 | % |
| 133 | % A description of each parameter follows: |
| 134 | % |
| 135 | % o image: Method ReadVICARImage returns a pointer to the image after |
| 136 | % reading. A null image is returned if there is a memory shortage or if |
| 137 | % the image cannot be read. |
| 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 Image *ReadVICARImage(const ImageInfo *image_info, |
| 146 | ExceptionInfo *exception) |
| 147 | { |
| 148 | char |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 149 | keyword[MagickPathExtent], |
| 150 | value[MagickPathExtent]; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 151 | |
cristy | b3f97ae | 2015-05-18 12:29:32 +0000 | [diff] [blame] | 152 | const unsigned char |
cristy | bd797f1 | 2015-01-24 20:42:32 +0000 | [diff] [blame] | 153 | *pixels; |
| 154 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 155 | Image |
| 156 | *image; |
| 157 | |
| 158 | int |
| 159 | c; |
| 160 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 161 | MagickBooleanType |
| 162 | status, |
| 163 | value_expected; |
| 164 | |
| 165 | QuantumInfo |
| 166 | *quantum_info; |
| 167 | |
| 168 | QuantumType |
| 169 | quantum_type; |
| 170 | |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 171 | register Quantum |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 172 | *q; |
| 173 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 174 | size_t |
| 175 | length; |
| 176 | |
cristy | 03533f2 | 2011-03-06 23:30:17 +0000 | [diff] [blame] | 177 | ssize_t |
cristy | c6da28e | 2011-04-28 01:41:35 +0000 | [diff] [blame] | 178 | count, |
| 179 | y; |
cristy | 03533f2 | 2011-03-06 23:30:17 +0000 | [diff] [blame] | 180 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 181 | /* |
| 182 | Open image file. |
| 183 | */ |
| 184 | assert(image_info != (const ImageInfo *) NULL); |
cristy | e1c94d9 | 2015-06-28 12:16:33 +0000 | [diff] [blame] | 185 | assert(image_info->signature == MagickCoreSignature); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 186 | if (image_info->debug != MagickFalse) |
| 187 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s", |
| 188 | image_info->filename); |
| 189 | assert(exception != (ExceptionInfo *) NULL); |
cristy | e1c94d9 | 2015-06-28 12:16:33 +0000 | [diff] [blame] | 190 | assert(exception->signature == MagickCoreSignature); |
cristy | 9950d57 | 2011-10-01 18:22:35 +0000 | [diff] [blame] | 191 | image=AcquireImage(image_info,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 192 | status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception); |
| 193 | if (status == MagickFalse) |
| 194 | { |
| 195 | image=DestroyImageList(image); |
| 196 | return((Image *) NULL); |
| 197 | } |
| 198 | /* |
| 199 | Decode image header. |
| 200 | */ |
| 201 | c=ReadBlobByte(image); |
| 202 | count=1; |
| 203 | if (c == EOF) |
| 204 | { |
| 205 | image=DestroyImage(image); |
| 206 | return((Image *) NULL); |
| 207 | } |
| 208 | length=0; |
| 209 | image->columns=0; |
| 210 | image->rows=0; |
| 211 | while (isgraph(c) && ((image->columns == 0) || (image->rows == 0))) |
| 212 | { |
| 213 | if (isalnum(c) == MagickFalse) |
| 214 | { |
| 215 | c=ReadBlobByte(image); |
| 216 | count++; |
| 217 | } |
| 218 | else |
| 219 | { |
| 220 | register char |
| 221 | *p; |
| 222 | |
| 223 | /* |
| 224 | Determine a keyword and its value. |
| 225 | */ |
| 226 | p=keyword; |
| 227 | do |
| 228 | { |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 229 | if ((size_t) (p-keyword) < (MagickPathExtent-1)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 230 | *p++=c; |
| 231 | c=ReadBlobByte(image); |
| 232 | count++; |
| 233 | } while (isalnum(c) || (c == '_')); |
| 234 | *p='\0'; |
| 235 | value_expected=MagickFalse; |
| 236 | while ((isspace((int) ((unsigned char) c)) != 0) || (c == '=')) |
| 237 | { |
| 238 | if (c == '=') |
| 239 | value_expected=MagickTrue; |
| 240 | c=ReadBlobByte(image); |
| 241 | count++; |
| 242 | } |
| 243 | if (value_expected == MagickFalse) |
| 244 | continue; |
| 245 | p=value; |
| 246 | while (isalnum(c)) |
| 247 | { |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 248 | if ((size_t) (p-value) < (MagickPathExtent-1)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 249 | *p++=c; |
| 250 | c=ReadBlobByte(image); |
| 251 | count++; |
| 252 | } |
| 253 | *p='\0'; |
| 254 | /* |
| 255 | Assign a value to the specified keyword. |
| 256 | */ |
| 257 | if (LocaleCompare(keyword,"Label_RECORDS") == 0) |
cristy | f2f2727 | 2009-12-17 14:48:46 +0000 | [diff] [blame] | 258 | length=(ssize_t) StringToLong(value); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 259 | if (LocaleCompare(keyword,"LBLSIZE") == 0) |
cristy | f2f2727 | 2009-12-17 14:48:46 +0000 | [diff] [blame] | 260 | length=(ssize_t) StringToLong(value); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 261 | if (LocaleCompare(keyword,"RECORD_BYTES") == 0) |
cristy | 5131f3e | 2009-12-18 03:09:10 +0000 | [diff] [blame] | 262 | image->columns=StringToUnsignedLong(value); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 263 | if (LocaleCompare(keyword,"NS") == 0) |
cristy | 5131f3e | 2009-12-18 03:09:10 +0000 | [diff] [blame] | 264 | image->columns=StringToUnsignedLong(value); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 265 | if (LocaleCompare(keyword,"LINES") == 0) |
cristy | 5131f3e | 2009-12-18 03:09:10 +0000 | [diff] [blame] | 266 | image->rows=StringToUnsignedLong(value); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 267 | if (LocaleCompare(keyword,"NL") == 0) |
cristy | 5131f3e | 2009-12-18 03:09:10 +0000 | [diff] [blame] | 268 | image->rows=StringToUnsignedLong(value); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 269 | } |
| 270 | while (isspace((int) ((unsigned char) c)) != 0) |
| 271 | { |
| 272 | c=ReadBlobByte(image); |
| 273 | count++; |
| 274 | } |
| 275 | } |
| 276 | while (count < (ssize_t) length) |
| 277 | { |
| 278 | c=ReadBlobByte(image); |
cristy | 89f839d | 2015-01-25 17:32:32 +0000 | [diff] [blame] | 279 | if (c == EOF) |
| 280 | break; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 281 | count++; |
| 282 | } |
| 283 | if ((image->columns == 0) || (image->rows == 0)) |
| 284 | ThrowReaderException(CorruptImageError,"NegativeOrZeroImageSize"); |
| 285 | image->depth=8; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 286 | if (image_info->ping != MagickFalse) |
| 287 | { |
| 288 | (void) CloseBlob(image); |
| 289 | return(GetFirstImageInList(image)); |
| 290 | } |
cristy | acabb84 | 2014-12-14 23:36:33 +0000 | [diff] [blame] | 291 | status=SetImageExtent(image,image->columns,image->rows,exception); |
| 292 | if (status == MagickFalse) |
| 293 | return(DestroyImageList(image)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 294 | /* |
| 295 | Read VICAR pixels. |
| 296 | */ |
cristy | 4cfe027 | 2012-06-21 11:27:09 +0000 | [diff] [blame] | 297 | (void) SetImageColorspace(image,GRAYColorspace,exception); |
cristy | 65bda43 | 2012-06-21 11:22:10 +0000 | [diff] [blame] | 298 | quantum_type=GrayQuantum; |
cristy | 5f766ef | 2014-12-14 21:12:47 +0000 | [diff] [blame] | 299 | quantum_info=AcquireQuantumInfo(image_info,image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 300 | if (quantum_info == (QuantumInfo *) NULL) |
| 301 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
cristy | 65bda43 | 2012-06-21 11:22:10 +0000 | [diff] [blame] | 302 | length=GetQuantumExtent(image,quantum_info,quantum_type); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 303 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 304 | { |
| 305 | q=QueueAuthenticPixels(image,0,y,image->columns,1,exception); |
cristy | acd2ed2 | 2011-08-30 01:44:23 +0000 | [diff] [blame] | 306 | if (q == (Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 307 | break; |
cristy | b3f97ae | 2015-05-18 12:29:32 +0000 | [diff] [blame] | 308 | pixels=(const unsigned char *) ReadBlobStream(image,length, |
| 309 | GetQuantumPixels(quantum_info),&count); |
cristy | bd797f1 | 2015-01-24 20:42:32 +0000 | [diff] [blame] | 310 | if (count != (ssize_t) length) |
| 311 | break; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 312 | (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info, |
| 313 | quantum_type,pixels,exception); |
| 314 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
| 315 | break; |
cristy | cee9711 | 2010-05-28 00:44:52 +0000 | [diff] [blame] | 316 | status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y, |
cristy | 4bad316 | 2011-03-06 18:44:39 +0000 | [diff] [blame] | 317 | image->rows); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 318 | if (status == MagickFalse) |
| 319 | break; |
| 320 | } |
| 321 | SetQuantumImageType(image,quantum_type); |
| 322 | quantum_info=DestroyQuantumInfo(quantum_info); |
| 323 | if (EOFBlob(image) != MagickFalse) |
| 324 | ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile", |
| 325 | image->filename); |
| 326 | (void) CloseBlob(image); |
| 327 | return(GetFirstImageInList(image)); |
| 328 | } |
| 329 | |
| 330 | /* |
| 331 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 332 | % % |
| 333 | % % |
| 334 | % % |
| 335 | % R e g i s t e r V I C A R I m a g e % |
| 336 | % % |
| 337 | % % |
| 338 | % % |
| 339 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 340 | % |
| 341 | % RegisterVICARImage() adds attributes for the VICAR image format to |
| 342 | % the list of supported formats. The attributes include the image format |
| 343 | % tag, a method to read and/or write the format, whether the format |
| 344 | % supports the saving of more than one frame to the same file or blob, |
| 345 | % whether the format supports native in-memory I/O, and a brief |
| 346 | % description of the format. |
| 347 | % |
| 348 | % The format of the RegisterVICARImage method is: |
| 349 | % |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 350 | % size_t RegisterVICARImage(void) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 351 | % |
| 352 | */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 353 | ModuleExport size_t RegisterVICARImage(void) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 354 | { |
| 355 | MagickInfo |
| 356 | *entry; |
| 357 | |
dirk | 06b627a | 2015-04-06 18:59:17 +0000 | [diff] [blame] | 358 | entry=AcquireMagickInfo("VICAR","VICAR","VICAR rasterfile format"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 359 | entry->decoder=(DecodeImageHandler *) ReadVICARImage; |
| 360 | entry->encoder=(EncodeImageHandler *) WriteVICARImage; |
| 361 | entry->magick=(IsImageFormatHandler *) IsVICAR; |
dirk | 08e9a11 | 2015-02-22 01:51:41 +0000 | [diff] [blame] | 362 | entry->flags^=CoderAdjoinFlag; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 363 | (void) RegisterMagickInfo(entry); |
| 364 | return(MagickImageCoderSignature); |
| 365 | } |
| 366 | |
| 367 | /* |
| 368 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 369 | % % |
| 370 | % % |
| 371 | % % |
| 372 | % U n r e g i s t e r V I C A R I m a g e % |
| 373 | % % |
| 374 | % % |
| 375 | % % |
| 376 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 377 | % |
| 378 | % UnregisterVICARImage() removes format registrations made by the |
| 379 | % VICAR module from the list of supported formats. |
| 380 | % |
| 381 | % The format of the UnregisterVICARImage method is: |
| 382 | % |
| 383 | % UnregisterVICARImage(void) |
| 384 | % |
| 385 | */ |
| 386 | ModuleExport void UnregisterVICARImage(void) |
| 387 | { |
| 388 | (void) UnregisterMagickInfo("VICAR"); |
| 389 | } |
| 390 | |
| 391 | /* |
| 392 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 393 | % % |
| 394 | % % |
| 395 | % % |
| 396 | % W r i t e V I C A R I m a g e % |
| 397 | % % |
| 398 | % % |
| 399 | % % |
| 400 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 401 | % |
| 402 | % WriteVICARImage() writes an image in the VICAR rasterfile format. |
| 403 | % Vicar files contain a text header, followed by one or more planes of binary |
| 404 | % grayscale image data. Vicar files are designed to allow many planes to be |
| 405 | % stacked together to form image cubes. This method only writes a single |
| 406 | % grayscale plane. |
| 407 | % |
cristy | 03533f2 | 2011-03-06 23:30:17 +0000 | [diff] [blame] | 408 | % WriteVICARImage was written contributed by gorelick@esther.la.asu.edu. |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 409 | % |
| 410 | % The format of the WriteVICARImage method is: |
| 411 | % |
| 412 | % MagickBooleanType WriteVICARImage(const ImageInfo *image_info, |
cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 413 | % Image *image,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 414 | % |
| 415 | % A description of each parameter follows. |
| 416 | % |
| 417 | % o image_info: the image info. |
| 418 | % |
| 419 | % o image: The image. |
| 420 | % |
cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 421 | % o exception: return any errors or warnings in this structure. |
| 422 | % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 423 | */ |
| 424 | static MagickBooleanType WriteVICARImage(const ImageInfo *image_info, |
cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 425 | Image *image,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 426 | { |
| 427 | char |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 428 | header[MagickPathExtent]; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 429 | |
| 430 | int |
| 431 | y; |
| 432 | |
| 433 | MagickBooleanType |
| 434 | status; |
| 435 | |
| 436 | QuantumInfo |
| 437 | *quantum_info; |
| 438 | |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 439 | register const Quantum |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 440 | *p; |
| 441 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 442 | size_t |
| 443 | length; |
| 444 | |
cristy | c6da28e | 2011-04-28 01:41:35 +0000 | [diff] [blame] | 445 | ssize_t |
| 446 | count; |
| 447 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 448 | unsigned char |
| 449 | *pixels; |
| 450 | |
| 451 | /* |
| 452 | Open output image file. |
| 453 | */ |
| 454 | assert(image_info != (const ImageInfo *) NULL); |
cristy | e1c94d9 | 2015-06-28 12:16:33 +0000 | [diff] [blame] | 455 | assert(image_info->signature == MagickCoreSignature); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 456 | assert(image != (Image *) NULL); |
cristy | e1c94d9 | 2015-06-28 12:16:33 +0000 | [diff] [blame] | 457 | assert(image->signature == MagickCoreSignature); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 458 | if (image->debug != MagickFalse) |
| 459 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 460 | assert(exception != (ExceptionInfo *) NULL); |
cristy | e1c94d9 | 2015-06-28 12:16:33 +0000 | [diff] [blame] | 461 | assert(exception->signature == MagickCoreSignature); |
cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 462 | status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 463 | if (status == MagickFalse) |
| 464 | return(status); |
cristy | af8d391 | 2014-02-21 14:50:33 +0000 | [diff] [blame] | 465 | (void) TransformImageColorspace(image,sRGBColorspace,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 466 | /* |
| 467 | Write header. |
| 468 | */ |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 469 | (void) ResetMagickMemory(header,' ',MagickPathExtent); |
| 470 | (void) FormatLocaleString(header,MagickPathExtent, |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 471 | "LBLSIZE=%.20g FORMAT='BYTE' TYPE='IMAGE' BUFSIZE=20000 DIM=2 EOL=0 " |
| 472 | "RECSIZE=%.20g ORG='BSQ' NL=%.20g NS=%.20g NB=1 N1=0 N2=0 N3=0 N4=0 NBB=0 " |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 473 | "NLB=0 TASK='ImageMagick'",(double) MagickPathExtent,(double) image->columns, |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 474 | (double) image->rows,(double) image->columns); |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 475 | (void) WriteBlob(image,MagickPathExtent,(unsigned char *) header); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 476 | /* |
| 477 | Write VICAR pixels. |
| 478 | */ |
| 479 | image->depth=8; |
cristy | 5f766ef | 2014-12-14 21:12:47 +0000 | [diff] [blame] | 480 | quantum_info=AcquireQuantumInfo(image_info,image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 481 | if (quantum_info == (QuantumInfo *) NULL) |
| 482 | ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed"); |
cristy | b3f97ae | 2015-05-18 12:29:32 +0000 | [diff] [blame] | 483 | pixels=(unsigned char *) GetQuantumPixels(quantum_info); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 484 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 485 | { |
cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 486 | p=GetVirtualPixels(image,0,y,image->columns,1,exception); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 487 | if (p == (const Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 488 | break; |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 489 | length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info, |
cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 490 | GrayQuantum,pixels,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 491 | count=WriteBlob(image,length,pixels); |
| 492 | if (count != (ssize_t) length) |
| 493 | break; |
cristy | cee9711 | 2010-05-28 00:44:52 +0000 | [diff] [blame] | 494 | status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y, |
cristy | 84c3d05 | 2011-03-07 19:22:02 +0000 | [diff] [blame] | 495 | image->rows); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 496 | if (status == MagickFalse) |
| 497 | break; |
| 498 | } |
| 499 | quantum_info=DestroyQuantumInfo(quantum_info); |
| 500 | (void) CloseBlob(image); |
| 501 | return(MagickTrue); |
| 502 | } |