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" },
153 { "LOGO", "MAGICK" },
166 { "NETSCAPE", "MAGICK" },
193 { "RADIAL-GRADIENT", "GRADIENT" },
199 { "ROSE", "MAGICK" },
205 { "TIFF64", "TIFF" },
208 { "UBRL", "BRAILLE" },
211 { "WIZARD", "MAGICK" },
213 { "WMFWIN32", "EMF" },
217 { "XTRNARRAY", "XTRN" },
218 { "XTRNBLOB", "XTRN" },
219 { "XTRNFILE", "XTRN" },
220 { "XTRNIMAGE", "XTRN" },
223 { "YCbCrA", "YCbCr" }
227 *coder_semaphore = (SemaphoreInfo *) NULL;
230 *coder_list = (SplayTreeInfo *) NULL;
232 static volatile MagickBooleanType
233 instantiate_coder = MagickFalse;
236 Forward declarations.
238 static MagickBooleanType
239 InitializeCoderList(ExceptionInfo *),
240 LoadCoderLists(const char *,ExceptionInfo *);
243 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
247 + C o d e r C o m p o n e n t G e n e s i s %
251 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
253 % CoderComponentGenesis() instantiates the coder component.
255 % The format of the CoderComponentGenesis method is:
257 % MagickBooleanType CoderComponentGenesis(void)
260 MagickPrivate MagickBooleanType CoderComponentGenesis(void)
262 AcquireSemaphoreInfo(&coder_semaphore);
267 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
271 + C o d e r C o m p o n e n t T e r m i n u s %
275 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
277 % CoderComponentTerminus() destroys the coder component.
279 % The format of the CoderComponentTerminus method is:
281 % CoderComponentTerminus(void)
284 MagickPrivate void CoderComponentTerminus(void)
286 if (coder_semaphore == (SemaphoreInfo *) NULL)
287 AcquireSemaphoreInfo(&coder_semaphore);
288 LockSemaphoreInfo(coder_semaphore);
289 if (coder_list != (SplayTreeInfo *) NULL)
290 coder_list=DestroySplayTree(coder_list);
291 instantiate_coder=MagickFalse;
292 UnlockSemaphoreInfo(coder_semaphore);
293 DestroySemaphoreInfo(&coder_semaphore);
297 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
301 + G e t C o d e r I n f o %
305 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
307 % GetCoderInfo searches the coder list for the specified name and if found
308 % returns attributes for that coder.
310 % The format of the GetCoderInfo method is:
312 % const CoderInfo *GetCoderInfo(const char *name,ExceptionInfo *exception)
314 % A description of each parameter follows:
316 % o name: the coder name.
318 % o exception: return any errors or warnings in this structure.
321 MagickExport const CoderInfo *GetCoderInfo(const char *name,
322 ExceptionInfo *exception)
324 assert(exception != (ExceptionInfo *) NULL);
325 if ((coder_list == (SplayTreeInfo *) NULL) ||
326 (instantiate_coder == MagickFalse))
327 if (InitializeCoderList(exception) == MagickFalse)
328 return((const CoderInfo *) NULL);
329 if ((coder_list == (SplayTreeInfo *) NULL) ||
330 (GetNumberOfNodesInSplayTree(coder_list) == 0))
331 return((const CoderInfo *) NULL);
332 if ((name == (const char *) NULL) || (LocaleCompare(name,"*") == 0))
334 ResetSplayTreeIterator(coder_list);
335 return((const CoderInfo *) GetNextValueInSplayTree(coder_list));
337 return((const CoderInfo *) GetValueFromSplayTree(coder_list,name));
341 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
345 % G e t C o d e r I n f o L i s t %
349 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
351 % GetCoderInfoList() returns any coder_map that match the specified pattern.
352 % The format of the GetCoderInfoList function is:
354 % const CoderInfo **GetCoderInfoList(const char *pattern,
355 % size_t *number_coders,ExceptionInfo *exception)
357 % A description of each parameter follows:
359 % o pattern: Specifies a pointer to a text string containing a pattern.
361 % o number_coders: This integer returns the number of coders in the list.
363 % o exception: return any errors or warnings in this structure.
367 static int CoderInfoCompare(const void *x,const void *y)
373 p=(const CoderInfo **) x,
374 q=(const CoderInfo **) y;
375 if (LocaleCompare((*p)->path,(*q)->path) == 0)
376 return(LocaleCompare((*p)->name,(*q)->name));
377 return(LocaleCompare((*p)->path,(*q)->path));
380 MagickExport const CoderInfo **GetCoderInfoList(const char *pattern,
381 size_t *number_coders,ExceptionInfo *exception)
386 register const CoderInfo
395 assert(pattern != (char *) NULL);
396 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
397 assert(number_coders != (size_t *) NULL);
399 p=GetCoderInfo("*",exception);
400 if (p == (const CoderInfo *) NULL)
401 return((const CoderInfo **) NULL);
402 coder_map=(const CoderInfo **) AcquireQuantumMemory((size_t)
403 GetNumberOfNodesInSplayTree(coder_list)+1UL,sizeof(*coder_map));
404 if (coder_map == (const CoderInfo **) NULL)
405 return((const CoderInfo **) NULL);
409 LockSemaphoreInfo(coder_semaphore);
410 ResetSplayTreeIterator(coder_list);
411 p=(const CoderInfo *) GetNextValueInSplayTree(coder_list);
412 for (i=0; p != (const CoderInfo *) NULL; )
414 if ((p->stealth == MagickFalse) &&
415 (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
417 p=(const CoderInfo *) GetNextValueInSplayTree(coder_list);
419 UnlockSemaphoreInfo(coder_semaphore);
420 qsort((void *) coder_map,(size_t) i,sizeof(*coder_map),CoderInfoCompare);
421 coder_map[i]=(CoderInfo *) NULL;
422 *number_coders=(size_t) i;
427 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
431 % G e t C o d e r L i s t %
435 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
437 % GetCoderList() returns any coder_map that match the specified pattern.
439 % The format of the GetCoderList function is:
441 % char **GetCoderList(const char *pattern,size_t *number_coders,
442 % ExceptionInfo *exception)
444 % A description of each parameter follows:
446 % o pattern: Specifies a pointer to a text string containing a pattern.
448 % o number_coders: This integer returns the number of coders in the list.
450 % o exception: return any errors or warnings in this structure.
454 static int CoderCompare(const void *x,const void *y)
462 return(LocaleCompare(*p,*q));
465 MagickExport char **GetCoderList(const char *pattern,
466 size_t *number_coders,ExceptionInfo *exception)
471 register const CoderInfo
480 assert(pattern != (char *) NULL);
481 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
482 assert(number_coders != (size_t *) NULL);
484 p=GetCoderInfo("*",exception);
485 if (p == (const CoderInfo *) NULL)
486 return((char **) NULL);
487 coder_map=(char **) AcquireQuantumMemory((size_t)
488 GetNumberOfNodesInSplayTree(coder_list)+1UL,sizeof(*coder_map));
489 if (coder_map == (char **) NULL)
490 return((char **) NULL);
494 LockSemaphoreInfo(coder_semaphore);
495 ResetSplayTreeIterator(coder_list);
496 p=(const CoderInfo *) GetNextValueInSplayTree(coder_list);
497 for (i=0; p != (const CoderInfo *) NULL; )
499 if ((p->stealth == MagickFalse) &&
500 (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
501 coder_map[i++]=ConstantString(p->name);
502 p=(const CoderInfo *) GetNextValueInSplayTree(coder_list);
504 UnlockSemaphoreInfo(coder_semaphore);
505 qsort((void *) coder_map,(size_t) i,sizeof(*coder_map),CoderCompare);
506 coder_map[i]=(char *) NULL;
507 *number_coders=(size_t) i;
512 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
516 + I n i t i a l i z e C o d e r L i s t %
520 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
522 % InitializeCoderList() initializes the coder list.
524 % The format of the InitializeCoderList method is:
526 % MagickBooleanType InitializeCoderList(ExceptionInfo *exception)
528 % A description of each parameter follows.
530 % o exception: return any errors or warnings in this structure.
533 static MagickBooleanType InitializeCoderList(ExceptionInfo *exception)
535 if ((coder_list == (SplayTreeInfo *) NULL) &&
536 (instantiate_coder == MagickFalse))
538 if (coder_semaphore == (SemaphoreInfo *) NULL)
539 AcquireSemaphoreInfo(&coder_semaphore);
540 LockSemaphoreInfo(coder_semaphore);
541 if ((coder_list == (SplayTreeInfo *) NULL) &&
542 (instantiate_coder == MagickFalse))
544 (void) LoadCoderLists(MagickCoderFilename,exception);
545 instantiate_coder=MagickTrue;
547 UnlockSemaphoreInfo(coder_semaphore);
549 return(coder_list != (SplayTreeInfo *) NULL ? MagickTrue : MagickFalse);
553 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
557 % L i s t C o d e r I n f o %
561 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
563 % ListCoderInfo() lists the coder info to a file.
565 % The format of the ListCoderInfo coder is:
567 % MagickBooleanType ListCoderInfo(FILE *file,ExceptionInfo *exception)
569 % A description of each parameter follows.
571 % o file: An pointer to a FILE.
573 % o exception: return any errors or warnings in this structure.
576 MagickExport MagickBooleanType ListCoderInfo(FILE *file,
577 ExceptionInfo *exception)
594 if (file == (const FILE *) NULL)
596 coder_info=GetCoderInfoList("*",&number_coders,exception);
597 if (coder_info == (const CoderInfo **) NULL)
599 path=(const char *) NULL;
600 for (i=0; i < (ssize_t) number_coders; i++)
602 if (coder_info[i]->stealth != MagickFalse)
604 if ((path == (const char *) NULL) ||
605 (LocaleCompare(path,coder_info[i]->path) != 0))
607 if (coder_info[i]->path != (char *) NULL)
608 (void) FormatLocaleFile(file,"\nPath: %s\n\n",coder_info[i]->path);
609 (void) FormatLocaleFile(file,"Magick Coder\n");
610 (void) FormatLocaleFile(file,
611 "-------------------------------------------------"
612 "------------------------------\n");
614 path=coder_info[i]->path;
615 (void) FormatLocaleFile(file,"%s",coder_info[i]->magick);
616 for (j=(ssize_t) strlen(coder_info[i]->magick); j <= 11; j++)
617 (void) FormatLocaleFile(file," ");
618 if (coder_info[i]->name != (char *) NULL)
619 (void) FormatLocaleFile(file,"%s",coder_info[i]->name);
620 (void) FormatLocaleFile(file,"\n");
622 coder_info=(const CoderInfo **) RelinquishMagickMemory((void *) coder_info);
628 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
632 + L o a d C o d e r L i s t %
636 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
638 % LoadCoderList() loads the coder configuration file which provides a
639 % mapping between coder attributes and a coder name.
641 % The format of the LoadCoderList coder is:
643 % MagickBooleanType LoadCoderList(const char *xml,const char *filename,
644 % const size_t depth,ExceptionInfo *exception)
646 % A description of each parameter follows:
648 % o xml: The coder list in XML format.
650 % o filename: The coder list filename.
652 % o depth: depth of <include /> statements.
654 % o exception: return any errors or warnings in this structure.
658 static void *DestroyCoderNode(void *coder_info)
663 p=(CoderInfo *) coder_info;
664 if (p->exempt == MagickFalse)
666 if (p->path != (char *) NULL)
667 p->path=DestroyString(p->path);
668 if (p->name != (char *) NULL)
669 p->name=DestroyString(p->name);
670 if (p->magick != (char *) NULL)
671 p->magick=DestroyString(p->magick);
673 return(RelinquishMagickMemory(p));
676 static MagickBooleanType LoadCoderList(const char *xml,const char *filename,
677 const size_t depth,ExceptionInfo *exception)
680 keyword[MaxTextExtent],
693 Load the coder map file.
695 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
696 "Loading coder configuration file \"%s\" ...",filename);
697 if (xml == (const char *) NULL)
699 if (coder_list == (SplayTreeInfo *) NULL)
701 coder_list=NewSplayTree(CompareSplayTreeString,RelinquishMagickMemory,
703 if (coder_list == (SplayTreeInfo *) NULL)
705 ThrowFileException(exception,ResourceLimitError,
706 "MemoryAllocationFailed",filename);
711 coder_info=(CoderInfo *) NULL;
712 token=AcquireString(xml);
713 for (q=(char *) xml; *q != '\0'; )
718 GetMagickToken(q,&q,token);
721 (void) CopyMagickString(keyword,token,MaxTextExtent);
722 if (LocaleNCompare(keyword,"<!DOCTYPE",9) == 0)
727 while ((LocaleNCompare(q,"]>",2) != 0) && (*q != '\0'))
728 GetMagickToken(q,&q,token);
731 if (LocaleNCompare(keyword,"<!--",4) == 0)
736 while ((LocaleNCompare(q,"->",2) != 0) && (*q != '\0'))
737 GetMagickToken(q,&q,token);
740 if (LocaleCompare(keyword,"<include") == 0)
745 while (((*token != '/') && (*(token+1) != '>')) && (*q != '\0'))
747 (void) CopyMagickString(keyword,token,MaxTextExtent);
748 GetMagickToken(q,&q,token);
751 GetMagickToken(q,&q,token);
752 if (LocaleCompare(keyword,"file") == 0)
755 (void) ThrowMagickException(exception,GetMagickModule(),
756 ConfigureError,"IncludeNodeNestedTooDeeply","'%s'",token);
763 GetPathComponent(filename,HeadPath,path);
765 (void) ConcatenateMagickString(path,DirectorySeparator,
767 if (*token == *DirectorySeparator)
768 (void) CopyMagickString(path,token,MaxTextExtent);
770 (void) ConcatenateMagickString(path,token,MaxTextExtent);
771 xml=FileToString(path,~0,exception);
772 if (xml != (char *) NULL)
774 status=LoadCoderList(xml,path,depth+1,exception);
775 xml=(char *) RelinquishMagickMemory(xml);
782 if (LocaleCompare(keyword,"<coder") == 0)
787 coder_info=(CoderInfo *) AcquireMagickMemory(sizeof(*coder_info));
788 if (coder_info == (CoderInfo *) NULL)
789 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
790 (void) ResetMagickMemory(coder_info,0,sizeof(*coder_info));
791 coder_info->path=ConstantString(filename);
792 coder_info->exempt=MagickFalse;
793 coder_info->signature=MagickSignature;
796 if (coder_info == (CoderInfo *) NULL)
798 if (LocaleCompare(keyword,"/>") == 0)
800 status=AddValueToSplayTree(coder_list,ConstantString(
801 coder_info->magick),coder_info);
802 if (status == MagickFalse)
803 (void) ThrowMagickException(exception,GetMagickModule(),
804 ResourceLimitError,"MemoryAllocationFailed","'%s'",
806 coder_info=(CoderInfo *) NULL;
808 GetMagickToken(q,(const char **) NULL,token);
811 GetMagickToken(q,&q,token);
812 GetMagickToken(q,&q,token);
818 if (LocaleCompare((char *) keyword,"magick") == 0)
820 coder_info->magick=ConstantString(token);
828 if (LocaleCompare((char *) keyword,"name") == 0)
830 coder_info->name=ConstantString(token);
838 if (LocaleCompare((char *) keyword,"stealth") == 0)
840 coder_info->stealth=IsStringTrue(token);
849 token=(char *) RelinquishMagickMemory(token);
854 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
858 % L o a d C o d e r L i s t s %
862 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
864 % LoadCoderLists() loads one or more coder configuration file which
865 % provides a mapping between coder attributes and a coder name.
867 % The format of the LoadCoderLists coder is:
869 % MagickBooleanType LoadCoderLists(const char *filename,
870 % ExceptionInfo *exception)
872 % A description of each parameter follows:
874 % o filename: the font file name.
876 % o exception: return any errors or warnings in this structure.
879 static MagickBooleanType LoadCoderLists(const char *filename,
880 ExceptionInfo *exception)
895 Load built-in coder map.
898 if (coder_list == (SplayTreeInfo *) NULL)
900 coder_list=NewSplayTree(CompareSplayTreeString,RelinquishMagickMemory,
902 if (coder_list == (SplayTreeInfo *) NULL)
904 ThrowFileException(exception,ResourceLimitError,
905 "MemoryAllocationFailed",filename);
909 for (i=0; i < (ssize_t) (sizeof(CoderMap)/sizeof(*CoderMap)); i++)
914 register const CoderMapInfo
918 coder_info=(CoderInfo *) AcquireMagickMemory(sizeof(*coder_info));
919 if (coder_info == (CoderInfo *) NULL)
921 (void) ThrowMagickException(exception,GetMagickModule(),
922 ResourceLimitError,"MemoryAllocationFailed","'%s'",coder_info->name);
925 (void) ResetMagickMemory(coder_info,0,sizeof(*coder_info));
926 coder_info->path=(char *) "[built-in]";
927 coder_info->magick=(char *) p->magick;
928 coder_info->name=(char *) p->name;
929 coder_info->exempt=MagickTrue;
930 coder_info->signature=MagickSignature;
931 status=AddValueToSplayTree(coder_list,ConstantString(coder_info->magick),
933 if (status == MagickFalse)
934 (void) ThrowMagickException(exception,GetMagickModule(),
935 ResourceLimitError,"MemoryAllocationFailed","'%s'",coder_info->name);
938 Load external coder map.
940 options=GetConfigureOptions(filename,exception);
941 option=(const StringInfo *) GetNextValueInLinkedList(options);
942 while (option != (const StringInfo *) NULL)
944 status|=LoadCoderList((const char *) GetStringInfoDatum(option),
945 GetStringInfoPath(option),0,exception);
946 option=(const StringInfo *) GetNextValueInLinkedList(options);
948 options=DestroyConfigureOptions(options);
949 return(status != 0 ? MagickTrue : MagickFalse);