2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6 % CCCC OOO DDDD EEEEE RRRR %
10 % CCCC OOO DDDD EEEEE R R %
13 % MagickCore Image Coder Methods %
20 % Copyright 1999-2015 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" },
144 { "IMPLICIT", "***" },
146 { "IPTCTEXT", "META" },
147 { "IPTCWTEXT", "META" },
148 { "ISOBRL", "BRAILLE" },
160 { "LOGO", "MAGICK" },
173 { "NETSCAPE", "MAGICK" },
200 { "RADIAL-GRADIENT", "GRADIENT" },
208 { "ROSE", "MAGICK" },
212 { "SPARSE-COLOR", "TXT" },
217 { "TIFF64", "TIFF" },
220 { "UBRL", "BRAILLE" },
223 { "WIZARD", "MAGICK" },
224 #if defined(MAGICKCORE_WINGDI32_DELEGATE)
231 { "XTRNARRAY", "XTRN" },
232 { "XTRNBLOB", "XTRN" },
233 { "XTRNFILE", "XTRN" },
234 { "XTRNIMAGE", "XTRN" },
237 { "YCbCrA", "YCbCr" }
241 *coder_semaphore = (SemaphoreInfo *) NULL;
244 *coder_cache = (SplayTreeInfo *) NULL;
247 Forward declarations.
249 static MagickBooleanType
250 IsCoderTreeInstantiated(ExceptionInfo *),
251 LoadCoderCache(SplayTreeInfo *,const char *,const char *,const size_t,
255 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
259 + A c q u i r e C o d e r C a c h e %
263 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
265 % AcquireCoderCache() caches one or more coder configurations which provides a
266 % mapping between coder attributes and a coder name.
268 % The format of the AcquireCoderCache coder is:
270 % SplayTreeInfo *AcquireCoderCache(const char *filename,
271 % ExceptionInfo *exception)
273 % A description of each parameter follows:
275 % o filename: the font file name.
277 % o exception: return any errors or warnings in this structure.
281 static void *DestroyCoderNode(void *coder_info)
286 p=(CoderInfo *) coder_info;
287 if (p->exempt == MagickFalse)
289 if (p->path != (char *) NULL)
290 p->path=DestroyString(p->path);
291 if (p->name != (char *) NULL)
292 p->name=DestroyString(p->name);
293 if (p->magick != (char *) NULL)
294 p->magick=DestroyString(p->magick);
296 return(RelinquishMagickMemory(p));
299 static SplayTreeInfo *AcquireCoderCache(const char *filename,
300 ExceptionInfo *exception)
318 Load external coder map.
320 coder_cache=NewSplayTree(CompareSplayTreeString,RelinquishMagickMemory,
322 if (coder_cache == (SplayTreeInfo *) NULL)
323 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
325 options=GetConfigureOptions(filename,exception);
326 option=(const StringInfo *) GetNextValueInLinkedList(options);
327 while (option != (const StringInfo *) NULL)
329 status&=LoadCoderCache(coder_cache,(const char *)
330 GetStringInfoDatum(option),GetStringInfoPath(option),0,exception);
331 option=(const StringInfo *) GetNextValueInLinkedList(options);
333 options=DestroyConfigureOptions(options);
335 Load built-in coder map.
337 for (i=0; i < (ssize_t) (sizeof(CoderMap)/sizeof(*CoderMap)); i++)
342 register const CoderMapInfo
346 coder_info=(CoderInfo *) AcquireMagickMemory(sizeof(*coder_info));
347 if (coder_info == (CoderInfo *) NULL)
349 (void) ThrowMagickException(exception,GetMagickModule(),
350 ResourceLimitError,"MemoryAllocationFailed","`%s'",p->name);
353 (void) ResetMagickMemory(coder_info,0,sizeof(*coder_info));
354 coder_info->path=(char *) "[built-in]";
355 coder_info->magick=(char *) p->magick;
356 coder_info->name=(char *) p->name;
357 coder_info->exempt=MagickTrue;
358 coder_info->signature=MagickSignature;
359 status&=AddValueToSplayTree(coder_cache,ConstantString(coder_info->magick),
361 if (status == MagickFalse)
362 (void) ThrowMagickException(exception,GetMagickModule(),
363 ResourceLimitError,"MemoryAllocationFailed","`%s'",coder_info->name);
369 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
373 + C o d e r C o m p o n e n t G e n e s i s %
377 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
379 % CoderComponentGenesis() instantiates the coder component.
381 % The format of the CoderComponentGenesis method is:
383 % MagickBooleanType CoderComponentGenesis(void)
386 MagickPrivate MagickBooleanType CoderComponentGenesis(void)
388 if (coder_semaphore == (SemaphoreInfo *) NULL)
389 coder_semaphore=AcquireSemaphoreInfo();
394 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
398 + C o d e r C o m p o n e n t T e r m i n u s %
402 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
404 % CoderComponentTerminus() destroys the coder component.
406 % The format of the CoderComponentTerminus method is:
408 % CoderComponentTerminus(void)
411 MagickPrivate void CoderComponentTerminus(void)
413 if (coder_semaphore == (SemaphoreInfo *) NULL)
414 ActivateSemaphoreInfo(&coder_semaphore);
415 LockSemaphoreInfo(coder_semaphore);
416 if (coder_cache != (SplayTreeInfo *) NULL)
417 coder_cache=DestroySplayTree(coder_cache);
418 UnlockSemaphoreInfo(coder_semaphore);
419 RelinquishSemaphoreInfo(&coder_semaphore);
423 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
427 + G e t C o d e r I n f o %
431 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
433 % GetCoderInfo searches the coder list for the specified name and if found
434 % returns attributes for that coder.
436 % The format of the GetCoderInfo method is:
438 % const CoderInfo *GetCoderInfo(const char *name,ExceptionInfo *exception)
440 % A description of each parameter follows:
442 % o name: the coder name.
444 % o exception: return any errors or warnings in this structure.
447 MagickExport const CoderInfo *GetCoderInfo(const char *name,
448 ExceptionInfo *exception)
453 assert(exception != (ExceptionInfo *) NULL);
454 if (IsCoderTreeInstantiated(exception) == MagickFalse)
455 return((const CoderInfo *) NULL);
456 LockSemaphoreInfo(coder_semaphore);
457 if ((name == (const char *) NULL) || (LocaleCompare(name,"*") == 0))
459 ResetSplayTreeIterator(coder_cache);
460 coder_info=(const CoderInfo *) GetNextValueInSplayTree(coder_cache);
461 UnlockSemaphoreInfo(coder_semaphore);
464 coder_info=(const CoderInfo *) GetValueFromSplayTree(coder_cache,name);
465 UnlockSemaphoreInfo(coder_semaphore);
470 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
474 % G e t C o d e r I n f o L i s t %
478 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
480 % GetCoderInfoList() returns any coder_map that match the specified pattern.
481 % The format of the GetCoderInfoList function is:
483 % const CoderInfo **GetCoderInfoList(const char *pattern,
484 % size_t *number_coders,ExceptionInfo *exception)
486 % A description of each parameter follows:
488 % o pattern: Specifies a pointer to a text string containing a pattern.
490 % o number_coders: This integer returns the number of coders in the list.
492 % o exception: return any errors or warnings in this structure.
496 static int CoderInfoCompare(const void *x,const void *y)
502 p=(const CoderInfo **) x,
503 q=(const CoderInfo **) y;
504 if (LocaleCompare((*p)->path,(*q)->path) == 0)
505 return(LocaleCompare((*p)->name,(*q)->name));
506 return(LocaleCompare((*p)->path,(*q)->path));
509 MagickExport const CoderInfo **GetCoderInfoList(const char *pattern,
510 size_t *number_coders,ExceptionInfo *exception)
515 register const CoderInfo
524 assert(pattern != (char *) NULL);
525 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
526 assert(number_coders != (size_t *) NULL);
528 p=GetCoderInfo("*",exception);
529 if (p == (const CoderInfo *) NULL)
530 return((const CoderInfo **) NULL);
531 coder_map=(const CoderInfo **) AcquireQuantumMemory((size_t)
532 GetNumberOfNodesInSplayTree(coder_cache)+1UL,sizeof(*coder_map));
533 if (coder_map == (const CoderInfo **) NULL)
534 return((const CoderInfo **) NULL);
538 LockSemaphoreInfo(coder_semaphore);
539 ResetSplayTreeIterator(coder_cache);
540 p=(const CoderInfo *) GetNextValueInSplayTree(coder_cache);
541 for (i=0; p != (const CoderInfo *) NULL; )
543 if ((p->stealth == MagickFalse) &&
544 (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
546 p=(const CoderInfo *) GetNextValueInSplayTree(coder_cache);
548 UnlockSemaphoreInfo(coder_semaphore);
549 qsort((void *) coder_map,(size_t) i,sizeof(*coder_map),CoderInfoCompare);
550 coder_map[i]=(CoderInfo *) NULL;
551 *number_coders=(size_t) i;
556 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
560 % G e t C o d e r L i s t %
564 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
566 % GetCoderList() returns any coder_map that match the specified pattern.
568 % The format of the GetCoderList function is:
570 % char **GetCoderList(const char *pattern,size_t *number_coders,
571 % ExceptionInfo *exception)
573 % A description of each parameter follows:
575 % o pattern: Specifies a pointer to a text string containing a pattern.
577 % o number_coders: This integer returns the number of coders in the list.
579 % o exception: return any errors or warnings in this structure.
583 static int CoderCompare(const void *x,const void *y)
591 return(LocaleCompare(*p,*q));
594 MagickExport char **GetCoderList(const char *pattern,
595 size_t *number_coders,ExceptionInfo *exception)
600 register const CoderInfo
609 assert(pattern != (char *) NULL);
610 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
611 assert(number_coders != (size_t *) NULL);
613 p=GetCoderInfo("*",exception);
614 if (p == (const CoderInfo *) NULL)
615 return((char **) NULL);
616 coder_map=(char **) AcquireQuantumMemory((size_t)
617 GetNumberOfNodesInSplayTree(coder_cache)+1UL,sizeof(*coder_map));
618 if (coder_map == (char **) NULL)
619 return((char **) NULL);
623 LockSemaphoreInfo(coder_semaphore);
624 ResetSplayTreeIterator(coder_cache);
625 p=(const CoderInfo *) GetNextValueInSplayTree(coder_cache);
626 for (i=0; p != (const CoderInfo *) NULL; )
628 if ((p->stealth == MagickFalse) &&
629 (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
630 coder_map[i++]=ConstantString(p->name);
631 p=(const CoderInfo *) GetNextValueInSplayTree(coder_cache);
633 UnlockSemaphoreInfo(coder_semaphore);
634 qsort((void *) coder_map,(size_t) i,sizeof(*coder_map),CoderCompare);
635 coder_map[i]=(char *) NULL;
636 *number_coders=(size_t) i;
641 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
645 + I s C o d e r T r e e I n s t a n t i a t e d %
649 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
651 % IsCoderTreeInstantiated() determines if the coder tree is instantiated. If
652 % not, it instantiates the tree and returns it.
654 % The format of the IsCoderInstantiated method is:
656 % MagickBooleanType IsCoderTreeInstantiated(ExceptionInfo *exception)
658 % A description of each parameter follows.
660 % o exception: return any errors or warnings in this structure.
663 static MagickBooleanType IsCoderTreeInstantiated(ExceptionInfo *exception)
665 if (coder_cache == (SplayTreeInfo *) NULL)
667 if (coder_semaphore == (SemaphoreInfo *) NULL)
668 ActivateSemaphoreInfo(&coder_semaphore);
669 LockSemaphoreInfo(coder_semaphore);
670 if (coder_cache == (SplayTreeInfo *) NULL)
671 coder_cache=AcquireCoderCache(MagickCoderFilename,exception);
672 UnlockSemaphoreInfo(coder_semaphore);
674 return(coder_cache != (SplayTreeInfo *) NULL ? MagickTrue : MagickFalse);
678 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
682 % L i s t C o d e r I n f o %
686 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
688 % ListCoderInfo() lists the coder info to a file.
690 % The format of the ListCoderInfo coder is:
692 % MagickBooleanType ListCoderInfo(FILE *file,ExceptionInfo *exception)
694 % A description of each parameter follows.
696 % o file: An pointer to a FILE.
698 % o exception: return any errors or warnings in this structure.
701 MagickExport MagickBooleanType ListCoderInfo(FILE *file,
702 ExceptionInfo *exception)
719 if (file == (const FILE *) NULL)
721 coder_info=GetCoderInfoList("*",&number_coders,exception);
722 if (coder_info == (const CoderInfo **) NULL)
724 path=(const char *) NULL;
725 for (i=0; i < (ssize_t) number_coders; i++)
727 if (coder_info[i]->stealth != MagickFalse)
729 if ((path == (const char *) NULL) ||
730 (LocaleCompare(path,coder_info[i]->path) != 0))
732 if (coder_info[i]->path != (char *) NULL)
733 (void) FormatLocaleFile(file,"\nPath: %s\n\n",coder_info[i]->path);
734 (void) FormatLocaleFile(file,"Magick Coder\n");
735 (void) FormatLocaleFile(file,
736 "-------------------------------------------------"
737 "------------------------------\n");
739 path=coder_info[i]->path;
740 (void) FormatLocaleFile(file,"%s",coder_info[i]->magick);
741 for (j=(ssize_t) strlen(coder_info[i]->magick); j <= 11; j++)
742 (void) FormatLocaleFile(file," ");
743 if (coder_info[i]->name != (char *) NULL)
744 (void) FormatLocaleFile(file,"%s",coder_info[i]->name);
745 (void) FormatLocaleFile(file,"\n");
747 coder_info=(const CoderInfo **) RelinquishMagickMemory((void *) coder_info);
753 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
757 + L o a d C o d e r L i s t %
761 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
763 % LoadCoderCache() loads the coder configurations which provides a
764 % mapping between coder attributes and a coder name.
766 % The format of the LoadCoderCache coder is:
768 % MagickBooleanType LoadCoderCache(SplayTreeInfo *coder_cache,
769 % const char *xml,const char *filename,const size_t depth,
770 % ExceptionInfo *exception)
772 % A description of each parameter follows:
774 % o xml: The coder list in XML format.
776 % o filename: The coder list filename.
778 % o depth: depth of <include /> statements.
780 % o exception: return any errors or warnings in this structure.
783 static MagickBooleanType LoadCoderCache(SplayTreeInfo *coder_cache,
784 const char *xml,const char *filename,const size_t depth,
785 ExceptionInfo *exception)
788 keyword[MaxTextExtent],
801 Load the coder map file.
803 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
804 "Loading coder configuration file \"%s\" ...",filename);
805 if (xml == (const char *) NULL)
808 coder_info=(CoderInfo *) NULL;
809 token=AcquireString(xml);
810 for (q=(char *) xml; *q != '\0'; )
815 GetMagickToken(q,&q,token);
818 (void) CopyMagickString(keyword,token,MaxTextExtent);
819 if (LocaleNCompare(keyword,"<!DOCTYPE",9) == 0)
824 while ((LocaleNCompare(q,"]>",2) != 0) && (*q != '\0'))
825 GetMagickToken(q,&q,token);
828 if (LocaleNCompare(keyword,"<!--",4) == 0)
833 while ((LocaleNCompare(q,"->",2) != 0) && (*q != '\0'))
834 GetMagickToken(q,&q,token);
837 if (LocaleCompare(keyword,"<include") == 0)
842 while (((*token != '/') && (*(token+1) != '>')) && (*q != '\0'))
844 (void) CopyMagickString(keyword,token,MaxTextExtent);
845 GetMagickToken(q,&q,token);
848 GetMagickToken(q,&q,token);
849 if (LocaleCompare(keyword,"file") == 0)
852 (void) ThrowMagickException(exception,GetMagickModule(),
853 ConfigureError,"IncludeNodeNestedTooDeeply","`%s'",token);
860 GetPathComponent(filename,HeadPath,path);
862 (void) ConcatenateMagickString(path,DirectorySeparator,
864 if (*token == *DirectorySeparator)
865 (void) CopyMagickString(path,token,MaxTextExtent);
867 (void) ConcatenateMagickString(path,token,MaxTextExtent);
868 xml=FileToXML(path,~0UL);
869 if (xml != (char *) NULL)
871 status&=LoadCoderCache(coder_cache,xml,path,depth+1,
873 xml=(char *) RelinquishMagickMemory(xml);
880 if (LocaleCompare(keyword,"<coder") == 0)
885 coder_info=(CoderInfo *) AcquireMagickMemory(sizeof(*coder_info));
886 if (coder_info == (CoderInfo *) NULL)
887 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
888 (void) ResetMagickMemory(coder_info,0,sizeof(*coder_info));
889 coder_info->path=ConstantString(filename);
890 coder_info->exempt=MagickFalse;
891 coder_info->signature=MagickSignature;
894 if (coder_info == (CoderInfo *) NULL)
896 if (LocaleCompare(keyword,"/>") == 0)
898 status=AddValueToSplayTree(coder_cache,ConstantString(
899 coder_info->magick),coder_info);
900 if (status == MagickFalse)
901 (void) ThrowMagickException(exception,GetMagickModule(),
902 ResourceLimitError,"MemoryAllocationFailed","`%s'",
904 coder_info=(CoderInfo *) NULL;
907 GetMagickToken(q,(const char **) NULL,token);
910 GetMagickToken(q,&q,token);
911 GetMagickToken(q,&q,token);
917 if (LocaleCompare((char *) keyword,"magick") == 0)
919 coder_info->magick=ConstantString(token);
927 if (LocaleCompare((char *) keyword,"name") == 0)
929 coder_info->name=ConstantString(token);
937 if (LocaleCompare((char *) keyword,"stealth") == 0)
939 coder_info->stealth=IsStringTrue(token);
948 token=(char *) RelinquishMagickMemory(token);
949 return(status != 0 ? MagickTrue : MagickFalse);