]> granicus.if.org Git - imagemagick/blob - MagickCore/coder.c
(no commit message)
[imagemagick] / MagickCore / coder.c
1 /*
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %                                                                             %
4 %                                                                             %
5 %                                                                             %
6 %                   CCCC   OOO   DDDD    EEEEE  RRRR                          %
7 %                  C      O   O  D   D   E      R   R                         %
8 %                  C      O   O  D   D   EEE    RRRR                          %
9 %                  C      O   O  D   D   E      R R                           %
10 %                   CCCC   OOO   DDDD    EEEEE  R  R                          %
11 %                                                                             %
12 %                                                                             %
13 %                     MagickCore Image Coder Methods                          %
14 %                                                                             %
15 %                              Software Design                                %
16 %                                   Cristy                                    %
17 %                                 May 2001                                    %
18 %                                                                             %
19 %                                                                             %
20 %  Copyright 1999-2014 ImageMagick Studio LLC, a non-profit organization      %
21 %  dedicated to making software imaging solutions freely available.           %
22 %                                                                             %
23 %  You may not use this file except in compliance with the License.  You may  %
24 %  obtain a copy of the License at                                            %
25 %                                                                             %
26 %    http://www.imagemagick.org/script/license.php                            %
27 %                                                                             %
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.                                             %
33 %                                                                             %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35 %
36 %
37 */
38 \f
39 /*
40   Include declarations.
41 */
42 #include "MagickCore/studio.h"
43 #include "MagickCore/blob.h"
44 #include "MagickCore/client.h"
45 #include "MagickCore/coder.h"
46 #include "MagickCore/coder-private.h"
47 #include "MagickCore/configure.h"
48 #include "MagickCore/draw.h"
49 #include "MagickCore/exception.h"
50 #include "MagickCore/exception-private.h"
51 #include "MagickCore/hashmap.h"
52 #include "MagickCore/log.h"
53 #include "MagickCore/memory_.h"
54 #include "MagickCore/option.h"
55 #include "MagickCore/semaphore.h"
56 #include "MagickCore/string_.h"
57 #include "MagickCore/splay-tree.h"
58 #include "MagickCore/token.h"
59 #include "MagickCore/utility.h"
60 #include "MagickCore/utility-private.h"
61 #include "MagickCore/xml-tree.h"
62 #include "MagickCore/xml-tree-private.h"
63 \f
64 /*
65   Define declarations.
66 */
67 #define MagickCoderFilename  "coder.xml"
68 \f
69 /*
70   Typedef declarations.
71 */
72 typedef struct _CoderMapInfo
73 {
74   const char
75     *magick,
76     *name;
77 } CoderMapInfo;
78 \f
79 /*
80   Static declarations.
81 */
82 static const CoderMapInfo
83   CoderMap[] =
84   {
85     { "3FR", "DNG" },
86     { "8BIM", "META" },
87     { "8BIMTEXT", "META" },
88     { "8BIMWTEXT", "META" },
89     { "AFM", "TTF" },
90     { "A", "RAW" },
91     { "AI", "PDF" },
92     { "APP1JPEG", "META" },
93     { "APP1", "META" },
94     { "ARW", "DNG" },
95     { "AVI", "MPEG" },
96     { "BIE", "JBIG" },
97     { "BMP2", "BMP" },
98     { "BMP3", "BMP" },
99     { "B", "RAW" },
100     { "BRF", "BRAILLE" },
101     { "BGRA", "BGR" },
102     { "CMYKA", "CMYK" },
103     { "C", "RAW" },
104     { "CAL", "CALS" },
105     { "CANVAS", "XC" },
106     { "CR2", "DNG" },
107     { "CRW", "DNG" },
108     { "CUR", "ICON" },
109     { "DCR", "DNG" },
110     { "DCX", "PCX" },
111     { "DFONT", "TTF" },
112     { "EPDF", "PDF" },
113     { "EPI", "PS" },
114     { "EPS2", "PS2" },
115     { "EPS3", "PS3" },
116     { "EPSF", "PS" },
117     { "EPSI", "PS" },
118     { "EPS", "PS" },
119     { "EPT2", "EPT" },
120     { "EPT3", "EPT" },
121     { "ERF", "DNG" },
122     { "EXIF", "META" },
123     { "FILE", "URL" },
124     { "FRACTAL", "PLASMA" },
125     { "FTP", "URL" },
126     { "FTS", "FITS" },
127     { "G3", "FAX" },
128     { "GIF87", "GIF" },
129     { "G", "RAW" },
130     { "GRANITE", "MAGICK" },
131     { "GROUP4", "TIFF" },
132     { "GV", "DOT" },
133     { "IIQ", "DNG" },
134     { "K25", "DNG" },
135     { "KDC", "DNG" },
136     { "H", "MAGICK" },
137     { "HTM", "HTML" },
138     { "HTTP", "URL" },
139     { "HTTPS", "URL" },
140     { "ICB", "TGA" },
141     { "ICC", "META" },
142     { "ICM", "META" },
143     { "ICO", "ICON" },
144     { "IMPLICIT", "***" },
145     { "IPTC", "META" },
146     { "IPTCTEXT", "META" },
147     { "IPTCWTEXT", "META" },
148     { "ISOBRL", "BRAILLE" },
149     { "JBG", "JBIG" },
150     { "JNG", "PNG" },
151     { "JPC", "JP2" },
152     { "JPT", "JP2" },
153     { "JPM", "JP2" },
154     { "J2C", "JP2" },
155     { "J2K", "JP2" },
156     { "JPG", "JPEG" },
157     { "JPX", "JP2" },
158     { "K", "RAW" },
159     { "LOGO", "MAGICK" },
160     { "M2V", "MPEG" },
161     { "M4V", "MPEG" },
162     { "M", "RAW" },
163     { "MNG", "PNG" },
164     { "MOV", "MPEG" },
165     { "MP4", "MPEG" },
166     { "MPG", "MPEG" },
167     { "MPRI", "MPR" },
168     { "MEF", "DNG" },
169     { "MRW", "DNG" },
170     { "MSVG", "SVG" },
171     { "NEF", "DNG" },
172     { "NETSCAPE", "MAGICK" },
173     { "NRW", "DNG" },
174     { "O", "RAW" },
175     { "ORF", "DNG" },
176     { "OTF", "TTF" },
177     { "P7", "PNM" },
178     { "PAL", "UYVY" },
179     { "PAM", "PNM" },
180     { "PBM", "PNM" },
181     { "PCDS", "PCD" },
182     { "PDFA", "PDF" },
183     { "PEF", "DNG" },
184     { "PEF", "DNG" },
185     { "PFA", "TTF" },
186     { "PFB", "TTF" },
187     { "PFM", "PNM" },
188     { "PGM", "PNM" },
189     { "PGX", "JP2" },
190     { "PICON", "XPM" },
191     { "PJPEG", "JPEG" },
192     { "PM", "XPM" },
193     { "PNG24", "PNG" },
194     { "PNG32", "PNG" },
195     { "PNG8", "PNG" },
196     { "PPM", "PNM" },
197     { "PSB", "PSD" },
198     { "PTIF", "TIFF" },
199     { "RADIAL-GRADIENT", "GRADIENT" },
200     { "RAF", "DNG" },
201     { "RAS", "SUN" },
202     { "RAW", "DNG" },
203     { "RGBA", "RGB" },
204     { "RGBO", "RGB" },
205     { "RMF", "DNG" },
206     { "R", "RAW" },
207     { "ROSE", "MAGICK" },
208     { "RW2", "DNG" },
209     { "SHTML", "HTML" },
210     { "SIX", "SIXEL" },
211     { "SPARSE-COLOR", "TXT" },
212     { "SR2", "DNG" },
213     { "SRF", "DNG" },
214     { "SVGZ", "SVG" },
215     { "TEXT", "TXT" },
216     { "TIFF64", "TIFF" },
217     { "TIF", "TIFF" },
218     { "TTC", "TTF" },
219     { "UBRL", "BRAILLE" },
220     { "VDA", "TGA" },
221     { "VST", "TGA" },
222     { "WIZARD", "MAGICK" },
223 #if defined(MAGICKCORE_WINGDI32_DELEGATE)
224     { "WMF", "EMF" },
225 #endif
226     { "WMV", "MPEG" },
227     { "WMZ", "WMF" },
228     { "X3f", "DNG" },
229     { "XMP", "META" },
230     { "XTRNARRAY", "XTRN" },
231     { "XTRNBLOB", "XTRN" },
232     { "XTRNFILE", "XTRN" },
233     { "XTRNIMAGE", "XTRN" },
234     { "XV", "VIFF" },
235     { "Y", "RAW" },
236     { "YCbCrA", "YCbCr" }
237  };
238
239 static SemaphoreInfo
240   *coder_semaphore = (SemaphoreInfo *) NULL;
241
242 static SplayTreeInfo
243   *coder_cache = (SplayTreeInfo *) NULL;
244 \f
245 /*
246   Forward declarations.
247 */
248 static MagickBooleanType
249   IsCoderTreeInstantiated(ExceptionInfo *),
250   LoadCoderCache(SplayTreeInfo *,const char *,const char *,const size_t,
251     ExceptionInfo *);
252 \f
253 /*
254 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
255 %                                                                             %
256 %                                                                             %
257 %                                                                             %
258 +  A c q u i r e C o d e r C a c h e                                          %
259 %                                                                             %
260 %                                                                             %
261 %                                                                             %
262 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
263 %
264 %  AcquireCoderCache() caches one or more coder configurations which provides a
265 %  mapping between coder attributes and a coder name.
266 %
267 %  The format of the AcquireCoderCache coder is:
268 %
269 %      SplayTreeInfo *AcquireCoderCache(const char *filename,
270 %        ExceptionInfo *exception)
271 %
272 %  A description of each parameter follows:
273 %
274 %    o filename: the font file name.
275 %
276 %    o exception: return any errors or warnings in this structure.
277 %
278 */
279
280 static void *DestroyCoderNode(void *coder_info)
281 {
282   register CoderInfo
283     *p;
284
285   p=(CoderInfo *) coder_info;
286   if (p->exempt == MagickFalse)
287     {
288       if (p->path != (char *) NULL)
289         p->path=DestroyString(p->path);
290       if (p->name != (char *) NULL)
291         p->name=DestroyString(p->name);
292       if (p->magick != (char *) NULL)
293         p->magick=DestroyString(p->magick);
294     }
295   return(RelinquishMagickMemory(p));
296 }
297
298 static SplayTreeInfo *AcquireCoderCache(const char *filename,
299   ExceptionInfo *exception)
300 {
301   const StringInfo
302     *option;
303
304   LinkedListInfo
305     *options;
306
307   MagickStatusType
308     status;
309
310   register ssize_t
311     i;
312
313   SplayTreeInfo
314     *coder_cache;
315
316   /*
317     Load external coder map.
318   */
319   coder_cache=NewSplayTree(CompareSplayTreeString,RelinquishMagickMemory,
320     DestroyCoderNode);
321   if (coder_cache == (SplayTreeInfo *) NULL)
322     ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
323   status=MagickTrue;
324   options=GetConfigureOptions(filename,exception);
325   option=(const StringInfo *) GetNextValueInLinkedList(options);
326   while (option != (const StringInfo *) NULL)
327   {
328     status&=LoadCoderCache(coder_cache,(const char *)
329       GetStringInfoDatum(option),GetStringInfoPath(option),0,exception);
330     option=(const StringInfo *) GetNextValueInLinkedList(options);
331   }
332   options=DestroyConfigureOptions(options);
333   /*
334     Load built-in coder map.
335   */
336   for (i=0; i < (ssize_t) (sizeof(CoderMap)/sizeof(*CoderMap)); i++)
337   {
338     CoderInfo
339       *coder_info;
340
341     register const CoderMapInfo
342       *p;
343
344     p=CoderMap+i;
345     coder_info=(CoderInfo *) AcquireMagickMemory(sizeof(*coder_info));
346     if (coder_info == (CoderInfo *) NULL)
347       {
348         (void) ThrowMagickException(exception,GetMagickModule(),
349           ResourceLimitError,"MemoryAllocationFailed","`%s'",p->name);
350         continue;
351       }
352     (void) ResetMagickMemory(coder_info,0,sizeof(*coder_info));
353     coder_info->path=(char *) "[built-in]";
354     coder_info->magick=(char *) p->magick;
355     coder_info->name=(char *) p->name;
356     coder_info->exempt=MagickTrue;
357     coder_info->signature=MagickSignature;
358     status&=AddValueToSplayTree(coder_cache,ConstantString(coder_info->magick),
359       coder_info);
360     if (status == MagickFalse)
361       (void) ThrowMagickException(exception,GetMagickModule(),
362         ResourceLimitError,"MemoryAllocationFailed","`%s'",coder_info->name);
363   }
364   return(coder_cache);
365 }
366 \f
367 /*
368 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
369 %                                                                             %
370 %                                                                             %
371 %                                                                             %
372 +   C o d e r C o m p o n e n t G e n e s i s                                 %
373 %                                                                             %
374 %                                                                             %
375 %                                                                             %
376 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
377 %
378 %  CoderComponentGenesis() instantiates the coder component.
379 %
380 %  The format of the CoderComponentGenesis method is:
381 %
382 %      MagickBooleanType CoderComponentGenesis(void)
383 %
384 */
385 MagickPrivate MagickBooleanType CoderComponentGenesis(void)
386 {
387   if (coder_semaphore == (SemaphoreInfo *) NULL)
388     coder_semaphore=AcquireSemaphoreInfo();
389   return(MagickTrue);
390 }
391 \f
392 /*
393 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
394 %                                                                             %
395 %                                                                             %
396 %                                                                             %
397 +   C o d e r C o m p o n e n t T e r m i n u s                               %
398 %                                                                             %
399 %                                                                             %
400 %                                                                             %
401 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
402 %
403 %  CoderComponentTerminus() destroys the coder component.
404 %
405 %  The format of the CoderComponentTerminus method is:
406 %
407 %      CoderComponentTerminus(void)
408 %
409 */
410 MagickPrivate void CoderComponentTerminus(void)
411 {
412   if (coder_semaphore == (SemaphoreInfo *) NULL)
413     ActivateSemaphoreInfo(&coder_semaphore);
414   LockSemaphoreInfo(coder_semaphore);
415   if (coder_cache != (SplayTreeInfo *) NULL)
416     coder_cache=DestroySplayTree(coder_cache);
417   UnlockSemaphoreInfo(coder_semaphore);
418   RelinquishSemaphoreInfo(&coder_semaphore);
419 }
420 \f
421 /*
422 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
423 %                                                                             %
424 %                                                                             %
425 %                                                                             %
426 +   G e t C o d e r I n f o                                                   %
427 %                                                                             %
428 %                                                                             %
429 %                                                                             %
430 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
431 %
432 %  GetCoderInfo searches the coder list for the specified name and if found
433 %  returns attributes for that coder.
434 %
435 %  The format of the GetCoderInfo method is:
436 %
437 %      const CoderInfo *GetCoderInfo(const char *name,ExceptionInfo *exception)
438 %
439 %  A description of each parameter follows:
440 %
441 %    o name: the coder name.
442 %
443 %    o exception: return any errors or warnings in this structure.
444 %
445 */
446 MagickExport const CoderInfo *GetCoderInfo(const char *name,
447   ExceptionInfo *exception)
448 {
449   const CoderInfo
450     *coder_info;
451
452   assert(exception != (ExceptionInfo *) NULL);
453   if (IsCoderTreeInstantiated(exception) == MagickFalse)
454     return((const CoderInfo *) NULL);
455   LockSemaphoreInfo(coder_semaphore);
456   if ((name == (const char *) NULL) || (LocaleCompare(name,"*") == 0))
457     {
458       ResetSplayTreeIterator(coder_cache);
459       coder_info=(const CoderInfo *) GetNextValueInSplayTree(coder_cache);
460       UnlockSemaphoreInfo(coder_semaphore);
461       return(coder_info);
462     }
463   coder_info=(const CoderInfo *) GetValueFromSplayTree(coder_cache,name);
464   UnlockSemaphoreInfo(coder_semaphore);
465   return(coder_info);
466 }
467 \f
468 /*
469 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
470 %                                                                             %
471 %                                                                             %
472 %                                                                             %
473 %   G e t C o d e r I n f o L i s t                                           %
474 %                                                                             %
475 %                                                                             %
476 %                                                                             %
477 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
478 %
479 %  GetCoderInfoList() returns any coder_map that match the specified pattern.
480 %  The format of the GetCoderInfoList function is:
481 %
482 %      const CoderInfo **GetCoderInfoList(const char *pattern,
483 %        size_t *number_coders,ExceptionInfo *exception)
484 %
485 %  A description of each parameter follows:
486 %
487 %    o pattern: Specifies a pointer to a text string containing a pattern.
488 %
489 %    o number_coders:  This integer returns the number of coders in the list.
490 %
491 %    o exception: return any errors or warnings in this structure.
492 %
493 */
494
495 static int CoderInfoCompare(const void *x,const void *y)
496 {
497   const CoderInfo
498     **p,
499     **q;
500
501   p=(const CoderInfo **) x,
502   q=(const CoderInfo **) y;
503   if (LocaleCompare((*p)->path,(*q)->path) == 0)
504     return(LocaleCompare((*p)->name,(*q)->name));
505   return(LocaleCompare((*p)->path,(*q)->path));
506 }
507
508 MagickExport const CoderInfo **GetCoderInfoList(const char *pattern,
509   size_t *number_coders,ExceptionInfo *exception)
510 {
511   const CoderInfo
512     **coder_map;
513
514   register const CoderInfo
515     *p;
516
517   register ssize_t
518     i;
519
520   /*
521     Allocate coder list.
522   */
523   assert(pattern != (char *) NULL);
524   (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
525   assert(number_coders != (size_t *) NULL);
526   *number_coders=0;
527   p=GetCoderInfo("*",exception);
528   if (p == (const CoderInfo *) NULL)
529     return((const CoderInfo **) NULL);
530   coder_map=(const CoderInfo **) AcquireQuantumMemory((size_t)
531     GetNumberOfNodesInSplayTree(coder_cache)+1UL,sizeof(*coder_map));
532   if (coder_map == (const CoderInfo **) NULL)
533     return((const CoderInfo **) NULL);
534   /*
535     Generate coder list.
536   */
537   LockSemaphoreInfo(coder_semaphore);
538   ResetSplayTreeIterator(coder_cache);
539   p=(const CoderInfo *) GetNextValueInSplayTree(coder_cache);
540   for (i=0; p != (const CoderInfo *) NULL; )
541   {
542     if ((p->stealth == MagickFalse) &&
543         (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
544       coder_map[i++]=p;
545     p=(const CoderInfo *) GetNextValueInSplayTree(coder_cache);
546   }
547   UnlockSemaphoreInfo(coder_semaphore);
548   qsort((void *) coder_map,(size_t) i,sizeof(*coder_map),CoderInfoCompare);
549   coder_map[i]=(CoderInfo *) NULL;
550   *number_coders=(size_t) i;
551   return(coder_map);
552 }
553 \f
554 /*
555 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
556 %                                                                             %
557 %                                                                             %
558 %                                                                             %
559 %   G e t C o d e r L i s t                                                   %
560 %                                                                             %
561 %                                                                             %
562 %                                                                             %
563 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
564 %
565 %  GetCoderList() returns any coder_map that match the specified pattern.
566 %
567 %  The format of the GetCoderList function is:
568 %
569 %      char **GetCoderList(const char *pattern,size_t *number_coders,
570 %        ExceptionInfo *exception)
571 %
572 %  A description of each parameter follows:
573 %
574 %    o pattern: Specifies a pointer to a text string containing a pattern.
575 %
576 %    o number_coders:  This integer returns the number of coders in the list.
577 %
578 %    o exception: return any errors or warnings in this structure.
579 %
580 */
581
582 static int CoderCompare(const void *x,const void *y)
583 {
584   register const char
585     **p,
586     **q;
587
588   p=(const char **) x;
589   q=(const char **) y;
590   return(LocaleCompare(*p,*q));
591 }
592
593 MagickExport char **GetCoderList(const char *pattern,
594   size_t *number_coders,ExceptionInfo *exception)
595 {
596   char
597     **coder_map;
598
599   register const CoderInfo
600     *p;
601
602   register ssize_t
603     i;
604
605   /*
606     Allocate coder list.
607   */
608   assert(pattern != (char *) NULL);
609   (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
610   assert(number_coders != (size_t *) NULL);
611   *number_coders=0;
612   p=GetCoderInfo("*",exception);
613   if (p == (const CoderInfo *) NULL)
614     return((char **) NULL);
615   coder_map=(char **) AcquireQuantumMemory((size_t)
616     GetNumberOfNodesInSplayTree(coder_cache)+1UL,sizeof(*coder_map));
617   if (coder_map == (char **) NULL)
618     return((char **) NULL);
619   /*
620     Generate coder list.
621   */
622   LockSemaphoreInfo(coder_semaphore);
623   ResetSplayTreeIterator(coder_cache);
624   p=(const CoderInfo *) GetNextValueInSplayTree(coder_cache);
625   for (i=0; p != (const CoderInfo *) NULL; )
626   {
627     if ((p->stealth == MagickFalse) &&
628         (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
629       coder_map[i++]=ConstantString(p->name);
630     p=(const CoderInfo *) GetNextValueInSplayTree(coder_cache);
631   }
632   UnlockSemaphoreInfo(coder_semaphore);
633   qsort((void *) coder_map,(size_t) i,sizeof(*coder_map),CoderCompare);
634   coder_map[i]=(char *) NULL;
635   *number_coders=(size_t) i;
636   return(coder_map);
637 }
638 \f
639 /*
640 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
641 %                                                                             %
642 %                                                                             %
643 %                                                                             %
644 +   I s C o d e r T r e e I n s t a n t i a t e d                             %
645 %                                                                             %
646 %                                                                             %
647 %                                                                             %
648 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
649 %
650 %  IsCoderTreeInstantiated() determines if the coder tree is instantiated.  If
651 %  not, it instantiates the tree and returns it.
652 %
653 %  The format of the IsCoderInstantiated method is:
654 %
655 %      MagickBooleanType IsCoderTreeInstantiated(ExceptionInfo *exception)
656 %
657 %  A description of each parameter follows.
658 %
659 %    o exception: return any errors or warnings in this structure.
660 %
661 */
662 static MagickBooleanType IsCoderTreeInstantiated(ExceptionInfo *exception)
663 {
664   if (coder_cache == (SplayTreeInfo *) NULL)
665     {
666       if (coder_semaphore == (SemaphoreInfo *) NULL)
667         ActivateSemaphoreInfo(&coder_semaphore);
668       LockSemaphoreInfo(coder_semaphore);
669       if (coder_cache == (SplayTreeInfo *) NULL)
670         coder_cache=AcquireCoderCache(MagickCoderFilename,exception);
671       UnlockSemaphoreInfo(coder_semaphore);
672     }
673   return(coder_cache != (SplayTreeInfo *) NULL ? MagickTrue : MagickFalse);
674 }
675 \f
676 /*
677 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
678 %                                                                             %
679 %                                                                             %
680 %                                                                             %
681 %  L i s t C o d e r I n f o                                                  %
682 %                                                                             %
683 %                                                                             %
684 %                                                                             %
685 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
686 %
687 %  ListCoderInfo() lists the coder info to a file.
688 %
689 %  The format of the ListCoderInfo coder is:
690 %
691 %      MagickBooleanType ListCoderInfo(FILE *file,ExceptionInfo *exception)
692 %
693 %  A description of each parameter follows.
694 %
695 %    o file:  An pointer to a FILE.
696 %
697 %    o exception: return any errors or warnings in this structure.
698 %
699 */
700 MagickExport MagickBooleanType ListCoderInfo(FILE *file,
701   ExceptionInfo *exception)
702 {
703   const char
704     *path;
705
706   const CoderInfo
707     **coder_info;
708
709   register ssize_t
710     i;
711
712   size_t
713     number_coders;
714
715   ssize_t
716     j;
717
718   if (file == (const FILE *) NULL)
719     file=stdout;
720   coder_info=GetCoderInfoList("*",&number_coders,exception);
721   if (coder_info == (const CoderInfo **) NULL)
722     return(MagickFalse);
723   path=(const char *) NULL;
724   for (i=0; i < (ssize_t) number_coders; i++)
725   {
726     if (coder_info[i]->stealth != MagickFalse)
727       continue;
728     if ((path == (const char *) NULL) ||
729         (LocaleCompare(path,coder_info[i]->path) != 0))
730       {
731         if (coder_info[i]->path != (char *) NULL)
732           (void) FormatLocaleFile(file,"\nPath: %s\n\n",coder_info[i]->path);
733         (void) FormatLocaleFile(file,"Magick      Coder\n");
734         (void) FormatLocaleFile(file,
735           "-------------------------------------------------"
736           "------------------------------\n");
737       }
738     path=coder_info[i]->path;
739     (void) FormatLocaleFile(file,"%s",coder_info[i]->magick);
740     for (j=(ssize_t) strlen(coder_info[i]->magick); j <= 11; j++)
741       (void) FormatLocaleFile(file," ");
742     if (coder_info[i]->name != (char *) NULL)
743       (void) FormatLocaleFile(file,"%s",coder_info[i]->name);
744     (void) FormatLocaleFile(file,"\n");
745   }
746   coder_info=(const CoderInfo **) RelinquishMagickMemory((void *) coder_info);
747   (void) fflush(file);
748   return(MagickTrue);
749 }
750 \f
751 /*
752 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
753 %                                                                             %
754 %                                                                             %
755 %                                                                             %
756 +   L o a d C o d e r L i s t                                                 %
757 %                                                                             %
758 %                                                                             %
759 %                                                                             %
760 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
761 %
762 %  LoadCoderCache() loads the coder configurations which provides a
763 %  mapping between coder attributes and a coder name.
764 %
765 %  The format of the LoadCoderCache coder is:
766 %
767 %      MagickBooleanType LoadCoderCache(SplayTreeInfo *coder_cache,
768 %        const char *xml,const char *filename,const size_t depth,
769 %        ExceptionInfo *exception)
770 %
771 %  A description of each parameter follows:
772 %
773 %    o xml:  The coder list in XML format.
774 %
775 %    o filename:  The coder list filename.
776 %
777 %    o depth: depth of <include /> statements.
778 %
779 %    o exception: return any errors or warnings in this structure.
780 %
781 */
782 static MagickBooleanType LoadCoderCache(SplayTreeInfo *coder_cache,
783   const char *xml,const char *filename,const size_t depth,
784   ExceptionInfo *exception)
785 {
786   char
787     keyword[MaxTextExtent],
788     *token;
789
790   const char
791     *q;
792
793   CoderInfo
794     *coder_info;
795
796   MagickStatusType
797     status;
798
799   /*
800     Load the coder map file.
801   */
802   (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
803     "Loading coder configuration file \"%s\" ...",filename);
804   if (xml == (const char *) NULL)
805     return(MagickFalse);
806   status=MagickTrue;
807   coder_info=(CoderInfo *) NULL;
808   token=AcquireString(xml);
809   for (q=(char *) xml; *q != '\0'; )
810   {
811     /*
812       Interpret XML.
813     */
814     GetMagickToken(q,&q,token);
815     if (*token == '\0')
816       break;
817     (void) CopyMagickString(keyword,token,MaxTextExtent);
818     if (LocaleNCompare(keyword,"<!DOCTYPE",9) == 0)
819       {
820         /*
821           Doctype element.
822         */
823         while ((LocaleNCompare(q,"]>",2) != 0) && (*q != '\0'))
824           GetMagickToken(q,&q,token);
825         continue;
826       }
827     if (LocaleNCompare(keyword,"<!--",4) == 0)
828       {
829         /*
830           Comment element.
831         */
832         while ((LocaleNCompare(q,"->",2) != 0) && (*q != '\0'))
833           GetMagickToken(q,&q,token);
834         continue;
835       }
836     if (LocaleCompare(keyword,"<include") == 0)
837       {
838         /*
839           Include element.
840         */
841         while (((*token != '/') && (*(token+1) != '>')) && (*q != '\0'))
842         {
843           (void) CopyMagickString(keyword,token,MaxTextExtent);
844           GetMagickToken(q,&q,token);
845           if (*token != '=')
846             continue;
847           GetMagickToken(q,&q,token);
848           if (LocaleCompare(keyword,"file") == 0)
849             {
850               if (depth > 200)
851                 (void) ThrowMagickException(exception,GetMagickModule(),
852                   ConfigureError,"IncludeNodeNestedTooDeeply","`%s'",token);
853               else
854                 {
855                   char
856                     path[MaxTextExtent],
857                     *xml;
858
859                   GetPathComponent(filename,HeadPath,path);
860                   if (*path != '\0')
861                     (void) ConcatenateMagickString(path,DirectorySeparator,
862                       MaxTextExtent);
863                   if (*token == *DirectorySeparator)
864                     (void) CopyMagickString(path,token,MaxTextExtent);
865                   else
866                     (void) ConcatenateMagickString(path,token,MaxTextExtent);
867                   xml=FileToXML(path,~0UL);
868                   if (xml != (char *) NULL)
869                     {
870                       status&=LoadCoderCache(coder_cache,xml,path,depth+1,
871                         exception);
872                       xml=(char *) RelinquishMagickMemory(xml);
873                     }
874                 }
875             }
876         }
877         continue;
878       }
879     if (LocaleCompare(keyword,"<coder") == 0)
880       {
881         /*
882           Coder element.
883         */
884         coder_info=(CoderInfo *) AcquireMagickMemory(sizeof(*coder_info));
885         if (coder_info == (CoderInfo *) NULL)
886           ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
887         (void) ResetMagickMemory(coder_info,0,sizeof(*coder_info));
888         coder_info->path=ConstantString(filename);
889         coder_info->exempt=MagickFalse;
890         coder_info->signature=MagickSignature;
891         continue;
892       }
893     if (coder_info == (CoderInfo *) NULL)
894       continue;
895     if (LocaleCompare(keyword,"/>") == 0)
896       {
897         status=AddValueToSplayTree(coder_cache,ConstantString(
898           coder_info->magick),coder_info);
899         if (status == MagickFalse)
900           (void) ThrowMagickException(exception,GetMagickModule(),
901             ResourceLimitError,"MemoryAllocationFailed","`%s'",
902             coder_info->magick);
903         coder_info=(CoderInfo *) NULL;
904         continue;
905       }
906     GetMagickToken(q,(const char **) NULL,token);
907     if (*token != '=')
908       continue;
909     GetMagickToken(q,&q,token);
910     GetMagickToken(q,&q,token);
911     switch (*keyword)
912     {
913       case 'M':
914       case 'm':
915       {
916         if (LocaleCompare((char *) keyword,"magick") == 0)
917           {
918             coder_info->magick=ConstantString(token);
919             break;
920           }
921         break;
922       }
923       case 'N':
924       case 'n':
925       {
926         if (LocaleCompare((char *) keyword,"name") == 0)
927           {
928             coder_info->name=ConstantString(token);
929             break;
930           }
931         break;
932       }
933       case 'S':
934       case 's':
935       {
936         if (LocaleCompare((char *) keyword,"stealth") == 0)
937           {
938             coder_info->stealth=IsStringTrue(token);
939             break;
940           }
941         break;
942       }
943       default:
944         break;
945     }
946   }
947   token=(char *) RelinquishMagickMemory(token);
948   return(status != 0 ? MagickTrue : MagickFalse);
949 }