]> granicus.if.org Git - imagemagick/blob - coders/vicar.c
(no commit message)
[imagemagick] / coders / vicar.c
1 /*
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 %                                                                             %
20 %  Copyright 1999-2011 ImageMagick Studio LLC, a non-profit organization      %
21 %  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 \f
39 /*
40   Include declarations.
41 */
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/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"
64 \f
65 /*
66   Forward declarations.
67 */
68 static MagickBooleanType
69   WriteVICARImage(const ImageInfo *,Image *);
70 \f
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 */
97 static 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 \f
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 */
143 static 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
156   MagickBooleanType
157     status,
158     value_expected;
159
160   QuantumInfo
161     *quantum_info;
162
163   QuantumType
164     quantum_type;
165
166   register Quantum
167     *q;
168
169   size_t
170     length;
171
172   ssize_t
173     count,
174     y;
175
176   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)
256           length=(ssize_t) StringToLong(value);
257         if (LocaleCompare(keyword,"LBLSIZE") == 0)
258           length=(ssize_t) StringToLong(value);
259         if (LocaleCompare(keyword,"RECORD_BYTES") == 0)
260           image->columns=StringToUnsignedLong(value);
261         if (LocaleCompare(keyword,"NS") == 0)
262           image->columns=StringToUnsignedLong(value);
263         if (LocaleCompare(keyword,"LINES") == 0)
264           image->rows=StringToUnsignedLong(value);
265         if (LocaleCompare(keyword,"NL") == 0)
266           image->rows=StringToUnsignedLong(value);
267       }
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);
298   for (y=0; y < (ssize_t) image->rows; y++)
299   {
300     q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
301     if (q == (const Quantum *) NULL)
302       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;
308     status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
309       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 \f
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 %      size_t RegisterVICARImage(void)
343 %
344 */
345 ModuleExport size_t 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 \f
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 */
380 ModuleExport void UnregisterVICARImage(void)
381 {
382   (void) UnregisterMagickInfo("VICAR");
383 }
384 \f
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 gorelick@esther.la.asu.edu.
403 %
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 */
416 static 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
431   register const Quantum
432     *p;
433
434   size_t
435     length;
436
437   ssize_t
438     count;
439
440   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);
461   (void) FormatLocaleString(header,MaxTextExtent,
462     "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);
466   (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);
475   for (y=0; y < (ssize_t) image->rows; y++)
476   {
477     p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
478     if (p == (const Quantum *) NULL)
479       break;
480     length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
481       GrayQuantum,pixels,&image->exception);
482     count=WriteBlob(image,length,pixels);
483     if (count != (ssize_t) length)
484       break;
485     status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
486       image->rows);
487     if (status == MagickFalse)
488       break;
489   }
490   quantum_info=DestroyQuantumInfo(quantum_info);
491   (void) CloseBlob(image);
492   return(MagickTrue);
493 }