2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6 % CCCC OOO DDDD EEEEE RRRR %
10 % CCCC OOO DDDD EEEEE R R %
13 % MagickCore Image Coder Methods %
20 % Copyright 1999-2010 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 "magick/studio.h"
43 #include "magick/blob.h"
44 #include "magick/client.h"
45 #include "magick/coder.h"
46 #include "magick/configure.h"
47 #include "magick/draw.h"
48 #include "magick/exception.h"
49 #include "magick/exception-private.h"
50 #include "magick/hashmap.h"
51 #include "magick/log.h"
52 #include "magick/memory_.h"
53 #include "magick/option.h"
54 #include "magick/semaphore.h"
55 #include "magick/string_.h"
56 #include "magick/splay-tree.h"
57 #include "magick/token.h"
58 #include "magick/utility.h"
59 #include "magick/xml-tree.h"
64 #define MagickCoderFilename "coder.xml"
69 typedef struct _CoderMapInfo
79 static const CoderMapInfo
84 { "8BIMTEXT", "META" },
85 { "8BIMWTEXT", "META" },
89 { "APP1JPEG", "META" },
121 { "FRACTAL", "PLASMA" },
127 { "GRANITE", "MAGICK" },
128 { "GROUP4", "TIFF" },
138 { "IMPLICIT", "***" },
140 { "IPTCTEXT", "META" },
141 { "IPTCWTEXT", "META" },
142 { "ISOBRL", "BRAILLE" },
150 { "LOGO", "MAGICK" },
162 { "NETSCAPE", "MAGICK" },
188 { "RADIAL-GRADIENT", "GRADIENT" },
194 { "ROSE", "MAGICK" },
200 { "TIFF64", "TIFF" },
203 { "UBRL", "BRAILLE" },
207 { "WMFWIN32", "EMF" },
211 { "XTRNARRAY", "XTRN" },
212 { "XTRNBLOB", "XTRN" },
213 { "XTRNBSTR", "XTRN" },
214 { "XTRNFILE", "XTRN" },
215 { "XTRNIMAGE", "XTRN" },
216 { "XTRNSTREAM", "XTRN" },
219 { "YCbCrA", "YCbCr" }
223 *coder_semaphore = (SemaphoreInfo *) NULL;
226 *coder_list = (SplayTreeInfo *) NULL;
228 static volatile MagickBooleanType
229 instantiate_coder = MagickFalse;
232 Forward declarations.
234 static MagickBooleanType
235 InitializeCoderList(ExceptionInfo *),
236 LoadCoderLists(const char *,ExceptionInfo *);
239 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
243 + C o d e r C o m p o n e n t G e n e s i s %
247 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
249 % CoderComponentGenesis() instantiates the coder component.
251 % The format of the CoderComponentGenesis method is:
253 % MagickBooleanType CoderComponentGenesis(void)
256 MagickExport MagickBooleanType CoderComponentGenesis(void)
258 AcquireSemaphoreInfo(&coder_semaphore);
263 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
267 + C o d e r C o m p o n e n t T e r m i n u s %
271 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
273 % CoderComponentTerminus() destroys the coder component.
275 % The format of the CoderComponentTerminus method is:
277 % CoderComponentTerminus(void)
280 MagickExport void CoderComponentTerminus(void)
282 if (coder_semaphore == (SemaphoreInfo *) NULL)
283 AcquireSemaphoreInfo(&coder_semaphore);
284 LockSemaphoreInfo(coder_semaphore);
285 if (coder_list != (SplayTreeInfo *) NULL)
286 coder_list=DestroySplayTree(coder_list);
287 instantiate_coder=MagickFalse;
288 UnlockSemaphoreInfo(coder_semaphore);
289 DestroySemaphoreInfo(&coder_semaphore);
293 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
297 + G e t C o d e r I n f o %
301 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
303 % GetCoderInfo searches the coder list for the specified name and if found
304 % returns attributes for that coder.
306 % The format of the GetCoderInfo method is:
308 % const CoderInfo *GetCoderInfo(const char *name,ExceptionInfo *exception)
310 % A description of each parameter follows:
312 % o name: the coder name.
314 % o exception: return any errors or warnings in this structure.
317 MagickExport const CoderInfo *GetCoderInfo(const char *name,
318 ExceptionInfo *exception)
320 assert(exception != (ExceptionInfo *) NULL);
321 if ((coder_list == (SplayTreeInfo *) NULL) ||
322 (instantiate_coder == MagickFalse))
323 if (InitializeCoderList(exception) == MagickFalse)
324 return((const CoderInfo *) NULL);
325 if ((coder_list == (SplayTreeInfo *) NULL) ||
326 (GetNumberOfNodesInSplayTree(coder_list) == 0))
327 return((const CoderInfo *) NULL);
328 if ((name == (const char *) NULL) || (LocaleCompare(name,"*") == 0))
330 ResetSplayTreeIterator(coder_list);
331 return((const CoderInfo *) GetNextValueInSplayTree(coder_list));
333 return((const CoderInfo *) GetValueFromSplayTree(coder_list,name));
337 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
341 % G e t C o d e r I n f o L i s t %
345 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
347 % GetCoderInfoList() returns any coder_map that match the specified pattern.
348 % The format of the GetCoderInfoList function is:
350 % const CoderInfo **GetCoderInfoList(const char *pattern,
351 % size_t *number_coders,ExceptionInfo *exception)
353 % A description of each parameter follows:
355 % o pattern: Specifies a pointer to a text string containing a pattern.
357 % o number_coders: This integer returns the number of coders in the list.
359 % o exception: return any errors or warnings in this structure.
363 static int CoderInfoCompare(const void *x,const void *y)
369 p=(const CoderInfo **) x,
370 q=(const CoderInfo **) y;
371 if (LocaleCompare((*p)->path,(*q)->path) == 0)
372 return(LocaleCompare((*p)->name,(*q)->name));
373 return(LocaleCompare((*p)->path,(*q)->path));
376 MagickExport const CoderInfo **GetCoderInfoList(const char *pattern,
377 size_t *number_coders,ExceptionInfo *exception)
382 register const CoderInfo
391 assert(pattern != (char *) NULL);
392 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
393 assert(number_coders != (size_t *) NULL);
395 p=GetCoderInfo("*",exception);
396 if (p == (const CoderInfo *) NULL)
397 return((const CoderInfo **) NULL);
398 coder_map=(const CoderInfo **) AcquireQuantumMemory((size_t)
399 GetNumberOfNodesInSplayTree(coder_list)+1UL,sizeof(*coder_map));
400 if (coder_map == (const CoderInfo **) NULL)
401 return((const CoderInfo **) NULL);
405 LockSemaphoreInfo(coder_semaphore);
406 ResetSplayTreeIterator(coder_list);
407 p=(const CoderInfo *) GetNextValueInSplayTree(coder_list);
408 for (i=0; p != (const CoderInfo *) NULL; )
410 if ((p->stealth == MagickFalse) &&
411 (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
413 p=(const CoderInfo *) GetNextValueInSplayTree(coder_list);
415 UnlockSemaphoreInfo(coder_semaphore);
416 qsort((void *) coder_map,(size_t) i,sizeof(*coder_map),CoderInfoCompare);
417 coder_map[i]=(CoderInfo *) NULL;
418 *number_coders=(size_t) i;
423 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
427 % G e t C o d e r L i s t %
431 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
433 % GetCoderList() returns any coder_map that match the specified pattern.
435 % The format of the GetCoderList function is:
437 % char **GetCoderList(const char *pattern,size_t *number_coders,
438 % ExceptionInfo *exception)
440 % A description of each parameter follows:
442 % o pattern: Specifies a pointer to a text string containing a pattern.
444 % o number_coders: This integer returns the number of coders in the list.
446 % o exception: return any errors or warnings in this structure.
450 static int CoderCompare(const void *x,const void *y)
458 return(LocaleCompare(*p,*q));
461 MagickExport char **GetCoderList(const char *pattern,
462 size_t *number_coders,ExceptionInfo *exception)
467 register const CoderInfo
476 assert(pattern != (char *) NULL);
477 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
478 assert(number_coders != (size_t *) NULL);
480 p=GetCoderInfo("*",exception);
481 if (p == (const CoderInfo *) NULL)
482 return((char **) NULL);
483 coder_map=(char **) AcquireQuantumMemory((size_t)
484 GetNumberOfNodesInSplayTree(coder_list)+1UL,sizeof(*coder_map));
485 if (coder_map == (char **) NULL)
486 return((char **) NULL);
490 LockSemaphoreInfo(coder_semaphore);
491 ResetSplayTreeIterator(coder_list);
492 p=(const CoderInfo *) GetNextValueInSplayTree(coder_list);
493 for (i=0; p != (const CoderInfo *) NULL; )
495 if ((p->stealth == MagickFalse) &&
496 (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
497 coder_map[i++]=ConstantString(p->name);
498 p=(const CoderInfo *) GetNextValueInSplayTree(coder_list);
500 UnlockSemaphoreInfo(coder_semaphore);
501 qsort((void *) coder_map,(size_t) i,sizeof(*coder_map),CoderCompare);
502 coder_map[i]=(char *) NULL;
503 *number_coders=(size_t) i;
508 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
512 + I n i t i a l i z e C o d e r L i s t %
516 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
518 % InitializeCoderList() initializes the coder list.
520 % The format of the InitializeCoderList method is:
522 % MagickBooleanType InitializeCoderList(ExceptionInfo *exception)
524 % A description of each parameter follows.
526 % o exception: return any errors or warnings in this structure.
529 static MagickBooleanType InitializeCoderList(ExceptionInfo *exception)
531 if ((coder_list == (SplayTreeInfo *) NULL) &&
532 (instantiate_coder == MagickFalse))
534 if (coder_semaphore == (SemaphoreInfo *) NULL)
535 AcquireSemaphoreInfo(&coder_semaphore);
536 LockSemaphoreInfo(coder_semaphore);
537 if ((coder_list == (SplayTreeInfo *) NULL) &&
538 (instantiate_coder == MagickFalse))
540 (void) LoadCoderLists(MagickCoderFilename,exception);
541 instantiate_coder=MagickTrue;
543 UnlockSemaphoreInfo(coder_semaphore);
545 return(coder_list != (SplayTreeInfo *) NULL ? MagickTrue : MagickFalse);
549 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
553 % L i s t C o d e r I n f o %
557 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
559 % ListCoderInfo() lists the coder info to a file.
561 % The format of the ListCoderInfo coder is:
563 % MagickBooleanType ListCoderInfo(FILE *file,ExceptionInfo *exception)
565 % A description of each parameter follows.
567 % o file: An pointer to a FILE.
569 % o exception: return any errors or warnings in this structure.
572 MagickExport MagickBooleanType ListCoderInfo(FILE *file,
573 ExceptionInfo *exception)
590 if (file == (const FILE *) NULL)
592 coder_info=GetCoderInfoList("*",&number_coders,exception);
593 if (coder_info == (const CoderInfo **) NULL)
595 path=(const char *) NULL;
596 for (i=0; i < (ssize_t) number_coders; i++)
598 if (coder_info[i]->stealth != MagickFalse)
600 if ((path == (const char *) NULL) ||
601 (LocaleCompare(path,coder_info[i]->path) != 0))
603 if (coder_info[i]->path != (char *) NULL)
604 (void) fprintf(file,"\nPath: %s\n\n",coder_info[i]->path);
605 (void) fprintf(file,"Magick Coder\n");
606 (void) fprintf(file,"-------------------------------------------------"
607 "------------------------------\n");
609 path=coder_info[i]->path;
610 (void) fprintf(file,"%s",coder_info[i]->magick);
611 for (j=(ssize_t) strlen(coder_info[i]->magick); j <= 11; j++)
612 (void) fprintf(file," ");
613 if (coder_info[i]->name != (char *) NULL)
614 (void) fprintf(file,"%s",coder_info[i]->name);
615 (void) fprintf(file,"\n");
617 coder_info=(const CoderInfo **) RelinquishMagickMemory((void *) coder_info);
623 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
627 + L o a d C o d e r L i s t %
631 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
633 % LoadCoderList() loads the coder configuration file which provides a
634 % mapping between coder attributes and a coder name.
636 % The format of the LoadCoderList coder is:
638 % MagickBooleanType LoadCoderList(const char *xml,const char *filename,
639 % const size_t depth,ExceptionInfo *exception)
641 % A description of each parameter follows:
643 % o xml: The coder list in XML format.
645 % o filename: The coder list filename.
647 % o depth: depth of <include /> statements.
649 % o exception: return any errors or warnings in this structure.
653 static void *DestroyCoderNode(void *coder_info)
658 p=(CoderInfo *) coder_info;
659 if (p->exempt == MagickFalse)
661 if (p->path != (char *) NULL)
662 p->path=DestroyString(p->path);
663 if (p->name != (char *) NULL)
664 p->name=DestroyString(p->name);
665 if (p->magick != (char *) NULL)
666 p->magick=DestroyString(p->magick);
668 return(RelinquishMagickMemory(p));
671 static MagickBooleanType LoadCoderList(const char *xml,const char *filename,
672 const size_t depth,ExceptionInfo *exception)
675 keyword[MaxTextExtent],
688 Load the coder map file.
690 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
691 "Loading coder configuration file \"%s\" ...",filename);
692 if (xml == (const char *) NULL)
694 if (coder_list == (SplayTreeInfo *) NULL)
696 coder_list=NewSplayTree(CompareSplayTreeString,RelinquishMagickMemory,
698 if (coder_list == (SplayTreeInfo *) NULL)
700 ThrowFileException(exception,ResourceLimitError,
701 "MemoryAllocationFailed",filename);
706 coder_info=(CoderInfo *) NULL;
707 token=AcquireString(xml);
708 for (q=(char *) xml; *q != '\0'; )
713 GetMagickToken(q,&q,token);
716 (void) CopyMagickString(keyword,token,MaxTextExtent);
717 if (LocaleNCompare(keyword,"<!DOCTYPE",9) == 0)
722 while ((LocaleNCompare(q,"]>",2) != 0) && (*q != '\0'))
723 GetMagickToken(q,&q,token);
726 if (LocaleNCompare(keyword,"<!--",4) == 0)
731 while ((LocaleNCompare(q,"->",2) != 0) && (*q != '\0'))
732 GetMagickToken(q,&q,token);
735 if (LocaleCompare(keyword,"<include") == 0)
740 while (((*token != '/') && (*(token+1) != '>')) && (*q != '\0'))
742 (void) CopyMagickString(keyword,token,MaxTextExtent);
743 GetMagickToken(q,&q,token);
746 GetMagickToken(q,&q,token);
747 if (LocaleCompare(keyword,"file") == 0)
750 (void) ThrowMagickException(exception,GetMagickModule(),
751 ConfigureError,"IncludeNodeNestedTooDeeply","`%s'",token);
758 GetPathComponent(filename,HeadPath,path);
760 (void) ConcatenateMagickString(path,DirectorySeparator,
762 if (*token == *DirectorySeparator)
763 (void) CopyMagickString(path,token,MaxTextExtent);
765 (void) ConcatenateMagickString(path,token,MaxTextExtent);
766 xml=FileToString(path,~0,exception);
767 if (xml != (char *) NULL)
769 status=LoadCoderList(xml,path,depth+1,exception);
770 xml=(char *) RelinquishMagickMemory(xml);
777 if (LocaleCompare(keyword,"<coder") == 0)
782 coder_info=(CoderInfo *) AcquireMagickMemory(sizeof(*coder_info));
783 if (coder_info == (CoderInfo *) NULL)
784 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
785 (void) ResetMagickMemory(coder_info,0,sizeof(*coder_info));
786 coder_info->path=ConstantString(filename);
787 coder_info->exempt=MagickFalse;
788 coder_info->signature=MagickSignature;
791 if (coder_info == (CoderInfo *) NULL)
793 if (LocaleCompare(keyword,"/>") == 0)
795 status=AddValueToSplayTree(coder_list,ConstantString(
796 coder_info->magick),coder_info);
797 if (status == MagickFalse)
798 (void) ThrowMagickException(exception,GetMagickModule(),
799 ResourceLimitError,"MemoryAllocationFailed","`%s'",
801 coder_info=(CoderInfo *) NULL;
803 GetMagickToken(q,(const char **) NULL,token);
806 GetMagickToken(q,&q,token);
807 GetMagickToken(q,&q,token);
813 if (LocaleCompare((char *) keyword,"magick") == 0)
815 coder_info->magick=ConstantString(token);
823 if (LocaleCompare((char *) keyword,"name") == 0)
825 coder_info->name=ConstantString(token);
833 if (LocaleCompare((char *) keyword,"stealth") == 0)
835 coder_info->stealth=IsMagickTrue(token);
844 token=(char *) RelinquishMagickMemory(token);
849 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
853 % L o a d C o d e r L i s t s %
857 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
859 % LoadCoderLists() loads one or more coder configuration file which
860 % provides a mapping between coder attributes and a coder name.
862 % The format of the LoadCoderLists coder is:
864 % MagickBooleanType LoadCoderLists(const char *filename,
865 % ExceptionInfo *exception)
867 % A description of each parameter follows:
869 % o filename: the font file name.
871 % o exception: return any errors or warnings in this structure.
874 static MagickBooleanType LoadCoderLists(const char *filename,
875 ExceptionInfo *exception)
890 Load built-in coder map.
893 if (coder_list == (SplayTreeInfo *) NULL)
895 coder_list=NewSplayTree(CompareSplayTreeString,RelinquishMagickMemory,
897 if (coder_list == (SplayTreeInfo *) NULL)
899 ThrowFileException(exception,ResourceLimitError,
900 "MemoryAllocationFailed",filename);
904 for (i=0; i < (ssize_t) (sizeof(CoderMap)/sizeof(*CoderMap)); i++)
909 register const CoderMapInfo
913 coder_info=(CoderInfo *) AcquireMagickMemory(sizeof(*coder_info));
914 if (coder_info == (CoderInfo *) NULL)
916 (void) ThrowMagickException(exception,GetMagickModule(),
917 ResourceLimitError,"MemoryAllocationFailed","`%s'",coder_info->name);
920 (void) ResetMagickMemory(coder_info,0,sizeof(*coder_info));
921 coder_info->path=(char *) "[built-in]";
922 coder_info->magick=(char *) p->magick;
923 coder_info->name=(char *) p->name;
924 coder_info->exempt=MagickTrue;
925 coder_info->signature=MagickSignature;
926 status=AddValueToSplayTree(coder_list,ConstantString(coder_info->magick),
928 if (status == MagickFalse)
929 (void) ThrowMagickException(exception,GetMagickModule(),
930 ResourceLimitError,"MemoryAllocationFailed","`%s'",coder_info->name);
933 Load external coder map.
935 options=GetConfigureOptions(filename,exception);
936 option=(const StringInfo *) GetNextValueInLinkedList(options);
937 while (option != (const StringInfo *) NULL)
939 status|=LoadCoderList((const char *) GetStringInfoDatum(option),
940 GetStringInfoPath(option),0,exception);
941 option=(const StringInfo *) GetNextValueInLinkedList(options);
943 options=DestroyConfigureOptions(options);
944 return(status != 0 ? MagickTrue : MagickFalse);