blob: 785111155c88858c1b5add6336629aefeb4f01ea [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% %
cristy1454be72011-12-19 01:52:48 +000020% Copyright 1999-2012 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"
cristyd1dd6e42011-09-04 01:46:08 +000060#include "MagickCore/utility-private.h"
cristy4c08aed2011-07-01 19:47:50 +000061#include "MagickCore/xml-tree.h"
cristy3ed852e2009-09-05 21:47:34 +000062
63/*
64 Define declarations.
65*/
66#define MagickCoderFilename "coder.xml"
67
68/*
cristy54a531d2009-10-21 17:58:01 +000069 Typedef declarations.
cristy3ed852e2009-09-05 21:47:34 +000070*/
cristye3e77a12009-10-16 00:47:21 +000071typedef struct _CoderMapInfo
72{
73 const char
74 *magick,
75 *name;
76} CoderMapInfo;
cristy54a531d2009-10-21 17:58:01 +000077
78/*
79 Static declarations.
80*/
cristye3e77a12009-10-16 00:47:21 +000081static 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" },
cristy0fc02592010-05-24 14:12:38 +000094 { "AVI", "MPEG" },
cristye3e77a12009-10-16 00:47:21 +000095 { "BIE", "JBIG" },
96 { "BMP2", "BMP" },
97 { "BMP3", "BMP" },
98 { "B", "RAW" },
99 { "BRF", "BRAILLE" },
cristy90dbac72010-08-22 15:08:40 +0000100 { "BGRA", "BGR" },
cristye3e77a12009-10-16 00:47:21 +0000101 { "CMYKA", "CMYK" },
102 { "C", "RAW" },
103 { "CAL", "CALS" },
cristyc05417c2010-09-21 16:30:32 +0000104 { "CANVAS", "XC" },
cristye3e77a12009-10-16 00:47:21 +0000105 { "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" },
cristye3e77a12009-10-16 00:47:21 +0000129 { "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" },
cristy02f1fda2009-12-10 15:23:56 +0000148 { "J2C", "JP2" },
cristy4faac682012-05-10 12:09:45 +0000149 { "J2K", "JP2" },
cristye3e77a12009-10-16 00:47:21 +0000150 { "JPG", "JPEG" },
151 { "JPX", "JP2" },
152 { "K", "RAW" },
153 { "LOGO", "MAGICK" },
154 { "M2V", "MPEG" },
155 { "M4V", "MPEG" },
156 { "M", "RAW" },
157 { "MNG", "PNG" },
cristy1dbd5a32010-11-28 22:23:27 +0000158 { "MOV", "MPEG" },
cristye3e77a12009-10-16 00:47:21 +0000159 { "MP4", "MPEG" },
160 { "MPG", "MPEG" },
161 { "MPRI", "MPR" },
cristy1be98ec2012-01-30 16:51:12 +0000162 { "MEF", "DNG" },
cristye3e77a12009-10-16 00:47:21 +0000163 { "MRW", "DNG" },
164 { "MSVG", "SVG" },
165 { "NEF", "DNG" },
166 { "NETSCAPE", "MAGICK" },
cristycb8f0ae2012-03-02 12:26:21 +0000167 { "NRW", "DNG" },
cristye3e77a12009-10-16 00:47:21 +0000168 { "O", "RAW" },
169 { "ORF", "DNG" },
170 { "OTF", "TTF" },
171 { "P7", "PNM" },
172 { "PAL", "UYVY" },
173 { "PAM", "PNM" },
174 { "PBM", "PNM" },
175 { "PCDS", "PCD" },
176 { "PDFA", "PDF" },
177 { "PEF", "DNG" },
178 { "PEF", "DNG" },
179 { "PFA", "TTF" },
180 { "PFB", "TTF" },
181 { "PFM", "PNM" },
182 { "PGM", "PNM" },
183 { "PGX", "JP2" },
184 { "PICON", "XPM" },
185 { "PJPEG", "JPEG" },
186 { "PM", "XPM" },
187 { "PNG24", "PNG" },
188 { "PNG32", "PNG" },
189 { "PNG8", "PNG" },
190 { "PPM", "PNM" },
cristyb4233012010-02-28 20:09:14 +0000191 { "PSB", "PSD" },
cristye3e77a12009-10-16 00:47:21 +0000192 { "PTIF", "TIFF" },
193 { "RADIAL-GRADIENT", "GRADIENT" },
194 { "RAF", "DNG" },
195 { "RAS", "SUN" },
cristye3e77a12009-10-16 00:47:21 +0000196 { "RGBA", "RGB" },
197 { "RGBO", "RGB" },
198 { "R", "RAW" },
199 { "ROSE", "MAGICK" },
200 { "SHTML", "HTML" },
201 { "SR2", "DNG" },
202 { "SRF", "DNG" },
203 { "SVGZ", "SVG" },
204 { "TEXT", "TXT" },
205 { "TIFF64", "TIFF" },
206 { "TIF", "TIFF" },
207 { "TTC", "TTF" },
208 { "UBRL", "BRAILLE" },
209 { "VDA", "TGA" },
210 { "VST", "TGA" },
cristyed40d622011-01-17 15:51:00 +0000211 { "WIZARD", "MAGICK" },
cristye3e77a12009-10-16 00:47:21 +0000212 { "WMV", "MPEG" },
213 { "WMFWIN32", "EMF" },
214 { "WMZ", "WMF" },
215 { "X3f", "DNG" },
216 { "XMP", "META" },
217 { "XTRNARRAY", "XTRN" },
218 { "XTRNBLOB", "XTRN" },
cristye3e77a12009-10-16 00:47:21 +0000219 { "XTRNFILE", "XTRN" },
220 { "XTRNIMAGE", "XTRN" },
cristye3e77a12009-10-16 00:47:21 +0000221 { "XV", "VIFF" },
222 { "Y", "RAW" },
cristy54a531d2009-10-21 17:58:01 +0000223 { "YCbCrA", "YCbCr" }
cristye3e77a12009-10-16 00:47:21 +0000224 };
225
cristy3ed852e2009-09-05 21:47:34 +0000226static SemaphoreInfo
227 *coder_semaphore = (SemaphoreInfo *) NULL;
228
229static SplayTreeInfo
230 *coder_list = (SplayTreeInfo *) NULL;
231
232static volatile MagickBooleanType
233 instantiate_coder = MagickFalse;
234
235/*
236 Forward declarations.
237*/
238static MagickBooleanType
239 InitializeCoderList(ExceptionInfo *),
240 LoadCoderLists(const char *,ExceptionInfo *);
241
242/*
243%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
244% %
245% %
246% %
cristyf34a1452009-10-24 22:29:27 +0000247+ 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 +0000248% %
249% %
250% %
251%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
252%
cristyf34a1452009-10-24 22:29:27 +0000253% CoderComponentGenesis() instantiates the coder component.
cristy3ed852e2009-09-05 21:47:34 +0000254%
cristyf34a1452009-10-24 22:29:27 +0000255% The format of the CoderComponentGenesis method is:
cristy3ed852e2009-09-05 21:47:34 +0000256%
cristyf34a1452009-10-24 22:29:27 +0000257% MagickBooleanType CoderComponentGenesis(void)
cristy3ed852e2009-09-05 21:47:34 +0000258%
259*/
cristy5ff4eaf2011-09-03 01:38:02 +0000260MagickPrivate MagickBooleanType CoderComponentGenesis(void)
cristyf34a1452009-10-24 22:29:27 +0000261{
cristy165b6092009-10-26 13:52:10 +0000262 AcquireSemaphoreInfo(&coder_semaphore);
cristyf34a1452009-10-24 22:29:27 +0000263 return(MagickTrue);
264}
265
266/*
267%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
268% %
269% %
270% %
271+ C o d e r C o m p o n e n t T e r m i n u s %
272% %
273% %
274% %
275%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
276%
277% CoderComponentTerminus() destroys the coder component.
278%
279% The format of the CoderComponentTerminus method is:
280%
281% CoderComponentTerminus(void)
282%
283*/
cristy5ff4eaf2011-09-03 01:38:02 +0000284MagickPrivate void CoderComponentTerminus(void)
cristy3ed852e2009-09-05 21:47:34 +0000285{
cristy18b17442009-10-25 18:36:48 +0000286 if (coder_semaphore == (SemaphoreInfo *) NULL)
287 AcquireSemaphoreInfo(&coder_semaphore);
cristyf84a1932010-01-03 18:00:18 +0000288 LockSemaphoreInfo(coder_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000289 if (coder_list != (SplayTreeInfo *) NULL)
290 coder_list=DestroySplayTree(coder_list);
291 instantiate_coder=MagickFalse;
cristyf84a1932010-01-03 18:00:18 +0000292 UnlockSemaphoreInfo(coder_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000293 DestroySemaphoreInfo(&coder_semaphore);
294}
295
296/*
297%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
298% %
299% %
300% %
301+ G e t C o d e r I n f o %
302% %
303% %
304% %
305%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
306%
307% GetCoderInfo searches the coder list for the specified name and if found
308% returns attributes for that coder.
309%
310% The format of the GetCoderInfo method is:
311%
312% const CoderInfo *GetCoderInfo(const char *name,ExceptionInfo *exception)
313%
314% A description of each parameter follows:
315%
316% o name: the coder name.
317%
318% o exception: return any errors or warnings in this structure.
319%
320*/
321MagickExport const CoderInfo *GetCoderInfo(const char *name,
322 ExceptionInfo *exception)
323{
324 assert(exception != (ExceptionInfo *) NULL);
325 if ((coder_list == (SplayTreeInfo *) NULL) ||
326 (instantiate_coder == MagickFalse))
327 if (InitializeCoderList(exception) == MagickFalse)
328 return((const CoderInfo *) NULL);
329 if ((coder_list == (SplayTreeInfo *) NULL) ||
330 (GetNumberOfNodesInSplayTree(coder_list) == 0))
331 return((const CoderInfo *) NULL);
332 if ((name == (const char *) NULL) || (LocaleCompare(name,"*") == 0))
333 {
334 ResetSplayTreeIterator(coder_list);
335 return((const CoderInfo *) GetNextValueInSplayTree(coder_list));
336 }
337 return((const CoderInfo *) GetValueFromSplayTree(coder_list,name));
338}
339
340/*
341%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
342% %
343% %
344% %
345% G e t C o d e r I n f o L i s t %
346% %
347% %
348% %
349%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
350%
351% GetCoderInfoList() returns any coder_map that match the specified pattern.
352% The format of the GetCoderInfoList function is:
353%
354% const CoderInfo **GetCoderInfoList(const char *pattern,
cristybb503372010-05-27 20:51:26 +0000355% size_t *number_coders,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000356%
357% A description of each parameter follows:
358%
359% o pattern: Specifies a pointer to a text string containing a pattern.
360%
361% o number_coders: This integer returns the number of coders in the list.
362%
363% o exception: return any errors or warnings in this structure.
364%
365*/
366
367static int CoderInfoCompare(const void *x,const void *y)
368{
369 const CoderInfo
370 **p,
371 **q;
372
373 p=(const CoderInfo **) x,
374 q=(const CoderInfo **) y;
375 if (LocaleCompare((*p)->path,(*q)->path) == 0)
376 return(LocaleCompare((*p)->name,(*q)->name));
377 return(LocaleCompare((*p)->path,(*q)->path));
378}
379
380MagickExport const CoderInfo **GetCoderInfoList(const char *pattern,
cristybb503372010-05-27 20:51:26 +0000381 size_t *number_coders,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000382{
383 const CoderInfo
384 **coder_map;
385
386 register const CoderInfo
387 *p;
388
cristybb503372010-05-27 20:51:26 +0000389 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000390 i;
391
392 /*
393 Allocate coder list.
394 */
395 assert(pattern != (char *) NULL);
396 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
cristybb503372010-05-27 20:51:26 +0000397 assert(number_coders != (size_t *) NULL);
cristy3ed852e2009-09-05 21:47:34 +0000398 *number_coders=0;
399 p=GetCoderInfo("*",exception);
400 if (p == (const CoderInfo *) NULL)
401 return((const CoderInfo **) NULL);
402 coder_map=(const CoderInfo **) AcquireQuantumMemory((size_t)
403 GetNumberOfNodesInSplayTree(coder_list)+1UL,sizeof(*coder_map));
404 if (coder_map == (const CoderInfo **) NULL)
405 return((const CoderInfo **) NULL);
406 /*
407 Generate coder list.
408 */
cristyf84a1932010-01-03 18:00:18 +0000409 LockSemaphoreInfo(coder_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000410 ResetSplayTreeIterator(coder_list);
411 p=(const CoderInfo *) GetNextValueInSplayTree(coder_list);
412 for (i=0; p != (const CoderInfo *) NULL; )
413 {
414 if ((p->stealth == MagickFalse) &&
415 (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
416 coder_map[i++]=p;
417 p=(const CoderInfo *) GetNextValueInSplayTree(coder_list);
418 }
cristyf84a1932010-01-03 18:00:18 +0000419 UnlockSemaphoreInfo(coder_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000420 qsort((void *) coder_map,(size_t) i,sizeof(*coder_map),CoderInfoCompare);
421 coder_map[i]=(CoderInfo *) NULL;
cristybb503372010-05-27 20:51:26 +0000422 *number_coders=(size_t) i;
cristy3ed852e2009-09-05 21:47:34 +0000423 return(coder_map);
424}
425
426/*
427%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
428% %
429% %
430% %
431% G e t C o d e r L i s t %
432% %
433% %
434% %
435%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
436%
437% GetCoderList() returns any coder_map that match the specified pattern.
438%
439% The format of the GetCoderList function is:
440%
cristybb503372010-05-27 20:51:26 +0000441% char **GetCoderList(const char *pattern,size_t *number_coders,
cristy3ed852e2009-09-05 21:47:34 +0000442% ExceptionInfo *exception)
443%
444% A description of each parameter follows:
445%
446% o pattern: Specifies a pointer to a text string containing a pattern.
447%
448% o number_coders: This integer returns the number of coders in the list.
449%
450% o exception: return any errors or warnings in this structure.
451%
452*/
453
454static int CoderCompare(const void *x,const void *y)
455{
456 register const char
457 **p,
458 **q;
459
460 p=(const char **) x;
461 q=(const char **) y;
462 return(LocaleCompare(*p,*q));
463}
464
465MagickExport char **GetCoderList(const char *pattern,
cristybb503372010-05-27 20:51:26 +0000466 size_t *number_coders,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000467{
468 char
469 **coder_map;
470
471 register const CoderInfo
472 *p;
473
cristybb503372010-05-27 20:51:26 +0000474 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000475 i;
476
477 /*
478 Allocate coder list.
479 */
480 assert(pattern != (char *) NULL);
481 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
cristybb503372010-05-27 20:51:26 +0000482 assert(number_coders != (size_t *) NULL);
cristy3ed852e2009-09-05 21:47:34 +0000483 *number_coders=0;
484 p=GetCoderInfo("*",exception);
485 if (p == (const CoderInfo *) NULL)
486 return((char **) NULL);
487 coder_map=(char **) AcquireQuantumMemory((size_t)
488 GetNumberOfNodesInSplayTree(coder_list)+1UL,sizeof(*coder_map));
489 if (coder_map == (char **) NULL)
490 return((char **) NULL);
491 /*
492 Generate coder list.
493 */
cristyf84a1932010-01-03 18:00:18 +0000494 LockSemaphoreInfo(coder_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000495 ResetSplayTreeIterator(coder_list);
496 p=(const CoderInfo *) GetNextValueInSplayTree(coder_list);
497 for (i=0; p != (const CoderInfo *) NULL; )
498 {
499 if ((p->stealth == MagickFalse) &&
500 (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
501 coder_map[i++]=ConstantString(p->name);
502 p=(const CoderInfo *) GetNextValueInSplayTree(coder_list);
503 }
cristyf84a1932010-01-03 18:00:18 +0000504 UnlockSemaphoreInfo(coder_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000505 qsort((void *) coder_map,(size_t) i,sizeof(*coder_map),CoderCompare);
506 coder_map[i]=(char *) NULL;
cristybb503372010-05-27 20:51:26 +0000507 *number_coders=(size_t) i;
cristy3ed852e2009-09-05 21:47:34 +0000508 return(coder_map);
509}
510
511/*
512%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
513% %
514% %
515% %
516+ I n i t i a l i z e C o d e r L i s t %
517% %
518% %
519% %
520%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
521%
522% InitializeCoderList() initializes the coder list.
523%
524% The format of the InitializeCoderList method is:
525%
526% MagickBooleanType InitializeCoderList(ExceptionInfo *exception)
527%
528% A description of each parameter follows.
529%
530% o exception: return any errors or warnings in this structure.
531%
532*/
533static MagickBooleanType InitializeCoderList(ExceptionInfo *exception)
534{
535 if ((coder_list == (SplayTreeInfo *) NULL) &&
536 (instantiate_coder == MagickFalse))
537 {
cristy4e1dff62009-10-25 20:36:03 +0000538 if (coder_semaphore == (SemaphoreInfo *) NULL)
539 AcquireSemaphoreInfo(&coder_semaphore);
cristyf84a1932010-01-03 18:00:18 +0000540 LockSemaphoreInfo(coder_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000541 if ((coder_list == (SplayTreeInfo *) NULL) &&
542 (instantiate_coder == MagickFalse))
543 {
544 (void) LoadCoderLists(MagickCoderFilename,exception);
545 instantiate_coder=MagickTrue;
546 }
cristyf84a1932010-01-03 18:00:18 +0000547 UnlockSemaphoreInfo(coder_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000548 }
549 return(coder_list != (SplayTreeInfo *) NULL ? MagickTrue : MagickFalse);
550}
551
552/*
553%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
554% %
555% %
556% %
557% L i s t C o d e r I n f o %
558% %
559% %
560% %
561%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
562%
563% ListCoderInfo() lists the coder info to a file.
564%
565% The format of the ListCoderInfo coder is:
566%
567% MagickBooleanType ListCoderInfo(FILE *file,ExceptionInfo *exception)
568%
569% A description of each parameter follows.
570%
571% o file: An pointer to a FILE.
572%
573% o exception: return any errors or warnings in this structure.
574%
575*/
576MagickExport MagickBooleanType ListCoderInfo(FILE *file,
577 ExceptionInfo *exception)
578{
579 const char
580 *path;
581
582 const CoderInfo
583 **coder_info;
584
cristybb503372010-05-27 20:51:26 +0000585 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000586 i;
587
cristybb503372010-05-27 20:51:26 +0000588 size_t
cristy3ed852e2009-09-05 21:47:34 +0000589 number_coders;
590
cristy9d314ff2011-03-09 01:30:28 +0000591 ssize_t
592 j;
593
cristy3ed852e2009-09-05 21:47:34 +0000594 if (file == (const FILE *) NULL)
595 file=stdout;
596 coder_info=GetCoderInfoList("*",&number_coders,exception);
597 if (coder_info == (const CoderInfo **) NULL)
598 return(MagickFalse);
599 path=(const char *) NULL;
cristybb503372010-05-27 20:51:26 +0000600 for (i=0; i < (ssize_t) number_coders; i++)
cristy3ed852e2009-09-05 21:47:34 +0000601 {
602 if (coder_info[i]->stealth != MagickFalse)
603 continue;
604 if ((path == (const char *) NULL) ||
605 (LocaleCompare(path,coder_info[i]->path) != 0))
606 {
607 if (coder_info[i]->path != (char *) NULL)
cristyb51dff52011-05-19 16:55:47 +0000608 (void) FormatLocaleFile(file,"\nPath: %s\n\n",coder_info[i]->path);
609 (void) FormatLocaleFile(file,"Magick Coder\n");
cristy1e604812011-05-19 18:07:50 +0000610 (void) FormatLocaleFile(file,
611 "-------------------------------------------------"
cristy3ed852e2009-09-05 21:47:34 +0000612 "------------------------------\n");
613 }
614 path=coder_info[i]->path;
cristyb51dff52011-05-19 16:55:47 +0000615 (void) FormatLocaleFile(file,"%s",coder_info[i]->magick);
cristybb503372010-05-27 20:51:26 +0000616 for (j=(ssize_t) strlen(coder_info[i]->magick); j <= 11; j++)
cristyb51dff52011-05-19 16:55:47 +0000617 (void) FormatLocaleFile(file," ");
cristy3ed852e2009-09-05 21:47:34 +0000618 if (coder_info[i]->name != (char *) NULL)
cristyb51dff52011-05-19 16:55:47 +0000619 (void) FormatLocaleFile(file,"%s",coder_info[i]->name);
620 (void) FormatLocaleFile(file,"\n");
cristy3ed852e2009-09-05 21:47:34 +0000621 }
622 coder_info=(const CoderInfo **) RelinquishMagickMemory((void *) coder_info);
623 (void) fflush(file);
624 return(MagickTrue);
625}
626
627/*
628%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
629% %
630% %
631% %
632+ L o a d C o d e r L i s t %
633% %
634% %
635% %
636%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
637%
638% LoadCoderList() loads the coder configuration file which provides a
639% mapping between coder attributes and a coder name.
640%
641% The format of the LoadCoderList coder is:
642%
643% MagickBooleanType LoadCoderList(const char *xml,const char *filename,
cristybb503372010-05-27 20:51:26 +0000644% const size_t depth,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000645%
646% A description of each parameter follows:
647%
648% o xml: The coder list in XML format.
649%
650% o filename: The coder list filename.
651%
652% o depth: depth of <include /> statements.
653%
654% o exception: return any errors or warnings in this structure.
655%
656*/
657
658static void *DestroyCoderNode(void *coder_info)
659{
660 register CoderInfo
661 *p;
662
663 p=(CoderInfo *) coder_info;
cristye3e77a12009-10-16 00:47:21 +0000664 if (p->exempt == MagickFalse)
665 {
666 if (p->path != (char *) NULL)
667 p->path=DestroyString(p->path);
668 if (p->name != (char *) NULL)
669 p->name=DestroyString(p->name);
670 if (p->magick != (char *) NULL)
671 p->magick=DestroyString(p->magick);
672 }
cristy3ed852e2009-09-05 21:47:34 +0000673 return(RelinquishMagickMemory(p));
674}
675
676static MagickBooleanType LoadCoderList(const char *xml,const char *filename,
cristybb503372010-05-27 20:51:26 +0000677 const size_t depth,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000678{
679 char
680 keyword[MaxTextExtent],
681 *token;
682
683 const char
684 *q;
685
686 CoderInfo
687 *coder_info;
688
689 MagickBooleanType
690 status;
691
692 /*
693 Load the coder map file.
694 */
695 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
696 "Loading coder configuration file \"%s\" ...",filename);
697 if (xml == (const char *) NULL)
698 return(MagickFalse);
699 if (coder_list == (SplayTreeInfo *) NULL)
700 {
701 coder_list=NewSplayTree(CompareSplayTreeString,RelinquishMagickMemory,
702 DestroyCoderNode);
703 if (coder_list == (SplayTreeInfo *) NULL)
704 {
705 ThrowFileException(exception,ResourceLimitError,
706 "MemoryAllocationFailed",filename);
707 return(MagickFalse);
708 }
709 }
710 status=MagickTrue;
711 coder_info=(CoderInfo *) NULL;
712 token=AcquireString(xml);
713 for (q=(char *) xml; *q != '\0'; )
714 {
715 /*
716 Interpret XML.
717 */
718 GetMagickToken(q,&q,token);
719 if (*token == '\0')
720 break;
721 (void) CopyMagickString(keyword,token,MaxTextExtent);
722 if (LocaleNCompare(keyword,"<!DOCTYPE",9) == 0)
723 {
724 /*
725 Doctype element.
726 */
727 while ((LocaleNCompare(q,"]>",2) != 0) && (*q != '\0'))
728 GetMagickToken(q,&q,token);
729 continue;
730 }
731 if (LocaleNCompare(keyword,"<!--",4) == 0)
732 {
733 /*
734 Comment element.
735 */
736 while ((LocaleNCompare(q,"->",2) != 0) && (*q != '\0'))
737 GetMagickToken(q,&q,token);
738 continue;
739 }
740 if (LocaleCompare(keyword,"<include") == 0)
741 {
742 /*
743 Include element.
744 */
745 while (((*token != '/') && (*(token+1) != '>')) && (*q != '\0'))
746 {
747 (void) CopyMagickString(keyword,token,MaxTextExtent);
748 GetMagickToken(q,&q,token);
749 if (*token != '=')
750 continue;
751 GetMagickToken(q,&q,token);
752 if (LocaleCompare(keyword,"file") == 0)
753 {
754 if (depth > 200)
755 (void) ThrowMagickException(exception,GetMagickModule(),
anthonye5b39652012-04-21 05:37:29 +0000756 ConfigureError,"IncludeNodeNestedTooDeeply","'%s'",token);
cristy3ed852e2009-09-05 21:47:34 +0000757 else
758 {
759 char
760 path[MaxTextExtent],
761 *xml;
762
763 GetPathComponent(filename,HeadPath,path);
764 if (*path != '\0')
765 (void) ConcatenateMagickString(path,DirectorySeparator,
766 MaxTextExtent);
767 if (*token == *DirectorySeparator)
768 (void) CopyMagickString(path,token,MaxTextExtent);
769 else
770 (void) ConcatenateMagickString(path,token,MaxTextExtent);
771 xml=FileToString(path,~0,exception);
772 if (xml != (char *) NULL)
773 {
774 status=LoadCoderList(xml,path,depth+1,exception);
775 xml=(char *) RelinquishMagickMemory(xml);
776 }
777 }
778 }
779 }
780 continue;
781 }
782 if (LocaleCompare(keyword,"<coder") == 0)
783 {
784 /*
785 Coder element.
786 */
cristy73bd4a52010-10-05 11:24:23 +0000787 coder_info=(CoderInfo *) AcquireMagickMemory(sizeof(*coder_info));
cristy3ed852e2009-09-05 21:47:34 +0000788 if (coder_info == (CoderInfo *) NULL)
789 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
790 (void) ResetMagickMemory(coder_info,0,sizeof(*coder_info));
791 coder_info->path=ConstantString(filename);
cristye3e77a12009-10-16 00:47:21 +0000792 coder_info->exempt=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +0000793 coder_info->signature=MagickSignature;
794 continue;
795 }
796 if (coder_info == (CoderInfo *) NULL)
797 continue;
798 if (LocaleCompare(keyword,"/>") == 0)
799 {
800 status=AddValueToSplayTree(coder_list,ConstantString(
801 coder_info->magick),coder_info);
802 if (status == MagickFalse)
803 (void) ThrowMagickException(exception,GetMagickModule(),
anthonye5b39652012-04-21 05:37:29 +0000804 ResourceLimitError,"MemoryAllocationFailed","'%s'",
cristy3ed852e2009-09-05 21:47:34 +0000805 coder_info->magick);
806 coder_info=(CoderInfo *) NULL;
807 }
808 GetMagickToken(q,(const char **) NULL,token);
809 if (*token != '=')
810 continue;
811 GetMagickToken(q,&q,token);
812 GetMagickToken(q,&q,token);
813 switch (*keyword)
814 {
815 case 'M':
816 case 'm':
817 {
818 if (LocaleCompare((char *) keyword,"magick") == 0)
819 {
820 coder_info->magick=ConstantString(token);
821 break;
822 }
823 break;
824 }
825 case 'N':
826 case 'n':
827 {
828 if (LocaleCompare((char *) keyword,"name") == 0)
829 {
830 coder_info->name=ConstantString(token);
831 break;
832 }
833 break;
834 }
835 case 'S':
836 case 's':
837 {
838 if (LocaleCompare((char *) keyword,"stealth") == 0)
839 {
anthony6f201312012-03-30 04:08:15 +0000840 coder_info->stealth=IsStringTrue(token);
cristy3ed852e2009-09-05 21:47:34 +0000841 break;
842 }
843 break;
844 }
845 default:
846 break;
847 }
848 }
849 token=(char *) RelinquishMagickMemory(token);
850 return(status);
851}
852
853/*
854%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
855% %
856% %
857% %
858% L o a d C o d e r L i s t s %
859% %
860% %
861% %
862%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
863%
864% LoadCoderLists() loads one or more coder configuration file which
865% provides a mapping between coder attributes and a coder name.
866%
867% The format of the LoadCoderLists coder is:
868%
869% MagickBooleanType LoadCoderLists(const char *filename,
870% ExceptionInfo *exception)
871%
872% A description of each parameter follows:
873%
874% o filename: the font file name.
875%
876% o exception: return any errors or warnings in this structure.
877%
878*/
879static MagickBooleanType LoadCoderLists(const char *filename,
880 ExceptionInfo *exception)
881{
cristy3ed852e2009-09-05 21:47:34 +0000882 const StringInfo
883 *option;
884
885 LinkedListInfo
886 *options;
887
888 MagickStatusType
889 status;
890
cristybb503372010-05-27 20:51:26 +0000891 register ssize_t
cristy54a531d2009-10-21 17:58:01 +0000892 i;
cristye3e77a12009-10-16 00:47:21 +0000893
894 /*
895 Load built-in coder map.
896 */
cristy3ed852e2009-09-05 21:47:34 +0000897 status=MagickFalse;
cristye3e77a12009-10-16 00:47:21 +0000898 if (coder_list == (SplayTreeInfo *) NULL)
899 {
900 coder_list=NewSplayTree(CompareSplayTreeString,RelinquishMagickMemory,
901 DestroyCoderNode);
902 if (coder_list == (SplayTreeInfo *) NULL)
903 {
904 ThrowFileException(exception,ResourceLimitError,
905 "MemoryAllocationFailed",filename);
906 return(MagickFalse);
907 }
908 }
cristybb503372010-05-27 20:51:26 +0000909 for (i=0; i < (ssize_t) (sizeof(CoderMap)/sizeof(*CoderMap)); i++)
cristye3e77a12009-10-16 00:47:21 +0000910 {
911 CoderInfo
912 *coder_info;
913
cristy54a531d2009-10-21 17:58:01 +0000914 register const CoderMapInfo
915 *p;
916
917 p=CoderMap+i;
cristy73bd4a52010-10-05 11:24:23 +0000918 coder_info=(CoderInfo *) AcquireMagickMemory(sizeof(*coder_info));
cristye3e77a12009-10-16 00:47:21 +0000919 if (coder_info == (CoderInfo *) NULL)
920 {
921 (void) ThrowMagickException(exception,GetMagickModule(),
anthonye5b39652012-04-21 05:37:29 +0000922 ResourceLimitError,"MemoryAllocationFailed","'%s'",coder_info->name);
cristye3e77a12009-10-16 00:47:21 +0000923 continue;
924 }
925 (void) ResetMagickMemory(coder_info,0,sizeof(*coder_info));
cristy38b8ed92009-10-16 01:20:21 +0000926 coder_info->path=(char *) "[built-in]";
cristye3e77a12009-10-16 00:47:21 +0000927 coder_info->magick=(char *) p->magick;
928 coder_info->name=(char *) p->name;
929 coder_info->exempt=MagickTrue;
930 coder_info->signature=MagickSignature;
931 status=AddValueToSplayTree(coder_list,ConstantString(coder_info->magick),
932 coder_info);
933 if (status == MagickFalse)
934 (void) ThrowMagickException(exception,GetMagickModule(),
anthonye5b39652012-04-21 05:37:29 +0000935 ResourceLimitError,"MemoryAllocationFailed","'%s'",coder_info->name);
cristye3e77a12009-10-16 00:47:21 +0000936 }
937 /*
938 Load external coder map.
939 */
cristy3ed852e2009-09-05 21:47:34 +0000940 options=GetConfigureOptions(filename,exception);
941 option=(const StringInfo *) GetNextValueInLinkedList(options);
942 while (option != (const StringInfo *) NULL)
943 {
944 status|=LoadCoderList((const char *) GetStringInfoDatum(option),
945 GetStringInfoPath(option),0,exception);
946 option=(const StringInfo *) GetNextValueInLinkedList(options);
947 }
948 options=DestroyConfigureOptions(options);
cristy3ed852e2009-09-05 21:47:34 +0000949 return(status != 0 ? MagickTrue : MagickFalse);
cristy3ed852e2009-09-05 21:47:34 +0000950}