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
76 static const CoderMapInfo
81 { "8BIMTEXT", "META" },
82 { "8BIMWTEXT", "META" },
86 { "APP1JPEG", "META" },
117 { "FRACTAL", "PLASMA" },
125 { "GRANITE", "MAGICK" },
126 { "GROUP4", "TIFF" },
136 { "IMPLICIT", "***" },
138 { "IPTCTEXT", "META" },
139 { "IPTCWTEXT", "META" },
140 { "ISOBRL", "BRAILLE" },
147 { "LOGO", "MAGICK" },
159 { "NETSCAPE", "MAGICK" },
184 { "RADIAL-GRADIENT", "GRADIENT" },
191 { "ROSE", "MAGICK" },
197 { "TIFF64", "TIFF" },
200 { "UBRL", "BRAILLE" },
204 { "WMFWIN32", "EMF" },
208 { "XTRNARRAY", "XTRN" },
209 { "XTRNBLOB", "XTRN" },
210 { "XTRNBSTR", "XTRN" },
211 { "XTRNFILE", "XTRN" },
212 { "XTRNIMAGE", "XTRN" },
213 { "XTRNSTREAM", "XTRN" },
216 { "YCbCrA", "YCbCr" },
217 { (const char *) NULL, (const char *) NULL }
221 *coder_semaphore = (SemaphoreInfo *) NULL;
224 *coder_list = (SplayTreeInfo *) NULL;
226 static volatile MagickBooleanType
227 instantiate_coder = MagickFalse;
230 Forward declarations.
232 static MagickBooleanType
233 InitializeCoderList(ExceptionInfo *),
234 LoadCoderLists(const char *,ExceptionInfo *);
237 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
241 + D e s t r o y C o d e r C o m p o n e n t %
245 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
247 % DestroyCoderComponent() destroys the coder component.
249 % The format of the DestroyCoderComponent method is:
251 % DestroyCoderComponent(void)
254 MagickExport void DestroyCoderComponent(void)
256 AcquireSemaphoreInfo(&coder_semaphore);
257 if (coder_list != (SplayTreeInfo *) NULL)
258 coder_list=DestroySplayTree(coder_list);
259 instantiate_coder=MagickFalse;
260 RelinquishSemaphoreInfo(coder_semaphore);
261 DestroySemaphoreInfo(&coder_semaphore);
265 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
269 + G e t C o d e r I n f o %
273 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
275 % GetCoderInfo searches the coder list for the specified name and if found
276 % returns attributes for that coder.
278 % The format of the GetCoderInfo method is:
280 % const CoderInfo *GetCoderInfo(const char *name,ExceptionInfo *exception)
282 % A description of each parameter follows:
284 % o name: the coder name.
286 % o exception: return any errors or warnings in this structure.
289 MagickExport const CoderInfo *GetCoderInfo(const char *name,
290 ExceptionInfo *exception)
292 assert(exception != (ExceptionInfo *) NULL);
293 if ((coder_list == (SplayTreeInfo *) NULL) ||
294 (instantiate_coder == MagickFalse))
295 if (InitializeCoderList(exception) == MagickFalse)
296 return((const CoderInfo *) NULL);
297 if ((coder_list == (SplayTreeInfo *) NULL) ||
298 (GetNumberOfNodesInSplayTree(coder_list) == 0))
299 return((const CoderInfo *) NULL);
300 if ((name == (const char *) NULL) || (LocaleCompare(name,"*") == 0))
302 ResetSplayTreeIterator(coder_list);
303 return((const CoderInfo *) GetNextValueInSplayTree(coder_list));
305 return((const CoderInfo *) GetValueFromSplayTree(coder_list,name));
309 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
313 % G e t C o d e r I n f o L i s t %
317 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
319 % GetCoderInfoList() returns any coder_map that match the specified pattern.
320 % The format of the GetCoderInfoList function is:
322 % const CoderInfo **GetCoderInfoList(const char *pattern,
323 % unsigned long *number_coders,ExceptionInfo *exception)
325 % A description of each parameter follows:
327 % o pattern: Specifies a pointer to a text string containing a pattern.
329 % o number_coders: This integer returns the number of coders in the list.
331 % o exception: return any errors or warnings in this structure.
335 static int CoderInfoCompare(const void *x,const void *y)
341 p=(const CoderInfo **) x,
342 q=(const CoderInfo **) y;
343 if (LocaleCompare((*p)->path,(*q)->path) == 0)
344 return(LocaleCompare((*p)->name,(*q)->name));
345 return(LocaleCompare((*p)->path,(*q)->path));
348 MagickExport const CoderInfo **GetCoderInfoList(const char *pattern,
349 unsigned long *number_coders,ExceptionInfo *exception)
354 register const CoderInfo
363 assert(pattern != (char *) NULL);
364 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
365 assert(number_coders != (unsigned long *) NULL);
367 p=GetCoderInfo("*",exception);
368 if (p == (const CoderInfo *) NULL)
369 return((const CoderInfo **) NULL);
370 coder_map=(const CoderInfo **) AcquireQuantumMemory((size_t)
371 GetNumberOfNodesInSplayTree(coder_list)+1UL,sizeof(*coder_map));
372 if (coder_map == (const CoderInfo **) NULL)
373 return((const CoderInfo **) NULL);
377 AcquireSemaphoreInfo(&coder_semaphore);
378 ResetSplayTreeIterator(coder_list);
379 p=(const CoderInfo *) GetNextValueInSplayTree(coder_list);
380 for (i=0; p != (const CoderInfo *) NULL; )
382 if ((p->stealth == MagickFalse) &&
383 (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
385 p=(const CoderInfo *) GetNextValueInSplayTree(coder_list);
387 RelinquishSemaphoreInfo(coder_semaphore);
388 qsort((void *) coder_map,(size_t) i,sizeof(*coder_map),CoderInfoCompare);
389 coder_map[i]=(CoderInfo *) NULL;
390 *number_coders=(unsigned long) i;
395 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
399 % G e t C o d e r L i s t %
403 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
405 % GetCoderList() returns any coder_map that match the specified pattern.
407 % The format of the GetCoderList function is:
409 % char **GetCoderList(const char *pattern,unsigned long *number_coders,
410 % ExceptionInfo *exception)
412 % A description of each parameter follows:
414 % o pattern: Specifies a pointer to a text string containing a pattern.
416 % o number_coders: This integer returns the number of coders in the list.
418 % o exception: return any errors or warnings in this structure.
422 static int CoderCompare(const void *x,const void *y)
430 return(LocaleCompare(*p,*q));
433 MagickExport char **GetCoderList(const char *pattern,
434 unsigned long *number_coders,ExceptionInfo *exception)
439 register const CoderInfo
448 assert(pattern != (char *) NULL);
449 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
450 assert(number_coders != (unsigned long *) NULL);
452 p=GetCoderInfo("*",exception);
453 if (p == (const CoderInfo *) NULL)
454 return((char **) NULL);
455 coder_map=(char **) AcquireQuantumMemory((size_t)
456 GetNumberOfNodesInSplayTree(coder_list)+1UL,sizeof(*coder_map));
457 if (coder_map == (char **) NULL)
458 return((char **) NULL);
462 AcquireSemaphoreInfo(&coder_semaphore);
463 ResetSplayTreeIterator(coder_list);
464 p=(const CoderInfo *) GetNextValueInSplayTree(coder_list);
465 for (i=0; p != (const CoderInfo *) NULL; )
467 if ((p->stealth == MagickFalse) &&
468 (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
469 coder_map[i++]=ConstantString(p->name);
470 p=(const CoderInfo *) GetNextValueInSplayTree(coder_list);
472 RelinquishSemaphoreInfo(coder_semaphore);
473 qsort((void *) coder_map,(size_t) i,sizeof(*coder_map),CoderCompare);
474 coder_map[i]=(char *) NULL;
475 *number_coders=(unsigned long) i;
480 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
484 + I n i t i a l i z e C o d e r L i s t %
488 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
490 % InitializeCoderList() initializes the coder list.
492 % The format of the InitializeCoderList method is:
494 % MagickBooleanType InitializeCoderList(ExceptionInfo *exception)
496 % A description of each parameter follows.
498 % o exception: return any errors or warnings in this structure.
501 static MagickBooleanType InitializeCoderList(ExceptionInfo *exception)
503 if ((coder_list == (SplayTreeInfo *) NULL) &&
504 (instantiate_coder == MagickFalse))
506 AcquireSemaphoreInfo(&coder_semaphore);
507 if ((coder_list == (SplayTreeInfo *) NULL) &&
508 (instantiate_coder == MagickFalse))
510 (void) LoadCoderLists(MagickCoderFilename,exception);
511 instantiate_coder=MagickTrue;
513 RelinquishSemaphoreInfo(coder_semaphore);
515 return(coder_list != (SplayTreeInfo *) NULL ? MagickTrue : MagickFalse);
519 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
523 + 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 %
527 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
529 % InstantiateCoderComponent() instantiates the coder component.
531 % The format of the InstantiateCoderComponent method is:
533 % MagickBooleanType InstantiateCoderComponent(void)
536 MagickExport MagickBooleanType InstantiateCoderComponent(void)
538 AcquireSemaphoreInfo(&coder_semaphore);
539 RelinquishSemaphoreInfo(coder_semaphore);
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 < (long) 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) fprintf(file,"\nPath: %s\n\n",coder_info[i]->path);
600 (void) fprintf(file,"Magick Coder\n");
601 (void) fprintf(file,"-------------------------------------------------"
602 "------------------------------\n");
604 path=coder_info[i]->path;
605 (void) fprintf(file,"%s",coder_info[i]->magick);
606 for (j=(long) strlen(coder_info[i]->magick); j <= 11; j++)
607 (void) fprintf(file," ");
608 if (coder_info[i]->name != (char *) NULL)
609 (void) fprintf(file,"%s",coder_info[i]->name);
610 (void) fprintf(file,"\n");
612 coder_info=(const CoderInfo **) RelinquishMagickMemory((void *) coder_info);
618 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
622 + L o a d C o d e r L i s t %
626 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
628 % LoadCoderList() loads the coder configuration file which provides a
629 % mapping between coder attributes and a coder name.
631 % The format of the LoadCoderList coder is:
633 % MagickBooleanType LoadCoderList(const char *xml,const char *filename,
634 % const unsigned long depth,ExceptionInfo *exception)
636 % A description of each parameter follows:
638 % o xml: The coder list in XML format.
640 % o filename: The coder list filename.
642 % o depth: depth of <include /> statements.
644 % o exception: return any errors or warnings in this structure.
648 static void *DestroyCoderNode(void *coder_info)
653 p=(CoderInfo *) coder_info;
654 if (p->exempt == MagickFalse)
656 if (p->path != (char *) NULL)
657 p->path=DestroyString(p->path);
658 if (p->name != (char *) NULL)
659 p->name=DestroyString(p->name);
660 if (p->magick != (char *) NULL)
661 p->magick=DestroyString(p->magick);
663 return(RelinquishMagickMemory(p));
666 static MagickBooleanType LoadCoderList(const char *xml,const char *filename,
667 const unsigned long depth,ExceptionInfo *exception)
670 keyword[MaxTextExtent],
683 Load the coder map file.
685 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
686 "Loading coder configuration file \"%s\" ...",filename);
687 if (xml == (const char *) NULL)
689 if (coder_list == (SplayTreeInfo *) NULL)
691 coder_list=NewSplayTree(CompareSplayTreeString,RelinquishMagickMemory,
693 if (coder_list == (SplayTreeInfo *) NULL)
695 ThrowFileException(exception,ResourceLimitError,
696 "MemoryAllocationFailed",filename);
701 coder_info=(CoderInfo *) NULL;
702 token=AcquireString(xml);
703 for (q=(char *) xml; *q != '\0'; )
708 GetMagickToken(q,&q,token);
711 (void) CopyMagickString(keyword,token,MaxTextExtent);
712 if (LocaleNCompare(keyword,"<!DOCTYPE",9) == 0)
717 while ((LocaleNCompare(q,"]>",2) != 0) && (*q != '\0'))
718 GetMagickToken(q,&q,token);
721 if (LocaleNCompare(keyword,"<!--",4) == 0)
726 while ((LocaleNCompare(q,"->",2) != 0) && (*q != '\0'))
727 GetMagickToken(q,&q,token);
730 if (LocaleCompare(keyword,"<include") == 0)
735 while (((*token != '/') && (*(token+1) != '>')) && (*q != '\0'))
737 (void) CopyMagickString(keyword,token,MaxTextExtent);
738 GetMagickToken(q,&q,token);
741 GetMagickToken(q,&q,token);
742 if (LocaleCompare(keyword,"file") == 0)
745 (void) ThrowMagickException(exception,GetMagickModule(),
746 ConfigureError,"IncludeNodeNestedTooDeeply","`%s'",token);
753 GetPathComponent(filename,HeadPath,path);
755 (void) ConcatenateMagickString(path,DirectorySeparator,
757 if (*token == *DirectorySeparator)
758 (void) CopyMagickString(path,token,MaxTextExtent);
760 (void) ConcatenateMagickString(path,token,MaxTextExtent);
761 xml=FileToString(path,~0,exception);
762 if (xml != (char *) NULL)
764 status=LoadCoderList(xml,path,depth+1,exception);
765 xml=(char *) RelinquishMagickMemory(xml);
772 if (LocaleCompare(keyword,"<coder") == 0)
777 coder_info=(CoderInfo *) AcquireMagickMemory(sizeof(*coder_info));
778 if (coder_info == (CoderInfo *) NULL)
779 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
780 (void) ResetMagickMemory(coder_info,0,sizeof(*coder_info));
781 coder_info->path=ConstantString(filename);
782 coder_info->exempt=MagickFalse;
783 coder_info->signature=MagickSignature;
786 if (coder_info == (CoderInfo *) NULL)
788 if (LocaleCompare(keyword,"/>") == 0)
790 status=AddValueToSplayTree(coder_list,ConstantString(
791 coder_info->magick),coder_info);
792 if (status == MagickFalse)
793 (void) ThrowMagickException(exception,GetMagickModule(),
794 ResourceLimitError,"MemoryAllocationFailed","`%s'",
796 coder_info=(CoderInfo *) NULL;
798 GetMagickToken(q,(const char **) NULL,token);
801 GetMagickToken(q,&q,token);
802 GetMagickToken(q,&q,token);
808 if (LocaleCompare((char *) keyword,"magick") == 0)
810 coder_info->magick=ConstantString(token);
818 if (LocaleCompare((char *) keyword,"name") == 0)
820 coder_info->name=ConstantString(token);
828 if (LocaleCompare((char *) keyword,"stealth") == 0)
830 coder_info->stealth=IsMagickTrue(token);
839 token=(char *) RelinquishMagickMemory(token);
844 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
848 % L o a d C o d e r L i s t s %
852 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
854 % LoadCoderLists() loads one or more coder configuration file which
855 % provides a mapping between coder attributes and a coder name.
857 % The format of the LoadCoderLists coder is:
859 % MagickBooleanType LoadCoderLists(const char *filename,
860 % ExceptionInfo *exception)
862 % A description of each parameter follows:
864 % o filename: the font file name.
866 % o exception: return any errors or warnings in this structure.
869 static MagickBooleanType LoadCoderLists(const char *filename,
870 ExceptionInfo *exception)
881 register const CoderMapInfo
885 Load built-in coder map.
888 if (coder_list == (SplayTreeInfo *) NULL)
890 coder_list=NewSplayTree(CompareSplayTreeString,RelinquishMagickMemory,
892 if (coder_list == (SplayTreeInfo *) NULL)
894 ThrowFileException(exception,ResourceLimitError,
895 "MemoryAllocationFailed",filename);
899 for (p=CoderMap; p->magick != (const char *) NULL; p++)
904 coder_info=(CoderInfo *) AcquireMagickMemory(sizeof(*coder_info));
905 if (coder_info == (CoderInfo *) NULL)
907 (void) ThrowMagickException(exception,GetMagickModule(),
908 ResourceLimitError,"MemoryAllocationFailed","`%s'",coder_info->name);
911 (void) ResetMagickMemory(coder_info,0,sizeof(*coder_info));
912 coder_info->path=(char *) "[built-in]";
913 coder_info->magick=(char *) p->magick;
914 coder_info->name=(char *) p->name;
915 coder_info->exempt=MagickTrue;
916 coder_info->signature=MagickSignature;
917 status=AddValueToSplayTree(coder_list,ConstantString(coder_info->magick),
919 if (status == MagickFalse)
920 (void) ThrowMagickException(exception,GetMagickModule(),
921 ResourceLimitError,"MemoryAllocationFailed","`%s'",coder_info->name);
924 Load external coder map.
926 options=GetConfigureOptions(filename,exception);
927 option=(const StringInfo *) GetNextValueInLinkedList(options);
928 while (option != (const StringInfo *) NULL)
930 status|=LoadCoderList((const char *) GetStringInfoDatum(option),
931 GetStringInfoPath(option),0,exception);
932 option=(const StringInfo *) GetNextValueInLinkedList(options);
934 options=DestroyConfigureOptions(options);
935 return(status != 0 ? MagickTrue : MagickFalse);