2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6 % V V IIIII FFFFF FFFFF %
13 % Read/Write Khoros Visualization Image Format %
20 % Copyright 1999-2009 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
26 % http://www.imagemagick.org/script/license.php %
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. %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
42 #include "magick/studio.h"
43 #include "magick/blob.h"
44 #include "magick/blob-private.h"
45 #include "magick/cache.h"
46 #include "magick/color.h"
47 #include "magick/color-private.h"
48 #include "magick/colorspace.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"
56 #include "magick/monitor.h"
57 #include "magick/monitor-private.h"
58 #include "magick/property.h"
59 #include "magick/quantum-private.h"
60 #include "magick/static.h"
61 #include "magick/string_.h"
62 #include "magick/module.h"
67 static MagickBooleanType
68 WriteVIFFImage(const ImageInfo *,Image *);
71 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
79 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
81 % IsVIFF() returns MagickTrue if the image format type, identified by the
82 % magick string, is VIFF.
84 % The format of the IsVIFF method is:
86 % MagickBooleanType IsVIFF(const unsigned char *magick,const size_t length)
88 % A description of each parameter follows:
90 % o magick: compare image format pattern against these bytes.
92 % o length: Specifies the length of the magick string.
96 static MagickBooleanType IsVIFF(const unsigned char *magick,const size_t length)
100 if (memcmp(magick,"\253\001",2) == 0)
106 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
110 % R e a d V I F F I m a g e %
114 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
116 % ReadVIFFImage() reads a Khoros Visualization image file and returns
117 % it. It allocates the memory necessary for the new Image structure and
118 % returns a pointer to the new image.
120 % The format of the ReadVIFFImage method is:
122 % Image *ReadVIFFImage(const ImageInfo *image_info,
123 % ExceptionInfo *exception)
125 % A description of each parameter follows:
127 % o image: Method ReadVIFFImage returns a pointer to the image after
128 % reading. A null image is returned if there is a memory shortage or if
129 % the image cannot be read.
131 % o image_info: the image info.
133 % o exception: return any errors or warnings in this structure.
136 static Image *ReadVIFFImage(const ImageInfo *image_info,
137 ExceptionInfo *exception)
139 #define VFF_CM_genericRGB 15
140 #define VFF_CM_ntscRGB 1
141 #define VFF_CM_NONE 0
142 #define VFF_DEP_DECORDER 0x4
143 #define VFF_DEP_NSORDER 0x8
144 #define VFF_DES_RAW 0
145 #define VFF_LOC_IMPLICIT 1
146 #define VFF_MAPTYP_NONE 0
147 #define VFF_MAPTYP_1_BYTE 1
148 #define VFF_MAPTYP_2_BYTE 2
149 #define VFF_MAPTYP_4_BYTE 4
150 #define VFF_MAPTYP_FLOAT 5
151 #define VFF_MAPTYP_DOUBLE 7
152 #define VFF_MS_NONE 0
153 #define VFF_MS_ONEPERBAND 1
154 #define VFF_MS_SHARED 3
155 #define VFF_TYP_BIT 0
156 #define VFF_TYP_1_BYTE 1
157 #define VFF_TYP_2_BYTE 2
158 #define VFF_TYP_4_BYTE 4
159 #define VFF_TYP_FLOAT 5
160 #define VFF_TYP_DOUBLE 9
162 typedef struct _ViffInfo
237 register unsigned char
259 assert(image_info != (const ImageInfo *) NULL);
260 assert(image_info->signature == MagickSignature);
261 if (image_info->debug != MagickFalse)
262 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
263 image_info->filename);
264 assert(exception != (ExceptionInfo *) NULL);
265 assert(exception->signature == MagickSignature);
266 image=AcquireImage(image_info);
267 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
268 if (status == MagickFalse)
270 image=DestroyImageList(image);
271 return((Image *) NULL);
274 Read VIFF header (1024 bytes).
276 count=ReadBlob(image,1,&viff_info.identifier);
280 Verify VIFF identifier.
282 if ((count == 0) || ((unsigned char) viff_info.identifier != 0xab))
283 ThrowReaderException(CorruptImageError,"NotAVIFFImage");
285 Initialize VIFF image.
287 count=ReadBlob(image,7,buffer);
288 viff_info.file_type=buffer[0];
289 viff_info.release=buffer[1];
290 viff_info.version=buffer[2];
291 viff_info.machine_dependency=buffer[3];
292 count=ReadBlob(image,512,(unsigned char *) viff_info.comment);
293 viff_info.comment[511]='\0';
294 if (strlen(viff_info.comment) > 4)
295 (void) SetImageProperty(image,"comment",viff_info.comment);
296 if ((viff_info.machine_dependency == VFF_DEP_DECORDER) ||
297 (viff_info.machine_dependency == VFF_DEP_NSORDER))
299 viff_info.rows=ReadBlobLSBLong(image);
300 viff_info.columns=ReadBlobLSBLong(image);
301 viff_info.subrows=ReadBlobLSBLong(image);
302 viff_info.x_offset=(long) ReadBlobLSBLong(image);
303 viff_info.y_offset=(long) ReadBlobLSBLong(image);
304 viff_info.x_bits_per_pixel=(float) ReadBlobLSBLong(image);
305 viff_info.y_bits_per_pixel=(float) ReadBlobLSBLong(image);
306 viff_info.location_type=ReadBlobLSBLong(image);
307 viff_info.location_dimension=ReadBlobLSBLong(image);
308 viff_info.number_of_images=ReadBlobLSBLong(image);
309 viff_info.number_data_bands=ReadBlobLSBLong(image);
310 viff_info.data_storage_type=ReadBlobLSBLong(image);
311 viff_info.data_encode_scheme=ReadBlobLSBLong(image);
312 viff_info.map_scheme=ReadBlobLSBLong(image);
313 viff_info.map_storage_type=ReadBlobLSBLong(image);
314 viff_info.map_rows=ReadBlobLSBLong(image);
315 viff_info.map_columns=ReadBlobLSBLong(image);
316 viff_info.map_subrows=ReadBlobLSBLong(image);
317 viff_info.map_enable=ReadBlobLSBLong(image);
318 viff_info.maps_per_cycle=ReadBlobLSBLong(image);
319 viff_info.color_space_model=ReadBlobLSBLong(image);
323 viff_info.rows=ReadBlobMSBLong(image);
324 viff_info.columns=ReadBlobMSBLong(image);
325 viff_info.subrows=ReadBlobMSBLong(image);
326 viff_info.x_offset=(long) ReadBlobMSBLong(image);
327 viff_info.y_offset=(long) ReadBlobMSBLong(image);
328 viff_info.x_bits_per_pixel=(float) ReadBlobMSBLong(image);
329 viff_info.y_bits_per_pixel=(float) ReadBlobMSBLong(image);
330 viff_info.location_type=ReadBlobMSBLong(image);
331 viff_info.location_dimension=ReadBlobMSBLong(image);
332 viff_info.number_of_images=ReadBlobMSBLong(image);
333 viff_info.number_data_bands=ReadBlobMSBLong(image);
334 viff_info.data_storage_type=ReadBlobMSBLong(image);
335 viff_info.data_encode_scheme=ReadBlobMSBLong(image);
336 viff_info.map_scheme=ReadBlobMSBLong(image);
337 viff_info.map_storage_type=ReadBlobMSBLong(image);
338 viff_info.map_rows=ReadBlobMSBLong(image);
339 viff_info.map_columns=ReadBlobMSBLong(image);
340 viff_info.map_subrows=ReadBlobMSBLong(image);
341 viff_info.map_enable=ReadBlobMSBLong(image);
342 viff_info.maps_per_cycle=ReadBlobMSBLong(image);
343 viff_info.color_space_model=ReadBlobMSBLong(image);
345 for (i=0; i < 420; i++)
346 (void) ReadBlobByte(image);
347 image->columns=viff_info.rows;
348 image->rows=viff_info.columns;
349 image->depth=viff_info.x_bits_per_pixel <= 8 ? 8UL : MAGICKCORE_QUANTUM_DEPTH;
351 Verify that we can read this VIFF image.
353 number_pixels=(MagickSizeType) viff_info.columns*viff_info.rows;
354 if (number_pixels != (size_t) number_pixels)
355 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
356 if (number_pixels == 0)
357 ThrowReaderException(CoderError,"ImageColumnOrRowSizeIsNotSupported");
358 if ((viff_info.number_data_bands < 1) || (viff_info.number_data_bands > 4))
359 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
360 if ((viff_info.data_storage_type != VFF_TYP_BIT) &&
361 (viff_info.data_storage_type != VFF_TYP_1_BYTE) &&
362 (viff_info.data_storage_type != VFF_TYP_2_BYTE) &&
363 (viff_info.data_storage_type != VFF_TYP_4_BYTE) &&
364 (viff_info.data_storage_type != VFF_TYP_FLOAT) &&
365 (viff_info.data_storage_type != VFF_TYP_DOUBLE))
366 ThrowReaderException(CoderError,"DataStorageTypeIsNotSupported");
367 if (viff_info.data_encode_scheme != VFF_DES_RAW)
368 ThrowReaderException(CoderError,"DataEncodingSchemeIsNotSupported");
369 if ((viff_info.map_storage_type != VFF_MAPTYP_NONE) &&
370 (viff_info.map_storage_type != VFF_MAPTYP_1_BYTE) &&
371 (viff_info.map_storage_type != VFF_MAPTYP_2_BYTE) &&
372 (viff_info.map_storage_type != VFF_MAPTYP_4_BYTE) &&
373 (viff_info.map_storage_type != VFF_MAPTYP_FLOAT) &&
374 (viff_info.map_storage_type != VFF_MAPTYP_DOUBLE))
375 ThrowReaderException(CoderError,"MapStorageTypeIsNotSupported");
376 if ((viff_info.color_space_model != VFF_CM_NONE) &&
377 (viff_info.color_space_model != VFF_CM_ntscRGB) &&
378 (viff_info.color_space_model != VFF_CM_genericRGB))
379 ThrowReaderException(CoderError,"ColorspaceModelIsNotSupported");
380 if (viff_info.location_type != VFF_LOC_IMPLICIT)
381 ThrowReaderException(CoderError,"LocationTypeIsNotSupported");
382 if (viff_info.number_of_images != 1)
383 ThrowReaderException(CoderError,"NumberOfImagesIsNotSupported");
384 if (viff_info.map_rows == 0)
385 viff_info.map_scheme=VFF_MS_NONE;
386 switch ((int) viff_info.map_scheme)
390 if (viff_info.number_data_bands < 3)
393 Create linear color ramp.
395 image->colors=image->depth <= 8 ? 256UL : 65536UL;
396 if (viff_info.data_storage_type == VFF_TYP_BIT)
398 if (AcquireImageColormap(image,image->colors) == MagickFalse)
399 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
403 case VFF_MS_ONEPERBAND:
410 Allocate VIFF colormap.
412 switch ((int) viff_info.map_storage_type)
414 case VFF_MAPTYP_1_BYTE: bytes_per_pixel=1; break;
415 case VFF_MAPTYP_2_BYTE: bytes_per_pixel=2; break;
416 case VFF_MAPTYP_4_BYTE: bytes_per_pixel=4; break;
417 case VFF_MAPTYP_FLOAT: bytes_per_pixel=4; break;
418 case VFF_MAPTYP_DOUBLE: bytes_per_pixel=8; break;
419 default: bytes_per_pixel=1; break;
421 image->colors=viff_info.map_columns;
422 if (AcquireImageColormap(image,image->colors) == MagickFalse)
423 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
424 viff_colormap=(unsigned char *) AcquireQuantumMemory(image->colors,
425 viff_info.map_rows*bytes_per_pixel*sizeof(*viff_colormap));
426 if (viff_colormap == (unsigned char *) NULL)
427 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
429 Read VIFF raster colormap.
431 count=ReadBlob(image,bytes_per_pixel*image->colors*viff_info.map_rows,
434 if (*(char *) &lsb_first &&
435 ((viff_info.machine_dependency != VFF_DEP_DECORDER) &&
436 (viff_info.machine_dependency != VFF_DEP_NSORDER)))
437 switch ((int) viff_info.map_storage_type)
439 case VFF_MAPTYP_2_BYTE:
441 MSBOrderShort(viff_colormap,(bytes_per_pixel*image->colors*
442 viff_info.map_rows));
445 case VFF_MAPTYP_4_BYTE:
446 case VFF_MAPTYP_FLOAT:
448 MSBOrderLong(viff_colormap,(bytes_per_pixel*image->colors*
449 viff_info.map_rows));
454 for (i=0; i < (long) (viff_info.map_rows*image->colors); i++)
456 switch ((int) viff_info.map_storage_type)
458 case VFF_MAPTYP_2_BYTE: value=1.0*((short *) viff_colormap)[i]; break;
459 case VFF_MAPTYP_4_BYTE: value=1.0*((int *) viff_colormap)[i]; break;
460 case VFF_MAPTYP_FLOAT: value=((float *) viff_colormap)[i]; break;
461 case VFF_MAPTYP_DOUBLE: value=((double *) viff_colormap)[i]; break;
462 default: value=1.0*viff_colormap[i]; break;
464 if (i < (long) image->colors)
466 image->colormap[i].red=ScaleCharToQuantum((unsigned char) value);
467 image->colormap[i].green=
468 ScaleCharToQuantum((unsigned char) value);
469 image->colormap[i].blue=ScaleCharToQuantum((unsigned char) value);
472 if (i < (long) (2*image->colors))
473 image->colormap[i % image->colors].green=
474 ScaleCharToQuantum((unsigned char) value);
476 if (i < (long) (3*image->colors))
477 image->colormap[i % image->colors].blue=
478 ScaleCharToQuantum((unsigned char) value);
480 viff_colormap=(unsigned char *) RelinquishMagickMemory(viff_colormap);
484 ThrowReaderException(CoderError,"ColormapTypeNotSupported");
487 Initialize image structure.
489 image->matte=viff_info.number_data_bands == 4 ? MagickTrue : MagickFalse;
490 image->storage_class=
491 (viff_info.number_data_bands < 3 ? PseudoClass : DirectClass);
492 image->columns=viff_info.rows;
493 image->rows=viff_info.columns;
494 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
495 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
498 Allocate VIFF pixels.
500 switch ((int) viff_info.data_storage_type)
502 case VFF_TYP_2_BYTE: bytes_per_pixel=2; break;
503 case VFF_TYP_4_BYTE: bytes_per_pixel=4; break;
504 case VFF_TYP_FLOAT: bytes_per_pixel=4; break;
505 case VFF_TYP_DOUBLE: bytes_per_pixel=8; break;
506 default: bytes_per_pixel=1; break;
508 if (viff_info.data_storage_type == VFF_TYP_BIT)
509 max_packets=((image->columns+7UL) >> 3UL)*image->rows;
511 max_packets=(unsigned long) (number_pixels*viff_info.number_data_bands);
512 viff_pixels=(unsigned char *) AcquireQuantumMemory(max_packets,
513 bytes_per_pixel*sizeof(*viff_pixels));
514 if (viff_pixels == (unsigned char *) NULL)
515 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
516 count=ReadBlob(image,bytes_per_pixel*max_packets,viff_pixels);
518 if (*(char *) &lsb_first &&
519 ((viff_info.machine_dependency != VFF_DEP_DECORDER) &&
520 (viff_info.machine_dependency != VFF_DEP_NSORDER)))
521 switch ((int) viff_info.data_storage_type)
525 MSBOrderShort(viff_pixels,bytes_per_pixel*max_packets);
531 MSBOrderLong(viff_pixels,bytes_per_pixel*max_packets);
538 if ((viff_info.data_storage_type != VFF_TYP_1_BYTE) &&
539 (viff_info.map_scheme == VFF_MS_NONE))
545 Determine scale factor.
547 switch ((int) viff_info.data_storage_type)
549 case VFF_TYP_2_BYTE: value=1.0*((short *) viff_pixels)[0]; break;
550 case VFF_TYP_4_BYTE: value=1.0*((int *) viff_pixels)[0]; break;
551 case VFF_TYP_FLOAT: value=((float *) viff_pixels)[0]; break;
552 case VFF_TYP_DOUBLE: value=((double *) viff_pixels)[0]; break;
553 default: value=1.0*viff_pixels[0]; break;
557 for (i=0; i < (long) max_packets; i++)
559 switch ((int) viff_info.data_storage_type)
561 case VFF_TYP_2_BYTE: value=1.0*((short *) viff_pixels)[i]; break;
562 case VFF_TYP_4_BYTE: value=1.0*((int *) viff_pixels)[i]; break;
563 case VFF_TYP_FLOAT: value=((float *) viff_pixels)[i]; break;
564 case VFF_TYP_DOUBLE: value=((double *) viff_pixels)[i]; break;
565 default: value=1.0*viff_pixels[i]; break;
567 if (value > max_value)
570 if (value < min_value)
573 if ((min_value == 0) && (max_value == 0))
576 if (min_value == max_value)
578 scale_factor=(MagickRealType) QuantumRange/min_value;
582 scale_factor=(MagickRealType) QuantumRange/(max_value-min_value);
585 Convert pixels to Quantum size.
587 p=(unsigned char *) viff_pixels;
588 for (i=0; i < (long) max_packets; i++)
590 switch ((int) viff_info.data_storage_type)
592 case VFF_TYP_2_BYTE: value=1.0*((short *) viff_pixels)[i]; break;
593 case VFF_TYP_4_BYTE: value=1.0*((int *) viff_pixels)[i]; break;
594 case VFF_TYP_FLOAT: value=((float *) viff_pixels)[i]; break;
595 case VFF_TYP_DOUBLE: value=((double *) viff_pixels)[i]; break;
596 default: value=1.0*viff_pixels[i]; break;
598 if (viff_info.map_scheme == VFF_MS_NONE)
600 value=(value-min_value)*scale_factor;
601 if (value > QuantumRange)
607 *p=(unsigned char) value;
611 Convert VIFF raster image to pixel packets.
613 p=(unsigned char *) viff_pixels;
614 if (viff_info.data_storage_type == VFF_TYP_BIT)
617 Convert bitmap scanline.
619 (void) SetImageType(image,BilevelType);
620 (void) SetImageType(image,PaletteType);
621 for (y=0; y < (long) image->rows; y++)
623 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
624 if (q == (PixelPacket *) NULL)
626 indexes=GetAuthenticIndexQueue(image);
627 for (x=0; x < (long) (image->columns-7); x+=8)
629 for (bit=0; bit < 8; bit++)
630 if (PixelIntensity(q) < ((MagickRealType) QuantumRange/2.0))
632 quantum=(unsigned long) indexes[x+bit];
634 indexes[x+bit]=(IndexPacket) quantum;
638 if ((image->columns % 8) != 0)
640 for (bit=0; bit < (long) (image->columns % 8); bit++)
641 if (PixelIntensity(q) < ((MagickRealType) QuantumRange/2.0))
643 quantum=(unsigned long) indexes[x+bit];
645 indexes[x+bit]=(IndexPacket) quantum;
649 if (SyncAuthenticPixels(image,exception) == MagickFalse)
651 if (image->previous == (Image *) NULL)
653 status=SetImageProgress(image,LoadImageTag,y,image->rows);
654 if (status == MagickFalse)
660 if (image->storage_class == PseudoClass)
661 for (y=0; y < (long) image->rows; y++)
663 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
664 if (q == (PixelPacket *) NULL)
666 indexes=GetAuthenticIndexQueue(image);
667 for (x=0; x < (long) image->columns; x++)
668 indexes[x]=(IndexPacket) (*p++);
669 if (SyncAuthenticPixels(image,exception) == MagickFalse)
671 if (image->previous == (Image *) NULL)
673 status=SetImageProgress(image,LoadImageTag,y,image->rows);
674 if (status == MagickFalse)
681 Convert DirectColor scanline.
683 number_pixels=(MagickSizeType) image->columns*image->rows;
684 for (y=0; y < (long) image->rows; y++)
686 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
687 if (q == (PixelPacket *) NULL)
689 for (x=0; x < (long) image->columns; x++)
691 q->red=ScaleCharToQuantum(*p);
692 q->green=ScaleCharToQuantum(*(p+number_pixels));
693 q->blue=ScaleCharToQuantum(*(p+2*number_pixels));
694 if (image->colors != 0)
696 q->red=image->colormap[(long) q->red].red;
697 q->green=image->colormap[(long) q->green].green;
698 q->blue=image->colormap[(long) q->blue].blue;
700 q->opacity=(Quantum) (image->matte ? QuantumRange-
701 ScaleCharToQuantum(*(p+number_pixels*3)) : OpaqueOpacity);
705 if (SyncAuthenticPixels(image,exception) == MagickFalse)
707 if (image->previous == (Image *) NULL)
709 status=SetImageProgress(image,LoadImageTag,y,image->rows);
710 if (status == MagickFalse)
715 viff_pixels=(unsigned char *) RelinquishMagickMemory(viff_pixels);
716 if (image->storage_class == PseudoClass)
717 (void) SyncImage(image);
718 if (EOFBlob(image) != MagickFalse)
720 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
725 Proceed to next image.
727 if (image_info->number_scenes != 0)
728 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
730 count=ReadBlob(image,1,&viff_info.identifier);
731 if ((count != 0) && (viff_info.identifier == 0xab))
734 Allocate next image structure.
736 AcquireNextImage(image_info,image);
737 if (GetNextImageInList(image) == (Image *) NULL)
739 image=DestroyImageList(image);
740 return((Image *) NULL);
742 image=SyncNextImageInList(image);
743 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
745 if (status == MagickFalse)
748 } while ((count != 0) && (viff_info.identifier == 0xab));
749 (void) CloseBlob(image);
750 return(GetFirstImageInList(image));
754 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
758 % R e g i s t e r V I F F I m a g e %
762 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
764 % RegisterVIFFImage() adds properties for the VIFF image format to
765 % the list of supported formats. The properties include the image format
766 % tag, a method to read and/or write the format, whether the format
767 % supports the saving of more than one frame to the same file or blob,
768 % whether the format supports native in-memory I/O, and a brief
769 % description of the format.
771 % The format of the RegisterVIFFImage method is:
773 % unsigned long RegisterVIFFImage(void)
776 ModuleExport unsigned long RegisterVIFFImage(void)
781 entry=SetMagickInfo("VIFF");
782 entry->decoder=(DecodeImageHandler *) ReadVIFFImage;
783 entry->encoder=(EncodeImageHandler *) WriteVIFFImage;
784 entry->magick=(IsImageFormatHandler *) IsVIFF;
785 entry->description=ConstantString("Khoros Visualization image");
786 entry->module=ConstantString("VIFF");
787 (void) RegisterMagickInfo(entry);
788 entry=SetMagickInfo("XV");
789 entry->decoder=(DecodeImageHandler *) ReadVIFFImage;
790 entry->encoder=(EncodeImageHandler *) WriteVIFFImage;
791 entry->description=ConstantString("Khoros Visualization image");
792 entry->module=ConstantString("VIFF");
793 (void) RegisterMagickInfo(entry);
794 return(MagickImageCoderSignature);
798 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
802 % U n r e g i s t e r V I F F I m a g e %
806 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
808 % UnregisterVIFFImage() removes format registrations made by the
809 % VIFF module from the list of supported formats.
811 % The format of the UnregisterVIFFImage method is:
813 % UnregisterVIFFImage(void)
816 ModuleExport void UnregisterVIFFImage(void)
818 (void) UnregisterMagickInfo("VIFF");
819 (void) UnregisterMagickInfo("XV");
823 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
827 % W r i t e V I F F I m a g e %
831 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
833 % WriteVIFFImage() writes an image to a file in the VIFF image format.
835 % The format of the WriteVIFFImage method is:
837 % MagickBooleanType WriteVIFFImage(const ImageInfo *image_info,
840 % A description of each parameter follows.
842 % o image_info: the image info.
844 % o image: The image.
848 static inline size_t MagickMin(const size_t x,const size_t y)
855 static MagickBooleanType WriteVIFFImage(const ImageInfo *image_info,
858 #define VFF_CM_genericRGB 15
859 #define VFF_CM_NONE 0
860 #define VFF_DEP_IEEEORDER 0x2
861 #define VFF_DES_RAW 0
862 #define VFF_LOC_IMPLICIT 1
863 #define VFF_MAPTYP_NONE 0
864 #define VFF_MAPTYP_1_BYTE 1
865 #define VFF_MS_NONE 0
866 #define VFF_MS_ONEPERBAND 1
867 #define VFF_TYP_BIT 0
868 #define VFF_TYP_1_BYTE 1
870 typedef struct _ViffInfo
927 register const IndexPacket
930 register const PixelPacket
939 register unsigned char
950 Open output image file.
952 assert(image_info != (const ImageInfo *) NULL);
953 assert(image_info->signature == MagickSignature);
954 assert(image != (Image *) NULL);
955 assert(image->signature == MagickSignature);
956 if (image->debug != MagickFalse)
957 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
958 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
959 if (status == MagickFalse)
961 (void) ResetMagickMemory(&viff_info,0,sizeof(ViffInfo));
966 Initialize VIFF image structure.
968 if (image->colorspace != RGBColorspace)
969 (void) TransformImageColorspace(image,RGBColorspace);
970 if (IsGrayImage(image,&image->exception) != MagickFalse)
971 (void) SetImageStorageClass(image,DirectClass);
972 viff_info.identifier=(char) 0xab;
973 viff_info.file_type=1;
976 viff_info.machine_dependency=VFF_DEP_IEEEORDER; /* IEEE byte ordering */
977 *viff_info.comment='\0';
978 value=GetImageProperty(image,"comment");
979 if (value != (const char *) NULL)
980 (void) CopyMagickString(viff_info.comment,value,MagickMin(strlen(value),511)+1);
981 viff_info.rows=image->columns;
982 viff_info.columns=image->rows;
984 viff_info.x_offset=(~0);
985 viff_info.y_offset=(~0);
986 viff_info.x_bits_per_pixel=0;
987 viff_info.y_bits_per_pixel=0;
988 viff_info.location_type=VFF_LOC_IMPLICIT;
989 viff_info.location_dimension=0;
990 viff_info.number_of_images=1;
991 viff_info.data_encode_scheme=VFF_DES_RAW;
992 viff_info.map_scheme=VFF_MS_NONE;
993 viff_info.map_storage_type=VFF_MAPTYP_NONE;
994 viff_info.map_rows=0;
995 viff_info.map_columns=0;
996 viff_info.map_subrows=0;
997 viff_info.map_enable=1; /* no colormap */
998 viff_info.maps_per_cycle=0;
999 number_pixels=(MagickSizeType) image->columns*image->rows;
1000 if (image->storage_class == DirectClass)
1003 Full color VIFF raster.
1005 viff_info.number_data_bands=image->matte ? 4UL : 3UL;
1006 viff_info.color_space_model=VFF_CM_genericRGB;
1007 viff_info.data_storage_type=VFF_TYP_1_BYTE;
1008 packets=viff_info.number_data_bands*number_pixels;
1012 viff_info.number_data_bands=1;
1013 viff_info.color_space_model=VFF_CM_NONE;
1014 viff_info.data_storage_type=VFF_TYP_1_BYTE;
1015 packets=number_pixels;
1016 if (IsGrayImage(image,&image->exception) == MagickFalse)
1019 Colormapped VIFF raster.
1021 viff_info.map_scheme=VFF_MS_ONEPERBAND;
1022 viff_info.map_storage_type=VFF_MAPTYP_1_BYTE;
1023 viff_info.map_rows=3;
1024 viff_info.map_columns=image->colors;
1027 if (image->colors <= 2)
1030 Monochrome VIFF raster.
1032 viff_info.data_storage_type=VFF_TYP_BIT;
1033 packets=((image->columns+7) >> 3)*image->rows;
1037 Write VIFF image header (pad to 1024 bytes).
1039 buffer[0]=(unsigned char) viff_info.identifier;
1040 buffer[1]=(unsigned char) viff_info.file_type;
1041 buffer[2]=(unsigned char) viff_info.release;
1042 buffer[3]=(unsigned char) viff_info.version;
1043 buffer[4]=(unsigned char) viff_info.machine_dependency;
1044 buffer[5]=(unsigned char) viff_info.reserve[0];
1045 buffer[6]=(unsigned char) viff_info.reserve[1];
1046 buffer[7]=(unsigned char) viff_info.reserve[2];
1047 (void) WriteBlob(image,8,buffer);
1048 (void) WriteBlob(image,512,(unsigned char *) viff_info.comment);
1049 (void) WriteBlobMSBLong(image,viff_info.rows);
1050 (void) WriteBlobMSBLong(image,viff_info.columns);
1051 (void) WriteBlobMSBLong(image,viff_info.subrows);
1052 (void) WriteBlobMSBLong(image,(unsigned long) viff_info.x_offset);
1053 (void) WriteBlobMSBLong(image,(unsigned long) viff_info.y_offset);
1054 viff_info.x_bits_per_pixel=1U*(63 << 24) | (128 << 16);
1055 (void) WriteBlobMSBLong(image,(unsigned long) viff_info.x_bits_per_pixel);
1056 viff_info.y_bits_per_pixel=1U*(63 << 24) | (128 << 16);
1057 (void) WriteBlobMSBLong(image,(unsigned long) viff_info.y_bits_per_pixel);
1058 (void) WriteBlobMSBLong(image,viff_info.location_type);
1059 (void) WriteBlobMSBLong(image,viff_info.location_dimension);
1060 (void) WriteBlobMSBLong(image,viff_info.number_of_images);
1061 (void) WriteBlobMSBLong(image,viff_info.number_data_bands);
1062 (void) WriteBlobMSBLong(image,viff_info.data_storage_type);
1063 (void) WriteBlobMSBLong(image,viff_info.data_encode_scheme);
1064 (void) WriteBlobMSBLong(image,viff_info.map_scheme);
1065 (void) WriteBlobMSBLong(image,viff_info.map_storage_type);
1066 (void) WriteBlobMSBLong(image,viff_info.map_rows);
1067 (void) WriteBlobMSBLong(image,viff_info.map_columns);
1068 (void) WriteBlobMSBLong(image,viff_info.map_subrows);
1069 (void) WriteBlobMSBLong(image,viff_info.map_enable);
1070 (void) WriteBlobMSBLong(image,viff_info.maps_per_cycle);
1071 (void) WriteBlobMSBLong(image,viff_info.color_space_model);
1072 for (i=0; i < 420; i++)
1073 (void) WriteBlobByte(image,'\0');
1075 Convert MIFF to VIFF raster pixels.
1077 viff_pixels=(unsigned char *) AcquireQuantumMemory((size_t) packets,
1078 sizeof(*viff_pixels));
1079 if (viff_pixels == (unsigned char *) NULL)
1080 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1082 if (image->storage_class == DirectClass)
1085 Convert DirectClass packet to VIFF RGB pixel.
1087 number_pixels=(MagickSizeType) image->columns*image->rows;
1088 for (y=0; y < (long) image->rows; y++)
1090 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1091 if (p == (const PixelPacket *) NULL)
1093 for (x=0; x < (long) image->columns; x++)
1095 *q=ScaleQuantumToChar(p->red);
1096 *(q+number_pixels)=ScaleQuantumToChar(p->green);
1097 *(q+number_pixels*2)=ScaleQuantumToChar(p->blue);
1098 if (image->matte != MagickFalse)
1099 *(q+number_pixels*3)=ScaleQuantumToChar((Quantum)
1100 (QuantumRange-p->opacity));
1104 if (image->previous == (Image *) NULL)
1106 status=SetImageProgress(image,SaveImageTag,y,image->rows);
1107 if (status == MagickFalse)
1113 if (IsGrayImage(image,&image->exception) == MagickFalse)
1119 Dump colormap to file.
1121 viff_colormap=(unsigned char *) AcquireQuantumMemory(image->colors,
1122 3*sizeof(*viff_colormap));
1123 if (viff_colormap == (unsigned char *) NULL)
1124 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1126 for (i=0; i < (long) image->colors; i++)
1127 *q++=ScaleQuantumToChar(image->colormap[i].red);
1128 for (i=0; i < (long) image->colors; i++)
1129 *q++=ScaleQuantumToChar(image->colormap[i].green);
1130 for (i=0; i < (long) image->colors; i++)
1131 *q++=ScaleQuantumToChar(image->colormap[i].blue);
1132 (void) WriteBlob(image,3*image->colors,viff_colormap);
1133 viff_colormap=(unsigned char *) RelinquishMagickMemory(viff_colormap);
1135 Convert PseudoClass packet to VIFF colormapped pixels.
1138 for (y=0; y < (long) image->rows; y++)
1140 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1141 if (p == (const PixelPacket *) NULL)
1143 indexes=GetVirtualIndexQueue(image);
1144 for (x=0; x < (long) image->columns; x++)
1145 *q++=(unsigned char) indexes[x];
1146 if (image->previous == (Image *) NULL)
1148 status=SetImageProgress(image,SaveImageTag,y,image->rows);
1149 if (status == MagickFalse)
1155 if (image->colors <= 2)
1161 register unsigned char
1166 Convert PseudoClass image to a VIFF monochrome image.
1168 (void) SetImageType(image,BilevelType);
1169 for (y=0; y < (long) image->rows; y++)
1171 p=GetVirtualPixels(image,0,y,image->columns,1,
1173 if (p == (const PixelPacket *) NULL)
1175 indexes=GetVirtualIndexQueue(image);
1178 for (x=0; x < (long) image->columns; x++)
1181 if (PixelIntensity(p) < ((MagickRealType) QuantumRange/2.0))
1192 *q++=byte >> (8-bit);
1193 if (image->previous == (Image *) NULL)
1195 status=SetImageProgress(image,SaveImageTag,y,image->rows);
1196 if (status == MagickFalse)
1204 Convert PseudoClass packet to VIFF grayscale pixel.
1206 for (y=0; y < (long) image->rows; y++)
1208 p=GetVirtualPixels(image,0,y,image->columns,1,
1210 if (p == (const PixelPacket *) NULL)
1212 for (x=0; x < (long) image->columns; x++)
1214 *q++=(unsigned char) PixelIntensityToQuantum(p);
1217 if (image->previous == (Image *) NULL)
1219 status=SetImageProgress(image,SaveImageTag,y,image->rows);
1220 if (status == MagickFalse)
1225 (void) WriteBlob(image,(size_t) packets,viff_pixels);
1226 viff_pixels=(unsigned char *) RelinquishMagickMemory(viff_pixels);
1227 if (GetNextImageInList(image) == (Image *) NULL)
1229 image=SyncNextImageInList(image);
1230 status=SetImageProgress(image,SaveImagesTag,scene++,
1231 GetImageListLength(image));
1232 if (status == MagickFalse)
1234 } while (image_info->adjoin != MagickFalse);
1235 (void) CloseBlob(image);