2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6 % V V IIIII FFFFF FFFFF %
13 % Read/Write Khoros Visualization Image Format %
20 % Copyright 1999-2018 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 % https://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 "MagickCore/studio.h"
43 #include "MagickCore/attribute.h"
44 #include "MagickCore/blob.h"
45 #include "MagickCore/blob-private.h"
46 #include "MagickCore/cache.h"
47 #include "MagickCore/color.h"
48 #include "MagickCore/color-private.h"
49 #include "MagickCore/colormap.h"
50 #include "MagickCore/colormap-private.h"
51 #include "MagickCore/colorspace.h"
52 #include "MagickCore/colorspace-private.h"
53 #include "MagickCore/exception.h"
54 #include "MagickCore/exception-private.h"
55 #include "MagickCore/image.h"
56 #include "MagickCore/image-private.h"
57 #include "MagickCore/list.h"
58 #include "MagickCore/magick.h"
59 #include "MagickCore/memory_.h"
60 #include "MagickCore/memory-private.h"
61 #include "MagickCore/monitor.h"
62 #include "MagickCore/monitor-private.h"
63 #include "MagickCore/pixel-accessor.h"
64 #include "MagickCore/property.h"
65 #include "MagickCore/quantum-private.h"
66 #include "MagickCore/static.h"
67 #include "MagickCore/string_.h"
68 #include "MagickCore/module.h"
73 static MagickBooleanType
74 WriteVIFFImage(const ImageInfo *,Image *,ExceptionInfo *);
77 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
85 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
87 % IsVIFF() returns MagickTrue if the image format type, identified by the
88 % magick string, is VIFF.
90 % The format of the IsVIFF method is:
92 % MagickBooleanType IsVIFF(const unsigned char *magick,const size_t length)
94 % A description of each parameter follows:
96 % o magick: compare image format pattern against these bytes.
98 % o length: Specifies the length of the magick string.
101 static MagickBooleanType IsVIFF(const unsigned char *magick,const size_t length)
105 if (memcmp(magick,"\253\001",2) == 0)
111 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
115 % R e a d V I F F I m a g e %
119 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
121 % ReadVIFFImage() reads a Khoros Visualization image file and returns
122 % it. It allocates the memory necessary for the new Image structure and
123 % returns a pointer to the new image.
125 % The format of the ReadVIFFImage method is:
127 % Image *ReadVIFFImage(const ImageInfo *image_info,
128 % ExceptionInfo *exception)
130 % A description of each parameter follows:
132 % o image: Method ReadVIFFImage returns a pointer to the image after
133 % reading. A null image is returned if there is a memory shortage or if
134 % the image cannot be read.
136 % o image_info: the image info.
138 % o exception: return any errors or warnings in this structure.
141 static Image *ReadVIFFImage(const ImageInfo *image_info,
142 ExceptionInfo *exception)
144 #define VFF_CM_genericRGB 15
145 #define VFF_CM_ntscRGB 1
146 #define VFF_CM_NONE 0
147 #define VFF_DEP_DECORDER 0x4
148 #define VFF_DEP_NSORDER 0x8
149 #define VFF_DES_RAW 0
150 #define VFF_LOC_IMPLICIT 1
151 #define VFF_MAPTYP_NONE 0
152 #define VFF_MAPTYP_1_BYTE 1
153 #define VFF_MAPTYP_2_BYTE 2
154 #define VFF_MAPTYP_4_BYTE 4
155 #define VFF_MAPTYP_FLOAT 5
156 #define VFF_MAPTYP_DOUBLE 7
157 #define VFF_MS_NONE 0
158 #define VFF_MS_ONEPERBAND 1
159 #define VFF_MS_SHARED 3
160 #define VFF_TYP_BIT 0
161 #define VFF_TYP_1_BYTE 1
162 #define VFF_TYP_2_BYTE 2
163 #define VFF_TYP_4_BYTE 4
164 #define VFF_TYP_FLOAT 5
165 #define VFF_TYP_DOUBLE 9
167 typedef struct _ViffInfo
236 register unsigned char
260 assert(image_info != (const ImageInfo *) NULL);
261 assert(image_info->signature == MagickCoreSignature);
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 == MagickCoreSignature);
267 image=AcquireImage(image_info,exception);
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 != 1) || ((unsigned char) viff_info.identifier != 0xab))
284 ThrowReaderException(CorruptImageError,"NotAVIFFImage");
286 Initialize VIFF image.
288 (void) ReadBlob(image,sizeof(viff_info.file_type),&viff_info.file_type);
289 (void) ReadBlob(image,sizeof(viff_info.release),&viff_info.release);
290 (void) ReadBlob(image,sizeof(viff_info.version),&viff_info.version);
291 (void) ReadBlob(image,sizeof(viff_info.machine_dependency),
292 &viff_info.machine_dependency);
293 (void) ReadBlob(image,sizeof(viff_info.reserve),viff_info.reserve);
294 count=ReadBlob(image,512,(unsigned char *) viff_info.comment);
296 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
297 viff_info.comment[511]='\0';
298 if (strlen(viff_info.comment) > 4)
299 (void) SetImageProperty(image,"comment",viff_info.comment,exception);
300 if ((viff_info.machine_dependency == VFF_DEP_DECORDER) ||
301 (viff_info.machine_dependency == VFF_DEP_NSORDER))
302 image->endian=LSBEndian;
304 image->endian=MSBEndian;
305 viff_info.rows=ReadBlobLong(image);
306 viff_info.columns=ReadBlobLong(image);
307 viff_info.subrows=ReadBlobLong(image);
308 viff_info.x_offset=ReadBlobSignedLong(image);
309 viff_info.y_offset=ReadBlobSignedLong(image);
310 viff_info.x_bits_per_pixel=(float) ReadBlobLong(image);
311 viff_info.y_bits_per_pixel=(float) ReadBlobLong(image);
312 viff_info.location_type=ReadBlobLong(image);
313 viff_info.location_dimension=ReadBlobLong(image);
314 viff_info.number_of_images=ReadBlobLong(image);
315 viff_info.number_data_bands=ReadBlobLong(image);
316 viff_info.data_storage_type=ReadBlobLong(image);
317 viff_info.data_encode_scheme=ReadBlobLong(image);
318 viff_info.map_scheme=ReadBlobLong(image);
319 viff_info.map_storage_type=ReadBlobLong(image);
320 viff_info.map_rows=ReadBlobLong(image);
321 viff_info.map_columns=ReadBlobLong(image);
322 viff_info.map_subrows=ReadBlobLong(image);
323 viff_info.map_enable=ReadBlobLong(image);
324 viff_info.maps_per_cycle=ReadBlobLong(image);
325 viff_info.color_space_model=ReadBlobLong(image);
326 for (i=0; i < 420; i++)
327 (void) ReadBlobByte(image);
328 if (EOFBlob(image) != MagickFalse)
329 ThrowReaderException(CorruptImageError,"UnexpectedEndOfFile");
330 number_pixels=(MagickSizeType) viff_info.columns*viff_info.rows;
331 if (number_pixels > GetBlobSize(image))
332 ThrowReaderException(CorruptImageError,"InsufficientImageDataInFile");
333 if (number_pixels != (size_t) number_pixels)
334 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
335 if (number_pixels == 0)
336 ThrowReaderException(CoderError,"ImageColumnOrRowSizeIsNotSupported");
337 image->columns=viff_info.rows;
338 image->rows=viff_info.columns;
339 image->depth=viff_info.x_bits_per_pixel <= 8 ? 8UL :
340 MAGICKCORE_QUANTUM_DEPTH;
341 image->alpha_trait=viff_info.number_data_bands == 4 ? BlendPixelTrait :
343 status=SetImageExtent(image,image->columns,image->rows,exception);
344 if (status == MagickFalse)
345 return(DestroyImageList(image));
346 (void) SetImageBackgroundColor(image,exception);
348 Verify that we can read this VIFF image.
350 if ((viff_info.number_data_bands < 1) || (viff_info.number_data_bands > 4))
351 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
352 if ((viff_info.data_storage_type != VFF_TYP_BIT) &&
353 (viff_info.data_storage_type != VFF_TYP_1_BYTE) &&
354 (viff_info.data_storage_type != VFF_TYP_2_BYTE) &&
355 (viff_info.data_storage_type != VFF_TYP_4_BYTE) &&
356 (viff_info.data_storage_type != VFF_TYP_FLOAT) &&
357 (viff_info.data_storage_type != VFF_TYP_DOUBLE))
358 ThrowReaderException(CoderError,"DataStorageTypeIsNotSupported");
359 if (viff_info.data_encode_scheme != VFF_DES_RAW)
360 ThrowReaderException(CoderError,"DataEncodingSchemeIsNotSupported");
361 if ((viff_info.map_storage_type != VFF_MAPTYP_NONE) &&
362 (viff_info.map_storage_type != VFF_MAPTYP_1_BYTE) &&
363 (viff_info.map_storage_type != VFF_MAPTYP_2_BYTE) &&
364 (viff_info.map_storage_type != VFF_MAPTYP_4_BYTE) &&
365 (viff_info.map_storage_type != VFF_MAPTYP_FLOAT) &&
366 (viff_info.map_storage_type != VFF_MAPTYP_DOUBLE))
367 ThrowReaderException(CoderError,"MapStorageTypeIsNotSupported");
368 if ((viff_info.color_space_model != VFF_CM_NONE) &&
369 (viff_info.color_space_model != VFF_CM_ntscRGB) &&
370 (viff_info.color_space_model != VFF_CM_genericRGB))
371 ThrowReaderException(CoderError,"ColorspaceModelIsNotSupported");
372 if (viff_info.location_type != VFF_LOC_IMPLICIT)
373 ThrowReaderException(CoderError,"LocationTypeIsNotSupported");
374 if (viff_info.number_of_images != 1)
375 ThrowReaderException(CoderError,"NumberOfImagesIsNotSupported");
376 if (viff_info.map_rows == 0)
377 viff_info.map_scheme=VFF_MS_NONE;
378 switch ((int) viff_info.map_scheme)
382 if (viff_info.number_data_bands < 3)
385 Create linear color ramp.
387 if (viff_info.data_storage_type == VFF_TYP_BIT)
390 if (viff_info.data_storage_type == VFF_MAPTYP_1_BYTE)
393 image->colors=image->depth <= 8 ? 256UL : 65536UL;
394 status=AcquireImageColormap(image,image->colors,exception);
395 if (status == MagickFalse)
396 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
400 case VFF_MS_ONEPERBAND:
407 Allocate VIFF colormap.
409 switch ((int) viff_info.map_storage_type)
411 case VFF_MAPTYP_1_BYTE: bytes_per_pixel=1; break;
412 case VFF_MAPTYP_2_BYTE: bytes_per_pixel=2; break;
413 case VFF_MAPTYP_4_BYTE: bytes_per_pixel=4; break;
414 case VFF_MAPTYP_FLOAT: bytes_per_pixel=4; break;
415 case VFF_MAPTYP_DOUBLE: bytes_per_pixel=8; break;
416 default: bytes_per_pixel=1; break;
418 image->colors=viff_info.map_columns;
419 if ((MagickSizeType) (viff_info.map_rows*image->colors) > GetBlobSize(image))
420 ThrowReaderException(CorruptImageError,"InsufficientImageDataInFile");
421 if (AcquireImageColormap(image,image->colors,exception) == MagickFalse)
422 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
423 if ((MagickSizeType) viff_info.map_rows > GetBlobSize(image))
424 ThrowReaderException(CorruptImageError,"InsufficientImageDataInFile");
425 if ((MagickSizeType) viff_info.map_rows >
426 (viff_info.map_rows*bytes_per_pixel*sizeof(*viff_colormap)))
427 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
428 viff_colormap=(unsigned char *) AcquireQuantumMemory(image->colors,
429 viff_info.map_rows*bytes_per_pixel*sizeof(*viff_colormap));
430 if (viff_colormap == (unsigned char *) NULL)
431 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
433 Read VIFF raster colormap.
435 count=ReadBlob(image,bytes_per_pixel*image->colors*viff_info.map_rows,
438 if (*(char *) &lsb_first &&
439 ((viff_info.machine_dependency != VFF_DEP_DECORDER) &&
440 (viff_info.machine_dependency != VFF_DEP_NSORDER)))
441 switch ((int) viff_info.map_storage_type)
443 case VFF_MAPTYP_2_BYTE:
445 MSBOrderShort(viff_colormap,(bytes_per_pixel*image->colors*
446 viff_info.map_rows));
449 case VFF_MAPTYP_4_BYTE:
450 case VFF_MAPTYP_FLOAT:
452 MSBOrderLong(viff_colormap,(bytes_per_pixel*image->colors*
453 viff_info.map_rows));
458 for (i=0; i < (ssize_t) (viff_info.map_rows*image->colors); i++)
460 switch ((int) viff_info.map_storage_type)
462 case VFF_MAPTYP_2_BYTE: value=1.0*((short *) viff_colormap)[i]; break;
463 case VFF_MAPTYP_4_BYTE: value=1.0*((int *) viff_colormap)[i]; break;
464 case VFF_MAPTYP_FLOAT: value=((float *) viff_colormap)[i]; break;
465 case VFF_MAPTYP_DOUBLE: value=((double *) viff_colormap)[i]; break;
466 default: value=1.0*viff_colormap[i]; break;
468 if (i < (ssize_t) image->colors)
470 image->colormap[i].red=(MagickRealType)
471 ScaleCharToQuantum((unsigned char) value);
472 image->colormap[i].green=(MagickRealType)
473 ScaleCharToQuantum((unsigned char) value);
474 image->colormap[i].blue=(MagickRealType)
475 ScaleCharToQuantum((unsigned char) value);
478 if (i < (ssize_t) (2*image->colors))
479 image->colormap[i % image->colors].green=(MagickRealType)
480 ScaleCharToQuantum((unsigned char) value);
482 if (i < (ssize_t) (3*image->colors))
483 image->colormap[i % image->colors].blue=(MagickRealType)
484 ScaleCharToQuantum((unsigned char) value);
486 viff_colormap=(unsigned char *) RelinquishMagickMemory(viff_colormap);
490 ThrowReaderException(CoderError,"ColormapTypeNotSupported");
492 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
493 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
495 if (viff_info.data_storage_type == VFF_TYP_BIT)
498 Create bi-level colormap.
501 if (AcquireImageColormap(image,image->colors,exception) == MagickFalse)
502 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
503 image->colorspace=GRAYColorspace;
506 Allocate VIFF pixels.
508 switch ((int) viff_info.data_storage_type)
510 case VFF_TYP_2_BYTE: bytes_per_pixel=2; break;
511 case VFF_TYP_4_BYTE: bytes_per_pixel=4; break;
512 case VFF_TYP_FLOAT: bytes_per_pixel=4; break;
513 case VFF_TYP_DOUBLE: bytes_per_pixel=8; break;
514 default: bytes_per_pixel=1; break;
516 if (viff_info.data_storage_type == VFF_TYP_BIT)
518 if (HeapOverflowSanityCheck((image->columns+7UL) >> 3UL,image->rows) != MagickFalse)
519 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
520 max_packets=((image->columns+7UL) >> 3UL)*image->rows;
524 if (HeapOverflowSanityCheck((size_t) number_pixels,viff_info.number_data_bands) != MagickFalse)
525 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
526 max_packets=(size_t) (number_pixels*viff_info.number_data_bands);
528 if ((MagickSizeType) (bytes_per_pixel*max_packets) > GetBlobSize(image))
529 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
530 pixels=(unsigned char *) AcquireQuantumMemory((size_t) MagickMax(
531 number_pixels,max_packets),bytes_per_pixel*sizeof(*pixels));
532 if (pixels == (unsigned char *) NULL)
533 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
534 (void) memset(pixels,0,MagickMax(number_pixels,max_packets)*
535 bytes_per_pixel*sizeof(*pixels));
536 count=ReadBlob(image,bytes_per_pixel*max_packets,pixels);
538 if (*(char *) &lsb_first &&
539 ((viff_info.machine_dependency != VFF_DEP_DECORDER) &&
540 (viff_info.machine_dependency != VFF_DEP_NSORDER)))
541 switch ((int) viff_info.data_storage_type)
545 MSBOrderShort(pixels,bytes_per_pixel*max_packets);
551 MSBOrderLong(pixels,bytes_per_pixel*max_packets);
558 if ((viff_info.data_storage_type != VFF_TYP_1_BYTE) &&
559 (viff_info.map_scheme == VFF_MS_NONE))
565 Determine scale factor.
567 switch ((int) viff_info.data_storage_type)
569 case VFF_TYP_2_BYTE: value=1.0*((short *) pixels)[0]; break;
570 case VFF_TYP_4_BYTE: value=1.0*((int *) pixels)[0]; break;
571 case VFF_TYP_FLOAT: value=((float *) pixels)[0]; break;
572 case VFF_TYP_DOUBLE: value=((double *) pixels)[0]; break;
573 default: value=1.0*pixels[0]; break;
577 for (i=0; i < (ssize_t) max_packets; i++)
579 switch ((int) viff_info.data_storage_type)
581 case VFF_TYP_2_BYTE: value=1.0*((short *) pixels)[i]; break;
582 case VFF_TYP_4_BYTE: value=1.0*((int *) pixels)[i]; break;
583 case VFF_TYP_FLOAT: value=((float *) pixels)[i]; break;
584 case VFF_TYP_DOUBLE: value=((double *) pixels)[i]; break;
585 default: value=1.0*pixels[i]; break;
587 if (value > max_value)
590 if (value < min_value)
593 if ((min_value == 0) && (max_value == 0))
596 if (min_value == max_value)
598 scale_factor=(double) QuantumRange/min_value;
602 scale_factor=(double) QuantumRange/(max_value-min_value);
605 Convert pixels to Quantum size.
607 p=(unsigned char *) pixels;
608 for (i=0; i < (ssize_t) max_packets; i++)
610 switch ((int) viff_info.data_storage_type)
612 case VFF_TYP_2_BYTE: value=1.0*((short *) pixels)[i]; break;
613 case VFF_TYP_4_BYTE: value=1.0*((int *) pixels)[i]; break;
614 case VFF_TYP_FLOAT: value=((float *) pixels)[i]; break;
615 case VFF_TYP_DOUBLE: value=((double *) pixels)[i]; break;
616 default: value=1.0*pixels[i]; break;
618 if (viff_info.map_scheme == VFF_MS_NONE)
620 value=(value-min_value)*scale_factor;
621 if (value > QuantumRange)
627 *p=(unsigned char) ((Quantum) value);
631 Convert VIFF raster image to pixel packets.
633 p=(unsigned char *) pixels;
634 if (viff_info.data_storage_type == VFF_TYP_BIT)
637 Convert bitmap scanline.
639 for (y=0; y < (ssize_t) image->rows; y++)
641 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
642 if (q == (Quantum *) NULL)
644 for (x=0; x < (ssize_t) (image->columns-7); x+=8)
646 for (bit=0; bit < 8; bit++)
648 quantum=(size_t) ((*p) & (0x01 << bit) ? 0 : 1);
649 SetPixelRed(image,quantum == 0 ? 0 : QuantumRange,q);
650 SetPixelGreen(image,quantum == 0 ? 0 : QuantumRange,q);
651 SetPixelBlue(image,quantum == 0 ? 0 : QuantumRange,q);
652 if (image->storage_class == PseudoClass)
653 SetPixelIndex(image,(Quantum) quantum,q);
654 q+=GetPixelChannels(image);
658 if ((image->columns % 8) != 0)
660 for (bit=0; bit < (int) (image->columns % 8); bit++)
662 quantum=(size_t) ((*p) & (0x01 << bit) ? 0 : 1);
663 SetPixelRed(image,quantum == 0 ? 0 : QuantumRange,q);
664 SetPixelGreen(image,quantum == 0 ? 0 : QuantumRange,q);
665 SetPixelBlue(image,quantum == 0 ? 0 : QuantumRange,q);
666 if (image->storage_class == PseudoClass)
667 SetPixelIndex(image,(Quantum) quantum,q);
668 q+=GetPixelChannels(image);
672 if (SyncAuthenticPixels(image,exception) == MagickFalse)
674 if (image->previous == (Image *) NULL)
676 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
678 if (status == MagickFalse)
684 if (image->storage_class == PseudoClass)
685 for (y=0; y < (ssize_t) image->rows; y++)
687 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
688 if (q == (Quantum *) NULL)
690 for (x=0; x < (ssize_t) image->columns; x++)
692 SetPixelIndex(image,*p++,q);
693 q+=GetPixelChannels(image);
695 if (SyncAuthenticPixels(image,exception) == MagickFalse)
697 if (image->previous == (Image *) NULL)
699 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
701 if (status == MagickFalse)
708 Convert DirectColor scanline.
710 number_pixels=(MagickSizeType) image->columns*image->rows;
711 for (y=0; y < (ssize_t) image->rows; y++)
713 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
714 if (q == (Quantum *) NULL)
716 for (x=0; x < (ssize_t) image->columns; x++)
718 SetPixelRed(image,ScaleCharToQuantum(*p),q);
719 SetPixelGreen(image,ScaleCharToQuantum(*(p+number_pixels)),q);
720 SetPixelBlue(image,ScaleCharToQuantum(*(p+2*number_pixels)),q);
721 if (image->colors != 0)
726 index=(ssize_t) GetPixelRed(image,q);
727 SetPixelRed(image,ClampToQuantum(image->colormap[
728 ConstrainColormapIndex(image,index,exception)].red),q);
729 index=(ssize_t) GetPixelGreen(image,q);
730 SetPixelGreen(image,ClampToQuantum(image->colormap[
731 ConstrainColormapIndex(image,index,exception)].green),q);
732 index=(ssize_t) GetPixelBlue(image,q);
733 SetPixelBlue(image,ClampToQuantum(image->colormap[
734 ConstrainColormapIndex(image,index,exception)].blue),q);
736 SetPixelAlpha(image,image->alpha_trait != UndefinedPixelTrait ?
737 ScaleCharToQuantum(*(p+number_pixels*3)) : OpaqueAlpha,q);
739 q+=GetPixelChannels(image);
741 if (SyncAuthenticPixels(image,exception) == MagickFalse)
743 if (image->previous == (Image *) NULL)
745 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
747 if (status == MagickFalse)
752 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
753 if (image->storage_class == PseudoClass)
754 (void) SyncImage(image,exception);
755 if (EOFBlob(image) != MagickFalse)
757 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
762 Proceed to next image.
764 if (image_info->number_scenes != 0)
765 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
767 count=ReadBlob(image,1,&viff_info.identifier);
768 if ((count == 1) && (viff_info.identifier == 0xab))
771 Allocate next image structure.
773 AcquireNextImage(image_info,image,exception);
774 if (GetNextImageInList(image) == (Image *) NULL)
776 image=DestroyImageList(image);
777 return((Image *) NULL);
779 image=SyncNextImageInList(image);
780 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
782 if (status == MagickFalse)
785 } while ((count != 0) && (viff_info.identifier == 0xab));
786 (void) CloseBlob(image);
787 if (status == MagickFalse)
788 return(DestroyImageList(image));
789 return(GetFirstImageInList(image));
793 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
797 % R e g i s t e r V I F F I m a g e %
801 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
803 % RegisterVIFFImage() adds properties for the VIFF image format to
804 % the list of supported formats. The properties include the image format
805 % tag, a method to read and/or write the format, whether the format
806 % supports the saving of more than one frame to the same file or blob,
807 % whether the format supports native in-memory I/O, and a brief
808 % description of the format.
810 % The format of the RegisterVIFFImage method is:
812 % size_t RegisterVIFFImage(void)
815 ModuleExport size_t RegisterVIFFImage(void)
820 entry=AcquireMagickInfo("VIFF","VIFF","Khoros Visualization image");
821 entry->decoder=(DecodeImageHandler *) ReadVIFFImage;
822 entry->encoder=(EncodeImageHandler *) WriteVIFFImage;
823 entry->magick=(IsImageFormatHandler *) IsVIFF;
824 entry->flags|=CoderDecoderSeekableStreamFlag;
825 (void) RegisterMagickInfo(entry);
826 entry=AcquireMagickInfo("VIFF","XV","Khoros Visualization image");
827 entry->decoder=(DecodeImageHandler *) ReadVIFFImage;
828 entry->encoder=(EncodeImageHandler *) WriteVIFFImage;
829 entry->flags|=CoderDecoderSeekableStreamFlag;
830 (void) RegisterMagickInfo(entry);
831 return(MagickImageCoderSignature);
835 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
839 % U n r e g i s t e r V I F F I m a g e %
843 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
845 % UnregisterVIFFImage() removes format registrations made by the
846 % VIFF module from the list of supported formats.
848 % The format of the UnregisterVIFFImage method is:
850 % UnregisterVIFFImage(void)
853 ModuleExport void UnregisterVIFFImage(void)
855 (void) UnregisterMagickInfo("VIFF");
856 (void) UnregisterMagickInfo("XV");
860 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
864 % W r i t e V I F F I m a g e %
868 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
870 % WriteVIFFImage() writes an image to a file in the VIFF image format.
872 % The format of the WriteVIFFImage method is:
874 % MagickBooleanType WriteVIFFImage(const ImageInfo *image_info,
875 % Image *image,ExceptionInfo *exception)
877 % A description of each parameter follows.
879 % o image_info: the image info.
881 % o image: The image.
883 % o exception: return any errors or warnings in this structure.
886 static MagickBooleanType WriteVIFFImage(const ImageInfo *image_info,
887 Image *image,ExceptionInfo *exception)
889 #define VFF_CM_genericRGB 15
890 #define VFF_CM_NONE 0
891 #define VFF_DEP_IEEEORDER 0x2
892 #define VFF_DES_RAW 0
893 #define VFF_LOC_IMPLICIT 1
894 #define VFF_MAPTYP_NONE 0
895 #define VFF_MAPTYP_1_BYTE 1
896 #define VFF_MS_NONE 0
897 #define VFF_MS_ONEPERBAND 1
898 #define VFF_TYP_BIT 0
899 #define VFF_TYP_1_BYTE 1
901 typedef struct _ViffInfo
956 register const Quantum
965 register unsigned char
981 Open output image file.
983 assert(image_info != (const ImageInfo *) NULL);
984 assert(image_info->signature == MagickCoreSignature);
985 assert(image != (Image *) NULL);
986 assert(image->signature == MagickCoreSignature);
987 if (image->debug != MagickFalse)
988 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
989 assert(exception != (ExceptionInfo *) NULL);
990 assert(exception->signature == MagickCoreSignature);
991 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
992 if (status == MagickFalse)
994 (void) memset(&viff_info,0,sizeof(ViffInfo));
996 imageListLength=GetImageListLength(image);
1000 Initialize VIFF image structure.
1002 (void) TransformImageColorspace(image,sRGBColorspace,exception);
1003 DisableMSCWarning(4310)
1004 viff_info.identifier=(char) 0xab;
1006 viff_info.file_type=1;
1007 viff_info.release=1;
1008 viff_info.version=3;
1009 viff_info.machine_dependency=VFF_DEP_IEEEORDER; /* IEEE byte ordering */
1010 *viff_info.comment='\0';
1011 value=GetImageProperty(image,"comment",exception);
1012 if (value != (const char *) NULL)
1013 (void) CopyMagickString(viff_info.comment,value,MagickMin(strlen(value),
1015 viff_info.rows=image->columns;
1016 viff_info.columns=image->rows;
1017 viff_info.subrows=0;
1018 viff_info.x_offset=(~0);
1019 viff_info.y_offset=(~0);
1020 viff_info.x_bits_per_pixel=0;
1021 viff_info.y_bits_per_pixel=0;
1022 viff_info.location_type=VFF_LOC_IMPLICIT;
1023 viff_info.location_dimension=0;
1024 viff_info.number_of_images=1;
1025 viff_info.data_encode_scheme=VFF_DES_RAW;
1026 viff_info.map_scheme=VFF_MS_NONE;
1027 viff_info.map_storage_type=VFF_MAPTYP_NONE;
1028 viff_info.map_rows=0;
1029 viff_info.map_columns=0;
1030 viff_info.map_subrows=0;
1031 viff_info.map_enable=1; /* no colormap */
1032 viff_info.maps_per_cycle=0;
1033 number_pixels=(MagickSizeType) image->columns*image->rows;
1034 if (image->storage_class == DirectClass)
1037 Full color VIFF raster.
1039 viff_info.number_data_bands=image->alpha_trait ? 4U : 3U;
1040 viff_info.color_space_model=VFF_CM_genericRGB;
1041 viff_info.data_storage_type=VFF_TYP_1_BYTE;
1042 packets=viff_info.number_data_bands*number_pixels;
1046 viff_info.number_data_bands=1;
1047 viff_info.color_space_model=VFF_CM_NONE;
1048 viff_info.data_storage_type=VFF_TYP_1_BYTE;
1049 packets=number_pixels;
1050 if (SetImageGray(image,exception) == MagickFalse)
1053 Colormapped VIFF raster.
1055 viff_info.map_scheme=VFF_MS_ONEPERBAND;
1056 viff_info.map_storage_type=VFF_MAPTYP_1_BYTE;
1057 viff_info.map_rows=3;
1058 viff_info.map_columns=(unsigned int) image->colors;
1061 if (image->colors <= 2)
1064 Monochrome VIFF raster.
1066 viff_info.data_storage_type=VFF_TYP_BIT;
1067 packets=((image->columns+7) >> 3)*image->rows;
1071 Write VIFF image header (pad to 1024 bytes).
1073 (void) WriteBlob(image,sizeof(viff_info.identifier),(unsigned char *)
1074 &viff_info.identifier);
1075 (void) WriteBlob(image,sizeof(viff_info.file_type),(unsigned char *)
1076 &viff_info.file_type);
1077 (void) WriteBlob(image,sizeof(viff_info.release),(unsigned char *)
1078 &viff_info.release);
1079 (void) WriteBlob(image,sizeof(viff_info.version),(unsigned char *)
1080 &viff_info.version);
1081 (void) WriteBlob(image,sizeof(viff_info.machine_dependency),
1082 (unsigned char *) &viff_info.machine_dependency);
1083 (void) WriteBlob(image,sizeof(viff_info.reserve),(unsigned char *)
1085 (void) WriteBlob(image,512,(unsigned char *) viff_info.comment);
1086 (void) WriteBlobMSBLong(image,(unsigned int) viff_info.rows);
1087 (void) WriteBlobMSBLong(image,(unsigned int) viff_info.columns);
1088 (void) WriteBlobMSBLong(image,(unsigned int) viff_info.subrows);
1089 (void) WriteBlobMSBLong(image,(unsigned int) viff_info.x_offset);
1090 (void) WriteBlobMSBLong(image,(unsigned int) viff_info.y_offset);
1091 viff_info.x_bits_per_pixel=(unsigned int) ((63 << 24) | (128 << 16));
1092 (void) WriteBlobMSBLong(image,(unsigned int) viff_info.x_bits_per_pixel);
1093 viff_info.y_bits_per_pixel=(unsigned int) ((63 << 24) | (128 << 16));
1094 (void) WriteBlobMSBLong(image,(unsigned int) viff_info.y_bits_per_pixel);
1095 (void) WriteBlobMSBLong(image,viff_info.location_type);
1096 (void) WriteBlobMSBLong(image,viff_info.location_dimension);
1097 (void) WriteBlobMSBLong(image,(unsigned int) viff_info.number_of_images);
1098 (void) WriteBlobMSBLong(image,(unsigned int) viff_info.number_data_bands);
1099 (void) WriteBlobMSBLong(image,(unsigned int) viff_info.data_storage_type);
1100 (void) WriteBlobMSBLong(image,(unsigned int) viff_info.data_encode_scheme);
1101 (void) WriteBlobMSBLong(image,(unsigned int) viff_info.map_scheme);
1102 (void) WriteBlobMSBLong(image,(unsigned int) viff_info.map_storage_type);
1103 (void) WriteBlobMSBLong(image,(unsigned int) viff_info.map_rows);
1104 (void) WriteBlobMSBLong(image,(unsigned int) viff_info.map_columns);
1105 (void) WriteBlobMSBLong(image,(unsigned int) viff_info.map_subrows);
1106 (void) WriteBlobMSBLong(image,(unsigned int) viff_info.map_enable);
1107 (void) WriteBlobMSBLong(image,(unsigned int) viff_info.maps_per_cycle);
1108 (void) WriteBlobMSBLong(image,(unsigned int) viff_info.color_space_model);
1109 for (i=0; i < 420; i++)
1110 (void) WriteBlobByte(image,'\0');
1112 Convert MIFF to VIFF raster pixels.
1114 pixel_info=AcquireVirtualMemory((size_t) packets,sizeof(*pixels));
1115 if (pixel_info == (MemoryInfo *) NULL)
1116 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1117 pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info);
1119 if (image->storage_class == DirectClass)
1122 Convert DirectClass packet to VIFF RGB pixel.
1124 number_pixels=(MagickSizeType) image->columns*image->rows;
1125 for (y=0; y < (ssize_t) image->rows; y++)
1127 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1128 if (p == (const Quantum *) NULL)
1130 for (x=0; x < (ssize_t) image->columns; x++)
1132 *q=ScaleQuantumToChar(GetPixelRed(image,p));
1133 *(q+number_pixels)=ScaleQuantumToChar(GetPixelGreen(image,p));
1134 *(q+number_pixels*2)=ScaleQuantumToChar(GetPixelBlue(image,p));
1135 if (image->alpha_trait != UndefinedPixelTrait)
1136 *(q+number_pixels*3)=ScaleQuantumToChar((Quantum)
1137 (GetPixelAlpha(image,p)));
1138 p+=GetPixelChannels(image);
1141 if (image->previous == (Image *) NULL)
1143 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1145 if (status == MagickFalse)
1151 if (SetImageGray(image,exception) == MagickFalse)
1157 Dump colormap to file.
1159 viff_colormap=(unsigned char *) AcquireQuantumMemory(image->colors,
1160 3*sizeof(*viff_colormap));
1161 if (viff_colormap == (unsigned char *) NULL)
1162 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1164 for (i=0; i < (ssize_t) image->colors; i++)
1165 *q++=ScaleQuantumToChar(ClampToQuantum(image->colormap[i].red));
1166 for (i=0; i < (ssize_t) image->colors; i++)
1167 *q++=ScaleQuantumToChar(ClampToQuantum(image->colormap[i].green));
1168 for (i=0; i < (ssize_t) image->colors; i++)
1169 *q++=ScaleQuantumToChar(ClampToQuantum(image->colormap[i].blue));
1170 (void) WriteBlob(image,3*image->colors,viff_colormap);
1171 viff_colormap=(unsigned char *) RelinquishMagickMemory(viff_colormap);
1173 Convert PseudoClass packet to VIFF colormapped pixels.
1176 for (y=0; y < (ssize_t) image->rows; y++)
1178 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1179 if (p == (const Quantum *) NULL)
1181 for (x=0; x < (ssize_t) image->columns; x++)
1183 *q++=(unsigned char) GetPixelIndex(image,p);
1184 p+=GetPixelChannels(image);
1186 if (image->previous == (Image *) NULL)
1188 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1190 if (status == MagickFalse)
1196 if (image->colors <= 2)
1202 register unsigned char
1207 Convert PseudoClass image to a VIFF monochrome image.
1209 for (y=0; y < (ssize_t) image->rows; y++)
1211 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1212 if (p == (const Quantum *) NULL)
1216 for (x=0; x < (ssize_t) image->columns; x++)
1219 if (GetPixelLuma(image,p) < (QuantumRange/2.0))
1228 p+=GetPixelChannels(image);
1231 *q++=byte >> (8-bit);
1232 if (image->previous == (Image *) NULL)
1234 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
1236 if (status == MagickFalse)
1244 Convert PseudoClass packet to VIFF grayscale pixel.
1246 for (y=0; y < (ssize_t) image->rows; y++)
1248 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1249 if (p == (const Quantum *) NULL)
1251 for (x=0; x < (ssize_t) image->columns; x++)
1253 *q++=(unsigned char) ClampToQuantum(GetPixelLuma(image,p));
1254 p+=GetPixelChannels(image);
1256 if (image->previous == (Image *) NULL)
1258 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
1260 if (status == MagickFalse)
1265 (void) WriteBlob(image,(size_t) packets,pixels);
1266 pixel_info=RelinquishVirtualMemory(pixel_info);
1267 if (GetNextImageInList(image) == (Image *) NULL)
1269 image=SyncNextImageInList(image);
1270 status=SetImageProgress(image,SaveImagesTag,scene++,imageListLength);
1271 if (status == MagickFalse)
1273 } while (image_info->adjoin != MagickFalse);
1274 (void) CloseBlob(image);