blob: 41794d09254e5508d8889eaf0314d70592547a00 [file] [log] [blame]
cristy490408a2011-07-07 14:42:05 +00001/*
2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3% %
4% %
5% %
6% X X TTTTT RRRR N N %
7% X X T R R NN N %
8% X T RRRR N N N %
9% X X T R R N NN %
10% X X T R R N N %
11% %
12% %
13% ImageMagickObject BLOB Interface. %
14% %
15% Software Design %
16% William Radcliffe %
17% May 2001 %
18% %
19% %
20% Copyright 1999-2007 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% http://www.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% This coder is a kind of backdoor used by the COM object that allows it to %
38% pass blobs back and forth using the coder interface. It simply encodes and %
39% decodes the filename as a comma delimited string and extracts the info it %
40% needs. The five methods of passing images are: %
41% %
42% FILE - same thing as filename so it should be a NOP %
43% IMAGE - passes an image and image info structure %
44% BLOB - passes binary blob containining the image %
45% STREAM - passes pointers to stream hooks in and does the hooking %
46% ARRAY - passes a pointer to a Win32 smart array and streams to it %
47% %
48% Of all of these, the only one getting any real use at the moment is the %
49% ARRAY handler. It is the primary way that images are shuttled back and %
50% forth as blobs via COM since this is what VB and VBSCRIPT use internally %
51% for this purpose. %
52%
53%
54*/
55
56/*
57 Include declarations.
58*/
59#if defined(_VISUALC_)
cristy154fa9d2011-08-05 14:25:15 +000060#include "MagickCore/studio.h"
61#include "MagickCore/blob.h"
62#include "MagickCore/blob-private.h"
63#include "MagickCore/constitute.h"
64#include "MagickCore/delegate.h"
65#include "MagickCore/exception.h"
66#include "MagickCore/exception-private.h"
67#include "MagickCore/image.h"
68#include "MagickCore/image-private.h"
69#include "MagickCore/list.h"
70#include "MagickCore/MagickCore.h"
71#include "MagickCore/memory_.h"
72#include "MagickCore/string_.h"
cristy490408a2011-07-07 14:42:05 +000073#define WIN32_LEAN_AND_MEAN
74#define VC_EXTRALEAN
75#include <windows.h>
76#include <ole2.h>
77
78/*
79 Forward declarations.
80*/
81static MagickBooleanType
cristy0a4d92f2011-08-31 19:25:00 +000082 WriteXTRNImage(const ImageInfo *,Image *,ExceptionInfo *exception);
cristy490408a2011-07-07 14:42:05 +000083
84/*
85%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
86% %
87% %
88% %
89% R e a d X T R N I m a g e %
90% %
91% %
92% %
93%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
94%
95% ReadXTRNImage() reads a XTRN image file and returns it. It
96% allocates the memory necessary for the new Image structure and returns a
97% pointer to the new image.
98%
99% The format of the ReadXTRNImage method is:
100%
101% Image *ReadXTRNImage(const ImageInfo *image_info,
102% ExceptionInfo *exception)
103%
104% A description of each parameter follows:
105%
106% o image_info: Specifies a pointer to an ImageInfo structure.
107%
108% o exception: return any errors or warnings in this structure.
109%
110*/
111static Image *ReadXTRNImage(const ImageInfo *image_info,
112 ExceptionInfo *exception)
113{
114 Image
115 *image;
116
117 ImageInfo
118 *clone_info;
119
120 void
121 *param1,
122 *param2,
123 *param3;
124
125 param1 = param2 = param3 = (void *) NULL;
126 image = (Image *) NULL;
127 clone_info=CloneImageInfo(image_info);
128 if (clone_info->filename == NULL)
129 {
130 clone_info=DestroyImageInfo(clone_info);
131 ThrowReaderException(FileOpenWarning,"No filename specified");
132 }
133 if (LocaleCompare(image_info->magick,"XTRNFILE") == 0)
134 {
135 image=ReadImage(clone_info,exception);
136 CatchException(exception);
137 }
138 else if (LocaleCompare(image_info->magick,"XTRNIMAGE") == 0)
139 {
140 Image
141 **image_ptr;
142
143#ifdef ALL_IMAGEINFO
144 ImageInfo
145 **image_info_ptr;
146#endif
147
148 (void) sscanf(clone_info->filename,"%lx,%lx",&param1,&param2);
149 image_ptr=(Image **) param2;
150 if (*image_ptr != (Image *)NULL)
cristyb541bd72012-02-20 14:55:11 +0000151 image=CloneImage(*image_ptr,0,0,MagickFalse,exception);
cristy490408a2011-07-07 14:42:05 +0000152#ifdef ALL_IMAGEINFO
153 image_info_ptr=(ImageInfo **) param1;
154 if (*image_info_ptr != (ImageInfo *)NULL)
155 image_info=*image_info_ptr;
156#endif
157 }
158 else if (LocaleCompare(image_info->magick,"XTRNBLOB") == 0)
159 {
160 char
161 **blob_data;
162
163 size_t
164 *blob_length;
165
166 char
167 filename[MaxTextExtent];
168
169 (void) sscanf(clone_info->filename,"%lx,%lx,%s",&param1,&param2,&filename);
170 blob_data=(char **) param1;
171 blob_length=(size_t *) param2;
172 image=BlobToImage(clone_info,*blob_data,*blob_length,exception);
173 CatchException(exception);
174 }
175 else if (LocaleCompare(image_info->magick,"XTRNSTREAM") == 0)
176 {
177#ifdef IMPLEMENT_THIS
178 MagickBooleanType
179 status;
180#endif
181
182 char
183 filename[MaxTextExtent];
184
185 size_t
186 (*fifo)(const Image *,const void *,const size_t);
187
188 (void) sscanf(clone_info->filename,"%lx,%lx,%s",&param1,&param2,&filename);
189 fifo=(size_t (*)(const Image *,const void *,const size_t)) param1;
190 clone_info->client_data=param2;
191#ifdef IMPLEMENT_THIS
192 status=ReadStream(clone_info,fifo,exception);
193 CatchException(exception);
194#endif
195 }
196 else if (LocaleCompare(image_info->magick,"XTRNARRAY") == 0)
197 {
198 SAFEARRAY
199 *pSafeArray;
200
201 char
202 *blob_data;
203
204 size_t
205 blob_length;
206
207 ssize_t
208 lBoundl,
209 lBoundu;
210
211 HRESULT
212 hr;
213
214 char
215 filename[MaxTextExtent];
216
217 filename[0] = '\0';
218 (void) sscanf(clone_info->filename,"%lx,%s",&param1,&filename);
219 hr = S_OK;
220 pSafeArray = (SAFEARRAY *) param1;
221 if (pSafeArray)
222 {
223 hr = SafeArrayGetLBound(pSafeArray, 1, &lBoundl);
224 if (SUCCEEDED(hr))
225 hr = SafeArrayGetUBound(pSafeArray, 1, &lBoundu);
226 if (SUCCEEDED(hr))
227 {
228 blob_length = lBoundu - lBoundl + 1;
229 hr = SafeArrayAccessData(pSafeArray, (void**)&blob_data);
230 if(SUCCEEDED(hr))
231 {
232 if (filename[0] != '\0')
233 {
234 (void) CopyMagickString(clone_info->filename,filename,
235 MaxTextExtent);
236 (void) CopyMagickString(clone_info->magick,filename,
237 MaxTextExtent);
238 }
239 else
240 {
241 *clone_info->magick = '\0';
242 clone_info->filename[0] = '\0';
243 }
244 image=BlobToImage(clone_info,blob_data,blob_length,exception);
245 hr = SafeArrayUnaccessData(pSafeArray);
246 CatchException(exception);
247 }
248 }
249 }
250 }
251 else if (LocaleCompare(image_info->magick,"XTRNBSTR") == 0)
252 {
253 BSTR
254 bstr;
255
256 char
257 *blob_data;
258
259 size_t
260 blob_length;
261
262 HRESULT
263 hr;
264
265 char
266 filename[MaxTextExtent];
267
268 filename[0] = '\0';
269 (void) sscanf(clone_info->filename,"%lx,%s",&param1,&filename);
270 hr = S_OK;
271 bstr = (BSTR) param1;
272 blob_length = SysStringLen(bstr) * 2;
273 blob_data = (char *)bstr;
274 if ((blob_data != (char *)NULL) && (blob_length>0))
275 {
276 if (filename[0] != '\0')
277 {
278 (void) CopyMagickString(clone_info->filename,filename,
279 MaxTextExtent);
280 (void) CopyMagickString(clone_info->magick,filename,
281 MaxTextExtent);
282 }
283 else
284 {
285 *clone_info->magick = '\0';
286 clone_info->filename[0] = '\0';
287 }
288 image=BlobToImage(clone_info,blob_data,blob_length,exception);
289 CatchException(exception);
290 }
291 }
292 clone_info=DestroyImageInfo(clone_info);
293 return(image);
294}
295
296/*
297%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
298% %
299% %
300% %
301% R e g i s t e r X T R N I m a g e %
302% %
303% %
304% %
305%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
306%
307% RegisterXTRNImage() adds attributes for the XTRN image format to
308% the list of supported formats. The attributes include the image format
309% tag, a method to read and/or write the format, whether the format
310% supports the saving of more than one frame to the same file or blob,
311% whether the format supports native in-memory I/O, and a brief
312% description of the format.
313%
314% The format of the RegisterXTRNImage method is:
315%
316% RegisterXTRNImage(void)
317%
318*/
319ModuleExport void RegisterXTRNImage(void)
320{
321 MagickInfo
322 *entry;
323
324 entry=SetMagickInfo("XTRNFILE");
325 entry->decoder=ReadXTRNImage;
326 entry->encoder=WriteXTRNImage;
327 entry->adjoin=MagickFalse;
328 entry->stealth=MagickTrue;
329 entry->description=ConstantString("External transfer of a file");
330 entry->module=ConstantString("XTRN");
331 RegisterMagickInfo(entry);
332
333 entry=SetMagickInfo("XTRNIMAGE");
334 entry->decoder=ReadXTRNImage;
335 entry->encoder=WriteXTRNImage;
336 entry->adjoin=MagickFalse;
337 entry->stealth=MagickTrue;
338 entry->description=ConstantString("External transfer of a image in memory");
339 entry->module=ConstantString("XTRN");
340 RegisterMagickInfo(entry);
341
342 entry=SetMagickInfo("XTRNBLOB");
343 entry->decoder=ReadXTRNImage;
344 entry->encoder=WriteXTRNImage;
345 entry->adjoin=MagickFalse;
346 entry->stealth=MagickTrue;
347 entry->description=ConstantString("IExternal transfer of a blob in memory");
348 entry->module=ConstantString("XTRN");
349 RegisterMagickInfo(entry);
350
351 entry=SetMagickInfo("XTRNSTREAM");
352 entry->decoder=ReadXTRNImage;
353 entry->encoder=WriteXTRNImage;
354 entry->adjoin=MagickFalse;
355 entry->stealth=MagickTrue;
356 entry->description=ConstantString("External transfer via a streaming interface");
357 entry->module=ConstantString("XTRN");
358 RegisterMagickInfo(entry);
359
360 entry=SetMagickInfo("XTRNARRAY");
361 entry->decoder=ReadXTRNImage;
362 entry->encoder=WriteXTRNImage;
363 entry->adjoin=MagickFalse;
364 entry->stealth=MagickTrue;
365 entry->description=ConstantString("External transfer via a smart array interface");
366 entry->module=ConstantString("XTRN");
367 RegisterMagickInfo(entry);
368
369 entry=SetMagickInfo("XTRNBSTR");
370 entry->decoder=ReadXTRNImage;
371 entry->encoder=WriteXTRNImage;
372 entry->adjoin=MagickFalse;
373 entry->stealth=MagickTrue;
374 entry->description=ConstantString("External transfer via a smart array interface");
375 entry->module=ConstantString("XTRN");
376 RegisterMagickInfo(entry);
377}
378
379/*
380%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
381% %
382% %
383% %
384% U n r e g i s t e r X T R N I m a g e %
385% %
386% %
387% %
388%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
389%
390% UnregisterXTRNImage() removes format registrations made by the
391% XTRN module from the list of supported formats.
392%
393% The format of the UnregisterXTRNImage method is:
394%
395% UnregisterXTRNImage(void)
396%
397*/
398ModuleExport void UnregisterXTRNImage(void)
399{
400 UnregisterMagickInfo("XTRNFILE");
401 UnregisterMagickInfo("XTRNIMAGE");
402 UnregisterMagickInfo("XTRNBLOB");
403 UnregisterMagickInfo("XTRNSTREAM");
404 UnregisterMagickInfo("XTRNARRAY");
405 UnregisterMagickInfo("XTRNBSTR");
406}
407
408/*
409%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
410% %
411% %
412% %
413% W r i t e X T R N I m a g e %
414% %
415% %
416% %
417%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
418%
419% WriteXTRNImage() writes an image in the XTRN encoded image format.
420% We use GIF because it is the only format that is compressed without
421% requiring addition optional delegates (TIFF, ZIP, etc).
422%
423% The format of the WriteXTRNImage method is:
424%
cristy0a4d92f2011-08-31 19:25:00 +0000425% MagickBooleanType WriteXTRNImage(const ImageInfo *image_info,
426% Image *image,ExceptionInfo *exception)
cristy490408a2011-07-07 14:42:05 +0000427%
428% A description of each parameter follows.
429%
430% o image_info: Specifies a pointer to an ImageInfo structure.
431%
432% o image: A pointer to a Image structure.
433%
cristy0a4d92f2011-08-31 19:25:00 +0000434% o exception: return any errors or warnings in this structure.
cristy490408a2011-07-07 14:42:05 +0000435%
436*/
437
438size_t SafeArrayFifo(const Image *image,const void *data,const size_t length)
439{
440 SAFEARRAYBOUND NewArrayBounds[1]; /* 1 Dimension */
441 size_t tlen=length;
442 SAFEARRAY *pSafeArray = (SAFEARRAY *)image->client_data;
443 if (pSafeArray != NULL)
444 {
445 ssize_t lBoundl, lBoundu, lCount;
446 HRESULT hr = S_OK;
447 /* First see how big the buffer currently is */
448 hr = SafeArrayGetLBound(pSafeArray, 1, &lBoundl);
449 if (FAILED(hr))
450 return MagickFalse;
451 hr = SafeArrayGetUBound(pSafeArray, 1, &lBoundu);
452 if (FAILED(hr))
453 return MagickFalse;
454 lCount = lBoundu - lBoundl + 1;
455
456 if (length>0)
457 {
458 unsigned char *pReturnBuffer = NULL;
459 NewArrayBounds[0].lLbound = 0; /* Start-Index 0 */
460 NewArrayBounds[0].cElements = (size_t) (length+lCount); /* # Elemente */
461 hr = SafeArrayRedim(pSafeArray, NewArrayBounds);
462 if (FAILED(hr))
463 return 0;
464 hr = SafeArrayAccessData(pSafeArray, (void**)&pReturnBuffer);
465 if( FAILED(hr) )
466 return 0;
467 (void) CopyMagickMemory( pReturnBuffer+lCount, (unsigned char *)data, length );
468 hr = SafeArrayUnaccessData(pSafeArray);
469 if( FAILED(hr) )
470 return 0;
471 }
472 else
473 {
474 /* Adjust the length of the buffer to fit */
475 }
476 }
477 return(tlen);
478}
479
cristyb541bd72012-02-20 14:55:11 +0000480static MagickBooleanType WriteXTRNImage(const ImageInfo *image_info,
481 Image *image,ExceptionInfo *exception)
cristy490408a2011-07-07 14:42:05 +0000482{
483 Image *
484 p;
485
486 ImageInfo
487 *clone_info;
488
489 int
490 scene;
491
492 MagickBooleanType
493 status;
494
495 void
496 *param1,
497 *param2,
498 *param3;
499
500 param1 = param2 = param3 = (void *) NULL;
501 if (LocaleCompare(image_info->magick,"XTRNFILE") == 0)
502 {
503 clone_info=CloneImageInfo(image_info);
cristy0a4d92f2011-08-31 19:25:00 +0000504 status=WriteImage(image_info,image,exception);
cristy490408a2011-07-07 14:42:05 +0000505 if (status == MagickFalse)
506 CatchImageException(image);
507 clone_info=DestroyImageInfo(clone_info);
508 }
509 else if (LocaleCompare(image_info->magick,"XTRNIMAGE") == 0)
510 {
511 Image
512 **image_ptr;
513
514 ImageInfo
515 **image_info_ptr;
516
517 clone_info=CloneImageInfo(image_info);
518 if (clone_info->filename[0])
519 {
520 (void) sscanf(clone_info->filename,"%lx,%lx",&param1,&param2);
521 image_info_ptr=(ImageInfo **) param1;
522 image_ptr=(Image **) param2;
523 if ((image_info_ptr != (ImageInfo **) NULL) &&
524 (image_ptr != (Image **) NULL))
525 {
cristy0a4d92f2011-08-31 19:25:00 +0000526 *image_ptr=CloneImage(image,0,0,MagickFalse,exception);
cristy490408a2011-07-07 14:42:05 +0000527 *image_info_ptr=clone_info;
528 }
529 }
530 }
531 else if (LocaleCompare(image_info->magick,"XTRNBLOB") == 0)
532 {
533 char
534 **blob_data;
535
cristy490408a2011-07-07 14:42:05 +0000536 size_t
537 *blob_length;
538
539 char
540 filename[MaxTextExtent];
541
542 clone_info=CloneImageInfo(image_info);
543 if (clone_info->filename[0])
544 {
545 (void) sscanf(clone_info->filename,"%lx,%lx,%s",
546 &param1,&param2,&filename);
547
548 blob_data=(char **) param1;
549 blob_length=(size_t *) param2;
550
551 scene = 0;
552 (void) CopyMagickString(clone_info->filename,filename,MaxTextExtent);
553 for (p=image; p != (Image *) NULL; p=GetNextImageInList(p))
554 {
555 (void) CopyMagickString(p->filename,filename,MaxTextExtent);
556 p->scene=scene++;
557 }
cristy0a4d92f2011-08-31 19:25:00 +0000558 SetImageInfo(clone_info,1,exception);
cristy490408a2011-07-07 14:42:05 +0000559 (void) CopyMagickString(image->magick,clone_info->magick,
560 MaxTextExtent);
cristy490408a2011-07-07 14:42:05 +0000561 if (*blob_length == 0)
562 *blob_length=8192;
563 *blob_data=(char *) ImageToBlob(clone_info,image,blob_length,
cristy0a4d92f2011-08-31 19:25:00 +0000564 exception);
cristy490408a2011-07-07 14:42:05 +0000565 if (*blob_data == NULL)
566 status=MagickFalse;
567 if (status == MagickFalse)
568 CatchImageException(image);
569 }
570 clone_info=DestroyImageInfo(clone_info);
571 }
572 else if (LocaleCompare(image_info->magick,"XTRNSTREAM") == 0)
573 {
574 size_t
575 (*fifo)(const Image *,const void *,const size_t);
576
577 char
578 filename[MaxTextExtent];
579
580 clone_info=CloneImageInfo(image_info);
581 if (clone_info->filename[0])
582 {
583 (void) sscanf(clone_info->filename,"%lx,%lx,%s",
584 &param1,&param2,&filename);
585
586 fifo=(size_t (*)(const Image *,const void *,const size_t)) param1;
587 image->client_data=param2;
588
589 scene=0;
590 (void) CopyMagickString(clone_info->filename,filename,MaxTextExtent);
591 for (p=image; p != (Image *) NULL; p=GetNextImageInList(p))
592 {
593 (void) CopyMagickString(p->filename,filename,MaxTextExtent);
594 p->scene=scene++;
595 }
cristy0a4d92f2011-08-31 19:25:00 +0000596 SetImageInfo(clone_info,1,exception);
cristy490408a2011-07-07 14:42:05 +0000597 (void) CopyMagickString(image->magick,clone_info->magick,
598 MaxTextExtent);
cristyb541bd72012-02-20 14:55:11 +0000599 status=WriteStream(clone_info,image,fifo,exception);
cristy490408a2011-07-07 14:42:05 +0000600 if (status == MagickFalse)
601 CatchImageException(image);
602 }
603 clone_info=DestroyImageInfo(clone_info);
604 }
605 else if (LocaleCompare(image_info->magick,"XTRNARRAY") == 0)
606 {
607 char
608 filename[MaxTextExtent];
609
610 clone_info=CloneImageInfo(image_info);
611 if (clone_info->filename[0])
612 {
613 (void) sscanf(clone_info->filename,"%lx,%s",
614 &param1,&filename);
615
616 image->client_data=param1;
617
618 scene = 0;
619 (void) CopyMagickString(clone_info->filename,filename,MaxTextExtent);
620 for (p=image; p != (Image *) NULL; p=GetNextImageInList(p))
621 {
622 (void) CopyMagickString(p->filename,filename,MaxTextExtent);
623 p->scene=scene++;
624 }
cristy0a4d92f2011-08-31 19:25:00 +0000625 SetImageInfo(clone_info,1,exception);
cristy490408a2011-07-07 14:42:05 +0000626 (void) CopyMagickString(image->magick,clone_info->magick,
627 MaxTextExtent);
cristyb541bd72012-02-20 14:55:11 +0000628 status=WriteStream(clone_info,image,SafeArrayFifo,exception);
cristy490408a2011-07-07 14:42:05 +0000629 if (status == MagickFalse)
630 CatchImageException(image);
631 }
632 clone_info=DestroyImageInfo(clone_info);
633 }
634 return(MagickTrue);
635}
636#endif