2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6 % V V IIIII FFFFF FFFFF %
13 % Read/Write Khoros Visualization Image Format %
20 % Copyright 1999-2010 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/colormap.h"
49 #include "magick/colorspace.h"
50 #include "magick/exception.h"
51 #include "magick/exception-private.h"
52 #include "magick/image.h"
53 #include "magick/image-private.h"
54 #include "magick/list.h"
55 #include "magick/magick.h"
56 #include "magick/memory_.h"
57 #include "magick/monitor.h"
58 #include "magick/monitor-private.h"
59 #include "magick/property.h"
60 #include "magick/quantum-private.h"
61 #include "magick/static.h"
62 #include "magick/string_.h"
63 #include "magick/module.h"
68 static MagickBooleanType
69 WriteVIFFImage(const ImageInfo *,Image *);
72 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
80 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
82 % IsVIFF() returns MagickTrue if the image format type, identified by the
83 % magick string, is VIFF.
85 % The format of the IsVIFF method is:
87 % MagickBooleanType IsVIFF(const unsigned char *magick,const size_t length)
89 % A description of each parameter follows:
91 % o magick: compare image format pattern against these bytes.
93 % o length: Specifies the length of the magick string.
97 static MagickBooleanType IsVIFF(const unsigned char *magick,const size_t length)
101 if (memcmp(magick,"\253\001",2) == 0)
107 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
111 % R e a d V I F F I m a g e %
115 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
117 % ReadVIFFImage() reads a Khoros Visualization image file and returns
118 % it. It allocates the memory necessary for the new Image structure and
119 % returns a pointer to the new image.
121 % The format of the ReadVIFFImage method is:
123 % Image *ReadVIFFImage(const ImageInfo *image_info,
124 % ExceptionInfo *exception)
126 % A description of each parameter follows:
128 % o image: Method ReadVIFFImage returns a pointer to the image after
129 % reading. A null image is returned if there is a memory shortage or if
130 % the image cannot be read.
132 % o image_info: the image info.
134 % o exception: return any errors or warnings in this structure.
137 static Image *ReadVIFFImage(const ImageInfo *image_info,
138 ExceptionInfo *exception)
140 #define VFF_CM_genericRGB 15
141 #define VFF_CM_ntscRGB 1
142 #define VFF_CM_NONE 0
143 #define VFF_DEP_DECORDER 0x4
144 #define VFF_DEP_NSORDER 0x8
145 #define VFF_DES_RAW 0
146 #define VFF_LOC_IMPLICIT 1
147 #define VFF_MAPTYP_NONE 0
148 #define VFF_MAPTYP_1_BYTE 1
149 #define VFF_MAPTYP_2_BYTE 2
150 #define VFF_MAPTYP_4_BYTE 4
151 #define VFF_MAPTYP_FLOAT 5
152 #define VFF_MAPTYP_DOUBLE 7
153 #define VFF_MS_NONE 0
154 #define VFF_MS_ONEPERBAND 1
155 #define VFF_MS_SHARED 3
156 #define VFF_TYP_BIT 0
157 #define VFF_TYP_1_BYTE 1
158 #define VFF_TYP_2_BYTE 2
159 #define VFF_TYP_4_BYTE 4
160 #define VFF_TYP_FLOAT 5
161 #define VFF_TYP_DOUBLE 9
163 typedef struct _ViffInfo
238 register unsigned char
260 assert(image_info != (const ImageInfo *) NULL);
261 assert(image_info->signature == MagickSignature);
262 if (image_info->debug != MagickFalse)
263 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
264 image_info->filename);
265 assert(exception != (ExceptionInfo *) NULL);
266 assert(exception->signature == MagickSignature);
267 image=AcquireImage(image_info);
268 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
269 if (status == MagickFalse)
271 image=DestroyImageList(image);
272 return((Image *) NULL);
275 Read VIFF header (1024 bytes).
277 count=ReadBlob(image,1,&viff_info.identifier);
281 Verify VIFF identifier.
283 if ((count == 0) || ((unsigned char) viff_info.identifier != 0xab))
284 ThrowReaderException(CorruptImageError,"NotAVIFFImage");
286 Initialize VIFF image.
288 count=ReadBlob(image,7,buffer);
289 viff_info.file_type=buffer[0];
290 viff_info.release=buffer[1];
291 viff_info.version=buffer[2];
292 viff_info.machine_dependency=buffer[3];
293 count=ReadBlob(image,512,(unsigned char *) viff_info.comment);
294 viff_info.comment[511]='\0';
295 if (strlen(viff_info.comment) > 4)
296 (void) SetImageProperty(image,"comment",viff_info.comment);
297 if ((viff_info.machine_dependency == VFF_DEP_DECORDER) ||
298 (viff_info.machine_dependency == VFF_DEP_NSORDER))
300 viff_info.rows=ReadBlobLSBLong(image);
301 viff_info.columns=ReadBlobLSBLong(image);
302 viff_info.subrows=ReadBlobLSBLong(image);
303 viff_info.x_offset=(ssize_t) ReadBlobLSBLong(image);
304 viff_info.y_offset=(ssize_t) ReadBlobLSBLong(image);
305 viff_info.x_bits_per_pixel=(float) ReadBlobLSBLong(image);
306 viff_info.y_bits_per_pixel=(float) ReadBlobLSBLong(image);
307 viff_info.location_type=ReadBlobLSBLong(image);
308 viff_info.location_dimension=ReadBlobLSBLong(image);
309 viff_info.number_of_images=ReadBlobLSBLong(image);
310 viff_info.number_data_bands=ReadBlobLSBLong(image);
311 viff_info.data_storage_type=ReadBlobLSBLong(image);
312 viff_info.data_encode_scheme=ReadBlobLSBLong(image);
313 viff_info.map_scheme=ReadBlobLSBLong(image);
314 viff_info.map_storage_type=ReadBlobLSBLong(image);
315 viff_info.map_rows=ReadBlobLSBLong(image);
316 viff_info.map_columns=ReadBlobLSBLong(image);
317 viff_info.map_subrows=ReadBlobLSBLong(image);
318 viff_info.map_enable=ReadBlobLSBLong(image);
319 viff_info.maps_per_cycle=ReadBlobLSBLong(image);
320 viff_info.color_space_model=ReadBlobLSBLong(image);
324 viff_info.rows=ReadBlobMSBLong(image);
325 viff_info.columns=ReadBlobMSBLong(image);
326 viff_info.subrows=ReadBlobMSBLong(image);
327 viff_info.x_offset=(ssize_t) ReadBlobMSBLong(image);
328 viff_info.y_offset=(ssize_t) ReadBlobMSBLong(image);
329 viff_info.x_bits_per_pixel=(float) ReadBlobMSBLong(image);
330 viff_info.y_bits_per_pixel=(float) ReadBlobMSBLong(image);
331 viff_info.location_type=ReadBlobMSBLong(image);
332 viff_info.location_dimension=ReadBlobMSBLong(image);
333 viff_info.number_of_images=ReadBlobMSBLong(image);
334 viff_info.number_data_bands=ReadBlobMSBLong(image);
335 viff_info.data_storage_type=ReadBlobMSBLong(image);
336 viff_info.data_encode_scheme=ReadBlobMSBLong(image);
337 viff_info.map_scheme=ReadBlobMSBLong(image);
338 viff_info.map_storage_type=ReadBlobMSBLong(image);
339 viff_info.map_rows=ReadBlobMSBLong(image);
340 viff_info.map_columns=ReadBlobMSBLong(image);
341 viff_info.map_subrows=ReadBlobMSBLong(image);
342 viff_info.map_enable=ReadBlobMSBLong(image);
343 viff_info.maps_per_cycle=ReadBlobMSBLong(image);
344 viff_info.color_space_model=ReadBlobMSBLong(image);
346 for (i=0; i < 420; i++)
347 (void) ReadBlobByte(image);
348 image->columns=viff_info.rows;
349 image->rows=viff_info.columns;
350 image->depth=viff_info.x_bits_per_pixel <= 8 ? 8UL : MAGICKCORE_QUANTUM_DEPTH;
352 Verify that we can read this VIFF image.
354 number_pixels=(MagickSizeType) viff_info.columns*viff_info.rows;
355 if (number_pixels != (size_t) number_pixels)
356 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
357 if (number_pixels == 0)
358 ThrowReaderException(CoderError,"ImageColumnOrRowSizeIsNotSupported");
359 if ((viff_info.number_data_bands < 1) || (viff_info.number_data_bands > 4))
360 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
361 if ((viff_info.data_storage_type != VFF_TYP_BIT) &&
362 (viff_info.data_storage_type != VFF_TYP_1_BYTE) &&
363 (viff_info.data_storage_type != VFF_TYP_2_BYTE) &&
364 (viff_info.data_storage_type != VFF_TYP_4_BYTE) &&
365 (viff_info.data_storage_type != VFF_TYP_FLOAT) &&
366 (viff_info.data_storage_type != VFF_TYP_DOUBLE))
367 ThrowReaderException(CoderError,"DataStorageTypeIsNotSupported");
368 if (viff_info.data_encode_scheme != VFF_DES_RAW)
369 ThrowReaderException(CoderError,"DataEncodingSchemeIsNotSupported");
370 if ((viff_info.map_storage_type != VFF_MAPTYP_NONE) &&
371 (viff_info.map_storage_type != VFF_MAPTYP_1_BYTE) &&
372 (viff_info.map_storage_type != VFF_MAPTYP_2_BYTE) &&
373 (viff_info.map_storage_type != VFF_MAPTYP_4_BYTE) &&
374 (viff_info.map_storage_type != VFF_MAPTYP_FLOAT) &&
375 (viff_info.map_storage_type != VFF_MAPTYP_DOUBLE))
376 ThrowReaderException(CoderError,"MapStorageTypeIsNotSupported");
377 if ((viff_info.color_space_model != VFF_CM_NONE) &&
378 (viff_info.color_space_model != VFF_CM_ntscRGB) &&
379 (viff_info.color_space_model != VFF_CM_genericRGB))
380 ThrowReaderException(CoderError,"ColorspaceModelIsNotSupported");
381 if (viff_info.location_type != VFF_LOC_IMPLICIT)
382 ThrowReaderException(CoderError,"LocationTypeIsNotSupported");
383 if (viff_info.number_of_images != 1)
384 ThrowReaderException(CoderError,"NumberOfImagesIsNotSupported");
385 if (viff_info.map_rows == 0)
386 viff_info.map_scheme=VFF_MS_NONE;
387 switch ((int) viff_info.map_scheme)
391 if (viff_info.number_data_bands < 3)
394 Create linear color ramp.
396 image->colors=image->depth <= 8 ? 256UL : 65536UL;
397 if (viff_info.data_storage_type == VFF_TYP_BIT)
399 if (AcquireImageColormap(image,image->colors) == MagickFalse)
400 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
404 case VFF_MS_ONEPERBAND:
411 Allocate VIFF colormap.
413 switch ((int) viff_info.map_storage_type)
415 case VFF_MAPTYP_1_BYTE: bytes_per_pixel=1; break;
416 case VFF_MAPTYP_2_BYTE: bytes_per_pixel=2; break;
417 case VFF_MAPTYP_4_BYTE: bytes_per_pixel=4; break;
418 case VFF_MAPTYP_FLOAT: bytes_per_pixel=4; break;
419 case VFF_MAPTYP_DOUBLE: bytes_per_pixel=8; break;
420 default: bytes_per_pixel=1; break;
422 image->colors=viff_info.map_columns;
423 if (AcquireImageColormap(image,image->colors) == MagickFalse)
424 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
425 viff_colormap=(unsigned char *) AcquireQuantumMemory(image->colors,
426 viff_info.map_rows*bytes_per_pixel*sizeof(*viff_colormap));
427 if (viff_colormap == (unsigned char *) NULL)
428 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
430 Read VIFF raster colormap.
432 count=ReadBlob(image,bytes_per_pixel*image->colors*viff_info.map_rows,
435 if (*(char *) &lsb_first &&
436 ((viff_info.machine_dependency != VFF_DEP_DECORDER) &&
437 (viff_info.machine_dependency != VFF_DEP_NSORDER)))
438 switch ((int) viff_info.map_storage_type)
440 case VFF_MAPTYP_2_BYTE:
442 MSBOrderShort(viff_colormap,(bytes_per_pixel*image->colors*
443 viff_info.map_rows));
446 case VFF_MAPTYP_4_BYTE:
447 case VFF_MAPTYP_FLOAT:
449 MSBOrderLong(viff_colormap,(bytes_per_pixel*image->colors*
450 viff_info.map_rows));
455 for (i=0; i < (ssize_t) (viff_info.map_rows*image->colors); i++)
457 switch ((int) viff_info.map_storage_type)
459 case VFF_MAPTYP_2_BYTE: value=1.0*((short *) viff_colormap)[i]; break;
460 case VFF_MAPTYP_4_BYTE: value=1.0*((int *) viff_colormap)[i]; break;
461 case VFF_MAPTYP_FLOAT: value=((float *) viff_colormap)[i]; break;
462 case VFF_MAPTYP_DOUBLE: value=((double *) viff_colormap)[i]; break;
463 default: value=1.0*viff_colormap[i]; break;
465 if (i < (ssize_t) image->colors)
467 image->colormap[i].red=ScaleCharToQuantum((unsigned char) value);
468 image->colormap[i].green=
469 ScaleCharToQuantum((unsigned char) value);
470 image->colormap[i].blue=ScaleCharToQuantum((unsigned char) value);
473 if (i < (ssize_t) (2*image->colors))
474 image->colormap[i % image->colors].green=
475 ScaleCharToQuantum((unsigned char) value);
477 if (i < (ssize_t) (3*image->colors))
478 image->colormap[i % image->colors].blue=
479 ScaleCharToQuantum((unsigned char) value);
481 viff_colormap=(unsigned char *) RelinquishMagickMemory(viff_colormap);
485 ThrowReaderException(CoderError,"ColormapTypeNotSupported");
488 Initialize image structure.
490 image->matte=viff_info.number_data_bands == 4 ? MagickTrue : MagickFalse;
491 image->storage_class=
492 (viff_info.number_data_bands < 3 ? PseudoClass : DirectClass);
493 image->columns=viff_info.rows;
494 image->rows=viff_info.columns;
495 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
496 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
499 Allocate VIFF pixels.
501 switch ((int) viff_info.data_storage_type)
503 case VFF_TYP_2_BYTE: bytes_per_pixel=2; break;
504 case VFF_TYP_4_BYTE: bytes_per_pixel=4; break;
505 case VFF_TYP_FLOAT: bytes_per_pixel=4; break;
506 case VFF_TYP_DOUBLE: bytes_per_pixel=8; break;
507 default: bytes_per_pixel=1; break;
509 if (viff_info.data_storage_type == VFF_TYP_BIT)
510 max_packets=((image->columns+7UL) >> 3UL)*image->rows;
512 max_packets=(size_t) (number_pixels*viff_info.number_data_bands);
513 viff_pixels=(unsigned char *) AcquireQuantumMemory(max_packets,
514 bytes_per_pixel*sizeof(*viff_pixels));
515 if (viff_pixels == (unsigned char *) NULL)
516 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
517 count=ReadBlob(image,bytes_per_pixel*max_packets,viff_pixels);
519 if (*(char *) &lsb_first &&
520 ((viff_info.machine_dependency != VFF_DEP_DECORDER) &&
521 (viff_info.machine_dependency != VFF_DEP_NSORDER)))
522 switch ((int) viff_info.data_storage_type)
526 MSBOrderShort(viff_pixels,bytes_per_pixel*max_packets);
532 MSBOrderLong(viff_pixels,bytes_per_pixel*max_packets);
539 if ((viff_info.data_storage_type != VFF_TYP_1_BYTE) &&
540 (viff_info.map_scheme == VFF_MS_NONE))
546 Determine scale factor.
548 switch ((int) viff_info.data_storage_type)
550 case VFF_TYP_2_BYTE: value=1.0*((short *) viff_pixels)[0]; break;
551 case VFF_TYP_4_BYTE: value=1.0*((int *) viff_pixels)[0]; break;
552 case VFF_TYP_FLOAT: value=((float *) viff_pixels)[0]; break;
553 case VFF_TYP_DOUBLE: value=((double *) viff_pixels)[0]; break;
554 default: value=1.0*viff_pixels[0]; break;
558 for (i=0; i < (ssize_t) max_packets; i++)
560 switch ((int) viff_info.data_storage_type)
562 case VFF_TYP_2_BYTE: value=1.0*((short *) viff_pixels)[i]; break;
563 case VFF_TYP_4_BYTE: value=1.0*((int *) viff_pixels)[i]; break;
564 case VFF_TYP_FLOAT: value=((float *) viff_pixels)[i]; break;
565 case VFF_TYP_DOUBLE: value=((double *) viff_pixels)[i]; break;
566 default: value=1.0*viff_pixels[i]; break;
568 if (value > max_value)
571 if (value < min_value)
574 if ((min_value == 0) && (max_value == 0))
577 if (min_value == max_value)
579 scale_factor=(MagickRealType) QuantumRange/min_value;
583 scale_factor=(MagickRealType) QuantumRange/(max_value-min_value);
586 Convert pixels to Quantum size.
588 p=(unsigned char *) viff_pixels;
589 for (i=0; i < (ssize_t) max_packets; i++)
591 switch ((int) viff_info.data_storage_type)
593 case VFF_TYP_2_BYTE: value=1.0*((short *) viff_pixels)[i]; break;
594 case VFF_TYP_4_BYTE: value=1.0*((int *) viff_pixels)[i]; break;
595 case VFF_TYP_FLOAT: value=((float *) viff_pixels)[i]; break;
596 case VFF_TYP_DOUBLE: value=((double *) viff_pixels)[i]; break;
597 default: value=1.0*viff_pixels[i]; break;
599 if (viff_info.map_scheme == VFF_MS_NONE)
601 value=(value-min_value)*scale_factor;
602 if (value > QuantumRange)
608 *p=(unsigned char) value;
612 Convert VIFF raster image to pixel packets.
614 p=(unsigned char *) viff_pixels;
615 if (viff_info.data_storage_type == VFF_TYP_BIT)
618 Convert bitmap scanline.
620 (void) SetImageType(image,BilevelType);
621 (void) SetImageType(image,PaletteType);
622 for (y=0; y < (ssize_t) image->rows; y++)
624 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
625 if (q == (PixelPacket *) NULL)
627 indexes=GetAuthenticIndexQueue(image);
628 for (x=0; x < (ssize_t) (image->columns-7); x+=8)
630 for (bit=0; bit < 8; bit++)
631 if (PixelIntensity(q) < ((MagickRealType) QuantumRange/2.0))
633 quantum=(size_t) indexes[x+bit];
635 indexes[x+bit]=(IndexPacket) quantum;
639 if ((image->columns % 8) != 0)
641 for (bit=0; bit < (ssize_t) (image->columns % 8); bit++)
642 if (PixelIntensity(q) < ((MagickRealType) QuantumRange/2.0))
644 quantum=(size_t) indexes[x+bit];
646 indexes[x+bit]=(IndexPacket) quantum;
650 if (SyncAuthenticPixels(image,exception) == MagickFalse)
652 if (image->previous == (Image *) NULL)
654 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
656 if (status == MagickFalse)
662 if (image->storage_class == PseudoClass)
663 for (y=0; y < (ssize_t) image->rows; y++)
665 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
666 if (q == (PixelPacket *) NULL)
668 indexes=GetAuthenticIndexQueue(image);
669 for (x=0; x < (ssize_t) image->columns; x++)
670 indexes[x]=(IndexPacket) (*p++);
671 if (SyncAuthenticPixels(image,exception) == MagickFalse)
673 if (image->previous == (Image *) NULL)
675 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
677 if (status == MagickFalse)
684 Convert DirectColor scanline.
686 number_pixels=(MagickSizeType) image->columns*image->rows;
687 for (y=0; y < (ssize_t) image->rows; y++)
689 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
690 if (q == (PixelPacket *) NULL)
692 for (x=0; x < (ssize_t) image->columns; x++)
694 q->red=ScaleCharToQuantum(*p);
695 q->green=ScaleCharToQuantum(*(p+number_pixels));
696 q->blue=ScaleCharToQuantum(*(p+2*number_pixels));
697 if (image->colors != 0)
699 q->red=image->colormap[(ssize_t) q->red].red;
700 q->green=image->colormap[(ssize_t) q->green].green;
701 q->blue=image->colormap[(ssize_t) q->blue].blue;
703 q->opacity=(Quantum) (image->matte ? QuantumRange-
704 ScaleCharToQuantum(*(p+number_pixels*3)) : OpaqueOpacity);
708 if (SyncAuthenticPixels(image,exception) == MagickFalse)
710 if (image->previous == (Image *) NULL)
712 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
714 if (status == MagickFalse)
719 viff_pixels=(unsigned char *) RelinquishMagickMemory(viff_pixels);
720 if (image->storage_class == PseudoClass)
721 (void) SyncImage(image);
722 if (EOFBlob(image) != MagickFalse)
724 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
729 Proceed to next image.
731 if (image_info->number_scenes != 0)
732 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
734 count=ReadBlob(image,1,&viff_info.identifier);
735 if ((count != 0) && (viff_info.identifier == 0xab))
738 Allocate next image structure.
740 AcquireNextImage(image_info,image);
741 if (GetNextImageInList(image) == (Image *) NULL)
743 image=DestroyImageList(image);
744 return((Image *) NULL);
746 image=SyncNextImageInList(image);
747 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
749 if (status == MagickFalse)
752 } while ((count != 0) && (viff_info.identifier == 0xab));
753 (void) CloseBlob(image);
754 return(GetFirstImageInList(image));
758 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
762 % R e g i s t e r V I F F I m a g e %
766 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
768 % RegisterVIFFImage() adds properties for the VIFF image format to
769 % the list of supported formats. The properties include the image format
770 % tag, a method to read and/or write the format, whether the format
771 % supports the saving of more than one frame to the same file or blob,
772 % whether the format supports native in-memory I/O, and a brief
773 % description of the format.
775 % The format of the RegisterVIFFImage method is:
777 % size_t RegisterVIFFImage(void)
780 ModuleExport size_t RegisterVIFFImage(void)
785 entry=SetMagickInfo("VIFF");
786 entry->decoder=(DecodeImageHandler *) ReadVIFFImage;
787 entry->encoder=(EncodeImageHandler *) WriteVIFFImage;
788 entry->magick=(IsImageFormatHandler *) IsVIFF;
789 entry->description=ConstantString("Khoros Visualization image");
790 entry->module=ConstantString("VIFF");
791 (void) RegisterMagickInfo(entry);
792 entry=SetMagickInfo("XV");
793 entry->decoder=(DecodeImageHandler *) ReadVIFFImage;
794 entry->encoder=(EncodeImageHandler *) WriteVIFFImage;
795 entry->description=ConstantString("Khoros Visualization image");
796 entry->module=ConstantString("VIFF");
797 (void) RegisterMagickInfo(entry);
798 return(MagickImageCoderSignature);
802 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
806 % U n r e g i s t e r V I F F I m a g e %
810 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
812 % UnregisterVIFFImage() removes format registrations made by the
813 % VIFF module from the list of supported formats.
815 % The format of the UnregisterVIFFImage method is:
817 % UnregisterVIFFImage(void)
820 ModuleExport void UnregisterVIFFImage(void)
822 (void) UnregisterMagickInfo("VIFF");
823 (void) UnregisterMagickInfo("XV");
827 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
831 % W r i t e V I F F I m a g e %
835 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
837 % WriteVIFFImage() writes an image to a file in the VIFF image format.
839 % The format of the WriteVIFFImage method is:
841 % MagickBooleanType WriteVIFFImage(const ImageInfo *image_info,
844 % A description of each parameter follows.
846 % o image_info: the image info.
848 % o image: The image.
852 static inline size_t MagickMin(const size_t x,const size_t y)
859 static MagickBooleanType WriteVIFFImage(const ImageInfo *image_info,
862 #define VFF_CM_genericRGB 15
863 #define VFF_CM_NONE 0
864 #define VFF_DEP_IEEEORDER 0x2
865 #define VFF_DES_RAW 0
866 #define VFF_LOC_IMPLICIT 1
867 #define VFF_MAPTYP_NONE 0
868 #define VFF_MAPTYP_1_BYTE 1
869 #define VFF_MS_NONE 0
870 #define VFF_MS_ONEPERBAND 1
871 #define VFF_TYP_BIT 0
872 #define VFF_TYP_1_BYTE 1
874 typedef struct _ViffInfo
929 register const IndexPacket
932 register const PixelPacket
941 register unsigned char
952 Open output image file.
954 assert(image_info != (const ImageInfo *) NULL);
955 assert(image_info->signature == MagickSignature);
956 assert(image != (Image *) NULL);
957 assert(image->signature == MagickSignature);
958 if (image->debug != MagickFalse)
959 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
960 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
961 if (status == MagickFalse)
963 (void) ResetMagickMemory(&viff_info,0,sizeof(ViffInfo));
968 Initialize VIFF image structure.
970 if (image->colorspace != RGBColorspace)
971 (void) TransformImageColorspace(image,RGBColorspace);
972 if (IsGrayImage(image,&image->exception) != MagickFalse)
973 (void) SetImageStorageClass(image,DirectClass);
974 viff_info.identifier=(char) 0xab;
975 viff_info.file_type=1;
978 viff_info.machine_dependency=VFF_DEP_IEEEORDER; /* IEEE byte ordering */
979 *viff_info.comment='\0';
980 value=GetImageProperty(image,"comment");
981 if (value != (const char *) NULL)
982 (void) CopyMagickString(viff_info.comment,value,MagickMin(strlen(value),511)+1);
983 viff_info.rows=image->columns;
984 viff_info.columns=image->rows;
986 viff_info.x_offset=(~0);
987 viff_info.y_offset=(~0);
988 viff_info.x_bits_per_pixel=0;
989 viff_info.y_bits_per_pixel=0;
990 viff_info.location_type=VFF_LOC_IMPLICIT;
991 viff_info.location_dimension=0;
992 viff_info.number_of_images=1;
993 viff_info.data_encode_scheme=VFF_DES_RAW;
994 viff_info.map_scheme=VFF_MS_NONE;
995 viff_info.map_storage_type=VFF_MAPTYP_NONE;
996 viff_info.map_rows=0;
997 viff_info.map_columns=0;
998 viff_info.map_subrows=0;
999 viff_info.map_enable=1; /* no colormap */
1000 viff_info.maps_per_cycle=0;
1001 number_pixels=(MagickSizeType) image->columns*image->rows;
1002 if (image->storage_class == DirectClass)
1005 Full color VIFF raster.
1007 viff_info.number_data_bands=image->matte ? 4UL : 3UL;
1008 viff_info.color_space_model=VFF_CM_genericRGB;
1009 viff_info.data_storage_type=VFF_TYP_1_BYTE;
1010 packets=viff_info.number_data_bands*number_pixels;
1014 viff_info.number_data_bands=1;
1015 viff_info.color_space_model=VFF_CM_NONE;
1016 viff_info.data_storage_type=VFF_TYP_1_BYTE;
1017 packets=number_pixels;
1018 if (IsGrayImage(image,&image->exception) == MagickFalse)
1021 Colormapped VIFF raster.
1023 viff_info.map_scheme=VFF_MS_ONEPERBAND;
1024 viff_info.map_storage_type=VFF_MAPTYP_1_BYTE;
1025 viff_info.map_rows=3;
1026 viff_info.map_columns=image->colors;
1029 if (image->colors <= 2)
1032 Monochrome VIFF raster.
1034 viff_info.data_storage_type=VFF_TYP_BIT;
1035 packets=((image->columns+7) >> 3)*image->rows;
1039 Write VIFF image header (pad to 1024 bytes).
1041 buffer[0]=(unsigned char) viff_info.identifier;
1042 buffer[1]=(unsigned char) viff_info.file_type;
1043 buffer[2]=(unsigned char) viff_info.release;
1044 buffer[3]=(unsigned char) viff_info.version;
1045 buffer[4]=(unsigned char) viff_info.machine_dependency;
1046 buffer[5]=(unsigned char) viff_info.reserve[0];
1047 buffer[6]=(unsigned char) viff_info.reserve[1];
1048 buffer[7]=(unsigned char) viff_info.reserve[2];
1049 (void) WriteBlob(image,8,buffer);
1050 (void) WriteBlob(image,512,(unsigned char *) viff_info.comment);
1051 (void) WriteBlobMSBLong(image,viff_info.rows);
1052 (void) WriteBlobMSBLong(image,viff_info.columns);
1053 (void) WriteBlobMSBLong(image,viff_info.subrows);
1054 (void) WriteBlobMSBLong(image,(size_t) viff_info.x_offset);
1055 (void) WriteBlobMSBLong(image,(size_t) viff_info.y_offset);
1056 viff_info.x_bits_per_pixel=1U*(63 << 24) | (128 << 16);
1057 (void) WriteBlobMSBLong(image,(size_t) viff_info.x_bits_per_pixel);
1058 viff_info.y_bits_per_pixel=1U*(63 << 24) | (128 << 16);
1059 (void) WriteBlobMSBLong(image,(size_t) viff_info.y_bits_per_pixel);
1060 (void) WriteBlobMSBLong(image,viff_info.location_type);
1061 (void) WriteBlobMSBLong(image,viff_info.location_dimension);
1062 (void) WriteBlobMSBLong(image,viff_info.number_of_images);
1063 (void) WriteBlobMSBLong(image,viff_info.number_data_bands);
1064 (void) WriteBlobMSBLong(image,viff_info.data_storage_type);
1065 (void) WriteBlobMSBLong(image,viff_info.data_encode_scheme);
1066 (void) WriteBlobMSBLong(image,viff_info.map_scheme);
1067 (void) WriteBlobMSBLong(image,viff_info.map_storage_type);
1068 (void) WriteBlobMSBLong(image,viff_info.map_rows);
1069 (void) WriteBlobMSBLong(image,viff_info.map_columns);
1070 (void) WriteBlobMSBLong(image,viff_info.map_subrows);
1071 (void) WriteBlobMSBLong(image,viff_info.map_enable);
1072 (void) WriteBlobMSBLong(image,viff_info.maps_per_cycle);
1073 (void) WriteBlobMSBLong(image,viff_info.color_space_model);
1074 for (i=0; i < 420; i++)
1075 (void) WriteBlobByte(image,'\0');
1077 Convert MIFF to VIFF raster pixels.
1079 viff_pixels=(unsigned char *) AcquireQuantumMemory((size_t) packets,
1080 sizeof(*viff_pixels));
1081 if (viff_pixels == (unsigned char *) NULL)
1082 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1084 if (image->storage_class == DirectClass)
1087 Convert DirectClass packet to VIFF RGB pixel.
1089 number_pixels=(MagickSizeType) image->columns*image->rows;
1090 for (y=0; y < (ssize_t) image->rows; y++)
1092 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1093 if (p == (const PixelPacket *) NULL)
1095 for (x=0; x < (ssize_t) image->columns; x++)
1097 *q=ScaleQuantumToChar(GetRedPixelComponent(p));
1098 *(q+number_pixels)=ScaleQuantumToChar(GetGreenPixelComponent(p));
1099 *(q+number_pixels*2)=ScaleQuantumToChar(GetBluePixelComponent(p));
1100 if (image->matte != MagickFalse)
1101 *(q+number_pixels*3)=ScaleQuantumToChar((Quantum)
1102 (GetAlphaPixelComponent(p)));
1106 if (image->previous == (Image *) NULL)
1108 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1110 if (status == MagickFalse)
1116 if (IsGrayImage(image,&image->exception) == MagickFalse)
1122 Dump colormap to file.
1124 viff_colormap=(unsigned char *) AcquireQuantumMemory(image->colors,
1125 3*sizeof(*viff_colormap));
1126 if (viff_colormap == (unsigned char *) NULL)
1127 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1129 for (i=0; i < (ssize_t) image->colors; i++)
1130 *q++=ScaleQuantumToChar(image->colormap[i].red);
1131 for (i=0; i < (ssize_t) image->colors; i++)
1132 *q++=ScaleQuantumToChar(image->colormap[i].green);
1133 for (i=0; i < (ssize_t) image->colors; i++)
1134 *q++=ScaleQuantumToChar(image->colormap[i].blue);
1135 (void) WriteBlob(image,3*image->colors,viff_colormap);
1136 viff_colormap=(unsigned char *) RelinquishMagickMemory(viff_colormap);
1138 Convert PseudoClass packet to VIFF colormapped pixels.
1141 for (y=0; y < (ssize_t) image->rows; y++)
1143 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1144 if (p == (const PixelPacket *) NULL)
1146 indexes=GetVirtualIndexQueue(image);
1147 for (x=0; x < (ssize_t) image->columns; x++)
1148 *q++=(unsigned char) indexes[x];
1149 if (image->previous == (Image *) NULL)
1151 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1153 if (status == MagickFalse)
1159 if (image->colors <= 2)
1165 register unsigned char
1170 Convert PseudoClass image to a VIFF monochrome image.
1172 (void) SetImageType(image,BilevelType);
1173 for (y=0; y < (ssize_t) image->rows; y++)
1175 p=GetVirtualPixels(image,0,y,image->columns,1,
1177 if (p == (const PixelPacket *) NULL)
1179 indexes=GetVirtualIndexQueue(image);
1182 for (x=0; x < (ssize_t) image->columns; x++)
1185 if (PixelIntensity(p) < ((MagickRealType) QuantumRange/2.0))
1196 *q++=byte >> (8-bit);
1197 if (image->previous == (Image *) NULL)
1199 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
1201 if (status == MagickFalse)
1209 Convert PseudoClass packet to VIFF grayscale pixel.
1211 for (y=0; y < (ssize_t) image->rows; y++)
1213 p=GetVirtualPixels(image,0,y,image->columns,1,
1215 if (p == (const PixelPacket *) NULL)
1217 for (x=0; x < (ssize_t) image->columns; x++)
1219 *q++=(unsigned char) PixelIntensityToQuantum(p);
1222 if (image->previous == (Image *) NULL)
1224 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
1226 if (status == MagickFalse)
1231 (void) WriteBlob(image,(size_t) packets,viff_pixels);
1232 viff_pixels=(unsigned char *) RelinquishMagickMemory(viff_pixels);
1233 if (GetNextImageInList(image) == (Image *) NULL)
1235 image=SyncNextImageInList(image);
1236 status=SetImageProgress(image,SaveImagesTag,scene++,
1237 GetImageListLength(image));
1238 if (status == MagickFalse)
1240 } while (image_info->adjoin != MagickFalse);
1241 (void) CloseBlob(image);