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" },
189 { "RADIAL-GRADIENT", "GRADIENT" },
196 { "ROSE", "MAGICK" },
202 { "TIFF64", "TIFF" },
205 { "UBRL", "BRAILLE" },
209 { "WMFWIN32", "EMF" },
213 { "XTRNARRAY", "XTRN" },
214 { "XTRNBLOB", "XTRN" },
215 { "XTRNBSTR", "XTRN" },
216 { "XTRNFILE", "XTRN" },
217 { "XTRNIMAGE", "XTRN" },
218 { "XTRNSTREAM", "XTRN" },
221 { "YCbCrA", "YCbCr" }
225 *coder_semaphore = (SemaphoreInfo *) NULL;
228 *coder_list = (SplayTreeInfo *) NULL;
230 static volatile MagickBooleanType
231 instantiate_coder = MagickFalse;
234 Forward declarations.
236 static MagickBooleanType
237 InitializeCoderList(ExceptionInfo *),
238 LoadCoderLists(const char *,ExceptionInfo *);
241 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
245 + C o d e r C o m p o n e n t G e n e s i s %
249 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
251 % CoderComponentGenesis() instantiates the coder component.
253 % The format of the CoderComponentGenesis method is:
255 % MagickBooleanType CoderComponentGenesis(void)
258 MagickExport MagickBooleanType CoderComponentGenesis(void)
260 AcquireSemaphoreInfo(&coder_semaphore);
265 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
269 + C o d e r C o m p o n e n t T e r m i n u s %
273 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
275 % CoderComponentTerminus() destroys the coder component.
277 % The format of the CoderComponentTerminus method is:
279 % CoderComponentTerminus(void)
282 MagickExport void CoderComponentTerminus(void)
284 if (coder_semaphore == (SemaphoreInfo *) NULL)
285 AcquireSemaphoreInfo(&coder_semaphore);
286 LockSemaphoreInfo(coder_semaphore);
287 if (coder_list != (SplayTreeInfo *) NULL)
288 coder_list=DestroySplayTree(coder_list);
289 instantiate_coder=MagickFalse;
290 UnlockSemaphoreInfo(coder_semaphore);
291 DestroySemaphoreInfo(&coder_semaphore);
295 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
299 + G e t C o d e r I n f o %
303 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
305 % GetCoderInfo searches the coder list for the specified name and if found
306 % returns attributes for that coder.
308 % The format of the GetCoderInfo method is:
310 % const CoderInfo *GetCoderInfo(const char *name,ExceptionInfo *exception)
312 % A description of each parameter follows:
314 % o name: the coder name.
316 % o exception: return any errors or warnings in this structure.
319 MagickExport const CoderInfo *GetCoderInfo(const char *name,
320 ExceptionInfo *exception)
322 assert(exception != (ExceptionInfo *) NULL);
323 if ((coder_list == (SplayTreeInfo *) NULL) ||
324 (instantiate_coder == MagickFalse))
325 if (InitializeCoderList(exception) == MagickFalse)
326 return((const CoderInfo *) NULL);
327 if ((coder_list == (SplayTreeInfo *) NULL) ||
328 (GetNumberOfNodesInSplayTree(coder_list) == 0))
329 return((const CoderInfo *) NULL);
330 if ((name == (const char *) NULL) || (LocaleCompare(name,"*") == 0))
332 ResetSplayTreeIterator(coder_list);
333 return((const CoderInfo *) GetNextValueInSplayTree(coder_list));
335 return((const CoderInfo *) GetValueFromSplayTree(coder_list,name));
339 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
343 % G e t C o d e r I n f o L i s t %
347 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
349 % GetCoderInfoList() returns any coder_map that match the specified pattern.
350 % The format of the GetCoderInfoList function is:
352 % const CoderInfo **GetCoderInfoList(const char *pattern,
353 % unsigned long *number_coders,ExceptionInfo *exception)
355 % A description of each parameter follows:
357 % o pattern: Specifies a pointer to a text string containing a pattern.
359 % o number_coders: This integer returns the number of coders in the list.
361 % o exception: return any errors or warnings in this structure.
365 static int CoderInfoCompare(const void *x,const void *y)
371 p=(const CoderInfo **) x,
372 q=(const CoderInfo **) y;
373 if (LocaleCompare((*p)->path,(*q)->path) == 0)
374 return(LocaleCompare((*p)->name,(*q)->name));
375 return(LocaleCompare((*p)->path,(*q)->path));
378 MagickExport const CoderInfo **GetCoderInfoList(const char *pattern,
379 unsigned long *number_coders,ExceptionInfo *exception)
384 register const CoderInfo
393 assert(pattern != (char *) NULL);
394 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
395 assert(number_coders != (unsigned long *) NULL);
397 p=GetCoderInfo("*",exception);
398 if (p == (const CoderInfo *) NULL)
399 return((const CoderInfo **) NULL);
400 coder_map=(const CoderInfo **) AcquireQuantumMemory((size_t)
401 GetNumberOfNodesInSplayTree(coder_list)+1UL,sizeof(*coder_map));
402 if (coder_map == (const CoderInfo **) NULL)
403 return((const CoderInfo **) NULL);
407 LockSemaphoreInfo(coder_semaphore);
408 ResetSplayTreeIterator(coder_list);
409 p=(const CoderInfo *) GetNextValueInSplayTree(coder_list);
410 for (i=0; p != (const CoderInfo *) NULL; )
412 if ((p->stealth == MagickFalse) &&
413 (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
415 p=(const CoderInfo *) GetNextValueInSplayTree(coder_list);
417 UnlockSemaphoreInfo(coder_semaphore);
418 qsort((void *) coder_map,(size_t) i,sizeof(*coder_map),CoderInfoCompare);
419 coder_map[i]=(CoderInfo *) NULL;
420 *number_coders=(unsigned long) i;
425 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
429 % G e t C o d e r L i s t %
433 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
435 % GetCoderList() returns any coder_map that match the specified pattern.
437 % The format of the GetCoderList function is:
439 % char **GetCoderList(const char *pattern,unsigned long *number_coders,
440 % ExceptionInfo *exception)
442 % A description of each parameter follows:
444 % o pattern: Specifies a pointer to a text string containing a pattern.
446 % o number_coders: This integer returns the number of coders in the list.
448 % o exception: return any errors or warnings in this structure.
452 static int CoderCompare(const void *x,const void *y)
460 return(LocaleCompare(*p,*q));
463 MagickExport char **GetCoderList(const char *pattern,
464 unsigned long *number_coders,ExceptionInfo *exception)
469 register const CoderInfo
478 assert(pattern != (char *) NULL);
479 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
480 assert(number_coders != (unsigned long *) NULL);
482 p=GetCoderInfo("*",exception);
483 if (p == (const CoderInfo *) NULL)
484 return((char **) NULL);
485 coder_map=(char **) AcquireQuantumMemory((size_t)
486 GetNumberOfNodesInSplayTree(coder_list)+1UL,sizeof(*coder_map));
487 if (coder_map == (char **) NULL)
488 return((char **) NULL);
492 LockSemaphoreInfo(coder_semaphore);
493 ResetSplayTreeIterator(coder_list);
494 p=(const CoderInfo *) GetNextValueInSplayTree(coder_list);
495 for (i=0; p != (const CoderInfo *) NULL; )
497 if ((p->stealth == MagickFalse) &&
498 (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
499 coder_map[i++]=ConstantString(p->name);
500 p=(const CoderInfo *) GetNextValueInSplayTree(coder_list);
502 UnlockSemaphoreInfo(coder_semaphore);
503 qsort((void *) coder_map,(size_t) i,sizeof(*coder_map),CoderCompare);
504 coder_map[i]=(char *) NULL;
505 *number_coders=(unsigned long) i;
510 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
514 + I n i t i a l i z e C o d e r L i s t %
518 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
520 % InitializeCoderList() initializes the coder list.
522 % The format of the InitializeCoderList method is:
524 % MagickBooleanType InitializeCoderList(ExceptionInfo *exception)
526 % A description of each parameter follows.
528 % o exception: return any errors or warnings in this structure.
531 static MagickBooleanType InitializeCoderList(ExceptionInfo *exception)
533 if ((coder_list == (SplayTreeInfo *) NULL) &&
534 (instantiate_coder == MagickFalse))
536 if (coder_semaphore == (SemaphoreInfo *) NULL)
537 AcquireSemaphoreInfo(&coder_semaphore);
538 LockSemaphoreInfo(coder_semaphore);
539 if ((coder_list == (SplayTreeInfo *) NULL) &&
540 (instantiate_coder == MagickFalse))
542 (void) LoadCoderLists(MagickCoderFilename,exception);
543 instantiate_coder=MagickTrue;
545 UnlockSemaphoreInfo(coder_semaphore);
547 return(coder_list != (SplayTreeInfo *) NULL ? MagickTrue : MagickFalse);
551 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
555 % L i s t C o d e r I n f o %
559 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
561 % ListCoderInfo() lists the coder info to a file.
563 % The format of the ListCoderInfo coder is:
565 % MagickBooleanType ListCoderInfo(FILE *file,ExceptionInfo *exception)
567 % A description of each parameter follows.
569 % o file: An pointer to a FILE.
571 % o exception: return any errors or warnings in this structure.
574 MagickExport MagickBooleanType ListCoderInfo(FILE *file,
575 ExceptionInfo *exception)
592 if (file == (const FILE *) NULL)
594 coder_info=GetCoderInfoList("*",&number_coders,exception);
595 if (coder_info == (const CoderInfo **) NULL)
597 path=(const char *) NULL;
598 for (i=0; i < (long) number_coders; i++)
600 if (coder_info[i]->stealth != MagickFalse)
602 if ((path == (const char *) NULL) ||
603 (LocaleCompare(path,coder_info[i]->path) != 0))
605 if (coder_info[i]->path != (char *) NULL)
606 (void) fprintf(file,"\nPath: %s\n\n",coder_info[i]->path);
607 (void) fprintf(file,"Magick Coder\n");
608 (void) fprintf(file,"-------------------------------------------------"
609 "------------------------------\n");
611 path=coder_info[i]->path;
612 (void) fprintf(file,"%s",coder_info[i]->magick);
613 for (j=(long) strlen(coder_info[i]->magick); j <= 11; j++)
614 (void) fprintf(file," ");
615 if (coder_info[i]->name != (char *) NULL)
616 (void) fprintf(file,"%s",coder_info[i]->name);
617 (void) fprintf(file,"\n");
619 coder_info=(const CoderInfo **) RelinquishMagickMemory((void *) coder_info);
625 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
629 + L o a d C o d e r L i s t %
633 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
635 % LoadCoderList() loads the coder configuration file which provides a
636 % mapping between coder attributes and a coder name.
638 % The format of the LoadCoderList coder is:
640 % MagickBooleanType LoadCoderList(const char *xml,const char *filename,
641 % const unsigned long depth,ExceptionInfo *exception)
643 % A description of each parameter follows:
645 % o xml: The coder list in XML format.
647 % o filename: The coder list filename.
649 % o depth: depth of <include /> statements.
651 % o exception: return any errors or warnings in this structure.
655 static void *DestroyCoderNode(void *coder_info)
660 p=(CoderInfo *) coder_info;
661 if (p->exempt == MagickFalse)
663 if (p->path != (char *) NULL)
664 p->path=DestroyString(p->path);
665 if (p->name != (char *) NULL)
666 p->name=DestroyString(p->name);
667 if (p->magick != (char *) NULL)
668 p->magick=DestroyString(p->magick);
670 return(RelinquishMagickMemory(p));
673 static MagickBooleanType LoadCoderList(const char *xml,const char *filename,
674 const unsigned long depth,ExceptionInfo *exception)
677 keyword[MaxTextExtent],
690 Load the coder map file.
692 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
693 "Loading coder configuration file \"%s\" ...",filename);
694 if (xml == (const char *) NULL)
696 if (coder_list == (SplayTreeInfo *) NULL)
698 coder_list=NewSplayTree(CompareSplayTreeString,RelinquishMagickMemory,
700 if (coder_list == (SplayTreeInfo *) NULL)
702 ThrowFileException(exception,ResourceLimitError,
703 "MemoryAllocationFailed",filename);
708 coder_info=(CoderInfo *) NULL;
709 token=AcquireString(xml);
710 for (q=(char *) xml; *q != '\0'; )
715 GetMagickToken(q,&q,token);
718 (void) CopyMagickString(keyword,token,MaxTextExtent);
719 if (LocaleNCompare(keyword,"<!DOCTYPE",9) == 0)
724 while ((LocaleNCompare(q,"]>",2) != 0) && (*q != '\0'))
725 GetMagickToken(q,&q,token);
728 if (LocaleNCompare(keyword,"<!--",4) == 0)
733 while ((LocaleNCompare(q,"->",2) != 0) && (*q != '\0'))
734 GetMagickToken(q,&q,token);
737 if (LocaleCompare(keyword,"<include") == 0)
742 while (((*token != '/') && (*(token+1) != '>')) && (*q != '\0'))
744 (void) CopyMagickString(keyword,token,MaxTextExtent);
745 GetMagickToken(q,&q,token);
748 GetMagickToken(q,&q,token);
749 if (LocaleCompare(keyword,"file") == 0)
752 (void) ThrowMagickException(exception,GetMagickModule(),
753 ConfigureError,"IncludeNodeNestedTooDeeply","`%s'",token);
760 GetPathComponent(filename,HeadPath,path);
762 (void) ConcatenateMagickString(path,DirectorySeparator,
764 if (*token == *DirectorySeparator)
765 (void) CopyMagickString(path,token,MaxTextExtent);
767 (void) ConcatenateMagickString(path,token,MaxTextExtent);
768 xml=FileToString(path,~0,exception);
769 if (xml != (char *) NULL)
771 status=LoadCoderList(xml,path,depth+1,exception);
772 xml=(char *) RelinquishMagickMemory(xml);
779 if (LocaleCompare(keyword,"<coder") == 0)
784 coder_info=(CoderInfo *) AcquireAlignedMemory(1,sizeof(*coder_info));
785 if (coder_info == (CoderInfo *) NULL)
786 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
787 (void) ResetMagickMemory(coder_info,0,sizeof(*coder_info));
788 coder_info->path=ConstantString(filename);
789 coder_info->exempt=MagickFalse;
790 coder_info->signature=MagickSignature;
793 if (coder_info == (CoderInfo *) NULL)
795 if (LocaleCompare(keyword,"/>") == 0)
797 status=AddValueToSplayTree(coder_list,ConstantString(
798 coder_info->magick),coder_info);
799 if (status == MagickFalse)
800 (void) ThrowMagickException(exception,GetMagickModule(),
801 ResourceLimitError,"MemoryAllocationFailed","`%s'",
803 coder_info=(CoderInfo *) NULL;
805 GetMagickToken(q,(const char **) NULL,token);
808 GetMagickToken(q,&q,token);
809 GetMagickToken(q,&q,token);
815 if (LocaleCompare((char *) keyword,"magick") == 0)
817 coder_info->magick=ConstantString(token);
825 if (LocaleCompare((char *) keyword,"name") == 0)
827 coder_info->name=ConstantString(token);
835 if (LocaleCompare((char *) keyword,"stealth") == 0)
837 coder_info->stealth=IsMagickTrue(token);
846 token=(char *) RelinquishMagickMemory(token);
851 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
855 % L o a d C o d e r L i s t s %
859 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
861 % LoadCoderLists() loads one or more coder configuration file which
862 % provides a mapping between coder attributes and a coder name.
864 % The format of the LoadCoderLists coder is:
866 % MagickBooleanType LoadCoderLists(const char *filename,
867 % ExceptionInfo *exception)
869 % A description of each parameter follows:
871 % o filename: the font file name.
873 % o exception: return any errors or warnings in this structure.
876 static MagickBooleanType LoadCoderLists(const char *filename,
877 ExceptionInfo *exception)
892 Load built-in coder map.
895 if (coder_list == (SplayTreeInfo *) NULL)
897 coder_list=NewSplayTree(CompareSplayTreeString,RelinquishMagickMemory,
899 if (coder_list == (SplayTreeInfo *) NULL)
901 ThrowFileException(exception,ResourceLimitError,
902 "MemoryAllocationFailed",filename);
906 for (i=0; i < (long) (sizeof(CoderMap)/sizeof(*CoderMap)); i++)
911 register const CoderMapInfo
915 coder_info=(CoderInfo *) AcquireAlignedMemory(1,sizeof(*coder_info));
916 if (coder_info == (CoderInfo *) NULL)
918 (void) ThrowMagickException(exception,GetMagickModule(),
919 ResourceLimitError,"MemoryAllocationFailed","`%s'",coder_info->name);
922 (void) ResetMagickMemory(coder_info,0,sizeof(*coder_info));
923 coder_info->path=(char *) "[built-in]";
924 coder_info->magick=(char *) p->magick;
925 coder_info->name=(char *) p->name;
926 coder_info->exempt=MagickTrue;
927 coder_info->signature=MagickSignature;
928 status=AddValueToSplayTree(coder_list,ConstantString(coder_info->magick),
930 if (status == MagickFalse)
931 (void) ThrowMagickException(exception,GetMagickModule(),
932 ResourceLimitError,"MemoryAllocationFailed","`%s'",coder_info->name);
935 Load external coder map.
937 options=GetConfigureOptions(filename,exception);
938 option=(const StringInfo *) GetNextValueInLinkedList(options);
939 while (option != (const StringInfo *) NULL)
941 status|=LoadCoderList((const char *) GetStringInfoDatum(option),
942 GetStringInfoPath(option),0,exception);
943 option=(const StringInfo *) GetNextValueInLinkedList(options);
945 options=DestroyConfigureOptions(options);
946 return(status != 0 ? MagickTrue : MagickFalse);