2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % ImageMagickObject BLOB Interface. %
20 % Copyright 1999-2015 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
26 % http://www.imagemagick.org/script/license.php %
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. %
35 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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: %
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 %
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 %
59 #include "MagickCore/studio.h"
60 #include "MagickCore/blob.h"
61 #include "MagickCore/blob-private.h"
62 #include "MagickCore/constitute.h"
63 #include "MagickCore/delegate.h"
64 #include "MagickCore/exception.h"
65 #include "MagickCore/exception-private.h"
66 #include "MagickCore/image.h"
67 #include "MagickCore/image-private.h"
68 #include "MagickCore/list.h"
69 #include "MagickCore/MagickCore.h"
70 #include "MagickCore/memory_.h"
71 #include "MagickCore/string_.h"
72 #if defined(_VISUALC_)
73 #define WIN32_LEAN_AND_MEAN
81 static MagickBooleanType
82 WriteXTRNImage(const ImageInfo *,Image *,ExceptionInfo *exception);
86 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
90 % R e a d X T R N I m a g e %
94 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
96 % ReadXTRNImage() reads a XTRN image file and returns it. It
97 % allocates the memory necessary for the new Image structure and returns a
98 % pointer to the new image.
100 % The format of the ReadXTRNImage method is:
102 % Image *ReadXTRNImage(const ImageInfo *image_info,
103 % ExceptionInfo *exception)
105 % A description of each parameter follows:
107 % o image_info: Specifies a pointer to an ImageInfo structure.
109 % o exception: return any errors or warnings in this structure.
112 #if defined(_VISUALC_)
113 static Image *ReadXTRNImage(const ImageInfo *image_info,
114 ExceptionInfo *exception)
127 param1 = param2 = param3 = (void *) NULL;
128 image = (Image *) NULL;
129 clone_info=CloneImageInfo(image_info);
130 if (clone_info->filename == NULL)
132 clone_info=DestroyImageInfo(clone_info);
133 ThrowReaderException(FileOpenWarning,"No filename specified");
135 if (LocaleCompare(image_info->magick,"XTRNFILE") == 0)
137 image=ReadImage(clone_info,exception);
138 CatchException(exception);
140 else if (LocaleCompare(image_info->magick,"XTRNIMAGE") == 0)
150 (void) sscanf(clone_info->filename,"%lx,%lx",¶m1,¶m2);
151 image_ptr=(Image **) param2;
152 if (*image_ptr != (Image *) NULL)
153 image=CloneImage(*image_ptr,0,0,MagickFalse,exception);
155 image_info_ptr=(ImageInfo **) param1;
156 if (*image_info_ptr != (ImageInfo *) NULL)
157 image_info=*image_info_ptr;
160 else if (LocaleCompare(image_info->magick,"XTRNBLOB") == 0)
169 filename[MagickPathExtent];
171 (void) sscanf(clone_info->filename,"%lx,%lx,%s",¶m1,¶m2,&filename);
172 blob_data=(char **) param1;
173 blob_length=(size_t *) param2;
174 image=BlobToImage(clone_info,*blob_data,*blob_length,exception);
175 CatchException(exception);
177 else if (LocaleCompare(image_info->magick,"XTRNARRAY") == 0)
181 filename[MagickPathExtent];
197 (void) sscanf(clone_info->filename,"%lx,%s",¶m1,&filename);
199 pSafeArray=(SAFEARRAY *) param1;
202 hr = SafeArrayGetLBound(pSafeArray, 1, &lBoundl);
205 hr = SafeArrayGetUBound(pSafeArray, 1, &lBoundu);
208 blob_length = lBoundu - lBoundl + 1;
209 hr = SafeArrayAccessData(pSafeArray,(void**) &blob_data);
212 *clone_info->filename='\0';
213 *clone_info->magick='\0';
214 if (*filename != '\0')
215 (void) CopyMagickString(clone_info->filename,filename,
217 image=BlobToImage(clone_info,blob_data,blob_length,
219 hr=SafeArrayUnaccessData(pSafeArray);
220 CatchException(exception);
226 clone_info=DestroyImageInfo(clone_info);
232 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
236 % R e g i s t e r X T R N I m a g e %
240 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
242 % RegisterXTRNImage() adds attributes for the XTRN image format to
243 % the list of supported formats. The attributes include the image format
244 % tag, a method to read and/or write the format, whether the format
245 % supports the saving of more than one frame to the same file or blob,
246 % whether the format supports native in-memory I/O, and a brief
247 % description of the format.
249 % The format of the RegisterXTRNImage method is:
251 % size_t RegisterXTRNImage(void)
254 ModuleExport size_t RegisterXTRNImage(void)
259 entry=AcquireMagickInfo("XTRN","XTRNFILE","External transfer of a file");
260 #if defined(_VISUALC_)
261 entry->decoder=ReadXTRNImage;
262 entry->encoder=WriteXTRNImage;
264 entry->flags^=CoderAdjoinFlag;
265 entry->flags|=CoderStealthFlag;
266 RegisterMagickInfo(entry);
267 entry=AcquireMagickInfo("XTRN","XTRNIMAGE",
268 "External transfer of a image in memory");
269 #if defined(_VISUALC_)
270 entry->decoder=ReadXTRNImage;
271 entry->encoder=WriteXTRNImage;
273 entry->flags^=CoderAdjoinFlag;
274 entry->flags|=CoderStealthFlag;
275 RegisterMagickInfo(entry);
276 entry=AcquireMagickInfo("XTRN","XTRNBLOB",
277 "IExternal transfer of a blob in memory");
278 #if defined(_VISUALC_)
279 entry->decoder=ReadXTRNImage;
280 entry->encoder=WriteXTRNImage;
282 entry->flags^=CoderAdjoinFlag;
283 entry->flags|=CoderStealthFlag;
284 RegisterMagickInfo(entry);
285 entry=AcquireMagickInfo("XTRN","XTRNARRAY",
286 "External transfer via a smart array interface");
287 #if defined(_VISUALC_)
288 entry->decoder=ReadXTRNImage;
289 entry->encoder=WriteXTRNImage;
291 entry->flags^=CoderAdjoinFlag;
292 entry->flags|=CoderStealthFlag;
293 RegisterMagickInfo(entry);
294 return(MagickImageCoderSignature);
298 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
302 % U n r e g i s t e r X T R N I m a g e %
306 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
308 % UnregisterXTRNImage() removes format registrations made by the
309 % XTRN module from the list of supported formats.
311 % The format of the UnregisterXTRNImage method is:
313 % UnregisterXTRNImage(void)
316 ModuleExport void UnregisterXTRNImage(void)
318 UnregisterMagickInfo("XTRNFILE");
319 UnregisterMagickInfo("XTRNIMAGE");
320 UnregisterMagickInfo("XTRNBLOB");
321 UnregisterMagickInfo("XTRNARRAY");
325 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
329 % W r i t e X T R N I m a g e %
333 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
335 % WriteXTRNImage() writes an image in the XTRN encoded image format.
336 % We use GIF because it is the only format that is compressed without
337 % requiring additional optional delegates (TIFF, ZIP, etc).
339 % The format of the WriteXTRNImage method is:
341 % MagickBooleanType WriteXTRNImage(const ImageInfo *image_info,
342 % Image *image,ExceptionInfo *exception)
344 % A description of each parameter follows.
346 % o image_info: Specifies a pointer to an ImageInfo structure.
348 % o image: A pointer to a Image structure.
350 % o exception: return any errors or warnings in this structure.
354 #if defined(_VISUALC_)
355 static size_t SafeArrayFifo(const Image *image,const void *data,
358 SAFEARRAYBOUND NewArrayBounds[1]; /* 1 Dimension */
360 SAFEARRAY *pSafeArray = (SAFEARRAY *)image->client_data;
361 if (pSafeArray != NULL)
363 long lBoundl, lBoundu, lCount;
365 /* First see how big the buffer currently is */
366 hr = SafeArrayGetLBound(pSafeArray, 1, &lBoundl);
369 hr = SafeArrayGetUBound(pSafeArray, 1, &lBoundu);
372 lCount = lBoundu - lBoundl + 1;
376 unsigned char *pReturnBuffer = NULL;
377 NewArrayBounds[0].lLbound = 0; /* Start-Index 0 */
378 NewArrayBounds[0].cElements = (unsigned long) (length+lCount); /* # Elemente */
379 hr = SafeArrayRedim(pSafeArray, NewArrayBounds);
382 hr = SafeArrayAccessData(pSafeArray, (void**)&pReturnBuffer);
385 (void) memcpy(pReturnBuffer+lCount,(unsigned char *) data,length);
386 hr=SafeArrayUnaccessData(pSafeArray);
392 /* Adjust the length of the buffer to fit */
399 #if defined(_VISUALC_)
400 static MagickBooleanType WriteXTRNImage(const ImageInfo *image_info,
401 Image *image,ExceptionInfo *exception)
420 param1 = param2 = param3 = (void *) NULL;
422 if (LocaleCompare(image_info->magick,"XTRNFILE") == 0)
424 clone_info=CloneImageInfo(image_info);
425 *clone_info->magick='\0';
426 status=WriteImage(clone_info,image,exception);
427 if (status == MagickFalse)
428 CatchImageException(image);
429 clone_info=DestroyImageInfo(clone_info);
431 else if (LocaleCompare(image_info->magick,"XTRNIMAGE") == 0)
439 clone_info=CloneImageInfo(image_info);
440 if (clone_info->filename[0])
442 (void) sscanf(clone_info->filename,"%lx,%lx",¶m1,¶m2);
443 image_info_ptr=(ImageInfo **) param1;
444 image_ptr=(Image **) param2;
445 if ((image_info_ptr != (ImageInfo **) NULL) &&
446 (image_ptr != (Image **) NULL))
448 *image_ptr=CloneImage(image,0,0,MagickFalse,exception);
449 *image_info_ptr=clone_info;
453 else if (LocaleCompare(image_info->magick,"XTRNBLOB") == 0)
462 filename[MagickPathExtent];
464 clone_info=CloneImageInfo(image_info);
465 if (clone_info->filename[0])
467 (void) sscanf(clone_info->filename,"%lx,%lx,%s",
468 ¶m1,¶m2,&filename);
470 blob_data=(char **) param1;
471 blob_length=(size_t *) param2;
473 (void) CopyMagickString(clone_info->filename,filename,MagickPathExtent);
474 for (p=image; p != (Image *) NULL; p=GetNextImageInList(p))
476 (void) CopyMagickString(p->filename,filename,MagickPathExtent);
479 SetImageInfo(clone_info,1,exception);
480 (void) CopyMagickString(image->magick,clone_info->magick,
482 if (*blob_length == 0)
484 *blob_data=(char *) ImageToBlob(clone_info,image,blob_length,
486 if (*blob_data == NULL)
488 if (status == MagickFalse)
489 CatchImageException(image);
491 clone_info=DestroyImageInfo(clone_info);
493 else if (LocaleCompare(image_info->magick,"XTRNARRAY") == 0)
496 filename[MagickPathExtent];
504 clone_info=CloneImageInfo(image_info);
505 if (*clone_info->filename != '\0')
507 (void) sscanf(clone_info->filename,"%lx,%s",¶m1,&filename);
508 image->client_data=param1;
510 (void) CopyMagickString(clone_info->filename,filename,MagickPathExtent);
511 for (p=image; p != (Image *) NULL; p=GetNextImageInList(p))
513 (void) CopyMagickString(p->filename,filename,MagickPathExtent);
516 SetImageInfo(clone_info,1,exception);
517 (void) CopyMagickString(image->magick,clone_info->magick,
519 blob_data=ImageToBlob(clone_info,image,&blob_length,
521 if (blob_data == (unsigned char *) NULL)
524 SafeArrayFifo(image,blob_data,blob_length);
525 if (status == MagickFalse)
526 CatchImageException(image);
528 clone_info=DestroyImageInfo(clone_info);