2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % ImageMagickObject BLOB Interface. %
20 % Copyright 1999-2007 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,&(*image_ptr)->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,"XTRNSTREAM") == 0)
177 #ifdef IMPLEMENT_THIS
183 filename[MaxTextExtent];
186 (*fifo)(const Image *,const void *,const size_t);
188 (void) sscanf(clone_info->filename,"%lx,%lx,%s",¶m1,¶m2,&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);
196 else if (LocaleCompare(image_info->magick,"XTRNARRAY") == 0)
215 filename[MaxTextExtent];
218 (void) sscanf(clone_info->filename,"%lx,%s",¶m1,&filename);
220 pSafeArray = (SAFEARRAY *) param1;
223 hr = SafeArrayGetLBound(pSafeArray, 1, &lBoundl);
225 hr = SafeArrayGetUBound(pSafeArray, 1, &lBoundu);
228 blob_length = lBoundu - lBoundl + 1;
229 hr = SafeArrayAccessData(pSafeArray, (void**)&blob_data);
232 if (filename[0] != '\0')
234 (void) CopyMagickString(clone_info->filename,filename,
236 (void) CopyMagickString(clone_info->magick,filename,
241 *clone_info->magick = '\0';
242 clone_info->filename[0] = '\0';
244 image=BlobToImage(clone_info,blob_data,blob_length,exception);
245 hr = SafeArrayUnaccessData(pSafeArray);
246 CatchException(exception);
251 else if (LocaleCompare(image_info->magick,"XTRNBSTR") == 0)
266 filename[MaxTextExtent];
269 (void) sscanf(clone_info->filename,"%lx,%s",¶m1,&filename);
271 bstr = (BSTR) param1;
272 blob_length = SysStringLen(bstr) * 2;
273 blob_data = (char *)bstr;
274 if ((blob_data != (char *)NULL) && (blob_length>0))
276 if (filename[0] != '\0')
278 (void) CopyMagickString(clone_info->filename,filename,
280 (void) CopyMagickString(clone_info->magick,filename,
285 *clone_info->magick = '\0';
286 clone_info->filename[0] = '\0';
288 image=BlobToImage(clone_info,blob_data,blob_length,exception);
289 CatchException(exception);
292 clone_info=DestroyImageInfo(clone_info);
297 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
301 % R e g i s t e r X T R N I m a g e %
305 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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.
314 % The format of the RegisterXTRNImage method is:
316 % RegisterXTRNImage(void)
319 ModuleExport void RegisterXTRNImage(void)
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);
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);
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);
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);
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);
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);
380 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
384 % U n r e g i s t e r X T R N I m a g e %
388 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
390 % UnregisterXTRNImage() removes format registrations made by the
391 % XTRN module from the list of supported formats.
393 % The format of the UnregisterXTRNImage method is:
395 % UnregisterXTRNImage(void)
398 ModuleExport void UnregisterXTRNImage(void)
400 UnregisterMagickInfo("XTRNFILE");
401 UnregisterMagickInfo("XTRNIMAGE");
402 UnregisterMagickInfo("XTRNBLOB");
403 UnregisterMagickInfo("XTRNSTREAM");
404 UnregisterMagickInfo("XTRNARRAY");
405 UnregisterMagickInfo("XTRNBSTR");
409 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
413 % W r i t e X T R N I m a g e %
417 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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).
423 % The format of the WriteXTRNImage method is:
425 % MagickBooleanType WriteXTRNImage(const ImageInfo *image_info,
426 % Image *image,ExceptionInfo *exception)
428 % A description of each parameter follows.
430 % o image_info: Specifies a pointer to an ImageInfo structure.
432 % o image: A pointer to a Image structure.
434 % o exception: return any errors or warnings in this structure.
438 size_t SafeArrayFifo(const Image *image,const void *data,const size_t length)
440 SAFEARRAYBOUND NewArrayBounds[1]; /* 1 Dimension */
442 SAFEARRAY *pSafeArray = (SAFEARRAY *)image->client_data;
443 if (pSafeArray != NULL)
445 ssize_t lBoundl, lBoundu, lCount;
447 /* First see how big the buffer currently is */
448 hr = SafeArrayGetLBound(pSafeArray, 1, &lBoundl);
451 hr = SafeArrayGetUBound(pSafeArray, 1, &lBoundu);
454 lCount = lBoundu - lBoundl + 1;
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);
464 hr = SafeArrayAccessData(pSafeArray, (void**)&pReturnBuffer);
467 (void) CopyMagickMemory( pReturnBuffer+lCount, (unsigned char *)data, length );
468 hr = SafeArrayUnaccessData(pSafeArray);
474 /* Adjust the length of the buffer to fit */
480 static MagickBooleanType WriteXTRNImage(const ImageInfo *image_info,Image *image,ExceptionInfo *exception)
499 param1 = param2 = param3 = (void *) NULL;
500 if (LocaleCompare(image_info->magick,"XTRNFILE") == 0)
502 clone_info=CloneImageInfo(image_info);
503 status=WriteImage(image_info,image,exception);
504 if (status == MagickFalse)
505 CatchImageException(image);
506 clone_info=DestroyImageInfo(clone_info);
508 else if (LocaleCompare(image_info->magick,"XTRNIMAGE") == 0)
516 clone_info=CloneImageInfo(image_info);
517 if (clone_info->filename[0])
519 (void) sscanf(clone_info->filename,"%lx,%lx",¶m1,¶m2);
520 image_info_ptr=(ImageInfo **) param1;
521 image_ptr=(Image **) param2;
522 if ((image_info_ptr != (ImageInfo **) NULL) &&
523 (image_ptr != (Image **) NULL))
525 *image_ptr=CloneImage(image,0,0,MagickFalse,exception);
526 *image_info_ptr=clone_info;
530 else if (LocaleCompare(image_info->magick,"XTRNBLOB") == 0)
539 filename[MaxTextExtent];
541 clone_info=CloneImageInfo(image_info);
542 if (clone_info->filename[0])
544 (void) sscanf(clone_info->filename,"%lx,%lx,%s",
545 ¶m1,¶m2,&filename);
547 blob_data=(char **) param1;
548 blob_length=(size_t *) param2;
551 (void) CopyMagickString(clone_info->filename,filename,MaxTextExtent);
552 for (p=image; p != (Image *) NULL; p=GetNextImageInList(p))
554 (void) CopyMagickString(p->filename,filename,MaxTextExtent);
557 SetImageInfo(clone_info,1,exception);
558 (void) CopyMagickString(image->magick,clone_info->magick,
560 if (*blob_length == 0)
562 *blob_data=(char *) ImageToBlob(clone_info,image,blob_length,
564 if (*blob_data == NULL)
566 if (status == MagickFalse)
567 CatchImageException(image);
569 clone_info=DestroyImageInfo(clone_info);
571 else if (LocaleCompare(image_info->magick,"XTRNSTREAM") == 0)
574 (*fifo)(const Image *,const void *,const size_t);
577 filename[MaxTextExtent];
579 clone_info=CloneImageInfo(image_info);
580 if (clone_info->filename[0])
582 (void) sscanf(clone_info->filename,"%lx,%lx,%s",
583 ¶m1,¶m2,&filename);
585 fifo=(size_t (*)(const Image *,const void *,const size_t)) param1;
586 image->client_data=param2;
589 (void) CopyMagickString(clone_info->filename,filename,MaxTextExtent);
590 for (p=image; p != (Image *) NULL; p=GetNextImageInList(p))
592 (void) CopyMagickString(p->filename,filename,MaxTextExtent);
595 SetImageInfo(clone_info,1,exception);
596 (void) CopyMagickString(image->magick,clone_info->magick,
598 status=WriteStream(clone_info,image,fifo);
599 if (status == MagickFalse)
600 CatchImageException(image);
602 clone_info=DestroyImageInfo(clone_info);
604 else if (LocaleCompare(image_info->magick,"XTRNARRAY") == 0)
607 filename[MaxTextExtent];
609 clone_info=CloneImageInfo(image_info);
610 if (clone_info->filename[0])
612 (void) sscanf(clone_info->filename,"%lx,%s",
615 image->client_data=param1;
618 (void) CopyMagickString(clone_info->filename,filename,MaxTextExtent);
619 for (p=image; p != (Image *) NULL; p=GetNextImageInList(p))
621 (void) CopyMagickString(p->filename,filename,MaxTextExtent);
624 SetImageInfo(clone_info,1,exception);
625 (void) CopyMagickString(image->magick,clone_info->magick,
627 status=WriteStream(clone_info,image,SafeArrayFifo);
628 if (status == MagickFalse)
629 CatchImageException(image);
631 clone_info=DestroyImageInfo(clone_info);