MagickCore  7.0.8
Convert, Edit, Or Compose Bitmap Images
coder.c
Go to the documentation of this file.
1 /*
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 % Cristy %
17 % May 2001 %
18 % %
19 % %
20 % Copyright 1999-2018 ImageMagick Studio LLC, a non-profit organization %
21 % 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 % https://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 "MagickCore/studio.h"
43 #include "MagickCore/blob.h"
44 #include "MagickCore/client.h"
45 #include "MagickCore/coder.h"
47 #include "MagickCore/configure.h"
48 #include "MagickCore/draw.h"
49 #include "MagickCore/exception.h"
51 #include "MagickCore/log.h"
52 #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"
61 #include "coders/coders.h"
62 
63 /*
64  Define declarations.
65 */
66 #define AddMagickCoder(coder) Magick ## coder ## Aliases
67 
68 /*
69  Typedef declarations.
70 */
71 typedef struct _CoderMapInfo
72 {
73  const char
75  *name;
76 } CoderMapInfo;
77 
78 /*
79  Static declarations.
80 */
81 static const CoderMapInfo
83  {
84  #include "coders/coders-list.h"
85  };
86 
87 static SemaphoreInfo
89 
90 static SplayTreeInfo
92 
93 /*
94  Forward declarations.
95 */
96 static MagickBooleanType
98 
99 /*
100 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
101 % %
102 % %
103 % %
104 + A c q u i r e C o d e r C a c h e %
105 % %
106 % %
107 % %
108 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
109 %
110 % AcquireCoderCache() caches one or more coder configurations which provides a
111 % mapping between coder attributes and a coder name.
112 %
113 % The format of the AcquireCoderCache coder is:
114 %
115 % SplayTreeInfo *AcquireCoderCache(const char *filename,
116 % ExceptionInfo *exception)
117 %
118 % A description of each parameter follows:
119 %
120 % o filename: the font file name.
121 %
122 % o exception: return any errors or warnings in this structure.
123 %
124 */
125 
126 static void *DestroyCoderNode(void *coder_info)
127 {
128  register CoderInfo
129  *p;
130 
131  p=(CoderInfo *) coder_info;
132  if (p->exempt == MagickFalse)
133  {
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);
140  }
141  return(RelinquishMagickMemory(p));
142 }
143 
145 {
147  status;
148 
149  register ssize_t
150  i;
151 
153  *cache;
154 
155  /*
156  Load built-in coder map.
157  */
160  status=MagickTrue;
161  for (i=0; i < (ssize_t) (sizeof(CoderMap)/sizeof(*CoderMap)); i++)
162  {
163  CoderInfo
164  *coder_info;
165 
166  register const CoderMapInfo
167  *p;
168 
169  p=CoderMap+i;
170  coder_info=(CoderInfo *) AcquireMagickMemory(sizeof(*coder_info));
171  if (coder_info == (CoderInfo *) NULL)
172  {
173  (void) ThrowMagickException(exception,GetMagickModule(),
174  ResourceLimitError,"MemoryAllocationFailed","`%s'",p->name);
175  continue;
176  }
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),
184  coder_info);
185  if (status == MagickFalse)
186  (void) ThrowMagickException(exception,GetMagickModule(),
187  ResourceLimitError,"MemoryAllocationFailed","`%s'",coder_info->name);
188  }
189  return(cache);
190 }
191 
192 /*
193 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
194 % %
195 % %
196 % %
197 + C o d e r C o m p o n e n t G e n e s i s %
198 % %
199 % %
200 % %
201 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
202 %
203 % CoderComponentGenesis() instantiates the coder component.
204 %
205 % The format of the CoderComponentGenesis method is:
206 %
207 % MagickBooleanType CoderComponentGenesis(void)
208 %
209 */
211 {
212  if (coder_semaphore == (SemaphoreInfo *) NULL)
214  return(MagickTrue);
215 }
216 
217 /*
218 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
219 % %
220 % %
221 % %
222 + C o d e r C o m p o n e n t T e r m i n u s %
223 % %
224 % %
225 % %
226 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
227 %
228 % CoderComponentTerminus() destroys the coder component.
229 %
230 % The format of the CoderComponentTerminus method is:
231 %
232 % CoderComponentTerminus(void)
233 %
234 */
236 {
237  if (coder_semaphore == (SemaphoreInfo *) NULL)
240  if (coder_cache != (SplayTreeInfo *) NULL)
244 }
245 
246 /*
247 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
248 % %
249 % %
250 % %
251 + G e t C o d e r I n f o %
252 % %
253 % %
254 % %
255 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
256 %
257 % GetCoderInfo searches the coder list for the specified name and if found
258 % returns attributes for that coder.
259 %
260 % The format of the GetCoderInfo method is:
261 %
262 % const CoderInfo *GetCoderInfo(const char *name,ExceptionInfo *exception)
263 %
264 % A description of each parameter follows:
265 %
266 % o name: the coder name.
267 %
268 % o exception: return any errors or warnings in this structure.
269 %
270 */
271 MagickExport const CoderInfo *GetCoderInfo(const char *name,
272  ExceptionInfo *exception)
273 {
274  assert(exception != (ExceptionInfo *) NULL);
275  if (IsCoderTreeInstantiated(exception) == MagickFalse)
276  return((const CoderInfo *) NULL);
277  if ((name == (const char *) NULL) || (LocaleCompare(name,"*") == 0))
279  return((const CoderInfo *) GetValueFromSplayTree(coder_cache,name));
280 }
281 
282 /*
283 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
284 % %
285 % %
286 % %
287 % G e t C o d e r I n f o L i s t %
288 % %
289 % %
290 % %
291 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
292 %
293 % GetCoderInfoList() returns any coder_map that match the specified pattern.
294 % The format of the GetCoderInfoList function is:
295 %
296 % const CoderInfo **GetCoderInfoList(const char *pattern,
297 % size_t *number_coders,ExceptionInfo *exception)
298 %
299 % A description of each parameter follows:
300 %
301 % o pattern: Specifies a pointer to a text string containing a pattern.
302 %
303 % o number_coders: This integer returns the number of coders in the list.
304 %
305 % o exception: return any errors or warnings in this structure.
306 %
307 */
308 
309 static int CoderInfoCompare(const void *x,const void *y)
310 {
311  const CoderInfo
312  **p,
313  **q;
314 
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));
320 }
321 
322 MagickExport const CoderInfo **GetCoderInfoList(const char *pattern,
323  size_t *number_coders,ExceptionInfo *exception)
324 {
325  const CoderInfo
326  **coder_map;
327 
328  register const CoderInfo
329  *p;
330 
331  register ssize_t
332  i;
333 
334  /*
335  Allocate coder list.
336  */
337  assert(pattern != (char *) NULL);
338  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
339  assert(number_coders != (size_t *) NULL);
340  *number_coders=0;
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);
348  /*
349  Generate coder list.
350  */
354  for (i=0; p != (const CoderInfo *) NULL; )
355  {
356  if ((p->stealth == MagickFalse) &&
357  (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
358  coder_map[i++]=p;
360  }
362  qsort((void *) coder_map,(size_t) i,sizeof(*coder_map),CoderInfoCompare);
363  coder_map[i]=(CoderInfo *) NULL;
364  *number_coders=(size_t) i;
365  return(coder_map);
366 }
367 
368 /*
369 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
370 % %
371 % %
372 % %
373 % G e t C o d e r L i s t %
374 % %
375 % %
376 % %
377 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
378 %
379 % GetCoderList() returns any coder_map that match the specified pattern.
380 %
381 % The format of the GetCoderList function is:
382 %
383 % char **GetCoderList(const char *pattern,size_t *number_coders,
384 % ExceptionInfo *exception)
385 %
386 % A description of each parameter follows:
387 %
388 % o pattern: Specifies a pointer to a text string containing a pattern.
389 %
390 % o number_coders: This integer returns the number of coders in the list.
391 %
392 % o exception: return any errors or warnings in this structure.
393 %
394 */
395 
396 static int CoderCompare(const void *x,const void *y)
397 {
398  register const char
399  **p,
400  **q;
401 
402  p=(const char **) x;
403  q=(const char **) y;
404  return(LocaleCompare(*p,*q));
405 }
406 
407 MagickExport char **GetCoderList(const char *pattern,
408  size_t *number_coders,ExceptionInfo *exception)
409 {
410  char
411  **coder_map;
412 
413  register const CoderInfo
414  *p;
415 
416  register ssize_t
417  i;
418 
419  /*
420  Allocate coder list.
421  */
422  assert(pattern != (char *) NULL);
423  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
424  assert(number_coders != (size_t *) NULL);
425  *number_coders=0;
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);
433  /*
434  Generate coder list.
435  */
439  for (i=0; p != (const CoderInfo *) NULL; )
440  {
441  if ((p->stealth == MagickFalse) &&
442  (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
443  coder_map[i++]=ConstantString(p->name);
445  }
447  qsort((void *) coder_map,(size_t) i,sizeof(*coder_map),CoderCompare);
448  coder_map[i]=(char *) NULL;
449  *number_coders=(size_t) i;
450  return(coder_map);
451 }
452 
453 /*
454 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
455 % %
456 % %
457 % %
458 + I s C o d e r T r e e I n s t a n t i a t e d %
459 % %
460 % %
461 % %
462 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
463 %
464 % IsCoderTreeInstantiated() determines if the coder tree is instantiated. If
465 % not, it instantiates the tree and returns it.
466 %
467 % The format of the IsCoderInstantiated method is:
468 %
469 % MagickBooleanType IsCoderTreeInstantiated(ExceptionInfo *exception)
470 %
471 % A description of each parameter follows.
472 %
473 % o exception: return any errors or warnings in this structure.
474 %
475 */
477 {
478  if (coder_cache == (SplayTreeInfo *) NULL)
479  {
480  if (coder_semaphore == (SemaphoreInfo *) NULL)
483  if (coder_cache == (SplayTreeInfo *) NULL)
484  coder_cache=AcquireCoderCache(exception);
486  }
487  return(coder_cache != (SplayTreeInfo *) NULL ? MagickTrue : MagickFalse);
488 }
489 
490 /*
491 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
492 % %
493 % %
494 % %
495 % L i s t C o d e r I n f o %
496 % %
497 % %
498 % %
499 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
500 %
501 % ListCoderInfo() lists the coder info to a file.
502 %
503 % The format of the ListCoderInfo coder is:
504 %
505 % MagickBooleanType ListCoderInfo(FILE *file,ExceptionInfo *exception)
506 %
507 % A description of each parameter follows.
508 %
509 % o file: An pointer to a FILE.
510 %
511 % o exception: return any errors or warnings in this structure.
512 %
513 */
515  ExceptionInfo *exception)
516 {
517  const char
518  *path;
519 
520  const CoderInfo
521  **coder_info;
522 
523  register ssize_t
524  i;
525 
526  size_t
527  number_coders;
528 
529  ssize_t
530  j;
531 
532  if (file == (const FILE *) NULL)
533  file=stdout;
534  coder_info=GetCoderInfoList("*",&number_coders,exception);
535  if (coder_info == (const CoderInfo **) NULL)
536  return(MagickFalse);
537  path=(const char *) NULL;
538  for (i=0; i < (ssize_t) number_coders; i++)
539  {
540  if (coder_info[i]->stealth != MagickFalse)
541  continue;
542  if ((path == (const char *) NULL) ||
543  (LocaleCompare(path,coder_info[i]->path) != 0))
544  {
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");
551  }
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");
559  }
560  coder_info=(const CoderInfo **) RelinquishMagickMemory((void *) coder_info);
561  (void) fflush(file);
562  return(MagickTrue);
563 }
MagickExport const CoderInfo ** GetCoderInfoList(const char *pattern, size_t *number_coders, ExceptionInfo *exception)
Definition: coder.c:322
MagickExport MagickBooleanType AddValueToSplayTree(SplayTreeInfo *splay_tree, const void *key, const void *value)
Definition: splay-tree.c:154
MagickBooleanType stealth
Definition: coder.h:33
MagickExport void UnlockSemaphoreInfo(SemaphoreInfo *semaphore_info)
Definition: semaphore.c:450
static MagickBooleanType IsCoderTreeInstantiated(ExceptionInfo *)
Definition: coder.c:476
MagickExport SemaphoreInfo * AcquireSemaphoreInfo(void)
Definition: semaphore.c:192
static int CoderCompare(const void *x, const void *y)
Definition: coder.c:396
MagickExport MagickBooleanType ListCoderInfo(FILE *file, ExceptionInfo *exception)
Definition: coder.c:514
Definition: log.h:52
static int CoderInfoCompare(const void *x, const void *y)
Definition: coder.c:309
MagickExport const void * GetNextValueInSplayTree(SplayTreeInfo *splay_tree)
Definition: splay-tree.c:823
MagickPrivate MagickBooleanType CoderComponentGenesis(void)
Definition: coder.c:210
#define MagickCoreSignature
MagickExport void LockSemaphoreInfo(SemaphoreInfo *semaphore_info)
Definition: semaphore.c:293
const char * magick
Definition: coder.c:74
MagickPrivate void CoderComponentTerminus(void)
Definition: coder.c:235
MagickExport ssize_t FormatLocaleFile(FILE *file, const char *magick_restrict format,...)
Definition: locale.c:409
MagickBooleanType
Definition: magick-type.h:158
unsigned int MagickStatusType
Definition: magick-type.h:121
static SplayTreeInfo * AcquireCoderCache(ExceptionInfo *exception)
Definition: coder.c:144
char * magick
Definition: coder.h:28
MagickExport void * AcquireQuantumMemory(const size_t count, const size_t quantum)
Definition: memory.c:533
static const CoderMapInfo CoderMap[]
Definition: coder.c:82
MagickExport SplayTreeInfo * DestroySplayTree(SplayTreeInfo *splay_tree)
Definition: splay-tree.c:682
MagickExport MagickBooleanType GlobExpression(const char *expression, const char *pattern, const MagickBooleanType case_insensitive)
Definition: token.c:347
static SemaphoreInfo * coder_semaphore
Definition: coder.c:88
MagickExport SplayTreeInfo * NewSplayTree(int(*compare)(const void *, const void *), void *(*relinquish_key)(void *), void *(*relinquish_value)(void *))
Definition: splay-tree.c:1141
MagickExport MagickBooleanType ThrowMagickException(ExceptionInfo *exception, const char *module, const char *function, const size_t line, const ExceptionType severity, const char *tag, const char *format,...)
Definition: exception.c:1064
MagickExport MagickBooleanType LogMagickEvent(const LogEventType type, const char *module, const char *function, const size_t line, const char *format,...)
Definition: log.c:1398
char * path
Definition: coder.h:28
MagickExport char ** GetCoderList(const char *pattern, size_t *number_coders, ExceptionInfo *exception)
Definition: coder.c:407
MagickExport const void * GetValueFromSplayTree(SplayTreeInfo *splay_tree, const void *key)
Definition: splay-tree.c:921
static SplayTreeInfo * coder_cache
Definition: coder.c:91
MagickExport int LocaleCompare(const char *p, const char *q)
Definition: locale.c:1440
#define GetMagickModule()
Definition: log.h:28
MagickExport int CompareSplayTreeString(const void *target, const void *source)
Definition: splay-tree.c:412
MagickBooleanType exempt
Definition: coder.h:33
MagickExport const CoderInfo * GetCoderInfo(const char *name, ExceptionInfo *exception)
Definition: coder.c:271
MagickExport char * DestroyString(char *string)
Definition: string.c:823
MagickExport void * AcquireMagickMemory(const size_t size)
Definition: memory.c:462
MagickExport void ActivateSemaphoreInfo(SemaphoreInfo **semaphore_info)
Definition: semaphore.c:97
struct _CoderMapInfo CoderMapInfo
MagickExport const void * GetRootValueFromSplayTree(SplayTreeInfo *splay_tree)
Definition: splay-tree.c:877
char * name
Definition: coder.h:28
MagickExport void ResetSplayTreeIterator(SplayTreeInfo *splay_tree)
Definition: splay-tree.c:1472
MagickExport void * RelinquishMagickMemory(void *memory)
Definition: memory.c:1054
MagickExport size_t GetNumberOfNodesInSplayTree(const SplayTreeInfo *splay_tree)
Definition: splay-tree.c:976
static void * DestroyCoderNode(void *coder_info)
Definition: coder.c:126
#define MagickPrivate
#define MagickExport
MagickExport void RelinquishSemaphoreInfo(SemaphoreInfo **semaphore_info)
Definition: semaphore.c:351
MagickExport char * ConstantString(const char *source)
Definition: string.c:700
size_t signature
Definition: coder.h:37
const char * name
Definition: coder.c:74