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-2011 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/blob.h"
44 #include "MagickCore/blob-private.h"
45 #include "MagickCore/cache.h"
46 #include "MagickCore/colormap.h"
47 #include "MagickCore/colorspace.h"
48 #include "MagickCore/constitute.h"
49 #include "MagickCore/exception.h"
50 #include "MagickCore/exception-private.h"
51 #include "MagickCore/image.h"
52 #include "MagickCore/image-private.h"
53 #include "MagickCore/list.h"
54 #include "MagickCore/magick.h"
55 #include "MagickCore/memory_.h"
56 #include "MagickCore/module.h"
57 #include "MagickCore/monitor.h"
58 #include "MagickCore/monitor-private.h"
59 #include "MagickCore/quantum-private.h"
60 #include "MagickCore/quantum-private.h"
61 #include "MagickCore/static.h"
62 #include "MagickCore/string_.h"
63 #include "MagickCore/string-private.h"
68 static MagickBooleanType
69 WriteVICARImage(const ImageInfo *,Image *);
72 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
80 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
82 % IsVICAR() returns MagickTrue if the image format type, identified by the
83 % magick string, is VICAR.
85 % The format of the IsVICAR method is:
87 % MagickBooleanType IsVICAR(const unsigned char *magick,
88 % const size_t length)
90 % A description of each parameter follows:
92 % o magick: compare image format pattern against these bytes.
94 % o length: Specifies the length of the magick string.
97 static MagickBooleanType IsVICAR(const unsigned char *magick,
102 if (LocaleNCompare((const char *) magick,"LBLSIZE",7) == 0)
104 if (LocaleNCompare((const char *) magick,"NJPL1I",6) == 0)
106 if (LocaleNCompare((const char *) magick,"PDS_VERSION_ID",14) == 0)
112 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
116 % R e a d V I C A R I m a g e %
120 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
122 % ReadVICARImage() reads a VICAR image file and returns it. It
123 % allocates the memory necessary for the new Image structure and returns a
124 % pointer to the new image.
126 % The format of the ReadVICARImage method is:
128 % Image *ReadVICARImage(const ImageInfo *image_info,
129 % ExceptionInfo *exception)
131 % A description of each parameter follows:
133 % o image: Method ReadVICARImage returns a pointer to the image after
134 % reading. A null image is returned if there is a memory shortage or if
135 % the image cannot be read.
137 % o image_info: the image info.
139 % o exception: return any errors or warnings in this structure.
143 static Image *ReadVICARImage(const ImageInfo *image_info,
144 ExceptionInfo *exception)
147 keyword[MaxTextExtent],
148 value[MaxTextExtent];
182 assert(image_info != (const ImageInfo *) NULL);
183 assert(image_info->signature == MagickSignature);
184 if (image_info->debug != MagickFalse)
185 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
186 image_info->filename);
187 assert(exception != (ExceptionInfo *) NULL);
188 assert(exception->signature == MagickSignature);
189 image=AcquireImage(image_info);
190 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
191 if (status == MagickFalse)
193 image=DestroyImageList(image);
194 return((Image *) NULL);
199 c=ReadBlobByte(image);
203 image=DestroyImage(image);
204 return((Image *) NULL);
209 while (isgraph(c) && ((image->columns == 0) || (image->rows == 0)))
211 if (isalnum(c) == MagickFalse)
213 c=ReadBlobByte(image);
222 Determine a keyword and its value.
227 if ((size_t) (p-keyword) < (MaxTextExtent-1))
229 c=ReadBlobByte(image);
231 } while (isalnum(c) || (c == '_'));
233 value_expected=MagickFalse;
234 while ((isspace((int) ((unsigned char) c)) != 0) || (c == '='))
237 value_expected=MagickTrue;
238 c=ReadBlobByte(image);
241 if (value_expected == MagickFalse)
246 if ((size_t) (p-value) < (MaxTextExtent-1))
248 c=ReadBlobByte(image);
253 Assign a value to the specified keyword.
255 if (LocaleCompare(keyword,"Label_RECORDS") == 0)
256 length=(ssize_t) StringToLong(value);
257 if (LocaleCompare(keyword,"LBLSIZE") == 0)
258 length=(ssize_t) StringToLong(value);
259 if (LocaleCompare(keyword,"RECORD_BYTES") == 0)
260 image->columns=StringToUnsignedLong(value);
261 if (LocaleCompare(keyword,"NS") == 0)
262 image->columns=StringToUnsignedLong(value);
263 if (LocaleCompare(keyword,"LINES") == 0)
264 image->rows=StringToUnsignedLong(value);
265 if (LocaleCompare(keyword,"NL") == 0)
266 image->rows=StringToUnsignedLong(value);
268 while (isspace((int) ((unsigned char) c)) != 0)
270 c=ReadBlobByte(image);
274 while (count < (ssize_t) length)
276 c=ReadBlobByte(image);
279 if ((image->columns == 0) || (image->rows == 0))
280 ThrowReaderException(CorruptImageError,"NegativeOrZeroImageSize");
282 if (AcquireImageColormap(image,256) == MagickFalse)
283 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
284 if (image_info->ping != MagickFalse)
286 (void) CloseBlob(image);
287 return(GetFirstImageInList(image));
292 quantum_type=IndexQuantum;
293 quantum_info=AcquireQuantumInfo(image_info,image);
294 if (quantum_info == (QuantumInfo *) NULL)
295 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
296 pixels=GetQuantumPixels(quantum_info);
297 length=GetQuantumExtent(image,quantum_info,IndexQuantum);
298 for (y=0; y < (ssize_t) image->rows; y++)
300 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
301 if (q == (const Quantum *) NULL)
303 count=ReadBlob(image,length,pixels);
304 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
305 quantum_type,pixels,exception);
306 if (SyncAuthenticPixels(image,exception) == MagickFalse)
308 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
310 if (status == MagickFalse)
313 SetQuantumImageType(image,quantum_type);
314 quantum_info=DestroyQuantumInfo(quantum_info);
315 if (EOFBlob(image) != MagickFalse)
316 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
318 (void) CloseBlob(image);
319 return(GetFirstImageInList(image));
323 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
327 % R e g i s t e r V I C A R I m a g e %
331 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
333 % RegisterVICARImage() adds attributes for the VICAR image format to
334 % the list of supported formats. The attributes include the image format
335 % tag, a method to read and/or write the format, whether the format
336 % supports the saving of more than one frame to the same file or blob,
337 % whether the format supports native in-memory I/O, and a brief
338 % description of the format.
340 % The format of the RegisterVICARImage method is:
342 % size_t RegisterVICARImage(void)
345 ModuleExport size_t RegisterVICARImage(void)
350 entry=SetMagickInfo("VICAR");
351 entry->decoder=(DecodeImageHandler *) ReadVICARImage;
352 entry->encoder=(EncodeImageHandler *) WriteVICARImage;
353 entry->magick=(IsImageFormatHandler *) IsVICAR;
354 entry->adjoin=MagickFalse;
355 entry->description=ConstantString("VICAR rasterfile format");
356 entry->module=ConstantString("VICAR");
357 (void) RegisterMagickInfo(entry);
358 return(MagickImageCoderSignature);
362 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
366 % U n r e g i s t e r V I C A R I m a g e %
370 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
372 % UnregisterVICARImage() removes format registrations made by the
373 % VICAR module from the list of supported formats.
375 % The format of the UnregisterVICARImage method is:
377 % UnregisterVICARImage(void)
380 ModuleExport void UnregisterVICARImage(void)
382 (void) UnregisterMagickInfo("VICAR");
386 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
390 % W r i t e V I C A R I m a g e %
394 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
396 % WriteVICARImage() writes an image in the VICAR rasterfile format.
397 % Vicar files contain a text header, followed by one or more planes of binary
398 % grayscale image data. Vicar files are designed to allow many planes to be
399 % stacked together to form image cubes. This method only writes a single
402 % WriteVICARImage was written contributed by gorelick@esther.la.asu.edu.
404 % The format of the WriteVICARImage method is:
406 % MagickBooleanType WriteVICARImage(const ImageInfo *image_info,
409 % A description of each parameter follows.
411 % o image_info: the image info.
413 % o image: The image.
416 static MagickBooleanType WriteVICARImage(const ImageInfo *image_info,
420 header[MaxTextExtent];
431 register const Quantum
444 Open output image file.
446 assert(image_info != (const ImageInfo *) NULL);
447 assert(image_info->signature == MagickSignature);
448 assert(image != (Image *) NULL);
449 assert(image->signature == MagickSignature);
450 if (image->debug != MagickFalse)
451 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
452 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
453 if (status == MagickFalse)
455 if (image->colorspace != RGBColorspace)
456 (void) TransformImageColorspace(image,RGBColorspace);
460 (void) ResetMagickMemory(header,' ',MaxTextExtent);
461 (void) FormatLocaleString(header,MaxTextExtent,
462 "LBLSIZE=%.20g FORMAT='BYTE' TYPE='IMAGE' BUFSIZE=20000 DIM=2 EOL=0 "
463 "RECSIZE=%.20g ORG='BSQ' NL=%.20g NS=%.20g NB=1 N1=0 N2=0 N3=0 N4=0 NBB=0 "
464 "NLB=0 TASK='ImageMagick'",(double) MaxTextExtent,(double) image->columns,
465 (double) image->rows,(double) image->columns);
466 (void) WriteBlob(image,MaxTextExtent,(unsigned char *) header);
471 quantum_info=AcquireQuantumInfo(image_info,image);
472 if (quantum_info == (QuantumInfo *) NULL)
473 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
474 pixels=GetQuantumPixels(quantum_info);
475 for (y=0; y < (ssize_t) image->rows; y++)
477 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
478 if (p == (const Quantum *) NULL)
480 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
481 GrayQuantum,pixels,&image->exception);
482 count=WriteBlob(image,length,pixels);
483 if (count != (ssize_t) length)
485 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
487 if (status == MagickFalse)
490 quantum_info=DestroyQuantumInfo(quantum_info);
491 (void) CloseBlob(image);