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