2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read/Write Palm Database ImageViewer Image Format %
20 % Copyright 1999-2017 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 % https://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
138 Forward declarations.
140 static MagickBooleanType
141 WritePDBImage(const ImageInfo *,Image *,ExceptionInfo *);
144 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
148 % D e c o d e I m a g e %
152 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
154 % DecodeImage unpacks the packed image pixels into runlength-encoded
157 % The format of the DecodeImage method is:
159 % MagickBooleanType DecodeImage(Image *image,unsigned char *pixels,
160 % const size_t length)
162 % A description of each parameter follows:
164 % o image: the address of a structure of type Image.
166 % o pixels: The address of a byte (8 bits) array of pixel data created by
167 % the decoding process.
169 % o length: Number of bytes to read into buffer 'pixels'.
172 static MagickBooleanType DecodeImage(Image *image, unsigned char *pixels,
175 #define RLE_MODE_NONE -1
176 #define RLE_MODE_COPY 0
177 #define RLE_MODE_RUN 1
179 int data = 0, count = 0;
181 int mode = RLE_MODE_NONE;
183 for (p = pixels; p < pixels + length; p++) {
185 data = ReadBlobByte( image );
186 if (-1 == data) return MagickFalse;
189 count = data - 128 + 1;
190 data = ReadBlobByte( image );
191 if (-1 == data) return MagickFalse;
193 mode = RLE_MODE_COPY;
198 if (RLE_MODE_COPY == mode) {
199 data = ReadBlobByte( image );
200 if (-1 == data) return MagickFalse;
202 *p = (unsigned char)data;
209 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
217 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
219 % IsPDB() returns MagickTrue if the image format type, identified by the
220 % magick string, is PDB.
222 % The format of the ReadPDBImage method is:
224 % MagickBooleanType IsPDB(const unsigned char *magick,const size_t length)
226 % A description of each parameter follows:
228 % o magick: compare image format pattern against these bytes.
230 % o length: Specifies the length of the magick string.
233 static MagickBooleanType IsPDB(const unsigned char *magick,const size_t length)
237 if (memcmp(magick+60,"vIMGView",8) == 0)
243 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
247 % R e a d P D B I m a g e %
251 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
253 % ReadPDBImage() reads an Pilot image file and returns it. It
254 % allocates the memory necessary for the new Image structure and returns a
255 % pointer to the new image.
257 % The format of the ReadPDBImage method is:
259 % Image *ReadPDBImage(const ImageInfo *image_info,ExceptionInfo *exception)
261 % A description of each parameter follows:
263 % o image_info: the image info.
265 % o exception: return any errors or warnings in this structure.
268 static Image *ReadPDBImage(const ImageInfo *image_info,ExceptionInfo *exception)
295 register unsigned char
316 assert(image_info != (const ImageInfo *) NULL);
317 assert(image_info->signature == MagickCoreSignature);
318 if (image_info->debug != MagickFalse)
319 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
320 image_info->filename);
321 assert(exception != (ExceptionInfo *) NULL);
322 assert(exception->signature == MagickCoreSignature);
323 image=AcquireImage(image_info,exception);
324 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
325 if (status == MagickFalse)
327 image=DestroyImageList(image);
328 return((Image *) NULL);
331 Determine if this a PDB image file.
333 count=ReadBlob(image,sizeof(pdb_info.name),(unsigned char *) pdb_info.name);
334 if (count != sizeof(pdb_info.name))
335 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
336 pdb_info.attributes=(short) ReadBlobMSBShort(image);
337 pdb_info.version=(short) ReadBlobMSBShort(image);
338 pdb_info.create_time=ReadBlobMSBLong(image);
339 pdb_info.modify_time=ReadBlobMSBLong(image);
340 pdb_info.archive_time=ReadBlobMSBLong(image);
341 pdb_info.modify_number=ReadBlobMSBLong(image);
342 pdb_info.application_info=ReadBlobMSBLong(image);
343 pdb_info.sort_info=ReadBlobMSBLong(image);
344 (void) ReadBlob(image,4,(unsigned char *) pdb_info.type);
345 (void) ReadBlob(image,4,(unsigned char *) pdb_info.id);
346 pdb_info.seed=ReadBlobMSBLong(image);
347 pdb_info.next_record=ReadBlobMSBLong(image);
348 pdb_info.number_records=(short) ReadBlobMSBShort(image);
349 if ((memcmp(pdb_info.type,"vIMG",4) != 0) ||
350 (memcmp(pdb_info.id,"View",4) != 0))
351 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
352 if (pdb_info.next_record != 0)
353 ThrowReaderException(CoderError,"MultipleRecordListNotSupported");
357 img_offset=(ssize_t) ReadBlobMSBSignedLong(image);
358 attributes=(unsigned char) ReadBlobByte(image);
360 count=ReadBlob(image,3,(unsigned char *) tag);
361 if (count != 3 || memcmp(tag,"\x6f\x80\x00",3) != 0)
362 ThrowReaderException(CorruptImageError,"CorruptImage");
363 if (pdb_info.number_records > 1)
365 comment_offset=(ssize_t) ReadBlobMSBSignedLong(image);
366 attributes=(unsigned char) ReadBlobByte(image);
367 count=ReadBlob(image,3,(unsigned char *) tag);
368 if (count != 3 || memcmp(tag,"\x6f\x80\x01",3) != 0)
369 ThrowReaderException(CorruptImageError,"CorruptImage");
371 num_pad_bytes = (size_t) (img_offset - TellBlob( image ));
372 while (num_pad_bytes-- != 0)
377 c=ReadBlobByte(image);
384 count=ReadBlob(image,sizeof(pdb_image.name),(unsigned char *) pdb_image.name);
385 if (count != sizeof(pdb_image.name))
386 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
387 pdb_image.version=ReadBlobByte(image);
388 pdb_image.type=(unsigned char) (ReadBlobByte(image));
389 pdb_image.reserved_1=ReadBlobMSBLong(image);
390 pdb_image.note=ReadBlobMSBLong(image);
391 pdb_image.x_last=(short) ReadBlobMSBShort(image);
392 pdb_image.y_last=(short) ReadBlobMSBShort(image);
393 pdb_image.reserved_2=ReadBlobMSBLong(image);
394 pdb_image.x_anchor=ReadBlobMSBShort(image);
395 pdb_image.y_anchor=ReadBlobMSBShort(image);
396 pdb_image.width=(short) ReadBlobMSBShort(image);
397 pdb_image.height=(short) ReadBlobMSBShort(image);
399 Initialize image structure.
401 image->columns=(size_t) pdb_image.width;
402 image->rows=(size_t) pdb_image.height;
404 image->storage_class=PseudoClass;
405 bits_per_pixel=pdb_image.type == 0 ? 2UL : pdb_image.type == 2 ? 4UL : 1UL;
407 if (AcquireImageColormap(image,one << bits_per_pixel,exception) == MagickFalse)
408 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
409 if (image_info->ping != MagickFalse)
411 (void) CloseBlob(image);
412 return(GetFirstImageInList(image));
414 status=SetImageExtent(image,image->columns,image->rows,exception);
415 if (status == MagickFalse)
416 return(DestroyImageList(image));
417 packets=(bits_per_pixel*image->columns+7)/8;
418 pixels=(unsigned char *) AcquireQuantumMemory(packets+257UL,image->rows*
420 if (pixels == (unsigned char *) NULL)
421 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
422 switch (pdb_image.version & 0x07)
426 image->compression=NoCompression;
427 count=(ssize_t) ReadBlob(image,packets*image->rows,pixels);
432 image->compression=RLECompression;
433 if (!DecodeImage(image,pixels,packets*image->rows))
435 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
436 ThrowReaderException(CorruptImageError,"RLEDecoderError");
442 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
443 ThrowReaderException(CorruptImageError,
444 "UnrecognizedImageCompressionType");
448 switch (bits_per_pixel)
456 Read 1-bit PDB image.
458 for (y=0; y < (ssize_t) image->rows; y++)
460 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
461 if (q == (Quantum *) NULL)
463 for (x=0; x < ((ssize_t) image->columns-7); x+=8)
465 for (bit=0; bit < 8; bit++)
467 index=(Quantum) (*p & (0x80 >> bit) ? 0x00 : 0x01);
468 SetPixelIndex(image,index,q);
469 q+=GetPixelChannels(image);
473 if (SyncAuthenticPixels(image,exception) == MagickFalse)
475 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
477 if (status == MagickFalse)
480 (void) SyncImage(image,exception);
486 Read 2-bit PDB image.
488 for (y=0; y < (ssize_t) image->rows; y++)
490 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
491 if (q == (Quantum *) NULL)
493 for (x=0; x < (ssize_t) image->columns-3; x+=4)
495 index=ConstrainColormapIndex(image,3UL-((*p >> 6) & 0x03),exception);
496 SetPixelIndex(image,index,q);
497 q+=GetPixelChannels(image);
498 index=ConstrainColormapIndex(image,3UL-((*p >> 4) & 0x03),exception);
499 SetPixelIndex(image,index,q);
500 q+=GetPixelChannels(image);
501 index=ConstrainColormapIndex(image,3UL-((*p >> 2) & 0x03),exception);
502 SetPixelIndex(image,index,q);
503 q+=GetPixelChannels(image);
504 index=ConstrainColormapIndex(image,3UL-((*p) & 0x03),exception);
505 SetPixelIndex(image,index,q);
507 q+=GetPixelChannels(image);
509 if (SyncAuthenticPixels(image,exception) == MagickFalse)
511 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
513 if (status == MagickFalse)
516 (void) SyncImage(image,exception);
522 Read 4-bit PDB image.
524 for (y=0; y < (ssize_t) image->rows; y++)
526 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
527 if (q == (Quantum *) NULL)
529 for (x=0; x < (ssize_t) image->columns-1; x+=2)
531 index=ConstrainColormapIndex(image,15UL-((*p >> 4) & 0x0f),exception);
532 SetPixelIndex(image,index,q);
533 q+=GetPixelChannels(image);
534 index=ConstrainColormapIndex(image,15UL-((*p) & 0x0f),exception);
535 SetPixelIndex(image,index,q);
537 q+=GetPixelChannels(image);
539 if (SyncAuthenticPixels(image,exception) == MagickFalse)
541 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
543 if (status == MagickFalse)
546 (void) SyncImage(image,exception);
551 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
552 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
555 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
556 if (EOFBlob(image) != MagickFalse)
557 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
559 if (pdb_info.number_records > 1)
573 num_pad_bytes = (size_t) (comment_offset - TellBlob( image ));
574 while (num_pad_bytes-- != 0)
579 c=ReadBlobByte(image);
587 c=ReadBlobByte(image);
588 length=MagickPathExtent;
589 comment=AcquireString((char *) NULL);
590 for (p=comment; c != EOF; p++)
592 if ((size_t) (p-comment+MagickPathExtent) >= length)
596 length+=MagickPathExtent;
597 comment=(char *) ResizeQuantumMemory(comment,length+MagickPathExtent,
599 if (comment == (char *) NULL)
601 p=comment+strlen(comment);
604 c=ReadBlobByte(image);
607 if (comment == (char *) NULL)
608 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
609 (void) SetImageProperty(image,"comment",comment,exception);
610 comment=DestroyString(comment);
612 (void) CloseBlob(image);
613 return(GetFirstImageInList(image));
617 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
621 % R e g i s t e r P D B I m a g e %
625 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
627 % RegisterPDBImage() adds properties for the PDB image format to
628 % the list of supported formats. The properties include the image format
629 % tag, a method to read and/or write the format, whether the format
630 % supports the saving of more than one frame to the same file or blob,
631 % whether the format supports native in-memory I/O, and a brief
632 % description of the format.
634 % The format of the RegisterPDBImage method is:
636 % size_t RegisterPDBImage(void)
639 ModuleExport size_t RegisterPDBImage(void)
644 entry=AcquireMagickInfo("PDB","PDB","Palm Database ImageViewer Format");
645 entry->decoder=(DecodeImageHandler *) ReadPDBImage;
646 entry->encoder=(EncodeImageHandler *) WritePDBImage;
647 entry->magick=(IsImageFormatHandler *) IsPDB;
648 (void) RegisterMagickInfo(entry);
649 return(MagickImageCoderSignature);
653 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
657 % U n r e g i s t e r P D B I m a g e %
661 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
663 % UnregisterPDBImage() removes format registrations made by the
664 % PDB module from the list of supported formats.
666 % The format of the UnregisterPDBImage method is:
668 % UnregisterPDBImage(void)
671 ModuleExport void UnregisterPDBImage(void)
673 (void) UnregisterMagickInfo("PDB");
677 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
681 % W r i t e P D B I m a g e %
685 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
687 % WritePDBImage() writes an image
689 % The format of the WritePDBImage method is:
691 % MagickBooleanType WritePDBImage(const ImageInfo *image_info,
692 % Image *image,ExceptionInfo *exception)
694 % A description of each parameter follows.
696 % o image_info: the image info.
698 % o image: The image.
700 % o exception: return any errors or warnings in this structure.
704 static unsigned char *EncodeRLE(unsigned char *destination,
705 unsigned char *source,size_t literal,size_t repeat)
708 *destination++=(unsigned char) (literal-1);
709 (void) CopyMagickMemory(destination,source,literal);
710 destination+=literal;
713 *destination++=(unsigned char) (0x80 | (repeat-1));
714 *destination++=source[literal];
719 static MagickBooleanType WritePDBImage(const ImageInfo *image_info,Image *image,
720 ExceptionInfo *exception)
740 register const Quantum
746 register unsigned char
765 Open output image file.
767 assert(image_info != (const ImageInfo *) NULL);
768 assert(image_info->signature == MagickCoreSignature);
769 assert(image != (Image *) NULL);
770 assert(image->signature == MagickCoreSignature);
771 if (image->debug != MagickFalse)
772 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
773 assert(exception != (ExceptionInfo *) NULL);
774 assert(exception->signature == MagickCoreSignature);
775 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
776 if (status == MagickFalse)
778 (void) TransformImageColorspace(image,sRGBColorspace,exception);
779 if (SetImageMonochrome(image,exception) != MagickFalse) {
781 } else if (image->colors <= 4) {
783 } else if (image->colors <= 8) {
788 (void) ResetMagickMemory(&pdb_info,0,sizeof(pdb_info));
789 (void) CopyMagickString(pdb_info.name,image_info->filename,
790 sizeof(pdb_info.name));
791 pdb_info.attributes=0;
793 pdb_info.create_time=time(NULL);
794 pdb_info.modify_time=pdb_info.create_time;
795 pdb_info.archive_time=0;
796 pdb_info.modify_number=0;
797 pdb_info.application_info=0;
798 pdb_info.sort_info=0;
799 (void) CopyMagickMemory(pdb_info.type,"vIMG",4);
800 (void) CopyMagickMemory(pdb_info.id,"View",4);
802 pdb_info.next_record=0;
803 comment=GetImageProperty(image,"comment",exception);
804 pdb_info.number_records=(comment == (const char *) NULL ? 1 : 2);
805 (void) WriteBlob(image,sizeof(pdb_info.name),(unsigned char *) pdb_info.name);
806 (void) WriteBlobMSBShort(image,(unsigned short) pdb_info.attributes);
807 (void) WriteBlobMSBShort(image,(unsigned short) pdb_info.version);
808 (void) WriteBlobMSBLong(image,(unsigned int) pdb_info.create_time);
809 (void) WriteBlobMSBLong(image,(unsigned int) pdb_info.modify_time);
810 (void) WriteBlobMSBLong(image,(unsigned int) pdb_info.archive_time);
811 (void) WriteBlobMSBLong(image,(unsigned int) pdb_info.modify_number);
812 (void) WriteBlobMSBLong(image,(unsigned int) pdb_info.application_info);
813 (void) WriteBlobMSBLong(image,(unsigned int) pdb_info.sort_info);
814 (void) WriteBlob(image,4,(unsigned char *) pdb_info.type);
815 (void) WriteBlob(image,4,(unsigned char *) pdb_info.id);
816 (void) WriteBlobMSBLong(image,(unsigned int) pdb_info.seed);
817 (void) WriteBlobMSBLong(image,(unsigned int) pdb_info.next_record);
818 (void) WriteBlobMSBShort(image,(unsigned short) pdb_info.number_records);
819 (void) CopyMagickString(pdb_image.name,pdb_info.name,sizeof(pdb_image.name));
820 pdb_image.version=1; /* RLE Compressed */
821 switch (bits_per_pixel)
823 case 1: pdb_image.type=(unsigned char) 0xff; break; /* monochrome */
824 case 2: pdb_image.type=(unsigned char) 0x00; break; /* 2 bit gray */
825 default: pdb_image.type=(unsigned char) 0x02; /* 4 bit gray */
827 pdb_image.reserved_1=0;
831 pdb_image.reserved_2=0;
832 pdb_image.x_anchor=(unsigned short) 0xffff;
833 pdb_image.y_anchor=(unsigned short) 0xffff;
834 pdb_image.width=(short) image->columns;
835 if (image->columns % 16)
836 pdb_image.width=(short) (16*(image->columns/16+1));
837 pdb_image.height=(short) image->rows;
838 packets=((bits_per_pixel*image->columns+7)/8);
839 packet_size=(size_t) (image->depth > 8 ? 2 : 1);
840 runlength=(unsigned char *) AcquireQuantumMemory(9UL*packets,
841 image->rows*sizeof(*runlength));
842 buffer=(unsigned char *) AcquireQuantumMemory(512,sizeof(*buffer));
843 scanline=(unsigned char *) AcquireQuantumMemory(image->columns,packet_size*
845 if ((runlength == (unsigned char *) NULL) ||
846 (buffer == (unsigned char *) NULL) ||
847 (scanline == (unsigned char *) NULL))
849 if (runlength != (unsigned char *) NULL)
850 runlength=(unsigned char *) RelinquishMagickMemory(runlength);
851 if (buffer != (unsigned char *) NULL)
852 buffer=(unsigned char *) RelinquishMagickMemory(buffer);
853 if (scanline != (unsigned char *) NULL)
854 scanline=(unsigned char *) RelinquishMagickMemory(scanline);
855 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
857 if (IssRGBCompatibleColorspace(image->colorspace) == MagickFalse)
858 (void) TransformImageColorspace(image,sRGBColorspace,exception);
860 Convert to GRAY raster scanline.
862 quantum_info=AcquireQuantumInfo(image_info,image);
863 if (quantum_info == (QuantumInfo *) NULL)
864 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
865 status=SetQuantumDepth(image,quantum_info,image->depth > 8 ? 16 : 8);
866 bits=8/(int) bits_per_pixel-1; /* start at most significant bits */
871 for (y=0; y < (ssize_t) image->rows; y++)
873 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
874 if (p == (const Quantum *) NULL)
876 (void) ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
877 GrayQuantum,scanline,exception);
878 for (x=0; x < (ssize_t) pdb_image.width; x++)
880 if (x < (ssize_t) image->columns)
881 buffer[literal+repeat]|=(0xff-scanline[x*packet_size]) >>
882 (8-bits_per_pixel) << bits*bits_per_pixel;
886 if (((literal+repeat) > 0) &&
887 (buffer[literal+repeat] == buffer[literal+repeat-1]))
897 q=EncodeRLE(q,buffer,literal,repeat);
908 q=EncodeRLE(q,buffer,literal,repeat);
909 buffer[0]=buffer[literal+repeat];
916 q=EncodeRLE(q,buffer,(literal < 0x80 ? literal : 0x80),0);
917 (void) CopyMagickMemory(buffer,buffer+literal+repeat,0x80);
921 bits=8/(int) bits_per_pixel-1;
922 buffer[literal+repeat]=0x00;
925 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
927 if (status == MagickFalse)
930 q=EncodeRLE(q,buffer,literal,repeat);
931 scanline=(unsigned char *) RelinquishMagickMemory(scanline);
932 buffer=(unsigned char *) RelinquishMagickMemory(buffer);
933 quantum_info=DestroyQuantumInfo(quantum_info);
935 Write the Image record header.
937 (void) WriteBlobMSBLong(image,(unsigned int)
938 (TellBlob(image)+8*pdb_info.number_records));
939 (void) WriteBlobByte(image,0x40);
940 (void) WriteBlobByte(image,0x6f);
941 (void) WriteBlobByte(image,0x80);
942 (void) WriteBlobByte(image,0);
943 if (pdb_info.number_records > 1)
946 Write the comment record header.
948 (void) WriteBlobMSBLong(image,(unsigned int) (TellBlob(image)+8+58+q-
950 (void) WriteBlobByte(image,0x40);
951 (void) WriteBlobByte(image,0x6f);
952 (void) WriteBlobByte(image,0x80);
953 (void) WriteBlobByte(image,1);
956 Write the Image data.
958 (void) WriteBlob(image,sizeof(pdb_image.name),(unsigned char *)
960 (void) WriteBlobByte(image,(unsigned char) pdb_image.version);
961 (void) WriteBlobByte(image,pdb_image.type);
962 (void) WriteBlobMSBLong(image,(unsigned int) pdb_image.reserved_1);
963 (void) WriteBlobMSBLong(image,(unsigned int) pdb_image.note);
964 (void) WriteBlobMSBShort(image,(unsigned short) pdb_image.x_last);
965 (void) WriteBlobMSBShort(image,(unsigned short) pdb_image.y_last);
966 (void) WriteBlobMSBLong(image,(unsigned int) pdb_image.reserved_2);
967 (void) WriteBlobMSBShort(image,pdb_image.x_anchor);
968 (void) WriteBlobMSBShort(image,pdb_image.y_anchor);
969 (void) WriteBlobMSBShort(image,(unsigned short) pdb_image.width);
970 (void) WriteBlobMSBShort(image,(unsigned short) pdb_image.height);
971 (void) WriteBlob(image,(size_t) (q-runlength),runlength);
972 runlength=(unsigned char *) RelinquishMagickMemory(runlength);
973 if (pdb_info.number_records > 1)
974 (void) WriteBlobString(image,comment);
975 (void) CloseBlob(image);