blob: 1231ba07356c02809534db5a3180e3721e51061c [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% %
Cristy7ce65e72015-12-12 18:03:16 -050020% Copyright 1999-2016 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"
dirkabed7e22016-01-31 17:10:21 +010051#include "MagickCore/linked-list.h"
cristy4c08aed2011-07-01 19:47:50 +000052#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"
cristy3291f512014-03-16 22:16:22 +000062#include "MagickCore/xml-tree-private.h"
cristy3ed852e2009-09-05 21:47:34 +000063
64/*
65 Define declarations.
66*/
67#define MagickCoderFilename "coder.xml"
68
69/*
cristy54a531d2009-10-21 17:58:01 +000070 Typedef declarations.
cristy3ed852e2009-09-05 21:47:34 +000071*/
cristye3e77a12009-10-16 00:47:21 +000072typedef struct _CoderMapInfo
73{
74 const char
75 *magick,
76 *name;
77} CoderMapInfo;
cristy54a531d2009-10-21 17:58:01 +000078
79/*
80 Static declarations.
81*/
cristye3e77a12009-10-16 00:47:21 +000082static const CoderMapInfo
83 CoderMap[] =
84 {
85 { "3FR", "DNG" },
86 { "8BIM", "META" },
87 { "8BIMTEXT", "META" },
88 { "8BIMWTEXT", "META" },
89 { "AFM", "TTF" },
90 { "A", "RAW" },
91 { "AI", "PDF" },
92 { "APP1JPEG", "META" },
93 { "APP1", "META" },
94 { "ARW", "DNG" },
cristy0fc02592010-05-24 14:12:38 +000095 { "AVI", "MPEG" },
cristye3e77a12009-10-16 00:47:21 +000096 { "BIE", "JBIG" },
97 { "BMP2", "BMP" },
98 { "BMP3", "BMP" },
99 { "B", "RAW" },
100 { "BRF", "BRAILLE" },
cristy90dbac72010-08-22 15:08:40 +0000101 { "BGRA", "BGR" },
dirk2a2d3d62015-07-27 11:09:58 +0000102 { "BGRO", "BGR" },
cristye3e77a12009-10-16 00:47:21 +0000103 { "CMYKA", "CMYK" },
104 { "C", "RAW" },
105 { "CAL", "CALS" },
cristyc05417c2010-09-21 16:30:32 +0000106 { "CANVAS", "XC" },
dirk2a2d3d62015-07-27 11:09:58 +0000107 { "CMYKA", "CMYK" },
cristye3e77a12009-10-16 00:47:21 +0000108 { "CR2", "DNG" },
109 { "CRW", "DNG" },
110 { "CUR", "ICON" },
Cristy6ffac4f2015-08-23 10:55:57 -0400111 { "DATA", "INLINE" },
cristye3e77a12009-10-16 00:47:21 +0000112 { "DCR", "DNG" },
113 { "DCX", "PCX" },
114 { "DFONT", "TTF" },
dirk2a2d3d62015-07-27 11:09:58 +0000115 { "DXT1", "DDS" },
116 { "DXT5", "DDS" },
cristye3e77a12009-10-16 00:47:21 +0000117 { "EPDF", "PDF" },
118 { "EPI", "PS" },
119 { "EPS2", "PS2" },
120 { "EPS3", "PS3" },
121 { "EPSF", "PS" },
122 { "EPSI", "PS" },
123 { "EPS", "PS" },
124 { "EPT2", "EPT" },
125 { "EPT3", "EPT" },
126 { "ERF", "DNG" },
127 { "EXIF", "META" },
128 { "FILE", "URL" },
129 { "FRACTAL", "PLASMA" },
130 { "FTP", "URL" },
131 { "FTS", "FITS" },
132 { "G3", "FAX" },
133 { "GIF87", "GIF" },
134 { "G", "RAW" },
cristye3e77a12009-10-16 00:47:21 +0000135 { "GRANITE", "MAGICK" },
136 { "GROUP4", "TIFF" },
cristy7f47ac62013-02-10 14:16:17 +0000137 { "GV", "DOT" },
dirk2a2d3d62015-07-27 11:09:58 +0000138 { "HTM", "HTML" },
139 { "ICB", "TGA" },
140 { "ICO", "ICON" },
cristya5338bb2014-11-05 18:25:16 +0000141 { "IIQ", "DNG" },
cristye3e77a12009-10-16 00:47:21 +0000142 { "K25", "DNG" },
143 { "KDC", "DNG" },
144 { "H", "MAGICK" },
145 { "HTM", "HTML" },
146 { "HTTP", "URL" },
dirk853b77b2013-09-21 19:04:42 +0000147 { "HTTPS", "URL" },
cristye3e77a12009-10-16 00:47:21 +0000148 { "ICB", "TGA" },
149 { "ICC", "META" },
150 { "ICM", "META" },
151 { "ICO", "ICON" },
152 { "IMPLICIT", "***" },
153 { "IPTC", "META" },
154 { "IPTCTEXT", "META" },
155 { "IPTCWTEXT", "META" },
156 { "ISOBRL", "BRAILLE" },
Samuel Thibault531aa152015-11-01 23:36:24 +0100157 { "ISOBRL6", "BRAILLE" },
cristye3e77a12009-10-16 00:47:21 +0000158 { "JBG", "JBIG" },
159 { "JNG", "PNG" },
160 { "JPC", "JP2" },
cristy2267fa42014-09-12 10:27:47 +0000161 { "JPT", "JP2" },
162 { "JPM", "JP2" },
cristy02f1fda2009-12-10 15:23:56 +0000163 { "J2C", "JP2" },
cristy4faac682012-05-10 12:09:45 +0000164 { "J2K", "JP2" },
dirk2a2d3d62015-07-27 11:09:58 +0000165 { "JNG", "PNG" },
cristyb338b712014-11-27 14:06:34 +0000166 { "JPE", "JPEG" },
cristye3e77a12009-10-16 00:47:21 +0000167 { "JPG", "JPEG" },
dirk2a2d3d62015-07-27 11:09:58 +0000168 { "JPM", "JP2" },
cristya663a4f2015-01-23 12:11:57 +0000169 { "JPS", "JPEG" },
dirk2a2d3d62015-07-27 11:09:58 +0000170 { "JPT", "JP2" },
cristye3e77a12009-10-16 00:47:21 +0000171 { "JPX", "JP2" },
172 { "K", "RAW" },
dirk2a2d3d62015-07-27 11:09:58 +0000173 { "K25", "DNG" },
174 { "KDC", "DNG" },
cristye3e77a12009-10-16 00:47:21 +0000175 { "LOGO", "MAGICK" },
dirk2a2d3d62015-07-27 11:09:58 +0000176 { "M", "RAW" },
cristye3e77a12009-10-16 00:47:21 +0000177 { "M2V", "MPEG" },
178 { "M4V", "MPEG" },
dirk2a2d3d62015-07-27 11:09:58 +0000179 { "MEF", "DNG" },
cristya3d81422015-04-03 13:00:33 +0000180 { "MKV", "MPEG" },
cristye3e77a12009-10-16 00:47:21 +0000181 { "MNG", "PNG" },
cristy1dbd5a32010-11-28 22:23:27 +0000182 { "MOV", "MPEG" },
cristye3e77a12009-10-16 00:47:21 +0000183 { "MP4", "MPEG" },
184 { "MPG", "MPEG" },
185 { "MPRI", "MPR" },
cristy1be98ec2012-01-30 16:51:12 +0000186 { "MEF", "DNG" },
cristye3e77a12009-10-16 00:47:21 +0000187 { "MRW", "DNG" },
188 { "MSVG", "SVG" },
189 { "NEF", "DNG" },
190 { "NETSCAPE", "MAGICK" },
cristycb8f0ae2012-03-02 12:26:21 +0000191 { "NRW", "DNG" },
cristye3e77a12009-10-16 00:47:21 +0000192 { "O", "RAW" },
193 { "ORF", "DNG" },
194 { "OTF", "TTF" },
195 { "P7", "PNM" },
196 { "PAL", "UYVY" },
197 { "PAM", "PNM" },
198 { "PBM", "PNM" },
199 { "PCDS", "PCD" },
dirk2a2d3d62015-07-27 11:09:58 +0000200 { "PCT", "PICT" },
cristye3e77a12009-10-16 00:47:21 +0000201 { "PDFA", "PDF" },
202 { "PEF", "DNG" },
203 { "PEF", "DNG" },
204 { "PFA", "TTF" },
205 { "PFB", "TTF" },
206 { "PFM", "PNM" },
207 { "PGM", "PNM" },
208 { "PGX", "JP2" },
209 { "PICON", "XPM" },
210 { "PJPEG", "JPEG" },
211 { "PM", "XPM" },
dirk2a2d3d62015-07-27 11:09:58 +0000212 { "PNG00", "PNG" },
cristye3e77a12009-10-16 00:47:21 +0000213 { "PNG24", "PNG" },
214 { "PNG32", "PNG" },
dirk2a2d3d62015-07-27 11:09:58 +0000215 { "PNG48", "PNG" },
216 { "PNG64", "PNG" },
cristye3e77a12009-10-16 00:47:21 +0000217 { "PNG8", "PNG" },
218 { "PPM", "PNM" },
cristyb4233012010-02-28 20:09:14 +0000219 { "PSB", "PSD" },
cristye3e77a12009-10-16 00:47:21 +0000220 { "PTIF", "TIFF" },
dirk2a2d3d62015-07-27 11:09:58 +0000221 { "R", "RAW" },
cristye3e77a12009-10-16 00:47:21 +0000222 { "RADIAL-GRADIENT", "GRADIENT" },
223 { "RAF", "DNG" },
224 { "RAS", "SUN" },
cristy1b1f0722014-06-10 10:14:59 +0000225 { "RAW", "DNG" },
cristye3e77a12009-10-16 00:47:21 +0000226 { "RGBA", "RGB" },
227 { "RGBO", "RGB" },
cristy15e22962014-02-21 22:56:26 +0000228 { "RMF", "DNG" },
cristye3e77a12009-10-16 00:47:21 +0000229 { "ROSE", "MAGICK" },
cristy98aceb82012-10-20 21:24:00 +0000230 { "RW2", "DNG" },
cristye3e77a12009-10-16 00:47:21 +0000231 { "SHTML", "HTML" },
dirka1e4ab82014-10-07 05:06:49 +0000232 { "SIX", "SIXEL" },
cristye6ee7492013-03-16 15:04:42 +0000233 { "SPARSE-COLOR", "TXT" },
cristye3e77a12009-10-16 00:47:21 +0000234 { "SR2", "DNG" },
235 { "SRF", "DNG" },
236 { "SVGZ", "SVG" },
237 { "TEXT", "TXT" },
238 { "TIFF64", "TIFF" },
239 { "TIF", "TIFF" },
240 { "TTC", "TTF" },
241 { "UBRL", "BRAILLE" },
Samuel Thibault531aa152015-11-01 23:36:24 +0100242 { "UBRL6", "BRAILLE" },
cristye3e77a12009-10-16 00:47:21 +0000243 { "VDA", "TGA" },
244 { "VST", "TGA" },
cristyed40d622011-01-17 15:51:00 +0000245 { "WIZARD", "MAGICK" },
dirk073ff372014-01-05 14:05:25 +0000246#if defined(MAGICKCORE_WINGDI32_DELEGATE)
247 { "WMF", "EMF" },
248#endif
cristye3e77a12009-10-16 00:47:21 +0000249 { "WMV", "MPEG" },
cristye3e77a12009-10-16 00:47:21 +0000250 { "WMZ", "WMF" },
251 { "X3f", "DNG" },
252 { "XMP", "META" },
253 { "XTRNARRAY", "XTRN" },
254 { "XTRNBLOB", "XTRN" },
cristye3e77a12009-10-16 00:47:21 +0000255 { "XTRNFILE", "XTRN" },
256 { "XTRNIMAGE", "XTRN" },
cristye3e77a12009-10-16 00:47:21 +0000257 { "XV", "VIFF" },
258 { "Y", "RAW" },
cristy54a531d2009-10-21 17:58:01 +0000259 { "YCbCrA", "YCbCr" }
cristye3e77a12009-10-16 00:47:21 +0000260 };
261
cristy3ed852e2009-09-05 21:47:34 +0000262static SemaphoreInfo
263 *coder_semaphore = (SemaphoreInfo *) NULL;
264
265static SplayTreeInfo
cristy86e5ac92014-03-16 19:27:39 +0000266 *coder_cache = (SplayTreeInfo *) NULL;
cristy3ed852e2009-09-05 21:47:34 +0000267
268/*
269 Forward declarations.
270*/
271static MagickBooleanType
cristy904e5912014-03-15 19:53:14 +0000272 IsCoderTreeInstantiated(ExceptionInfo *),
cristy86e5ac92014-03-16 19:27:39 +0000273 LoadCoderCache(SplayTreeInfo *,const char *,const char *,const size_t,
274 ExceptionInfo *);
275
276/*
277%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
278% %
279% %
280% %
cristy6c6322e2014-09-09 22:39:21 +0000281+ A c q u i r e C o d e r C a c h e %
cristy86e5ac92014-03-16 19:27:39 +0000282% %
283% %
284% %
285%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
286%
cristy6c6322e2014-09-09 22:39:21 +0000287% AcquireCoderCache() caches one or more coder configurations which provides a
288% mapping between coder attributes and a coder name.
cristy86e5ac92014-03-16 19:27:39 +0000289%
290% The format of the AcquireCoderCache coder is:
291%
292% SplayTreeInfo *AcquireCoderCache(const char *filename,
293% ExceptionInfo *exception)
294%
295% A description of each parameter follows:
296%
297% o filename: the font file name.
298%
299% o exception: return any errors or warnings in this structure.
300%
301*/
302
303static void *DestroyCoderNode(void *coder_info)
304{
305 register CoderInfo
306 *p;
307
308 p=(CoderInfo *) coder_info;
309 if (p->exempt == MagickFalse)
310 {
311 if (p->path != (char *) NULL)
312 p->path=DestroyString(p->path);
313 if (p->name != (char *) NULL)
314 p->name=DestroyString(p->name);
315 if (p->magick != (char *) NULL)
316 p->magick=DestroyString(p->magick);
317 }
318 return(RelinquishMagickMemory(p));
319}
320
321static SplayTreeInfo *AcquireCoderCache(const char *filename,
322 ExceptionInfo *exception)
323{
324 const StringInfo
325 *option;
326
327 LinkedListInfo
328 *options;
329
330 MagickStatusType
331 status;
332
333 register ssize_t
334 i;
335
336 SplayTreeInfo
337 *coder_cache;
338
339 /*
340 Load external coder map.
341 */
342 coder_cache=NewSplayTree(CompareSplayTreeString,RelinquishMagickMemory,
343 DestroyCoderNode);
344 if (coder_cache == (SplayTreeInfo *) NULL)
345 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
346 status=MagickTrue;
347 options=GetConfigureOptions(filename,exception);
348 option=(const StringInfo *) GetNextValueInLinkedList(options);
349 while (option != (const StringInfo *) NULL)
350 {
cristycd2cd182014-03-18 12:10:55 +0000351 status&=LoadCoderCache(coder_cache,(const char *)
352 GetStringInfoDatum(option),GetStringInfoPath(option),0,exception);
cristy86e5ac92014-03-16 19:27:39 +0000353 option=(const StringInfo *) GetNextValueInLinkedList(options);
354 }
355 options=DestroyConfigureOptions(options);
356 /*
357 Load built-in coder map.
358 */
359 for (i=0; i < (ssize_t) (sizeof(CoderMap)/sizeof(*CoderMap)); i++)
360 {
361 CoderInfo
362 *coder_info;
363
364 register const CoderMapInfo
365 *p;
366
367 p=CoderMap+i;
368 coder_info=(CoderInfo *) AcquireMagickMemory(sizeof(*coder_info));
369 if (coder_info == (CoderInfo *) NULL)
370 {
371 (void) ThrowMagickException(exception,GetMagickModule(),
372 ResourceLimitError,"MemoryAllocationFailed","`%s'",p->name);
373 continue;
374 }
375 (void) ResetMagickMemory(coder_info,0,sizeof(*coder_info));
376 coder_info->path=(char *) "[built-in]";
377 coder_info->magick=(char *) p->magick;
378 coder_info->name=(char *) p->name;
379 coder_info->exempt=MagickTrue;
cristye1c94d92015-06-28 12:16:33 +0000380 coder_info->signature=MagickCoreSignature;
cristy86e5ac92014-03-16 19:27:39 +0000381 status&=AddValueToSplayTree(coder_cache,ConstantString(coder_info->magick),
382 coder_info);
383 if (status == MagickFalse)
384 (void) ThrowMagickException(exception,GetMagickModule(),
385 ResourceLimitError,"MemoryAllocationFailed","`%s'",coder_info->name);
386 }
387 return(coder_cache);
388}
cristy3ed852e2009-09-05 21:47:34 +0000389
390/*
391%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
392% %
393% %
394% %
cristyf34a1452009-10-24 22:29:27 +0000395+ 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 +0000396% %
397% %
398% %
399%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
400%
cristyf34a1452009-10-24 22:29:27 +0000401% CoderComponentGenesis() instantiates the coder component.
cristy3ed852e2009-09-05 21:47:34 +0000402%
cristyf34a1452009-10-24 22:29:27 +0000403% The format of the CoderComponentGenesis method is:
cristy3ed852e2009-09-05 21:47:34 +0000404%
cristyf34a1452009-10-24 22:29:27 +0000405% MagickBooleanType CoderComponentGenesis(void)
cristy3ed852e2009-09-05 21:47:34 +0000406%
407*/
cristy5ff4eaf2011-09-03 01:38:02 +0000408MagickPrivate MagickBooleanType CoderComponentGenesis(void)
cristyf34a1452009-10-24 22:29:27 +0000409{
cristy7c977062014-04-04 14:05:53 +0000410 if (coder_semaphore == (SemaphoreInfo *) NULL)
411 coder_semaphore=AcquireSemaphoreInfo();
cristyf34a1452009-10-24 22:29:27 +0000412 return(MagickTrue);
413}
414
415/*
416%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
417% %
418% %
419% %
420+ C o d e r C o m p o n e n t T e r m i n u s %
421% %
422% %
423% %
424%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
425%
426% CoderComponentTerminus() destroys the coder component.
427%
428% The format of the CoderComponentTerminus method is:
429%
430% CoderComponentTerminus(void)
431%
432*/
cristy5ff4eaf2011-09-03 01:38:02 +0000433MagickPrivate void CoderComponentTerminus(void)
cristy3ed852e2009-09-05 21:47:34 +0000434{
cristy18b17442009-10-25 18:36:48 +0000435 if (coder_semaphore == (SemaphoreInfo *) NULL)
cristy04b11db2014-02-16 15:10:39 +0000436 ActivateSemaphoreInfo(&coder_semaphore);
cristyf84a1932010-01-03 18:00:18 +0000437 LockSemaphoreInfo(coder_semaphore);
cristy86e5ac92014-03-16 19:27:39 +0000438 if (coder_cache != (SplayTreeInfo *) NULL)
439 coder_cache=DestroySplayTree(coder_cache);
cristyf84a1932010-01-03 18:00:18 +0000440 UnlockSemaphoreInfo(coder_semaphore);
cristy3d162a92014-02-16 14:05:06 +0000441 RelinquishSemaphoreInfo(&coder_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000442}
443
444/*
445%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
446% %
447% %
448% %
449+ G e t C o d e r I n f o %
450% %
451% %
452% %
453%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
454%
455% GetCoderInfo searches the coder list for the specified name and if found
456% returns attributes for that coder.
457%
458% The format of the GetCoderInfo method is:
459%
460% const CoderInfo *GetCoderInfo(const char *name,ExceptionInfo *exception)
461%
462% A description of each parameter follows:
463%
464% o name: the coder name.
465%
466% o exception: return any errors or warnings in this structure.
467%
468*/
469MagickExport const CoderInfo *GetCoderInfo(const char *name,
470 ExceptionInfo *exception)
471{
cristy49d4d222014-03-16 00:37:58 +0000472 const CoderInfo
473 *coder_info;
474
cristy3ed852e2009-09-05 21:47:34 +0000475 assert(exception != (ExceptionInfo *) NULL);
cristy904e5912014-03-15 19:53:14 +0000476 if (IsCoderTreeInstantiated(exception) == MagickFalse)
477 return((const CoderInfo *) NULL);
cristy49d4d222014-03-16 00:37:58 +0000478 LockSemaphoreInfo(coder_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000479 if ((name == (const char *) NULL) || (LocaleCompare(name,"*") == 0))
480 {
cristy86e5ac92014-03-16 19:27:39 +0000481 ResetSplayTreeIterator(coder_cache);
482 coder_info=(const CoderInfo *) GetNextValueInSplayTree(coder_cache);
cristy49d4d222014-03-16 00:37:58 +0000483 UnlockSemaphoreInfo(coder_semaphore);
484 return(coder_info);
cristy3ed852e2009-09-05 21:47:34 +0000485 }
cristy86e5ac92014-03-16 19:27:39 +0000486 coder_info=(const CoderInfo *) GetValueFromSplayTree(coder_cache,name);
cristy49d4d222014-03-16 00:37:58 +0000487 UnlockSemaphoreInfo(coder_semaphore);
488 return(coder_info);
cristy3ed852e2009-09-05 21:47:34 +0000489}
490
491/*
492%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
493% %
494% %
495% %
496% G e t C o d e r I n f o L i s t %
497% %
498% %
499% %
500%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
501%
502% GetCoderInfoList() returns any coder_map that match the specified pattern.
503% The format of the GetCoderInfoList function is:
504%
505% const CoderInfo **GetCoderInfoList(const char *pattern,
cristybb503372010-05-27 20:51:26 +0000506% size_t *number_coders,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000507%
508% A description of each parameter follows:
509%
510% o pattern: Specifies a pointer to a text string containing a pattern.
511%
512% o number_coders: This integer returns the number of coders in the list.
513%
514% o exception: return any errors or warnings in this structure.
515%
516*/
517
518static int CoderInfoCompare(const void *x,const void *y)
519{
520 const CoderInfo
521 **p,
522 **q;
523
524 p=(const CoderInfo **) x,
525 q=(const CoderInfo **) y;
526 if (LocaleCompare((*p)->path,(*q)->path) == 0)
527 return(LocaleCompare((*p)->name,(*q)->name));
528 return(LocaleCompare((*p)->path,(*q)->path));
529}
530
531MagickExport const CoderInfo **GetCoderInfoList(const char *pattern,
cristybb503372010-05-27 20:51:26 +0000532 size_t *number_coders,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000533{
534 const CoderInfo
535 **coder_map;
536
537 register const CoderInfo
538 *p;
539
cristybb503372010-05-27 20:51:26 +0000540 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000541 i;
542
543 /*
544 Allocate coder list.
545 */
546 assert(pattern != (char *) NULL);
547 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
cristybb503372010-05-27 20:51:26 +0000548 assert(number_coders != (size_t *) NULL);
cristy3ed852e2009-09-05 21:47:34 +0000549 *number_coders=0;
550 p=GetCoderInfo("*",exception);
551 if (p == (const CoderInfo *) NULL)
552 return((const CoderInfo **) NULL);
553 coder_map=(const CoderInfo **) AcquireQuantumMemory((size_t)
cristy86e5ac92014-03-16 19:27:39 +0000554 GetNumberOfNodesInSplayTree(coder_cache)+1UL,sizeof(*coder_map));
cristy3ed852e2009-09-05 21:47:34 +0000555 if (coder_map == (const CoderInfo **) NULL)
556 return((const CoderInfo **) NULL);
557 /*
558 Generate coder list.
559 */
cristyf84a1932010-01-03 18:00:18 +0000560 LockSemaphoreInfo(coder_semaphore);
cristy86e5ac92014-03-16 19:27:39 +0000561 ResetSplayTreeIterator(coder_cache);
562 p=(const CoderInfo *) GetNextValueInSplayTree(coder_cache);
cristy3ed852e2009-09-05 21:47:34 +0000563 for (i=0; p != (const CoderInfo *) NULL; )
564 {
565 if ((p->stealth == MagickFalse) &&
566 (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
567 coder_map[i++]=p;
cristy86e5ac92014-03-16 19:27:39 +0000568 p=(const CoderInfo *) GetNextValueInSplayTree(coder_cache);
cristy3ed852e2009-09-05 21:47:34 +0000569 }
cristyf84a1932010-01-03 18:00:18 +0000570 UnlockSemaphoreInfo(coder_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000571 qsort((void *) coder_map,(size_t) i,sizeof(*coder_map),CoderInfoCompare);
572 coder_map[i]=(CoderInfo *) NULL;
cristybb503372010-05-27 20:51:26 +0000573 *number_coders=(size_t) i;
cristy3ed852e2009-09-05 21:47:34 +0000574 return(coder_map);
575}
576
577/*
578%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
579% %
580% %
581% %
582% G e t C o d e r L i s t %
583% %
584% %
585% %
586%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
587%
588% GetCoderList() returns any coder_map that match the specified pattern.
589%
590% The format of the GetCoderList function is:
591%
cristybb503372010-05-27 20:51:26 +0000592% char **GetCoderList(const char *pattern,size_t *number_coders,
cristy3ed852e2009-09-05 21:47:34 +0000593% ExceptionInfo *exception)
594%
595% A description of each parameter follows:
596%
597% o pattern: Specifies a pointer to a text string containing a pattern.
598%
599% o number_coders: This integer returns the number of coders in the list.
600%
601% o exception: return any errors or warnings in this structure.
602%
603*/
604
605static int CoderCompare(const void *x,const void *y)
606{
607 register const char
608 **p,
609 **q;
610
611 p=(const char **) x;
612 q=(const char **) y;
613 return(LocaleCompare(*p,*q));
614}
615
616MagickExport char **GetCoderList(const char *pattern,
cristybb503372010-05-27 20:51:26 +0000617 size_t *number_coders,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000618{
619 char
620 **coder_map;
621
622 register const CoderInfo
623 *p;
624
cristybb503372010-05-27 20:51:26 +0000625 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000626 i;
627
628 /*
629 Allocate coder list.
630 */
631 assert(pattern != (char *) NULL);
632 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
cristybb503372010-05-27 20:51:26 +0000633 assert(number_coders != (size_t *) NULL);
cristy3ed852e2009-09-05 21:47:34 +0000634 *number_coders=0;
635 p=GetCoderInfo("*",exception);
636 if (p == (const CoderInfo *) NULL)
637 return((char **) NULL);
638 coder_map=(char **) AcquireQuantumMemory((size_t)
cristy86e5ac92014-03-16 19:27:39 +0000639 GetNumberOfNodesInSplayTree(coder_cache)+1UL,sizeof(*coder_map));
cristy3ed852e2009-09-05 21:47:34 +0000640 if (coder_map == (char **) NULL)
641 return((char **) NULL);
642 /*
643 Generate coder list.
644 */
cristyf84a1932010-01-03 18:00:18 +0000645 LockSemaphoreInfo(coder_semaphore);
cristy86e5ac92014-03-16 19:27:39 +0000646 ResetSplayTreeIterator(coder_cache);
647 p=(const CoderInfo *) GetNextValueInSplayTree(coder_cache);
cristy3ed852e2009-09-05 21:47:34 +0000648 for (i=0; p != (const CoderInfo *) NULL; )
649 {
650 if ((p->stealth == MagickFalse) &&
651 (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
652 coder_map[i++]=ConstantString(p->name);
cristy86e5ac92014-03-16 19:27:39 +0000653 p=(const CoderInfo *) GetNextValueInSplayTree(coder_cache);
cristy3ed852e2009-09-05 21:47:34 +0000654 }
cristyf84a1932010-01-03 18:00:18 +0000655 UnlockSemaphoreInfo(coder_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000656 qsort((void *) coder_map,(size_t) i,sizeof(*coder_map),CoderCompare);
657 coder_map[i]=(char *) NULL;
cristybb503372010-05-27 20:51:26 +0000658 *number_coders=(size_t) i;
cristy3ed852e2009-09-05 21:47:34 +0000659 return(coder_map);
660}
661
662/*
663%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
664% %
665% %
666% %
cristy904e5912014-03-15 19:53:14 +0000667+ 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 +0000668% %
669% %
670% %
671%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
672%
cristycd2cd182014-03-18 12:10:55 +0000673% IsCoderTreeInstantiated() determines if the coder tree is instantiated. If
674% not, it instantiates the tree and returns it.
cristy3ed852e2009-09-05 21:47:34 +0000675%
cristy904e5912014-03-15 19:53:14 +0000676% The format of the IsCoderInstantiated method is:
cristy3ed852e2009-09-05 21:47:34 +0000677%
cristy904e5912014-03-15 19:53:14 +0000678% MagickBooleanType IsCoderTreeInstantiated(ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000679%
680% A description of each parameter follows.
681%
682% o exception: return any errors or warnings in this structure.
683%
684*/
cristy904e5912014-03-15 19:53:14 +0000685static MagickBooleanType IsCoderTreeInstantiated(ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000686{
cristy86e5ac92014-03-16 19:27:39 +0000687 if (coder_cache == (SplayTreeInfo *) NULL)
688 {
689 if (coder_semaphore == (SemaphoreInfo *) NULL)
690 ActivateSemaphoreInfo(&coder_semaphore);
691 LockSemaphoreInfo(coder_semaphore);
692 if (coder_cache == (SplayTreeInfo *) NULL)
cristycd2cd182014-03-18 12:10:55 +0000693 coder_cache=AcquireCoderCache(MagickCoderFilename,exception);
cristy86e5ac92014-03-16 19:27:39 +0000694 UnlockSemaphoreInfo(coder_semaphore);
695 }
696 return(coder_cache != (SplayTreeInfo *) NULL ? MagickTrue : MagickFalse);
cristy3ed852e2009-09-05 21:47:34 +0000697}
698
699/*
700%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
701% %
702% %
703% %
704% L i s t C o d e r I n f o %
705% %
706% %
707% %
708%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
709%
710% ListCoderInfo() lists the coder info to a file.
711%
712% The format of the ListCoderInfo coder is:
713%
714% MagickBooleanType ListCoderInfo(FILE *file,ExceptionInfo *exception)
715%
716% A description of each parameter follows.
717%
718% o file: An pointer to a FILE.
719%
720% o exception: return any errors or warnings in this structure.
721%
722*/
723MagickExport MagickBooleanType ListCoderInfo(FILE *file,
724 ExceptionInfo *exception)
725{
726 const char
727 *path;
728
729 const CoderInfo
730 **coder_info;
731
cristybb503372010-05-27 20:51:26 +0000732 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000733 i;
734
cristybb503372010-05-27 20:51:26 +0000735 size_t
cristy3ed852e2009-09-05 21:47:34 +0000736 number_coders;
737
cristy9d314ff2011-03-09 01:30:28 +0000738 ssize_t
739 j;
740
cristy3ed852e2009-09-05 21:47:34 +0000741 if (file == (const FILE *) NULL)
742 file=stdout;
743 coder_info=GetCoderInfoList("*",&number_coders,exception);
744 if (coder_info == (const CoderInfo **) NULL)
745 return(MagickFalse);
746 path=(const char *) NULL;
cristybb503372010-05-27 20:51:26 +0000747 for (i=0; i < (ssize_t) number_coders; i++)
cristy3ed852e2009-09-05 21:47:34 +0000748 {
749 if (coder_info[i]->stealth != MagickFalse)
750 continue;
751 if ((path == (const char *) NULL) ||
752 (LocaleCompare(path,coder_info[i]->path) != 0))
753 {
754 if (coder_info[i]->path != (char *) NULL)
cristyb51dff52011-05-19 16:55:47 +0000755 (void) FormatLocaleFile(file,"\nPath: %s\n\n",coder_info[i]->path);
756 (void) FormatLocaleFile(file,"Magick Coder\n");
cristy1e604812011-05-19 18:07:50 +0000757 (void) FormatLocaleFile(file,
758 "-------------------------------------------------"
cristy3ed852e2009-09-05 21:47:34 +0000759 "------------------------------\n");
760 }
761 path=coder_info[i]->path;
cristyb51dff52011-05-19 16:55:47 +0000762 (void) FormatLocaleFile(file,"%s",coder_info[i]->magick);
cristybb503372010-05-27 20:51:26 +0000763 for (j=(ssize_t) strlen(coder_info[i]->magick); j <= 11; j++)
cristyb51dff52011-05-19 16:55:47 +0000764 (void) FormatLocaleFile(file," ");
cristy3ed852e2009-09-05 21:47:34 +0000765 if (coder_info[i]->name != (char *) NULL)
cristyb51dff52011-05-19 16:55:47 +0000766 (void) FormatLocaleFile(file,"%s",coder_info[i]->name);
767 (void) FormatLocaleFile(file,"\n");
cristy3ed852e2009-09-05 21:47:34 +0000768 }
769 coder_info=(const CoderInfo **) RelinquishMagickMemory((void *) coder_info);
770 (void) fflush(file);
771 return(MagickTrue);
772}
773
774/*
775%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
776% %
777% %
778% %
779+ L o a d C o d e r L i s t %
780% %
781% %
782% %
783%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
784%
cristy86e5ac92014-03-16 19:27:39 +0000785% LoadCoderCache() loads the coder configurations which provides a
cristy3ed852e2009-09-05 21:47:34 +0000786% mapping between coder attributes and a coder name.
787%
cristy86e5ac92014-03-16 19:27:39 +0000788% The format of the LoadCoderCache coder is:
cristy3ed852e2009-09-05 21:47:34 +0000789%
cristy86e5ac92014-03-16 19:27:39 +0000790% MagickBooleanType LoadCoderCache(SplayTreeInfo *coder_cache,
791% const char *xml,const char *filename,const size_t depth,
792% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000793%
794% A description of each parameter follows:
795%
796% o xml: The coder list in XML format.
797%
798% o filename: The coder list filename.
799%
800% o depth: depth of <include /> statements.
801%
802% o exception: return any errors or warnings in this structure.
803%
804*/
cristy86e5ac92014-03-16 19:27:39 +0000805static MagickBooleanType LoadCoderCache(SplayTreeInfo *coder_cache,
806 const char *xml,const char *filename,const size_t depth,
807 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000808{
809 char
cristy151b66d2015-04-15 10:50:31 +0000810 keyword[MagickPathExtent],
cristy3ed852e2009-09-05 21:47:34 +0000811 *token;
812
813 const char
814 *q;
815
816 CoderInfo
817 *coder_info;
818
cristy759ba912014-06-26 11:59:43 +0000819 MagickStatusType
cristy3ed852e2009-09-05 21:47:34 +0000820 status;
821
822 /*
823 Load the coder map file.
824 */
825 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
826 "Loading coder configuration file \"%s\" ...",filename);
827 if (xml == (const char *) NULL)
828 return(MagickFalse);
cristy3ed852e2009-09-05 21:47:34 +0000829 status=MagickTrue;
830 coder_info=(CoderInfo *) NULL;
831 token=AcquireString(xml);
832 for (q=(char *) xml; *q != '\0'; )
833 {
834 /*
835 Interpret XML.
836 */
837 GetMagickToken(q,&q,token);
838 if (*token == '\0')
839 break;
cristy151b66d2015-04-15 10:50:31 +0000840 (void) CopyMagickString(keyword,token,MagickPathExtent);
cristy3ed852e2009-09-05 21:47:34 +0000841 if (LocaleNCompare(keyword,"<!DOCTYPE",9) == 0)
842 {
843 /*
844 Doctype element.
845 */
846 while ((LocaleNCompare(q,"]>",2) != 0) && (*q != '\0'))
847 GetMagickToken(q,&q,token);
848 continue;
849 }
850 if (LocaleNCompare(keyword,"<!--",4) == 0)
851 {
852 /*
853 Comment element.
854 */
855 while ((LocaleNCompare(q,"->",2) != 0) && (*q != '\0'))
856 GetMagickToken(q,&q,token);
857 continue;
858 }
859 if (LocaleCompare(keyword,"<include") == 0)
860 {
861 /*
862 Include element.
863 */
864 while (((*token != '/') && (*(token+1) != '>')) && (*q != '\0'))
865 {
cristy151b66d2015-04-15 10:50:31 +0000866 (void) CopyMagickString(keyword,token,MagickPathExtent);
cristy3ed852e2009-09-05 21:47:34 +0000867 GetMagickToken(q,&q,token);
868 if (*token != '=')
869 continue;
870 GetMagickToken(q,&q,token);
871 if (LocaleCompare(keyword,"file") == 0)
872 {
873 if (depth > 200)
874 (void) ThrowMagickException(exception,GetMagickModule(),
cristyefe601c2013-01-05 17:51:12 +0000875 ConfigureError,"IncludeNodeNestedTooDeeply","`%s'",token);
cristy3ed852e2009-09-05 21:47:34 +0000876 else
877 {
878 char
cristy151b66d2015-04-15 10:50:31 +0000879 path[MagickPathExtent],
dirkb81f0ab2015-10-20 23:01:53 +0200880 *file_xml;
cristy3ed852e2009-09-05 21:47:34 +0000881
882 GetPathComponent(filename,HeadPath,path);
883 if (*path != '\0')
884 (void) ConcatenateMagickString(path,DirectorySeparator,
cristy151b66d2015-04-15 10:50:31 +0000885 MagickPathExtent);
cristy3ed852e2009-09-05 21:47:34 +0000886 if (*token == *DirectorySeparator)
cristy151b66d2015-04-15 10:50:31 +0000887 (void) CopyMagickString(path,token,MagickPathExtent);
cristy3ed852e2009-09-05 21:47:34 +0000888 else
cristy151b66d2015-04-15 10:50:31 +0000889 (void) ConcatenateMagickString(path,token,MagickPathExtent);
dirkb81f0ab2015-10-20 23:01:53 +0200890 file_xml=FileToXML(path,~0UL);
891 if (file_xml != (char *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000892 {
dirkb81f0ab2015-10-20 23:01:53 +0200893 status&=LoadCoderCache(coder_cache,file_xml,path,depth+1,
cristy86e5ac92014-03-16 19:27:39 +0000894 exception);
dirkb81f0ab2015-10-20 23:01:53 +0200895 file_xml=DestroyString(file_xml);
cristy3ed852e2009-09-05 21:47:34 +0000896 }
897 }
898 }
899 }
900 continue;
901 }
902 if (LocaleCompare(keyword,"<coder") == 0)
903 {
904 /*
905 Coder element.
906 */
cristy73bd4a52010-10-05 11:24:23 +0000907 coder_info=(CoderInfo *) AcquireMagickMemory(sizeof(*coder_info));
cristy3ed852e2009-09-05 21:47:34 +0000908 if (coder_info == (CoderInfo *) NULL)
909 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
910 (void) ResetMagickMemory(coder_info,0,sizeof(*coder_info));
911 coder_info->path=ConstantString(filename);
cristye3e77a12009-10-16 00:47:21 +0000912 coder_info->exempt=MagickFalse;
cristye1c94d92015-06-28 12:16:33 +0000913 coder_info->signature=MagickCoreSignature;
cristy3ed852e2009-09-05 21:47:34 +0000914 continue;
915 }
916 if (coder_info == (CoderInfo *) NULL)
917 continue;
918 if (LocaleCompare(keyword,"/>") == 0)
919 {
cristy86e5ac92014-03-16 19:27:39 +0000920 status=AddValueToSplayTree(coder_cache,ConstantString(
cristy3ed852e2009-09-05 21:47:34 +0000921 coder_info->magick),coder_info);
922 if (status == MagickFalse)
923 (void) ThrowMagickException(exception,GetMagickModule(),
cristyefe601c2013-01-05 17:51:12 +0000924 ResourceLimitError,"MemoryAllocationFailed","`%s'",
cristy3ed852e2009-09-05 21:47:34 +0000925 coder_info->magick);
926 coder_info=(CoderInfo *) NULL;
cristyd45122f2014-01-14 23:46:16 +0000927 continue;
cristy3ed852e2009-09-05 21:47:34 +0000928 }
929 GetMagickToken(q,(const char **) NULL,token);
930 if (*token != '=')
931 continue;
932 GetMagickToken(q,&q,token);
933 GetMagickToken(q,&q,token);
934 switch (*keyword)
935 {
936 case 'M':
937 case 'm':
938 {
939 if (LocaleCompare((char *) keyword,"magick") == 0)
940 {
941 coder_info->magick=ConstantString(token);
942 break;
943 }
944 break;
945 }
946 case 'N':
947 case 'n':
948 {
949 if (LocaleCompare((char *) keyword,"name") == 0)
950 {
951 coder_info->name=ConstantString(token);
952 break;
953 }
954 break;
955 }
956 case 'S':
957 case 's':
958 {
959 if (LocaleCompare((char *) keyword,"stealth") == 0)
960 {
anthony6f201312012-03-30 04:08:15 +0000961 coder_info->stealth=IsStringTrue(token);
cristy3ed852e2009-09-05 21:47:34 +0000962 break;
963 }
964 break;
965 }
966 default:
967 break;
968 }
969 }
970 token=(char *) RelinquishMagickMemory(token);
cristy759ba912014-06-26 11:59:43 +0000971 return(status != 0 ? MagickTrue : MagickFalse);
cristy3ed852e2009-09-05 21:47:34 +0000972}