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" },
158 { "LOGO", "MAGICK" },
171 { "NETSCAPE", "MAGICK" },
198 { "RADIAL-GRADIENT", "GRADIENT" },
206 { "ROSE", "MAGICK" },
209 { "SPARSE-COLOR", "TXT" },
214 { "TIFF64", "TIFF" },
217 { "UBRL", "BRAILLE" },
220 { "WIZARD", "MAGICK" },
221 #if defined(MAGICKCORE_WINGDI32_DELEGATE)
228 { "XTRNARRAY", "XTRN" },
229 { "XTRNBLOB", "XTRN" },
230 { "XTRNFILE", "XTRN" },
231 { "XTRNIMAGE", "XTRN" },
234 { "YCbCrA", "YCbCr" }
238 *coder_semaphore = (SemaphoreInfo *) NULL;
241 *coder_cache = (SplayTreeInfo *) NULL;
244 Forward declarations.
246 static MagickBooleanType
247 IsCoderTreeInstantiated(ExceptionInfo *),
248 LoadCoderCache(SplayTreeInfo *,const char *,const char *,const size_t,
252 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
256 + A c q u i r e C o d e r C a c h e %
260 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
262 % AcquireCoderCache() caches one or more coder configurations which provides a
263 % mapping between coder attributes and a coder name.
265 % The format of the AcquireCoderCache coder is:
267 % SplayTreeInfo *AcquireCoderCache(const char *filename,
268 % ExceptionInfo *exception)
270 % A description of each parameter follows:
272 % o filename: the font file name.
274 % o exception: return any errors or warnings in this structure.
278 static void *DestroyCoderNode(void *coder_info)
283 p=(CoderInfo *) coder_info;
284 if (p->exempt == MagickFalse)
286 if (p->path != (char *) NULL)
287 p->path=DestroyString(p->path);
288 if (p->name != (char *) NULL)
289 p->name=DestroyString(p->name);
290 if (p->magick != (char *) NULL)
291 p->magick=DestroyString(p->magick);
293 return(RelinquishMagickMemory(p));
296 static SplayTreeInfo *AcquireCoderCache(const char *filename,
297 ExceptionInfo *exception)
315 Load external coder map.
317 coder_cache=NewSplayTree(CompareSplayTreeString,RelinquishMagickMemory,
319 if (coder_cache == (SplayTreeInfo *) NULL)
320 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
322 options=GetConfigureOptions(filename,exception);
323 option=(const StringInfo *) GetNextValueInLinkedList(options);
324 while (option != (const StringInfo *) NULL)
326 status&=LoadCoderCache(coder_cache,(const char *)
327 GetStringInfoDatum(option),GetStringInfoPath(option),0,exception);
328 option=(const StringInfo *) GetNextValueInLinkedList(options);
330 options=DestroyConfigureOptions(options);
332 Load built-in coder map.
334 for (i=0; i < (ssize_t) (sizeof(CoderMap)/sizeof(*CoderMap)); i++)
339 register const CoderMapInfo
343 coder_info=(CoderInfo *) AcquireMagickMemory(sizeof(*coder_info));
344 if (coder_info == (CoderInfo *) NULL)
346 (void) ThrowMagickException(exception,GetMagickModule(),
347 ResourceLimitError,"MemoryAllocationFailed","`%s'",p->name);
350 (void) ResetMagickMemory(coder_info,0,sizeof(*coder_info));
351 coder_info->path=(char *) "[built-in]";
352 coder_info->magick=(char *) p->magick;
353 coder_info->name=(char *) p->name;
354 coder_info->exempt=MagickTrue;
355 coder_info->signature=MagickSignature;
356 status&=AddValueToSplayTree(coder_cache,ConstantString(coder_info->magick),
358 if (status == MagickFalse)
359 (void) ThrowMagickException(exception,GetMagickModule(),
360 ResourceLimitError,"MemoryAllocationFailed","`%s'",coder_info->name);
366 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
370 + C o d e r C o m p o n e n t G e n e s i s %
374 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
376 % CoderComponentGenesis() instantiates the coder component.
378 % The format of the CoderComponentGenesis method is:
380 % MagickBooleanType CoderComponentGenesis(void)
383 MagickPrivate MagickBooleanType CoderComponentGenesis(void)
385 if (coder_semaphore == (SemaphoreInfo *) NULL)
386 coder_semaphore=AcquireSemaphoreInfo();
391 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
395 + C o d e r C o m p o n e n t T e r m i n u s %
399 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
401 % CoderComponentTerminus() destroys the coder component.
403 % The format of the CoderComponentTerminus method is:
405 % CoderComponentTerminus(void)
408 MagickPrivate void CoderComponentTerminus(void)
410 if (coder_semaphore == (SemaphoreInfo *) NULL)
411 ActivateSemaphoreInfo(&coder_semaphore);
412 LockSemaphoreInfo(coder_semaphore);
413 if (coder_cache != (SplayTreeInfo *) NULL)
414 coder_cache=DestroySplayTree(coder_cache);
415 UnlockSemaphoreInfo(coder_semaphore);
416 RelinquishSemaphoreInfo(&coder_semaphore);
420 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
424 + G e t C o d e r I n f o %
428 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
430 % GetCoderInfo searches the coder list for the specified name and if found
431 % returns attributes for that coder.
433 % The format of the GetCoderInfo method is:
435 % const CoderInfo *GetCoderInfo(const char *name,ExceptionInfo *exception)
437 % A description of each parameter follows:
439 % o name: the coder name.
441 % o exception: return any errors or warnings in this structure.
444 MagickExport const CoderInfo *GetCoderInfo(const char *name,
445 ExceptionInfo *exception)
450 assert(exception != (ExceptionInfo *) NULL);
451 if (IsCoderTreeInstantiated(exception) == MagickFalse)
452 return((const CoderInfo *) NULL);
453 LockSemaphoreInfo(coder_semaphore);
454 if ((name == (const char *) NULL) || (LocaleCompare(name,"*") == 0))
456 ResetSplayTreeIterator(coder_cache);
457 coder_info=(const CoderInfo *) GetNextValueInSplayTree(coder_cache);
458 UnlockSemaphoreInfo(coder_semaphore);
461 coder_info=(const CoderInfo *) GetValueFromSplayTree(coder_cache,name);
462 UnlockSemaphoreInfo(coder_semaphore);
467 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
471 % G e t C o d e r I n f o L i s t %
475 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
477 % GetCoderInfoList() returns any coder_map that match the specified pattern.
478 % The format of the GetCoderInfoList function is:
480 % const CoderInfo **GetCoderInfoList(const char *pattern,
481 % size_t *number_coders,ExceptionInfo *exception)
483 % A description of each parameter follows:
485 % o pattern: Specifies a pointer to a text string containing a pattern.
487 % o number_coders: This integer returns the number of coders in the list.
489 % o exception: return any errors or warnings in this structure.
493 static int CoderInfoCompare(const void *x,const void *y)
499 p=(const CoderInfo **) x,
500 q=(const CoderInfo **) y;
501 if (LocaleCompare((*p)->path,(*q)->path) == 0)
502 return(LocaleCompare((*p)->name,(*q)->name));
503 return(LocaleCompare((*p)->path,(*q)->path));
506 MagickExport const CoderInfo **GetCoderInfoList(const char *pattern,
507 size_t *number_coders,ExceptionInfo *exception)
512 register const CoderInfo
521 assert(pattern != (char *) NULL);
522 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
523 assert(number_coders != (size_t *) NULL);
525 p=GetCoderInfo("*",exception);
526 if (p == (const CoderInfo *) NULL)
527 return((const CoderInfo **) NULL);
528 coder_map=(const CoderInfo **) AcquireQuantumMemory((size_t)
529 GetNumberOfNodesInSplayTree(coder_cache)+1UL,sizeof(*coder_map));
530 if (coder_map == (const CoderInfo **) NULL)
531 return((const CoderInfo **) NULL);
535 LockSemaphoreInfo(coder_semaphore);
536 ResetSplayTreeIterator(coder_cache);
537 p=(const CoderInfo *) GetNextValueInSplayTree(coder_cache);
538 for (i=0; p != (const CoderInfo *) NULL; )
540 if ((p->stealth == MagickFalse) &&
541 (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
543 p=(const CoderInfo *) GetNextValueInSplayTree(coder_cache);
545 UnlockSemaphoreInfo(coder_semaphore);
546 qsort((void *) coder_map,(size_t) i,sizeof(*coder_map),CoderInfoCompare);
547 coder_map[i]=(CoderInfo *) NULL;
548 *number_coders=(size_t) i;
553 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
557 % G e t C o d e r L i s t %
561 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
563 % GetCoderList() returns any coder_map that match the specified pattern.
565 % The format of the GetCoderList function is:
567 % char **GetCoderList(const char *pattern,size_t *number_coders,
568 % ExceptionInfo *exception)
570 % A description of each parameter follows:
572 % o pattern: Specifies a pointer to a text string containing a pattern.
574 % o number_coders: This integer returns the number of coders in the list.
576 % o exception: return any errors or warnings in this structure.
580 static int CoderCompare(const void *x,const void *y)
588 return(LocaleCompare(*p,*q));
591 MagickExport char **GetCoderList(const char *pattern,
592 size_t *number_coders,ExceptionInfo *exception)
597 register const CoderInfo
606 assert(pattern != (char *) NULL);
607 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
608 assert(number_coders != (size_t *) NULL);
610 p=GetCoderInfo("*",exception);
611 if (p == (const CoderInfo *) NULL)
612 return((char **) NULL);
613 coder_map=(char **) AcquireQuantumMemory((size_t)
614 GetNumberOfNodesInSplayTree(coder_cache)+1UL,sizeof(*coder_map));
615 if (coder_map == (char **) NULL)
616 return((char **) NULL);
620 LockSemaphoreInfo(coder_semaphore);
621 ResetSplayTreeIterator(coder_cache);
622 p=(const CoderInfo *) GetNextValueInSplayTree(coder_cache);
623 for (i=0; p != (const CoderInfo *) NULL; )
625 if ((p->stealth == MagickFalse) &&
626 (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
627 coder_map[i++]=ConstantString(p->name);
628 p=(const CoderInfo *) GetNextValueInSplayTree(coder_cache);
630 UnlockSemaphoreInfo(coder_semaphore);
631 qsort((void *) coder_map,(size_t) i,sizeof(*coder_map),CoderCompare);
632 coder_map[i]=(char *) NULL;
633 *number_coders=(size_t) i;
638 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
642 + I s C o d e r T r e e I n s t a n t i a t e d %
646 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
648 % IsCoderTreeInstantiated() determines if the coder tree is instantiated. If
649 % not, it instantiates the tree and returns it.
651 % The format of the IsCoderInstantiated method is:
653 % MagickBooleanType IsCoderTreeInstantiated(ExceptionInfo *exception)
655 % A description of each parameter follows.
657 % o exception: return any errors or warnings in this structure.
660 static MagickBooleanType IsCoderTreeInstantiated(ExceptionInfo *exception)
662 if (coder_cache == (SplayTreeInfo *) NULL)
664 if (coder_semaphore == (SemaphoreInfo *) NULL)
665 ActivateSemaphoreInfo(&coder_semaphore);
666 LockSemaphoreInfo(coder_semaphore);
667 if (coder_cache == (SplayTreeInfo *) NULL)
668 coder_cache=AcquireCoderCache(MagickCoderFilename,exception);
669 UnlockSemaphoreInfo(coder_semaphore);
671 return(coder_cache != (SplayTreeInfo *) NULL ? MagickTrue : MagickFalse);
675 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
679 % L i s t C o d e r I n f o %
683 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
685 % ListCoderInfo() lists the coder info to a file.
687 % The format of the ListCoderInfo coder is:
689 % MagickBooleanType ListCoderInfo(FILE *file,ExceptionInfo *exception)
691 % A description of each parameter follows.
693 % o file: An pointer to a FILE.
695 % o exception: return any errors or warnings in this structure.
698 MagickExport MagickBooleanType ListCoderInfo(FILE *file,
699 ExceptionInfo *exception)
716 if (file == (const FILE *) NULL)
718 coder_info=GetCoderInfoList("*",&number_coders,exception);
719 if (coder_info == (const CoderInfo **) NULL)
721 path=(const char *) NULL;
722 for (i=0; i < (ssize_t) number_coders; i++)
724 if (coder_info[i]->stealth != MagickFalse)
726 if ((path == (const char *) NULL) ||
727 (LocaleCompare(path,coder_info[i]->path) != 0))
729 if (coder_info[i]->path != (char *) NULL)
730 (void) FormatLocaleFile(file,"\nPath: %s\n\n",coder_info[i]->path);
731 (void) FormatLocaleFile(file,"Magick Coder\n");
732 (void) FormatLocaleFile(file,
733 "-------------------------------------------------"
734 "------------------------------\n");
736 path=coder_info[i]->path;
737 (void) FormatLocaleFile(file,"%s",coder_info[i]->magick);
738 for (j=(ssize_t) strlen(coder_info[i]->magick); j <= 11; j++)
739 (void) FormatLocaleFile(file," ");
740 if (coder_info[i]->name != (char *) NULL)
741 (void) FormatLocaleFile(file,"%s",coder_info[i]->name);
742 (void) FormatLocaleFile(file,"\n");
744 coder_info=(const CoderInfo **) RelinquishMagickMemory((void *) coder_info);
750 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
754 + L o a d C o d e r L i s t %
758 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
760 % LoadCoderCache() loads the coder configurations which provides a
761 % mapping between coder attributes and a coder name.
763 % The format of the LoadCoderCache coder is:
765 % MagickBooleanType LoadCoderCache(SplayTreeInfo *coder_cache,
766 % const char *xml,const char *filename,const size_t depth,
767 % ExceptionInfo *exception)
769 % A description of each parameter follows:
771 % o xml: The coder list in XML format.
773 % o filename: The coder list filename.
775 % o depth: depth of <include /> statements.
777 % o exception: return any errors or warnings in this structure.
780 static MagickBooleanType LoadCoderCache(SplayTreeInfo *coder_cache,
781 const char *xml,const char *filename,const size_t depth,
782 ExceptionInfo *exception)
785 keyword[MaxTextExtent],
798 Load the coder map file.
800 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
801 "Loading coder configuration file \"%s\" ...",filename);
802 if (xml == (const char *) NULL)
805 coder_info=(CoderInfo *) NULL;
806 token=AcquireString(xml);
807 for (q=(char *) xml; *q != '\0'; )
812 GetMagickToken(q,&q,token);
815 (void) CopyMagickString(keyword,token,MaxTextExtent);
816 if (LocaleNCompare(keyword,"<!DOCTYPE",9) == 0)
821 while ((LocaleNCompare(q,"]>",2) != 0) && (*q != '\0'))
822 GetMagickToken(q,&q,token);
825 if (LocaleNCompare(keyword,"<!--",4) == 0)
830 while ((LocaleNCompare(q,"->",2) != 0) && (*q != '\0'))
831 GetMagickToken(q,&q,token);
834 if (LocaleCompare(keyword,"<include") == 0)
839 while (((*token != '/') && (*(token+1) != '>')) && (*q != '\0'))
841 (void) CopyMagickString(keyword,token,MaxTextExtent);
842 GetMagickToken(q,&q,token);
845 GetMagickToken(q,&q,token);
846 if (LocaleCompare(keyword,"file") == 0)
849 (void) ThrowMagickException(exception,GetMagickModule(),
850 ConfigureError,"IncludeNodeNestedTooDeeply","`%s'",token);
857 GetPathComponent(filename,HeadPath,path);
859 (void) ConcatenateMagickString(path,DirectorySeparator,
861 if (*token == *DirectorySeparator)
862 (void) CopyMagickString(path,token,MaxTextExtent);
864 (void) ConcatenateMagickString(path,token,MaxTextExtent);
865 xml=FileToXML(path,~0UL);
866 if (xml != (char *) NULL)
868 status&=LoadCoderCache(coder_cache,xml,path,depth+1,
870 xml=(char *) RelinquishMagickMemory(xml);
877 if (LocaleCompare(keyword,"<coder") == 0)
882 coder_info=(CoderInfo *) AcquireMagickMemory(sizeof(*coder_info));
883 if (coder_info == (CoderInfo *) NULL)
884 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
885 (void) ResetMagickMemory(coder_info,0,sizeof(*coder_info));
886 coder_info->path=ConstantString(filename);
887 coder_info->exempt=MagickFalse;
888 coder_info->signature=MagickSignature;
891 if (coder_info == (CoderInfo *) NULL)
893 if (LocaleCompare(keyword,"/>") == 0)
895 status=AddValueToSplayTree(coder_cache,ConstantString(
896 coder_info->magick),coder_info);
897 if (status == MagickFalse)
898 (void) ThrowMagickException(exception,GetMagickModule(),
899 ResourceLimitError,"MemoryAllocationFailed","`%s'",
901 coder_info=(CoderInfo *) NULL;
904 GetMagickToken(q,(const char **) NULL,token);
907 GetMagickToken(q,&q,token);
908 GetMagickToken(q,&q,token);
914 if (LocaleCompare((char *) keyword,"magick") == 0)
916 coder_info->magick=ConstantString(token);
924 if (LocaleCompare((char *) keyword,"name") == 0)
926 coder_info->name=ConstantString(token);
934 if (LocaleCompare((char *) keyword,"stealth") == 0)
936 coder_info->stealth=IsStringTrue(token);
945 token=(char *) RelinquishMagickMemory(token);
946 return(status != 0 ? MagickTrue : MagickFalse);