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