blob: e190b422ebd34454e6204e41abc001e52f7d9a50 [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% %
Cristy93b707b2017-12-06 07:05:51 -050020% Copyright 1999-2018 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% %
Cristy9ddfcca2018-09-09 19:46:34 -040026% https://imagemagick.org/script/license.php %
cristy3ed852e2009-09-05 21:47:34 +000027% %
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"
Dirk Lemstra06344a02017-10-15 10:10:01 +020054#include "MagickCore/memory-private.h"
cristy4c08aed2011-07-01 19:47:50 +000055#include "MagickCore/option.h"
56#include "MagickCore/semaphore.h"
57#include "MagickCore/string_.h"
58#include "MagickCore/splay-tree.h"
59#include "MagickCore/token.h"
60#include "MagickCore/utility.h"
cristyd1dd6e42011-09-04 01:46:08 +000061#include "MagickCore/utility-private.h"
cristy4c08aed2011-07-01 19:47:50 +000062#include "MagickCore/xml-tree.h"
cristy3291f512014-03-16 22:16:22 +000063#include "MagickCore/xml-tree-private.h"
cristy3ed852e2009-09-05 21:47:34 +000064
65/*
cristy54a531d2009-10-21 17:58:01 +000066 Typedef declarations.
cristy3ed852e2009-09-05 21:47:34 +000067*/
cristye3e77a12009-10-16 00:47:21 +000068typedef struct _CoderMapInfo
69{
70 const char
71 *magick,
72 *name;
73} CoderMapInfo;
cristy54a531d2009-10-21 17:58:01 +000074
75/*
76 Static declarations.
77*/
cristye3e77a12009-10-16 00:47:21 +000078static const CoderMapInfo
79 CoderMap[] =
80 {
81 { "3FR", "DNG" },
Cristyb066eae2017-01-20 09:15:54 -050082 { "3GP", "MPEG" },
83 { "3G2", "MPEG" },
cristye3e77a12009-10-16 00:47:21 +000084 { "8BIMTEXT", "META" },
85 { "8BIMWTEXT", "META" },
86 { "AFM", "TTF" },
87 { "A", "RAW" },
88 { "AI", "PDF" },
89 { "APP1JPEG", "META" },
90 { "APP1", "META" },
91 { "ARW", "DNG" },
cristy0fc02592010-05-24 14:12:38 +000092 { "AVI", "MPEG" },
cristye3e77a12009-10-16 00:47:21 +000093 { "BIE", "JBIG" },
94 { "BMP2", "BMP" },
95 { "BMP3", "BMP" },
96 { "B", "RAW" },
97 { "BRF", "BRAILLE" },
cristy90dbac72010-08-22 15:08:40 +000098 { "BGRA", "BGR" },
dirk2a2d3d62015-07-27 11:09:58 +000099 { "BGRO", "BGR" },
cristye3e77a12009-10-16 00:47:21 +0000100 { "CMYKA", "CMYK" },
101 { "C", "RAW" },
102 { "CAL", "CALS" },
cristyc05417c2010-09-21 16:30:32 +0000103 { "CANVAS", "XC" },
dirk2a2d3d62015-07-27 11:09:58 +0000104 { "CMYKA", "CMYK" },
cristye3e77a12009-10-16 00:47:21 +0000105 { "CR2", "DNG" },
106 { "CRW", "DNG" },
107 { "CUR", "ICON" },
Cristy6ffac4f2015-08-23 10:55:57 -0400108 { "DATA", "INLINE" },
cristye3e77a12009-10-16 00:47:21 +0000109 { "DCR", "DNG" },
110 { "DCX", "PCX" },
111 { "DFONT", "TTF" },
dirk2a2d3d62015-07-27 11:09:58 +0000112 { "DXT1", "DDS" },
113 { "DXT5", "DDS" },
cristye3e77a12009-10-16 00:47:21 +0000114 { "EPDF", "PDF" },
115 { "EPI", "PS" },
116 { "EPS2", "PS2" },
117 { "EPS3", "PS3" },
118 { "EPSF", "PS" },
119 { "EPSI", "PS" },
120 { "EPS", "PS" },
121 { "EPT2", "EPT" },
122 { "EPT3", "EPT" },
123 { "ERF", "DNG" },
124 { "EXIF", "META" },
125 { "FILE", "URL" },
126 { "FRACTAL", "PLASMA" },
127 { "FTP", "URL" },
128 { "FTS", "FITS" },
129 { "G3", "FAX" },
dirk52456ef2016-06-17 15:29:46 +0200130 { "G4", "FAX" },
cristye3e77a12009-10-16 00:47:21 +0000131 { "GIF87", "GIF" },
132 { "G", "RAW" },
cristye3e77a12009-10-16 00:47:21 +0000133 { "GRANITE", "MAGICK" },
Cristyb2d24412018-03-09 19:41:19 -0500134 { "GRAYA", "GRAY" },
cristye3e77a12009-10-16 00:47:21 +0000135 { "GROUP4", "TIFF" },
cristy7f47ac62013-02-10 14:16:17 +0000136 { "GV", "DOT" },
dirk2a2d3d62015-07-27 11:09:58 +0000137 { "HTM", "HTML" },
138 { "ICB", "TGA" },
139 { "ICO", "ICON" },
Cristyea59e6b2018-02-09 19:32:57 -0500140 { "ICODIB", "DIB" },
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" },
cristye3e77a12009-10-16 00:47:21 +0000208 { "PICON", "XPM" },
209 { "PJPEG", "JPEG" },
210 { "PM", "XPM" },
dirk2a2d3d62015-07-27 11:09:58 +0000211 { "PNG00", "PNG" },
cristye3e77a12009-10-16 00:47:21 +0000212 { "PNG24", "PNG" },
213 { "PNG32", "PNG" },
dirk2a2d3d62015-07-27 11:09:58 +0000214 { "PNG48", "PNG" },
215 { "PNG64", "PNG" },
cristye3e77a12009-10-16 00:47:21 +0000216 { "PNG8", "PNG" },
217 { "PPM", "PNM" },
cristyb4233012010-02-28 20:09:14 +0000218 { "PSB", "PSD" },
cristye3e77a12009-10-16 00:47:21 +0000219 { "PTIF", "TIFF" },
dirk2a2d3d62015-07-27 11:09:58 +0000220 { "R", "RAW" },
cristye3e77a12009-10-16 00:47:21 +0000221 { "RADIAL-GRADIENT", "GRADIENT" },
222 { "RAF", "DNG" },
223 { "RAS", "SUN" },
cristy1b1f0722014-06-10 10:14:59 +0000224 { "RAW", "DNG" },
cristye3e77a12009-10-16 00:47:21 +0000225 { "RGBA", "RGB" },
226 { "RGBO", "RGB" },
cristy15e22962014-02-21 22:56:26 +0000227 { "RMF", "DNG" },
cristye3e77a12009-10-16 00:47:21 +0000228 { "ROSE", "MAGICK" },
cristy98aceb82012-10-20 21:24:00 +0000229 { "RW2", "DNG" },
cristye3e77a12009-10-16 00:47:21 +0000230 { "SHTML", "HTML" },
dirka1e4ab82014-10-07 05:06:49 +0000231 { "SIX", "SIXEL" },
cristye6ee7492013-03-16 15:04:42 +0000232 { "SPARSE-COLOR", "TXT" },
cristye3e77a12009-10-16 00:47:21 +0000233 { "SR2", "DNG" },
234 { "SRF", "DNG" },
235 { "SVGZ", "SVG" },
236 { "TEXT", "TXT" },
237 { "TIFF64", "TIFF" },
238 { "TIF", "TIFF" },
239 { "TTC", "TTF" },
240 { "UBRL", "BRAILLE" },
Samuel Thibault531aa152015-11-01 23:36:24 +0100241 { "UBRL6", "BRAILLE" },
cristye3e77a12009-10-16 00:47:21 +0000242 { "VDA", "TGA" },
243 { "VST", "TGA" },
cristyed40d622011-01-17 15:51:00 +0000244 { "WIZARD", "MAGICK" },
dirk073ff372014-01-05 14:05:25 +0000245#if defined(MAGICKCORE_WINGDI32_DELEGATE)
246 { "WMF", "EMF" },
247#endif
cristye3e77a12009-10-16 00:47:21 +0000248 { "WMV", "MPEG" },
cristye3e77a12009-10-16 00:47:21 +0000249 { "WMZ", "WMF" },
250 { "X3f", "DNG" },
251 { "XMP", "META" },
252 { "XTRNARRAY", "XTRN" },
cristye3e77a12009-10-16 00:47:21 +0000253 { "XV", "VIFF" },
254 { "Y", "RAW" },
cristy54a531d2009-10-21 17:58:01 +0000255 { "YCbCrA", "YCbCr" }
cristye3e77a12009-10-16 00:47:21 +0000256 };
257
cristy3ed852e2009-09-05 21:47:34 +0000258static SemaphoreInfo
259 *coder_semaphore = (SemaphoreInfo *) NULL;
260
261static SplayTreeInfo
cristy86e5ac92014-03-16 19:27:39 +0000262 *coder_cache = (SplayTreeInfo *) NULL;
cristy3ed852e2009-09-05 21:47:34 +0000263
264/*
265 Forward declarations.
266*/
267static MagickBooleanType
Dirk Lemstrae2939a42018-10-04 23:20:08 +0200268 IsCoderTreeInstantiated(ExceptionInfo *);
cristy86e5ac92014-03-16 19:27:39 +0000269
270/*
271%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
272% %
273% %
274% %
cristy6c6322e2014-09-09 22:39:21 +0000275+ A c q u i r e C o d e r C a c h e %
cristy86e5ac92014-03-16 19:27:39 +0000276% %
277% %
278% %
279%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
280%
cristy6c6322e2014-09-09 22:39:21 +0000281% AcquireCoderCache() caches one or more coder configurations which provides a
282% mapping between coder attributes and a coder name.
cristy86e5ac92014-03-16 19:27:39 +0000283%
284% The format of the AcquireCoderCache coder is:
285%
286% SplayTreeInfo *AcquireCoderCache(const char *filename,
287% ExceptionInfo *exception)
288%
289% A description of each parameter follows:
290%
291% o filename: the font file name.
292%
293% o exception: return any errors or warnings in this structure.
294%
295*/
296
297static void *DestroyCoderNode(void *coder_info)
298{
299 register CoderInfo
300 *p;
301
302 p=(CoderInfo *) coder_info;
303 if (p->exempt == MagickFalse)
304 {
305 if (p->path != (char *) NULL)
306 p->path=DestroyString(p->path);
307 if (p->name != (char *) NULL)
308 p->name=DestroyString(p->name);
309 if (p->magick != (char *) NULL)
310 p->magick=DestroyString(p->magick);
311 }
312 return(RelinquishMagickMemory(p));
313}
314
Dirk Lemstrae2939a42018-10-04 23:20:08 +0200315static SplayTreeInfo *AcquireCoderCache(ExceptionInfo *exception)
cristy86e5ac92014-03-16 19:27:39 +0000316{
cristy86e5ac92014-03-16 19:27:39 +0000317 MagickStatusType
318 status;
319
320 register ssize_t
321 i;
322
323 SplayTreeInfo
dirkb18d8642016-07-17 19:32:43 +0200324 *cache;
cristy86e5ac92014-03-16 19:27:39 +0000325
326 /*
Dirk Lemstrae2939a42018-10-04 23:20:08 +0200327 Load built-in coder map.
cristy86e5ac92014-03-16 19:27:39 +0000328 */
dirkb18d8642016-07-17 19:32:43 +0200329 cache=NewSplayTree(CompareSplayTreeString,RelinquishMagickMemory,
cristy86e5ac92014-03-16 19:27:39 +0000330 DestroyCoderNode);
cristy86e5ac92014-03-16 19:27:39 +0000331 status=MagickTrue;
cristy86e5ac92014-03-16 19:27:39 +0000332 for (i=0; i < (ssize_t) (sizeof(CoderMap)/sizeof(*CoderMap)); i++)
333 {
334 CoderInfo
335 *coder_info;
336
337 register const CoderMapInfo
338 *p;
339
340 p=CoderMap+i;
341 coder_info=(CoderInfo *) AcquireMagickMemory(sizeof(*coder_info));
342 if (coder_info == (CoderInfo *) NULL)
343 {
344 (void) ThrowMagickException(exception,GetMagickModule(),
345 ResourceLimitError,"MemoryAllocationFailed","`%s'",p->name);
346 continue;
347 }
Cristy81bfff22018-03-10 07:58:31 -0500348 (void) memset(coder_info,0,sizeof(*coder_info));
cristy86e5ac92014-03-16 19:27:39 +0000349 coder_info->path=(char *) "[built-in]";
350 coder_info->magick=(char *) p->magick;
351 coder_info->name=(char *) p->name;
352 coder_info->exempt=MagickTrue;
cristye1c94d92015-06-28 12:16:33 +0000353 coder_info->signature=MagickCoreSignature;
dirkb18d8642016-07-17 19:32:43 +0200354 status&=AddValueToSplayTree(cache,ConstantString(coder_info->magick),
cristy86e5ac92014-03-16 19:27:39 +0000355 coder_info);
356 if (status == MagickFalse)
357 (void) ThrowMagickException(exception,GetMagickModule(),
358 ResourceLimitError,"MemoryAllocationFailed","`%s'",coder_info->name);
359 }
dirkb18d8642016-07-17 19:32:43 +0200360 return(cache);
cristy86e5ac92014-03-16 19:27:39 +0000361}
cristy3ed852e2009-09-05 21:47:34 +0000362
363/*
364%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
365% %
366% %
367% %
cristyf34a1452009-10-24 22:29:27 +0000368+ 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 +0000369% %
370% %
371% %
372%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
373%
cristyf34a1452009-10-24 22:29:27 +0000374% CoderComponentGenesis() instantiates the coder component.
cristy3ed852e2009-09-05 21:47:34 +0000375%
cristyf34a1452009-10-24 22:29:27 +0000376% The format of the CoderComponentGenesis method is:
cristy3ed852e2009-09-05 21:47:34 +0000377%
cristyf34a1452009-10-24 22:29:27 +0000378% MagickBooleanType CoderComponentGenesis(void)
cristy3ed852e2009-09-05 21:47:34 +0000379%
380*/
cristy5ff4eaf2011-09-03 01:38:02 +0000381MagickPrivate MagickBooleanType CoderComponentGenesis(void)
cristyf34a1452009-10-24 22:29:27 +0000382{
cristy7c977062014-04-04 14:05:53 +0000383 if (coder_semaphore == (SemaphoreInfo *) NULL)
384 coder_semaphore=AcquireSemaphoreInfo();
cristyf34a1452009-10-24 22:29:27 +0000385 return(MagickTrue);
386}
387
388/*
389%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
390% %
391% %
392% %
393+ C o d e r C o m p o n e n t T e r m i n u s %
394% %
395% %
396% %
397%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
398%
399% CoderComponentTerminus() destroys the coder component.
400%
401% The format of the CoderComponentTerminus method is:
402%
403% CoderComponentTerminus(void)
404%
405*/
cristy5ff4eaf2011-09-03 01:38:02 +0000406MagickPrivate void CoderComponentTerminus(void)
cristy3ed852e2009-09-05 21:47:34 +0000407{
cristy18b17442009-10-25 18:36:48 +0000408 if (coder_semaphore == (SemaphoreInfo *) NULL)
cristy04b11db2014-02-16 15:10:39 +0000409 ActivateSemaphoreInfo(&coder_semaphore);
cristyf84a1932010-01-03 18:00:18 +0000410 LockSemaphoreInfo(coder_semaphore);
cristy86e5ac92014-03-16 19:27:39 +0000411 if (coder_cache != (SplayTreeInfo *) NULL)
412 coder_cache=DestroySplayTree(coder_cache);
cristyf84a1932010-01-03 18:00:18 +0000413 UnlockSemaphoreInfo(coder_semaphore);
cristy3d162a92014-02-16 14:05:06 +0000414 RelinquishSemaphoreInfo(&coder_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000415}
416
417/*
418%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
419% %
420% %
421% %
422+ G e t C o d e r I n f o %
423% %
424% %
425% %
426%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
427%
428% GetCoderInfo searches the coder list for the specified name and if found
429% returns attributes for that coder.
430%
431% The format of the GetCoderInfo method is:
432%
433% const CoderInfo *GetCoderInfo(const char *name,ExceptionInfo *exception)
434%
435% A description of each parameter follows:
436%
437% o name: the coder name.
438%
439% o exception: return any errors or warnings in this structure.
440%
441*/
442MagickExport const CoderInfo *GetCoderInfo(const char *name,
443 ExceptionInfo *exception)
444{
445 assert(exception != (ExceptionInfo *) NULL);
cristy904e5912014-03-15 19:53:14 +0000446 if (IsCoderTreeInstantiated(exception) == MagickFalse)
447 return((const CoderInfo *) NULL);
cristy3ed852e2009-09-05 21:47:34 +0000448 if ((name == (const char *) NULL) || (LocaleCompare(name,"*") == 0))
Cristy1d8b1f12017-09-16 10:13:23 -0400449 return((const CoderInfo *) GetRootValueFromSplayTree(coder_cache));
450 return((const CoderInfo *) GetValueFromSplayTree(coder_cache,name));
cristy3ed852e2009-09-05 21:47:34 +0000451}
452
453/*
454%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
455% %
456% %
457% %
458% G e t C o d e r I n f o L i s t %
459% %
460% %
461% %
462%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
463%
464% GetCoderInfoList() returns any coder_map that match the specified pattern.
465% The format of the GetCoderInfoList function is:
466%
467% const CoderInfo **GetCoderInfoList(const char *pattern,
cristybb503372010-05-27 20:51:26 +0000468% size_t *number_coders,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000469%
470% A description of each parameter follows:
471%
472% o pattern: Specifies a pointer to a text string containing a pattern.
473%
474% o number_coders: This integer returns the number of coders in the list.
475%
476% o exception: return any errors or warnings in this structure.
477%
478*/
479
480static int CoderInfoCompare(const void *x,const void *y)
481{
482 const CoderInfo
483 **p,
484 **q;
485
486 p=(const CoderInfo **) x,
487 q=(const CoderInfo **) y;
488 if (LocaleCompare((*p)->path,(*q)->path) == 0)
489 return(LocaleCompare((*p)->name,(*q)->name));
490 return(LocaleCompare((*p)->path,(*q)->path));
491}
492
493MagickExport const CoderInfo **GetCoderInfoList(const char *pattern,
cristybb503372010-05-27 20:51:26 +0000494 size_t *number_coders,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000495{
496 const CoderInfo
497 **coder_map;
498
499 register const CoderInfo
500 *p;
501
cristybb503372010-05-27 20:51:26 +0000502 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000503 i;
504
505 /*
506 Allocate coder list.
507 */
508 assert(pattern != (char *) NULL);
509 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
cristybb503372010-05-27 20:51:26 +0000510 assert(number_coders != (size_t *) NULL);
cristy3ed852e2009-09-05 21:47:34 +0000511 *number_coders=0;
512 p=GetCoderInfo("*",exception);
513 if (p == (const CoderInfo *) NULL)
514 return((const CoderInfo **) NULL);
515 coder_map=(const CoderInfo **) AcquireQuantumMemory((size_t)
cristy86e5ac92014-03-16 19:27:39 +0000516 GetNumberOfNodesInSplayTree(coder_cache)+1UL,sizeof(*coder_map));
cristy3ed852e2009-09-05 21:47:34 +0000517 if (coder_map == (const CoderInfo **) NULL)
518 return((const CoderInfo **) NULL);
519 /*
520 Generate coder list.
521 */
cristyf84a1932010-01-03 18:00:18 +0000522 LockSemaphoreInfo(coder_semaphore);
cristy86e5ac92014-03-16 19:27:39 +0000523 ResetSplayTreeIterator(coder_cache);
524 p=(const CoderInfo *) GetNextValueInSplayTree(coder_cache);
cristy3ed852e2009-09-05 21:47:34 +0000525 for (i=0; p != (const CoderInfo *) NULL; )
526 {
527 if ((p->stealth == MagickFalse) &&
528 (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
529 coder_map[i++]=p;
cristy86e5ac92014-03-16 19:27:39 +0000530 p=(const CoderInfo *) GetNextValueInSplayTree(coder_cache);
cristy3ed852e2009-09-05 21:47:34 +0000531 }
cristyf84a1932010-01-03 18:00:18 +0000532 UnlockSemaphoreInfo(coder_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000533 qsort((void *) coder_map,(size_t) i,sizeof(*coder_map),CoderInfoCompare);
534 coder_map[i]=(CoderInfo *) NULL;
cristybb503372010-05-27 20:51:26 +0000535 *number_coders=(size_t) i;
cristy3ed852e2009-09-05 21:47:34 +0000536 return(coder_map);
537}
538
539/*
540%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
541% %
542% %
543% %
544% G e t C o d e r L i s t %
545% %
546% %
547% %
548%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
549%
550% GetCoderList() returns any coder_map that match the specified pattern.
551%
552% The format of the GetCoderList function is:
553%
cristybb503372010-05-27 20:51:26 +0000554% char **GetCoderList(const char *pattern,size_t *number_coders,
cristy3ed852e2009-09-05 21:47:34 +0000555% ExceptionInfo *exception)
556%
557% A description of each parameter follows:
558%
559% o pattern: Specifies a pointer to a text string containing a pattern.
560%
561% o number_coders: This integer returns the number of coders in the list.
562%
563% o exception: return any errors or warnings in this structure.
564%
565*/
566
567static int CoderCompare(const void *x,const void *y)
568{
569 register const char
570 **p,
571 **q;
572
573 p=(const char **) x;
574 q=(const char **) y;
575 return(LocaleCompare(*p,*q));
576}
577
578MagickExport char **GetCoderList(const char *pattern,
cristybb503372010-05-27 20:51:26 +0000579 size_t *number_coders,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000580{
581 char
582 **coder_map;
583
584 register const CoderInfo
585 *p;
586
cristybb503372010-05-27 20:51:26 +0000587 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000588 i;
589
590 /*
591 Allocate coder list.
592 */
593 assert(pattern != (char *) NULL);
594 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
cristybb503372010-05-27 20:51:26 +0000595 assert(number_coders != (size_t *) NULL);
cristy3ed852e2009-09-05 21:47:34 +0000596 *number_coders=0;
597 p=GetCoderInfo("*",exception);
598 if (p == (const CoderInfo *) NULL)
599 return((char **) NULL);
600 coder_map=(char **) AcquireQuantumMemory((size_t)
cristy86e5ac92014-03-16 19:27:39 +0000601 GetNumberOfNodesInSplayTree(coder_cache)+1UL,sizeof(*coder_map));
cristy3ed852e2009-09-05 21:47:34 +0000602 if (coder_map == (char **) NULL)
603 return((char **) NULL);
604 /*
605 Generate coder list.
606 */
cristyf84a1932010-01-03 18:00:18 +0000607 LockSemaphoreInfo(coder_semaphore);
cristy86e5ac92014-03-16 19:27:39 +0000608 ResetSplayTreeIterator(coder_cache);
609 p=(const CoderInfo *) GetNextValueInSplayTree(coder_cache);
cristy3ed852e2009-09-05 21:47:34 +0000610 for (i=0; p != (const CoderInfo *) NULL; )
611 {
612 if ((p->stealth == MagickFalse) &&
613 (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
614 coder_map[i++]=ConstantString(p->name);
cristy86e5ac92014-03-16 19:27:39 +0000615 p=(const CoderInfo *) GetNextValueInSplayTree(coder_cache);
cristy3ed852e2009-09-05 21:47:34 +0000616 }
cristyf84a1932010-01-03 18:00:18 +0000617 UnlockSemaphoreInfo(coder_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000618 qsort((void *) coder_map,(size_t) i,sizeof(*coder_map),CoderCompare);
619 coder_map[i]=(char *) NULL;
cristybb503372010-05-27 20:51:26 +0000620 *number_coders=(size_t) i;
cristy3ed852e2009-09-05 21:47:34 +0000621 return(coder_map);
622}
623
624/*
625%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
626% %
627% %
628% %
cristy904e5912014-03-15 19:53:14 +0000629+ 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 +0000630% %
631% %
632% %
633%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
634%
cristycd2cd182014-03-18 12:10:55 +0000635% IsCoderTreeInstantiated() determines if the coder tree is instantiated. If
636% not, it instantiates the tree and returns it.
cristy3ed852e2009-09-05 21:47:34 +0000637%
cristy904e5912014-03-15 19:53:14 +0000638% The format of the IsCoderInstantiated method is:
cristy3ed852e2009-09-05 21:47:34 +0000639%
cristy904e5912014-03-15 19:53:14 +0000640% MagickBooleanType IsCoderTreeInstantiated(ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000641%
642% A description of each parameter follows.
643%
644% o exception: return any errors or warnings in this structure.
645%
646*/
cristy904e5912014-03-15 19:53:14 +0000647static MagickBooleanType IsCoderTreeInstantiated(ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000648{
cristy86e5ac92014-03-16 19:27:39 +0000649 if (coder_cache == (SplayTreeInfo *) NULL)
650 {
651 if (coder_semaphore == (SemaphoreInfo *) NULL)
652 ActivateSemaphoreInfo(&coder_semaphore);
653 LockSemaphoreInfo(coder_semaphore);
654 if (coder_cache == (SplayTreeInfo *) NULL)
Dirk Lemstrae2939a42018-10-04 23:20:08 +0200655 coder_cache=AcquireCoderCache(exception);
cristy86e5ac92014-03-16 19:27:39 +0000656 UnlockSemaphoreInfo(coder_semaphore);
657 }
658 return(coder_cache != (SplayTreeInfo *) NULL ? MagickTrue : MagickFalse);
cristy3ed852e2009-09-05 21:47:34 +0000659}
660
661/*
662%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
663% %
664% %
665% %
666% L i s t C o d e r I n f o %
667% %
668% %
669% %
670%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
671%
672% ListCoderInfo() lists the coder info to a file.
673%
674% The format of the ListCoderInfo coder is:
675%
676% MagickBooleanType ListCoderInfo(FILE *file,ExceptionInfo *exception)
677%
678% A description of each parameter follows.
679%
680% o file: An pointer to a FILE.
681%
682% o exception: return any errors or warnings in this structure.
683%
684*/
685MagickExport MagickBooleanType ListCoderInfo(FILE *file,
686 ExceptionInfo *exception)
687{
688 const char
689 *path;
690
691 const CoderInfo
692 **coder_info;
693
cristybb503372010-05-27 20:51:26 +0000694 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000695 i;
696
cristybb503372010-05-27 20:51:26 +0000697 size_t
cristy3ed852e2009-09-05 21:47:34 +0000698 number_coders;
699
cristy9d314ff2011-03-09 01:30:28 +0000700 ssize_t
701 j;
702
cristy3ed852e2009-09-05 21:47:34 +0000703 if (file == (const FILE *) NULL)
704 file=stdout;
705 coder_info=GetCoderInfoList("*",&number_coders,exception);
706 if (coder_info == (const CoderInfo **) NULL)
707 return(MagickFalse);
708 path=(const char *) NULL;
cristybb503372010-05-27 20:51:26 +0000709 for (i=0; i < (ssize_t) number_coders; i++)
cristy3ed852e2009-09-05 21:47:34 +0000710 {
711 if (coder_info[i]->stealth != MagickFalse)
712 continue;
713 if ((path == (const char *) NULL) ||
714 (LocaleCompare(path,coder_info[i]->path) != 0))
715 {
716 if (coder_info[i]->path != (char *) NULL)
cristyb51dff52011-05-19 16:55:47 +0000717 (void) FormatLocaleFile(file,"\nPath: %s\n\n",coder_info[i]->path);
718 (void) FormatLocaleFile(file,"Magick Coder\n");
cristy1e604812011-05-19 18:07:50 +0000719 (void) FormatLocaleFile(file,
720 "-------------------------------------------------"
cristy3ed852e2009-09-05 21:47:34 +0000721 "------------------------------\n");
722 }
723 path=coder_info[i]->path;
cristyb51dff52011-05-19 16:55:47 +0000724 (void) FormatLocaleFile(file,"%s",coder_info[i]->magick);
cristybb503372010-05-27 20:51:26 +0000725 for (j=(ssize_t) strlen(coder_info[i]->magick); j <= 11; j++)
cristyb51dff52011-05-19 16:55:47 +0000726 (void) FormatLocaleFile(file," ");
cristy3ed852e2009-09-05 21:47:34 +0000727 if (coder_info[i]->name != (char *) NULL)
cristyb51dff52011-05-19 16:55:47 +0000728 (void) FormatLocaleFile(file,"%s",coder_info[i]->name);
729 (void) FormatLocaleFile(file,"\n");
cristy3ed852e2009-09-05 21:47:34 +0000730 }
731 coder_info=(const CoderInfo **) RelinquishMagickMemory((void *) coder_info);
732 (void) fflush(file);
733 return(MagickTrue);
734}