2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
10 % BBBB LLLLL OOO BBBB %
13 % MagickCore Binary Large OBjectS Methods %
20 % Copyright 1999-2010 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. %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
43 #include "magick/studio.h"
44 #include "magick/blob.h"
45 #include "magick/blob-private.h"
46 #include "magick/cache.h"
47 #include "magick/client.h"
48 #include "magick/constitute.h"
49 #include "magick/delegate.h"
50 #include "magick/exception.h"
51 #include "magick/exception-private.h"
52 #include "magick/image-private.h"
53 #include "magick/list.h"
54 #include "magick/log.h"
55 #include "magick/magick.h"
56 #include "magick/memory_.h"
57 #include "magick/policy.h"
58 #include "magick/resource_.h"
59 #include "magick/semaphore.h"
60 #include "magick/string_.h"
61 #include "magick/string-private.h"
62 #include "magick/utility.h"
63 #if defined(MAGICKCORE_HAVE_MMAP_FILEIO) && !defined(__WINDOWS__)
64 # include <sys/mman.h>
66 #if defined(MAGICKCORE_ZLIB_DELEGATE)
69 #if defined(MAGICKCORE_BZLIB_DELEGATE)
76 #define MagickMaxBlobExtent 65541
77 #if defined(MAGICKCORE_HAVE_FSEEKO)
81 #if !defined(MAP_ANONYMOUS) && defined(MAP_ANON)
82 # define MAP_ANONYMOUS MAP_ANON
84 #if !defined(MAP_FAILED)
85 #define MAP_FAILED ((void *) -1)
92 #define _O_BINARY O_BINARY
150 Forward declarations.
156 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
160 + A t t a c h B l o b %
164 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
166 % AttachBlob() attaches a blob to the BlobInfo structure.
168 % The format of the AttachBlob method is:
170 % void AttachBlob(BlobInfo *blob_info,const void *blob,const size_t length)
172 % A description of each parameter follows:
174 % o blob_info: Specifies a pointer to a BlobInfo structure.
176 % o blob: the address of a character stream in one of the image formats
177 % understood by ImageMagick.
179 % o length: This size_t integer reflects the length in bytes of the blob.
182 MagickExport void AttachBlob(BlobInfo *blob_info,const void *blob,
185 assert(blob_info != (BlobInfo *) NULL);
186 if (blob_info->debug != MagickFalse)
187 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
188 blob_info->length=length;
189 blob_info->extent=length;
190 blob_info->quantum=(size_t) MagickMaxBlobExtent;
192 blob_info->type=BlobStream;
193 blob_info->file=(FILE *) NULL;
194 blob_info->data=(unsigned char *) blob;
195 blob_info->mapped=MagickFalse;
199 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
203 + B l o b T o F i l e %
207 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
209 % BlobToFile() writes a blob to a file. It returns MagickFalse if an error
210 % occurs otherwise MagickTrue.
212 % The format of the BlobToFile method is:
214 % MagickBooleanType BlobToFile(char *filename,const void *blob,
215 % const size_t length,ExceptionInfo *exception)
217 % A description of each parameter follows:
219 % o filename: Write the blob to this file.
221 % o blob: the address of a blob.
223 % o length: This length in bytes of the blob.
225 % o exception: return any errors or warnings in this structure.
229 static inline size_t MagickMin(const size_t x,const size_t y)
236 MagickExport MagickBooleanType BlobToFile(char *filename,const void *blob,
237 const size_t length,ExceptionInfo *exception)
248 assert(filename != (const char *) NULL);
249 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",filename);
250 assert(blob != (const void *) NULL);
251 if (*filename == '\0')
252 file=AcquireUniqueFileResource(filename);
254 file=open(filename,O_RDWR | O_CREAT | O_EXCL | O_BINARY,S_MODE);
257 ThrowFileException(exception,BlobError,"UnableToWriteBlob",filename);
260 for (i=0; i < length; i+=count)
262 count=(ssize_t) write(file,(const char *) blob+i,MagickMin(length-i,(size_t)
274 ThrowFileException(exception,BlobError,"UnableToWriteBlob",filename);
281 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
285 % B l o b T o I m a g e %
289 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
291 % BlobToImage() implements direct to memory image formats. It returns the
294 % The format of the BlobToImage method is:
296 % Image *BlobToImage(const ImageInfo *image_info,const void *blob,
297 % const size_t length,ExceptionInfo *exception)
299 % A description of each parameter follows:
301 % o image_info: the image info.
303 % o blob: the address of a character stream in one of the image formats
304 % understood by ImageMagick.
306 % o length: This size_t integer reflects the length in bytes of the blob.
308 % o exception: return any errors or warnings in this structure.
311 MagickExport Image *BlobToImage(const ImageInfo *image_info,const void *blob,
312 const size_t length,ExceptionInfo *exception)
327 assert(image_info != (ImageInfo *) NULL);
328 assert(image_info->signature == MagickSignature);
329 if (image_info->debug != MagickFalse)
330 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
331 image_info->filename);
332 assert(exception != (ExceptionInfo *) NULL);
333 if ((blob == (const void *) NULL) || (length == 0))
335 (void) ThrowMagickException(exception,GetMagickModule(),BlobError,
336 "ZeroLengthBlobNotPermitted","`%s'",image_info->filename);
337 return((Image *) NULL);
339 blob_info=CloneImageInfo(image_info);
340 blob_info->blob=(void *) blob;
341 blob_info->length=length;
342 if (*blob_info->magick == '\0')
343 (void) SetImageInfo(blob_info,MagickFalse,exception);
344 magick_info=GetMagickInfo(blob_info->magick,exception);
345 if (magick_info == (const MagickInfo *) NULL)
347 blob_info=DestroyImageInfo(blob_info);
348 (void) ThrowMagickException(exception,GetMagickModule(),
349 MissingDelegateError,"NoDecodeDelegateForThisImageFormat","`%s'",
350 image_info->filename);
351 return((Image *) NULL);
353 if (GetMagickBlobSupport(magick_info) != MagickFalse)
356 Native blob support for this image format.
358 (void) CopyMagickString(blob_info->filename,image_info->filename,
360 (void) CopyMagickString(blob_info->magick,image_info->magick,
362 image=ReadImage(blob_info,exception);
363 if (image != (Image *) NULL)
364 (void) DetachBlob(image->blob);
365 blob_info=DestroyImageInfo(blob_info);
369 Write blob to a temporary file on disk.
371 blob_info->blob=(void *) NULL;
373 *blob_info->filename='\0';
374 status=BlobToFile(blob_info->filename,blob,length,exception);
375 if (status == MagickFalse)
377 (void) RelinquishUniqueFileResource(blob_info->filename);
378 blob_info=DestroyImageInfo(blob_info);
379 return((Image *) NULL);
381 clone_info=CloneImageInfo(blob_info);
382 (void) FormatMagickString(clone_info->filename,MaxTextExtent,"%s:%s",
383 blob_info->magick,blob_info->filename);
384 image=ReadImage(clone_info,exception);
385 clone_info=DestroyImageInfo(clone_info);
386 (void) RelinquishUniqueFileResource(blob_info->filename);
387 blob_info=DestroyImageInfo(blob_info);
392 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
396 + C l o n e B l o b I n f o %
400 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
402 % CloneBlobInfo() makes a duplicate of the given blob info structure, or if
403 % blob info is NULL, a new one.
405 % The format of the CloneBlobInfo method is:
407 % BlobInfo *CloneBlobInfo(const BlobInfo *blob_info)
409 % A description of each parameter follows:
411 % o blob_info: the blob info.
414 MagickExport BlobInfo *CloneBlobInfo(const BlobInfo *blob_info)
419 clone_info=(BlobInfo *) AcquireAlignedMemory(1,sizeof(*clone_info));
420 if (clone_info == (BlobInfo *) NULL)
421 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
422 GetBlobInfo(clone_info);
423 if (blob_info == (BlobInfo *) NULL)
425 clone_info->length=blob_info->length;
426 clone_info->extent=blob_info->extent;
427 clone_info->synchronize=blob_info->synchronize;
428 clone_info->quantum=blob_info->quantum;
429 clone_info->mapped=blob_info->mapped;
430 clone_info->eof=blob_info->eof;
431 clone_info->offset=blob_info->offset;
432 clone_info->size=blob_info->size;
433 clone_info->exempt=blob_info->exempt;
434 clone_info->status=blob_info->status;
435 clone_info->temporary=blob_info->temporary;
436 clone_info->type=blob_info->type;
437 clone_info->file=blob_info->file;
438 clone_info->properties=blob_info->properties;
439 clone_info->stream=blob_info->stream;
440 clone_info->data=blob_info->data;
441 clone_info->debug=IsEventLogging();
442 clone_info->reference_count=1;
447 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
451 + C l o s e B l o b %
455 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
457 % CloseBlob() closes a stream associated with the image.
459 % The format of the CloseBlob method is:
461 % MagickBooleanType CloseBlob(Image *image)
463 % A description of each parameter follows:
465 % o image: the image.
468 MagickExport MagickBooleanType CloseBlob(Image *image)
476 assert(image != (Image *) NULL);
477 assert(image->signature == MagickSignature);
478 if (image->debug != MagickFalse)
479 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
480 assert(image->blob != (BlobInfo *) NULL);
481 if (image->blob->type == UndefinedStream)
483 if (image->blob->synchronize != MagickFalse)
485 image->blob->size=GetBlobSize(image);
486 image->blob->eof=MagickFalse;
487 if (image->blob->exempt != MagickFalse)
489 image->blob->type=UndefinedStream;
493 switch (image->blob->type)
495 case UndefinedStream:
501 status=ferror(image->blob->file);
506 #if defined(MAGICKCORE_ZLIB_DELEGATE)
507 (void) gzerror(image->blob->file,&status);
513 #if defined(MAGICKCORE_BZLIB_DELEGATE)
514 (void) BZ2_bzerror((BZFILE *) image->blob->file,&status);
522 image->blob->status=status < 0 ? MagickTrue : MagickFalse;
523 switch (image->blob->type)
525 case UndefinedStream:
530 if (image->blob->synchronize != MagickFalse)
531 status=fsync(fileno(image->blob->file));
532 status=fclose(image->blob->file);
537 #if defined(MAGICKCORE_HAVE_PCLOSE)
538 status=pclose(image->blob->file);
544 #if defined(MAGICKCORE_ZLIB_DELEGATE)
545 status=gzclose(image->blob->file);
551 #if defined(MAGICKCORE_BZLIB_DELEGATE)
552 BZ2_bzclose((BZFILE *) image->blob->file);
560 (void) DetachBlob(image->blob);
561 image->blob->status=status < 0 ? MagickTrue : MagickFalse;
562 return(image->blob->status);
566 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
570 + D e s t r o y B l o b %
574 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
576 % DestroyBlob() deallocates memory associated with a blob.
578 % The format of the DestroyBlob method is:
580 % void DestroyBlob(Image *image)
582 % A description of each parameter follows:
584 % o image: the image.
587 MagickExport void DestroyBlob(Image *image)
592 assert(image != (Image *) NULL);
593 assert(image->signature == MagickSignature);
594 if (image->debug != MagickFalse)
595 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
596 assert(image->blob != (BlobInfo *) NULL);
597 assert(image->blob->signature == MagickSignature);
599 LockSemaphoreInfo(image->blob->semaphore);
600 image->blob->reference_count--;
601 assert(image->blob->reference_count >= 0);
602 if (image->blob->reference_count == 0)
604 UnlockSemaphoreInfo(image->blob->semaphore);
605 if (destroy == MagickFalse)
607 (void) CloseBlob(image);
608 if (image->blob->mapped != MagickFalse)
609 (void) UnmapBlob(image->blob->data,image->blob->length);
610 if (image->blob->semaphore != (SemaphoreInfo *) NULL)
611 DestroySemaphoreInfo(&image->blob->semaphore);
612 image->blob->signature=(~MagickSignature);
613 image->blob=(BlobInfo *) RelinquishMagickMemory(image->blob);
617 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
621 + D e t a c h B l o b %
625 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
627 % DetachBlob() detaches a blob from the BlobInfo structure.
629 % The format of the DetachBlob method is:
631 % unsigned char *DetachBlob(BlobInfo *blob_info)
633 % A description of each parameter follows:
635 % o blob_info: Specifies a pointer to a BlobInfo structure.
638 MagickExport unsigned char *DetachBlob(BlobInfo *blob_info)
643 assert(blob_info != (BlobInfo *) NULL);
644 if (blob_info->debug != MagickFalse)
645 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
646 if (blob_info->mapped != MagickFalse)
647 (void) UnmapBlob(blob_info->data,blob_info->length);
648 blob_info->mapped=MagickFalse;
651 blob_info->eof=MagickFalse;
652 blob_info->exempt=MagickFalse;
653 blob_info->type=UndefinedStream;
654 blob_info->file=(FILE *) NULL;
655 data=blob_info->data;
656 blob_info->data=(unsigned char *) NULL;
657 blob_info->stream=(StreamHandler) NULL;
662 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
666 + D u p l i c a t e s B l o b %
670 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
672 % DuplicateBlob() duplicates a blob descriptor.
674 % The format of the DuplicateBlob method is:
676 % void DuplicateBlob(Image *image,const Image *duplicate)
678 % A description of each parameter follows:
680 % o image: the image.
682 % o duplicate: the duplicate image.
685 MagickExport void DuplicateBlob(Image *image,const Image *duplicate)
687 assert(image != (Image *) NULL);
688 assert(image->signature == MagickSignature);
689 if (image->debug != MagickFalse)
690 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
691 assert(duplicate != (Image *) NULL);
692 assert(duplicate->signature == MagickSignature);
694 image->blob=ReferenceBlob(duplicate->blob);
698 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
706 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
708 % EOFBlob() returns a non-zero value when EOF has been detected reading from
711 % The format of the EOFBlob method is:
713 % int EOFBlob(const Image *image)
715 % A description of each parameter follows:
717 % o image: the image.
720 MagickExport int EOFBlob(const Image *image)
722 assert(image != (Image *) NULL);
723 assert(image->signature == MagickSignature);
724 if (image->debug != MagickFalse)
725 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
726 assert(image->blob != (BlobInfo *) NULL);
727 assert(image->blob->type != UndefinedStream);
728 switch (image->blob->type)
730 case UndefinedStream:
736 image->blob->eof=feof(image->blob->file) != 0 ? MagickTrue : MagickFalse;
741 image->blob->eof=MagickFalse;
746 #if defined(MAGICKCORE_BZLIB_DELEGATE)
751 (void) BZ2_bzerror((BZFILE *) image->blob->file,&status);
752 image->blob->eof=status == BZ_UNEXPECTED_EOF ? MagickTrue : MagickFalse;
758 image->blob->eof=MagickFalse;
764 return((int) image->blob->eof);
768 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
772 + F i l e T o B l o b %
776 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
778 % FileToBlob() returns the contents of a file as a blob. It returns the
779 % file as a blob and its length. If an error occurs, NULL is returned.
781 % The format of the FileToBlob method is:
783 % unsigned char *FileToBlob(const char *filename,const size_t extent,
784 % size_t *length,ExceptionInfo *exception)
786 % A description of each parameter follows:
788 % o blob: FileToBlob() returns the contents of a file as a blob. If
789 % an error occurs NULL is returned.
791 % o filename: the filename.
793 % o extent: The maximum length of the blob.
795 % o length: On return, this reflects the actual length of the blob.
797 % o exception: return any errors or warnings in this structure.
800 MagickExport unsigned char *FileToBlob(const char *filename,const size_t extent,
801 size_t *length,ExceptionInfo *exception)
821 assert(filename != (const char *) NULL);
822 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",filename);
823 assert(exception != (ExceptionInfo *) NULL);
826 if (LocaleCompare(filename,"-") != 0)
827 file=open(filename,O_RDONLY | O_BINARY);
830 ThrowFileException(exception,BlobError,"UnableToOpenFile",filename);
831 return((unsigned char *) NULL);
833 offset=(MagickOffsetType) MagickSeek(file,0,SEEK_END);
835 if ((offset < 0) || (offset != (MagickOffsetType) ((ssize_t) offset)))
844 Stream is not seekable.
846 quantum=(size_t) MagickMaxBufferExtent;
847 if ((fstat(file,&file_info) == 0) && (file_info.st_size != 0))
848 quantum=MagickMin((size_t) file_info.st_size,MagickMaxBufferExtent);
849 blob=(unsigned char *) AcquireQuantumMemory(quantum,sizeof(*blob));
850 for (i=0; blob != (unsigned char *) NULL; i+=count)
852 count=(ssize_t) read(file,blob+i,quantum);
859 if (~(1UL*i) < (quantum+1))
861 blob=(unsigned char *) RelinquishMagickMemory(blob);
864 blob=(unsigned char *) ResizeQuantumMemory(blob,i+quantum+1,
866 if ((size_t) (i+count) >= extent)
870 if (blob == (unsigned char *) NULL)
872 (void) ThrowMagickException(exception,GetMagickModule(),
873 ResourceLimitError,"MemoryAllocationFailed","`%s'",filename);
874 return((unsigned char *) NULL);
876 *length=MagickMin(i+count,extent);
880 *length=MagickMin((size_t) offset,extent);
881 blob=(unsigned char *) NULL;
882 if (~(*length) >= MaxTextExtent)
883 blob=(unsigned char *) AcquireQuantumMemory(*length+MaxTextExtent,
885 if (blob == (unsigned char *) NULL)
888 (void) ThrowMagickException(exception,GetMagickModule(),
889 ResourceLimitError,"MemoryAllocationFailed","`%s'",filename);
890 return((unsigned char *) NULL);
892 map=MapBlob(file,ReadMode,0,*length);
893 if (map != (unsigned char *) NULL)
895 (void) CopyMagickMemory(blob,map,*length);
896 (void) UnmapBlob(map,*length);
900 (void) MagickSeek(file,0,SEEK_SET);
901 for (i=0; i < *length; i+=count)
903 count=(ssize_t) read(file,blob+i,MagickMin(*length-i,(size_t)
915 blob=(unsigned char *) RelinquishMagickMemory(blob);
916 ThrowFileException(exception,BlobError,"UnableToReadBlob",filename);
917 return((unsigned char *) NULL);
926 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
930 % F i l e T o I m a g e %
934 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
936 % FileToImage() write the contents of a file to an image.
938 % The format of the FileToImage method is:
940 % MagickBooleanType FileToImage(Image *,const char *filename)
942 % A description of each parameter follows:
944 % o image: the image.
946 % o filename: the filename.
950 static inline ssize_t WriteBlobStream(Image *image,const size_t length,
951 const unsigned char *data)
956 register unsigned char
959 assert(image->blob != (BlobInfo *) NULL);
960 if (image->blob->type != BlobStream)
961 return(WriteBlob(image,length,data));
962 assert(image->blob->type != UndefinedStream);
963 assert(data != (void *) NULL);
964 extent=(MagickSizeType) (image->blob->offset+(MagickOffsetType) length);
965 if (extent >= image->blob->extent)
967 image->blob->quantum<<=1;
968 extent=image->blob->extent+image->blob->quantum+length;
969 if (SetBlobExtent(image,extent) == MagickFalse)
972 q=image->blob->data+image->blob->offset;
973 (void) CopyMagickMemory(q,data,length);
974 image->blob->offset+=length;
975 if (image->blob->offset >= (MagickOffsetType) image->blob->length)
976 image->blob->length=(size_t) image->blob->offset;
977 return((ssize_t) length);
980 MagickExport MagickBooleanType FileToImage(Image *image,const char *filename)
998 assert(image != (const Image *) NULL);
999 assert(image->signature == MagickSignature);
1000 assert(filename != (const char *) NULL);
1001 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",filename);
1002 file=open(filename,O_RDONLY | O_BINARY);
1005 ThrowFileException(&image->exception,BlobError,"UnableToOpenBlob",
1007 return(MagickFalse);
1009 quantum=(size_t) MagickMaxBufferExtent;
1010 if ((fstat(file,&file_info) == 0) && (file_info.st_size != 0))
1011 quantum=MagickMin((size_t) file_info.st_size,MagickMaxBufferExtent);
1012 blob=(unsigned char *) AcquireQuantumMemory(quantum,sizeof(*blob));
1013 if (blob == (unsigned char *) NULL)
1015 ThrowFileException(&image->exception,ResourceLimitError,
1016 "MemoryAllocationFailed",filename);
1017 return(MagickFalse);
1021 count=(ssize_t) read(file,blob,quantum);
1028 length=(size_t) count;
1029 count=WriteBlobStream(image,length,blob);
1030 if (count != (ssize_t) length)
1032 ThrowFileException(&image->exception,BlobError,"UnableToWriteBlob",
1038 blob=(unsigned char *) RelinquishMagickMemory(blob);
1043 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1047 + G e t B l o b E r r o r %
1051 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1053 % GetBlobError() returns MagickTrue if the blob associated with the specified
1054 % image encountered an error.
1056 % The format of the GetBlobError method is:
1058 % MagickBooleanType GetBlobError(const Image *image)
1060 % A description of each parameter follows:
1062 % o image: the image.
1065 MagickExport MagickBooleanType GetBlobError(const Image *image)
1067 assert(image != (const Image *) NULL);
1068 assert(image->signature == MagickSignature);
1069 if (image->debug != MagickFalse)
1070 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1071 return(image->blob->status);
1075 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1079 + G e t B l o b F i l e H a n d l e %
1083 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1085 % GetBlobFileHandle() returns the file handle associated with the image blob.
1087 % The format of the GetBlobFile method is:
1089 % FILE *GetBlobFileHandle(const Image *image)
1091 % A description of each parameter follows:
1093 % o image: the image.
1096 MagickExport FILE *GetBlobFileHandle(const Image *image)
1098 assert(image != (const Image *) NULL);
1099 assert(image->signature == MagickSignature);
1100 return(image->blob->file);
1104 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1108 + G e t B l o b I n f o %
1112 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1114 % GetBlobInfo() initializes the BlobInfo structure.
1116 % The format of the GetBlobInfo method is:
1118 % void GetBlobInfo(BlobInfo *blob_info)
1120 % A description of each parameter follows:
1122 % o blob_info: Specifies a pointer to a BlobInfo structure.
1125 MagickExport void GetBlobInfo(BlobInfo *blob_info)
1127 assert(blob_info != (BlobInfo *) NULL);
1128 (void) ResetMagickMemory(blob_info,0,sizeof(*blob_info));
1129 blob_info->type=UndefinedStream;
1130 blob_info->quantum=(size_t) MagickMaxBlobExtent;
1131 blob_info->properties.st_mtime=time((time_t *) NULL);
1132 blob_info->properties.st_ctime=time((time_t *) NULL);
1133 blob_info->debug=IsEventLogging();
1134 blob_info->reference_count=1;
1135 blob_info->semaphore=AllocateSemaphoreInfo();
1136 blob_info->signature=MagickSignature;
1140 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1144 % G e t B l o b P r o p e r t i e s %
1148 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1150 % GetBlobProperties() returns information about an image blob.
1152 % The format of the GetBlobProperties method is:
1154 % const struct stat *GetBlobProperties(const Image *image)
1156 % A description of each parameter follows:
1158 % o image: the image.
1161 MagickExport const struct stat *GetBlobProperties(const Image *image)
1163 assert(image != (Image *) NULL);
1164 assert(image->signature == MagickSignature);
1165 if (image->debug != MagickFalse)
1166 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1167 return(&image->blob->properties);
1171 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1175 + G e t B l o b S i z e %
1179 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1181 % GetBlobSize() returns the current length of the image file or blob; zero is
1182 % returned if the size cannot be determined.
1184 % The format of the GetBlobSize method is:
1186 % MagickSizeType GetBlobSize(const Image *image)
1188 % A description of each parameter follows:
1190 % o image: the image.
1193 MagickExport MagickSizeType GetBlobSize(const Image *image)
1198 assert(image != (Image *) NULL);
1199 assert(image->signature == MagickSignature);
1200 if (image->debug != MagickFalse)
1201 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1202 assert(image->blob != (BlobInfo *) NULL);
1204 switch (image->blob->type)
1206 case UndefinedStream:
1208 length=image->blob->size;
1213 if (fstat(fileno(image->blob->file),&image->blob->properties) == 0)
1214 length=(MagickSizeType) image->blob->properties.st_size;
1217 case StandardStream:
1220 length=image->blob->size;
1229 status=GetPathAttributes(image->filename,&image->blob->properties);
1230 if (status != MagickFalse)
1231 length=(MagickSizeType) image->blob->properties.st_size;
1238 length=(MagickSizeType) image->blob->length;
1246 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1250 + G e t B l o b S t r e a m D a t a %
1254 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1256 % GetBlobStreamData() returns the stream data for the image.
1258 % The format of the GetBlobStreamData method is:
1260 % unsigned char *GetBlobStreamData(const Image *image)
1262 % A description of each parameter follows:
1264 % o image: the image.
1267 MagickExport unsigned char *GetBlobStreamData(const Image *image)
1269 assert(image != (const Image *) NULL);
1270 assert(image->signature == MagickSignature);
1271 return(image->blob->data);
1275 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1279 + G e t B l o b S t r e a m H a n d l e r %
1283 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1285 % GetBlobStreamHandler() returns the stream handler for the image.
1287 % The format of the GetBlobStreamHandler method is:
1289 % StreamHandler GetBlobStreamHandler(const Image *image)
1291 % A description of each parameter follows:
1293 % o image: the image.
1296 MagickExport StreamHandler GetBlobStreamHandler(const Image *image)
1298 assert(image != (const Image *) NULL);
1299 assert(image->signature == MagickSignature);
1300 if (image->debug != MagickFalse)
1301 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1302 return(image->blob->stream);
1306 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1310 % I m a g e T o B l o b %
1314 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1316 % ImageToBlob() implements direct to memory image formats. It returns the
1317 % image as a blob and its length. The magick member of the ImageInfo structure
1318 % determines the format of the returned blob (GIF, JPEG, PNG, etc.)
1320 % The format of the ImageToBlob method is:
1322 % unsigned char *ImageToBlob(const ImageInfo *image_info,Image *image,
1323 % size_t *length,ExceptionInfo *exception)
1325 % A description of each parameter follows:
1327 % o image_info: the image info.
1329 % o image: the image.
1331 % o length: This pointer to a size_t integer sets the initial length of the
1332 % blob. On return, it reflects the actual length of the blob.
1334 % o exception: return any errors or warnings in this structure.
1337 MagickExport unsigned char *ImageToBlob(const ImageInfo *image_info,
1338 Image *image,size_t *length,ExceptionInfo *exception)
1352 assert(image_info != (const ImageInfo *) NULL);
1353 assert(image_info->signature == MagickSignature);
1354 if (image_info->debug != MagickFalse)
1355 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
1356 image_info->filename);
1357 assert(image != (Image *) NULL);
1358 assert(image->signature == MagickSignature);
1359 assert(exception != (ExceptionInfo *) NULL);
1361 blob=(unsigned char *) NULL;
1362 blob_info=CloneImageInfo(image_info);
1363 blob_info->adjoin=MagickFalse;
1364 (void) SetImageInfo(blob_info,MagickTrue,exception);
1365 if (*blob_info->magick != '\0')
1366 (void) CopyMagickString(image->magick,blob_info->magick,MaxTextExtent);
1367 magick_info=GetMagickInfo(image->magick,exception);
1368 if (magick_info == (const MagickInfo *) NULL)
1370 (void) ThrowMagickException(exception,GetMagickModule(),
1371 MissingDelegateError,"NoDecodeDelegateForThisImageFormat","`%s'",
1375 (void) CopyMagickString(blob_info->magick,image->magick,MaxTextExtent);
1376 if (GetMagickBlobSupport(magick_info) != MagickFalse)
1379 Native blob support for this image format.
1381 blob_info->length=0;
1382 blob_info->blob=(void *) AcquireQuantumMemory(MagickMaxBlobExtent,
1383 sizeof(unsigned char));
1384 if (blob_info->blob == (void *) NULL)
1385 (void) ThrowMagickException(exception,GetMagickModule(),
1386 ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
1389 (void) CloseBlob(image);
1390 image->blob->exempt=MagickTrue;
1391 *image->filename='\0';
1392 status=WriteImage(blob_info,image);
1393 if ((status == MagickFalse) || (image->blob->length == 0))
1394 InheritException(exception,&image->exception);
1397 *length=image->blob->length;
1398 blob=DetachBlob(image->blob);
1399 blob=(unsigned char *) ResizeQuantumMemory(blob,*length,
1407 unique[MaxTextExtent];
1413 Write file to disk in blob image format.
1415 file=AcquireUniqueFileResource(unique);
1418 ThrowFileException(exception,BlobError,"UnableToWriteBlob",
1419 image_info->filename);
1423 blob_info->file=fdopen(file,"wb");
1424 if (blob_info->file != (FILE *) NULL)
1426 (void) FormatMagickString(image->filename,MaxTextExtent,"%s:%s",
1427 image->magick,unique);
1428 status=WriteImage(blob_info,image);
1429 (void) fclose(blob_info->file);
1430 if (status == MagickFalse)
1431 InheritException(exception,&image->exception);
1433 blob=FileToBlob(image->filename,~0UL,length,exception);
1435 (void) RelinquishUniqueFileResource(unique);
1438 blob_info=DestroyImageInfo(blob_info);
1443 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1447 % I m a g e T o F i l e %
1451 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1453 % ImageToFile() writes an image to a file. It returns MagickFalse if an error
1454 % occurs otherwise MagickTrue.
1456 % The format of the ImageToFile method is:
1458 % MagickBooleanType ImageToFile(Image *image,char *filename,
1459 % ExceptionInfo *exception)
1461 % A description of each parameter follows:
1463 % o image: the image.
1465 % o filename: Write the image to this file.
1467 % o exception: return any errors or warnings in this structure.
1471 static inline const unsigned char *ReadBlobStream(Image *image,
1472 const size_t length,unsigned char *data,ssize_t *count)
1474 assert(count != (ssize_t *) NULL);
1475 assert(image->blob != (BlobInfo *) NULL);
1476 if (image->blob->type != BlobStream)
1478 *count=ReadBlob(image,length,data);
1481 if (image->blob->offset >= (MagickOffsetType) image->blob->length)
1484 image->blob->eof=MagickTrue;
1487 data=image->blob->data+image->blob->offset;
1488 *count=(ssize_t) MagickMin(length,(size_t) (image->blob->length-
1489 image->blob->offset));
1490 image->blob->offset+=(*count);
1491 if (*count != (ssize_t) length)
1492 image->blob->eof=MagickTrue;
1496 MagickExport MagickBooleanType ImageToFile(Image *image,char *filename,
1497 ExceptionInfo *exception)
1502 register const unsigned char
1521 assert(image != (Image *) NULL);
1522 assert(image->signature == MagickSignature);
1523 assert(image->blob != (BlobInfo *) NULL);
1524 assert(image->blob->type != UndefinedStream);
1525 if (image->debug != MagickFalse)
1526 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",filename);
1527 assert(filename != (const char *) NULL);
1528 if (*filename == '\0')
1529 file=AcquireUniqueFileResource(filename);
1531 if (LocaleCompare(filename,"-") == 0)
1532 file=fileno(stdout);
1534 file=open(filename,O_RDWR | O_CREAT | O_EXCL | O_BINARY,S_MODE);
1537 ThrowFileException(exception,BlobError,"UnableToWriteBlob",filename);
1538 return(MagickFalse);
1540 quantum=(size_t) MagickMaxBufferExtent;
1541 if ((fstat(file,&file_info) == 0) && (file_info.st_size != 0))
1542 quantum=MagickMin((size_t) file_info.st_size,MagickMaxBufferExtent);
1543 buffer=(unsigned char *) AcquireQuantumMemory(quantum,sizeof(*buffer));
1544 if (buffer == (unsigned char *) NULL)
1547 (void) ThrowMagickException(exception,GetMagickModule(),
1548 ResourceLimitError,"MemoryAllocationError","`%s'",filename);
1549 return(MagickFalse);
1552 p=ReadBlobStream(image,quantum,buffer,&count);
1553 for (i=0; count > 0; p=ReadBlobStream(image,quantum,buffer,&count))
1555 length=(size_t) count;
1556 for (i=0; i < length; i+=count)
1558 count=write(file,p+i,(size_t) (length-i));
1570 buffer=(unsigned char *) RelinquishMagickMemory(buffer);
1573 ThrowFileException(exception,BlobError,"UnableToWriteBlob",filename);
1574 return(MagickFalse);
1580 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1584 % I m a g e s T o B l o b %
1588 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1590 % ImagesToBlob() implements direct to memory image formats. It returns the
1591 % image sequence as a blob and its length. The magick member of the ImageInfo
1592 % structure determines the format of the returned blob (GIF, JPEG, PNG, etc.)
1594 % Note, some image formats do not permit multiple images to the same image
1595 % stream (e.g. JPEG). in this instance, just the first image of the
1596 % sequence is returned as a blob.
1598 % The format of the ImagesToBlob method is:
1600 % unsigned char *ImagesToBlob(const ImageInfo *image_info,Image *images,
1601 % size_t *length,ExceptionInfo *exception)
1603 % A description of each parameter follows:
1605 % o image_info: the image info.
1607 % o images: the image list.
1609 % o length: This pointer to a size_t integer sets the initial length of the
1610 % blob. On return, it reflects the actual length of the blob.
1612 % o exception: return any errors or warnings in this structure.
1615 MagickExport unsigned char *ImagesToBlob(const ImageInfo *image_info,
1616 Image *images,size_t *length,ExceptionInfo *exception)
1630 assert(image_info != (const ImageInfo *) NULL);
1631 assert(image_info->signature == MagickSignature);
1632 if (image_info->debug != MagickFalse)
1633 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
1634 image_info->filename);
1635 assert(images != (Image *) NULL);
1636 assert(images->signature == MagickSignature);
1637 assert(exception != (ExceptionInfo *) NULL);
1639 blob=(unsigned char *) NULL;
1640 blob_info=CloneImageInfo(image_info);
1641 (void) SetImageInfo(blob_info,MagickTrue,exception);
1642 if (*blob_info->magick != '\0')
1643 (void) CopyMagickString(images->magick,blob_info->magick,MaxTextExtent);
1644 if (blob_info->adjoin == MagickFalse)
1646 blob_info=DestroyImageInfo(blob_info);
1647 return(ImageToBlob(image_info,images,length,exception));
1649 magick_info=GetMagickInfo(images->magick,exception);
1650 if (magick_info == (const MagickInfo *) NULL)
1652 (void) ThrowMagickException(exception,GetMagickModule(),
1653 MissingDelegateError,"NoDecodeDelegateForThisImageFormat","`%s'",
1657 (void) CopyMagickString(blob_info->magick,images->magick,MaxTextExtent);
1658 if (GetMagickBlobSupport(magick_info) != MagickFalse)
1661 Native blob support for this images format.
1663 blob_info->length=0;
1664 blob_info->blob=(void *) AcquireQuantumMemory(MagickMaxBlobExtent,
1665 sizeof(unsigned char));
1666 if (blob_info->blob == (void *) NULL)
1667 (void) ThrowMagickException(exception,GetMagickModule(),
1668 ResourceLimitError,"MemoryAllocationFailed","`%s'",images->filename);
1671 images->blob->exempt=MagickTrue;
1672 *images->filename='\0';
1673 status=WriteImages(blob_info,images,images->filename,exception);
1674 if ((status == MagickFalse) || (images->blob->length == 0))
1675 InheritException(exception,&images->exception);
1678 *length=images->blob->length;
1679 blob=DetachBlob(images->blob);
1680 blob=(unsigned char *) ResizeQuantumMemory(blob,*length,
1688 filename[MaxTextExtent],
1689 unique[MaxTextExtent];
1695 Write file to disk in blob images format.
1697 file=AcquireUniqueFileResource(unique);
1700 ThrowFileException(exception,FileOpenError,"UnableToWriteBlob",
1701 image_info->filename);
1705 blob_info->file=fdopen(file,"wb");
1706 if (blob_info->file != (FILE *) NULL)
1708 (void) FormatMagickString(filename,MaxTextExtent,"%s:%s",
1709 images->magick,unique);
1710 status=WriteImages(blob_info,images,filename,exception);
1711 (void) fclose(blob_info->file);
1712 if (status == MagickFalse)
1713 InheritException(exception,&images->exception);
1715 blob=FileToBlob(images->filename,~0UL,length,exception);
1717 (void) RelinquishUniqueFileResource(unique);
1720 blob_info=DestroyImageInfo(blob_info);
1724 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1728 % I n j e c t I m a g e B l o b %
1732 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1734 % InjectImageBlob() injects the image with a copy of itself in the specified
1735 % format (e.g. inject JPEG into a PDF image).
1737 % The format of the InjectImageBlob method is:
1739 % MagickBooleanType InjectImageBlob(const ImageInfo *image_info,
1740 % Image *image,Image *inject_image,const char *format,
1741 % ExceptionInfo *exception)
1743 % A description of each parameter follows:
1745 % o image_info: the image info..
1747 % o image: the image.
1749 % o inject_image: inject into the image stream.
1751 % o format: the image format.
1753 % o exception: return any errors or warnings in this structure.
1756 MagickExport MagickBooleanType InjectImageBlob(const ImageInfo *image_info,
1757 Image *image,Image *inject_image,const char *format,ExceptionInfo *exception)
1760 filename[MaxTextExtent];
1793 Write inject image to a temporary file.
1795 assert(image_info != (ImageInfo *) NULL);
1796 assert(image_info->signature == MagickSignature);
1797 assert(image != (Image *) NULL);
1798 assert(image->signature == MagickSignature);
1799 if (image->debug != MagickFalse)
1800 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1801 assert(inject_image != (Image *) NULL);
1802 assert(inject_image->signature == MagickSignature);
1803 assert(exception != (ExceptionInfo *) NULL);
1804 unique_file=(FILE *) NULL;
1805 file=AcquireUniqueFileResource(filename);
1807 unique_file=fdopen(file,"wb");
1808 if ((file == -1) || (unique_file == (FILE *) NULL))
1810 (void) CopyMagickString(image->filename,filename,MaxTextExtent);
1811 ThrowFileException(exception,FileOpenError,"UnableToCreateTemporaryFile",
1813 return(MagickFalse);
1815 byte_image=CloneImage(inject_image,0,0,MagickFalse,exception);
1816 if (byte_image == (Image *) NULL)
1818 (void) fclose(unique_file);
1819 (void) RelinquishUniqueFileResource(filename);
1820 return(MagickFalse);
1822 (void) FormatMagickString(byte_image->filename,MaxTextExtent,"%s:%s",format,
1824 DestroyBlob(byte_image);
1825 byte_image->blob=CloneBlobInfo((BlobInfo *) NULL);
1826 write_info=CloneImageInfo(image_info);
1827 SetImageInfoFile(write_info,unique_file);
1828 status=WriteImage(write_info,byte_image);
1829 write_info=DestroyImageInfo(write_info);
1830 byte_image=DestroyImage(byte_image);
1831 (void) fclose(unique_file);
1832 if (status == MagickFalse)
1834 (void) RelinquishUniqueFileResource(filename);
1835 return(MagickFalse);
1838 Inject into image stream.
1840 file=open(filename,O_RDONLY | O_BINARY);
1843 (void) RelinquishUniqueFileResource(filename);
1844 ThrowFileException(exception,FileOpenError,"UnableToOpenFile",
1845 image_info->filename);
1846 return(MagickFalse);
1848 quantum=(size_t) MagickMaxBufferExtent;
1849 if ((fstat(file,&file_info) == 0) && (file_info.st_size != 0))
1850 quantum=MagickMin((size_t) file_info.st_size,MagickMaxBufferExtent);
1851 buffer=(unsigned char *) AcquireQuantumMemory(quantum,sizeof(*buffer));
1852 if (buffer == (unsigned char *) NULL)
1854 (void) RelinquishUniqueFileResource(filename);
1855 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
1858 for (i=0; ; i+=count)
1860 count=(ssize_t) read(file,buffer,quantum);
1867 status=WriteBlobStream(image,(size_t) count,buffer) == count ? MagickTrue :
1871 (void) RelinquishUniqueFileResource(filename);
1872 buffer=(unsigned char *) RelinquishMagickMemory(buffer);
1877 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1881 + I s B l o b E x e m p t %
1885 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1887 % IsBlobExempt() returns true if the blob is exempt.
1889 % The format of the IsBlobExempt method is:
1891 % MagickBooleanType IsBlobExempt(const Image *image)
1893 % A description of each parameter follows:
1895 % o image: the image.
1898 MagickExport MagickBooleanType IsBlobExempt(const Image *image)
1900 assert(image != (const Image *) NULL);
1901 assert(image->signature == MagickSignature);
1902 if (image->debug != MagickFalse)
1903 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1904 return(image->blob->exempt);
1908 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1912 + I s B l o b S e e k a b l e %
1916 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1918 % IsBlobSeekable() returns true if the blob is seekable.
1920 % The format of the IsBlobSeekable method is:
1922 % MagickBooleanType IsBlobSeekable(const Image *image)
1924 % A description of each parameter follows:
1926 % o image: the image.
1929 MagickExport MagickBooleanType IsBlobSeekable(const Image *image)
1934 assert(image != (const Image *) NULL);
1935 assert(image->signature == MagickSignature);
1936 if (image->debug != MagickFalse)
1937 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1938 seekable=(image->blob->type == FileStream) ||
1939 (image->blob->type == BlobStream) ? MagickTrue : MagickFalse;
1944 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1948 + I s B l o b T e m p o r a r y %
1952 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1954 % IsBlobTemporary() returns true if the blob is temporary.
1956 % The format of the IsBlobTemporary method is:
1958 % MagickBooleanType IsBlobTemporary(const Image *image)
1960 % A description of each parameter follows:
1962 % o image: the image.
1965 MagickExport MagickBooleanType IsBlobTemporary(const Image *image)
1967 assert(image != (const Image *) NULL);
1968 assert(image->signature == MagickSignature);
1969 if (image->debug != MagickFalse)
1970 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1971 return(image->blob->temporary);
1975 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1983 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1985 % MapBlob() creates a mapping from a file to a binary large object.
1987 % The format of the MapBlob method is:
1989 % unsigned char *MapBlob(int file,const MapMode mode,
1990 % const MagickOffsetType offset,const size_t length)
1992 % A description of each parameter follows:
1994 % o file: map this file descriptor.
1996 % o mode: ReadMode, WriteMode, or IOMode.
1998 % o offset: starting at this offset within the file.
2000 % o length: the length of the mapping is returned in this pointer.
2003 MagickExport unsigned char *MapBlob(int file,const MapMode mode,
2004 const MagickOffsetType offset,const size_t length)
2006 #if defined(MAGICKCORE_HAVE_MMAP_FILEIO)
2019 #if defined(MAP_ANONYMOUS)
2020 flags|=MAP_ANONYMOUS;
2022 return((unsigned char *) NULL);
2029 protection=PROT_READ;
2031 map=(unsigned char *) mmap((char *) NULL,length,protection,flags,file,
2037 protection=PROT_WRITE;
2039 map=(unsigned char *) mmap((char *) NULL,length,protection,flags,file,
2045 protection=PROT_READ | PROT_WRITE;
2047 map=(unsigned char *) mmap((char *) NULL,length,protection,flags,file,
2052 if (map == (unsigned char *) MAP_FAILED)
2053 return((unsigned char *) NULL);
2060 return((unsigned char *) NULL);
2065 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2069 + M S B O r d e r L o n g %
2073 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2075 % MSBOrderLong() converts a least-significant byte first buffer of integers to
2076 % most-significant byte first.
2078 % The format of the MSBOrderLong method is:
2080 % void MSBOrderLong(unsigned char *buffer,const size_t length)
2082 % A description of each parameter follows.
2084 % o buffer: Specifies a pointer to a buffer of integers.
2086 % o length: Specifies the length of the buffer.
2089 MagickExport void MSBOrderLong(unsigned char *buffer,const size_t length)
2094 register unsigned char
2098 assert(buffer != (unsigned char *) NULL);
2105 *buffer++=(unsigned char) c;
2109 *buffer++=(unsigned char) c;
2115 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2119 + M S B O r d e r S h o r t %
2123 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2125 % MSBOrderShort() converts a least-significant byte first buffer of integers
2126 % to most-significant byte first.
2128 % The format of the MSBOrderShort method is:
2130 % void MSBOrderShort(unsigned char *p,const size_t length)
2132 % A description of each parameter follows.
2134 % o p: Specifies a pointer to a buffer of integers.
2136 % o length: Specifies the length of the buffer.
2139 MagickExport void MSBOrderShort(unsigned char *p,const size_t length)
2144 register unsigned char
2147 assert(p != (unsigned char *) NULL);
2154 *p++=(unsigned char) c;
2159 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2167 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2169 % OpenBlob() opens a file associated with the image. A file name of '-' sets
2170 % the file to stdin for type 'r' and stdout for type 'w'. If the filename
2171 % suffix is '.gz' or '.Z', the image is decompressed for type 'r' and
2172 % compressed for type 'w'. If the filename prefix is '|', it is piped to or
2173 % from a system command.
2175 % The format of the OpenBlob method is:
2177 % MagickBooleanType OpenBlob(const ImageInfo *image_info,Image *image,
2178 % const BlobMode mode,ExceptionInfo *exception)
2180 % A description of each parameter follows:
2182 % o image_info: the image info.
2184 % o image: the image.
2186 % o mode: the mode for opening the file.
2189 MagickExport MagickBooleanType OpenBlob(const ImageInfo *image_info,
2190 Image *image,const BlobMode mode,ExceptionInfo *exception)
2193 filename[MaxTextExtent];
2204 assert(image_info != (ImageInfo *) NULL);
2205 assert(image_info->signature == MagickSignature);
2206 if (image_info->debug != MagickFalse)
2207 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
2208 image_info->filename);
2209 assert(image != (Image *) NULL);
2210 assert(image->signature == MagickSignature);
2211 if (image_info->blob != (void *) NULL)
2213 if (image_info->stream != (StreamHandler) NULL)
2214 image->blob->stream=(StreamHandler) image_info->stream;
2215 AttachBlob(image->blob,image_info->blob,image_info->length);
2218 (void) DetachBlob(image->blob);
2221 default: type="r"; break;
2222 case ReadBlobMode: type="r"; break;
2223 case ReadBinaryBlobMode: type="rb"; break;
2224 case WriteBlobMode: type="w"; break;
2225 case WriteBinaryBlobMode: type="w+b"; break;
2226 case AppendBlobMode: type="a"; break;
2227 case AppendBinaryBlobMode: type="a+b"; break;
2230 image->blob->synchronize=image_info->synchronize;
2231 if (image_info->stream != (StreamHandler) NULL)
2233 image->blob->stream=(StreamHandler) image_info->stream;
2236 image->blob->type=FifoStream;
2244 (void) CopyMagickString(filename,image->filename,MaxTextExtent);
2245 rights=ReadPolicyRights;
2247 rights=WritePolicyRights;
2248 if (IsRightsAuthorized(PathPolicyDomain,rights,filename) == MagickFalse)
2251 (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
2252 "NotAuthorized","`%s'",filename);
2253 return(MagickFalse);
2255 if ((LocaleCompare(filename,"-") == 0) ||
2256 ((*filename == '\0') && (image_info->file == (FILE *) NULL)))
2258 image->blob->file=(*type == 'r') ? stdin : stdout;
2259 #if defined(__WINDOWS__) || defined(__OS2__)
2260 if (strchr(type,'b') != (char *) NULL)
2261 setmode(_fileno(image->blob->file),_O_BINARY);
2263 image->blob->type=StandardStream;
2264 image->blob->exempt=MagickTrue;
2267 if (LocaleNCompare(filename,"fd:",3) == 0)
2270 mode[MaxTextExtent];
2274 image->blob->file=fdopen(StringToLong(filename+3),mode);
2275 #if defined(__WINDOWS__) || defined(__OS2__)
2276 if (strchr(type,'b') != (char *) NULL)
2277 setmode(_fileno(image->blob->file),_O_BINARY);
2279 image->blob->type=StandardStream;
2280 image->blob->exempt=MagickTrue;
2283 #if defined(MAGICKCORE_HAVE_POPEN)
2284 if (*filename == '|')
2287 mode[MaxTextExtent];
2290 Pipe image to or from a system command.
2292 #if defined(SIGPIPE)
2294 (void) signal(SIGPIPE,SIG_IGN);
2298 image->blob->file=(FILE *) popen(filename+1,mode);
2299 if (image->blob->file == (FILE *) NULL)
2301 ThrowFileException(exception,BlobError,"UnableToOpenBlob",filename);
2302 return(MagickFalse);
2304 image->blob->type=PipeStream;
2305 image->blob->exempt=MagickTrue;
2309 status=GetPathAttributes(filename,&image->blob->properties);
2310 #if defined(S_ISFIFO)
2311 if ((status == MagickTrue) && S_ISFIFO(image->blob->properties.st_mode))
2313 image->blob->file=(FILE *) OpenMagickStream(filename,type);
2314 if (image->blob->file == (FILE *) NULL)
2316 ThrowFileException(exception,BlobError,"UnableToOpenBlob",filename);
2317 return(MagickFalse);
2319 image->blob->type=FileStream;
2320 image->blob->exempt=MagickTrue;
2326 (void) CopyMagickString(filename,image->filename,MaxTextExtent);
2327 if (image_info->adjoin == MagickFalse)
2330 Form filename for multi-part images.
2332 (void) InterpretImageFilename(image_info,image,image->filename,(int)
2333 image->scene,filename);
2334 if ((image->previous != (Image *) NULL) ||
2335 (GetNextImageInList(image) != (Image *) NULL))
2337 if (LocaleCompare(filename,image->filename) == 0)
2340 extension[MaxTextExtent],
2341 path[MaxTextExtent];
2343 GetPathComponent(image->filename,RootPath,path);
2344 GetPathComponent(image->filename,ExtensionPath,extension);
2345 if (*extension == '\0')
2346 (void) FormatMagickString(filename,MaxTextExtent,"%s-%lu",
2349 (void) FormatMagickString(filename,MaxTextExtent,
2350 "%s-%lu.%s",path,image->scene,extension);
2353 (void) CopyMagickString(image->filename,filename,MaxTextExtent);
2354 #if defined(macintosh)
2355 SetApplicationType(filename,image_info->magick,'8BIM');
2359 #if defined(MAGICKCORE_ZLIB_DELEGATE)
2360 if (((strlen(filename) > 2) &&
2361 (LocaleCompare(filename+strlen(filename)-2,".Z") == 0)) ||
2362 ((strlen(filename) > 3) &&
2363 (LocaleCompare(filename+strlen(filename)-3,".gz") == 0)) ||
2364 ((strlen(filename) > 4) &&
2365 (LocaleCompare(filename+strlen(filename)-4,".wmz") == 0)) ||
2366 ((strlen(filename) > 5) &&
2367 (LocaleCompare(filename+strlen(filename)-5,".svgz") == 0)))
2369 image->blob->file=(FILE *) gzopen(filename,type);
2370 if (image->blob->file != (FILE *) NULL)
2371 image->blob->type=ZipStream;
2375 #if defined(MAGICKCORE_BZLIB_DELEGATE)
2376 if ((strlen(filename) > 4) &&
2377 (LocaleCompare(filename+strlen(filename)-4,".bz2") == 0))
2379 image->blob->file=(FILE *) BZ2_bzopen(filename,type);
2380 if (image->blob->file != (FILE *) NULL)
2381 image->blob->type=BZipStream;
2385 if (image_info->file != (FILE *) NULL)
2387 image->blob->file=image_info->file;
2388 image->blob->type=FileStream;
2389 image->blob->exempt=MagickTrue;
2393 image->blob->file=(FILE *) OpenMagickStream(filename,type);
2394 if (image->blob->file != (FILE *) NULL)
2396 image->blob->type=FileStream;
2397 #if defined(MAGICKCORE_HAVE_SETVBUF)
2398 (void) setvbuf(image->blob->file,(char *) NULL,(int) _IOFBF,
2409 (void) ResetMagickMemory(magick,0,sizeof(magick));
2410 count=fread(magick,1,sizeof(magick),image->blob->file);
2411 (void) rewind(image->blob->file);
2412 (void) LogMagickEvent(BlobEvent,GetMagickModule(),
2413 " read %ld magic header bytes",(long) count);
2414 #if defined(MAGICKCORE_ZLIB_DELEGATE)
2415 if (((int) magick[0] == 0x1F) && ((int) magick[1] == 0x8B) &&
2416 ((int) magick[2] == 0x08))
2418 (void) fclose(image->blob->file);
2419 image->blob->file=(FILE *) gzopen(filename,type);
2420 if (image->blob->file != (FILE *) NULL)
2421 image->blob->type=ZipStream;
2424 #if defined(MAGICKCORE_BZLIB_DELEGATE)
2425 if (strncmp((char *) magick,"BZh",3) == 0)
2427 (void) fclose(image->blob->file);
2428 image->blob->file=(FILE *) BZ2_bzopen(filename,type);
2429 if (image->blob->file != (FILE *) NULL)
2430 image->blob->type=BZipStream;
2436 if ((image->blob->type == FileStream) && (*type == 'r'))
2447 sans_exception=AcquireExceptionInfo();
2448 magick_info=GetMagickInfo(image_info->magick,sans_exception);
2449 sans_exception=DestroyExceptionInfo(sans_exception);
2450 properties=(&image->blob->properties);
2451 if ((magick_info != (const MagickInfo *) NULL) &&
2452 (GetMagickBlobSupport(magick_info) != MagickFalse) &&
2453 (properties->st_size <= MagickMaxBufferExtent))
2461 length=(size_t) properties->st_size;
2462 blob=MapBlob(fileno(image->blob->file),ReadMode,0,length);
2463 if (blob != (void *) NULL)
2466 Format supports blobs-- use memory-mapped I/O.
2468 if (image_info->file != (FILE *) NULL)
2469 image->blob->exempt=MagickFalse;
2472 (void) fclose(image->blob->file);
2473 image->blob->file=(FILE *) NULL;
2475 AttachBlob(image->blob,blob,length);
2476 image->blob->mapped=MagickTrue;
2480 image->blob->status=MagickFalse;
2481 if (image->blob->type != UndefinedStream)
2482 image->blob->size=GetBlobSize(image);
2485 ThrowFileException(exception,BlobError,"UnableToOpenBlob",filename);
2486 return(MagickFalse);
2492 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2500 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2502 % PingBlob() returns all the attributes of an image or image sequence except
2503 % for the pixels. It is much faster and consumes far less memory than
2504 % BlobToImage(). On failure, a NULL image is returned and exception
2505 % describes the reason for the failure.
2507 % The format of the PingBlob method is:
2509 % Image *PingBlob(const ImageInfo *image_info,const void *blob,
2510 % const size_t length,ExceptionInfo *exception)
2512 % A description of each parameter follows:
2514 % o image_info: the image info.
2516 % o blob: the address of a character stream in one of the image formats
2517 % understood by ImageMagick.
2519 % o length: This size_t integer reflects the length in bytes of the blob.
2521 % o exception: return any errors or warnings in this structure.
2525 #if defined(__cplusplus) || defined(c_plusplus)
2529 static size_t PingStream(const Image *magick_unused(image),
2530 const void *magick_unused(pixels),const size_t columns)
2535 #if defined(__cplusplus) || defined(c_plusplus)
2539 MagickExport Image *PingBlob(const ImageInfo *image_info,const void *blob,
2540 const size_t length,ExceptionInfo *exception)
2548 assert(image_info != (ImageInfo *) NULL);
2549 assert(image_info->signature == MagickSignature);
2550 if (image_info->debug != MagickFalse)
2551 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
2552 image_info->filename);
2553 assert(exception != (ExceptionInfo *) NULL);
2554 if ((blob == (const void *) NULL) || (length == 0))
2556 (void) ThrowMagickException(exception,GetMagickModule(),BlobError,
2557 "UnrecognizedImageFormat","`%s'",image_info->magick);
2558 return((Image *) NULL);
2560 ping_info=CloneImageInfo(image_info);
2561 ping_info->blob=(void *) AcquireQuantumMemory(length,sizeof(unsigned char));
2562 if (ping_info->blob == (const void *) NULL)
2564 (void) ThrowMagickException(exception,GetMagickModule(),
2565 ResourceLimitFatalError,"MemoryAllocationFailed","`%s'","");
2566 return((Image *) NULL);
2568 (void) CopyMagickMemory(ping_info->blob,blob,length);
2569 ping_info->length=length;
2570 ping_info->ping=MagickTrue;
2571 image=ReadStream(ping_info,&PingStream,exception);
2572 ping_info->blob=(void *) RelinquishMagickMemory(ping_info->blob);
2573 ping_info=DestroyImageInfo(ping_info);
2578 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2586 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2588 % ReadBlob() reads data from the blob or image file and returns it. It
2589 % returns the number of bytes read.
2591 % The format of the ReadBlob method is:
2593 % ssize_t ReadBlob(Image *image,const size_t length,unsigned char *data)
2595 % A description of each parameter follows:
2597 % o image: the image.
2599 % o length: Specifies an integer representing the number of bytes to read
2602 % o data: Specifies an area to place the information requested from the
2606 MagickExport ssize_t ReadBlob(Image *image,const size_t length,
2607 unsigned char *data)
2612 register unsigned char
2618 assert(image != (Image *) NULL);
2619 assert(image->signature == MagickSignature);
2620 assert(image->blob != (BlobInfo *) NULL);
2621 assert(image->blob->type != UndefinedStream);
2624 assert(data != (void *) NULL);
2627 switch (image->blob->type)
2629 case UndefinedStream:
2632 case StandardStream:
2639 count=(ssize_t) fread(q,1,length,image->blob->file);
2644 c=getc(image->blob->file);
2647 *q++=(unsigned char) c;
2652 c=getc(image->blob->file);
2655 *q++=(unsigned char) c;
2665 #if defined(MAGICKCORE_ZLIB_DELEGATE)
2670 count=(ssize_t) gzread(image->blob->file,q,(unsigned int) length);
2675 c=gzgetc(image->blob->file);
2678 *q++=(unsigned char) c;
2683 c=gzgetc(image->blob->file);
2686 *q++=(unsigned char) c;
2697 #if defined(MAGICKCORE_BZLIB_DELEGATE)
2698 count=(ssize_t) BZ2_bzread((BZFILE *) image->blob->file,q,(int) length);
2706 register const unsigned char
2709 if (image->blob->offset >= (MagickOffsetType) image->blob->length)
2711 image->blob->eof=MagickTrue;
2714 p=image->blob->data+image->blob->offset;
2715 count=(ssize_t) MagickMin(length,(size_t) (image->blob->length-
2716 image->blob->offset));
2717 image->blob->offset+=count;
2718 if (count != (ssize_t) length)
2719 image->blob->eof=MagickTrue;
2720 (void) CopyMagickMemory(q,p,(size_t) count);
2728 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2732 + R e a d B l o b B y t e %
2736 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2738 % ReadBlobByte() reads a single byte from the image file and returns it.
2740 % The format of the ReadBlobByte method is:
2742 % int ReadBlobByte(Image *image)
2744 % A description of each parameter follows.
2746 % o image: the image.
2749 MagickExport int ReadBlobByte(Image *image)
2751 register const unsigned char
2760 assert(image != (Image *) NULL);
2761 assert(image->signature == MagickSignature);
2762 p=ReadBlobStream(image,1,buffer,&count);
2769 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2773 + R e a d B l o b D o u b l e %
2777 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2779 % ReadBlobDouble() reads a double value as a 64-bit quantity in the byte-order
2780 % specified by the endian member of the image structure.
2782 % The format of the ReadBlobDouble method is:
2784 % double ReadBlobDouble(Image *image)
2786 % A description of each parameter follows.
2788 % o image: the image.
2791 MagickExport double ReadBlobDouble(Image *image)
2802 quantum.double_value=0.0;
2803 quantum.unsigned_value=ReadBlobLongLong(image);
2804 return(quantum.double_value);
2808 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2812 + R e a d B l o b F l o a t %
2816 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2818 % ReadBlobFloat() reads a float value as a 32-bit quantity in the byte-order
2819 % specified by the endian member of the image structure.
2821 % The format of the ReadBlobFloat method is:
2823 % float ReadBlobFloat(Image *image)
2825 % A description of each parameter follows.
2827 % o image: the image.
2830 MagickExport float ReadBlobFloat(Image *image)
2841 quantum.float_value=0.0;
2842 quantum.unsigned_value=ReadBlobLong(image);
2843 return(quantum.float_value);
2847 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2851 + R e a d B l o b L o n g %
2855 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2857 % ReadBlobLong() reads a long value as a 32-bit quantity in the byte-order
2858 % specified by the endian member of the image structure.
2860 % The format of the ReadBlobLong method is:
2862 % unsigned int ReadBlobLong(Image *image)
2864 % A description of each parameter follows.
2866 % o image: the image.
2869 MagickExport unsigned int ReadBlobLong(Image *image)
2871 register const unsigned char
2883 assert(image != (Image *) NULL);
2884 assert(image->signature == MagickSignature);
2886 p=ReadBlobStream(image,4,buffer,&count);
2889 if (image->endian == LSBEndian)
2891 value=(unsigned int) (*p++);
2892 value|=((unsigned int) (*p++)) << 8;
2893 value|=((unsigned int) (*p++)) << 16;
2894 value|=((unsigned int) (*p++)) << 24;
2897 value=((unsigned int) (*p++)) << 24;
2898 value|=((unsigned int) (*p++)) << 16;
2899 value|=((unsigned int) (*p++)) << 8;
2900 value|=((unsigned int) (*p++));
2905 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2909 + R e a d B l o b L o n g L o n g %
2913 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2915 % ReadBlobLongLong() reads a long value as a 64-bit quantity in the byte-order
2916 % specified by the endian member of the image structure.
2918 % The format of the ReadBlobLong method is:
2920 % MagickSizeType ReadBlobLong(Image *image)
2922 % A description of each parameter follows.
2924 % o image: the image.
2927 MagickExport MagickSizeType ReadBlobLongLong(Image *image)
2929 register const unsigned char
2941 assert(image != (Image *) NULL);
2942 assert(image->signature == MagickSignature);
2944 p=ReadBlobStream(image,8,buffer,&count);
2946 return(MagickULLConstant(0));
2947 if (image->endian == LSBEndian)
2949 value=(MagickSizeType) (*p++);
2950 value|=((MagickSizeType) (*p++)) << 8;
2951 value|=((MagickSizeType) (*p++)) << 16;
2952 value|=((MagickSizeType) (*p++)) << 24;
2953 value|=((MagickSizeType) (*p++)) << 32;
2954 value|=((MagickSizeType) (*p++)) << 40;
2955 value|=((MagickSizeType) (*p++)) << 48;
2956 value|=((MagickSizeType) (*p++)) << 56;
2957 return(value & MagickULLConstant(0xffffffffffffffff));
2959 value=((MagickSizeType) (*p++)) << 56;
2960 value|=((MagickSizeType) (*p++)) << 48;
2961 value|=((MagickSizeType) (*p++)) << 40;
2962 value|=((MagickSizeType) (*p++)) << 32;
2963 value|=((MagickSizeType) (*p++)) << 24;
2964 value|=((MagickSizeType) (*p++)) << 16;
2965 value|=((MagickSizeType) (*p++)) << 8;
2966 value|=((MagickSizeType) (*p++));
2967 return(value & MagickULLConstant(0xffffffffffffffff));
2971 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2975 + R e a d B l o b S h o r t %
2979 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2981 % ReadBlobShort() reads a short value as a 16-bit quantity in the byte-order
2982 % specified by the endian member of the image structure.
2984 % The format of the ReadBlobShort method is:
2986 % unsigned short ReadBlobShort(Image *image)
2988 % A description of each parameter follows.
2990 % o image: the image.
2993 MagickExport unsigned short ReadBlobShort(Image *image)
2995 register const unsigned char
2998 register unsigned int
3007 assert(image != (Image *) NULL);
3008 assert(image->signature == MagickSignature);
3010 p=ReadBlobStream(image,2,buffer,&count);
3012 return((unsigned short) 0U);
3013 if (image->endian == LSBEndian)
3015 value=(unsigned int) (*p++);
3016 value|=((unsigned int) (*p++)) << 8;
3017 return((unsigned short) (value & 0xffff));
3019 value=(unsigned int) ((*p++) << 8);
3020 value|=(unsigned int) (*p++);
3021 return((unsigned short) (value & 0xffff));
3025 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3029 + R e a d B l o b L S B L o n g %
3033 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3035 % ReadBlobLSBLong() reads a long value as a 32-bit quantity in
3036 % least-significant byte first order.
3038 % The format of the ReadBlobLSBLong method is:
3040 % unsigned int ReadBlobLSBLong(Image *image)
3042 % A description of each parameter follows.
3044 % o image: the image.
3047 MagickExport unsigned int ReadBlobLSBLong(Image *image)
3049 register const unsigned char
3052 register unsigned int
3061 assert(image != (Image *) NULL);
3062 assert(image->signature == MagickSignature);
3064 p=ReadBlobStream(image,4,buffer,&count);
3067 value=(unsigned int) (*p++);
3068 value|=((unsigned int) (*p++)) << 8;
3069 value|=((unsigned int) (*p++)) << 16;
3070 value|=((unsigned int) (*p++)) << 24;
3075 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3079 + R e a d B l o b L S B S h o r t %
3083 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3085 % ReadBlobLSBShort() reads a short value as a 16-bit quantity in
3086 % least-significant byte first order.
3088 % The format of the ReadBlobLSBShort method is:
3090 % unsigned short ReadBlobLSBShort(Image *image)
3092 % A description of each parameter follows.
3094 % o image: the image.
3097 MagickExport unsigned short ReadBlobLSBShort(Image *image)
3099 register const unsigned char
3102 register unsigned int
3111 assert(image != (Image *) NULL);
3112 assert(image->signature == MagickSignature);
3114 p=ReadBlobStream(image,2,buffer,&count);
3116 return((unsigned short) 0U);
3117 value=(unsigned int) (*p++);
3118 value|=((unsigned int) ((*p++)) << 8);
3119 return((unsigned short) (value & 0xffff));
3123 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3127 + R e a d B l o b M S B L o n g %
3131 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3133 % ReadBlobMSBLong() reads a long value as a 32-bit quantity in
3134 % most-significant byte first order.
3136 % The format of the ReadBlobMSBLong method is:
3138 % unsigned int ReadBlobMSBLong(Image *image)
3140 % A description of each parameter follows.
3142 % o image: the image.
3145 MagickExport unsigned int ReadBlobMSBLong(Image *image)
3147 register const unsigned char
3150 register unsigned int
3159 assert(image != (Image *) NULL);
3160 assert(image->signature == MagickSignature);
3162 p=ReadBlobStream(image,4,buffer,&count);
3165 value=((unsigned int) (*p++) << 24);
3166 value|=((unsigned int) (*p++) << 16);
3167 value|=((unsigned int) (*p++) << 8);
3168 value|=(unsigned int) (*p++);
3173 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3177 + R e a d B l o b M S B S h o r t %
3181 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3183 % ReadBlobMSBShort() reads a short value as a 16-bit quantity in
3184 % most-significant byte first order.
3186 % The format of the ReadBlobMSBShort method is:
3188 % unsigned short ReadBlobMSBShort(Image *image)
3190 % A description of each parameter follows.
3192 % o image: the image.
3195 MagickExport unsigned short ReadBlobMSBShort(Image *image)
3197 register const unsigned char
3200 register unsigned int
3209 assert(image != (Image *) NULL);
3210 assert(image->signature == MagickSignature);
3212 p=ReadBlobStream(image,2,buffer,&count);
3214 return((unsigned short) 0U);
3215 value=(unsigned int) ((*p++) << 8);
3216 value|=(unsigned int) (*p++);
3217 return((unsigned short) (value & 0xffff));
3221 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3225 + R e a d B l o b S t r i n g %
3229 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3231 % ReadBlobString() reads characters from a blob or file until a newline
3232 % character is read or an end-of-file condition is encountered.
3234 % The format of the ReadBlobString method is:
3236 % char *ReadBlobString(Image *image,char *string)
3238 % A description of each parameter follows:
3240 % o image: the image.
3242 % o string: the address of a character buffer.
3245 MagickExport char *ReadBlobString(Image *image,char *string)
3247 register const unsigned char
3259 assert(image != (Image *) NULL);
3260 assert(image->signature == MagickSignature);
3261 for (i=0; i < (MaxTextExtent-1L); i++)
3263 p=ReadBlobStream(image,1,buffer,&count);
3267 return((char *) NULL);
3270 string[i]=(char) (*p);
3271 if ((string[i] == '\n') || (string[i] == '\r'))
3279 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3283 + R e f e r e n c e B l o b %
3287 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3289 % ReferenceBlob() increments the reference count associated with the pixel
3290 % blob returning a pointer to the blob.
3292 % The format of the ReferenceBlob method is:
3294 % BlobInfo ReferenceBlob(BlobInfo *blob_info)
3296 % A description of each parameter follows:
3298 % o blob_info: the blob_info.
3301 MagickExport BlobInfo *ReferenceBlob(BlobInfo *blob)
3303 assert(blob != (BlobInfo *) NULL);
3304 assert(blob->signature == MagickSignature);
3305 if (blob->debug != MagickFalse)
3306 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
3307 LockSemaphoreInfo(blob->semaphore);
3308 blob->reference_count++;
3309 UnlockSemaphoreInfo(blob->semaphore);
3314 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3322 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3324 % SeekBlob() sets the offset in bytes from the beginning of a blob or file
3325 % and returns the resulting offset.
3327 % The format of the SeekBlob method is:
3329 % MagickOffsetType SeekBlob(Image *image,const MagickOffsetType offset,
3332 % A description of each parameter follows:
3334 % o image: the image.
3336 % o offset: Specifies an integer representing the offset in bytes.
3338 % o whence: Specifies an integer representing how the offset is
3339 % treated relative to the beginning of the blob as follows:
3341 % SEEK_SET Set position equal to offset bytes.
3342 % SEEK_CUR Set position to current location plus offset.
3343 % SEEK_END Set position to EOF plus offset.
3346 MagickExport MagickOffsetType SeekBlob(Image *image,
3347 const MagickOffsetType offset,const int whence)
3349 assert(image != (Image *) NULL);
3350 assert(image->signature == MagickSignature);
3351 if (image->debug != MagickFalse)
3352 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3353 assert(image->blob != (BlobInfo *) NULL);
3354 assert(image->blob->type != UndefinedStream);
3355 switch (image->blob->type)
3357 case UndefinedStream:
3361 if (fseek(image->blob->file,offset,whence) < 0)
3363 image->blob->offset=TellBlob(image);
3366 case StandardStream:
3370 #if defined(MAGICKCORE_ZLIB_DELEGATE)
3371 if (gzseek(image->blob->file,(off_t) offset,whence) < 0)
3374 image->blob->offset=TellBlob(image);
3390 image->blob->offset=offset;
3395 if ((image->blob->offset+offset) < 0)
3397 image->blob->offset+=offset;
3402 if (((MagickOffsetType) image->blob->length+offset) < 0)
3404 image->blob->offset=image->blob->length+offset;
3408 if (image->blob->offset <= (MagickOffsetType)
3409 ((off_t) image->blob->length))
3410 image->blob->eof=MagickFalse;
3412 if (image->blob->mapped != MagickFalse)
3416 image->blob->extent=(size_t) (image->blob->offset+
3417 image->blob->quantum);
3418 image->blob->data=(unsigned char *) ResizeQuantumMemory(
3419 image->blob->data,image->blob->extent+1,
3420 sizeof(*image->blob->data));
3421 (void) SyncBlob(image);
3422 if (image->blob->data == (unsigned char *) NULL)
3424 (void) DetachBlob(image->blob);
3431 return(image->blob->offset);
3435 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3439 + S e t B l o b E x e m p t %
3443 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3445 % SetBlobExempt() sets the blob exempt status.
3447 % The format of the SetBlobExempt method is:
3449 % MagickBooleanType SetBlobExempt(const Image *image,
3450 % const MagickBooleanType exempt)
3452 % A description of each parameter follows:
3454 % o image: the image.
3456 % o exempt: Set to true if this blob is exempt from being closed.
3459 MagickExport void SetBlobExempt(Image *image,const MagickBooleanType exempt)
3461 assert(image != (const Image *) NULL);
3462 assert(image->signature == MagickSignature);
3463 if (image->debug != MagickFalse)
3464 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3465 image->blob->exempt=exempt;
3469 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3473 + S e t B l o b E x t e n t %
3477 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3479 % SetBlobExtent() ensures enough space is allocated for the blob. If the
3480 % method is successful, subsequent writes to bytes in the specified range are
3481 % guaranteed not to fail.
3483 % The format of the SetBlobExtent method is:
3485 % MagickBooleanType SetBlobExtent(Image *image,const MagickSizeType extent)
3487 % A description of each parameter follows:
3489 % o image: the image.
3491 % o extent: the blob maximum extent.
3494 MagickExport MagickBooleanType SetBlobExtent(Image *image,
3495 const MagickSizeType extent)
3497 assert(image != (Image *) NULL);
3498 assert(image->signature == MagickSignature);
3499 if (image->debug != MagickFalse)
3500 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3501 assert(image->blob != (BlobInfo *) NULL);
3502 assert(image->blob->type != UndefinedStream);
3503 switch (image->blob->type)
3505 case UndefinedStream:
3509 if (extent != (MagickSizeType) ((off_t) extent))
3510 return(MagickFalse);
3511 #if !defined(MAGICKCORE_POSIX_FALLOCATE)
3512 return(MagickFalse);
3521 offset=TellBlob(image);
3522 status=posix_fallocate(fileno(image->blob->file),(off_t) offset,
3523 (off_t) (extent-offset));
3525 return(MagickFalse);
3530 case StandardStream:
3533 return(MagickFalse);
3535 return(MagickFalse);
3537 return(MagickFalse);
3540 if (image->blob->mapped != MagickFalse)
3542 if (image->blob->file == (FILE *) NULL)
3543 return(MagickFalse);
3544 (void) UnmapBlob(image->blob->data,image->blob->length);
3545 #if !defined(MAGICKCORE_POSIX_FALLOCATE)
3546 return(MagickFalse);
3555 offset=TellBlob(image);
3556 status=posix_fallocate(fileno(image->blob->file),(off_t) offset,
3557 (off_t) (extent-offset));
3559 return(MagickFalse);
3561 image->blob->data=(unsigned char*) MapBlob(fileno(image->blob->file),
3562 WriteMode,0,(size_t) extent);
3563 image->blob->extent=(size_t) extent;
3564 image->blob->length=(size_t) extent;
3565 (void) SyncBlob(image);
3569 if (extent != (MagickSizeType) ((size_t) extent))
3570 return(MagickFalse);
3571 image->blob->extent=(size_t) extent;
3572 image->blob->data=(unsigned char *) ResizeQuantumMemory(image->blob->data,
3573 image->blob->extent+1,sizeof(*image->blob->data));
3574 (void) SyncBlob(image);
3575 if (image->blob->data == (unsigned char *) NULL)
3577 (void) DetachBlob(image->blob);
3578 return(MagickFalse);
3587 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3595 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3597 % SyncBlob() flushes the datastream if it is a file or synchronizes the data
3598 % attributes if it is an blob.
3600 % The format of the SyncBlob method is:
3602 % int SyncBlob(Image *image)
3604 % A description of each parameter follows:
3606 % o image: the image.
3609 static int SyncBlob(Image *image)
3614 assert(image != (Image *) NULL);
3615 assert(image->signature == MagickSignature);
3616 if (image->debug != MagickFalse)
3617 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3618 assert(image->blob != (BlobInfo *) NULL);
3619 assert(image->blob->type != UndefinedStream);
3621 switch (image->blob->type)
3623 case UndefinedStream:
3626 case StandardStream:
3629 status=fflush(image->blob->file);
3634 #if defined(MAGICKCORE_ZLIB_DELEGATE)
3635 status=gzflush(image->blob->file,Z_SYNC_FLUSH);
3641 #if defined(MAGICKCORE_BZLIB_DELEGATE)
3642 status=BZ2_bzflush((BZFILE *) image->blob->file);
3650 #if defined(MAGICKCORE_HAVE_MMAP_FILEIO)
3651 if (image->blob->mapped != MagickFalse)
3652 status=msync(image->blob->data,image->blob->length,MS_SYNC);
3661 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3669 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3671 % TellBlob() obtains the current value of the blob or file position.
3673 % The format of the TellBlob method is:
3675 % MagickOffsetType TellBlob(const Image *image)
3677 % A description of each parameter follows:
3679 % o image: the image.
3682 MagickExport MagickOffsetType TellBlob(const Image *image)
3687 assert(image != (Image *) NULL);
3688 assert(image->signature == MagickSignature);
3689 if (image->debug != MagickFalse)
3690 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3691 assert(image->blob != (BlobInfo *) NULL);
3692 assert(image->blob->type != UndefinedStream);
3694 switch (image->blob->type)
3696 case UndefinedStream:
3700 offset=ftell(image->blob->file);
3703 case StandardStream:
3708 #if defined(MAGICKCORE_ZLIB_DELEGATE)
3709 offset=(MagickOffsetType) gztell(image->blob->file);
3719 offset=image->blob->offset;
3727 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3731 + U n m a p B l o b %
3735 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3737 % UnmapBlob() deallocates the binary large object previously allocated with
3738 % the MapBlob method.
3740 % The format of the UnmapBlob method is:
3742 % MagickBooleanType UnmapBlob(void *map,const size_t length)
3744 % A description of each parameter follows:
3746 % o map: the address of the binary large object.
3748 % o length: the length of the binary large object.
3751 MagickExport MagickBooleanType UnmapBlob(void *map,const size_t length)
3753 #if defined(MAGICKCORE_HAVE_MMAP_FILEIO)
3757 status=munmap(map,length);
3758 return(status == -1 ? MagickFalse : MagickTrue);
3762 return(MagickFalse);
3767 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3771 + W r i t e B l o b %
3775 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3777 % WriteBlob() writes data to a blob or image file. It returns the number of
3780 % The format of the WriteBlob method is:
3782 % ssize_t WriteBlob(Image *image,const size_t length,
3783 % const unsigned char *data)
3785 % A description of each parameter follows:
3787 % o image: the image.
3789 % o length: Specifies an integer representing the number of bytes to
3790 % write to the file.
3792 % o data: The address of the data to write to the blob or file.
3795 MagickExport ssize_t WriteBlob(Image *image,const size_t length,
3796 const unsigned char *data)
3801 register const unsigned char
3807 assert(image != (Image *) NULL);
3808 assert(image->signature == MagickSignature);
3809 assert(data != (const unsigned char *) NULL);
3810 assert(image->blob != (BlobInfo *) NULL);
3811 assert(image->blob->type != UndefinedStream);
3816 switch (image->blob->type)
3818 case UndefinedStream:
3821 case StandardStream:
3828 count=(ssize_t) fwrite((const char *) data,1,length,
3834 c=putc((int) *p++,image->blob->file);
3841 c=putc((int) *p++,image->blob->file);
3853 #if defined(MAGICKCORE_ZLIB_DELEGATE)
3858 count=(ssize_t) gzwrite(image->blob->file,(void *) data,
3859 (unsigned int) length);
3864 c=gzputc(image->blob->file,(int) *p++);
3871 c=gzputc(image->blob->file,(int) *p++);
3884 #if defined(MAGICKCORE_BZLIB_DELEGATE)
3885 count=(ssize_t) BZ2_bzwrite((BZFILE *) image->blob->file,(void *) data,
3892 count=(ssize_t) image->blob->stream(image,data,length);
3897 register unsigned char
3900 if ((image->blob->offset+(MagickOffsetType) length) >=
3901 (MagickOffsetType) image->blob->extent)
3903 if (image->blob->mapped != MagickFalse)
3905 image->blob->quantum<<=1;
3906 image->blob->extent+=length+image->blob->quantum;
3907 image->blob->data=(unsigned char *) ResizeQuantumMemory(
3908 image->blob->data,image->blob->extent+1,sizeof(*image->blob->data));
3909 (void) SyncBlob(image);
3910 if (image->blob->data == (unsigned char *) NULL)
3912 (void) DetachBlob(image->blob);
3916 q=image->blob->data+image->blob->offset;
3917 (void) CopyMagickMemory(q,p,length);
3918 image->blob->offset+=length;
3919 if (image->blob->offset >= (MagickOffsetType) image->blob->length)
3920 image->blob->length=(size_t) image->blob->offset;
3921 count=(ssize_t) length;
3928 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3932 + W r i t e B l o b B y t e %
3936 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3938 % WriteBlobByte() write an integer to a blob. It returns the number of bytes
3939 % written (either 0 or 1);
3941 % The format of the WriteBlobByte method is:
3943 % ssize_t WriteBlobByte(Image *image,const unsigned char value)
3945 % A description of each parameter follows.
3947 % o image: the image.
3949 % o value: Specifies the value to write.
3952 MagickExport ssize_t WriteBlobByte(Image *image,const unsigned char value)
3954 assert(image != (Image *) NULL);
3955 assert(image->signature == MagickSignature);
3956 return(WriteBlobStream(image,1,&value));
3960 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3964 + W r i t e B l o b F l o a t %
3968 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3970 % WriteBlobFloat() writes a float value as a 32-bit quantity in the byte-order
3971 % specified by the endian member of the image structure.
3973 % The format of the WriteBlobFloat method is:
3975 % ssize_t WriteBlobFloat(Image *image,const float value)
3977 % A description of each parameter follows.
3979 % o image: the image.
3981 % o value: Specifies the value to write.
3984 MagickExport ssize_t WriteBlobFloat(Image *image,const float value)
3995 quantum.unsigned_value=0U;
3996 quantum.float_value=value;
3997 return(WriteBlobLong(image,quantum.unsigned_value));
4001 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4005 + W r i t e B l o b L o n g %
4009 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4011 % WriteBlobLong() writes a long value as a 32-bit quantity in the byte-order
4012 % specified by the endian member of the image structure.
4014 % The format of the WriteBlobLong method is:
4016 % ssize_t WriteBlobLong(Image *image,const unsigned int value)
4018 % A description of each parameter follows.
4020 % o image: the image.
4022 % o value: Specifies the value to write.
4025 MagickExport ssize_t WriteBlobLong(Image *image,const unsigned int value)
4030 assert(image != (Image *) NULL);
4031 assert(image->signature == MagickSignature);
4032 if (image->endian == LSBEndian)
4034 buffer[0]=(unsigned char) value;
4035 buffer[1]=(unsigned char) (value >> 8);
4036 buffer[2]=(unsigned char) (value >> 16);
4037 buffer[3]=(unsigned char) (value >> 24);
4038 return(WriteBlobStream(image,4,buffer));
4040 buffer[0]=(unsigned char) (value >> 24);
4041 buffer[1]=(unsigned char) (value >> 16);
4042 buffer[2]=(unsigned char) (value >> 8);
4043 buffer[3]=(unsigned char) value;
4044 return(WriteBlobStream(image,4,buffer));
4048 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4052 + W r i t e B l o b S h o r t %
4056 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4058 % WriteBlobShort() writes a short value as a 16-bit quantity in the
4059 % byte-order specified by the endian member of the image structure.
4061 % The format of the WriteBlobShort method is:
4063 % ssize_t WriteBlobShort(Image *image,const unsigned short value)
4065 % A description of each parameter follows.
4067 % o image: the image.
4069 % o value: Specifies the value to write.
4072 MagickExport ssize_t WriteBlobShort(Image *image,const unsigned short value)
4077 assert(image != (Image *) NULL);
4078 assert(image->signature == MagickSignature);
4079 if (image->endian == LSBEndian)
4081 buffer[0]=(unsigned char) value;
4082 buffer[1]=(unsigned char) (value >> 8);
4083 return(WriteBlobStream(image,2,buffer));
4085 buffer[0]=(unsigned char) (value >> 8);
4086 buffer[1]=(unsigned char) value;
4087 return(WriteBlobStream(image,2,buffer));
4091 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4095 + W r i t e B l o b L S B L o n g %
4099 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4101 % WriteBlobLSBLong() writes a long value as a 32-bit quantity in
4102 % least-significant byte first order.
4104 % The format of the WriteBlobLSBLong method is:
4106 % ssize_t WriteBlobLSBLong(Image *image,const unsigned int value)
4108 % A description of each parameter follows.
4110 % o image: the image.
4112 % o value: Specifies the value to write.
4115 MagickExport ssize_t WriteBlobLSBLong(Image *image,const unsigned int value)
4120 assert(image != (Image *) NULL);
4121 assert(image->signature == MagickSignature);
4122 buffer[0]=(unsigned char) value;
4123 buffer[1]=(unsigned char) (value >> 8);
4124 buffer[2]=(unsigned char) (value >> 16);
4125 buffer[3]=(unsigned char) (value >> 24);
4126 return(WriteBlobStream(image,4,buffer));
4130 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4134 + W r i t e B l o b L S B S h o r t %
4138 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4140 % WriteBlobLSBShort() writes a long value as a 16-bit quantity in
4141 % least-significant byte first order.
4143 % The format of the WriteBlobLSBShort method is:
4145 % ssize_t WriteBlobLSBShort(Image *image,const unsigned short value)
4147 % A description of each parameter follows.
4149 % o image: the image.
4151 % o value: Specifies the value to write.
4154 MagickExport ssize_t WriteBlobLSBShort(Image *image,const unsigned short value)
4159 assert(image != (Image *) NULL);
4160 assert(image->signature == MagickSignature);
4161 buffer[0]=(unsigned char) value;
4162 buffer[1]=(unsigned char) (value >> 8);
4163 return(WriteBlobStream(image,2,buffer));
4167 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4171 + W r i t e B l o b M S B L o n g %
4175 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4177 % WriteBlobMSBLong() writes a long value as a 32-bit quantity in
4178 % most-significant byte first order.
4180 % The format of the WriteBlobMSBLong method is:
4182 % ssize_t WriteBlobMSBLong(Image *image,const unsigned int value)
4184 % A description of each parameter follows.
4186 % o value: Specifies the value to write.
4188 % o image: the image.
4191 MagickExport ssize_t WriteBlobMSBLong(Image *image,const unsigned int value)
4196 assert(image != (Image *) NULL);
4197 assert(image->signature == MagickSignature);
4198 buffer[0]=(unsigned char) (value >> 24);
4199 buffer[1]=(unsigned char) (value >> 16);
4200 buffer[2]=(unsigned char) (value >> 8);
4201 buffer[3]=(unsigned char) value;
4202 return(WriteBlobStream(image,4,buffer));
4206 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4210 + W r i t e B l o b M S B S h o r t %
4214 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4216 % WriteBlobMSBShort() writes a long value as a 16-bit quantity in
4217 % most-significant byte first order.
4219 % The format of the WriteBlobMSBShort method is:
4221 % ssize_t WriteBlobMSBShort(Image *image,const unsigned short value)
4223 % A description of each parameter follows.
4225 % o value: Specifies the value to write.
4227 % o file: Specifies the file to write the data to.
4230 MagickExport ssize_t WriteBlobMSBShort(Image *image,const unsigned short value)
4235 assert(image != (Image *) NULL);
4236 assert(image->signature == MagickSignature);
4237 buffer[0]=(unsigned char) (value >> 8);
4238 buffer[1]=(unsigned char) value;
4239 return(WriteBlobStream(image,2,buffer));
4243 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4247 + W r i t e B l o b S t r i n g %
4251 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4253 % WriteBlobString() write a string to a blob. It returns the number of
4254 % characters written.
4256 % The format of the WriteBlobString method is:
4258 % ssize_t WriteBlobString(Image *image,const char *string)
4260 % A description of each parameter follows.
4262 % o image: the image.
4264 % o string: Specifies the string to write.
4267 MagickExport ssize_t WriteBlobString(Image *image,const char *string)
4269 assert(image != (Image *) NULL);
4270 assert(image->signature == MagickSignature);
4271 assert(string != (const char *) NULL);
4272 return(WriteBlobStream(image,strlen(string),(const unsigned char *) string));