]> granicus.if.org Git - imagemagick/blob - coders/mpc.c
https://github.com/ImageMagick/ImageMagick/issues/563
[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 %    https://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-trait") == 0)
343                   {
344                     ssize_t
345                       alpha_trait;
346
347                     alpha_trait=ParseCommandOption(MagickPixelTraitOptions,
348                       MagickFalse,options);
349                     if (alpha_trait < 0)
350                       break;
351                     image->alpha_trait=(PixelTrait) alpha_trait;
352                     break;
353                   }
354                 (void) SetImageProperty(image,keyword,options,exception);
355                 break;
356               }
357               case 'b':
358               case 'B':
359               {
360                 if (LocaleCompare(keyword,"background-color") == 0)
361                   {
362                     (void) QueryColorCompliance(options,AllCompliance,
363                       &image->background_color,exception);
364                     break;
365                   }
366                 if (LocaleCompare(keyword,"blue-primary") == 0)
367                   {
368                     flags=ParseGeometry(options,&geometry_info);
369                     image->chromaticity.blue_primary.x=geometry_info.rho;
370                     image->chromaticity.blue_primary.y=geometry_info.sigma;
371                     if ((flags & SigmaValue) == 0)
372                       image->chromaticity.blue_primary.y=
373                         image->chromaticity.blue_primary.x;
374                     break;
375                   }
376                 if (LocaleCompare(keyword,"border-color") == 0)
377                   {
378                     (void) QueryColorCompliance(options,AllCompliance,
379                       &image->border_color,exception);
380                     break;
381                   }
382                 (void) SetImageProperty(image,keyword,options,exception);
383                 break;
384               }
385               case 'c':
386               case 'C':
387               {
388                 if (LocaleCompare(keyword,"class") == 0)
389                   {
390                     ssize_t
391                       storage_class;
392
393                     storage_class=ParseCommandOption(MagickClassOptions,
394                       MagickFalse,options);
395                     if (storage_class < 0)
396                       break;
397                     image->storage_class=(ClassType) storage_class;
398                     break;
399                   }
400                 if (LocaleCompare(keyword,"colors") == 0)
401                   {
402                     image->colors=StringToUnsignedLong(options);
403                     break;
404                   }
405                 if (LocaleCompare(keyword,"colorspace") == 0)
406                   {
407                     ssize_t
408                       colorspace;
409
410                     colorspace=ParseCommandOption(MagickColorspaceOptions,
411                       MagickFalse,options);
412                     if (colorspace < 0)
413                       break;
414                     image->colorspace=(ColorspaceType) colorspace;
415                     break;
416                   }
417                 if (LocaleCompare(keyword,"compression") == 0)
418                   {
419                     ssize_t
420                       compression;
421
422                     compression=ParseCommandOption(MagickCompressOptions,
423                       MagickFalse,options);
424                     if (compression < 0)
425                       break;
426                     image->compression=(CompressionType) compression;
427                     break;
428                   }
429                 if (LocaleCompare(keyword,"columns") == 0)
430                   {
431                     image->columns=StringToUnsignedLong(options);
432                     break;
433                   }
434                 (void) SetImageProperty(image,keyword,options,exception);
435                 break;
436               }
437               case 'd':
438               case 'D':
439               {
440                 if (LocaleCompare(keyword,"delay") == 0)
441                   {
442                     image->delay=StringToUnsignedLong(options);
443                     break;
444                   }
445                 if (LocaleCompare(keyword,"depth") == 0)
446                   {
447                     image->depth=StringToUnsignedLong(options);
448                     break;
449                   }
450                 if (LocaleCompare(keyword,"dispose") == 0)
451                   {
452                     ssize_t
453                       dispose;
454
455                     dispose=ParseCommandOption(MagickDisposeOptions,MagickFalse,
456                       options);
457                     if (dispose < 0)
458                       break;
459                     image->dispose=(DisposeType) dispose;
460                     break;
461                   }
462                 (void) SetImageProperty(image,keyword,options,exception);
463                 break;
464               }
465               case 'e':
466               case 'E':
467               {
468                 if (LocaleCompare(keyword,"endian") == 0)
469                   {
470                     ssize_t
471                       endian;
472
473                     endian=ParseCommandOption(MagickEndianOptions,MagickFalse,
474                       options);
475                     if (endian < 0)
476                       break;
477                     image->endian=(EndianType) endian;
478                     break;
479                   }
480                 if (LocaleCompare(keyword,"error") == 0)
481                   {
482                     image->error.mean_error_per_pixel=StringToDouble(options,
483                       (char **) NULL);
484                     break;
485                   }
486                 (void) SetImageProperty(image,keyword,options,exception);
487                 break;
488               }
489               case 'g':
490               case 'G':
491               {
492                 if (LocaleCompare(keyword,"gamma") == 0)
493                   {
494                     image->gamma=StringToDouble(options,(char **) NULL);
495                     break;
496                   }
497                 if (LocaleCompare(keyword,"green-primary") == 0)
498                   {
499                     flags=ParseGeometry(options,&geometry_info);
500                     image->chromaticity.green_primary.x=geometry_info.rho;
501                     image->chromaticity.green_primary.y=geometry_info.sigma;
502                     if ((flags & SigmaValue) == 0)
503                       image->chromaticity.green_primary.y=
504                         image->chromaticity.green_primary.x;
505                     break;
506                   }
507                 (void) SetImageProperty(image,keyword,options,exception);
508                 break;
509               }
510               case 'i':
511               case 'I':
512               {
513                 if (LocaleCompare(keyword,"id") == 0)
514                   {
515                     (void) CopyMagickString(id,options,MagickPathExtent);
516                     break;
517                   }
518                 if (LocaleCompare(keyword,"iterations") == 0)
519                   {
520                     image->iterations=StringToUnsignedLong(options);
521                     break;
522                   }
523                 (void) SetImageProperty(image,keyword,options,exception);
524                 break;
525               }
526               case 'm':
527               case 'M':
528               {
529                 if (LocaleCompare(keyword,"magick-signature") == 0)
530                   {
531                     signature=(unsigned int) StringToUnsignedLong(options);
532                     break;
533                   }
534                 if (LocaleCompare(keyword,"mattecolor") == 0)
535                   {
536                     (void) QueryColorCompliance(options,AllCompliance,
537                       &image->matte_color,exception);
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 'n':
561               case 'N':
562               {
563                 if (LocaleCompare(keyword,"number-channels") == 0)
564                   {
565                     image->number_channels=StringToUnsignedLong(options);
566                     break;
567                   }
568                 if (LocaleCompare(keyword,"number-meta-channels") == 0)
569                   {
570                     image->number_meta_channels=StringToUnsignedLong(options);
571                     if (image->number_meta_channels > MaxPixelChannels)
572                       ThrowReaderException(CorruptImageError,
573                         "ImproperImageHeader");
574                     break;
575                   }
576                 break;
577               }
578               case 'o':
579               case 'O':
580               {
581                 if (LocaleCompare(keyword,"orientation") == 0)
582                   {
583                     ssize_t
584                       orientation;
585
586                     orientation=ParseCommandOption(MagickOrientationOptions,
587                       MagickFalse,options);
588                     if (orientation < 0)
589                       break;
590                     image->orientation=(OrientationType) orientation;
591                     break;
592                   }
593                 (void) SetImageProperty(image,keyword,options,exception);
594                 break;
595               }
596               case 'p':
597               case 'P':
598               {
599                 if (LocaleCompare(keyword,"page") == 0)
600                   {
601                     char
602                       *geometry;
603
604                     geometry=GetPageGeometry(options);
605                     (void) ParseAbsoluteGeometry(geometry,&image->page);
606                     geometry=DestroyString(geometry);
607                     break;
608                   }
609                 if (LocaleCompare(keyword,"pixel-intensity") == 0)
610                   {
611                     ssize_t
612                       intensity;
613
614                     intensity=ParseCommandOption(MagickPixelIntensityOptions,
615                       MagickFalse,options);
616                     if (intensity < 0)
617                       break;
618                     image->intensity=(PixelIntensityMethod) intensity;
619                     break;
620                   }
621                 if ((LocaleNCompare(keyword,"profile:",8) == 0) ||
622                     (LocaleNCompare(keyword,"profile-",8) == 0))
623                   {
624                     if (profiles == (LinkedListInfo *) NULL)
625                       profiles=NewLinkedList(0);
626                     (void) AppendValueToLinkedList(profiles,
627                       AcquireString(keyword+8));
628                     profile=BlobToStringInfo((const void *) NULL,(size_t)
629                       StringToLong(options));
630                     if (profile == (StringInfo *) NULL)
631                       ThrowReaderException(ResourceLimitError,
632                         "MemoryAllocationFailed");
633                     (void) SetImageProfile(image,keyword+8,profile,exception);
634                     profile=DestroyStringInfo(profile);
635                     break;
636                   }
637                 (void) SetImageProperty(image,keyword,options,exception);
638                 break;
639               }
640               case 'q':
641               case 'Q':
642               {
643                 if (LocaleCompare(keyword,"quality") == 0)
644                   {
645                     image->quality=StringToUnsignedLong(options);
646                     break;
647                   }
648                 (void) SetImageProperty(image,keyword,options,exception);
649                 break;
650               }
651               case 'r':
652               case 'R':
653               {
654                 if (LocaleCompare(keyword,"red-primary") == 0)
655                   {
656                     flags=ParseGeometry(options,&geometry_info);
657                     image->chromaticity.red_primary.x=geometry_info.rho;
658                     if ((flags & SigmaValue) != 0)
659                       image->chromaticity.red_primary.y=geometry_info.sigma;
660                     break;
661                   }
662                 if (LocaleCompare(keyword,"rendering-intent") == 0)
663                   {
664                     ssize_t
665                       rendering_intent;
666
667                     rendering_intent=ParseCommandOption(MagickIntentOptions,
668                       MagickFalse,options);
669                     if (rendering_intent < 0)
670                       break;
671                     image->rendering_intent=(RenderingIntent) rendering_intent;
672                     break;
673                   }
674                 if (LocaleCompare(keyword,"resolution") == 0)
675                   {
676                     flags=ParseGeometry(options,&geometry_info);
677                     image->resolution.x=geometry_info.rho;
678                     image->resolution.y=geometry_info.sigma;
679                     if ((flags & SigmaValue) == 0)
680                       image->resolution.y=image->resolution.x;
681                     break;
682                   }
683                 if (LocaleCompare(keyword,"rows") == 0)
684                   {
685                     image->rows=StringToUnsignedLong(options);
686                     break;
687                   }
688                 (void) SetImageProperty(image,keyword,options,exception);
689                 break;
690               }
691               case 's':
692               case 'S':
693               {
694                 if (LocaleCompare(keyword,"scene") == 0)
695                   {
696                     image->scene=StringToUnsignedLong(options);
697                     break;
698                   }
699                 (void) SetImageProperty(image,keyword,options,exception);
700                 break;
701               }
702               case 't':
703               case 'T':
704               {
705                 if (LocaleCompare(keyword,"ticks-per-second") == 0)
706                   {
707                     image->ticks_per_second=(ssize_t) StringToLong(options);
708                     break;
709                   }
710                 if (LocaleCompare(keyword,"tile-offset") == 0)
711                   {
712                     char
713                       *geometry;
714
715                     geometry=GetPageGeometry(options);
716                     (void) ParseAbsoluteGeometry(geometry,&image->tile_offset);
717                     geometry=DestroyString(geometry);
718                   }
719                 if (LocaleCompare(keyword,"type") == 0)
720                   {
721                     ssize_t
722                       type;
723
724                     type=ParseCommandOption(MagickTypeOptions,MagickFalse,
725                       options);
726                     if (type < 0)
727                       break;
728                     image->type=(ImageType) type;
729                     break;
730                   }
731                 (void) SetImageProperty(image,keyword,options,exception);
732                 break;
733               }
734               case 'u':
735               case 'U':
736               {
737                 if (LocaleCompare(keyword,"units") == 0)
738                   {
739                     ssize_t
740                       units;
741
742                     units=ParseCommandOption(MagickResolutionOptions,
743                       MagickFalse,options);
744                     if (units < 0)
745                       break;
746                     image->units=(ResolutionType) units;
747                     break;
748                   }
749                 (void) SetImageProperty(image,keyword,options,exception);
750                 break;
751               }
752               case 'w':
753               case 'W':
754               {
755                 if (LocaleCompare(keyword,"white-point") == 0)
756                   {
757                     flags=ParseGeometry(options,&geometry_info);
758                     image->chromaticity.white_point.x=geometry_info.rho;
759                     image->chromaticity.white_point.y=geometry_info.sigma;
760                     if ((flags & SigmaValue) == 0)
761                       image->chromaticity.white_point.y=
762                         image->chromaticity.white_point.x;
763                     break;
764                   }
765                 (void) SetImageProperty(image,keyword,options,exception);
766                 break;
767               }
768               default:
769               {
770                 (void) SetImageProperty(image,keyword,options,exception);
771                 break;
772               }
773             }
774           }
775         else
776           c=ReadBlobByte(image);
777       while (isspace((int) ((unsigned char) c)) != 0)
778         c=ReadBlobByte(image);
779     }
780     options=DestroyString(options);
781     (void) ReadBlobByte(image);
782     /*
783       Verify that required image information is defined.
784     */
785     if ((LocaleCompare(id,"MagickCache") != 0) ||
786         (image->storage_class == UndefinedClass) ||
787         (image->compression == UndefinedCompression) || (image->columns == 0) ||
788         (image->rows == 0))
789       {
790         if (profiles != (LinkedListInfo *) NULL)
791           profiles=DestroyLinkedList(profiles,RelinquishMagickMemory);
792         ThrowReaderException(CorruptImageError,"ImproperImageHeader");
793       }
794     if (signature != GetMagickSignature((const StringInfo *) NULL))
795       {
796         if (profiles != (LinkedListInfo *) NULL)
797           profiles=DestroyLinkedList(profiles,RelinquishMagickMemory);
798         ThrowReaderException(CacheError,"IncompatibleAPI");
799       }
800     if (image->montage != (char *) NULL)
801       {
802         register char
803           *p;
804
805         /*
806           Image directory.
807         */
808         length=MagickPathExtent;
809         image->directory=AcquireString((char *) NULL);
810         p=image->directory;
811         do
812         {
813           *p='\0';
814           if ((strlen(image->directory)+MagickPathExtent) >= length)
815             {
816               /*
817                 Allocate more memory for the image directory.
818               */
819               length<<=1;
820               image->directory=(char *) ResizeQuantumMemory(image->directory,
821                 length+MagickPathExtent,sizeof(*image->directory));
822               if (image->directory == (char *) NULL)
823                 {
824                   if (profiles != (LinkedListInfo *) NULL)
825                     profiles=DestroyLinkedList(profiles,RelinquishMagickMemory);
826                   ThrowReaderException(CorruptImageError,
827                     "UnableToReadImageData");
828                 }
829               p=image->directory+strlen(image->directory);
830             }
831           c=ReadBlobByte(image);
832           *p++=(char) c;
833         } while (c != (int) '\0');
834       }
835     if (profiles != (LinkedListInfo *) NULL)
836       {
837         const char
838           *name;
839
840         const StringInfo
841           *profile;
842
843         register unsigned char
844           *p;
845
846         /*
847           Read image profiles.
848         */
849         ResetLinkedListIterator(profiles);
850         name=(const char *) GetNextValueInLinkedList(profiles);
851         while (name != (const char *) NULL)
852         {
853           profile=GetImageProfile(image,name);
854           if (profile != (StringInfo *) NULL)
855             {
856               p=GetStringInfoDatum(profile);
857               count=ReadBlob(image,GetStringInfoLength(profile),p);
858             }
859           name=(const char *) GetNextValueInLinkedList(profiles);
860         }
861         profiles=DestroyLinkedList(profiles,RelinquishMagickMemory);
862       }
863     depth=GetImageQuantumDepth(image,MagickFalse);
864     if (image->storage_class == PseudoClass)
865       {
866         size_t
867           packet_size;
868
869         unsigned char
870           *colormap;
871
872         /*
873           Create image colormap.
874         */
875         packet_size=(size_t) (3UL*depth/8UL);
876         if ((MagickSizeType) (packet_size*image->colors) > GetBlobSize(image))
877           ThrowReaderException(CorruptImageError,"InsufficientImageDataInFile");
878         image->colormap=(PixelInfo *) AcquireQuantumMemory(image->colors+1,
879           sizeof(*image->colormap));
880         if (image->colormap == (PixelInfo *) NULL)
881           ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
882         if (image->colors != 0)
883           {
884             /*
885               Read image colormap from file.
886             */
887             colormap=(unsigned char *) AcquireQuantumMemory(image->colors,
888               packet_size*sizeof(*colormap));
889             if (colormap == (unsigned char *) NULL)
890               ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
891             count=ReadBlob(image,packet_size*image->colors,colormap);
892             if (count != (ssize_t) (packet_size*image->colors))
893               {
894                 colormap=(unsigned char *) RelinquishMagickMemory(colormap);
895                 ThrowReaderException(CorruptImageError,
896                   "InsufficientImageDataInFile");
897               }
898             p=colormap;
899             switch (depth)
900             {
901               default:
902                 colormap=(unsigned char *) RelinquishMagickMemory(colormap);
903                 ThrowReaderException(CorruptImageError,
904                   "ImageDepthNotSupported");
905               case 8:
906               {
907                 unsigned char
908                   pixel;
909
910                 for (i=0; i < (ssize_t) image->colors; i++)
911                 {
912                   p=PushCharPixel(p,&pixel);
913                   image->colormap[i].red=(MagickRealType)
914                     ScaleCharToQuantum((unsigned char) pixel);
915                   p=PushCharPixel(p,&pixel);
916                   image->colormap[i].green=(MagickRealType)
917                     ScaleCharToQuantum((unsigned char) pixel);
918                   p=PushCharPixel(p,&pixel);
919                   image->colormap[i].blue=(MagickRealType)
920                     ScaleCharToQuantum((unsigned char) pixel);
921                 }
922                 break;
923               }
924               case 16:
925               {
926                 unsigned short
927                   pixel;
928
929                 for (i=0; i < (ssize_t) image->colors; i++)
930                 {
931                   p=PushShortPixel(MSBEndian,p,&pixel);
932                   image->colormap[i].red=(MagickRealType)
933                     ScaleShortToQuantum((Quantum) pixel);
934                   p=PushShortPixel(MSBEndian,p,&pixel);
935                   image->colormap[i].green=(MagickRealType)
936                     ScaleShortToQuantum((Quantum) pixel);
937                   p=PushShortPixel(MSBEndian,p,&pixel);
938                   image->colormap[i].blue=(MagickRealType)
939                     ScaleShortToQuantum((Quantum) pixel);
940                 }
941                 break;
942               }
943               case 32:
944               {
945                 unsigned int
946                   pixel;
947
948                 for (i=0; i < (ssize_t) image->colors; i++)
949                 {
950                   p=PushLongPixel(MSBEndian,p,&pixel);
951                   image->colormap[i].red=(MagickRealType)
952                     ScaleLongToQuantum((Quantum) pixel);
953                   p=PushLongPixel(MSBEndian,p,&pixel);
954                   image->colormap[i].green=(MagickRealType)
955                     ScaleLongToQuantum((Quantum) pixel);
956                   p=PushLongPixel(MSBEndian,p,&pixel);
957                   image->colormap[i].blue=(MagickRealType)
958                     ScaleLongToQuantum((Quantum) pixel);
959                 }
960                 break;
961               }
962             }
963             colormap=(unsigned char *) RelinquishMagickMemory(colormap);
964           }
965       }
966     if (EOFBlob(image) != MagickFalse)
967       {
968         ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
969           image->filename);
970         break;
971       }
972     if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
973       if (image->scene >= (image_info->scene+image_info->number_scenes-1))
974         break;
975     if ((AcquireMagickResource(WidthResource,image->columns) == MagickFalse) ||
976         (AcquireMagickResource(HeightResource,image->rows) == MagickFalse))
977       ThrowReaderException(ImageError,"WidthOrHeightExceedsLimit");
978     /*
979       Attach persistent pixel cache.
980     */
981     status=PersistPixelCache(image,cache_filename,MagickTrue,&offset,exception);
982     if (status == MagickFalse)
983       ThrowReaderException(CacheError,"UnableToPersistPixelCache");
984     /*
985       Proceed to next image.
986     */
987     do
988     {
989       c=ReadBlobByte(image);
990     } while ((isgraph(c) == MagickFalse) && (c != EOF));
991     if (c != EOF)
992       {
993         /*
994           Allocate next image structure.
995         */
996         AcquireNextImage(image_info,image,exception);
997         if (GetNextImageInList(image) == (Image *) NULL)
998           {
999             image=DestroyImageList(image);
1000             return((Image *) NULL);
1001           }
1002         image=SyncNextImageInList(image);
1003         status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
1004           GetBlobSize(image));
1005         if (status == MagickFalse)
1006           break;
1007       }
1008   } while (c != EOF);
1009   (void) CloseBlob(image);
1010   return(GetFirstImageInList(image));
1011 }
1012 \f
1013 /*
1014 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1015 %                                                                             %
1016 %                                                                             %
1017 %                                                                             %
1018 %   R e g i s t e r M P C I m a g e                                           %
1019 %                                                                             %
1020 %                                                                             %
1021 %                                                                             %
1022 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1023 %
1024 %  RegisterMPCImage() adds properties for the Cache image format to
1025 %  the list of supported formats.  The properties include the image format
1026 %  tag, a method to read and/or write the format, whether the format
1027 %  supports the saving of more than one frame to the same file or blob,
1028 %  whether the format supports native in-memory I/O, and a brief
1029 %  description of the format.
1030 %
1031 %  The format of the RegisterMPCImage method is:
1032 %
1033 %      size_t RegisterMPCImage(void)
1034 %
1035 */
1036 ModuleExport size_t RegisterMPCImage(void)
1037 {
1038   MagickInfo
1039     *entry;
1040
1041   entry=AcquireMagickInfo("MPC","CACHE",
1042     "Magick Persistent Cache image format");
1043   entry->flags|=CoderStealthFlag;
1044   (void) RegisterMagickInfo(entry);
1045   entry=AcquireMagickInfo("MPC","MPC","Magick Persistent Cache image format");
1046   entry->decoder=(DecodeImageHandler *) ReadMPCImage;
1047   entry->encoder=(EncodeImageHandler *) WriteMPCImage;
1048   entry->magick=(IsImageFormatHandler *) IsMPC;
1049   entry->flags|=CoderDecoderSeekableStreamFlag;
1050   (void) RegisterMagickInfo(entry);
1051   return(MagickImageCoderSignature);
1052 }
1053 \f
1054 /*
1055 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1056 %                                                                             %
1057 %                                                                             %
1058 %                                                                             %
1059 %   U n r e g i s t e r M P C I m a g e                                       %
1060 %                                                                             %
1061 %                                                                             %
1062 %                                                                             %
1063 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1064 %
1065 %  UnregisterMPCImage() removes format registrations made by the
1066 %  MPC module from the list of supported formats.
1067 %
1068 %  The format of the UnregisterMPCImage method is:
1069 %
1070 %      UnregisterMPCImage(void)
1071 %
1072 */
1073 ModuleExport void UnregisterMPCImage(void)
1074 {
1075   (void) UnregisterMagickInfo("CACHE");
1076   (void) UnregisterMagickInfo("MPC");
1077 }
1078 \f
1079 /*
1080 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1081 %                                                                             %
1082 %                                                                             %
1083 %                                                                             %
1084 %   W r i t e M P C I m a g e                                                 %
1085 %                                                                             %
1086 %                                                                             %
1087 %                                                                             %
1088 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1089 %
1090 %  WriteMPCImage() writes an Magick Persistent Cache image to a file.
1091 %
1092 %  The format of the WriteMPCImage method is:
1093 %
1094 %      MagickBooleanType WriteMPCImage(const ImageInfo *image_info,
1095 %        Image *image,ExceptionInfo *exception)
1096 %
1097 %  A description of each parameter follows:
1098 %
1099 %    o image_info: the image info.
1100 %
1101 %    o image: the image.
1102 %
1103 %    o exception: return any errors or warnings in this structure.
1104 %
1105 */
1106
1107 static inline int open_utf8(const char *path,int flags,mode_t mode)
1108 {
1109 #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__MINGW64__)
1110   return(open(path,flags,mode));
1111 #else
1112    int
1113      status;
1114
1115    wchar_t
1116      *path_wide;
1117
1118    path_wide=create_wchar_path(path);
1119    if (path_wide == (wchar_t *) NULL)
1120      return(-1);
1121    status=_wopen(path_wide,flags,mode);
1122    path_wide=(wchar_t *) RelinquishMagickMemory(path_wide);
1123    return(status);
1124 #endif
1125 }
1126
1127 static MagickBooleanType WriteMPCImage(const ImageInfo *image_info,Image *image,
1128   ExceptionInfo *exception)
1129 {
1130   char
1131     buffer[MagickPathExtent],
1132     cache_filename[MagickPathExtent];
1133
1134   const char
1135     *property,
1136     *value;
1137
1138   MagickBooleanType
1139     status;
1140
1141   MagickOffsetType
1142     offset,
1143     scene;
1144
1145   register ssize_t
1146     i;
1147
1148   size_t
1149     depth;
1150
1151   /*
1152     Open persistent cache.
1153   */
1154   assert(image_info != (const ImageInfo *) NULL);
1155   assert(image_info->signature == MagickCoreSignature);
1156   assert(image != (Image *) NULL);
1157   assert(image->signature == MagickCoreSignature);
1158   if (image->debug != MagickFalse)
1159     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1160   assert(exception != (ExceptionInfo *) NULL);
1161   assert(exception->signature == MagickCoreSignature);
1162   status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
1163   if (status == MagickFalse)
1164     return(status);
1165   (void) CopyMagickString(cache_filename,image->filename,MagickPathExtent);
1166   AppendImageFormat("cache",cache_filename);
1167   scene=0;
1168   offset=0;
1169   do
1170   {
1171     /*
1172       Write persistent cache meta-information.
1173     */
1174     depth=GetImageQuantumDepth(image,MagickTrue);
1175     if ((image->storage_class == PseudoClass) &&
1176         (image->colors > (size_t) (GetQuantumRange(image->depth)+1)))
1177       (void) SetImageStorageClass(image,DirectClass,exception);
1178     (void) WriteBlobString(image,"id=MagickCache\n");
1179     (void) FormatLocaleString(buffer,MagickPathExtent,"magick-signature=%u\n",
1180       GetMagickSignature((const StringInfo *) NULL));
1181     (void) WriteBlobString(image,buffer);
1182     (void) FormatLocaleString(buffer,MagickPathExtent,
1183       "class=%s  colors=%.20g  alpha-trait=%s\n",CommandOptionToMnemonic(
1184       MagickClassOptions,image->storage_class),(double) image->colors,
1185       CommandOptionToMnemonic(MagickPixelTraitOptions,(ssize_t)
1186       image->alpha_trait));
1187     (void) WriteBlobString(image,buffer);
1188     (void) FormatLocaleString(buffer,MagickPathExtent,
1189       "number-channels=%.20g  number-meta-channels=%.20g\n",
1190       (double) image->number_channels,(double) image->number_meta_channels);
1191     (void) WriteBlobString(image,buffer);
1192     (void) FormatLocaleString(buffer,MagickPathExtent,
1193       "columns=%.20g  rows=%.20g depth=%.20g\n",(double) image->columns,
1194       (double) image->rows,(double) image->depth);
1195     (void) WriteBlobString(image,buffer);
1196     if (image->type != UndefinedType)
1197       {
1198         (void) FormatLocaleString(buffer,MagickPathExtent,"type=%s\n",
1199           CommandOptionToMnemonic(MagickTypeOptions,image->type));
1200         (void) WriteBlobString(image,buffer);
1201       }
1202     (void) FormatLocaleString(buffer,MagickPathExtent,"colorspace=%s\n",
1203       CommandOptionToMnemonic(MagickColorspaceOptions,image->colorspace));
1204     (void) WriteBlobString(image,buffer);
1205     if (image->intensity != UndefinedPixelIntensityMethod)
1206       {
1207         (void) FormatLocaleString(buffer,MagickPathExtent,
1208           "pixel-intensity=%s\n",CommandOptionToMnemonic(
1209           MagickPixelIntensityOptions,image->intensity));
1210         (void) WriteBlobString(image,buffer);
1211       }
1212     if (image->endian != UndefinedEndian)
1213       {
1214         (void) FormatLocaleString(buffer,MagickPathExtent,"endian=%s\n",
1215           CommandOptionToMnemonic(MagickEndianOptions,image->endian));
1216         (void) WriteBlobString(image,buffer);
1217       }
1218     if (image->compression != UndefinedCompression)
1219       {
1220         (void) FormatLocaleString(buffer,MagickPathExtent,
1221           "compression=%s  quality=%.20g\n",CommandOptionToMnemonic(
1222           MagickCompressOptions,image->compression),(double) image->quality);
1223         (void) WriteBlobString(image,buffer);
1224       }
1225     if (image->units != UndefinedResolution)
1226       {
1227         (void) FormatLocaleString(buffer,MagickPathExtent,"units=%s\n",
1228           CommandOptionToMnemonic(MagickResolutionOptions,image->units));
1229         (void) WriteBlobString(image,buffer);
1230       }
1231     if ((image->resolution.x != 0) || (image->resolution.y != 0))
1232       {
1233         (void) FormatLocaleString(buffer,MagickPathExtent,
1234           "resolution=%gx%g\n",image->resolution.x,image->resolution.y);
1235         (void) WriteBlobString(image,buffer);
1236       }
1237     if ((image->page.width != 0) || (image->page.height != 0))
1238       {
1239         (void) FormatLocaleString(buffer,MagickPathExtent,
1240           "page=%.20gx%.20g%+.20g%+.20g\n",(double) image->page.width,(double)
1241           image->page.height,(double) image->page.x,(double) image->page.y);
1242         (void) WriteBlobString(image,buffer);
1243       }
1244     else
1245       if ((image->page.x != 0) || (image->page.y != 0))
1246         {
1247           (void) FormatLocaleString(buffer,MagickPathExtent,"page=%+ld%+ld\n",
1248             (long) image->page.x,(long) image->page.y);
1249           (void) WriteBlobString(image,buffer);
1250         }
1251     if ((image->tile_offset.x != 0) || (image->tile_offset.y != 0))
1252       {
1253         (void) FormatLocaleString(buffer,MagickPathExtent,
1254           "tile-offset=%+ld%+ld\n",(long) image->tile_offset.x,(long)
1255            image->tile_offset.y);
1256         (void) WriteBlobString(image,buffer);
1257       }
1258     if ((GetNextImageInList(image) != (Image *) NULL) ||
1259         (GetPreviousImageInList(image) != (Image *) NULL))
1260       {
1261         if (image->scene == 0)
1262           (void) FormatLocaleString(buffer,MagickPathExtent,
1263             "iterations=%.20g  delay=%.20g  ticks-per-second=%.20g\n",(double)
1264             image->iterations,(double) image->delay,(double)
1265             image->ticks_per_second);
1266         else
1267           (void) FormatLocaleString(buffer,MagickPathExtent,"scene=%.20g  "
1268             "iterations=%.20g  delay=%.20g  ticks-per-second=%.20g\n",
1269             (double) image->scene,(double) image->iterations,(double)
1270             image->delay,(double) image->ticks_per_second);
1271         (void) WriteBlobString(image,buffer);
1272       }
1273     else
1274       {
1275         if (image->scene != 0)
1276           {
1277             (void) FormatLocaleString(buffer,MagickPathExtent,"scene=%.20g\n",
1278               (double) image->scene);
1279             (void) WriteBlobString(image,buffer);
1280           }
1281         if (image->iterations != 0)
1282           {
1283             (void) FormatLocaleString(buffer,MagickPathExtent,
1284               "iterations=%.20g\n",(double) image->iterations);
1285             (void) WriteBlobString(image,buffer);
1286           }
1287         if (image->delay != 0)
1288           {
1289             (void) FormatLocaleString(buffer,MagickPathExtent,"delay=%.20g\n",
1290               (double) image->delay);
1291             (void) WriteBlobString(image,buffer);
1292           }
1293         if (image->ticks_per_second != UndefinedTicksPerSecond)
1294           {
1295             (void) FormatLocaleString(buffer,MagickPathExtent,
1296               "ticks-per-second=%.20g\n",(double) image->ticks_per_second);
1297             (void) WriteBlobString(image,buffer);
1298           }
1299       }
1300     if (image->gravity != UndefinedGravity)
1301       {
1302         (void) FormatLocaleString(buffer,MagickPathExtent,"gravity=%s\n",
1303           CommandOptionToMnemonic(MagickGravityOptions,image->gravity));
1304         (void) WriteBlobString(image,buffer);
1305       }
1306     if (image->dispose != UndefinedDispose)
1307       {
1308         (void) FormatLocaleString(buffer,MagickPathExtent,"dispose=%s\n",
1309           CommandOptionToMnemonic(MagickDisposeOptions,image->dispose));
1310         (void) WriteBlobString(image,buffer);
1311       }
1312     if (image->rendering_intent != UndefinedIntent)
1313       {
1314         (void) FormatLocaleString(buffer,MagickPathExtent,
1315           "rendering-intent=%s\n",CommandOptionToMnemonic(MagickIntentOptions,
1316           image->rendering_intent));
1317         (void) WriteBlobString(image,buffer);
1318       }
1319     if (image->gamma != 0.0)
1320       {
1321         (void) FormatLocaleString(buffer,MagickPathExtent,"gamma=%g\n",
1322           image->gamma);
1323         (void) WriteBlobString(image,buffer);
1324       }
1325     if (image->chromaticity.white_point.x != 0.0)
1326       {
1327         /*
1328           Note chomaticity points.
1329         */
1330         (void) FormatLocaleString(buffer,MagickPathExtent,"red-primary="
1331           "%g,%g  green-primary=%g,%g  blue-primary=%g,%g\n",
1332           image->chromaticity.red_primary.x,image->chromaticity.red_primary.y,
1333           image->chromaticity.green_primary.x,
1334           image->chromaticity.green_primary.y,
1335           image->chromaticity.blue_primary.x,
1336           image->chromaticity.blue_primary.y);
1337         (void) WriteBlobString(image,buffer);
1338         (void) FormatLocaleString(buffer,MagickPathExtent,
1339           "white-point=%g,%g\n",image->chromaticity.white_point.x,
1340           image->chromaticity.white_point.y);
1341         (void) WriteBlobString(image,buffer);
1342       }
1343     if (image->orientation != UndefinedOrientation)
1344       {
1345         (void) FormatLocaleString(buffer,MagickPathExtent,
1346           "orientation=%s\n",CommandOptionToMnemonic(MagickOrientationOptions,
1347           image->orientation));
1348         (void) WriteBlobString(image,buffer);
1349       }
1350     if (image->profiles != (void *) NULL)
1351       {
1352         const char
1353           *name;
1354
1355         const StringInfo
1356           *profile;
1357
1358         /*
1359           Generic profile.
1360         */
1361         ResetImageProfileIterator(image);
1362         for (name=GetNextImageProfile(image); name != (const char *) NULL; )
1363         {
1364           profile=GetImageProfile(image,name);
1365           if (profile != (StringInfo *) NULL)
1366             {
1367               (void) FormatLocaleString(buffer,MagickPathExtent,
1368                 "profile:%s=%.20g\n",name,(double)
1369                 GetStringInfoLength(profile));
1370               (void) WriteBlobString(image,buffer);
1371             }
1372           name=GetNextImageProfile(image);
1373         }
1374       }
1375     if (image->montage != (char *) NULL)
1376       {
1377         (void) FormatLocaleString(buffer,MagickPathExtent,"montage=%s\n",
1378           image->montage);
1379         (void) WriteBlobString(image,buffer);
1380       }
1381     ResetImagePropertyIterator(image);
1382     property=GetNextImageProperty(image);
1383     while (property != (const char *) NULL)
1384     {
1385       (void) FormatLocaleString(buffer,MagickPathExtent,"%s=",property);
1386       (void) WriteBlobString(image,buffer);
1387       value=GetImageProperty(image,property,exception);
1388       if (value != (const char *) NULL)
1389         {
1390           size_t
1391             length;
1392
1393           length=strlen(value);
1394           for (i=0; i < (ssize_t) length; i++)
1395             if (isspace((int) ((unsigned char) value[i])) != 0)
1396               break;
1397           if ((i == (ssize_t) length) && (i != 0))
1398             (void) WriteBlob(image,length,(const unsigned char *) value);
1399           else
1400             {
1401               (void) WriteBlobByte(image,'{');
1402               if (strchr(value,'}') == (char *) NULL)
1403                 (void) WriteBlob(image,length,(const unsigned char *) value);
1404               else
1405                 for (i=0; i < (ssize_t) length; i++)
1406                 {
1407                   if (value[i] == (int) '}')
1408                     (void) WriteBlobByte(image,'\\');
1409                   (void) WriteBlobByte(image,(unsigned char) value[i]);
1410                 }
1411               (void) WriteBlobByte(image,'}');
1412             }
1413         }
1414       (void) WriteBlobByte(image,'\n');
1415       property=GetNextImageProperty(image);
1416     }
1417     (void) WriteBlobString(image,"\f\n:\032");
1418     if (image->montage != (char *) NULL)
1419       {
1420         /*
1421           Write montage tile directory.
1422         */
1423         if (image->directory != (char *) NULL)
1424           (void) WriteBlobString(image,image->directory);
1425         (void) WriteBlobByte(image,'\0');
1426       }
1427     if (image->profiles != 0)
1428       {
1429         const char
1430           *name;
1431
1432         const StringInfo
1433           *profile;
1434
1435         /*
1436           Write image profiles.
1437         */
1438         ResetImageProfileIterator(image);
1439         name=GetNextImageProfile(image);
1440         while (name != (const char *) NULL)
1441         {
1442           profile=GetImageProfile(image,name);
1443           (void) WriteBlob(image,GetStringInfoLength(profile),
1444             GetStringInfoDatum(profile));
1445           name=GetNextImageProfile(image);
1446         }
1447       }
1448     if (image->storage_class == PseudoClass)
1449       {
1450         size_t
1451           packet_size;
1452
1453         unsigned char
1454           *colormap,
1455           *q;
1456
1457         /*
1458           Allocate colormap.
1459         */
1460         packet_size=(size_t) (3UL*depth/8UL);
1461         colormap=(unsigned char *) AcquireQuantumMemory(image->colors,
1462           packet_size*sizeof(*colormap));
1463         if (colormap == (unsigned char *) NULL)
1464           return(MagickFalse);
1465         /*
1466           Write colormap to file.
1467         */
1468         q=colormap;
1469         for (i=0; i < (ssize_t) image->colors; i++)
1470         {
1471           switch (depth)
1472           {
1473             default:
1474               ThrowWriterException(CorruptImageError,"ImageDepthNotSupported");
1475             case 32:
1476             {
1477               unsigned int
1478                 pixel;
1479
1480               pixel=ScaleQuantumToLong((Quantum) image->colormap[i].red);
1481               q=PopLongPixel(MSBEndian,pixel,q);
1482               pixel=ScaleQuantumToLong((Quantum) image->colormap[i].green);
1483               q=PopLongPixel(MSBEndian,pixel,q);
1484               pixel=ScaleQuantumToLong((Quantum) image->colormap[i].blue);
1485               q=PopLongPixel(MSBEndian,pixel,q);
1486               break;
1487             }
1488             case 16:
1489             {
1490               unsigned short
1491                 pixel;
1492
1493               pixel=ScaleQuantumToShort((Quantum) image->colormap[i].red);
1494               q=PopShortPixel(MSBEndian,pixel,q);
1495               pixel=ScaleQuantumToShort((Quantum) image->colormap[i].green);
1496               q=PopShortPixel(MSBEndian,pixel,q);
1497               pixel=ScaleQuantumToShort((Quantum) image->colormap[i].blue);
1498               q=PopShortPixel(MSBEndian,pixel,q);
1499               break;
1500             }
1501             case 8:
1502             {
1503               unsigned char
1504                 pixel;
1505
1506               pixel=(unsigned char) ScaleQuantumToChar((Quantum)
1507                 image->colormap[i].red);
1508               q=PopCharPixel(pixel,q);
1509               pixel=(unsigned char) ScaleQuantumToChar((Quantum)
1510                 image->colormap[i].green);
1511               q=PopCharPixel(pixel,q);
1512               pixel=(unsigned char) ScaleQuantumToChar((Quantum)
1513                 image->colormap[i].blue);
1514               q=PopCharPixel(pixel,q);
1515               break;
1516             }
1517           }
1518         }
1519         (void) WriteBlob(image,packet_size*image->colors,colormap);
1520         colormap=(unsigned char *) RelinquishMagickMemory(colormap);
1521       }
1522     {
1523       int
1524         file;
1525
1526       MagickSizeType
1527         length;
1528
1529       register MagickOffsetType
1530         i;
1531
1532       ssize_t
1533         count;
1534
1535       unsigned char
1536         *pixels;
1537
1538       /*
1539         Persistent pixel cache.
1540       */
1541       pixels=(unsigned char *)  GetImagePixelCachePixels(image,&length);
1542       if (pixels == (unsigned char *) NULL)
1543         ThrowWriterException(CacheError,"UnableToWritePixelCache");
1544       if (file == -1)
1545         ThrowWriterException(FileOpenError,"UnableToOpenFile");
1546       file=open_utf8(cache_filename,O_WRONLY | O_CREAT | O_BINARY,S_MODE);
1547       if (file == -1)
1548         ThrowWriterException(FileOpenError,"UnableToOpenFile");
1549       count=0;
1550       for (i=0; i < (MagickOffsetType) length; i+=count)
1551       {
1552 #if !defined(MAGICKCORE_HAVE_PWRITE)
1553         count=write(file,pixels+i,(size_t) MagickMin(length-i,(size_t)
1554           SSIZE_MAX));
1555 #else
1556         count=pwrite(file,pixels+i,(size_t) MagickMin(length-i,(size_t)
1557           SSIZE_MAX),(off_t) i);
1558 #endif 
1559         if (count <= 0)
1560           {
1561             count=0;
1562             if (errno != EINTR)
1563               break;
1564           }
1565       }
1566       close(file);
1567       if (i < (MagickOffsetType) length)
1568         ThrowWriterException(CacheError,"UnableToWritePixelCache");
1569     }
1570     if (GetNextImageInList(image) == (Image *) NULL)
1571       break;
1572     image=SyncNextImageInList(image);
1573     if (image->progress_monitor != (MagickProgressMonitor) NULL)
1574       {
1575         status=image->progress_monitor(SaveImagesTag,scene,
1576           GetImageListLength(image),image->client_data);
1577         if (status == MagickFalse)
1578           break;
1579       }
1580     scene++;
1581   } while (image_info->adjoin != MagickFalse);
1582   (void) CloseBlob(image);
1583   return(status);
1584 }