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_list = (SplayTreeInfo *) NULL;
240 Forward declarations.
242 static MagickBooleanType
243 InitializeCoderList(ExceptionInfo *),
244 LoadCoderLists(const char *,ExceptionInfo *);
247 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
251 + C o d e r C o m p o n e n t G e n e s i s %
255 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
257 % CoderComponentGenesis() instantiates the coder component.
259 % The format of the CoderComponentGenesis method is:
261 % MagickBooleanType CoderComponentGenesis(void)
264 MagickPrivate MagickBooleanType CoderComponentGenesis(void)
266 coder_semaphore=AcquireSemaphoreInfo();
271 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
275 + C o d e r C o m p o n e n t T e r m i n u s %
279 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
281 % CoderComponentTerminus() destroys the coder component.
283 % The format of the CoderComponentTerminus method is:
285 % CoderComponentTerminus(void)
288 MagickPrivate void CoderComponentTerminus(void)
290 if (coder_semaphore == (SemaphoreInfo *) NULL)
291 ActivateSemaphoreInfo(&coder_semaphore);
292 LockSemaphoreInfo(coder_semaphore);
293 if (coder_list != (SplayTreeInfo *) NULL)
294 coder_list=DestroySplayTree(coder_list);
295 UnlockSemaphoreInfo(coder_semaphore);
296 RelinquishSemaphoreInfo(&coder_semaphore);
300 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
304 + G e t C o d e r I n f o %
308 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
310 % GetCoderInfo searches the coder list for the specified name and if found
311 % returns attributes for that coder.
313 % The format of the GetCoderInfo method is:
315 % const CoderInfo *GetCoderInfo(const char *name,ExceptionInfo *exception)
317 % A description of each parameter follows:
319 % o name: the coder name.
321 % o exception: return any errors or warnings in this structure.
324 MagickExport const CoderInfo *GetCoderInfo(const char *name,
325 ExceptionInfo *exception)
327 assert(exception != (ExceptionInfo *) NULL);
328 if (coder_list == (SplayTreeInfo *) NULL)
329 if (InitializeCoderList(exception) == MagickFalse)
330 return((const CoderInfo *) NULL);
331 if ((name == (const char *) NULL) || (LocaleCompare(name,"*") == 0))
333 ResetSplayTreeIterator(coder_list);
334 return((const CoderInfo *) GetNextValueInSplayTree(coder_list));
336 return((const CoderInfo *) GetValueFromSplayTree(coder_list,name));
340 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
344 % G e t C o d e r I n f o L i s t %
348 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
350 % GetCoderInfoList() returns any coder_map that match the specified pattern.
351 % The format of the GetCoderInfoList function is:
353 % const CoderInfo **GetCoderInfoList(const char *pattern,
354 % size_t *number_coders,ExceptionInfo *exception)
356 % A description of each parameter follows:
358 % o pattern: Specifies a pointer to a text string containing a pattern.
360 % o number_coders: This integer returns the number of coders in the list.
362 % o exception: return any errors or warnings in this structure.
366 static int CoderInfoCompare(const void *x,const void *y)
372 p=(const CoderInfo **) x,
373 q=(const CoderInfo **) y;
374 if (LocaleCompare((*p)->path,(*q)->path) == 0)
375 return(LocaleCompare((*p)->name,(*q)->name));
376 return(LocaleCompare((*p)->path,(*q)->path));
379 MagickExport const CoderInfo **GetCoderInfoList(const char *pattern,
380 size_t *number_coders,ExceptionInfo *exception)
385 register const CoderInfo
394 assert(pattern != (char *) NULL);
395 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
396 assert(number_coders != (size_t *) NULL);
398 p=GetCoderInfo("*",exception);
399 if (p == (const CoderInfo *) NULL)
400 return((const CoderInfo **) NULL);
401 coder_map=(const CoderInfo **) AcquireQuantumMemory((size_t)
402 GetNumberOfNodesInSplayTree(coder_list)+1UL,sizeof(*coder_map));
403 if (coder_map == (const CoderInfo **) NULL)
404 return((const CoderInfo **) NULL);
408 LockSemaphoreInfo(coder_semaphore);
409 ResetSplayTreeIterator(coder_list);
410 p=(const CoderInfo *) GetNextValueInSplayTree(coder_list);
411 for (i=0; p != (const CoderInfo *) NULL; )
413 if ((p->stealth == MagickFalse) &&
414 (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
416 p=(const CoderInfo *) GetNextValueInSplayTree(coder_list);
418 UnlockSemaphoreInfo(coder_semaphore);
419 qsort((void *) coder_map,(size_t) i,sizeof(*coder_map),CoderInfoCompare);
420 coder_map[i]=(CoderInfo *) NULL;
421 *number_coders=(size_t) i;
426 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
430 % G e t C o d e r L i s t %
434 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
436 % GetCoderList() returns any coder_map that match the specified pattern.
438 % The format of the GetCoderList function is:
440 % char **GetCoderList(const char *pattern,size_t *number_coders,
441 % ExceptionInfo *exception)
443 % A description of each parameter follows:
445 % o pattern: Specifies a pointer to a text string containing a pattern.
447 % o number_coders: This integer returns the number of coders in the list.
449 % o exception: return any errors or warnings in this structure.
453 static int CoderCompare(const void *x,const void *y)
461 return(LocaleCompare(*p,*q));
464 MagickExport char **GetCoderList(const char *pattern,
465 size_t *number_coders,ExceptionInfo *exception)
470 register const CoderInfo
479 assert(pattern != (char *) NULL);
480 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
481 assert(number_coders != (size_t *) NULL);
483 p=GetCoderInfo("*",exception);
484 if (p == (const CoderInfo *) NULL)
485 return((char **) NULL);
486 coder_map=(char **) AcquireQuantumMemory((size_t)
487 GetNumberOfNodesInSplayTree(coder_list)+1UL,sizeof(*coder_map));
488 if (coder_map == (char **) NULL)
489 return((char **) NULL);
493 LockSemaphoreInfo(coder_semaphore);
494 ResetSplayTreeIterator(coder_list);
495 p=(const CoderInfo *) GetNextValueInSplayTree(coder_list);
496 for (i=0; p != (const CoderInfo *) NULL; )
498 if ((p->stealth == MagickFalse) &&
499 (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
500 coder_map[i++]=ConstantString(p->name);
501 p=(const CoderInfo *) GetNextValueInSplayTree(coder_list);
503 UnlockSemaphoreInfo(coder_semaphore);
504 qsort((void *) coder_map,(size_t) i,sizeof(*coder_map),CoderCompare);
505 coder_map[i]=(char *) NULL;
506 *number_coders=(size_t) i;
511 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
515 + I n i t i a l i z e C o d e r L i s t %
519 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
521 % InitializeCoderList() initializes the coder list.
523 % The format of the InitializeCoderList method is:
525 % MagickBooleanType InitializeCoderList(ExceptionInfo *exception)
527 % A description of each parameter follows.
529 % o exception: return any errors or warnings in this structure.
532 static MagickBooleanType InitializeCoderList(ExceptionInfo *exception)
534 if (coder_semaphore == (SemaphoreInfo *) NULL)
535 ActivateSemaphoreInfo(&coder_semaphore);
536 LockSemaphoreInfo(coder_semaphore);
537 if (coder_list == (SplayTreeInfo *) NULL)
538 (void) LoadCoderLists(MagickCoderFilename,exception);
539 UnlockSemaphoreInfo(coder_semaphore);
540 return(coder_list != (SplayTreeInfo *) NULL ? MagickTrue : MagickFalse);
544 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
548 % L i s t C o d e r I n f o %
552 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
554 % ListCoderInfo() lists the coder info to a file.
556 % The format of the ListCoderInfo coder is:
558 % MagickBooleanType ListCoderInfo(FILE *file,ExceptionInfo *exception)
560 % A description of each parameter follows.
562 % o file: An pointer to a FILE.
564 % o exception: return any errors or warnings in this structure.
567 MagickExport MagickBooleanType ListCoderInfo(FILE *file,
568 ExceptionInfo *exception)
585 if (file == (const FILE *) NULL)
587 coder_info=GetCoderInfoList("*",&number_coders,exception);
588 if (coder_info == (const CoderInfo **) NULL)
590 path=(const char *) NULL;
591 for (i=0; i < (ssize_t) number_coders; i++)
593 if (coder_info[i]->stealth != MagickFalse)
595 if ((path == (const char *) NULL) ||
596 (LocaleCompare(path,coder_info[i]->path) != 0))
598 if (coder_info[i]->path != (char *) NULL)
599 (void) FormatLocaleFile(file,"\nPath: %s\n\n",coder_info[i]->path);
600 (void) FormatLocaleFile(file,"Magick Coder\n");
601 (void) FormatLocaleFile(file,
602 "-------------------------------------------------"
603 "------------------------------\n");
605 path=coder_info[i]->path;
606 (void) FormatLocaleFile(file,"%s",coder_info[i]->magick);
607 for (j=(ssize_t) strlen(coder_info[i]->magick); j <= 11; j++)
608 (void) FormatLocaleFile(file," ");
609 if (coder_info[i]->name != (char *) NULL)
610 (void) FormatLocaleFile(file,"%s",coder_info[i]->name);
611 (void) FormatLocaleFile(file,"\n");
613 coder_info=(const CoderInfo **) RelinquishMagickMemory((void *) coder_info);
619 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
623 + L o a d C o d e r L i s t %
627 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
629 % LoadCoderList() loads the coder configuration file which provides a
630 % mapping between coder attributes and a coder name.
632 % The format of the LoadCoderList coder is:
634 % MagickBooleanType LoadCoderList(const char *xml,const char *filename,
635 % const size_t depth,ExceptionInfo *exception)
637 % A description of each parameter follows:
639 % o xml: The coder list in XML format.
641 % o filename: The coder list filename.
643 % o depth: depth of <include /> statements.
645 % o exception: return any errors or warnings in this structure.
649 static void *DestroyCoderNode(void *coder_info)
654 p=(CoderInfo *) coder_info;
655 if (p->exempt == MagickFalse)
657 if (p->path != (char *) NULL)
658 p->path=DestroyString(p->path);
659 if (p->name != (char *) NULL)
660 p->name=DestroyString(p->name);
661 if (p->magick != (char *) NULL)
662 p->magick=DestroyString(p->magick);
664 return(RelinquishMagickMemory(p));
667 static MagickBooleanType LoadCoderList(const char *xml,const char *filename,
668 const size_t depth,ExceptionInfo *exception)
671 keyword[MaxTextExtent],
684 Load the coder map file.
686 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
687 "Loading coder configuration file \"%s\" ...",filename);
688 if (xml == (const char *) NULL)
690 if (coder_list == (SplayTreeInfo *) NULL)
692 coder_list=NewSplayTree(CompareSplayTreeString,RelinquishMagickMemory,
694 if (coder_list == (SplayTreeInfo *) NULL)
696 ThrowFileException(exception,ResourceLimitError,
697 "MemoryAllocationFailed",filename);
702 coder_info=(CoderInfo *) NULL;
703 token=AcquireString(xml);
704 for (q=(char *) xml; *q != '\0'; )
709 GetMagickToken(q,&q,token);
712 (void) CopyMagickString(keyword,token,MaxTextExtent);
713 if (LocaleNCompare(keyword,"<!DOCTYPE",9) == 0)
718 while ((LocaleNCompare(q,"]>",2) != 0) && (*q != '\0'))
719 GetMagickToken(q,&q,token);
722 if (LocaleNCompare(keyword,"<!--",4) == 0)
727 while ((LocaleNCompare(q,"->",2) != 0) && (*q != '\0'))
728 GetMagickToken(q,&q,token);
731 if (LocaleCompare(keyword,"<include") == 0)
736 while (((*token != '/') && (*(token+1) != '>')) && (*q != '\0'))
738 (void) CopyMagickString(keyword,token,MaxTextExtent);
739 GetMagickToken(q,&q,token);
742 GetMagickToken(q,&q,token);
743 if (LocaleCompare(keyword,"file") == 0)
746 (void) ThrowMagickException(exception,GetMagickModule(),
747 ConfigureError,"IncludeNodeNestedTooDeeply","`%s'",token);
754 GetPathComponent(filename,HeadPath,path);
756 (void) ConcatenateMagickString(path,DirectorySeparator,
758 if (*token == *DirectorySeparator)
759 (void) CopyMagickString(path,token,MaxTextExtent);
761 (void) ConcatenateMagickString(path,token,MaxTextExtent);
762 xml=FileToString(path,~0UL,exception);
763 if (xml != (char *) NULL)
765 status=LoadCoderList(xml,path,depth+1,exception);
766 xml=(char *) RelinquishMagickMemory(xml);
773 if (LocaleCompare(keyword,"<coder") == 0)
778 coder_info=(CoderInfo *) AcquireMagickMemory(sizeof(*coder_info));
779 if (coder_info == (CoderInfo *) NULL)
780 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
781 (void) ResetMagickMemory(coder_info,0,sizeof(*coder_info));
782 coder_info->path=ConstantString(filename);
783 coder_info->exempt=MagickFalse;
784 coder_info->signature=MagickSignature;
787 if (coder_info == (CoderInfo *) NULL)
789 if (LocaleCompare(keyword,"/>") == 0)
791 status=AddValueToSplayTree(coder_list,ConstantString(
792 coder_info->magick),coder_info);
793 if (status == MagickFalse)
794 (void) ThrowMagickException(exception,GetMagickModule(),
795 ResourceLimitError,"MemoryAllocationFailed","`%s'",
797 coder_info=(CoderInfo *) NULL;
800 GetMagickToken(q,(const char **) NULL,token);
803 GetMagickToken(q,&q,token);
804 GetMagickToken(q,&q,token);
810 if (LocaleCompare((char *) keyword,"magick") == 0)
812 coder_info->magick=ConstantString(token);
820 if (LocaleCompare((char *) keyword,"name") == 0)
822 coder_info->name=ConstantString(token);
830 if (LocaleCompare((char *) keyword,"stealth") == 0)
832 coder_info->stealth=IsStringTrue(token);
841 token=(char *) RelinquishMagickMemory(token);
846 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
850 % L o a d C o d e r L i s t s %
854 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
856 % LoadCoderLists() loads one or more coder configuration file which
857 % provides a mapping between coder attributes and a coder name.
859 % The format of the LoadCoderLists coder is:
861 % MagickBooleanType LoadCoderLists(const char *filename,
862 % ExceptionInfo *exception)
864 % A description of each parameter follows:
866 % o filename: the font file name.
868 % o exception: return any errors or warnings in this structure.
871 static MagickBooleanType LoadCoderLists(const char *filename,
872 ExceptionInfo *exception)
887 Load external coder map.
889 if (coder_list == (SplayTreeInfo *) NULL)
891 coder_list=NewSplayTree(CompareSplayTreeString,RelinquishMagickMemory,
893 if (coder_list == (SplayTreeInfo *) NULL)
895 ThrowFileException(exception,ResourceLimitError,
896 "MemoryAllocationFailed",filename);
901 options=GetConfigureOptions(filename,exception);
902 option=(const StringInfo *) GetNextValueInLinkedList(options);
903 while (option != (const StringInfo *) NULL)
905 status&=LoadCoderList((const char *) GetStringInfoDatum(option),
906 GetStringInfoPath(option),0,exception);
907 option=(const StringInfo *) GetNextValueInLinkedList(options);
909 options=DestroyConfigureOptions(options);
911 Load built-in coder map.
913 for (i=0; i < (ssize_t) (sizeof(CoderMap)/sizeof(*CoderMap)); i++)
918 register const CoderMapInfo
922 coder_info=(CoderInfo *) AcquireMagickMemory(sizeof(*coder_info));
923 if (coder_info == (CoderInfo *) NULL)
925 (void) ThrowMagickException(exception,GetMagickModule(),
926 ResourceLimitError,"MemoryAllocationFailed","`%s'",p->name);
929 (void) ResetMagickMemory(coder_info,0,sizeof(*coder_info));
930 coder_info->path=(char *) "[built-in]";
931 coder_info->magick=(char *) p->magick;
932 coder_info->name=(char *) p->name;
933 coder_info->exempt=MagickTrue;
934 coder_info->signature=MagickSignature;
935 status&=AddValueToSplayTree(coder_list,ConstantString(coder_info->magick),
937 if (status == MagickFalse)
938 (void) ThrowMagickException(exception,GetMagickModule(),
939 ResourceLimitError,"MemoryAllocationFailed","`%s'",coder_info->name);
941 return(status != 0 ? MagickTrue : MagickFalse);