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" },
161 { "LOGO", "MAGICK" },
175 { "NETSCAPE", "MAGICK" },
202 { "RADIAL-GRADIENT", "GRADIENT" },
210 { "ROSE", "MAGICK" },
214 { "SPARSE-COLOR", "TXT" },
219 { "TIFF64", "TIFF" },
222 { "UBRL", "BRAILLE" },
225 { "WIZARD", "MAGICK" },
226 #if defined(MAGICKCORE_WINGDI32_DELEGATE)
233 { "XTRNARRAY", "XTRN" },
234 { "XTRNBLOB", "XTRN" },
235 { "XTRNFILE", "XTRN" },
236 { "XTRNIMAGE", "XTRN" },
239 { "YCbCrA", "YCbCr" }
243 *coder_semaphore = (SemaphoreInfo *) NULL;
246 *coder_cache = (SplayTreeInfo *) NULL;
249 Forward declarations.
251 static MagickBooleanType
252 IsCoderTreeInstantiated(ExceptionInfo *),
253 LoadCoderCache(SplayTreeInfo *,const char *,const char *,const size_t,
257 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
261 + A c q u i r e C o d e r C a c h e %
265 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
267 % AcquireCoderCache() caches one or more coder configurations which provides a
268 % mapping between coder attributes and a coder name.
270 % The format of the AcquireCoderCache coder is:
272 % SplayTreeInfo *AcquireCoderCache(const char *filename,
273 % ExceptionInfo *exception)
275 % A description of each parameter follows:
277 % o filename: the font file name.
279 % o exception: return any errors or warnings in this structure.
283 static void *DestroyCoderNode(void *coder_info)
288 p=(CoderInfo *) coder_info;
289 if (p->exempt == MagickFalse)
291 if (p->path != (char *) NULL)
292 p->path=DestroyString(p->path);
293 if (p->name != (char *) NULL)
294 p->name=DestroyString(p->name);
295 if (p->magick != (char *) NULL)
296 p->magick=DestroyString(p->magick);
298 return(RelinquishMagickMemory(p));
301 static SplayTreeInfo *AcquireCoderCache(const char *filename,
302 ExceptionInfo *exception)
320 Load external coder map.
322 coder_cache=NewSplayTree(CompareSplayTreeString,RelinquishMagickMemory,
324 if (coder_cache == (SplayTreeInfo *) NULL)
325 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
327 options=GetConfigureOptions(filename,exception);
328 option=(const StringInfo *) GetNextValueInLinkedList(options);
329 while (option != (const StringInfo *) NULL)
331 status&=LoadCoderCache(coder_cache,(const char *)
332 GetStringInfoDatum(option),GetStringInfoPath(option),0,exception);
333 option=(const StringInfo *) GetNextValueInLinkedList(options);
335 options=DestroyConfigureOptions(options);
337 Load built-in coder map.
339 for (i=0; i < (ssize_t) (sizeof(CoderMap)/sizeof(*CoderMap)); i++)
344 register const CoderMapInfo
348 coder_info=(CoderInfo *) AcquireMagickMemory(sizeof(*coder_info));
349 if (coder_info == (CoderInfo *) NULL)
351 (void) ThrowMagickException(exception,GetMagickModule(),
352 ResourceLimitError,"MemoryAllocationFailed","`%s'",p->name);
355 (void) ResetMagickMemory(coder_info,0,sizeof(*coder_info));
356 coder_info->path=(char *) "[built-in]";
357 coder_info->magick=(char *) p->magick;
358 coder_info->name=(char *) p->name;
359 coder_info->exempt=MagickTrue;
360 coder_info->signature=MagickSignature;
361 status&=AddValueToSplayTree(coder_cache,ConstantString(coder_info->magick),
363 if (status == MagickFalse)
364 (void) ThrowMagickException(exception,GetMagickModule(),
365 ResourceLimitError,"MemoryAllocationFailed","`%s'",coder_info->name);
371 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
375 + C o d e r C o m p o n e n t G e n e s i s %
379 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
381 % CoderComponentGenesis() instantiates the coder component.
383 % The format of the CoderComponentGenesis method is:
385 % MagickBooleanType CoderComponentGenesis(void)
388 MagickPrivate MagickBooleanType CoderComponentGenesis(void)
390 if (coder_semaphore == (SemaphoreInfo *) NULL)
391 coder_semaphore=AcquireSemaphoreInfo();
396 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
400 + C o d e r C o m p o n e n t T e r m i n u s %
404 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
406 % CoderComponentTerminus() destroys the coder component.
408 % The format of the CoderComponentTerminus method is:
410 % CoderComponentTerminus(void)
413 MagickPrivate void CoderComponentTerminus(void)
415 if (coder_semaphore == (SemaphoreInfo *) NULL)
416 ActivateSemaphoreInfo(&coder_semaphore);
417 LockSemaphoreInfo(coder_semaphore);
418 if (coder_cache != (SplayTreeInfo *) NULL)
419 coder_cache=DestroySplayTree(coder_cache);
420 UnlockSemaphoreInfo(coder_semaphore);
421 RelinquishSemaphoreInfo(&coder_semaphore);
425 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
429 + G e t C o d e r I n f o %
433 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
435 % GetCoderInfo searches the coder list for the specified name and if found
436 % returns attributes for that coder.
438 % The format of the GetCoderInfo method is:
440 % const CoderInfo *GetCoderInfo(const char *name,ExceptionInfo *exception)
442 % A description of each parameter follows:
444 % o name: the coder name.
446 % o exception: return any errors or warnings in this structure.
449 MagickExport const CoderInfo *GetCoderInfo(const char *name,
450 ExceptionInfo *exception)
455 assert(exception != (ExceptionInfo *) NULL);
456 if (IsCoderTreeInstantiated(exception) == MagickFalse)
457 return((const CoderInfo *) NULL);
458 LockSemaphoreInfo(coder_semaphore);
459 if ((name == (const char *) NULL) || (LocaleCompare(name,"*") == 0))
461 ResetSplayTreeIterator(coder_cache);
462 coder_info=(const CoderInfo *) GetNextValueInSplayTree(coder_cache);
463 UnlockSemaphoreInfo(coder_semaphore);
466 coder_info=(const CoderInfo *) GetValueFromSplayTree(coder_cache,name);
467 UnlockSemaphoreInfo(coder_semaphore);
472 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
476 % G e t C o d e r I n f o L i s t %
480 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
482 % GetCoderInfoList() returns any coder_map that match the specified pattern.
483 % The format of the GetCoderInfoList function is:
485 % const CoderInfo **GetCoderInfoList(const char *pattern,
486 % size_t *number_coders,ExceptionInfo *exception)
488 % A description of each parameter follows:
490 % o pattern: Specifies a pointer to a text string containing a pattern.
492 % o number_coders: This integer returns the number of coders in the list.
494 % o exception: return any errors or warnings in this structure.
498 static int CoderInfoCompare(const void *x,const void *y)
504 p=(const CoderInfo **) x,
505 q=(const CoderInfo **) y;
506 if (LocaleCompare((*p)->path,(*q)->path) == 0)
507 return(LocaleCompare((*p)->name,(*q)->name));
508 return(LocaleCompare((*p)->path,(*q)->path));
511 MagickExport const CoderInfo **GetCoderInfoList(const char *pattern,
512 size_t *number_coders,ExceptionInfo *exception)
517 register const CoderInfo
526 assert(pattern != (char *) NULL);
527 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
528 assert(number_coders != (size_t *) NULL);
530 p=GetCoderInfo("*",exception);
531 if (p == (const CoderInfo *) NULL)
532 return((const CoderInfo **) NULL);
533 coder_map=(const CoderInfo **) AcquireQuantumMemory((size_t)
534 GetNumberOfNodesInSplayTree(coder_cache)+1UL,sizeof(*coder_map));
535 if (coder_map == (const CoderInfo **) NULL)
536 return((const CoderInfo **) NULL);
540 LockSemaphoreInfo(coder_semaphore);
541 ResetSplayTreeIterator(coder_cache);
542 p=(const CoderInfo *) GetNextValueInSplayTree(coder_cache);
543 for (i=0; p != (const CoderInfo *) NULL; )
545 if ((p->stealth == MagickFalse) &&
546 (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
548 p=(const CoderInfo *) GetNextValueInSplayTree(coder_cache);
550 UnlockSemaphoreInfo(coder_semaphore);
551 qsort((void *) coder_map,(size_t) i,sizeof(*coder_map),CoderInfoCompare);
552 coder_map[i]=(CoderInfo *) NULL;
553 *number_coders=(size_t) i;
558 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
562 % G e t C o d e r L i s t %
566 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
568 % GetCoderList() returns any coder_map that match the specified pattern.
570 % The format of the GetCoderList function is:
572 % char **GetCoderList(const char *pattern,size_t *number_coders,
573 % ExceptionInfo *exception)
575 % A description of each parameter follows:
577 % o pattern: Specifies a pointer to a text string containing a pattern.
579 % o number_coders: This integer returns the number of coders in the list.
581 % o exception: return any errors or warnings in this structure.
585 static int CoderCompare(const void *x,const void *y)
593 return(LocaleCompare(*p,*q));
596 MagickExport char **GetCoderList(const char *pattern,
597 size_t *number_coders,ExceptionInfo *exception)
602 register const CoderInfo
611 assert(pattern != (char *) NULL);
612 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
613 assert(number_coders != (size_t *) NULL);
615 p=GetCoderInfo("*",exception);
616 if (p == (const CoderInfo *) NULL)
617 return((char **) NULL);
618 coder_map=(char **) AcquireQuantumMemory((size_t)
619 GetNumberOfNodesInSplayTree(coder_cache)+1UL,sizeof(*coder_map));
620 if (coder_map == (char **) NULL)
621 return((char **) NULL);
625 LockSemaphoreInfo(coder_semaphore);
626 ResetSplayTreeIterator(coder_cache);
627 p=(const CoderInfo *) GetNextValueInSplayTree(coder_cache);
628 for (i=0; p != (const CoderInfo *) NULL; )
630 if ((p->stealth == MagickFalse) &&
631 (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
632 coder_map[i++]=ConstantString(p->name);
633 p=(const CoderInfo *) GetNextValueInSplayTree(coder_cache);
635 UnlockSemaphoreInfo(coder_semaphore);
636 qsort((void *) coder_map,(size_t) i,sizeof(*coder_map),CoderCompare);
637 coder_map[i]=(char *) NULL;
638 *number_coders=(size_t) i;
643 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
647 + I s C o d e r T r e e I n s t a n t i a t e d %
651 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
653 % IsCoderTreeInstantiated() determines if the coder tree is instantiated. If
654 % not, it instantiates the tree and returns it.
656 % The format of the IsCoderInstantiated method is:
658 % MagickBooleanType IsCoderTreeInstantiated(ExceptionInfo *exception)
660 % A description of each parameter follows.
662 % o exception: return any errors or warnings in this structure.
665 static MagickBooleanType IsCoderTreeInstantiated(ExceptionInfo *exception)
667 if (coder_cache == (SplayTreeInfo *) NULL)
669 if (coder_semaphore == (SemaphoreInfo *) NULL)
670 ActivateSemaphoreInfo(&coder_semaphore);
671 LockSemaphoreInfo(coder_semaphore);
672 if (coder_cache == (SplayTreeInfo *) NULL)
673 coder_cache=AcquireCoderCache(MagickCoderFilename,exception);
674 UnlockSemaphoreInfo(coder_semaphore);
676 return(coder_cache != (SplayTreeInfo *) NULL ? MagickTrue : MagickFalse);
680 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
684 % L i s t C o d e r I n f o %
688 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
690 % ListCoderInfo() lists the coder info to a file.
692 % The format of the ListCoderInfo coder is:
694 % MagickBooleanType ListCoderInfo(FILE *file,ExceptionInfo *exception)
696 % A description of each parameter follows.
698 % o file: An pointer to a FILE.
700 % o exception: return any errors or warnings in this structure.
703 MagickExport MagickBooleanType ListCoderInfo(FILE *file,
704 ExceptionInfo *exception)
721 if (file == (const FILE *) NULL)
723 coder_info=GetCoderInfoList("*",&number_coders,exception);
724 if (coder_info == (const CoderInfo **) NULL)
726 path=(const char *) NULL;
727 for (i=0; i < (ssize_t) number_coders; i++)
729 if (coder_info[i]->stealth != MagickFalse)
731 if ((path == (const char *) NULL) ||
732 (LocaleCompare(path,coder_info[i]->path) != 0))
734 if (coder_info[i]->path != (char *) NULL)
735 (void) FormatLocaleFile(file,"\nPath: %s\n\n",coder_info[i]->path);
736 (void) FormatLocaleFile(file,"Magick Coder\n");
737 (void) FormatLocaleFile(file,
738 "-------------------------------------------------"
739 "------------------------------\n");
741 path=coder_info[i]->path;
742 (void) FormatLocaleFile(file,"%s",coder_info[i]->magick);
743 for (j=(ssize_t) strlen(coder_info[i]->magick); j <= 11; j++)
744 (void) FormatLocaleFile(file," ");
745 if (coder_info[i]->name != (char *) NULL)
746 (void) FormatLocaleFile(file,"%s",coder_info[i]->name);
747 (void) FormatLocaleFile(file,"\n");
749 coder_info=(const CoderInfo **) RelinquishMagickMemory((void *) coder_info);
755 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
759 + L o a d C o d e r L i s t %
763 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
765 % LoadCoderCache() loads the coder configurations which provides a
766 % mapping between coder attributes and a coder name.
768 % The format of the LoadCoderCache coder is:
770 % MagickBooleanType LoadCoderCache(SplayTreeInfo *coder_cache,
771 % const char *xml,const char *filename,const size_t depth,
772 % ExceptionInfo *exception)
774 % A description of each parameter follows:
776 % o xml: The coder list in XML format.
778 % o filename: The coder list filename.
780 % o depth: depth of <include /> statements.
782 % o exception: return any errors or warnings in this structure.
785 static MagickBooleanType LoadCoderCache(SplayTreeInfo *coder_cache,
786 const char *xml,const char *filename,const size_t depth,
787 ExceptionInfo *exception)
790 keyword[MaxTextExtent],
803 Load the coder map file.
805 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
806 "Loading coder configuration file \"%s\" ...",filename);
807 if (xml == (const char *) NULL)
810 coder_info=(CoderInfo *) NULL;
811 token=AcquireString(xml);
812 for (q=(char *) xml; *q != '\0'; )
817 GetMagickToken(q,&q,token);
820 (void) CopyMagickString(keyword,token,MaxTextExtent);
821 if (LocaleNCompare(keyword,"<!DOCTYPE",9) == 0)
826 while ((LocaleNCompare(q,"]>",2) != 0) && (*q != '\0'))
827 GetMagickToken(q,&q,token);
830 if (LocaleNCompare(keyword,"<!--",4) == 0)
835 while ((LocaleNCompare(q,"->",2) != 0) && (*q != '\0'))
836 GetMagickToken(q,&q,token);
839 if (LocaleCompare(keyword,"<include") == 0)
844 while (((*token != '/') && (*(token+1) != '>')) && (*q != '\0'))
846 (void) CopyMagickString(keyword,token,MaxTextExtent);
847 GetMagickToken(q,&q,token);
850 GetMagickToken(q,&q,token);
851 if (LocaleCompare(keyword,"file") == 0)
854 (void) ThrowMagickException(exception,GetMagickModule(),
855 ConfigureError,"IncludeNodeNestedTooDeeply","`%s'",token);
862 GetPathComponent(filename,HeadPath,path);
864 (void) ConcatenateMagickString(path,DirectorySeparator,
866 if (*token == *DirectorySeparator)
867 (void) CopyMagickString(path,token,MaxTextExtent);
869 (void) ConcatenateMagickString(path,token,MaxTextExtent);
870 xml=FileToXML(path,~0UL);
871 if (xml != (char *) NULL)
873 status&=LoadCoderCache(coder_cache,xml,path,depth+1,
875 xml=(char *) RelinquishMagickMemory(xml);
882 if (LocaleCompare(keyword,"<coder") == 0)
887 coder_info=(CoderInfo *) AcquireMagickMemory(sizeof(*coder_info));
888 if (coder_info == (CoderInfo *) NULL)
889 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
890 (void) ResetMagickMemory(coder_info,0,sizeof(*coder_info));
891 coder_info->path=ConstantString(filename);
892 coder_info->exempt=MagickFalse;
893 coder_info->signature=MagickSignature;
896 if (coder_info == (CoderInfo *) NULL)
898 if (LocaleCompare(keyword,"/>") == 0)
900 status=AddValueToSplayTree(coder_cache,ConstantString(
901 coder_info->magick),coder_info);
902 if (status == MagickFalse)
903 (void) ThrowMagickException(exception,GetMagickModule(),
904 ResourceLimitError,"MemoryAllocationFailed","`%s'",
906 coder_info=(CoderInfo *) NULL;
909 GetMagickToken(q,(const char **) NULL,token);
912 GetMagickToken(q,&q,token);
913 GetMagickToken(q,&q,token);
919 if (LocaleCompare((char *) keyword,"magick") == 0)
921 coder_info->magick=ConstantString(token);
929 if (LocaleCompare((char *) keyword,"name") == 0)
931 coder_info->name=ConstantString(token);
939 if (LocaleCompare((char *) keyword,"stealth") == 0)
941 coder_info->stealth=IsStringTrue(token);
950 token=(char *) RelinquishMagickMemory(token);
951 return(status != 0 ? MagickTrue : MagickFalse);