2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6 % CCCC OOO DDDD EEEEE RRRR %
10 % CCCC OOO DDDD EEEEE R R %
13 % MagickCore Image Coder Methods %
20 % Copyright 1999-2013 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"
66 #define MagickCoderFilename "coder.xml"
71 typedef struct _CoderMapInfo
81 static const CoderMapInfo
86 { "8BIMTEXT", "META" },
87 { "8BIMWTEXT", "META" },
91 { "APP1JPEG", "META" },
123 { "FRACTAL", "PLASMA" },
129 { "GRANITE", "MAGICK" },
130 { "GROUP4", "TIFF" },
141 { "IMPLICIT", "***" },
143 { "IPTCTEXT", "META" },
144 { "IPTCWTEXT", "META" },
145 { "ISOBRL", "BRAILLE" },
154 { "LOGO", "MAGICK" },
167 { "NETSCAPE", "MAGICK" },
194 { "RADIAL-GRADIENT", "GRADIENT" },
200 { "ROSE", "MAGICK" },
207 { "TIFF64", "TIFF" },
210 { "UBRL", "BRAILLE" },
213 { "WIZARD", "MAGICK" },
215 { "WMFWIN32", "EMF" },
219 { "XTRNARRAY", "XTRN" },
220 { "XTRNBLOB", "XTRN" },
221 { "XTRNFILE", "XTRN" },
222 { "XTRNIMAGE", "XTRN" },
225 { "YCbCrA", "YCbCr" }
229 *coder_semaphore = (SemaphoreInfo *) NULL;
232 *coder_list = (SplayTreeInfo *) NULL;
234 static volatile MagickBooleanType
235 instantiate_coder = MagickFalse;
238 Forward declarations.
240 static MagickBooleanType
241 InitializeCoderList(ExceptionInfo *),
242 LoadCoderLists(const char *,ExceptionInfo *);
245 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
249 + C o d e r C o m p o n e n t G e n e s i s %
253 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
255 % CoderComponentGenesis() instantiates the coder component.
257 % The format of the CoderComponentGenesis method is:
259 % MagickBooleanType CoderComponentGenesis(void)
262 MagickPrivate MagickBooleanType CoderComponentGenesis(void)
264 AcquireSemaphoreInfo(&coder_semaphore);
269 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
273 + C o d e r C o m p o n e n t T e r m i n u s %
277 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
279 % CoderComponentTerminus() destroys the coder component.
281 % The format of the CoderComponentTerminus method is:
283 % CoderComponentTerminus(void)
286 MagickPrivate void CoderComponentTerminus(void)
288 if (coder_semaphore == (SemaphoreInfo *) NULL)
289 AcquireSemaphoreInfo(&coder_semaphore);
290 LockSemaphoreInfo(coder_semaphore);
291 if (coder_list != (SplayTreeInfo *) NULL)
292 coder_list=DestroySplayTree(coder_list);
293 instantiate_coder=MagickFalse;
294 UnlockSemaphoreInfo(coder_semaphore);
295 DestroySemaphoreInfo(&coder_semaphore);
299 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
303 + G e t C o d e r I n f o %
307 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
309 % GetCoderInfo searches the coder list for the specified name and if found
310 % returns attributes for that coder.
312 % The format of the GetCoderInfo method is:
314 % const CoderInfo *GetCoderInfo(const char *name,ExceptionInfo *exception)
316 % A description of each parameter follows:
318 % o name: the coder name.
320 % o exception: return any errors or warnings in this structure.
323 MagickExport const CoderInfo *GetCoderInfo(const char *name,
324 ExceptionInfo *exception)
326 assert(exception != (ExceptionInfo *) NULL);
327 if ((coder_list == (SplayTreeInfo *) NULL) ||
328 (instantiate_coder == MagickFalse))
329 if (InitializeCoderList(exception) == MagickFalse)
330 return((const CoderInfo *) NULL);
331 if ((coder_list == (SplayTreeInfo *) NULL) ||
332 (GetNumberOfNodesInSplayTree(coder_list) == 0))
333 return((const CoderInfo *) NULL);
334 if ((name == (const char *) NULL) || (LocaleCompare(name,"*") == 0))
336 ResetSplayTreeIterator(coder_list);
337 return((const CoderInfo *) GetNextValueInSplayTree(coder_list));
339 return((const CoderInfo *) GetValueFromSplayTree(coder_list,name));
343 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
347 % G e t C o d e r I n f o L i s t %
351 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
353 % GetCoderInfoList() returns any coder_map that match the specified pattern.
354 % The format of the GetCoderInfoList function is:
356 % const CoderInfo **GetCoderInfoList(const char *pattern,
357 % size_t *number_coders,ExceptionInfo *exception)
359 % A description of each parameter follows:
361 % o pattern: Specifies a pointer to a text string containing a pattern.
363 % o number_coders: This integer returns the number of coders in the list.
365 % o exception: return any errors or warnings in this structure.
369 static int CoderInfoCompare(const void *x,const void *y)
375 p=(const CoderInfo **) x,
376 q=(const CoderInfo **) y;
377 if (LocaleCompare((*p)->path,(*q)->path) == 0)
378 return(LocaleCompare((*p)->name,(*q)->name));
379 return(LocaleCompare((*p)->path,(*q)->path));
382 MagickExport const CoderInfo **GetCoderInfoList(const char *pattern,
383 size_t *number_coders,ExceptionInfo *exception)
388 register const CoderInfo
397 assert(pattern != (char *) NULL);
398 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
399 assert(number_coders != (size_t *) NULL);
401 p=GetCoderInfo("*",exception);
402 if (p == (const CoderInfo *) NULL)
403 return((const CoderInfo **) NULL);
404 coder_map=(const CoderInfo **) AcquireQuantumMemory((size_t)
405 GetNumberOfNodesInSplayTree(coder_list)+1UL,sizeof(*coder_map));
406 if (coder_map == (const CoderInfo **) NULL)
407 return((const CoderInfo **) NULL);
411 LockSemaphoreInfo(coder_semaphore);
412 ResetSplayTreeIterator(coder_list);
413 p=(const CoderInfo *) GetNextValueInSplayTree(coder_list);
414 for (i=0; p != (const CoderInfo *) NULL; )
416 if ((p->stealth == MagickFalse) &&
417 (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
419 p=(const CoderInfo *) GetNextValueInSplayTree(coder_list);
421 UnlockSemaphoreInfo(coder_semaphore);
422 qsort((void *) coder_map,(size_t) i,sizeof(*coder_map),CoderInfoCompare);
423 coder_map[i]=(CoderInfo *) NULL;
424 *number_coders=(size_t) i;
429 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
433 % G e t C o d e r L i s t %
437 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
439 % GetCoderList() returns any coder_map that match the specified pattern.
441 % The format of the GetCoderList function is:
443 % char **GetCoderList(const char *pattern,size_t *number_coders,
444 % ExceptionInfo *exception)
446 % A description of each parameter follows:
448 % o pattern: Specifies a pointer to a text string containing a pattern.
450 % o number_coders: This integer returns the number of coders in the list.
452 % o exception: return any errors or warnings in this structure.
456 static int CoderCompare(const void *x,const void *y)
464 return(LocaleCompare(*p,*q));
467 MagickExport char **GetCoderList(const char *pattern,
468 size_t *number_coders,ExceptionInfo *exception)
473 register const CoderInfo
482 assert(pattern != (char *) NULL);
483 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
484 assert(number_coders != (size_t *) NULL);
486 p=GetCoderInfo("*",exception);
487 if (p == (const CoderInfo *) NULL)
488 return((char **) NULL);
489 coder_map=(char **) AcquireQuantumMemory((size_t)
490 GetNumberOfNodesInSplayTree(coder_list)+1UL,sizeof(*coder_map));
491 if (coder_map == (char **) NULL)
492 return((char **) NULL);
496 LockSemaphoreInfo(coder_semaphore);
497 ResetSplayTreeIterator(coder_list);
498 p=(const CoderInfo *) GetNextValueInSplayTree(coder_list);
499 for (i=0; p != (const CoderInfo *) NULL; )
501 if ((p->stealth == MagickFalse) &&
502 (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
503 coder_map[i++]=ConstantString(p->name);
504 p=(const CoderInfo *) GetNextValueInSplayTree(coder_list);
506 UnlockSemaphoreInfo(coder_semaphore);
507 qsort((void *) coder_map,(size_t) i,sizeof(*coder_map),CoderCompare);
508 coder_map[i]=(char *) NULL;
509 *number_coders=(size_t) i;
514 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
518 + I n i t i a l i z e C o d e r L i s t %
522 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
524 % InitializeCoderList() initializes the coder list.
526 % The format of the InitializeCoderList method is:
528 % MagickBooleanType InitializeCoderList(ExceptionInfo *exception)
530 % A description of each parameter follows.
532 % o exception: return any errors or warnings in this structure.
535 static MagickBooleanType InitializeCoderList(ExceptionInfo *exception)
537 if ((coder_list == (SplayTreeInfo *) NULL) &&
538 (instantiate_coder == MagickFalse))
540 if (coder_semaphore == (SemaphoreInfo *) NULL)
541 AcquireSemaphoreInfo(&coder_semaphore);
542 LockSemaphoreInfo(coder_semaphore);
543 if ((coder_list == (SplayTreeInfo *) NULL) &&
544 (instantiate_coder == MagickFalse))
546 (void) LoadCoderLists(MagickCoderFilename,exception);
547 instantiate_coder=MagickTrue;
549 UnlockSemaphoreInfo(coder_semaphore);
551 return(coder_list != (SplayTreeInfo *) NULL ? MagickTrue : MagickFalse);
555 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
559 % L i s t C o d e r I n f o %
563 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
565 % ListCoderInfo() lists the coder info to a file.
567 % The format of the ListCoderInfo coder is:
569 % MagickBooleanType ListCoderInfo(FILE *file,ExceptionInfo *exception)
571 % A description of each parameter follows.
573 % o file: An pointer to a FILE.
575 % o exception: return any errors or warnings in this structure.
578 MagickExport MagickBooleanType ListCoderInfo(FILE *file,
579 ExceptionInfo *exception)
596 if (file == (const FILE *) NULL)
598 coder_info=GetCoderInfoList("*",&number_coders,exception);
599 if (coder_info == (const CoderInfo **) NULL)
601 path=(const char *) NULL;
602 for (i=0; i < (ssize_t) number_coders; i++)
604 if (coder_info[i]->stealth != MagickFalse)
606 if ((path == (const char *) NULL) ||
607 (LocaleCompare(path,coder_info[i]->path) != 0))
609 if (coder_info[i]->path != (char *) NULL)
610 (void) FormatLocaleFile(file,"\nPath: %s\n\n",coder_info[i]->path);
611 (void) FormatLocaleFile(file,"Magick Coder\n");
612 (void) FormatLocaleFile(file,
613 "-------------------------------------------------"
614 "------------------------------\n");
616 path=coder_info[i]->path;
617 (void) FormatLocaleFile(file,"%s",coder_info[i]->magick);
618 for (j=(ssize_t) strlen(coder_info[i]->magick); j <= 11; j++)
619 (void) FormatLocaleFile(file," ");
620 if (coder_info[i]->name != (char *) NULL)
621 (void) FormatLocaleFile(file,"%s",coder_info[i]->name);
622 (void) FormatLocaleFile(file,"\n");
624 coder_info=(const CoderInfo **) RelinquishMagickMemory((void *) coder_info);
630 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
634 + L o a d C o d e r L i s t %
638 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
640 % LoadCoderList() loads the coder configuration file which provides a
641 % mapping between coder attributes and a coder name.
643 % The format of the LoadCoderList coder is:
645 % MagickBooleanType LoadCoderList(const char *xml,const char *filename,
646 % const size_t depth,ExceptionInfo *exception)
648 % A description of each parameter follows:
650 % o xml: The coder list in XML format.
652 % o filename: The coder list filename.
654 % o depth: depth of <include /> statements.
656 % o exception: return any errors or warnings in this structure.
660 static void *DestroyCoderNode(void *coder_info)
665 p=(CoderInfo *) coder_info;
666 if (p->exempt == MagickFalse)
668 if (p->path != (char *) NULL)
669 p->path=DestroyString(p->path);
670 if (p->name != (char *) NULL)
671 p->name=DestroyString(p->name);
672 if (p->magick != (char *) NULL)
673 p->magick=DestroyString(p->magick);
675 return(RelinquishMagickMemory(p));
678 static MagickBooleanType LoadCoderList(const char *xml,const char *filename,
679 const size_t depth,ExceptionInfo *exception)
682 keyword[MaxTextExtent],
695 Load the coder map file.
697 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
698 "Loading coder configuration file \"%s\" ...",filename);
699 if (xml == (const char *) NULL)
701 if (coder_list == (SplayTreeInfo *) NULL)
703 coder_list=NewSplayTree(CompareSplayTreeString,RelinquishMagickMemory,
705 if (coder_list == (SplayTreeInfo *) NULL)
707 ThrowFileException(exception,ResourceLimitError,
708 "MemoryAllocationFailed",filename);
713 coder_info=(CoderInfo *) NULL;
714 token=AcquireString(xml);
715 for (q=(char *) xml; *q != '\0'; )
720 GetMagickToken(q,&q,token);
723 (void) CopyMagickString(keyword,token,MaxTextExtent);
724 if (LocaleNCompare(keyword,"<!DOCTYPE",9) == 0)
729 while ((LocaleNCompare(q,"]>",2) != 0) && (*q != '\0'))
730 GetMagickToken(q,&q,token);
733 if (LocaleNCompare(keyword,"<!--",4) == 0)
738 while ((LocaleNCompare(q,"->",2) != 0) && (*q != '\0'))
739 GetMagickToken(q,&q,token);
742 if (LocaleCompare(keyword,"<include") == 0)
747 while (((*token != '/') && (*(token+1) != '>')) && (*q != '\0'))
749 (void) CopyMagickString(keyword,token,MaxTextExtent);
750 GetMagickToken(q,&q,token);
753 GetMagickToken(q,&q,token);
754 if (LocaleCompare(keyword,"file") == 0)
757 (void) ThrowMagickException(exception,GetMagickModule(),
758 ConfigureError,"IncludeNodeNestedTooDeeply","`%s'",token);
765 GetPathComponent(filename,HeadPath,path);
767 (void) ConcatenateMagickString(path,DirectorySeparator,
769 if (*token == *DirectorySeparator)
770 (void) CopyMagickString(path,token,MaxTextExtent);
772 (void) ConcatenateMagickString(path,token,MaxTextExtent);
773 xml=FileToString(path,~0,exception);
774 if (xml != (char *) NULL)
776 status=LoadCoderList(xml,path,depth+1,exception);
777 xml=(char *) RelinquishMagickMemory(xml);
784 if (LocaleCompare(keyword,"<coder") == 0)
789 coder_info=(CoderInfo *) AcquireMagickMemory(sizeof(*coder_info));
790 if (coder_info == (CoderInfo *) NULL)
791 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
792 (void) ResetMagickMemory(coder_info,0,sizeof(*coder_info));
793 coder_info->path=ConstantString(filename);
794 coder_info->exempt=MagickFalse;
795 coder_info->signature=MagickSignature;
798 if (coder_info == (CoderInfo *) NULL)
800 if (LocaleCompare(keyword,"/>") == 0)
802 status=AddValueToSplayTree(coder_list,ConstantString(
803 coder_info->magick),coder_info);
804 if (status == MagickFalse)
805 (void) ThrowMagickException(exception,GetMagickModule(),
806 ResourceLimitError,"MemoryAllocationFailed","`%s'",
808 coder_info=(CoderInfo *) NULL;
810 GetMagickToken(q,(const char **) NULL,token);
813 GetMagickToken(q,&q,token);
814 GetMagickToken(q,&q,token);
820 if (LocaleCompare((char *) keyword,"magick") == 0)
822 coder_info->magick=ConstantString(token);
830 if (LocaleCompare((char *) keyword,"name") == 0)
832 coder_info->name=ConstantString(token);
840 if (LocaleCompare((char *) keyword,"stealth") == 0)
842 coder_info->stealth=IsStringTrue(token);
851 token=(char *) RelinquishMagickMemory(token);
856 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
860 % L o a d C o d e r L i s t s %
864 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
866 % LoadCoderLists() loads one or more coder configuration file which
867 % provides a mapping between coder attributes and a coder name.
869 % The format of the LoadCoderLists coder is:
871 % MagickBooleanType LoadCoderLists(const char *filename,
872 % ExceptionInfo *exception)
874 % A description of each parameter follows:
876 % o filename: the font file name.
878 % o exception: return any errors or warnings in this structure.
881 static MagickBooleanType LoadCoderLists(const char *filename,
882 ExceptionInfo *exception)
897 Load built-in coder map.
900 if (coder_list == (SplayTreeInfo *) NULL)
902 coder_list=NewSplayTree(CompareSplayTreeString,RelinquishMagickMemory,
904 if (coder_list == (SplayTreeInfo *) NULL)
906 ThrowFileException(exception,ResourceLimitError,
907 "MemoryAllocationFailed",filename);
911 for (i=0; i < (ssize_t) (sizeof(CoderMap)/sizeof(*CoderMap)); i++)
916 register const CoderMapInfo
920 coder_info=(CoderInfo *) AcquireMagickMemory(sizeof(*coder_info));
921 if (coder_info == (CoderInfo *) NULL)
923 (void) ThrowMagickException(exception,GetMagickModule(),
924 ResourceLimitError,"MemoryAllocationFailed","`%s'",coder_info->name);
927 (void) ResetMagickMemory(coder_info,0,sizeof(*coder_info));
928 coder_info->path=(char *) "[built-in]";
929 coder_info->magick=(char *) p->magick;
930 coder_info->name=(char *) p->name;
931 coder_info->exempt=MagickTrue;
932 coder_info->signature=MagickSignature;
933 status=AddValueToSplayTree(coder_list,ConstantString(coder_info->magick),
935 if (status == MagickFalse)
936 (void) ThrowMagickException(exception,GetMagickModule(),
937 ResourceLimitError,"MemoryAllocationFailed","`%s'",coder_info->name);
940 Load external coder map.
942 options=GetConfigureOptions(filename,exception);
943 option=(const StringInfo *) GetNextValueInLinkedList(options);
944 while (option != (const StringInfo *) NULL)
946 status|=LoadCoderList((const char *) GetStringInfoDatum(option),
947 GetStringInfoPath(option),0,exception);
948 option=(const StringInfo *) GetNextValueInLinkedList(options);
950 options=DestroyConfigureOptions(options);
951 return(status != 0 ? MagickTrue : MagickFalse);