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"
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" },
142 { "IMPLICIT", "***" },
144 { "IPTCTEXT", "META" },
145 { "IPTCWTEXT", "META" },
146 { "ISOBRL", "BRAILLE" },
155 { "LOGO", "MAGICK" },
168 { "NETSCAPE", "MAGICK" },
195 { "RADIAL-GRADIENT", "GRADIENT" },
201 { "ROSE", "MAGICK" },
204 { "SPARSE-COLOR", "TXT" },
209 { "TIFF64", "TIFF" },
212 { "UBRL", "BRAILLE" },
215 { "WIZARD", "MAGICK" },
216 #if defined(MAGICKCORE_WINGDI32_DELEGATE)
223 { "XTRNARRAY", "XTRN" },
224 { "XTRNBLOB", "XTRN" },
225 { "XTRNFILE", "XTRN" },
226 { "XTRNIMAGE", "XTRN" },
229 { "YCbCrA", "YCbCr" }
233 *coder_semaphore = (SemaphoreInfo *) NULL;
236 *coder_list = (SplayTreeInfo *) NULL;
238 static volatile MagickBooleanType
239 instantiate_coder = MagickFalse;
242 Forward declarations.
244 static MagickBooleanType
245 InitializeCoderList(ExceptionInfo *),
246 LoadCoderLists(const char *,ExceptionInfo *);
249 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
253 + C o d e r C o m p o n e n t G e n e s i s %
257 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
259 % CoderComponentGenesis() instantiates the coder component.
261 % The format of the CoderComponentGenesis method is:
263 % MagickBooleanType CoderComponentGenesis(void)
266 MagickPrivate MagickBooleanType CoderComponentGenesis(void)
268 AcquireSemaphoreInfo(&coder_semaphore);
273 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
277 + C o d e r C o m p o n e n t T e r m i n u s %
281 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
283 % CoderComponentTerminus() destroys the coder component.
285 % The format of the CoderComponentTerminus method is:
287 % CoderComponentTerminus(void)
290 MagickPrivate void CoderComponentTerminus(void)
292 if (coder_semaphore == (SemaphoreInfo *) NULL)
293 AcquireSemaphoreInfo(&coder_semaphore);
294 LockSemaphoreInfo(coder_semaphore);
295 if (coder_list != (SplayTreeInfo *) NULL)
296 coder_list=DestroySplayTree(coder_list);
297 instantiate_coder=MagickFalse;
298 UnlockSemaphoreInfo(coder_semaphore);
299 DestroySemaphoreInfo(&coder_semaphore);
303 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
307 + G e t C o d e r I n f o %
311 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
313 % GetCoderInfo searches the coder list for the specified name and if found
314 % returns attributes for that coder.
316 % The format of the GetCoderInfo method is:
318 % const CoderInfo *GetCoderInfo(const char *name,ExceptionInfo *exception)
320 % A description of each parameter follows:
322 % o name: the coder name.
324 % o exception: return any errors or warnings in this structure.
327 MagickExport const CoderInfo *GetCoderInfo(const char *name,
328 ExceptionInfo *exception)
330 assert(exception != (ExceptionInfo *) NULL);
331 if ((coder_list == (SplayTreeInfo *) NULL) ||
332 (instantiate_coder == MagickFalse))
333 if (InitializeCoderList(exception) == MagickFalse)
334 return((const CoderInfo *) NULL);
335 if ((coder_list == (SplayTreeInfo *) NULL) ||
336 (GetNumberOfNodesInSplayTree(coder_list) == 0))
337 return((const CoderInfo *) NULL);
338 if ((name == (const char *) NULL) || (LocaleCompare(name,"*") == 0))
340 ResetSplayTreeIterator(coder_list);
341 return((const CoderInfo *) GetNextValueInSplayTree(coder_list));
343 return((const CoderInfo *) GetValueFromSplayTree(coder_list,name));
347 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
351 % G e t C o d e r I n f o L i s t %
355 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
357 % GetCoderInfoList() returns any coder_map that match the specified pattern.
358 % The format of the GetCoderInfoList function is:
360 % const CoderInfo **GetCoderInfoList(const char *pattern,
361 % size_t *number_coders,ExceptionInfo *exception)
363 % A description of each parameter follows:
365 % o pattern: Specifies a pointer to a text string containing a pattern.
367 % o number_coders: This integer returns the number of coders in the list.
369 % o exception: return any errors or warnings in this structure.
373 static int CoderInfoCompare(const void *x,const void *y)
379 p=(const CoderInfo **) x,
380 q=(const CoderInfo **) y;
381 if (LocaleCompare((*p)->path,(*q)->path) == 0)
382 return(LocaleCompare((*p)->name,(*q)->name));
383 return(LocaleCompare((*p)->path,(*q)->path));
386 MagickExport const CoderInfo **GetCoderInfoList(const char *pattern,
387 size_t *number_coders,ExceptionInfo *exception)
392 register const CoderInfo
401 assert(pattern != (char *) NULL);
402 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
403 assert(number_coders != (size_t *) NULL);
405 p=GetCoderInfo("*",exception);
406 if (p == (const CoderInfo *) NULL)
407 return((const CoderInfo **) NULL);
408 coder_map=(const CoderInfo **) AcquireQuantumMemory((size_t)
409 GetNumberOfNodesInSplayTree(coder_list)+1UL,sizeof(*coder_map));
410 if (coder_map == (const CoderInfo **) NULL)
411 return((const CoderInfo **) NULL);
415 LockSemaphoreInfo(coder_semaphore);
416 ResetSplayTreeIterator(coder_list);
417 p=(const CoderInfo *) GetNextValueInSplayTree(coder_list);
418 for (i=0; p != (const CoderInfo *) NULL; )
420 if ((p->stealth == MagickFalse) &&
421 (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
423 p=(const CoderInfo *) GetNextValueInSplayTree(coder_list);
425 UnlockSemaphoreInfo(coder_semaphore);
426 qsort((void *) coder_map,(size_t) i,sizeof(*coder_map),CoderInfoCompare);
427 coder_map[i]=(CoderInfo *) NULL;
428 *number_coders=(size_t) i;
433 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
437 % G e t C o d e r L i s t %
441 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
443 % GetCoderList() returns any coder_map that match the specified pattern.
445 % The format of the GetCoderList function is:
447 % char **GetCoderList(const char *pattern,size_t *number_coders,
448 % ExceptionInfo *exception)
450 % A description of each parameter follows:
452 % o pattern: Specifies a pointer to a text string containing a pattern.
454 % o number_coders: This integer returns the number of coders in the list.
456 % o exception: return any errors or warnings in this structure.
460 static int CoderCompare(const void *x,const void *y)
468 return(LocaleCompare(*p,*q));
471 MagickExport char **GetCoderList(const char *pattern,
472 size_t *number_coders,ExceptionInfo *exception)
477 register const CoderInfo
486 assert(pattern != (char *) NULL);
487 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
488 assert(number_coders != (size_t *) NULL);
490 p=GetCoderInfo("*",exception);
491 if (p == (const CoderInfo *) NULL)
492 return((char **) NULL);
493 coder_map=(char **) AcquireQuantumMemory((size_t)
494 GetNumberOfNodesInSplayTree(coder_list)+1UL,sizeof(*coder_map));
495 if (coder_map == (char **) NULL)
496 return((char **) NULL);
500 LockSemaphoreInfo(coder_semaphore);
501 ResetSplayTreeIterator(coder_list);
502 p=(const CoderInfo *) GetNextValueInSplayTree(coder_list);
503 for (i=0; p != (const CoderInfo *) NULL; )
505 if ((p->stealth == MagickFalse) &&
506 (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
507 coder_map[i++]=ConstantString(p->name);
508 p=(const CoderInfo *) GetNextValueInSplayTree(coder_list);
510 UnlockSemaphoreInfo(coder_semaphore);
511 qsort((void *) coder_map,(size_t) i,sizeof(*coder_map),CoderCompare);
512 coder_map[i]=(char *) NULL;
513 *number_coders=(size_t) i;
518 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
522 + I n i t i a l i z e C o d e r L i s t %
526 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
528 % InitializeCoderList() initializes the coder list.
530 % The format of the InitializeCoderList method is:
532 % MagickBooleanType InitializeCoderList(ExceptionInfo *exception)
534 % A description of each parameter follows.
536 % o exception: return any errors or warnings in this structure.
539 static MagickBooleanType InitializeCoderList(ExceptionInfo *exception)
541 if ((coder_list == (SplayTreeInfo *) NULL) &&
542 (instantiate_coder == MagickFalse))
544 if (coder_semaphore == (SemaphoreInfo *) NULL)
545 AcquireSemaphoreInfo(&coder_semaphore);
546 LockSemaphoreInfo(coder_semaphore);
547 if ((coder_list == (SplayTreeInfo *) NULL) &&
548 (instantiate_coder == MagickFalse))
550 (void) LoadCoderLists(MagickCoderFilename,exception);
551 instantiate_coder=MagickTrue;
553 UnlockSemaphoreInfo(coder_semaphore);
555 return(coder_list != (SplayTreeInfo *) NULL ? MagickTrue : MagickFalse);
559 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
563 % L i s t C o d e r I n f o %
567 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
569 % ListCoderInfo() lists the coder info to a file.
571 % The format of the ListCoderInfo coder is:
573 % MagickBooleanType ListCoderInfo(FILE *file,ExceptionInfo *exception)
575 % A description of each parameter follows.
577 % o file: An pointer to a FILE.
579 % o exception: return any errors or warnings in this structure.
582 MagickExport MagickBooleanType ListCoderInfo(FILE *file,
583 ExceptionInfo *exception)
600 if (file == (const FILE *) NULL)
602 coder_info=GetCoderInfoList("*",&number_coders,exception);
603 if (coder_info == (const CoderInfo **) NULL)
605 path=(const char *) NULL;
606 for (i=0; i < (ssize_t) number_coders; i++)
608 if (coder_info[i]->stealth != MagickFalse)
610 if ((path == (const char *) NULL) ||
611 (LocaleCompare(path,coder_info[i]->path) != 0))
613 if (coder_info[i]->path != (char *) NULL)
614 (void) FormatLocaleFile(file,"\nPath: %s\n\n",coder_info[i]->path);
615 (void) FormatLocaleFile(file,"Magick Coder\n");
616 (void) FormatLocaleFile(file,
617 "-------------------------------------------------"
618 "------------------------------\n");
620 path=coder_info[i]->path;
621 (void) FormatLocaleFile(file,"%s",coder_info[i]->magick);
622 for (j=(ssize_t) strlen(coder_info[i]->magick); j <= 11; j++)
623 (void) FormatLocaleFile(file," ");
624 if (coder_info[i]->name != (char *) NULL)
625 (void) FormatLocaleFile(file,"%s",coder_info[i]->name);
626 (void) FormatLocaleFile(file,"\n");
628 coder_info=(const CoderInfo **) RelinquishMagickMemory((void *) coder_info);
634 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
638 + L o a d C o d e r L i s t %
642 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
644 % LoadCoderList() loads the coder configuration file which provides a
645 % mapping between coder attributes and a coder name.
647 % The format of the LoadCoderList coder is:
649 % MagickBooleanType LoadCoderList(const char *xml,const char *filename,
650 % const size_t depth,ExceptionInfo *exception)
652 % A description of each parameter follows:
654 % o xml: The coder list in XML format.
656 % o filename: The coder list filename.
658 % o depth: depth of <include /> statements.
660 % o exception: return any errors or warnings in this structure.
664 static void *DestroyCoderNode(void *coder_info)
669 p=(CoderInfo *) coder_info;
670 if (p->exempt == MagickFalse)
672 if (p->path != (char *) NULL)
673 p->path=DestroyString(p->path);
674 if (p->name != (char *) NULL)
675 p->name=DestroyString(p->name);
676 if (p->magick != (char *) NULL)
677 p->magick=DestroyString(p->magick);
679 return(RelinquishMagickMemory(p));
682 static MagickBooleanType LoadCoderList(const char *xml,const char *filename,
683 const size_t depth,ExceptionInfo *exception)
686 keyword[MaxTextExtent],
699 Load the coder map file.
701 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
702 "Loading coder configuration file \"%s\" ...",filename);
703 if (xml == (const char *) NULL)
705 if (coder_list == (SplayTreeInfo *) NULL)
707 coder_list=NewSplayTree(CompareSplayTreeString,RelinquishMagickMemory,
709 if (coder_list == (SplayTreeInfo *) NULL)
711 ThrowFileException(exception,ResourceLimitError,
712 "MemoryAllocationFailed",filename);
717 coder_info=(CoderInfo *) NULL;
718 token=AcquireString(xml);
719 for (q=(char *) xml; *q != '\0'; )
724 GetMagickToken(q,&q,token);
727 (void) CopyMagickString(keyword,token,MaxTextExtent);
728 if (LocaleNCompare(keyword,"<!DOCTYPE",9) == 0)
733 while ((LocaleNCompare(q,"]>",2) != 0) && (*q != '\0'))
734 GetMagickToken(q,&q,token);
737 if (LocaleNCompare(keyword,"<!--",4) == 0)
742 while ((LocaleNCompare(q,"->",2) != 0) && (*q != '\0'))
743 GetMagickToken(q,&q,token);
746 if (LocaleCompare(keyword,"<include") == 0)
751 while (((*token != '/') && (*(token+1) != '>')) && (*q != '\0'))
753 (void) CopyMagickString(keyword,token,MaxTextExtent);
754 GetMagickToken(q,&q,token);
757 GetMagickToken(q,&q,token);
758 if (LocaleCompare(keyword,"file") == 0)
761 (void) ThrowMagickException(exception,GetMagickModule(),
762 ConfigureError,"IncludeNodeNestedTooDeeply","`%s'",token);
769 GetPathComponent(filename,HeadPath,path);
771 (void) ConcatenateMagickString(path,DirectorySeparator,
773 if (*token == *DirectorySeparator)
774 (void) CopyMagickString(path,token,MaxTextExtent);
776 (void) ConcatenateMagickString(path,token,MaxTextExtent);
777 xml=FileToString(path,~0UL,exception);
778 if (xml != (char *) NULL)
780 status=LoadCoderList(xml,path,depth+1,exception);
781 xml=(char *) RelinquishMagickMemory(xml);
788 if (LocaleCompare(keyword,"<coder") == 0)
793 coder_info=(CoderInfo *) AcquireMagickMemory(sizeof(*coder_info));
794 if (coder_info == (CoderInfo *) NULL)
795 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
796 (void) ResetMagickMemory(coder_info,0,sizeof(*coder_info));
797 coder_info->path=ConstantString(filename);
798 coder_info->exempt=MagickFalse;
799 coder_info->signature=MagickSignature;
802 if (coder_info == (CoderInfo *) NULL)
804 if (LocaleCompare(keyword,"/>") == 0)
806 status=AddValueToSplayTree(coder_list,ConstantString(
807 coder_info->magick),coder_info);
808 if (status == MagickFalse)
809 (void) ThrowMagickException(exception,GetMagickModule(),
810 ResourceLimitError,"MemoryAllocationFailed","`%s'",
812 coder_info=(CoderInfo *) NULL;
815 GetMagickToken(q,(const char **) NULL,token);
818 GetMagickToken(q,&q,token);
819 GetMagickToken(q,&q,token);
825 if (LocaleCompare((char *) keyword,"magick") == 0)
827 coder_info->magick=ConstantString(token);
835 if (LocaleCompare((char *) keyword,"name") == 0)
837 coder_info->name=ConstantString(token);
845 if (LocaleCompare((char *) keyword,"stealth") == 0)
847 coder_info->stealth=IsStringTrue(token);
856 token=(char *) RelinquishMagickMemory(token);
861 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
865 % L o a d C o d e r L i s t s %
869 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
871 % LoadCoderLists() loads one or more coder configuration file which
872 % provides a mapping between coder attributes and a coder name.
874 % The format of the LoadCoderLists coder is:
876 % MagickBooleanType LoadCoderLists(const char *filename,
877 % ExceptionInfo *exception)
879 % A description of each parameter follows:
881 % o filename: the font file name.
883 % o exception: return any errors or warnings in this structure.
886 static MagickBooleanType LoadCoderLists(const char *filename,
887 ExceptionInfo *exception)
902 Load external coder map.
904 if (coder_list == (SplayTreeInfo *) NULL)
906 coder_list=NewSplayTree(CompareSplayTreeString,RelinquishMagickMemory,
908 if (coder_list == (SplayTreeInfo *) NULL)
910 ThrowFileException(exception,ResourceLimitError,
911 "MemoryAllocationFailed",filename);
916 options=GetConfigureOptions(filename,exception);
917 option=(const StringInfo *) GetNextValueInLinkedList(options);
918 while (option != (const StringInfo *) NULL)
920 status&=LoadCoderList((const char *) GetStringInfoDatum(option),
921 GetStringInfoPath(option),0,exception);
922 option=(const StringInfo *) GetNextValueInLinkedList(options);
924 options=DestroyConfigureOptions(options);
926 Load built-in coder map.
928 for (i=0; i < (ssize_t) (sizeof(CoderMap)/sizeof(*CoderMap)); i++)
933 register const CoderMapInfo
937 coder_info=(CoderInfo *) AcquireMagickMemory(sizeof(*coder_info));
938 if (coder_info == (CoderInfo *) NULL)
940 (void) ThrowMagickException(exception,GetMagickModule(),
941 ResourceLimitError,"MemoryAllocationFailed","`%s'",p->name);
944 (void) ResetMagickMemory(coder_info,0,sizeof(*coder_info));
945 coder_info->path=(char *) "[built-in]";
946 coder_info->magick=(char *) p->magick;
947 coder_info->name=(char *) p->name;
948 coder_info->exempt=MagickTrue;
949 coder_info->signature=MagickSignature;
950 status&=AddValueToSplayTree(coder_list,ConstantString(coder_info->magick),
952 if (status == MagickFalse)
953 (void) ThrowMagickException(exception,GetMagickModule(),
954 ResourceLimitError,"MemoryAllocationFailed","`%s'",coder_info->name);
956 return(status != 0 ? MagickTrue : MagickFalse);