2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6 % CCCC OOO DDDD EEEEE RRRR %
10 % CCCC OOO DDDD EEEEE R R %
13 % MagickCore Image Coder Methods %
20 % Copyright 1999-2013 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" },
141 { "IMPLICIT", "***" },
143 { "IPTCTEXT", "META" },
144 { "IPTCWTEXT", "META" },
145 { "ISOBRL", "BRAILLE" },
154 { "LOGO", "MAGICK" },
167 { "NETSCAPE", "MAGICK" },
194 { "RADIAL-GRADIENT", "GRADIENT" },
200 { "ROSE", "MAGICK" },
203 { "SPARSE-COLOR", "TXT" },
208 { "TIFF64", "TIFF" },
211 { "UBRL", "BRAILLE" },
214 { "WIZARD", "MAGICK" },
216 { "WMFWIN32", "EMF" },
220 { "XTRNARRAY", "XTRN" },
221 { "XTRNBLOB", "XTRN" },
222 { "XTRNFILE", "XTRN" },
223 { "XTRNIMAGE", "XTRN" },
226 { "YCbCrA", "YCbCr" }
230 *coder_semaphore = (SemaphoreInfo *) NULL;
233 *coder_list = (SplayTreeInfo *) NULL;
235 static volatile MagickBooleanType
236 instantiate_coder = MagickFalse;
239 Forward declarations.
241 static MagickBooleanType
242 InitializeCoderList(ExceptionInfo *),
243 LoadCoderLists(const char *,ExceptionInfo *);
246 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
250 + C o d e r C o m p o n e n t G e n e s i s %
254 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
256 % CoderComponentGenesis() instantiates the coder component.
258 % The format of the CoderComponentGenesis method is:
260 % MagickBooleanType CoderComponentGenesis(void)
263 MagickPrivate MagickBooleanType CoderComponentGenesis(void)
265 AcquireSemaphoreInfo(&coder_semaphore);
270 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
274 + C o d e r C o m p o n e n t T e r m i n u s %
278 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
280 % CoderComponentTerminus() destroys the coder component.
282 % The format of the CoderComponentTerminus method is:
284 % CoderComponentTerminus(void)
287 MagickPrivate void CoderComponentTerminus(void)
289 if (coder_semaphore == (SemaphoreInfo *) NULL)
290 AcquireSemaphoreInfo(&coder_semaphore);
291 LockSemaphoreInfo(coder_semaphore);
292 if (coder_list != (SplayTreeInfo *) NULL)
293 coder_list=DestroySplayTree(coder_list);
294 instantiate_coder=MagickFalse;
295 UnlockSemaphoreInfo(coder_semaphore);
296 DestroySemaphoreInfo(&coder_semaphore);
300 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
304 + G e t C o d e r I n f o %
308 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
310 % GetCoderInfo searches the coder list for the specified name and if found
311 % returns attributes for that coder.
313 % The format of the GetCoderInfo method is:
315 % const CoderInfo *GetCoderInfo(const char *name,ExceptionInfo *exception)
317 % A description of each parameter follows:
319 % o name: the coder name.
321 % o exception: return any errors or warnings in this structure.
324 MagickExport const CoderInfo *GetCoderInfo(const char *name,
325 ExceptionInfo *exception)
327 assert(exception != (ExceptionInfo *) NULL);
328 if ((coder_list == (SplayTreeInfo *) NULL) ||
329 (instantiate_coder == MagickFalse))
330 if (InitializeCoderList(exception) == MagickFalse)
331 return((const CoderInfo *) NULL);
332 if ((coder_list == (SplayTreeInfo *) NULL) ||
333 (GetNumberOfNodesInSplayTree(coder_list) == 0))
334 return((const CoderInfo *) NULL);
335 if ((name == (const char *) NULL) || (LocaleCompare(name,"*") == 0))
337 ResetSplayTreeIterator(coder_list);
338 return((const CoderInfo *) GetNextValueInSplayTree(coder_list));
340 return((const CoderInfo *) GetValueFromSplayTree(coder_list,name));
344 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
348 % G e t C o d e r I n f o L i s t %
352 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
354 % GetCoderInfoList() returns any coder_map that match the specified pattern.
355 % The format of the GetCoderInfoList function is:
357 % const CoderInfo **GetCoderInfoList(const char *pattern,
358 % size_t *number_coders,ExceptionInfo *exception)
360 % A description of each parameter follows:
362 % o pattern: Specifies a pointer to a text string containing a pattern.
364 % o number_coders: This integer returns the number of coders in the list.
366 % o exception: return any errors or warnings in this structure.
370 static int CoderInfoCompare(const void *x,const void *y)
376 p=(const CoderInfo **) x,
377 q=(const CoderInfo **) y;
378 if (LocaleCompare((*p)->path,(*q)->path) == 0)
379 return(LocaleCompare((*p)->name,(*q)->name));
380 return(LocaleCompare((*p)->path,(*q)->path));
383 MagickExport const CoderInfo **GetCoderInfoList(const char *pattern,
384 size_t *number_coders,ExceptionInfo *exception)
389 register const CoderInfo
398 assert(pattern != (char *) NULL);
399 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
400 assert(number_coders != (size_t *) NULL);
402 p=GetCoderInfo("*",exception);
403 if (p == (const CoderInfo *) NULL)
404 return((const CoderInfo **) NULL);
405 coder_map=(const CoderInfo **) AcquireQuantumMemory((size_t)
406 GetNumberOfNodesInSplayTree(coder_list)+1UL,sizeof(*coder_map));
407 if (coder_map == (const CoderInfo **) NULL)
408 return((const CoderInfo **) NULL);
412 LockSemaphoreInfo(coder_semaphore);
413 ResetSplayTreeIterator(coder_list);
414 p=(const CoderInfo *) GetNextValueInSplayTree(coder_list);
415 for (i=0; p != (const CoderInfo *) NULL; )
417 if ((p->stealth == MagickFalse) &&
418 (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
420 p=(const CoderInfo *) GetNextValueInSplayTree(coder_list);
422 UnlockSemaphoreInfo(coder_semaphore);
423 qsort((void *) coder_map,(size_t) i,sizeof(*coder_map),CoderInfoCompare);
424 coder_map[i]=(CoderInfo *) NULL;
425 *number_coders=(size_t) i;
430 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
434 % G e t C o d e r L i s t %
438 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
440 % GetCoderList() returns any coder_map that match the specified pattern.
442 % The format of the GetCoderList function is:
444 % char **GetCoderList(const char *pattern,size_t *number_coders,
445 % ExceptionInfo *exception)
447 % A description of each parameter follows:
449 % o pattern: Specifies a pointer to a text string containing a pattern.
451 % o number_coders: This integer returns the number of coders in the list.
453 % o exception: return any errors or warnings in this structure.
457 static int CoderCompare(const void *x,const void *y)
465 return(LocaleCompare(*p,*q));
468 MagickExport char **GetCoderList(const char *pattern,
469 size_t *number_coders,ExceptionInfo *exception)
474 register const CoderInfo
483 assert(pattern != (char *) NULL);
484 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
485 assert(number_coders != (size_t *) NULL);
487 p=GetCoderInfo("*",exception);
488 if (p == (const CoderInfo *) NULL)
489 return((char **) NULL);
490 coder_map=(char **) AcquireQuantumMemory((size_t)
491 GetNumberOfNodesInSplayTree(coder_list)+1UL,sizeof(*coder_map));
492 if (coder_map == (char **) NULL)
493 return((char **) NULL);
497 LockSemaphoreInfo(coder_semaphore);
498 ResetSplayTreeIterator(coder_list);
499 p=(const CoderInfo *) GetNextValueInSplayTree(coder_list);
500 for (i=0; p != (const CoderInfo *) NULL; )
502 if ((p->stealth == MagickFalse) &&
503 (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
504 coder_map[i++]=ConstantString(p->name);
505 p=(const CoderInfo *) GetNextValueInSplayTree(coder_list);
507 UnlockSemaphoreInfo(coder_semaphore);
508 qsort((void *) coder_map,(size_t) i,sizeof(*coder_map),CoderCompare);
509 coder_map[i]=(char *) NULL;
510 *number_coders=(size_t) i;
515 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
519 + I n i t i a l i z e C o d e r L i s t %
523 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
525 % InitializeCoderList() initializes the coder list.
527 % The format of the InitializeCoderList method is:
529 % MagickBooleanType InitializeCoderList(ExceptionInfo *exception)
531 % A description of each parameter follows.
533 % o exception: return any errors or warnings in this structure.
536 static MagickBooleanType InitializeCoderList(ExceptionInfo *exception)
538 if ((coder_list == (SplayTreeInfo *) NULL) &&
539 (instantiate_coder == MagickFalse))
541 if (coder_semaphore == (SemaphoreInfo *) NULL)
542 AcquireSemaphoreInfo(&coder_semaphore);
543 LockSemaphoreInfo(coder_semaphore);
544 if ((coder_list == (SplayTreeInfo *) NULL) &&
545 (instantiate_coder == MagickFalse))
547 (void) LoadCoderLists(MagickCoderFilename,exception);
548 instantiate_coder=MagickTrue;
550 UnlockSemaphoreInfo(coder_semaphore);
552 return(coder_list != (SplayTreeInfo *) NULL ? MagickTrue : MagickFalse);
556 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
560 % L i s t C o d e r I n f o %
564 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
566 % ListCoderInfo() lists the coder info to a file.
568 % The format of the ListCoderInfo coder is:
570 % MagickBooleanType ListCoderInfo(FILE *file,ExceptionInfo *exception)
572 % A description of each parameter follows.
574 % o file: An pointer to a FILE.
576 % o exception: return any errors or warnings in this structure.
579 MagickExport MagickBooleanType ListCoderInfo(FILE *file,
580 ExceptionInfo *exception)
597 if (file == (const FILE *) NULL)
599 coder_info=GetCoderInfoList("*",&number_coders,exception);
600 if (coder_info == (const CoderInfo **) NULL)
602 path=(const char *) NULL;
603 for (i=0; i < (ssize_t) number_coders; i++)
605 if (coder_info[i]->stealth != MagickFalse)
607 if ((path == (const char *) NULL) ||
608 (LocaleCompare(path,coder_info[i]->path) != 0))
610 if (coder_info[i]->path != (char *) NULL)
611 (void) FormatLocaleFile(file,"\nPath: %s\n\n",coder_info[i]->path);
612 (void) FormatLocaleFile(file,"Magick Coder\n");
613 (void) FormatLocaleFile(file,
614 "-------------------------------------------------"
615 "------------------------------\n");
617 path=coder_info[i]->path;
618 (void) FormatLocaleFile(file,"%s",coder_info[i]->magick);
619 for (j=(ssize_t) strlen(coder_info[i]->magick); j <= 11; j++)
620 (void) FormatLocaleFile(file," ");
621 if (coder_info[i]->name != (char *) NULL)
622 (void) FormatLocaleFile(file,"%s",coder_info[i]->name);
623 (void) FormatLocaleFile(file,"\n");
625 coder_info=(const CoderInfo **) RelinquishMagickMemory((void *) coder_info);
631 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
635 + L o a d C o d e r L i s t %
639 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
641 % LoadCoderList() loads the coder configuration file which provides a
642 % mapping between coder attributes and a coder name.
644 % The format of the LoadCoderList coder is:
646 % MagickBooleanType LoadCoderList(const char *xml,const char *filename,
647 % const size_t depth,ExceptionInfo *exception)
649 % A description of each parameter follows:
651 % o xml: The coder list in XML format.
653 % o filename: The coder list filename.
655 % o depth: depth of <include /> statements.
657 % o exception: return any errors or warnings in this structure.
661 static void *DestroyCoderNode(void *coder_info)
666 p=(CoderInfo *) coder_info;
667 if (p->exempt == MagickFalse)
669 if (p->path != (char *) NULL)
670 p->path=DestroyString(p->path);
671 if (p->name != (char *) NULL)
672 p->name=DestroyString(p->name);
673 if (p->magick != (char *) NULL)
674 p->magick=DestroyString(p->magick);
676 return(RelinquishMagickMemory(p));
679 static MagickBooleanType LoadCoderList(const char *xml,const char *filename,
680 const size_t depth,ExceptionInfo *exception)
683 keyword[MaxTextExtent],
696 Load the coder map file.
698 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
699 "Loading coder configuration file \"%s\" ...",filename);
700 if (xml == (const char *) NULL)
702 if (coder_list == (SplayTreeInfo *) NULL)
704 coder_list=NewSplayTree(CompareSplayTreeString,RelinquishMagickMemory,
706 if (coder_list == (SplayTreeInfo *) NULL)
708 ThrowFileException(exception,ResourceLimitError,
709 "MemoryAllocationFailed",filename);
714 coder_info=(CoderInfo *) NULL;
715 token=AcquireString(xml);
716 for (q=(char *) xml; *q != '\0'; )
721 GetMagickToken(q,&q,token);
724 (void) CopyMagickString(keyword,token,MaxTextExtent);
725 if (LocaleNCompare(keyword,"<!DOCTYPE",9) == 0)
730 while ((LocaleNCompare(q,"]>",2) != 0) && (*q != '\0'))
731 GetMagickToken(q,&q,token);
734 if (LocaleNCompare(keyword,"<!--",4) == 0)
739 while ((LocaleNCompare(q,"->",2) != 0) && (*q != '\0'))
740 GetMagickToken(q,&q,token);
743 if (LocaleCompare(keyword,"<include") == 0)
748 while (((*token != '/') && (*(token+1) != '>')) && (*q != '\0'))
750 (void) CopyMagickString(keyword,token,MaxTextExtent);
751 GetMagickToken(q,&q,token);
754 GetMagickToken(q,&q,token);
755 if (LocaleCompare(keyword,"file") == 0)
758 (void) ThrowMagickException(exception,GetMagickModule(),
759 ConfigureError,"IncludeNodeNestedTooDeeply","`%s'",token);
766 GetPathComponent(filename,HeadPath,path);
768 (void) ConcatenateMagickString(path,DirectorySeparator,
770 if (*token == *DirectorySeparator)
771 (void) CopyMagickString(path,token,MaxTextExtent);
773 (void) ConcatenateMagickString(path,token,MaxTextExtent);
774 xml=FileToString(path,~0,exception);
775 if (xml != (char *) NULL)
777 status=LoadCoderList(xml,path,depth+1,exception);
778 xml=(char *) RelinquishMagickMemory(xml);
785 if (LocaleCompare(keyword,"<coder") == 0)
790 coder_info=(CoderInfo *) AcquireMagickMemory(sizeof(*coder_info));
791 if (coder_info == (CoderInfo *) NULL)
792 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
793 (void) ResetMagickMemory(coder_info,0,sizeof(*coder_info));
794 coder_info->path=ConstantString(filename);
795 coder_info->exempt=MagickFalse;
796 coder_info->signature=MagickSignature;
799 if (coder_info == (CoderInfo *) NULL)
801 if (LocaleCompare(keyword,"/>") == 0)
803 status=AddValueToSplayTree(coder_list,ConstantString(
804 coder_info->magick),coder_info);
805 if (status == MagickFalse)
806 (void) ThrowMagickException(exception,GetMagickModule(),
807 ResourceLimitError,"MemoryAllocationFailed","`%s'",
809 coder_info=(CoderInfo *) NULL;
811 GetMagickToken(q,(const char **) NULL,token);
814 GetMagickToken(q,&q,token);
815 GetMagickToken(q,&q,token);
821 if (LocaleCompare((char *) keyword,"magick") == 0)
823 coder_info->magick=ConstantString(token);
831 if (LocaleCompare((char *) keyword,"name") == 0)
833 coder_info->name=ConstantString(token);
841 if (LocaleCompare((char *) keyword,"stealth") == 0)
843 coder_info->stealth=IsStringTrue(token);
852 token=(char *) RelinquishMagickMemory(token);
857 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
861 % L o a d C o d e r L i s t s %
865 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
867 % LoadCoderLists() loads one or more coder configuration file which
868 % provides a mapping between coder attributes and a coder name.
870 % The format of the LoadCoderLists coder is:
872 % MagickBooleanType LoadCoderLists(const char *filename,
873 % ExceptionInfo *exception)
875 % A description of each parameter follows:
877 % o filename: the font file name.
879 % o exception: return any errors or warnings in this structure.
882 static MagickBooleanType LoadCoderLists(const char *filename,
883 ExceptionInfo *exception)
898 Load built-in coder map.
901 if (coder_list == (SplayTreeInfo *) NULL)
903 coder_list=NewSplayTree(CompareSplayTreeString,RelinquishMagickMemory,
905 if (coder_list == (SplayTreeInfo *) NULL)
907 ThrowFileException(exception,ResourceLimitError,
908 "MemoryAllocationFailed",filename);
912 for (i=0; i < (ssize_t) (sizeof(CoderMap)/sizeof(*CoderMap)); i++)
917 register const CoderMapInfo
921 coder_info=(CoderInfo *) AcquireMagickMemory(sizeof(*coder_info));
922 if (coder_info == (CoderInfo *) NULL)
924 (void) ThrowMagickException(exception,GetMagickModule(),
925 ResourceLimitError,"MemoryAllocationFailed","`%s'",coder_info->name);
928 (void) ResetMagickMemory(coder_info,0,sizeof(*coder_info));
929 coder_info->path=(char *) "[built-in]";
930 coder_info->magick=(char *) p->magick;
931 coder_info->name=(char *) p->name;
932 coder_info->exempt=MagickTrue;
933 coder_info->signature=MagickSignature;
934 status=AddValueToSplayTree(coder_list,ConstantString(coder_info->magick),
936 if (status == MagickFalse)
937 (void) ThrowMagickException(exception,GetMagickModule(),
938 ResourceLimitError,"MemoryAllocationFailed","`%s'",coder_info->name);
941 Load external coder map.
943 options=GetConfigureOptions(filename,exception);
944 option=(const StringInfo *) GetNextValueInLinkedList(options);
945 while (option != (const StringInfo *) NULL)
947 status|=LoadCoderList((const char *) GetStringInfoDatum(option),
948 GetStringInfoPath(option),0,exception);
949 option=(const StringInfo *) GetNextValueInLinkedList(options);
951 options=DestroyConfigureOptions(options);
952 return(status != 0 ? MagickTrue : MagickFalse);