blob: 7eb93c0d7ea06fb54b00790c187d621da2582699 [file] [log] [blame]
cristy3ed852e2009-09-05 21:47:34 +00001/*
2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3% %
4% %
5% %
6% CCCC OOO DDDD EEEEE RRRR %
7% C O O D D E R R %
8% C O O D D EEE RRRR %
9% C O O D D E R R %
10% CCCC OOO DDDD EEEEE R R %
11% %
12% %
13% MagickCore Image Coder Methods %
14% %
15% Software Design %
16% John Cristy %
17% May 2001 %
18% %
19% %
cristy7e41fe82010-12-04 23:12:08 +000020% Copyright 1999-2011 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*/
42#include "magick/studio.h"
43#include "magick/blob.h"
44#include "magick/client.h"
45#include "magick/coder.h"
46#include "magick/configure.h"
47#include "magick/draw.h"
48#include "magick/exception.h"
49#include "magick/exception-private.h"
50#include "magick/hashmap.h"
51#include "magick/log.h"
52#include "magick/memory_.h"
53#include "magick/option.h"
54#include "magick/semaphore.h"
55#include "magick/string_.h"
56#include "magick/splay-tree.h"
57#include "magick/token.h"
58#include "magick/utility.h"
59#include "magick/xml-tree.h"
60
61/*
62 Define declarations.
63*/
64#define MagickCoderFilename "coder.xml"
65
66/*
cristy54a531d2009-10-21 17:58:01 +000067 Typedef declarations.
cristy3ed852e2009-09-05 21:47:34 +000068*/
cristye3e77a12009-10-16 00:47:21 +000069typedef struct _CoderMapInfo
70{
71 const char
72 *magick,
73 *name;
74} CoderMapInfo;
cristy54a531d2009-10-21 17:58:01 +000075
76/*
77 Static declarations.
78*/
cristye3e77a12009-10-16 00:47:21 +000079static const CoderMapInfo
80 CoderMap[] =
81 {
82 { "3FR", "DNG" },
83 { "8BIM", "META" },
84 { "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" },
cristye3e77a12009-10-16 00:47:21 +000099 { "CMYKA", "CMYK" },
100 { "C", "RAW" },
101 { "CAL", "CALS" },
cristyc05417c2010-09-21 16:30:32 +0000102 { "CANVAS", "XC" },
cristye3e77a12009-10-16 00:47:21 +0000103 { "CR2", "DNG" },
104 { "CRW", "DNG" },
105 { "CUR", "ICON" },
106 { "DCR", "DNG" },
107 { "DCX", "PCX" },
108 { "DFONT", "TTF" },
109 { "EPDF", "PDF" },
110 { "EPI", "PS" },
111 { "EPS2", "PS2" },
112 { "EPS3", "PS3" },
113 { "EPSF", "PS" },
114 { "EPSI", "PS" },
115 { "EPS", "PS" },
116 { "EPT2", "EPT" },
117 { "EPT3", "EPT" },
118 { "ERF", "DNG" },
119 { "EXIF", "META" },
120 { "FILE", "URL" },
121 { "FRACTAL", "PLASMA" },
122 { "FTP", "URL" },
123 { "FTS", "FITS" },
124 { "G3", "FAX" },
125 { "GIF87", "GIF" },
126 { "G", "RAW" },
cristye3e77a12009-10-16 00:47:21 +0000127 { "GRANITE", "MAGICK" },
128 { "GROUP4", "TIFF" },
129 { "K25", "DNG" },
130 { "KDC", "DNG" },
131 { "H", "MAGICK" },
132 { "HTM", "HTML" },
133 { "HTTP", "URL" },
134 { "ICB", "TGA" },
135 { "ICC", "META" },
136 { "ICM", "META" },
137 { "ICO", "ICON" },
138 { "IMPLICIT", "***" },
139 { "IPTC", "META" },
140 { "IPTCTEXT", "META" },
141 { "IPTCWTEXT", "META" },
142 { "ISOBRL", "BRAILLE" },
143 { "JBG", "JBIG" },
144 { "JNG", "PNG" },
145 { "JPC", "JP2" },
cristy02f1fda2009-12-10 15:23:56 +0000146 { "J2C", "JP2" },
cristye3e77a12009-10-16 00:47:21 +0000147 { "JPG", "JPEG" },
148 { "JPX", "JP2" },
149 { "K", "RAW" },
150 { "LOGO", "MAGICK" },
151 { "M2V", "MPEG" },
152 { "M4V", "MPEG" },
153 { "M", "RAW" },
154 { "MNG", "PNG" },
cristy1dbd5a32010-11-28 22:23:27 +0000155 { "MOV", "MPEG" },
cristye3e77a12009-10-16 00:47:21 +0000156 { "MP4", "MPEG" },
157 { "MPG", "MPEG" },
158 { "MPRI", "MPR" },
159 { "MRW", "DNG" },
160 { "MSVG", "SVG" },
161 { "NEF", "DNG" },
162 { "NETSCAPE", "MAGICK" },
163 { "O", "RAW" },
164 { "ORF", "DNG" },
165 { "OTF", "TTF" },
166 { "P7", "PNM" },
167 { "PAL", "UYVY" },
168 { "PAM", "PNM" },
169 { "PBM", "PNM" },
170 { "PCDS", "PCD" },
171 { "PDFA", "PDF" },
172 { "PEF", "DNG" },
173 { "PEF", "DNG" },
174 { "PFA", "TTF" },
175 { "PFB", "TTF" },
176 { "PFM", "PNM" },
177 { "PGM", "PNM" },
178 { "PGX", "JP2" },
179 { "PICON", "XPM" },
180 { "PJPEG", "JPEG" },
181 { "PM", "XPM" },
182 { "PNG24", "PNG" },
183 { "PNG32", "PNG" },
184 { "PNG8", "PNG" },
185 { "PPM", "PNM" },
cristyb4233012010-02-28 20:09:14 +0000186 { "PSB", "PSD" },
cristye3e77a12009-10-16 00:47:21 +0000187 { "PTIF", "TIFF" },
188 { "RADIAL-GRADIENT", "GRADIENT" },
189 { "RAF", "DNG" },
190 { "RAS", "SUN" },
cristye3e77a12009-10-16 00:47:21 +0000191 { "RGBA", "RGB" },
192 { "RGBO", "RGB" },
193 { "R", "RAW" },
194 { "ROSE", "MAGICK" },
195 { "SHTML", "HTML" },
196 { "SR2", "DNG" },
197 { "SRF", "DNG" },
198 { "SVGZ", "SVG" },
199 { "TEXT", "TXT" },
200 { "TIFF64", "TIFF" },
201 { "TIF", "TIFF" },
202 { "TTC", "TTF" },
203 { "UBRL", "BRAILLE" },
204 { "VDA", "TGA" },
205 { "VST", "TGA" },
cristyed40d622011-01-17 15:51:00 +0000206 { "WIZARD", "MAGICK" },
cristye3e77a12009-10-16 00:47:21 +0000207 { "WMV", "MPEG" },
208 { "WMFWIN32", "EMF" },
209 { "WMZ", "WMF" },
210 { "X3f", "DNG" },
211 { "XMP", "META" },
212 { "XTRNARRAY", "XTRN" },
213 { "XTRNBLOB", "XTRN" },
cristye3e77a12009-10-16 00:47:21 +0000214 { "XTRNFILE", "XTRN" },
215 { "XTRNIMAGE", "XTRN" },
cristye3e77a12009-10-16 00:47:21 +0000216 { "XV", "VIFF" },
217 { "Y", "RAW" },
cristy54a531d2009-10-21 17:58:01 +0000218 { "YCbCrA", "YCbCr" }
cristye3e77a12009-10-16 00:47:21 +0000219 };
220
cristy3ed852e2009-09-05 21:47:34 +0000221static SemaphoreInfo
222 *coder_semaphore = (SemaphoreInfo *) NULL;
223
224static SplayTreeInfo
225 *coder_list = (SplayTreeInfo *) NULL;
226
227static volatile MagickBooleanType
228 instantiate_coder = MagickFalse;
229
230/*
231 Forward declarations.
232*/
233static MagickBooleanType
234 InitializeCoderList(ExceptionInfo *),
235 LoadCoderLists(const char *,ExceptionInfo *);
236
237/*
238%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
239% %
240% %
241% %
cristyf34a1452009-10-24 22:29:27 +0000242+ 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 +0000243% %
244% %
245% %
246%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
247%
cristyf34a1452009-10-24 22:29:27 +0000248% CoderComponentGenesis() instantiates the coder component.
cristy3ed852e2009-09-05 21:47:34 +0000249%
cristyf34a1452009-10-24 22:29:27 +0000250% The format of the CoderComponentGenesis method is:
cristy3ed852e2009-09-05 21:47:34 +0000251%
cristyf34a1452009-10-24 22:29:27 +0000252% MagickBooleanType CoderComponentGenesis(void)
cristy3ed852e2009-09-05 21:47:34 +0000253%
254*/
cristyf34a1452009-10-24 22:29:27 +0000255MagickExport MagickBooleanType CoderComponentGenesis(void)
256{
cristy165b6092009-10-26 13:52:10 +0000257 AcquireSemaphoreInfo(&coder_semaphore);
cristyf34a1452009-10-24 22:29:27 +0000258 return(MagickTrue);
259}
260
261/*
262%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
263% %
264% %
265% %
266+ C o d e r C o m p o n e n t T e r m i n u s %
267% %
268% %
269% %
270%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
271%
272% CoderComponentTerminus() destroys the coder component.
273%
274% The format of the CoderComponentTerminus method is:
275%
276% CoderComponentTerminus(void)
277%
278*/
279MagickExport void CoderComponentTerminus(void)
cristy3ed852e2009-09-05 21:47:34 +0000280{
cristy18b17442009-10-25 18:36:48 +0000281 if (coder_semaphore == (SemaphoreInfo *) NULL)
282 AcquireSemaphoreInfo(&coder_semaphore);
cristyf84a1932010-01-03 18:00:18 +0000283 LockSemaphoreInfo(coder_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000284 if (coder_list != (SplayTreeInfo *) NULL)
285 coder_list=DestroySplayTree(coder_list);
286 instantiate_coder=MagickFalse;
cristyf84a1932010-01-03 18:00:18 +0000287 UnlockSemaphoreInfo(coder_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000288 DestroySemaphoreInfo(&coder_semaphore);
289}
290
291/*
292%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
293% %
294% %
295% %
296+ G e t C o d e r I n f o %
297% %
298% %
299% %
300%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
301%
302% GetCoderInfo searches the coder list for the specified name and if found
303% returns attributes for that coder.
304%
305% The format of the GetCoderInfo method is:
306%
307% const CoderInfo *GetCoderInfo(const char *name,ExceptionInfo *exception)
308%
309% A description of each parameter follows:
310%
311% o name: the coder name.
312%
313% o exception: return any errors or warnings in this structure.
314%
315*/
316MagickExport const CoderInfo *GetCoderInfo(const char *name,
317 ExceptionInfo *exception)
318{
319 assert(exception != (ExceptionInfo *) NULL);
320 if ((coder_list == (SplayTreeInfo *) NULL) ||
321 (instantiate_coder == MagickFalse))
322 if (InitializeCoderList(exception) == MagickFalse)
323 return((const CoderInfo *) NULL);
324 if ((coder_list == (SplayTreeInfo *) NULL) ||
325 (GetNumberOfNodesInSplayTree(coder_list) == 0))
326 return((const CoderInfo *) NULL);
327 if ((name == (const char *) NULL) || (LocaleCompare(name,"*") == 0))
328 {
329 ResetSplayTreeIterator(coder_list);
330 return((const CoderInfo *) GetNextValueInSplayTree(coder_list));
331 }
332 return((const CoderInfo *) GetValueFromSplayTree(coder_list,name));
333}
334
335/*
336%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
337% %
338% %
339% %
340% G e t C o d e r I n f o L i s t %
341% %
342% %
343% %
344%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
345%
346% GetCoderInfoList() returns any coder_map that match the specified pattern.
347% The format of the GetCoderInfoList function is:
348%
349% const CoderInfo **GetCoderInfoList(const char *pattern,
cristybb503372010-05-27 20:51:26 +0000350% size_t *number_coders,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000351%
352% A description of each parameter follows:
353%
354% o pattern: Specifies a pointer to a text string containing a pattern.
355%
356% o number_coders: This integer returns the number of coders in the list.
357%
358% o exception: return any errors or warnings in this structure.
359%
360*/
361
362static int CoderInfoCompare(const void *x,const void *y)
363{
364 const CoderInfo
365 **p,
366 **q;
367
368 p=(const CoderInfo **) x,
369 q=(const CoderInfo **) y;
370 if (LocaleCompare((*p)->path,(*q)->path) == 0)
371 return(LocaleCompare((*p)->name,(*q)->name));
372 return(LocaleCompare((*p)->path,(*q)->path));
373}
374
375MagickExport const CoderInfo **GetCoderInfoList(const char *pattern,
cristybb503372010-05-27 20:51:26 +0000376 size_t *number_coders,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000377{
378 const CoderInfo
379 **coder_map;
380
381 register const CoderInfo
382 *p;
383
cristybb503372010-05-27 20:51:26 +0000384 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000385 i;
386
387 /*
388 Allocate coder list.
389 */
390 assert(pattern != (char *) NULL);
391 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
cristybb503372010-05-27 20:51:26 +0000392 assert(number_coders != (size_t *) NULL);
cristy3ed852e2009-09-05 21:47:34 +0000393 *number_coders=0;
394 p=GetCoderInfo("*",exception);
395 if (p == (const CoderInfo *) NULL)
396 return((const CoderInfo **) NULL);
397 coder_map=(const CoderInfo **) AcquireQuantumMemory((size_t)
398 GetNumberOfNodesInSplayTree(coder_list)+1UL,sizeof(*coder_map));
399 if (coder_map == (const CoderInfo **) NULL)
400 return((const CoderInfo **) NULL);
401 /*
402 Generate coder list.
403 */
cristyf84a1932010-01-03 18:00:18 +0000404 LockSemaphoreInfo(coder_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000405 ResetSplayTreeIterator(coder_list);
406 p=(const CoderInfo *) GetNextValueInSplayTree(coder_list);
407 for (i=0; p != (const CoderInfo *) NULL; )
408 {
409 if ((p->stealth == MagickFalse) &&
410 (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
411 coder_map[i++]=p;
412 p=(const CoderInfo *) GetNextValueInSplayTree(coder_list);
413 }
cristyf84a1932010-01-03 18:00:18 +0000414 UnlockSemaphoreInfo(coder_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000415 qsort((void *) coder_map,(size_t) i,sizeof(*coder_map),CoderInfoCompare);
416 coder_map[i]=(CoderInfo *) NULL;
cristybb503372010-05-27 20:51:26 +0000417 *number_coders=(size_t) i;
cristy3ed852e2009-09-05 21:47:34 +0000418 return(coder_map);
419}
420
421/*
422%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
423% %
424% %
425% %
426% G e t C o d e r L i s t %
427% %
428% %
429% %
430%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
431%
432% GetCoderList() returns any coder_map that match the specified pattern.
433%
434% The format of the GetCoderList function is:
435%
cristybb503372010-05-27 20:51:26 +0000436% char **GetCoderList(const char *pattern,size_t *number_coders,
cristy3ed852e2009-09-05 21:47:34 +0000437% ExceptionInfo *exception)
438%
439% A description of each parameter follows:
440%
441% o pattern: Specifies a pointer to a text string containing a pattern.
442%
443% o number_coders: This integer returns the number of coders in the list.
444%
445% o exception: return any errors or warnings in this structure.
446%
447*/
448
449static int CoderCompare(const void *x,const void *y)
450{
451 register const char
452 **p,
453 **q;
454
455 p=(const char **) x;
456 q=(const char **) y;
457 return(LocaleCompare(*p,*q));
458}
459
460MagickExport char **GetCoderList(const char *pattern,
cristybb503372010-05-27 20:51:26 +0000461 size_t *number_coders,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000462{
463 char
464 **coder_map;
465
466 register const CoderInfo
467 *p;
468
cristybb503372010-05-27 20:51:26 +0000469 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000470 i;
471
472 /*
473 Allocate coder list.
474 */
475 assert(pattern != (char *) NULL);
476 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
cristybb503372010-05-27 20:51:26 +0000477 assert(number_coders != (size_t *) NULL);
cristy3ed852e2009-09-05 21:47:34 +0000478 *number_coders=0;
479 p=GetCoderInfo("*",exception);
480 if (p == (const CoderInfo *) NULL)
481 return((char **) NULL);
482 coder_map=(char **) AcquireQuantumMemory((size_t)
483 GetNumberOfNodesInSplayTree(coder_list)+1UL,sizeof(*coder_map));
484 if (coder_map == (char **) NULL)
485 return((char **) NULL);
486 /*
487 Generate coder list.
488 */
cristyf84a1932010-01-03 18:00:18 +0000489 LockSemaphoreInfo(coder_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000490 ResetSplayTreeIterator(coder_list);
491 p=(const CoderInfo *) GetNextValueInSplayTree(coder_list);
492 for (i=0; p != (const CoderInfo *) NULL; )
493 {
494 if ((p->stealth == MagickFalse) &&
495 (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
496 coder_map[i++]=ConstantString(p->name);
497 p=(const CoderInfo *) GetNextValueInSplayTree(coder_list);
498 }
cristyf84a1932010-01-03 18:00:18 +0000499 UnlockSemaphoreInfo(coder_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000500 qsort((void *) coder_map,(size_t) i,sizeof(*coder_map),CoderCompare);
501 coder_map[i]=(char *) NULL;
cristybb503372010-05-27 20:51:26 +0000502 *number_coders=(size_t) i;
cristy3ed852e2009-09-05 21:47:34 +0000503 return(coder_map);
504}
505
506/*
507%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
508% %
509% %
510% %
511+ I n i t i a l i z e C o d e r L i s t %
512% %
513% %
514% %
515%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
516%
517% InitializeCoderList() initializes the coder list.
518%
519% The format of the InitializeCoderList method is:
520%
521% MagickBooleanType InitializeCoderList(ExceptionInfo *exception)
522%
523% A description of each parameter follows.
524%
525% o exception: return any errors or warnings in this structure.
526%
527*/
528static MagickBooleanType InitializeCoderList(ExceptionInfo *exception)
529{
530 if ((coder_list == (SplayTreeInfo *) NULL) &&
531 (instantiate_coder == MagickFalse))
532 {
cristy4e1dff62009-10-25 20:36:03 +0000533 if (coder_semaphore == (SemaphoreInfo *) NULL)
534 AcquireSemaphoreInfo(&coder_semaphore);
cristyf84a1932010-01-03 18:00:18 +0000535 LockSemaphoreInfo(coder_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000536 if ((coder_list == (SplayTreeInfo *) NULL) &&
537 (instantiate_coder == MagickFalse))
538 {
539 (void) LoadCoderLists(MagickCoderFilename,exception);
540 instantiate_coder=MagickTrue;
541 }
cristyf84a1932010-01-03 18:00:18 +0000542 UnlockSemaphoreInfo(coder_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000543 }
544 return(coder_list != (SplayTreeInfo *) NULL ? MagickTrue : MagickFalse);
545}
546
547/*
548%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
549% %
550% %
551% %
552% L i s t C o d e r I n f o %
553% %
554% %
555% %
556%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
557%
558% ListCoderInfo() lists the coder info to a file.
559%
560% The format of the ListCoderInfo coder is:
561%
562% MagickBooleanType ListCoderInfo(FILE *file,ExceptionInfo *exception)
563%
564% A description of each parameter follows.
565%
566% o file: An pointer to a FILE.
567%
568% o exception: return any errors or warnings in this structure.
569%
570*/
571MagickExport MagickBooleanType ListCoderInfo(FILE *file,
572 ExceptionInfo *exception)
573{
574 const char
575 *path;
576
577 const CoderInfo
578 **coder_info;
579
cristybb503372010-05-27 20:51:26 +0000580 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000581 i;
582
cristybb503372010-05-27 20:51:26 +0000583 size_t
cristy3ed852e2009-09-05 21:47:34 +0000584 number_coders;
585
cristy9d314ff2011-03-09 01:30:28 +0000586 ssize_t
587 j;
588
cristy3ed852e2009-09-05 21:47:34 +0000589 if (file == (const FILE *) NULL)
590 file=stdout;
591 coder_info=GetCoderInfoList("*",&number_coders,exception);
592 if (coder_info == (const CoderInfo **) NULL)
593 return(MagickFalse);
594 path=(const char *) NULL;
cristybb503372010-05-27 20:51:26 +0000595 for (i=0; i < (ssize_t) number_coders; i++)
cristy3ed852e2009-09-05 21:47:34 +0000596 {
597 if (coder_info[i]->stealth != MagickFalse)
598 continue;
599 if ((path == (const char *) NULL) ||
600 (LocaleCompare(path,coder_info[i]->path) != 0))
601 {
602 if (coder_info[i]->path != (char *) NULL)
cristyb51dff52011-05-19 16:55:47 +0000603 (void) FormatLocaleFile(file,"\nPath: %s\n\n",coder_info[i]->path);
604 (void) FormatLocaleFile(file,"Magick Coder\n");
cristy1e604812011-05-19 18:07:50 +0000605 (void) FormatLocaleFile(file,
606 "-------------------------------------------------"
cristy3ed852e2009-09-05 21:47:34 +0000607 "------------------------------\n");
608 }
609 path=coder_info[i]->path;
cristyb51dff52011-05-19 16:55:47 +0000610 (void) FormatLocaleFile(file,"%s",coder_info[i]->magick);
cristybb503372010-05-27 20:51:26 +0000611 for (j=(ssize_t) strlen(coder_info[i]->magick); j <= 11; j++)
cristyb51dff52011-05-19 16:55:47 +0000612 (void) FormatLocaleFile(file," ");
cristy3ed852e2009-09-05 21:47:34 +0000613 if (coder_info[i]->name != (char *) NULL)
cristyb51dff52011-05-19 16:55:47 +0000614 (void) FormatLocaleFile(file,"%s",coder_info[i]->name);
615 (void) FormatLocaleFile(file,"\n");
cristy3ed852e2009-09-05 21:47:34 +0000616 }
617 coder_info=(const CoderInfo **) RelinquishMagickMemory((void *) coder_info);
618 (void) fflush(file);
619 return(MagickTrue);
620}
621
622/*
623%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
624% %
625% %
626% %
627+ L o a d C o d e r L i s t %
628% %
629% %
630% %
631%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
632%
633% LoadCoderList() loads the coder configuration file which provides a
634% mapping between coder attributes and a coder name.
635%
636% The format of the LoadCoderList coder is:
637%
638% MagickBooleanType LoadCoderList(const char *xml,const char *filename,
cristybb503372010-05-27 20:51:26 +0000639% const size_t depth,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000640%
641% A description of each parameter follows:
642%
643% o xml: The coder list in XML format.
644%
645% o filename: The coder list filename.
646%
647% o depth: depth of <include /> statements.
648%
649% o exception: return any errors or warnings in this structure.
650%
651*/
652
653static void *DestroyCoderNode(void *coder_info)
654{
655 register CoderInfo
656 *p;
657
658 p=(CoderInfo *) coder_info;
cristye3e77a12009-10-16 00:47:21 +0000659 if (p->exempt == MagickFalse)
660 {
661 if (p->path != (char *) NULL)
662 p->path=DestroyString(p->path);
663 if (p->name != (char *) NULL)
664 p->name=DestroyString(p->name);
665 if (p->magick != (char *) NULL)
666 p->magick=DestroyString(p->magick);
667 }
cristy3ed852e2009-09-05 21:47:34 +0000668 return(RelinquishMagickMemory(p));
669}
670
671static MagickBooleanType LoadCoderList(const char *xml,const char *filename,
cristybb503372010-05-27 20:51:26 +0000672 const size_t depth,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000673{
674 char
675 keyword[MaxTextExtent],
676 *token;
677
678 const char
679 *q;
680
681 CoderInfo
682 *coder_info;
683
684 MagickBooleanType
685 status;
686
687 /*
688 Load the coder map file.
689 */
690 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
691 "Loading coder configuration file \"%s\" ...",filename);
692 if (xml == (const char *) NULL)
693 return(MagickFalse);
694 if (coder_list == (SplayTreeInfo *) NULL)
695 {
696 coder_list=NewSplayTree(CompareSplayTreeString,RelinquishMagickMemory,
697 DestroyCoderNode);
698 if (coder_list == (SplayTreeInfo *) NULL)
699 {
700 ThrowFileException(exception,ResourceLimitError,
701 "MemoryAllocationFailed",filename);
702 return(MagickFalse);
703 }
704 }
705 status=MagickTrue;
706 coder_info=(CoderInfo *) NULL;
707 token=AcquireString(xml);
708 for (q=(char *) xml; *q != '\0'; )
709 {
710 /*
711 Interpret XML.
712 */
713 GetMagickToken(q,&q,token);
714 if (*token == '\0')
715 break;
716 (void) CopyMagickString(keyword,token,MaxTextExtent);
717 if (LocaleNCompare(keyword,"<!DOCTYPE",9) == 0)
718 {
719 /*
720 Doctype element.
721 */
722 while ((LocaleNCompare(q,"]>",2) != 0) && (*q != '\0'))
723 GetMagickToken(q,&q,token);
724 continue;
725 }
726 if (LocaleNCompare(keyword,"<!--",4) == 0)
727 {
728 /*
729 Comment element.
730 */
731 while ((LocaleNCompare(q,"->",2) != 0) && (*q != '\0'))
732 GetMagickToken(q,&q,token);
733 continue;
734 }
735 if (LocaleCompare(keyword,"<include") == 0)
736 {
737 /*
738 Include element.
739 */
740 while (((*token != '/') && (*(token+1) != '>')) && (*q != '\0'))
741 {
742 (void) CopyMagickString(keyword,token,MaxTextExtent);
743 GetMagickToken(q,&q,token);
744 if (*token != '=')
745 continue;
746 GetMagickToken(q,&q,token);
747 if (LocaleCompare(keyword,"file") == 0)
748 {
749 if (depth > 200)
750 (void) ThrowMagickException(exception,GetMagickModule(),
751 ConfigureError,"IncludeNodeNestedTooDeeply","`%s'",token);
752 else
753 {
754 char
755 path[MaxTextExtent],
756 *xml;
757
758 GetPathComponent(filename,HeadPath,path);
759 if (*path != '\0')
760 (void) ConcatenateMagickString(path,DirectorySeparator,
761 MaxTextExtent);
762 if (*token == *DirectorySeparator)
763 (void) CopyMagickString(path,token,MaxTextExtent);
764 else
765 (void) ConcatenateMagickString(path,token,MaxTextExtent);
766 xml=FileToString(path,~0,exception);
767 if (xml != (char *) NULL)
768 {
769 status=LoadCoderList(xml,path,depth+1,exception);
770 xml=(char *) RelinquishMagickMemory(xml);
771 }
772 }
773 }
774 }
775 continue;
776 }
777 if (LocaleCompare(keyword,"<coder") == 0)
778 {
779 /*
780 Coder element.
781 */
cristy73bd4a52010-10-05 11:24:23 +0000782 coder_info=(CoderInfo *) AcquireMagickMemory(sizeof(*coder_info));
cristy3ed852e2009-09-05 21:47:34 +0000783 if (coder_info == (CoderInfo *) NULL)
784 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
785 (void) ResetMagickMemory(coder_info,0,sizeof(*coder_info));
786 coder_info->path=ConstantString(filename);
cristye3e77a12009-10-16 00:47:21 +0000787 coder_info->exempt=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +0000788 coder_info->signature=MagickSignature;
789 continue;
790 }
791 if (coder_info == (CoderInfo *) NULL)
792 continue;
793 if (LocaleCompare(keyword,"/>") == 0)
794 {
795 status=AddValueToSplayTree(coder_list,ConstantString(
796 coder_info->magick),coder_info);
797 if (status == MagickFalse)
798 (void) ThrowMagickException(exception,GetMagickModule(),
799 ResourceLimitError,"MemoryAllocationFailed","`%s'",
800 coder_info->magick);
801 coder_info=(CoderInfo *) NULL;
802 }
803 GetMagickToken(q,(const char **) NULL,token);
804 if (*token != '=')
805 continue;
806 GetMagickToken(q,&q,token);
807 GetMagickToken(q,&q,token);
808 switch (*keyword)
809 {
810 case 'M':
811 case 'm':
812 {
813 if (LocaleCompare((char *) keyword,"magick") == 0)
814 {
815 coder_info->magick=ConstantString(token);
816 break;
817 }
818 break;
819 }
820 case 'N':
821 case 'n':
822 {
823 if (LocaleCompare((char *) keyword,"name") == 0)
824 {
825 coder_info->name=ConstantString(token);
826 break;
827 }
828 break;
829 }
830 case 'S':
831 case 's':
832 {
833 if (LocaleCompare((char *) keyword,"stealth") == 0)
834 {
835 coder_info->stealth=IsMagickTrue(token);
836 break;
837 }
838 break;
839 }
840 default:
841 break;
842 }
843 }
844 token=(char *) RelinquishMagickMemory(token);
845 return(status);
846}
847
848/*
849%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
850% %
851% %
852% %
853% L o a d C o d e r L i s t s %
854% %
855% %
856% %
857%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
858%
859% LoadCoderLists() loads one or more coder configuration file which
860% provides a mapping between coder attributes and a coder name.
861%
862% The format of the LoadCoderLists coder is:
863%
864% MagickBooleanType LoadCoderLists(const char *filename,
865% ExceptionInfo *exception)
866%
867% A description of each parameter follows:
868%
869% o filename: the font file name.
870%
871% o exception: return any errors or warnings in this structure.
872%
873*/
874static MagickBooleanType LoadCoderLists(const char *filename,
875 ExceptionInfo *exception)
876{
cristy3ed852e2009-09-05 21:47:34 +0000877 const StringInfo
878 *option;
879
880 LinkedListInfo
881 *options;
882
883 MagickStatusType
884 status;
885
cristybb503372010-05-27 20:51:26 +0000886 register ssize_t
cristy54a531d2009-10-21 17:58:01 +0000887 i;
cristye3e77a12009-10-16 00:47:21 +0000888
889 /*
890 Load built-in coder map.
891 */
cristy3ed852e2009-09-05 21:47:34 +0000892 status=MagickFalse;
cristye3e77a12009-10-16 00:47:21 +0000893 if (coder_list == (SplayTreeInfo *) NULL)
894 {
895 coder_list=NewSplayTree(CompareSplayTreeString,RelinquishMagickMemory,
896 DestroyCoderNode);
897 if (coder_list == (SplayTreeInfo *) NULL)
898 {
899 ThrowFileException(exception,ResourceLimitError,
900 "MemoryAllocationFailed",filename);
901 return(MagickFalse);
902 }
903 }
cristybb503372010-05-27 20:51:26 +0000904 for (i=0; i < (ssize_t) (sizeof(CoderMap)/sizeof(*CoderMap)); i++)
cristye3e77a12009-10-16 00:47:21 +0000905 {
906 CoderInfo
907 *coder_info;
908
cristy54a531d2009-10-21 17:58:01 +0000909 register const CoderMapInfo
910 *p;
911
912 p=CoderMap+i;
cristy73bd4a52010-10-05 11:24:23 +0000913 coder_info=(CoderInfo *) AcquireMagickMemory(sizeof(*coder_info));
cristye3e77a12009-10-16 00:47:21 +0000914 if (coder_info == (CoderInfo *) NULL)
915 {
916 (void) ThrowMagickException(exception,GetMagickModule(),
917 ResourceLimitError,"MemoryAllocationFailed","`%s'",coder_info->name);
918 continue;
919 }
920 (void) ResetMagickMemory(coder_info,0,sizeof(*coder_info));
cristy38b8ed92009-10-16 01:20:21 +0000921 coder_info->path=(char *) "[built-in]";
cristye3e77a12009-10-16 00:47:21 +0000922 coder_info->magick=(char *) p->magick;
923 coder_info->name=(char *) p->name;
924 coder_info->exempt=MagickTrue;
925 coder_info->signature=MagickSignature;
926 status=AddValueToSplayTree(coder_list,ConstantString(coder_info->magick),
927 coder_info);
928 if (status == MagickFalse)
929 (void) ThrowMagickException(exception,GetMagickModule(),
930 ResourceLimitError,"MemoryAllocationFailed","`%s'",coder_info->name);
931 }
932 /*
933 Load external coder map.
934 */
cristy3ed852e2009-09-05 21:47:34 +0000935 options=GetConfigureOptions(filename,exception);
936 option=(const StringInfo *) GetNextValueInLinkedList(options);
937 while (option != (const StringInfo *) NULL)
938 {
939 status|=LoadCoderList((const char *) GetStringInfoDatum(option),
940 GetStringInfoPath(option),0,exception);
941 option=(const StringInfo *) GetNextValueInLinkedList(options);
942 }
943 options=DestroyConfigureOptions(options);
cristy3ed852e2009-09-05 21:47:34 +0000944 return(status != 0 ? MagickTrue : MagickFalse);
cristy3ed852e2009-09-05 21:47:34 +0000945}