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