2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6 % CCCC OOO DDDD EEEEE RRRR %
10 % CCCC OOO DDDD EEEEE R R %
13 % MagickCore Image Coder Methods %
20 % Copyright 1999-2009 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 "magick/studio.h"
43 #include "magick/blob.h"
44 #include "magick/client.h"
45 #include "magick/coder.h"
46 #include "magick/configure.h"
47 #include "magick/draw.h"
48 #include "magick/exception.h"
49 #include "magick/exception-private.h"
50 #include "magick/hashmap.h"
51 #include "magick/log.h"
52 #include "magick/memory_.h"
53 #include "magick/option.h"
54 #include "magick/semaphore.h"
55 #include "magick/string_.h"
56 #include "magick/splay-tree.h"
57 #include "magick/token.h"
58 #include "magick/utility.h"
59 #include "magick/xml-tree.h"
64 #define MagickCoderFilename "coder.xml"
69 typedef struct _CoderMapInfo
79 static const CoderMapInfo
84 { "8BIMTEXT", "META" },
85 { "8BIMWTEXT", "META" },
89 { "APP1JPEG", "META" },
120 { "FRACTAL", "PLASMA" },
128 { "GRANITE", "MAGICK" },
129 { "GROUP4", "TIFF" },
139 { "IMPLICIT", "***" },
141 { "IPTCTEXT", "META" },
142 { "IPTCWTEXT", "META" },
143 { "ISOBRL", "BRAILLE" },
150 { "LOGO", "MAGICK" },
162 { "NETSCAPE", "MAGICK" },
187 { "RADIAL-GRADIENT", "GRADIENT" },
194 { "ROSE", "MAGICK" },
200 { "TIFF64", "TIFF" },
203 { "UBRL", "BRAILLE" },
207 { "WMFWIN32", "EMF" },
211 { "XTRNARRAY", "XTRN" },
212 { "XTRNBLOB", "XTRN" },
213 { "XTRNBSTR", "XTRN" },
214 { "XTRNFILE", "XTRN" },
215 { "XTRNIMAGE", "XTRN" },
216 { "XTRNSTREAM", "XTRN" },
219 { "YCbCrA", "YCbCr" }
223 *coder_semaphore = (SemaphoreInfo *) NULL;
226 *coder_list = (SplayTreeInfo *) NULL;
228 static volatile MagickBooleanType
229 instantiate_coder = MagickFalse;
232 Forward declarations.
234 static MagickBooleanType
235 InitializeCoderList(ExceptionInfo *),
236 LoadCoderLists(const char *,ExceptionInfo *);
239 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
243 + C o d e r C o m p o n e n t G e n e s i s %
247 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
249 % CoderComponentGenesis() instantiates the coder component.
251 % The format of the CoderComponentGenesis method is:
253 % MagickBooleanType CoderComponentGenesis(void)
256 MagickExport MagickBooleanType CoderComponentGenesis(void)
258 AcquireSemaphoreInfo(&coder_semaphore);
259 RelinquishSemaphoreInfo(coder_semaphore);
264 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
268 + C o d e r C o m p o n e n t T e r m i n u s %
272 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
274 % CoderComponentTerminus() destroys the coder component.
276 % The format of the CoderComponentTerminus method is:
278 % CoderComponentTerminus(void)
281 MagickExport void CoderComponentTerminus(void)
283 AcquireSemaphoreInfo(&coder_semaphore);
284 if (coder_list != (SplayTreeInfo *) NULL)
285 coder_list=DestroySplayTree(coder_list);
286 instantiate_coder=MagickFalse;
287 RelinquishSemaphoreInfo(coder_semaphore);
288 DestroySemaphoreInfo(&coder_semaphore);
292 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
296 + G e t C o d e r I n f o %
300 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
302 % GetCoderInfo searches the coder list for the specified name and if found
303 % returns attributes for that coder.
305 % The format of the GetCoderInfo method is:
307 % const CoderInfo *GetCoderInfo(const char *name,ExceptionInfo *exception)
309 % A description of each parameter follows:
311 % o name: the coder name.
313 % o exception: return any errors or warnings in this structure.
316 MagickExport const CoderInfo *GetCoderInfo(const char *name,
317 ExceptionInfo *exception)
319 assert(exception != (ExceptionInfo *) NULL);
320 if ((coder_list == (SplayTreeInfo *) NULL) ||
321 (instantiate_coder == MagickFalse))
322 if (InitializeCoderList(exception) == MagickFalse)
323 return((const CoderInfo *) NULL);
324 if ((coder_list == (SplayTreeInfo *) NULL) ||
325 (GetNumberOfNodesInSplayTree(coder_list) == 0))
326 return((const CoderInfo *) NULL);
327 if ((name == (const char *) NULL) || (LocaleCompare(name,"*") == 0))
329 ResetSplayTreeIterator(coder_list);
330 return((const CoderInfo *) GetNextValueInSplayTree(coder_list));
332 return((const CoderInfo *) GetValueFromSplayTree(coder_list,name));
336 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
340 % G e t C o d e r I n f o L i s t %
344 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
346 % GetCoderInfoList() returns any coder_map that match the specified pattern.
347 % The format of the GetCoderInfoList function is:
349 % const CoderInfo **GetCoderInfoList(const char *pattern,
350 % unsigned long *number_coders,ExceptionInfo *exception)
352 % A description of each parameter follows:
354 % o pattern: Specifies a pointer to a text string containing a pattern.
356 % o number_coders: This integer returns the number of coders in the list.
358 % o exception: return any errors or warnings in this structure.
362 static int CoderInfoCompare(const void *x,const void *y)
368 p=(const CoderInfo **) x,
369 q=(const CoderInfo **) y;
370 if (LocaleCompare((*p)->path,(*q)->path) == 0)
371 return(LocaleCompare((*p)->name,(*q)->name));
372 return(LocaleCompare((*p)->path,(*q)->path));
375 MagickExport const CoderInfo **GetCoderInfoList(const char *pattern,
376 unsigned long *number_coders,ExceptionInfo *exception)
381 register const CoderInfo
390 assert(pattern != (char *) NULL);
391 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
392 assert(number_coders != (unsigned long *) NULL);
394 p=GetCoderInfo("*",exception);
395 if (p == (const CoderInfo *) NULL)
396 return((const CoderInfo **) NULL);
397 coder_map=(const CoderInfo **) AcquireQuantumMemory((size_t)
398 GetNumberOfNodesInSplayTree(coder_list)+1UL,sizeof(*coder_map));
399 if (coder_map == (const CoderInfo **) NULL)
400 return((const CoderInfo **) NULL);
404 AcquireSemaphoreInfo(&coder_semaphore);
405 ResetSplayTreeIterator(coder_list);
406 p=(const CoderInfo *) GetNextValueInSplayTree(coder_list);
407 for (i=0; p != (const CoderInfo *) NULL; )
409 if ((p->stealth == MagickFalse) &&
410 (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
412 p=(const CoderInfo *) GetNextValueInSplayTree(coder_list);
414 RelinquishSemaphoreInfo(coder_semaphore);
415 qsort((void *) coder_map,(size_t) i,sizeof(*coder_map),CoderInfoCompare);
416 coder_map[i]=(CoderInfo *) NULL;
417 *number_coders=(unsigned long) i;
422 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
426 % G e t C o d e r L i s t %
430 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
432 % GetCoderList() returns any coder_map that match the specified pattern.
434 % The format of the GetCoderList function is:
436 % char **GetCoderList(const char *pattern,unsigned long *number_coders,
437 % ExceptionInfo *exception)
439 % A description of each parameter follows:
441 % o pattern: Specifies a pointer to a text string containing a pattern.
443 % o number_coders: This integer returns the number of coders in the list.
445 % o exception: return any errors or warnings in this structure.
449 static int CoderCompare(const void *x,const void *y)
457 return(LocaleCompare(*p,*q));
460 MagickExport char **GetCoderList(const char *pattern,
461 unsigned long *number_coders,ExceptionInfo *exception)
466 register const CoderInfo
475 assert(pattern != (char *) NULL);
476 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
477 assert(number_coders != (unsigned long *) NULL);
479 p=GetCoderInfo("*",exception);
480 if (p == (const CoderInfo *) NULL)
481 return((char **) NULL);
482 coder_map=(char **) AcquireQuantumMemory((size_t)
483 GetNumberOfNodesInSplayTree(coder_list)+1UL,sizeof(*coder_map));
484 if (coder_map == (char **) NULL)
485 return((char **) NULL);
489 AcquireSemaphoreInfo(&coder_semaphore);
490 ResetSplayTreeIterator(coder_list);
491 p=(const CoderInfo *) GetNextValueInSplayTree(coder_list);
492 for (i=0; p != (const CoderInfo *) NULL; )
494 if ((p->stealth == MagickFalse) &&
495 (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
496 coder_map[i++]=ConstantString(p->name);
497 p=(const CoderInfo *) GetNextValueInSplayTree(coder_list);
499 RelinquishSemaphoreInfo(coder_semaphore);
500 qsort((void *) coder_map,(size_t) i,sizeof(*coder_map),CoderCompare);
501 coder_map[i]=(char *) NULL;
502 *number_coders=(unsigned long) i;
507 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
511 + I n i t i a l i z e C o d e r L i s t %
515 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
517 % InitializeCoderList() initializes the coder list.
519 % The format of the InitializeCoderList method is:
521 % MagickBooleanType InitializeCoderList(ExceptionInfo *exception)
523 % A description of each parameter follows.
525 % o exception: return any errors or warnings in this structure.
528 static MagickBooleanType InitializeCoderList(ExceptionInfo *exception)
530 if ((coder_list == (SplayTreeInfo *) NULL) &&
531 (instantiate_coder == MagickFalse))
533 AcquireSemaphoreInfo(&coder_semaphore);
534 if ((coder_list == (SplayTreeInfo *) NULL) &&
535 (instantiate_coder == MagickFalse))
537 (void) LoadCoderLists(MagickCoderFilename,exception);
538 instantiate_coder=MagickTrue;
540 RelinquishSemaphoreInfo(coder_semaphore);
542 return(coder_list != (SplayTreeInfo *) NULL ? MagickTrue : MagickFalse);
546 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
550 % L i s t C o d e r I n f o %
554 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
556 % ListCoderInfo() lists the coder info to a file.
558 % The format of the ListCoderInfo coder is:
560 % MagickBooleanType ListCoderInfo(FILE *file,ExceptionInfo *exception)
562 % A description of each parameter follows.
564 % o file: An pointer to a FILE.
566 % o exception: return any errors or warnings in this structure.
569 MagickExport MagickBooleanType ListCoderInfo(FILE *file,
570 ExceptionInfo *exception)
587 if (file == (const FILE *) NULL)
589 coder_info=GetCoderInfoList("*",&number_coders,exception);
590 if (coder_info == (const CoderInfo **) NULL)
592 path=(const char *) NULL;
593 for (i=0; i < (long) number_coders; i++)
595 if (coder_info[i]->stealth != MagickFalse)
597 if ((path == (const char *) NULL) ||
598 (LocaleCompare(path,coder_info[i]->path) != 0))
600 if (coder_info[i]->path != (char *) NULL)
601 (void) fprintf(file,"\nPath: %s\n\n",coder_info[i]->path);
602 (void) fprintf(file,"Magick Coder\n");
603 (void) fprintf(file,"-------------------------------------------------"
604 "------------------------------\n");
606 path=coder_info[i]->path;
607 (void) fprintf(file,"%s",coder_info[i]->magick);
608 for (j=(long) strlen(coder_info[i]->magick); j <= 11; j++)
609 (void) fprintf(file," ");
610 if (coder_info[i]->name != (char *) NULL)
611 (void) fprintf(file,"%s",coder_info[i]->name);
612 (void) fprintf(file,"\n");
614 coder_info=(const CoderInfo **) RelinquishMagickMemory((void *) coder_info);
620 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
624 + L o a d C o d e r L i s t %
628 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
630 % LoadCoderList() loads the coder configuration file which provides a
631 % mapping between coder attributes and a coder name.
633 % The format of the LoadCoderList coder is:
635 % MagickBooleanType LoadCoderList(const char *xml,const char *filename,
636 % const unsigned long depth,ExceptionInfo *exception)
638 % A description of each parameter follows:
640 % o xml: The coder list in XML format.
642 % o filename: The coder list filename.
644 % o depth: depth of <include /> statements.
646 % o exception: return any errors or warnings in this structure.
650 static void *DestroyCoderNode(void *coder_info)
655 p=(CoderInfo *) coder_info;
656 if (p->exempt == MagickFalse)
658 if (p->path != (char *) NULL)
659 p->path=DestroyString(p->path);
660 if (p->name != (char *) NULL)
661 p->name=DestroyString(p->name);
662 if (p->magick != (char *) NULL)
663 p->magick=DestroyString(p->magick);
665 return(RelinquishMagickMemory(p));
668 static MagickBooleanType LoadCoderList(const char *xml,const char *filename,
669 const unsigned long depth,ExceptionInfo *exception)
672 keyword[MaxTextExtent],
685 Load the coder map file.
687 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
688 "Loading coder configuration file \"%s\" ...",filename);
689 if (xml == (const char *) NULL)
691 if (coder_list == (SplayTreeInfo *) NULL)
693 coder_list=NewSplayTree(CompareSplayTreeString,RelinquishMagickMemory,
695 if (coder_list == (SplayTreeInfo *) NULL)
697 ThrowFileException(exception,ResourceLimitError,
698 "MemoryAllocationFailed",filename);
703 coder_info=(CoderInfo *) NULL;
704 token=AcquireString(xml);
705 for (q=(char *) xml; *q != '\0'; )
710 GetMagickToken(q,&q,token);
713 (void) CopyMagickString(keyword,token,MaxTextExtent);
714 if (LocaleNCompare(keyword,"<!DOCTYPE",9) == 0)
719 while ((LocaleNCompare(q,"]>",2) != 0) && (*q != '\0'))
720 GetMagickToken(q,&q,token);
723 if (LocaleNCompare(keyword,"<!--",4) == 0)
728 while ((LocaleNCompare(q,"->",2) != 0) && (*q != '\0'))
729 GetMagickToken(q,&q,token);
732 if (LocaleCompare(keyword,"<include") == 0)
737 while (((*token != '/') && (*(token+1) != '>')) && (*q != '\0'))
739 (void) CopyMagickString(keyword,token,MaxTextExtent);
740 GetMagickToken(q,&q,token);
743 GetMagickToken(q,&q,token);
744 if (LocaleCompare(keyword,"file") == 0)
747 (void) ThrowMagickException(exception,GetMagickModule(),
748 ConfigureError,"IncludeNodeNestedTooDeeply","`%s'",token);
755 GetPathComponent(filename,HeadPath,path);
757 (void) ConcatenateMagickString(path,DirectorySeparator,
759 if (*token == *DirectorySeparator)
760 (void) CopyMagickString(path,token,MaxTextExtent);
762 (void) ConcatenateMagickString(path,token,MaxTextExtent);
763 xml=FileToString(path,~0,exception);
764 if (xml != (char *) NULL)
766 status=LoadCoderList(xml,path,depth+1,exception);
767 xml=(char *) RelinquishMagickMemory(xml);
774 if (LocaleCompare(keyword,"<coder") == 0)
779 coder_info=(CoderInfo *) AcquireMagickMemory(sizeof(*coder_info));
780 if (coder_info == (CoderInfo *) NULL)
781 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
782 (void) ResetMagickMemory(coder_info,0,sizeof(*coder_info));
783 coder_info->path=ConstantString(filename);
784 coder_info->exempt=MagickFalse;
785 coder_info->signature=MagickSignature;
788 if (coder_info == (CoderInfo *) NULL)
790 if (LocaleCompare(keyword,"/>") == 0)
792 status=AddValueToSplayTree(coder_list,ConstantString(
793 coder_info->magick),coder_info);
794 if (status == MagickFalse)
795 (void) ThrowMagickException(exception,GetMagickModule(),
796 ResourceLimitError,"MemoryAllocationFailed","`%s'",
798 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=IsMagickTrue(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 built-in coder map.
890 if (coder_list == (SplayTreeInfo *) NULL)
892 coder_list=NewSplayTree(CompareSplayTreeString,RelinquishMagickMemory,
894 if (coder_list == (SplayTreeInfo *) NULL)
896 ThrowFileException(exception,ResourceLimitError,
897 "MemoryAllocationFailed",filename);
901 for (i=0; i < (long) (sizeof(CoderMap)/sizeof(*CoderMap)); i++)
906 register const CoderMapInfo
910 coder_info=(CoderInfo *) AcquireMagickMemory(sizeof(*coder_info));
911 if (coder_info == (CoderInfo *) NULL)
913 (void) ThrowMagickException(exception,GetMagickModule(),
914 ResourceLimitError,"MemoryAllocationFailed","`%s'",coder_info->name);
917 (void) ResetMagickMemory(coder_info,0,sizeof(*coder_info));
918 coder_info->path=(char *) "[built-in]";
919 coder_info->magick=(char *) p->magick;
920 coder_info->name=(char *) p->name;
921 coder_info->exempt=MagickTrue;
922 coder_info->signature=MagickSignature;
923 status=AddValueToSplayTree(coder_list,ConstantString(coder_info->magick),
925 if (status == MagickFalse)
926 (void) ThrowMagickException(exception,GetMagickModule(),
927 ResourceLimitError,"MemoryAllocationFailed","`%s'",coder_info->name);
930 Load external coder map.
932 options=GetConfigureOptions(filename,exception);
933 option=(const StringInfo *) GetNextValueInLinkedList(options);
934 while (option != (const StringInfo *) NULL)
936 status|=LoadCoderList((const char *) GetStringInfoDatum(option),
937 GetStringInfoPath(option),0,exception);
938 option=(const StringInfo *) GetNextValueInLinkedList(options);
940 options=DestroyConfigureOptions(options);
941 return(status != 0 ? MagickTrue : MagickFalse);