blob: f4def8f17ae6dbaf2a75530268c800c89b0d9724 [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{
327 assert(exception != (ExceptionInfo *) NULL);
cristy904e5912014-03-15 19:53:14 +0000328 if (IsCoderTreeInstantiated(exception) == MagickFalse)
329 return((const CoderInfo *) NULL);
cristy3ed852e2009-09-05 21:47:34 +0000330 if ((name == (const char *) NULL) || (LocaleCompare(name,"*") == 0))
331 {
332 ResetSplayTreeIterator(coder_list);
333 return((const CoderInfo *) GetNextValueInSplayTree(coder_list));
334 }
335 return((const CoderInfo *) GetValueFromSplayTree(coder_list,name));
336}
337
338/*
339%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
340% %
341% %
342% %
343% G e t C o d e r I n f o L i s t %
344% %
345% %
346% %
347%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
348%
349% GetCoderInfoList() returns any coder_map that match the specified pattern.
350% The format of the GetCoderInfoList function is:
351%
352% const CoderInfo **GetCoderInfoList(const char *pattern,
cristybb503372010-05-27 20:51:26 +0000353% size_t *number_coders,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000354%
355% A description of each parameter follows:
356%
357% o pattern: Specifies a pointer to a text string containing a pattern.
358%
359% o number_coders: This integer returns the number of coders in the list.
360%
361% o exception: return any errors or warnings in this structure.
362%
363*/
364
365static int CoderInfoCompare(const void *x,const void *y)
366{
367 const CoderInfo
368 **p,
369 **q;
370
371 p=(const CoderInfo **) x,
372 q=(const CoderInfo **) y;
373 if (LocaleCompare((*p)->path,(*q)->path) == 0)
374 return(LocaleCompare((*p)->name,(*q)->name));
375 return(LocaleCompare((*p)->path,(*q)->path));
376}
377
378MagickExport const CoderInfo **GetCoderInfoList(const char *pattern,
cristybb503372010-05-27 20:51:26 +0000379 size_t *number_coders,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000380{
381 const CoderInfo
382 **coder_map;
383
384 register const CoderInfo
385 *p;
386
cristybb503372010-05-27 20:51:26 +0000387 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000388 i;
389
390 /*
391 Allocate coder list.
392 */
393 assert(pattern != (char *) NULL);
394 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
cristybb503372010-05-27 20:51:26 +0000395 assert(number_coders != (size_t *) NULL);
cristy3ed852e2009-09-05 21:47:34 +0000396 *number_coders=0;
397 p=GetCoderInfo("*",exception);
398 if (p == (const CoderInfo *) NULL)
399 return((const CoderInfo **) NULL);
400 coder_map=(const CoderInfo **) AcquireQuantumMemory((size_t)
401 GetNumberOfNodesInSplayTree(coder_list)+1UL,sizeof(*coder_map));
402 if (coder_map == (const CoderInfo **) NULL)
403 return((const CoderInfo **) NULL);
404 /*
405 Generate coder list.
406 */
cristyf84a1932010-01-03 18:00:18 +0000407 LockSemaphoreInfo(coder_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000408 ResetSplayTreeIterator(coder_list);
409 p=(const CoderInfo *) GetNextValueInSplayTree(coder_list);
410 for (i=0; p != (const CoderInfo *) NULL; )
411 {
412 if ((p->stealth == MagickFalse) &&
413 (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
414 coder_map[i++]=p;
415 p=(const CoderInfo *) GetNextValueInSplayTree(coder_list);
416 }
cristyf84a1932010-01-03 18:00:18 +0000417 UnlockSemaphoreInfo(coder_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000418 qsort((void *) coder_map,(size_t) i,sizeof(*coder_map),CoderInfoCompare);
419 coder_map[i]=(CoderInfo *) NULL;
cristybb503372010-05-27 20:51:26 +0000420 *number_coders=(size_t) i;
cristy3ed852e2009-09-05 21:47:34 +0000421 return(coder_map);
422}
423
424/*
425%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
426% %
427% %
428% %
429% G e t C o d e r L i s t %
430% %
431% %
432% %
433%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
434%
435% GetCoderList() returns any coder_map that match the specified pattern.
436%
437% The format of the GetCoderList function is:
438%
cristybb503372010-05-27 20:51:26 +0000439% char **GetCoderList(const char *pattern,size_t *number_coders,
cristy3ed852e2009-09-05 21:47:34 +0000440% ExceptionInfo *exception)
441%
442% A description of each parameter follows:
443%
444% o pattern: Specifies a pointer to a text string containing a pattern.
445%
446% o number_coders: This integer returns the number of coders in the list.
447%
448% o exception: return any errors or warnings in this structure.
449%
450*/
451
452static int CoderCompare(const void *x,const void *y)
453{
454 register const char
455 **p,
456 **q;
457
458 p=(const char **) x;
459 q=(const char **) y;
460 return(LocaleCompare(*p,*q));
461}
462
463MagickExport char **GetCoderList(const char *pattern,
cristybb503372010-05-27 20:51:26 +0000464 size_t *number_coders,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000465{
466 char
467 **coder_map;
468
469 register const CoderInfo
470 *p;
471
cristybb503372010-05-27 20:51:26 +0000472 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000473 i;
474
475 /*
476 Allocate coder list.
477 */
478 assert(pattern != (char *) NULL);
479 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
cristybb503372010-05-27 20:51:26 +0000480 assert(number_coders != (size_t *) NULL);
cristy3ed852e2009-09-05 21:47:34 +0000481 *number_coders=0;
482 p=GetCoderInfo("*",exception);
483 if (p == (const CoderInfo *) NULL)
484 return((char **) NULL);
485 coder_map=(char **) AcquireQuantumMemory((size_t)
486 GetNumberOfNodesInSplayTree(coder_list)+1UL,sizeof(*coder_map));
487 if (coder_map == (char **) NULL)
488 return((char **) NULL);
489 /*
490 Generate coder list.
491 */
cristyf84a1932010-01-03 18:00:18 +0000492 LockSemaphoreInfo(coder_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000493 ResetSplayTreeIterator(coder_list);
494 p=(const CoderInfo *) GetNextValueInSplayTree(coder_list);
495 for (i=0; p != (const CoderInfo *) NULL; )
496 {
497 if ((p->stealth == MagickFalse) &&
498 (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
499 coder_map[i++]=ConstantString(p->name);
500 p=(const CoderInfo *) GetNextValueInSplayTree(coder_list);
501 }
cristyf84a1932010-01-03 18:00:18 +0000502 UnlockSemaphoreInfo(coder_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000503 qsort((void *) coder_map,(size_t) i,sizeof(*coder_map),CoderCompare);
504 coder_map[i]=(char *) NULL;
cristybb503372010-05-27 20:51:26 +0000505 *number_coders=(size_t) i;
cristy3ed852e2009-09-05 21:47:34 +0000506 return(coder_map);
507}
508
509/*
510%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
511% %
512% %
513% %
cristy904e5912014-03-15 19:53:14 +0000514+ 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 +0000515% %
516% %
517% %
518%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
519%
cristy904e5912014-03-15 19:53:14 +0000520% IsCoderTreeInstantiated() determines if the coder tree is instantiated.
521% If not, it instantiates the tree and returns it.
cristy3ed852e2009-09-05 21:47:34 +0000522%
cristy904e5912014-03-15 19:53:14 +0000523% The format of the IsCoderInstantiated method is:
cristy3ed852e2009-09-05 21:47:34 +0000524%
cristy904e5912014-03-15 19:53:14 +0000525% MagickBooleanType IsCoderTreeInstantiated(ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000526%
527% A description of each parameter follows.
528%
529% o exception: return any errors or warnings in this structure.
530%
531*/
cristy904e5912014-03-15 19:53:14 +0000532static MagickBooleanType IsCoderTreeInstantiated(ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000533{
cristy1460f262014-03-15 17:38:05 +0000534 if (coder_semaphore == (SemaphoreInfo *) NULL)
535 ActivateSemaphoreInfo(&coder_semaphore);
536 LockSemaphoreInfo(coder_semaphore);
537 if (coder_list == (SplayTreeInfo *) NULL)
538 (void) LoadCoderLists(MagickCoderFilename,exception);
539 UnlockSemaphoreInfo(coder_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000540 return(coder_list != (SplayTreeInfo *) NULL ? MagickTrue : MagickFalse);
541}
542
543/*
544%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
545% %
546% %
547% %
548% L i s t C o d e r I n f o %
549% %
550% %
551% %
552%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
553%
554% ListCoderInfo() lists the coder info to a file.
555%
556% The format of the ListCoderInfo coder is:
557%
558% MagickBooleanType ListCoderInfo(FILE *file,ExceptionInfo *exception)
559%
560% A description of each parameter follows.
561%
562% o file: An pointer to a FILE.
563%
564% o exception: return any errors or warnings in this structure.
565%
566*/
567MagickExport MagickBooleanType ListCoderInfo(FILE *file,
568 ExceptionInfo *exception)
569{
570 const char
571 *path;
572
573 const CoderInfo
574 **coder_info;
575
cristybb503372010-05-27 20:51:26 +0000576 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000577 i;
578
cristybb503372010-05-27 20:51:26 +0000579 size_t
cristy3ed852e2009-09-05 21:47:34 +0000580 number_coders;
581
cristy9d314ff2011-03-09 01:30:28 +0000582 ssize_t
583 j;
584
cristy3ed852e2009-09-05 21:47:34 +0000585 if (file == (const FILE *) NULL)
586 file=stdout;
587 coder_info=GetCoderInfoList("*",&number_coders,exception);
588 if (coder_info == (const CoderInfo **) NULL)
589 return(MagickFalse);
590 path=(const char *) NULL;
cristybb503372010-05-27 20:51:26 +0000591 for (i=0; i < (ssize_t) number_coders; i++)
cristy3ed852e2009-09-05 21:47:34 +0000592 {
593 if (coder_info[i]->stealth != MagickFalse)
594 continue;
595 if ((path == (const char *) NULL) ||
596 (LocaleCompare(path,coder_info[i]->path) != 0))
597 {
598 if (coder_info[i]->path != (char *) NULL)
cristyb51dff52011-05-19 16:55:47 +0000599 (void) FormatLocaleFile(file,"\nPath: %s\n\n",coder_info[i]->path);
600 (void) FormatLocaleFile(file,"Magick Coder\n");
cristy1e604812011-05-19 18:07:50 +0000601 (void) FormatLocaleFile(file,
602 "-------------------------------------------------"
cristy3ed852e2009-09-05 21:47:34 +0000603 "------------------------------\n");
604 }
605 path=coder_info[i]->path;
cristyb51dff52011-05-19 16:55:47 +0000606 (void) FormatLocaleFile(file,"%s",coder_info[i]->magick);
cristybb503372010-05-27 20:51:26 +0000607 for (j=(ssize_t) strlen(coder_info[i]->magick); j <= 11; j++)
cristyb51dff52011-05-19 16:55:47 +0000608 (void) FormatLocaleFile(file," ");
cristy3ed852e2009-09-05 21:47:34 +0000609 if (coder_info[i]->name != (char *) NULL)
cristyb51dff52011-05-19 16:55:47 +0000610 (void) FormatLocaleFile(file,"%s",coder_info[i]->name);
611 (void) FormatLocaleFile(file,"\n");
cristy3ed852e2009-09-05 21:47:34 +0000612 }
613 coder_info=(const CoderInfo **) RelinquishMagickMemory((void *) coder_info);
614 (void) fflush(file);
615 return(MagickTrue);
616}
617
618/*
619%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
620% %
621% %
622% %
623+ L o a d C o d e r L i s t %
624% %
625% %
626% %
627%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
628%
629% LoadCoderList() loads the coder configuration file which provides a
630% mapping between coder attributes and a coder name.
631%
632% The format of the LoadCoderList coder is:
633%
634% MagickBooleanType LoadCoderList(const char *xml,const char *filename,
cristybb503372010-05-27 20:51:26 +0000635% const size_t depth,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000636%
637% A description of each parameter follows:
638%
639% o xml: The coder list in XML format.
640%
641% o filename: The coder list filename.
642%
643% o depth: depth of <include /> statements.
644%
645% o exception: return any errors or warnings in this structure.
646%
647*/
648
649static void *DestroyCoderNode(void *coder_info)
650{
651 register CoderInfo
652 *p;
653
654 p=(CoderInfo *) coder_info;
cristye3e77a12009-10-16 00:47:21 +0000655 if (p->exempt == MagickFalse)
656 {
657 if (p->path != (char *) NULL)
658 p->path=DestroyString(p->path);
659 if (p->name != (char *) NULL)
660 p->name=DestroyString(p->name);
661 if (p->magick != (char *) NULL)
662 p->magick=DestroyString(p->magick);
663 }
cristy3ed852e2009-09-05 21:47:34 +0000664 return(RelinquishMagickMemory(p));
665}
666
667static MagickBooleanType LoadCoderList(const char *xml,const char *filename,
cristybb503372010-05-27 20:51:26 +0000668 const size_t depth,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000669{
670 char
671 keyword[MaxTextExtent],
672 *token;
673
674 const char
675 *q;
676
677 CoderInfo
678 *coder_info;
679
680 MagickBooleanType
681 status;
682
683 /*
684 Load the coder map file.
685 */
686 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
687 "Loading coder configuration file \"%s\" ...",filename);
688 if (xml == (const char *) NULL)
689 return(MagickFalse);
690 if (coder_list == (SplayTreeInfo *) NULL)
691 {
692 coder_list=NewSplayTree(CompareSplayTreeString,RelinquishMagickMemory,
693 DestroyCoderNode);
694 if (coder_list == (SplayTreeInfo *) NULL)
695 {
696 ThrowFileException(exception,ResourceLimitError,
697 "MemoryAllocationFailed",filename);
698 return(MagickFalse);
699 }
700 }
701 status=MagickTrue;
702 coder_info=(CoderInfo *) NULL;
703 token=AcquireString(xml);
704 for (q=(char *) xml; *q != '\0'; )
705 {
706 /*
707 Interpret XML.
708 */
709 GetMagickToken(q,&q,token);
710 if (*token == '\0')
711 break;
712 (void) CopyMagickString(keyword,token,MaxTextExtent);
713 if (LocaleNCompare(keyword,"<!DOCTYPE",9) == 0)
714 {
715 /*
716 Doctype element.
717 */
718 while ((LocaleNCompare(q,"]>",2) != 0) && (*q != '\0'))
719 GetMagickToken(q,&q,token);
720 continue;
721 }
722 if (LocaleNCompare(keyword,"<!--",4) == 0)
723 {
724 /*
725 Comment element.
726 */
727 while ((LocaleNCompare(q,"->",2) != 0) && (*q != '\0'))
728 GetMagickToken(q,&q,token);
729 continue;
730 }
731 if (LocaleCompare(keyword,"<include") == 0)
732 {
733 /*
734 Include element.
735 */
736 while (((*token != '/') && (*(token+1) != '>')) && (*q != '\0'))
737 {
738 (void) CopyMagickString(keyword,token,MaxTextExtent);
739 GetMagickToken(q,&q,token);
740 if (*token != '=')
741 continue;
742 GetMagickToken(q,&q,token);
743 if (LocaleCompare(keyword,"file") == 0)
744 {
745 if (depth > 200)
746 (void) ThrowMagickException(exception,GetMagickModule(),
cristyefe601c2013-01-05 17:51:12 +0000747 ConfigureError,"IncludeNodeNestedTooDeeply","`%s'",token);
cristy3ed852e2009-09-05 21:47:34 +0000748 else
749 {
750 char
751 path[MaxTextExtent],
752 *xml;
753
754 GetPathComponent(filename,HeadPath,path);
755 if (*path != '\0')
756 (void) ConcatenateMagickString(path,DirectorySeparator,
757 MaxTextExtent);
758 if (*token == *DirectorySeparator)
759 (void) CopyMagickString(path,token,MaxTextExtent);
760 else
761 (void) ConcatenateMagickString(path,token,MaxTextExtent);
cristy3a5987c2013-11-07 14:18:46 +0000762 xml=FileToString(path,~0UL,exception);
cristy3ed852e2009-09-05 21:47:34 +0000763 if (xml != (char *) NULL)
764 {
765 status=LoadCoderList(xml,path,depth+1,exception);
766 xml=(char *) RelinquishMagickMemory(xml);
767 }
768 }
769 }
770 }
771 continue;
772 }
773 if (LocaleCompare(keyword,"<coder") == 0)
774 {
775 /*
776 Coder element.
777 */
cristy73bd4a52010-10-05 11:24:23 +0000778 coder_info=(CoderInfo *) AcquireMagickMemory(sizeof(*coder_info));
cristy3ed852e2009-09-05 21:47:34 +0000779 if (coder_info == (CoderInfo *) NULL)
780 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
781 (void) ResetMagickMemory(coder_info,0,sizeof(*coder_info));
782 coder_info->path=ConstantString(filename);
cristye3e77a12009-10-16 00:47:21 +0000783 coder_info->exempt=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +0000784 coder_info->signature=MagickSignature;
785 continue;
786 }
787 if (coder_info == (CoderInfo *) NULL)
788 continue;
789 if (LocaleCompare(keyword,"/>") == 0)
790 {
791 status=AddValueToSplayTree(coder_list,ConstantString(
792 coder_info->magick),coder_info);
793 if (status == MagickFalse)
794 (void) ThrowMagickException(exception,GetMagickModule(),
cristyefe601c2013-01-05 17:51:12 +0000795 ResourceLimitError,"MemoryAllocationFailed","`%s'",
cristy3ed852e2009-09-05 21:47:34 +0000796 coder_info->magick);
797 coder_info=(CoderInfo *) NULL;
cristyd45122f2014-01-14 23:46:16 +0000798 continue;
cristy3ed852e2009-09-05 21:47:34 +0000799 }
800 GetMagickToken(q,(const char **) NULL,token);
801 if (*token != '=')
802 continue;
803 GetMagickToken(q,&q,token);
804 GetMagickToken(q,&q,token);
805 switch (*keyword)
806 {
807 case 'M':
808 case 'm':
809 {
810 if (LocaleCompare((char *) keyword,"magick") == 0)
811 {
812 coder_info->magick=ConstantString(token);
813 break;
814 }
815 break;
816 }
817 case 'N':
818 case 'n':
819 {
820 if (LocaleCompare((char *) keyword,"name") == 0)
821 {
822 coder_info->name=ConstantString(token);
823 break;
824 }
825 break;
826 }
827 case 'S':
828 case 's':
829 {
830 if (LocaleCompare((char *) keyword,"stealth") == 0)
831 {
anthony6f201312012-03-30 04:08:15 +0000832 coder_info->stealth=IsStringTrue(token);
cristy3ed852e2009-09-05 21:47:34 +0000833 break;
834 }
835 break;
836 }
837 default:
838 break;
839 }
840 }
841 token=(char *) RelinquishMagickMemory(token);
842 return(status);
843}
844
845/*
846%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
847% %
848% %
849% %
850% L o a d C o d e r L i s t s %
851% %
852% %
853% %
854%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
855%
856% LoadCoderLists() loads one or more coder configuration file which
857% provides a mapping between coder attributes and a coder name.
858%
859% The format of the LoadCoderLists coder is:
860%
861% MagickBooleanType LoadCoderLists(const char *filename,
862% ExceptionInfo *exception)
863%
864% A description of each parameter follows:
865%
866% o filename: the font file name.
867%
868% o exception: return any errors or warnings in this structure.
869%
870*/
871static MagickBooleanType LoadCoderLists(const char *filename,
872 ExceptionInfo *exception)
873{
cristy3ed852e2009-09-05 21:47:34 +0000874 const StringInfo
875 *option;
876
877 LinkedListInfo
878 *options;
879
880 MagickStatusType
881 status;
882
cristybb503372010-05-27 20:51:26 +0000883 register ssize_t
cristy54a531d2009-10-21 17:58:01 +0000884 i;
cristye3e77a12009-10-16 00:47:21 +0000885
886 /*
cristy398fd022013-06-26 23:08:33 +0000887 Load external coder map.
cristye3e77a12009-10-16 00:47:21 +0000888 */
cristye3e77a12009-10-16 00:47:21 +0000889 if (coder_list == (SplayTreeInfo *) NULL)
890 {
891 coder_list=NewSplayTree(CompareSplayTreeString,RelinquishMagickMemory,
892 DestroyCoderNode);
893 if (coder_list == (SplayTreeInfo *) NULL)
894 {
895 ThrowFileException(exception,ResourceLimitError,
896 "MemoryAllocationFailed",filename);
897 return(MagickFalse);
898 }
899 }
cristy398fd022013-06-26 23:08:33 +0000900 status=MagickTrue;
901 options=GetConfigureOptions(filename,exception);
902 option=(const StringInfo *) GetNextValueInLinkedList(options);
903 while (option != (const StringInfo *) NULL)
904 {
cristyaae13f62013-08-15 14:41:32 +0000905 status&=LoadCoderList((const char *) GetStringInfoDatum(option),
cristy398fd022013-06-26 23:08:33 +0000906 GetStringInfoPath(option),0,exception);
907 option=(const StringInfo *) GetNextValueInLinkedList(options);
908 }
909 options=DestroyConfigureOptions(options);
910 /*
911 Load built-in coder map.
912 */
cristybb503372010-05-27 20:51:26 +0000913 for (i=0; i < (ssize_t) (sizeof(CoderMap)/sizeof(*CoderMap)); i++)
cristye3e77a12009-10-16 00:47:21 +0000914 {
915 CoderInfo
916 *coder_info;
917
cristy54a531d2009-10-21 17:58:01 +0000918 register const CoderMapInfo
919 *p;
920
921 p=CoderMap+i;
cristy73bd4a52010-10-05 11:24:23 +0000922 coder_info=(CoderInfo *) AcquireMagickMemory(sizeof(*coder_info));
cristye3e77a12009-10-16 00:47:21 +0000923 if (coder_info == (CoderInfo *) NULL)
924 {
925 (void) ThrowMagickException(exception,GetMagickModule(),
cristyd45122f2014-01-14 23:46:16 +0000926 ResourceLimitError,"MemoryAllocationFailed","`%s'",p->name);
cristye3e77a12009-10-16 00:47:21 +0000927 continue;
928 }
929 (void) ResetMagickMemory(coder_info,0,sizeof(*coder_info));
cristy38b8ed92009-10-16 01:20:21 +0000930 coder_info->path=(char *) "[built-in]";
cristye3e77a12009-10-16 00:47:21 +0000931 coder_info->magick=(char *) p->magick;
932 coder_info->name=(char *) p->name;
933 coder_info->exempt=MagickTrue;
934 coder_info->signature=MagickSignature;
cristyaae13f62013-08-15 14:41:32 +0000935 status&=AddValueToSplayTree(coder_list,ConstantString(coder_info->magick),
cristye3e77a12009-10-16 00:47:21 +0000936 coder_info);
937 if (status == MagickFalse)
938 (void) ThrowMagickException(exception,GetMagickModule(),
cristyefe601c2013-01-05 17:51:12 +0000939 ResourceLimitError,"MemoryAllocationFailed","`%s'",coder_info->name);
cristye3e77a12009-10-16 00:47:21 +0000940 }
cristy3ed852e2009-09-05 21:47:34 +0000941 return(status != 0 ? MagickTrue : MagickFalse);
cristy3ed852e2009-09-05 21:47:34 +0000942}