blob: 1cf2ca460053b1c59e4cb42fd3d85c0652089b4d [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" },
cristy98aceb82012-10-20 21:24:00 +0000200 { "RW2", "DNG" },
cristye3e77a12009-10-16 00:47:21 +0000201 { "SHTML", "HTML" },
202 { "SR2", "DNG" },
203 { "SRF", "DNG" },
204 { "SVGZ", "SVG" },
205 { "TEXT", "TXT" },
206 { "TIFF64", "TIFF" },
207 { "TIF", "TIFF" },
208 { "TTC", "TTF" },
209 { "UBRL", "BRAILLE" },
210 { "VDA", "TGA" },
211 { "VST", "TGA" },
cristyed40d622011-01-17 15:51:00 +0000212 { "WIZARD", "MAGICK" },
cristye3e77a12009-10-16 00:47:21 +0000213 { "WMV", "MPEG" },
214 { "WMFWIN32", "EMF" },
215 { "WMZ", "WMF" },
216 { "X3f", "DNG" },
217 { "XMP", "META" },
218 { "XTRNARRAY", "XTRN" },
219 { "XTRNBLOB", "XTRN" },
cristye3e77a12009-10-16 00:47:21 +0000220 { "XTRNFILE", "XTRN" },
221 { "XTRNIMAGE", "XTRN" },
cristye3e77a12009-10-16 00:47:21 +0000222 { "XV", "VIFF" },
223 { "Y", "RAW" },
cristy54a531d2009-10-21 17:58:01 +0000224 { "YCbCrA", "YCbCr" }
cristye3e77a12009-10-16 00:47:21 +0000225 };
226
cristy3ed852e2009-09-05 21:47:34 +0000227static SemaphoreInfo
228 *coder_semaphore = (SemaphoreInfo *) NULL;
229
230static SplayTreeInfo
231 *coder_list = (SplayTreeInfo *) NULL;
232
233static volatile MagickBooleanType
234 instantiate_coder = MagickFalse;
235
236/*
237 Forward declarations.
238*/
239static MagickBooleanType
240 InitializeCoderList(ExceptionInfo *),
241 LoadCoderLists(const char *,ExceptionInfo *);
242
243/*
244%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
245% %
246% %
247% %
cristyf34a1452009-10-24 22:29:27 +0000248+ 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 +0000249% %
250% %
251% %
252%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
253%
cristyf34a1452009-10-24 22:29:27 +0000254% CoderComponentGenesis() instantiates the coder component.
cristy3ed852e2009-09-05 21:47:34 +0000255%
cristyf34a1452009-10-24 22:29:27 +0000256% The format of the CoderComponentGenesis method is:
cristy3ed852e2009-09-05 21:47:34 +0000257%
cristyf34a1452009-10-24 22:29:27 +0000258% MagickBooleanType CoderComponentGenesis(void)
cristy3ed852e2009-09-05 21:47:34 +0000259%
260*/
cristy5ff4eaf2011-09-03 01:38:02 +0000261MagickPrivate MagickBooleanType CoderComponentGenesis(void)
cristyf34a1452009-10-24 22:29:27 +0000262{
cristy165b6092009-10-26 13:52:10 +0000263 AcquireSemaphoreInfo(&coder_semaphore);
cristyf34a1452009-10-24 22:29:27 +0000264 return(MagickTrue);
265}
266
267/*
268%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
269% %
270% %
271% %
272+ C o d e r C o m p o n e n t T e r m i n u s %
273% %
274% %
275% %
276%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
277%
278% CoderComponentTerminus() destroys the coder component.
279%
280% The format of the CoderComponentTerminus method is:
281%
282% CoderComponentTerminus(void)
283%
284*/
cristy5ff4eaf2011-09-03 01:38:02 +0000285MagickPrivate void CoderComponentTerminus(void)
cristy3ed852e2009-09-05 21:47:34 +0000286{
cristy18b17442009-10-25 18:36:48 +0000287 if (coder_semaphore == (SemaphoreInfo *) NULL)
288 AcquireSemaphoreInfo(&coder_semaphore);
cristyf84a1932010-01-03 18:00:18 +0000289 LockSemaphoreInfo(coder_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000290 if (coder_list != (SplayTreeInfo *) NULL)
291 coder_list=DestroySplayTree(coder_list);
292 instantiate_coder=MagickFalse;
cristyf84a1932010-01-03 18:00:18 +0000293 UnlockSemaphoreInfo(coder_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000294 DestroySemaphoreInfo(&coder_semaphore);
295}
296
297/*
298%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
299% %
300% %
301% %
302+ G e t C o d e r I n f o %
303% %
304% %
305% %
306%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
307%
308% GetCoderInfo searches the coder list for the specified name and if found
309% returns attributes for that coder.
310%
311% The format of the GetCoderInfo method is:
312%
313% const CoderInfo *GetCoderInfo(const char *name,ExceptionInfo *exception)
314%
315% A description of each parameter follows:
316%
317% o name: the coder name.
318%
319% o exception: return any errors or warnings in this structure.
320%
321*/
322MagickExport const CoderInfo *GetCoderInfo(const char *name,
323 ExceptionInfo *exception)
324{
325 assert(exception != (ExceptionInfo *) NULL);
326 if ((coder_list == (SplayTreeInfo *) NULL) ||
327 (instantiate_coder == MagickFalse))
328 if (InitializeCoderList(exception) == MagickFalse)
329 return((const CoderInfo *) NULL);
330 if ((coder_list == (SplayTreeInfo *) NULL) ||
331 (GetNumberOfNodesInSplayTree(coder_list) == 0))
332 return((const CoderInfo *) NULL);
333 if ((name == (const char *) NULL) || (LocaleCompare(name,"*") == 0))
334 {
335 ResetSplayTreeIterator(coder_list);
336 return((const CoderInfo *) GetNextValueInSplayTree(coder_list));
337 }
338 return((const CoderInfo *) GetValueFromSplayTree(coder_list,name));
339}
340
341/*
342%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
343% %
344% %
345% %
346% G e t C o d e r I n f o L i s t %
347% %
348% %
349% %
350%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
351%
352% GetCoderInfoList() returns any coder_map that match the specified pattern.
353% The format of the GetCoderInfoList function is:
354%
355% const CoderInfo **GetCoderInfoList(const char *pattern,
cristybb503372010-05-27 20:51:26 +0000356% size_t *number_coders,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000357%
358% A description of each parameter follows:
359%
360% o pattern: Specifies a pointer to a text string containing a pattern.
361%
362% o number_coders: This integer returns the number of coders in the list.
363%
364% o exception: return any errors or warnings in this structure.
365%
366*/
367
368static int CoderInfoCompare(const void *x,const void *y)
369{
370 const CoderInfo
371 **p,
372 **q;
373
374 p=(const CoderInfo **) x,
375 q=(const CoderInfo **) y;
376 if (LocaleCompare((*p)->path,(*q)->path) == 0)
377 return(LocaleCompare((*p)->name,(*q)->name));
378 return(LocaleCompare((*p)->path,(*q)->path));
379}
380
381MagickExport const CoderInfo **GetCoderInfoList(const char *pattern,
cristybb503372010-05-27 20:51:26 +0000382 size_t *number_coders,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000383{
384 const CoderInfo
385 **coder_map;
386
387 register const CoderInfo
388 *p;
389
cristybb503372010-05-27 20:51:26 +0000390 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000391 i;
392
393 /*
394 Allocate coder list.
395 */
396 assert(pattern != (char *) NULL);
397 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
cristybb503372010-05-27 20:51:26 +0000398 assert(number_coders != (size_t *) NULL);
cristy3ed852e2009-09-05 21:47:34 +0000399 *number_coders=0;
400 p=GetCoderInfo("*",exception);
401 if (p == (const CoderInfo *) NULL)
402 return((const CoderInfo **) NULL);
403 coder_map=(const CoderInfo **) AcquireQuantumMemory((size_t)
404 GetNumberOfNodesInSplayTree(coder_list)+1UL,sizeof(*coder_map));
405 if (coder_map == (const CoderInfo **) NULL)
406 return((const CoderInfo **) NULL);
407 /*
408 Generate coder list.
409 */
cristyf84a1932010-01-03 18:00:18 +0000410 LockSemaphoreInfo(coder_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000411 ResetSplayTreeIterator(coder_list);
412 p=(const CoderInfo *) GetNextValueInSplayTree(coder_list);
413 for (i=0; p != (const CoderInfo *) NULL; )
414 {
415 if ((p->stealth == MagickFalse) &&
416 (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
417 coder_map[i++]=p;
418 p=(const CoderInfo *) GetNextValueInSplayTree(coder_list);
419 }
cristyf84a1932010-01-03 18:00:18 +0000420 UnlockSemaphoreInfo(coder_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000421 qsort((void *) coder_map,(size_t) i,sizeof(*coder_map),CoderInfoCompare);
422 coder_map[i]=(CoderInfo *) NULL;
cristybb503372010-05-27 20:51:26 +0000423 *number_coders=(size_t) i;
cristy3ed852e2009-09-05 21:47:34 +0000424 return(coder_map);
425}
426
427/*
428%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
429% %
430% %
431% %
432% G e t C o d e r L i s t %
433% %
434% %
435% %
436%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
437%
438% GetCoderList() returns any coder_map that match the specified pattern.
439%
440% The format of the GetCoderList function is:
441%
cristybb503372010-05-27 20:51:26 +0000442% char **GetCoderList(const char *pattern,size_t *number_coders,
cristy3ed852e2009-09-05 21:47:34 +0000443% ExceptionInfo *exception)
444%
445% A description of each parameter follows:
446%
447% o pattern: Specifies a pointer to a text string containing a pattern.
448%
449% o number_coders: This integer returns the number of coders in the list.
450%
451% o exception: return any errors or warnings in this structure.
452%
453*/
454
455static int CoderCompare(const void *x,const void *y)
456{
457 register const char
458 **p,
459 **q;
460
461 p=(const char **) x;
462 q=(const char **) y;
463 return(LocaleCompare(*p,*q));
464}
465
466MagickExport char **GetCoderList(const char *pattern,
cristybb503372010-05-27 20:51:26 +0000467 size_t *number_coders,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000468{
469 char
470 **coder_map;
471
472 register const CoderInfo
473 *p;
474
cristybb503372010-05-27 20:51:26 +0000475 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000476 i;
477
478 /*
479 Allocate coder list.
480 */
481 assert(pattern != (char *) NULL);
482 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
cristybb503372010-05-27 20:51:26 +0000483 assert(number_coders != (size_t *) NULL);
cristy3ed852e2009-09-05 21:47:34 +0000484 *number_coders=0;
485 p=GetCoderInfo("*",exception);
486 if (p == (const CoderInfo *) NULL)
487 return((char **) NULL);
488 coder_map=(char **) AcquireQuantumMemory((size_t)
489 GetNumberOfNodesInSplayTree(coder_list)+1UL,sizeof(*coder_map));
490 if (coder_map == (char **) NULL)
491 return((char **) NULL);
492 /*
493 Generate coder list.
494 */
cristyf84a1932010-01-03 18:00:18 +0000495 LockSemaphoreInfo(coder_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000496 ResetSplayTreeIterator(coder_list);
497 p=(const CoderInfo *) GetNextValueInSplayTree(coder_list);
498 for (i=0; p != (const CoderInfo *) NULL; )
499 {
500 if ((p->stealth == MagickFalse) &&
501 (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
502 coder_map[i++]=ConstantString(p->name);
503 p=(const CoderInfo *) GetNextValueInSplayTree(coder_list);
504 }
cristyf84a1932010-01-03 18:00:18 +0000505 UnlockSemaphoreInfo(coder_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000506 qsort((void *) coder_map,(size_t) i,sizeof(*coder_map),CoderCompare);
507 coder_map[i]=(char *) NULL;
cristybb503372010-05-27 20:51:26 +0000508 *number_coders=(size_t) i;
cristy3ed852e2009-09-05 21:47:34 +0000509 return(coder_map);
510}
511
512/*
513%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
514% %
515% %
516% %
517+ I n i t i a l i z e C o d e r L i s t %
518% %
519% %
520% %
521%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
522%
523% InitializeCoderList() initializes the coder list.
524%
525% The format of the InitializeCoderList method is:
526%
527% MagickBooleanType InitializeCoderList(ExceptionInfo *exception)
528%
529% A description of each parameter follows.
530%
531% o exception: return any errors or warnings in this structure.
532%
533*/
534static MagickBooleanType InitializeCoderList(ExceptionInfo *exception)
535{
536 if ((coder_list == (SplayTreeInfo *) NULL) &&
537 (instantiate_coder == MagickFalse))
538 {
cristy4e1dff62009-10-25 20:36:03 +0000539 if (coder_semaphore == (SemaphoreInfo *) NULL)
540 AcquireSemaphoreInfo(&coder_semaphore);
cristyf84a1932010-01-03 18:00:18 +0000541 LockSemaphoreInfo(coder_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000542 if ((coder_list == (SplayTreeInfo *) NULL) &&
543 (instantiate_coder == MagickFalse))
544 {
545 (void) LoadCoderLists(MagickCoderFilename,exception);
546 instantiate_coder=MagickTrue;
547 }
cristyf84a1932010-01-03 18:00:18 +0000548 UnlockSemaphoreInfo(coder_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000549 }
550 return(coder_list != (SplayTreeInfo *) NULL ? MagickTrue : MagickFalse);
551}
552
553/*
554%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
555% %
556% %
557% %
558% L i s t C o d e r I n f o %
559% %
560% %
561% %
562%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
563%
564% ListCoderInfo() lists the coder info to a file.
565%
566% The format of the ListCoderInfo coder is:
567%
568% MagickBooleanType ListCoderInfo(FILE *file,ExceptionInfo *exception)
569%
570% A description of each parameter follows.
571%
572% o file: An pointer to a FILE.
573%
574% o exception: return any errors or warnings in this structure.
575%
576*/
577MagickExport MagickBooleanType ListCoderInfo(FILE *file,
578 ExceptionInfo *exception)
579{
580 const char
581 *path;
582
583 const CoderInfo
584 **coder_info;
585
cristybb503372010-05-27 20:51:26 +0000586 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000587 i;
588
cristybb503372010-05-27 20:51:26 +0000589 size_t
cristy3ed852e2009-09-05 21:47:34 +0000590 number_coders;
591
cristy9d314ff2011-03-09 01:30:28 +0000592 ssize_t
593 j;
594
cristy3ed852e2009-09-05 21:47:34 +0000595 if (file == (const FILE *) NULL)
596 file=stdout;
597 coder_info=GetCoderInfoList("*",&number_coders,exception);
598 if (coder_info == (const CoderInfo **) NULL)
599 return(MagickFalse);
600 path=(const char *) NULL;
cristybb503372010-05-27 20:51:26 +0000601 for (i=0; i < (ssize_t) number_coders; i++)
cristy3ed852e2009-09-05 21:47:34 +0000602 {
603 if (coder_info[i]->stealth != MagickFalse)
604 continue;
605 if ((path == (const char *) NULL) ||
606 (LocaleCompare(path,coder_info[i]->path) != 0))
607 {
608 if (coder_info[i]->path != (char *) NULL)
cristyb51dff52011-05-19 16:55:47 +0000609 (void) FormatLocaleFile(file,"\nPath: %s\n\n",coder_info[i]->path);
610 (void) FormatLocaleFile(file,"Magick Coder\n");
cristy1e604812011-05-19 18:07:50 +0000611 (void) FormatLocaleFile(file,
612 "-------------------------------------------------"
cristy3ed852e2009-09-05 21:47:34 +0000613 "------------------------------\n");
614 }
615 path=coder_info[i]->path;
cristyb51dff52011-05-19 16:55:47 +0000616 (void) FormatLocaleFile(file,"%s",coder_info[i]->magick);
cristybb503372010-05-27 20:51:26 +0000617 for (j=(ssize_t) strlen(coder_info[i]->magick); j <= 11; j++)
cristyb51dff52011-05-19 16:55:47 +0000618 (void) FormatLocaleFile(file," ");
cristy3ed852e2009-09-05 21:47:34 +0000619 if (coder_info[i]->name != (char *) NULL)
cristyb51dff52011-05-19 16:55:47 +0000620 (void) FormatLocaleFile(file,"%s",coder_info[i]->name);
621 (void) FormatLocaleFile(file,"\n");
cristy3ed852e2009-09-05 21:47:34 +0000622 }
623 coder_info=(const CoderInfo **) RelinquishMagickMemory((void *) coder_info);
624 (void) fflush(file);
625 return(MagickTrue);
626}
627
628/*
629%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
630% %
631% %
632% %
633+ L o a d C o d e r L i s t %
634% %
635% %
636% %
637%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
638%
639% LoadCoderList() loads the coder configuration file which provides a
640% mapping between coder attributes and a coder name.
641%
642% The format of the LoadCoderList coder is:
643%
644% MagickBooleanType LoadCoderList(const char *xml,const char *filename,
cristybb503372010-05-27 20:51:26 +0000645% const size_t depth,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000646%
647% A description of each parameter follows:
648%
649% o xml: The coder list in XML format.
650%
651% o filename: The coder list filename.
652%
653% o depth: depth of <include /> statements.
654%
655% o exception: return any errors or warnings in this structure.
656%
657*/
658
659static void *DestroyCoderNode(void *coder_info)
660{
661 register CoderInfo
662 *p;
663
664 p=(CoderInfo *) coder_info;
cristye3e77a12009-10-16 00:47:21 +0000665 if (p->exempt == MagickFalse)
666 {
667 if (p->path != (char *) NULL)
668 p->path=DestroyString(p->path);
669 if (p->name != (char *) NULL)
670 p->name=DestroyString(p->name);
671 if (p->magick != (char *) NULL)
672 p->magick=DestroyString(p->magick);
673 }
cristy3ed852e2009-09-05 21:47:34 +0000674 return(RelinquishMagickMemory(p));
675}
676
677static MagickBooleanType LoadCoderList(const char *xml,const char *filename,
cristybb503372010-05-27 20:51:26 +0000678 const size_t depth,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000679{
680 char
681 keyword[MaxTextExtent],
682 *token;
683
684 const char
685 *q;
686
687 CoderInfo
688 *coder_info;
689
690 MagickBooleanType
691 status;
692
693 /*
694 Load the coder map file.
695 */
696 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
697 "Loading coder configuration file \"%s\" ...",filename);
698 if (xml == (const char *) NULL)
699 return(MagickFalse);
700 if (coder_list == (SplayTreeInfo *) NULL)
701 {
702 coder_list=NewSplayTree(CompareSplayTreeString,RelinquishMagickMemory,
703 DestroyCoderNode);
704 if (coder_list == (SplayTreeInfo *) NULL)
705 {
706 ThrowFileException(exception,ResourceLimitError,
707 "MemoryAllocationFailed",filename);
708 return(MagickFalse);
709 }
710 }
711 status=MagickTrue;
712 coder_info=(CoderInfo *) NULL;
713 token=AcquireString(xml);
714 for (q=(char *) xml; *q != '\0'; )
715 {
716 /*
717 Interpret XML.
718 */
719 GetMagickToken(q,&q,token);
720 if (*token == '\0')
721 break;
722 (void) CopyMagickString(keyword,token,MaxTextExtent);
723 if (LocaleNCompare(keyword,"<!DOCTYPE",9) == 0)
724 {
725 /*
726 Doctype element.
727 */
728 while ((LocaleNCompare(q,"]>",2) != 0) && (*q != '\0'))
729 GetMagickToken(q,&q,token);
730 continue;
731 }
732 if (LocaleNCompare(keyword,"<!--",4) == 0)
733 {
734 /*
735 Comment element.
736 */
737 while ((LocaleNCompare(q,"->",2) != 0) && (*q != '\0'))
738 GetMagickToken(q,&q,token);
739 continue;
740 }
741 if (LocaleCompare(keyword,"<include") == 0)
742 {
743 /*
744 Include element.
745 */
746 while (((*token != '/') && (*(token+1) != '>')) && (*q != '\0'))
747 {
748 (void) CopyMagickString(keyword,token,MaxTextExtent);
749 GetMagickToken(q,&q,token);
750 if (*token != '=')
751 continue;
752 GetMagickToken(q,&q,token);
753 if (LocaleCompare(keyword,"file") == 0)
754 {
755 if (depth > 200)
756 (void) ThrowMagickException(exception,GetMagickModule(),
anthonye5b39652012-04-21 05:37:29 +0000757 ConfigureError,"IncludeNodeNestedTooDeeply","'%s'",token);
cristy3ed852e2009-09-05 21:47:34 +0000758 else
759 {
760 char
761 path[MaxTextExtent],
762 *xml;
763
764 GetPathComponent(filename,HeadPath,path);
765 if (*path != '\0')
766 (void) ConcatenateMagickString(path,DirectorySeparator,
767 MaxTextExtent);
768 if (*token == *DirectorySeparator)
769 (void) CopyMagickString(path,token,MaxTextExtent);
770 else
771 (void) ConcatenateMagickString(path,token,MaxTextExtent);
772 xml=FileToString(path,~0,exception);
773 if (xml != (char *) NULL)
774 {
775 status=LoadCoderList(xml,path,depth+1,exception);
776 xml=(char *) RelinquishMagickMemory(xml);
777 }
778 }
779 }
780 }
781 continue;
782 }
783 if (LocaleCompare(keyword,"<coder") == 0)
784 {
785 /*
786 Coder element.
787 */
cristy73bd4a52010-10-05 11:24:23 +0000788 coder_info=(CoderInfo *) AcquireMagickMemory(sizeof(*coder_info));
cristy3ed852e2009-09-05 21:47:34 +0000789 if (coder_info == (CoderInfo *) NULL)
790 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
791 (void) ResetMagickMemory(coder_info,0,sizeof(*coder_info));
792 coder_info->path=ConstantString(filename);
cristye3e77a12009-10-16 00:47:21 +0000793 coder_info->exempt=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +0000794 coder_info->signature=MagickSignature;
795 continue;
796 }
797 if (coder_info == (CoderInfo *) NULL)
798 continue;
799 if (LocaleCompare(keyword,"/>") == 0)
800 {
801 status=AddValueToSplayTree(coder_list,ConstantString(
802 coder_info->magick),coder_info);
803 if (status == MagickFalse)
804 (void) ThrowMagickException(exception,GetMagickModule(),
anthonye5b39652012-04-21 05:37:29 +0000805 ResourceLimitError,"MemoryAllocationFailed","'%s'",
cristy3ed852e2009-09-05 21:47:34 +0000806 coder_info->magick);
807 coder_info=(CoderInfo *) NULL;
808 }
809 GetMagickToken(q,(const char **) NULL,token);
810 if (*token != '=')
811 continue;
812 GetMagickToken(q,&q,token);
813 GetMagickToken(q,&q,token);
814 switch (*keyword)
815 {
816 case 'M':
817 case 'm':
818 {
819 if (LocaleCompare((char *) keyword,"magick") == 0)
820 {
821 coder_info->magick=ConstantString(token);
822 break;
823 }
824 break;
825 }
826 case 'N':
827 case 'n':
828 {
829 if (LocaleCompare((char *) keyword,"name") == 0)
830 {
831 coder_info->name=ConstantString(token);
832 break;
833 }
834 break;
835 }
836 case 'S':
837 case 's':
838 {
839 if (LocaleCompare((char *) keyword,"stealth") == 0)
840 {
anthony6f201312012-03-30 04:08:15 +0000841 coder_info->stealth=IsStringTrue(token);
cristy3ed852e2009-09-05 21:47:34 +0000842 break;
843 }
844 break;
845 }
846 default:
847 break;
848 }
849 }
850 token=(char *) RelinquishMagickMemory(token);
851 return(status);
852}
853
854/*
855%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
856% %
857% %
858% %
859% L o a d C o d e r L i s t s %
860% %
861% %
862% %
863%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
864%
865% LoadCoderLists() loads one or more coder configuration file which
866% provides a mapping between coder attributes and a coder name.
867%
868% The format of the LoadCoderLists coder is:
869%
870% MagickBooleanType LoadCoderLists(const char *filename,
871% ExceptionInfo *exception)
872%
873% A description of each parameter follows:
874%
875% o filename: the font file name.
876%
877% o exception: return any errors or warnings in this structure.
878%
879*/
880static MagickBooleanType LoadCoderLists(const char *filename,
881 ExceptionInfo *exception)
882{
cristy3ed852e2009-09-05 21:47:34 +0000883 const StringInfo
884 *option;
885
886 LinkedListInfo
887 *options;
888
889 MagickStatusType
890 status;
891
cristybb503372010-05-27 20:51:26 +0000892 register ssize_t
cristy54a531d2009-10-21 17:58:01 +0000893 i;
cristye3e77a12009-10-16 00:47:21 +0000894
895 /*
896 Load built-in coder map.
897 */
cristy3ed852e2009-09-05 21:47:34 +0000898 status=MagickFalse;
cristye3e77a12009-10-16 00:47:21 +0000899 if (coder_list == (SplayTreeInfo *) NULL)
900 {
901 coder_list=NewSplayTree(CompareSplayTreeString,RelinquishMagickMemory,
902 DestroyCoderNode);
903 if (coder_list == (SplayTreeInfo *) NULL)
904 {
905 ThrowFileException(exception,ResourceLimitError,
906 "MemoryAllocationFailed",filename);
907 return(MagickFalse);
908 }
909 }
cristybb503372010-05-27 20:51:26 +0000910 for (i=0; i < (ssize_t) (sizeof(CoderMap)/sizeof(*CoderMap)); i++)
cristye3e77a12009-10-16 00:47:21 +0000911 {
912 CoderInfo
913 *coder_info;
914
cristy54a531d2009-10-21 17:58:01 +0000915 register const CoderMapInfo
916 *p;
917
918 p=CoderMap+i;
cristy73bd4a52010-10-05 11:24:23 +0000919 coder_info=(CoderInfo *) AcquireMagickMemory(sizeof(*coder_info));
cristye3e77a12009-10-16 00:47:21 +0000920 if (coder_info == (CoderInfo *) NULL)
921 {
922 (void) ThrowMagickException(exception,GetMagickModule(),
anthonye5b39652012-04-21 05:37:29 +0000923 ResourceLimitError,"MemoryAllocationFailed","'%s'",coder_info->name);
cristye3e77a12009-10-16 00:47:21 +0000924 continue;
925 }
926 (void) ResetMagickMemory(coder_info,0,sizeof(*coder_info));
cristy38b8ed92009-10-16 01:20:21 +0000927 coder_info->path=(char *) "[built-in]";
cristye3e77a12009-10-16 00:47:21 +0000928 coder_info->magick=(char *) p->magick;
929 coder_info->name=(char *) p->name;
930 coder_info->exempt=MagickTrue;
931 coder_info->signature=MagickSignature;
932 status=AddValueToSplayTree(coder_list,ConstantString(coder_info->magick),
933 coder_info);
934 if (status == MagickFalse)
935 (void) ThrowMagickException(exception,GetMagickModule(),
anthonye5b39652012-04-21 05:37:29 +0000936 ResourceLimitError,"MemoryAllocationFailed","'%s'",coder_info->name);
cristye3e77a12009-10-16 00:47:21 +0000937 }
938 /*
939 Load external coder map.
940 */
cristy3ed852e2009-09-05 21:47:34 +0000941 options=GetConfigureOptions(filename,exception);
942 option=(const StringInfo *) GetNextValueInLinkedList(options);
943 while (option != (const StringInfo *) NULL)
944 {
945 status|=LoadCoderList((const char *) GetStringInfoDatum(option),
946 GetStringInfoPath(option),0,exception);
947 option=(const StringInfo *) GetNextValueInLinkedList(options);
948 }
949 options=DestroyConfigureOptions(options);
cristy3ed852e2009-09-05 21:47:34 +0000950 return(status != 0 ? MagickTrue : MagickFalse);
cristy3ed852e2009-09-05 21:47:34 +0000951}