blob: 07d936545ab62b81321470aa397cc1fbef71564e [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"
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"
cristy3ed852e2009-09-05 21:47:34 +000064
65/*
66 Forward declarations.
67*/
68static MagickBooleanType
69 WriteVICARImage(const ImageInfo *,Image *);
70
71/*
72%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
73% %
74% %
75% %
76% I s V I C A R %
77% %
78% %
79% %
80%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
81%
82% IsVICAR() returns MagickTrue if the image format type, identified by the
83% magick string, is VICAR.
84%
85% The format of the IsVICAR method is:
86%
87% MagickBooleanType IsVICAR(const unsigned char *magick,
88% const size_t length)
89%
90% A description of each parameter follows:
91%
92% o magick: compare image format pattern against these bytes.
93%
94% o length: Specifies the length of the magick string.
95%
96*/
97static MagickBooleanType IsVICAR(const unsigned char *magick,
98 const size_t length)
99{
100 if (length < 14)
101 return(MagickFalse);
102 if (LocaleNCompare((const char *) magick,"LBLSIZE",7) == 0)
103 return(MagickTrue);
104 if (LocaleNCompare((const char *) magick,"NJPL1I",6) == 0)
105 return(MagickTrue);
106 if (LocaleNCompare((const char *) magick,"PDS_VERSION_ID",14) == 0)
107 return(MagickTrue);
108 return(MagickFalse);
109}
110
111/*
112%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
113% %
114% %
115% %
116% R e a d V I C A R I m a g e %
117% %
118% %
119% %
120%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
121%
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.
125%
126% The format of the ReadVICARImage method is:
127%
128% Image *ReadVICARImage(const ImageInfo *image_info,
129% ExceptionInfo *exception)
130%
131% A description of each parameter follows:
132%
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.
136%
137% o image_info: the image info.
138%
139% o exception: return any errors or warnings in this structure.
140%
141%
142*/
143static Image *ReadVICARImage(const ImageInfo *image_info,
144 ExceptionInfo *exception)
145{
146 char
147 keyword[MaxTextExtent],
148 value[MaxTextExtent];
149
150 Image
151 *image;
152
153 int
154 c;
155
cristy3ed852e2009-09-05 21:47:34 +0000156 MagickBooleanType
157 status,
158 value_expected;
159
160 QuantumInfo
161 *quantum_info;
162
163 QuantumType
164 quantum_type;
165
cristy4c08aed2011-07-01 19:47:50 +0000166 register Quantum
cristy3ed852e2009-09-05 21:47:34 +0000167 *q;
168
cristy3ed852e2009-09-05 21:47:34 +0000169 size_t
170 length;
171
cristy03533f22011-03-06 23:30:17 +0000172 ssize_t
cristyc6da28e2011-04-28 01:41:35 +0000173 count,
174 y;
cristy03533f22011-03-06 23:30:17 +0000175
cristy3ed852e2009-09-05 21:47:34 +0000176 unsigned char
177 *pixels;
178
179 /*
180 Open image file.
181 */
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)
192 {
193 image=DestroyImageList(image);
194 return((Image *) NULL);
195 }
196 /*
197 Decode image header.
198 */
199 c=ReadBlobByte(image);
200 count=1;
201 if (c == EOF)
202 {
203 image=DestroyImage(image);
204 return((Image *) NULL);
205 }
206 length=0;
207 image->columns=0;
208 image->rows=0;
209 while (isgraph(c) && ((image->columns == 0) || (image->rows == 0)))
210 {
211 if (isalnum(c) == MagickFalse)
212 {
213 c=ReadBlobByte(image);
214 count++;
215 }
216 else
217 {
218 register char
219 *p;
220
221 /*
222 Determine a keyword and its value.
223 */
224 p=keyword;
225 do
226 {
227 if ((size_t) (p-keyword) < (MaxTextExtent-1))
228 *p++=c;
229 c=ReadBlobByte(image);
230 count++;
231 } while (isalnum(c) || (c == '_'));
232 *p='\0';
233 value_expected=MagickFalse;
234 while ((isspace((int) ((unsigned char) c)) != 0) || (c == '='))
235 {
236 if (c == '=')
237 value_expected=MagickTrue;
238 c=ReadBlobByte(image);
239 count++;
240 }
241 if (value_expected == MagickFalse)
242 continue;
243 p=value;
244 while (isalnum(c))
245 {
246 if ((size_t) (p-value) < (MaxTextExtent-1))
247 *p++=c;
248 c=ReadBlobByte(image);
249 count++;
250 }
251 *p='\0';
252 /*
253 Assign a value to the specified keyword.
254 */
255 if (LocaleCompare(keyword,"Label_RECORDS") == 0)
cristyf2f27272009-12-17 14:48:46 +0000256 length=(ssize_t) StringToLong(value);
cristy3ed852e2009-09-05 21:47:34 +0000257 if (LocaleCompare(keyword,"LBLSIZE") == 0)
cristyf2f27272009-12-17 14:48:46 +0000258 length=(ssize_t) StringToLong(value);
cristy3ed852e2009-09-05 21:47:34 +0000259 if (LocaleCompare(keyword,"RECORD_BYTES") == 0)
cristy5131f3e2009-12-18 03:09:10 +0000260 image->columns=StringToUnsignedLong(value);
cristy3ed852e2009-09-05 21:47:34 +0000261 if (LocaleCompare(keyword,"NS") == 0)
cristy5131f3e2009-12-18 03:09:10 +0000262 image->columns=StringToUnsignedLong(value);
cristy3ed852e2009-09-05 21:47:34 +0000263 if (LocaleCompare(keyword,"LINES") == 0)
cristy5131f3e2009-12-18 03:09:10 +0000264 image->rows=StringToUnsignedLong(value);
cristy3ed852e2009-09-05 21:47:34 +0000265 if (LocaleCompare(keyword,"NL") == 0)
cristy5131f3e2009-12-18 03:09:10 +0000266 image->rows=StringToUnsignedLong(value);
cristy3ed852e2009-09-05 21:47:34 +0000267 }
268 while (isspace((int) ((unsigned char) c)) != 0)
269 {
270 c=ReadBlobByte(image);
271 count++;
272 }
273 }
274 while (count < (ssize_t) length)
275 {
276 c=ReadBlobByte(image);
277 count++;
278 }
279 if ((image->columns == 0) || (image->rows == 0))
280 ThrowReaderException(CorruptImageError,"NegativeOrZeroImageSize");
281 image->depth=8;
282 if (AcquireImageColormap(image,256) == MagickFalse)
283 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
284 if (image_info->ping != MagickFalse)
285 {
286 (void) CloseBlob(image);
287 return(GetFirstImageInList(image));
288 }
289 /*
290 Read VICAR pixels.
291 */
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);
cristybb503372010-05-27 20:51:26 +0000298 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000299 {
300 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +0000301 if (q == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000302 break;
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)
307 break;
cristycee97112010-05-28 00:44:52 +0000308 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
cristy4bad3162011-03-06 18:44:39 +0000309 image->rows);
cristy3ed852e2009-09-05 21:47:34 +0000310 if (status == MagickFalse)
311 break;
312 }
313 SetQuantumImageType(image,quantum_type);
314 quantum_info=DestroyQuantumInfo(quantum_info);
315 if (EOFBlob(image) != MagickFalse)
316 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
317 image->filename);
318 (void) CloseBlob(image);
319 return(GetFirstImageInList(image));
320}
321
322/*
323%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
324% %
325% %
326% %
327% R e g i s t e r V I C A R I m a g e %
328% %
329% %
330% %
331%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
332%
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.
339%
340% The format of the RegisterVICARImage method is:
341%
cristybb503372010-05-27 20:51:26 +0000342% size_t RegisterVICARImage(void)
cristy3ed852e2009-09-05 21:47:34 +0000343%
344*/
cristybb503372010-05-27 20:51:26 +0000345ModuleExport size_t RegisterVICARImage(void)
cristy3ed852e2009-09-05 21:47:34 +0000346{
347 MagickInfo
348 *entry;
349
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);
359}
360
361/*
362%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
363% %
364% %
365% %
366% U n r e g i s t e r V I C A R I m a g e %
367% %
368% %
369% %
370%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
371%
372% UnregisterVICARImage() removes format registrations made by the
373% VICAR module from the list of supported formats.
374%
375% The format of the UnregisterVICARImage method is:
376%
377% UnregisterVICARImage(void)
378%
379*/
380ModuleExport void UnregisterVICARImage(void)
381{
382 (void) UnregisterMagickInfo("VICAR");
383}
384
385/*
386%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
387% %
388% %
389% %
390% W r i t e V I C A R I m a g e %
391% %
392% %
393% %
394%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
395%
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
400% grayscale plane.
401%
cristy03533f22011-03-06 23:30:17 +0000402% WriteVICARImage was written contributed by gorelick@esther.la.asu.edu.
cristy3ed852e2009-09-05 21:47:34 +0000403%
404% The format of the WriteVICARImage method is:
405%
406% MagickBooleanType WriteVICARImage(const ImageInfo *image_info,
407% Image *image)
408%
409% A description of each parameter follows.
410%
411% o image_info: the image info.
412%
413% o image: The image.
414%
415*/
416static MagickBooleanType WriteVICARImage(const ImageInfo *image_info,
417 Image *image)
418{
419 char
420 header[MaxTextExtent];
421
422 int
423 y;
424
425 MagickBooleanType
426 status;
427
428 QuantumInfo
429 *quantum_info;
430
cristy4c08aed2011-07-01 19:47:50 +0000431 register const Quantum
cristy3ed852e2009-09-05 21:47:34 +0000432 *p;
433
cristy3ed852e2009-09-05 21:47:34 +0000434 size_t
435 length;
436
cristyc6da28e2011-04-28 01:41:35 +0000437 ssize_t
438 count;
439
cristy3ed852e2009-09-05 21:47:34 +0000440 unsigned char
441 *pixels;
442
443 /*
444 Open output image file.
445 */
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)
454 return(status);
455 if (image->colorspace != RGBColorspace)
456 (void) TransformImageColorspace(image,RGBColorspace);
457 /*
458 Write header.
459 */
460 (void) ResetMagickMemory(header,' ',MaxTextExtent);
cristyb51dff52011-05-19 16:55:47 +0000461 (void) FormatLocaleString(header,MaxTextExtent,
cristye8c25f92010-06-03 00:53:06 +0000462 "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);
cristy3ed852e2009-09-05 21:47:34 +0000466 (void) WriteBlob(image,MaxTextExtent,(unsigned char *) header);
467 /*
468 Write VICAR pixels.
469 */
470 image->depth=8;
471 quantum_info=AcquireQuantumInfo(image_info,image);
472 if (quantum_info == (QuantumInfo *) NULL)
473 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
474 pixels=GetQuantumPixels(quantum_info);
cristybb503372010-05-27 20:51:26 +0000475 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000476 {
477 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
cristy4c08aed2011-07-01 19:47:50 +0000478 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000479 break;
cristy4c08aed2011-07-01 19:47:50 +0000480 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
cristy3ed852e2009-09-05 21:47:34 +0000481 GrayQuantum,pixels,&image->exception);
482 count=WriteBlob(image,length,pixels);
483 if (count != (ssize_t) length)
484 break;
cristycee97112010-05-28 00:44:52 +0000485 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
cristy84c3d052011-03-07 19:22:02 +0000486 image->rows);
cristy3ed852e2009-09-05 21:47:34 +0000487 if (status == MagickFalse)
488 break;
489 }
490 quantum_info=DestroyQuantumInfo(quantum_info);
491 (void) CloseBlob(image);
492 return(MagickTrue);
493}