2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read/Write Palm Pixmap. %
17 % Christopher R. Hawks %
20 % Copyright 1999-2004 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
26 % http://www.imagemagick.org/script/license.php %
28 % Unless required by applicable law or agreed to in writing, software %
29 % distributed under the License is distributed on an "AS IS" BASIS, %
30 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31 % See the License for the specific language governing permissions and %
32 % limitations under the License. %
35 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
37 % Based on pnmtopalm by Bill Janssen and ppmtobmp by Ian Goldberg.
44 #include "MagickCore/studio.h"
45 #include "MagickCore/attribute.h"
46 #include "MagickCore/blob.h"
47 #include "MagickCore/blob-private.h"
48 #include "MagickCore/cache.h"
49 #include "MagickCore/color.h"
50 #include "MagickCore/colormap.h"
51 #include "MagickCore/colormap-private.h"
52 #include "MagickCore/color-private.h"
53 #include "MagickCore/colorspace.h"
54 #include "MagickCore/colorspace-private.h"
55 #include "MagickCore/constitute.h"
56 #include "MagickCore/exception.h"
57 #include "MagickCore/histogram.h"
58 #include "MagickCore/image.h"
59 #include "MagickCore/image-private.h"
60 #include "MagickCore/list.h"
61 #include "MagickCore/magick.h"
62 #include "MagickCore/memory_.h"
63 #include "MagickCore/monitor.h"
64 #include "MagickCore/monitor-private.h"
65 #include "MagickCore/paint.h"
66 #include "MagickCore/pixel-accessor.h"
67 #include "MagickCore/property.h"
68 #include "MagickCore/quantize.h"
69 #include "MagickCore/quantum-private.h"
70 #include "MagickCore/static.h"
71 #include "MagickCore/string_.h"
72 #include "MagickCore/module.h"
73 #include "MagickCore/utility.h"
78 #define PALM_IS_COMPRESSED_FLAG 0x8000
79 #define PALM_HAS_COLORMAP_FLAG 0x4000
80 #define PALM_HAS_FOUR_BYTE_FIELD 0x0200
81 #define PALM_HAS_TRANSPARENCY_FLAG 0x2000
82 #define PALM_IS_INDIRECT 0x1000
83 #define PALM_IS_FOR_SCREEN 0x0800
84 #define PALM_IS_DIRECT_COLOR 0x0400
85 #define PALM_COMPRESSION_SCANLINE 0x00
86 #define PALM_COMPRESSION_RLE 0x01
87 #define PALM_COMPRESSION_NONE 0xFF
90 The 256 color system palette for Palm Computing Devices.
95 {255, 255,255}, {255, 204,255}, {255, 153,255}, {255, 102,255},
96 {255, 51,255}, {255, 0,255}, {255, 255,204}, {255, 204,204},
97 {255, 153,204}, {255, 102,204}, {255, 51,204}, {255, 0,204},
98 {255, 255,153}, {255, 204,153}, {255, 153,153}, {255, 102,153},
99 {255, 51,153}, {255, 0,153}, {204, 255,255}, {204, 204,255},
100 {204, 153,255}, {204, 102,255}, {204, 51,255}, {204, 0,255},
101 {204, 255,204}, {204, 204,204}, {204, 153,204}, {204, 102,204},
102 {204, 51,204}, {204, 0,204}, {204, 255,153}, {204, 204,153},
103 {204, 153,153}, {204, 102,153}, {204, 51,153}, {204, 0,153},
104 {153, 255,255}, {153, 204,255}, {153, 153,255}, {153, 102,255},
105 {153, 51,255}, {153, 0,255}, {153, 255,204}, {153, 204,204},
106 {153, 153,204}, {153, 102,204}, {153, 51,204}, {153, 0,204},
107 {153, 255,153}, {153, 204,153}, {153, 153,153}, {153, 102,153},
108 {153, 51,153}, {153, 0,153}, {102, 255,255}, {102, 204,255},
109 {102, 153,255}, {102, 102,255}, {102, 51,255}, {102, 0,255},
110 {102, 255,204}, {102, 204,204}, {102, 153,204}, {102, 102,204},
111 {102, 51,204}, {102, 0,204}, {102, 255,153}, {102, 204,153},
112 {102, 153,153}, {102, 102,153}, {102, 51,153}, {102, 0,153},
113 { 51, 255,255}, { 51, 204,255}, { 51, 153,255}, { 51, 102,255},
114 { 51, 51,255}, { 51, 0,255}, { 51, 255,204}, { 51, 204,204},
115 { 51, 153,204}, { 51, 102,204}, { 51, 51,204}, { 51, 0,204},
116 { 51, 255,153}, { 51, 204,153}, { 51, 153,153}, { 51, 102,153},
117 { 51, 51,153}, { 51, 0,153}, { 0, 255,255}, { 0, 204,255},
118 { 0, 153,255}, { 0, 102,255}, { 0, 51,255}, { 0, 0,255},
119 { 0, 255,204}, { 0, 204,204}, { 0, 153,204}, { 0, 102,204},
120 { 0, 51,204}, { 0, 0,204}, { 0, 255,153}, { 0, 204,153},
121 { 0, 153,153}, { 0, 102,153}, { 0, 51,153}, { 0, 0,153},
122 {255, 255,102}, {255, 204,102}, {255, 153,102}, {255, 102,102},
123 {255, 51,102}, {255, 0,102}, {255, 255, 51}, {255, 204, 51},
124 {255, 153, 51}, {255, 102, 51}, {255, 51, 51}, {255, 0, 51},
125 {255, 255, 0}, {255, 204, 0}, {255, 153, 0}, {255, 102, 0},
126 {255, 51, 0}, {255, 0, 0}, {204, 255,102}, {204, 204,102},
127 {204, 153,102}, {204, 102,102}, {204, 51,102}, {204, 0,102},
128 {204, 255, 51}, {204, 204, 51}, {204, 153, 51}, {204, 102, 51},
129 {204, 51, 51}, {204, 0, 51}, {204, 255, 0}, {204, 204, 0},
130 {204, 153, 0}, {204, 102, 0}, {204, 51, 0}, {204, 0, 0},
131 {153, 255,102}, {153, 204,102}, {153, 153,102}, {153, 102,102},
132 {153, 51,102}, {153, 0,102}, {153, 255, 51}, {153, 204, 51},
133 {153, 153, 51}, {153, 102, 51}, {153, 51, 51}, {153, 0, 51},
134 {153, 255, 0}, {153, 204, 0}, {153, 153, 0}, {153, 102, 0},
135 {153, 51, 0}, {153, 0, 0}, {102, 255,102}, {102, 204,102},
136 {102, 153,102}, {102, 102,102}, {102, 51,102}, {102, 0,102},
137 {102, 255, 51}, {102, 204, 51}, {102, 153, 51}, {102, 102, 51},
138 {102, 51, 51}, {102, 0, 51}, {102, 255, 0}, {102, 204, 0},
139 {102, 153, 0}, {102, 102, 0}, {102, 51, 0}, {102, 0, 0},
140 { 51, 255,102}, { 51, 204,102}, { 51, 153,102}, { 51, 102,102},
141 { 51, 51,102}, { 51, 0,102}, { 51, 255, 51}, { 51, 204, 51},
142 { 51, 153, 51}, { 51, 102, 51}, { 51, 51, 51}, { 51, 0, 51},
143 { 51, 255, 0}, { 51, 204, 0}, { 51, 153, 0}, { 51, 102, 0},
144 { 51, 51, 0}, { 51, 0, 0}, { 0, 255,102}, { 0, 204,102},
145 { 0, 153,102}, { 0, 102,102}, { 0, 51,102}, { 0, 0,102},
146 { 0, 255, 51}, { 0, 204, 51}, { 0, 153, 51}, { 0, 102, 51},
147 { 0, 51, 51}, { 0, 0, 51}, { 0, 255, 0}, { 0, 204, 0},
148 { 0, 153, 0}, { 0, 102, 0}, { 0, 51, 0}, { 17, 17, 17},
149 { 34, 34, 34}, { 68, 68, 68}, { 85, 85, 85}, {119, 119,119},
150 {136, 136,136}, {170, 170,170}, {187, 187,187}, {221, 221,221},
151 {238, 238,238}, {192, 192,192}, {128, 0, 0}, {128, 0,128},
152 { 0, 128, 0}, { 0, 128,128}, { 0, 0, 0}, { 0, 0, 0},
153 { 0, 0, 0}, { 0, 0, 0}, { 0, 0, 0}, { 0, 0, 0},
154 { 0, 0, 0}, { 0, 0, 0}, { 0, 0, 0}, { 0, 0, 0},
155 { 0, 0, 0}, { 0, 0, 0}, { 0, 0, 0}, { 0, 0, 0},
156 { 0, 0, 0}, { 0, 0, 0}, { 0, 0, 0}, { 0, 0, 0},
157 { 0, 0, 0}, { 0, 0, 0}, { 0, 0, 0}, { 0, 0, 0},
158 { 0, 0, 0}, { 0, 0, 0}, { 0, 0, 0}, { 0, 0, 0}
162 Forward declarations.
164 static MagickBooleanType
165 WritePALMImage(const ImageInfo *,Image *,ExceptionInfo *);
168 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
172 % F i n d C o l o r %
176 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
178 % FindColor() returns the index of the matching entry from PalmPalette for a
181 % The format of the FindColor method is:
183 % int FindColor(const Image *image,PixelInfo *pixel)
185 % A description of each parameter follows:
187 % o int: the index of the matching color or -1 if not found/
189 % o pixel: a pointer to the PixelInfo to be matched.
192 static int FindColor(const Image *image,PixelInfo *packet)
197 for (i=0; i < 256; i++)
198 if (ScaleQuantumToChar(packet->red) == PalmPalette[i][0] &&
199 ScaleQuantumToChar(packet->green) == PalmPalette[i][1] &&
200 ScaleQuantumToChar(packet->blue) == PalmPalette[i][2])
206 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
210 % R e a d P A L M I m a g e %
214 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
216 % ReadPALMImage() reads an image of raw bites in LSB order and returns it. It
217 % allocates the memory necessary for the new Image structure and returns a
218 % pointer to the new image.
220 % The format of the ReadPALMImage method is:
222 % Image *ReadPALMImage(const ImageInfo *image_info,
223 % ExceptionInfo *exception)
225 % A description of each parameter follows:
227 % o image_info: Specifies a pointer to an ImageInfo structure.
229 % o exception: return any errors or warnings in this structure.
233 static inline size_t MagickMax(const size_t x,const size_t y)
240 static inline ssize_t MagickMin(const ssize_t x,const ssize_t y)
247 static Image *ReadPALMImage(const ImageInfo *image_info,
248 ExceptionInfo *exception)
306 assert(image_info != (const ImageInfo *) NULL);
307 assert(image_info->signature == MagickSignature);
308 if (image_info->debug != MagickFalse)
309 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
310 image_info->filename);
311 assert(exception != (ExceptionInfo *) NULL);
312 assert(exception->signature == MagickSignature);
313 image=AcquireImage(image_info,exception);
314 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
315 if (status == MagickFalse)
317 (void) DestroyImageList(image);
318 return((Image *) NULL);
323 image->columns=ReadBlobMSBShort(image);
324 image->rows=ReadBlobMSBShort(image);
325 if (EOFBlob(image) != MagickFalse)
326 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
327 if ((image->columns == 0) || (image->rows == 0))
328 ThrowReaderException(CorruptImageError,"NegativeOrZeroImageSize");
329 bytes_per_row=ReadBlobMSBShort(image);
330 flags=ReadBlobMSBShort(image);
331 bits_per_pixel=(size_t) ReadBlobByte(image);
332 if (bits_per_pixel > 16)
333 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
334 version=(size_t) ReadBlobByte(image);
336 nextDepthOffset=(size_t) ReadBlobMSBShort(image);
337 transparentIndex=(size_t) ReadBlobByte(image);
338 compressionType=(size_t) ReadBlobByte(image);
339 pad=ReadBlobMSBShort(image);
342 Initialize image colormap.
345 if ((bits_per_pixel < 16) &&
346 (AcquireImageColormap(image,one << bits_per_pixel,exception) == MagickFalse))
347 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
348 GetPixelInfo(image,&transpix);
349 if (bits_per_pixel == 16) /* Direct Color */
351 redbits=(size_t) ReadBlobByte(image); /* # of bits of red */
353 greenbits=(size_t) ReadBlobByte(image); /* # of bits of green */
355 bluebits=(size_t) ReadBlobByte(image); /* # of bits of blue */
357 ReadBlobByte(image); /* reserved by Palm */
358 ReadBlobByte(image); /* reserved by Palm */
359 transpix.red=(double) (QuantumRange*ReadBlobByte(image)/31);
360 transpix.green=(double) (QuantumRange*ReadBlobByte(image)/63);
361 transpix.blue=(double) (QuantumRange*ReadBlobByte(image)/31);
363 if (bits_per_pixel == 8)
368 if (flags & PALM_HAS_COLORMAP_FLAG)
370 count=(ssize_t) ReadBlobMSBShort(image);
371 for (i=0; i < (ssize_t) count; i++)
374 index=ConstrainColormapIndex(image,255-i,exception);
375 image->colormap[(int) index].red=
376 ScaleCharToQuantum((unsigned char) ReadBlobByte(image));
377 image->colormap[(int) index].green=
378 ScaleCharToQuantum((unsigned char) ReadBlobByte(image));
379 image->colormap[(int) index].blue=
380 ScaleCharToQuantum((unsigned char) ReadBlobByte(image));
385 for (i=0; i < (ssize_t) (1L << bits_per_pixel); i++)
387 index=ConstrainColormapIndex(image,255-i,exception);
388 image->colormap[(int) index].red=
389 ScaleCharToQuantum(PalmPalette[i][0]);
390 image->colormap[(int) index].green=
391 ScaleCharToQuantum(PalmPalette[i][1]);
392 image->colormap[(int) index].blue=
393 ScaleCharToQuantum(PalmPalette[i][2]);
397 if (flags & PALM_IS_COMPRESSED_FLAG)
398 size=ReadBlobMSBShort(image);
400 image->storage_class=DirectClass;
401 if (bits_per_pixel < 16)
403 image->storage_class=PseudoClass;
406 one_row=(unsigned char *) AcquireQuantumMemory(bytes_per_row,
408 if (one_row == (unsigned char *) NULL)
409 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
410 lastrow=(unsigned char *) NULL;
411 if (compressionType == PALM_COMPRESSION_SCANLINE) {
412 lastrow=(unsigned char *) AcquireQuantumMemory(bytes_per_row,
414 if (lastrow == (unsigned char *) NULL)
415 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
417 mask=(1l << bits_per_pixel)-1;
418 for (y = 0; y < (ssize_t) image->rows; y++)
420 if ((flags & PALM_IS_COMPRESSED_FLAG) == 0)
422 /* TODO move out of loop! */
423 image->compression=NoCompression;
424 count=ReadBlob(image,bytes_per_row,one_row);
428 if (compressionType == PALM_COMPRESSION_RLE)
430 /* TODO move out of loop! */
431 image->compression=RLECompression;
432 for (i=0; i < (ssize_t) bytes_per_row; )
434 count=(ssize_t) ReadBlobByte(image);
435 count=MagickMin(count,(ssize_t) bytes_per_row-i);
436 byte=(size_t) ReadBlobByte(image);
437 (void) ResetMagickMemory(one_row+i,(int) byte,(size_t) count);
442 if (compressionType == PALM_COMPRESSION_SCANLINE)
447 /* TODO move out of loop! */
449 image->compression=FaxCompression;
450 for (i=0; i < (ssize_t) bytes_per_row; i+=8)
452 count=(ssize_t) ReadBlobByte(image);
453 byte=1UL*MagickMin((ssize_t) bytes_per_row-i,8);
454 for (bit=0; bit < byte; bit++)
456 if ((y == 0) || (count & (one << (7 - bit))))
457 one_row[i+bit]=(unsigned char) ReadBlobByte(image);
459 one_row[i+bit]=lastrow[i+bit];
462 (void) CopyMagickMemory(lastrow, one_row, bytes_per_row);
466 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
467 if (q == (Quantum *) NULL)
469 if (bits_per_pixel == 16)
471 if (image->columns > (2*bytes_per_row))
472 ThrowReaderException(CorruptImageError,"CorruptImage");
473 for (x=0; x < (ssize_t) image->columns; x++)
475 color16=(*ptr++ << 8);
477 SetPixelRed(image,(QuantumRange*((color16 >> 11) & 0x1f))/0x1f,q);
478 SetPixelGreen(image,(QuantumRange*((color16 >> 5) & 0x3f))/0x3f,q);
479 SetPixelBlue(image,(QuantumRange*((color16 >> 0) & 0x1f))/0x1f,q);
480 SetPixelAlpha(image,OpaqueAlpha,q);
481 q+=GetPixelChannels(image);
486 bit=8-bits_per_pixel;
487 for (x=0; x < (ssize_t) image->columns; x++)
489 if ((size_t) (ptr-one_row) >= bytes_per_row)
490 ThrowReaderException(CorruptImageError,"CorruptImage");
491 index=(Quantum) (mask-(((*ptr) & (mask << bit)) >> bit));
492 SetPixelIndex(image,index,q);
493 SetPixelInfoPixel(image,image->colormap+(ssize_t) index,q);
499 bit=8-bits_per_pixel;
501 q+=GetPixelChannels(image);
503 if (SyncAuthenticPixels(image,exception) == MagickFalse)
506 if (image->previous == (Image *) NULL)
508 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
510 if (status == MagickFalse)
514 if (flags & PALM_HAS_TRANSPARENCY_FLAG)
516 if (bits_per_pixel != 16)
517 transpix=image->colormap[mask-transparentIndex];
518 (void) TransparentPaintImage(image,&transpix,(Quantum) TransparentAlpha,
519 MagickFalse,exception);
521 one_row=(unsigned char *) RelinquishMagickMemory(one_row);
522 if (compressionType == PALM_COMPRESSION_SCANLINE)
523 lastrow=(unsigned char *) RelinquishMagickMemory(lastrow);
525 Proceed to next image. Copied from coders/pnm.c
527 if (image_info->number_scenes != 0)
528 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
530 if (nextDepthOffset != 0)
535 totalOffset+=(MagickOffsetType) (nextDepthOffset*4);
536 if (totalOffset >= (MagickOffsetType) GetBlobSize(image))
538 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
542 seekNextDepth=SeekBlob(image,totalOffset,SEEK_SET);
544 if (seekNextDepth != totalOffset)
545 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
547 Allocate next image structure. Copied from coders/pnm.c
549 AcquireNextImage(image_info,image,exception);
550 if (GetNextImageInList(image) == (Image *) NULL)
552 (void) DestroyImageList(image);
553 return((Image *) NULL);
555 image=SyncNextImageInList(image);
556 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
558 if (status == MagickFalse)
561 } while (nextDepthOffset != 0);
562 (void) CloseBlob(image);
563 return(GetFirstImageInList(image));
567 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
571 % R e g i s t e r P A L M I m a g e %
575 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
577 % RegisterPALMImage() adds properties for the PALM image format to the list of
578 % supported formats. The properties include the image format tag, a method to
579 % read and/or write the format, whether the format supports the saving of more
580 % than one frame to the same file or blob, whether the format supports native
581 % in-memory I/O, and a brief description of the format.
583 % The format of the RegisterPALMImage method is:
585 % size_t RegisterPALMImage(void)
588 ModuleExport size_t RegisterPALMImage(void)
593 entry=SetMagickInfo("PALM");
594 entry->decoder=(DecodeImageHandler *) ReadPALMImage;
595 entry->encoder=(EncodeImageHandler *) WritePALMImage;
596 entry->seekable_stream=MagickTrue;
597 entry->description=ConstantString("Palm pixmap");
598 entry->module=ConstantString("PALM");
599 (void) RegisterMagickInfo(entry);
600 return(MagickImageCoderSignature);
604 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
608 % U n r e g i s t e r P A L M I m a g e %
612 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
614 % UnregisterPALMImage() removes format registrations made by the PALM
615 % module from the list of supported formats.
617 % The format of the UnregisterPALMImage method is:
619 % UnregisterPALMImage(void)
622 ModuleExport void UnregisterPALMImage(void)
624 (void) UnregisterMagickInfo("PALM");
628 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
632 % W r i t e P A L M I m a g e %
636 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
638 % WritePALMImage() writes an image of raw bits in LSB order to a file.
640 % The format of the WritePALMImage method is:
642 % MagickBooleanType WritePALMImage(const ImageInfo *image_info,
643 % Image *image,ExceptionInfo *exception)
645 % A description of each parameter follows.
647 % o image_info: Specifies a pointer to an ImageInfo structure.
649 % o image: A pointer to a Image structure.
651 % o exception: return any errors or warnings in this structure.
654 static MagickBooleanType WritePALMImage(const ImageInfo *image_info,
655 Image *image,ExceptionInfo *exception)
680 register const Quantum
710 Open output image file.
712 assert(image_info != (const ImageInfo *) NULL);
713 assert(image_info->signature == MagickSignature);
714 assert(image != (Image *) NULL);
715 assert(image->signature == MagickSignature);
716 if (image->debug != MagickFalse)
717 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
718 assert(exception != (ExceptionInfo *) NULL);
719 assert(exception->signature == MagickSignature);
720 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
721 if (status == MagickFalse)
723 quantize_info=AcquireQuantizeInfo(image_info);
736 if (IssRGBCompatibleColorspace(image->colorspace) == MagickFalse)
737 (void) TransformImageColorspace(image,sRGBColorspace,exception);
738 count=GetNumberColors(image,NULL,exception);
739 for (bits_per_pixel=1; (one << bits_per_pixel) < count; bits_per_pixel*=2) ;
740 if (image_info->depth > 100)
741 bits_per_pixel=image_info->depth-100;
742 if (bits_per_pixel < 16)
743 (void) TransformImageColorspace(image,image->colorspace,exception);
744 if (bits_per_pixel < 8)
746 (void) TransformImageColorspace(image,GRAYColorspace,exception);
747 (void) SetImageType(image,PaletteType,exception);
748 (void) SortColormapByIntensity(image,exception);
750 if ((image->storage_class == PseudoClass) && (image->colors > 256))
751 (void) SetImageStorageClass(image,DirectClass,exception);
752 if (image->storage_class == PseudoClass)
753 flags|=PALM_HAS_COLORMAP_FLAG;
755 flags|=PALM_IS_DIRECT_COLOR;
756 (void) WriteBlobMSBShort(image,(unsigned short) image->columns); /* width */
757 (void) WriteBlobMSBShort(image,(unsigned short) image->rows); /* height */
758 bytes_per_row=((image->columns+(16/bits_per_pixel-1))/(16/
760 (void) WriteBlobMSBShort(image,(unsigned short) bytes_per_row);
761 if ((image_info->compression == RLECompression) ||
762 (image_info->compression == FaxCompression))
763 flags|=PALM_IS_COMPRESSED_FLAG;
764 (void) WriteBlobMSBShort(image, flags);
765 (void) WriteBlobByte(image,(unsigned char) bits_per_pixel);
766 if (bits_per_pixel > 1)
768 if ((image_info->compression == RLECompression) ||
769 (image_info->compression == FaxCompression))
771 (void) WriteBlobByte(image,version);
772 (void) WriteBlobMSBShort(image,0); /* nextDepthOffset */
773 (void) WriteBlobByte(image,(unsigned char) transparentIndex);
774 if (image_info->compression == RLECompression)
775 (void) WriteBlobByte(image,PALM_COMPRESSION_RLE);
777 if (image_info->compression == FaxCompression)
778 (void) WriteBlobByte(image,PALM_COMPRESSION_SCANLINE);
780 (void) WriteBlobByte(image,PALM_COMPRESSION_NONE);
781 (void) WriteBlobMSBShort(image,0); /* reserved */
783 if (bits_per_pixel == 16)
785 (void) WriteBlobByte(image,5); /* # of bits of red */
786 (void) WriteBlobByte(image,6); /* # of bits of green */
787 (void) WriteBlobByte(image,5); /* # of bits of blue */
788 (void) WriteBlobByte(image,0); /* reserved by Palm */
789 (void) WriteBlobMSBLong(image,0); /* no transparent color, YET */
792 if (bits_per_pixel == 8)
794 if (flags & PALM_HAS_COLORMAP_FLAG) /* Write out colormap */
796 quantize_info->dither_method=IsPaletteImage(image,exception)
797 == MagickFalse ? RiemersmaDitherMethod : NoDitherMethod;
798 quantize_info->number_colors=image->colors;
799 (void) QuantizeImage(quantize_info,image,exception);
800 (void) WriteBlobMSBShort(image,(unsigned short) image->colors);
801 for (count = 0; count < image->colors; count++)
803 (void) WriteBlobByte(image,(unsigned char) count);
804 (void) WriteBlobByte(image,ScaleQuantumToChar(
805 image->colormap[count].red));
806 (void) WriteBlobByte(image,
807 ScaleQuantumToChar(image->colormap[count].green));
808 (void) WriteBlobByte(image,
809 ScaleQuantumToChar(image->colormap[count].blue));
813 else /* Map colors to Palm standard colormap */
818 affinity_image=ConstituteImage(256,1,"RGB",CharPixel,&PalmPalette,
820 (void) TransformImageColorspace(affinity_image,
821 affinity_image->colorspace,exception);
822 (void) RemapImage(quantize_info,image,affinity_image,exception);
823 for (y=0; y < (ssize_t) image->rows; y++)
825 q=GetAuthenticPixels(image,0,y,image->columns,1,exception);
826 for (x=0; x < (ssize_t) image->columns; x++)
828 SetPixelIndex(image,FindColor(image,&image->colormap[(ssize_t)
829 GetPixelIndex(image,q)]),q);
830 q+=GetPixelChannels(image);
833 affinity_image=DestroyImage(affinity_image);
836 if (flags & PALM_IS_COMPRESSED_FLAG)
837 (void) WriteBlobMSBShort(image,0); /* fill in size later */
838 lastrow=(unsigned char *) NULL;
839 if (image_info->compression == FaxCompression)
840 lastrow=(unsigned char *) AcquireQuantumMemory(bytes_per_row,
842 /* TODO check whether memory really was acquired? */
843 one_row=(unsigned char *) AcquireQuantumMemory(bytes_per_row,
845 if (one_row == (unsigned char *) NULL)
846 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
847 for (y=0; y < (int) image->rows; y++)
850 (void) ResetMagickMemory(ptr,0,bytes_per_row);
851 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
852 if (p == (const Quantum *) NULL)
854 if (bits_per_pixel == 16)
856 for (x=0; x < (int) image->columns; x++)
858 color16=(unsigned short) ((((31*(size_t) GetPixelRed(image,p))/
859 (size_t) QuantumRange) << 11) | (((63*(size_t)
860 GetPixelGreen(image,p))/(size_t) QuantumRange) << 5) |
861 ((31*(size_t) GetPixelBlue(image,p))/(size_t) QuantumRange));
862 if (GetPixelAlpha(image,p) == (Quantum) TransparentAlpha)
864 transpix.red=GetPixelRed(image,p);
865 transpix.green=GetPixelGreen(image,p);
866 transpix.blue=GetPixelBlue(image,p);
867 transpix.alpha=GetPixelAlpha(image,p);
868 flags|=PALM_HAS_TRANSPARENCY_FLAG;
870 *ptr++=(unsigned char) ((color16 >> 8) & 0xff);
871 *ptr++=(unsigned char) (color16 & 0xff);
872 p+=GetPixelChannels(image);
878 bit=(unsigned char) (8-bits_per_pixel);
879 for (x=0; x < (int) image->columns; x++)
881 if (bits_per_pixel >= 8)
882 color=(unsigned char) GetPixelIndex(image,p);
884 color=(unsigned char) (GetPixelIndex(image,p)*
885 ((one << bits_per_pixel)-1)/MagickMax(1*image->colors-1,1));
888 bit-=(unsigned char) bits_per_pixel;
893 bit=(unsigned char) (8-bits_per_pixel);
895 p+=GetPixelChannels(image);
897 if ((image->columns % (8/bits_per_pixel)) != 0)
900 if (image_info->compression == RLECompression)
903 while (x < (ssize_t) bytes_per_row)
907 while ((one_row[++x] == byte) && (count < 255) &&
908 (x < (ssize_t) bytes_per_row))
910 (void) WriteBlobByte(image,(unsigned char) count);
911 (void) WriteBlobByte(image,(unsigned char) byte);
915 if (image_info->compression == FaxCompression)
921 for (x = 0; x < (ssize_t) bytes_per_row; x += 8)
924 for (bit=0, byte=0; bit < (unsigned char) MagickMin(8,(ssize_t) bytes_per_row-x); bit++)
926 if ((y == 0) || (lastrow[x + bit] != one_row[x + bit]))
928 byte |= (1 << (7 - bit));
929 *tptr++ = (char) one_row[x + bit];
932 (void) WriteBlobByte(image, byte);
933 (void) WriteBlob(image,tptr-tmpbuf,(unsigned char *) tmpbuf);
935 (void) CopyMagickMemory(lastrow,one_row,bytes_per_row);
938 (void) WriteBlob(image,bytes_per_row,one_row);
940 if (flags & PALM_HAS_TRANSPARENCY_FLAG)
942 offset=SeekBlob(image,currentOffset+6,SEEK_SET);
943 (void) WriteBlobMSBShort(image,flags);
944 offset=SeekBlob(image,currentOffset+12,SEEK_SET);
945 (void) WriteBlobByte(image,(unsigned char) transparentIndex); /* trans index */
947 if (bits_per_pixel == 16)
949 offset=SeekBlob(image,currentOffset+20,SEEK_SET);
950 (void) WriteBlobByte(image,0); /* reserved by Palm */
951 (void) WriteBlobByte(image,(unsigned char) ((31*transpix.red)/QuantumRange));
952 (void) WriteBlobByte(image,(unsigned char) ((63*transpix.green)/QuantumRange));
953 (void) WriteBlobByte(image,(unsigned char) ((31*transpix.blue)/QuantumRange));
955 if (flags & PALM_IS_COMPRESSED_FLAG) /* fill in size now */
957 offset=SeekBlob(image,currentOffset+offset,SEEK_SET);
958 (void) WriteBlobMSBShort(image,(unsigned short) (GetBlobSize(image)-
959 currentOffset-offset));
961 if (one_row != (unsigned char *) NULL)
962 one_row=(unsigned char *) RelinquishMagickMemory(one_row);
963 if (lastrow != (unsigned char *) NULL)
964 lastrow=(unsigned char *) RelinquishMagickMemory(lastrow);
965 if (GetNextImageInList(image) == (Image *) NULL)
967 /* padding to 4 byte word */
968 for (cc = (GetBlobSize(image))%4; cc > 0; cc--)
970 (void) WriteBlobByte(image,0);
972 /* write nextDepthOffset and return to end of image */
973 offset=SeekBlob(image,currentOffset+10,SEEK_SET);
974 nextDepthOffset=(size_t) ((GetBlobSize(image)-currentOffset)/4);
975 (void) WriteBlobMSBShort(image,(unsigned short) nextDepthOffset);
976 currentOffset=(MagickOffsetType) GetBlobSize(image);
977 offset=SeekBlob(image,currentOffset,SEEK_SET);
978 image=SyncNextImageInList(image);
979 status=SetImageProgress(image,SaveImagesTag,scene++,
980 GetImageListLength(image));
981 if (status == MagickFalse)
983 } while (image_info->adjoin != MagickFalse);
984 quantize_info=DestroyQuantizeInfo(quantize_info);
985 (void) CloseBlob(image);