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" },
217 { "WMFWIN32", "EMF" },
221 { "XTRNARRAY", "XTRN" },
222 { "XTRNBLOB", "XTRN" },
223 { "XTRNFILE", "XTRN" },
224 { "XTRNIMAGE", "XTRN" },
227 { "YCbCrA", "YCbCr" }
231 *coder_semaphore = (SemaphoreInfo *) NULL;
234 *coder_list = (SplayTreeInfo *) NULL;
236 static volatile MagickBooleanType
237 instantiate_coder = MagickFalse;
240 Forward declarations.
242 static MagickBooleanType
243 InitializeCoderList(ExceptionInfo *),
244 LoadCoderLists(const char *,ExceptionInfo *);
247 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
251 + C o d e r C o m p o n e n t G e n e s i s %
255 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
257 % CoderComponentGenesis() instantiates the coder component.
259 % The format of the CoderComponentGenesis method is:
261 % MagickBooleanType CoderComponentGenesis(void)
264 MagickPrivate MagickBooleanType CoderComponentGenesis(void)
266 AcquireSemaphoreInfo(&coder_semaphore);
271 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
275 + C o d e r C o m p o n e n t T e r m i n u s %
279 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
281 % CoderComponentTerminus() destroys the coder component.
283 % The format of the CoderComponentTerminus method is:
285 % CoderComponentTerminus(void)
288 MagickPrivate void CoderComponentTerminus(void)
290 if (coder_semaphore == (SemaphoreInfo *) NULL)
291 AcquireSemaphoreInfo(&coder_semaphore);
292 LockSemaphoreInfo(coder_semaphore);
293 if (coder_list != (SplayTreeInfo *) NULL)
294 coder_list=DestroySplayTree(coder_list);
295 instantiate_coder=MagickFalse;
296 UnlockSemaphoreInfo(coder_semaphore);
297 DestroySemaphoreInfo(&coder_semaphore);
301 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
305 + G e t C o d e r I n f o %
309 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
311 % GetCoderInfo searches the coder list for the specified name and if found
312 % returns attributes for that coder.
314 % The format of the GetCoderInfo method is:
316 % const CoderInfo *GetCoderInfo(const char *name,ExceptionInfo *exception)
318 % A description of each parameter follows:
320 % o name: the coder name.
322 % o exception: return any errors or warnings in this structure.
325 MagickExport const CoderInfo *GetCoderInfo(const char *name,
326 ExceptionInfo *exception)
328 assert(exception != (ExceptionInfo *) NULL);
329 if ((coder_list == (SplayTreeInfo *) NULL) ||
330 (instantiate_coder == MagickFalse))
331 if (InitializeCoderList(exception) == MagickFalse)
332 return((const CoderInfo *) NULL);
333 if ((coder_list == (SplayTreeInfo *) NULL) ||
334 (GetNumberOfNodesInSplayTree(coder_list) == 0))
335 return((const CoderInfo *) NULL);
336 if ((name == (const char *) NULL) || (LocaleCompare(name,"*") == 0))
338 ResetSplayTreeIterator(coder_list);
339 return((const CoderInfo *) GetNextValueInSplayTree(coder_list));
341 return((const CoderInfo *) GetValueFromSplayTree(coder_list,name));
345 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
349 % G e t C o d e r I n f o L i s t %
353 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
355 % GetCoderInfoList() returns any coder_map that match the specified pattern.
356 % The format of the GetCoderInfoList function is:
358 % const CoderInfo **GetCoderInfoList(const char *pattern,
359 % size_t *number_coders,ExceptionInfo *exception)
361 % A description of each parameter follows:
363 % o pattern: Specifies a pointer to a text string containing a pattern.
365 % o number_coders: This integer returns the number of coders in the list.
367 % o exception: return any errors or warnings in this structure.
371 static int CoderInfoCompare(const void *x,const void *y)
377 p=(const CoderInfo **) x,
378 q=(const CoderInfo **) y;
379 if (LocaleCompare((*p)->path,(*q)->path) == 0)
380 return(LocaleCompare((*p)->name,(*q)->name));
381 return(LocaleCompare((*p)->path,(*q)->path));
384 MagickExport const CoderInfo **GetCoderInfoList(const char *pattern,
385 size_t *number_coders,ExceptionInfo *exception)
390 register const CoderInfo
399 assert(pattern != (char *) NULL);
400 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
401 assert(number_coders != (size_t *) NULL);
403 p=GetCoderInfo("*",exception);
404 if (p == (const CoderInfo *) NULL)
405 return((const CoderInfo **) NULL);
406 coder_map=(const CoderInfo **) AcquireQuantumMemory((size_t)
407 GetNumberOfNodesInSplayTree(coder_list)+1UL,sizeof(*coder_map));
408 if (coder_map == (const CoderInfo **) NULL)
409 return((const CoderInfo **) NULL);
413 LockSemaphoreInfo(coder_semaphore);
414 ResetSplayTreeIterator(coder_list);
415 p=(const CoderInfo *) GetNextValueInSplayTree(coder_list);
416 for (i=0; p != (const CoderInfo *) NULL; )
418 if ((p->stealth == MagickFalse) &&
419 (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
421 p=(const CoderInfo *) GetNextValueInSplayTree(coder_list);
423 UnlockSemaphoreInfo(coder_semaphore);
424 qsort((void *) coder_map,(size_t) i,sizeof(*coder_map),CoderInfoCompare);
425 coder_map[i]=(CoderInfo *) NULL;
426 *number_coders=(size_t) i;
431 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
435 % G e t C o d e r L i s t %
439 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
441 % GetCoderList() returns any coder_map that match the specified pattern.
443 % The format of the GetCoderList function is:
445 % char **GetCoderList(const char *pattern,size_t *number_coders,
446 % ExceptionInfo *exception)
448 % A description of each parameter follows:
450 % o pattern: Specifies a pointer to a text string containing a pattern.
452 % o number_coders: This integer returns the number of coders in the list.
454 % o exception: return any errors or warnings in this structure.
458 static int CoderCompare(const void *x,const void *y)
466 return(LocaleCompare(*p,*q));
469 MagickExport char **GetCoderList(const char *pattern,
470 size_t *number_coders,ExceptionInfo *exception)
475 register const CoderInfo
484 assert(pattern != (char *) NULL);
485 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
486 assert(number_coders != (size_t *) NULL);
488 p=GetCoderInfo("*",exception);
489 if (p == (const CoderInfo *) NULL)
490 return((char **) NULL);
491 coder_map=(char **) AcquireQuantumMemory((size_t)
492 GetNumberOfNodesInSplayTree(coder_list)+1UL,sizeof(*coder_map));
493 if (coder_map == (char **) NULL)
494 return((char **) NULL);
498 LockSemaphoreInfo(coder_semaphore);
499 ResetSplayTreeIterator(coder_list);
500 p=(const CoderInfo *) GetNextValueInSplayTree(coder_list);
501 for (i=0; p != (const CoderInfo *) NULL; )
503 if ((p->stealth == MagickFalse) &&
504 (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
505 coder_map[i++]=ConstantString(p->name);
506 p=(const CoderInfo *) GetNextValueInSplayTree(coder_list);
508 UnlockSemaphoreInfo(coder_semaphore);
509 qsort((void *) coder_map,(size_t) i,sizeof(*coder_map),CoderCompare);
510 coder_map[i]=(char *) NULL;
511 *number_coders=(size_t) i;
516 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
520 + I n i t i a l i z e C o d e r L i s t %
524 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
526 % InitializeCoderList() initializes the coder list.
528 % The format of the InitializeCoderList method is:
530 % MagickBooleanType InitializeCoderList(ExceptionInfo *exception)
532 % A description of each parameter follows.
534 % o exception: return any errors or warnings in this structure.
537 static MagickBooleanType InitializeCoderList(ExceptionInfo *exception)
539 if ((coder_list == (SplayTreeInfo *) NULL) &&
540 (instantiate_coder == MagickFalse))
542 if (coder_semaphore == (SemaphoreInfo *) NULL)
543 AcquireSemaphoreInfo(&coder_semaphore);
544 LockSemaphoreInfo(coder_semaphore);
545 if ((coder_list == (SplayTreeInfo *) NULL) &&
546 (instantiate_coder == MagickFalse))
548 (void) LoadCoderLists(MagickCoderFilename,exception);
549 instantiate_coder=MagickTrue;
551 UnlockSemaphoreInfo(coder_semaphore);
553 return(coder_list != (SplayTreeInfo *) NULL ? MagickTrue : MagickFalse);
557 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
561 % L i s t C o d e r I n f o %
565 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
567 % ListCoderInfo() lists the coder info to a file.
569 % The format of the ListCoderInfo coder is:
571 % MagickBooleanType ListCoderInfo(FILE *file,ExceptionInfo *exception)
573 % A description of each parameter follows.
575 % o file: An pointer to a FILE.
577 % o exception: return any errors or warnings in this structure.
580 MagickExport MagickBooleanType ListCoderInfo(FILE *file,
581 ExceptionInfo *exception)
598 if (file == (const FILE *) NULL)
600 coder_info=GetCoderInfoList("*",&number_coders,exception);
601 if (coder_info == (const CoderInfo **) NULL)
603 path=(const char *) NULL;
604 for (i=0; i < (ssize_t) number_coders; i++)
606 if (coder_info[i]->stealth != MagickFalse)
608 if ((path == (const char *) NULL) ||
609 (LocaleCompare(path,coder_info[i]->path) != 0))
611 if (coder_info[i]->path != (char *) NULL)
612 (void) FormatLocaleFile(file,"\nPath: %s\n\n",coder_info[i]->path);
613 (void) FormatLocaleFile(file,"Magick Coder\n");
614 (void) FormatLocaleFile(file,
615 "-------------------------------------------------"
616 "------------------------------\n");
618 path=coder_info[i]->path;
619 (void) FormatLocaleFile(file,"%s",coder_info[i]->magick);
620 for (j=(ssize_t) strlen(coder_info[i]->magick); j <= 11; j++)
621 (void) FormatLocaleFile(file," ");
622 if (coder_info[i]->name != (char *) NULL)
623 (void) FormatLocaleFile(file,"%s",coder_info[i]->name);
624 (void) FormatLocaleFile(file,"\n");
626 coder_info=(const CoderInfo **) RelinquishMagickMemory((void *) coder_info);
632 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
636 + L o a d C o d e r L i s t %
640 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
642 % LoadCoderList() loads the coder configuration file which provides a
643 % mapping between coder attributes and a coder name.
645 % The format of the LoadCoderList coder is:
647 % MagickBooleanType LoadCoderList(const char *xml,const char *filename,
648 % const size_t depth,ExceptionInfo *exception)
650 % A description of each parameter follows:
652 % o xml: The coder list in XML format.
654 % o filename: The coder list filename.
656 % o depth: depth of <include /> statements.
658 % o exception: return any errors or warnings in this structure.
662 static void *DestroyCoderNode(void *coder_info)
667 p=(CoderInfo *) coder_info;
668 if (p->exempt == MagickFalse)
670 if (p->path != (char *) NULL)
671 p->path=DestroyString(p->path);
672 if (p->name != (char *) NULL)
673 p->name=DestroyString(p->name);
674 if (p->magick != (char *) NULL)
675 p->magick=DestroyString(p->magick);
677 return(RelinquishMagickMemory(p));
680 static MagickBooleanType LoadCoderList(const char *xml,const char *filename,
681 const size_t depth,ExceptionInfo *exception)
684 keyword[MaxTextExtent],
697 Load the coder map file.
699 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
700 "Loading coder configuration file \"%s\" ...",filename);
701 if (xml == (const char *) NULL)
703 if (coder_list == (SplayTreeInfo *) NULL)
705 coder_list=NewSplayTree(CompareSplayTreeString,RelinquishMagickMemory,
707 if (coder_list == (SplayTreeInfo *) NULL)
709 ThrowFileException(exception,ResourceLimitError,
710 "MemoryAllocationFailed",filename);
715 coder_info=(CoderInfo *) NULL;
716 token=AcquireString(xml);
717 for (q=(char *) xml; *q != '\0'; )
722 GetMagickToken(q,&q,token);
725 (void) CopyMagickString(keyword,token,MaxTextExtent);
726 if (LocaleNCompare(keyword,"<!DOCTYPE",9) == 0)
731 while ((LocaleNCompare(q,"]>",2) != 0) && (*q != '\0'))
732 GetMagickToken(q,&q,token);
735 if (LocaleNCompare(keyword,"<!--",4) == 0)
740 while ((LocaleNCompare(q,"->",2) != 0) && (*q != '\0'))
741 GetMagickToken(q,&q,token);
744 if (LocaleCompare(keyword,"<include") == 0)
749 while (((*token != '/') && (*(token+1) != '>')) && (*q != '\0'))
751 (void) CopyMagickString(keyword,token,MaxTextExtent);
752 GetMagickToken(q,&q,token);
755 GetMagickToken(q,&q,token);
756 if (LocaleCompare(keyword,"file") == 0)
759 (void) ThrowMagickException(exception,GetMagickModule(),
760 ConfigureError,"IncludeNodeNestedTooDeeply","`%s'",token);
767 GetPathComponent(filename,HeadPath,path);
769 (void) ConcatenateMagickString(path,DirectorySeparator,
771 if (*token == *DirectorySeparator)
772 (void) CopyMagickString(path,token,MaxTextExtent);
774 (void) ConcatenateMagickString(path,token,MaxTextExtent);
775 xml=FileToString(path,~0UL,exception);
776 if (xml != (char *) NULL)
778 status=LoadCoderList(xml,path,depth+1,exception);
779 xml=(char *) RelinquishMagickMemory(xml);
786 if (LocaleCompare(keyword,"<coder") == 0)
791 coder_info=(CoderInfo *) AcquireMagickMemory(sizeof(*coder_info));
792 if (coder_info == (CoderInfo *) NULL)
793 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
794 (void) ResetMagickMemory(coder_info,0,sizeof(*coder_info));
795 coder_info->path=ConstantString(filename);
796 coder_info->exempt=MagickFalse;
797 coder_info->signature=MagickSignature;
800 if (coder_info == (CoderInfo *) NULL)
802 if (LocaleCompare(keyword,"/>") == 0)
804 status=AddValueToSplayTree(coder_list,ConstantString(
805 coder_info->magick),coder_info);
806 if (status == MagickFalse)
807 (void) ThrowMagickException(exception,GetMagickModule(),
808 ResourceLimitError,"MemoryAllocationFailed","`%s'",
810 coder_info=(CoderInfo *) NULL;
812 GetMagickToken(q,(const char **) NULL,token);
815 GetMagickToken(q,&q,token);
816 GetMagickToken(q,&q,token);
822 if (LocaleCompare((char *) keyword,"magick") == 0)
824 coder_info->magick=ConstantString(token);
832 if (LocaleCompare((char *) keyword,"name") == 0)
834 coder_info->name=ConstantString(token);
842 if (LocaleCompare((char *) keyword,"stealth") == 0)
844 coder_info->stealth=IsStringTrue(token);
853 token=(char *) RelinquishMagickMemory(token);
858 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
862 % L o a d C o d e r L i s t s %
866 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
868 % LoadCoderLists() loads one or more coder configuration file which
869 % provides a mapping between coder attributes and a coder name.
871 % The format of the LoadCoderLists coder is:
873 % MagickBooleanType LoadCoderLists(const char *filename,
874 % ExceptionInfo *exception)
876 % A description of each parameter follows:
878 % o filename: the font file name.
880 % o exception: return any errors or warnings in this structure.
883 static MagickBooleanType LoadCoderLists(const char *filename,
884 ExceptionInfo *exception)
899 Load external coder map.
901 if (coder_list == (SplayTreeInfo *) NULL)
903 coder_list=NewSplayTree(CompareSplayTreeString,RelinquishMagickMemory,
905 if (coder_list == (SplayTreeInfo *) NULL)
907 ThrowFileException(exception,ResourceLimitError,
908 "MemoryAllocationFailed",filename);
913 options=GetConfigureOptions(filename,exception);
914 option=(const StringInfo *) GetNextValueInLinkedList(options);
915 while (option != (const StringInfo *) NULL)
917 status&=LoadCoderList((const char *) GetStringInfoDatum(option),
918 GetStringInfoPath(option),0,exception);
919 option=(const StringInfo *) GetNextValueInLinkedList(options);
921 options=DestroyConfigureOptions(options);
923 Load built-in coder map.
925 for (i=0; i < (ssize_t) (sizeof(CoderMap)/sizeof(*CoderMap)); i++)
930 register const CoderMapInfo
934 coder_info=(CoderInfo *) AcquireMagickMemory(sizeof(*coder_info));
935 if (coder_info == (CoderInfo *) NULL)
937 (void) ThrowMagickException(exception,GetMagickModule(),
938 ResourceLimitError,"MemoryAllocationFailed","`%s'",coder_info->name);
941 (void) ResetMagickMemory(coder_info,0,sizeof(*coder_info));
942 coder_info->path=(char *) "[built-in]";
943 coder_info->magick=(char *) p->magick;
944 coder_info->name=(char *) p->name;
945 coder_info->exempt=MagickTrue;
946 coder_info->signature=MagickSignature;
947 status&=AddValueToSplayTree(coder_list,ConstantString(coder_info->magick),
949 if (status == MagickFalse)
950 (void) ThrowMagickException(exception,GetMagickModule(),
951 ResourceLimitError,"MemoryAllocationFailed","`%s'",coder_info->name);
953 return(status != 0 ? MagickTrue : MagickFalse);