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