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-2010 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 "magick/studio.h"
43 #include "magick/blob.h"
44 #include "magick/blob-private.h"
45 #include "magick/cache.h"
46 #include "magick/colormap.h"
47 #include "magick/colorspace.h"
48 #include "magick/constitute.h"
49 #include "magick/exception.h"
50 #include "magick/exception-private.h"
51 #include "magick/image.h"
52 #include "magick/image-private.h"
53 #include "magick/list.h"
54 #include "magick/magick.h"
55 #include "magick/memory_.h"
56 #include "magick/module.h"
57 #include "magick/monitor.h"
58 #include "magick/monitor-private.h"
59 #include "magick/quantum-private.h"
60 #include "magick/quantum-private.h"
61 #include "magick/static.h"
62 #include "magick/string_.h"
63 #include "magick/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];
184 assert(image_info != (const ImageInfo *) NULL);
185 assert(image_info->signature == MagickSignature);
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 == MagickSignature);
191 image=AcquireImage(image_info);
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) < (MaxTextExtent-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) < (MaxTextExtent-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);
281 if ((image->columns == 0) || (image->rows == 0))
282 ThrowReaderException(CorruptImageError,"NegativeOrZeroImageSize");
284 if (AcquireImageColormap(image,256) == MagickFalse)
285 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
286 if (image_info->ping != MagickFalse)
288 (void) CloseBlob(image);
289 return(GetFirstImageInList(image));
294 quantum_type=IndexQuantum;
295 quantum_info=AcquireQuantumInfo(image_info,image);
296 if (quantum_info == (QuantumInfo *) NULL)
297 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
298 pixels=GetQuantumPixels(quantum_info);
299 length=GetQuantumExtent(image,quantum_info,IndexQuantum);
300 for (y=0; y < (long) image->rows; y++)
302 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
303 if (q == (PixelPacket *) NULL)
305 count=ReadBlob(image,length,pixels);
306 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
307 quantum_type,pixels,exception);
308 if (SyncAuthenticPixels(image,exception) == MagickFalse)
310 status=SetImageProgress(image,LoadImageTag,y,image->rows);
311 if (status == MagickFalse)
314 SetQuantumImageType(image,quantum_type);
315 quantum_info=DestroyQuantumInfo(quantum_info);
316 if (EOFBlob(image) != MagickFalse)
317 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
319 (void) CloseBlob(image);
320 return(GetFirstImageInList(image));
324 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
328 % R e g i s t e r V I C A R I m a g e %
332 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
334 % RegisterVICARImage() adds attributes for the VICAR image format to
335 % the list of supported formats. The attributes include the image format
336 % tag, a method to read and/or write the format, whether the format
337 % supports the saving of more than one frame to the same file or blob,
338 % whether the format supports native in-memory I/O, and a brief
339 % description of the format.
341 % The format of the RegisterVICARImage method is:
343 % unsigned long RegisterVICARImage(void)
346 ModuleExport unsigned long RegisterVICARImage(void)
351 entry=SetMagickInfo("VICAR");
352 entry->decoder=(DecodeImageHandler *) ReadVICARImage;
353 entry->encoder=(EncodeImageHandler *) WriteVICARImage;
354 entry->magick=(IsImageFormatHandler *) IsVICAR;
355 entry->adjoin=MagickFalse;
356 entry->description=ConstantString("VICAR rasterfile format");
357 entry->module=ConstantString("VICAR");
358 (void) RegisterMagickInfo(entry);
359 return(MagickImageCoderSignature);
363 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
367 % U n r e g i s t e r V I C A R I m a g e %
371 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
373 % UnregisterVICARImage() removes format registrations made by the
374 % VICAR module from the list of supported formats.
376 % The format of the UnregisterVICARImage method is:
378 % UnregisterVICARImage(void)
381 ModuleExport void UnregisterVICARImage(void)
383 (void) UnregisterMagickInfo("VICAR");
387 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
391 % W r i t e V I C A R I m a g e %
395 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
397 % WriteVICARImage() writes an image in the VICAR rasterfile format.
398 % Vicar files contain a text header, followed by one or more planes of binary
399 % grayscale image data. Vicar files are designed to allow many planes to be
400 % stacked together to form image cubes. This method only writes a single
403 % WriteVICARImage was written contributed by
404 % gorelick@esther.la.asu.edu.
406 % The format of the WriteVICARImage method is:
408 % MagickBooleanType WriteVICARImage(const ImageInfo *image_info,
411 % A description of each parameter follows.
413 % o image_info: the image info.
415 % o image: The image.
418 static MagickBooleanType WriteVICARImage(const ImageInfo *image_info,
422 header[MaxTextExtent];
433 register const PixelPacket
446 Open output image file.
448 assert(image_info != (const ImageInfo *) NULL);
449 assert(image_info->signature == MagickSignature);
450 assert(image != (Image *) NULL);
451 assert(image->signature == MagickSignature);
452 if (image->debug != MagickFalse)
453 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
454 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
455 if (status == MagickFalse)
457 if (image->colorspace != RGBColorspace)
458 (void) TransformImageColorspace(image,RGBColorspace);
462 (void) ResetMagickMemory(header,' ',MaxTextExtent);
463 (void) FormatMagickString(header,MaxTextExtent,
464 "LBLSIZE=%lu FORMAT='BYTE' TYPE='IMAGE' BUFSIZE=20000 DIM=2 EOL=0 "
465 "RECSIZE=%lu ORG='BSQ' NL=%lu NS=%lu NB=1 N1=0 N2=0 N3=0 N4=0 NBB=0 "
466 "NLB=0 TASK='ImageMagick'",(unsigned long) MaxTextExtent,image->columns,
467 image->rows,image->columns);
468 (void) WriteBlob(image,MaxTextExtent,(unsigned char *) header);
473 quantum_info=AcquireQuantumInfo(image_info,image);
474 if (quantum_info == (QuantumInfo *) NULL)
475 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
476 pixels=GetQuantumPixels(quantum_info);
477 for (y=0; y < (long) image->rows; y++)
479 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
480 if (p == (const PixelPacket *) NULL)
482 length=ExportQuantumPixels(image,(const CacheView *) NULL,quantum_info,
483 GrayQuantum,pixels,&image->exception);
484 count=WriteBlob(image,length,pixels);
485 if (count != (ssize_t) length)
487 status=SetImageProgress(image,SaveImageTag,y,image->rows);
488 if (status == MagickFalse)
491 quantum_info=DestroyQuantumInfo(quantum_info);
492 (void) CloseBlob(image);