2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6 % CCCC OOO DDDD EEEEE RRRR %
10 % CCCC OOO DDDD EEEEE R R %
13 % MagickCore Image Coder Methods %
20 % Copyright 1999-2014 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
26 % http://www.imagemagick.org/script/license.php %
28 % Unless required by applicable law or agreed to in writing, software %
29 % distributed under the License is distributed on an "AS IS" BASIS, %
30 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31 % See the License for the specific language governing permissions and %
32 % limitations under the License. %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
42 #include "MagickCore/studio.h"
43 #include "MagickCore/blob.h"
44 #include "MagickCore/client.h"
45 #include "MagickCore/coder.h"
46 #include "MagickCore/coder-private.h"
47 #include "MagickCore/configure.h"
48 #include "MagickCore/draw.h"
49 #include "MagickCore/exception.h"
50 #include "MagickCore/exception-private.h"
51 #include "MagickCore/hashmap.h"
52 #include "MagickCore/log.h"
53 #include "MagickCore/memory_.h"
54 #include "MagickCore/option.h"
55 #include "MagickCore/semaphore.h"
56 #include "MagickCore/string_.h"
57 #include "MagickCore/splay-tree.h"
58 #include "MagickCore/token.h"
59 #include "MagickCore/utility.h"
60 #include "MagickCore/utility-private.h"
61 #include "MagickCore/xml-tree.h"
62 #include "MagickCore/xml-tree-private.h"
67 #define MagickCoderFilename "coder.xml"
72 typedef struct _CoderMapInfo
82 static const CoderMapInfo
87 { "8BIMTEXT", "META" },
88 { "8BIMWTEXT", "META" },
92 { "APP1JPEG", "META" },
100 { "BRF", "BRAILLE" },
124 { "FRACTAL", "PLASMA" },
130 { "GRANITE", "MAGICK" },
131 { "GROUP4", "TIFF" },
144 { "IMPLICIT", "***" },
146 { "IPTCTEXT", "META" },
147 { "IPTCWTEXT", "META" },
148 { "ISOBRL", "BRAILLE" },
159 { "LOGO", "MAGICK" },
172 { "NETSCAPE", "MAGICK" },
199 { "RADIAL-GRADIENT", "GRADIENT" },
207 { "ROSE", "MAGICK" },
211 { "SPARSE-COLOR", "TXT" },
216 { "TIFF64", "TIFF" },
219 { "UBRL", "BRAILLE" },
222 { "WIZARD", "MAGICK" },
223 #if defined(MAGICKCORE_WINGDI32_DELEGATE)
230 { "XTRNARRAY", "XTRN" },
231 { "XTRNBLOB", "XTRN" },
232 { "XTRNFILE", "XTRN" },
233 { "XTRNIMAGE", "XTRN" },
236 { "YCbCrA", "YCbCr" }
240 *coder_semaphore = (SemaphoreInfo *) NULL;
243 *coder_cache = (SplayTreeInfo *) NULL;
246 Forward declarations.
248 static MagickBooleanType
249 IsCoderTreeInstantiated(ExceptionInfo *),
250 LoadCoderCache(SplayTreeInfo *,const char *,const char *,const size_t,
254 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
258 + A c q u i r e C o d e r C a c h e %
262 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
264 % AcquireCoderCache() caches one or more coder configurations which provides a
265 % mapping between coder attributes and a coder name.
267 % The format of the AcquireCoderCache coder is:
269 % SplayTreeInfo *AcquireCoderCache(const char *filename,
270 % ExceptionInfo *exception)
272 % A description of each parameter follows:
274 % o filename: the font file name.
276 % o exception: return any errors or warnings in this structure.
280 static void *DestroyCoderNode(void *coder_info)
285 p=(CoderInfo *) coder_info;
286 if (p->exempt == MagickFalse)
288 if (p->path != (char *) NULL)
289 p->path=DestroyString(p->path);
290 if (p->name != (char *) NULL)
291 p->name=DestroyString(p->name);
292 if (p->magick != (char *) NULL)
293 p->magick=DestroyString(p->magick);
295 return(RelinquishMagickMemory(p));
298 static SplayTreeInfo *AcquireCoderCache(const char *filename,
299 ExceptionInfo *exception)
317 Load external coder map.
319 coder_cache=NewSplayTree(CompareSplayTreeString,RelinquishMagickMemory,
321 if (coder_cache == (SplayTreeInfo *) NULL)
322 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
324 options=GetConfigureOptions(filename,exception);
325 option=(const StringInfo *) GetNextValueInLinkedList(options);
326 while (option != (const StringInfo *) NULL)
328 status&=LoadCoderCache(coder_cache,(const char *)
329 GetStringInfoDatum(option),GetStringInfoPath(option),0,exception);
330 option=(const StringInfo *) GetNextValueInLinkedList(options);
332 options=DestroyConfigureOptions(options);
334 Load built-in coder map.
336 for (i=0; i < (ssize_t) (sizeof(CoderMap)/sizeof(*CoderMap)); i++)
341 register const CoderMapInfo
345 coder_info=(CoderInfo *) AcquireMagickMemory(sizeof(*coder_info));
346 if (coder_info == (CoderInfo *) NULL)
348 (void) ThrowMagickException(exception,GetMagickModule(),
349 ResourceLimitError,"MemoryAllocationFailed","`%s'",p->name);
352 (void) ResetMagickMemory(coder_info,0,sizeof(*coder_info));
353 coder_info->path=(char *) "[built-in]";
354 coder_info->magick=(char *) p->magick;
355 coder_info->name=(char *) p->name;
356 coder_info->exempt=MagickTrue;
357 coder_info->signature=MagickSignature;
358 status&=AddValueToSplayTree(coder_cache,ConstantString(coder_info->magick),
360 if (status == MagickFalse)
361 (void) ThrowMagickException(exception,GetMagickModule(),
362 ResourceLimitError,"MemoryAllocationFailed","`%s'",coder_info->name);
368 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
372 + C o d e r C o m p o n e n t G e n e s i s %
376 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
378 % CoderComponentGenesis() instantiates the coder component.
380 % The format of the CoderComponentGenesis method is:
382 % MagickBooleanType CoderComponentGenesis(void)
385 MagickPrivate MagickBooleanType CoderComponentGenesis(void)
387 if (coder_semaphore == (SemaphoreInfo *) NULL)
388 coder_semaphore=AcquireSemaphoreInfo();
393 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
397 + C o d e r C o m p o n e n t T e r m i n u s %
401 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
403 % CoderComponentTerminus() destroys the coder component.
405 % The format of the CoderComponentTerminus method is:
407 % CoderComponentTerminus(void)
410 MagickPrivate void CoderComponentTerminus(void)
412 if (coder_semaphore == (SemaphoreInfo *) NULL)
413 ActivateSemaphoreInfo(&coder_semaphore);
414 LockSemaphoreInfo(coder_semaphore);
415 if (coder_cache != (SplayTreeInfo *) NULL)
416 coder_cache=DestroySplayTree(coder_cache);
417 UnlockSemaphoreInfo(coder_semaphore);
418 RelinquishSemaphoreInfo(&coder_semaphore);
422 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
426 + G e t C o d e r I n f o %
430 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
432 % GetCoderInfo searches the coder list for the specified name and if found
433 % returns attributes for that coder.
435 % The format of the GetCoderInfo method is:
437 % const CoderInfo *GetCoderInfo(const char *name,ExceptionInfo *exception)
439 % A description of each parameter follows:
441 % o name: the coder name.
443 % o exception: return any errors or warnings in this structure.
446 MagickExport const CoderInfo *GetCoderInfo(const char *name,
447 ExceptionInfo *exception)
452 assert(exception != (ExceptionInfo *) NULL);
453 if (IsCoderTreeInstantiated(exception) == MagickFalse)
454 return((const CoderInfo *) NULL);
455 LockSemaphoreInfo(coder_semaphore);
456 if ((name == (const char *) NULL) || (LocaleCompare(name,"*") == 0))
458 ResetSplayTreeIterator(coder_cache);
459 coder_info=(const CoderInfo *) GetNextValueInSplayTree(coder_cache);
460 UnlockSemaphoreInfo(coder_semaphore);
463 coder_info=(const CoderInfo *) GetValueFromSplayTree(coder_cache,name);
464 UnlockSemaphoreInfo(coder_semaphore);
469 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
473 % G e t C o d e r I n f o L i s t %
477 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
479 % GetCoderInfoList() returns any coder_map that match the specified pattern.
480 % The format of the GetCoderInfoList function is:
482 % const CoderInfo **GetCoderInfoList(const char *pattern,
483 % size_t *number_coders,ExceptionInfo *exception)
485 % A description of each parameter follows:
487 % o pattern: Specifies a pointer to a text string containing a pattern.
489 % o number_coders: This integer returns the number of coders in the list.
491 % o exception: return any errors or warnings in this structure.
495 static int CoderInfoCompare(const void *x,const void *y)
501 p=(const CoderInfo **) x,
502 q=(const CoderInfo **) y;
503 if (LocaleCompare((*p)->path,(*q)->path) == 0)
504 return(LocaleCompare((*p)->name,(*q)->name));
505 return(LocaleCompare((*p)->path,(*q)->path));
508 MagickExport const CoderInfo **GetCoderInfoList(const char *pattern,
509 size_t *number_coders,ExceptionInfo *exception)
514 register const CoderInfo
523 assert(pattern != (char *) NULL);
524 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
525 assert(number_coders != (size_t *) NULL);
527 p=GetCoderInfo("*",exception);
528 if (p == (const CoderInfo *) NULL)
529 return((const CoderInfo **) NULL);
530 coder_map=(const CoderInfo **) AcquireQuantumMemory((size_t)
531 GetNumberOfNodesInSplayTree(coder_cache)+1UL,sizeof(*coder_map));
532 if (coder_map == (const CoderInfo **) NULL)
533 return((const CoderInfo **) NULL);
537 LockSemaphoreInfo(coder_semaphore);
538 ResetSplayTreeIterator(coder_cache);
539 p=(const CoderInfo *) GetNextValueInSplayTree(coder_cache);
540 for (i=0; p != (const CoderInfo *) NULL; )
542 if ((p->stealth == MagickFalse) &&
543 (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
545 p=(const CoderInfo *) GetNextValueInSplayTree(coder_cache);
547 UnlockSemaphoreInfo(coder_semaphore);
548 qsort((void *) coder_map,(size_t) i,sizeof(*coder_map),CoderInfoCompare);
549 coder_map[i]=(CoderInfo *) NULL;
550 *number_coders=(size_t) i;
555 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
559 % G e t C o d e r L i s t %
563 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
565 % GetCoderList() returns any coder_map that match the specified pattern.
567 % The format of the GetCoderList function is:
569 % char **GetCoderList(const char *pattern,size_t *number_coders,
570 % ExceptionInfo *exception)
572 % A description of each parameter follows:
574 % o pattern: Specifies a pointer to a text string containing a pattern.
576 % o number_coders: This integer returns the number of coders in the list.
578 % o exception: return any errors or warnings in this structure.
582 static int CoderCompare(const void *x,const void *y)
590 return(LocaleCompare(*p,*q));
593 MagickExport char **GetCoderList(const char *pattern,
594 size_t *number_coders,ExceptionInfo *exception)
599 register const CoderInfo
608 assert(pattern != (char *) NULL);
609 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
610 assert(number_coders != (size_t *) NULL);
612 p=GetCoderInfo("*",exception);
613 if (p == (const CoderInfo *) NULL)
614 return((char **) NULL);
615 coder_map=(char **) AcquireQuantumMemory((size_t)
616 GetNumberOfNodesInSplayTree(coder_cache)+1UL,sizeof(*coder_map));
617 if (coder_map == (char **) NULL)
618 return((char **) NULL);
622 LockSemaphoreInfo(coder_semaphore);
623 ResetSplayTreeIterator(coder_cache);
624 p=(const CoderInfo *) GetNextValueInSplayTree(coder_cache);
625 for (i=0; p != (const CoderInfo *) NULL; )
627 if ((p->stealth == MagickFalse) &&
628 (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
629 coder_map[i++]=ConstantString(p->name);
630 p=(const CoderInfo *) GetNextValueInSplayTree(coder_cache);
632 UnlockSemaphoreInfo(coder_semaphore);
633 qsort((void *) coder_map,(size_t) i,sizeof(*coder_map),CoderCompare);
634 coder_map[i]=(char *) NULL;
635 *number_coders=(size_t) i;
640 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
644 + I s C o d e r T r e e I n s t a n t i a t e d %
648 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
650 % IsCoderTreeInstantiated() determines if the coder tree is instantiated. If
651 % not, it instantiates the tree and returns it.
653 % The format of the IsCoderInstantiated method is:
655 % MagickBooleanType IsCoderTreeInstantiated(ExceptionInfo *exception)
657 % A description of each parameter follows.
659 % o exception: return any errors or warnings in this structure.
662 static MagickBooleanType IsCoderTreeInstantiated(ExceptionInfo *exception)
664 if (coder_cache == (SplayTreeInfo *) NULL)
666 if (coder_semaphore == (SemaphoreInfo *) NULL)
667 ActivateSemaphoreInfo(&coder_semaphore);
668 LockSemaphoreInfo(coder_semaphore);
669 if (coder_cache == (SplayTreeInfo *) NULL)
670 coder_cache=AcquireCoderCache(MagickCoderFilename,exception);
671 UnlockSemaphoreInfo(coder_semaphore);
673 return(coder_cache != (SplayTreeInfo *) NULL ? MagickTrue : MagickFalse);
677 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
681 % L i s t C o d e r I n f o %
685 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
687 % ListCoderInfo() lists the coder info to a file.
689 % The format of the ListCoderInfo coder is:
691 % MagickBooleanType ListCoderInfo(FILE *file,ExceptionInfo *exception)
693 % A description of each parameter follows.
695 % o file: An pointer to a FILE.
697 % o exception: return any errors or warnings in this structure.
700 MagickExport MagickBooleanType ListCoderInfo(FILE *file,
701 ExceptionInfo *exception)
718 if (file == (const FILE *) NULL)
720 coder_info=GetCoderInfoList("*",&number_coders,exception);
721 if (coder_info == (const CoderInfo **) NULL)
723 path=(const char *) NULL;
724 for (i=0; i < (ssize_t) number_coders; i++)
726 if (coder_info[i]->stealth != MagickFalse)
728 if ((path == (const char *) NULL) ||
729 (LocaleCompare(path,coder_info[i]->path) != 0))
731 if (coder_info[i]->path != (char *) NULL)
732 (void) FormatLocaleFile(file,"\nPath: %s\n\n",coder_info[i]->path);
733 (void) FormatLocaleFile(file,"Magick Coder\n");
734 (void) FormatLocaleFile(file,
735 "-------------------------------------------------"
736 "------------------------------\n");
738 path=coder_info[i]->path;
739 (void) FormatLocaleFile(file,"%s",coder_info[i]->magick);
740 for (j=(ssize_t) strlen(coder_info[i]->magick); j <= 11; j++)
741 (void) FormatLocaleFile(file," ");
742 if (coder_info[i]->name != (char *) NULL)
743 (void) FormatLocaleFile(file,"%s",coder_info[i]->name);
744 (void) FormatLocaleFile(file,"\n");
746 coder_info=(const CoderInfo **) RelinquishMagickMemory((void *) coder_info);
752 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
756 + L o a d C o d e r L i s t %
760 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
762 % LoadCoderCache() loads the coder configurations which provides a
763 % mapping between coder attributes and a coder name.
765 % The format of the LoadCoderCache coder is:
767 % MagickBooleanType LoadCoderCache(SplayTreeInfo *coder_cache,
768 % const char *xml,const char *filename,const size_t depth,
769 % ExceptionInfo *exception)
771 % A description of each parameter follows:
773 % o xml: The coder list in XML format.
775 % o filename: The coder list filename.
777 % o depth: depth of <include /> statements.
779 % o exception: return any errors or warnings in this structure.
782 static MagickBooleanType LoadCoderCache(SplayTreeInfo *coder_cache,
783 const char *xml,const char *filename,const size_t depth,
784 ExceptionInfo *exception)
787 keyword[MaxTextExtent],
800 Load the coder map file.
802 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
803 "Loading coder configuration file \"%s\" ...",filename);
804 if (xml == (const char *) NULL)
807 coder_info=(CoderInfo *) NULL;
808 token=AcquireString(xml);
809 for (q=(char *) xml; *q != '\0'; )
814 GetMagickToken(q,&q,token);
817 (void) CopyMagickString(keyword,token,MaxTextExtent);
818 if (LocaleNCompare(keyword,"<!DOCTYPE",9) == 0)
823 while ((LocaleNCompare(q,"]>",2) != 0) && (*q != '\0'))
824 GetMagickToken(q,&q,token);
827 if (LocaleNCompare(keyword,"<!--",4) == 0)
832 while ((LocaleNCompare(q,"->",2) != 0) && (*q != '\0'))
833 GetMagickToken(q,&q,token);
836 if (LocaleCompare(keyword,"<include") == 0)
841 while (((*token != '/') && (*(token+1) != '>')) && (*q != '\0'))
843 (void) CopyMagickString(keyword,token,MaxTextExtent);
844 GetMagickToken(q,&q,token);
847 GetMagickToken(q,&q,token);
848 if (LocaleCompare(keyword,"file") == 0)
851 (void) ThrowMagickException(exception,GetMagickModule(),
852 ConfigureError,"IncludeNodeNestedTooDeeply","`%s'",token);
859 GetPathComponent(filename,HeadPath,path);
861 (void) ConcatenateMagickString(path,DirectorySeparator,
863 if (*token == *DirectorySeparator)
864 (void) CopyMagickString(path,token,MaxTextExtent);
866 (void) ConcatenateMagickString(path,token,MaxTextExtent);
867 xml=FileToXML(path,~0UL);
868 if (xml != (char *) NULL)
870 status&=LoadCoderCache(coder_cache,xml,path,depth+1,
872 xml=(char *) RelinquishMagickMemory(xml);
879 if (LocaleCompare(keyword,"<coder") == 0)
884 coder_info=(CoderInfo *) AcquireMagickMemory(sizeof(*coder_info));
885 if (coder_info == (CoderInfo *) NULL)
886 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
887 (void) ResetMagickMemory(coder_info,0,sizeof(*coder_info));
888 coder_info->path=ConstantString(filename);
889 coder_info->exempt=MagickFalse;
890 coder_info->signature=MagickSignature;
893 if (coder_info == (CoderInfo *) NULL)
895 if (LocaleCompare(keyword,"/>") == 0)
897 status=AddValueToSplayTree(coder_cache,ConstantString(
898 coder_info->magick),coder_info);
899 if (status == MagickFalse)
900 (void) ThrowMagickException(exception,GetMagickModule(),
901 ResourceLimitError,"MemoryAllocationFailed","`%s'",
903 coder_info=(CoderInfo *) NULL;
906 GetMagickToken(q,(const char **) NULL,token);
909 GetMagickToken(q,&q,token);
910 GetMagickToken(q,&q,token);
916 if (LocaleCompare((char *) keyword,"magick") == 0)
918 coder_info->magick=ConstantString(token);
926 if (LocaleCompare((char *) keyword,"name") == 0)
928 coder_info->name=ConstantString(token);
936 if (LocaleCompare((char *) keyword,"stealth") == 0)
938 coder_info->stealth=IsStringTrue(token);
947 token=(char *) RelinquishMagickMemory(token);
948 return(status != 0 ? MagickTrue : MagickFalse);