2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6 % CCCC OOO DDDD EEEEE RRRR %
10 % CCCC OOO DDDD EEEEE R R %
13 % MagickCore Image Coder Methods %
20 % Copyright 1999-2015 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" },
111 { "DATA", "INLINE" },
129 { "FRACTAL", "PLASMA" },
135 { "GRANITE", "MAGICK" },
136 { "GROUP4", "TIFF" },
152 { "IMPLICIT", "***" },
154 { "IPTCTEXT", "META" },
155 { "IPTCWTEXT", "META" },
156 { "ISOBRL", "BRAILLE" },
174 { "LOGO", "MAGICK" },
189 { "NETSCAPE", "MAGICK" },
221 { "RADIAL-GRADIENT", "GRADIENT" },
228 { "ROSE", "MAGICK" },
232 { "SPARSE-COLOR", "TXT" },
237 { "TIFF64", "TIFF" },
240 { "UBRL", "BRAILLE" },
243 { "WIZARD", "MAGICK" },
244 #if defined(MAGICKCORE_WINGDI32_DELEGATE)
251 { "XTRNARRAY", "XTRN" },
252 { "XTRNBLOB", "XTRN" },
253 { "XTRNFILE", "XTRN" },
254 { "XTRNIMAGE", "XTRN" },
257 { "YCbCrA", "YCbCr" }
261 *coder_semaphore = (SemaphoreInfo *) NULL;
264 *coder_cache = (SplayTreeInfo *) NULL;
267 Forward declarations.
269 static MagickBooleanType
270 IsCoderTreeInstantiated(ExceptionInfo *),
271 LoadCoderCache(SplayTreeInfo *,const char *,const char *,const size_t,
275 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
279 + A c q u i r e C o d e r C a c h e %
283 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
285 % AcquireCoderCache() caches one or more coder configurations which provides a
286 % mapping between coder attributes and a coder name.
288 % The format of the AcquireCoderCache coder is:
290 % SplayTreeInfo *AcquireCoderCache(const char *filename,
291 % ExceptionInfo *exception)
293 % A description of each parameter follows:
295 % o filename: the font file name.
297 % o exception: return any errors or warnings in this structure.
301 static void *DestroyCoderNode(void *coder_info)
306 p=(CoderInfo *) coder_info;
307 if (p->exempt == MagickFalse)
309 if (p->path != (char *) NULL)
310 p->path=DestroyString(p->path);
311 if (p->name != (char *) NULL)
312 p->name=DestroyString(p->name);
313 if (p->magick != (char *) NULL)
314 p->magick=DestroyString(p->magick);
316 return(RelinquishMagickMemory(p));
319 static SplayTreeInfo *AcquireCoderCache(const char *filename,
320 ExceptionInfo *exception)
338 Load external coder map.
340 coder_cache=NewSplayTree(CompareSplayTreeString,RelinquishMagickMemory,
342 if (coder_cache == (SplayTreeInfo *) NULL)
343 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
345 options=GetConfigureOptions(filename,exception);
346 option=(const StringInfo *) GetNextValueInLinkedList(options);
347 while (option != (const StringInfo *) NULL)
349 status&=LoadCoderCache(coder_cache,(const char *)
350 GetStringInfoDatum(option),GetStringInfoPath(option),0,exception);
351 option=(const StringInfo *) GetNextValueInLinkedList(options);
353 options=DestroyConfigureOptions(options);
355 Load built-in coder map.
357 for (i=0; i < (ssize_t) (sizeof(CoderMap)/sizeof(*CoderMap)); i++)
362 register const CoderMapInfo
366 coder_info=(CoderInfo *) AcquireMagickMemory(sizeof(*coder_info));
367 if (coder_info == (CoderInfo *) NULL)
369 (void) ThrowMagickException(exception,GetMagickModule(),
370 ResourceLimitError,"MemoryAllocationFailed","`%s'",p->name);
373 (void) ResetMagickMemory(coder_info,0,sizeof(*coder_info));
374 coder_info->path=(char *) "[built-in]";
375 coder_info->magick=(char *) p->magick;
376 coder_info->name=(char *) p->name;
377 coder_info->exempt=MagickTrue;
378 coder_info->signature=MagickCoreSignature;
379 status&=AddValueToSplayTree(coder_cache,ConstantString(coder_info->magick),
381 if (status == MagickFalse)
382 (void) ThrowMagickException(exception,GetMagickModule(),
383 ResourceLimitError,"MemoryAllocationFailed","`%s'",coder_info->name);
389 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
393 + C o d e r C o m p o n e n t G e n e s i s %
397 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
399 % CoderComponentGenesis() instantiates the coder component.
401 % The format of the CoderComponentGenesis method is:
403 % MagickBooleanType CoderComponentGenesis(void)
406 MagickPrivate MagickBooleanType CoderComponentGenesis(void)
408 if (coder_semaphore == (SemaphoreInfo *) NULL)
409 coder_semaphore=AcquireSemaphoreInfo();
414 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
418 + C o d e r C o m p o n e n t T e r m i n u s %
422 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
424 % CoderComponentTerminus() destroys the coder component.
426 % The format of the CoderComponentTerminus method is:
428 % CoderComponentTerminus(void)
431 MagickPrivate void CoderComponentTerminus(void)
433 if (coder_semaphore == (SemaphoreInfo *) NULL)
434 ActivateSemaphoreInfo(&coder_semaphore);
435 LockSemaphoreInfo(coder_semaphore);
436 if (coder_cache != (SplayTreeInfo *) NULL)
437 coder_cache=DestroySplayTree(coder_cache);
438 UnlockSemaphoreInfo(coder_semaphore);
439 RelinquishSemaphoreInfo(&coder_semaphore);
443 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
447 + G e t C o d e r I n f o %
451 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
453 % GetCoderInfo searches the coder list for the specified name and if found
454 % returns attributes for that coder.
456 % The format of the GetCoderInfo method is:
458 % const CoderInfo *GetCoderInfo(const char *name,ExceptionInfo *exception)
460 % A description of each parameter follows:
462 % o name: the coder name.
464 % o exception: return any errors or warnings in this structure.
467 MagickExport const CoderInfo *GetCoderInfo(const char *name,
468 ExceptionInfo *exception)
473 assert(exception != (ExceptionInfo *) NULL);
474 if (IsCoderTreeInstantiated(exception) == MagickFalse)
475 return((const CoderInfo *) NULL);
476 LockSemaphoreInfo(coder_semaphore);
477 if ((name == (const char *) NULL) || (LocaleCompare(name,"*") == 0))
479 ResetSplayTreeIterator(coder_cache);
480 coder_info=(const CoderInfo *) GetNextValueInSplayTree(coder_cache);
481 UnlockSemaphoreInfo(coder_semaphore);
484 coder_info=(const CoderInfo *) GetValueFromSplayTree(coder_cache,name);
485 UnlockSemaphoreInfo(coder_semaphore);
490 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
494 % G e t C o d e r I n f o L i s t %
498 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
500 % GetCoderInfoList() returns any coder_map that match the specified pattern.
501 % The format of the GetCoderInfoList function is:
503 % const CoderInfo **GetCoderInfoList(const char *pattern,
504 % size_t *number_coders,ExceptionInfo *exception)
506 % A description of each parameter follows:
508 % o pattern: Specifies a pointer to a text string containing a pattern.
510 % o number_coders: This integer returns the number of coders in the list.
512 % o exception: return any errors or warnings in this structure.
516 static int CoderInfoCompare(const void *x,const void *y)
522 p=(const CoderInfo **) x,
523 q=(const CoderInfo **) y;
524 if (LocaleCompare((*p)->path,(*q)->path) == 0)
525 return(LocaleCompare((*p)->name,(*q)->name));
526 return(LocaleCompare((*p)->path,(*q)->path));
529 MagickExport const CoderInfo **GetCoderInfoList(const char *pattern,
530 size_t *number_coders,ExceptionInfo *exception)
535 register const CoderInfo
544 assert(pattern != (char *) NULL);
545 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
546 assert(number_coders != (size_t *) NULL);
548 p=GetCoderInfo("*",exception);
549 if (p == (const CoderInfo *) NULL)
550 return((const CoderInfo **) NULL);
551 coder_map=(const CoderInfo **) AcquireQuantumMemory((size_t)
552 GetNumberOfNodesInSplayTree(coder_cache)+1UL,sizeof(*coder_map));
553 if (coder_map == (const CoderInfo **) NULL)
554 return((const CoderInfo **) NULL);
558 LockSemaphoreInfo(coder_semaphore);
559 ResetSplayTreeIterator(coder_cache);
560 p=(const CoderInfo *) GetNextValueInSplayTree(coder_cache);
561 for (i=0; p != (const CoderInfo *) NULL; )
563 if ((p->stealth == MagickFalse) &&
564 (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
566 p=(const CoderInfo *) GetNextValueInSplayTree(coder_cache);
568 UnlockSemaphoreInfo(coder_semaphore);
569 qsort((void *) coder_map,(size_t) i,sizeof(*coder_map),CoderInfoCompare);
570 coder_map[i]=(CoderInfo *) NULL;
571 *number_coders=(size_t) i;
576 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
580 % G e t C o d e r L i s t %
584 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
586 % GetCoderList() returns any coder_map that match the specified pattern.
588 % The format of the GetCoderList function is:
590 % char **GetCoderList(const char *pattern,size_t *number_coders,
591 % ExceptionInfo *exception)
593 % A description of each parameter follows:
595 % o pattern: Specifies a pointer to a text string containing a pattern.
597 % o number_coders: This integer returns the number of coders in the list.
599 % o exception: return any errors or warnings in this structure.
603 static int CoderCompare(const void *x,const void *y)
611 return(LocaleCompare(*p,*q));
614 MagickExport char **GetCoderList(const char *pattern,
615 size_t *number_coders,ExceptionInfo *exception)
620 register const CoderInfo
629 assert(pattern != (char *) NULL);
630 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
631 assert(number_coders != (size_t *) NULL);
633 p=GetCoderInfo("*",exception);
634 if (p == (const CoderInfo *) NULL)
635 return((char **) NULL);
636 coder_map=(char **) AcquireQuantumMemory((size_t)
637 GetNumberOfNodesInSplayTree(coder_cache)+1UL,sizeof(*coder_map));
638 if (coder_map == (char **) NULL)
639 return((char **) NULL);
643 LockSemaphoreInfo(coder_semaphore);
644 ResetSplayTreeIterator(coder_cache);
645 p=(const CoderInfo *) GetNextValueInSplayTree(coder_cache);
646 for (i=0; p != (const CoderInfo *) NULL; )
648 if ((p->stealth == MagickFalse) &&
649 (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
650 coder_map[i++]=ConstantString(p->name);
651 p=(const CoderInfo *) GetNextValueInSplayTree(coder_cache);
653 UnlockSemaphoreInfo(coder_semaphore);
654 qsort((void *) coder_map,(size_t) i,sizeof(*coder_map),CoderCompare);
655 coder_map[i]=(char *) NULL;
656 *number_coders=(size_t) i;
661 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
665 + I s C o d e r T r e e I n s t a n t i a t e d %
669 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
671 % IsCoderTreeInstantiated() determines if the coder tree is instantiated. If
672 % not, it instantiates the tree and returns it.
674 % The format of the IsCoderInstantiated method is:
676 % MagickBooleanType IsCoderTreeInstantiated(ExceptionInfo *exception)
678 % A description of each parameter follows.
680 % o exception: return any errors or warnings in this structure.
683 static MagickBooleanType IsCoderTreeInstantiated(ExceptionInfo *exception)
685 if (coder_cache == (SplayTreeInfo *) NULL)
687 if (coder_semaphore == (SemaphoreInfo *) NULL)
688 ActivateSemaphoreInfo(&coder_semaphore);
689 LockSemaphoreInfo(coder_semaphore);
690 if (coder_cache == (SplayTreeInfo *) NULL)
691 coder_cache=AcquireCoderCache(MagickCoderFilename,exception);
692 UnlockSemaphoreInfo(coder_semaphore);
694 return(coder_cache != (SplayTreeInfo *) NULL ? MagickTrue : MagickFalse);
698 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
702 % L i s t C o d e r I n f o %
706 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
708 % ListCoderInfo() lists the coder info to a file.
710 % The format of the ListCoderInfo coder is:
712 % MagickBooleanType ListCoderInfo(FILE *file,ExceptionInfo *exception)
714 % A description of each parameter follows.
716 % o file: An pointer to a FILE.
718 % o exception: return any errors or warnings in this structure.
721 MagickExport MagickBooleanType ListCoderInfo(FILE *file,
722 ExceptionInfo *exception)
739 if (file == (const FILE *) NULL)
741 coder_info=GetCoderInfoList("*",&number_coders,exception);
742 if (coder_info == (const CoderInfo **) NULL)
744 path=(const char *) NULL;
745 for (i=0; i < (ssize_t) number_coders; i++)
747 if (coder_info[i]->stealth != MagickFalse)
749 if ((path == (const char *) NULL) ||
750 (LocaleCompare(path,coder_info[i]->path) != 0))
752 if (coder_info[i]->path != (char *) NULL)
753 (void) FormatLocaleFile(file,"\nPath: %s\n\n",coder_info[i]->path);
754 (void) FormatLocaleFile(file,"Magick Coder\n");
755 (void) FormatLocaleFile(file,
756 "-------------------------------------------------"
757 "------------------------------\n");
759 path=coder_info[i]->path;
760 (void) FormatLocaleFile(file,"%s",coder_info[i]->magick);
761 for (j=(ssize_t) strlen(coder_info[i]->magick); j <= 11; j++)
762 (void) FormatLocaleFile(file," ");
763 if (coder_info[i]->name != (char *) NULL)
764 (void) FormatLocaleFile(file,"%s",coder_info[i]->name);
765 (void) FormatLocaleFile(file,"\n");
767 coder_info=(const CoderInfo **) RelinquishMagickMemory((void *) coder_info);
773 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
777 + L o a d C o d e r L i s t %
781 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
783 % LoadCoderCache() loads the coder configurations which provides a
784 % mapping between coder attributes and a coder name.
786 % The format of the LoadCoderCache coder is:
788 % MagickBooleanType LoadCoderCache(SplayTreeInfo *coder_cache,
789 % const char *xml,const char *filename,const size_t depth,
790 % ExceptionInfo *exception)
792 % A description of each parameter follows:
794 % o xml: The coder list in XML format.
796 % o filename: The coder list filename.
798 % o depth: depth of <include /> statements.
800 % o exception: return any errors or warnings in this structure.
803 static MagickBooleanType LoadCoderCache(SplayTreeInfo *coder_cache,
804 const char *xml,const char *filename,const size_t depth,
805 ExceptionInfo *exception)
808 keyword[MagickPathExtent],
821 Load the coder map file.
823 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
824 "Loading coder configuration file \"%s\" ...",filename);
825 if (xml == (const char *) NULL)
828 coder_info=(CoderInfo *) NULL;
829 token=AcquireString(xml);
830 for (q=(char *) xml; *q != '\0'; )
835 GetMagickToken(q,&q,token);
838 (void) CopyMagickString(keyword,token,MagickPathExtent);
839 if (LocaleNCompare(keyword,"<!DOCTYPE",9) == 0)
844 while ((LocaleNCompare(q,"]>",2) != 0) && (*q != '\0'))
845 GetMagickToken(q,&q,token);
848 if (LocaleNCompare(keyword,"<!--",4) == 0)
853 while ((LocaleNCompare(q,"->",2) != 0) && (*q != '\0'))
854 GetMagickToken(q,&q,token);
857 if (LocaleCompare(keyword,"<include") == 0)
862 while (((*token != '/') && (*(token+1) != '>')) && (*q != '\0'))
864 (void) CopyMagickString(keyword,token,MagickPathExtent);
865 GetMagickToken(q,&q,token);
868 GetMagickToken(q,&q,token);
869 if (LocaleCompare(keyword,"file") == 0)
872 (void) ThrowMagickException(exception,GetMagickModule(),
873 ConfigureError,"IncludeNodeNestedTooDeeply","`%s'",token);
877 path[MagickPathExtent],
880 GetPathComponent(filename,HeadPath,path);
882 (void) ConcatenateMagickString(path,DirectorySeparator,
884 if (*token == *DirectorySeparator)
885 (void) CopyMagickString(path,token,MagickPathExtent);
887 (void) ConcatenateMagickString(path,token,MagickPathExtent);
888 xml=FileToXML(path,~0UL);
889 if (xml != (char *) NULL)
891 status&=LoadCoderCache(coder_cache,xml,path,depth+1,
893 xml=(char *) RelinquishMagickMemory(xml);
900 if (LocaleCompare(keyword,"<coder") == 0)
905 coder_info=(CoderInfo *) AcquireMagickMemory(sizeof(*coder_info));
906 if (coder_info == (CoderInfo *) NULL)
907 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
908 (void) ResetMagickMemory(coder_info,0,sizeof(*coder_info));
909 coder_info->path=ConstantString(filename);
910 coder_info->exempt=MagickFalse;
911 coder_info->signature=MagickCoreSignature;
914 if (coder_info == (CoderInfo *) NULL)
916 if (LocaleCompare(keyword,"/>") == 0)
918 status=AddValueToSplayTree(coder_cache,ConstantString(
919 coder_info->magick),coder_info);
920 if (status == MagickFalse)
921 (void) ThrowMagickException(exception,GetMagickModule(),
922 ResourceLimitError,"MemoryAllocationFailed","`%s'",
924 coder_info=(CoderInfo *) NULL;
927 GetMagickToken(q,(const char **) NULL,token);
930 GetMagickToken(q,&q,token);
931 GetMagickToken(q,&q,token);
937 if (LocaleCompare((char *) keyword,"magick") == 0)
939 coder_info->magick=ConstantString(token);
947 if (LocaleCompare((char *) keyword,"name") == 0)
949 coder_info->name=ConstantString(token);
957 if (LocaleCompare((char *) keyword,"stealth") == 0)
959 coder_info->stealth=IsStringTrue(token);
968 token=(char *) RelinquishMagickMemory(token);
969 return(status != 0 ? MagickTrue : MagickFalse);