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 + D e s t r o y C o d e r C o m p o n e n t %
247 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
249 % DestroyCoderComponent() destroys the coder component.
251 % The format of the DestroyCoderComponent method is:
253 % DestroyCoderComponent(void)
256 MagickExport void DestroyCoderComponent(void)
258 AcquireSemaphoreInfo(&coder_semaphore);
259 if (coder_list != (SplayTreeInfo *) NULL)
260 coder_list=DestroySplayTree(coder_list);
261 instantiate_coder=MagickFalse;
262 RelinquishSemaphoreInfo(coder_semaphore);
263 DestroySemaphoreInfo(&coder_semaphore);
267 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
271 + G e t C o d e r I n f o %
275 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
277 % GetCoderInfo searches the coder list for the specified name and if found
278 % returns attributes for that coder.
280 % The format of the GetCoderInfo method is:
282 % const CoderInfo *GetCoderInfo(const char *name,ExceptionInfo *exception)
284 % A description of each parameter follows:
286 % o name: the coder name.
288 % o exception: return any errors or warnings in this structure.
291 MagickExport const CoderInfo *GetCoderInfo(const char *name,
292 ExceptionInfo *exception)
294 assert(exception != (ExceptionInfo *) NULL);
295 if ((coder_list == (SplayTreeInfo *) NULL) ||
296 (instantiate_coder == MagickFalse))
297 if (InitializeCoderList(exception) == MagickFalse)
298 return((const CoderInfo *) NULL);
299 if ((coder_list == (SplayTreeInfo *) NULL) ||
300 (GetNumberOfNodesInSplayTree(coder_list) == 0))
301 return((const CoderInfo *) NULL);
302 if ((name == (const char *) NULL) || (LocaleCompare(name,"*") == 0))
304 ResetSplayTreeIterator(coder_list);
305 return((const CoderInfo *) GetNextValueInSplayTree(coder_list));
307 return((const CoderInfo *) GetValueFromSplayTree(coder_list,name));
311 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
315 % G e t C o d e r I n f o L i s t %
319 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
321 % GetCoderInfoList() returns any coder_map that match the specified pattern.
322 % The format of the GetCoderInfoList function is:
324 % const CoderInfo **GetCoderInfoList(const char *pattern,
325 % unsigned long *number_coders,ExceptionInfo *exception)
327 % A description of each parameter follows:
329 % o pattern: Specifies a pointer to a text string containing a pattern.
331 % o number_coders: This integer returns the number of coders in the list.
333 % o exception: return any errors or warnings in this structure.
337 static int CoderInfoCompare(const void *x,const void *y)
343 p=(const CoderInfo **) x,
344 q=(const CoderInfo **) y;
345 if (LocaleCompare((*p)->path,(*q)->path) == 0)
346 return(LocaleCompare((*p)->name,(*q)->name));
347 return(LocaleCompare((*p)->path,(*q)->path));
350 MagickExport const CoderInfo **GetCoderInfoList(const char *pattern,
351 unsigned long *number_coders,ExceptionInfo *exception)
356 register const CoderInfo
365 assert(pattern != (char *) NULL);
366 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
367 assert(number_coders != (unsigned long *) NULL);
369 p=GetCoderInfo("*",exception);
370 if (p == (const CoderInfo *) NULL)
371 return((const CoderInfo **) NULL);
372 coder_map=(const CoderInfo **) AcquireQuantumMemory((size_t)
373 GetNumberOfNodesInSplayTree(coder_list)+1UL,sizeof(*coder_map));
374 if (coder_map == (const CoderInfo **) NULL)
375 return((const CoderInfo **) NULL);
379 AcquireSemaphoreInfo(&coder_semaphore);
380 ResetSplayTreeIterator(coder_list);
381 p=(const CoderInfo *) GetNextValueInSplayTree(coder_list);
382 for (i=0; p != (const CoderInfo *) NULL; )
384 if ((p->stealth == MagickFalse) &&
385 (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
387 p=(const CoderInfo *) GetNextValueInSplayTree(coder_list);
389 RelinquishSemaphoreInfo(coder_semaphore);
390 qsort((void *) coder_map,(size_t) i,sizeof(*coder_map),CoderInfoCompare);
391 coder_map[i]=(CoderInfo *) NULL;
392 *number_coders=(unsigned long) i;
397 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
401 % G e t C o d e r L i s t %
405 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
407 % GetCoderList() returns any coder_map that match the specified pattern.
409 % The format of the GetCoderList function is:
411 % char **GetCoderList(const char *pattern,unsigned long *number_coders,
412 % ExceptionInfo *exception)
414 % A description of each parameter follows:
416 % o pattern: Specifies a pointer to a text string containing a pattern.
418 % o number_coders: This integer returns the number of coders in the list.
420 % o exception: return any errors or warnings in this structure.
424 static int CoderCompare(const void *x,const void *y)
432 return(LocaleCompare(*p,*q));
435 MagickExport char **GetCoderList(const char *pattern,
436 unsigned long *number_coders,ExceptionInfo *exception)
441 register const CoderInfo
450 assert(pattern != (char *) NULL);
451 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
452 assert(number_coders != (unsigned long *) NULL);
454 p=GetCoderInfo("*",exception);
455 if (p == (const CoderInfo *) NULL)
456 return((char **) NULL);
457 coder_map=(char **) AcquireQuantumMemory((size_t)
458 GetNumberOfNodesInSplayTree(coder_list)+1UL,sizeof(*coder_map));
459 if (coder_map == (char **) NULL)
460 return((char **) NULL);
464 AcquireSemaphoreInfo(&coder_semaphore);
465 ResetSplayTreeIterator(coder_list);
466 p=(const CoderInfo *) GetNextValueInSplayTree(coder_list);
467 for (i=0; p != (const CoderInfo *) NULL; )
469 if ((p->stealth == MagickFalse) &&
470 (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
471 coder_map[i++]=ConstantString(p->name);
472 p=(const CoderInfo *) GetNextValueInSplayTree(coder_list);
474 RelinquishSemaphoreInfo(coder_semaphore);
475 qsort((void *) coder_map,(size_t) i,sizeof(*coder_map),CoderCompare);
476 coder_map[i]=(char *) NULL;
477 *number_coders=(unsigned long) i;
482 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
486 + I n i t i a l i z e C o d e r L i s t %
490 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
492 % InitializeCoderList() initializes the coder list.
494 % The format of the InitializeCoderList method is:
496 % MagickBooleanType InitializeCoderList(ExceptionInfo *exception)
498 % A description of each parameter follows.
500 % o exception: return any errors or warnings in this structure.
503 static MagickBooleanType InitializeCoderList(ExceptionInfo *exception)
505 if ((coder_list == (SplayTreeInfo *) NULL) &&
506 (instantiate_coder == MagickFalse))
508 AcquireSemaphoreInfo(&coder_semaphore);
509 if ((coder_list == (SplayTreeInfo *) NULL) &&
510 (instantiate_coder == MagickFalse))
512 (void) LoadCoderLists(MagickCoderFilename,exception);
513 instantiate_coder=MagickTrue;
515 RelinquishSemaphoreInfo(coder_semaphore);
517 return(coder_list != (SplayTreeInfo *) NULL ? MagickTrue : MagickFalse);
521 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
525 + I n s t a n t i a t e C o d e r C o m p o n e n t %
529 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
531 % InstantiateCoderComponent() instantiates the coder component.
533 % The format of the InstantiateCoderComponent method is:
535 % MagickBooleanType InstantiateCoderComponent(void)
538 MagickExport MagickBooleanType InstantiateCoderComponent(void)
540 AcquireSemaphoreInfo(&coder_semaphore);
541 RelinquishSemaphoreInfo(coder_semaphore);
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);