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 "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 < (ssize_t) 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,(MagickOffsetType) y,
312 if (status == MagickFalse)
315 SetQuantumImageType(image,quantum_type);
316 quantum_info=DestroyQuantumInfo(quantum_info);
317 if (EOFBlob(image) != MagickFalse)
318 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
320 (void) CloseBlob(image);
321 return(GetFirstImageInList(image));
325 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
329 % R e g i s t e r V I C A R I m a g e %
333 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
335 % RegisterVICARImage() adds attributes for the VICAR image format to
336 % the list of supported formats. The attributes include the image format
337 % tag, a method to read and/or write the format, whether the format
338 % supports the saving of more than one frame to the same file or blob,
339 % whether the format supports native in-memory I/O, and a brief
340 % description of the format.
342 % The format of the RegisterVICARImage method is:
344 % size_t RegisterVICARImage(void)
347 ModuleExport size_t RegisterVICARImage(void)
352 entry=SetMagickInfo("VICAR");
353 entry->decoder=(DecodeImageHandler *) ReadVICARImage;
354 entry->encoder=(EncodeImageHandler *) WriteVICARImage;
355 entry->magick=(IsImageFormatHandler *) IsVICAR;
356 entry->adjoin=MagickFalse;
357 entry->description=ConstantString("VICAR rasterfile format");
358 entry->module=ConstantString("VICAR");
359 (void) RegisterMagickInfo(entry);
360 return(MagickImageCoderSignature);
364 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
368 % U n r e g i s t e r V I C A R I m a g e %
372 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
374 % UnregisterVICARImage() removes format registrations made by the
375 % VICAR module from the list of supported formats.
377 % The format of the UnregisterVICARImage method is:
379 % UnregisterVICARImage(void)
382 ModuleExport void UnregisterVICARImage(void)
384 (void) UnregisterMagickInfo("VICAR");
388 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
392 % W r i t e V I C A R I m a g e %
396 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
398 % WriteVICARImage() writes an image in the VICAR rasterfile format.
399 % Vicar files contain a text header, followed by one or more planes of binary
400 % grayscale image data. Vicar files are designed to allow many planes to be
401 % stacked together to form image cubes. This method only writes a single
404 % WriteVICARImage was written contributed by
405 % gorelick@esther.la.asu.edu.
407 % The format of the WriteVICARImage method is:
409 % MagickBooleanType WriteVICARImage(const ImageInfo *image_info,
412 % A description of each parameter follows.
414 % o image_info: the image info.
416 % o image: The image.
419 static MagickBooleanType WriteVICARImage(const ImageInfo *image_info,
423 header[MaxTextExtent];
434 register const PixelPacket
447 Open output image file.
449 assert(image_info != (const ImageInfo *) NULL);
450 assert(image_info->signature == MagickSignature);
451 assert(image != (Image *) NULL);
452 assert(image->signature == MagickSignature);
453 if (image->debug != MagickFalse)
454 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
455 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
456 if (status == MagickFalse)
458 if (image->colorspace != RGBColorspace)
459 (void) TransformImageColorspace(image,RGBColorspace);
463 (void) ResetMagickMemory(header,' ',MaxTextExtent);
464 (void) FormatMagickString(header,MaxTextExtent,
465 "LBLSIZE=%.20g FORMAT='BYTE' TYPE='IMAGE' BUFSIZE=20000 DIM=2 EOL=0 "
466 "RECSIZE=%.20g ORG='BSQ' NL=%.20g NS=%.20g NB=1 N1=0 N2=0 N3=0 N4=0 NBB=0 "
467 "NLB=0 TASK='ImageMagick'",(double) MaxTextExtent,(double) image->columns,
468 (double) image->rows,(double) image->columns);
469 (void) WriteBlob(image,MaxTextExtent,(unsigned char *) header);
474 quantum_info=AcquireQuantumInfo(image_info,image);
475 if (quantum_info == (QuantumInfo *) NULL)
476 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
477 pixels=GetQuantumPixels(quantum_info);
478 for (y=0; y < (ssize_t) image->rows; y++)
480 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
481 if (p == (const PixelPacket *) NULL)
483 length=ExportQuantumPixels(image,(const CacheView *) NULL,quantum_info,
484 GrayQuantum,pixels,&image->exception);
485 count=WriteBlob(image,length,pixels);
486 if (count != (ssize_t) length)
488 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
490 if (status == MagickFalse)
493 quantum_info=DestroyQuantumInfo(quantum_info);
494 (void) CloseBlob(image);