2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6 % V V IIIII CCCC AAA RRRR %
10 % V IIIII CCCC A A R R %
13 % Read/Write VICAR Rasterfile Format %
20 % Copyright 1999-2017 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
26 % http://www.imagemagick.org/script/license.php %
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. %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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/colormap.h"
48 #include "MagickCore/colorspace.h"
49 #include "MagickCore/colorspace-private.h"
50 #include "MagickCore/constitute.h"
51 #include "MagickCore/exception.h"
52 #include "MagickCore/exception-private.h"
53 #include "MagickCore/image.h"
54 #include "MagickCore/image-private.h"
55 #include "MagickCore/list.h"
56 #include "MagickCore/magick.h"
57 #include "MagickCore/memory_.h"
58 #include "MagickCore/module.h"
59 #include "MagickCore/monitor.h"
60 #include "MagickCore/monitor-private.h"
61 #include "MagickCore/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"
70 static MagickBooleanType
71 WriteVICARImage(const ImageInfo *,Image *,ExceptionInfo *);
74 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
82 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
84 % IsVICAR() returns MagickTrue if the image format type, identified by the
85 % magick string, is VICAR.
87 % The format of the IsVICAR method is:
89 % MagickBooleanType IsVICAR(const unsigned char *magick,
90 % const size_t length)
92 % A description of each parameter follows:
94 % o magick: compare image format pattern against these bytes.
96 % o length: Specifies the length of the magick string.
99 static MagickBooleanType IsVICAR(const unsigned char *magick,
104 if (LocaleNCompare((const char *) magick,"LBLSIZE",7) == 0)
106 if (LocaleNCompare((const char *) magick,"NJPL1I",6) == 0)
108 if (LocaleNCompare((const char *) magick,"PDS_VERSION_ID",14) == 0)
114 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
118 % R e a d V I C A R I m a g e %
122 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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.
128 % The format of the ReadVICARImage method is:
130 % Image *ReadVICARImage(const ImageInfo *image_info,
131 % ExceptionInfo *exception)
133 % A description of each parameter follows:
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.
139 % o image_info: the image info.
141 % o exception: return any errors or warnings in this structure.
145 static Image *ReadVICARImage(const ImageInfo *image_info,
146 ExceptionInfo *exception)
149 keyword[MagickPathExtent],
150 value[MagickPathExtent];
184 assert(image_info != (const ImageInfo *) NULL);
185 assert(image_info->signature == MagickCoreSignature);
186 if (image_info->debug != MagickFalse)
187 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
188 image_info->filename);
189 assert(exception != (ExceptionInfo *) NULL);
190 assert(exception->signature == MagickCoreSignature);
191 image=AcquireImage(image_info,exception);
192 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
193 if (status == MagickFalse)
195 image=DestroyImageList(image);
196 return((Image *) NULL);
201 c=ReadBlobByte(image);
205 image=DestroyImage(image);
206 return((Image *) NULL);
211 while (isgraph(c) && ((image->columns == 0) || (image->rows == 0)))
213 if (isalnum(c) == MagickFalse)
215 c=ReadBlobByte(image);
224 Determine a keyword and its value.
229 if ((size_t) (p-keyword) < (MagickPathExtent-1))
231 c=ReadBlobByte(image);
233 } while (isalnum(c) || (c == '_'));
235 value_expected=MagickFalse;
236 while ((isspace((int) ((unsigned char) c)) != 0) || (c == '='))
239 value_expected=MagickTrue;
240 c=ReadBlobByte(image);
243 if (value_expected == MagickFalse)
248 if ((size_t) (p-value) < (MagickPathExtent-1))
250 c=ReadBlobByte(image);
255 Assign a value to the specified keyword.
257 if (LocaleCompare(keyword,"Label_RECORDS") == 0)
258 length=(ssize_t) StringToLong(value);
259 if (LocaleCompare(keyword,"LBLSIZE") == 0)
260 length=(ssize_t) StringToLong(value);
261 if (LocaleCompare(keyword,"RECORD_BYTES") == 0)
262 image->columns=StringToUnsignedLong(value);
263 if (LocaleCompare(keyword,"NS") == 0)
264 image->columns=StringToUnsignedLong(value);
265 if (LocaleCompare(keyword,"LINES") == 0)
266 image->rows=StringToUnsignedLong(value);
267 if (LocaleCompare(keyword,"NL") == 0)
268 image->rows=StringToUnsignedLong(value);
270 while (isspace((int) ((unsigned char) c)) != 0)
272 c=ReadBlobByte(image);
276 while (count < (ssize_t) length)
278 c=ReadBlobByte(image);
283 if ((image->columns == 0) || (image->rows == 0))
284 ThrowReaderException(CorruptImageError,"NegativeOrZeroImageSize");
286 if (image_info->ping != MagickFalse)
288 (void) CloseBlob(image);
289 return(GetFirstImageInList(image));
291 status=SetImageExtent(image,image->columns,image->rows,exception);
292 if (status == MagickFalse)
293 return(DestroyImageList(image));
297 (void) SetImageColorspace(image,GRAYColorspace,exception);
298 quantum_type=GrayQuantum;
299 quantum_info=AcquireQuantumInfo(image_info,image);
300 if (quantum_info == (QuantumInfo *) NULL)
301 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
302 length=GetQuantumExtent(image,quantum_info,quantum_type);
303 for (y=0; y < (ssize_t) image->rows; y++)
305 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
306 if (q == (Quantum *) NULL)
308 pixels=(const unsigned char *) ReadBlobStream(image,length,
309 GetQuantumPixels(quantum_info),&count);
310 if (count != (ssize_t) length)
312 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
313 quantum_type,pixels,exception);
314 if (SyncAuthenticPixels(image,exception) == MagickFalse)
316 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
318 if (status == MagickFalse)
321 SetQuantumImageType(image,quantum_type);
322 quantum_info=DestroyQuantumInfo(quantum_info);
323 if (EOFBlob(image) != MagickFalse)
324 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
326 (void) CloseBlob(image);
327 return(GetFirstImageInList(image));
331 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
335 % R e g i s t e r V I C A R I m a g e %
339 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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.
348 % The format of the RegisterVICARImage method is:
350 % size_t RegisterVICARImage(void)
353 ModuleExport size_t RegisterVICARImage(void)
358 entry=AcquireMagickInfo("VICAR","VICAR","VICAR rasterfile format");
359 entry->decoder=(DecodeImageHandler *) ReadVICARImage;
360 entry->encoder=(EncodeImageHandler *) WriteVICARImage;
361 entry->magick=(IsImageFormatHandler *) IsVICAR;
362 entry->flags^=CoderAdjoinFlag;
363 (void) RegisterMagickInfo(entry);
364 return(MagickImageCoderSignature);
368 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
372 % U n r e g i s t e r V I C A R I m a g e %
376 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
378 % UnregisterVICARImage() removes format registrations made by the
379 % VICAR module from the list of supported formats.
381 % The format of the UnregisterVICARImage method is:
383 % UnregisterVICARImage(void)
386 ModuleExport void UnregisterVICARImage(void)
388 (void) UnregisterMagickInfo("VICAR");
392 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
396 % W r i t e V I C A R I m a g e %
400 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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
408 % WriteVICARImage was written contributed by gorelick@esther.la.asu.edu.
410 % The format of the WriteVICARImage method is:
412 % MagickBooleanType WriteVICARImage(const ImageInfo *image_info,
413 % Image *image,ExceptionInfo *exception)
415 % A description of each parameter follows.
417 % o image_info: the image info.
419 % o image: The image.
421 % o exception: return any errors or warnings in this structure.
424 static MagickBooleanType WriteVICARImage(const ImageInfo *image_info,
425 Image *image,ExceptionInfo *exception)
428 header[MagickPathExtent];
439 register const Quantum
452 Open output image file.
454 assert(image_info != (const ImageInfo *) NULL);
455 assert(image_info->signature == MagickCoreSignature);
456 assert(image != (Image *) NULL);
457 assert(image->signature == MagickCoreSignature);
458 if (image->debug != MagickFalse)
459 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
460 assert(exception != (ExceptionInfo *) NULL);
461 assert(exception->signature == MagickCoreSignature);
462 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
463 if (status == MagickFalse)
465 (void) TransformImageColorspace(image,sRGBColorspace,exception);
469 (void) ResetMagickMemory(header,' ',MagickPathExtent);
470 (void) FormatLocaleString(header,MagickPathExtent,
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 "
473 "NLB=0 TASK='ImageMagick'",(double) MagickPathExtent,(double) image->columns,
474 (double) image->rows,(double) image->columns);
475 (void) WriteBlob(image,MagickPathExtent,(unsigned char *) header);
480 quantum_info=AcquireQuantumInfo(image_info,image);
481 if (quantum_info == (QuantumInfo *) NULL)
482 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
483 pixels=(unsigned char *) GetQuantumPixels(quantum_info);
484 for (y=0; y < (ssize_t) image->rows; y++)
486 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
487 if (p == (const Quantum *) NULL)
489 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
490 GrayQuantum,pixels,exception);
491 count=WriteBlob(image,length,pixels);
492 if (count != (ssize_t) length)
494 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
496 if (status == MagickFalse)
499 quantum_info=DestroyQuantumInfo(quantum_info);
500 (void) CloseBlob(image);