blob: 036c8cec5b06244e39e0b34f2bfe8bf260d311ca [file] [log] [blame]
cristy3ed852e2009-09-05 21:47:34 +00001/*
2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3% %
4% %
5% %
6% V V IIIII FFFFF FFFFF %
7% V V I F F %
8% V V I FFF FFF %
9% V V I F F %
10% V IIIII F F %
11% %
12% %
13% Read/Write Khoros Visualization Image Format %
14% %
15% Software Design %
16% John Cristy %
17% July 1992 %
18% %
19% %
cristy7e41fe82010-12-04 23:12:08 +000020% Copyright 1999-2011 ImageMagick Studio LLC, a non-profit organization %
cristy3ed852e2009-09-05 21:47:34 +000021% dedicated to making software imaging solutions freely available. %
22% %
23% You may not use this file except in compliance with the License. You may %
24% obtain a copy of the License at %
25% %
26% http://www.imagemagick.org/script/license.php %
27% %
28% Unless required by applicable law or agreed to in writing, software %
29% distributed under the License is distributed on an "AS IS" BASIS, %
30% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31% See the License for the specific language governing permissions and %
32% limitations under the License. %
33% %
34%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35%
36%
37*/
38
39/*
40 Include declarations.
41*/
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"
cristye7e40552010-04-24 21:34:22 +000048#include "magick/colormap.h"
cristy3ed852e2009-09-05 21:47:34 +000049#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"
64
65/*
66 Forward declarations.
67*/
68static MagickBooleanType
69 WriteVIFFImage(const ImageInfo *,Image *);
70
71/*
72%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
73% %
74% %
75% %
76% I s V I F F %
77% %
78% %
79% %
80%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
81%
82% IsVIFF() returns MagickTrue if the image format type, identified by the
83% magick string, is VIFF.
84%
85% The format of the IsVIFF method is:
86%
87% MagickBooleanType IsVIFF(const unsigned char *magick,const size_t length)
88%
89% A description of each parameter follows:
90%
91% o magick: compare image format pattern against these bytes.
92%
93% o length: Specifies the length of the magick string.
94%
cristy3ed852e2009-09-05 21:47:34 +000095*/
96static MagickBooleanType IsVIFF(const unsigned char *magick,const size_t length)
97{
98 if (length < 2)
99 return(MagickFalse);
100 if (memcmp(magick,"\253\001",2) == 0)
101 return(MagickTrue);
102 return(MagickFalse);
103}
104
105/*
106%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
107% %
108% %
109% %
110% R e a d V I F F I m a g e %
111% %
112% %
113% %
114%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
115%
116% ReadVIFFImage() reads a Khoros Visualization image file and returns
117% it. It allocates the memory necessary for the new Image structure and
118% returns a pointer to the new image.
119%
120% The format of the ReadVIFFImage method is:
121%
122% Image *ReadVIFFImage(const ImageInfo *image_info,
123% ExceptionInfo *exception)
124%
125% A description of each parameter follows:
126%
127% o image: Method ReadVIFFImage returns a pointer to the image after
128% reading. A null image is returned if there is a memory shortage or if
129% the image cannot be read.
130%
131% o image_info: the image info.
132%
133% o exception: return any errors or warnings in this structure.
134%
135*/
136static Image *ReadVIFFImage(const ImageInfo *image_info,
137 ExceptionInfo *exception)
138{
139#define VFF_CM_genericRGB 15
140#define VFF_CM_ntscRGB 1
141#define VFF_CM_NONE 0
142#define VFF_DEP_DECORDER 0x4
143#define VFF_DEP_NSORDER 0x8
144#define VFF_DES_RAW 0
145#define VFF_LOC_IMPLICIT 1
146#define VFF_MAPTYP_NONE 0
147#define VFF_MAPTYP_1_BYTE 1
148#define VFF_MAPTYP_2_BYTE 2
149#define VFF_MAPTYP_4_BYTE 4
150#define VFF_MAPTYP_FLOAT 5
151#define VFF_MAPTYP_DOUBLE 7
152#define VFF_MS_NONE 0
153#define VFF_MS_ONEPERBAND 1
154#define VFF_MS_SHARED 3
155#define VFF_TYP_BIT 0
156#define VFF_TYP_1_BYTE 1
157#define VFF_TYP_2_BYTE 2
158#define VFF_TYP_4_BYTE 4
159#define VFF_TYP_FLOAT 5
160#define VFF_TYP_DOUBLE 9
161
162 typedef struct _ViffInfo
163 {
164 unsigned char
165 identifier,
166 file_type,
167 release,
168 version,
169 machine_dependency,
170 reserve[3];
171
172 char
173 comment[512];
174
cristy5d89f432010-05-30 00:00:26 +0000175 unsigned int
cristy3ed852e2009-09-05 21:47:34 +0000176 rows,
177 columns,
178 subrows;
179
cristyeaedf062010-05-29 22:36:02 +0000180 int
cristy3ed852e2009-09-05 21:47:34 +0000181 x_offset,
182 y_offset;
183
184 float
185 x_bits_per_pixel,
186 y_bits_per_pixel;
187
cristyeaedf062010-05-29 22:36:02 +0000188 unsigned int
cristy3ed852e2009-09-05 21:47:34 +0000189 location_type,
190 location_dimension,
191 number_of_images,
192 number_data_bands,
193 data_storage_type,
194 data_encode_scheme,
195 map_scheme,
196 map_storage_type,
197 map_rows,
198 map_columns,
199 map_subrows,
200 map_enable,
201 maps_per_cycle,
202 color_space_model;
203 } ViffInfo;
204
205 double
206 min_value,
207 scale_factor,
208 value;
209
210 Image
211 *image;
212
213 int
214 bit;
215
cristybb503372010-05-27 20:51:26 +0000216 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000217 y;
218
219 MagickBooleanType
220 status;
221
222 MagickSizeType
223 number_pixels;
224
225 register IndexPacket
226 *indexes;
227
cristybb503372010-05-27 20:51:26 +0000228 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000229 x;
230
231 register PixelPacket
232 *q;
233
cristybb503372010-05-27 20:51:26 +0000234 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000235 i;
236
237 register unsigned char
238 *p;
239
240 ssize_t
241 count;
242
243 unsigned char
244 buffer[7],
245 *viff_pixels;
246
cristybb503372010-05-27 20:51:26 +0000247 size_t
cristy3ed852e2009-09-05 21:47:34 +0000248 bytes_per_pixel,
249 lsb_first,
250 max_packets,
251 quantum;
252
253 ViffInfo
254 viff_info;
255
256 /*
257 Open image file.
258 */
259 assert(image_info != (const ImageInfo *) NULL);
260 assert(image_info->signature == MagickSignature);
261 if (image_info->debug != MagickFalse)
262 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
263 image_info->filename);
264 assert(exception != (ExceptionInfo *) NULL);
265 assert(exception->signature == MagickSignature);
266 image=AcquireImage(image_info);
267 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
268 if (status == MagickFalse)
269 {
270 image=DestroyImageList(image);
271 return((Image *) NULL);
272 }
273 /*
274 Read VIFF header (1024 bytes).
275 */
276 count=ReadBlob(image,1,&viff_info.identifier);
277 do
278 {
279 /*
280 Verify VIFF identifier.
281 */
282 if ((count == 0) || ((unsigned char) viff_info.identifier != 0xab))
283 ThrowReaderException(CorruptImageError,"NotAVIFFImage");
284 /*
285 Initialize VIFF image.
286 */
287 count=ReadBlob(image,7,buffer);
288 viff_info.file_type=buffer[0];
289 viff_info.release=buffer[1];
290 viff_info.version=buffer[2];
291 viff_info.machine_dependency=buffer[3];
292 count=ReadBlob(image,512,(unsigned char *) viff_info.comment);
293 viff_info.comment[511]='\0';
294 if (strlen(viff_info.comment) > 4)
295 (void) SetImageProperty(image,"comment",viff_info.comment);
296 if ((viff_info.machine_dependency == VFF_DEP_DECORDER) ||
297 (viff_info.machine_dependency == VFF_DEP_NSORDER))
298 {
299 viff_info.rows=ReadBlobLSBLong(image);
300 viff_info.columns=ReadBlobLSBLong(image);
301 viff_info.subrows=ReadBlobLSBLong(image);
cristy6cff05d2010-09-02 11:22:46 +0000302 viff_info.x_offset=(int) ReadBlobLSBLong(image);
303 viff_info.y_offset=(int) ReadBlobLSBLong(image);
cristy3ed852e2009-09-05 21:47:34 +0000304 viff_info.x_bits_per_pixel=(float) ReadBlobLSBLong(image);
305 viff_info.y_bits_per_pixel=(float) ReadBlobLSBLong(image);
306 viff_info.location_type=ReadBlobLSBLong(image);
307 viff_info.location_dimension=ReadBlobLSBLong(image);
308 viff_info.number_of_images=ReadBlobLSBLong(image);
309 viff_info.number_data_bands=ReadBlobLSBLong(image);
310 viff_info.data_storage_type=ReadBlobLSBLong(image);
311 viff_info.data_encode_scheme=ReadBlobLSBLong(image);
312 viff_info.map_scheme=ReadBlobLSBLong(image);
313 viff_info.map_storage_type=ReadBlobLSBLong(image);
314 viff_info.map_rows=ReadBlobLSBLong(image);
315 viff_info.map_columns=ReadBlobLSBLong(image);
316 viff_info.map_subrows=ReadBlobLSBLong(image);
317 viff_info.map_enable=ReadBlobLSBLong(image);
318 viff_info.maps_per_cycle=ReadBlobLSBLong(image);
319 viff_info.color_space_model=ReadBlobLSBLong(image);
320 }
321 else
322 {
323 viff_info.rows=ReadBlobMSBLong(image);
324 viff_info.columns=ReadBlobMSBLong(image);
325 viff_info.subrows=ReadBlobMSBLong(image);
cristy6cff05d2010-09-02 11:22:46 +0000326 viff_info.x_offset=(int) ReadBlobMSBLong(image);
327 viff_info.y_offset=(int) ReadBlobMSBLong(image);
cristy3ed852e2009-09-05 21:47:34 +0000328 viff_info.x_bits_per_pixel=(float) ReadBlobMSBLong(image);
329 viff_info.y_bits_per_pixel=(float) ReadBlobMSBLong(image);
330 viff_info.location_type=ReadBlobMSBLong(image);
331 viff_info.location_dimension=ReadBlobMSBLong(image);
332 viff_info.number_of_images=ReadBlobMSBLong(image);
333 viff_info.number_data_bands=ReadBlobMSBLong(image);
334 viff_info.data_storage_type=ReadBlobMSBLong(image);
335 viff_info.data_encode_scheme=ReadBlobMSBLong(image);
336 viff_info.map_scheme=ReadBlobMSBLong(image);
337 viff_info.map_storage_type=ReadBlobMSBLong(image);
338 viff_info.map_rows=ReadBlobMSBLong(image);
339 viff_info.map_columns=ReadBlobMSBLong(image);
340 viff_info.map_subrows=ReadBlobMSBLong(image);
341 viff_info.map_enable=ReadBlobMSBLong(image);
342 viff_info.maps_per_cycle=ReadBlobMSBLong(image);
343 viff_info.color_space_model=ReadBlobMSBLong(image);
344 }
345 for (i=0; i < 420; i++)
346 (void) ReadBlobByte(image);
347 image->columns=viff_info.rows;
348 image->rows=viff_info.columns;
349 image->depth=viff_info.x_bits_per_pixel <= 8 ? 8UL : MAGICKCORE_QUANTUM_DEPTH;
350 /*
351 Verify that we can read this VIFF image.
352 */
353 number_pixels=(MagickSizeType) viff_info.columns*viff_info.rows;
354 if (number_pixels != (size_t) number_pixels)
355 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
356 if (number_pixels == 0)
357 ThrowReaderException(CoderError,"ImageColumnOrRowSizeIsNotSupported");
358 if ((viff_info.number_data_bands < 1) || (viff_info.number_data_bands > 4))
359 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
360 if ((viff_info.data_storage_type != VFF_TYP_BIT) &&
361 (viff_info.data_storage_type != VFF_TYP_1_BYTE) &&
362 (viff_info.data_storage_type != VFF_TYP_2_BYTE) &&
363 (viff_info.data_storage_type != VFF_TYP_4_BYTE) &&
364 (viff_info.data_storage_type != VFF_TYP_FLOAT) &&
365 (viff_info.data_storage_type != VFF_TYP_DOUBLE))
366 ThrowReaderException(CoderError,"DataStorageTypeIsNotSupported");
367 if (viff_info.data_encode_scheme != VFF_DES_RAW)
368 ThrowReaderException(CoderError,"DataEncodingSchemeIsNotSupported");
369 if ((viff_info.map_storage_type != VFF_MAPTYP_NONE) &&
370 (viff_info.map_storage_type != VFF_MAPTYP_1_BYTE) &&
371 (viff_info.map_storage_type != VFF_MAPTYP_2_BYTE) &&
372 (viff_info.map_storage_type != VFF_MAPTYP_4_BYTE) &&
373 (viff_info.map_storage_type != VFF_MAPTYP_FLOAT) &&
374 (viff_info.map_storage_type != VFF_MAPTYP_DOUBLE))
375 ThrowReaderException(CoderError,"MapStorageTypeIsNotSupported");
376 if ((viff_info.color_space_model != VFF_CM_NONE) &&
377 (viff_info.color_space_model != VFF_CM_ntscRGB) &&
378 (viff_info.color_space_model != VFF_CM_genericRGB))
379 ThrowReaderException(CoderError,"ColorspaceModelIsNotSupported");
380 if (viff_info.location_type != VFF_LOC_IMPLICIT)
381 ThrowReaderException(CoderError,"LocationTypeIsNotSupported");
382 if (viff_info.number_of_images != 1)
383 ThrowReaderException(CoderError,"NumberOfImagesIsNotSupported");
384 if (viff_info.map_rows == 0)
385 viff_info.map_scheme=VFF_MS_NONE;
386 switch ((int) viff_info.map_scheme)
387 {
388 case VFF_MS_NONE:
389 {
390 if (viff_info.number_data_bands < 3)
391 {
392 /*
393 Create linear color ramp.
394 */
395 image->colors=image->depth <= 8 ? 256UL : 65536UL;
396 if (viff_info.data_storage_type == VFF_TYP_BIT)
397 image->colors=2;
398 if (AcquireImageColormap(image,image->colors) == MagickFalse)
399 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
400 }
401 break;
402 }
403 case VFF_MS_ONEPERBAND:
404 case VFF_MS_SHARED:
405 {
406 unsigned char
407 *viff_colormap;
408
409 /*
410 Allocate VIFF colormap.
411 */
412 switch ((int) viff_info.map_storage_type)
413 {
414 case VFF_MAPTYP_1_BYTE: bytes_per_pixel=1; break;
415 case VFF_MAPTYP_2_BYTE: bytes_per_pixel=2; break;
416 case VFF_MAPTYP_4_BYTE: bytes_per_pixel=4; break;
417 case VFF_MAPTYP_FLOAT: bytes_per_pixel=4; break;
418 case VFF_MAPTYP_DOUBLE: bytes_per_pixel=8; break;
419 default: bytes_per_pixel=1; break;
420 }
421 image->colors=viff_info.map_columns;
422 if (AcquireImageColormap(image,image->colors) == MagickFalse)
423 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
424 viff_colormap=(unsigned char *) AcquireQuantumMemory(image->colors,
425 viff_info.map_rows*bytes_per_pixel*sizeof(*viff_colormap));
426 if (viff_colormap == (unsigned char *) NULL)
427 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
428 /*
429 Read VIFF raster colormap.
430 */
431 count=ReadBlob(image,bytes_per_pixel*image->colors*viff_info.map_rows,
432 viff_colormap);
433 lsb_first=1;
434 if (*(char *) &lsb_first &&
435 ((viff_info.machine_dependency != VFF_DEP_DECORDER) &&
436 (viff_info.machine_dependency != VFF_DEP_NSORDER)))
437 switch ((int) viff_info.map_storage_type)
438 {
439 case VFF_MAPTYP_2_BYTE:
440 {
441 MSBOrderShort(viff_colormap,(bytes_per_pixel*image->colors*
442 viff_info.map_rows));
443 break;
444 }
445 case VFF_MAPTYP_4_BYTE:
446 case VFF_MAPTYP_FLOAT:
447 {
448 MSBOrderLong(viff_colormap,(bytes_per_pixel*image->colors*
449 viff_info.map_rows));
450 break;
451 }
452 default: break;
453 }
cristybb503372010-05-27 20:51:26 +0000454 for (i=0; i < (ssize_t) (viff_info.map_rows*image->colors); i++)
cristy3ed852e2009-09-05 21:47:34 +0000455 {
456 switch ((int) viff_info.map_storage_type)
457 {
458 case VFF_MAPTYP_2_BYTE: value=1.0*((short *) viff_colormap)[i]; break;
459 case VFF_MAPTYP_4_BYTE: value=1.0*((int *) viff_colormap)[i]; break;
460 case VFF_MAPTYP_FLOAT: value=((float *) viff_colormap)[i]; break;
461 case VFF_MAPTYP_DOUBLE: value=((double *) viff_colormap)[i]; break;
462 default: value=1.0*viff_colormap[i]; break;
463 }
cristybb503372010-05-27 20:51:26 +0000464 if (i < (ssize_t) image->colors)
cristy3ed852e2009-09-05 21:47:34 +0000465 {
466 image->colormap[i].red=ScaleCharToQuantum((unsigned char) value);
467 image->colormap[i].green=
468 ScaleCharToQuantum((unsigned char) value);
469 image->colormap[i].blue=ScaleCharToQuantum((unsigned char) value);
470 }
471 else
cristybb503372010-05-27 20:51:26 +0000472 if (i < (ssize_t) (2*image->colors))
cristy3ed852e2009-09-05 21:47:34 +0000473 image->colormap[i % image->colors].green=
474 ScaleCharToQuantum((unsigned char) value);
475 else
cristybb503372010-05-27 20:51:26 +0000476 if (i < (ssize_t) (3*image->colors))
cristy3ed852e2009-09-05 21:47:34 +0000477 image->colormap[i % image->colors].blue=
478 ScaleCharToQuantum((unsigned char) value);
479 }
480 viff_colormap=(unsigned char *) RelinquishMagickMemory(viff_colormap);
481 break;
482 }
483 default:
484 ThrowReaderException(CoderError,"ColormapTypeNotSupported");
485 }
486 /*
487 Initialize image structure.
488 */
489 image->matte=viff_info.number_data_bands == 4 ? MagickTrue : MagickFalse;
490 image->storage_class=
491 (viff_info.number_data_bands < 3 ? PseudoClass : DirectClass);
492 image->columns=viff_info.rows;
493 image->rows=viff_info.columns;
494 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
495 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
496 break;
497 /*
498 Allocate VIFF pixels.
499 */
500 switch ((int) viff_info.data_storage_type)
501 {
502 case VFF_TYP_2_BYTE: bytes_per_pixel=2; break;
503 case VFF_TYP_4_BYTE: bytes_per_pixel=4; break;
504 case VFF_TYP_FLOAT: bytes_per_pixel=4; break;
505 case VFF_TYP_DOUBLE: bytes_per_pixel=8; break;
506 default: bytes_per_pixel=1; break;
507 }
508 if (viff_info.data_storage_type == VFF_TYP_BIT)
509 max_packets=((image->columns+7UL) >> 3UL)*image->rows;
510 else
cristybb503372010-05-27 20:51:26 +0000511 max_packets=(size_t) (number_pixels*viff_info.number_data_bands);
cristy3ed852e2009-09-05 21:47:34 +0000512 viff_pixels=(unsigned char *) AcquireQuantumMemory(max_packets,
513 bytes_per_pixel*sizeof(*viff_pixels));
514 if (viff_pixels == (unsigned char *) NULL)
515 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
516 count=ReadBlob(image,bytes_per_pixel*max_packets,viff_pixels);
517 lsb_first=1;
518 if (*(char *) &lsb_first &&
519 ((viff_info.machine_dependency != VFF_DEP_DECORDER) &&
520 (viff_info.machine_dependency != VFF_DEP_NSORDER)))
521 switch ((int) viff_info.data_storage_type)
522 {
523 case VFF_TYP_2_BYTE:
524 {
525 MSBOrderShort(viff_pixels,bytes_per_pixel*max_packets);
526 break;
527 }
528 case VFF_TYP_4_BYTE:
529 case VFF_TYP_FLOAT:
530 {
531 MSBOrderLong(viff_pixels,bytes_per_pixel*max_packets);
532 break;
533 }
534 default: break;
535 }
536 min_value=0.0;
537 scale_factor=1.0;
538 if ((viff_info.data_storage_type != VFF_TYP_1_BYTE) &&
539 (viff_info.map_scheme == VFF_MS_NONE))
540 {
541 double
542 max_value;
543
544 /*
545 Determine scale factor.
546 */
547 switch ((int) viff_info.data_storage_type)
548 {
549 case VFF_TYP_2_BYTE: value=1.0*((short *) viff_pixels)[0]; break;
550 case VFF_TYP_4_BYTE: value=1.0*((int *) viff_pixels)[0]; break;
551 case VFF_TYP_FLOAT: value=((float *) viff_pixels)[0]; break;
552 case VFF_TYP_DOUBLE: value=((double *) viff_pixels)[0]; break;
553 default: value=1.0*viff_pixels[0]; break;
554 }
555 max_value=value;
556 min_value=value;
cristybb503372010-05-27 20:51:26 +0000557 for (i=0; i < (ssize_t) max_packets; i++)
cristy3ed852e2009-09-05 21:47:34 +0000558 {
559 switch ((int) viff_info.data_storage_type)
560 {
561 case VFF_TYP_2_BYTE: value=1.0*((short *) viff_pixels)[i]; break;
562 case VFF_TYP_4_BYTE: value=1.0*((int *) viff_pixels)[i]; break;
563 case VFF_TYP_FLOAT: value=((float *) viff_pixels)[i]; break;
564 case VFF_TYP_DOUBLE: value=((double *) viff_pixels)[i]; break;
565 default: value=1.0*viff_pixels[i]; break;
566 }
567 if (value > max_value)
568 max_value=value;
569 else
570 if (value < min_value)
571 min_value=value;
572 }
573 if ((min_value == 0) && (max_value == 0))
574 scale_factor=0;
575 else
576 if (min_value == max_value)
577 {
578 scale_factor=(MagickRealType) QuantumRange/min_value;
579 min_value=0;
580 }
581 else
582 scale_factor=(MagickRealType) QuantumRange/(max_value-min_value);
583 }
584 /*
585 Convert pixels to Quantum size.
586 */
587 p=(unsigned char *) viff_pixels;
cristybb503372010-05-27 20:51:26 +0000588 for (i=0; i < (ssize_t) max_packets; i++)
cristy3ed852e2009-09-05 21:47:34 +0000589 {
590 switch ((int) viff_info.data_storage_type)
591 {
592 case VFF_TYP_2_BYTE: value=1.0*((short *) viff_pixels)[i]; break;
593 case VFF_TYP_4_BYTE: value=1.0*((int *) viff_pixels)[i]; break;
594 case VFF_TYP_FLOAT: value=((float *) viff_pixels)[i]; break;
595 case VFF_TYP_DOUBLE: value=((double *) viff_pixels)[i]; break;
596 default: value=1.0*viff_pixels[i]; break;
597 }
598 if (viff_info.map_scheme == VFF_MS_NONE)
599 {
600 value=(value-min_value)*scale_factor;
601 if (value > QuantumRange)
602 value=QuantumRange;
603 else
604 if (value < 0)
605 value=0;
606 }
607 *p=(unsigned char) value;
608 p++;
609 }
610 /*
611 Convert VIFF raster image to pixel packets.
612 */
613 p=(unsigned char *) viff_pixels;
614 if (viff_info.data_storage_type == VFF_TYP_BIT)
615 {
616 /*
617 Convert bitmap scanline.
618 */
619 (void) SetImageType(image,BilevelType);
620 (void) SetImageType(image,PaletteType);
cristybb503372010-05-27 20:51:26 +0000621 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000622 {
623 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
624 if (q == (PixelPacket *) NULL)
625 break;
626 indexes=GetAuthenticIndexQueue(image);
cristybb503372010-05-27 20:51:26 +0000627 for (x=0; x < (ssize_t) (image->columns-7); x+=8)
cristy3ed852e2009-09-05 21:47:34 +0000628 {
629 for (bit=0; bit < 8; bit++)
630 if (PixelIntensity(q) < ((MagickRealType) QuantumRange/2.0))
631 {
cristybb503372010-05-27 20:51:26 +0000632 quantum=(size_t) indexes[x+bit];
cristy3ed852e2009-09-05 21:47:34 +0000633 quantum|=0x01;
634 indexes[x+bit]=(IndexPacket) quantum;
635 }
636 p++;
637 }
638 if ((image->columns % 8) != 0)
639 {
cristybb503372010-05-27 20:51:26 +0000640 for (bit=0; bit < (ssize_t) (image->columns % 8); bit++)
cristy3ed852e2009-09-05 21:47:34 +0000641 if (PixelIntensity(q) < ((MagickRealType) QuantumRange/2.0))
642 {
cristybb503372010-05-27 20:51:26 +0000643 quantum=(size_t) indexes[x+bit];
cristy3ed852e2009-09-05 21:47:34 +0000644 quantum|=0x01;
645 indexes[x+bit]=(IndexPacket) quantum;
646 }
647 p++;
648 }
649 if (SyncAuthenticPixels(image,exception) == MagickFalse)
650 break;
651 if (image->previous == (Image *) NULL)
652 {
cristycee97112010-05-28 00:44:52 +0000653 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
654 image->rows);
cristy3ed852e2009-09-05 21:47:34 +0000655 if (status == MagickFalse)
656 break;
657 }
658 }
659 }
660 else
661 if (image->storage_class == PseudoClass)
cristybb503372010-05-27 20:51:26 +0000662 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000663 {
664 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
665 if (q == (PixelPacket *) NULL)
666 break;
667 indexes=GetAuthenticIndexQueue(image);
cristybb503372010-05-27 20:51:26 +0000668 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000669 indexes[x]=(IndexPacket) (*p++);
670 if (SyncAuthenticPixels(image,exception) == MagickFalse)
671 break;
672 if (image->previous == (Image *) NULL)
673 {
cristycee97112010-05-28 00:44:52 +0000674 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
675 image->rows);
cristy3ed852e2009-09-05 21:47:34 +0000676 if (status == MagickFalse)
677 break;
678 }
679 }
680 else
681 {
682 /*
683 Convert DirectColor scanline.
684 */
685 number_pixels=(MagickSizeType) image->columns*image->rows;
cristybb503372010-05-27 20:51:26 +0000686 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000687 {
688 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
689 if (q == (PixelPacket *) NULL)
690 break;
cristybb503372010-05-27 20:51:26 +0000691 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000692 {
cristy5fde9aa2011-04-23 01:31:53 +0000693 SetRedPixelComponent(q,ScaleCharToQuantum(*p));
cristy3ed852e2009-09-05 21:47:34 +0000694 q->green=ScaleCharToQuantum(*(p+number_pixels));
695 q->blue=ScaleCharToQuantum(*(p+2*number_pixels));
696 if (image->colors != 0)
697 {
cristybb503372010-05-27 20:51:26 +0000698 q->red=image->colormap[(ssize_t) q->red].red;
699 q->green=image->colormap[(ssize_t) q->green].green;
700 q->blue=image->colormap[(ssize_t) q->blue].blue;
cristy3ed852e2009-09-05 21:47:34 +0000701 }
702 q->opacity=(Quantum) (image->matte ? QuantumRange-
703 ScaleCharToQuantum(*(p+number_pixels*3)) : OpaqueOpacity);
704 p++;
705 q++;
706 }
707 if (SyncAuthenticPixels(image,exception) == MagickFalse)
708 break;
709 if (image->previous == (Image *) NULL)
710 {
cristycee97112010-05-28 00:44:52 +0000711 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
712 image->rows);
cristy3ed852e2009-09-05 21:47:34 +0000713 if (status == MagickFalse)
714 break;
715 }
716 }
717 }
718 viff_pixels=(unsigned char *) RelinquishMagickMemory(viff_pixels);
719 if (image->storage_class == PseudoClass)
720 (void) SyncImage(image);
721 if (EOFBlob(image) != MagickFalse)
722 {
723 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
724 image->filename);
725 break;
726 }
727 /*
728 Proceed to next image.
729 */
730 if (image_info->number_scenes != 0)
731 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
732 break;
733 count=ReadBlob(image,1,&viff_info.identifier);
734 if ((count != 0) && (viff_info.identifier == 0xab))
735 {
736 /*
737 Allocate next image structure.
738 */
739 AcquireNextImage(image_info,image);
740 if (GetNextImageInList(image) == (Image *) NULL)
741 {
742 image=DestroyImageList(image);
743 return((Image *) NULL);
744 }
745 image=SyncNextImageInList(image);
746 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
747 GetBlobSize(image));
748 if (status == MagickFalse)
749 break;
750 }
751 } while ((count != 0) && (viff_info.identifier == 0xab));
752 (void) CloseBlob(image);
753 return(GetFirstImageInList(image));
754}
755
756/*
757%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
758% %
759% %
760% %
761% R e g i s t e r V I F F I m a g e %
762% %
763% %
764% %
765%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
766%
767% RegisterVIFFImage() adds properties for the VIFF image format to
768% the list of supported formats. The properties include the image format
769% tag, a method to read and/or write the format, whether the format
770% supports the saving of more than one frame to the same file or blob,
771% whether the format supports native in-memory I/O, and a brief
772% description of the format.
773%
774% The format of the RegisterVIFFImage method is:
775%
cristybb503372010-05-27 20:51:26 +0000776% size_t RegisterVIFFImage(void)
cristy3ed852e2009-09-05 21:47:34 +0000777%
778*/
cristybb503372010-05-27 20:51:26 +0000779ModuleExport size_t RegisterVIFFImage(void)
cristy3ed852e2009-09-05 21:47:34 +0000780{
781 MagickInfo
782 *entry;
783
784 entry=SetMagickInfo("VIFF");
785 entry->decoder=(DecodeImageHandler *) ReadVIFFImage;
786 entry->encoder=(EncodeImageHandler *) WriteVIFFImage;
787 entry->magick=(IsImageFormatHandler *) IsVIFF;
788 entry->description=ConstantString("Khoros Visualization image");
789 entry->module=ConstantString("VIFF");
790 (void) RegisterMagickInfo(entry);
791 entry=SetMagickInfo("XV");
792 entry->decoder=(DecodeImageHandler *) ReadVIFFImage;
793 entry->encoder=(EncodeImageHandler *) WriteVIFFImage;
794 entry->description=ConstantString("Khoros Visualization image");
795 entry->module=ConstantString("VIFF");
796 (void) RegisterMagickInfo(entry);
797 return(MagickImageCoderSignature);
798}
799
800/*
801%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
802% %
803% %
804% %
805% U n r e g i s t e r V I F F I m a g e %
806% %
807% %
808% %
809%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
810%
811% UnregisterVIFFImage() removes format registrations made by the
812% VIFF module from the list of supported formats.
813%
814% The format of the UnregisterVIFFImage method is:
815%
816% UnregisterVIFFImage(void)
817%
818*/
819ModuleExport void UnregisterVIFFImage(void)
820{
821 (void) UnregisterMagickInfo("VIFF");
822 (void) UnregisterMagickInfo("XV");
823}
824
825/*
826%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
827% %
828% %
829% %
830% W r i t e V I F F I m a g e %
831% %
832% %
833% %
834%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
835%
836% WriteVIFFImage() writes an image to a file in the VIFF image format.
837%
838% The format of the WriteVIFFImage method is:
839%
840% MagickBooleanType WriteVIFFImage(const ImageInfo *image_info,
841% Image *image)
842%
843% A description of each parameter follows.
844%
845% o image_info: the image info.
846%
847% o image: The image.
848%
849*/
850
851static inline size_t MagickMin(const size_t x,const size_t y)
852{
853 if (x < y)
854 return(x);
855 return(y);
856}
857
858static MagickBooleanType WriteVIFFImage(const ImageInfo *image_info,
859 Image *image)
860{
861#define VFF_CM_genericRGB 15
862#define VFF_CM_NONE 0
863#define VFF_DEP_IEEEORDER 0x2
864#define VFF_DES_RAW 0
865#define VFF_LOC_IMPLICIT 1
866#define VFF_MAPTYP_NONE 0
867#define VFF_MAPTYP_1_BYTE 1
868#define VFF_MS_NONE 0
869#define VFF_MS_ONEPERBAND 1
870#define VFF_TYP_BIT 0
871#define VFF_TYP_1_BYTE 1
872
873 typedef struct _ViffInfo
874 {
875 char
876 identifier,
877 file_type,
878 release,
879 version,
880 machine_dependency,
881 reserve[3],
882 comment[512];
883
cristybb503372010-05-27 20:51:26 +0000884 size_t
cristy3ed852e2009-09-05 21:47:34 +0000885 rows,
886 columns,
887 subrows;
888
cristy5d89f432010-05-30 00:00:26 +0000889 int
cristy3ed852e2009-09-05 21:47:34 +0000890 x_offset,
891 y_offset;
892
893 unsigned int
894 x_bits_per_pixel,
cristy5d89f432010-05-30 00:00:26 +0000895 y_bits_per_pixel,
cristy3ed852e2009-09-05 21:47:34 +0000896 location_type,
897 location_dimension,
898 number_of_images,
899 number_data_bands,
900 data_storage_type,
901 data_encode_scheme,
902 map_scheme,
903 map_storage_type,
904 map_rows,
905 map_columns,
906 map_subrows,
907 map_enable,
908 maps_per_cycle,
909 color_space_model;
910 } ViffInfo;
911
912 const char
913 *value;
914
cristybb503372010-05-27 20:51:26 +0000915 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000916 y;
917
918 MagickBooleanType
919 status;
920
921 MagickOffsetType
922 scene;
923
924 MagickSizeType
925 number_pixels,
926 packets;
927
928 register const IndexPacket
929 *indexes;
930
931 register const PixelPacket
932 *p;
933
cristybb503372010-05-27 20:51:26 +0000934 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000935 x;
936
cristybb503372010-05-27 20:51:26 +0000937 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000938 i;
939
940 register unsigned char
941 *q;
942
943 unsigned char
944 buffer[8],
945 *viff_pixels;
946
947 ViffInfo
948 viff_info;
949
950 /*
951 Open output image file.
952 */
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)
961 return(status);
962 (void) ResetMagickMemory(&viff_info,0,sizeof(ViffInfo));
963 scene=0;
964 do
965 {
966 /*
967 Initialize VIFF image structure.
968 */
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;
975 viff_info.release=1;
976 viff_info.version=3;
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;
984 viff_info.subrows=0;
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)
1002 {
1003 /*
1004 Full color VIFF raster.
1005 */
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;
1010 }
1011 else
1012 {
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)
1018 {
1019 /*
1020 Colormapped VIFF raster.
1021 */
1022 viff_info.map_scheme=VFF_MS_ONEPERBAND;
1023 viff_info.map_storage_type=VFF_MAPTYP_1_BYTE;
1024 viff_info.map_rows=3;
cristy0b29b252010-05-30 01:59:46 +00001025 viff_info.map_columns=(unsigned int) image->colors;
cristy3ed852e2009-09-05 21:47:34 +00001026 }
1027 else
1028 if (image->colors <= 2)
1029 {
1030 /*
1031 Monochrome VIFF raster.
1032 */
1033 viff_info.data_storage_type=VFF_TYP_BIT;
1034 packets=((image->columns+7) >> 3)*image->rows;
1035 }
1036 }
1037 /*
1038 Write VIFF image header (pad to 1024 bytes).
1039 */
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);
cristy0b29b252010-05-30 01:59:46 +00001050 (void) WriteBlobMSBLong(image,(unsigned int) viff_info.rows);
1051 (void) WriteBlobMSBLong(image,(unsigned int) viff_info.columns);
cristyf6fe0a12010-05-30 00:44:47 +00001052 (void) WriteBlobMSBLong(image,(unsigned int) viff_info.subrows);
1053 (void) WriteBlobMSBLong(image,(unsigned int) viff_info.x_offset);
1054 (void) WriteBlobMSBLong(image,(unsigned int) viff_info.y_offset);
cristy3ed852e2009-09-05 21:47:34 +00001055 viff_info.x_bits_per_pixel=1U*(63 << 24) | (128 << 16);
cristyf6fe0a12010-05-30 00:44:47 +00001056 (void) WriteBlobMSBLong(image,(unsigned int) viff_info.x_bits_per_pixel);
cristy3ed852e2009-09-05 21:47:34 +00001057 viff_info.y_bits_per_pixel=1U*(63 << 24) | (128 << 16);
cristyf6fe0a12010-05-30 00:44:47 +00001058 (void) WriteBlobMSBLong(image,(unsigned int) viff_info.y_bits_per_pixel);
cristy3ed852e2009-09-05 21:47:34 +00001059 (void) WriteBlobMSBLong(image,viff_info.location_type);
1060 (void) WriteBlobMSBLong(image,viff_info.location_dimension);
cristyf6fe0a12010-05-30 00:44:47 +00001061 (void) WriteBlobMSBLong(image,(unsigned int) viff_info.number_of_images);
1062 (void) WriteBlobMSBLong(image,(unsigned int) viff_info.number_data_bands);
1063 (void) WriteBlobMSBLong(image,(unsigned int) viff_info.data_storage_type);
1064 (void) WriteBlobMSBLong(image,(unsigned int) viff_info.data_encode_scheme);
1065 (void) WriteBlobMSBLong(image,(unsigned int) viff_info.map_scheme);
1066 (void) WriteBlobMSBLong(image,(unsigned int) viff_info.map_storage_type);
1067 (void) WriteBlobMSBLong(image,(unsigned int) viff_info.map_rows);
1068 (void) WriteBlobMSBLong(image,(unsigned int) viff_info.map_columns);
1069 (void) WriteBlobMSBLong(image,(unsigned int) viff_info.map_subrows);
1070 (void) WriteBlobMSBLong(image,(unsigned int) viff_info.map_enable);
1071 (void) WriteBlobMSBLong(image,(unsigned int) viff_info.maps_per_cycle);
1072 (void) WriteBlobMSBLong(image,(unsigned int) viff_info.color_space_model);
cristy3ed852e2009-09-05 21:47:34 +00001073 for (i=0; i < 420; i++)
1074 (void) WriteBlobByte(image,'\0');
1075 /*
1076 Convert MIFF to VIFF raster pixels.
1077 */
1078 viff_pixels=(unsigned char *) AcquireQuantumMemory((size_t) packets,
1079 sizeof(*viff_pixels));
1080 if (viff_pixels == (unsigned char *) NULL)
1081 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1082 q=viff_pixels;
1083 if (image->storage_class == DirectClass)
1084 {
1085 /*
1086 Convert DirectClass packet to VIFF RGB pixel.
1087 */
1088 number_pixels=(MagickSizeType) image->columns*image->rows;
cristybb503372010-05-27 20:51:26 +00001089 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001090 {
1091 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1092 if (p == (const PixelPacket *) NULL)
1093 break;
cristybb503372010-05-27 20:51:26 +00001094 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001095 {
cristyce70c172010-01-07 17:15:30 +00001096 *q=ScaleQuantumToChar(GetRedPixelComponent(p));
1097 *(q+number_pixels)=ScaleQuantumToChar(GetGreenPixelComponent(p));
1098 *(q+number_pixels*2)=ScaleQuantumToChar(GetBluePixelComponent(p));
cristy3ed852e2009-09-05 21:47:34 +00001099 if (image->matte != MagickFalse)
1100 *(q+number_pixels*3)=ScaleQuantumToChar((Quantum)
cristy46f08202010-01-10 04:04:21 +00001101 (GetAlphaPixelComponent(p)));
cristy3ed852e2009-09-05 21:47:34 +00001102 p++;
1103 q++;
1104 }
1105 if (image->previous == (Image *) NULL)
1106 {
cristycee97112010-05-28 00:44:52 +00001107 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1108 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001109 if (status == MagickFalse)
1110 break;
1111 }
1112 }
1113 }
1114 else
1115 if (IsGrayImage(image,&image->exception) == MagickFalse)
1116 {
1117 unsigned char
1118 *viff_colormap;
1119
1120 /*
1121 Dump colormap to file.
1122 */
1123 viff_colormap=(unsigned char *) AcquireQuantumMemory(image->colors,
1124 3*sizeof(*viff_colormap));
1125 if (viff_colormap == (unsigned char *) NULL)
1126 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1127 q=viff_colormap;
cristybb503372010-05-27 20:51:26 +00001128 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00001129 *q++=ScaleQuantumToChar(image->colormap[i].red);
cristybb503372010-05-27 20:51:26 +00001130 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00001131 *q++=ScaleQuantumToChar(image->colormap[i].green);
cristybb503372010-05-27 20:51:26 +00001132 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00001133 *q++=ScaleQuantumToChar(image->colormap[i].blue);
1134 (void) WriteBlob(image,3*image->colors,viff_colormap);
1135 viff_colormap=(unsigned char *) RelinquishMagickMemory(viff_colormap);
1136 /*
1137 Convert PseudoClass packet to VIFF colormapped pixels.
1138 */
1139 q=viff_pixels;
cristybb503372010-05-27 20:51:26 +00001140 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001141 {
1142 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1143 if (p == (const PixelPacket *) NULL)
1144 break;
1145 indexes=GetVirtualIndexQueue(image);
cristybb503372010-05-27 20:51:26 +00001146 for (x=0; x < (ssize_t) image->columns; x++)
cristy4e82e512011-04-24 01:33:42 +00001147 *q++=(unsigned char) GetIndexPixelComponent(indexes+x);
cristy3ed852e2009-09-05 21:47:34 +00001148 if (image->previous == (Image *) NULL)
1149 {
cristycee97112010-05-28 00:44:52 +00001150 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1151 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001152 if (status == MagickFalse)
1153 break;
1154 }
1155 }
1156 }
1157 else
1158 if (image->colors <= 2)
1159 {
cristybb503372010-05-27 20:51:26 +00001160 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001161 x,
1162 y;
1163
1164 register unsigned char
1165 bit,
1166 byte;
1167
1168 /*
1169 Convert PseudoClass image to a VIFF monochrome image.
1170 */
1171 (void) SetImageType(image,BilevelType);
cristybb503372010-05-27 20:51:26 +00001172 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001173 {
1174 p=GetVirtualPixels(image,0,y,image->columns,1,
1175 &image->exception);
1176 if (p == (const PixelPacket *) NULL)
1177 break;
1178 indexes=GetVirtualIndexQueue(image);
1179 bit=0;
1180 byte=0;
cristybb503372010-05-27 20:51:26 +00001181 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001182 {
1183 byte>>=1;
1184 if (PixelIntensity(p) < ((MagickRealType) QuantumRange/2.0))
1185 byte|=0x80;
1186 bit++;
1187 if (bit == 8)
1188 {
1189 *q++=byte;
1190 bit=0;
1191 byte=0;
1192 }
1193 }
1194 if (bit != 0)
1195 *q++=byte >> (8-bit);
1196 if (image->previous == (Image *) NULL)
1197 {
cristy5d89f432010-05-30 00:00:26 +00001198 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
1199 y,image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001200 if (status == MagickFalse)
1201 break;
1202 }
1203 }
1204 }
1205 else
1206 {
1207 /*
1208 Convert PseudoClass packet to VIFF grayscale pixel.
1209 */
cristybb503372010-05-27 20:51:26 +00001210 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001211 {
1212 p=GetVirtualPixels(image,0,y,image->columns,1,
1213 &image->exception);
1214 if (p == (const PixelPacket *) NULL)
1215 break;
cristybb503372010-05-27 20:51:26 +00001216 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001217 {
1218 *q++=(unsigned char) PixelIntensityToQuantum(p);
1219 p++;
1220 }
1221 if (image->previous == (Image *) NULL)
1222 {
cristy5d89f432010-05-30 00:00:26 +00001223 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
1224 y,image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001225 if (status == MagickFalse)
1226 break;
1227 }
1228 }
1229 }
1230 (void) WriteBlob(image,(size_t) packets,viff_pixels);
1231 viff_pixels=(unsigned char *) RelinquishMagickMemory(viff_pixels);
1232 if (GetNextImageInList(image) == (Image *) NULL)
1233 break;
1234 image=SyncNextImageInList(image);
1235 status=SetImageProgress(image,SaveImagesTag,scene++,
1236 GetImageListLength(image));
1237 if (status == MagickFalse)
1238 break;
1239 } while (image_info->adjoin != MagickFalse);
1240 (void) CloseBlob(image);
1241 return(MagickTrue);
1242}