blob: 96c5112192c0fb6c5bcf6843aade2f5d0273d9b8 [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"
46#include "magick/colorspace.h"
47#include "magick/constitute.h"
48#include "magick/exception.h"
49#include "magick/exception-private.h"
50#include "magick/image.h"
51#include "magick/image-private.h"
52#include "magick/list.h"
53#include "magick/magick.h"
54#include "magick/memory_.h"
cristyf2f27272009-12-17 14:48:46 +000055#include "magick/module.h"
cristy3ed852e2009-09-05 21:47:34 +000056#include "magick/monitor.h"
57#include "magick/monitor-private.h"
58#include "magick/quantum-private.h"
59#include "magick/quantum-private.h"
60#include "magick/static.h"
61#include "magick/string_.h"
cristyf2f27272009-12-17 14:48:46 +000062#include "magick/string-private.h"
cristy3ed852e2009-09-05 21:47:34 +000063
64/*
65 Forward declarations.
66*/
67static MagickBooleanType
68 WriteVICARImage(const ImageInfo *,Image *);
69
70/*
71%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
72% %
73% %
74% %
75% I s V I C A R %
76% %
77% %
78% %
79%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
80%
81% IsVICAR() returns MagickTrue if the image format type, identified by the
82% magick string, is VICAR.
83%
84% The format of the IsVICAR method is:
85%
86% MagickBooleanType IsVICAR(const unsigned char *magick,
87% const size_t length)
88%
89% A description of each parameter follows:
90%
91% o magick: compare image format pattern against these bytes.
92%
93% o length: Specifies the length of the magick string.
94%
95*/
96static MagickBooleanType IsVICAR(const unsigned char *magick,
97 const size_t length)
98{
99 if (length < 14)
100 return(MagickFalse);
101 if (LocaleNCompare((const char *) magick,"LBLSIZE",7) == 0)
102 return(MagickTrue);
103 if (LocaleNCompare((const char *) magick,"NJPL1I",6) == 0)
104 return(MagickTrue);
105 if (LocaleNCompare((const char *) magick,"PDS_VERSION_ID",14) == 0)
106 return(MagickTrue);
107 return(MagickFalse);
108}
109
110/*
111%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
112% %
113% %
114% %
115% R e a d V I C A R I m a g e %
116% %
117% %
118% %
119%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
120%
121% ReadVICARImage() reads a VICAR image file and returns it. It
122% allocates the memory necessary for the new Image structure and returns a
123% pointer to the new image.
124%
125% The format of the ReadVICARImage method is:
126%
127% Image *ReadVICARImage(const ImageInfo *image_info,
128% ExceptionInfo *exception)
129%
130% A description of each parameter follows:
131%
132% o image: Method ReadVICARImage returns a pointer to the image after
133% reading. A null image is returned if there is a memory shortage or if
134% the image cannot be read.
135%
136% o image_info: the image info.
137%
138% o exception: return any errors or warnings in this structure.
139%
140%
141*/
142static Image *ReadVICARImage(const ImageInfo *image_info,
143 ExceptionInfo *exception)
144{
145 char
146 keyword[MaxTextExtent],
147 value[MaxTextExtent];
148
149 Image
150 *image;
151
152 int
153 c;
154
155 long
156 y;
157
158 MagickBooleanType
159 status,
160 value_expected;
161
162 QuantumInfo
163 *quantum_info;
164
165 QuantumType
166 quantum_type;
167
168 register PixelPacket
169 *q;
170
171 ssize_t
172 count;
173
174 size_t
175 length;
176
177 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;
283 if (AcquireImageColormap(image,256) == MagickFalse)
284 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);
299 for (y=0; y < (long) image->rows; y++)
300 {
301 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
302 if (q == (PixelPacket *) NULL)
303 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;
309 status=SetImageProgress(image,LoadImageTag,y,image->rows);
310 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%
342% unsigned long RegisterVICARImage(void)
343%
344*/
345ModuleExport unsigned long RegisterVICARImage(void)
346{
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%
402% WriteVICARImage was written contributed by
403% gorelick@esther.la.asu.edu.
404%
405% The format of the WriteVICARImage method is:
406%
407% MagickBooleanType WriteVICARImage(const ImageInfo *image_info,
408% Image *image)
409%
410% A description of each parameter follows.
411%
412% o image_info: the image info.
413%
414% o image: The image.
415%
416*/
417static MagickBooleanType WriteVICARImage(const ImageInfo *image_info,
418 Image *image)
419{
420 char
421 header[MaxTextExtent];
422
423 int
424 y;
425
426 MagickBooleanType
427 status;
428
429 QuantumInfo
430 *quantum_info;
431
432 register const PixelPacket
433 *p;
434
435 ssize_t
436 count;
437
438 size_t
439 length;
440
441 unsigned char
442 *pixels;
443
444 /*
445 Open output image file.
446 */
447 assert(image_info != (const ImageInfo *) NULL);
448 assert(image_info->signature == MagickSignature);
449 assert(image != (Image *) NULL);
450 assert(image->signature == MagickSignature);
451 if (image->debug != MagickFalse)
452 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
453 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
454 if (status == MagickFalse)
455 return(status);
456 if (image->colorspace != RGBColorspace)
457 (void) TransformImageColorspace(image,RGBColorspace);
458 /*
459 Write header.
460 */
461 (void) ResetMagickMemory(header,' ',MaxTextExtent);
462 (void) FormatMagickString(header,MaxTextExtent,
463 "LBLSIZE=%lu FORMAT='BYTE' TYPE='IMAGE' BUFSIZE=20000 DIM=2 EOL=0 "
464 "RECSIZE=%lu ORG='BSQ' NL=%lu NS=%lu NB=1 N1=0 N2=0 N3=0 N4=0 NBB=0 "
465 "NLB=0 TASK='ImageMagick'",(unsigned long) MaxTextExtent,image->columns,
466 image->rows,image->columns);
467 (void) WriteBlob(image,MaxTextExtent,(unsigned char *) header);
468 /*
469 Write VICAR pixels.
470 */
471 image->depth=8;
472 quantum_info=AcquireQuantumInfo(image_info,image);
473 if (quantum_info == (QuantumInfo *) NULL)
474 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
475 pixels=GetQuantumPixels(quantum_info);
476 for (y=0; y < (long) image->rows; y++)
477 {
478 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
479 if (p == (const PixelPacket *) NULL)
480 break;
481 length=ExportQuantumPixels(image,(const CacheView *) NULL,quantum_info,
482 GrayQuantum,pixels,&image->exception);
483 count=WriteBlob(image,length,pixels);
484 if (count != (ssize_t) length)
485 break;
486 status=SetImageProgress(image,SaveImageTag,y,image->rows);
487 if (status == MagickFalse)
488 break;
489 }
490 quantum_info=DestroyQuantumInfo(quantum_info);
491 (void) CloseBlob(image);
492 return(MagickTrue);
493}