MagickWand  7.1.0
Convert, Edit, Or Compose Bitmap Images
wand.c
1 /*
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 % %
4 % %
5 % %
6 % W W AAA N N DDDD %
7 % W W A A NN N D D %
8 % W W W AAAAA N N N D D %
9 % WW WW A A N NN D D %
10 % W W A A N N DDDD %
11 % %
12 % %
13 % MagickWand Support Methods %
14 % %
15 % Software Design %
16 % Cristy %
17 % May 2004 %
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/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 % Assign unique ID's to each magick wand. We store the ID's in a splay-tree
37 % to track which ID's persist and which were destroyed.
38 %
39 %
40 */
41 ␌
42 /*
43  Include declarations.
44 */
45 #include "MagickWand/studio.h"
46 #include "MagickWand/MagickWand.h"
47 #include "MagickWand/magick-wand-private.h"
48 #include "MagickWand/wand.h"
49 ␌
50 static SplayTreeInfo
51  *wand_ids = (SplayTreeInfo *) NULL;
52 
53 static MagickBooleanType
54  instantiate_wand = MagickFalse;
55 
56 static SemaphoreInfo
57  *wand_semaphore = (SemaphoreInfo *) NULL;
58 ␌
59 /*
60 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
61 % %
62 % %
63 % %
64 % A c q u i r e W a n d I d %
65 % %
66 % %
67 % %
68 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
69 %
70 % AcquireWandId() returns a unique wand id.
71 %
72 % The format of the AcquireWandId() method is:
73 %
74 % size_t AcquireWandId()
75 %
76 */
77 WandExport size_t AcquireWandId(void)
78 {
79  const size_t
80  *wand_id = (const size_t *) NULL;
81 
82  size_t
83  id;
84 
85  static size_t
86  isn = 0;
87 
88  if (wand_semaphore == (SemaphoreInfo *) NULL)
89  ActivateSemaphoreInfo(&wand_semaphore);
90  LockSemaphoreInfo(wand_semaphore);
91  if (wand_ids == (SplayTreeInfo *) NULL)
92  wand_ids=NewSplayTree((int (*)(const void *,const void *)) NULL,
93  (void *(*)(void *)) NULL,(void *(*)(void *)) NULL);
94  id=isn++;
95  (void) AddValueToSplayTree(wand_ids,wand_id+id,wand_id+id);
96  instantiate_wand=MagickTrue;
97  UnlockSemaphoreInfo(wand_semaphore);
98  return(id);
99 }
100 ␌
101 /*
102 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
103 % %
104 % %
105 % %
106 % D e s t r o y W a n d I d s %
107 % %
108 % %
109 % %
110 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
111 %
112 % DestroyWandIds() deallocates memory associated with the wand id's.
113 %
114 % The format of the DestroyWandIds() method is:
115 %
116 % void DestroyWandIds(void)
117 %
118 % A description of each parameter follows:
119 %
120 */
121 WandExport void DestroyWandIds(void)
122 {
123  if (wand_semaphore == (SemaphoreInfo *) NULL)
124  ActivateSemaphoreInfo(&wand_semaphore);
125  LockSemaphoreInfo(wand_semaphore);
126  if (wand_ids != (SplayTreeInfo *) NULL)
127  wand_ids=DestroySplayTree(wand_ids);
128  instantiate_wand=MagickFalse;
129  UnlockSemaphoreInfo(wand_semaphore);
130  RelinquishSemaphoreInfo(&wand_semaphore);
131 }
132 ␌
133 /*
134 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
135 % %
136 % %
137 % %
138 % R e l i n q u i s h W a n d I d %
139 % %
140 % %
141 % %
142 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
143 %
144 % RelinquishWandId() relinquishes a unique wand id.
145 %
146 % The format of the RelinquishWandId() method is:
147 %
148 % void RelinquishWandId(const size_t *id)
149 %
150 % A description of each parameter follows:
151 %
152 % o id: a unique wand id.
153 %
154 */
155 WandExport void RelinquishWandId(const size_t id)
156 {
157  const size_t
158  *wand_id = (const size_t *) NULL;
159 
160  LockSemaphoreInfo(wand_semaphore);
161  if (wand_ids != (SplayTreeInfo *) NULL)
162  (void) DeleteNodeFromSplayTree(wand_ids,wand_id+id);
163  UnlockSemaphoreInfo(wand_semaphore);
164 }