]> 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-2015 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     { "JPE", "JPEG" },
157     { "JPG", "JPEG" },
158     { "JPS", "JPEG" },
159     { "JPX", "JP2" },
160     { "K", "RAW" },
161     { "LOGO", "MAGICK" },
162     { "M2V", "MPEG" },
163     { "M4V", "MPEG" },
164     { "M", "RAW" },
165     { "MKV", "MPEG" },
166     { "MNG", "PNG" },
167     { "MOV", "MPEG" },
168     { "MP4", "MPEG" },
169     { "MPG", "MPEG" },
170     { "MPRI", "MPR" },
171     { "MEF", "DNG" },
172     { "MRW", "DNG" },
173     { "MSVG", "SVG" },
174     { "NEF", "DNG" },
175     { "NETSCAPE", "MAGICK" },
176     { "NRW", "DNG" },
177     { "O", "RAW" },
178     { "ORF", "DNG" },
179     { "OTF", "TTF" },
180     { "P7", "PNM" },
181     { "PAL", "UYVY" },
182     { "PAM", "PNM" },
183     { "PBM", "PNM" },
184     { "PCDS", "PCD" },
185     { "PDFA", "PDF" },
186     { "PEF", "DNG" },
187     { "PEF", "DNG" },
188     { "PFA", "TTF" },
189     { "PFB", "TTF" },
190     { "PFM", "PNM" },
191     { "PGM", "PNM" },
192     { "PGX", "JP2" },
193     { "PICON", "XPM" },
194     { "PJPEG", "JPEG" },
195     { "PM", "XPM" },
196     { "PNG24", "PNG" },
197     { "PNG32", "PNG" },
198     { "PNG8", "PNG" },
199     { "PPM", "PNM" },
200     { "PSB", "PSD" },
201     { "PTIF", "TIFF" },
202     { "RADIAL-GRADIENT", "GRADIENT" },
203     { "RAF", "DNG" },
204     { "RAS", "SUN" },
205     { "RAW", "DNG" },
206     { "RGBA", "RGB" },
207     { "RGBO", "RGB" },
208     { "RMF", "DNG" },
209     { "R", "RAW" },
210     { "ROSE", "MAGICK" },
211     { "RW2", "DNG" },
212     { "SHTML", "HTML" },
213     { "SIX", "SIXEL" },
214     { "SPARSE-COLOR", "TXT" },
215     { "SR2", "DNG" },
216     { "SRF", "DNG" },
217     { "SVGZ", "SVG" },
218     { "TEXT", "TXT" },
219     { "TIFF64", "TIFF" },
220     { "TIF", "TIFF" },
221     { "TTC", "TTF" },
222     { "UBRL", "BRAILLE" },
223     { "VDA", "TGA" },
224     { "VST", "TGA" },
225     { "WIZARD", "MAGICK" },
226 #if defined(MAGICKCORE_WINGDI32_DELEGATE)
227     { "WMF", "EMF" },
228 #endif
229     { "WMV", "MPEG" },
230     { "WMZ", "WMF" },
231     { "X3f", "DNG" },
232     { "XMP", "META" },
233     { "XTRNARRAY", "XTRN" },
234     { "XTRNBLOB", "XTRN" },
235     { "XTRNFILE", "XTRN" },
236     { "XTRNIMAGE", "XTRN" },
237     { "XV", "VIFF" },
238     { "Y", "RAW" },
239     { "YCbCrA", "YCbCr" }
240  };
241
242 static SemaphoreInfo
243   *coder_semaphore = (SemaphoreInfo *) NULL;
244
245 static SplayTreeInfo
246   *coder_cache = (SplayTreeInfo *) NULL;
247 \f
248 /*
249   Forward declarations.
250 */
251 static MagickBooleanType
252   IsCoderTreeInstantiated(ExceptionInfo *),
253   LoadCoderCache(SplayTreeInfo *,const char *,const char *,const size_t,
254     ExceptionInfo *);
255 \f
256 /*
257 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
258 %                                                                             %
259 %                                                                             %
260 %                                                                             %
261 +  A c q u i r e C o d e r C a c h e                                          %
262 %                                                                             %
263 %                                                                             %
264 %                                                                             %
265 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
266 %
267 %  AcquireCoderCache() caches one or more coder configurations which provides a
268 %  mapping between coder attributes and a coder name.
269 %
270 %  The format of the AcquireCoderCache coder is:
271 %
272 %      SplayTreeInfo *AcquireCoderCache(const char *filename,
273 %        ExceptionInfo *exception)
274 %
275 %  A description of each parameter follows:
276 %
277 %    o filename: the font file name.
278 %
279 %    o exception: return any errors or warnings in this structure.
280 %
281 */
282
283 static void *DestroyCoderNode(void *coder_info)
284 {
285   register CoderInfo
286     *p;
287
288   p=(CoderInfo *) coder_info;
289   if (p->exempt == MagickFalse)
290     {
291       if (p->path != (char *) NULL)
292         p->path=DestroyString(p->path);
293       if (p->name != (char *) NULL)
294         p->name=DestroyString(p->name);
295       if (p->magick != (char *) NULL)
296         p->magick=DestroyString(p->magick);
297     }
298   return(RelinquishMagickMemory(p));
299 }
300
301 static SplayTreeInfo *AcquireCoderCache(const char *filename,
302   ExceptionInfo *exception)
303 {
304   const StringInfo
305     *option;
306
307   LinkedListInfo
308     *options;
309
310   MagickStatusType
311     status;
312
313   register ssize_t
314     i;
315
316   SplayTreeInfo
317     *coder_cache;
318
319   /*
320     Load external coder map.
321   */
322   coder_cache=NewSplayTree(CompareSplayTreeString,RelinquishMagickMemory,
323     DestroyCoderNode);
324   if (coder_cache == (SplayTreeInfo *) NULL)
325     ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
326   status=MagickTrue;
327   options=GetConfigureOptions(filename,exception);
328   option=(const StringInfo *) GetNextValueInLinkedList(options);
329   while (option != (const StringInfo *) NULL)
330   {
331     status&=LoadCoderCache(coder_cache,(const char *)
332       GetStringInfoDatum(option),GetStringInfoPath(option),0,exception);
333     option=(const StringInfo *) GetNextValueInLinkedList(options);
334   }
335   options=DestroyConfigureOptions(options);
336   /*
337     Load built-in coder map.
338   */
339   for (i=0; i < (ssize_t) (sizeof(CoderMap)/sizeof(*CoderMap)); i++)
340   {
341     CoderInfo
342       *coder_info;
343
344     register const CoderMapInfo
345       *p;
346
347     p=CoderMap+i;
348     coder_info=(CoderInfo *) AcquireMagickMemory(sizeof(*coder_info));
349     if (coder_info == (CoderInfo *) NULL)
350       {
351         (void) ThrowMagickException(exception,GetMagickModule(),
352           ResourceLimitError,"MemoryAllocationFailed","`%s'",p->name);
353         continue;
354       }
355     (void) ResetMagickMemory(coder_info,0,sizeof(*coder_info));
356     coder_info->path=(char *) "[built-in]";
357     coder_info->magick=(char *) p->magick;
358     coder_info->name=(char *) p->name;
359     coder_info->exempt=MagickTrue;
360     coder_info->signature=MagickSignature;
361     status&=AddValueToSplayTree(coder_cache,ConstantString(coder_info->magick),
362       coder_info);
363     if (status == MagickFalse)
364       (void) ThrowMagickException(exception,GetMagickModule(),
365         ResourceLimitError,"MemoryAllocationFailed","`%s'",coder_info->name);
366   }
367   return(coder_cache);
368 }
369 \f
370 /*
371 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
372 %                                                                             %
373 %                                                                             %
374 %                                                                             %
375 +   C o d e r C o m p o n e n t G e n e s i s                                 %
376 %                                                                             %
377 %                                                                             %
378 %                                                                             %
379 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
380 %
381 %  CoderComponentGenesis() instantiates the coder component.
382 %
383 %  The format of the CoderComponentGenesis method is:
384 %
385 %      MagickBooleanType CoderComponentGenesis(void)
386 %
387 */
388 MagickPrivate MagickBooleanType CoderComponentGenesis(void)
389 {
390   if (coder_semaphore == (SemaphoreInfo *) NULL)
391     coder_semaphore=AcquireSemaphoreInfo();
392   return(MagickTrue);
393 }
394 \f
395 /*
396 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
397 %                                                                             %
398 %                                                                             %
399 %                                                                             %
400 +   C o d e r C o m p o n e n t T e r m i n u s                               %
401 %                                                                             %
402 %                                                                             %
403 %                                                                             %
404 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
405 %
406 %  CoderComponentTerminus() destroys the coder component.
407 %
408 %  The format of the CoderComponentTerminus method is:
409 %
410 %      CoderComponentTerminus(void)
411 %
412 */
413 MagickPrivate void CoderComponentTerminus(void)
414 {
415   if (coder_semaphore == (SemaphoreInfo *) NULL)
416     ActivateSemaphoreInfo(&coder_semaphore);
417   LockSemaphoreInfo(coder_semaphore);
418   if (coder_cache != (SplayTreeInfo *) NULL)
419     coder_cache=DestroySplayTree(coder_cache);
420   UnlockSemaphoreInfo(coder_semaphore);
421   RelinquishSemaphoreInfo(&coder_semaphore);
422 }
423 \f
424 /*
425 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
426 %                                                                             %
427 %                                                                             %
428 %                                                                             %
429 +   G e t C o d e r I n f o                                                   %
430 %                                                                             %
431 %                                                                             %
432 %                                                                             %
433 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
434 %
435 %  GetCoderInfo searches the coder list for the specified name and if found
436 %  returns attributes for that coder.
437 %
438 %  The format of the GetCoderInfo method is:
439 %
440 %      const CoderInfo *GetCoderInfo(const char *name,ExceptionInfo *exception)
441 %
442 %  A description of each parameter follows:
443 %
444 %    o name: the coder name.
445 %
446 %    o exception: return any errors or warnings in this structure.
447 %
448 */
449 MagickExport const CoderInfo *GetCoderInfo(const char *name,
450   ExceptionInfo *exception)
451 {
452   const CoderInfo
453     *coder_info;
454
455   assert(exception != (ExceptionInfo *) NULL);
456   if (IsCoderTreeInstantiated(exception) == MagickFalse)
457     return((const CoderInfo *) NULL);
458   LockSemaphoreInfo(coder_semaphore);
459   if ((name == (const char *) NULL) || (LocaleCompare(name,"*") == 0))
460     {
461       ResetSplayTreeIterator(coder_cache);
462       coder_info=(const CoderInfo *) GetNextValueInSplayTree(coder_cache);
463       UnlockSemaphoreInfo(coder_semaphore);
464       return(coder_info);
465     }
466   coder_info=(const CoderInfo *) GetValueFromSplayTree(coder_cache,name);
467   UnlockSemaphoreInfo(coder_semaphore);
468   return(coder_info);
469 }
470 \f
471 /*
472 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
473 %                                                                             %
474 %                                                                             %
475 %                                                                             %
476 %   G e t C o d e r I n f o L i s t                                           %
477 %                                                                             %
478 %                                                                             %
479 %                                                                             %
480 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
481 %
482 %  GetCoderInfoList() returns any coder_map that match the specified pattern.
483 %  The format of the GetCoderInfoList function is:
484 %
485 %      const CoderInfo **GetCoderInfoList(const char *pattern,
486 %        size_t *number_coders,ExceptionInfo *exception)
487 %
488 %  A description of each parameter follows:
489 %
490 %    o pattern: Specifies a pointer to a text string containing a pattern.
491 %
492 %    o number_coders:  This integer returns the number of coders in the list.
493 %
494 %    o exception: return any errors or warnings in this structure.
495 %
496 */
497
498 static int CoderInfoCompare(const void *x,const void *y)
499 {
500   const CoderInfo
501     **p,
502     **q;
503
504   p=(const CoderInfo **) x,
505   q=(const CoderInfo **) y;
506   if (LocaleCompare((*p)->path,(*q)->path) == 0)
507     return(LocaleCompare((*p)->name,(*q)->name));
508   return(LocaleCompare((*p)->path,(*q)->path));
509 }
510
511 MagickExport const CoderInfo **GetCoderInfoList(const char *pattern,
512   size_t *number_coders,ExceptionInfo *exception)
513 {
514   const CoderInfo
515     **coder_map;
516
517   register const CoderInfo
518     *p;
519
520   register ssize_t
521     i;
522
523   /*
524     Allocate coder list.
525   */
526   assert(pattern != (char *) NULL);
527   (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
528   assert(number_coders != (size_t *) NULL);
529   *number_coders=0;
530   p=GetCoderInfo("*",exception);
531   if (p == (const CoderInfo *) NULL)
532     return((const CoderInfo **) NULL);
533   coder_map=(const CoderInfo **) AcquireQuantumMemory((size_t)
534     GetNumberOfNodesInSplayTree(coder_cache)+1UL,sizeof(*coder_map));
535   if (coder_map == (const CoderInfo **) NULL)
536     return((const CoderInfo **) NULL);
537   /*
538     Generate coder list.
539   */
540   LockSemaphoreInfo(coder_semaphore);
541   ResetSplayTreeIterator(coder_cache);
542   p=(const CoderInfo *) GetNextValueInSplayTree(coder_cache);
543   for (i=0; p != (const CoderInfo *) NULL; )
544   {
545     if ((p->stealth == MagickFalse) &&
546         (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
547       coder_map[i++]=p;
548     p=(const CoderInfo *) GetNextValueInSplayTree(coder_cache);
549   }
550   UnlockSemaphoreInfo(coder_semaphore);
551   qsort((void *) coder_map,(size_t) i,sizeof(*coder_map),CoderInfoCompare);
552   coder_map[i]=(CoderInfo *) NULL;
553   *number_coders=(size_t) i;
554   return(coder_map);
555 }
556 \f
557 /*
558 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
559 %                                                                             %
560 %                                                                             %
561 %                                                                             %
562 %   G e t C o d e r L i s t                                                   %
563 %                                                                             %
564 %                                                                             %
565 %                                                                             %
566 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
567 %
568 %  GetCoderList() returns any coder_map that match the specified pattern.
569 %
570 %  The format of the GetCoderList function is:
571 %
572 %      char **GetCoderList(const char *pattern,size_t *number_coders,
573 %        ExceptionInfo *exception)
574 %
575 %  A description of each parameter follows:
576 %
577 %    o pattern: Specifies a pointer to a text string containing a pattern.
578 %
579 %    o number_coders:  This integer returns the number of coders in the list.
580 %
581 %    o exception: return any errors or warnings in this structure.
582 %
583 */
584
585 static int CoderCompare(const void *x,const void *y)
586 {
587   register const char
588     **p,
589     **q;
590
591   p=(const char **) x;
592   q=(const char **) y;
593   return(LocaleCompare(*p,*q));
594 }
595
596 MagickExport char **GetCoderList(const char *pattern,
597   size_t *number_coders,ExceptionInfo *exception)
598 {
599   char
600     **coder_map;
601
602   register const CoderInfo
603     *p;
604
605   register ssize_t
606     i;
607
608   /*
609     Allocate coder list.
610   */
611   assert(pattern != (char *) NULL);
612   (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
613   assert(number_coders != (size_t *) NULL);
614   *number_coders=0;
615   p=GetCoderInfo("*",exception);
616   if (p == (const CoderInfo *) NULL)
617     return((char **) NULL);
618   coder_map=(char **) AcquireQuantumMemory((size_t)
619     GetNumberOfNodesInSplayTree(coder_cache)+1UL,sizeof(*coder_map));
620   if (coder_map == (char **) NULL)
621     return((char **) NULL);
622   /*
623     Generate coder list.
624   */
625   LockSemaphoreInfo(coder_semaphore);
626   ResetSplayTreeIterator(coder_cache);
627   p=(const CoderInfo *) GetNextValueInSplayTree(coder_cache);
628   for (i=0; p != (const CoderInfo *) NULL; )
629   {
630     if ((p->stealth == MagickFalse) &&
631         (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
632       coder_map[i++]=ConstantString(p->name);
633     p=(const CoderInfo *) GetNextValueInSplayTree(coder_cache);
634   }
635   UnlockSemaphoreInfo(coder_semaphore);
636   qsort((void *) coder_map,(size_t) i,sizeof(*coder_map),CoderCompare);
637   coder_map[i]=(char *) NULL;
638   *number_coders=(size_t) i;
639   return(coder_map);
640 }
641 \f
642 /*
643 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
644 %                                                                             %
645 %                                                                             %
646 %                                                                             %
647 +   I s C o d e r T r e e I n s t a n t i a t e d                             %
648 %                                                                             %
649 %                                                                             %
650 %                                                                             %
651 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
652 %
653 %  IsCoderTreeInstantiated() determines if the coder tree is instantiated.  If
654 %  not, it instantiates the tree and returns it.
655 %
656 %  The format of the IsCoderInstantiated method is:
657 %
658 %      MagickBooleanType IsCoderTreeInstantiated(ExceptionInfo *exception)
659 %
660 %  A description of each parameter follows.
661 %
662 %    o exception: return any errors or warnings in this structure.
663 %
664 */
665 static MagickBooleanType IsCoderTreeInstantiated(ExceptionInfo *exception)
666 {
667   if (coder_cache == (SplayTreeInfo *) NULL)
668     {
669       if (coder_semaphore == (SemaphoreInfo *) NULL)
670         ActivateSemaphoreInfo(&coder_semaphore);
671       LockSemaphoreInfo(coder_semaphore);
672       if (coder_cache == (SplayTreeInfo *) NULL)
673         coder_cache=AcquireCoderCache(MagickCoderFilename,exception);
674       UnlockSemaphoreInfo(coder_semaphore);
675     }
676   return(coder_cache != (SplayTreeInfo *) NULL ? MagickTrue : MagickFalse);
677 }
678 \f
679 /*
680 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
681 %                                                                             %
682 %                                                                             %
683 %                                                                             %
684 %  L i s t C o d e r I n f o                                                  %
685 %                                                                             %
686 %                                                                             %
687 %                                                                             %
688 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
689 %
690 %  ListCoderInfo() lists the coder info to a file.
691 %
692 %  The format of the ListCoderInfo coder is:
693 %
694 %      MagickBooleanType ListCoderInfo(FILE *file,ExceptionInfo *exception)
695 %
696 %  A description of each parameter follows.
697 %
698 %    o file:  An pointer to a FILE.
699 %
700 %    o exception: return any errors or warnings in this structure.
701 %
702 */
703 MagickExport MagickBooleanType ListCoderInfo(FILE *file,
704   ExceptionInfo *exception)
705 {
706   const char
707     *path;
708
709   const CoderInfo
710     **coder_info;
711
712   register ssize_t
713     i;
714
715   size_t
716     number_coders;
717
718   ssize_t
719     j;
720
721   if (file == (const FILE *) NULL)
722     file=stdout;
723   coder_info=GetCoderInfoList("*",&number_coders,exception);
724   if (coder_info == (const CoderInfo **) NULL)
725     return(MagickFalse);
726   path=(const char *) NULL;
727   for (i=0; i < (ssize_t) number_coders; i++)
728   {
729     if (coder_info[i]->stealth != MagickFalse)
730       continue;
731     if ((path == (const char *) NULL) ||
732         (LocaleCompare(path,coder_info[i]->path) != 0))
733       {
734         if (coder_info[i]->path != (char *) NULL)
735           (void) FormatLocaleFile(file,"\nPath: %s\n\n",coder_info[i]->path);
736         (void) FormatLocaleFile(file,"Magick      Coder\n");
737         (void) FormatLocaleFile(file,
738           "-------------------------------------------------"
739           "------------------------------\n");
740       }
741     path=coder_info[i]->path;
742     (void) FormatLocaleFile(file,"%s",coder_info[i]->magick);
743     for (j=(ssize_t) strlen(coder_info[i]->magick); j <= 11; j++)
744       (void) FormatLocaleFile(file," ");
745     if (coder_info[i]->name != (char *) NULL)
746       (void) FormatLocaleFile(file,"%s",coder_info[i]->name);
747     (void) FormatLocaleFile(file,"\n");
748   }
749   coder_info=(const CoderInfo **) RelinquishMagickMemory((void *) coder_info);
750   (void) fflush(file);
751   return(MagickTrue);
752 }
753 \f
754 /*
755 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
756 %                                                                             %
757 %                                                                             %
758 %                                                                             %
759 +   L o a d C o d e r L i s t                                                 %
760 %                                                                             %
761 %                                                                             %
762 %                                                                             %
763 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
764 %
765 %  LoadCoderCache() loads the coder configurations which provides a
766 %  mapping between coder attributes and a coder name.
767 %
768 %  The format of the LoadCoderCache coder is:
769 %
770 %      MagickBooleanType LoadCoderCache(SplayTreeInfo *coder_cache,
771 %        const char *xml,const char *filename,const size_t depth,
772 %        ExceptionInfo *exception)
773 %
774 %  A description of each parameter follows:
775 %
776 %    o xml:  The coder list in XML format.
777 %
778 %    o filename:  The coder list filename.
779 %
780 %    o depth: depth of <include /> statements.
781 %
782 %    o exception: return any errors or warnings in this structure.
783 %
784 */
785 static MagickBooleanType LoadCoderCache(SplayTreeInfo *coder_cache,
786   const char *xml,const char *filename,const size_t depth,
787   ExceptionInfo *exception)
788 {
789   char
790     keyword[MaxTextExtent],
791     *token;
792
793   const char
794     *q;
795
796   CoderInfo
797     *coder_info;
798
799   MagickStatusType
800     status;
801
802   /*
803     Load the coder map file.
804   */
805   (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
806     "Loading coder configuration file \"%s\" ...",filename);
807   if (xml == (const char *) NULL)
808     return(MagickFalse);
809   status=MagickTrue;
810   coder_info=(CoderInfo *) NULL;
811   token=AcquireString(xml);
812   for (q=(char *) xml; *q != '\0'; )
813   {
814     /*
815       Interpret XML.
816     */
817     GetMagickToken(q,&q,token);
818     if (*token == '\0')
819       break;
820     (void) CopyMagickString(keyword,token,MaxTextExtent);
821     if (LocaleNCompare(keyword,"<!DOCTYPE",9) == 0)
822       {
823         /*
824           Doctype element.
825         */
826         while ((LocaleNCompare(q,"]>",2) != 0) && (*q != '\0'))
827           GetMagickToken(q,&q,token);
828         continue;
829       }
830     if (LocaleNCompare(keyword,"<!--",4) == 0)
831       {
832         /*
833           Comment element.
834         */
835         while ((LocaleNCompare(q,"->",2) != 0) && (*q != '\0'))
836           GetMagickToken(q,&q,token);
837         continue;
838       }
839     if (LocaleCompare(keyword,"<include") == 0)
840       {
841         /*
842           Include element.
843         */
844         while (((*token != '/') && (*(token+1) != '>')) && (*q != '\0'))
845         {
846           (void) CopyMagickString(keyword,token,MaxTextExtent);
847           GetMagickToken(q,&q,token);
848           if (*token != '=')
849             continue;
850           GetMagickToken(q,&q,token);
851           if (LocaleCompare(keyword,"file") == 0)
852             {
853               if (depth > 200)
854                 (void) ThrowMagickException(exception,GetMagickModule(),
855                   ConfigureError,"IncludeNodeNestedTooDeeply","`%s'",token);
856               else
857                 {
858                   char
859                     path[MaxTextExtent],
860                     *xml;
861
862                   GetPathComponent(filename,HeadPath,path);
863                   if (*path != '\0')
864                     (void) ConcatenateMagickString(path,DirectorySeparator,
865                       MaxTextExtent);
866                   if (*token == *DirectorySeparator)
867                     (void) CopyMagickString(path,token,MaxTextExtent);
868                   else
869                     (void) ConcatenateMagickString(path,token,MaxTextExtent);
870                   xml=FileToXML(path,~0UL);
871                   if (xml != (char *) NULL)
872                     {
873                       status&=LoadCoderCache(coder_cache,xml,path,depth+1,
874                         exception);
875                       xml=(char *) RelinquishMagickMemory(xml);
876                     }
877                 }
878             }
879         }
880         continue;
881       }
882     if (LocaleCompare(keyword,"<coder") == 0)
883       {
884         /*
885           Coder element.
886         */
887         coder_info=(CoderInfo *) AcquireMagickMemory(sizeof(*coder_info));
888         if (coder_info == (CoderInfo *) NULL)
889           ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
890         (void) ResetMagickMemory(coder_info,0,sizeof(*coder_info));
891         coder_info->path=ConstantString(filename);
892         coder_info->exempt=MagickFalse;
893         coder_info->signature=MagickSignature;
894         continue;
895       }
896     if (coder_info == (CoderInfo *) NULL)
897       continue;
898     if (LocaleCompare(keyword,"/>") == 0)
899       {
900         status=AddValueToSplayTree(coder_cache,ConstantString(
901           coder_info->magick),coder_info);
902         if (status == MagickFalse)
903           (void) ThrowMagickException(exception,GetMagickModule(),
904             ResourceLimitError,"MemoryAllocationFailed","`%s'",
905             coder_info->magick);
906         coder_info=(CoderInfo *) NULL;
907         continue;
908       }
909     GetMagickToken(q,(const char **) NULL,token);
910     if (*token != '=')
911       continue;
912     GetMagickToken(q,&q,token);
913     GetMagickToken(q,&q,token);
914     switch (*keyword)
915     {
916       case 'M':
917       case 'm':
918       {
919         if (LocaleCompare((char *) keyword,"magick") == 0)
920           {
921             coder_info->magick=ConstantString(token);
922             break;
923           }
924         break;
925       }
926       case 'N':
927       case 'n':
928       {
929         if (LocaleCompare((char *) keyword,"name") == 0)
930           {
931             coder_info->name=ConstantString(token);
932             break;
933           }
934         break;
935       }
936       case 'S':
937       case 's':
938       {
939         if (LocaleCompare((char *) keyword,"stealth") == 0)
940           {
941             coder_info->stealth=IsStringTrue(token);
942             break;
943           }
944         break;
945       }
946       default:
947         break;
948     }
949   }
950   token=(char *) RelinquishMagickMemory(token);
951   return(status != 0 ? MagickTrue : MagickFalse);
952 }