Debian Codegraph @

coders/dmr.c

raw symbols SCIP
1 /*
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 % %
4 % %
5 % %
6 % DDDD M M RRRR %
7 % D D MM MM R R %
8 % D D M M M RRRR %
9 % D D M M R R %
10 % DDDD M M R R %
11 % %
12 % %
13 % Get or put content to a Digital Media Repository. %
14 % %
15 % Software Design %
16 % Cristy %
17 % May 2023 %
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 Include declarations.
41 */
42 #include "MagickCore/studio.h"
43 #include "MagickCore/blob.h"
44 #include "MagickCore/blob-private.h"
45 #include "MagickCore/client.h"
46 #include "MagickCore/colormap.h"
47 #include "MagickCore/exception.h"
48 #include "MagickCore/exception-private.h"
49 #include "MagickCore/image.h"
50 #include "MagickCore/image-private.h"
51 #include "MagickCore/list.h"
52 #include "MagickCore/magick.h"
53 #include "MagickCore/memory_.h"
54 #include "MagickCore/monitor.h"
55 #include "MagickCore/monitor-private.h"
56 #include "MagickCore/pixel-accessor.h"
57 #include "MagickCore/quantum-private.h"
58 #include "MagickCore/static.h"
59 #include "MagickCore/string_.h"
60 #include "MagickCore/module.h"
61 #include "MagickCore/utility.h"
62 #include "MagickCore/xwindow-private.h"
63 #if defined(MAGICKCORE_DMR_DELEGATE)
64 #include <MagickCache/MagickCache.h>
65 #endif
66
67 #if defined(MAGICKCORE_DMR_DELEGATE)
68 /*
69 Forward declarations.
70 */
71 static MagickBooleanType
72 WriteDMRImage(const ImageInfo *,Image *,ExceptionInfo *);
73
74 /*
75 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
76 % %
77 % %
78 % %
79 % R e a d D M R I m a g e %
80 % %
81 % %
82 % %
83 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
84 %
85 % ReadDMRImage() reads an image from a digital media repository and returns it.
86 % It allocates the memory necessary for the new Image structure and returns a
87 % pointer to the new image.
88 %
89 % The format of the ReadDMRImage method is:
90 %
91 % Image *ReadDMRImage(const ImageInfo *image_info,
92 % ExceptionInfo *exception)
93 %
94 % A description of each parameter follows:
95 %
96 % o image_info: the image info.
97 %
98 % o exception: return any errors or warnings in this structure.
99 %
100 */
101 static Image *ReadDMRImage(const ImageInfo *image_info,ExceptionInfo *exception)
102 {
103 #define DestroyDMRResources() \
104 { \
105 if (cache != (MagickCache *) NULL) \
106 cache=DestroyMagickCache(cache); \
107 if (message != (char *) NULL) \
108 message=DestroyString(message); \
109 if (passkey != (StringInfo *) NULL) \
110 passkey=DestroyStringInfo(passkey); \
111 if (passphrase != (StringInfo *) NULL) \
112 passphrase=DestroyStringInfo(passphrase); \
113 if (path != (char *) NULL) \
114 path=DestroyString(path); \
115 if (resource != (MagickCacheResource *) NULL) \
116 resource=DestroyMagickCacheResource(resource); \
117 }
118 #define ThrowDMRReadException() \
119 { \
120 DestroyDMRResources(); \
121 if (image != (Image *) NULL) \
122 image=DestroyImageList(image); \
123 return(image); \
124 }
125
126 char
127 *message = (char *) NULL,
128 *path = (char *) NULL;
129
130 const char
131 *option;
132
133 Image
134 *image = (Image *) NULL;
135
136 MagickBooleanType
137 status;
138
139 MagickCache
140 *cache = (MagickCache *) NULL;
141
142 MagickCacheResource
143 *resource = (MagickCacheResource *) NULL;
144
145 MagickCacheResourceType
146 type;
147
148 StringInfo
149 *passkey = (StringInfo *) NULL,
150 *passphrase = (StringInfo *) NULL;
151
152 assert(image_info != (const ImageInfo *) NULL);
153 assert(image_info->signature == MagickCoreSignature);
154 assert(exception != (ExceptionInfo *) NULL);
155 assert(exception->signature == MagickCoreSignature);
156 if (IsEventLogging() != MagickFalse)
157 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
158 image_info->filename);
159 option=GetImageOption(image_info,"dmr:path");
160 if (option != (const char *) NULL)
161 path=ConstantString(option);
162 option=GetImageOption(image_info,"dmr:passkey");
163 if (option != (const char *) NULL)
164 {
165 passkey=FileToStringInfo(option,~0UL,exception);
166 if (passkey == (StringInfo *) NULL)
167 {
168 message=GetExceptionMessage(errno);
169 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
170 "unable to read passkey","`%s': %s",option,message);
171 ThrowDMRReadException();
172 }
173 }
174 option=GetImageOption(image_info,"dmr:passphrase");
175 if (option != (const char *) NULL)
176 {
177 passphrase=FileToStringInfo(option,~0UL,exception);
178 if (passphrase == (StringInfo *) NULL)
179 {
180 message=GetExceptionMessage(errno);
181 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
182 "unable to read passphrase","`%s': %s",option,message);
183 ThrowDMRReadException();
184 }
185 }
186 cache=AcquireMagickCache(path,passkey);
187 if (cache == (MagickCache *) NULL)
188 {
189 message=GetExceptionMessage(errno);
190 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
191 "unable to open digital media repository","`%s': %s",path,message);
192 ThrowDMRReadException();
193 }
194 resource=AcquireMagickCacheResource(cache,image_info->filename);
195 type=GetMagickCacheResourceType(resource);
196 image=(Image *) NULL;
197 switch (type)
198 {
199 case ImageResourceType:
200 {
201 image=GetMagickCacheResourceImage(cache,resource,(const char *) NULL);
202 if (image != (Image *) NULL)
203 image=CloneImageList(image,exception);
204 break;
205 }
206 case BlobResourceType:
207 {
208 ImageInfo
209 *read_info;
210
211 void *blob = GetMagickCacheResourceBlob(cache,resource);
212 if (blob == (void *) NULL)
213 break;
214 read_info=AcquireImageInfo();
215 image=BlobToImage(read_info,blob,GetMagickCacheResourceExtent(
216 resource),exception);
217 read_info=DestroyImageInfo(read_info);
218 break;
219 }
220 case MetaResourceType:
221 {
222 const char *meta = GetMagickCacheResourceMeta(cache,resource);
223 if (meta == (char *) NULL)
224 break;
225 image=AcquireImage(image_info,exception);
226 (void) SetImageExtent(image,1,1,exception);
227 (void) SetImageBackgroundColor(image,exception);
228 (void) SetImageProperty(image,"dmr:meta",meta,exception);
229 break;
230 }
231 default:
232 {
233 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
234 "resource type not supported","`%s'",image_info->filename);
235 ThrowDMRReadException();
236 }
237 }
238 if (image == (Image *) NULL)
239 {
240 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
241 "no such resource","`%s'",image_info->filename);
242 ThrowDMRReadException();
243 }
244 if (passphrase != (StringInfo *) NULL)
245 {
246 status=PasskeyDecipherImage(image,passphrase,exception);
247 if (status == MagickFalse)
248 ThrowDMRReadException();
249 }
250 DestroyDMRResources();
251 return(image);
252 }
253 #endif
254
255 /*
256 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
257 % %
258 % %
259 % %
260 % R e g i s t e r D M R I m a g e %
261 % %
262 % %
263 % %
264 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
265 %
266 % RegisterDMRImage() adds attributes for the Magick Cache repository format
267 % to the list of supported formats. The attributes include the image format
268 % tag, a method to read and/or write the format, whether the format supports
269 % the saving of more than one frame to the same file or blob, whether the
270 % format supports native in-memory I/O, and a brief description of the format.
271 %
272 % The format of the RegisterDMRImage method is:
273 %
274 % size_t RegisterDMRImage(void)
275 %
276 */
277 ModuleExport size_t RegisterDMRImage(void)
278 {
279 MagickInfo
280 *entry;
281
282 entry=AcquireMagickInfo("DMR","DMR","Digital Media Repository");
283 #if defined(MAGICKCORE_DMR_DELEGATE)
284 entry->decoder=(DecodeImageHandler *) ReadDMRImage;
285 entry->encoder=(EncodeImageHandler *) WriteDMRImage;
286 #endif
287 entry->flags^=CoderBlobSupportFlag;
288 (void) RegisterMagickInfo(entry);
289 return(MagickImageCoderSignature);
290 }
291
292 /*
293 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
294 % %
295 % %
296 % %
297 % U n r e g i s t e r D M R I m a g e %
298 % %
299 % %
300 % %
301 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
302 %
303 % UnregisterDMRImage() removes format registrations made by the MCR module
304 % from the list of supported formats.
305 %
306 % The format of the UnregisterDMRImage method is:
307 %
308 % UnregisterDMRImage(void)
309 %
310 */
311 ModuleExport void UnregisterDMRImage(void)
312 {
313 (void) UnregisterMagickInfo("DMR");
314 }
315
316 #if defined(MAGICKCORE_DMR_DELEGATE)
317 /*
318 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
319 % %
320 % %
321 % %
322 % W r i t e D M R I m a g e %
323 % %
324 % %
325 % %
326 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
327 %
328 % WriteDMRImage() puts an image to the digital media repository.
329 %
330 % The format of the WriteDMRImage method is:
331 %
332 % MagickBooleanType WriteDMRImage(const ImageInfo *image_info,
333 % Image *image,ExceptionInfo *exception)
334 %
335 % A description of each parameter follows.
336 %
337 % o image_info: the image info.
338 %
339 % o image: The image.
340 %
341 % o exception: return any errors or warnings in this structure.
342 %
343 */
344 static MagickBooleanType WriteDMRImage(const ImageInfo *image_info,Image *image,
345 ExceptionInfo *exception)
346 {
347 #define ThrowDMRWriteException() \
348 { \
349 DestroyDMRResources(); \
350 return(MagickFalse); \
351 }
352
353 char
354 *message = (char *) NULL,
355 *path = (char *) NULL;
356
357 const char
358 *option;
359
360 MagickBooleanType
361 status;
362
363 MagickCache
364 *cache = (MagickCache *) NULL;
365
366 MagickCacheResource
367 *resource = (MagickCacheResource *) NULL;
368
369 MagickCacheResourceType
370 type;
371
372 StringInfo
373 *passkey = (StringInfo *) NULL,
374 *passphrase = (StringInfo *) NULL;
375
376 time_t
377 ttl = 0;
378
379 assert(image_info != (const ImageInfo *) NULL);
380 assert(image_info->signature == MagickCoreSignature);
381 assert(image != (const Image *) NULL);
382 assert(image->signature == MagickCoreSignature);
383 assert(exception != (ExceptionInfo *) NULL);
384 assert(exception->signature == MagickCoreSignature);
385 if (IsEventLogging() != MagickFalse)
386 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
387 image_info->filename);
388 option=GetImageOption(image_info,"dmr:path");
389 if (option != (const char *) NULL)
390 path=ConstantString(option);
391 option=GetImageOption(image_info,"dmr:passkey");
392 if (option != (const char *) NULL)
393 {
394 passkey=FileToStringInfo(option,~0UL,exception);
395 if (passkey == (StringInfo *) NULL)
396 {
397 message=GetExceptionMessage(errno);
398 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
399 "unable to read passkey","`%s': %s",option,message);
400 ThrowDMRWriteException();
401 }
402 }
403 option=GetImageOption(image_info,"dmr:passphrase");
404 if (option != (const char *) NULL)
405 {
406 passphrase=FileToStringInfo(option,~0UL,exception);
407 if (passphrase == (StringInfo *) NULL)
408 {
409 message=GetExceptionMessage(errno);
410 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
411 "unable to read passphrase","`%s': %s",option,message);
412 ThrowDMRWriteException();
413 }
414 }
415 option=GetImageOption(image_info,"dmr:ttl");
416 if (option != (const char *) NULL)
417 {
418 char
419 *q;
420
421 /*
422 Time to live, absolute or relative, e.g. 1440, 2 hours, 3 days, ...
423 */
424 ttl=(time_t) InterpretLocaleValue(option,&q);
425 if (q != option)
426 {
427 while (isspace((int) ((unsigned char) *q)) != 0)
428 q++;
429 if (LocaleNCompare(q,"second",6) == 0)
430 ttl*=1;
431 if (LocaleNCompare(q,"minute",6) == 0)
432 ttl*=60;
433 if (LocaleNCompare(q,"hour",4) == 0)
434 ttl*=3600;
435 if (LocaleNCompare(q,"day",3) == 0)
436 ttl*=86400;
437 if (LocaleNCompare(q,"week",4) == 0)
438 ttl*=604800;
439 if (LocaleNCompare(q,"month",5) == 0)
440 ttl*=2628000;
441 if (LocaleNCompare(q,"year",4) == 0)
442 ttl*=31536000;
443 }
444 }
445 cache=AcquireMagickCache(path,passkey);
446 if (cache == (MagickCache *) NULL)
447 {
448 message=GetExceptionMessage(errno);
449 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
450 "unable to open digital media repository","`%s': %s",path,message);
451 ThrowDMRWriteException();
452 }
453 resource=AcquireMagickCacheResource(cache,image_info->filename);
454 SetMagickCacheResourceTTL(resource,ttl);
455 type=GetMagickCacheResourceType(resource);
456 switch (type)
457 {
458 case ImageResourceType:
459 {
460 Image
461 *resource_image;
462
463 resource_image=CloneImageList(image,exception);
464 if (resource_image == (Image *) NULL)
465 ThrowDMRWriteException();
466 if (passphrase != (StringInfo *) NULL)
467 {
468 status=PasskeyEncipherImage(resource_image,passphrase,exception);
469 if (status == MagickFalse)
470 ThrowDMRWriteException();
471 }
472 status=PutMagickCacheResourceImage(cache,resource,resource_image);
473 resource_image=DestroyImageList(resource_image);
474 break;
475 }
476 case BlobResourceType:
477 {
478 size_t
479 extent;
480
481 ImageInfo *write_info = AcquireImageInfo();
482 void *blob = ImageToBlob(write_info,image,&extent,exception);
483 write_info=DestroyImageInfo(write_info);
484 if (blob == (void *) NULL)
485 ThrowDMRWriteException();
486 status=PutMagickCacheResourceBlob(cache,resource,extent,blob);
487 blob=RelinquishMagickMemory(blob);
488 break;
489 }
490 case MetaResourceType:
491 {
492 const char *meta = GetImageProperty(image,"dmr:meta",exception);
493 if (meta == (const char *) NULL)
494 ThrowDMRWriteException();
495 status=PutMagickCacheResourceMeta(cache,resource,meta);
496 if (status == MagickFalse)
497 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
498 "no such metadata","`%s'",image->filename);
499 break;
500 }
501 default:
502 {
503 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
504 "resource type not supported","`%s'",image->filename);
505 ThrowDMRWriteException();
506 break;
507 }
508 }
509 if (status == MagickFalse)
510 ThrowDMRWriteException();
511 DestroyDMRResources();
512 return(status);
513 }
514 #endif