2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6 % CCCC OOO DDDD EEEEE RRRR %
10 % CCCC OOO DDDD EEEEE R R %
13 % MagickCore Image Coder Methods %
20 % Copyright 1999-2014 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"
62 #include "MagickCore/xml-tree-private.h"
67 #define MagickCoderFilename "coder.xml"
72 typedef struct _CoderMapInfo
82 static const CoderMapInfo
87 { "8BIMTEXT", "META" },
88 { "8BIMWTEXT", "META" },
92 { "APP1JPEG", "META" },
100 { "BRF", "BRAILLE" },
124 { "FRACTAL", "PLASMA" },
130 { "GRANITE", "MAGICK" },
131 { "GROUP4", "TIFF" },
143 { "IMPLICIT", "***" },
145 { "IPTCTEXT", "META" },
146 { "IPTCWTEXT", "META" },
147 { "ISOBRL", "BRAILLE" },
156 { "LOGO", "MAGICK" },
169 { "NETSCAPE", "MAGICK" },
196 { "RADIAL-GRADIENT", "GRADIENT" },
203 { "ROSE", "MAGICK" },
206 { "SPARSE-COLOR", "TXT" },
211 { "TIFF64", "TIFF" },
214 { "UBRL", "BRAILLE" },
217 { "WIZARD", "MAGICK" },
218 #if defined(MAGICKCORE_WINGDI32_DELEGATE)
225 { "XTRNARRAY", "XTRN" },
226 { "XTRNBLOB", "XTRN" },
227 { "XTRNFILE", "XTRN" },
228 { "XTRNIMAGE", "XTRN" },
231 { "YCbCrA", "YCbCr" }
235 *coder_semaphore = (SemaphoreInfo *) NULL;
238 *coder_cache = (SplayTreeInfo *) NULL;
241 Forward declarations.
243 static MagickBooleanType
244 IsCoderTreeInstantiated(ExceptionInfo *),
245 LoadCoderCache(SplayTreeInfo *,const char *,const char *,const size_t,
249 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
253 % A c q u i r e C o d e r S p l a y T r e e %
257 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
259 % AcquireCoderCache() caches one or more coder configurations which
260 % provides a mapping between coder attributes and a coder name.
262 % The format of the AcquireCoderCache coder is:
264 % SplayTreeInfo *AcquireCoderCache(const char *filename,
265 % ExceptionInfo *exception)
267 % A description of each parameter follows:
269 % o filename: the font file name.
271 % o exception: return any errors or warnings in this structure.
275 static void *DestroyCoderNode(void *coder_info)
280 p=(CoderInfo *) coder_info;
281 if (p->exempt == MagickFalse)
283 if (p->path != (char *) NULL)
284 p->path=DestroyString(p->path);
285 if (p->name != (char *) NULL)
286 p->name=DestroyString(p->name);
287 if (p->magick != (char *) NULL)
288 p->magick=DestroyString(p->magick);
290 return(RelinquishMagickMemory(p));
293 static SplayTreeInfo *AcquireCoderCache(const char *filename,
294 ExceptionInfo *exception)
312 Load external coder map.
314 coder_cache=NewSplayTree(CompareSplayTreeString,RelinquishMagickMemory,
316 if (coder_cache == (SplayTreeInfo *) NULL)
317 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
319 options=GetConfigureOptions(filename,exception);
320 option=(const StringInfo *) GetNextValueInLinkedList(options);
321 while (option != (const StringInfo *) NULL)
323 status&=LoadCoderCache(coder_cache,(const char *)
324 GetStringInfoDatum(option),GetStringInfoPath(option),0,exception);
325 option=(const StringInfo *) GetNextValueInLinkedList(options);
327 options=DestroyConfigureOptions(options);
329 Load built-in coder map.
331 for (i=0; i < (ssize_t) (sizeof(CoderMap)/sizeof(*CoderMap)); i++)
336 register const CoderMapInfo
340 coder_info=(CoderInfo *) AcquireMagickMemory(sizeof(*coder_info));
341 if (coder_info == (CoderInfo *) NULL)
343 (void) ThrowMagickException(exception,GetMagickModule(),
344 ResourceLimitError,"MemoryAllocationFailed","`%s'",p->name);
347 (void) ResetMagickMemory(coder_info,0,sizeof(*coder_info));
348 coder_info->path=(char *) "[built-in]";
349 coder_info->magick=(char *) p->magick;
350 coder_info->name=(char *) p->name;
351 coder_info->exempt=MagickTrue;
352 coder_info->signature=MagickSignature;
353 status&=AddValueToSplayTree(coder_cache,ConstantString(coder_info->magick),
355 if (status == MagickFalse)
356 (void) ThrowMagickException(exception,GetMagickModule(),
357 ResourceLimitError,"MemoryAllocationFailed","`%s'",coder_info->name);
363 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
367 + C o d e r C o m p o n e n t G e n e s i s %
371 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
373 % CoderComponentGenesis() instantiates the coder component.
375 % The format of the CoderComponentGenesis method is:
377 % MagickBooleanType CoderComponentGenesis(void)
380 MagickPrivate MagickBooleanType CoderComponentGenesis(void)
382 if (coder_semaphore == (SemaphoreInfo *) NULL)
383 coder_semaphore=AcquireSemaphoreInfo();
388 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
392 + C o d e r C o m p o n e n t T e r m i n u s %
396 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
398 % CoderComponentTerminus() destroys the coder component.
400 % The format of the CoderComponentTerminus method is:
402 % CoderComponentTerminus(void)
405 MagickPrivate void CoderComponentTerminus(void)
407 if (coder_semaphore == (SemaphoreInfo *) NULL)
408 ActivateSemaphoreInfo(&coder_semaphore);
409 LockSemaphoreInfo(coder_semaphore);
410 if (coder_cache != (SplayTreeInfo *) NULL)
411 coder_cache=DestroySplayTree(coder_cache);
412 UnlockSemaphoreInfo(coder_semaphore);
413 RelinquishSemaphoreInfo(&coder_semaphore);
417 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
421 + G e t C o d e r I n f o %
425 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
427 % GetCoderInfo searches the coder list for the specified name and if found
428 % returns attributes for that coder.
430 % The format of the GetCoderInfo method is:
432 % const CoderInfo *GetCoderInfo(const char *name,ExceptionInfo *exception)
434 % A description of each parameter follows:
436 % o name: the coder name.
438 % o exception: return any errors or warnings in this structure.
441 MagickExport const CoderInfo *GetCoderInfo(const char *name,
442 ExceptionInfo *exception)
447 assert(exception != (ExceptionInfo *) NULL);
448 if (IsCoderTreeInstantiated(exception) == MagickFalse)
449 return((const CoderInfo *) NULL);
450 LockSemaphoreInfo(coder_semaphore);
451 if ((name == (const char *) NULL) || (LocaleCompare(name,"*") == 0))
453 ResetSplayTreeIterator(coder_cache);
454 coder_info=(const CoderInfo *) GetNextValueInSplayTree(coder_cache);
455 UnlockSemaphoreInfo(coder_semaphore);
458 coder_info=(const CoderInfo *) GetValueFromSplayTree(coder_cache,name);
459 UnlockSemaphoreInfo(coder_semaphore);
464 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
468 % G e t C o d e r I n f o L i s t %
472 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
474 % GetCoderInfoList() returns any coder_map that match the specified pattern.
475 % The format of the GetCoderInfoList function is:
477 % const CoderInfo **GetCoderInfoList(const char *pattern,
478 % size_t *number_coders,ExceptionInfo *exception)
480 % A description of each parameter follows:
482 % o pattern: Specifies a pointer to a text string containing a pattern.
484 % o number_coders: This integer returns the number of coders in the list.
486 % o exception: return any errors or warnings in this structure.
490 static int CoderInfoCompare(const void *x,const void *y)
496 p=(const CoderInfo **) x,
497 q=(const CoderInfo **) y;
498 if (LocaleCompare((*p)->path,(*q)->path) == 0)
499 return(LocaleCompare((*p)->name,(*q)->name));
500 return(LocaleCompare((*p)->path,(*q)->path));
503 MagickExport const CoderInfo **GetCoderInfoList(const char *pattern,
504 size_t *number_coders,ExceptionInfo *exception)
509 register const CoderInfo
518 assert(pattern != (char *) NULL);
519 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
520 assert(number_coders != (size_t *) NULL);
522 p=GetCoderInfo("*",exception);
523 if (p == (const CoderInfo *) NULL)
524 return((const CoderInfo **) NULL);
525 coder_map=(const CoderInfo **) AcquireQuantumMemory((size_t)
526 GetNumberOfNodesInSplayTree(coder_cache)+1UL,sizeof(*coder_map));
527 if (coder_map == (const CoderInfo **) NULL)
528 return((const CoderInfo **) NULL);
532 LockSemaphoreInfo(coder_semaphore);
533 ResetSplayTreeIterator(coder_cache);
534 p=(const CoderInfo *) GetNextValueInSplayTree(coder_cache);
535 for (i=0; p != (const CoderInfo *) NULL; )
537 if ((p->stealth == MagickFalse) &&
538 (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
540 p=(const CoderInfo *) GetNextValueInSplayTree(coder_cache);
542 UnlockSemaphoreInfo(coder_semaphore);
543 qsort((void *) coder_map,(size_t) i,sizeof(*coder_map),CoderInfoCompare);
544 coder_map[i]=(CoderInfo *) NULL;
545 *number_coders=(size_t) i;
550 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
554 % G e t C o d e r L i s t %
558 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
560 % GetCoderList() returns any coder_map that match the specified pattern.
562 % The format of the GetCoderList function is:
564 % char **GetCoderList(const char *pattern,size_t *number_coders,
565 % ExceptionInfo *exception)
567 % A description of each parameter follows:
569 % o pattern: Specifies a pointer to a text string containing a pattern.
571 % o number_coders: This integer returns the number of coders in the list.
573 % o exception: return any errors or warnings in this structure.
577 static int CoderCompare(const void *x,const void *y)
585 return(LocaleCompare(*p,*q));
588 MagickExport char **GetCoderList(const char *pattern,
589 size_t *number_coders,ExceptionInfo *exception)
594 register const CoderInfo
603 assert(pattern != (char *) NULL);
604 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
605 assert(number_coders != (size_t *) NULL);
607 p=GetCoderInfo("*",exception);
608 if (p == (const CoderInfo *) NULL)
609 return((char **) NULL);
610 coder_map=(char **) AcquireQuantumMemory((size_t)
611 GetNumberOfNodesInSplayTree(coder_cache)+1UL,sizeof(*coder_map));
612 if (coder_map == (char **) NULL)
613 return((char **) NULL);
617 LockSemaphoreInfo(coder_semaphore);
618 ResetSplayTreeIterator(coder_cache);
619 p=(const CoderInfo *) GetNextValueInSplayTree(coder_cache);
620 for (i=0; p != (const CoderInfo *) NULL; )
622 if ((p->stealth == MagickFalse) &&
623 (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
624 coder_map[i++]=ConstantString(p->name);
625 p=(const CoderInfo *) GetNextValueInSplayTree(coder_cache);
627 UnlockSemaphoreInfo(coder_semaphore);
628 qsort((void *) coder_map,(size_t) i,sizeof(*coder_map),CoderCompare);
629 coder_map[i]=(char *) NULL;
630 *number_coders=(size_t) i;
635 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
639 + I s C o d e r T r e e I n s t a n t i a t e d %
643 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
645 % IsCoderTreeInstantiated() determines if the coder tree is instantiated. If
646 % not, it instantiates the tree and returns it.
648 % The format of the IsCoderInstantiated method is:
650 % MagickBooleanType IsCoderTreeInstantiated(ExceptionInfo *exception)
652 % A description of each parameter follows.
654 % o exception: return any errors or warnings in this structure.
657 static MagickBooleanType IsCoderTreeInstantiated(ExceptionInfo *exception)
659 if (coder_cache == (SplayTreeInfo *) NULL)
661 if (coder_semaphore == (SemaphoreInfo *) NULL)
662 ActivateSemaphoreInfo(&coder_semaphore);
663 LockSemaphoreInfo(coder_semaphore);
664 if (coder_cache == (SplayTreeInfo *) NULL)
665 coder_cache=AcquireCoderCache(MagickCoderFilename,exception);
666 UnlockSemaphoreInfo(coder_semaphore);
668 return(coder_cache != (SplayTreeInfo *) NULL ? MagickTrue : MagickFalse);
672 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
676 % L i s t C o d e r I n f o %
680 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
682 % ListCoderInfo() lists the coder info to a file.
684 % The format of the ListCoderInfo coder is:
686 % MagickBooleanType ListCoderInfo(FILE *file,ExceptionInfo *exception)
688 % A description of each parameter follows.
690 % o file: An pointer to a FILE.
692 % o exception: return any errors or warnings in this structure.
695 MagickExport MagickBooleanType ListCoderInfo(FILE *file,
696 ExceptionInfo *exception)
713 if (file == (const FILE *) NULL)
715 coder_info=GetCoderInfoList("*",&number_coders,exception);
716 if (coder_info == (const CoderInfo **) NULL)
718 path=(const char *) NULL;
719 for (i=0; i < (ssize_t) number_coders; i++)
721 if (coder_info[i]->stealth != MagickFalse)
723 if ((path == (const char *) NULL) ||
724 (LocaleCompare(path,coder_info[i]->path) != 0))
726 if (coder_info[i]->path != (char *) NULL)
727 (void) FormatLocaleFile(file,"\nPath: %s\n\n",coder_info[i]->path);
728 (void) FormatLocaleFile(file,"Magick Coder\n");
729 (void) FormatLocaleFile(file,
730 "-------------------------------------------------"
731 "------------------------------\n");
733 path=coder_info[i]->path;
734 (void) FormatLocaleFile(file,"%s",coder_info[i]->magick);
735 for (j=(ssize_t) strlen(coder_info[i]->magick); j <= 11; j++)
736 (void) FormatLocaleFile(file," ");
737 if (coder_info[i]->name != (char *) NULL)
738 (void) FormatLocaleFile(file,"%s",coder_info[i]->name);
739 (void) FormatLocaleFile(file,"\n");
741 coder_info=(const CoderInfo **) RelinquishMagickMemory((void *) coder_info);
747 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
751 + L o a d C o d e r L i s t %
755 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
757 % LoadCoderCache() loads the coder configurations which provides a
758 % mapping between coder attributes and a coder name.
760 % The format of the LoadCoderCache coder is:
762 % MagickBooleanType LoadCoderCache(SplayTreeInfo *coder_cache,
763 % const char *xml,const char *filename,const size_t depth,
764 % ExceptionInfo *exception)
766 % A description of each parameter follows:
768 % o xml: The coder list in XML format.
770 % o filename: The coder list filename.
772 % o depth: depth of <include /> statements.
774 % o exception: return any errors or warnings in this structure.
777 static MagickBooleanType LoadCoderCache(SplayTreeInfo *coder_cache,
778 const char *xml,const char *filename,const size_t depth,
779 ExceptionInfo *exception)
782 keyword[MaxTextExtent],
795 Load the coder map file.
797 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
798 "Loading coder configuration file \"%s\" ...",filename);
799 if (xml == (const char *) NULL)
802 coder_info=(CoderInfo *) NULL;
803 token=AcquireString(xml);
804 for (q=(char *) xml; *q != '\0'; )
809 GetMagickToken(q,&q,token);
812 (void) CopyMagickString(keyword,token,MaxTextExtent);
813 if (LocaleNCompare(keyword,"<!DOCTYPE",9) == 0)
818 while ((LocaleNCompare(q,"]>",2) != 0) && (*q != '\0'))
819 GetMagickToken(q,&q,token);
822 if (LocaleNCompare(keyword,"<!--",4) == 0)
827 while ((LocaleNCompare(q,"->",2) != 0) && (*q != '\0'))
828 GetMagickToken(q,&q,token);
831 if (LocaleCompare(keyword,"<include") == 0)
836 while (((*token != '/') && (*(token+1) != '>')) && (*q != '\0'))
838 (void) CopyMagickString(keyword,token,MaxTextExtent);
839 GetMagickToken(q,&q,token);
842 GetMagickToken(q,&q,token);
843 if (LocaleCompare(keyword,"file") == 0)
846 (void) ThrowMagickException(exception,GetMagickModule(),
847 ConfigureError,"IncludeNodeNestedTooDeeply","`%s'",token);
854 GetPathComponent(filename,HeadPath,path);
856 (void) ConcatenateMagickString(path,DirectorySeparator,
858 if (*token == *DirectorySeparator)
859 (void) CopyMagickString(path,token,MaxTextExtent);
861 (void) ConcatenateMagickString(path,token,MaxTextExtent);
862 xml=FileToXML(path,~0UL);
863 if (xml != (char *) NULL)
865 status&=LoadCoderCache(coder_cache,xml,path,depth+1,
867 xml=(char *) RelinquishMagickMemory(xml);
874 if (LocaleCompare(keyword,"<coder") == 0)
879 coder_info=(CoderInfo *) AcquireMagickMemory(sizeof(*coder_info));
880 if (coder_info == (CoderInfo *) NULL)
881 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
882 (void) ResetMagickMemory(coder_info,0,sizeof(*coder_info));
883 coder_info->path=ConstantString(filename);
884 coder_info->exempt=MagickFalse;
885 coder_info->signature=MagickSignature;
888 if (coder_info == (CoderInfo *) NULL)
890 if (LocaleCompare(keyword,"/>") == 0)
892 status=AddValueToSplayTree(coder_cache,ConstantString(
893 coder_info->magick),coder_info);
894 if (status == MagickFalse)
895 (void) ThrowMagickException(exception,GetMagickModule(),
896 ResourceLimitError,"MemoryAllocationFailed","`%s'",
898 coder_info=(CoderInfo *) NULL;
901 GetMagickToken(q,(const char **) NULL,token);
904 GetMagickToken(q,&q,token);
905 GetMagickToken(q,&q,token);
911 if (LocaleCompare((char *) keyword,"magick") == 0)
913 coder_info->magick=ConstantString(token);
921 if (LocaleCompare((char *) keyword,"name") == 0)
923 coder_info->name=ConstantString(token);
931 if (LocaleCompare((char *) keyword,"stealth") == 0)
933 coder_info->stealth=IsStringTrue(token);
942 token=(char *) RelinquishMagickMemory(token);