2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6 % CCCC OOO DDDD EEEEE RRRR %
10 % CCCC OOO DDDD EEEEE R R %
13 % MagickCore Image Coder Methods %
20 % Copyright 1999-2012 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" },
152 { "LOGO", "MAGICK" },
164 { "NETSCAPE", "MAGICK" },
190 { "RADIAL-GRADIENT", "GRADIENT" },
196 { "ROSE", "MAGICK" },
202 { "TIFF64", "TIFF" },
205 { "UBRL", "BRAILLE" },
208 { "WIZARD", "MAGICK" },
210 { "WMFWIN32", "EMF" },
214 { "XTRNARRAY", "XTRN" },
215 { "XTRNBLOB", "XTRN" },
216 { "XTRNFILE", "XTRN" },
217 { "XTRNIMAGE", "XTRN" },
220 { "YCbCrA", "YCbCr" }
224 *coder_semaphore = (SemaphoreInfo *) NULL;
227 *coder_list = (SplayTreeInfo *) NULL;
229 static volatile MagickBooleanType
230 instantiate_coder = MagickFalse;
233 Forward declarations.
235 static MagickBooleanType
236 InitializeCoderList(ExceptionInfo *),
237 LoadCoderLists(const char *,ExceptionInfo *);
240 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
244 + C o d e r C o m p o n e n t G e n e s i s %
248 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
250 % CoderComponentGenesis() instantiates the coder component.
252 % The format of the CoderComponentGenesis method is:
254 % MagickBooleanType CoderComponentGenesis(void)
257 MagickPrivate MagickBooleanType CoderComponentGenesis(void)
259 AcquireSemaphoreInfo(&coder_semaphore);
264 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
268 + C o d e r C o m p o n e n t T e r m i n u s %
272 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
274 % CoderComponentTerminus() destroys the coder component.
276 % The format of the CoderComponentTerminus method is:
278 % CoderComponentTerminus(void)
281 MagickPrivate void CoderComponentTerminus(void)
283 if (coder_semaphore == (SemaphoreInfo *) NULL)
284 AcquireSemaphoreInfo(&coder_semaphore);
285 LockSemaphoreInfo(coder_semaphore);
286 if (coder_list != (SplayTreeInfo *) NULL)
287 coder_list=DestroySplayTree(coder_list);
288 instantiate_coder=MagickFalse;
289 UnlockSemaphoreInfo(coder_semaphore);
290 DestroySemaphoreInfo(&coder_semaphore);
294 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
298 + G e t C o d e r I n f o %
302 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
304 % GetCoderInfo searches the coder list for the specified name and if found
305 % returns attributes for that coder.
307 % The format of the GetCoderInfo method is:
309 % const CoderInfo *GetCoderInfo(const char *name,ExceptionInfo *exception)
311 % A description of each parameter follows:
313 % o name: the coder name.
315 % o exception: return any errors or warnings in this structure.
318 MagickExport const CoderInfo *GetCoderInfo(const char *name,
319 ExceptionInfo *exception)
321 assert(exception != (ExceptionInfo *) NULL);
322 if ((coder_list == (SplayTreeInfo *) NULL) ||
323 (instantiate_coder == MagickFalse))
324 if (InitializeCoderList(exception) == MagickFalse)
325 return((const CoderInfo *) NULL);
326 if ((coder_list == (SplayTreeInfo *) NULL) ||
327 (GetNumberOfNodesInSplayTree(coder_list) == 0))
328 return((const CoderInfo *) NULL);
329 if ((name == (const char *) NULL) || (LocaleCompare(name,"*") == 0))
331 ResetSplayTreeIterator(coder_list);
332 return((const CoderInfo *) GetNextValueInSplayTree(coder_list));
334 return((const CoderInfo *) GetValueFromSplayTree(coder_list,name));
338 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
342 % G e t C o d e r I n f o L i s t %
346 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
348 % GetCoderInfoList() returns any coder_map that match the specified pattern.
349 % The format of the GetCoderInfoList function is:
351 % const CoderInfo **GetCoderInfoList(const char *pattern,
352 % size_t *number_coders,ExceptionInfo *exception)
354 % A description of each parameter follows:
356 % o pattern: Specifies a pointer to a text string containing a pattern.
358 % o number_coders: This integer returns the number of coders in the list.
360 % o exception: return any errors or warnings in this structure.
364 static int CoderInfoCompare(const void *x,const void *y)
370 p=(const CoderInfo **) x,
371 q=(const CoderInfo **) y;
372 if (LocaleCompare((*p)->path,(*q)->path) == 0)
373 return(LocaleCompare((*p)->name,(*q)->name));
374 return(LocaleCompare((*p)->path,(*q)->path));
377 MagickExport const CoderInfo **GetCoderInfoList(const char *pattern,
378 size_t *number_coders,ExceptionInfo *exception)
383 register const CoderInfo
392 assert(pattern != (char *) NULL);
393 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
394 assert(number_coders != (size_t *) NULL);
396 p=GetCoderInfo("*",exception);
397 if (p == (const CoderInfo *) NULL)
398 return((const CoderInfo **) NULL);
399 coder_map=(const CoderInfo **) AcquireQuantumMemory((size_t)
400 GetNumberOfNodesInSplayTree(coder_list)+1UL,sizeof(*coder_map));
401 if (coder_map == (const CoderInfo **) NULL)
402 return((const CoderInfo **) NULL);
406 LockSemaphoreInfo(coder_semaphore);
407 ResetSplayTreeIterator(coder_list);
408 p=(const CoderInfo *) GetNextValueInSplayTree(coder_list);
409 for (i=0; p != (const CoderInfo *) NULL; )
411 if ((p->stealth == MagickFalse) &&
412 (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
414 p=(const CoderInfo *) GetNextValueInSplayTree(coder_list);
416 UnlockSemaphoreInfo(coder_semaphore);
417 qsort((void *) coder_map,(size_t) i,sizeof(*coder_map),CoderInfoCompare);
418 coder_map[i]=(CoderInfo *) NULL;
419 *number_coders=(size_t) i;
424 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
428 % G e t C o d e r L i s t %
432 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
434 % GetCoderList() returns any coder_map that match the specified pattern.
436 % The format of the GetCoderList function is:
438 % char **GetCoderList(const char *pattern,size_t *number_coders,
439 % ExceptionInfo *exception)
441 % A description of each parameter follows:
443 % o pattern: Specifies a pointer to a text string containing a pattern.
445 % o number_coders: This integer returns the number of coders in the list.
447 % o exception: return any errors or warnings in this structure.
451 static int CoderCompare(const void *x,const void *y)
459 return(LocaleCompare(*p,*q));
462 MagickExport char **GetCoderList(const char *pattern,
463 size_t *number_coders,ExceptionInfo *exception)
468 register const CoderInfo
477 assert(pattern != (char *) NULL);
478 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
479 assert(number_coders != (size_t *) NULL);
481 p=GetCoderInfo("*",exception);
482 if (p == (const CoderInfo *) NULL)
483 return((char **) NULL);
484 coder_map=(char **) AcquireQuantumMemory((size_t)
485 GetNumberOfNodesInSplayTree(coder_list)+1UL,sizeof(*coder_map));
486 if (coder_map == (char **) NULL)
487 return((char **) NULL);
491 LockSemaphoreInfo(coder_semaphore);
492 ResetSplayTreeIterator(coder_list);
493 p=(const CoderInfo *) GetNextValueInSplayTree(coder_list);
494 for (i=0; p != (const CoderInfo *) NULL; )
496 if ((p->stealth == MagickFalse) &&
497 (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
498 coder_map[i++]=ConstantString(p->name);
499 p=(const CoderInfo *) GetNextValueInSplayTree(coder_list);
501 UnlockSemaphoreInfo(coder_semaphore);
502 qsort((void *) coder_map,(size_t) i,sizeof(*coder_map),CoderCompare);
503 coder_map[i]=(char *) NULL;
504 *number_coders=(size_t) i;
509 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
513 + I n i t i a l i z e C o d e r L i s t %
517 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
519 % InitializeCoderList() initializes the coder list.
521 % The format of the InitializeCoderList method is:
523 % MagickBooleanType InitializeCoderList(ExceptionInfo *exception)
525 % A description of each parameter follows.
527 % o exception: return any errors or warnings in this structure.
530 static MagickBooleanType InitializeCoderList(ExceptionInfo *exception)
532 if ((coder_list == (SplayTreeInfo *) NULL) &&
533 (instantiate_coder == MagickFalse))
535 if (coder_semaphore == (SemaphoreInfo *) NULL)
536 AcquireSemaphoreInfo(&coder_semaphore);
537 LockSemaphoreInfo(coder_semaphore);
538 if ((coder_list == (SplayTreeInfo *) NULL) &&
539 (instantiate_coder == MagickFalse))
541 (void) LoadCoderLists(MagickCoderFilename,exception);
542 instantiate_coder=MagickTrue;
544 UnlockSemaphoreInfo(coder_semaphore);
546 return(coder_list != (SplayTreeInfo *) NULL ? MagickTrue : MagickFalse);
550 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
554 % L i s t C o d e r I n f o %
558 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
560 % ListCoderInfo() lists the coder info to a file.
562 % The format of the ListCoderInfo coder is:
564 % MagickBooleanType ListCoderInfo(FILE *file,ExceptionInfo *exception)
566 % A description of each parameter follows.
568 % o file: An pointer to a FILE.
570 % o exception: return any errors or warnings in this structure.
573 MagickExport MagickBooleanType ListCoderInfo(FILE *file,
574 ExceptionInfo *exception)
591 if (file == (const FILE *) NULL)
593 coder_info=GetCoderInfoList("*",&number_coders,exception);
594 if (coder_info == (const CoderInfo **) NULL)
596 path=(const char *) NULL;
597 for (i=0; i < (ssize_t) number_coders; i++)
599 if (coder_info[i]->stealth != MagickFalse)
601 if ((path == (const char *) NULL) ||
602 (LocaleCompare(path,coder_info[i]->path) != 0))
604 if (coder_info[i]->path != (char *) NULL)
605 (void) FormatLocaleFile(file,"\nPath: %s\n\n",coder_info[i]->path);
606 (void) FormatLocaleFile(file,"Magick Coder\n");
607 (void) FormatLocaleFile(file,
608 "-------------------------------------------------"
609 "------------------------------\n");
611 path=coder_info[i]->path;
612 (void) FormatLocaleFile(file,"%s",coder_info[i]->magick);
613 for (j=(ssize_t) strlen(coder_info[i]->magick); j <= 11; j++)
614 (void) FormatLocaleFile(file," ");
615 if (coder_info[i]->name != (char *) NULL)
616 (void) FormatLocaleFile(file,"%s",coder_info[i]->name);
617 (void) FormatLocaleFile(file,"\n");
619 coder_info=(const CoderInfo **) RelinquishMagickMemory((void *) coder_info);
625 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
629 + L o a d C o d e r L i s t %
633 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
635 % LoadCoderList() loads the coder configuration file which provides a
636 % mapping between coder attributes and a coder name.
638 % The format of the LoadCoderList coder is:
640 % MagickBooleanType LoadCoderList(const char *xml,const char *filename,
641 % const size_t depth,ExceptionInfo *exception)
643 % A description of each parameter follows:
645 % o xml: The coder list in XML format.
647 % o filename: The coder list filename.
649 % o depth: depth of <include /> statements.
651 % o exception: return any errors or warnings in this structure.
655 static void *DestroyCoderNode(void *coder_info)
660 p=(CoderInfo *) coder_info;
661 if (p->exempt == MagickFalse)
663 if (p->path != (char *) NULL)
664 p->path=DestroyString(p->path);
665 if (p->name != (char *) NULL)
666 p->name=DestroyString(p->name);
667 if (p->magick != (char *) NULL)
668 p->magick=DestroyString(p->magick);
670 return(RelinquishMagickMemory(p));
673 static MagickBooleanType LoadCoderList(const char *xml,const char *filename,
674 const size_t depth,ExceptionInfo *exception)
677 keyword[MaxTextExtent],
690 Load the coder map file.
692 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
693 "Loading coder configuration file \"%s\" ...",filename);
694 if (xml == (const char *) NULL)
696 if (coder_list == (SplayTreeInfo *) NULL)
698 coder_list=NewSplayTree(CompareSplayTreeString,RelinquishMagickMemory,
700 if (coder_list == (SplayTreeInfo *) NULL)
702 ThrowFileException(exception,ResourceLimitError,
703 "MemoryAllocationFailed",filename);
708 coder_info=(CoderInfo *) NULL;
709 token=AcquireString(xml);
710 for (q=(char *) xml; *q != '\0'; )
715 GetMagickToken(q,&q,token);
718 (void) CopyMagickString(keyword,token,MaxTextExtent);
719 if (LocaleNCompare(keyword,"<!DOCTYPE",9) == 0)
724 while ((LocaleNCompare(q,"]>",2) != 0) && (*q != '\0'))
725 GetMagickToken(q,&q,token);
728 if (LocaleNCompare(keyword,"<!--",4) == 0)
733 while ((LocaleNCompare(q,"->",2) != 0) && (*q != '\0'))
734 GetMagickToken(q,&q,token);
737 if (LocaleCompare(keyword,"<include") == 0)
742 while (((*token != '/') && (*(token+1) != '>')) && (*q != '\0'))
744 (void) CopyMagickString(keyword,token,MaxTextExtent);
745 GetMagickToken(q,&q,token);
748 GetMagickToken(q,&q,token);
749 if (LocaleCompare(keyword,"file") == 0)
752 (void) ThrowMagickException(exception,GetMagickModule(),
753 ConfigureError,"IncludeNodeNestedTooDeeply","`%s'",token);
760 GetPathComponent(filename,HeadPath,path);
762 (void) ConcatenateMagickString(path,DirectorySeparator,
764 if (*token == *DirectorySeparator)
765 (void) CopyMagickString(path,token,MaxTextExtent);
767 (void) ConcatenateMagickString(path,token,MaxTextExtent);
768 xml=FileToString(path,~0,exception);
769 if (xml != (char *) NULL)
771 status=LoadCoderList(xml,path,depth+1,exception);
772 xml=(char *) RelinquishMagickMemory(xml);
779 if (LocaleCompare(keyword,"<coder") == 0)
784 coder_info=(CoderInfo *) AcquireMagickMemory(sizeof(*coder_info));
785 if (coder_info == (CoderInfo *) NULL)
786 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
787 (void) ResetMagickMemory(coder_info,0,sizeof(*coder_info));
788 coder_info->path=ConstantString(filename);
789 coder_info->exempt=MagickFalse;
790 coder_info->signature=MagickSignature;
793 if (coder_info == (CoderInfo *) NULL)
795 if (LocaleCompare(keyword,"/>") == 0)
797 status=AddValueToSplayTree(coder_list,ConstantString(
798 coder_info->magick),coder_info);
799 if (status == MagickFalse)
800 (void) ThrowMagickException(exception,GetMagickModule(),
801 ResourceLimitError,"MemoryAllocationFailed","`%s'",
803 coder_info=(CoderInfo *) NULL;
805 GetMagickToken(q,(const char **) NULL,token);
808 GetMagickToken(q,&q,token);
809 GetMagickToken(q,&q,token);
815 if (LocaleCompare((char *) keyword,"magick") == 0)
817 coder_info->magick=ConstantString(token);
825 if (LocaleCompare((char *) keyword,"name") == 0)
827 coder_info->name=ConstantString(token);
835 if (LocaleCompare((char *) keyword,"stealth") == 0)
837 coder_info->stealth=IsMagickTrue(token);
846 token=(char *) RelinquishMagickMemory(token);
851 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
855 % L o a d C o d e r L i s t s %
859 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
861 % LoadCoderLists() loads one or more coder configuration file which
862 % provides a mapping between coder attributes and a coder name.
864 % The format of the LoadCoderLists coder is:
866 % MagickBooleanType LoadCoderLists(const char *filename,
867 % ExceptionInfo *exception)
869 % A description of each parameter follows:
871 % o filename: the font file name.
873 % o exception: return any errors or warnings in this structure.
876 static MagickBooleanType LoadCoderLists(const char *filename,
877 ExceptionInfo *exception)
892 Load built-in coder map.
895 if (coder_list == (SplayTreeInfo *) NULL)
897 coder_list=NewSplayTree(CompareSplayTreeString,RelinquishMagickMemory,
899 if (coder_list == (SplayTreeInfo *) NULL)
901 ThrowFileException(exception,ResourceLimitError,
902 "MemoryAllocationFailed",filename);
906 for (i=0; i < (ssize_t) (sizeof(CoderMap)/sizeof(*CoderMap)); i++)
911 register const CoderMapInfo
915 coder_info=(CoderInfo *) AcquireMagickMemory(sizeof(*coder_info));
916 if (coder_info == (CoderInfo *) NULL)
918 (void) ThrowMagickException(exception,GetMagickModule(),
919 ResourceLimitError,"MemoryAllocationFailed","`%s'",coder_info->name);
922 (void) ResetMagickMemory(coder_info,0,sizeof(*coder_info));
923 coder_info->path=(char *) "[built-in]";
924 coder_info->magick=(char *) p->magick;
925 coder_info->name=(char *) p->name;
926 coder_info->exempt=MagickTrue;
927 coder_info->signature=MagickSignature;
928 status=AddValueToSplayTree(coder_list,ConstantString(coder_info->magick),
930 if (status == MagickFalse)
931 (void) ThrowMagickException(exception,GetMagickModule(),
932 ResourceLimitError,"MemoryAllocationFailed","`%s'",coder_info->name);
935 Load external coder map.
937 options=GetConfigureOptions(filename,exception);
938 option=(const StringInfo *) GetNextValueInLinkedList(options);
939 while (option != (const StringInfo *) NULL)
941 status|=LoadCoderList((const char *) GetStringInfoDatum(option),
942 GetStringInfoPath(option),0,exception);
943 option=(const StringInfo *) GetNextValueInLinkedList(options);
945 options=DestroyConfigureOptions(options);
946 return(status != 0 ? MagickTrue : MagickFalse);