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" },
203 { "ROSE", "MAGICK" },
206 { "SPARSE-COLOR", "TXT" },
211 { "TIFF64", "TIFF" },
214 { "UBRL", "BRAILLE" },
217 { "WIZARD", "MAGICK" },
218 #if defined(MAGICKCORE_WINGDI32_DELEGATE)
225 { "XTRNARRAY", "XTRN" },
226 { "XTRNBLOB", "XTRN" },
227 { "XTRNFILE", "XTRN" },
228 { "XTRNIMAGE", "XTRN" },
231 { "YCbCrA", "YCbCr" }
235 *coder_semaphore = (SemaphoreInfo *) NULL;
238 *coder_cache = (SplayTreeInfo *) NULL;
241 Forward declarations.
243 static MagickBooleanType
244 IsCoderTreeInstantiated(ExceptionInfo *),
245 LoadCoderCache(SplayTreeInfo *,const char *,const char *,const size_t,
249 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
253 % A c q u i r e C o d e r S p l a y T r e e %
257 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
259 % AcquireCoderCache() caches one or more coder configurations which
260 % provides a mapping between coder attributes and a coder name.
262 % The format of the AcquireCoderCache coder is:
264 % SplayTreeInfo *AcquireCoderCache(const char *filename,
265 % ExceptionInfo *exception)
267 % A description of each parameter follows:
269 % o filename: the font file name.
271 % o exception: return any errors or warnings in this structure.
275 static void *DestroyCoderNode(void *coder_info)
280 p=(CoderInfo *) coder_info;
281 if (p->exempt == MagickFalse)
283 if (p->path != (char *) NULL)
284 p->path=DestroyString(p->path);
285 if (p->name != (char *) NULL)
286 p->name=DestroyString(p->name);
287 if (p->magick != (char *) NULL)
288 p->magick=DestroyString(p->magick);
290 return(RelinquishMagickMemory(p));
293 static SplayTreeInfo *AcquireCoderCache(const char *filename,
294 ExceptionInfo *exception)
312 Load external coder map.
314 coder_cache=NewSplayTree(CompareSplayTreeString,RelinquishMagickMemory,
316 if (coder_cache == (SplayTreeInfo *) NULL)
317 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
319 options=GetConfigureOptions(filename,exception);
320 option=(const StringInfo *) GetNextValueInLinkedList(options);
321 while (option != (const StringInfo *) NULL)
323 status&=LoadCoderCache(coder_cache,(const char *)
324 GetStringInfoDatum(option),GetStringInfoPath(option),0,exception);
325 option=(const StringInfo *) GetNextValueInLinkedList(options);
327 options=DestroyConfigureOptions(options);
329 Load built-in coder map.
331 for (i=0; i < (ssize_t) (sizeof(CoderMap)/sizeof(*CoderMap)); i++)
336 register const CoderMapInfo
340 coder_info=(CoderInfo *) AcquireMagickMemory(sizeof(*coder_info));
341 if (coder_info == (CoderInfo *) NULL)
343 (void) ThrowMagickException(exception,GetMagickModule(),
344 ResourceLimitError,"MemoryAllocationFailed","`%s'",p->name);
347 (void) ResetMagickMemory(coder_info,0,sizeof(*coder_info));
348 coder_info->path=(char *) "[built-in]";
349 coder_info->magick=(char *) p->magick;
350 coder_info->name=(char *) p->name;
351 coder_info->exempt=MagickTrue;
352 coder_info->signature=MagickSignature;
353 status&=AddValueToSplayTree(coder_cache,ConstantString(coder_info->magick),
355 if (status == MagickFalse)
356 (void) ThrowMagickException(exception,GetMagickModule(),
357 ResourceLimitError,"MemoryAllocationFailed","`%s'",coder_info->name);
363 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
367 + C o d e r C o m p o n e n t G e n e s i s %
371 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
373 % CoderComponentGenesis() instantiates the coder component.
375 % The format of the CoderComponentGenesis method is:
377 % MagickBooleanType CoderComponentGenesis(void)
380 MagickPrivate MagickBooleanType CoderComponentGenesis(void)
382 coder_semaphore=AcquireSemaphoreInfo();
387 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
391 + C o d e r C o m p o n e n t T e r m i n u s %
395 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
397 % CoderComponentTerminus() destroys the coder component.
399 % The format of the CoderComponentTerminus method is:
401 % CoderComponentTerminus(void)
404 MagickPrivate void CoderComponentTerminus(void)
406 if (coder_semaphore == (SemaphoreInfo *) NULL)
407 ActivateSemaphoreInfo(&coder_semaphore);
408 LockSemaphoreInfo(coder_semaphore);
409 if (coder_cache != (SplayTreeInfo *) NULL)
410 coder_cache=DestroySplayTree(coder_cache);
411 UnlockSemaphoreInfo(coder_semaphore);
412 RelinquishSemaphoreInfo(&coder_semaphore);
416 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
420 + G e t C o d e r I n f o %
424 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
426 % GetCoderInfo searches the coder list for the specified name and if found
427 % returns attributes for that coder.
429 % The format of the GetCoderInfo method is:
431 % const CoderInfo *GetCoderInfo(const char *name,ExceptionInfo *exception)
433 % A description of each parameter follows:
435 % o name: the coder name.
437 % o exception: return any errors or warnings in this structure.
440 MagickExport const CoderInfo *GetCoderInfo(const char *name,
441 ExceptionInfo *exception)
446 assert(exception != (ExceptionInfo *) NULL);
447 if (IsCoderTreeInstantiated(exception) == MagickFalse)
448 return((const CoderInfo *) NULL);
449 LockSemaphoreInfo(coder_semaphore);
450 if ((name == (const char *) NULL) || (LocaleCompare(name,"*") == 0))
452 ResetSplayTreeIterator(coder_cache);
453 coder_info=(const CoderInfo *) GetNextValueInSplayTree(coder_cache);
454 UnlockSemaphoreInfo(coder_semaphore);
457 coder_info=(const CoderInfo *) GetValueFromSplayTree(coder_cache,name);
458 UnlockSemaphoreInfo(coder_semaphore);
463 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
467 % G e t C o d e r I n f o L i s t %
471 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
473 % GetCoderInfoList() returns any coder_map that match the specified pattern.
474 % The format of the GetCoderInfoList function is:
476 % const CoderInfo **GetCoderInfoList(const char *pattern,
477 % size_t *number_coders,ExceptionInfo *exception)
479 % A description of each parameter follows:
481 % o pattern: Specifies a pointer to a text string containing a pattern.
483 % o number_coders: This integer returns the number of coders in the list.
485 % o exception: return any errors or warnings in this structure.
489 static int CoderInfoCompare(const void *x,const void *y)
495 p=(const CoderInfo **) x,
496 q=(const CoderInfo **) y;
497 if (LocaleCompare((*p)->path,(*q)->path) == 0)
498 return(LocaleCompare((*p)->name,(*q)->name));
499 return(LocaleCompare((*p)->path,(*q)->path));
502 MagickExport const CoderInfo **GetCoderInfoList(const char *pattern,
503 size_t *number_coders,ExceptionInfo *exception)
508 register const CoderInfo
517 assert(pattern != (char *) NULL);
518 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
519 assert(number_coders != (size_t *) NULL);
521 p=GetCoderInfo("*",exception);
522 if (p == (const CoderInfo *) NULL)
523 return((const CoderInfo **) NULL);
524 coder_map=(const CoderInfo **) AcquireQuantumMemory((size_t)
525 GetNumberOfNodesInSplayTree(coder_cache)+1UL,sizeof(*coder_map));
526 if (coder_map == (const CoderInfo **) NULL)
527 return((const CoderInfo **) NULL);
531 LockSemaphoreInfo(coder_semaphore);
532 ResetSplayTreeIterator(coder_cache);
533 p=(const CoderInfo *) GetNextValueInSplayTree(coder_cache);
534 for (i=0; p != (const CoderInfo *) NULL; )
536 if ((p->stealth == MagickFalse) &&
537 (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
539 p=(const CoderInfo *) GetNextValueInSplayTree(coder_cache);
541 UnlockSemaphoreInfo(coder_semaphore);
542 qsort((void *) coder_map,(size_t) i,sizeof(*coder_map),CoderInfoCompare);
543 coder_map[i]=(CoderInfo *) NULL;
544 *number_coders=(size_t) i;
549 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
553 % G e t C o d e r L i s t %
557 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
559 % GetCoderList() returns any coder_map that match the specified pattern.
561 % The format of the GetCoderList function is:
563 % char **GetCoderList(const char *pattern,size_t *number_coders,
564 % ExceptionInfo *exception)
566 % A description of each parameter follows:
568 % o pattern: Specifies a pointer to a text string containing a pattern.
570 % o number_coders: This integer returns the number of coders in the list.
572 % o exception: return any errors or warnings in this structure.
576 static int CoderCompare(const void *x,const void *y)
584 return(LocaleCompare(*p,*q));
587 MagickExport char **GetCoderList(const char *pattern,
588 size_t *number_coders,ExceptionInfo *exception)
593 register const CoderInfo
602 assert(pattern != (char *) NULL);
603 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
604 assert(number_coders != (size_t *) NULL);
606 p=GetCoderInfo("*",exception);
607 if (p == (const CoderInfo *) NULL)
608 return((char **) NULL);
609 coder_map=(char **) AcquireQuantumMemory((size_t)
610 GetNumberOfNodesInSplayTree(coder_cache)+1UL,sizeof(*coder_map));
611 if (coder_map == (char **) NULL)
612 return((char **) NULL);
616 LockSemaphoreInfo(coder_semaphore);
617 ResetSplayTreeIterator(coder_cache);
618 p=(const CoderInfo *) GetNextValueInSplayTree(coder_cache);
619 for (i=0; p != (const CoderInfo *) NULL; )
621 if ((p->stealth == MagickFalse) &&
622 (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
623 coder_map[i++]=ConstantString(p->name);
624 p=(const CoderInfo *) GetNextValueInSplayTree(coder_cache);
626 UnlockSemaphoreInfo(coder_semaphore);
627 qsort((void *) coder_map,(size_t) i,sizeof(*coder_map),CoderCompare);
628 coder_map[i]=(char *) NULL;
629 *number_coders=(size_t) i;
634 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
638 + I s C o d e r T r e e I n s t a n t i a t e d %
642 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
644 % IsCoderTreeInstantiated() determines if the coder tree is instantiated. If
645 % not, it instantiates the tree and returns it.
647 % The format of the IsCoderInstantiated method is:
649 % MagickBooleanType IsCoderTreeInstantiated(ExceptionInfo *exception)
651 % A description of each parameter follows.
653 % o exception: return any errors or warnings in this structure.
656 static MagickBooleanType IsCoderTreeInstantiated(ExceptionInfo *exception)
658 if (coder_cache == (SplayTreeInfo *) NULL)
660 if (coder_semaphore == (SemaphoreInfo *) NULL)
661 ActivateSemaphoreInfo(&coder_semaphore);
662 LockSemaphoreInfo(coder_semaphore);
663 if (coder_cache == (SplayTreeInfo *) NULL)
664 coder_cache=AcquireCoderCache(MagickCoderFilename,exception);
665 UnlockSemaphoreInfo(coder_semaphore);
667 return(coder_cache != (SplayTreeInfo *) NULL ? MagickTrue : MagickFalse);
671 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
675 % L i s t C o d e r I n f o %
679 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
681 % ListCoderInfo() lists the coder info to a file.
683 % The format of the ListCoderInfo coder is:
685 % MagickBooleanType ListCoderInfo(FILE *file,ExceptionInfo *exception)
687 % A description of each parameter follows.
689 % o file: An pointer to a FILE.
691 % o exception: return any errors or warnings in this structure.
694 MagickExport MagickBooleanType ListCoderInfo(FILE *file,
695 ExceptionInfo *exception)
712 if (file == (const FILE *) NULL)
714 coder_info=GetCoderInfoList("*",&number_coders,exception);
715 if (coder_info == (const CoderInfo **) NULL)
717 path=(const char *) NULL;
718 for (i=0; i < (ssize_t) number_coders; i++)
720 if (coder_info[i]->stealth != MagickFalse)
722 if ((path == (const char *) NULL) ||
723 (LocaleCompare(path,coder_info[i]->path) != 0))
725 if (coder_info[i]->path != (char *) NULL)
726 (void) FormatLocaleFile(file,"\nPath: %s\n\n",coder_info[i]->path);
727 (void) FormatLocaleFile(file,"Magick Coder\n");
728 (void) FormatLocaleFile(file,
729 "-------------------------------------------------"
730 "------------------------------\n");
732 path=coder_info[i]->path;
733 (void) FormatLocaleFile(file,"%s",coder_info[i]->magick);
734 for (j=(ssize_t) strlen(coder_info[i]->magick); j <= 11; j++)
735 (void) FormatLocaleFile(file," ");
736 if (coder_info[i]->name != (char *) NULL)
737 (void) FormatLocaleFile(file,"%s",coder_info[i]->name);
738 (void) FormatLocaleFile(file,"\n");
740 coder_info=(const CoderInfo **) RelinquishMagickMemory((void *) coder_info);
746 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
750 + L o a d C o d e r L i s t %
754 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
756 % LoadCoderCache() loads the coder configurations which provides a
757 % mapping between coder attributes and a coder name.
759 % The format of the LoadCoderCache coder is:
761 % MagickBooleanType LoadCoderCache(SplayTreeInfo *coder_cache,
762 % const char *xml,const char *filename,const size_t depth,
763 % ExceptionInfo *exception)
765 % A description of each parameter follows:
767 % o xml: The coder list in XML format.
769 % o filename: The coder list filename.
771 % o depth: depth of <include /> statements.
773 % o exception: return any errors or warnings in this structure.
776 static MagickBooleanType LoadCoderCache(SplayTreeInfo *coder_cache,
777 const char *xml,const char *filename,const size_t depth,
778 ExceptionInfo *exception)
781 keyword[MaxTextExtent],
794 Load the coder map file.
796 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
797 "Loading coder configuration file \"%s\" ...",filename);
798 if (xml == (const char *) NULL)
801 coder_info=(CoderInfo *) NULL;
802 token=AcquireString(xml);
803 for (q=(char *) xml; *q != '\0'; )
808 GetMagickToken(q,&q,token);
811 (void) CopyMagickString(keyword,token,MaxTextExtent);
812 if (LocaleNCompare(keyword,"<!DOCTYPE",9) == 0)
817 while ((LocaleNCompare(q,"]>",2) != 0) && (*q != '\0'))
818 GetMagickToken(q,&q,token);
821 if (LocaleNCompare(keyword,"<!--",4) == 0)
826 while ((LocaleNCompare(q,"->",2) != 0) && (*q != '\0'))
827 GetMagickToken(q,&q,token);
830 if (LocaleCompare(keyword,"<include") == 0)
835 while (((*token != '/') && (*(token+1) != '>')) && (*q != '\0'))
837 (void) CopyMagickString(keyword,token,MaxTextExtent);
838 GetMagickToken(q,&q,token);
841 GetMagickToken(q,&q,token);
842 if (LocaleCompare(keyword,"file") == 0)
845 (void) ThrowMagickException(exception,GetMagickModule(),
846 ConfigureError,"IncludeNodeNestedTooDeeply","`%s'",token);
853 GetPathComponent(filename,HeadPath,path);
855 (void) ConcatenateMagickString(path,DirectorySeparator,
857 if (*token == *DirectorySeparator)
858 (void) CopyMagickString(path,token,MaxTextExtent);
860 (void) ConcatenateMagickString(path,token,MaxTextExtent);
861 xml=FileToXML(path,~0UL);
862 if (xml != (char *) NULL)
864 status&=LoadCoderCache(coder_cache,xml,path,depth+1,
866 xml=(char *) RelinquishMagickMemory(xml);
873 if (LocaleCompare(keyword,"<coder") == 0)
878 coder_info=(CoderInfo *) AcquireMagickMemory(sizeof(*coder_info));
879 if (coder_info == (CoderInfo *) NULL)
880 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
881 (void) ResetMagickMemory(coder_info,0,sizeof(*coder_info));
882 coder_info->path=ConstantString(filename);
883 coder_info->exempt=MagickFalse;
884 coder_info->signature=MagickSignature;
887 if (coder_info == (CoderInfo *) NULL)
889 if (LocaleCompare(keyword,"/>") == 0)
891 status=AddValueToSplayTree(coder_cache,ConstantString(
892 coder_info->magick),coder_info);
893 if (status == MagickFalse)
894 (void) ThrowMagickException(exception,GetMagickModule(),
895 ResourceLimitError,"MemoryAllocationFailed","`%s'",
897 coder_info=(CoderInfo *) NULL;
900 GetMagickToken(q,(const char **) NULL,token);
903 GetMagickToken(q,&q,token);
904 GetMagickToken(q,&q,token);
910 if (LocaleCompare((char *) keyword,"magick") == 0)
912 coder_info->magick=ConstantString(token);
920 if (LocaleCompare((char *) keyword,"name") == 0)
922 coder_info->name=ConstantString(token);
930 if (LocaleCompare((char *) keyword,"stealth") == 0)
932 coder_info->stealth=IsStringTrue(token);
941 token=(char *) RelinquishMagickMemory(token);