blob: 9db3547a802cafe649c74d901b17bbb145a40915 [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 %
cristyde984cd2013-12-01 14:49:27 +000016% Cristy %
cristy3ed852e2009-09-05 21:47:34 +000017% May 2001 %
18% %
19% %
cristyfe676ee2013-11-18 13:03:38 +000020% Copyright 1999-2014 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" },
cristy7f47ac62013-02-10 14:16:17 +0000131 { "GV", "DOT" },
cristye3e77a12009-10-16 00:47:21 +0000132 { "K25", "DNG" },
133 { "KDC", "DNG" },
134 { "H", "MAGICK" },
135 { "HTM", "HTML" },
136 { "HTTP", "URL" },
dirk853b77b2013-09-21 19:04:42 +0000137 { "HTTPS", "URL" },
cristye3e77a12009-10-16 00:47:21 +0000138 { "ICB", "TGA" },
139 { "ICC", "META" },
140 { "ICM", "META" },
141 { "ICO", "ICON" },
142 { "IMPLICIT", "***" },
143 { "IPTC", "META" },
144 { "IPTCTEXT", "META" },
145 { "IPTCWTEXT", "META" },
146 { "ISOBRL", "BRAILLE" },
147 { "JBG", "JBIG" },
148 { "JNG", "PNG" },
149 { "JPC", "JP2" },
cristy02f1fda2009-12-10 15:23:56 +0000150 { "J2C", "JP2" },
cristy4faac682012-05-10 12:09:45 +0000151 { "J2K", "JP2" },
cristye3e77a12009-10-16 00:47:21 +0000152 { "JPG", "JPEG" },
153 { "JPX", "JP2" },
154 { "K", "RAW" },
155 { "LOGO", "MAGICK" },
156 { "M2V", "MPEG" },
157 { "M4V", "MPEG" },
158 { "M", "RAW" },
159 { "MNG", "PNG" },
cristy1dbd5a32010-11-28 22:23:27 +0000160 { "MOV", "MPEG" },
cristye3e77a12009-10-16 00:47:21 +0000161 { "MP4", "MPEG" },
162 { "MPG", "MPEG" },
163 { "MPRI", "MPR" },
cristy1be98ec2012-01-30 16:51:12 +0000164 { "MEF", "DNG" },
cristye3e77a12009-10-16 00:47:21 +0000165 { "MRW", "DNG" },
166 { "MSVG", "SVG" },
167 { "NEF", "DNG" },
168 { "NETSCAPE", "MAGICK" },
cristycb8f0ae2012-03-02 12:26:21 +0000169 { "NRW", "DNG" },
cristye3e77a12009-10-16 00:47:21 +0000170 { "O", "RAW" },
171 { "ORF", "DNG" },
172 { "OTF", "TTF" },
173 { "P7", "PNM" },
174 { "PAL", "UYVY" },
175 { "PAM", "PNM" },
176 { "PBM", "PNM" },
177 { "PCDS", "PCD" },
178 { "PDFA", "PDF" },
179 { "PEF", "DNG" },
180 { "PEF", "DNG" },
181 { "PFA", "TTF" },
182 { "PFB", "TTF" },
183 { "PFM", "PNM" },
184 { "PGM", "PNM" },
185 { "PGX", "JP2" },
186 { "PICON", "XPM" },
187 { "PJPEG", "JPEG" },
188 { "PM", "XPM" },
189 { "PNG24", "PNG" },
190 { "PNG32", "PNG" },
191 { "PNG8", "PNG" },
192 { "PPM", "PNM" },
cristyb4233012010-02-28 20:09:14 +0000193 { "PSB", "PSD" },
cristye3e77a12009-10-16 00:47:21 +0000194 { "PTIF", "TIFF" },
195 { "RADIAL-GRADIENT", "GRADIENT" },
196 { "RAF", "DNG" },
197 { "RAS", "SUN" },
cristye3e77a12009-10-16 00:47:21 +0000198 { "RGBA", "RGB" },
199 { "RGBO", "RGB" },
cristy15e22962014-02-21 22:56:26 +0000200 { "RMF", "DNG" },
cristye3e77a12009-10-16 00:47:21 +0000201 { "R", "RAW" },
202 { "ROSE", "MAGICK" },
cristy98aceb82012-10-20 21:24:00 +0000203 { "RW2", "DNG" },
cristye3e77a12009-10-16 00:47:21 +0000204 { "SHTML", "HTML" },
cristye6ee7492013-03-16 15:04:42 +0000205 { "SPARSE-COLOR", "TXT" },
cristye3e77a12009-10-16 00:47:21 +0000206 { "SR2", "DNG" },
207 { "SRF", "DNG" },
208 { "SVGZ", "SVG" },
209 { "TEXT", "TXT" },
210 { "TIFF64", "TIFF" },
211 { "TIF", "TIFF" },
212 { "TTC", "TTF" },
213 { "UBRL", "BRAILLE" },
214 { "VDA", "TGA" },
215 { "VST", "TGA" },
cristyed40d622011-01-17 15:51:00 +0000216 { "WIZARD", "MAGICK" },
dirk073ff372014-01-05 14:05:25 +0000217#if defined(MAGICKCORE_WINGDI32_DELEGATE)
218 { "WMF", "EMF" },
219#endif
cristye3e77a12009-10-16 00:47:21 +0000220 { "WMV", "MPEG" },
cristye3e77a12009-10-16 00:47:21 +0000221 { "WMZ", "WMF" },
222 { "X3f", "DNG" },
223 { "XMP", "META" },
224 { "XTRNARRAY", "XTRN" },
225 { "XTRNBLOB", "XTRN" },
cristye3e77a12009-10-16 00:47:21 +0000226 { "XTRNFILE", "XTRN" },
227 { "XTRNIMAGE", "XTRN" },
cristye3e77a12009-10-16 00:47:21 +0000228 { "XV", "VIFF" },
229 { "Y", "RAW" },
cristy54a531d2009-10-21 17:58:01 +0000230 { "YCbCrA", "YCbCr" }
cristye3e77a12009-10-16 00:47:21 +0000231 };
232
cristy3ed852e2009-09-05 21:47:34 +0000233static SemaphoreInfo
234 *coder_semaphore = (SemaphoreInfo *) NULL;
235
236static SplayTreeInfo
237 *coder_list = (SplayTreeInfo *) NULL;
cristy3ed852e2009-09-05 21:47:34 +0000238
239/*
240 Forward declarations.
241*/
242static MagickBooleanType
cristy904e5912014-03-15 19:53:14 +0000243 IsCoderTreeInstantiated(ExceptionInfo *),
cristy3ed852e2009-09-05 21:47:34 +0000244 LoadCoderLists(const char *,ExceptionInfo *);
245
246/*
247%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
248% %
249% %
250% %
cristyf34a1452009-10-24 22:29:27 +0000251+ 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 +0000252% %
253% %
254% %
255%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
256%
cristyf34a1452009-10-24 22:29:27 +0000257% CoderComponentGenesis() instantiates the coder component.
cristy3ed852e2009-09-05 21:47:34 +0000258%
cristyf34a1452009-10-24 22:29:27 +0000259% The format of the CoderComponentGenesis method is:
cristy3ed852e2009-09-05 21:47:34 +0000260%
cristyf34a1452009-10-24 22:29:27 +0000261% MagickBooleanType CoderComponentGenesis(void)
cristy3ed852e2009-09-05 21:47:34 +0000262%
263*/
cristy5ff4eaf2011-09-03 01:38:02 +0000264MagickPrivate MagickBooleanType CoderComponentGenesis(void)
cristyf34a1452009-10-24 22:29:27 +0000265{
cristy3d162a92014-02-16 14:05:06 +0000266 coder_semaphore=AcquireSemaphoreInfo();
cristyf34a1452009-10-24 22:29:27 +0000267 return(MagickTrue);
268}
269
270/*
271%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
272% %
273% %
274% %
275+ C o d e r C o m p o n e n t T e r m i n u s %
276% %
277% %
278% %
279%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
280%
281% CoderComponentTerminus() destroys the coder component.
282%
283% The format of the CoderComponentTerminus method is:
284%
285% CoderComponentTerminus(void)
286%
287*/
cristy5ff4eaf2011-09-03 01:38:02 +0000288MagickPrivate void CoderComponentTerminus(void)
cristy3ed852e2009-09-05 21:47:34 +0000289{
cristy18b17442009-10-25 18:36:48 +0000290 if (coder_semaphore == (SemaphoreInfo *) NULL)
cristy04b11db2014-02-16 15:10:39 +0000291 ActivateSemaphoreInfo(&coder_semaphore);
cristyf84a1932010-01-03 18:00:18 +0000292 LockSemaphoreInfo(coder_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000293 if (coder_list != (SplayTreeInfo *) NULL)
294 coder_list=DestroySplayTree(coder_list);
cristyf84a1932010-01-03 18:00:18 +0000295 UnlockSemaphoreInfo(coder_semaphore);
cristy3d162a92014-02-16 14:05:06 +0000296 RelinquishSemaphoreInfo(&coder_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000297}
298
299/*
300%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
301% %
302% %
303% %
304+ G e t C o d e r I n f o %
305% %
306% %
307% %
308%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
309%
310% GetCoderInfo searches the coder list for the specified name and if found
311% returns attributes for that coder.
312%
313% The format of the GetCoderInfo method is:
314%
315% const CoderInfo *GetCoderInfo(const char *name,ExceptionInfo *exception)
316%
317% A description of each parameter follows:
318%
319% o name: the coder name.
320%
321% o exception: return any errors or warnings in this structure.
322%
323*/
324MagickExport const CoderInfo *GetCoderInfo(const char *name,
325 ExceptionInfo *exception)
326{
cristy49d4d222014-03-16 00:37:58 +0000327 const CoderInfo
328 *coder_info;
329
cristy3ed852e2009-09-05 21:47:34 +0000330 assert(exception != (ExceptionInfo *) NULL);
cristy904e5912014-03-15 19:53:14 +0000331 if (IsCoderTreeInstantiated(exception) == MagickFalse)
332 return((const CoderInfo *) NULL);
cristy49d4d222014-03-16 00:37:58 +0000333 LockSemaphoreInfo(coder_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000334 if ((name == (const char *) NULL) || (LocaleCompare(name,"*") == 0))
335 {
336 ResetSplayTreeIterator(coder_list);
cristy49d4d222014-03-16 00:37:58 +0000337 coder_info=(const CoderInfo *) GetNextValueInSplayTree(coder_list);
338 UnlockSemaphoreInfo(coder_semaphore);
339 return(coder_info);
cristy3ed852e2009-09-05 21:47:34 +0000340 }
cristy49d4d222014-03-16 00:37:58 +0000341 coder_info=(const CoderInfo *) GetValueFromSplayTree(coder_list,name);
342 UnlockSemaphoreInfo(coder_semaphore);
343 return(coder_info);
cristy3ed852e2009-09-05 21:47:34 +0000344}
345
346/*
347%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
348% %
349% %
350% %
351% G e t C o d e r I n f o L i s t %
352% %
353% %
354% %
355%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
356%
357% GetCoderInfoList() returns any coder_map that match the specified pattern.
358% The format of the GetCoderInfoList function is:
359%
360% const CoderInfo **GetCoderInfoList(const char *pattern,
cristybb503372010-05-27 20:51:26 +0000361% size_t *number_coders,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000362%
363% A description of each parameter follows:
364%
365% o pattern: Specifies a pointer to a text string containing a pattern.
366%
367% o number_coders: This integer returns the number of coders in the list.
368%
369% o exception: return any errors or warnings in this structure.
370%
371*/
372
373static int CoderInfoCompare(const void *x,const void *y)
374{
375 const CoderInfo
376 **p,
377 **q;
378
379 p=(const CoderInfo **) x,
380 q=(const CoderInfo **) y;
381 if (LocaleCompare((*p)->path,(*q)->path) == 0)
382 return(LocaleCompare((*p)->name,(*q)->name));
383 return(LocaleCompare((*p)->path,(*q)->path));
384}
385
386MagickExport const CoderInfo **GetCoderInfoList(const char *pattern,
cristybb503372010-05-27 20:51:26 +0000387 size_t *number_coders,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000388{
389 const CoderInfo
390 **coder_map;
391
392 register const CoderInfo
393 *p;
394
cristybb503372010-05-27 20:51:26 +0000395 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000396 i;
397
398 /*
399 Allocate coder list.
400 */
401 assert(pattern != (char *) NULL);
402 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
cristybb503372010-05-27 20:51:26 +0000403 assert(number_coders != (size_t *) NULL);
cristy3ed852e2009-09-05 21:47:34 +0000404 *number_coders=0;
405 p=GetCoderInfo("*",exception);
406 if (p == (const CoderInfo *) NULL)
407 return((const CoderInfo **) NULL);
408 coder_map=(const CoderInfo **) AcquireQuantumMemory((size_t)
409 GetNumberOfNodesInSplayTree(coder_list)+1UL,sizeof(*coder_map));
410 if (coder_map == (const CoderInfo **) NULL)
411 return((const CoderInfo **) NULL);
412 /*
413 Generate coder list.
414 */
cristyf84a1932010-01-03 18:00:18 +0000415 LockSemaphoreInfo(coder_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000416 ResetSplayTreeIterator(coder_list);
417 p=(const CoderInfo *) GetNextValueInSplayTree(coder_list);
418 for (i=0; p != (const CoderInfo *) NULL; )
419 {
420 if ((p->stealth == MagickFalse) &&
421 (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
422 coder_map[i++]=p;
423 p=(const CoderInfo *) GetNextValueInSplayTree(coder_list);
424 }
cristyf84a1932010-01-03 18:00:18 +0000425 UnlockSemaphoreInfo(coder_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000426 qsort((void *) coder_map,(size_t) i,sizeof(*coder_map),CoderInfoCompare);
427 coder_map[i]=(CoderInfo *) NULL;
cristybb503372010-05-27 20:51:26 +0000428 *number_coders=(size_t) i;
cristy3ed852e2009-09-05 21:47:34 +0000429 return(coder_map);
430}
431
432/*
433%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
434% %
435% %
436% %
437% G e t C o d e r L i s t %
438% %
439% %
440% %
441%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
442%
443% GetCoderList() returns any coder_map that match the specified pattern.
444%
445% The format of the GetCoderList function is:
446%
cristybb503372010-05-27 20:51:26 +0000447% char **GetCoderList(const char *pattern,size_t *number_coders,
cristy3ed852e2009-09-05 21:47:34 +0000448% ExceptionInfo *exception)
449%
450% A description of each parameter follows:
451%
452% o pattern: Specifies a pointer to a text string containing a pattern.
453%
454% o number_coders: This integer returns the number of coders in the list.
455%
456% o exception: return any errors or warnings in this structure.
457%
458*/
459
460static int CoderCompare(const void *x,const void *y)
461{
462 register const char
463 **p,
464 **q;
465
466 p=(const char **) x;
467 q=(const char **) y;
468 return(LocaleCompare(*p,*q));
469}
470
471MagickExport char **GetCoderList(const char *pattern,
cristybb503372010-05-27 20:51:26 +0000472 size_t *number_coders,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000473{
474 char
475 **coder_map;
476
477 register const CoderInfo
478 *p;
479
cristybb503372010-05-27 20:51:26 +0000480 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000481 i;
482
483 /*
484 Allocate coder list.
485 */
486 assert(pattern != (char *) NULL);
487 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
cristybb503372010-05-27 20:51:26 +0000488 assert(number_coders != (size_t *) NULL);
cristy3ed852e2009-09-05 21:47:34 +0000489 *number_coders=0;
490 p=GetCoderInfo("*",exception);
491 if (p == (const CoderInfo *) NULL)
492 return((char **) NULL);
493 coder_map=(char **) AcquireQuantumMemory((size_t)
494 GetNumberOfNodesInSplayTree(coder_list)+1UL,sizeof(*coder_map));
495 if (coder_map == (char **) NULL)
496 return((char **) NULL);
497 /*
498 Generate coder list.
499 */
cristyf84a1932010-01-03 18:00:18 +0000500 LockSemaphoreInfo(coder_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000501 ResetSplayTreeIterator(coder_list);
502 p=(const CoderInfo *) GetNextValueInSplayTree(coder_list);
503 for (i=0; p != (const CoderInfo *) NULL; )
504 {
505 if ((p->stealth == MagickFalse) &&
506 (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
507 coder_map[i++]=ConstantString(p->name);
508 p=(const CoderInfo *) GetNextValueInSplayTree(coder_list);
509 }
cristyf84a1932010-01-03 18:00:18 +0000510 UnlockSemaphoreInfo(coder_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000511 qsort((void *) coder_map,(size_t) i,sizeof(*coder_map),CoderCompare);
512 coder_map[i]=(char *) NULL;
cristybb503372010-05-27 20:51:26 +0000513 *number_coders=(size_t) i;
cristy3ed852e2009-09-05 21:47:34 +0000514 return(coder_map);
515}
516
517/*
518%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
519% %
520% %
521% %
cristy904e5912014-03-15 19:53:14 +0000522+ I s C o d e r T r e e I n s t a n t i a t e d %
cristy3ed852e2009-09-05 21:47:34 +0000523% %
524% %
525% %
526%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
527%
cristy904e5912014-03-15 19:53:14 +0000528% IsCoderTreeInstantiated() determines if the coder tree is instantiated.
529% If not, it instantiates the tree and returns it.
cristy3ed852e2009-09-05 21:47:34 +0000530%
cristy904e5912014-03-15 19:53:14 +0000531% The format of the IsCoderInstantiated method is:
cristy3ed852e2009-09-05 21:47:34 +0000532%
cristy904e5912014-03-15 19:53:14 +0000533% MagickBooleanType IsCoderTreeInstantiated(ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000534%
535% A description of each parameter follows.
536%
537% o exception: return any errors or warnings in this structure.
538%
539*/
cristy904e5912014-03-15 19:53:14 +0000540static MagickBooleanType IsCoderTreeInstantiated(ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000541{
cristy1460f262014-03-15 17:38:05 +0000542 if (coder_semaphore == (SemaphoreInfo *) NULL)
543 ActivateSemaphoreInfo(&coder_semaphore);
544 LockSemaphoreInfo(coder_semaphore);
545 if (coder_list == (SplayTreeInfo *) NULL)
546 (void) LoadCoderLists(MagickCoderFilename,exception);
547 UnlockSemaphoreInfo(coder_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000548 return(coder_list != (SplayTreeInfo *) NULL ? MagickTrue : MagickFalse);
549}
550
551/*
552%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
553% %
554% %
555% %
556% L i s t C o d e r I n f o %
557% %
558% %
559% %
560%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
561%
562% ListCoderInfo() lists the coder info to a file.
563%
564% The format of the ListCoderInfo coder is:
565%
566% MagickBooleanType ListCoderInfo(FILE *file,ExceptionInfo *exception)
567%
568% A description of each parameter follows.
569%
570% o file: An pointer to a FILE.
571%
572% o exception: return any errors or warnings in this structure.
573%
574*/
575MagickExport MagickBooleanType ListCoderInfo(FILE *file,
576 ExceptionInfo *exception)
577{
578 const char
579 *path;
580
581 const CoderInfo
582 **coder_info;
583
cristybb503372010-05-27 20:51:26 +0000584 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000585 i;
586
cristybb503372010-05-27 20:51:26 +0000587 size_t
cristy3ed852e2009-09-05 21:47:34 +0000588 number_coders;
589
cristy9d314ff2011-03-09 01:30:28 +0000590 ssize_t
591 j;
592
cristy3ed852e2009-09-05 21:47:34 +0000593 if (file == (const FILE *) NULL)
594 file=stdout;
595 coder_info=GetCoderInfoList("*",&number_coders,exception);
596 if (coder_info == (const CoderInfo **) NULL)
597 return(MagickFalse);
598 path=(const char *) NULL;
cristybb503372010-05-27 20:51:26 +0000599 for (i=0; i < (ssize_t) number_coders; i++)
cristy3ed852e2009-09-05 21:47:34 +0000600 {
601 if (coder_info[i]->stealth != MagickFalse)
602 continue;
603 if ((path == (const char *) NULL) ||
604 (LocaleCompare(path,coder_info[i]->path) != 0))
605 {
606 if (coder_info[i]->path != (char *) NULL)
cristyb51dff52011-05-19 16:55:47 +0000607 (void) FormatLocaleFile(file,"\nPath: %s\n\n",coder_info[i]->path);
608 (void) FormatLocaleFile(file,"Magick Coder\n");
cristy1e604812011-05-19 18:07:50 +0000609 (void) FormatLocaleFile(file,
610 "-------------------------------------------------"
cristy3ed852e2009-09-05 21:47:34 +0000611 "------------------------------\n");
612 }
613 path=coder_info[i]->path;
cristyb51dff52011-05-19 16:55:47 +0000614 (void) FormatLocaleFile(file,"%s",coder_info[i]->magick);
cristybb503372010-05-27 20:51:26 +0000615 for (j=(ssize_t) strlen(coder_info[i]->magick); j <= 11; j++)
cristyb51dff52011-05-19 16:55:47 +0000616 (void) FormatLocaleFile(file," ");
cristy3ed852e2009-09-05 21:47:34 +0000617 if (coder_info[i]->name != (char *) NULL)
cristyb51dff52011-05-19 16:55:47 +0000618 (void) FormatLocaleFile(file,"%s",coder_info[i]->name);
619 (void) FormatLocaleFile(file,"\n");
cristy3ed852e2009-09-05 21:47:34 +0000620 }
621 coder_info=(const CoderInfo **) RelinquishMagickMemory((void *) coder_info);
622 (void) fflush(file);
623 return(MagickTrue);
624}
625
626/*
627%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
628% %
629% %
630% %
631+ L o a d C o d e r L i s t %
632% %
633% %
634% %
635%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
636%
637% LoadCoderList() loads the coder configuration file which provides a
638% mapping between coder attributes and a coder name.
639%
640% The format of the LoadCoderList coder is:
641%
642% MagickBooleanType LoadCoderList(const char *xml,const char *filename,
cristybb503372010-05-27 20:51:26 +0000643% const size_t depth,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000644%
645% A description of each parameter follows:
646%
647% o xml: The coder list in XML format.
648%
649% o filename: The coder list filename.
650%
651% o depth: depth of <include /> statements.
652%
653% o exception: return any errors or warnings in this structure.
654%
655*/
656
657static void *DestroyCoderNode(void *coder_info)
658{
659 register CoderInfo
660 *p;
661
662 p=(CoderInfo *) coder_info;
cristye3e77a12009-10-16 00:47:21 +0000663 if (p->exempt == MagickFalse)
664 {
665 if (p->path != (char *) NULL)
666 p->path=DestroyString(p->path);
667 if (p->name != (char *) NULL)
668 p->name=DestroyString(p->name);
669 if (p->magick != (char *) NULL)
670 p->magick=DestroyString(p->magick);
671 }
cristy3ed852e2009-09-05 21:47:34 +0000672 return(RelinquishMagickMemory(p));
673}
674
675static MagickBooleanType LoadCoderList(const char *xml,const char *filename,
cristybb503372010-05-27 20:51:26 +0000676 const size_t depth,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000677{
678 char
679 keyword[MaxTextExtent],
680 *token;
681
682 const char
683 *q;
684
685 CoderInfo
686 *coder_info;
687
688 MagickBooleanType
689 status;
690
691 /*
692 Load the coder map file.
693 */
694 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
695 "Loading coder configuration file \"%s\" ...",filename);
696 if (xml == (const char *) NULL)
697 return(MagickFalse);
698 if (coder_list == (SplayTreeInfo *) NULL)
699 {
700 coder_list=NewSplayTree(CompareSplayTreeString,RelinquishMagickMemory,
701 DestroyCoderNode);
702 if (coder_list == (SplayTreeInfo *) NULL)
703 {
704 ThrowFileException(exception,ResourceLimitError,
705 "MemoryAllocationFailed",filename);
706 return(MagickFalse);
707 }
708 }
709 status=MagickTrue;
710 coder_info=(CoderInfo *) NULL;
711 token=AcquireString(xml);
712 for (q=(char *) xml; *q != '\0'; )
713 {
714 /*
715 Interpret XML.
716 */
717 GetMagickToken(q,&q,token);
718 if (*token == '\0')
719 break;
720 (void) CopyMagickString(keyword,token,MaxTextExtent);
721 if (LocaleNCompare(keyword,"<!DOCTYPE",9) == 0)
722 {
723 /*
724 Doctype element.
725 */
726 while ((LocaleNCompare(q,"]>",2) != 0) && (*q != '\0'))
727 GetMagickToken(q,&q,token);
728 continue;
729 }
730 if (LocaleNCompare(keyword,"<!--",4) == 0)
731 {
732 /*
733 Comment element.
734 */
735 while ((LocaleNCompare(q,"->",2) != 0) && (*q != '\0'))
736 GetMagickToken(q,&q,token);
737 continue;
738 }
739 if (LocaleCompare(keyword,"<include") == 0)
740 {
741 /*
742 Include element.
743 */
744 while (((*token != '/') && (*(token+1) != '>')) && (*q != '\0'))
745 {
746 (void) CopyMagickString(keyword,token,MaxTextExtent);
747 GetMagickToken(q,&q,token);
748 if (*token != '=')
749 continue;
750 GetMagickToken(q,&q,token);
751 if (LocaleCompare(keyword,"file") == 0)
752 {
753 if (depth > 200)
754 (void) ThrowMagickException(exception,GetMagickModule(),
cristyefe601c2013-01-05 17:51:12 +0000755 ConfigureError,"IncludeNodeNestedTooDeeply","`%s'",token);
cristy3ed852e2009-09-05 21:47:34 +0000756 else
757 {
758 char
759 path[MaxTextExtent],
760 *xml;
761
762 GetPathComponent(filename,HeadPath,path);
763 if (*path != '\0')
764 (void) ConcatenateMagickString(path,DirectorySeparator,
765 MaxTextExtent);
766 if (*token == *DirectorySeparator)
767 (void) CopyMagickString(path,token,MaxTextExtent);
768 else
769 (void) ConcatenateMagickString(path,token,MaxTextExtent);
cristy3a5987c2013-11-07 14:18:46 +0000770 xml=FileToString(path,~0UL,exception);
cristy3ed852e2009-09-05 21:47:34 +0000771 if (xml != (char *) NULL)
772 {
773 status=LoadCoderList(xml,path,depth+1,exception);
774 xml=(char *) RelinquishMagickMemory(xml);
775 }
776 }
777 }
778 }
779 continue;
780 }
781 if (LocaleCompare(keyword,"<coder") == 0)
782 {
783 /*
784 Coder element.
785 */
cristy73bd4a52010-10-05 11:24:23 +0000786 coder_info=(CoderInfo *) AcquireMagickMemory(sizeof(*coder_info));
cristy3ed852e2009-09-05 21:47:34 +0000787 if (coder_info == (CoderInfo *) NULL)
788 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
789 (void) ResetMagickMemory(coder_info,0,sizeof(*coder_info));
790 coder_info->path=ConstantString(filename);
cristye3e77a12009-10-16 00:47:21 +0000791 coder_info->exempt=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +0000792 coder_info->signature=MagickSignature;
793 continue;
794 }
795 if (coder_info == (CoderInfo *) NULL)
796 continue;
797 if (LocaleCompare(keyword,"/>") == 0)
798 {
799 status=AddValueToSplayTree(coder_list,ConstantString(
800 coder_info->magick),coder_info);
801 if (status == MagickFalse)
802 (void) ThrowMagickException(exception,GetMagickModule(),
cristyefe601c2013-01-05 17:51:12 +0000803 ResourceLimitError,"MemoryAllocationFailed","`%s'",
cristy3ed852e2009-09-05 21:47:34 +0000804 coder_info->magick);
805 coder_info=(CoderInfo *) NULL;
cristyd45122f2014-01-14 23:46:16 +0000806 continue;
cristy3ed852e2009-09-05 21:47:34 +0000807 }
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 /*
cristy398fd022013-06-26 23:08:33 +0000895 Load external coder map.
cristye3e77a12009-10-16 00:47:21 +0000896 */
cristye3e77a12009-10-16 00:47:21 +0000897 if (coder_list == (SplayTreeInfo *) NULL)
898 {
899 coder_list=NewSplayTree(CompareSplayTreeString,RelinquishMagickMemory,
900 DestroyCoderNode);
901 if (coder_list == (SplayTreeInfo *) NULL)
902 {
903 ThrowFileException(exception,ResourceLimitError,
904 "MemoryAllocationFailed",filename);
905 return(MagickFalse);
906 }
907 }
cristy398fd022013-06-26 23:08:33 +0000908 status=MagickTrue;
909 options=GetConfigureOptions(filename,exception);
910 option=(const StringInfo *) GetNextValueInLinkedList(options);
911 while (option != (const StringInfo *) NULL)
912 {
cristyaae13f62013-08-15 14:41:32 +0000913 status&=LoadCoderList((const char *) GetStringInfoDatum(option),
cristy398fd022013-06-26 23:08:33 +0000914 GetStringInfoPath(option),0,exception);
915 option=(const StringInfo *) GetNextValueInLinkedList(options);
916 }
917 options=DestroyConfigureOptions(options);
918 /*
919 Load built-in coder map.
920 */
cristybb503372010-05-27 20:51:26 +0000921 for (i=0; i < (ssize_t) (sizeof(CoderMap)/sizeof(*CoderMap)); i++)
cristye3e77a12009-10-16 00:47:21 +0000922 {
923 CoderInfo
924 *coder_info;
925
cristy54a531d2009-10-21 17:58:01 +0000926 register const CoderMapInfo
927 *p;
928
929 p=CoderMap+i;
cristy73bd4a52010-10-05 11:24:23 +0000930 coder_info=(CoderInfo *) AcquireMagickMemory(sizeof(*coder_info));
cristye3e77a12009-10-16 00:47:21 +0000931 if (coder_info == (CoderInfo *) NULL)
932 {
933 (void) ThrowMagickException(exception,GetMagickModule(),
cristyd45122f2014-01-14 23:46:16 +0000934 ResourceLimitError,"MemoryAllocationFailed","`%s'",p->name);
cristye3e77a12009-10-16 00:47:21 +0000935 continue;
936 }
937 (void) ResetMagickMemory(coder_info,0,sizeof(*coder_info));
cristy38b8ed92009-10-16 01:20:21 +0000938 coder_info->path=(char *) "[built-in]";
cristye3e77a12009-10-16 00:47:21 +0000939 coder_info->magick=(char *) p->magick;
940 coder_info->name=(char *) p->name;
941 coder_info->exempt=MagickTrue;
942 coder_info->signature=MagickSignature;
cristyaae13f62013-08-15 14:41:32 +0000943 status&=AddValueToSplayTree(coder_list,ConstantString(coder_info->magick),
cristye3e77a12009-10-16 00:47:21 +0000944 coder_info);
945 if (status == MagickFalse)
946 (void) ThrowMagickException(exception,GetMagickModule(),
cristyefe601c2013-01-05 17:51:12 +0000947 ResourceLimitError,"MemoryAllocationFailed","`%s'",coder_info->name);
cristye3e77a12009-10-16 00:47:21 +0000948 }
cristy3ed852e2009-09-05 21:47:34 +0000949 return(status != 0 ? MagickTrue : MagickFalse);
cristy3ed852e2009-09-05 21:47:34 +0000950}