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" },
204 { "ROSE", "MAGICK" },
207 { "SPARSE-COLOR", "TXT" },
212 { "TIFF64", "TIFF" },
215 { "UBRL", "BRAILLE" },
218 { "WIZARD", "MAGICK" },
219 #if defined(MAGICKCORE_WINGDI32_DELEGATE)
226 { "XTRNARRAY", "XTRN" },
227 { "XTRNBLOB", "XTRN" },
228 { "XTRNFILE", "XTRN" },
229 { "XTRNIMAGE", "XTRN" },
232 { "YCbCrA", "YCbCr" }
236 *coder_semaphore = (SemaphoreInfo *) NULL;
239 *coder_cache = (SplayTreeInfo *) NULL;
242 Forward declarations.
244 static MagickBooleanType
245 IsCoderTreeInstantiated(ExceptionInfo *),
246 LoadCoderCache(SplayTreeInfo *,const char *,const char *,const size_t,
250 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
254 + A c q u i r e C o d e r C a c h e %
258 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
260 % AcquireCoderCache() caches one or more coder configurations which provides a
261 % mapping between coder attributes and a coder name.
263 % The format of the AcquireCoderCache coder is:
265 % SplayTreeInfo *AcquireCoderCache(const char *filename,
266 % ExceptionInfo *exception)
268 % A description of each parameter follows:
270 % o filename: the font file name.
272 % o exception: return any errors or warnings in this structure.
276 static void *DestroyCoderNode(void *coder_info)
281 p=(CoderInfo *) coder_info;
282 if (p->exempt == MagickFalse)
284 if (p->path != (char *) NULL)
285 p->path=DestroyString(p->path);
286 if (p->name != (char *) NULL)
287 p->name=DestroyString(p->name);
288 if (p->magick != (char *) NULL)
289 p->magick=DestroyString(p->magick);
291 return(RelinquishMagickMemory(p));
294 static SplayTreeInfo *AcquireCoderCache(const char *filename,
295 ExceptionInfo *exception)
313 Load external coder map.
315 coder_cache=NewSplayTree(CompareSplayTreeString,RelinquishMagickMemory,
317 if (coder_cache == (SplayTreeInfo *) NULL)
318 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
320 options=GetConfigureOptions(filename,exception);
321 option=(const StringInfo *) GetNextValueInLinkedList(options);
322 while (option != (const StringInfo *) NULL)
324 status&=LoadCoderCache(coder_cache,(const char *)
325 GetStringInfoDatum(option),GetStringInfoPath(option),0,exception);
326 option=(const StringInfo *) GetNextValueInLinkedList(options);
328 options=DestroyConfigureOptions(options);
330 Load built-in coder map.
332 for (i=0; i < (ssize_t) (sizeof(CoderMap)/sizeof(*CoderMap)); i++)
337 register const CoderMapInfo
341 coder_info=(CoderInfo *) AcquireMagickMemory(sizeof(*coder_info));
342 if (coder_info == (CoderInfo *) NULL)
344 (void) ThrowMagickException(exception,GetMagickModule(),
345 ResourceLimitError,"MemoryAllocationFailed","`%s'",p->name);
348 (void) ResetMagickMemory(coder_info,0,sizeof(*coder_info));
349 coder_info->path=(char *) "[built-in]";
350 coder_info->magick=(char *) p->magick;
351 coder_info->name=(char *) p->name;
352 coder_info->exempt=MagickTrue;
353 coder_info->signature=MagickSignature;
354 status&=AddValueToSplayTree(coder_cache,ConstantString(coder_info->magick),
356 if (status == MagickFalse)
357 (void) ThrowMagickException(exception,GetMagickModule(),
358 ResourceLimitError,"MemoryAllocationFailed","`%s'",coder_info->name);
364 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
368 + C o d e r C o m p o n e n t G e n e s i s %
372 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
374 % CoderComponentGenesis() instantiates the coder component.
376 % The format of the CoderComponentGenesis method is:
378 % MagickBooleanType CoderComponentGenesis(void)
381 MagickPrivate MagickBooleanType CoderComponentGenesis(void)
383 if (coder_semaphore == (SemaphoreInfo *) NULL)
384 coder_semaphore=AcquireSemaphoreInfo();
389 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
393 + C o d e r C o m p o n e n t T e r m i n u s %
397 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
399 % CoderComponentTerminus() destroys the coder component.
401 % The format of the CoderComponentTerminus method is:
403 % CoderComponentTerminus(void)
406 MagickPrivate void CoderComponentTerminus(void)
408 if (coder_semaphore == (SemaphoreInfo *) NULL)
409 ActivateSemaphoreInfo(&coder_semaphore);
410 LockSemaphoreInfo(coder_semaphore);
411 if (coder_cache != (SplayTreeInfo *) NULL)
412 coder_cache=DestroySplayTree(coder_cache);
413 UnlockSemaphoreInfo(coder_semaphore);
414 RelinquishSemaphoreInfo(&coder_semaphore);
418 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
422 + G e t C o d e r I n f o %
426 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
428 % GetCoderInfo searches the coder list for the specified name and if found
429 % returns attributes for that coder.
431 % The format of the GetCoderInfo method is:
433 % const CoderInfo *GetCoderInfo(const char *name,ExceptionInfo *exception)
435 % A description of each parameter follows:
437 % o name: the coder name.
439 % o exception: return any errors or warnings in this structure.
442 MagickExport const CoderInfo *GetCoderInfo(const char *name,
443 ExceptionInfo *exception)
448 assert(exception != (ExceptionInfo *) NULL);
449 if (IsCoderTreeInstantiated(exception) == MagickFalse)
450 return((const CoderInfo *) NULL);
451 LockSemaphoreInfo(coder_semaphore);
452 if ((name == (const char *) NULL) || (LocaleCompare(name,"*") == 0))
454 ResetSplayTreeIterator(coder_cache);
455 coder_info=(const CoderInfo *) GetNextValueInSplayTree(coder_cache);
456 UnlockSemaphoreInfo(coder_semaphore);
459 coder_info=(const CoderInfo *) GetValueFromSplayTree(coder_cache,name);
460 UnlockSemaphoreInfo(coder_semaphore);
465 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
469 % G e t C o d e r I n f o L i s t %
473 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
475 % GetCoderInfoList() returns any coder_map that match the specified pattern.
476 % The format of the GetCoderInfoList function is:
478 % const CoderInfo **GetCoderInfoList(const char *pattern,
479 % size_t *number_coders,ExceptionInfo *exception)
481 % A description of each parameter follows:
483 % o pattern: Specifies a pointer to a text string containing a pattern.
485 % o number_coders: This integer returns the number of coders in the list.
487 % o exception: return any errors or warnings in this structure.
491 static int CoderInfoCompare(const void *x,const void *y)
497 p=(const CoderInfo **) x,
498 q=(const CoderInfo **) y;
499 if (LocaleCompare((*p)->path,(*q)->path) == 0)
500 return(LocaleCompare((*p)->name,(*q)->name));
501 return(LocaleCompare((*p)->path,(*q)->path));
504 MagickExport const CoderInfo **GetCoderInfoList(const char *pattern,
505 size_t *number_coders,ExceptionInfo *exception)
510 register const CoderInfo
519 assert(pattern != (char *) NULL);
520 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
521 assert(number_coders != (size_t *) NULL);
523 p=GetCoderInfo("*",exception);
524 if (p == (const CoderInfo *) NULL)
525 return((const CoderInfo **) NULL);
526 coder_map=(const CoderInfo **) AcquireQuantumMemory((size_t)
527 GetNumberOfNodesInSplayTree(coder_cache)+1UL,sizeof(*coder_map));
528 if (coder_map == (const CoderInfo **) NULL)
529 return((const CoderInfo **) NULL);
533 LockSemaphoreInfo(coder_semaphore);
534 ResetSplayTreeIterator(coder_cache);
535 p=(const CoderInfo *) GetNextValueInSplayTree(coder_cache);
536 for (i=0; p != (const CoderInfo *) NULL; )
538 if ((p->stealth == MagickFalse) &&
539 (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
541 p=(const CoderInfo *) GetNextValueInSplayTree(coder_cache);
543 UnlockSemaphoreInfo(coder_semaphore);
544 qsort((void *) coder_map,(size_t) i,sizeof(*coder_map),CoderInfoCompare);
545 coder_map[i]=(CoderInfo *) NULL;
546 *number_coders=(size_t) i;
551 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
555 % G e t C o d e r L i s t %
559 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
561 % GetCoderList() returns any coder_map that match the specified pattern.
563 % The format of the GetCoderList function is:
565 % char **GetCoderList(const char *pattern,size_t *number_coders,
566 % ExceptionInfo *exception)
568 % A description of each parameter follows:
570 % o pattern: Specifies a pointer to a text string containing a pattern.
572 % o number_coders: This integer returns the number of coders in the list.
574 % o exception: return any errors or warnings in this structure.
578 static int CoderCompare(const void *x,const void *y)
586 return(LocaleCompare(*p,*q));
589 MagickExport char **GetCoderList(const char *pattern,
590 size_t *number_coders,ExceptionInfo *exception)
595 register const CoderInfo
604 assert(pattern != (char *) NULL);
605 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
606 assert(number_coders != (size_t *) NULL);
608 p=GetCoderInfo("*",exception);
609 if (p == (const CoderInfo *) NULL)
610 return((char **) NULL);
611 coder_map=(char **) AcquireQuantumMemory((size_t)
612 GetNumberOfNodesInSplayTree(coder_cache)+1UL,sizeof(*coder_map));
613 if (coder_map == (char **) NULL)
614 return((char **) NULL);
618 LockSemaphoreInfo(coder_semaphore);
619 ResetSplayTreeIterator(coder_cache);
620 p=(const CoderInfo *) GetNextValueInSplayTree(coder_cache);
621 for (i=0; p != (const CoderInfo *) NULL; )
623 if ((p->stealth == MagickFalse) &&
624 (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
625 coder_map[i++]=ConstantString(p->name);
626 p=(const CoderInfo *) GetNextValueInSplayTree(coder_cache);
628 UnlockSemaphoreInfo(coder_semaphore);
629 qsort((void *) coder_map,(size_t) i,sizeof(*coder_map),CoderCompare);
630 coder_map[i]=(char *) NULL;
631 *number_coders=(size_t) i;
636 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
640 + I s C o d e r T r e e I n s t a n t i a t e d %
644 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
646 % IsCoderTreeInstantiated() determines if the coder tree is instantiated. If
647 % not, it instantiates the tree and returns it.
649 % The format of the IsCoderInstantiated method is:
651 % MagickBooleanType IsCoderTreeInstantiated(ExceptionInfo *exception)
653 % A description of each parameter follows.
655 % o exception: return any errors or warnings in this structure.
658 static MagickBooleanType IsCoderTreeInstantiated(ExceptionInfo *exception)
660 if (coder_cache == (SplayTreeInfo *) NULL)
662 if (coder_semaphore == (SemaphoreInfo *) NULL)
663 ActivateSemaphoreInfo(&coder_semaphore);
664 LockSemaphoreInfo(coder_semaphore);
665 if (coder_cache == (SplayTreeInfo *) NULL)
666 coder_cache=AcquireCoderCache(MagickCoderFilename,exception);
667 UnlockSemaphoreInfo(coder_semaphore);
669 return(coder_cache != (SplayTreeInfo *) NULL ? MagickTrue : MagickFalse);
673 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
677 % L i s t C o d e r I n f o %
681 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
683 % ListCoderInfo() lists the coder info to a file.
685 % The format of the ListCoderInfo coder is:
687 % MagickBooleanType ListCoderInfo(FILE *file,ExceptionInfo *exception)
689 % A description of each parameter follows.
691 % o file: An pointer to a FILE.
693 % o exception: return any errors or warnings in this structure.
696 MagickExport MagickBooleanType ListCoderInfo(FILE *file,
697 ExceptionInfo *exception)
714 if (file == (const FILE *) NULL)
716 coder_info=GetCoderInfoList("*",&number_coders,exception);
717 if (coder_info == (const CoderInfo **) NULL)
719 path=(const char *) NULL;
720 for (i=0; i < (ssize_t) number_coders; i++)
722 if (coder_info[i]->stealth != MagickFalse)
724 if ((path == (const char *) NULL) ||
725 (LocaleCompare(path,coder_info[i]->path) != 0))
727 if (coder_info[i]->path != (char *) NULL)
728 (void) FormatLocaleFile(file,"\nPath: %s\n\n",coder_info[i]->path);
729 (void) FormatLocaleFile(file,"Magick Coder\n");
730 (void) FormatLocaleFile(file,
731 "-------------------------------------------------"
732 "------------------------------\n");
734 path=coder_info[i]->path;
735 (void) FormatLocaleFile(file,"%s",coder_info[i]->magick);
736 for (j=(ssize_t) strlen(coder_info[i]->magick); j <= 11; j++)
737 (void) FormatLocaleFile(file," ");
738 if (coder_info[i]->name != (char *) NULL)
739 (void) FormatLocaleFile(file,"%s",coder_info[i]->name);
740 (void) FormatLocaleFile(file,"\n");
742 coder_info=(const CoderInfo **) RelinquishMagickMemory((void *) coder_info);
748 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
752 + L o a d C o d e r L i s t %
756 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
758 % LoadCoderCache() loads the coder configurations which provides a
759 % mapping between coder attributes and a coder name.
761 % The format of the LoadCoderCache coder is:
763 % MagickBooleanType LoadCoderCache(SplayTreeInfo *coder_cache,
764 % const char *xml,const char *filename,const size_t depth,
765 % ExceptionInfo *exception)
767 % A description of each parameter follows:
769 % o xml: The coder list in XML format.
771 % o filename: The coder list filename.
773 % o depth: depth of <include /> statements.
775 % o exception: return any errors or warnings in this structure.
778 static MagickBooleanType LoadCoderCache(SplayTreeInfo *coder_cache,
779 const char *xml,const char *filename,const size_t depth,
780 ExceptionInfo *exception)
783 keyword[MaxTextExtent],
796 Load the coder map file.
798 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
799 "Loading coder configuration file \"%s\" ...",filename);
800 if (xml == (const char *) NULL)
803 coder_info=(CoderInfo *) NULL;
804 token=AcquireString(xml);
805 for (q=(char *) xml; *q != '\0'; )
810 GetMagickToken(q,&q,token);
813 (void) CopyMagickString(keyword,token,MaxTextExtent);
814 if (LocaleNCompare(keyword,"<!DOCTYPE",9) == 0)
819 while ((LocaleNCompare(q,"]>",2) != 0) && (*q != '\0'))
820 GetMagickToken(q,&q,token);
823 if (LocaleNCompare(keyword,"<!--",4) == 0)
828 while ((LocaleNCompare(q,"->",2) != 0) && (*q != '\0'))
829 GetMagickToken(q,&q,token);
832 if (LocaleCompare(keyword,"<include") == 0)
837 while (((*token != '/') && (*(token+1) != '>')) && (*q != '\0'))
839 (void) CopyMagickString(keyword,token,MaxTextExtent);
840 GetMagickToken(q,&q,token);
843 GetMagickToken(q,&q,token);
844 if (LocaleCompare(keyword,"file") == 0)
847 (void) ThrowMagickException(exception,GetMagickModule(),
848 ConfigureError,"IncludeNodeNestedTooDeeply","`%s'",token);
855 GetPathComponent(filename,HeadPath,path);
857 (void) ConcatenateMagickString(path,DirectorySeparator,
859 if (*token == *DirectorySeparator)
860 (void) CopyMagickString(path,token,MaxTextExtent);
862 (void) ConcatenateMagickString(path,token,MaxTextExtent);
863 xml=FileToXML(path,~0UL);
864 if (xml != (char *) NULL)
866 status&=LoadCoderCache(coder_cache,xml,path,depth+1,
868 xml=(char *) RelinquishMagickMemory(xml);
875 if (LocaleCompare(keyword,"<coder") == 0)
880 coder_info=(CoderInfo *) AcquireMagickMemory(sizeof(*coder_info));
881 if (coder_info == (CoderInfo *) NULL)
882 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
883 (void) ResetMagickMemory(coder_info,0,sizeof(*coder_info));
884 coder_info->path=ConstantString(filename);
885 coder_info->exempt=MagickFalse;
886 coder_info->signature=MagickSignature;
889 if (coder_info == (CoderInfo *) NULL)
891 if (LocaleCompare(keyword,"/>") == 0)
893 status=AddValueToSplayTree(coder_cache,ConstantString(
894 coder_info->magick),coder_info);
895 if (status == MagickFalse)
896 (void) ThrowMagickException(exception,GetMagickModule(),
897 ResourceLimitError,"MemoryAllocationFailed","`%s'",
899 coder_info=(CoderInfo *) NULL;
902 GetMagickToken(q,(const char **) NULL,token);
905 GetMagickToken(q,&q,token);
906 GetMagickToken(q,&q,token);
912 if (LocaleCompare((char *) keyword,"magick") == 0)
914 coder_info->magick=ConstantString(token);
922 if (LocaleCompare((char *) keyword,"name") == 0)
924 coder_info->name=ConstantString(token);
932 if (LocaleCompare((char *) keyword,"stealth") == 0)
934 coder_info->stealth=IsStringTrue(token);
943 token=(char *) RelinquishMagickMemory(token);
944 return(status != 0 ? MagickTrue : MagickFalse);