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