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-2012 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/colorspace-private.h"
49 #include "MagickCore/constitute.h"
50 #include "MagickCore/exception.h"
51 #include "MagickCore/exception-private.h"
52 #include "MagickCore/image.h"
53 #include "MagickCore/image-private.h"
54 #include "MagickCore/list.h"
55 #include "MagickCore/magick.h"
56 #include "MagickCore/memory_.h"
57 #include "MagickCore/module.h"
58 #include "MagickCore/monitor.h"
59 #include "MagickCore/monitor-private.h"
60 #include "MagickCore/quantum-private.h"
61 #include "MagickCore/quantum-private.h"
62 #include "MagickCore/static.h"
63 #include "MagickCore/string_.h"
64 #include "MagickCore/string-private.h"
69 static MagickBooleanType
70 WriteVICARImage(const ImageInfo *,Image *,ExceptionInfo *);
73 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
81 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
83 % IsVICAR() returns MagickTrue if the image format type, identified by the
84 % magick string, is VICAR.
86 % The format of the IsVICAR method is:
88 % MagickBooleanType IsVICAR(const unsigned char *magick,
89 % const size_t length)
91 % A description of each parameter follows:
93 % o magick: compare image format pattern against these bytes.
95 % o length: Specifies the length of the magick string.
98 static MagickBooleanType IsVICAR(const unsigned char *magick,
103 if (LocaleNCompare((const char *) magick,"LBLSIZE",7) == 0)
105 if (LocaleNCompare((const char *) magick,"NJPL1I",6) == 0)
107 if (LocaleNCompare((const char *) magick,"PDS_VERSION_ID",14) == 0)
113 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
117 % R e a d V I C A R I m a g e %
121 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
123 % ReadVICARImage() reads a VICAR image file and returns it. It
124 % allocates the memory necessary for the new Image structure and returns a
125 % pointer to the new image.
127 % The format of the ReadVICARImage method is:
129 % Image *ReadVICARImage(const ImageInfo *image_info,
130 % ExceptionInfo *exception)
132 % A description of each parameter follows:
134 % o image: Method ReadVICARImage returns a pointer to the image after
135 % reading. A null image is returned if there is a memory shortage or if
136 % the image cannot be read.
138 % o image_info: the image info.
140 % o exception: return any errors or warnings in this structure.
144 static Image *ReadVICARImage(const ImageInfo *image_info,
145 ExceptionInfo *exception)
148 keyword[MaxTextExtent],
149 value[MaxTextExtent];
183 assert(image_info != (const ImageInfo *) NULL);
184 assert(image_info->signature == MagickSignature);
185 if (image_info->debug != MagickFalse)
186 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
187 image_info->filename);
188 assert(exception != (ExceptionInfo *) NULL);
189 assert(exception->signature == MagickSignature);
190 image=AcquireImage(image_info,exception);
191 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
192 if (status == MagickFalse)
194 image=DestroyImageList(image);
195 return((Image *) NULL);
200 c=ReadBlobByte(image);
204 image=DestroyImage(image);
205 return((Image *) NULL);
210 while (isgraph(c) && ((image->columns == 0) || (image->rows == 0)))
212 if (isalnum(c) == MagickFalse)
214 c=ReadBlobByte(image);
223 Determine a keyword and its value.
228 if ((size_t) (p-keyword) < (MaxTextExtent-1))
230 c=ReadBlobByte(image);
232 } while (isalnum(c) || (c == '_'));
234 value_expected=MagickFalse;
235 while ((isspace((int) ((unsigned char) c)) != 0) || (c == '='))
238 value_expected=MagickTrue;
239 c=ReadBlobByte(image);
242 if (value_expected == MagickFalse)
247 if ((size_t) (p-value) < (MaxTextExtent-1))
249 c=ReadBlobByte(image);
254 Assign a value to the specified keyword.
256 if (LocaleCompare(keyword,"Label_RECORDS") == 0)
257 length=(ssize_t) StringToLong(value);
258 if (LocaleCompare(keyword,"LBLSIZE") == 0)
259 length=(ssize_t) StringToLong(value);
260 if (LocaleCompare(keyword,"RECORD_BYTES") == 0)
261 image->columns=StringToUnsignedLong(value);
262 if (LocaleCompare(keyword,"NS") == 0)
263 image->columns=StringToUnsignedLong(value);
264 if (LocaleCompare(keyword,"LINES") == 0)
265 image->rows=StringToUnsignedLong(value);
266 if (LocaleCompare(keyword,"NL") == 0)
267 image->rows=StringToUnsignedLong(value);
269 while (isspace((int) ((unsigned char) c)) != 0)
271 c=ReadBlobByte(image);
275 while (count < (ssize_t) length)
277 c=ReadBlobByte(image);
280 if ((image->columns == 0) || (image->rows == 0))
281 ThrowReaderException(CorruptImageError,"NegativeOrZeroImageSize");
283 if (AcquireImageColormap(image,256,exception) == MagickFalse)
284 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
285 if (image_info->ping != MagickFalse)
287 (void) CloseBlob(image);
288 return(GetFirstImageInList(image));
293 quantum_type=IndexQuantum;
294 quantum_info=AcquireQuantumInfo(image_info,image);
295 if (quantum_info == (QuantumInfo *) NULL)
296 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
297 pixels=GetQuantumPixels(quantum_info);
298 length=GetQuantumExtent(image,quantum_info,IndexQuantum);
299 for (y=0; y < (ssize_t) image->rows; y++)
301 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
302 if (q == (Quantum *) NULL)
304 count=ReadBlob(image,length,pixels);
305 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
306 quantum_type,pixels,exception);
307 if (SyncAuthenticPixels(image,exception) == MagickFalse)
309 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
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 % size_t RegisterVICARImage(void)
346 ModuleExport size_t 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 gorelick@esther.la.asu.edu.
405 % The format of the WriteVICARImage method is:
407 % MagickBooleanType WriteVICARImage(const ImageInfo *image_info,
408 % Image *image,ExceptionInfo *exception)
410 % A description of each parameter follows.
412 % o image_info: the image info.
414 % o image: The image.
416 % o exception: return any errors or warnings in this structure.
419 static MagickBooleanType WriteVICARImage(const ImageInfo *image_info,
420 Image *image,ExceptionInfo *exception)
423 header[MaxTextExtent];
434 register const Quantum
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 assert(exception != (ExceptionInfo *) NULL);
456 assert(exception->signature == MagickSignature);
457 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
458 if (status == MagickFalse)
460 if (IsRGBColorspace(image->colorspace) == MagickFalse)
461 (void) TransformImageColorspace(image,sRGBColorspace,exception);
465 (void) ResetMagickMemory(header,' ',MaxTextExtent);
466 (void) FormatLocaleString(header,MaxTextExtent,
467 "LBLSIZE=%.20g FORMAT='BYTE' TYPE='IMAGE' BUFSIZE=20000 DIM=2 EOL=0 "
468 "RECSIZE=%.20g ORG='BSQ' NL=%.20g NS=%.20g NB=1 N1=0 N2=0 N3=0 N4=0 NBB=0 "
469 "NLB=0 TASK='ImageMagick'",(double) MaxTextExtent,(double) image->columns,
470 (double) image->rows,(double) image->columns);
471 (void) WriteBlob(image,MaxTextExtent,(unsigned char *) header);
476 quantum_info=AcquireQuantumInfo(image_info,image);
477 if (quantum_info == (QuantumInfo *) NULL)
478 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
479 pixels=GetQuantumPixels(quantum_info);
480 for (y=0; y < (ssize_t) image->rows; y++)
482 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
483 if (p == (const Quantum *) NULL)
485 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
486 GrayQuantum,pixels,exception);
487 count=WriteBlob(image,length,pixels);
488 if (count != (ssize_t) length)
490 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
492 if (status == MagickFalse)
495 quantum_info=DestroyQuantumInfo(quantum_info);
496 (void) CloseBlob(image);