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" },
140 { "IMPLICIT", "***" },
142 { "IPTCTEXT", "META" },
143 { "IPTCWTEXT", "META" },
144 { "ISOBRL", "BRAILLE" },
153 { "LOGO", "MAGICK" },
166 { "NETSCAPE", "MAGICK" },
193 { "RADIAL-GRADIENT", "GRADIENT" },
199 { "ROSE", "MAGICK" },
206 { "TIFF64", "TIFF" },
209 { "UBRL", "BRAILLE" },
212 { "WIZARD", "MAGICK" },
214 { "WMFWIN32", "EMF" },
218 { "XTRNARRAY", "XTRN" },
219 { "XTRNBLOB", "XTRN" },
220 { "XTRNFILE", "XTRN" },
221 { "XTRNIMAGE", "XTRN" },
224 { "YCbCrA", "YCbCr" }
228 *coder_semaphore = (SemaphoreInfo *) NULL;
231 *coder_list = (SplayTreeInfo *) NULL;
233 static volatile MagickBooleanType
234 instantiate_coder = MagickFalse;
237 Forward declarations.
239 static MagickBooleanType
240 InitializeCoderList(ExceptionInfo *),
241 LoadCoderLists(const char *,ExceptionInfo *);
244 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
248 + C o d e r C o m p o n e n t G e n e s i s %
252 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
254 % CoderComponentGenesis() instantiates the coder component.
256 % The format of the CoderComponentGenesis method is:
258 % MagickBooleanType CoderComponentGenesis(void)
261 MagickPrivate MagickBooleanType CoderComponentGenesis(void)
263 AcquireSemaphoreInfo(&coder_semaphore);
268 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
272 + C o d e r C o m p o n e n t T e r m i n u s %
276 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
278 % CoderComponentTerminus() destroys the coder component.
280 % The format of the CoderComponentTerminus method is:
282 % CoderComponentTerminus(void)
285 MagickPrivate void CoderComponentTerminus(void)
287 if (coder_semaphore == (SemaphoreInfo *) NULL)
288 AcquireSemaphoreInfo(&coder_semaphore);
289 LockSemaphoreInfo(coder_semaphore);
290 if (coder_list != (SplayTreeInfo *) NULL)
291 coder_list=DestroySplayTree(coder_list);
292 instantiate_coder=MagickFalse;
293 UnlockSemaphoreInfo(coder_semaphore);
294 DestroySemaphoreInfo(&coder_semaphore);
298 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
302 + G e t C o d e r I n f o %
306 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
308 % GetCoderInfo searches the coder list for the specified name and if found
309 % returns attributes for that coder.
311 % The format of the GetCoderInfo method is:
313 % const CoderInfo *GetCoderInfo(const char *name,ExceptionInfo *exception)
315 % A description of each parameter follows:
317 % o name: the coder name.
319 % o exception: return any errors or warnings in this structure.
322 MagickExport const CoderInfo *GetCoderInfo(const char *name,
323 ExceptionInfo *exception)
325 assert(exception != (ExceptionInfo *) NULL);
326 if ((coder_list == (SplayTreeInfo *) NULL) ||
327 (instantiate_coder == MagickFalse))
328 if (InitializeCoderList(exception) == MagickFalse)
329 return((const CoderInfo *) NULL);
330 if ((coder_list == (SplayTreeInfo *) NULL) ||
331 (GetNumberOfNodesInSplayTree(coder_list) == 0))
332 return((const CoderInfo *) NULL);
333 if ((name == (const char *) NULL) || (LocaleCompare(name,"*") == 0))
335 ResetSplayTreeIterator(coder_list);
336 return((const CoderInfo *) GetNextValueInSplayTree(coder_list));
338 return((const CoderInfo *) GetValueFromSplayTree(coder_list,name));
342 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
346 % G e t C o d e r I n f o L i s t %
350 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
352 % GetCoderInfoList() returns any coder_map that match the specified pattern.
353 % The format of the GetCoderInfoList function is:
355 % const CoderInfo **GetCoderInfoList(const char *pattern,
356 % size_t *number_coders,ExceptionInfo *exception)
358 % A description of each parameter follows:
360 % o pattern: Specifies a pointer to a text string containing a pattern.
362 % o number_coders: This integer returns the number of coders in the list.
364 % o exception: return any errors or warnings in this structure.
368 static int CoderInfoCompare(const void *x,const void *y)
374 p=(const CoderInfo **) x,
375 q=(const CoderInfo **) y;
376 if (LocaleCompare((*p)->path,(*q)->path) == 0)
377 return(LocaleCompare((*p)->name,(*q)->name));
378 return(LocaleCompare((*p)->path,(*q)->path));
381 MagickExport const CoderInfo **GetCoderInfoList(const char *pattern,
382 size_t *number_coders,ExceptionInfo *exception)
387 register const CoderInfo
396 assert(pattern != (char *) NULL);
397 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
398 assert(number_coders != (size_t *) NULL);
400 p=GetCoderInfo("*",exception);
401 if (p == (const CoderInfo *) NULL)
402 return((const CoderInfo **) NULL);
403 coder_map=(const CoderInfo **) AcquireQuantumMemory((size_t)
404 GetNumberOfNodesInSplayTree(coder_list)+1UL,sizeof(*coder_map));
405 if (coder_map == (const CoderInfo **) NULL)
406 return((const CoderInfo **) NULL);
410 LockSemaphoreInfo(coder_semaphore);
411 ResetSplayTreeIterator(coder_list);
412 p=(const CoderInfo *) GetNextValueInSplayTree(coder_list);
413 for (i=0; p != (const CoderInfo *) NULL; )
415 if ((p->stealth == MagickFalse) &&
416 (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
418 p=(const CoderInfo *) GetNextValueInSplayTree(coder_list);
420 UnlockSemaphoreInfo(coder_semaphore);
421 qsort((void *) coder_map,(size_t) i,sizeof(*coder_map),CoderInfoCompare);
422 coder_map[i]=(CoderInfo *) NULL;
423 *number_coders=(size_t) i;
428 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
432 % G e t C o d e r L i s t %
436 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
438 % GetCoderList() returns any coder_map that match the specified pattern.
440 % The format of the GetCoderList function is:
442 % char **GetCoderList(const char *pattern,size_t *number_coders,
443 % ExceptionInfo *exception)
445 % A description of each parameter follows:
447 % o pattern: Specifies a pointer to a text string containing a pattern.
449 % o number_coders: This integer returns the number of coders in the list.
451 % o exception: return any errors or warnings in this structure.
455 static int CoderCompare(const void *x,const void *y)
463 return(LocaleCompare(*p,*q));
466 MagickExport char **GetCoderList(const char *pattern,
467 size_t *number_coders,ExceptionInfo *exception)
472 register const CoderInfo
481 assert(pattern != (char *) NULL);
482 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
483 assert(number_coders != (size_t *) NULL);
485 p=GetCoderInfo("*",exception);
486 if (p == (const CoderInfo *) NULL)
487 return((char **) NULL);
488 coder_map=(char **) AcquireQuantumMemory((size_t)
489 GetNumberOfNodesInSplayTree(coder_list)+1UL,sizeof(*coder_map));
490 if (coder_map == (char **) NULL)
491 return((char **) NULL);
495 LockSemaphoreInfo(coder_semaphore);
496 ResetSplayTreeIterator(coder_list);
497 p=(const CoderInfo *) GetNextValueInSplayTree(coder_list);
498 for (i=0; p != (const CoderInfo *) NULL; )
500 if ((p->stealth == MagickFalse) &&
501 (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
502 coder_map[i++]=ConstantString(p->name);
503 p=(const CoderInfo *) GetNextValueInSplayTree(coder_list);
505 UnlockSemaphoreInfo(coder_semaphore);
506 qsort((void *) coder_map,(size_t) i,sizeof(*coder_map),CoderCompare);
507 coder_map[i]=(char *) NULL;
508 *number_coders=(size_t) i;
513 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
517 + I n i t i a l i z e C o d e r L i s t %
521 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
523 % InitializeCoderList() initializes the coder list.
525 % The format of the InitializeCoderList method is:
527 % MagickBooleanType InitializeCoderList(ExceptionInfo *exception)
529 % A description of each parameter follows.
531 % o exception: return any errors or warnings in this structure.
534 static MagickBooleanType InitializeCoderList(ExceptionInfo *exception)
536 if ((coder_list == (SplayTreeInfo *) NULL) &&
537 (instantiate_coder == MagickFalse))
539 if (coder_semaphore == (SemaphoreInfo *) NULL)
540 AcquireSemaphoreInfo(&coder_semaphore);
541 LockSemaphoreInfo(coder_semaphore);
542 if ((coder_list == (SplayTreeInfo *) NULL) &&
543 (instantiate_coder == MagickFalse))
545 (void) LoadCoderLists(MagickCoderFilename,exception);
546 instantiate_coder=MagickTrue;
548 UnlockSemaphoreInfo(coder_semaphore);
550 return(coder_list != (SplayTreeInfo *) NULL ? MagickTrue : MagickFalse);
554 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
558 % L i s t C o d e r I n f o %
562 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
564 % ListCoderInfo() lists the coder info to a file.
566 % The format of the ListCoderInfo coder is:
568 % MagickBooleanType ListCoderInfo(FILE *file,ExceptionInfo *exception)
570 % A description of each parameter follows.
572 % o file: An pointer to a FILE.
574 % o exception: return any errors or warnings in this structure.
577 MagickExport MagickBooleanType ListCoderInfo(FILE *file,
578 ExceptionInfo *exception)
595 if (file == (const FILE *) NULL)
597 coder_info=GetCoderInfoList("*",&number_coders,exception);
598 if (coder_info == (const CoderInfo **) NULL)
600 path=(const char *) NULL;
601 for (i=0; i < (ssize_t) number_coders; i++)
603 if (coder_info[i]->stealth != MagickFalse)
605 if ((path == (const char *) NULL) ||
606 (LocaleCompare(path,coder_info[i]->path) != 0))
608 if (coder_info[i]->path != (char *) NULL)
609 (void) FormatLocaleFile(file,"\nPath: %s\n\n",coder_info[i]->path);
610 (void) FormatLocaleFile(file,"Magick Coder\n");
611 (void) FormatLocaleFile(file,
612 "-------------------------------------------------"
613 "------------------------------\n");
615 path=coder_info[i]->path;
616 (void) FormatLocaleFile(file,"%s",coder_info[i]->magick);
617 for (j=(ssize_t) strlen(coder_info[i]->magick); j <= 11; j++)
618 (void) FormatLocaleFile(file," ");
619 if (coder_info[i]->name != (char *) NULL)
620 (void) FormatLocaleFile(file,"%s",coder_info[i]->name);
621 (void) FormatLocaleFile(file,"\n");
623 coder_info=(const CoderInfo **) RelinquishMagickMemory((void *) coder_info);
629 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
633 + L o a d C o d e r L i s t %
637 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
639 % LoadCoderList() loads the coder configuration file which provides a
640 % mapping between coder attributes and a coder name.
642 % The format of the LoadCoderList coder is:
644 % MagickBooleanType LoadCoderList(const char *xml,const char *filename,
645 % const size_t depth,ExceptionInfo *exception)
647 % A description of each parameter follows:
649 % o xml: The coder list in XML format.
651 % o filename: The coder list filename.
653 % o depth: depth of <include /> statements.
655 % o exception: return any errors or warnings in this structure.
659 static void *DestroyCoderNode(void *coder_info)
664 p=(CoderInfo *) coder_info;
665 if (p->exempt == MagickFalse)
667 if (p->path != (char *) NULL)
668 p->path=DestroyString(p->path);
669 if (p->name != (char *) NULL)
670 p->name=DestroyString(p->name);
671 if (p->magick != (char *) NULL)
672 p->magick=DestroyString(p->magick);
674 return(RelinquishMagickMemory(p));
677 static MagickBooleanType LoadCoderList(const char *xml,const char *filename,
678 const size_t depth,ExceptionInfo *exception)
681 keyword[MaxTextExtent],
694 Load the coder map file.
696 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
697 "Loading coder configuration file \"%s\" ...",filename);
698 if (xml == (const char *) NULL)
700 if (coder_list == (SplayTreeInfo *) NULL)
702 coder_list=NewSplayTree(CompareSplayTreeString,RelinquishMagickMemory,
704 if (coder_list == (SplayTreeInfo *) NULL)
706 ThrowFileException(exception,ResourceLimitError,
707 "MemoryAllocationFailed",filename);
712 coder_info=(CoderInfo *) NULL;
713 token=AcquireString(xml);
714 for (q=(char *) xml; *q != '\0'; )
719 GetMagickToken(q,&q,token);
722 (void) CopyMagickString(keyword,token,MaxTextExtent);
723 if (LocaleNCompare(keyword,"<!DOCTYPE",9) == 0)
728 while ((LocaleNCompare(q,"]>",2) != 0) && (*q != '\0'))
729 GetMagickToken(q,&q,token);
732 if (LocaleNCompare(keyword,"<!--",4) == 0)
737 while ((LocaleNCompare(q,"->",2) != 0) && (*q != '\0'))
738 GetMagickToken(q,&q,token);
741 if (LocaleCompare(keyword,"<include") == 0)
746 while (((*token != '/') && (*(token+1) != '>')) && (*q != '\0'))
748 (void) CopyMagickString(keyword,token,MaxTextExtent);
749 GetMagickToken(q,&q,token);
752 GetMagickToken(q,&q,token);
753 if (LocaleCompare(keyword,"file") == 0)
756 (void) ThrowMagickException(exception,GetMagickModule(),
757 ConfigureError,"IncludeNodeNestedTooDeeply","'%s'",token);
764 GetPathComponent(filename,HeadPath,path);
766 (void) ConcatenateMagickString(path,DirectorySeparator,
768 if (*token == *DirectorySeparator)
769 (void) CopyMagickString(path,token,MaxTextExtent);
771 (void) ConcatenateMagickString(path,token,MaxTextExtent);
772 xml=FileToString(path,~0,exception);
773 if (xml != (char *) NULL)
775 status=LoadCoderList(xml,path,depth+1,exception);
776 xml=(char *) RelinquishMagickMemory(xml);
783 if (LocaleCompare(keyword,"<coder") == 0)
788 coder_info=(CoderInfo *) AcquireMagickMemory(sizeof(*coder_info));
789 if (coder_info == (CoderInfo *) NULL)
790 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
791 (void) ResetMagickMemory(coder_info,0,sizeof(*coder_info));
792 coder_info->path=ConstantString(filename);
793 coder_info->exempt=MagickFalse;
794 coder_info->signature=MagickSignature;
797 if (coder_info == (CoderInfo *) NULL)
799 if (LocaleCompare(keyword,"/>") == 0)
801 status=AddValueToSplayTree(coder_list,ConstantString(
802 coder_info->magick),coder_info);
803 if (status == MagickFalse)
804 (void) ThrowMagickException(exception,GetMagickModule(),
805 ResourceLimitError,"MemoryAllocationFailed","'%s'",
807 coder_info=(CoderInfo *) NULL;
809 GetMagickToken(q,(const char **) NULL,token);
812 GetMagickToken(q,&q,token);
813 GetMagickToken(q,&q,token);
819 if (LocaleCompare((char *) keyword,"magick") == 0)
821 coder_info->magick=ConstantString(token);
829 if (LocaleCompare((char *) keyword,"name") == 0)
831 coder_info->name=ConstantString(token);
839 if (LocaleCompare((char *) keyword,"stealth") == 0)
841 coder_info->stealth=IsStringTrue(token);
850 token=(char *) RelinquishMagickMemory(token);
855 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
859 % L o a d C o d e r L i s t s %
863 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
865 % LoadCoderLists() loads one or more coder configuration file which
866 % provides a mapping between coder attributes and a coder name.
868 % The format of the LoadCoderLists coder is:
870 % MagickBooleanType LoadCoderLists(const char *filename,
871 % ExceptionInfo *exception)
873 % A description of each parameter follows:
875 % o filename: the font file name.
877 % o exception: return any errors or warnings in this structure.
880 static MagickBooleanType LoadCoderLists(const char *filename,
881 ExceptionInfo *exception)
896 Load built-in coder map.
899 if (coder_list == (SplayTreeInfo *) NULL)
901 coder_list=NewSplayTree(CompareSplayTreeString,RelinquishMagickMemory,
903 if (coder_list == (SplayTreeInfo *) NULL)
905 ThrowFileException(exception,ResourceLimitError,
906 "MemoryAllocationFailed",filename);
910 for (i=0; i < (ssize_t) (sizeof(CoderMap)/sizeof(*CoderMap)); i++)
915 register const CoderMapInfo
919 coder_info=(CoderInfo *) AcquireMagickMemory(sizeof(*coder_info));
920 if (coder_info == (CoderInfo *) NULL)
922 (void) ThrowMagickException(exception,GetMagickModule(),
923 ResourceLimitError,"MemoryAllocationFailed","'%s'",coder_info->name);
926 (void) ResetMagickMemory(coder_info,0,sizeof(*coder_info));
927 coder_info->path=(char *) "[built-in]";
928 coder_info->magick=(char *) p->magick;
929 coder_info->name=(char *) p->name;
930 coder_info->exempt=MagickTrue;
931 coder_info->signature=MagickSignature;
932 status=AddValueToSplayTree(coder_list,ConstantString(coder_info->magick),
934 if (status == MagickFalse)
935 (void) ThrowMagickException(exception,GetMagickModule(),
936 ResourceLimitError,"MemoryAllocationFailed","'%s'",coder_info->name);
939 Load external coder map.
941 options=GetConfigureOptions(filename,exception);
942 option=(const StringInfo *) GetNextValueInLinkedList(options);
943 while (option != (const StringInfo *) NULL)
945 status|=LoadCoderList((const char *) GetStringInfoDatum(option),
946 GetStringInfoPath(option),0,exception);
947 option=(const StringInfo *) GetNextValueInLinkedList(options);
949 options=DestroyConfigureOptions(options);
950 return(status != 0 ? MagickTrue : MagickFalse);