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 #if defined(_VISUALC_)
60 #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"
73 #define WIN32_LEAN_AND_MEAN
81 static MagickBooleanType
82 WriteXTRNImage(const ImageInfo *,Image *,ExceptionInfo *exception);
85 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
89 % R e a d X T R N I m a g e %
93 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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.
99 % The format of the ReadXTRNImage method is:
101 % Image *ReadXTRNImage(const ImageInfo *image_info,
102 % ExceptionInfo *exception)
104 % A description of each parameter follows:
106 % o image_info: Specifies a pointer to an ImageInfo structure.
108 % o exception: return any errors or warnings in this structure.
111 static Image *ReadXTRNImage(const ImageInfo *image_info,
112 ExceptionInfo *exception)
125 param1 = param2 = param3 = (void *) NULL;
126 image = (Image *) NULL;
127 clone_info=CloneImageInfo(image_info);
128 if (clone_info->filename == NULL)
130 clone_info=DestroyImageInfo(clone_info);
131 ThrowReaderException(FileOpenWarning,"No filename specified");
133 if (LocaleCompare(image_info->magick,"XTRNFILE") == 0)
135 image=ReadImage(clone_info,exception);
136 CatchException(exception);
138 else if (LocaleCompare(image_info->magick,"XTRNIMAGE") == 0)
148 (void) sscanf(clone_info->filename,"%lx,%lx",¶m1,¶m2);
149 image_ptr=(Image **) param2;
150 if (*image_ptr != (Image *) NULL)
151 image=CloneImage(*image_ptr,0,0,MagickFalse,exception);
153 image_info_ptr=(ImageInfo **) param1;
154 if (*image_info_ptr != (ImageInfo *) NULL)
155 image_info=*image_info_ptr;
158 else if (LocaleCompare(image_info->magick,"XTRNBLOB") == 0)
167 filename[MaxTextExtent];
169 (void) sscanf(clone_info->filename,"%lx,%lx,%s",¶m1,¶m2,&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);
175 else if (LocaleCompare(image_info->magick,"XTRNARRAY") == 0)
179 filename[MaxTextExtent];
195 (void) sscanf(clone_info->filename,"%lx,%s",¶m1,&filename);
197 pSafeArray=(SAFEARRAY *) param1;
200 hr = SafeArrayGetLBound(pSafeArray, 1, &lBoundl);
202 hr = SafeArrayGetUBound(pSafeArray, 1, &lBoundu);
205 blob_length = lBoundu - lBoundl + 1;
206 hr = SafeArrayAccessData(pSafeArray,(void**) &blob_data);
209 *clone_info->filename='\0';
210 *clone_info->magick='\0';
211 if (*filename != '\0')
212 (void) CopyMagickString(clone_info->filename,filename,
214 image=BlobToImage(clone_info,blob_data,blob_length,exception);
215 hr=SafeArrayUnaccessData(pSafeArray);
216 CatchException(exception);
221 clone_info=DestroyImageInfo(clone_info);
226 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
230 % R e g i s t e r X T R N I m a g e %
234 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
236 % RegisterXTRNImage() adds attributes for the XTRN image format to
237 % the list of supported formats. The attributes include the image format
238 % tag, a method to read and/or write the format, whether the format
239 % supports the saving of more than one frame to the same file or blob,
240 % whether the format supports native in-memory I/O, and a brief
241 % description of the format.
243 % The format of the RegisterXTRNImage method is:
245 % size_t RegisterXTRNImage(void)
248 ModuleExport size_t RegisterXTRNImage(void)
253 entry=SetMagickInfo("XTRNFILE");
254 entry->decoder=ReadXTRNImage;
255 entry->encoder=WriteXTRNImage;
256 entry->adjoin=MagickFalse;
257 entry->stealth=MagickTrue;
258 entry->description=ConstantString("External transfer of a file");
259 entry->module=ConstantString("XTRN");
260 RegisterMagickInfo(entry);
261 entry=SetMagickInfo("XTRNIMAGE");
262 entry->decoder=ReadXTRNImage;
263 entry->encoder=WriteXTRNImage;
264 entry->adjoin=MagickFalse;
265 entry->stealth=MagickTrue;
266 entry->description=ConstantString("External transfer of a image in memory");
267 entry->module=ConstantString("XTRN");
268 RegisterMagickInfo(entry);
269 entry=SetMagickInfo("XTRNBLOB");
270 entry->decoder=ReadXTRNImage;
271 entry->encoder=WriteXTRNImage;
272 entry->adjoin=MagickFalse;
273 entry->stealth=MagickTrue;
274 entry->description=ConstantString("IExternal transfer of a blob in memory");
275 entry->module=ConstantString("XTRN");
276 RegisterMagickInfo(entry);
277 entry=SetMagickInfo("XTRNARRAY");
278 entry->decoder=ReadXTRNImage;
279 entry->encoder=WriteXTRNImage;
280 entry->adjoin=MagickFalse;
281 entry->stealth=MagickTrue;
282 entry->description=ConstantString(
283 "External transfer via a smart array interface");
284 entry->module=ConstantString("XTRN");
285 RegisterMagickInfo(entry);
286 return(MagickImageCoderSignature);
290 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
294 % U n r e g i s t e r X T R N I m a g e %
298 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
300 % UnregisterXTRNImage() removes format registrations made by the
301 % XTRN module from the list of supported formats.
303 % The format of the UnregisterXTRNImage method is:
305 % UnregisterXTRNImage(void)
308 ModuleExport void UnregisterXTRNImage(void)
310 UnregisterMagickInfo("XTRNFILE");
311 UnregisterMagickInfo("XTRNIMAGE");
312 UnregisterMagickInfo("XTRNBLOB");
313 UnregisterMagickInfo("XTRNARRAY");
317 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
321 % W r i t e X T R N I m a g e %
325 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
327 % WriteXTRNImage() writes an image in the XTRN encoded image format.
328 % We use GIF because it is the only format that is compressed without
329 % requiring additional optional delegates (TIFF, ZIP, etc).
331 % The format of the WriteXTRNImage method is:
333 % MagickBooleanType WriteXTRNImage(const ImageInfo *image_info,
334 % Image *image,ExceptionInfo *exception)
336 % A description of each parameter follows.
338 % o image_info: Specifies a pointer to an ImageInfo structure.
340 % o image: A pointer to a Image structure.
342 % o exception: return any errors or warnings in this structure.
346 size_t SafeArrayFifo(const Image *image,const void *data,const size_t length)
348 SAFEARRAYBOUND NewArrayBounds[1]; /* 1 Dimension */
350 SAFEARRAY *pSafeArray = (SAFEARRAY *)image->client_data;
351 if (pSafeArray != NULL)
353 long lBoundl, lBoundu, lCount;
355 /* First see how big the buffer currently is */
356 hr = SafeArrayGetLBound(pSafeArray, 1, &lBoundl);
359 hr = SafeArrayGetUBound(pSafeArray, 1, &lBoundu);
362 lCount = lBoundu - lBoundl + 1;
366 unsigned char *pReturnBuffer = NULL;
367 NewArrayBounds[0].lLbound = 0; /* Start-Index 0 */
368 NewArrayBounds[0].cElements = (unsigned long) (length+lCount); /* # Elemente */
369 hr = SafeArrayRedim(pSafeArray, NewArrayBounds);
372 hr = SafeArrayAccessData(pSafeArray, (void**)&pReturnBuffer);
375 (void) memcpy(pReturnBuffer+lCount,(unsigned char *) data,length);
376 hr=SafeArrayUnaccessData(pSafeArray);
382 /* Adjust the length of the buffer to fit */
388 static MagickBooleanType WriteXTRNImage(const ImageInfo *image_info,
389 Image *image,ExceptionInfo *exception)
408 param1 = param2 = param3 = (void *) NULL;
409 if (LocaleCompare(image_info->magick,"XTRNFILE") == 0)
411 clone_info=CloneImageInfo(image_info);
412 status=WriteImage(image_info,image,exception);
413 if (status == MagickFalse)
414 CatchImageException(image);
415 clone_info=DestroyImageInfo(clone_info);
417 else if (LocaleCompare(image_info->magick,"XTRNIMAGE") == 0)
425 clone_info=CloneImageInfo(image_info);
426 if (clone_info->filename[0])
428 (void) sscanf(clone_info->filename,"%lx,%lx",¶m1,¶m2);
429 image_info_ptr=(ImageInfo **) param1;
430 image_ptr=(Image **) param2;
431 if ((image_info_ptr != (ImageInfo **) NULL) &&
432 (image_ptr != (Image **) NULL))
434 *image_ptr=CloneImage(image,0,0,MagickFalse,exception);
435 *image_info_ptr=clone_info;
439 else if (LocaleCompare(image_info->magick,"XTRNBLOB") == 0)
448 filename[MaxTextExtent];
450 clone_info=CloneImageInfo(image_info);
451 if (clone_info->filename[0])
453 (void) sscanf(clone_info->filename,"%lx,%lx,%s",
454 ¶m1,¶m2,&filename);
456 blob_data=(char **) param1;
457 blob_length=(size_t *) param2;
459 (void) CopyMagickString(clone_info->filename,filename,MaxTextExtent);
460 for (p=image; p != (Image *) NULL; p=GetNextImageInList(p))
462 (void) CopyMagickString(p->filename,filename,MaxTextExtent);
465 SetImageInfo(clone_info,1,exception);
466 (void) CopyMagickString(image->magick,clone_info->magick,
468 if (*blob_length == 0)
470 *blob_data=(char *) ImageToBlob(clone_info,image,blob_length,
472 if (*blob_data == NULL)
474 if (status == MagickFalse)
475 CatchImageException(image);
477 clone_info=DestroyImageInfo(clone_info);
479 else if (LocaleCompare(image_info->magick,"XTRNARRAY") == 0)
482 filename[MaxTextExtent];
490 clone_info=CloneImageInfo(image_info);
491 if (*clone_info->filename != '\0')
493 (void) sscanf(clone_info->filename,"%lx,%s",¶m1,&filename);
494 image->client_data=param1;
496 (void) CopyMagickString(clone_info->filename,filename,MaxTextExtent);
497 for (p=image; p != (Image *) NULL; p=GetNextImageInList(p))
499 (void) CopyMagickString(p->filename,filename,MaxTextExtent);
502 SetImageInfo(clone_info,1,exception);
503 (void) CopyMagickString(image->magick,clone_info->magick,
505 blob_data=ImageToBlob(clone_info,image,&blob_length,
507 if (blob_data == (unsigned char *) NULL)
510 SafeArrayFifo(image,blob_data,blob_length);
511 if (status == MagickFalse)
512 CatchImageException(image);
514 clone_info=DestroyImageInfo(clone_info);