MagickCore  6.9.12-67
Convert, Edit, Or Compose Bitmap Images
 All Data Structures
registry.c
1 /*
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 % %
4 % %
5 % %
6 % RRRR EEEEE GGG IIIII SSSSS TTTTT RRRR Y Y %
7 % R R E G I SS T R R Y Y %
8 % RRRR EEE G GGG I SSS T RRRR Y %
9 % R R E G G I SS T R R Y %
10 % R R EEEEE GGG IIIII SSSSS T R R Y %
11 % %
12 % %
13 % MagickCore Registry Methods %
14 % %
15 % Software Design %
16 % Cristy %
17 % March 2000 %
18 % %
19 % %
20 % Copyright 1999-2021 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 /*
41  Include declarations.
42 */
43 #include "magick/studio.h"
44 #include "magick/exception.h"
45 #include "magick/exception-private.h"
46 #include "magick/image.h"
47 #include "magick/list.h"
48 #include "magick/memory_.h"
49 #include "magick/memory-private.h"
50 #include "magick/registry.h"
51 #include "magick/splay-tree.h"
52 #include "magick/string_.h"
53 #include "magick/utility.h"
54 
55 /*
56  Typedef declarations.
57 */
58 typedef struct _RegistryInfo
59 {
60  RegistryType
61  type;
62 
63  void
64  *value;
65 
66  size_t
67  signature;
68 } RegistryInfo;
69 
70 /*
71  Static declarations.
72 */
73 static SplayTreeInfo
74  *registry = (SplayTreeInfo *) NULL;
75 
76 static SemaphoreInfo
77  *registry_semaphore = (SemaphoreInfo *) NULL;
78 
79 /*
80 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
81 % %
82 % %
83 % %
84 % D e f i n e I m a g e R e g i s t r y %
85 % %
86 % %
87 % %
88 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
89 %
90 % DefineImageRegistry() associates a key/value pair with the image registry.
91 %
92 % The format of the DefineImageRegistry method is:
93 %
94 % MagickBooleanType DefineImageRegistry(const RegistryType type,
95 % const char *option,ExceptionInfo *exception)
96 %
97 % A description of each parameter follows:
98 %
99 % o type: the type.
100 %
101 % o option: the option.
102 %
103 % o exception: the exception.
104 %
105 */
106 MagickExport MagickBooleanType DefineImageRegistry(const RegistryType type,
107  const char *option,ExceptionInfo *exception)
108 {
109  char
110  key[MaxTextExtent],
111  value[MaxTextExtent];
112 
113  char
114  *p;
115 
116  assert(option != (const char *) NULL);
117  (void) CopyMagickString(key,option,MaxTextExtent);
118  for (p=key; *p != '\0'; p++)
119  if (*p == '=')
120  break;
121  *value='\0';
122  if (*p == '=')
123  (void) CopyMagickString(value,p+1,MaxTextExtent);
124  *p='\0';
125  return(SetImageRegistry(type,key,value,exception));
126 }
127 
128 /*
129 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
130 % %
131 % %
132 % %
133 % D e l e t e I m a g e R e g i s t r y %
134 % %
135 % %
136 % %
137 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
138 %
139 % DeleteImageRegistry() deletes a key from the image registry.
140 %
141 % The format of the DeleteImageRegistry method is:
142 %
143 % MagickBooleanType DeleteImageRegistry(const char *key)
144 %
145 % A description of each parameter follows:
146 %
147 % o key: the registry.
148 %
149 */
150 MagickExport MagickBooleanType DeleteImageRegistry(const char *key)
151 {
152  assert(key != (const char *) NULL);
153  if (IsEventLogging() != MagickFalse)
154  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",key);
155  if (registry == (void *) NULL)
156  return(MagickFalse);
157  return(DeleteNodeFromSplayTree(registry,key));
158 }
159 
160 /*
161 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
162 % %
163 % %
164 % %
165 % G e t I m a g e R e g i s t r y %
166 % %
167 % %
168 % %
169 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
170 %
171 % GetImageRegistry() returns a value associated with an image registry key.
172 %
173 % The format of the GetImageRegistry method is:
174 %
175 % void *GetImageRegistry(const RegistryType type,const char *key,
176 % ExceptionInfo *exception)
177 %
178 % A description of each parameter follows:
179 %
180 % o type: the type.
181 %
182 % o key: the key.
183 %
184 % o exception: the exception.
185 %
186 */
187 MagickExport void *GetImageRegistry(const RegistryType type,const char *key,
188  ExceptionInfo *exception)
189 {
190  void
191  *value;
192 
194  *registry_info;
195 
196  assert(key != (const char *) NULL);
197  if (IsEventLogging() != MagickFalse)
198  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",key);
199  if (registry == (void *) NULL)
200  return((void *) NULL);
201  registry_info=(RegistryInfo *) GetValueFromSplayTree(registry,key);
202  if (registry_info == (void *) NULL)
203  return((void *) NULL);
204  value=(void *) NULL;
205  switch (type)
206  {
207  case ImageRegistryType:
208  {
209  if (type == registry_info->type)
210  value=(void *) CloneImageList((Image *) registry_info->value,exception);
211  break;
212  }
213  case ImageInfoRegistryType:
214  {
215  if (type == registry_info->type)
216  value=(void *) CloneImageInfo((ImageInfo *) registry_info->value);
217  break;
218  }
219  case StringRegistryType:
220  {
221  switch (registry_info->type)
222  {
223  case ImageRegistryType:
224  {
225  value=(Image *) ConstantString(((Image *)
226  registry_info->value)->filename);
227  break;
228  }
229  case ImageInfoRegistryType:
230  {
231  value=(Image *) ConstantString(((ImageInfo *)
232  registry_info->value)->filename);
233  break;
234  }
235  case StringRegistryType:
236  {
237  value=(void *) ConstantString((char *) registry_info->value);
238  break;
239  }
240  default:
241  break;
242  }
243  break;
244  }
245  default:
246  break;
247  }
248  return(value);
249 }
250 
251 /*
252 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
253 % %
254 % %
255 % %
256 % G e t N e x t I m a g e R e g i s t r y %
257 % %
258 % %
259 % %
260 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
261 %
262 % GetNextImageRegistry() gets the next image registry value.
263 %
264 % The format of the GetNextImageRegistry method is:
265 %
266 % char *GetNextImageRegistry(void)
267 %
268 */
269 MagickExport char *GetNextImageRegistry(void)
270 {
271  if (IsEventLogging() != MagickFalse)
272  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
273  if (registry == (void *) NULL)
274  return((char *) NULL);
275  return((char *) GetNextKeyInSplayTree(registry));
276 }
277 
278 /*
279 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
280 % %
281 % %
282 % %
283 + R e g i s t r y C o m p o n e n t G e n e s i s %
284 % %
285 % %
286 % %
287 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
288 %
289 % RegistryComponentGenesis() instantiates the registry component.
290 %
291 % The format of the RegistryComponentGenesis method is:
292 %
293 % MagickBooleanType RegistryComponentGenesis(void)
294 %
295 */
296 MagickExport MagickBooleanType RegistryComponentGenesis(void)
297 {
298  if (registry_semaphore == (SemaphoreInfo *) NULL)
299  registry_semaphore=AllocateSemaphoreInfo();
300  return(MagickTrue);
301 }
302 
303 /*
304 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
305 % %
306 % %
307 % %
308 % R e g i s t r y C o m p o n e n t T e r m i n u s %
309 % %
310 % %
311 % %
312 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
313 %
314 % RegistryComponentTerminus() destroys the registry component.
315 %
316 % The format of the DestroyDefines method is:
317 %
318 % void RegistryComponentTerminus(void)
319 %
320 */
321 MagickExport void RegistryComponentTerminus(void)
322 {
323  if (registry_semaphore == (SemaphoreInfo *) NULL)
324  ActivateSemaphoreInfo(&registry_semaphore);
325  LockSemaphoreInfo(registry_semaphore);
326  if (IsEventLogging() != MagickFalse)
327  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
328  if (registry != (void *) NULL)
329  registry=DestroySplayTree(registry);
330  UnlockSemaphoreInfo(registry_semaphore);
331  DestroySemaphoreInfo(&registry_semaphore);
332 }
333 
334 /*
335 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
336 % %
337 % %
338 % %
339 % R e m o v e I m a g e R e g i s t r y %
340 % %
341 % %
342 % %
343 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
344 %
345 % RemoveImageRegistry() removes a key from the image registry and returns its
346 % value.
347 %
348 % The format of the RemoveImageRegistry method is:
349 %
350 % void *RemoveImageRegistry(const char *key)
351 %
352 % A description of each parameter follows:
353 %
354 % o key: the registry.
355 %
356 */
357 MagickExport void *RemoveImageRegistry(const char *key)
358 {
359  assert(key != (const char *) NULL);
360  if (IsEventLogging() != MagickFalse)
361  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",key);
362  if (registry == (void *) NULL)
363  return((void *) NULL);
364  return(RemoveNodeFromSplayTree(registry,key));
365 }
366 
367 /*
368 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
369 % %
370 % %
371 % %
372 % R e s e t I m a g e R e g i s t r y I t e r a t o r %
373 % %
374 % %
375 % %
376 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
377 %
378 % ResetImageRegistryIterator() resets the registry iterator. Use it in
379 % conjunction with GetNextImageRegistry() to iterate over all the values
380 % in the image registry.
381 %
382 % The format of the ResetImageRegistryIterator method is:
383 %
384 % ResetImageRegistryIterator(void)
385 %
386 */
387 MagickExport void ResetImageRegistryIterator(void)
388 {
389  if (IsEventLogging() != MagickFalse)
390  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
391  if (registry == (void *) NULL)
392  return;
393  ResetSplayTreeIterator(registry);
394 }
395 
396 /*
397 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
398 % %
399 % %
400 % %
401 % S e t I m a g e R e g i s t r y %
402 % %
403 % %
404 % %
405 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
406 %
407 % SetImageRegistry() associates a value with an image registry key.
408 %
409 % The format of the SetImageRegistry method is:
410 %
411 % MagickBooleanType SetImageRegistry(const RegistryType type,
412 % const char *key,const void *value,ExceptionInfo *exception)
413 %
414 % A description of each parameter follows:
415 %
416 % o type: the type.
417 %
418 % o key: the key.
419 %
420 % o value: the value.
421 %
422 % o exception: the exception.
423 %
424 */
425 
426 static void *DestroyRegistryNode(void *registry_info)
427 {
429  *p;
430 
431  p=(RegistryInfo *) registry_info;
432  switch (p->type)
433  {
434  case StringRegistryType:
435  default:
436  {
437  p->value=RelinquishMagickMemory(p->value);
438  break;
439  }
440  case ImageRegistryType:
441  {
442  p->value=(void *) DestroyImageList((Image *) p->value);
443  break;
444  }
445  case ImageInfoRegistryType:
446  {
447  p->value=(void *) DestroyImageInfo((ImageInfo *) p->value);
448  break;
449  }
450  }
451  return(RelinquishMagickMemory(p));
452 }
453 
454 MagickExport MagickBooleanType SetImageRegistry(const RegistryType type,
455  const char *key,const void *value,ExceptionInfo *exception)
456 {
458  *registry_info;
459 
460  void
461  *clone_value;
462 
463  assert(key != (const char *) NULL);
464  if (IsEventLogging() != MagickFalse)
465  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",key);
466  if (value == (const void *) NULL)
467  return(MagickFalse);
468  clone_value=(void *) NULL;
469  switch (type)
470  {
471  case StringRegistryType:
472  default:
473  {
474  const char
475  *string;
476 
477  string=(const char *) value;
478  clone_value=(void *) ConstantString(string);
479  break;
480  }
481  case ImageRegistryType:
482  {
483  const Image
484  *image;
485 
486  image=(const Image *) value;
487  if ((image == (const Image *) NULL) ||
488  (image->signature != MagickCoreSignature))
489  {
490  (void) ThrowMagickException(exception,GetMagickModule(),RegistryError,
491  "UnableToSetRegistry","%s",key);
492  return(MagickFalse);
493  }
494  clone_value=(void *) CloneImageList(image,exception);
495  break;
496  }
497  case ImageInfoRegistryType:
498  {
499  const ImageInfo
500  *image_info;
501 
502  image_info=(const ImageInfo *) value;
503  if ((image_info == (const ImageInfo *) NULL) ||
504  (image_info->signature != MagickCoreSignature))
505  {
506  (void) ThrowMagickException(exception,GetMagickModule(),RegistryError,
507  "UnableToSetRegistry","%s",key);
508  return(MagickFalse);
509  }
510  clone_value=(void *) CloneImageInfo(image_info);
511  break;
512  }
513  }
514  if (clone_value == (void *) NULL)
515  return(MagickFalse);
516  registry_info=(RegistryInfo *) AcquireCriticalMemory(sizeof(*registry_info));
517  (void) memset(registry_info,0,sizeof(*registry_info));
518  registry_info->type=type;
519  registry_info->value=clone_value;
520  registry_info->signature=MagickCoreSignature;
521  if (registry == (SplayTreeInfo *) NULL)
522  {
523  if (registry_semaphore == (SemaphoreInfo *) NULL)
524  ActivateSemaphoreInfo(&registry_semaphore);
525  LockSemaphoreInfo(registry_semaphore);
526  if (registry == (SplayTreeInfo *) NULL)
527  registry=NewSplayTree(CompareSplayTreeString,RelinquishMagickMemory,
528  DestroyRegistryNode);
529  UnlockSemaphoreInfo(registry_semaphore);
530  }
531  return(AddValueToSplayTree(registry,ConstantString(key),registry_info));
532 }
Definition: image.h:152