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