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