2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read/Write Palm Database ImageViewer Image Format %
20 % Copyright 1999-2011 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 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
37 20071202 TS * rewrote RLE decoder - old version could cause buffer overflows
38 * failure of RLE decoding now thows error RLEDecoderError
39 * fixed bug in RLE decoding - now all rows are decoded, not just
41 * fixed bug in reader - record offsets now handled correctly
42 * fixed bug in reader - only bits 0..2 indicate compression type
43 * in writer: now using image color count instead of depth
49 #include "MagickCore/studio.h"
50 #include "MagickCore/attribute.h"
51 #include "MagickCore/blob.h"
52 #include "MagickCore/blob-private.h"
53 #include "MagickCore/cache.h"
54 #include "MagickCore/colormap-private.h"
55 #include "MagickCore/color-private.h"
56 #include "MagickCore/colormap.h"
57 #include "MagickCore/colorspace.h"
58 #include "MagickCore/colorspace-private.h"
59 #include "MagickCore/constitute.h"
60 #include "MagickCore/exception.h"
61 #include "MagickCore/exception-private.h"
62 #include "MagickCore/image.h"
63 #include "MagickCore/image-private.h"
64 #include "MagickCore/list.h"
65 #include "MagickCore/magick.h"
66 #include "MagickCore/memory_.h"
67 #include "MagickCore/monitor.h"
68 #include "MagickCore/monitor-private.h"
69 #include "MagickCore/pixel-accessor.h"
70 #include "MagickCore/property.h"
71 #include "MagickCore/quantum-private.h"
72 #include "MagickCore/quantum-private.h"
73 #include "MagickCore/static.h"
74 #include "MagickCore/string_.h"
75 #include "MagickCore/module.h"
80 typedef struct _PDBInfo
98 type[4], /* database type identifier "vIMG" */
99 id[4]; /* database creator identifier "View" */
109 typedef struct _PDBImage
134 Forward declarations.
136 static MagickBooleanType
137 WritePDBImage(const ImageInfo *,Image *,ExceptionInfo *);
140 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
144 % D e c o d e I m a g e %
148 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
150 % DecodeImage unpacks the packed image pixels into runlength-encoded
153 % The format of the DecodeImage method is:
155 % MagickBooleanType DecodeImage(Image *image,unsigned char *pixels,
156 % const size_t length)
158 % A description of each parameter follows:
160 % o image: the address of a structure of type Image.
162 % o pixels: The address of a byte (8 bits) array of pixel data created by
163 % the decoding process.
165 % o length: Number of bytes to read into buffer 'pixels'.
168 static MagickBooleanType DecodeImage(Image *image, unsigned char *pixels,
171 #define RLE_MODE_NONE -1
172 #define RLE_MODE_COPY 0
173 #define RLE_MODE_RUN 1
175 int data = 0, count = 0;
177 int mode = RLE_MODE_NONE;
179 for (p = pixels; p < pixels + length; p++) {
181 data = ReadBlobByte( image );
182 if (-1 == data) return MagickFalse;
185 count = data - 128 + 1;
186 data = ReadBlobByte( image );
187 if (-1 == data) return MagickFalse;
189 mode = RLE_MODE_COPY;
194 if (RLE_MODE_COPY == mode) {
195 data = ReadBlobByte( image );
196 if (-1 == data) return MagickFalse;
198 *p = (unsigned char)data;
205 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
213 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
215 % IsPDB() returns MagickTrue if the image format type, identified by the
216 % magick string, is PDB.
218 % The format of the ReadPDBImage method is:
220 % MagickBooleanType IsPDB(const unsigned char *magick,const size_t length)
222 % A description of each parameter follows:
224 % o magick: compare image format pattern against these bytes.
226 % o length: Specifies the length of the magick string.
229 static MagickBooleanType IsPDB(const unsigned char *magick,const size_t length)
233 if (memcmp(magick+60,"vIMGView",8) == 0)
239 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
243 % R e a d P D B I m a g e %
247 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
249 % ReadPDBImage() reads an Pilot image file and returns it. It
250 % allocates the memory necessary for the new Image structure and returns a
251 % pointer to the new image.
253 % The format of the ReadPDBImage method is:
255 % Image *ReadPDBImage(const ImageInfo *image_info,ExceptionInfo *exception)
257 % A description of each parameter follows:
259 % o image_info: the image info.
261 % o exception: return any errors or warnings in this structure.
264 static Image *ReadPDBImage(const ImageInfo *image_info,ExceptionInfo *exception)
291 register unsigned char
296 num_pad_bytes, /* TS */
312 assert(image_info != (const ImageInfo *) NULL);
313 assert(image_info->signature == MagickSignature);
314 if (image_info->debug != MagickFalse)
315 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
316 image_info->filename);
317 assert(exception != (ExceptionInfo *) NULL);
318 assert(exception->signature == MagickSignature);
319 image=AcquireImage(image_info,exception);
320 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
321 if (status == MagickFalse)
323 image=DestroyImageList(image);
324 return((Image *) NULL);
327 Determine if this a PDB image file.
329 count=ReadBlob(image,32,(unsigned char *) pdb_info.name);
330 pdb_info.attributes=(short) ReadBlobMSBShort(image);
331 pdb_info.version=(short) ReadBlobMSBShort(image);
332 pdb_info.create_time=ReadBlobMSBLong(image);
333 pdb_info.modify_time=ReadBlobMSBLong(image);
334 pdb_info.archive_time=ReadBlobMSBLong(image);
335 pdb_info.modify_number=ReadBlobMSBLong(image);
336 pdb_info.application_info=ReadBlobMSBLong(image);
337 pdb_info.sort_info=ReadBlobMSBLong(image);
338 count=ReadBlob(image,4,(unsigned char *) pdb_info.type);
339 count=ReadBlob(image,4,(unsigned char *) pdb_info.id);
340 if ((count == 0) || (memcmp(pdb_info.type,"vIMG",4) != 0) ||
341 (memcmp(pdb_info.id,"View",4) != 0))
342 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
343 pdb_info.seed=ReadBlobMSBLong(image);
344 pdb_info.next_record=ReadBlobMSBLong(image);
345 pdb_info.number_records=(short) ReadBlobMSBShort(image);
346 if (pdb_info.next_record != 0)
347 ThrowReaderException(CoderError,"MultipleRecordListNotSupported");
351 img_offset=(int) ReadBlobMSBLong(image); /* TS */
352 attributes=(unsigned char) ReadBlobByte(image);
354 count=ReadBlob(image,3,(unsigned char *) tag);
355 if (count != 3 || memcmp(tag,"\x6f\x80\x00",3) != 0)
356 ThrowReaderException(CorruptImageError,"CorruptImage");
357 if (pdb_info.number_records > 1)
359 comment_offset=(int) ReadBlobMSBLong(image);
360 attributes=(unsigned char) ReadBlobByte(image);
361 count=ReadBlob(image,3,(unsigned char *) tag);
362 if (count != 3 || memcmp(tag,"\x6f\x80\x01",3) != 0)
363 ThrowReaderException(CorruptImageError,"CorruptImage");
365 num_pad_bytes = (size_t) (img_offset - TellBlob( image ));
366 while (num_pad_bytes--) ReadBlobByte( image );
370 count=ReadBlob(image,32,(unsigned char *) pdb_image.name);
371 pdb_image.version=ReadBlobByte(image);
372 pdb_image.type=ReadBlobByte(image);
373 pdb_image.reserved_1=ReadBlobMSBLong(image);
374 pdb_image.note=ReadBlobMSBLong(image);
375 pdb_image.x_last=(short) ReadBlobMSBShort(image);
376 pdb_image.y_last=(short) ReadBlobMSBShort(image);
377 pdb_image.reserved_2=ReadBlobMSBLong(image);
378 pdb_image.x_anchor=(short) ReadBlobMSBShort(image);
379 pdb_image.y_anchor=(short) ReadBlobMSBShort(image);
380 pdb_image.width=(short) ReadBlobMSBShort(image);
381 pdb_image.height=(short) ReadBlobMSBShort(image);
383 Initialize image structure.
385 image->columns=(size_t) pdb_image.width;
386 image->rows=(size_t) pdb_image.height;
388 image->storage_class=PseudoClass;
389 bits_per_pixel=pdb_image.type == 0 ? 2UL : pdb_image.type == 2 ? 4UL : 1UL;
391 if (AcquireImageColormap(image,one << bits_per_pixel,exception) == MagickFalse)
392 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
393 if (image_info->ping != MagickFalse)
395 (void) CloseBlob(image);
396 return(GetFirstImageInList(image));
398 packets=bits_per_pixel*image->columns/8;
399 pixels=(unsigned char *) AcquireQuantumMemory(packets+256UL,image->rows*
401 if (pixels == (unsigned char *) NULL)
402 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
404 switch (pdb_image.version & 7) /* TS */
408 image->compression=NoCompression;
409 count=(ssize_t) ReadBlob(image, packets * image -> rows, pixels);
414 image->compression=RLECompression;
415 if (!DecodeImage(image, pixels, packets * image -> rows))
416 ThrowReaderException( CorruptImageError, "RLEDecoderError" ); /* TS */
420 ThrowReaderException(CorruptImageError,
421 "UnrecognizedImageCompressionType" );
424 switch (bits_per_pixel)
432 Read 1-bit PDB image.
434 for (y=0; y < (ssize_t) image->rows; y++)
436 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
437 if (q == (Quantum *) NULL)
439 for (x=0; x < ((ssize_t) image->columns-7); x+=8)
441 for (bit=0; bit < 8; bit++)
443 index=(Quantum) (*p & (0x80 >> bit) ? 0x00 : 0x01);
444 SetPixelIndex(image,index,q);
445 q+=GetPixelChannels(image);
449 if (SyncAuthenticPixels(image,exception) == MagickFalse)
451 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
453 if (status == MagickFalse)
456 (void) SyncImage(image);
462 Read 2-bit PDB image.
464 for (y=0; y < (ssize_t) image->rows; y++)
466 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
467 if (q == (Quantum *) NULL)
469 for (x=0; x < (ssize_t) image->columns; x+=4)
471 index=ConstrainColormapIndex(image,3UL-((*p >> 6) & 0x03));
472 SetPixelIndex(image,index,q);
473 q+=GetPixelChannels(image);
474 index=ConstrainColormapIndex(image,3UL-((*p >> 4) & 0x03));
475 SetPixelIndex(image,index,q);
476 q+=GetPixelChannels(image);
477 index=ConstrainColormapIndex(image,3UL-((*p >> 2) & 0x03));
478 SetPixelIndex(image,index,q);
479 q+=GetPixelChannels(image);
480 index=ConstrainColormapIndex(image,3UL-((*p) & 0x03));
481 SetPixelIndex(image,index,q);
483 q+=GetPixelChannels(image);
485 if (SyncAuthenticPixels(image,exception) == MagickFalse)
487 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
489 if (status == MagickFalse)
492 (void) SyncImage(image);
498 Read 4-bit PDB image.
500 for (y=0; y < (ssize_t) image->rows; y++)
502 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
503 if (q == (Quantum *) NULL)
505 for (x=0; x < (ssize_t) image->columns; x+=2)
507 index=ConstrainColormapIndex(image,15UL-((*p >> 4) & 0x0f));
508 SetPixelIndex(image,index,q);
509 q+=GetPixelChannels(image);
510 index=ConstrainColormapIndex(image,15UL-((*p) & 0x0f));
511 SetPixelIndex(image,index,q);
513 q+=GetPixelChannels(image);
515 if (SyncAuthenticPixels(image,exception) == MagickFalse)
517 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
519 if (status == MagickFalse)
522 (void) SyncImage(image);
526 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
529 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
530 if (EOFBlob(image) != MagickFalse)
531 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
533 if (pdb_info.number_records > 1) /* TS */
547 num_pad_bytes = (size_t) (comment_offset - TellBlob( image ));
548 while (num_pad_bytes--) ReadBlobByte( image );
553 c=ReadBlobByte(image);
554 length=MaxTextExtent;
555 comment=AcquireString((char *) NULL);
556 for (p=comment; c != EOF; p++)
558 if ((size_t) (p-comment+MaxTextExtent) >= length)
562 length+=MaxTextExtent;
563 comment=(char *) ResizeQuantumMemory(comment,length+MaxTextExtent,
565 if (comment == (char *) NULL)
567 p=comment+strlen(comment);
570 c=ReadBlobByte(image);
573 if (comment == (char *) NULL)
574 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
575 (void) SetImageProperty(image,"comment",comment);
576 comment=DestroyString(comment);
578 (void) CloseBlob(image);
579 return(GetFirstImageInList(image));
583 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
587 % R e g i s t e r P D B I m a g e %
591 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
593 % RegisterPDBImage() adds properties for the PDB image format to
594 % the list of supported formats. The properties include the image format
595 % tag, a method to read and/or write the format, whether the format
596 % supports the saving of more than one frame to the same file or blob,
597 % whether the format supports native in-memory I/O, and a brief
598 % description of the format.
600 % The format of the RegisterPDBImage method is:
602 % size_t RegisterPDBImage(void)
605 ModuleExport size_t RegisterPDBImage(void)
610 entry=SetMagickInfo("PDB");
611 entry->decoder=(DecodeImageHandler *) ReadPDBImage;
612 entry->encoder=(EncodeImageHandler *) WritePDBImage;
613 entry->magick=(IsImageFormatHandler *) IsPDB;
614 entry->description=ConstantString("Palm Database ImageViewer Format");
615 entry->module=ConstantString("PDB");
616 (void) RegisterMagickInfo(entry);
617 return(MagickImageCoderSignature);
621 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
625 % U n r e g i s t e r P D B I m a g e %
629 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
631 % UnregisterPDBImage() removes format registrations made by the
632 % PDB module from the list of supported formats.
634 % The format of the UnregisterPDBImage method is:
636 % UnregisterPDBImage(void)
639 ModuleExport void UnregisterPDBImage(void)
641 (void) UnregisterMagickInfo("PDB");
645 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
649 % W r i t e P D B I m a g e %
653 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
655 % WritePDBImage() writes an image
657 % The format of the WritePDBImage method is:
659 % MagickBooleanType WritePDBImage(const ImageInfo *image_info,
660 % Image *image,ExceptionInfo *exception)
662 % A description of each parameter follows.
664 % o image_info: the image info.
666 % o image: The image.
668 % o exception: return any errors or warnings in this structure.
672 static unsigned char *EncodeRLE(unsigned char *destination,
673 unsigned char *source,size_t literal,size_t repeat)
676 *destination++=(unsigned char) (literal-1);
677 (void) CopyMagickMemory(destination,source,literal);
678 destination+=literal;
681 *destination++=(unsigned char) (0x80 | (repeat-1));
682 *destination++=source[literal];
687 static MagickBooleanType WritePDBImage(const ImageInfo *image_info,Image *image,
688 ExceptionInfo *exception)
708 register const Quantum
714 register unsigned char
733 Open output image file.
735 assert(image_info != (const ImageInfo *) NULL);
736 assert(image_info->signature == MagickSignature);
737 assert(image != (Image *) NULL);
738 assert(image->signature == MagickSignature);
739 if (image->debug != MagickFalse)
740 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
741 assert(exception != (ExceptionInfo *) NULL);
742 assert(exception->signature == MagickSignature);
743 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
744 if (status == MagickFalse)
746 if (IsRGBColorspace(image->colorspace) == MagickFalse)
747 (void) TransformImageColorspace(image,RGBColorspace);
749 if (image -> colors <= 2 || GetImageType( image, exception ) == BilevelType) { /* TS */
751 } else if (image -> colors <= 4) {
753 } else if (image -> colors <= 8) {
759 (void) ResetMagickMemory(pdb_info.name,0,32);
760 (void) CopyMagickString(pdb_info.name,image_info->filename,32);
761 pdb_info.attributes=0;
763 pdb_info.create_time=time(NULL);
764 pdb_info.modify_time=pdb_info.create_time;
765 pdb_info.archive_time=0;
766 pdb_info.modify_number=0;
767 pdb_info.application_info=0;
768 pdb_info.sort_info=0;
769 (void) CopyMagickMemory(pdb_info.type,"vIMG",4);
770 (void) CopyMagickMemory(pdb_info.id,"View",4);
772 pdb_info.next_record=0;
773 comment=GetImageProperty(image,"comment");
774 pdb_info.number_records=(comment == (const char *) NULL ? 1 : 2);
775 (void) WriteBlob(image,32,(unsigned char *) pdb_info.name);
776 (void) WriteBlobMSBShort(image,(unsigned short) pdb_info.attributes);
777 (void) WriteBlobMSBShort(image,(unsigned short) pdb_info.version);
778 (void) WriteBlobMSBLong(image,(unsigned int) pdb_info.create_time);
779 (void) WriteBlobMSBLong(image,(unsigned int) pdb_info.modify_time);
780 (void) WriteBlobMSBLong(image,(unsigned int) pdb_info.archive_time);
781 (void) WriteBlobMSBLong(image,(unsigned int) pdb_info.modify_number);
782 (void) WriteBlobMSBLong(image,(unsigned int) pdb_info.application_info);
783 (void) WriteBlobMSBLong(image,(unsigned int) pdb_info.sort_info);
784 (void) WriteBlob(image,4,(unsigned char *) pdb_info.type);
785 (void) WriteBlob(image,4,(unsigned char *) pdb_info.id);
786 (void) WriteBlobMSBLong(image,(unsigned int) pdb_info.seed);
787 (void) WriteBlobMSBLong(image,(unsigned int) pdb_info.next_record);
788 (void) WriteBlobMSBShort(image,(unsigned short) pdb_info.number_records);
789 (void) CopyMagickString(pdb_image.name,pdb_info.name,32);
790 pdb_image.version=1; /* RLE Compressed */
791 switch (bits_per_pixel)
793 case 1: pdb_image.type=(char) 0xff; break; /* monochrome */
794 case 2: pdb_image.type=(char) 0x00; break; /* 2 bit gray */
795 default: pdb_image.type=(char) 0x02; /* 4 bit gray */
797 pdb_image.reserved_1=0;
801 pdb_image.reserved_2=0;
802 pdb_image.x_anchor=(short) 0xffff;
803 pdb_image.y_anchor=(short) 0xffff;
804 pdb_image.width=(short) image->columns;
805 if (image->columns % 16)
806 pdb_image.width=(short) (16*(image->columns/16+1));
807 pdb_image.height=(short) image->rows;
808 packets=(bits_per_pixel*image->columns/8)*image->rows;
809 runlength=(unsigned char *) AcquireQuantumMemory(2UL*packets,
811 if (runlength == (unsigned char *) NULL)
812 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
813 buffer=(unsigned char *) AcquireQuantumMemory(256UL,sizeof(*buffer));
814 if (buffer == (unsigned char *) NULL)
815 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
816 packet_size=(size_t) (image->depth > 8 ? 2: 1);
817 scanline=(unsigned char *) AcquireQuantumMemory(image->columns,packet_size*
819 if (scanline == (unsigned char *) NULL)
820 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
821 if (IsRGBColorspace(image->colorspace) == MagickFalse)
822 (void) TransformImageColorspace(image,RGBColorspace);
824 Convert to GRAY raster scanline.
826 quantum_info=AcquireQuantumInfo(image_info,image);
827 if (quantum_info == (QuantumInfo *) NULL)
828 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
829 bits=8/(int) bits_per_pixel-1; /* start at most significant bits */
834 for (y=0; y < (ssize_t) image->rows; y++)
836 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
837 if (p == (const Quantum *) NULL)
839 (void) ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
840 GrayQuantum,scanline,exception);
841 for (x=0; x < pdb_image.width; x++)
843 if (x < (ssize_t) image->columns)
844 buffer[literal+repeat]|=(0xff-scanline[x*packet_size]) >>
845 (8-bits_per_pixel) << bits*bits_per_pixel;
849 if (((literal+repeat) > 0) &&
850 (buffer[literal+repeat] == buffer[literal+repeat-1]))
860 q=EncodeRLE(q,buffer,literal,repeat);
871 q=EncodeRLE(q,buffer,literal,repeat);
872 buffer[0]=buffer[literal+repeat];
879 q=EncodeRLE(q,buffer,(literal < 0x80 ? literal : 0x80),0);
880 (void) CopyMagickMemory(buffer,buffer+literal+repeat,0x80);
884 bits=8/(int) bits_per_pixel-1;
885 buffer[literal+repeat]=0x00;
888 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
890 if (status == MagickFalse)
893 q=EncodeRLE(q,buffer,literal,repeat);
894 scanline=(unsigned char *) RelinquishMagickMemory(scanline);
895 buffer=(unsigned char *) RelinquishMagickMemory(buffer);
896 quantum_info=DestroyQuantumInfo(quantum_info);
898 Write the Image record header.
900 (void) WriteBlobMSBLong(image,(unsigned int)
901 (TellBlob(image)+8*pdb_info.number_records));
902 (void) WriteBlobByte(image,0x40);
903 (void) WriteBlobByte(image,0x6f);
904 (void) WriteBlobByte(image,0x80);
905 (void) WriteBlobByte(image,0);
906 if (pdb_info.number_records > 1)
909 Write the comment record header.
911 (void) WriteBlobMSBLong(image,(unsigned int) (TellBlob(image)+8+58+q-
913 (void) WriteBlobByte(image,0x40);
914 (void) WriteBlobByte(image,0x6f);
915 (void) WriteBlobByte(image,0x80);
916 (void) WriteBlobByte(image,1);
919 Write the Image data.
921 (void) WriteBlob(image,32,(unsigned char *) pdb_image.name);
922 (void) WriteBlobByte(image,(unsigned char) pdb_image.version);
923 (void) WriteBlobByte(image,(unsigned char) pdb_image.type);
924 (void) WriteBlobMSBLong(image,(unsigned int) pdb_image.reserved_1);
925 (void) WriteBlobMSBLong(image,(unsigned int) pdb_image.note);
926 (void) WriteBlobMSBShort(image,(unsigned short) pdb_image.x_last);
927 (void) WriteBlobMSBShort(image,(unsigned short) pdb_image.y_last);
928 (void) WriteBlobMSBLong(image,(unsigned int) pdb_image.reserved_2);
929 (void) WriteBlobMSBShort(image,(unsigned short) pdb_image.x_anchor);
930 (void) WriteBlobMSBShort(image,(unsigned short) pdb_image.y_anchor);
931 (void) WriteBlobMSBShort(image,(unsigned short) pdb_image.width);
932 (void) WriteBlobMSBShort(image,(unsigned short) pdb_image.height);
933 (void) WriteBlob(image,(size_t) (q-runlength),runlength);
934 runlength=(unsigned char *) RelinquishMagickMemory(runlength);
935 if (pdb_info.number_records > 1)
936 (void) WriteBlobString(image,comment);
937 (void) CloseBlob(image);