blob: 8fb497d3bd5e980df0de0a38c879d12aaa56ac7f [file] [log] [blame]
cristy3ed852e2009-09-05 21:47:34 +00001/*
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% %
cristy7e41fe82010-12-04 23:12:08 +000020% Copyright 1999-2011 ImageMagick Studio LLC, a non-profit organization %
cristy3ed852e2009-09-05 21:47:34 +000021% 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
39/*
40 Include declarations.
41*/
cristy4c08aed2011-07-01 19:47:50 +000042#include "MagickCore/studio.h"
43#include "MagickCore/blob.h"
44#include "MagickCore/client.h"
45#include "MagickCore/coder.h"
cristy5ff4eaf2011-09-03 01:38:02 +000046#include "MagickCore/coder-private.h"
cristy4c08aed2011-07-01 19:47:50 +000047#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/xml-tree.h"
cristy3ed852e2009-09-05 21:47:34 +000061
62/*
63 Define declarations.
64*/
65#define MagickCoderFilename "coder.xml"
66
67/*
cristy54a531d2009-10-21 17:58:01 +000068 Typedef declarations.
cristy3ed852e2009-09-05 21:47:34 +000069*/
cristye3e77a12009-10-16 00:47:21 +000070typedef struct _CoderMapInfo
71{
72 const char
73 *magick,
74 *name;
75} CoderMapInfo;
cristy54a531d2009-10-21 17:58:01 +000076
77/*
78 Static declarations.
79*/
cristye3e77a12009-10-16 00:47:21 +000080static const CoderMapInfo
81 CoderMap[] =
82 {
83 { "3FR", "DNG" },
84 { "8BIM", "META" },
85 { "8BIMTEXT", "META" },
86 { "8BIMWTEXT", "META" },
87 { "AFM", "TTF" },
88 { "A", "RAW" },
89 { "AI", "PDF" },
90 { "APP1JPEG", "META" },
91 { "APP1", "META" },
92 { "ARW", "DNG" },
cristy0fc02592010-05-24 14:12:38 +000093 { "AVI", "MPEG" },
cristye3e77a12009-10-16 00:47:21 +000094 { "BIE", "JBIG" },
95 { "BMP2", "BMP" },
96 { "BMP3", "BMP" },
97 { "B", "RAW" },
98 { "BRF", "BRAILLE" },
cristy90dbac72010-08-22 15:08:40 +000099 { "BGRA", "BGR" },
cristye3e77a12009-10-16 00:47:21 +0000100 { "CMYKA", "CMYK" },
101 { "C", "RAW" },
102 { "CAL", "CALS" },
cristyc05417c2010-09-21 16:30:32 +0000103 { "CANVAS", "XC" },
cristye3e77a12009-10-16 00:47:21 +0000104 { "CR2", "DNG" },
105 { "CRW", "DNG" },
106 { "CUR", "ICON" },
107 { "DCR", "DNG" },
108 { "DCX", "PCX" },
109 { "DFONT", "TTF" },
110 { "EPDF", "PDF" },
111 { "EPI", "PS" },
112 { "EPS2", "PS2" },
113 { "EPS3", "PS3" },
114 { "EPSF", "PS" },
115 { "EPSI", "PS" },
116 { "EPS", "PS" },
117 { "EPT2", "EPT" },
118 { "EPT3", "EPT" },
119 { "ERF", "DNG" },
120 { "EXIF", "META" },
121 { "FILE", "URL" },
122 { "FRACTAL", "PLASMA" },
123 { "FTP", "URL" },
124 { "FTS", "FITS" },
125 { "G3", "FAX" },
126 { "GIF87", "GIF" },
127 { "G", "RAW" },
cristye3e77a12009-10-16 00:47:21 +0000128 { "GRANITE", "MAGICK" },
129 { "GROUP4", "TIFF" },
130 { "K25", "DNG" },
131 { "KDC", "DNG" },
132 { "H", "MAGICK" },
133 { "HTM", "HTML" },
134 { "HTTP", "URL" },
135 { "ICB", "TGA" },
136 { "ICC", "META" },
137 { "ICM", "META" },
138 { "ICO", "ICON" },
139 { "IMPLICIT", "***" },
140 { "IPTC", "META" },
141 { "IPTCTEXT", "META" },
142 { "IPTCWTEXT", "META" },
143 { "ISOBRL", "BRAILLE" },
144 { "JBG", "JBIG" },
145 { "JNG", "PNG" },
146 { "JPC", "JP2" },
cristy02f1fda2009-12-10 15:23:56 +0000147 { "J2C", "JP2" },
cristye3e77a12009-10-16 00:47:21 +0000148 { "JPG", "JPEG" },
149 { "JPX", "JP2" },
150 { "K", "RAW" },
151 { "LOGO", "MAGICK" },
152 { "M2V", "MPEG" },
153 { "M4V", "MPEG" },
154 { "M", "RAW" },
155 { "MNG", "PNG" },
cristy1dbd5a32010-11-28 22:23:27 +0000156 { "MOV", "MPEG" },
cristye3e77a12009-10-16 00:47:21 +0000157 { "MP4", "MPEG" },
158 { "MPG", "MPEG" },
159 { "MPRI", "MPR" },
160 { "MRW", "DNG" },
161 { "MSVG", "SVG" },
162 { "NEF", "DNG" },
163 { "NETSCAPE", "MAGICK" },
164 { "O", "RAW" },
165 { "ORF", "DNG" },
166 { "OTF", "TTF" },
167 { "P7", "PNM" },
168 { "PAL", "UYVY" },
169 { "PAM", "PNM" },
170 { "PBM", "PNM" },
171 { "PCDS", "PCD" },
172 { "PDFA", "PDF" },
173 { "PEF", "DNG" },
174 { "PEF", "DNG" },
175 { "PFA", "TTF" },
176 { "PFB", "TTF" },
177 { "PFM", "PNM" },
178 { "PGM", "PNM" },
179 { "PGX", "JP2" },
180 { "PICON", "XPM" },
181 { "PJPEG", "JPEG" },
182 { "PM", "XPM" },
183 { "PNG24", "PNG" },
184 { "PNG32", "PNG" },
185 { "PNG8", "PNG" },
186 { "PPM", "PNM" },
cristyb4233012010-02-28 20:09:14 +0000187 { "PSB", "PSD" },
cristye3e77a12009-10-16 00:47:21 +0000188 { "PTIF", "TIFF" },
189 { "RADIAL-GRADIENT", "GRADIENT" },
190 { "RAF", "DNG" },
191 { "RAS", "SUN" },
cristye3e77a12009-10-16 00:47:21 +0000192 { "RGBA", "RGB" },
193 { "RGBO", "RGB" },
194 { "R", "RAW" },
195 { "ROSE", "MAGICK" },
196 { "SHTML", "HTML" },
197 { "SR2", "DNG" },
198 { "SRF", "DNG" },
199 { "SVGZ", "SVG" },
200 { "TEXT", "TXT" },
201 { "TIFF64", "TIFF" },
202 { "TIF", "TIFF" },
203 { "TTC", "TTF" },
204 { "UBRL", "BRAILLE" },
205 { "VDA", "TGA" },
206 { "VST", "TGA" },
cristyed40d622011-01-17 15:51:00 +0000207 { "WIZARD", "MAGICK" },
cristye3e77a12009-10-16 00:47:21 +0000208 { "WMV", "MPEG" },
209 { "WMFWIN32", "EMF" },
210 { "WMZ", "WMF" },
211 { "X3f", "DNG" },
212 { "XMP", "META" },
213 { "XTRNARRAY", "XTRN" },
214 { "XTRNBLOB", "XTRN" },
cristye3e77a12009-10-16 00:47:21 +0000215 { "XTRNFILE", "XTRN" },
216 { "XTRNIMAGE", "XTRN" },
cristye3e77a12009-10-16 00:47:21 +0000217 { "XV", "VIFF" },
218 { "Y", "RAW" },
cristy54a531d2009-10-21 17:58:01 +0000219 { "YCbCrA", "YCbCr" }
cristye3e77a12009-10-16 00:47:21 +0000220 };
221
cristy3ed852e2009-09-05 21:47:34 +0000222static SemaphoreInfo
223 *coder_semaphore = (SemaphoreInfo *) NULL;
224
225static SplayTreeInfo
226 *coder_list = (SplayTreeInfo *) NULL;
227
228static volatile MagickBooleanType
229 instantiate_coder = MagickFalse;
230
231/*
232 Forward declarations.
233*/
234static MagickBooleanType
235 InitializeCoderList(ExceptionInfo *),
236 LoadCoderLists(const char *,ExceptionInfo *);
237
238/*
239%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
240% %
241% %
242% %
cristyf34a1452009-10-24 22:29:27 +0000243+ C o d e r C o m p o n e n t G e n e s i s %
cristy3ed852e2009-09-05 21:47:34 +0000244% %
245% %
246% %
247%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
248%
cristyf34a1452009-10-24 22:29:27 +0000249% CoderComponentGenesis() instantiates the coder component.
cristy3ed852e2009-09-05 21:47:34 +0000250%
cristyf34a1452009-10-24 22:29:27 +0000251% The format of the CoderComponentGenesis method is:
cristy3ed852e2009-09-05 21:47:34 +0000252%
cristyf34a1452009-10-24 22:29:27 +0000253% MagickBooleanType CoderComponentGenesis(void)
cristy3ed852e2009-09-05 21:47:34 +0000254%
255*/
cristy5ff4eaf2011-09-03 01:38:02 +0000256MagickPrivate MagickBooleanType CoderComponentGenesis(void)
cristyf34a1452009-10-24 22:29:27 +0000257{
cristy165b6092009-10-26 13:52:10 +0000258 AcquireSemaphoreInfo(&coder_semaphore);
cristyf34a1452009-10-24 22:29:27 +0000259 return(MagickTrue);
260}
261
262/*
263%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
264% %
265% %
266% %
267+ C o d e r C o m p o n e n t T e r m i n u s %
268% %
269% %
270% %
271%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
272%
273% CoderComponentTerminus() destroys the coder component.
274%
275% The format of the CoderComponentTerminus method is:
276%
277% CoderComponentTerminus(void)
278%
279*/
cristy5ff4eaf2011-09-03 01:38:02 +0000280MagickPrivate void CoderComponentTerminus(void)
cristy3ed852e2009-09-05 21:47:34 +0000281{
cristy18b17442009-10-25 18:36:48 +0000282 if (coder_semaphore == (SemaphoreInfo *) NULL)
283 AcquireSemaphoreInfo(&coder_semaphore);
cristyf84a1932010-01-03 18:00:18 +0000284 LockSemaphoreInfo(coder_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000285 if (coder_list != (SplayTreeInfo *) NULL)
286 coder_list=DestroySplayTree(coder_list);
287 instantiate_coder=MagickFalse;
cristyf84a1932010-01-03 18:00:18 +0000288 UnlockSemaphoreInfo(coder_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000289 DestroySemaphoreInfo(&coder_semaphore);
290}
291
292/*
293%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
294% %
295% %
296% %
297+ G e t C o d e r I n f o %
298% %
299% %
300% %
301%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
302%
303% GetCoderInfo searches the coder list for the specified name and if found
304% returns attributes for that coder.
305%
306% The format of the GetCoderInfo method is:
307%
308% const CoderInfo *GetCoderInfo(const char *name,ExceptionInfo *exception)
309%
310% A description of each parameter follows:
311%
312% o name: the coder name.
313%
314% o exception: return any errors or warnings in this structure.
315%
316*/
317MagickExport const CoderInfo *GetCoderInfo(const char *name,
318 ExceptionInfo *exception)
319{
320 assert(exception != (ExceptionInfo *) NULL);
321 if ((coder_list == (SplayTreeInfo *) NULL) ||
322 (instantiate_coder == MagickFalse))
323 if (InitializeCoderList(exception) == MagickFalse)
324 return((const CoderInfo *) NULL);
325 if ((coder_list == (SplayTreeInfo *) NULL) ||
326 (GetNumberOfNodesInSplayTree(coder_list) == 0))
327 return((const CoderInfo *) NULL);
328 if ((name == (const char *) NULL) || (LocaleCompare(name,"*") == 0))
329 {
330 ResetSplayTreeIterator(coder_list);
331 return((const CoderInfo *) GetNextValueInSplayTree(coder_list));
332 }
333 return((const CoderInfo *) GetValueFromSplayTree(coder_list,name));
334}
335
336/*
337%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
338% %
339% %
340% %
341% G e t C o d e r I n f o L i s t %
342% %
343% %
344% %
345%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
346%
347% GetCoderInfoList() returns any coder_map that match the specified pattern.
348% The format of the GetCoderInfoList function is:
349%
350% const CoderInfo **GetCoderInfoList(const char *pattern,
cristybb503372010-05-27 20:51:26 +0000351% size_t *number_coders,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000352%
353% A description of each parameter follows:
354%
355% o pattern: Specifies a pointer to a text string containing a pattern.
356%
357% o number_coders: This integer returns the number of coders in the list.
358%
359% o exception: return any errors or warnings in this structure.
360%
361*/
362
363static int CoderInfoCompare(const void *x,const void *y)
364{
365 const CoderInfo
366 **p,
367 **q;
368
369 p=(const CoderInfo **) x,
370 q=(const CoderInfo **) y;
371 if (LocaleCompare((*p)->path,(*q)->path) == 0)
372 return(LocaleCompare((*p)->name,(*q)->name));
373 return(LocaleCompare((*p)->path,(*q)->path));
374}
375
376MagickExport const CoderInfo **GetCoderInfoList(const char *pattern,
cristybb503372010-05-27 20:51:26 +0000377 size_t *number_coders,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000378{
379 const CoderInfo
380 **coder_map;
381
382 register const CoderInfo
383 *p;
384
cristybb503372010-05-27 20:51:26 +0000385 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000386 i;
387
388 /*
389 Allocate coder list.
390 */
391 assert(pattern != (char *) NULL);
392 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
cristybb503372010-05-27 20:51:26 +0000393 assert(number_coders != (size_t *) NULL);
cristy3ed852e2009-09-05 21:47:34 +0000394 *number_coders=0;
395 p=GetCoderInfo("*",exception);
396 if (p == (const CoderInfo *) NULL)
397 return((const CoderInfo **) NULL);
398 coder_map=(const CoderInfo **) AcquireQuantumMemory((size_t)
399 GetNumberOfNodesInSplayTree(coder_list)+1UL,sizeof(*coder_map));
400 if (coder_map == (const CoderInfo **) NULL)
401 return((const CoderInfo **) NULL);
402 /*
403 Generate coder list.
404 */
cristyf84a1932010-01-03 18:00:18 +0000405 LockSemaphoreInfo(coder_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000406 ResetSplayTreeIterator(coder_list);
407 p=(const CoderInfo *) GetNextValueInSplayTree(coder_list);
408 for (i=0; p != (const CoderInfo *) NULL; )
409 {
410 if ((p->stealth == MagickFalse) &&
411 (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
412 coder_map[i++]=p;
413 p=(const CoderInfo *) GetNextValueInSplayTree(coder_list);
414 }
cristyf84a1932010-01-03 18:00:18 +0000415 UnlockSemaphoreInfo(coder_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000416 qsort((void *) coder_map,(size_t) i,sizeof(*coder_map),CoderInfoCompare);
417 coder_map[i]=(CoderInfo *) NULL;
cristybb503372010-05-27 20:51:26 +0000418 *number_coders=(size_t) i;
cristy3ed852e2009-09-05 21:47:34 +0000419 return(coder_map);
420}
421
422/*
423%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
424% %
425% %
426% %
427% G e t C o d e r L i s t %
428% %
429% %
430% %
431%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
432%
433% GetCoderList() returns any coder_map that match the specified pattern.
434%
435% The format of the GetCoderList function is:
436%
cristybb503372010-05-27 20:51:26 +0000437% char **GetCoderList(const char *pattern,size_t *number_coders,
cristy3ed852e2009-09-05 21:47:34 +0000438% ExceptionInfo *exception)
439%
440% A description of each parameter follows:
441%
442% o pattern: Specifies a pointer to a text string containing a pattern.
443%
444% o number_coders: This integer returns the number of coders in the list.
445%
446% o exception: return any errors or warnings in this structure.
447%
448*/
449
450static int CoderCompare(const void *x,const void *y)
451{
452 register const char
453 **p,
454 **q;
455
456 p=(const char **) x;
457 q=(const char **) y;
458 return(LocaleCompare(*p,*q));
459}
460
461MagickExport char **GetCoderList(const char *pattern,
cristybb503372010-05-27 20:51:26 +0000462 size_t *number_coders,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000463{
464 char
465 **coder_map;
466
467 register const CoderInfo
468 *p;
469
cristybb503372010-05-27 20:51:26 +0000470 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000471 i;
472
473 /*
474 Allocate coder list.
475 */
476 assert(pattern != (char *) NULL);
477 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
cristybb503372010-05-27 20:51:26 +0000478 assert(number_coders != (size_t *) NULL);
cristy3ed852e2009-09-05 21:47:34 +0000479 *number_coders=0;
480 p=GetCoderInfo("*",exception);
481 if (p == (const CoderInfo *) NULL)
482 return((char **) NULL);
483 coder_map=(char **) AcquireQuantumMemory((size_t)
484 GetNumberOfNodesInSplayTree(coder_list)+1UL,sizeof(*coder_map));
485 if (coder_map == (char **) NULL)
486 return((char **) NULL);
487 /*
488 Generate coder list.
489 */
cristyf84a1932010-01-03 18:00:18 +0000490 LockSemaphoreInfo(coder_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000491 ResetSplayTreeIterator(coder_list);
492 p=(const CoderInfo *) GetNextValueInSplayTree(coder_list);
493 for (i=0; p != (const CoderInfo *) NULL; )
494 {
495 if ((p->stealth == MagickFalse) &&
496 (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
497 coder_map[i++]=ConstantString(p->name);
498 p=(const CoderInfo *) GetNextValueInSplayTree(coder_list);
499 }
cristyf84a1932010-01-03 18:00:18 +0000500 UnlockSemaphoreInfo(coder_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000501 qsort((void *) coder_map,(size_t) i,sizeof(*coder_map),CoderCompare);
502 coder_map[i]=(char *) NULL;
cristybb503372010-05-27 20:51:26 +0000503 *number_coders=(size_t) i;
cristy3ed852e2009-09-05 21:47:34 +0000504 return(coder_map);
505}
506
507/*
508%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
509% %
510% %
511% %
512+ I n i t i a l i z e C o d e r L i s t %
513% %
514% %
515% %
516%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
517%
518% InitializeCoderList() initializes the coder list.
519%
520% The format of the InitializeCoderList method is:
521%
522% MagickBooleanType InitializeCoderList(ExceptionInfo *exception)
523%
524% A description of each parameter follows.
525%
526% o exception: return any errors or warnings in this structure.
527%
528*/
529static MagickBooleanType InitializeCoderList(ExceptionInfo *exception)
530{
531 if ((coder_list == (SplayTreeInfo *) NULL) &&
532 (instantiate_coder == MagickFalse))
533 {
cristy4e1dff62009-10-25 20:36:03 +0000534 if (coder_semaphore == (SemaphoreInfo *) NULL)
535 AcquireSemaphoreInfo(&coder_semaphore);
cristyf84a1932010-01-03 18:00:18 +0000536 LockSemaphoreInfo(coder_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000537 if ((coder_list == (SplayTreeInfo *) NULL) &&
538 (instantiate_coder == MagickFalse))
539 {
540 (void) LoadCoderLists(MagickCoderFilename,exception);
541 instantiate_coder=MagickTrue;
542 }
cristyf84a1932010-01-03 18:00:18 +0000543 UnlockSemaphoreInfo(coder_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000544 }
545 return(coder_list != (SplayTreeInfo *) NULL ? MagickTrue : MagickFalse);
546}
547
548/*
549%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
550% %
551% %
552% %
553% L i s t C o d e r I n f o %
554% %
555% %
556% %
557%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
558%
559% ListCoderInfo() lists the coder info to a file.
560%
561% The format of the ListCoderInfo coder is:
562%
563% MagickBooleanType ListCoderInfo(FILE *file,ExceptionInfo *exception)
564%
565% A description of each parameter follows.
566%
567% o file: An pointer to a FILE.
568%
569% o exception: return any errors or warnings in this structure.
570%
571*/
572MagickExport MagickBooleanType ListCoderInfo(FILE *file,
573 ExceptionInfo *exception)
574{
575 const char
576 *path;
577
578 const CoderInfo
579 **coder_info;
580
cristybb503372010-05-27 20:51:26 +0000581 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000582 i;
583
cristybb503372010-05-27 20:51:26 +0000584 size_t
cristy3ed852e2009-09-05 21:47:34 +0000585 number_coders;
586
cristy9d314ff2011-03-09 01:30:28 +0000587 ssize_t
588 j;
589
cristy3ed852e2009-09-05 21:47:34 +0000590 if (file == (const FILE *) NULL)
591 file=stdout;
592 coder_info=GetCoderInfoList("*",&number_coders,exception);
593 if (coder_info == (const CoderInfo **) NULL)
594 return(MagickFalse);
595 path=(const char *) NULL;
cristybb503372010-05-27 20:51:26 +0000596 for (i=0; i < (ssize_t) number_coders; i++)
cristy3ed852e2009-09-05 21:47:34 +0000597 {
598 if (coder_info[i]->stealth != MagickFalse)
599 continue;
600 if ((path == (const char *) NULL) ||
601 (LocaleCompare(path,coder_info[i]->path) != 0))
602 {
603 if (coder_info[i]->path != (char *) NULL)
cristyb51dff52011-05-19 16:55:47 +0000604 (void) FormatLocaleFile(file,"\nPath: %s\n\n",coder_info[i]->path);
605 (void) FormatLocaleFile(file,"Magick Coder\n");
cristy1e604812011-05-19 18:07:50 +0000606 (void) FormatLocaleFile(file,
607 "-------------------------------------------------"
cristy3ed852e2009-09-05 21:47:34 +0000608 "------------------------------\n");
609 }
610 path=coder_info[i]->path;
cristyb51dff52011-05-19 16:55:47 +0000611 (void) FormatLocaleFile(file,"%s",coder_info[i]->magick);
cristybb503372010-05-27 20:51:26 +0000612 for (j=(ssize_t) strlen(coder_info[i]->magick); j <= 11; j++)
cristyb51dff52011-05-19 16:55:47 +0000613 (void) FormatLocaleFile(file," ");
cristy3ed852e2009-09-05 21:47:34 +0000614 if (coder_info[i]->name != (char *) NULL)
cristyb51dff52011-05-19 16:55:47 +0000615 (void) FormatLocaleFile(file,"%s",coder_info[i]->name);
616 (void) FormatLocaleFile(file,"\n");
cristy3ed852e2009-09-05 21:47:34 +0000617 }
618 coder_info=(const CoderInfo **) RelinquishMagickMemory((void *) coder_info);
619 (void) fflush(file);
620 return(MagickTrue);
621}
622
623/*
624%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
625% %
626% %
627% %
628+ L o a d C o d e r L i s t %
629% %
630% %
631% %
632%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
633%
634% LoadCoderList() loads the coder configuration file which provides a
635% mapping between coder attributes and a coder name.
636%
637% The format of the LoadCoderList coder is:
638%
639% MagickBooleanType LoadCoderList(const char *xml,const char *filename,
cristybb503372010-05-27 20:51:26 +0000640% const size_t depth,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000641%
642% A description of each parameter follows:
643%
644% o xml: The coder list in XML format.
645%
646% o filename: The coder list filename.
647%
648% o depth: depth of <include /> statements.
649%
650% o exception: return any errors or warnings in this structure.
651%
652*/
653
654static void *DestroyCoderNode(void *coder_info)
655{
656 register CoderInfo
657 *p;
658
659 p=(CoderInfo *) coder_info;
cristye3e77a12009-10-16 00:47:21 +0000660 if (p->exempt == MagickFalse)
661 {
662 if (p->path != (char *) NULL)
663 p->path=DestroyString(p->path);
664 if (p->name != (char *) NULL)
665 p->name=DestroyString(p->name);
666 if (p->magick != (char *) NULL)
667 p->magick=DestroyString(p->magick);
668 }
cristy3ed852e2009-09-05 21:47:34 +0000669 return(RelinquishMagickMemory(p));
670}
671
672static MagickBooleanType LoadCoderList(const char *xml,const char *filename,
cristybb503372010-05-27 20:51:26 +0000673 const size_t depth,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000674{
675 char
676 keyword[MaxTextExtent],
677 *token;
678
679 const char
680 *q;
681
682 CoderInfo
683 *coder_info;
684
685 MagickBooleanType
686 status;
687
688 /*
689 Load the coder map file.
690 */
691 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
692 "Loading coder configuration file \"%s\" ...",filename);
693 if (xml == (const char *) NULL)
694 return(MagickFalse);
695 if (coder_list == (SplayTreeInfo *) NULL)
696 {
697 coder_list=NewSplayTree(CompareSplayTreeString,RelinquishMagickMemory,
698 DestroyCoderNode);
699 if (coder_list == (SplayTreeInfo *) NULL)
700 {
701 ThrowFileException(exception,ResourceLimitError,
702 "MemoryAllocationFailed",filename);
703 return(MagickFalse);
704 }
705 }
706 status=MagickTrue;
707 coder_info=(CoderInfo *) NULL;
708 token=AcquireString(xml);
709 for (q=(char *) xml; *q != '\0'; )
710 {
711 /*
712 Interpret XML.
713 */
714 GetMagickToken(q,&q,token);
715 if (*token == '\0')
716 break;
717 (void) CopyMagickString(keyword,token,MaxTextExtent);
718 if (LocaleNCompare(keyword,"<!DOCTYPE",9) == 0)
719 {
720 /*
721 Doctype element.
722 */
723 while ((LocaleNCompare(q,"]>",2) != 0) && (*q != '\0'))
724 GetMagickToken(q,&q,token);
725 continue;
726 }
727 if (LocaleNCompare(keyword,"<!--",4) == 0)
728 {
729 /*
730 Comment element.
731 */
732 while ((LocaleNCompare(q,"->",2) != 0) && (*q != '\0'))
733 GetMagickToken(q,&q,token);
734 continue;
735 }
736 if (LocaleCompare(keyword,"<include") == 0)
737 {
738 /*
739 Include element.
740 */
741 while (((*token != '/') && (*(token+1) != '>')) && (*q != '\0'))
742 {
743 (void) CopyMagickString(keyword,token,MaxTextExtent);
744 GetMagickToken(q,&q,token);
745 if (*token != '=')
746 continue;
747 GetMagickToken(q,&q,token);
748 if (LocaleCompare(keyword,"file") == 0)
749 {
750 if (depth > 200)
751 (void) ThrowMagickException(exception,GetMagickModule(),
752 ConfigureError,"IncludeNodeNestedTooDeeply","`%s'",token);
753 else
754 {
755 char
756 path[MaxTextExtent],
757 *xml;
758
759 GetPathComponent(filename,HeadPath,path);
760 if (*path != '\0')
761 (void) ConcatenateMagickString(path,DirectorySeparator,
762 MaxTextExtent);
763 if (*token == *DirectorySeparator)
764 (void) CopyMagickString(path,token,MaxTextExtent);
765 else
766 (void) ConcatenateMagickString(path,token,MaxTextExtent);
767 xml=FileToString(path,~0,exception);
768 if (xml != (char *) NULL)
769 {
770 status=LoadCoderList(xml,path,depth+1,exception);
771 xml=(char *) RelinquishMagickMemory(xml);
772 }
773 }
774 }
775 }
776 continue;
777 }
778 if (LocaleCompare(keyword,"<coder") == 0)
779 {
780 /*
781 Coder element.
782 */
cristy73bd4a52010-10-05 11:24:23 +0000783 coder_info=(CoderInfo *) AcquireMagickMemory(sizeof(*coder_info));
cristy3ed852e2009-09-05 21:47:34 +0000784 if (coder_info == (CoderInfo *) NULL)
785 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
786 (void) ResetMagickMemory(coder_info,0,sizeof(*coder_info));
787 coder_info->path=ConstantString(filename);
cristye3e77a12009-10-16 00:47:21 +0000788 coder_info->exempt=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +0000789 coder_info->signature=MagickSignature;
790 continue;
791 }
792 if (coder_info == (CoderInfo *) NULL)
793 continue;
794 if (LocaleCompare(keyword,"/>") == 0)
795 {
796 status=AddValueToSplayTree(coder_list,ConstantString(
797 coder_info->magick),coder_info);
798 if (status == MagickFalse)
799 (void) ThrowMagickException(exception,GetMagickModule(),
800 ResourceLimitError,"MemoryAllocationFailed","`%s'",
801 coder_info->magick);
802 coder_info=(CoderInfo *) NULL;
803 }
804 GetMagickToken(q,(const char **) NULL,token);
805 if (*token != '=')
806 continue;
807 GetMagickToken(q,&q,token);
808 GetMagickToken(q,&q,token);
809 switch (*keyword)
810 {
811 case 'M':
812 case 'm':
813 {
814 if (LocaleCompare((char *) keyword,"magick") == 0)
815 {
816 coder_info->magick=ConstantString(token);
817 break;
818 }
819 break;
820 }
821 case 'N':
822 case 'n':
823 {
824 if (LocaleCompare((char *) keyword,"name") == 0)
825 {
826 coder_info->name=ConstantString(token);
827 break;
828 }
829 break;
830 }
831 case 'S':
832 case 's':
833 {
834 if (LocaleCompare((char *) keyword,"stealth") == 0)
835 {
836 coder_info->stealth=IsMagickTrue(token);
837 break;
838 }
839 break;
840 }
841 default:
842 break;
843 }
844 }
845 token=(char *) RelinquishMagickMemory(token);
846 return(status);
847}
848
849/*
850%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
851% %
852% %
853% %
854% L o a d C o d e r L i s t s %
855% %
856% %
857% %
858%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
859%
860% LoadCoderLists() loads one or more coder configuration file which
861% provides a mapping between coder attributes and a coder name.
862%
863% The format of the LoadCoderLists coder is:
864%
865% MagickBooleanType LoadCoderLists(const char *filename,
866% ExceptionInfo *exception)
867%
868% A description of each parameter follows:
869%
870% o filename: the font file name.
871%
872% o exception: return any errors or warnings in this structure.
873%
874*/
875static MagickBooleanType LoadCoderLists(const char *filename,
876 ExceptionInfo *exception)
877{
cristy3ed852e2009-09-05 21:47:34 +0000878 const StringInfo
879 *option;
880
881 LinkedListInfo
882 *options;
883
884 MagickStatusType
885 status;
886
cristybb503372010-05-27 20:51:26 +0000887 register ssize_t
cristy54a531d2009-10-21 17:58:01 +0000888 i;
cristye3e77a12009-10-16 00:47:21 +0000889
890 /*
891 Load built-in coder map.
892 */
cristy3ed852e2009-09-05 21:47:34 +0000893 status=MagickFalse;
cristye3e77a12009-10-16 00:47:21 +0000894 if (coder_list == (SplayTreeInfo *) NULL)
895 {
896 coder_list=NewSplayTree(CompareSplayTreeString,RelinquishMagickMemory,
897 DestroyCoderNode);
898 if (coder_list == (SplayTreeInfo *) NULL)
899 {
900 ThrowFileException(exception,ResourceLimitError,
901 "MemoryAllocationFailed",filename);
902 return(MagickFalse);
903 }
904 }
cristybb503372010-05-27 20:51:26 +0000905 for (i=0; i < (ssize_t) (sizeof(CoderMap)/sizeof(*CoderMap)); i++)
cristye3e77a12009-10-16 00:47:21 +0000906 {
907 CoderInfo
908 *coder_info;
909
cristy54a531d2009-10-21 17:58:01 +0000910 register const CoderMapInfo
911 *p;
912
913 p=CoderMap+i;
cristy73bd4a52010-10-05 11:24:23 +0000914 coder_info=(CoderInfo *) AcquireMagickMemory(sizeof(*coder_info));
cristye3e77a12009-10-16 00:47:21 +0000915 if (coder_info == (CoderInfo *) NULL)
916 {
917 (void) ThrowMagickException(exception,GetMagickModule(),
918 ResourceLimitError,"MemoryAllocationFailed","`%s'",coder_info->name);
919 continue;
920 }
921 (void) ResetMagickMemory(coder_info,0,sizeof(*coder_info));
cristy38b8ed92009-10-16 01:20:21 +0000922 coder_info->path=(char *) "[built-in]";
cristye3e77a12009-10-16 00:47:21 +0000923 coder_info->magick=(char *) p->magick;
924 coder_info->name=(char *) p->name;
925 coder_info->exempt=MagickTrue;
926 coder_info->signature=MagickSignature;
927 status=AddValueToSplayTree(coder_list,ConstantString(coder_info->magick),
928 coder_info);
929 if (status == MagickFalse)
930 (void) ThrowMagickException(exception,GetMagickModule(),
931 ResourceLimitError,"MemoryAllocationFailed","`%s'",coder_info->name);
932 }
933 /*
934 Load external coder map.
935 */
cristy3ed852e2009-09-05 21:47:34 +0000936 options=GetConfigureOptions(filename,exception);
937 option=(const StringInfo *) GetNextValueInLinkedList(options);
938 while (option != (const StringInfo *) NULL)
939 {
940 status|=LoadCoderList((const char *) GetStringInfoDatum(option),
941 GetStringInfoPath(option),0,exception);
942 option=(const StringInfo *) GetNextValueInLinkedList(options);
943 }
944 options=DestroyConfigureOptions(options);
cristy3ed852e2009-09-05 21:47:34 +0000945 return(status != 0 ? MagickTrue : MagickFalse);
cristy3ed852e2009-09-05 21:47:34 +0000946}