blob: 54f1dcd85a9c9bc3f88432f3ec3b61026163da57 [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% %
cristy16af1cb2009-12-11 21:38:29 +000020% Copyright 1999-2010 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*/
42#include "magick/studio.h"
43#include "magick/blob.h"
44#include "magick/blob-private.h"
45#include "magick/cache.h"
cristye7e40552010-04-24 21:34:22 +000046#include "magick/colormap.h"
cristy3ed852e2009-09-05 21:47:34 +000047#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"
cristyf2f27272009-12-17 14:48:46 +000056#include "magick/module.h"
cristy3ed852e2009-09-05 21:47:34 +000057#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"
cristyf2f27272009-12-17 14:48:46 +000063#include "magick/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
cristybb503372010-05-27 20:51:26 +0000156 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000157 y;
158
159 MagickBooleanType
160 status,
161 value_expected;
162
163 QuantumInfo
164 *quantum_info;
165
166 QuantumType
167 quantum_type;
168
169 register PixelPacket
170 *q;
171
172 ssize_t
173 count;
174
175 size_t
176 length;
177
178 unsigned char
179 *pixels;
180
181 /*
182 Open image file.
183 */
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)
194 {
195 image=DestroyImageList(image);
196 return((Image *) NULL);
197 }
198 /*
199 Decode image header.
200 */
201 c=ReadBlobByte(image);
202 count=1;
203 if (c == EOF)
204 {
205 image=DestroyImage(image);
206 return((Image *) NULL);
207 }
208 length=0;
209 image->columns=0;
210 image->rows=0;
211 while (isgraph(c) && ((image->columns == 0) || (image->rows == 0)))
212 {
213 if (isalnum(c) == MagickFalse)
214 {
215 c=ReadBlobByte(image);
216 count++;
217 }
218 else
219 {
220 register char
221 *p;
222
223 /*
224 Determine a keyword and its value.
225 */
226 p=keyword;
227 do
228 {
229 if ((size_t) (p-keyword) < (MaxTextExtent-1))
230 *p++=c;
231 c=ReadBlobByte(image);
232 count++;
233 } while (isalnum(c) || (c == '_'));
234 *p='\0';
235 value_expected=MagickFalse;
236 while ((isspace((int) ((unsigned char) c)) != 0) || (c == '='))
237 {
238 if (c == '=')
239 value_expected=MagickTrue;
240 c=ReadBlobByte(image);
241 count++;
242 }
243 if (value_expected == MagickFalse)
244 continue;
245 p=value;
246 while (isalnum(c))
247 {
248 if ((size_t) (p-value) < (MaxTextExtent-1))
249 *p++=c;
250 c=ReadBlobByte(image);
251 count++;
252 }
253 *p='\0';
254 /*
255 Assign a value to the specified keyword.
256 */
257 if (LocaleCompare(keyword,"Label_RECORDS") == 0)
cristyf2f27272009-12-17 14:48:46 +0000258 length=(ssize_t) StringToLong(value);
cristy3ed852e2009-09-05 21:47:34 +0000259 if (LocaleCompare(keyword,"LBLSIZE") == 0)
cristyf2f27272009-12-17 14:48:46 +0000260 length=(ssize_t) StringToLong(value);
cristy3ed852e2009-09-05 21:47:34 +0000261 if (LocaleCompare(keyword,"RECORD_BYTES") == 0)
cristy5131f3e2009-12-18 03:09:10 +0000262 image->columns=StringToUnsignedLong(value);
cristy3ed852e2009-09-05 21:47:34 +0000263 if (LocaleCompare(keyword,"NS") == 0)
cristy5131f3e2009-12-18 03:09:10 +0000264 image->columns=StringToUnsignedLong(value);
cristy3ed852e2009-09-05 21:47:34 +0000265 if (LocaleCompare(keyword,"LINES") == 0)
cristy5131f3e2009-12-18 03:09:10 +0000266 image->rows=StringToUnsignedLong(value);
cristy3ed852e2009-09-05 21:47:34 +0000267 if (LocaleCompare(keyword,"NL") == 0)
cristy5131f3e2009-12-18 03:09:10 +0000268 image->rows=StringToUnsignedLong(value);
cristy3ed852e2009-09-05 21:47:34 +0000269 }
270 while (isspace((int) ((unsigned char) c)) != 0)
271 {
272 c=ReadBlobByte(image);
273 count++;
274 }
275 }
276 while (count < (ssize_t) length)
277 {
278 c=ReadBlobByte(image);
279 count++;
280 }
281 if ((image->columns == 0) || (image->rows == 0))
282 ThrowReaderException(CorruptImageError,"NegativeOrZeroImageSize");
283 image->depth=8;
284 if (AcquireImageColormap(image,256) == MagickFalse)
285 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
286 if (image_info->ping != MagickFalse)
287 {
288 (void) CloseBlob(image);
289 return(GetFirstImageInList(image));
290 }
291 /*
292 Read VICAR pixels.
293 */
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);
cristybb503372010-05-27 20:51:26 +0000300 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000301 {
302 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
303 if (q == (PixelPacket *) NULL)
304 break;
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)
309 break;
cristycee97112010-05-28 00:44:52 +0000310 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
311 image->rows);
cristy3ed852e2009-09-05 21:47:34 +0000312 if (status == MagickFalse)
313 break;
314 }
315 SetQuantumImageType(image,quantum_type);
316 quantum_info=DestroyQuantumInfo(quantum_info);
317 if (EOFBlob(image) != MagickFalse)
318 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
319 image->filename);
320 (void) CloseBlob(image);
321 return(GetFirstImageInList(image));
322}
323
324/*
325%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
326% %
327% %
328% %
329% R e g i s t e r V I C A R I m a g e %
330% %
331% %
332% %
333%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
334%
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.
341%
342% The format of the RegisterVICARImage method is:
343%
cristybb503372010-05-27 20:51:26 +0000344% size_t RegisterVICARImage(void)
cristy3ed852e2009-09-05 21:47:34 +0000345%
346*/
cristybb503372010-05-27 20:51:26 +0000347ModuleExport size_t RegisterVICARImage(void)
cristy3ed852e2009-09-05 21:47:34 +0000348{
349 MagickInfo
350 *entry;
351
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);
361}
362
363/*
364%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
365% %
366% %
367% %
368% U n r e g i s t e r V I C A R I m a g e %
369% %
370% %
371% %
372%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
373%
374% UnregisterVICARImage() removes format registrations made by the
375% VICAR module from the list of supported formats.
376%
377% The format of the UnregisterVICARImage method is:
378%
379% UnregisterVICARImage(void)
380%
381*/
382ModuleExport void UnregisterVICARImage(void)
383{
384 (void) UnregisterMagickInfo("VICAR");
385}
386
387/*
388%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
389% %
390% %
391% %
392% W r i t e V I C A R I m a g e %
393% %
394% %
395% %
396%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
397%
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
402% grayscale plane.
403%
404% WriteVICARImage was written contributed by
405% gorelick@esther.la.asu.edu.
406%
407% The format of the WriteVICARImage method is:
408%
409% MagickBooleanType WriteVICARImage(const ImageInfo *image_info,
410% Image *image)
411%
412% A description of each parameter follows.
413%
414% o image_info: the image info.
415%
416% o image: The image.
417%
418*/
419static MagickBooleanType WriteVICARImage(const ImageInfo *image_info,
420 Image *image)
421{
422 char
423 header[MaxTextExtent];
424
425 int
426 y;
427
428 MagickBooleanType
429 status;
430
431 QuantumInfo
432 *quantum_info;
433
434 register const PixelPacket
435 *p;
436
437 ssize_t
438 count;
439
440 size_t
441 length;
442
443 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);
455 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
456 if (status == MagickFalse)
457 return(status);
458 if (image->colorspace != RGBColorspace)
459 (void) TransformImageColorspace(image,RGBColorspace);
460 /*
461 Write header.
462 */
463 (void) ResetMagickMemory(header,' ',MaxTextExtent);
464 (void) FormatMagickString(header,MaxTextExtent,
cristye8c25f92010-06-03 00:53:06 +0000465 "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);
cristy3ed852e2009-09-05 21:47:34 +0000469 (void) WriteBlob(image,MaxTextExtent,(unsigned char *) header);
470 /*
471 Write VICAR pixels.
472 */
473 image->depth=8;
474 quantum_info=AcquireQuantumInfo(image_info,image);
475 if (quantum_info == (QuantumInfo *) NULL)
476 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
477 pixels=GetQuantumPixels(quantum_info);
cristybb503372010-05-27 20:51:26 +0000478 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000479 {
480 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
481 if (p == (const PixelPacket *) NULL)
482 break;
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)
487 break;
cristycee97112010-05-28 00:44:52 +0000488 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
489 image->rows);
cristy3ed852e2009-09-05 21:47:34 +0000490 if (status == MagickFalse)
491 break;
492 }
493 quantum_info=DestroyQuantumInfo(quantum_info);
494 (void) CloseBlob(image);
495 return(MagickTrue);
496}