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"
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" },
142 { "IMPLICIT", "***" },
144 { "IPTCTEXT", "META" },
145 { "IPTCWTEXT", "META" },
146 { "ISOBRL", "BRAILLE" },
155 { "LOGO", "MAGICK" },
168 { "NETSCAPE", "MAGICK" },
195 { "RADIAL-GRADIENT", "GRADIENT" },
202 { "ROSE", "MAGICK" },
205 { "SPARSE-COLOR", "TXT" },
210 { "TIFF64", "TIFF" },
213 { "UBRL", "BRAILLE" },
216 { "WIZARD", "MAGICK" },
217 #if defined(MAGICKCORE_WINGDI32_DELEGATE)
224 { "XTRNARRAY", "XTRN" },
225 { "XTRNBLOB", "XTRN" },
226 { "XTRNFILE", "XTRN" },
227 { "XTRNIMAGE", "XTRN" },
230 { "YCbCrA", "YCbCr" }
234 *coder_semaphore = (SemaphoreInfo *) NULL;
237 *coder_cache = (SplayTreeInfo *) NULL;
240 Forward declarations.
242 static MagickBooleanType
243 IsCoderTreeInstantiated(ExceptionInfo *),
244 LoadCoderCache(SplayTreeInfo *,const char *,const char *,const size_t,
248 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
252 % A c q u i r e C o d e r S p l a y T r e e %
256 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
258 % AcquireCoderCache() caches one or more coder configurations which
259 % provides a mapping between coder attributes and a coder name.
261 % The format of the AcquireCoderCache coder is:
263 % SplayTreeInfo *AcquireCoderCache(const char *filename,
264 % ExceptionInfo *exception)
266 % A description of each parameter follows:
268 % o filename: the font file name.
270 % o exception: return any errors or warnings in this structure.
274 static void *DestroyCoderNode(void *coder_info)
279 p=(CoderInfo *) coder_info;
280 if (p->exempt == MagickFalse)
282 if (p->path != (char *) NULL)
283 p->path=DestroyString(p->path);
284 if (p->name != (char *) NULL)
285 p->name=DestroyString(p->name);
286 if (p->magick != (char *) NULL)
287 p->magick=DestroyString(p->magick);
289 return(RelinquishMagickMemory(p));
292 static SplayTreeInfo *AcquireCoderCache(const char *filename,
293 ExceptionInfo *exception)
311 Load external coder map.
313 coder_cache=NewSplayTree(CompareSplayTreeString,RelinquishMagickMemory,
315 if (coder_cache == (SplayTreeInfo *) NULL)
316 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
318 options=GetConfigureOptions(filename,exception);
319 option=(const StringInfo *) GetNextValueInLinkedList(options);
320 while (option != (const StringInfo *) NULL)
322 status&=LoadCoderCache(coder_cache,(const char *) GetStringInfoDatum(option),
323 GetStringInfoPath(option),0,exception);
324 option=(const StringInfo *) GetNextValueInLinkedList(options);
326 options=DestroyConfigureOptions(options);
328 Load built-in coder map.
330 for (i=0; i < (ssize_t) (sizeof(CoderMap)/sizeof(*CoderMap)); i++)
335 register const CoderMapInfo
339 coder_info=(CoderInfo *) AcquireMagickMemory(sizeof(*coder_info));
340 if (coder_info == (CoderInfo *) NULL)
342 (void) ThrowMagickException(exception,GetMagickModule(),
343 ResourceLimitError,"MemoryAllocationFailed","`%s'",p->name);
346 (void) ResetMagickMemory(coder_info,0,sizeof(*coder_info));
347 coder_info->path=(char *) "[built-in]";
348 coder_info->magick=(char *) p->magick;
349 coder_info->name=(char *) p->name;
350 coder_info->exempt=MagickTrue;
351 coder_info->signature=MagickSignature;
352 status&=AddValueToSplayTree(coder_cache,ConstantString(coder_info->magick),
354 if (status == MagickFalse)
355 (void) ThrowMagickException(exception,GetMagickModule(),
356 ResourceLimitError,"MemoryAllocationFailed","`%s'",coder_info->name);
362 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
366 + C o d e r C o m p o n e n t G e n e s i s %
370 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
372 % CoderComponentGenesis() instantiates the coder component.
374 % The format of the CoderComponentGenesis method is:
376 % MagickBooleanType CoderComponentGenesis(void)
379 MagickPrivate MagickBooleanType CoderComponentGenesis(void)
381 coder_semaphore=AcquireSemaphoreInfo();
386 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
390 + C o d e r C o m p o n e n t T e r m i n u s %
394 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
396 % CoderComponentTerminus() destroys the coder component.
398 % The format of the CoderComponentTerminus method is:
400 % CoderComponentTerminus(void)
403 MagickPrivate void CoderComponentTerminus(void)
405 if (coder_semaphore == (SemaphoreInfo *) NULL)
406 ActivateSemaphoreInfo(&coder_semaphore);
407 LockSemaphoreInfo(coder_semaphore);
408 if (coder_cache != (SplayTreeInfo *) NULL)
409 coder_cache=DestroySplayTree(coder_cache);
410 UnlockSemaphoreInfo(coder_semaphore);
411 RelinquishSemaphoreInfo(&coder_semaphore);
415 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
419 + G e t C o d e r I n f o %
423 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
425 % GetCoderInfo searches the coder list for the specified name and if found
426 % returns attributes for that coder.
428 % The format of the GetCoderInfo method is:
430 % const CoderInfo *GetCoderInfo(const char *name,ExceptionInfo *exception)
432 % A description of each parameter follows:
434 % o name: the coder name.
436 % o exception: return any errors or warnings in this structure.
439 MagickExport const CoderInfo *GetCoderInfo(const char *name,
440 ExceptionInfo *exception)
445 assert(exception != (ExceptionInfo *) NULL);
446 if (IsCoderTreeInstantiated(exception) == MagickFalse)
447 return((const CoderInfo *) NULL);
448 LockSemaphoreInfo(coder_semaphore);
449 if ((name == (const char *) NULL) || (LocaleCompare(name,"*") == 0))
451 ResetSplayTreeIterator(coder_cache);
452 coder_info=(const CoderInfo *) GetNextValueInSplayTree(coder_cache);
453 UnlockSemaphoreInfo(coder_semaphore);
456 coder_info=(const CoderInfo *) GetValueFromSplayTree(coder_cache,name);
457 UnlockSemaphoreInfo(coder_semaphore);
462 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
466 % G e t C o d e r I n f o L i s t %
470 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
472 % GetCoderInfoList() returns any coder_map that match the specified pattern.
473 % The format of the GetCoderInfoList function is:
475 % const CoderInfo **GetCoderInfoList(const char *pattern,
476 % size_t *number_coders,ExceptionInfo *exception)
478 % A description of each parameter follows:
480 % o pattern: Specifies a pointer to a text string containing a pattern.
482 % o number_coders: This integer returns the number of coders in the list.
484 % o exception: return any errors or warnings in this structure.
488 static int CoderInfoCompare(const void *x,const void *y)
494 p=(const CoderInfo **) x,
495 q=(const CoderInfo **) y;
496 if (LocaleCompare((*p)->path,(*q)->path) == 0)
497 return(LocaleCompare((*p)->name,(*q)->name));
498 return(LocaleCompare((*p)->path,(*q)->path));
501 MagickExport const CoderInfo **GetCoderInfoList(const char *pattern,
502 size_t *number_coders,ExceptionInfo *exception)
507 register const CoderInfo
516 assert(pattern != (char *) NULL);
517 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
518 assert(number_coders != (size_t *) NULL);
520 p=GetCoderInfo("*",exception);
521 if (p == (const CoderInfo *) NULL)
522 return((const CoderInfo **) NULL);
523 coder_map=(const CoderInfo **) AcquireQuantumMemory((size_t)
524 GetNumberOfNodesInSplayTree(coder_cache)+1UL,sizeof(*coder_map));
525 if (coder_map == (const CoderInfo **) NULL)
526 return((const CoderInfo **) NULL);
530 LockSemaphoreInfo(coder_semaphore);
531 ResetSplayTreeIterator(coder_cache);
532 p=(const CoderInfo *) GetNextValueInSplayTree(coder_cache);
533 for (i=0; p != (const CoderInfo *) NULL; )
535 if ((p->stealth == MagickFalse) &&
536 (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
538 p=(const CoderInfo *) GetNextValueInSplayTree(coder_cache);
540 UnlockSemaphoreInfo(coder_semaphore);
541 qsort((void *) coder_map,(size_t) i,sizeof(*coder_map),CoderInfoCompare);
542 coder_map[i]=(CoderInfo *) NULL;
543 *number_coders=(size_t) i;
548 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
552 % G e t C o d e r L i s t %
556 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
558 % GetCoderList() returns any coder_map that match the specified pattern.
560 % The format of the GetCoderList function is:
562 % char **GetCoderList(const char *pattern,size_t *number_coders,
563 % ExceptionInfo *exception)
565 % A description of each parameter follows:
567 % o pattern: Specifies a pointer to a text string containing a pattern.
569 % o number_coders: This integer returns the number of coders in the list.
571 % o exception: return any errors or warnings in this structure.
575 static int CoderCompare(const void *x,const void *y)
583 return(LocaleCompare(*p,*q));
586 MagickExport char **GetCoderList(const char *pattern,
587 size_t *number_coders,ExceptionInfo *exception)
592 register const CoderInfo
601 assert(pattern != (char *) NULL);
602 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
603 assert(number_coders != (size_t *) NULL);
605 p=GetCoderInfo("*",exception);
606 if (p == (const CoderInfo *) NULL)
607 return((char **) NULL);
608 coder_map=(char **) AcquireQuantumMemory((size_t)
609 GetNumberOfNodesInSplayTree(coder_cache)+1UL,sizeof(*coder_map));
610 if (coder_map == (char **) NULL)
611 return((char **) NULL);
615 LockSemaphoreInfo(coder_semaphore);
616 ResetSplayTreeIterator(coder_cache);
617 p=(const CoderInfo *) GetNextValueInSplayTree(coder_cache);
618 for (i=0; p != (const CoderInfo *) NULL; )
620 if ((p->stealth == MagickFalse) &&
621 (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
622 coder_map[i++]=ConstantString(p->name);
623 p=(const CoderInfo *) GetNextValueInSplayTree(coder_cache);
625 UnlockSemaphoreInfo(coder_semaphore);
626 qsort((void *) coder_map,(size_t) i,sizeof(*coder_map),CoderCompare);
627 coder_map[i]=(char *) NULL;
628 *number_coders=(size_t) i;
633 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
637 + I s C o d e r T r e e I n s t a n t i a t e d %
641 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
643 % IsCoderTreeInstantiated() determines if the coder tree is instantiated.
644 % If not, it instantiates the tree and returns it.
646 % The format of the IsCoderInstantiated method is:
648 % MagickBooleanType IsCoderTreeInstantiated(ExceptionInfo *exception)
650 % A description of each parameter follows.
652 % o exception: return any errors or warnings in this structure.
655 static MagickBooleanType IsCoderTreeInstantiated(ExceptionInfo *exception)
657 if (coder_cache == (SplayTreeInfo *) NULL)
659 if (coder_semaphore == (SemaphoreInfo *) NULL)
660 ActivateSemaphoreInfo(&coder_semaphore);
661 LockSemaphoreInfo(coder_semaphore);
662 if (coder_cache == (SplayTreeInfo *) NULL)
663 (void) AcquireCoderCache(MagickCoderFilename,exception);
664 UnlockSemaphoreInfo(coder_semaphore);
666 return(coder_cache != (SplayTreeInfo *) NULL ? MagickTrue : MagickFalse);
670 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
674 % L i s t C o d e r I n f o %
678 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
680 % ListCoderInfo() lists the coder info to a file.
682 % The format of the ListCoderInfo coder is:
684 % MagickBooleanType ListCoderInfo(FILE *file,ExceptionInfo *exception)
686 % A description of each parameter follows.
688 % o file: An pointer to a FILE.
690 % o exception: return any errors or warnings in this structure.
693 MagickExport MagickBooleanType ListCoderInfo(FILE *file,
694 ExceptionInfo *exception)
711 if (file == (const FILE *) NULL)
713 coder_info=GetCoderInfoList("*",&number_coders,exception);
714 if (coder_info == (const CoderInfo **) NULL)
716 path=(const char *) NULL;
717 for (i=0; i < (ssize_t) number_coders; i++)
719 if (coder_info[i]->stealth != MagickFalse)
721 if ((path == (const char *) NULL) ||
722 (LocaleCompare(path,coder_info[i]->path) != 0))
724 if (coder_info[i]->path != (char *) NULL)
725 (void) FormatLocaleFile(file,"\nPath: %s\n\n",coder_info[i]->path);
726 (void) FormatLocaleFile(file,"Magick Coder\n");
727 (void) FormatLocaleFile(file,
728 "-------------------------------------------------"
729 "------------------------------\n");
731 path=coder_info[i]->path;
732 (void) FormatLocaleFile(file,"%s",coder_info[i]->magick);
733 for (j=(ssize_t) strlen(coder_info[i]->magick); j <= 11; j++)
734 (void) FormatLocaleFile(file," ");
735 if (coder_info[i]->name != (char *) NULL)
736 (void) FormatLocaleFile(file,"%s",coder_info[i]->name);
737 (void) FormatLocaleFile(file,"\n");
739 coder_info=(const CoderInfo **) RelinquishMagickMemory((void *) coder_info);
745 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
749 + L o a d C o d e r L i s t %
753 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
755 % LoadCoderCache() loads the coder configurations which provides a
756 % mapping between coder attributes and a coder name.
758 % The format of the LoadCoderCache coder is:
760 % MagickBooleanType LoadCoderCache(SplayTreeInfo *coder_cache,
761 % const char *xml,const char *filename,const size_t depth,
762 % ExceptionInfo *exception)
764 % A description of each parameter follows:
766 % o xml: The coder list in XML format.
768 % o filename: The coder list filename.
770 % o depth: depth of <include /> statements.
772 % o exception: return any errors or warnings in this structure.
775 static MagickBooleanType LoadCoderCache(SplayTreeInfo *coder_cache,
776 const char *xml,const char *filename,const size_t depth,
777 ExceptionInfo *exception)
780 keyword[MaxTextExtent],
793 Load the coder map file.
795 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
796 "Loading coder configuration file \"%s\" ...",filename);
797 if (xml == (const char *) NULL)
800 coder_info=(CoderInfo *) NULL;
801 token=AcquireString(xml);
802 for (q=(char *) xml; *q != '\0'; )
807 GetMagickToken(q,&q,token);
810 (void) CopyMagickString(keyword,token,MaxTextExtent);
811 if (LocaleNCompare(keyword,"<!DOCTYPE",9) == 0)
816 while ((LocaleNCompare(q,"]>",2) != 0) && (*q != '\0'))
817 GetMagickToken(q,&q,token);
820 if (LocaleNCompare(keyword,"<!--",4) == 0)
825 while ((LocaleNCompare(q,"->",2) != 0) && (*q != '\0'))
826 GetMagickToken(q,&q,token);
829 if (LocaleCompare(keyword,"<include") == 0)
834 while (((*token != '/') && (*(token+1) != '>')) && (*q != '\0'))
836 (void) CopyMagickString(keyword,token,MaxTextExtent);
837 GetMagickToken(q,&q,token);
840 GetMagickToken(q,&q,token);
841 if (LocaleCompare(keyword,"file") == 0)
844 (void) ThrowMagickException(exception,GetMagickModule(),
845 ConfigureError,"IncludeNodeNestedTooDeeply","`%s'",token);
852 GetPathComponent(filename,HeadPath,path);
854 (void) ConcatenateMagickString(path,DirectorySeparator,
856 if (*token == *DirectorySeparator)
857 (void) CopyMagickString(path,token,MaxTextExtent);
859 (void) ConcatenateMagickString(path,token,MaxTextExtent);
860 xml=FileToString(path,~0UL,exception);
861 if (xml != (char *) NULL)
863 status&=LoadCoderCache(coder_cache,xml,path,depth+1,
865 xml=(char *) RelinquishMagickMemory(xml);
872 if (LocaleCompare(keyword,"<coder") == 0)
877 coder_info=(CoderInfo *) AcquireMagickMemory(sizeof(*coder_info));
878 if (coder_info == (CoderInfo *) NULL)
879 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
880 (void) ResetMagickMemory(coder_info,0,sizeof(*coder_info));
881 coder_info->path=ConstantString(filename);
882 coder_info->exempt=MagickFalse;
883 coder_info->signature=MagickSignature;
886 if (coder_info == (CoderInfo *) NULL)
888 if (LocaleCompare(keyword,"/>") == 0)
890 status=AddValueToSplayTree(coder_cache,ConstantString(
891 coder_info->magick),coder_info);
892 if (status == MagickFalse)
893 (void) ThrowMagickException(exception,GetMagickModule(),
894 ResourceLimitError,"MemoryAllocationFailed","`%s'",
896 coder_info=(CoderInfo *) NULL;
899 GetMagickToken(q,(const char **) NULL,token);
902 GetMagickToken(q,&q,token);
903 GetMagickToken(q,&q,token);
909 if (LocaleCompare((char *) keyword,"magick") == 0)
911 coder_info->magick=ConstantString(token);
919 if (LocaleCompare((char *) keyword,"name") == 0)
921 coder_info->name=ConstantString(token);
929 if (LocaleCompare((char *) keyword,"stealth") == 0)
931 coder_info->stealth=IsStringTrue(token);
940 token=(char *) RelinquishMagickMemory(token);