]> granicus.if.org Git - imagemagick/blob - coders/mpc.c
Fixed loop of doom.
[imagemagick] / coders / mpc.c
1 /*
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %                                                                             %
4 %                                                                             %
5 %                                                                             %
6 %                            M   M  PPPP    CCCC                              %
7 %                            MM MM  P   P  C                                  %
8 %                            M M M  PPPP   C                                  %
9 %                            M   M  P      C                                  %
10 %                            M   M  P       CCCC                              %
11 %                                                                             %
12 %                                                                             %
13 %              Read/Write Magick Persistant Cache Image Format                %
14 %                                                                             %
15 %                              Software Design                                %
16 %                                   Cristy                                    %
17 %                                 March 2000                                  %
18 %                                                                             %
19 %                                                                             %
20 %  Copyright 1999-2017 ImageMagick Studio LLC, a non-profit organization      %
21 %  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 \f
40 /*
41   Include declarations.
42 */
43 #include "MagickCore/studio.h"
44 #include "MagickCore/artifact.h"
45 #include "MagickCore/attribute.h"
46 #include "MagickCore/blob.h"
47 #include "MagickCore/blob-private.h"
48 #include "MagickCore/cache.h"
49 #include "MagickCore/color.h"
50 #include "MagickCore/color-private.h"
51 #include "MagickCore/colormap.h"
52 #include "MagickCore/constitute.h"
53 #include "MagickCore/exception.h"
54 #include "MagickCore/exception-private.h"
55 #include "MagickCore/geometry.h"
56 #include "MagickCore/image.h"
57 #include "MagickCore/image-private.h"
58 #include "MagickCore/linked-list.h"
59 #include "MagickCore/list.h"
60 #include "MagickCore/magick.h"
61 #include "MagickCore/memory_.h"
62 #include "MagickCore/module.h"
63 #include "MagickCore/monitor.h"
64 #include "MagickCore/monitor-private.h"
65 #include "MagickCore/option.h"
66 #include "MagickCore/profile.h"
67 #include "MagickCore/property.h"
68 #include "MagickCore/quantum-private.h"
69 #include "MagickCore/resource_.h"
70 #include "MagickCore/static.h"
71 #include "MagickCore/statistic.h"
72 #include "MagickCore/string_.h"
73 #include "MagickCore/string-private.h"
74 #include "MagickCore/utility.h"
75 #include "MagickCore/version-private.h"
76 \f
77 /*
78   Forward declarations.
79 */
80 static MagickBooleanType
81   WriteMPCImage(const ImageInfo *,Image *,ExceptionInfo *);
82 \f
83 /*
84 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
85 %                                                                             %
86 %                                                                             %
87 %                                                                             %
88 %   I s M P C                                                                 %
89 %                                                                             %
90 %                                                                             %
91 %                                                                             %
92 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
93 %
94 %  IsMPC() returns MagickTrue if the image format type, identified by the
95 %  magick string, is an Magick Persistent Cache image.
96 %
97 %  The format of the IsMPC method is:
98 %
99 %      MagickBooleanType IsMPC(const unsigned char *magick,const size_t length)
100 %
101 %  A description of each parameter follows:
102 %
103 %    o magick: compare image format pattern against these bytes.
104 %
105 %    o length: Specifies the length of the magick string.
106 %
107 */
108 static MagickBooleanType IsMPC(const unsigned char *magick,const size_t length)
109 {
110   if (length < 14)
111     return(MagickFalse);
112   if (LocaleNCompare((const char *) magick,"id=MagickCache",14) == 0)
113     return(MagickTrue);
114   return(MagickFalse);
115 }
116 \f
117 /*
118 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
119 %                                                                             %
120 %                                                                             %
121 %                                                                             %
122 %   R e a d C A C H E I m a g e                                               %
123 %                                                                             %
124 %                                                                             %
125 %                                                                             %
126 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
127 %
128 %  ReadMPCImage() reads an Magick Persistent Cache image file and returns
129 %  it.  It allocates the memory necessary for the new Image structure and
130 %  returns a pointer to the new image.
131 %
132 %  The format of the ReadMPCImage method is:
133 %
134 %      Image *ReadMPCImage(const ImageInfo *image_info,ExceptionInfo *exception)
135 %
136 %  Decompression code contributed by Kyle Shorter.
137 %
138 %  A description of each parameter follows:
139 %
140 %    o image_info: the image info.
141 %
142 %    o exception: return any errors or warnings in this structure.
143 %
144 */
145 static Image *ReadMPCImage(const ImageInfo *image_info,ExceptionInfo *exception)
146 {
147   char
148     cache_filename[MagickPathExtent],
149     id[MagickPathExtent],
150     keyword[MagickPathExtent],
151     *options;
152
153   const unsigned char
154     *p;
155
156   GeometryInfo
157     geometry_info;
158
159   Image
160     *image;
161
162   int
163     c;
164
165   LinkedListInfo
166     *profiles;
167
168   MagickBooleanType
169     status;
170
171   MagickOffsetType
172     offset;
173
174   MagickStatusType
175     flags;
176
177   register ssize_t
178     i;
179
180   size_t
181     depth,
182     length;
183
184   ssize_t
185     count;
186
187   StringInfo
188     *profile;
189
190   unsigned int
191     signature;
192
193   /*
194     Open image file.
195   */
196   assert(image_info != (const ImageInfo *) NULL);
197   assert(image_info->signature == MagickCoreSignature);
198   if (image_info->debug != MagickFalse)
199     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
200       image_info->filename);
201   assert(exception != (ExceptionInfo *) NULL);
202   assert(exception->signature == MagickCoreSignature);
203   image=AcquireImage(image_info,exception);
204   status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
205   if (status == MagickFalse)
206     {
207       image=DestroyImageList(image);
208       return((Image *) NULL);
209     }
210   (void) CopyMagickString(cache_filename,image->filename,MagickPathExtent);
211   AppendImageFormat("cache",cache_filename);
212   c=ReadBlobByte(image);
213   if (c == EOF)
214     {
215       image=DestroyImage(image);
216       return((Image *) NULL);
217     }
218   *id='\0';
219   (void) ResetMagickMemory(keyword,0,sizeof(keyword));
220   offset=0;
221   do
222   {
223     /*
224       Decode image header;  header terminates one character beyond a ':'.
225     */
226     profiles=(LinkedListInfo *) NULL;
227     length=MagickPathExtent;
228     options=AcquireString((char *) NULL);
229     signature=GetMagickSignature((const StringInfo *) NULL);
230     image->depth=8;
231     image->compression=NoCompression;
232     while ((isgraph(c) != MagickFalse) && (c != (int) ':'))
233     {
234       register char
235         *p;
236
237       if (c == (int) '{')
238         {
239           char
240             *comment;
241
242           /*
243             Read comment-- any text between { }.
244           */
245           length=MagickPathExtent;
246           comment=AcquireString((char *) NULL);
247           for (p=comment; comment != (char *) NULL; p++)
248           {
249             c=ReadBlobByte(image);
250             if (c == (int) '\\')
251               c=ReadBlobByte(image);
252             else
253               if ((c == EOF) || (c == (int) '}'))
254                 break;
255             if ((size_t) (p-comment+1) >= length)
256               {
257                 *p='\0';
258                 length<<=1;
259                 comment=(char *) ResizeQuantumMemory(comment,length+
260                   MagickPathExtent,sizeof(*comment));
261                 if (comment == (char *) NULL)
262                   break;
263                 p=comment+strlen(comment);
264               }
265             *p=(char) c;
266           }
267           if (comment == (char *) NULL)
268             ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
269           *p='\0';
270           (void) SetImageProperty(image,"comment",comment,exception);
271           comment=DestroyString(comment);
272           c=ReadBlobByte(image);
273         }
274       else
275         if (isalnum(c) != MagickFalse)
276           {
277             /*
278               Get the keyword.
279             */
280             length=MagickPathExtent;
281             p=keyword;
282             do
283             {
284               if (c == (int) '=')
285                 break;
286               if ((size_t) (p-keyword) < (MagickPathExtent-1))
287                 *p++=(char) c;
288               c=ReadBlobByte(image);
289             } while (c != EOF);
290             *p='\0';
291             p=options;
292             while (isspace((int) ((unsigned char) c)) != 0)
293               c=ReadBlobByte(image);
294             if (c == (int) '=')
295               {
296                 /*
297                   Get the keyword value.
298                 */
299                 c=ReadBlobByte(image);
300                 while ((c != (int) '}') && (c != EOF))
301                 {
302                   if ((size_t) (p-options+1) >= length)
303                     {
304                       *p='\0';
305                       length<<=1;
306                       options=(char *) ResizeQuantumMemory(options,length+
307                         MagickPathExtent,sizeof(*options));
308                       if (options == (char *) NULL)
309                         break;
310                       p=options+strlen(options);
311                     }
312                   *p++=(char) c;
313                   c=ReadBlobByte(image);
314                   if (c == '\\')
315                     {
316                       c=ReadBlobByte(image);
317                       if (c == (int) '}')
318                         {
319                           *p++=(char) c;
320                           c=ReadBlobByte(image);
321                         }
322                     }
323                   if (*options != '{')
324                     if (isspace((int) ((unsigned char) c)) != 0)
325                       break;
326                 }
327                 if (options == (char *) NULL)
328                   ThrowReaderException(ResourceLimitError,
329                     "MemoryAllocationFailed");
330               }
331             *p='\0';
332             if (*options == '{')
333               (void) CopyMagickString(options,options+1,strlen(options));
334             /*
335               Assign a value to the specified keyword.
336             */
337             switch (*keyword)
338             {
339               case 'a':
340               case 'A':
341               {
342                 if (LocaleCompare(keyword,"alpha-color") == 0)
343                   {
344                     (void) QueryColorCompliance(options,AllCompliance,
345                       &image->alpha_color,exception);
346                     break;
347                   }
348                 if (LocaleCompare(keyword,"alpha-trait") == 0)
349                   {
350                     ssize_t
351                       alpha_trait;
352
353                     alpha_trait=ParseCommandOption(MagickPixelTraitOptions,
354                       MagickFalse,options);
355                     if (alpha_trait < 0)
356                       break;
357                     image->alpha_trait=(PixelTrait) alpha_trait;
358                     break;
359                   }
360                 (void) SetImageProperty(image,keyword,options,exception);
361                 break;
362               }
363               case 'b':
364               case 'B':
365               {
366                 if (LocaleCompare(keyword,"background-color") == 0)
367                   {
368                     (void) QueryColorCompliance(options,AllCompliance,
369                       &image->background_color,exception);
370                     break;
371                   }
372                 if (LocaleCompare(keyword,"blue-primary") == 0)
373                   {
374                     flags=ParseGeometry(options,&geometry_info);
375                     image->chromaticity.blue_primary.x=geometry_info.rho;
376                     image->chromaticity.blue_primary.y=geometry_info.sigma;
377                     if ((flags & SigmaValue) == 0)
378                       image->chromaticity.blue_primary.y=
379                         image->chromaticity.blue_primary.x;
380                     break;
381                   }
382                 if (LocaleCompare(keyword,"border-color") == 0)
383                   {
384                     (void) QueryColorCompliance(options,AllCompliance,
385                       &image->border_color,exception);
386                     break;
387                   }
388                 (void) SetImageProperty(image,keyword,options,exception);
389                 break;
390               }
391               case 'c':
392               case 'C':
393               {
394                 if (LocaleCompare(keyword,"class") == 0)
395                   {
396                     ssize_t
397                       storage_class;
398
399                     storage_class=ParseCommandOption(MagickClassOptions,
400                       MagickFalse,options);
401                     if (storage_class < 0)
402                       break;
403                     image->storage_class=(ClassType) storage_class;
404                     break;
405                   }
406                 if (LocaleCompare(keyword,"colors") == 0)
407                   {
408                     image->colors=StringToUnsignedLong(options);
409                     break;
410                   }
411                 if (LocaleCompare(keyword,"colorspace") == 0)
412                   {
413                     ssize_t
414                       colorspace;
415
416                     colorspace=ParseCommandOption(MagickColorspaceOptions,
417                       MagickFalse,options);
418                     if (colorspace < 0)
419                       break;
420                     image->colorspace=(ColorspaceType) colorspace;
421                     break;
422                   }
423                 if (LocaleCompare(keyword,"compression") == 0)
424                   {
425                     ssize_t
426                       compression;
427
428                     compression=ParseCommandOption(MagickCompressOptions,
429                       MagickFalse,options);
430                     if (compression < 0)
431                       break;
432                     image->compression=(CompressionType) compression;
433                     break;
434                   }
435                 if (LocaleCompare(keyword,"columns") == 0)
436                   {
437                     image->columns=StringToUnsignedLong(options);
438                     break;
439                   }
440                 (void) SetImageProperty(image,keyword,options,exception);
441                 break;
442               }
443               case 'd':
444               case 'D':
445               {
446                 if (LocaleCompare(keyword,"delay") == 0)
447                   {
448                     image->delay=StringToUnsignedLong(options);
449                     break;
450                   }
451                 if (LocaleCompare(keyword,"depth") == 0)
452                   {
453                     image->depth=StringToUnsignedLong(options);
454                     break;
455                   }
456                 if (LocaleCompare(keyword,"dispose") == 0)
457                   {
458                     ssize_t
459                       dispose;
460
461                     dispose=ParseCommandOption(MagickDisposeOptions,MagickFalse,
462                       options);
463                     if (dispose < 0)
464                       break;
465                     image->dispose=(DisposeType) dispose;
466                     break;
467                   }
468                 (void) SetImageProperty(image,keyword,options,exception);
469                 break;
470               }
471               case 'e':
472               case 'E':
473               {
474                 if (LocaleCompare(keyword,"endian") == 0)
475                   {
476                     ssize_t
477                       endian;
478
479                     endian=ParseCommandOption(MagickEndianOptions,MagickFalse,
480                       options);
481                     if (endian < 0)
482                       break;
483                     image->endian=(EndianType) endian;
484                     break;
485                   }
486                 if (LocaleCompare(keyword,"error") == 0)
487                   {
488                     image->error.mean_error_per_pixel=StringToDouble(options,
489                       (char **) NULL);
490                     break;
491                   }
492                 (void) SetImageProperty(image,keyword,options,exception);
493                 break;
494               }
495               case 'g':
496               case 'G':
497               {
498                 if (LocaleCompare(keyword,"gamma") == 0)
499                   {
500                     image->gamma=StringToDouble(options,(char **) NULL);
501                     break;
502                   }
503                 if (LocaleCompare(keyword,"green-primary") == 0)
504                   {
505                     flags=ParseGeometry(options,&geometry_info);
506                     image->chromaticity.green_primary.x=geometry_info.rho;
507                     image->chromaticity.green_primary.y=geometry_info.sigma;
508                     if ((flags & SigmaValue) == 0)
509                       image->chromaticity.green_primary.y=
510                         image->chromaticity.green_primary.x;
511                     break;
512                   }
513                 (void) SetImageProperty(image,keyword,options,exception);
514                 break;
515               }
516               case 'i':
517               case 'I':
518               {
519                 if (LocaleCompare(keyword,"id") == 0)
520                   {
521                     (void) CopyMagickString(id,options,MagickPathExtent);
522                     break;
523                   }
524                 if (LocaleCompare(keyword,"iterations") == 0)
525                   {
526                     image->iterations=StringToUnsignedLong(options);
527                     break;
528                   }
529                 (void) SetImageProperty(image,keyword,options,exception);
530                 break;
531               }
532               case 'm':
533               case 'M':
534               {
535                 if (LocaleCompare(keyword,"magick-signature") == 0)
536                   {
537                     signature=(unsigned int) StringToUnsignedLong(options);
538                     break;
539                   }
540                 if (LocaleCompare(keyword,"maximum-error") == 0)
541                   {
542                     image->error.normalized_maximum_error=StringToDouble(
543                       options,(char **) NULL);
544                     break;
545                   }
546                 if (LocaleCompare(keyword,"mean-error") == 0)
547                   {
548                     image->error.normalized_mean_error=StringToDouble(options,
549                       (char **) NULL);
550                     break;
551                   }
552                 if (LocaleCompare(keyword,"montage") == 0)
553                   {
554                     (void) CloneString(&image->montage,options);
555                     break;
556                   }
557                 (void) SetImageProperty(image,keyword,options,exception);
558                 break;
559               }
560               case 'o':
561               case 'O':
562               {
563                 if (LocaleCompare(keyword,"orientation") == 0)
564                   {
565                     ssize_t
566                       orientation;
567
568                     orientation=ParseCommandOption(MagickOrientationOptions,
569                       MagickFalse,options);
570                     if (orientation < 0)
571                       break;
572                     image->orientation=(OrientationType) orientation;
573                     break;
574                   }
575                 (void) SetImageProperty(image,keyword,options,exception);
576                 break;
577               }
578               case 'p':
579               case 'P':
580               {
581                 if (LocaleCompare(keyword,"page") == 0)
582                   {
583                     char
584                       *geometry;
585
586                     geometry=GetPageGeometry(options);
587                     (void) ParseAbsoluteGeometry(geometry,&image->page);
588                     geometry=DestroyString(geometry);
589                     break;
590                   }
591                 if (LocaleCompare(keyword,"pixel-intensity") == 0)
592                   {
593                     ssize_t
594                       intensity;
595
596                     intensity=ParseCommandOption(MagickPixelIntensityOptions,
597                       MagickFalse,options);
598                     if (intensity < 0)
599                       break;
600                     image->intensity=(PixelIntensityMethod) intensity;
601                     break;
602                   }
603                 if ((LocaleNCompare(keyword,"profile:",8) == 0) ||
604                     (LocaleNCompare(keyword,"profile-",8) == 0))
605                   {
606                     if (profiles == (LinkedListInfo *) NULL)
607                       profiles=NewLinkedList(0);
608                     (void) AppendValueToLinkedList(profiles,
609                       AcquireString(keyword+8));
610                     profile=BlobToStringInfo((const void *) NULL,(size_t)
611                       StringToLong(options));
612                     if (profile == (StringInfo *) NULL)
613                       ThrowReaderException(ResourceLimitError,
614                         "MemoryAllocationFailed");
615                     (void) SetImageProfile(image,keyword+8,profile,exception);
616                     profile=DestroyStringInfo(profile);
617                     break;
618                   }
619                 (void) SetImageProperty(image,keyword,options,exception);
620                 break;
621               }
622               case 'q':
623               case 'Q':
624               {
625                 if (LocaleCompare(keyword,"quality") == 0)
626                   {
627                     image->quality=StringToUnsignedLong(options);
628                     break;
629                   }
630                 (void) SetImageProperty(image,keyword,options,exception);
631                 break;
632               }
633               case 'r':
634               case 'R':
635               {
636                 if (LocaleCompare(keyword,"red-primary") == 0)
637                   {
638                     flags=ParseGeometry(options,&geometry_info);
639                     image->chromaticity.red_primary.x=geometry_info.rho;
640                     if ((flags & SigmaValue) != 0)
641                       image->chromaticity.red_primary.y=geometry_info.sigma;
642                     break;
643                   }
644                 if (LocaleCompare(keyword,"rendering-intent") == 0)
645                   {
646                     ssize_t
647                       rendering_intent;
648
649                     rendering_intent=ParseCommandOption(MagickIntentOptions,
650                       MagickFalse,options);
651                     if (rendering_intent < 0)
652                       break;
653                     image->rendering_intent=(RenderingIntent) rendering_intent;
654                     break;
655                   }
656                 if (LocaleCompare(keyword,"resolution") == 0)
657                   {
658                     flags=ParseGeometry(options,&geometry_info);
659                     image->resolution.x=geometry_info.rho;
660                     image->resolution.y=geometry_info.sigma;
661                     if ((flags & SigmaValue) == 0)
662                       image->resolution.y=image->resolution.x;
663                     break;
664                   }
665                 if (LocaleCompare(keyword,"rows") == 0)
666                   {
667                     image->rows=StringToUnsignedLong(options);
668                     break;
669                   }
670                 (void) SetImageProperty(image,keyword,options,exception);
671                 break;
672               }
673               case 's':
674               case 'S':
675               {
676                 if (LocaleCompare(keyword,"scene") == 0)
677                   {
678                     image->scene=StringToUnsignedLong(options);
679                     break;
680                   }
681                 (void) SetImageProperty(image,keyword,options,exception);
682                 break;
683               }
684               case 't':
685               case 'T':
686               {
687                 if (LocaleCompare(keyword,"ticks-per-second") == 0)
688                   {
689                     image->ticks_per_second=(ssize_t) StringToLong(options);
690                     break;
691                   }
692                 if (LocaleCompare(keyword,"tile-offset") == 0)
693                   {
694                     char
695                       *geometry;
696
697                     geometry=GetPageGeometry(options);
698                     (void) ParseAbsoluteGeometry(geometry,&image->tile_offset);
699                     geometry=DestroyString(geometry);
700                   }
701                 if (LocaleCompare(keyword,"type") == 0)
702                   {
703                     ssize_t
704                       type;
705
706                     type=ParseCommandOption(MagickTypeOptions,MagickFalse,
707                       options);
708                     if (type < 0)
709                       break;
710                     image->type=(ImageType) type;
711                     break;
712                   }
713                 (void) SetImageProperty(image,keyword,options,exception);
714                 break;
715               }
716               case 'u':
717               case 'U':
718               {
719                 if (LocaleCompare(keyword,"units") == 0)
720                   {
721                     ssize_t
722                       units;
723
724                     units=ParseCommandOption(MagickResolutionOptions,
725                       MagickFalse,options);
726                     if (units < 0)
727                       break;
728                     image->units=(ResolutionType) units;
729                     break;
730                   }
731                 (void) SetImageProperty(image,keyword,options,exception);
732                 break;
733               }
734               case 'w':
735               case 'W':
736               {
737                 if (LocaleCompare(keyword,"white-point") == 0)
738                   {
739                     flags=ParseGeometry(options,&geometry_info);
740                     image->chromaticity.white_point.x=geometry_info.rho;
741                     image->chromaticity.white_point.y=geometry_info.sigma;
742                     if ((flags & SigmaValue) == 0)
743                       image->chromaticity.white_point.y=
744                         image->chromaticity.white_point.x;
745                     break;
746                   }
747                 (void) SetImageProperty(image,keyword,options,exception);
748                 break;
749               }
750               default:
751               {
752                 (void) SetImageProperty(image,keyword,options,exception);
753                 break;
754               }
755             }
756           }
757         else
758           c=ReadBlobByte(image);
759       while (isspace((int) ((unsigned char) c)) != 0)
760         c=ReadBlobByte(image);
761     }
762     options=DestroyString(options);
763     (void) ReadBlobByte(image);
764     /*
765       Verify that required image information is defined.
766     */
767     if ((LocaleCompare(id,"MagickCache") != 0) ||
768         (image->storage_class == UndefinedClass) ||
769         (image->compression == UndefinedCompression) || (image->columns == 0) ||
770         (image->rows == 0))
771       ThrowReaderException(CorruptImageError,"ImproperImageHeader");
772     if (signature != GetMagickSignature((const StringInfo *) NULL))
773       ThrowReaderException(CacheError,"IncompatibleAPI");
774     if (image->montage != (char *) NULL)
775       {
776         register char
777           *p;
778
779         /*
780           Image directory.
781         */
782         length=MagickPathExtent;
783         image->directory=AcquireString((char *) NULL);
784         p=image->directory;
785         do
786         {
787           *p='\0';
788           if ((strlen(image->directory)+MagickPathExtent) >= length)
789             {
790               /*
791                 Allocate more memory for the image directory.
792               */
793               length<<=1;
794               image->directory=(char *) ResizeQuantumMemory(image->directory,
795                 length+MagickPathExtent,sizeof(*image->directory));
796               if (image->directory == (char *) NULL)
797                 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
798               p=image->directory+strlen(image->directory);
799             }
800           c=ReadBlobByte(image);
801           *p++=(char) c;
802         } while (c != (int) '\0');
803       }
804     if (profiles != (LinkedListInfo *) NULL)
805       {
806         const char
807           *name;
808
809         const StringInfo
810           *profile;
811
812         register unsigned char
813           *p;
814
815         /*
816           Read image profiles.
817         */
818         ResetLinkedListIterator(profiles);
819         name=(const char *) GetNextValueInLinkedList(profiles);
820         while (name != (const char *) NULL)
821         {
822           profile=GetImageProfile(image,name);
823           if (profile != (StringInfo *) NULL)
824             {
825               p=GetStringInfoDatum(profile);
826               count=ReadBlob(image,GetStringInfoLength(profile),p);
827             }
828           name=(const char *) GetNextValueInLinkedList(profiles);
829         }
830         profiles=DestroyLinkedList(profiles,RelinquishMagickMemory);
831       }
832     depth=GetImageQuantumDepth(image,MagickFalse);
833     if (image->storage_class == PseudoClass)
834       {
835         /*
836           Create image colormap.
837         */
838         image->colormap=(PixelInfo *) AcquireQuantumMemory(image->colors+1,
839           sizeof(*image->colormap));
840         if (image->colormap == (PixelInfo *) NULL)
841           ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
842         if (image->colors != 0)
843           {
844             size_t
845               packet_size;
846
847             unsigned char
848               *colormap;
849
850             /*
851               Read image colormap from file.
852             */
853             packet_size=(size_t) (3UL*depth/8UL);
854             colormap=(unsigned char *) AcquireQuantumMemory(image->colors,
855               packet_size*sizeof(*colormap));
856             if (colormap == (unsigned char *) NULL)
857               ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
858             count=ReadBlob(image,packet_size*image->colors,colormap);
859             if (count != (ssize_t) (packet_size*image->colors))
860               ThrowReaderException(CorruptImageError,
861                 "InsufficientImageDataInFile");
862             p=colormap;
863             switch (depth)
864             {
865               default:
866                 ThrowReaderException(CorruptImageError,
867                   "ImageDepthNotSupported");
868               case 8:
869               {
870                 unsigned char
871                   pixel;
872
873                 for (i=0; i < (ssize_t) image->colors; i++)
874                 {
875                   p=PushCharPixel(p,&pixel);
876                   image->colormap[i].red=ScaleCharToQuantum(pixel);
877                   p=PushCharPixel(p,&pixel);
878                   image->colormap[i].green=ScaleCharToQuantum(pixel);
879                   p=PushCharPixel(p,&pixel);
880                   image->colormap[i].blue=ScaleCharToQuantum(pixel);
881                 }
882                 break;
883               }
884               case 16:
885               {
886                 unsigned short
887                   pixel;
888
889                 for (i=0; i < (ssize_t) image->colors; i++)
890                 {
891                   p=PushShortPixel(MSBEndian,p,&pixel);
892                   image->colormap[i].red=ScaleShortToQuantum(pixel);
893                   p=PushShortPixel(MSBEndian,p,&pixel);
894                   image->colormap[i].green=ScaleShortToQuantum(pixel);
895                   p=PushShortPixel(MSBEndian,p,&pixel);
896                   image->colormap[i].blue=ScaleShortToQuantum(pixel);
897                 }
898                 break;
899               }
900               case 32:
901               {
902                 unsigned int
903                   pixel;
904
905                 for (i=0; i < (ssize_t) image->colors; i++)
906                 {
907                   p=PushLongPixel(MSBEndian,p,&pixel);
908                   image->colormap[i].red=ScaleLongToQuantum(pixel);
909                   p=PushLongPixel(MSBEndian,p,&pixel);
910                   image->colormap[i].green=ScaleLongToQuantum(pixel);
911                   p=PushLongPixel(MSBEndian,p,&pixel);
912                   image->colormap[i].blue=ScaleLongToQuantum(pixel);
913                 }
914                 break;
915               }
916             }
917             colormap=(unsigned char *) RelinquishMagickMemory(colormap);
918           }
919       }
920     if (EOFBlob(image) != MagickFalse)
921       {
922         ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
923           image->filename);
924         break;
925       }
926     if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
927       if (image->scene >= (image_info->scene+image_info->number_scenes-1))
928         break;
929     if ((AcquireMagickResource(WidthResource,image->columns) == MagickFalse) ||
930         (AcquireMagickResource(HeightResource,image->rows) == MagickFalse))
931       ThrowReaderException(ImageError,"WidthOrHeightExceedsLimit");
932     /*
933       Attach persistent pixel cache.
934     */
935     status=PersistPixelCache(image,cache_filename,MagickTrue,&offset,exception);
936     if (status == MagickFalse)
937       ThrowReaderException(CacheError,"UnableToPersistPixelCache");
938     /*
939       Proceed to next image.
940     */
941     do
942     {
943       c=ReadBlobByte(image);
944     } while ((isgraph(c) == MagickFalse) && (c != EOF));
945     if (c != EOF)
946       {
947         /*
948           Allocate next image structure.
949         */
950         AcquireNextImage(image_info,image,exception);
951         if (GetNextImageInList(image) == (Image *) NULL)
952           {
953             image=DestroyImageList(image);
954             return((Image *) NULL);
955           }
956         image=SyncNextImageInList(image);
957         status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
958           GetBlobSize(image));
959         if (status == MagickFalse)
960           break;
961       }
962   } while (c != EOF);
963   (void) CloseBlob(image);
964   return(GetFirstImageInList(image));
965 }
966 \f
967 /*
968 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
969 %                                                                             %
970 %                                                                             %
971 %                                                                             %
972 %   R e g i s t e r M P C I m a g e                                           %
973 %                                                                             %
974 %                                                                             %
975 %                                                                             %
976 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
977 %
978 %  RegisterMPCImage() adds properties for the Cache image format to
979 %  the list of supported formats.  The properties include the image format
980 %  tag, a method to read and/or write the format, whether the format
981 %  supports the saving of more than one frame to the same file or blob,
982 %  whether the format supports native in-memory I/O, and a brief
983 %  description of the format.
984 %
985 %  The format of the RegisterMPCImage method is:
986 %
987 %      size_t RegisterMPCImage(void)
988 %
989 */
990 ModuleExport size_t RegisterMPCImage(void)
991 {
992   MagickInfo
993     *entry;
994
995   entry=AcquireMagickInfo("MPC","CACHE",
996     "Magick Persistent Cache image format");
997   entry->flags|=CoderStealthFlag;
998   (void) RegisterMagickInfo(entry);
999   entry=AcquireMagickInfo("MPC","MPC","Magick Persistent Cache image format");
1000   entry->decoder=(DecodeImageHandler *) ReadMPCImage;
1001   entry->encoder=(EncodeImageHandler *) WriteMPCImage;
1002   entry->magick=(IsImageFormatHandler *) IsMPC;
1003   (void) RegisterMagickInfo(entry);
1004   return(MagickImageCoderSignature);
1005 }
1006 \f
1007 /*
1008 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1009 %                                                                             %
1010 %                                                                             %
1011 %                                                                             %
1012 %   U n r e g i s t e r M P C I m a g e                                       %
1013 %                                                                             %
1014 %                                                                             %
1015 %                                                                             %
1016 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1017 %
1018 %  UnregisterMPCImage() removes format registrations made by the
1019 %  MPC module from the list of supported formats.
1020 %
1021 %  The format of the UnregisterMPCImage method is:
1022 %
1023 %      UnregisterMPCImage(void)
1024 %
1025 */
1026 ModuleExport void UnregisterMPCImage(void)
1027 {
1028   (void) UnregisterMagickInfo("CACHE");
1029   (void) UnregisterMagickInfo("MPC");
1030 }
1031 \f
1032 /*
1033 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1034 %                                                                             %
1035 %                                                                             %
1036 %                                                                             %
1037 %   W r i t e M P C I m a g e                                                 %
1038 %                                                                             %
1039 %                                                                             %
1040 %                                                                             %
1041 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1042 %
1043 %  WriteMPCImage() writes an Magick Persistent Cache image to a file.
1044 %
1045 %  The format of the WriteMPCImage method is:
1046 %
1047 %      MagickBooleanType WriteMPCImage(const ImageInfo *image_info,
1048 %        Image *image,ExceptionInfo *exception)
1049 %
1050 %  A description of each parameter follows:
1051 %
1052 %    o image_info: the image info.
1053 %
1054 %    o image: the image.
1055 %
1056 %    o exception: return any errors or warnings in this structure.
1057 %
1058 */
1059 static MagickBooleanType WriteMPCImage(const ImageInfo *image_info,Image *image,
1060   ExceptionInfo *exception)
1061 {
1062   char
1063     buffer[MagickPathExtent],
1064     cache_filename[MagickPathExtent];
1065
1066   const char
1067     *property,
1068     *value;
1069
1070   MagickBooleanType
1071     status;
1072
1073   MagickOffsetType
1074     offset,
1075     scene;
1076
1077   register ssize_t
1078     i;
1079
1080   size_t
1081     depth;
1082
1083   /*
1084     Open persistent cache.
1085   */
1086   assert(image_info != (const ImageInfo *) NULL);
1087   assert(image_info->signature == MagickCoreSignature);
1088   assert(image != (Image *) NULL);
1089   assert(image->signature == MagickCoreSignature);
1090   if (image->debug != MagickFalse)
1091     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1092   assert(exception != (ExceptionInfo *) NULL);
1093   assert(exception->signature == MagickCoreSignature);
1094   status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
1095   if (status == MagickFalse)
1096     return(status);
1097   (void) CopyMagickString(cache_filename,image->filename,MagickPathExtent);
1098   AppendImageFormat("cache",cache_filename);
1099   scene=0;
1100   offset=0;
1101   do
1102   {
1103     /*
1104       Write persistent cache meta-information.
1105     */
1106     depth=GetImageQuantumDepth(image,MagickTrue);
1107     if ((image->storage_class == PseudoClass) &&
1108         (image->colors > (size_t) (GetQuantumRange(image->depth)+1)))
1109       (void) SetImageStorageClass(image,DirectClass,exception);
1110     (void) WriteBlobString(image,"id=MagickCache\n");
1111     (void) FormatLocaleString(buffer,MagickPathExtent,"magick-signature=%u\n",
1112       GetMagickSignature((const StringInfo *) NULL));
1113     (void) WriteBlobString(image,buffer);
1114     (void) FormatLocaleString(buffer,MagickPathExtent,
1115       "class=%s  colors=%.20g  alpha-trait=%s\n",CommandOptionToMnemonic(
1116       MagickClassOptions,image->storage_class),(double) image->colors,
1117       CommandOptionToMnemonic(MagickPixelTraitOptions,(ssize_t)
1118       image->alpha_trait));
1119     (void) WriteBlobString(image,buffer);
1120     (void) FormatLocaleString(buffer,MagickPathExtent,
1121       "columns=%.20g  rows=%.20g depth=%.20g\n",(double) image->columns,
1122       (double) image->rows,(double) image->depth);
1123     (void) WriteBlobString(image,buffer);
1124     if (image->type != UndefinedType)
1125       {
1126         (void) FormatLocaleString(buffer,MagickPathExtent,"type=%s\n",
1127           CommandOptionToMnemonic(MagickTypeOptions,image->type));
1128         (void) WriteBlobString(image,buffer);
1129       }
1130     if (image->colorspace != UndefinedColorspace)
1131       {
1132         (void) FormatLocaleString(buffer,MagickPathExtent,"colorspace=%s\n",
1133           CommandOptionToMnemonic(MagickColorspaceOptions,image->colorspace));
1134         (void) WriteBlobString(image,buffer);
1135       }
1136     if (image->intensity != UndefinedPixelIntensityMethod)
1137       {
1138         (void) FormatLocaleString(buffer,MagickPathExtent,
1139           "pixel-intensity=%s\n",CommandOptionToMnemonic(
1140           MagickPixelIntensityOptions,image->intensity));
1141         (void) WriteBlobString(image,buffer);
1142       }
1143     if (image->endian != UndefinedEndian)
1144       {
1145         (void) FormatLocaleString(buffer,MagickPathExtent,"endian=%s\n",
1146           CommandOptionToMnemonic(MagickEndianOptions,image->endian));
1147         (void) WriteBlobString(image,buffer);
1148       }
1149     if (image->compression != UndefinedCompression)
1150       {
1151         (void) FormatLocaleString(buffer,MagickPathExtent,
1152           "compression=%s  quality=%.20g\n",CommandOptionToMnemonic(
1153           MagickCompressOptions,image->compression),(double) image->quality);
1154         (void) WriteBlobString(image,buffer);
1155       }
1156     if (image->units != UndefinedResolution)
1157       {
1158         (void) FormatLocaleString(buffer,MagickPathExtent,"units=%s\n",
1159           CommandOptionToMnemonic(MagickResolutionOptions,image->units));
1160         (void) WriteBlobString(image,buffer);
1161       }
1162     if ((image->resolution.x != 0) || (image->resolution.y != 0))
1163       {
1164         (void) FormatLocaleString(buffer,MagickPathExtent,
1165           "resolution=%gx%g\n",image->resolution.x,image->resolution.y);
1166         (void) WriteBlobString(image,buffer);
1167       }
1168     if ((image->page.width != 0) || (image->page.height != 0))
1169       {
1170         (void) FormatLocaleString(buffer,MagickPathExtent,
1171           "page=%.20gx%.20g%+.20g%+.20g\n",(double) image->page.width,(double)
1172           image->page.height,(double) image->page.x,(double) image->page.y);
1173         (void) WriteBlobString(image,buffer);
1174       }
1175     else
1176       if ((image->page.x != 0) || (image->page.y != 0))
1177         {
1178           (void) FormatLocaleString(buffer,MagickPathExtent,"page=%+ld%+ld\n",
1179             (long) image->page.x,(long) image->page.y);
1180           (void) WriteBlobString(image,buffer);
1181         }
1182     if ((image->tile_offset.x != 0) || (image->tile_offset.y != 0))
1183       {
1184         (void) FormatLocaleString(buffer,MagickPathExtent,
1185           "tile-offset=%+ld%+ld\n",(long) image->tile_offset.x,(long)
1186            image->tile_offset.y);
1187         (void) WriteBlobString(image,buffer);
1188       }
1189     if ((GetNextImageInList(image) != (Image *) NULL) ||
1190         (GetPreviousImageInList(image) != (Image *) NULL))
1191       {
1192         if (image->scene == 0)
1193           (void) FormatLocaleString(buffer,MagickPathExtent,
1194             "iterations=%.20g  delay=%.20g  ticks-per-second=%.20g\n",(double)
1195             image->iterations,(double) image->delay,(double)
1196             image->ticks_per_second);
1197         else
1198           (void) FormatLocaleString(buffer,MagickPathExtent,"scene=%.20g  "
1199             "iterations=%.20g  delay=%.20g  ticks-per-second=%.20g\n",
1200             (double) image->scene,(double) image->iterations,(double)
1201             image->delay,(double) image->ticks_per_second);
1202         (void) WriteBlobString(image,buffer);
1203       }
1204     else
1205       {
1206         if (image->scene != 0)
1207           {
1208             (void) FormatLocaleString(buffer,MagickPathExtent,"scene=%.20g\n",
1209               (double) image->scene);
1210             (void) WriteBlobString(image,buffer);
1211           }
1212         if (image->iterations != 0)
1213           {
1214             (void) FormatLocaleString(buffer,MagickPathExtent,
1215               "iterations=%.20g\n",(double) image->iterations);
1216             (void) WriteBlobString(image,buffer);
1217           }
1218         if (image->delay != 0)
1219           {
1220             (void) FormatLocaleString(buffer,MagickPathExtent,"delay=%.20g\n",
1221               (double) image->delay);
1222             (void) WriteBlobString(image,buffer);
1223           }
1224         if (image->ticks_per_second != UndefinedTicksPerSecond)
1225           {
1226             (void) FormatLocaleString(buffer,MagickPathExtent,
1227               "ticks-per-second=%.20g\n",(double) image->ticks_per_second);
1228             (void) WriteBlobString(image,buffer);
1229           }
1230       }
1231     if (image->gravity != UndefinedGravity)
1232       {
1233         (void) FormatLocaleString(buffer,MagickPathExtent,"gravity=%s\n",
1234           CommandOptionToMnemonic(MagickGravityOptions,image->gravity));
1235         (void) WriteBlobString(image,buffer);
1236       }
1237     if (image->dispose != UndefinedDispose)
1238       {
1239         (void) FormatLocaleString(buffer,MagickPathExtent,"dispose=%s\n",
1240           CommandOptionToMnemonic(MagickDisposeOptions,image->dispose));
1241         (void) WriteBlobString(image,buffer);
1242       }
1243     if (image->rendering_intent != UndefinedIntent)
1244       {
1245         (void) FormatLocaleString(buffer,MagickPathExtent,
1246           "rendering-intent=%s\n",CommandOptionToMnemonic(MagickIntentOptions,
1247           image->rendering_intent));
1248         (void) WriteBlobString(image,buffer);
1249       }
1250     if (image->gamma != 0.0)
1251       {
1252         (void) FormatLocaleString(buffer,MagickPathExtent,"gamma=%g\n",
1253           image->gamma);
1254         (void) WriteBlobString(image,buffer);
1255       }
1256     if (image->chromaticity.white_point.x != 0.0)
1257       {
1258         /*
1259           Note chomaticity points.
1260         */
1261         (void) FormatLocaleString(buffer,MagickPathExtent,"red-primary="
1262           "%g,%g  green-primary=%g,%g  blue-primary=%g,%g\n",
1263           image->chromaticity.red_primary.x,image->chromaticity.red_primary.y,
1264           image->chromaticity.green_primary.x,
1265           image->chromaticity.green_primary.y,
1266           image->chromaticity.blue_primary.x,
1267           image->chromaticity.blue_primary.y);
1268         (void) WriteBlobString(image,buffer);
1269         (void) FormatLocaleString(buffer,MagickPathExtent,
1270           "white-point=%g,%g\n",image->chromaticity.white_point.x,
1271           image->chromaticity.white_point.y);
1272         (void) WriteBlobString(image,buffer);
1273       }
1274     if (image->orientation != UndefinedOrientation)
1275       {
1276         (void) FormatLocaleString(buffer,MagickPathExtent,
1277           "orientation=%s\n",CommandOptionToMnemonic(MagickOrientationOptions,
1278           image->orientation));
1279         (void) WriteBlobString(image,buffer);
1280       }
1281     if (image->profiles != (void *) NULL)
1282       {
1283         const char
1284           *name;
1285
1286         const StringInfo
1287           *profile;
1288
1289         /*
1290           Generic profile.
1291         */
1292         ResetImageProfileIterator(image);
1293         for (name=GetNextImageProfile(image); name != (const char *) NULL; )
1294         {
1295           profile=GetImageProfile(image,name);
1296           if (profile != (StringInfo *) NULL)
1297             {
1298               (void) FormatLocaleString(buffer,MagickPathExtent,
1299                 "profile:%s=%.20g\n",name,(double)
1300                 GetStringInfoLength(profile));
1301               (void) WriteBlobString(image,buffer);
1302             }
1303           name=GetNextImageProfile(image);
1304         }
1305       }
1306     if (image->montage != (char *) NULL)
1307       {
1308         (void) FormatLocaleString(buffer,MagickPathExtent,"montage=%s\n",
1309           image->montage);
1310         (void) WriteBlobString(image,buffer);
1311       }
1312     ResetImagePropertyIterator(image);
1313     property=GetNextImageProperty(image);
1314     while (property != (const char *) NULL)
1315     {
1316       (void) FormatLocaleString(buffer,MagickPathExtent,"%s=",property);
1317       (void) WriteBlobString(image,buffer);
1318       value=GetImageProperty(image,property,exception);
1319       if (value != (const char *) NULL)
1320         {
1321           size_t
1322             length;
1323
1324           length=strlen(value);
1325           for (i=0; i < (ssize_t) length; i++)
1326             if (isspace((int) ((unsigned char) value[i])) != 0)
1327               break;
1328           if ((i == (ssize_t) length) && (i != 0))
1329             (void) WriteBlob(image,length,(const unsigned char *) value);
1330           else
1331             {
1332               (void) WriteBlobByte(image,'{');
1333               if (strchr(value,'}') == (char *) NULL)
1334                 (void) WriteBlob(image,length,(const unsigned char *) value);
1335               else
1336                 for (i=0; i < (ssize_t) length; i++)
1337                 {
1338                   if (value[i] == (int) '}')
1339                     (void) WriteBlobByte(image,'\\');
1340                   (void) WriteBlobByte(image,value[i]);
1341                 }
1342               (void) WriteBlobByte(image,'}');
1343             }
1344         }
1345       (void) WriteBlobByte(image,'\n');
1346       property=GetNextImageProperty(image);
1347     }
1348     (void) WriteBlobString(image,"\f\n:\032");
1349     if (image->montage != (char *) NULL)
1350       {
1351         /*
1352           Write montage tile directory.
1353         */
1354         if (image->directory != (char *) NULL)
1355           (void) WriteBlobString(image,image->directory);
1356         (void) WriteBlobByte(image,'\0');
1357       }
1358     if (image->profiles != 0)
1359       {
1360         const char
1361           *name;
1362
1363         const StringInfo
1364           *profile;
1365
1366         /*
1367           Write image profiles.
1368         */
1369         ResetImageProfileIterator(image);
1370         name=GetNextImageProfile(image);
1371         while (name != (const char *) NULL)
1372         {
1373           profile=GetImageProfile(image,name);
1374           (void) WriteBlob(image,GetStringInfoLength(profile),
1375             GetStringInfoDatum(profile));
1376           name=GetNextImageProfile(image);
1377         }
1378       }
1379     if (image->storage_class == PseudoClass)
1380       {
1381         size_t
1382           packet_size;
1383
1384         unsigned char
1385           *colormap,
1386           *q;
1387
1388         /*
1389           Allocate colormap.
1390         */
1391         packet_size=(size_t) (3UL*depth/8UL);
1392         colormap=(unsigned char *) AcquireQuantumMemory(image->colors,
1393           packet_size*sizeof(*colormap));
1394         if (colormap == (unsigned char *) NULL)
1395           return(MagickFalse);
1396         /*
1397           Write colormap to file.
1398         */
1399         q=colormap;
1400         for (i=0; i < (ssize_t) image->colors; i++)
1401         {
1402           switch (depth)
1403           {
1404             default:
1405               ThrowWriterException(CorruptImageError,"ImageDepthNotSupported");
1406             case 32:
1407             {
1408               unsigned int
1409                 pixel;
1410
1411               pixel=ScaleQuantumToLong(image->colormap[i].red);
1412               q=PopLongPixel(MSBEndian,pixel,q);
1413               pixel=ScaleQuantumToLong(image->colormap[i].green);
1414               q=PopLongPixel(MSBEndian,pixel,q);
1415               pixel=ScaleQuantumToLong(image->colormap[i].blue);
1416               q=PopLongPixel(MSBEndian,pixel,q);
1417               break;
1418             }
1419             case 16:
1420             {
1421               unsigned short
1422                 pixel;
1423
1424               pixel=ScaleQuantumToShort(image->colormap[i].red);
1425               q=PopShortPixel(MSBEndian,pixel,q);
1426               pixel=ScaleQuantumToShort(image->colormap[i].green);
1427               q=PopShortPixel(MSBEndian,pixel,q);
1428               pixel=ScaleQuantumToShort(image->colormap[i].blue);
1429               q=PopShortPixel(MSBEndian,pixel,q);
1430               break;
1431             }
1432             case 8:
1433             {
1434               unsigned char
1435                 pixel;
1436
1437               pixel=(unsigned char) ScaleQuantumToChar(image->colormap[i].red);
1438               q=PopCharPixel(pixel,q);
1439               pixel=(unsigned char) ScaleQuantumToChar(
1440                 image->colormap[i].green);
1441               q=PopCharPixel(pixel,q);
1442               pixel=(unsigned char) ScaleQuantumToChar(image->colormap[i].blue);
1443               q=PopCharPixel(pixel,q);
1444               break;
1445             }
1446           }
1447         }
1448         (void) WriteBlob(image,packet_size*image->colors,colormap);
1449         colormap=(unsigned char *) RelinquishMagickMemory(colormap);
1450       }
1451     /*
1452       Initialize persistent pixel cache.
1453     */
1454     status=PersistPixelCache(image,cache_filename,MagickFalse,&offset,
1455       exception);
1456     if (status == MagickFalse)
1457       ThrowWriterException(CacheError,"UnableToPersistPixelCache");
1458     if (GetNextImageInList(image) == (Image *) NULL)
1459       break;
1460     image=SyncNextImageInList(image);
1461     if (image->progress_monitor != (MagickProgressMonitor) NULL)
1462       {
1463         status=image->progress_monitor(SaveImagesTag,scene,
1464           GetImageListLength(image),image->client_data);
1465         if (status == MagickFalse)
1466           break;
1467       }
1468     scene++;
1469   } while (image_info->adjoin != MagickFalse);
1470   (void) CloseBlob(image);
1471   return(status);
1472 }