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=(long) ReadBlobLSBLong(image);
304 viff_info.y_offset=(long) 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=(long) ReadBlobMSBLong(image);
328 viff_info.y_offset=(long) 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 < (long) (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 < (long) 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 < (long) (2*image->colors))
474 image->colormap[i % image->colors].green=
475 ScaleCharToQuantum((unsigned char) value);
477 if (i < (long) (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=(unsigned long) (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 < (long) 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 < (long) 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 < (long) 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 < (long) (image->columns-7); x+=8)
630 for (bit=0; bit < 8; bit++)
631 if (PixelIntensity(q) < ((MagickRealType) QuantumRange/2.0))
633 quantum=(unsigned long) indexes[x+bit];
635 indexes[x+bit]=(IndexPacket) quantum;
639 if ((image->columns % 8) != 0)
641 for (bit=0; bit < (long) (image->columns % 8); bit++)
642 if (PixelIntensity(q) < ((MagickRealType) QuantumRange/2.0))
644 quantum=(unsigned long) 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,y,image->rows);
655 if (status == MagickFalse)
661 if (image->storage_class == PseudoClass)
662 for (y=0; y < (long) image->rows; y++)
664 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
665 if (q == (PixelPacket *) NULL)
667 indexes=GetAuthenticIndexQueue(image);
668 for (x=0; x < (long) image->columns; x++)
669 indexes[x]=(IndexPacket) (*p++);
670 if (SyncAuthenticPixels(image,exception) == MagickFalse)
672 if (image->previous == (Image *) NULL)
674 status=SetImageProgress(image,LoadImageTag,y,image->rows);
675 if (status == MagickFalse)
682 Convert DirectColor scanline.
684 number_pixels=(MagickSizeType) image->columns*image->rows;
685 for (y=0; y < (long) image->rows; y++)
687 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
688 if (q == (PixelPacket *) NULL)
690 for (x=0; x < (long) image->columns; x++)
692 q->red=ScaleCharToQuantum(*p);
693 q->green=ScaleCharToQuantum(*(p+number_pixels));
694 q->blue=ScaleCharToQuantum(*(p+2*number_pixels));
695 if (image->colors != 0)
697 q->red=image->colormap[(long) q->red].red;
698 q->green=image->colormap[(long) q->green].green;
699 q->blue=image->colormap[(long) q->blue].blue;
701 q->opacity=(Quantum) (image->matte ? QuantumRange-
702 ScaleCharToQuantum(*(p+number_pixels*3)) : OpaqueOpacity);
706 if (SyncAuthenticPixels(image,exception) == MagickFalse)
708 if (image->previous == (Image *) NULL)
710 status=SetImageProgress(image,LoadImageTag,y,image->rows);
711 if (status == MagickFalse)
716 viff_pixels=(unsigned char *) RelinquishMagickMemory(viff_pixels);
717 if (image->storage_class == PseudoClass)
718 (void) SyncImage(image);
719 if (EOFBlob(image) != MagickFalse)
721 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
726 Proceed to next image.
728 if (image_info->number_scenes != 0)
729 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
731 count=ReadBlob(image,1,&viff_info.identifier);
732 if ((count != 0) && (viff_info.identifier == 0xab))
735 Allocate next image structure.
737 AcquireNextImage(image_info,image);
738 if (GetNextImageInList(image) == (Image *) NULL)
740 image=DestroyImageList(image);
741 return((Image *) NULL);
743 image=SyncNextImageInList(image);
744 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
746 if (status == MagickFalse)
749 } while ((count != 0) && (viff_info.identifier == 0xab));
750 (void) CloseBlob(image);
751 return(GetFirstImageInList(image));
755 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
759 % R e g i s t e r V I F F I m a g e %
763 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
765 % RegisterVIFFImage() adds properties for the VIFF image format to
766 % the list of supported formats. The properties include the image format
767 % tag, a method to read and/or write the format, whether the format
768 % supports the saving of more than one frame to the same file or blob,
769 % whether the format supports native in-memory I/O, and a brief
770 % description of the format.
772 % The format of the RegisterVIFFImage method is:
774 % unsigned long RegisterVIFFImage(void)
777 ModuleExport unsigned long RegisterVIFFImage(void)
782 entry=SetMagickInfo("VIFF");
783 entry->decoder=(DecodeImageHandler *) ReadVIFFImage;
784 entry->encoder=(EncodeImageHandler *) WriteVIFFImage;
785 entry->magick=(IsImageFormatHandler *) IsVIFF;
786 entry->description=ConstantString("Khoros Visualization image");
787 entry->module=ConstantString("VIFF");
788 (void) RegisterMagickInfo(entry);
789 entry=SetMagickInfo("XV");
790 entry->decoder=(DecodeImageHandler *) ReadVIFFImage;
791 entry->encoder=(EncodeImageHandler *) WriteVIFFImage;
792 entry->description=ConstantString("Khoros Visualization image");
793 entry->module=ConstantString("VIFF");
794 (void) RegisterMagickInfo(entry);
795 return(MagickImageCoderSignature);
799 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
803 % U n r e g i s t e r V I F F I m a g e %
807 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
809 % UnregisterVIFFImage() removes format registrations made by the
810 % VIFF module from the list of supported formats.
812 % The format of the UnregisterVIFFImage method is:
814 % UnregisterVIFFImage(void)
817 ModuleExport void UnregisterVIFFImage(void)
819 (void) UnregisterMagickInfo("VIFF");
820 (void) UnregisterMagickInfo("XV");
824 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
828 % W r i t e V I F F I m a g e %
832 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
834 % WriteVIFFImage() writes an image to a file in the VIFF image format.
836 % The format of the WriteVIFFImage method is:
838 % MagickBooleanType WriteVIFFImage(const ImageInfo *image_info,
841 % A description of each parameter follows.
843 % o image_info: the image info.
845 % o image: The image.
849 static inline size_t MagickMin(const size_t x,const size_t y)
856 static MagickBooleanType WriteVIFFImage(const ImageInfo *image_info,
859 #define VFF_CM_genericRGB 15
860 #define VFF_CM_NONE 0
861 #define VFF_DEP_IEEEORDER 0x2
862 #define VFF_DES_RAW 0
863 #define VFF_LOC_IMPLICIT 1
864 #define VFF_MAPTYP_NONE 0
865 #define VFF_MAPTYP_1_BYTE 1
866 #define VFF_MS_NONE 0
867 #define VFF_MS_ONEPERBAND 1
868 #define VFF_TYP_BIT 0
869 #define VFF_TYP_1_BYTE 1
871 typedef struct _ViffInfo
928 register const IndexPacket
931 register const PixelPacket
940 register unsigned char
951 Open output image file.
953 assert(image_info != (const ImageInfo *) NULL);
954 assert(image_info->signature == MagickSignature);
955 assert(image != (Image *) NULL);
956 assert(image->signature == MagickSignature);
957 if (image->debug != MagickFalse)
958 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
959 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
960 if (status == MagickFalse)
962 (void) ResetMagickMemory(&viff_info,0,sizeof(ViffInfo));
967 Initialize VIFF image structure.
969 if (image->colorspace != RGBColorspace)
970 (void) TransformImageColorspace(image,RGBColorspace);
971 if (IsGrayImage(image,&image->exception) != MagickFalse)
972 (void) SetImageStorageClass(image,DirectClass);
973 viff_info.identifier=(char) 0xab;
974 viff_info.file_type=1;
977 viff_info.machine_dependency=VFF_DEP_IEEEORDER; /* IEEE byte ordering */
978 *viff_info.comment='\0';
979 value=GetImageProperty(image,"comment");
980 if (value != (const char *) NULL)
981 (void) CopyMagickString(viff_info.comment,value,MagickMin(strlen(value),511)+1);
982 viff_info.rows=image->columns;
983 viff_info.columns=image->rows;
985 viff_info.x_offset=(~0);
986 viff_info.y_offset=(~0);
987 viff_info.x_bits_per_pixel=0;
988 viff_info.y_bits_per_pixel=0;
989 viff_info.location_type=VFF_LOC_IMPLICIT;
990 viff_info.location_dimension=0;
991 viff_info.number_of_images=1;
992 viff_info.data_encode_scheme=VFF_DES_RAW;
993 viff_info.map_scheme=VFF_MS_NONE;
994 viff_info.map_storage_type=VFF_MAPTYP_NONE;
995 viff_info.map_rows=0;
996 viff_info.map_columns=0;
997 viff_info.map_subrows=0;
998 viff_info.map_enable=1; /* no colormap */
999 viff_info.maps_per_cycle=0;
1000 number_pixels=(MagickSizeType) image->columns*image->rows;
1001 if (image->storage_class == DirectClass)
1004 Full color VIFF raster.
1006 viff_info.number_data_bands=image->matte ? 4UL : 3UL;
1007 viff_info.color_space_model=VFF_CM_genericRGB;
1008 viff_info.data_storage_type=VFF_TYP_1_BYTE;
1009 packets=viff_info.number_data_bands*number_pixels;
1013 viff_info.number_data_bands=1;
1014 viff_info.color_space_model=VFF_CM_NONE;
1015 viff_info.data_storage_type=VFF_TYP_1_BYTE;
1016 packets=number_pixels;
1017 if (IsGrayImage(image,&image->exception) == MagickFalse)
1020 Colormapped VIFF raster.
1022 viff_info.map_scheme=VFF_MS_ONEPERBAND;
1023 viff_info.map_storage_type=VFF_MAPTYP_1_BYTE;
1024 viff_info.map_rows=3;
1025 viff_info.map_columns=image->colors;
1028 if (image->colors <= 2)
1031 Monochrome VIFF raster.
1033 viff_info.data_storage_type=VFF_TYP_BIT;
1034 packets=((image->columns+7) >> 3)*image->rows;
1038 Write VIFF image header (pad to 1024 bytes).
1040 buffer[0]=(unsigned char) viff_info.identifier;
1041 buffer[1]=(unsigned char) viff_info.file_type;
1042 buffer[2]=(unsigned char) viff_info.release;
1043 buffer[3]=(unsigned char) viff_info.version;
1044 buffer[4]=(unsigned char) viff_info.machine_dependency;
1045 buffer[5]=(unsigned char) viff_info.reserve[0];
1046 buffer[6]=(unsigned char) viff_info.reserve[1];
1047 buffer[7]=(unsigned char) viff_info.reserve[2];
1048 (void) WriteBlob(image,8,buffer);
1049 (void) WriteBlob(image,512,(unsigned char *) viff_info.comment);
1050 (void) WriteBlobMSBLong(image,viff_info.rows);
1051 (void) WriteBlobMSBLong(image,viff_info.columns);
1052 (void) WriteBlobMSBLong(image,viff_info.subrows);
1053 (void) WriteBlobMSBLong(image,(unsigned long) viff_info.x_offset);
1054 (void) WriteBlobMSBLong(image,(unsigned long) viff_info.y_offset);
1055 viff_info.x_bits_per_pixel=1U*(63 << 24) | (128 << 16);
1056 (void) WriteBlobMSBLong(image,(unsigned long) viff_info.x_bits_per_pixel);
1057 viff_info.y_bits_per_pixel=1U*(63 << 24) | (128 << 16);
1058 (void) WriteBlobMSBLong(image,(unsigned long) viff_info.y_bits_per_pixel);
1059 (void) WriteBlobMSBLong(image,viff_info.location_type);
1060 (void) WriteBlobMSBLong(image,viff_info.location_dimension);
1061 (void) WriteBlobMSBLong(image,viff_info.number_of_images);
1062 (void) WriteBlobMSBLong(image,viff_info.number_data_bands);
1063 (void) WriteBlobMSBLong(image,viff_info.data_storage_type);
1064 (void) WriteBlobMSBLong(image,viff_info.data_encode_scheme);
1065 (void) WriteBlobMSBLong(image,viff_info.map_scheme);
1066 (void) WriteBlobMSBLong(image,viff_info.map_storage_type);
1067 (void) WriteBlobMSBLong(image,viff_info.map_rows);
1068 (void) WriteBlobMSBLong(image,viff_info.map_columns);
1069 (void) WriteBlobMSBLong(image,viff_info.map_subrows);
1070 (void) WriteBlobMSBLong(image,viff_info.map_enable);
1071 (void) WriteBlobMSBLong(image,viff_info.maps_per_cycle);
1072 (void) WriteBlobMSBLong(image,viff_info.color_space_model);
1073 for (i=0; i < 420; i++)
1074 (void) WriteBlobByte(image,'\0');
1076 Convert MIFF to VIFF raster pixels.
1078 viff_pixels=(unsigned char *) AcquireQuantumMemory((size_t) packets,
1079 sizeof(*viff_pixels));
1080 if (viff_pixels == (unsigned char *) NULL)
1081 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1083 if (image->storage_class == DirectClass)
1086 Convert DirectClass packet to VIFF RGB pixel.
1088 number_pixels=(MagickSizeType) image->columns*image->rows;
1089 for (y=0; y < (long) image->rows; y++)
1091 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1092 if (p == (const PixelPacket *) NULL)
1094 for (x=0; x < (long) image->columns; x++)
1096 *q=ScaleQuantumToChar(GetRedPixelComponent(p));
1097 *(q+number_pixels)=ScaleQuantumToChar(GetGreenPixelComponent(p));
1098 *(q+number_pixels*2)=ScaleQuantumToChar(GetBluePixelComponent(p));
1099 if (image->matte != MagickFalse)
1100 *(q+number_pixels*3)=ScaleQuantumToChar((Quantum)
1101 (GetAlphaPixelComponent(p)));
1105 if (image->previous == (Image *) NULL)
1107 status=SetImageProgress(image,SaveImageTag,y,image->rows);
1108 if (status == MagickFalse)
1114 if (IsGrayImage(image,&image->exception) == MagickFalse)
1120 Dump colormap to file.
1122 viff_colormap=(unsigned char *) AcquireQuantumMemory(image->colors,
1123 3*sizeof(*viff_colormap));
1124 if (viff_colormap == (unsigned char *) NULL)
1125 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1127 for (i=0; i < (long) image->colors; i++)
1128 *q++=ScaleQuantumToChar(image->colormap[i].red);
1129 for (i=0; i < (long) image->colors; i++)
1130 *q++=ScaleQuantumToChar(image->colormap[i].green);
1131 for (i=0; i < (long) image->colors; i++)
1132 *q++=ScaleQuantumToChar(image->colormap[i].blue);
1133 (void) WriteBlob(image,3*image->colors,viff_colormap);
1134 viff_colormap=(unsigned char *) RelinquishMagickMemory(viff_colormap);
1136 Convert PseudoClass packet to VIFF colormapped pixels.
1139 for (y=0; y < (long) image->rows; y++)
1141 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1142 if (p == (const PixelPacket *) NULL)
1144 indexes=GetVirtualIndexQueue(image);
1145 for (x=0; x < (long) image->columns; x++)
1146 *q++=(unsigned char) indexes[x];
1147 if (image->previous == (Image *) NULL)
1149 status=SetImageProgress(image,SaveImageTag,y,image->rows);
1150 if (status == MagickFalse)
1156 if (image->colors <= 2)
1162 register unsigned char
1167 Convert PseudoClass image to a VIFF monochrome image.
1169 (void) SetImageType(image,BilevelType);
1170 for (y=0; y < (long) image->rows; y++)
1172 p=GetVirtualPixels(image,0,y,image->columns,1,
1174 if (p == (const PixelPacket *) NULL)
1176 indexes=GetVirtualIndexQueue(image);
1179 for (x=0; x < (long) image->columns; x++)
1182 if (PixelIntensity(p) < ((MagickRealType) QuantumRange/2.0))
1193 *q++=byte >> (8-bit);
1194 if (image->previous == (Image *) NULL)
1196 status=SetImageProgress(image,SaveImageTag,y,image->rows);
1197 if (status == MagickFalse)
1205 Convert PseudoClass packet to VIFF grayscale pixel.
1207 for (y=0; y < (long) image->rows; y++)
1209 p=GetVirtualPixels(image,0,y,image->columns,1,
1211 if (p == (const PixelPacket *) NULL)
1213 for (x=0; x < (long) image->columns; x++)
1215 *q++=(unsigned char) PixelIntensityToQuantum(p);
1218 if (image->previous == (Image *) NULL)
1220 status=SetImageProgress(image,SaveImageTag,y,image->rows);
1221 if (status == MagickFalse)
1226 (void) WriteBlob(image,(size_t) packets,viff_pixels);
1227 viff_pixels=(unsigned char *) RelinquishMagickMemory(viff_pixels);
1228 if (GetNextImageInList(image) == (Image *) NULL)
1230 image=SyncNextImageInList(image);
1231 status=SetImageProgress(image,SaveImagesTag,scene++,
1232 GetImageListLength(image));
1233 if (status == MagickFalse)
1235 } while (image_info->adjoin != MagickFalse);
1236 (void) CloseBlob(image);