blob: ddfcb73948a7ddfbbec588386e4785f1005fce7c [file] [log] [blame]
cristy3ed852e2009-09-05 21:47:34 +00001/*
2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3% %
4% %
5% %
6% V V IIIII CCCC AAA RRRR %
7% V V I C A A R R %
8% V V I C AAAAA RRRR %
9% V V I C A A R R %
10% V IIIII CCCC A A R R %
11% %
12% %
13% Read/Write VICAR Rasterfile Format %
14% %
15% Software Design %
16% John Cristy %
17% July 1992 %
18% %
19% %
cristy7e41fe82010-12-04 23:12:08 +000020% Copyright 1999-2011 ImageMagick Studio LLC, a non-profit organization %
cristy3ed852e2009-09-05 21:47:34 +000021% dedicated to making software imaging solutions freely available. %
22% %
23% You may not use this file except in compliance with the License. You may %
24% obtain a copy of the License at %
25% %
26% http://www.imagemagick.org/script/license.php %
27% %
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. %
33% %
34%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35%
36%
37*/
38
39/*
40 Include declarations.
41*/
cristy4c08aed2011-07-01 19:47:50 +000042#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"
cristy510d06a2011-07-06 23:43:54 +000048#include "MagickCore/colorspace-private.h"
cristy4c08aed2011-07-01 19:47:50 +000049#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"
cristy3ed852e2009-09-05 21:47:34 +000065
66/*
67 Forward declarations.
68*/
69static MagickBooleanType
cristy3a37efd2011-08-28 20:31:03 +000070 WriteVICARImage(const ImageInfo *,Image *,ExceptionInfo *);
cristy3ed852e2009-09-05 21:47:34 +000071
72/*
73%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
74% %
75% %
76% %
77% I s V I C A R %
78% %
79% %
80% %
81%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
82%
83% IsVICAR() returns MagickTrue if the image format type, identified by the
84% magick string, is VICAR.
85%
86% The format of the IsVICAR method is:
87%
88% MagickBooleanType IsVICAR(const unsigned char *magick,
89% const size_t length)
90%
91% A description of each parameter follows:
92%
93% o magick: compare image format pattern against these bytes.
94%
95% o length: Specifies the length of the magick string.
96%
97*/
98static MagickBooleanType IsVICAR(const unsigned char *magick,
99 const size_t length)
100{
101 if (length < 14)
102 return(MagickFalse);
103 if (LocaleNCompare((const char *) magick,"LBLSIZE",7) == 0)
104 return(MagickTrue);
105 if (LocaleNCompare((const char *) magick,"NJPL1I",6) == 0)
106 return(MagickTrue);
107 if (LocaleNCompare((const char *) magick,"PDS_VERSION_ID",14) == 0)
108 return(MagickTrue);
109 return(MagickFalse);
110}
111
112/*
113%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
114% %
115% %
116% %
117% R e a d V I C A R I m a g e %
118% %
119% %
120% %
121%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
122%
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.
126%
127% The format of the ReadVICARImage method is:
128%
129% Image *ReadVICARImage(const ImageInfo *image_info,
130% ExceptionInfo *exception)
131%
132% A description of each parameter follows:
133%
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.
137%
138% o image_info: the image info.
139%
140% o exception: return any errors or warnings in this structure.
141%
142%
143*/
144static Image *ReadVICARImage(const ImageInfo *image_info,
145 ExceptionInfo *exception)
146{
147 char
148 keyword[MaxTextExtent],
149 value[MaxTextExtent];
150
151 Image
152 *image;
153
154 int
155 c;
156
cristy3ed852e2009-09-05 21:47:34 +0000157 MagickBooleanType
158 status,
159 value_expected;
160
161 QuantumInfo
162 *quantum_info;
163
164 QuantumType
165 quantum_type;
166
cristy4c08aed2011-07-01 19:47:50 +0000167 register Quantum
cristy3ed852e2009-09-05 21:47:34 +0000168 *q;
169
cristy3ed852e2009-09-05 21:47:34 +0000170 size_t
171 length;
172
cristy03533f22011-03-06 23:30:17 +0000173 ssize_t
cristyc6da28e2011-04-28 01:41:35 +0000174 count,
175 y;
cristy03533f22011-03-06 23:30:17 +0000176
cristy3ed852e2009-09-05 21:47:34 +0000177 unsigned char
178 *pixels;
179
180 /*
181 Open image file.
182 */
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);
191 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
192 if (status == MagickFalse)
193 {
194 image=DestroyImageList(image);
195 return((Image *) NULL);
196 }
197 /*
198 Decode image header.
199 */
200 c=ReadBlobByte(image);
201 count=1;
202 if (c == EOF)
203 {
204 image=DestroyImage(image);
205 return((Image *) NULL);
206 }
207 length=0;
208 image->columns=0;
209 image->rows=0;
210 while (isgraph(c) && ((image->columns == 0) || (image->rows == 0)))
211 {
212 if (isalnum(c) == MagickFalse)
213 {
214 c=ReadBlobByte(image);
215 count++;
216 }
217 else
218 {
219 register char
220 *p;
221
222 /*
223 Determine a keyword and its value.
224 */
225 p=keyword;
226 do
227 {
228 if ((size_t) (p-keyword) < (MaxTextExtent-1))
229 *p++=c;
230 c=ReadBlobByte(image);
231 count++;
232 } while (isalnum(c) || (c == '_'));
233 *p='\0';
234 value_expected=MagickFalse;
235 while ((isspace((int) ((unsigned char) c)) != 0) || (c == '='))
236 {
237 if (c == '=')
238 value_expected=MagickTrue;
239 c=ReadBlobByte(image);
240 count++;
241 }
242 if (value_expected == MagickFalse)
243 continue;
244 p=value;
245 while (isalnum(c))
246 {
247 if ((size_t) (p-value) < (MaxTextExtent-1))
248 *p++=c;
249 c=ReadBlobByte(image);
250 count++;
251 }
252 *p='\0';
253 /*
254 Assign a value to the specified keyword.
255 */
256 if (LocaleCompare(keyword,"Label_RECORDS") == 0)
cristyf2f27272009-12-17 14:48:46 +0000257 length=(ssize_t) StringToLong(value);
cristy3ed852e2009-09-05 21:47:34 +0000258 if (LocaleCompare(keyword,"LBLSIZE") == 0)
cristyf2f27272009-12-17 14:48:46 +0000259 length=(ssize_t) StringToLong(value);
cristy3ed852e2009-09-05 21:47:34 +0000260 if (LocaleCompare(keyword,"RECORD_BYTES") == 0)
cristy5131f3e2009-12-18 03:09:10 +0000261 image->columns=StringToUnsignedLong(value);
cristy3ed852e2009-09-05 21:47:34 +0000262 if (LocaleCompare(keyword,"NS") == 0)
cristy5131f3e2009-12-18 03:09:10 +0000263 image->columns=StringToUnsignedLong(value);
cristy3ed852e2009-09-05 21:47:34 +0000264 if (LocaleCompare(keyword,"LINES") == 0)
cristy5131f3e2009-12-18 03:09:10 +0000265 image->rows=StringToUnsignedLong(value);
cristy3ed852e2009-09-05 21:47:34 +0000266 if (LocaleCompare(keyword,"NL") == 0)
cristy5131f3e2009-12-18 03:09:10 +0000267 image->rows=StringToUnsignedLong(value);
cristy3ed852e2009-09-05 21:47:34 +0000268 }
269 while (isspace((int) ((unsigned char) c)) != 0)
270 {
271 c=ReadBlobByte(image);
272 count++;
273 }
274 }
275 while (count < (ssize_t) length)
276 {
277 c=ReadBlobByte(image);
278 count++;
279 }
280 if ((image->columns == 0) || (image->rows == 0))
281 ThrowReaderException(CorruptImageError,"NegativeOrZeroImageSize");
282 image->depth=8;
cristy018f07f2011-09-04 21:15:19 +0000283 if (AcquireImageColormap(image,256,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000284 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
285 if (image_info->ping != MagickFalse)
286 {
287 (void) CloseBlob(image);
288 return(GetFirstImageInList(image));
289 }
290 /*
291 Read VICAR pixels.
292 */
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);
cristybb503372010-05-27 20:51:26 +0000299 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000300 {
301 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +0000302 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000303 break;
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)
308 break;
cristycee97112010-05-28 00:44:52 +0000309 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
cristy4bad3162011-03-06 18:44:39 +0000310 image->rows);
cristy3ed852e2009-09-05 21:47:34 +0000311 if (status == MagickFalse)
312 break;
313 }
314 SetQuantumImageType(image,quantum_type);
315 quantum_info=DestroyQuantumInfo(quantum_info);
316 if (EOFBlob(image) != MagickFalse)
317 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
318 image->filename);
319 (void) CloseBlob(image);
320 return(GetFirstImageInList(image));
321}
322
323/*
324%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
325% %
326% %
327% %
328% R e g i s t e r V I C A R I m a g e %
329% %
330% %
331% %
332%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
333%
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.
340%
341% The format of the RegisterVICARImage method is:
342%
cristybb503372010-05-27 20:51:26 +0000343% size_t RegisterVICARImage(void)
cristy3ed852e2009-09-05 21:47:34 +0000344%
345*/
cristybb503372010-05-27 20:51:26 +0000346ModuleExport size_t RegisterVICARImage(void)
cristy3ed852e2009-09-05 21:47:34 +0000347{
348 MagickInfo
349 *entry;
350
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);
360}
361
362/*
363%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
364% %
365% %
366% %
367% U n r e g i s t e r V I C A R I m a g e %
368% %
369% %
370% %
371%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
372%
373% UnregisterVICARImage() removes format registrations made by the
374% VICAR module from the list of supported formats.
375%
376% The format of the UnregisterVICARImage method is:
377%
378% UnregisterVICARImage(void)
379%
380*/
381ModuleExport void UnregisterVICARImage(void)
382{
383 (void) UnregisterMagickInfo("VICAR");
384}
385
386/*
387%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
388% %
389% %
390% %
391% W r i t e V I C A R I m a g e %
392% %
393% %
394% %
395%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
396%
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
401% grayscale plane.
402%
cristy03533f22011-03-06 23:30:17 +0000403% WriteVICARImage was written contributed by gorelick@esther.la.asu.edu.
cristy3ed852e2009-09-05 21:47:34 +0000404%
405% The format of the WriteVICARImage method is:
406%
407% MagickBooleanType WriteVICARImage(const ImageInfo *image_info,
cristy3a37efd2011-08-28 20:31:03 +0000408% Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000409%
410% A description of each parameter follows.
411%
412% o image_info: the image info.
413%
414% o image: The image.
415%
cristy3a37efd2011-08-28 20:31:03 +0000416% o exception: return any errors or warnings in this structure.
417%
cristy3ed852e2009-09-05 21:47:34 +0000418*/
419static MagickBooleanType WriteVICARImage(const ImageInfo *image_info,
cristy3a37efd2011-08-28 20:31:03 +0000420 Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000421{
422 char
423 header[MaxTextExtent];
424
425 int
426 y;
427
428 MagickBooleanType
429 status;
430
431 QuantumInfo
432 *quantum_info;
433
cristy4c08aed2011-07-01 19:47:50 +0000434 register const Quantum
cristy3ed852e2009-09-05 21:47:34 +0000435 *p;
436
cristy3ed852e2009-09-05 21:47:34 +0000437 size_t
438 length;
439
cristyc6da28e2011-04-28 01:41:35 +0000440 ssize_t
441 count;
442
cristy3ed852e2009-09-05 21:47:34 +0000443 unsigned char
444 *pixels;
445
446 /*
447 Open output image file.
448 */
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);
cristy3a37efd2011-08-28 20:31:03 +0000455 assert(exception != (ExceptionInfo *) NULL);
456 assert(exception->signature == MagickSignature);
457 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
cristy3ed852e2009-09-05 21:47:34 +0000458 if (status == MagickFalse)
459 return(status);
cristy510d06a2011-07-06 23:43:54 +0000460 if (IsRGBColorspace(image->colorspace) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000461 (void) TransformImageColorspace(image,RGBColorspace);
462 /*
463 Write header.
464 */
465 (void) ResetMagickMemory(header,' ',MaxTextExtent);
cristyb51dff52011-05-19 16:55:47 +0000466 (void) FormatLocaleString(header,MaxTextExtent,
cristye8c25f92010-06-03 00:53:06 +0000467 "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);
cristy3ed852e2009-09-05 21:47:34 +0000471 (void) WriteBlob(image,MaxTextExtent,(unsigned char *) header);
472 /*
473 Write VICAR pixels.
474 */
475 image->depth=8;
476 quantum_info=AcquireQuantumInfo(image_info,image);
477 if (quantum_info == (QuantumInfo *) NULL)
478 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
479 pixels=GetQuantumPixels(quantum_info);
cristybb503372010-05-27 20:51:26 +0000480 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000481 {
cristy3a37efd2011-08-28 20:31:03 +0000482 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +0000483 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000484 break;
cristy4c08aed2011-07-01 19:47:50 +0000485 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
cristy3a37efd2011-08-28 20:31:03 +0000486 GrayQuantum,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +0000487 count=WriteBlob(image,length,pixels);
488 if (count != (ssize_t) length)
489 break;
cristycee97112010-05-28 00:44:52 +0000490 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
cristy84c3d052011-03-07 19:22:02 +0000491 image->rows);
cristy3ed852e2009-09-05 21:47:34 +0000492 if (status == MagickFalse)
493 break;
494 }
495 quantum_info=DestroyQuantumInfo(quantum_info);
496 (void) CloseBlob(image);
497 return(MagickTrue);
498}