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,0,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->extent=image->blob->size;
487 image->blob->eof=MagickFalse;
488 if (image->blob->exempt != MagickFalse)
490 image->blob->type=UndefinedStream;
494 switch (image->blob->type)
496 case UndefinedStream:
502 status=ferror(image->blob->file);
507 #if defined(MAGICKCORE_ZLIB_DELEGATE)
508 (void) gzerror(image->blob->file,&status);
514 #if defined(MAGICKCORE_BZLIB_DELEGATE)
515 (void) BZ2_bzerror((BZFILE *) image->blob->file,&status);
523 image->blob->status=status < 0 ? MagickTrue : MagickFalse;
524 switch (image->blob->type)
526 case UndefinedStream:
531 if (image->blob->synchronize != MagickFalse)
532 status=fsync(fileno(image->blob->file));
533 status=fclose(image->blob->file);
538 #if defined(MAGICKCORE_HAVE_PCLOSE)
539 status=pclose(image->blob->file);
545 #if defined(MAGICKCORE_ZLIB_DELEGATE)
546 status=gzclose(image->blob->file);
552 #if defined(MAGICKCORE_BZLIB_DELEGATE)
553 BZ2_bzclose((BZFILE *) image->blob->file);
561 (void) DetachBlob(image->blob);
562 image->blob->status=status < 0 ? MagickTrue : MagickFalse;
563 return(image->blob->status);
567 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
571 + D e s t r o y B l o b %
575 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
577 % DestroyBlob() deallocates memory associated with a blob.
579 % The format of the DestroyBlob method is:
581 % void DestroyBlob(Image *image)
583 % A description of each parameter follows:
585 % o image: the image.
588 MagickExport void DestroyBlob(Image *image)
593 assert(image != (Image *) NULL);
594 assert(image->signature == MagickSignature);
595 if (image->debug != MagickFalse)
596 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
597 assert(image->blob != (BlobInfo *) NULL);
598 assert(image->blob->signature == MagickSignature);
600 LockSemaphoreInfo(image->blob->semaphore);
601 image->blob->reference_count--;
602 assert(image->blob->reference_count >= 0);
603 if (image->blob->reference_count == 0)
605 UnlockSemaphoreInfo(image->blob->semaphore);
606 if (destroy == MagickFalse)
608 (void) CloseBlob(image);
609 if (image->blob->mapped != MagickFalse)
610 (void) UnmapBlob(image->blob->data,image->blob->length);
611 if (image->blob->semaphore != (SemaphoreInfo *) NULL)
612 DestroySemaphoreInfo(&image->blob->semaphore);
613 image->blob->signature=(~MagickSignature);
614 image->blob=(BlobInfo *) RelinquishMagickMemory(image->blob);
618 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
622 + D e t a c h B l o b %
626 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
628 % DetachBlob() detaches a blob from the BlobInfo structure.
630 % The format of the DetachBlob method is:
632 % unsigned char *DetachBlob(BlobInfo *blob_info)
634 % A description of each parameter follows:
636 % o blob_info: Specifies a pointer to a BlobInfo structure.
639 MagickExport unsigned char *DetachBlob(BlobInfo *blob_info)
644 assert(blob_info != (BlobInfo *) NULL);
645 if (blob_info->debug != MagickFalse)
646 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
647 if (blob_info->mapped != MagickFalse)
648 (void) UnmapBlob(blob_info->data,blob_info->length);
649 blob_info->mapped=MagickFalse;
652 blob_info->eof=MagickFalse;
653 blob_info->exempt=MagickFalse;
654 blob_info->type=UndefinedStream;
655 blob_info->file=(FILE *) NULL;
656 data=blob_info->data;
657 blob_info->data=(unsigned char *) NULL;
658 blob_info->stream=(StreamHandler) NULL;
663 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
667 + D u p l i c a t e s B l o b %
671 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
673 % DuplicateBlob() duplicates a blob descriptor.
675 % The format of the DuplicateBlob method is:
677 % void DuplicateBlob(Image *image,const Image *duplicate)
679 % A description of each parameter follows:
681 % o image: the image.
683 % o duplicate: the duplicate image.
686 MagickExport void DuplicateBlob(Image *image,const Image *duplicate)
688 assert(image != (Image *) NULL);
689 assert(image->signature == MagickSignature);
690 if (image->debug != MagickFalse)
691 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
692 assert(duplicate != (Image *) NULL);
693 assert(duplicate->signature == MagickSignature);
695 image->blob=ReferenceBlob(duplicate->blob);
699 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
707 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
709 % EOFBlob() returns a non-zero value when EOF has been detected reading from
712 % The format of the EOFBlob method is:
714 % int EOFBlob(const Image *image)
716 % A description of each parameter follows:
718 % o image: the image.
721 MagickExport int EOFBlob(const Image *image)
723 assert(image != (Image *) NULL);
724 assert(image->signature == MagickSignature);
725 if (image->debug != MagickFalse)
726 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
727 assert(image->blob != (BlobInfo *) NULL);
728 assert(image->blob->type != UndefinedStream);
729 switch (image->blob->type)
731 case UndefinedStream:
737 image->blob->eof=feof(image->blob->file) != 0 ? MagickTrue : MagickFalse;
742 image->blob->eof=MagickFalse;
747 #if defined(MAGICKCORE_BZLIB_DELEGATE)
752 (void) BZ2_bzerror((BZFILE *) image->blob->file,&status);
753 image->blob->eof=status == BZ_UNEXPECTED_EOF ? MagickTrue : MagickFalse;
759 image->blob->eof=MagickFalse;
765 return((int) image->blob->eof);
769 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
773 + F i l e T o B l o b %
777 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
779 % FileToBlob() returns the contents of a file as a blob. It returns the
780 % file as a blob and its length. If an error occurs, NULL is returned.
782 % The format of the FileToBlob method is:
784 % unsigned char *FileToBlob(const char *filename,const size_t extent,
785 % size_t *length,ExceptionInfo *exception)
787 % A description of each parameter follows:
789 % o blob: FileToBlob() returns the contents of a file as a blob. If
790 % an error occurs NULL is returned.
792 % o filename: the filename.
794 % o extent: The maximum length of the blob.
796 % o length: On return, this reflects the actual length of the blob.
798 % o exception: return any errors or warnings in this structure.
801 MagickExport unsigned char *FileToBlob(const char *filename,const size_t extent,
802 size_t *length,ExceptionInfo *exception)
822 assert(filename != (const char *) NULL);
823 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",filename);
824 assert(exception != (ExceptionInfo *) NULL);
827 if (LocaleCompare(filename,"-") != 0)
828 file=open(filename,O_RDONLY | O_BINARY);
831 ThrowFileException(exception,BlobError,"UnableToOpenFile",filename);
832 return((unsigned char *) NULL);
834 offset=(MagickOffsetType) MagickSeek(file,0,SEEK_END);
836 if ((offset < 0) || (offset != (MagickOffsetType) ((ssize_t) offset)))
845 Stream is not seekable.
847 quantum=(size_t) MagickMaxBufferExtent;
848 if ((fstat(file,&file_info) == 0) && (file_info.st_size != 0))
849 quantum=MagickMin((size_t) file_info.st_size,MagickMaxBufferExtent);
850 blob=(unsigned char *) AcquireQuantumMemory(quantum,sizeof(*blob));
851 for (i=0; blob != (unsigned char *) NULL; i+=count)
853 count=(ssize_t) read(file,blob+i,quantum);
860 if (~(1UL*i) < (quantum+1))
862 blob=(unsigned char *) RelinquishMagickMemory(blob);
865 blob=(unsigned char *) ResizeQuantumMemory(blob,i+quantum+1,
867 if ((size_t) (i+count) >= extent)
871 if (blob == (unsigned char *) NULL)
873 (void) ThrowMagickException(exception,GetMagickModule(),
874 ResourceLimitError,"MemoryAllocationFailed","`%s'",filename);
875 return((unsigned char *) NULL);
877 *length=MagickMin(i+count,extent);
881 *length=MagickMin((size_t) offset,extent);
882 blob=(unsigned char *) NULL;
883 if (~(*length) >= MaxTextExtent)
884 blob=(unsigned char *) AcquireQuantumMemory(*length+MaxTextExtent,
886 if (blob == (unsigned char *) NULL)
889 (void) ThrowMagickException(exception,GetMagickModule(),
890 ResourceLimitError,"MemoryAllocationFailed","`%s'",filename);
891 return((unsigned char *) NULL);
893 map=MapBlob(file,ReadMode,0,*length);
894 if (map != (unsigned char *) NULL)
896 (void) CopyMagickMemory(blob,map,*length);
897 (void) UnmapBlob(map,*length);
901 (void) MagickSeek(file,0,SEEK_SET);
902 for (i=0; i < *length; i+=count)
904 count=(ssize_t) read(file,blob+i,MagickMin(*length-i,(size_t)
916 blob=(unsigned char *) RelinquishMagickMemory(blob);
917 ThrowFileException(exception,BlobError,"UnableToReadBlob",filename);
918 return((unsigned char *) NULL);
927 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
931 % F i l e T o I m a g e %
935 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
937 % FileToImage() write the contents of a file to an image.
939 % The format of the FileToImage method is:
941 % MagickBooleanType FileToImage(Image *,const char *filename)
943 % A description of each parameter follows:
945 % o image: the image.
947 % o filename: the filename.
951 static inline ssize_t WriteBlobStream(Image *image,const size_t length,
952 const unsigned char *data)
957 register unsigned char
960 assert(image->blob != (BlobInfo *) NULL);
961 if (image->blob->type != BlobStream)
962 return(WriteBlob(image,length,data));
963 assert(image->blob->type != UndefinedStream);
964 assert(data != (void *) NULL);
965 extent=(MagickSizeType) (image->blob->offset+(MagickOffsetType) length);
966 if (extent >= image->blob->extent)
968 image->blob->quantum<<=1;
969 extent=image->blob->extent+image->blob->quantum+length;
970 if (SetBlobExtent(image,extent) == MagickFalse)
973 q=image->blob->data+image->blob->offset;
974 (void) CopyMagickMemory(q,data,length);
975 image->blob->offset+=length;
976 if (image->blob->offset >= (MagickOffsetType) image->blob->length)
977 image->blob->length=(size_t) image->blob->offset;
978 return((ssize_t) length);
981 MagickExport MagickBooleanType FileToImage(Image *image,const char *filename)
999 assert(image != (const Image *) NULL);
1000 assert(image->signature == MagickSignature);
1001 assert(filename != (const char *) NULL);
1002 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",filename);
1003 file=open(filename,O_RDONLY | O_BINARY);
1006 ThrowFileException(&image->exception,BlobError,"UnableToOpenBlob",
1008 return(MagickFalse);
1010 quantum=(size_t) MagickMaxBufferExtent;
1011 if ((fstat(file,&file_info) == 0) && (file_info.st_size != 0))
1012 quantum=MagickMin((size_t) file_info.st_size,MagickMaxBufferExtent);
1013 blob=(unsigned char *) AcquireQuantumMemory(quantum,sizeof(*blob));
1014 if (blob == (unsigned char *) NULL)
1016 ThrowFileException(&image->exception,ResourceLimitError,
1017 "MemoryAllocationFailed",filename);
1018 return(MagickFalse);
1022 count=(ssize_t) read(file,blob,quantum);
1029 length=(size_t) count;
1030 count=WriteBlobStream(image,length,blob);
1031 if (count != (ssize_t) length)
1033 ThrowFileException(&image->exception,BlobError,"UnableToWriteBlob",
1039 blob=(unsigned char *) RelinquishMagickMemory(blob);
1044 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1048 + G e t B l o b E r r o r %
1052 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1054 % GetBlobError() returns MagickTrue if the blob associated with the specified
1055 % image encountered an error.
1057 % The format of the GetBlobError method is:
1059 % MagickBooleanType GetBlobError(const Image *image)
1061 % A description of each parameter follows:
1063 % o image: the image.
1066 MagickExport MagickBooleanType GetBlobError(const Image *image)
1068 assert(image != (const Image *) NULL);
1069 assert(image->signature == MagickSignature);
1070 if (image->debug != MagickFalse)
1071 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1072 return(image->blob->status);
1076 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1080 + G e t B l o b F i l e H a n d l e %
1084 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1086 % GetBlobFileHandle() returns the file handle associated with the image blob.
1088 % The format of the GetBlobFile method is:
1090 % FILE *GetBlobFileHandle(const Image *image)
1092 % A description of each parameter follows:
1094 % o image: the image.
1097 MagickExport FILE *GetBlobFileHandle(const Image *image)
1099 assert(image != (const Image *) NULL);
1100 assert(image->signature == MagickSignature);
1101 return(image->blob->file);
1105 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1109 + G e t B l o b I n f o %
1113 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1115 % GetBlobInfo() initializes the BlobInfo structure.
1117 % The format of the GetBlobInfo method is:
1119 % void GetBlobInfo(BlobInfo *blob_info)
1121 % A description of each parameter follows:
1123 % o blob_info: Specifies a pointer to a BlobInfo structure.
1126 MagickExport void GetBlobInfo(BlobInfo *blob_info)
1128 assert(blob_info != (BlobInfo *) NULL);
1129 (void) ResetMagickMemory(blob_info,0,sizeof(*blob_info));
1130 blob_info->type=UndefinedStream;
1131 blob_info->quantum=(size_t) MagickMaxBlobExtent;
1132 blob_info->properties.st_mtime=time((time_t *) NULL);
1133 blob_info->properties.st_ctime=time((time_t *) NULL);
1134 blob_info->debug=IsEventLogging();
1135 blob_info->reference_count=1;
1136 blob_info->semaphore=AllocateSemaphoreInfo();
1137 blob_info->signature=MagickSignature;
1141 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1145 % G e t B l o b P r o p e r t i e s %
1149 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1151 % GetBlobProperties() returns information about an image blob.
1153 % The format of the GetBlobProperties method is:
1155 % const struct stat *GetBlobProperties(const Image *image)
1157 % A description of each parameter follows:
1159 % o image: the image.
1162 MagickExport const struct stat *GetBlobProperties(const Image *image)
1164 assert(image != (Image *) NULL);
1165 assert(image->signature == MagickSignature);
1166 if (image->debug != MagickFalse)
1167 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1168 return(&image->blob->properties);
1172 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1176 + G e t B l o b S i z e %
1180 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1182 % GetBlobSize() returns the current length of the image file or blob; zero is
1183 % returned if the size cannot be determined.
1185 % The format of the GetBlobSize method is:
1187 % MagickSizeType GetBlobSize(const Image *image)
1189 % A description of each parameter follows:
1191 % o image: the image.
1194 MagickExport MagickSizeType GetBlobSize(const Image *image)
1199 assert(image != (Image *) NULL);
1200 assert(image->signature == MagickSignature);
1201 if (image->debug != MagickFalse)
1202 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1203 assert(image->blob != (BlobInfo *) NULL);
1205 switch (image->blob->type)
1207 case UndefinedStream:
1209 extent=image->blob->size;
1214 if (fstat(fileno(image->blob->file),&image->blob->properties) == 0)
1215 extent=(MagickSizeType) image->blob->properties.st_size;
1218 case StandardStream:
1221 extent=image->blob->size;
1230 status=GetPathAttributes(image->filename,&image->blob->properties);
1231 if (status != MagickFalse)
1232 extent=(MagickSizeType) image->blob->properties.st_size;
1239 extent=(MagickSizeType) image->blob->length;
1247 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1251 + G e t B l o b S t r e a m D a t a %
1255 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1257 % GetBlobStreamData() returns the stream data for the image.
1259 % The format of the GetBlobStreamData method is:
1261 % unsigned char *GetBlobStreamData(const Image *image)
1263 % A description of each parameter follows:
1265 % o image: the image.
1268 MagickExport unsigned char *GetBlobStreamData(const Image *image)
1270 assert(image != (const Image *) NULL);
1271 assert(image->signature == MagickSignature);
1272 return(image->blob->data);
1276 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1280 + G e t B l o b S t r e a m H a n d l e r %
1284 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1286 % GetBlobStreamHandler() returns the stream handler for the image.
1288 % The format of the GetBlobStreamHandler method is:
1290 % StreamHandler GetBlobStreamHandler(const Image *image)
1292 % A description of each parameter follows:
1294 % o image: the image.
1297 MagickExport StreamHandler GetBlobStreamHandler(const Image *image)
1299 assert(image != (const Image *) NULL);
1300 assert(image->signature == MagickSignature);
1301 if (image->debug != MagickFalse)
1302 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1303 return(image->blob->stream);
1307 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1311 % I m a g e T o B l o b %
1315 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1317 % ImageToBlob() implements direct to memory image formats. It returns the
1318 % image as a blob and its length. The magick member of the ImageInfo structure
1319 % determines the format of the returned blob (GIF, JPEG, PNG, etc.)
1321 % The format of the ImageToBlob method is:
1323 % unsigned char *ImageToBlob(const ImageInfo *image_info,Image *image,
1324 % size_t *length,ExceptionInfo *exception)
1326 % A description of each parameter follows:
1328 % o image_info: the image info.
1330 % o image: the image.
1332 % o length: This pointer to a size_t integer sets the initial length of the
1333 % blob. On return, it reflects the actual length of the blob.
1335 % o exception: return any errors or warnings in this structure.
1338 MagickExport unsigned char *ImageToBlob(const ImageInfo *image_info,
1339 Image *image,size_t *length,ExceptionInfo *exception)
1353 assert(image_info != (const ImageInfo *) NULL);
1354 assert(image_info->signature == MagickSignature);
1355 if (image_info->debug != MagickFalse)
1356 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
1357 image_info->filename);
1358 assert(image != (Image *) NULL);
1359 assert(image->signature == MagickSignature);
1360 assert(exception != (ExceptionInfo *) NULL);
1362 blob=(unsigned char *) NULL;
1363 blob_info=CloneImageInfo(image_info);
1364 blob_info->adjoin=MagickFalse;
1365 (void) SetImageInfo(blob_info,1,exception);
1366 if (*blob_info->magick != '\0')
1367 (void) CopyMagickString(image->magick,blob_info->magick,MaxTextExtent);
1368 magick_info=GetMagickInfo(image->magick,exception);
1369 if (magick_info == (const MagickInfo *) NULL)
1371 (void) ThrowMagickException(exception,GetMagickModule(),
1372 MissingDelegateError,"NoDecodeDelegateForThisImageFormat","`%s'",
1376 (void) CopyMagickString(blob_info->magick,image->magick,MaxTextExtent);
1377 if (GetMagickBlobSupport(magick_info) != MagickFalse)
1380 Native blob support for this image format.
1382 blob_info->length=0;
1383 blob_info->blob=(void *) AcquireQuantumMemory(MagickMaxBlobExtent,
1384 sizeof(unsigned char));
1385 if (blob_info->blob == (void *) NULL)
1386 (void) ThrowMagickException(exception,GetMagickModule(),
1387 ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
1390 (void) CloseBlob(image);
1391 image->blob->exempt=MagickTrue;
1392 *image->filename='\0';
1393 status=WriteImage(blob_info,image);
1394 if ((status == MagickFalse) || (image->blob->length == 0))
1395 InheritException(exception,&image->exception);
1398 *length=image->blob->length;
1399 blob=DetachBlob(image->blob);
1400 blob=(unsigned char *) ResizeQuantumMemory(blob,*length,
1408 unique[MaxTextExtent];
1414 Write file to disk in blob image format.
1416 file=AcquireUniqueFileResource(unique);
1419 ThrowFileException(exception,BlobError,"UnableToWriteBlob",
1420 image_info->filename);
1424 blob_info->file=fdopen(file,"wb");
1425 if (blob_info->file != (FILE *) NULL)
1427 (void) FormatMagickString(image->filename,MaxTextExtent,"%s:%s",
1428 image->magick,unique);
1429 status=WriteImage(blob_info,image);
1430 (void) fclose(blob_info->file);
1431 if (status == MagickFalse)
1432 InheritException(exception,&image->exception);
1434 blob=FileToBlob(image->filename,~0UL,length,exception);
1436 (void) RelinquishUniqueFileResource(unique);
1439 blob_info=DestroyImageInfo(blob_info);
1444 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1448 % I m a g e T o F i l e %
1452 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1454 % ImageToFile() writes an image to a file. It returns MagickFalse if an error
1455 % occurs otherwise MagickTrue.
1457 % The format of the ImageToFile method is:
1459 % MagickBooleanType ImageToFile(Image *image,char *filename,
1460 % ExceptionInfo *exception)
1462 % A description of each parameter follows:
1464 % o image: the image.
1466 % o filename: Write the image to this file.
1468 % o exception: return any errors or warnings in this structure.
1472 static inline const unsigned char *ReadBlobStream(Image *image,
1473 const size_t length,unsigned char *data,ssize_t *count)
1475 assert(count != (ssize_t *) NULL);
1476 assert(image->blob != (BlobInfo *) NULL);
1477 if (image->blob->type != BlobStream)
1479 *count=ReadBlob(image,length,data);
1482 if (image->blob->offset >= (MagickOffsetType) image->blob->length)
1485 image->blob->eof=MagickTrue;
1488 data=image->blob->data+image->blob->offset;
1489 *count=(ssize_t) MagickMin(length,(size_t) (image->blob->length-
1490 image->blob->offset));
1491 image->blob->offset+=(*count);
1492 if (*count != (ssize_t) length)
1493 image->blob->eof=MagickTrue;
1497 MagickExport MagickBooleanType ImageToFile(Image *image,char *filename,
1498 ExceptionInfo *exception)
1503 register const unsigned char
1522 assert(image != (Image *) NULL);
1523 assert(image->signature == MagickSignature);
1524 assert(image->blob != (BlobInfo *) NULL);
1525 assert(image->blob->type != UndefinedStream);
1526 if (image->debug != MagickFalse)
1527 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",filename);
1528 assert(filename != (const char *) NULL);
1529 if (*filename == '\0')
1530 file=AcquireUniqueFileResource(filename);
1532 if (LocaleCompare(filename,"-") == 0)
1533 file=fileno(stdout);
1535 file=open(filename,O_RDWR | O_CREAT | O_EXCL | O_BINARY,S_MODE);
1538 ThrowFileException(exception,BlobError,"UnableToWriteBlob",filename);
1539 return(MagickFalse);
1541 quantum=(size_t) MagickMaxBufferExtent;
1542 if ((fstat(file,&file_info) == 0) && (file_info.st_size != 0))
1543 quantum=MagickMin((size_t) file_info.st_size,MagickMaxBufferExtent);
1544 buffer=(unsigned char *) AcquireQuantumMemory(quantum,sizeof(*buffer));
1545 if (buffer == (unsigned char *) NULL)
1548 (void) ThrowMagickException(exception,GetMagickModule(),
1549 ResourceLimitError,"MemoryAllocationError","`%s'",filename);
1550 return(MagickFalse);
1553 p=ReadBlobStream(image,quantum,buffer,&count);
1554 for (i=0; count > 0; p=ReadBlobStream(image,quantum,buffer,&count))
1556 length=(size_t) count;
1557 for (i=0; i < length; i+=count)
1559 count=write(file,p+i,(size_t) (length-i));
1571 buffer=(unsigned char *) RelinquishMagickMemory(buffer);
1574 ThrowFileException(exception,BlobError,"UnableToWriteBlob",filename);
1575 return(MagickFalse);
1581 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1585 % I m a g e s T o B l o b %
1589 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1591 % ImagesToBlob() implements direct to memory image formats. It returns the
1592 % image sequence as a blob and its length. The magick member of the ImageInfo
1593 % structure determines the format of the returned blob (GIF, JPEG, PNG, etc.)
1595 % Note, some image formats do not permit multiple images to the same image
1596 % stream (e.g. JPEG). in this instance, just the first image of the
1597 % sequence is returned as a blob.
1599 % The format of the ImagesToBlob method is:
1601 % unsigned char *ImagesToBlob(const ImageInfo *image_info,Image *images,
1602 % size_t *length,ExceptionInfo *exception)
1604 % A description of each parameter follows:
1606 % o image_info: the image info.
1608 % o images: the image list.
1610 % o length: This pointer to a size_t integer sets the initial length of the
1611 % blob. On return, it reflects the actual length of the blob.
1613 % o exception: return any errors or warnings in this structure.
1616 MagickExport unsigned char *ImagesToBlob(const ImageInfo *image_info,
1617 Image *images,size_t *length,ExceptionInfo *exception)
1631 assert(image_info != (const ImageInfo *) NULL);
1632 assert(image_info->signature == MagickSignature);
1633 if (image_info->debug != MagickFalse)
1634 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
1635 image_info->filename);
1636 assert(images != (Image *) NULL);
1637 assert(images->signature == MagickSignature);
1638 assert(exception != (ExceptionInfo *) NULL);
1640 blob=(unsigned char *) NULL;
1641 blob_info=CloneImageInfo(image_info);
1642 (void) SetImageInfo(blob_info,(unsigned int) GetImageListLength(images),
1644 if (*blob_info->magick != '\0')
1645 (void) CopyMagickString(images->magick,blob_info->magick,MaxTextExtent);
1646 if (blob_info->adjoin == MagickFalse)
1648 blob_info=DestroyImageInfo(blob_info);
1649 return(ImageToBlob(image_info,images,length,exception));
1651 magick_info=GetMagickInfo(images->magick,exception);
1652 if (magick_info == (const MagickInfo *) NULL)
1654 (void) ThrowMagickException(exception,GetMagickModule(),
1655 MissingDelegateError,"NoDecodeDelegateForThisImageFormat","`%s'",
1659 (void) CopyMagickString(blob_info->magick,images->magick,MaxTextExtent);
1660 if (GetMagickBlobSupport(magick_info) != MagickFalse)
1663 Native blob support for this images format.
1665 blob_info->length=0;
1666 blob_info->blob=(void *) AcquireQuantumMemory(MagickMaxBlobExtent,
1667 sizeof(unsigned char));
1668 if (blob_info->blob == (void *) NULL)
1669 (void) ThrowMagickException(exception,GetMagickModule(),
1670 ResourceLimitError,"MemoryAllocationFailed","`%s'",images->filename);
1673 images->blob->exempt=MagickTrue;
1674 *images->filename='\0';
1675 status=WriteImages(blob_info,images,images->filename,exception);
1676 if ((status == MagickFalse) || (images->blob->length == 0))
1677 InheritException(exception,&images->exception);
1680 *length=images->blob->length;
1681 blob=DetachBlob(images->blob);
1682 blob=(unsigned char *) ResizeQuantumMemory(blob,*length,
1690 filename[MaxTextExtent],
1691 unique[MaxTextExtent];
1697 Write file to disk in blob images format.
1699 file=AcquireUniqueFileResource(unique);
1702 ThrowFileException(exception,FileOpenError,"UnableToWriteBlob",
1703 image_info->filename);
1707 blob_info->file=fdopen(file,"wb");
1708 if (blob_info->file != (FILE *) NULL)
1710 (void) FormatMagickString(filename,MaxTextExtent,"%s:%s",
1711 images->magick,unique);
1712 status=WriteImages(blob_info,images,filename,exception);
1713 (void) fclose(blob_info->file);
1714 if (status == MagickFalse)
1715 InheritException(exception,&images->exception);
1717 blob=FileToBlob(images->filename,~0UL,length,exception);
1719 (void) RelinquishUniqueFileResource(unique);
1722 blob_info=DestroyImageInfo(blob_info);
1726 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1730 % I n j e c t I m a g e B l o b %
1734 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1736 % InjectImageBlob() injects the image with a copy of itself in the specified
1737 % format (e.g. inject JPEG into a PDF image).
1739 % The format of the InjectImageBlob method is:
1741 % MagickBooleanType InjectImageBlob(const ImageInfo *image_info,
1742 % Image *image,Image *inject_image,const char *format,
1743 % ExceptionInfo *exception)
1745 % A description of each parameter follows:
1747 % o image_info: the image info..
1749 % o image: the image.
1751 % o inject_image: inject into the image stream.
1753 % o format: the image format.
1755 % o exception: return any errors or warnings in this structure.
1758 MagickExport MagickBooleanType InjectImageBlob(const ImageInfo *image_info,
1759 Image *image,Image *inject_image,const char *format,ExceptionInfo *exception)
1762 filename[MaxTextExtent];
1795 Write inject image to a temporary file.
1797 assert(image_info != (ImageInfo *) NULL);
1798 assert(image_info->signature == MagickSignature);
1799 assert(image != (Image *) NULL);
1800 assert(image->signature == MagickSignature);
1801 if (image->debug != MagickFalse)
1802 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1803 assert(inject_image != (Image *) NULL);
1804 assert(inject_image->signature == MagickSignature);
1805 assert(exception != (ExceptionInfo *) NULL);
1806 unique_file=(FILE *) NULL;
1807 file=AcquireUniqueFileResource(filename);
1809 unique_file=fdopen(file,"wb");
1810 if ((file == -1) || (unique_file == (FILE *) NULL))
1812 (void) CopyMagickString(image->filename,filename,MaxTextExtent);
1813 ThrowFileException(exception,FileOpenError,"UnableToCreateTemporaryFile",
1815 return(MagickFalse);
1817 byte_image=CloneImage(inject_image,0,0,MagickFalse,exception);
1818 if (byte_image == (Image *) NULL)
1820 (void) fclose(unique_file);
1821 (void) RelinquishUniqueFileResource(filename);
1822 return(MagickFalse);
1824 (void) FormatMagickString(byte_image->filename,MaxTextExtent,"%s:%s",format,
1826 DestroyBlob(byte_image);
1827 byte_image->blob=CloneBlobInfo((BlobInfo *) NULL);
1828 write_info=CloneImageInfo(image_info);
1829 SetImageInfoFile(write_info,unique_file);
1830 status=WriteImage(write_info,byte_image);
1831 write_info=DestroyImageInfo(write_info);
1832 byte_image=DestroyImage(byte_image);
1833 (void) fclose(unique_file);
1834 if (status == MagickFalse)
1836 (void) RelinquishUniqueFileResource(filename);
1837 return(MagickFalse);
1840 Inject into image stream.
1842 file=open(filename,O_RDONLY | O_BINARY);
1845 (void) RelinquishUniqueFileResource(filename);
1846 ThrowFileException(exception,FileOpenError,"UnableToOpenFile",
1847 image_info->filename);
1848 return(MagickFalse);
1850 quantum=(size_t) MagickMaxBufferExtent;
1851 if ((fstat(file,&file_info) == 0) && (file_info.st_size != 0))
1852 quantum=MagickMin((size_t) file_info.st_size,MagickMaxBufferExtent);
1853 buffer=(unsigned char *) AcquireQuantumMemory(quantum,sizeof(*buffer));
1854 if (buffer == (unsigned char *) NULL)
1856 (void) RelinquishUniqueFileResource(filename);
1857 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
1860 for (i=0; ; i+=count)
1862 count=(ssize_t) read(file,buffer,quantum);
1869 status=WriteBlobStream(image,(size_t) count,buffer) == count ? MagickTrue :
1873 (void) RelinquishUniqueFileResource(filename);
1874 buffer=(unsigned char *) RelinquishMagickMemory(buffer);
1879 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1883 + I s B l o b E x e m p t %
1887 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1889 % IsBlobExempt() returns true if the blob is exempt.
1891 % The format of the IsBlobExempt method is:
1893 % MagickBooleanType IsBlobExempt(const Image *image)
1895 % A description of each parameter follows:
1897 % o image: the image.
1900 MagickExport MagickBooleanType IsBlobExempt(const Image *image)
1902 assert(image != (const Image *) NULL);
1903 assert(image->signature == MagickSignature);
1904 if (image->debug != MagickFalse)
1905 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1906 return(image->blob->exempt);
1910 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1914 + I s B l o b S e e k a b l e %
1918 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1920 % IsBlobSeekable() returns true if the blob is seekable.
1922 % The format of the IsBlobSeekable method is:
1924 % MagickBooleanType IsBlobSeekable(const Image *image)
1926 % A description of each parameter follows:
1928 % o image: the image.
1931 MagickExport MagickBooleanType IsBlobSeekable(const Image *image)
1936 assert(image != (const Image *) NULL);
1937 assert(image->signature == MagickSignature);
1938 if (image->debug != MagickFalse)
1939 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1940 seekable=(image->blob->type == FileStream) ||
1941 (image->blob->type == BlobStream) ? MagickTrue : MagickFalse;
1946 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1950 + I s B l o b T e m p o r a r y %
1954 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1956 % IsBlobTemporary() returns true if the blob is temporary.
1958 % The format of the IsBlobTemporary method is:
1960 % MagickBooleanType IsBlobTemporary(const Image *image)
1962 % A description of each parameter follows:
1964 % o image: the image.
1967 MagickExport MagickBooleanType IsBlobTemporary(const Image *image)
1969 assert(image != (const Image *) NULL);
1970 assert(image->signature == MagickSignature);
1971 if (image->debug != MagickFalse)
1972 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1973 return(image->blob->temporary);
1977 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1985 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1987 % MapBlob() creates a mapping from a file to a binary large object.
1989 % The format of the MapBlob method is:
1991 % unsigned char *MapBlob(int file,const MapMode mode,
1992 % const MagickOffsetType offset,const size_t length)
1994 % A description of each parameter follows:
1996 % o file: map this file descriptor.
1998 % o mode: ReadMode, WriteMode, or IOMode.
2000 % o offset: starting at this offset within the file.
2002 % o length: the length of the mapping is returned in this pointer.
2005 MagickExport unsigned char *MapBlob(int file,const MapMode mode,
2006 const MagickOffsetType offset,const size_t length)
2008 #if defined(MAGICKCORE_HAVE_MMAP_FILEIO)
2021 #if defined(MAP_ANONYMOUS)
2022 flags|=MAP_ANONYMOUS;
2024 return((unsigned char *) NULL);
2031 protection=PROT_READ;
2033 map=(unsigned char *) mmap((char *) NULL,length,protection,flags,file,
2039 protection=PROT_WRITE;
2041 map=(unsigned char *) mmap((char *) NULL,length,protection,flags,file,
2047 protection=PROT_READ | PROT_WRITE;
2049 map=(unsigned char *) mmap((char *) NULL,length,protection,flags,file,
2054 if (map == (unsigned char *) MAP_FAILED)
2055 return((unsigned char *) NULL);
2062 return((unsigned char *) NULL);
2067 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2071 + M S B O r d e r L o n g %
2075 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2077 % MSBOrderLong() converts a least-significant byte first buffer of integers to
2078 % most-significant byte first.
2080 % The format of the MSBOrderLong method is:
2082 % void MSBOrderLong(unsigned char *buffer,const size_t length)
2084 % A description of each parameter follows.
2086 % o buffer: Specifies a pointer to a buffer of integers.
2088 % o length: Specifies the length of the buffer.
2091 MagickExport void MSBOrderLong(unsigned char *buffer,const size_t length)
2096 register unsigned char
2100 assert(buffer != (unsigned char *) NULL);
2107 *buffer++=(unsigned char) c;
2111 *buffer++=(unsigned char) c;
2117 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2121 + M S B O r d e r S h o r t %
2125 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2127 % MSBOrderShort() converts a least-significant byte first buffer of integers
2128 % to most-significant byte first.
2130 % The format of the MSBOrderShort method is:
2132 % void MSBOrderShort(unsigned char *p,const size_t length)
2134 % A description of each parameter follows.
2136 % o p: Specifies a pointer to a buffer of integers.
2138 % o length: Specifies the length of the buffer.
2141 MagickExport void MSBOrderShort(unsigned char *p,const size_t length)
2146 register unsigned char
2149 assert(p != (unsigned char *) NULL);
2156 *p++=(unsigned char) c;
2161 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2169 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2171 % OpenBlob() opens a file associated with the image. A file name of '-' sets
2172 % the file to stdin for type 'r' and stdout for type 'w'. If the filename
2173 % suffix is '.gz' or '.Z', the image is decompressed for type 'r' and
2174 % compressed for type 'w'. If the filename prefix is '|', it is piped to or
2175 % from a system command.
2177 % The format of the OpenBlob method is:
2179 % MagickBooleanType OpenBlob(const ImageInfo *image_info,Image *image,
2180 % const BlobMode mode,ExceptionInfo *exception)
2182 % A description of each parameter follows:
2184 % o image_info: the image info.
2186 % o image: the image.
2188 % o mode: the mode for opening the file.
2191 MagickExport MagickBooleanType OpenBlob(const ImageInfo *image_info,
2192 Image *image,const BlobMode mode,ExceptionInfo *exception)
2195 filename[MaxTextExtent];
2206 assert(image_info != (ImageInfo *) NULL);
2207 assert(image_info->signature == MagickSignature);
2208 if (image_info->debug != MagickFalse)
2209 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
2210 image_info->filename);
2211 assert(image != (Image *) NULL);
2212 assert(image->signature == MagickSignature);
2213 if (image_info->blob != (void *) NULL)
2215 if (image_info->stream != (StreamHandler) NULL)
2216 image->blob->stream=(StreamHandler) image_info->stream;
2217 AttachBlob(image->blob,image_info->blob,image_info->length);
2220 (void) DetachBlob(image->blob);
2223 default: type="r"; break;
2224 case ReadBlobMode: type="r"; break;
2225 case ReadBinaryBlobMode: type="rb"; break;
2226 case WriteBlobMode: type="w"; break;
2227 case WriteBinaryBlobMode: type="w+b"; break;
2228 case AppendBlobMode: type="a"; break;
2229 case AppendBinaryBlobMode: type="a+b"; break;
2232 image->blob->synchronize=image_info->synchronize;
2233 if (image_info->stream != (StreamHandler) NULL)
2235 image->blob->stream=(StreamHandler) image_info->stream;
2238 image->blob->type=FifoStream;
2246 (void) CopyMagickString(filename,image->filename,MaxTextExtent);
2247 rights=ReadPolicyRights;
2249 rights=WritePolicyRights;
2250 if (IsRightsAuthorized(PathPolicyDomain,rights,filename) == MagickFalse)
2253 (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
2254 "NotAuthorized","`%s'",filename);
2255 return(MagickFalse);
2257 if ((LocaleCompare(filename,"-") == 0) ||
2258 ((*filename == '\0') && (image_info->file == (FILE *) NULL)))
2260 image->blob->file=(*type == 'r') ? stdin : stdout;
2261 #if defined(__WINDOWS__) || defined(__OS2__)
2262 if (strchr(type,'b') != (char *) NULL)
2263 setmode(_fileno(image->blob->file),_O_BINARY);
2265 image->blob->type=StandardStream;
2266 image->blob->exempt=MagickTrue;
2269 if (LocaleNCompare(filename,"fd:",3) == 0)
2272 mode[MaxTextExtent];
2276 image->blob->file=fdopen(StringToLong(filename+3),mode);
2277 #if defined(__WINDOWS__) || defined(__OS2__)
2278 if (strchr(type,'b') != (char *) NULL)
2279 setmode(_fileno(image->blob->file),_O_BINARY);
2281 image->blob->type=StandardStream;
2282 image->blob->exempt=MagickTrue;
2285 #if defined(MAGICKCORE_HAVE_POPEN)
2286 if (*filename == '|')
2289 mode[MaxTextExtent];
2292 Pipe image to or from a system command.
2294 #if defined(SIGPIPE)
2296 (void) signal(SIGPIPE,SIG_IGN);
2300 image->blob->file=(FILE *) popen(filename+1,mode);
2301 if (image->blob->file == (FILE *) NULL)
2303 ThrowFileException(exception,BlobError,"UnableToOpenBlob",filename);
2304 return(MagickFalse);
2306 image->blob->type=PipeStream;
2307 image->blob->exempt=MagickTrue;
2311 status=GetPathAttributes(filename,&image->blob->properties);
2312 #if defined(S_ISFIFO)
2313 if ((status == MagickTrue) && S_ISFIFO(image->blob->properties.st_mode))
2315 image->blob->file=(FILE *) OpenMagickStream(filename,type);
2316 if (image->blob->file == (FILE *) NULL)
2318 ThrowFileException(exception,BlobError,"UnableToOpenBlob",filename);
2319 return(MagickFalse);
2321 image->blob->type=FileStream;
2322 image->blob->exempt=MagickTrue;
2328 (void) CopyMagickString(filename,image->filename,MaxTextExtent);
2329 if (image_info->adjoin == MagickFalse)
2332 Form filename for multi-part images.
2334 (void) InterpretImageFilename(image_info,image,image->filename,(int)
2335 image->scene,filename);
2336 if ((LocaleCompare(filename,image->filename) == 0) &&
2337 ((GetPreviousImageInList(image) != (Image *) NULL) ||
2338 (GetNextImageInList(image) != (Image *) NULL)))
2341 extension[MaxTextExtent],
2342 path[MaxTextExtent];
2344 GetPathComponent(image->filename,RootPath,path);
2345 GetPathComponent(image->filename,ExtensionPath,extension);
2346 if (*extension == '\0')
2347 (void) FormatMagickString(filename,MaxTextExtent,"%s-%lu",
2350 (void) FormatMagickString(filename,MaxTextExtent,
2351 "%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 L o n g L o n g %
3181 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3183 % ReadBlobMSBLongLong() reads a long value as a 64-bit quantity in
3184 % most-significant byte first order.
3186 % The format of the ReadBlobMSBLongLong method is:
3188 % unsigned int ReadBlobMSBLongLong(Image *image)
3190 % A description of each parameter follows.
3192 % o image: the image.
3195 MagickExport MagickSizeType ReadBlobMSBLongLong(Image *image)
3197 register const unsigned char
3200 register MagickSizeType
3209 assert(image != (Image *) NULL);
3210 assert(image->signature == MagickSignature);
3212 p=ReadBlobStream(image,8,buffer,&count);
3214 return(MagickULLConstant(0));
3215 value=((MagickSizeType) (*p++)) << 56;
3216 value|=((MagickSizeType) (*p++)) << 48;
3217 value|=((MagickSizeType) (*p++)) << 40;
3218 value|=((MagickSizeType) (*p++)) << 32;
3219 value|=((MagickSizeType) (*p++)) << 24;
3220 value|=((MagickSizeType) (*p++)) << 16;
3221 value|=((MagickSizeType) (*p++)) << 8;
3222 value|=((MagickSizeType) (*p++));
3223 return(value & MagickULLConstant(0xffffffffffffffff));
3227 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3231 + R e a d B l o b M S B S h o r t %
3235 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3237 % ReadBlobMSBShort() reads a short value as a 16-bit quantity in
3238 % most-significant byte first order.
3240 % The format of the ReadBlobMSBShort method is:
3242 % unsigned short ReadBlobMSBShort(Image *image)
3244 % A description of each parameter follows.
3246 % o image: the image.
3249 MagickExport unsigned short ReadBlobMSBShort(Image *image)
3251 register const unsigned char
3254 register unsigned int
3263 assert(image != (Image *) NULL);
3264 assert(image->signature == MagickSignature);
3266 p=ReadBlobStream(image,2,buffer,&count);
3268 return((unsigned short) 0U);
3269 value=(unsigned int) ((*p++) << 8);
3270 value|=(unsigned int) (*p++);
3271 return((unsigned short) (value & 0xffff));
3275 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3279 + R e a d B l o b S t r i n g %
3283 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3285 % ReadBlobString() reads characters from a blob or file until a newline
3286 % character is read or an end-of-file condition is encountered.
3288 % The format of the ReadBlobString method is:
3290 % char *ReadBlobString(Image *image,char *string)
3292 % A description of each parameter follows:
3294 % o image: the image.
3296 % o string: the address of a character buffer.
3299 MagickExport char *ReadBlobString(Image *image,char *string)
3301 register const unsigned char
3313 assert(image != (Image *) NULL);
3314 assert(image->signature == MagickSignature);
3315 for (i=0; i < (MaxTextExtent-1L); i++)
3317 p=ReadBlobStream(image,1,buffer,&count);
3321 return((char *) NULL);
3324 string[i]=(char) (*p);
3325 if ((string[i] == '\n') || (string[i] == '\r'))
3333 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3337 + R e f e r e n c e B l o b %
3341 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3343 % ReferenceBlob() increments the reference count associated with the pixel
3344 % blob returning a pointer to the blob.
3346 % The format of the ReferenceBlob method is:
3348 % BlobInfo ReferenceBlob(BlobInfo *blob_info)
3350 % A description of each parameter follows:
3352 % o blob_info: the blob_info.
3355 MagickExport BlobInfo *ReferenceBlob(BlobInfo *blob)
3357 assert(blob != (BlobInfo *) NULL);
3358 assert(blob->signature == MagickSignature);
3359 if (blob->debug != MagickFalse)
3360 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
3361 LockSemaphoreInfo(blob->semaphore);
3362 blob->reference_count++;
3363 UnlockSemaphoreInfo(blob->semaphore);
3368 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3376 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3378 % SeekBlob() sets the offset in bytes from the beginning of a blob or file
3379 % and returns the resulting offset.
3381 % The format of the SeekBlob method is:
3383 % MagickOffsetType SeekBlob(Image *image,const MagickOffsetType offset,
3386 % A description of each parameter follows:
3388 % o image: the image.
3390 % o offset: Specifies an integer representing the offset in bytes.
3392 % o whence: Specifies an integer representing how the offset is
3393 % treated relative to the beginning of the blob as follows:
3395 % SEEK_SET Set position equal to offset bytes.
3396 % SEEK_CUR Set position to current location plus offset.
3397 % SEEK_END Set position to EOF plus offset.
3400 MagickExport MagickOffsetType SeekBlob(Image *image,
3401 const MagickOffsetType offset,const int whence)
3403 assert(image != (Image *) NULL);
3404 assert(image->signature == MagickSignature);
3405 if (image->debug != MagickFalse)
3406 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3407 assert(image->blob != (BlobInfo *) NULL);
3408 assert(image->blob->type != UndefinedStream);
3409 switch (image->blob->type)
3411 case UndefinedStream:
3415 if (fseek(image->blob->file,offset,whence) < 0)
3417 image->blob->offset=TellBlob(image);
3420 case StandardStream:
3424 #if defined(MAGICKCORE_ZLIB_DELEGATE)
3425 if (gzseek(image->blob->file,(off_t) offset,whence) < 0)
3428 image->blob->offset=TellBlob(image);
3444 image->blob->offset=offset;
3449 if ((image->blob->offset+offset) < 0)
3451 image->blob->offset+=offset;
3456 if (((MagickOffsetType) image->blob->length+offset) < 0)
3458 image->blob->offset=image->blob->length+offset;
3462 if (image->blob->offset <= (MagickOffsetType)
3463 ((off_t) image->blob->length))
3464 image->blob->eof=MagickFalse;
3466 if (image->blob->mapped != MagickFalse)
3470 image->blob->extent=(size_t) (image->blob->offset+
3471 image->blob->quantum);
3472 image->blob->data=(unsigned char *) ResizeQuantumMemory(
3473 image->blob->data,image->blob->extent+1,
3474 sizeof(*image->blob->data));
3475 (void) SyncBlob(image);
3476 if (image->blob->data == (unsigned char *) NULL)
3478 (void) DetachBlob(image->blob);
3485 return(image->blob->offset);
3489 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3493 + S e t B l o b E x e m p t %
3497 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3499 % SetBlobExempt() sets the blob exempt status.
3501 % The format of the SetBlobExempt method is:
3503 % MagickBooleanType SetBlobExempt(const Image *image,
3504 % const MagickBooleanType exempt)
3506 % A description of each parameter follows:
3508 % o image: the image.
3510 % o exempt: Set to true if this blob is exempt from being closed.
3513 MagickExport void SetBlobExempt(Image *image,const MagickBooleanType exempt)
3515 assert(image != (const Image *) NULL);
3516 assert(image->signature == MagickSignature);
3517 if (image->debug != MagickFalse)
3518 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3519 image->blob->exempt=exempt;
3523 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3527 + S e t B l o b E x t e n t %
3531 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3533 % SetBlobExtent() ensures enough space is allocated for the blob. If the
3534 % method is successful, subsequent writes to bytes in the specified range are
3535 % guaranteed not to fail.
3537 % The format of the SetBlobExtent method is:
3539 % MagickBooleanType SetBlobExtent(Image *image,const MagickSizeType extent)
3541 % A description of each parameter follows:
3543 % o image: the image.
3545 % o extent: the blob maximum extent.
3548 MagickExport MagickBooleanType SetBlobExtent(Image *image,
3549 const MagickSizeType extent)
3551 assert(image != (Image *) NULL);
3552 assert(image->signature == MagickSignature);
3553 if (image->debug != MagickFalse)
3554 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3555 assert(image->blob != (BlobInfo *) NULL);
3556 assert(image->blob->type != UndefinedStream);
3557 switch (image->blob->type)
3559 case UndefinedStream:
3563 if (extent != (MagickSizeType) ((off_t) extent))
3564 return(MagickFalse);
3565 #if !defined(MAGICKCORE_POSIX_FALLOCATE)
3566 return(MagickFalse);
3575 offset=TellBlob(image);
3576 status=posix_fallocate(fileno(image->blob->file),(off_t) offset,
3577 (off_t) (extent-offset));
3579 return(MagickFalse);
3584 case StandardStream:
3587 return(MagickFalse);
3589 return(MagickFalse);
3591 return(MagickFalse);
3594 if (image->blob->mapped != MagickFalse)
3596 if (image->blob->file == (FILE *) NULL)
3597 return(MagickFalse);
3598 (void) UnmapBlob(image->blob->data,image->blob->length);
3599 #if !defined(MAGICKCORE_POSIX_FALLOCATE)
3600 return(MagickFalse);
3609 offset=TellBlob(image);
3610 status=posix_fallocate(fileno(image->blob->file),(off_t) offset,
3611 (off_t) (extent-offset));
3613 return(MagickFalse);
3615 image->blob->data=(unsigned char*) MapBlob(fileno(image->blob->file),
3616 WriteMode,0,(size_t) extent);
3617 image->blob->extent=(size_t) extent;
3618 image->blob->length=(size_t) extent;
3619 (void) SyncBlob(image);
3623 if (extent != (MagickSizeType) ((size_t) extent))
3624 return(MagickFalse);
3625 image->blob->extent=(size_t) extent;
3626 image->blob->data=(unsigned char *) ResizeQuantumMemory(image->blob->data,
3627 image->blob->extent+1,sizeof(*image->blob->data));
3628 (void) SyncBlob(image);
3629 if (image->blob->data == (unsigned char *) NULL)
3631 (void) DetachBlob(image->blob);
3632 return(MagickFalse);
3641 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3649 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3651 % SyncBlob() flushes the datastream if it is a file or synchronizes the data
3652 % attributes if it is an blob.
3654 % The format of the SyncBlob method is:
3656 % int SyncBlob(Image *image)
3658 % A description of each parameter follows:
3660 % o image: the image.
3663 static int SyncBlob(Image *image)
3668 assert(image != (Image *) NULL);
3669 assert(image->signature == MagickSignature);
3670 if (image->debug != MagickFalse)
3671 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3672 assert(image->blob != (BlobInfo *) NULL);
3673 assert(image->blob->type != UndefinedStream);
3675 switch (image->blob->type)
3677 case UndefinedStream:
3680 case StandardStream:
3683 status=fflush(image->blob->file);
3688 #if defined(MAGICKCORE_ZLIB_DELEGATE)
3689 status=gzflush(image->blob->file,Z_SYNC_FLUSH);
3695 #if defined(MAGICKCORE_BZLIB_DELEGATE)
3696 status=BZ2_bzflush((BZFILE *) image->blob->file);
3704 #if defined(MAGICKCORE_HAVE_MMAP_FILEIO)
3705 if (image->blob->mapped != MagickFalse)
3706 status=msync(image->blob->data,image->blob->length,MS_SYNC);
3715 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3723 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3725 % TellBlob() obtains the current value of the blob or file position.
3727 % The format of the TellBlob method is:
3729 % MagickOffsetType TellBlob(const Image *image)
3731 % A description of each parameter follows:
3733 % o image: the image.
3736 MagickExport MagickOffsetType TellBlob(const Image *image)
3741 assert(image != (Image *) NULL);
3742 assert(image->signature == MagickSignature);
3743 if (image->debug != MagickFalse)
3744 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3745 assert(image->blob != (BlobInfo *) NULL);
3746 assert(image->blob->type != UndefinedStream);
3748 switch (image->blob->type)
3750 case UndefinedStream:
3754 offset=ftell(image->blob->file);
3757 case StandardStream:
3762 #if defined(MAGICKCORE_ZLIB_DELEGATE)
3763 offset=(MagickOffsetType) gztell(image->blob->file);
3773 offset=image->blob->offset;
3781 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3785 + U n m a p B l o b %
3789 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3791 % UnmapBlob() deallocates the binary large object previously allocated with
3792 % the MapBlob method.
3794 % The format of the UnmapBlob method is:
3796 % MagickBooleanType UnmapBlob(void *map,const size_t length)
3798 % A description of each parameter follows:
3800 % o map: the address of the binary large object.
3802 % o length: the length of the binary large object.
3805 MagickExport MagickBooleanType UnmapBlob(void *map,const size_t length)
3807 #if defined(MAGICKCORE_HAVE_MMAP_FILEIO)
3811 status=munmap(map,length);
3812 return(status == -1 ? MagickFalse : MagickTrue);
3816 return(MagickFalse);
3821 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3825 + W r i t e B l o b %
3829 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3831 % WriteBlob() writes data to a blob or image file. It returns the number of
3834 % The format of the WriteBlob method is:
3836 % ssize_t WriteBlob(Image *image,const size_t length,
3837 % const unsigned char *data)
3839 % A description of each parameter follows:
3841 % o image: the image.
3843 % o length: Specifies an integer representing the number of bytes to
3844 % write to the file.
3846 % o data: The address of the data to write to the blob or file.
3849 MagickExport ssize_t WriteBlob(Image *image,const size_t length,
3850 const unsigned char *data)
3855 register const unsigned char
3861 assert(image != (Image *) NULL);
3862 assert(image->signature == MagickSignature);
3863 assert(data != (const unsigned char *) NULL);
3864 assert(image->blob != (BlobInfo *) NULL);
3865 assert(image->blob->type != UndefinedStream);
3870 switch (image->blob->type)
3872 case UndefinedStream:
3875 case StandardStream:
3882 count=(ssize_t) fwrite((const char *) data,1,length,
3888 c=putc((int) *p++,image->blob->file);
3895 c=putc((int) *p++,image->blob->file);
3907 #if defined(MAGICKCORE_ZLIB_DELEGATE)
3912 count=(ssize_t) gzwrite(image->blob->file,(void *) data,
3913 (unsigned int) length);
3918 c=gzputc(image->blob->file,(int) *p++);
3925 c=gzputc(image->blob->file,(int) *p++);
3938 #if defined(MAGICKCORE_BZLIB_DELEGATE)
3939 count=(ssize_t) BZ2_bzwrite((BZFILE *) image->blob->file,(void *) data,
3946 count=(ssize_t) image->blob->stream(image,data,length);
3951 register unsigned char
3954 if ((image->blob->offset+(MagickOffsetType) length) >=
3955 (MagickOffsetType) image->blob->extent)
3957 if (image->blob->mapped != MagickFalse)
3959 image->blob->quantum<<=1;
3960 image->blob->extent+=length+image->blob->quantum;
3961 image->blob->data=(unsigned char *) ResizeQuantumMemory(
3962 image->blob->data,image->blob->extent+1,sizeof(*image->blob->data));
3963 (void) SyncBlob(image);
3964 if (image->blob->data == (unsigned char *) NULL)
3966 (void) DetachBlob(image->blob);
3970 q=image->blob->data+image->blob->offset;
3971 (void) CopyMagickMemory(q,p,length);
3972 image->blob->offset+=length;
3973 if (image->blob->offset >= (MagickOffsetType) image->blob->length)
3974 image->blob->length=(size_t) image->blob->offset;
3975 count=(ssize_t) length;
3982 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3986 + W r i t e B l o b B y t e %
3990 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3992 % WriteBlobByte() write an integer to a blob. It returns the number of bytes
3993 % written (either 0 or 1);
3995 % The format of the WriteBlobByte method is:
3997 % ssize_t WriteBlobByte(Image *image,const unsigned char value)
3999 % A description of each parameter follows.
4001 % o image: the image.
4003 % o value: Specifies the value to write.
4006 MagickExport ssize_t WriteBlobByte(Image *image,const unsigned char value)
4008 assert(image != (Image *) NULL);
4009 assert(image->signature == MagickSignature);
4010 return(WriteBlobStream(image,1,&value));
4014 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4018 + W r i t e B l o b F l o a t %
4022 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4024 % WriteBlobFloat() writes a float value as a 32-bit quantity in the byte-order
4025 % specified by the endian member of the image structure.
4027 % The format of the WriteBlobFloat method is:
4029 % ssize_t WriteBlobFloat(Image *image,const float value)
4031 % A description of each parameter follows.
4033 % o image: the image.
4035 % o value: Specifies the value to write.
4038 MagickExport ssize_t WriteBlobFloat(Image *image,const float value)
4049 quantum.unsigned_value=0U;
4050 quantum.float_value=value;
4051 return(WriteBlobLong(image,quantum.unsigned_value));
4055 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4059 + W r i t e B l o b L o n g %
4063 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4065 % WriteBlobLong() writes a long value as a 32-bit quantity in the byte-order
4066 % specified by the endian member of the image structure.
4068 % The format of the WriteBlobLong method is:
4070 % ssize_t WriteBlobLong(Image *image,const unsigned int value)
4072 % A description of each parameter follows.
4074 % o image: the image.
4076 % o value: Specifies the value to write.
4079 MagickExport ssize_t WriteBlobLong(Image *image,const unsigned int value)
4084 assert(image != (Image *) NULL);
4085 assert(image->signature == MagickSignature);
4086 if (image->endian == LSBEndian)
4088 buffer[0]=(unsigned char) value;
4089 buffer[1]=(unsigned char) (value >> 8);
4090 buffer[2]=(unsigned char) (value >> 16);
4091 buffer[3]=(unsigned char) (value >> 24);
4092 return(WriteBlobStream(image,4,buffer));
4094 buffer[0]=(unsigned char) (value >> 24);
4095 buffer[1]=(unsigned char) (value >> 16);
4096 buffer[2]=(unsigned char) (value >> 8);
4097 buffer[3]=(unsigned char) value;
4098 return(WriteBlobStream(image,4,buffer));
4102 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4106 + W r i t e B l o b S h o r t %
4110 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4112 % WriteBlobShort() writes a short value as a 16-bit quantity in the
4113 % byte-order specified by the endian member of the image structure.
4115 % The format of the WriteBlobShort method is:
4117 % ssize_t WriteBlobShort(Image *image,const unsigned short value)
4119 % A description of each parameter follows.
4121 % o image: the image.
4123 % o value: Specifies the value to write.
4126 MagickExport ssize_t WriteBlobShort(Image *image,const unsigned short value)
4131 assert(image != (Image *) NULL);
4132 assert(image->signature == MagickSignature);
4133 if (image->endian == LSBEndian)
4135 buffer[0]=(unsigned char) value;
4136 buffer[1]=(unsigned char) (value >> 8);
4137 return(WriteBlobStream(image,2,buffer));
4139 buffer[0]=(unsigned char) (value >> 8);
4140 buffer[1]=(unsigned char) value;
4141 return(WriteBlobStream(image,2,buffer));
4145 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4149 + W r i t e B l o b L S B L o n g %
4153 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4155 % WriteBlobLSBLong() writes a long value as a 32-bit quantity in
4156 % least-significant byte first order.
4158 % The format of the WriteBlobLSBLong method is:
4160 % ssize_t WriteBlobLSBLong(Image *image,const unsigned int value)
4162 % A description of each parameter follows.
4164 % o image: the image.
4166 % o value: Specifies the value to write.
4169 MagickExport ssize_t WriteBlobLSBLong(Image *image,const unsigned int value)
4174 assert(image != (Image *) NULL);
4175 assert(image->signature == MagickSignature);
4176 buffer[0]=(unsigned char) value;
4177 buffer[1]=(unsigned char) (value >> 8);
4178 buffer[2]=(unsigned char) (value >> 16);
4179 buffer[3]=(unsigned char) (value >> 24);
4180 return(WriteBlobStream(image,4,buffer));
4184 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4188 + W r i t e B l o b L S B S h o r t %
4192 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4194 % WriteBlobLSBShort() writes a long value as a 16-bit quantity in
4195 % least-significant byte first order.
4197 % The format of the WriteBlobLSBShort method is:
4199 % ssize_t WriteBlobLSBShort(Image *image,const unsigned short value)
4201 % A description of each parameter follows.
4203 % o image: the image.
4205 % o value: Specifies the value to write.
4208 MagickExport ssize_t WriteBlobLSBShort(Image *image,const unsigned short value)
4213 assert(image != (Image *) NULL);
4214 assert(image->signature == MagickSignature);
4215 buffer[0]=(unsigned char) value;
4216 buffer[1]=(unsigned char) (value >> 8);
4217 return(WriteBlobStream(image,2,buffer));
4221 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4225 + W r i t e B l o b M S B L o n g %
4229 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4231 % WriteBlobMSBLong() writes a long value as a 32-bit quantity in
4232 % most-significant byte first order.
4234 % The format of the WriteBlobMSBLong method is:
4236 % ssize_t WriteBlobMSBLong(Image *image,const unsigned int value)
4238 % A description of each parameter follows.
4240 % o value: Specifies the value to write.
4242 % o image: the image.
4245 MagickExport ssize_t WriteBlobMSBLong(Image *image,const unsigned int value)
4250 assert(image != (Image *) NULL);
4251 assert(image->signature == MagickSignature);
4252 buffer[0]=(unsigned char) (value >> 24);
4253 buffer[1]=(unsigned char) (value >> 16);
4254 buffer[2]=(unsigned char) (value >> 8);
4255 buffer[3]=(unsigned char) value;
4256 return(WriteBlobStream(image,4,buffer));
4260 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4264 + W r i t e B l o b M S B S h o r t %
4268 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4270 % WriteBlobMSBShort() writes a long value as a 16-bit quantity in
4271 % most-significant byte first order.
4273 % The format of the WriteBlobMSBShort method is:
4275 % ssize_t WriteBlobMSBShort(Image *image,const unsigned short value)
4277 % A description of each parameter follows.
4279 % o value: Specifies the value to write.
4281 % o file: Specifies the file to write the data to.
4284 MagickExport ssize_t WriteBlobMSBShort(Image *image,const unsigned short value)
4289 assert(image != (Image *) NULL);
4290 assert(image->signature == MagickSignature);
4291 buffer[0]=(unsigned char) (value >> 8);
4292 buffer[1]=(unsigned char) value;
4293 return(WriteBlobStream(image,2,buffer));
4297 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4301 + W r i t e B l o b S t r i n g %
4305 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4307 % WriteBlobString() write a string to a blob. It returns the number of
4308 % characters written.
4310 % The format of the WriteBlobString method is:
4312 % ssize_t WriteBlobString(Image *image,const char *string)
4314 % A description of each parameter follows.
4316 % o image: the image.
4318 % o string: Specifies the string to write.
4321 MagickExport ssize_t WriteBlobString(Image *image,const char *string)
4323 assert(image != (Image *) NULL);
4324 assert(image->signature == MagickSignature);
4325 assert(string != (const char *) NULL);
4326 return(WriteBlobStream(image,strlen(string),(const unsigned char *) string));