2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6 % CCCC OOO DDDD EEEEE RRRR %
10 % CCCC OOO DDDD EEEEE R R %
13 % MagickCore Image Coder Methods %
20 % Copyright 1999-2009 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
76 static const CoderMapInfo
81 { "8BIMTEXT", "META" },
82 { "8BIMWTEXT", "META" },
86 { "APP1JPEG", "META" },
117 { "FRACTAL", "PLASMA" },
125 { "GRANITE", "MAGICK" },
126 { "GROUP4", "TIFF" },
136 { "IMPLICIT", "***" },
138 { "IPTCTEXT", "META" },
139 { "IPTCWTEXT", "META" },
140 { "ISOBRL", "BRAILLE" },
147 { "LOGO", "MAGICK" },
159 { "NETSCAPE", "MAGICK" },
184 { "RADIAL-GRADIENT", "GRADIENT" },
191 { "ROSE", "MAGICK" },
197 { "TIFF64", "TIFF" },
200 { "UBRL", "BRAILLE" },
204 { "WMFWIN32", "EMF" },
208 { "XTRNARRAY", "XTRN" },
209 { "XTRNBLOB", "XTRN" },
210 { "XTRNBSTR", "XTRN" },
211 { "XTRNFILE", "XTRN" },
212 { "XTRNIMAGE", "XTRN" },
213 { "XTRNSTREAM", "XTRN" },
216 { "YCbCrA", "YCbCr" },
217 { (const char *) NULL, (const char *) NULL }
221 *coder_semaphore = (SemaphoreInfo *) NULL;
224 *coder_list = (SplayTreeInfo *) NULL;
226 static volatile MagickBooleanType
227 instantiate_coder = MagickFalse;
230 Forward declarations.
232 static MagickBooleanType
233 InitializeCoderList(ExceptionInfo *),
234 LoadCoderLists(const char *,ExceptionInfo *);
237 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
241 + D e s t r o y C o d e r L i s t %
245 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
247 % DestroyCoderList() deallocates memory associated with the font list.
249 % The format of the DestroyCoderList method is:
251 % DestroyCoderList(void)
254 MagickExport void DestroyCoderList(void)
256 AcquireSemaphoreInfo(&coder_semaphore);
257 if (coder_list != (SplayTreeInfo *) NULL)
258 coder_list=DestroySplayTree(coder_list);
259 instantiate_coder=MagickFalse;
260 RelinquishSemaphoreInfo(coder_semaphore);
261 DestroySemaphoreInfo(&coder_semaphore);
265 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
269 + G e t C o d e r I n f o %
273 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
275 % GetCoderInfo searches the coder list for the specified name and if found
276 % returns attributes for that coder.
278 % The format of the GetCoderInfo method is:
280 % const CoderInfo *GetCoderInfo(const char *name,ExceptionInfo *exception)
282 % A description of each parameter follows:
284 % o name: the coder name.
286 % o exception: return any errors or warnings in this structure.
289 MagickExport const CoderInfo *GetCoderInfo(const char *name,
290 ExceptionInfo *exception)
292 assert(exception != (ExceptionInfo *) NULL);
293 if ((coder_list == (SplayTreeInfo *) NULL) ||
294 (instantiate_coder == MagickFalse))
295 if (InitializeCoderList(exception) == MagickFalse)
296 return((const CoderInfo *) NULL);
297 if ((coder_list == (SplayTreeInfo *) NULL) ||
298 (GetNumberOfNodesInSplayTree(coder_list) == 0))
299 return((const CoderInfo *) NULL);
300 if ((name == (const char *) NULL) || (LocaleCompare(name,"*") == 0))
302 ResetSplayTreeIterator(coder_list);
303 return((const CoderInfo *) GetNextValueInSplayTree(coder_list));
305 return((const CoderInfo *) GetValueFromSplayTree(coder_list,name));
309 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
313 % G e t C o d e r I n f o L i s t %
317 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
319 % GetCoderInfoList() returns any coder_map that match the specified pattern.
320 % The format of the GetCoderInfoList function is:
322 % const CoderInfo **GetCoderInfoList(const char *pattern,
323 % unsigned long *number_coders,ExceptionInfo *exception)
325 % A description of each parameter follows:
327 % o pattern: Specifies a pointer to a text string containing a pattern.
329 % o number_coders: This integer returns the number of coders in the list.
331 % o exception: return any errors or warnings in this structure.
335 static int CoderInfoCompare(const void *x,const void *y)
341 p=(const CoderInfo **) x,
342 q=(const CoderInfo **) y;
343 if (LocaleCompare((*p)->path,(*q)->path) == 0)
344 return(LocaleCompare((*p)->name,(*q)->name));
345 return(LocaleCompare((*p)->path,(*q)->path));
348 MagickExport const CoderInfo **GetCoderInfoList(const char *pattern,
349 unsigned long *number_coders,ExceptionInfo *exception)
354 register const CoderInfo
363 assert(pattern != (char *) NULL);
364 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
365 assert(number_coders != (unsigned long *) NULL);
367 p=GetCoderInfo("*",exception);
368 if (p == (const CoderInfo *) NULL)
369 return((const CoderInfo **) NULL);
370 coder_map=(const CoderInfo **) AcquireQuantumMemory((size_t)
371 GetNumberOfNodesInSplayTree(coder_list)+1UL,sizeof(*coder_map));
372 if (coder_map == (const CoderInfo **) NULL)
373 return((const CoderInfo **) NULL);
377 AcquireSemaphoreInfo(&coder_semaphore);
378 ResetSplayTreeIterator(coder_list);
379 p=(const CoderInfo *) GetNextValueInSplayTree(coder_list);
380 for (i=0; p != (const CoderInfo *) NULL; )
382 if ((p->stealth == MagickFalse) &&
383 (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
385 p=(const CoderInfo *) GetNextValueInSplayTree(coder_list);
387 RelinquishSemaphoreInfo(coder_semaphore);
388 qsort((void *) coder_map,(size_t) i,sizeof(*coder_map),CoderInfoCompare);
389 coder_map[i]=(CoderInfo *) NULL;
390 *number_coders=(unsigned long) i;
395 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
399 % G e t C o d e r L i s t %
403 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
405 % GetCoderList() returns any coder_map that match the specified pattern.
407 % The format of the GetCoderList function is:
409 % char **GetCoderList(const char *pattern,unsigned long *number_coders,
410 % ExceptionInfo *exception)
412 % A description of each parameter follows:
414 % o pattern: Specifies a pointer to a text string containing a pattern.
416 % o number_coders: This integer returns the number of coders in the list.
418 % o exception: return any errors or warnings in this structure.
422 static int CoderCompare(const void *x,const void *y)
430 return(LocaleCompare(*p,*q));
433 MagickExport char **GetCoderList(const char *pattern,
434 unsigned long *number_coders,ExceptionInfo *exception)
439 register const CoderInfo
448 assert(pattern != (char *) NULL);
449 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
450 assert(number_coders != (unsigned long *) NULL);
452 p=GetCoderInfo("*",exception);
453 if (p == (const CoderInfo *) NULL)
454 return((char **) NULL);
455 coder_map=(char **) AcquireQuantumMemory((size_t)
456 GetNumberOfNodesInSplayTree(coder_list)+1UL,sizeof(*coder_map));
457 if (coder_map == (char **) NULL)
458 return((char **) NULL);
462 AcquireSemaphoreInfo(&coder_semaphore);
463 ResetSplayTreeIterator(coder_list);
464 p=(const CoderInfo *) GetNextValueInSplayTree(coder_list);
465 for (i=0; p != (const CoderInfo *) NULL; )
467 if ((p->stealth == MagickFalse) &&
468 (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
469 coder_map[i++]=ConstantString(p->name);
470 p=(const CoderInfo *) GetNextValueInSplayTree(coder_list);
472 RelinquishSemaphoreInfo(coder_semaphore);
473 qsort((void *) coder_map,(size_t) i,sizeof(*coder_map),CoderCompare);
474 coder_map[i]=(char *) NULL;
475 *number_coders=(unsigned long) i;
480 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
484 + I n i t i a l i z e C o d e r L i s t %
488 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
490 % InitializeCoderList() initializes the coder list.
492 % The format of the InitializeCoderList method is:
494 % MagickBooleanType InitializeCoderList(ExceptionInfo *exception)
496 % A description of each parameter follows.
498 % o exception: return any errors or warnings in this structure.
501 static MagickBooleanType InitializeCoderList(ExceptionInfo *exception)
503 if ((coder_list == (SplayTreeInfo *) NULL) &&
504 (instantiate_coder == MagickFalse))
506 AcquireSemaphoreInfo(&coder_semaphore);
507 if ((coder_list == (SplayTreeInfo *) NULL) &&
508 (instantiate_coder == MagickFalse))
510 (void) LoadCoderLists(MagickCoderFilename,exception);
511 instantiate_coder=MagickTrue;
513 RelinquishSemaphoreInfo(coder_semaphore);
515 return(coder_list != (SplayTreeInfo *) NULL ? MagickTrue : MagickFalse);
519 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
523 % L i s t C o d e r I n f o %
527 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
529 % ListCoderInfo() lists the coder info to a file.
531 % The format of the ListCoderInfo coder is:
533 % MagickBooleanType ListCoderInfo(FILE *file,ExceptionInfo *exception)
535 % A description of each parameter follows.
537 % o file: An pointer to a FILE.
539 % o exception: return any errors or warnings in this structure.
542 MagickExport MagickBooleanType ListCoderInfo(FILE *file,
543 ExceptionInfo *exception)
560 if (file == (const FILE *) NULL)
562 coder_info=GetCoderInfoList("*",&number_coders,exception);
563 if (coder_info == (const CoderInfo **) NULL)
565 path=(const char *) NULL;
566 for (i=0; i < (long) number_coders; i++)
568 if (coder_info[i]->stealth != MagickFalse)
570 if ((path == (const char *) NULL) ||
571 (LocaleCompare(path,coder_info[i]->path) != 0))
573 if (coder_info[i]->path != (char *) NULL)
574 (void) fprintf(file,"\nPath: %s\n\n",coder_info[i]->path);
575 (void) fprintf(file,"Magick Coder\n");
576 (void) fprintf(file,"-------------------------------------------------"
577 "------------------------------\n");
579 path=coder_info[i]->path;
580 (void) fprintf(file,"%s",coder_info[i]->magick);
581 for (j=(long) strlen(coder_info[i]->magick); j <= 11; j++)
582 (void) fprintf(file," ");
583 if (coder_info[i]->name != (char *) NULL)
584 (void) fprintf(file,"%s",coder_info[i]->name);
585 (void) fprintf(file,"\n");
587 coder_info=(const CoderInfo **) RelinquishMagickMemory((void *) coder_info);
593 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
597 + L o a d C o d e r L i s t %
601 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
603 % LoadCoderList() loads the coder configuration file which provides a
604 % mapping between coder attributes and a coder name.
606 % The format of the LoadCoderList coder is:
608 % MagickBooleanType LoadCoderList(const char *xml,const char *filename,
609 % const unsigned long depth,ExceptionInfo *exception)
611 % A description of each parameter follows:
613 % o xml: The coder list in XML format.
615 % o filename: The coder list filename.
617 % o depth: depth of <include /> statements.
619 % o exception: return any errors or warnings in this structure.
623 static void *DestroyCoderNode(void *coder_info)
628 p=(CoderInfo *) coder_info;
629 if (p->exempt == MagickFalse)
631 if (p->path != (char *) NULL)
632 p->path=DestroyString(p->path);
633 if (p->name != (char *) NULL)
634 p->name=DestroyString(p->name);
635 if (p->magick != (char *) NULL)
636 p->magick=DestroyString(p->magick);
638 return(RelinquishMagickMemory(p));
641 static MagickBooleanType LoadCoderList(const char *xml,const char *filename,
642 const unsigned long depth,ExceptionInfo *exception)
645 keyword[MaxTextExtent],
658 Load the coder map file.
660 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
661 "Loading coder configuration file \"%s\" ...",filename);
662 if (xml == (const char *) NULL)
664 if (coder_list == (SplayTreeInfo *) NULL)
666 coder_list=NewSplayTree(CompareSplayTreeString,RelinquishMagickMemory,
668 if (coder_list == (SplayTreeInfo *) NULL)
670 ThrowFileException(exception,ResourceLimitError,
671 "MemoryAllocationFailed",filename);
676 coder_info=(CoderInfo *) NULL;
677 token=AcquireString(xml);
678 for (q=(char *) xml; *q != '\0'; )
683 GetMagickToken(q,&q,token);
686 (void) CopyMagickString(keyword,token,MaxTextExtent);
687 if (LocaleNCompare(keyword,"<!DOCTYPE",9) == 0)
692 while ((LocaleNCompare(q,"]>",2) != 0) && (*q != '\0'))
693 GetMagickToken(q,&q,token);
696 if (LocaleNCompare(keyword,"<!--",4) == 0)
701 while ((LocaleNCompare(q,"->",2) != 0) && (*q != '\0'))
702 GetMagickToken(q,&q,token);
705 if (LocaleCompare(keyword,"<include") == 0)
710 while (((*token != '/') && (*(token+1) != '>')) && (*q != '\0'))
712 (void) CopyMagickString(keyword,token,MaxTextExtent);
713 GetMagickToken(q,&q,token);
716 GetMagickToken(q,&q,token);
717 if (LocaleCompare(keyword,"file") == 0)
720 (void) ThrowMagickException(exception,GetMagickModule(),
721 ConfigureError,"IncludeNodeNestedTooDeeply","`%s'",token);
728 GetPathComponent(filename,HeadPath,path);
730 (void) ConcatenateMagickString(path,DirectorySeparator,
732 if (*token == *DirectorySeparator)
733 (void) CopyMagickString(path,token,MaxTextExtent);
735 (void) ConcatenateMagickString(path,token,MaxTextExtent);
736 xml=FileToString(path,~0,exception);
737 if (xml != (char *) NULL)
739 status=LoadCoderList(xml,path,depth+1,exception);
740 xml=(char *) RelinquishMagickMemory(xml);
747 if (LocaleCompare(keyword,"<coder") == 0)
752 coder_info=(CoderInfo *) AcquireMagickMemory(sizeof(*coder_info));
753 if (coder_info == (CoderInfo *) NULL)
754 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
755 (void) ResetMagickMemory(coder_info,0,sizeof(*coder_info));
756 coder_info->path=ConstantString(filename);
757 coder_info->exempt=MagickFalse;
758 coder_info->signature=MagickSignature;
761 if (coder_info == (CoderInfo *) NULL)
763 if (LocaleCompare(keyword,"/>") == 0)
765 status=AddValueToSplayTree(coder_list,ConstantString(
766 coder_info->magick),coder_info);
767 if (status == MagickFalse)
768 (void) ThrowMagickException(exception,GetMagickModule(),
769 ResourceLimitError,"MemoryAllocationFailed","`%s'",
771 coder_info=(CoderInfo *) NULL;
773 GetMagickToken(q,(const char **) NULL,token);
776 GetMagickToken(q,&q,token);
777 GetMagickToken(q,&q,token);
783 if (LocaleCompare((char *) keyword,"magick") == 0)
785 coder_info->magick=ConstantString(token);
793 if (LocaleCompare((char *) keyword,"name") == 0)
795 coder_info->name=ConstantString(token);
803 if (LocaleCompare((char *) keyword,"stealth") == 0)
805 coder_info->stealth=IsMagickTrue(token);
814 token=(char *) RelinquishMagickMemory(token);
819 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
823 % L o a d C o d e r L i s t s %
827 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
829 % LoadCoderLists() loads one or more coder configuration file which
830 % provides a mapping between coder attributes and a coder name.
832 % The format of the LoadCoderLists coder is:
834 % MagickBooleanType LoadCoderLists(const char *filename,
835 % ExceptionInfo *exception)
837 % A description of each parameter follows:
839 % o filename: the font file name.
841 % o exception: return any errors or warnings in this structure.
844 static MagickBooleanType LoadCoderLists(const char *filename,
845 ExceptionInfo *exception)
856 register const CoderMapInfo
860 Load built-in coder map.
863 if (coder_list == (SplayTreeInfo *) NULL)
865 coder_list=NewSplayTree(CompareSplayTreeString,RelinquishMagickMemory,
867 if (coder_list == (SplayTreeInfo *) NULL)
869 ThrowFileException(exception,ResourceLimitError,
870 "MemoryAllocationFailed",filename);
874 for (p=CoderMap; p->magick != (const char *) NULL; p++)
879 coder_info=(CoderInfo *) AcquireMagickMemory(sizeof(*coder_info));
880 if (coder_info == (CoderInfo *) NULL)
882 (void) ThrowMagickException(exception,GetMagickModule(),
883 ResourceLimitError,"MemoryAllocationFailed","`%s'",coder_info->name);
886 (void) ResetMagickMemory(coder_info,0,sizeof(*coder_info));
887 coder_info->path=(char *) "[built-in]";
888 coder_info->magick=(char *) p->magick;
889 coder_info->name=(char *) p->name;
890 coder_info->exempt=MagickTrue;
891 coder_info->signature=MagickSignature;
892 status=AddValueToSplayTree(coder_list,ConstantString(coder_info->magick),
894 if (status == MagickFalse)
895 (void) ThrowMagickException(exception,GetMagickModule(),
896 ResourceLimitError,"MemoryAllocationFailed","`%s'",coder_info->name);
899 Load external coder map.
901 options=GetConfigureOptions(filename,exception);
902 option=(const StringInfo *) GetNextValueInLinkedList(options);
903 while (option != (const StringInfo *) NULL)
905 status|=LoadCoderList((const char *) GetStringInfoDatum(option),
906 GetStringInfoPath(option),0,exception);
907 option=(const StringInfo *) GetNextValueInLinkedList(options);
909 options=DestroyConfigureOptions(options);
910 return(status != 0 ? MagickTrue : MagickFalse);