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" },
165 { "NETSCAPE", "MAGICK" },
192 { "RADIAL-GRADIENT", "GRADIENT" },
198 { "ROSE", "MAGICK" },
204 { "TIFF64", "TIFF" },
207 { "UBRL", "BRAILLE" },
210 { "WIZARD", "MAGICK" },
212 { "WMFWIN32", "EMF" },
216 { "XTRNARRAY", "XTRN" },
217 { "XTRNBLOB", "XTRN" },
218 { "XTRNFILE", "XTRN" },
219 { "XTRNIMAGE", "XTRN" },
222 { "YCbCrA", "YCbCr" }
226 *coder_semaphore = (SemaphoreInfo *) NULL;
229 *coder_list = (SplayTreeInfo *) NULL;
231 static volatile MagickBooleanType
232 instantiate_coder = MagickFalse;
235 Forward declarations.
237 static MagickBooleanType
238 InitializeCoderList(ExceptionInfo *),
239 LoadCoderLists(const char *,ExceptionInfo *);
242 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
246 + C o d e r C o m p o n e n t G e n e s i s %
250 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
252 % CoderComponentGenesis() instantiates the coder component.
254 % The format of the CoderComponentGenesis method is:
256 % MagickBooleanType CoderComponentGenesis(void)
259 MagickPrivate MagickBooleanType CoderComponentGenesis(void)
261 AcquireSemaphoreInfo(&coder_semaphore);
266 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
270 + C o d e r C o m p o n e n t T e r m i n u s %
274 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
276 % CoderComponentTerminus() destroys the coder component.
278 % The format of the CoderComponentTerminus method is:
280 % CoderComponentTerminus(void)
283 MagickPrivate void CoderComponentTerminus(void)
285 if (coder_semaphore == (SemaphoreInfo *) NULL)
286 AcquireSemaphoreInfo(&coder_semaphore);
287 LockSemaphoreInfo(coder_semaphore);
288 if (coder_list != (SplayTreeInfo *) NULL)
289 coder_list=DestroySplayTree(coder_list);
290 instantiate_coder=MagickFalse;
291 UnlockSemaphoreInfo(coder_semaphore);
292 DestroySemaphoreInfo(&coder_semaphore);
296 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
300 + G e t C o d e r I n f o %
304 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
306 % GetCoderInfo searches the coder list for the specified name and if found
307 % returns attributes for that coder.
309 % The format of the GetCoderInfo method is:
311 % const CoderInfo *GetCoderInfo(const char *name,ExceptionInfo *exception)
313 % A description of each parameter follows:
315 % o name: the coder name.
317 % o exception: return any errors or warnings in this structure.
320 MagickExport const CoderInfo *GetCoderInfo(const char *name,
321 ExceptionInfo *exception)
323 assert(exception != (ExceptionInfo *) NULL);
324 if ((coder_list == (SplayTreeInfo *) NULL) ||
325 (instantiate_coder == MagickFalse))
326 if (InitializeCoderList(exception) == MagickFalse)
327 return((const CoderInfo *) NULL);
328 if ((coder_list == (SplayTreeInfo *) NULL) ||
329 (GetNumberOfNodesInSplayTree(coder_list) == 0))
330 return((const CoderInfo *) NULL);
331 if ((name == (const char *) NULL) || (LocaleCompare(name,"*") == 0))
333 ResetSplayTreeIterator(coder_list);
334 return((const CoderInfo *) GetNextValueInSplayTree(coder_list));
336 return((const CoderInfo *) GetValueFromSplayTree(coder_list,name));
340 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
344 % G e t C o d e r I n f o L i s t %
348 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
350 % GetCoderInfoList() returns any coder_map that match the specified pattern.
351 % The format of the GetCoderInfoList function is:
353 % const CoderInfo **GetCoderInfoList(const char *pattern,
354 % size_t *number_coders,ExceptionInfo *exception)
356 % A description of each parameter follows:
358 % o pattern: Specifies a pointer to a text string containing a pattern.
360 % o number_coders: This integer returns the number of coders in the list.
362 % o exception: return any errors or warnings in this structure.
366 static int CoderInfoCompare(const void *x,const void *y)
372 p=(const CoderInfo **) x,
373 q=(const CoderInfo **) y;
374 if (LocaleCompare((*p)->path,(*q)->path) == 0)
375 return(LocaleCompare((*p)->name,(*q)->name));
376 return(LocaleCompare((*p)->path,(*q)->path));
379 MagickExport const CoderInfo **GetCoderInfoList(const char *pattern,
380 size_t *number_coders,ExceptionInfo *exception)
385 register const CoderInfo
394 assert(pattern != (char *) NULL);
395 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
396 assert(number_coders != (size_t *) NULL);
398 p=GetCoderInfo("*",exception);
399 if (p == (const CoderInfo *) NULL)
400 return((const CoderInfo **) NULL);
401 coder_map=(const CoderInfo **) AcquireQuantumMemory((size_t)
402 GetNumberOfNodesInSplayTree(coder_list)+1UL,sizeof(*coder_map));
403 if (coder_map == (const CoderInfo **) NULL)
404 return((const CoderInfo **) NULL);
408 LockSemaphoreInfo(coder_semaphore);
409 ResetSplayTreeIterator(coder_list);
410 p=(const CoderInfo *) GetNextValueInSplayTree(coder_list);
411 for (i=0; p != (const CoderInfo *) NULL; )
413 if ((p->stealth == MagickFalse) &&
414 (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
416 p=(const CoderInfo *) GetNextValueInSplayTree(coder_list);
418 UnlockSemaphoreInfo(coder_semaphore);
419 qsort((void *) coder_map,(size_t) i,sizeof(*coder_map),CoderInfoCompare);
420 coder_map[i]=(CoderInfo *) NULL;
421 *number_coders=(size_t) i;
426 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
430 % G e t C o d e r L i s t %
434 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
436 % GetCoderList() returns any coder_map that match the specified pattern.
438 % The format of the GetCoderList function is:
440 % char **GetCoderList(const char *pattern,size_t *number_coders,
441 % ExceptionInfo *exception)
443 % A description of each parameter follows:
445 % o pattern: Specifies a pointer to a text string containing a pattern.
447 % o number_coders: This integer returns the number of coders in the list.
449 % o exception: return any errors or warnings in this structure.
453 static int CoderCompare(const void *x,const void *y)
461 return(LocaleCompare(*p,*q));
464 MagickExport char **GetCoderList(const char *pattern,
465 size_t *number_coders,ExceptionInfo *exception)
470 register const CoderInfo
479 assert(pattern != (char *) NULL);
480 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
481 assert(number_coders != (size_t *) NULL);
483 p=GetCoderInfo("*",exception);
484 if (p == (const CoderInfo *) NULL)
485 return((char **) NULL);
486 coder_map=(char **) AcquireQuantumMemory((size_t)
487 GetNumberOfNodesInSplayTree(coder_list)+1UL,sizeof(*coder_map));
488 if (coder_map == (char **) NULL)
489 return((char **) NULL);
493 LockSemaphoreInfo(coder_semaphore);
494 ResetSplayTreeIterator(coder_list);
495 p=(const CoderInfo *) GetNextValueInSplayTree(coder_list);
496 for (i=0; p != (const CoderInfo *) NULL; )
498 if ((p->stealth == MagickFalse) &&
499 (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
500 coder_map[i++]=ConstantString(p->name);
501 p=(const CoderInfo *) GetNextValueInSplayTree(coder_list);
503 UnlockSemaphoreInfo(coder_semaphore);
504 qsort((void *) coder_map,(size_t) i,sizeof(*coder_map),CoderCompare);
505 coder_map[i]=(char *) NULL;
506 *number_coders=(size_t) i;
511 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
515 + I n i t i a l i z e C o d e r L i s t %
519 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
521 % InitializeCoderList() initializes the coder list.
523 % The format of the InitializeCoderList method is:
525 % MagickBooleanType InitializeCoderList(ExceptionInfo *exception)
527 % A description of each parameter follows.
529 % o exception: return any errors or warnings in this structure.
532 static MagickBooleanType InitializeCoderList(ExceptionInfo *exception)
534 if ((coder_list == (SplayTreeInfo *) NULL) &&
535 (instantiate_coder == MagickFalse))
537 if (coder_semaphore == (SemaphoreInfo *) NULL)
538 AcquireSemaphoreInfo(&coder_semaphore);
539 LockSemaphoreInfo(coder_semaphore);
540 if ((coder_list == (SplayTreeInfo *) NULL) &&
541 (instantiate_coder == MagickFalse))
543 (void) LoadCoderLists(MagickCoderFilename,exception);
544 instantiate_coder=MagickTrue;
546 UnlockSemaphoreInfo(coder_semaphore);
548 return(coder_list != (SplayTreeInfo *) NULL ? MagickTrue : MagickFalse);
552 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
556 % L i s t C o d e r I n f o %
560 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
562 % ListCoderInfo() lists the coder info to a file.
564 % The format of the ListCoderInfo coder is:
566 % MagickBooleanType ListCoderInfo(FILE *file,ExceptionInfo *exception)
568 % A description of each parameter follows.
570 % o file: An pointer to a FILE.
572 % o exception: return any errors or warnings in this structure.
575 MagickExport MagickBooleanType ListCoderInfo(FILE *file,
576 ExceptionInfo *exception)
593 if (file == (const FILE *) NULL)
595 coder_info=GetCoderInfoList("*",&number_coders,exception);
596 if (coder_info == (const CoderInfo **) NULL)
598 path=(const char *) NULL;
599 for (i=0; i < (ssize_t) number_coders; i++)
601 if (coder_info[i]->stealth != MagickFalse)
603 if ((path == (const char *) NULL) ||
604 (LocaleCompare(path,coder_info[i]->path) != 0))
606 if (coder_info[i]->path != (char *) NULL)
607 (void) FormatLocaleFile(file,"\nPath: %s\n\n",coder_info[i]->path);
608 (void) FormatLocaleFile(file,"Magick Coder\n");
609 (void) FormatLocaleFile(file,
610 "-------------------------------------------------"
611 "------------------------------\n");
613 path=coder_info[i]->path;
614 (void) FormatLocaleFile(file,"%s",coder_info[i]->magick);
615 for (j=(ssize_t) strlen(coder_info[i]->magick); j <= 11; j++)
616 (void) FormatLocaleFile(file," ");
617 if (coder_info[i]->name != (char *) NULL)
618 (void) FormatLocaleFile(file,"%s",coder_info[i]->name);
619 (void) FormatLocaleFile(file,"\n");
621 coder_info=(const CoderInfo **) RelinquishMagickMemory((void *) coder_info);
627 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
631 + L o a d C o d e r L i s t %
635 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
637 % LoadCoderList() loads the coder configuration file which provides a
638 % mapping between coder attributes and a coder name.
640 % The format of the LoadCoderList coder is:
642 % MagickBooleanType LoadCoderList(const char *xml,const char *filename,
643 % const size_t depth,ExceptionInfo *exception)
645 % A description of each parameter follows:
647 % o xml: The coder list in XML format.
649 % o filename: The coder list filename.
651 % o depth: depth of <include /> statements.
653 % o exception: return any errors or warnings in this structure.
657 static void *DestroyCoderNode(void *coder_info)
662 p=(CoderInfo *) coder_info;
663 if (p->exempt == MagickFalse)
665 if (p->path != (char *) NULL)
666 p->path=DestroyString(p->path);
667 if (p->name != (char *) NULL)
668 p->name=DestroyString(p->name);
669 if (p->magick != (char *) NULL)
670 p->magick=DestroyString(p->magick);
672 return(RelinquishMagickMemory(p));
675 static MagickBooleanType LoadCoderList(const char *xml,const char *filename,
676 const size_t depth,ExceptionInfo *exception)
679 keyword[MaxTextExtent],
692 Load the coder map file.
694 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
695 "Loading coder configuration file \"%s\" ...",filename);
696 if (xml == (const char *) NULL)
698 if (coder_list == (SplayTreeInfo *) NULL)
700 coder_list=NewSplayTree(CompareSplayTreeString,RelinquishMagickMemory,
702 if (coder_list == (SplayTreeInfo *) NULL)
704 ThrowFileException(exception,ResourceLimitError,
705 "MemoryAllocationFailed",filename);
710 coder_info=(CoderInfo *) NULL;
711 token=AcquireString(xml);
712 for (q=(char *) xml; *q != '\0'; )
717 GetMagickToken(q,&q,token);
720 (void) CopyMagickString(keyword,token,MaxTextExtent);
721 if (LocaleNCompare(keyword,"<!DOCTYPE",9) == 0)
726 while ((LocaleNCompare(q,"]>",2) != 0) && (*q != '\0'))
727 GetMagickToken(q,&q,token);
730 if (LocaleNCompare(keyword,"<!--",4) == 0)
735 while ((LocaleNCompare(q,"->",2) != 0) && (*q != '\0'))
736 GetMagickToken(q,&q,token);
739 if (LocaleCompare(keyword,"<include") == 0)
744 while (((*token != '/') && (*(token+1) != '>')) && (*q != '\0'))
746 (void) CopyMagickString(keyword,token,MaxTextExtent);
747 GetMagickToken(q,&q,token);
750 GetMagickToken(q,&q,token);
751 if (LocaleCompare(keyword,"file") == 0)
754 (void) ThrowMagickException(exception,GetMagickModule(),
755 ConfigureError,"IncludeNodeNestedTooDeeply","`%s'",token);
762 GetPathComponent(filename,HeadPath,path);
764 (void) ConcatenateMagickString(path,DirectorySeparator,
766 if (*token == *DirectorySeparator)
767 (void) CopyMagickString(path,token,MaxTextExtent);
769 (void) ConcatenateMagickString(path,token,MaxTextExtent);
770 xml=FileToString(path,~0,exception);
771 if (xml != (char *) NULL)
773 status=LoadCoderList(xml,path,depth+1,exception);
774 xml=(char *) RelinquishMagickMemory(xml);
781 if (LocaleCompare(keyword,"<coder") == 0)
786 coder_info=(CoderInfo *) AcquireMagickMemory(sizeof(*coder_info));
787 if (coder_info == (CoderInfo *) NULL)
788 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
789 (void) ResetMagickMemory(coder_info,0,sizeof(*coder_info));
790 coder_info->path=ConstantString(filename);
791 coder_info->exempt=MagickFalse;
792 coder_info->signature=MagickSignature;
795 if (coder_info == (CoderInfo *) NULL)
797 if (LocaleCompare(keyword,"/>") == 0)
799 status=AddValueToSplayTree(coder_list,ConstantString(
800 coder_info->magick),coder_info);
801 if (status == MagickFalse)
802 (void) ThrowMagickException(exception,GetMagickModule(),
803 ResourceLimitError,"MemoryAllocationFailed","`%s'",
805 coder_info=(CoderInfo *) NULL;
807 GetMagickToken(q,(const char **) NULL,token);
810 GetMagickToken(q,&q,token);
811 GetMagickToken(q,&q,token);
817 if (LocaleCompare((char *) keyword,"magick") == 0)
819 coder_info->magick=ConstantString(token);
827 if (LocaleCompare((char *) keyword,"name") == 0)
829 coder_info->name=ConstantString(token);
837 if (LocaleCompare((char *) keyword,"stealth") == 0)
839 coder_info->stealth=IsStringTrue(token);
848 token=(char *) RelinquishMagickMemory(token);
853 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
857 % L o a d C o d e r L i s t s %
861 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
863 % LoadCoderLists() loads one or more coder configuration file which
864 % provides a mapping between coder attributes and a coder name.
866 % The format of the LoadCoderLists coder is:
868 % MagickBooleanType LoadCoderLists(const char *filename,
869 % ExceptionInfo *exception)
871 % A description of each parameter follows:
873 % o filename: the font file name.
875 % o exception: return any errors or warnings in this structure.
878 static MagickBooleanType LoadCoderLists(const char *filename,
879 ExceptionInfo *exception)
894 Load built-in coder map.
897 if (coder_list == (SplayTreeInfo *) NULL)
899 coder_list=NewSplayTree(CompareSplayTreeString,RelinquishMagickMemory,
901 if (coder_list == (SplayTreeInfo *) NULL)
903 ThrowFileException(exception,ResourceLimitError,
904 "MemoryAllocationFailed",filename);
908 for (i=0; i < (ssize_t) (sizeof(CoderMap)/sizeof(*CoderMap)); i++)
913 register const CoderMapInfo
917 coder_info=(CoderInfo *) AcquireMagickMemory(sizeof(*coder_info));
918 if (coder_info == (CoderInfo *) NULL)
920 (void) ThrowMagickException(exception,GetMagickModule(),
921 ResourceLimitError,"MemoryAllocationFailed","`%s'",coder_info->name);
924 (void) ResetMagickMemory(coder_info,0,sizeof(*coder_info));
925 coder_info->path=(char *) "[built-in]";
926 coder_info->magick=(char *) p->magick;
927 coder_info->name=(char *) p->name;
928 coder_info->exempt=MagickTrue;
929 coder_info->signature=MagickSignature;
930 status=AddValueToSplayTree(coder_list,ConstantString(coder_info->magick),
932 if (status == MagickFalse)
933 (void) ThrowMagickException(exception,GetMagickModule(),
934 ResourceLimitError,"MemoryAllocationFailed","`%s'",coder_info->name);
937 Load external coder map.
939 options=GetConfigureOptions(filename,exception);
940 option=(const StringInfo *) GetNextValueInLinkedList(options);
941 while (option != (const StringInfo *) NULL)
943 status|=LoadCoderList((const char *) GetStringInfoDatum(option),
944 GetStringInfoPath(option),0,exception);
945 option=(const StringInfo *) GetNextValueInLinkedList(options);
947 options=DestroyConfigureOptions(options);
948 return(status != 0 ? MagickTrue : MagickFalse);