2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6 % CCCC OOO DDDD EEEEE RRRR %
10 % CCCC OOO DDDD EEEEE R R %
13 % MagickCore Image Coder Methods %
20 % Copyright 1999-2018 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
26 % https://imagemagick.org/script/license.php %
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. %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
42 #include "MagickCore/studio.h"
43 #include "MagickCore/blob.h"
44 #include "MagickCore/client.h"
45 #include "MagickCore/coder.h"
46 #include "MagickCore/coder-private.h"
47 #include "MagickCore/configure.h"
48 #include "MagickCore/draw.h"
49 #include "MagickCore/exception.h"
50 #include "MagickCore/exception-private.h"
51 #include "MagickCore/log.h"
52 #include "MagickCore/memory_.h"
53 #include "MagickCore/memory-private.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"
60 #include "MagickCore/utility-private.h"
61 #include "coders/coders.h"
66 #define AddMagickCoder(coder) Magick ## coder ## Aliases
71 typedef struct _CoderMapInfo
81 static const CoderMapInfo
84 #include "coders/coders-list.h"
88 *coder_semaphore = (SemaphoreInfo *) NULL;
91 *coder_cache = (SplayTreeInfo *) NULL;
96 static MagickBooleanType
97 IsCoderTreeInstantiated(ExceptionInfo *);
100 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
104 + A c q u i r e C o d e r C a c h e %
108 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
110 % AcquireCoderCache() caches one or more coder configurations which provides a
111 % mapping between coder attributes and a coder name.
113 % The format of the AcquireCoderCache coder is:
115 % SplayTreeInfo *AcquireCoderCache(const char *filename,
116 % ExceptionInfo *exception)
118 % A description of each parameter follows:
120 % o filename: the font file name.
122 % o exception: return any errors or warnings in this structure.
126 static void *DestroyCoderNode(void *coder_info)
131 p=(CoderInfo *) coder_info;
132 if (p->exempt == MagickFalse)
134 if (p->path != (char *) NULL)
135 p->path=DestroyString(p->path);
136 if (p->name != (char *) NULL)
137 p->name=DestroyString(p->name);
138 if (p->magick != (char *) NULL)
139 p->magick=DestroyString(p->magick);
141 return(RelinquishMagickMemory(p));
144 static SplayTreeInfo *AcquireCoderCache(ExceptionInfo *exception)
156 Load built-in coder map.
158 cache=NewSplayTree(CompareSplayTreeString,RelinquishMagickMemory,
161 for (i=0; i < (ssize_t) (sizeof(CoderMap)/sizeof(*CoderMap)); i++)
166 register const CoderMapInfo
170 coder_info=(CoderInfo *) AcquireMagickMemory(sizeof(*coder_info));
171 if (coder_info == (CoderInfo *) NULL)
173 (void) ThrowMagickException(exception,GetMagickModule(),
174 ResourceLimitError,"MemoryAllocationFailed","`%s'",p->name);
177 (void) memset(coder_info,0,sizeof(*coder_info));
178 coder_info->path=(char *) "[built-in]";
179 coder_info->magick=(char *) p->magick;
180 coder_info->name=(char *) p->name;
181 coder_info->exempt=MagickTrue;
182 coder_info->signature=MagickCoreSignature;
183 status&=AddValueToSplayTree(cache,ConstantString(coder_info->magick),
185 if (status == MagickFalse)
186 (void) ThrowMagickException(exception,GetMagickModule(),
187 ResourceLimitError,"MemoryAllocationFailed","`%s'",coder_info->name);
193 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
197 + C o d e r C o m p o n e n t G e n e s i s %
201 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
203 % CoderComponentGenesis() instantiates the coder component.
205 % The format of the CoderComponentGenesis method is:
207 % MagickBooleanType CoderComponentGenesis(void)
210 MagickPrivate MagickBooleanType CoderComponentGenesis(void)
212 if (coder_semaphore == (SemaphoreInfo *) NULL)
213 coder_semaphore=AcquireSemaphoreInfo();
218 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
222 + C o d e r C o m p o n e n t T e r m i n u s %
226 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
228 % CoderComponentTerminus() destroys the coder component.
230 % The format of the CoderComponentTerminus method is:
232 % CoderComponentTerminus(void)
235 MagickPrivate void CoderComponentTerminus(void)
237 if (coder_semaphore == (SemaphoreInfo *) NULL)
238 ActivateSemaphoreInfo(&coder_semaphore);
239 LockSemaphoreInfo(coder_semaphore);
240 if (coder_cache != (SplayTreeInfo *) NULL)
241 coder_cache=DestroySplayTree(coder_cache);
242 UnlockSemaphoreInfo(coder_semaphore);
243 RelinquishSemaphoreInfo(&coder_semaphore);
247 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
251 + G e t C o d e r I n f o %
255 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
257 % GetCoderInfo searches the coder list for the specified name and if found
258 % returns attributes for that coder.
260 % The format of the GetCoderInfo method is:
262 % const CoderInfo *GetCoderInfo(const char *name,ExceptionInfo *exception)
264 % A description of each parameter follows:
266 % o name: the coder name.
268 % o exception: return any errors or warnings in this structure.
271 MagickExport const CoderInfo *GetCoderInfo(const char *name,
272 ExceptionInfo *exception)
274 assert(exception != (ExceptionInfo *) NULL);
275 if (IsCoderTreeInstantiated(exception) == MagickFalse)
276 return((const CoderInfo *) NULL);
277 if ((name == (const char *) NULL) || (LocaleCompare(name,"*") == 0))
278 return((const CoderInfo *) GetRootValueFromSplayTree(coder_cache));
279 return((const CoderInfo *) GetValueFromSplayTree(coder_cache,name));
283 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
287 % G e t C o d e r I n f o L i s t %
291 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
293 % GetCoderInfoList() returns any coder_map that match the specified pattern.
294 % The format of the GetCoderInfoList function is:
296 % const CoderInfo **GetCoderInfoList(const char *pattern,
297 % size_t *number_coders,ExceptionInfo *exception)
299 % A description of each parameter follows:
301 % o pattern: Specifies a pointer to a text string containing a pattern.
303 % o number_coders: This integer returns the number of coders in the list.
305 % o exception: return any errors or warnings in this structure.
309 static int CoderInfoCompare(const void *x,const void *y)
315 p=(const CoderInfo **) x,
316 q=(const CoderInfo **) y;
317 if (LocaleCompare((*p)->path,(*q)->path) == 0)
318 return(LocaleCompare((*p)->name,(*q)->name));
319 return(LocaleCompare((*p)->path,(*q)->path));
322 MagickExport const CoderInfo **GetCoderInfoList(const char *pattern,
323 size_t *number_coders,ExceptionInfo *exception)
328 register const CoderInfo
337 assert(pattern != (char *) NULL);
338 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
339 assert(number_coders != (size_t *) NULL);
341 p=GetCoderInfo("*",exception);
342 if (p == (const CoderInfo *) NULL)
343 return((const CoderInfo **) NULL);
344 coder_map=(const CoderInfo **) AcquireQuantumMemory((size_t)
345 GetNumberOfNodesInSplayTree(coder_cache)+1UL,sizeof(*coder_map));
346 if (coder_map == (const CoderInfo **) NULL)
347 return((const CoderInfo **) NULL);
351 LockSemaphoreInfo(coder_semaphore);
352 ResetSplayTreeIterator(coder_cache);
353 p=(const CoderInfo *) GetNextValueInSplayTree(coder_cache);
354 for (i=0; p != (const CoderInfo *) NULL; )
356 if ((p->stealth == MagickFalse) &&
357 (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
359 p=(const CoderInfo *) GetNextValueInSplayTree(coder_cache);
361 UnlockSemaphoreInfo(coder_semaphore);
362 qsort((void *) coder_map,(size_t) i,sizeof(*coder_map),CoderInfoCompare);
363 coder_map[i]=(CoderInfo *) NULL;
364 *number_coders=(size_t) i;
369 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
373 % G e t C o d e r L i s t %
377 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
379 % GetCoderList() returns any coder_map that match the specified pattern.
381 % The format of the GetCoderList function is:
383 % char **GetCoderList(const char *pattern,size_t *number_coders,
384 % ExceptionInfo *exception)
386 % A description of each parameter follows:
388 % o pattern: Specifies a pointer to a text string containing a pattern.
390 % o number_coders: This integer returns the number of coders in the list.
392 % o exception: return any errors or warnings in this structure.
396 static int CoderCompare(const void *x,const void *y)
404 return(LocaleCompare(*p,*q));
407 MagickExport char **GetCoderList(const char *pattern,
408 size_t *number_coders,ExceptionInfo *exception)
413 register const CoderInfo
422 assert(pattern != (char *) NULL);
423 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
424 assert(number_coders != (size_t *) NULL);
426 p=GetCoderInfo("*",exception);
427 if (p == (const CoderInfo *) NULL)
428 return((char **) NULL);
429 coder_map=(char **) AcquireQuantumMemory((size_t)
430 GetNumberOfNodesInSplayTree(coder_cache)+1UL,sizeof(*coder_map));
431 if (coder_map == (char **) NULL)
432 return((char **) NULL);
436 LockSemaphoreInfo(coder_semaphore);
437 ResetSplayTreeIterator(coder_cache);
438 p=(const CoderInfo *) GetNextValueInSplayTree(coder_cache);
439 for (i=0; p != (const CoderInfo *) NULL; )
441 if ((p->stealth == MagickFalse) &&
442 (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
443 coder_map[i++]=ConstantString(p->name);
444 p=(const CoderInfo *) GetNextValueInSplayTree(coder_cache);
446 UnlockSemaphoreInfo(coder_semaphore);
447 qsort((void *) coder_map,(size_t) i,sizeof(*coder_map),CoderCompare);
448 coder_map[i]=(char *) NULL;
449 *number_coders=(size_t) i;
454 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
458 + I s C o d e r T r e e I n s t a n t i a t e d %
462 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
464 % IsCoderTreeInstantiated() determines if the coder tree is instantiated. If
465 % not, it instantiates the tree and returns it.
467 % The format of the IsCoderInstantiated method is:
469 % MagickBooleanType IsCoderTreeInstantiated(ExceptionInfo *exception)
471 % A description of each parameter follows.
473 % o exception: return any errors or warnings in this structure.
476 static MagickBooleanType IsCoderTreeInstantiated(ExceptionInfo *exception)
478 if (coder_cache == (SplayTreeInfo *) NULL)
480 if (coder_semaphore == (SemaphoreInfo *) NULL)
481 ActivateSemaphoreInfo(&coder_semaphore);
482 LockSemaphoreInfo(coder_semaphore);
483 if (coder_cache == (SplayTreeInfo *) NULL)
484 coder_cache=AcquireCoderCache(exception);
485 UnlockSemaphoreInfo(coder_semaphore);
487 return(coder_cache != (SplayTreeInfo *) NULL ? MagickTrue : MagickFalse);
491 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
495 % L i s t C o d e r I n f o %
499 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
501 % ListCoderInfo() lists the coder info to a file.
503 % The format of the ListCoderInfo coder is:
505 % MagickBooleanType ListCoderInfo(FILE *file,ExceptionInfo *exception)
507 % A description of each parameter follows.
509 % o file: An pointer to a FILE.
511 % o exception: return any errors or warnings in this structure.
514 MagickExport MagickBooleanType ListCoderInfo(FILE *file,
515 ExceptionInfo *exception)
532 if (file == (const FILE *) NULL)
534 coder_info=GetCoderInfoList("*",&number_coders,exception);
535 if (coder_info == (const CoderInfo **) NULL)
537 path=(const char *) NULL;
538 for (i=0; i < (ssize_t) number_coders; i++)
540 if (coder_info[i]->stealth != MagickFalse)
542 if ((path == (const char *) NULL) ||
543 (LocaleCompare(path,coder_info[i]->path) != 0))
545 if (coder_info[i]->path != (char *) NULL)
546 (void) FormatLocaleFile(file,"\nPath: %s\n\n",coder_info[i]->path);
547 (void) FormatLocaleFile(file,"Magick Coder\n");
548 (void) FormatLocaleFile(file,
549 "-------------------------------------------------"
550 "------------------------------\n");
552 path=coder_info[i]->path;
553 (void) FormatLocaleFile(file,"%s",coder_info[i]->magick);
554 for (j=(ssize_t) strlen(coder_info[i]->magick); j <= 11; j++)
555 (void) FormatLocaleFile(file," ");
556 if (coder_info[i]->name != (char *) NULL)
557 (void) FormatLocaleFile(file,"%s",coder_info[i]->name);
558 (void) FormatLocaleFile(file,"\n");
560 coder_info=(const CoderInfo **) RelinquishMagickMemory((void *) coder_info);