2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6 % CCCC OOO DDDD EEEEE RRRR %
10 % CCCC OOO DDDD EEEEE R R %
13 % MagickCore Image Coder Methods %
20 % Copyright 1999-2010 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" },
151 { "LOGO", "MAGICK" },
163 { "NETSCAPE", "MAGICK" },
188 { "RADIAL-GRADIENT", "GRADIENT" },
195 { "ROSE", "MAGICK" },
201 { "TIFF64", "TIFF" },
204 { "UBRL", "BRAILLE" },
208 { "WMFWIN32", "EMF" },
212 { "XTRNARRAY", "XTRN" },
213 { "XTRNBLOB", "XTRN" },
214 { "XTRNBSTR", "XTRN" },
215 { "XTRNFILE", "XTRN" },
216 { "XTRNIMAGE", "XTRN" },
217 { "XTRNSTREAM", "XTRN" },
220 { "YCbCrA", "YCbCr" }
224 *coder_semaphore = (SemaphoreInfo *) NULL;
227 *coder_list = (SplayTreeInfo *) NULL;
229 static volatile MagickBooleanType
230 instantiate_coder = MagickFalse;
233 Forward declarations.
235 static MagickBooleanType
236 InitializeCoderList(ExceptionInfo *),
237 LoadCoderLists(const char *,ExceptionInfo *);
240 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
244 + C o d e r C o m p o n e n t G e n e s i s %
248 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
250 % CoderComponentGenesis() instantiates the coder component.
252 % The format of the CoderComponentGenesis method is:
254 % MagickBooleanType CoderComponentGenesis(void)
257 MagickExport MagickBooleanType CoderComponentGenesis(void)
259 AcquireSemaphoreInfo(&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 if (coder_semaphore == (SemaphoreInfo *) NULL)
284 AcquireSemaphoreInfo(&coder_semaphore);
285 LockSemaphoreInfo(coder_semaphore);
286 if (coder_list != (SplayTreeInfo *) NULL)
287 coder_list=DestroySplayTree(coder_list);
288 instantiate_coder=MagickFalse;
289 UnlockSemaphoreInfo(coder_semaphore);
290 DestroySemaphoreInfo(&coder_semaphore);
294 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
298 + G e t C o d e r I n f o %
302 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
304 % GetCoderInfo searches the coder list for the specified name and if found
305 % returns attributes for that coder.
307 % The format of the GetCoderInfo method is:
309 % const CoderInfo *GetCoderInfo(const char *name,ExceptionInfo *exception)
311 % A description of each parameter follows:
313 % o name: the coder name.
315 % o exception: return any errors or warnings in this structure.
318 MagickExport const CoderInfo *GetCoderInfo(const char *name,
319 ExceptionInfo *exception)
321 assert(exception != (ExceptionInfo *) NULL);
322 if ((coder_list == (SplayTreeInfo *) NULL) ||
323 (instantiate_coder == MagickFalse))
324 if (InitializeCoderList(exception) == MagickFalse)
325 return((const CoderInfo *) NULL);
326 if ((coder_list == (SplayTreeInfo *) NULL) ||
327 (GetNumberOfNodesInSplayTree(coder_list) == 0))
328 return((const CoderInfo *) NULL);
329 if ((name == (const char *) NULL) || (LocaleCompare(name,"*") == 0))
331 ResetSplayTreeIterator(coder_list);
332 return((const CoderInfo *) GetNextValueInSplayTree(coder_list));
334 return((const CoderInfo *) GetValueFromSplayTree(coder_list,name));
338 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
342 % G e t C o d e r I n f o L i s t %
346 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
348 % GetCoderInfoList() returns any coder_map that match the specified pattern.
349 % The format of the GetCoderInfoList function is:
351 % const CoderInfo **GetCoderInfoList(const char *pattern,
352 % unsigned long *number_coders,ExceptionInfo *exception)
354 % A description of each parameter follows:
356 % o pattern: Specifies a pointer to a text string containing a pattern.
358 % o number_coders: This integer returns the number of coders in the list.
360 % o exception: return any errors or warnings in this structure.
364 static int CoderInfoCompare(const void *x,const void *y)
370 p=(const CoderInfo **) x,
371 q=(const CoderInfo **) y;
372 if (LocaleCompare((*p)->path,(*q)->path) == 0)
373 return(LocaleCompare((*p)->name,(*q)->name));
374 return(LocaleCompare((*p)->path,(*q)->path));
377 MagickExport const CoderInfo **GetCoderInfoList(const char *pattern,
378 unsigned long *number_coders,ExceptionInfo *exception)
383 register const CoderInfo
392 assert(pattern != (char *) NULL);
393 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
394 assert(number_coders != (unsigned long *) NULL);
396 p=GetCoderInfo("*",exception);
397 if (p == (const CoderInfo *) NULL)
398 return((const CoderInfo **) NULL);
399 coder_map=(const CoderInfo **) AcquireQuantumMemory((size_t)
400 GetNumberOfNodesInSplayTree(coder_list)+1UL,sizeof(*coder_map));
401 if (coder_map == (const CoderInfo **) NULL)
402 return((const CoderInfo **) NULL);
406 LockSemaphoreInfo(coder_semaphore);
407 ResetSplayTreeIterator(coder_list);
408 p=(const CoderInfo *) GetNextValueInSplayTree(coder_list);
409 for (i=0; p != (const CoderInfo *) NULL; )
411 if ((p->stealth == MagickFalse) &&
412 (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
414 p=(const CoderInfo *) GetNextValueInSplayTree(coder_list);
416 UnlockSemaphoreInfo(coder_semaphore);
417 qsort((void *) coder_map,(size_t) i,sizeof(*coder_map),CoderInfoCompare);
418 coder_map[i]=(CoderInfo *) NULL;
419 *number_coders=(unsigned long) i;
424 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
428 % G e t C o d e r L i s t %
432 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
434 % GetCoderList() returns any coder_map that match the specified pattern.
436 % The format of the GetCoderList function is:
438 % char **GetCoderList(const char *pattern,unsigned long *number_coders,
439 % ExceptionInfo *exception)
441 % A description of each parameter follows:
443 % o pattern: Specifies a pointer to a text string containing a pattern.
445 % o number_coders: This integer returns the number of coders in the list.
447 % o exception: return any errors or warnings in this structure.
451 static int CoderCompare(const void *x,const void *y)
459 return(LocaleCompare(*p,*q));
462 MagickExport char **GetCoderList(const char *pattern,
463 unsigned long *number_coders,ExceptionInfo *exception)
468 register const CoderInfo
477 assert(pattern != (char *) NULL);
478 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
479 assert(number_coders != (unsigned long *) NULL);
481 p=GetCoderInfo("*",exception);
482 if (p == (const CoderInfo *) NULL)
483 return((char **) NULL);
484 coder_map=(char **) AcquireQuantumMemory((size_t)
485 GetNumberOfNodesInSplayTree(coder_list)+1UL,sizeof(*coder_map));
486 if (coder_map == (char **) NULL)
487 return((char **) NULL);
491 LockSemaphoreInfo(coder_semaphore);
492 ResetSplayTreeIterator(coder_list);
493 p=(const CoderInfo *) GetNextValueInSplayTree(coder_list);
494 for (i=0; p != (const CoderInfo *) NULL; )
496 if ((p->stealth == MagickFalse) &&
497 (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
498 coder_map[i++]=ConstantString(p->name);
499 p=(const CoderInfo *) GetNextValueInSplayTree(coder_list);
501 UnlockSemaphoreInfo(coder_semaphore);
502 qsort((void *) coder_map,(size_t) i,sizeof(*coder_map),CoderCompare);
503 coder_map[i]=(char *) NULL;
504 *number_coders=(unsigned long) i;
509 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
513 + I n i t i a l i z e C o d e r L i s t %
517 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
519 % InitializeCoderList() initializes the coder list.
521 % The format of the InitializeCoderList method is:
523 % MagickBooleanType InitializeCoderList(ExceptionInfo *exception)
525 % A description of each parameter follows.
527 % o exception: return any errors or warnings in this structure.
530 static MagickBooleanType InitializeCoderList(ExceptionInfo *exception)
532 if ((coder_list == (SplayTreeInfo *) NULL) &&
533 (instantiate_coder == MagickFalse))
535 if (coder_semaphore == (SemaphoreInfo *) NULL)
536 AcquireSemaphoreInfo(&coder_semaphore);
537 LockSemaphoreInfo(coder_semaphore);
538 if ((coder_list == (SplayTreeInfo *) NULL) &&
539 (instantiate_coder == MagickFalse))
541 (void) LoadCoderLists(MagickCoderFilename,exception);
542 instantiate_coder=MagickTrue;
544 UnlockSemaphoreInfo(coder_semaphore);
546 return(coder_list != (SplayTreeInfo *) NULL ? MagickTrue : MagickFalse);
550 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
554 % L i s t C o d e r I n f o %
558 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
560 % ListCoderInfo() lists the coder info to a file.
562 % The format of the ListCoderInfo coder is:
564 % MagickBooleanType ListCoderInfo(FILE *file,ExceptionInfo *exception)
566 % A description of each parameter follows.
568 % o file: An pointer to a FILE.
570 % o exception: return any errors or warnings in this structure.
573 MagickExport MagickBooleanType ListCoderInfo(FILE *file,
574 ExceptionInfo *exception)
591 if (file == (const FILE *) NULL)
593 coder_info=GetCoderInfoList("*",&number_coders,exception);
594 if (coder_info == (const CoderInfo **) NULL)
596 path=(const char *) NULL;
597 for (i=0; i < (long) number_coders; i++)
599 if (coder_info[i]->stealth != MagickFalse)
601 if ((path == (const char *) NULL) ||
602 (LocaleCompare(path,coder_info[i]->path) != 0))
604 if (coder_info[i]->path != (char *) NULL)
605 (void) fprintf(file,"\nPath: %s\n\n",coder_info[i]->path);
606 (void) fprintf(file,"Magick Coder\n");
607 (void) fprintf(file,"-------------------------------------------------"
608 "------------------------------\n");
610 path=coder_info[i]->path;
611 (void) fprintf(file,"%s",coder_info[i]->magick);
612 for (j=(long) strlen(coder_info[i]->magick); j <= 11; j++)
613 (void) fprintf(file," ");
614 if (coder_info[i]->name != (char *) NULL)
615 (void) fprintf(file,"%s",coder_info[i]->name);
616 (void) fprintf(file,"\n");
618 coder_info=(const CoderInfo **) RelinquishMagickMemory((void *) coder_info);
624 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
628 + L o a d C o d e r L i s t %
632 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
634 % LoadCoderList() loads the coder configuration file which provides a
635 % mapping between coder attributes and a coder name.
637 % The format of the LoadCoderList coder is:
639 % MagickBooleanType LoadCoderList(const char *xml,const char *filename,
640 % const unsigned long depth,ExceptionInfo *exception)
642 % A description of each parameter follows:
644 % o xml: The coder list in XML format.
646 % o filename: The coder list filename.
648 % o depth: depth of <include /> statements.
650 % o exception: return any errors or warnings in this structure.
654 static void *DestroyCoderNode(void *coder_info)
659 p=(CoderInfo *) coder_info;
660 if (p->exempt == MagickFalse)
662 if (p->path != (char *) NULL)
663 p->path=DestroyString(p->path);
664 if (p->name != (char *) NULL)
665 p->name=DestroyString(p->name);
666 if (p->magick != (char *) NULL)
667 p->magick=DestroyString(p->magick);
669 return(RelinquishMagickMemory(p));
672 static MagickBooleanType LoadCoderList(const char *xml,const char *filename,
673 const unsigned long depth,ExceptionInfo *exception)
676 keyword[MaxTextExtent],
689 Load the coder map file.
691 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
692 "Loading coder configuration file \"%s\" ...",filename);
693 if (xml == (const char *) NULL)
695 if (coder_list == (SplayTreeInfo *) NULL)
697 coder_list=NewSplayTree(CompareSplayTreeString,RelinquishMagickMemory,
699 if (coder_list == (SplayTreeInfo *) NULL)
701 ThrowFileException(exception,ResourceLimitError,
702 "MemoryAllocationFailed",filename);
707 coder_info=(CoderInfo *) NULL;
708 token=AcquireString(xml);
709 for (q=(char *) xml; *q != '\0'; )
714 GetMagickToken(q,&q,token);
717 (void) CopyMagickString(keyword,token,MaxTextExtent);
718 if (LocaleNCompare(keyword,"<!DOCTYPE",9) == 0)
723 while ((LocaleNCompare(q,"]>",2) != 0) && (*q != '\0'))
724 GetMagickToken(q,&q,token);
727 if (LocaleNCompare(keyword,"<!--",4) == 0)
732 while ((LocaleNCompare(q,"->",2) != 0) && (*q != '\0'))
733 GetMagickToken(q,&q,token);
736 if (LocaleCompare(keyword,"<include") == 0)
741 while (((*token != '/') && (*(token+1) != '>')) && (*q != '\0'))
743 (void) CopyMagickString(keyword,token,MaxTextExtent);
744 GetMagickToken(q,&q,token);
747 GetMagickToken(q,&q,token);
748 if (LocaleCompare(keyword,"file") == 0)
751 (void) ThrowMagickException(exception,GetMagickModule(),
752 ConfigureError,"IncludeNodeNestedTooDeeply","`%s'",token);
759 GetPathComponent(filename,HeadPath,path);
761 (void) ConcatenateMagickString(path,DirectorySeparator,
763 if (*token == *DirectorySeparator)
764 (void) CopyMagickString(path,token,MaxTextExtent);
766 (void) ConcatenateMagickString(path,token,MaxTextExtent);
767 xml=FileToString(path,~0,exception);
768 if (xml != (char *) NULL)
770 status=LoadCoderList(xml,path,depth+1,exception);
771 xml=(char *) RelinquishMagickMemory(xml);
778 if (LocaleCompare(keyword,"<coder") == 0)
783 coder_info=(CoderInfo *) AcquireAlignedMemory(1,sizeof(*coder_info));
784 if (coder_info == (CoderInfo *) NULL)
785 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
786 (void) ResetMagickMemory(coder_info,0,sizeof(*coder_info));
787 coder_info->path=ConstantString(filename);
788 coder_info->exempt=MagickFalse;
789 coder_info->signature=MagickSignature;
792 if (coder_info == (CoderInfo *) NULL)
794 if (LocaleCompare(keyword,"/>") == 0)
796 status=AddValueToSplayTree(coder_list,ConstantString(
797 coder_info->magick),coder_info);
798 if (status == MagickFalse)
799 (void) ThrowMagickException(exception,GetMagickModule(),
800 ResourceLimitError,"MemoryAllocationFailed","`%s'",
802 coder_info=(CoderInfo *) NULL;
804 GetMagickToken(q,(const char **) NULL,token);
807 GetMagickToken(q,&q,token);
808 GetMagickToken(q,&q,token);
814 if (LocaleCompare((char *) keyword,"magick") == 0)
816 coder_info->magick=ConstantString(token);
824 if (LocaleCompare((char *) keyword,"name") == 0)
826 coder_info->name=ConstantString(token);
834 if (LocaleCompare((char *) keyword,"stealth") == 0)
836 coder_info->stealth=IsMagickTrue(token);
845 token=(char *) RelinquishMagickMemory(token);
850 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
854 % L o a d C o d e r L i s t s %
858 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
860 % LoadCoderLists() loads one or more coder configuration file which
861 % provides a mapping between coder attributes and a coder name.
863 % The format of the LoadCoderLists coder is:
865 % MagickBooleanType LoadCoderLists(const char *filename,
866 % ExceptionInfo *exception)
868 % A description of each parameter follows:
870 % o filename: the font file name.
872 % o exception: return any errors or warnings in this structure.
875 static MagickBooleanType LoadCoderLists(const char *filename,
876 ExceptionInfo *exception)
891 Load built-in coder map.
894 if (coder_list == (SplayTreeInfo *) NULL)
896 coder_list=NewSplayTree(CompareSplayTreeString,RelinquishMagickMemory,
898 if (coder_list == (SplayTreeInfo *) NULL)
900 ThrowFileException(exception,ResourceLimitError,
901 "MemoryAllocationFailed",filename);
905 for (i=0; i < (long) (sizeof(CoderMap)/sizeof(*CoderMap)); i++)
910 register const CoderMapInfo
914 coder_info=(CoderInfo *) AcquireAlignedMemory(1,sizeof(*coder_info));
915 if (coder_info == (CoderInfo *) NULL)
917 (void) ThrowMagickException(exception,GetMagickModule(),
918 ResourceLimitError,"MemoryAllocationFailed","`%s'",coder_info->name);
921 (void) ResetMagickMemory(coder_info,0,sizeof(*coder_info));
922 coder_info->path=(char *) "[built-in]";
923 coder_info->magick=(char *) p->magick;
924 coder_info->name=(char *) p->name;
925 coder_info->exempt=MagickTrue;
926 coder_info->signature=MagickSignature;
927 status=AddValueToSplayTree(coder_list,ConstantString(coder_info->magick),
929 if (status == MagickFalse)
930 (void) ThrowMagickException(exception,GetMagickModule(),
931 ResourceLimitError,"MemoryAllocationFailed","`%s'",coder_info->name);
934 Load external coder map.
936 options=GetConfigureOptions(filename,exception);
937 option=(const StringInfo *) GetNextValueInLinkedList(options);
938 while (option != (const StringInfo *) NULL)
940 status|=LoadCoderList((const char *) GetStringInfoDatum(option),
941 GetStringInfoPath(option),0,exception);
942 option=(const StringInfo *) GetNextValueInLinkedList(options);
944 options=DestroyConfigureOptions(options);
945 return(status != 0 ? MagickTrue : MagickFalse);