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 "magick/studio.h"
45 #include "magick/blob.h"
46 #include "magick/blob-private.h"
47 #include "magick/cache.h"
48 #include "magick/color.h"
49 #include "magick/colormap.h"
50 #include "magick/colormap-private.h"
51 #include "magick/color-private.h"
52 #include "magick/colorspace.h"
53 #include "magick/constitute.h"
54 #include "magick/exception.h"
55 #include "magick/histogram.h"
56 #include "magick/image.h"
57 #include "magick/list.h"
58 #include "magick/magick.h"
59 #include "magick/memory_.h"
60 #include "magick/monitor.h"
61 #include "magick/monitor-private.h"
62 #include "magick/paint.h"
63 #include "magick/pixel-private.h"
64 #include "magick/property.h"
65 #include "magick/quantize.h"
66 #include "magick/quantum-private.h"
67 #include "magick/static.h"
68 #include "magick/string_.h"
69 #include "magick/module.h"
70 #include "magick/utility.h"
75 #define PALM_IS_COMPRESSED_FLAG 0x8000
76 #define PALM_HAS_COLORMAP_FLAG 0x4000
77 #define PALM_HAS_FOUR_BYTE_FIELD 0x0200
78 #define PALM_HAS_TRANSPARENCY_FLAG 0x2000
79 #define PALM_IS_INDIRECT 0x1000
80 #define PALM_IS_FOR_SCREEN 0x0800
81 #define PALM_IS_DIRECT_COLOR 0x0400
82 #define PALM_COMPRESSION_SCANLINE 0x00
83 #define PALM_COMPRESSION_RLE 0x01
84 #define PALM_COMPRESSION_NONE 0xFF
87 The 256 color system palette for Palm Computing Devices.
92 {255, 255,255}, {255, 204,255}, {255, 153,255}, {255, 102,255},
93 {255, 51,255}, {255, 0,255}, {255, 255,204}, {255, 204,204},
94 {255, 153,204}, {255, 102,204}, {255, 51,204}, {255, 0,204},
95 {255, 255,153}, {255, 204,153}, {255, 153,153}, {255, 102,153},
96 {255, 51,153}, {255, 0,153}, {204, 255,255}, {204, 204,255},
97 {204, 153,255}, {204, 102,255}, {204, 51,255}, {204, 0,255},
98 {204, 255,204}, {204, 204,204}, {204, 153,204}, {204, 102,204},
99 {204, 51,204}, {204, 0,204}, {204, 255,153}, {204, 204,153},
100 {204, 153,153}, {204, 102,153}, {204, 51,153}, {204, 0,153},
101 {153, 255,255}, {153, 204,255}, {153, 153,255}, {153, 102,255},
102 {153, 51,255}, {153, 0,255}, {153, 255,204}, {153, 204,204},
103 {153, 153,204}, {153, 102,204}, {153, 51,204}, {153, 0,204},
104 {153, 255,153}, {153, 204,153}, {153, 153,153}, {153, 102,153},
105 {153, 51,153}, {153, 0,153}, {102, 255,255}, {102, 204,255},
106 {102, 153,255}, {102, 102,255}, {102, 51,255}, {102, 0,255},
107 {102, 255,204}, {102, 204,204}, {102, 153,204}, {102, 102,204},
108 {102, 51,204}, {102, 0,204}, {102, 255,153}, {102, 204,153},
109 {102, 153,153}, {102, 102,153}, {102, 51,153}, {102, 0,153},
110 { 51, 255,255}, { 51, 204,255}, { 51, 153,255}, { 51, 102,255},
111 { 51, 51,255}, { 51, 0,255}, { 51, 255,204}, { 51, 204,204},
112 { 51, 153,204}, { 51, 102,204}, { 51, 51,204}, { 51, 0,204},
113 { 51, 255,153}, { 51, 204,153}, { 51, 153,153}, { 51, 102,153},
114 { 51, 51,153}, { 51, 0,153}, { 0, 255,255}, { 0, 204,255},
115 { 0, 153,255}, { 0, 102,255}, { 0, 51,255}, { 0, 0,255},
116 { 0, 255,204}, { 0, 204,204}, { 0, 153,204}, { 0, 102,204},
117 { 0, 51,204}, { 0, 0,204}, { 0, 255,153}, { 0, 204,153},
118 { 0, 153,153}, { 0, 102,153}, { 0, 51,153}, { 0, 0,153},
119 {255, 255,102}, {255, 204,102}, {255, 153,102}, {255, 102,102},
120 {255, 51,102}, {255, 0,102}, {255, 255, 51}, {255, 204, 51},
121 {255, 153, 51}, {255, 102, 51}, {255, 51, 51}, {255, 0, 51},
122 {255, 255, 0}, {255, 204, 0}, {255, 153, 0}, {255, 102, 0},
123 {255, 51, 0}, {255, 0, 0}, {204, 255,102}, {204, 204,102},
124 {204, 153,102}, {204, 102,102}, {204, 51,102}, {204, 0,102},
125 {204, 255, 51}, {204, 204, 51}, {204, 153, 51}, {204, 102, 51},
126 {204, 51, 51}, {204, 0, 51}, {204, 255, 0}, {204, 204, 0},
127 {204, 153, 0}, {204, 102, 0}, {204, 51, 0}, {204, 0, 0},
128 {153, 255,102}, {153, 204,102}, {153, 153,102}, {153, 102,102},
129 {153, 51,102}, {153, 0,102}, {153, 255, 51}, {153, 204, 51},
130 {153, 153, 51}, {153, 102, 51}, {153, 51, 51}, {153, 0, 51},
131 {153, 255, 0}, {153, 204, 0}, {153, 153, 0}, {153, 102, 0},
132 {153, 51, 0}, {153, 0, 0}, {102, 255,102}, {102, 204,102},
133 {102, 153,102}, {102, 102,102}, {102, 51,102}, {102, 0,102},
134 {102, 255, 51}, {102, 204, 51}, {102, 153, 51}, {102, 102, 51},
135 {102, 51, 51}, {102, 0, 51}, {102, 255, 0}, {102, 204, 0},
136 {102, 153, 0}, {102, 102, 0}, {102, 51, 0}, {102, 0, 0},
137 { 51, 255,102}, { 51, 204,102}, { 51, 153,102}, { 51, 102,102},
138 { 51, 51,102}, { 51, 0,102}, { 51, 255, 51}, { 51, 204, 51},
139 { 51, 153, 51}, { 51, 102, 51}, { 51, 51, 51}, { 51, 0, 51},
140 { 51, 255, 0}, { 51, 204, 0}, { 51, 153, 0}, { 51, 102, 0},
141 { 51, 51, 0}, { 51, 0, 0}, { 0, 255,102}, { 0, 204,102},
142 { 0, 153,102}, { 0, 102,102}, { 0, 51,102}, { 0, 0,102},
143 { 0, 255, 51}, { 0, 204, 51}, { 0, 153, 51}, { 0, 102, 51},
144 { 0, 51, 51}, { 0, 0, 51}, { 0, 255, 0}, { 0, 204, 0},
145 { 0, 153, 0}, { 0, 102, 0}, { 0, 51, 0}, { 17, 17, 17},
146 { 34, 34, 34}, { 68, 68, 68}, { 85, 85, 85}, {119, 119,119},
147 {136, 136,136}, {170, 170,170}, {187, 187,187}, {221, 221,221},
148 {238, 238,238}, {192, 192,192}, {128, 0, 0}, {128, 0,128},
149 { 0, 128, 0}, { 0, 128,128}, { 0, 0, 0}, { 0, 0, 0},
150 { 0, 0, 0}, { 0, 0, 0}, { 0, 0, 0}, { 0, 0, 0},
151 { 0, 0, 0}, { 0, 0, 0}, { 0, 0, 0}, { 0, 0, 0},
152 { 0, 0, 0}, { 0, 0, 0}, { 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}
159 Forward declarations.
161 static MagickBooleanType
162 WritePALMImage(const ImageInfo *,Image *);
165 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
169 % F i n d C o l o r %
173 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
175 % FindColor() returns the index of the matching entry from PalmPalette for a
178 % The format of the FindColor method is:
180 % int FindColor(PixelPacket *pixel)
182 % A description of each parameter follows:
184 % o int: the index of the matching color or -1 if not found/
186 % o pixel: a pointer to the PixelPacket to be matched.
189 static int FindColor(PixelPacket *pixel)
194 for (i=0; i < 256; i++)
195 if (ScaleQuantumToChar(pixel->red) == PalmPalette[i][0] &&
196 ScaleQuantumToChar(pixel->green) == PalmPalette[i][1] &&
197 ScaleQuantumToChar(pixel->blue) == PalmPalette[i][2])
203 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
207 % R e a d P A L M I m a g e %
211 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
213 % ReadPALMImage() reads an image of raw bites in LSB order and returns it. It
214 % allocates the memory necessary for the new Image structure and returns a
215 % pointer to the new image.
217 % The format of the ReadPALMImage method is:
219 % Image *ReadPALMImage(const ImageInfo *image_info,
220 % ExceptionInfo *exception)
222 % A description of each parameter follows:
224 % o image_info: Specifies a pointer to an ImageInfo structure.
226 % o exception: return any errors or warnings in this structure.
230 static inline size_t MagickMax(const size_t x,const size_t y)
237 static inline ssize_t MagickMin(const ssize_t x,const ssize_t y)
244 static Image *ReadPALMImage(const ImageInfo *image_info,
245 ExceptionInfo *exception)
308 assert(image_info != (const ImageInfo *) NULL);
309 assert(image_info->signature == MagickSignature);
310 if (image_info->debug != MagickFalse)
311 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
312 image_info->filename);
313 assert(exception != (ExceptionInfo *) NULL);
314 assert(exception->signature == MagickSignature);
315 image=AcquireImage(image_info);
316 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
317 if (status == MagickFalse)
319 (void) DestroyImageList(image);
320 return((Image *) NULL);
325 image->columns=ReadBlobMSBShort(image);
326 image->rows=ReadBlobMSBShort(image);
327 if (EOFBlob(image) != MagickFalse)
328 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
330 Copied from coders/pnm.c. TODO other checks ...
332 if ((image->columns == 0) || (image->rows == 0))
333 ThrowReaderException(CorruptImageError,"NegativeOrZeroImageSize");
334 bytes_per_row=ReadBlobMSBShort(image);
335 flags=ReadBlobMSBShort(image);
336 bits_per_pixel=(size_t) ReadBlobByte(image);
337 if (bits_per_pixel > 16)
338 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
339 version=(size_t) ReadBlobByte(image);
341 nextDepthOffset=(size_t) ReadBlobMSBShort(image);
342 transparentIndex=(size_t) ReadBlobByte(image);
343 compressionType=(size_t) ReadBlobByte(image);
344 pad=ReadBlobMSBShort(image);
347 Initialize image colormap.
350 if ((bits_per_pixel < 16) &&
351 (AcquireImageColormap(image,one << bits_per_pixel) == MagickFalse))
352 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
353 GetMagickPixelPacket(image,&transpix);
354 if (bits_per_pixel == 16) /* Direct Color */
356 redbits=(size_t) ReadBlobByte(image); /* # of bits of red */
358 greenbits=(size_t) ReadBlobByte(image); /* # of bits of green */
360 bluebits=(size_t) ReadBlobByte(image); /* # of bits of blue */
362 ReadBlobByte(image); /* reserved by Palm */
363 ReadBlobByte(image); /* reserved by Palm */
364 transpix.red=(MagickRealType) (QuantumRange*ReadBlobByte(image)/31);
365 transpix.green=(MagickRealType) (QuantumRange*ReadBlobByte(image)/63);
366 transpix.blue=(MagickRealType) (QuantumRange*ReadBlobByte(image)/31);
368 if (bits_per_pixel == 8)
373 if (flags & PALM_HAS_COLORMAP_FLAG)
375 count=(ssize_t) ReadBlobMSBShort(image);
376 for (i=0; i < (ssize_t) count; i++)
379 index=ConstrainColormapIndex(image,255-i);
380 image->colormap[(int) index].red=
381 ScaleCharToQuantum((unsigned char) ReadBlobByte(image));
382 image->colormap[(int) index].green=
383 ScaleCharToQuantum((unsigned char) ReadBlobByte(image));
384 image->colormap[(int) index].blue=
385 ScaleCharToQuantum((unsigned char) ReadBlobByte(image));
390 for (i=0; i < (ssize_t) (1L << bits_per_pixel); i++)
392 index=ConstrainColormapIndex(image,255-i);
393 image->colormap[(int) index].red=
394 ScaleCharToQuantum(PalmPalette[i][0]);
395 image->colormap[(int) index].green=
396 ScaleCharToQuantum(PalmPalette[i][1]);
397 image->colormap[(int) index].blue=
398 ScaleCharToQuantum(PalmPalette[i][2]);
402 if (flags & PALM_IS_COMPRESSED_FLAG)
403 size=ReadBlobMSBShort(image);
405 image->storage_class=DirectClass;
406 if (bits_per_pixel < 16)
408 image->storage_class=PseudoClass;
411 one_row=(unsigned char *) AcquireQuantumMemory(bytes_per_row,
413 if (one_row == (unsigned char *) NULL)
414 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
415 lastrow=(unsigned char *) NULL;
416 if (compressionType == PALM_COMPRESSION_SCANLINE) {
417 lastrow=(unsigned char *) AcquireQuantumMemory(bytes_per_row,
419 if (lastrow == (unsigned char *) NULL)
420 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
422 mask=(1l << bits_per_pixel)-1;
423 for (y = 0; y < (ssize_t) image->rows; y++)
425 if ((flags & PALM_IS_COMPRESSED_FLAG) == 0)
427 /* TODO move out of loop! */
428 image->compression=NoCompression;
429 count=ReadBlob(image,bytes_per_row,one_row);
433 if (compressionType == PALM_COMPRESSION_RLE)
435 /* TODO move out of loop! */
436 image->compression=RLECompression;
437 for (i=0; i < (ssize_t) bytes_per_row; )
439 count=(ssize_t) ReadBlobByte(image);
440 count=MagickMin(count,(ssize_t) bytes_per_row-i);
441 byte=(size_t) ReadBlobByte(image);
442 (void) ResetMagickMemory(one_row+i,(int) byte,(size_t) count);
447 if (compressionType == PALM_COMPRESSION_SCANLINE)
452 /* TODO move out of loop! */
454 image->compression=FaxCompression;
455 for (i=0; i < (ssize_t) bytes_per_row; i+=8)
457 count=(ssize_t) ReadBlobByte(image);
458 byte=1UL*MagickMin((ssize_t) bytes_per_row-i,8);
459 for (bit=0; bit < byte; bit++)
461 if ((y == 0) || (count & (one << (7 - bit))))
462 one_row[i+bit]=(unsigned char) ReadBlobByte(image);
464 one_row[i+bit]=lastrow[i+bit];
467 (void) CopyMagickMemory(lastrow, one_row, bytes_per_row);
471 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
472 if (q == (PixelPacket *) NULL)
474 indexes=GetAuthenticIndexQueue(image);
475 if (bits_per_pixel == 16)
477 if (image->columns > (2*bytes_per_row))
478 ThrowReaderException(CorruptImageError,"CorruptImage");
479 for (x=0; x < (ssize_t) image->columns; x++)
481 color16=(*ptr++ << 8);
483 q->red=(Quantum) ((QuantumRange*((color16 >> 11) & 0x1f))/0x1f);
484 q->green=(Quantum) ((QuantumRange*((color16 >> 5) & 0x3f))/0x3f);
485 q->blue=(Quantum) ((QuantumRange*((color16 >> 0) & 0x1f))/0x1f);
486 SetOpacityPixelComponent(q,OpaqueOpacity);
492 bit=8-bits_per_pixel;
493 for (x=0; x < (ssize_t) image->columns; x++)
495 if ((size_t) (ptr-one_row) >= bytes_per_row)
496 ThrowReaderException(CorruptImageError,"CorruptImage");
497 index=(IndexPacket) (mask-(((*ptr) & (mask << bit)) >> bit));
499 *q++=image->colormap[(ssize_t) index];
505 bit=8-bits_per_pixel;
508 if (SyncAuthenticPixels(image,exception) == MagickFalse)
511 if (image->previous == (Image *) NULL)
513 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
515 if (status == MagickFalse)
519 if (flags & PALM_HAS_TRANSPARENCY_FLAG)
521 if (bits_per_pixel != 16)
522 SetMagickPixelPacket(image,image->colormap+(mask-transparentIndex),
523 (const IndexPacket *) NULL,&transpix);
524 (void) TransparentPaintImage(image,&transpix,(Quantum)
525 TransparentOpacity,MagickFalse);
527 one_row=(unsigned char *) RelinquishMagickMemory(one_row);
528 if (compressionType == PALM_COMPRESSION_SCANLINE)
529 lastrow=(unsigned char *) RelinquishMagickMemory(lastrow);
531 Proceed to next image. Copied from coders/pnm.c
533 if (image_info->number_scenes != 0)
534 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
536 if (nextDepthOffset != 0)
541 totalOffset+=(MagickOffsetType) (nextDepthOffset*4);
542 if (totalOffset >= (MagickOffsetType) GetBlobSize(image))
544 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
548 seekNextDepth=SeekBlob(image,totalOffset,SEEK_SET);
550 if (seekNextDepth != totalOffset)
551 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
553 Allocate next image structure. Copied from coders/pnm.c
555 AcquireNextImage(image_info,image);
556 if (GetNextImageInList(image) == (Image *) NULL)
558 (void) DestroyImageList(image);
559 return((Image *) NULL);
561 image=SyncNextImageInList(image);
562 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
564 if (status == MagickFalse)
567 } while (nextDepthOffset != 0);
568 (void) CloseBlob(image);
569 return(GetFirstImageInList(image));
573 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
577 % R e g i s t e r P A L M I m a g e %
581 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
583 % RegisterPALMImage() adds properties for the PALM image format to the list of
584 % supported formats. The properties include the image format tag, a method to
585 % read and/or write the format, whether the format supports the saving of more
586 % than one frame to the same file or blob, whether the format supports native
587 % in-memory I/O, and a brief description of the format.
589 % The format of the RegisterPALMImage method is:
591 % size_t RegisterPALMImage(void)
594 ModuleExport size_t RegisterPALMImage(void)
599 entry=SetMagickInfo("PALM");
600 entry->decoder=(DecodeImageHandler *) ReadPALMImage;
601 entry->encoder=(EncodeImageHandler *) WritePALMImage;
602 entry->seekable_stream=MagickTrue;
603 entry->description=ConstantString("Palm pixmap");
604 entry->module=ConstantString("PALM");
605 (void) RegisterMagickInfo(entry);
606 return(MagickImageCoderSignature);
610 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
614 % U n r e g i s t e r P A L M I m a g e %
618 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
620 % UnregisterPALMImage() removes format registrations made by the PALM
621 % module from the list of supported formats.
623 % The format of the UnregisterPALMImage method is:
625 % UnregisterPALMImage(void)
628 ModuleExport void UnregisterPALMImage(void)
630 (void) UnregisterMagickInfo("PALM");
634 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
638 % W r i t e P A L M I m a g e %
642 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
644 % WritePALMImage() writes an image of raw bits in LSB order to a file.
646 % The format of the WritePALMImage method is:
648 % MagickBooleanType WritePALMImage(const ImageInfo *image_info,
651 % A description of each parameter follows.
653 % o image_info: Specifies a pointer to an ImageInfo structure.
655 % o image: A pointer to a Image structure.
658 static MagickBooleanType WritePALMImage(const ImageInfo *image_info,
718 Open output image file.
720 assert(image_info != (const ImageInfo *) NULL);
721 assert(image_info->signature == MagickSignature);
722 assert(image != (Image *) NULL);
723 assert(image->signature == MagickSignature);
724 if (image->debug != MagickFalse)
725 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
726 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&exception);
727 if (status == MagickFalse)
729 GetExceptionInfo(&exception);
730 quantize_info=AcquireQuantizeInfo(image_info);
743 if (image->colorspace != RGBColorspace)
744 (void) TransformImageColorspace(image,RGBColorspace);
745 count=GetNumberColors(image,NULL,&exception);
746 for (bits_per_pixel=1; (one << bits_per_pixel) < count; bits_per_pixel*=2) ;
747 if (image_info->depth > 100)
748 bits_per_pixel=image_info->depth-100;
749 if (bits_per_pixel < 16)
750 (void) TransformImageColorspace(image,image->colorspace);
751 if (bits_per_pixel < 8)
753 (void) TransformImageColorspace(image,GRAYColorspace);
754 (void) SetImageType(image,PaletteType);
755 (void) SortColormapByIntensity(image);
757 if ((image->storage_class == PseudoClass) && (image->colors > 256))
758 (void) SetImageStorageClass(image,DirectClass);
759 if (image->storage_class == PseudoClass)
760 flags|=PALM_HAS_COLORMAP_FLAG;
762 flags|=PALM_IS_DIRECT_COLOR;
763 (void) WriteBlobMSBShort(image,(unsigned short) image->columns); /* width */
764 (void) WriteBlobMSBShort(image,(unsigned short) image->rows); /* height */
765 bytes_per_row=((image->columns+(16/bits_per_pixel-1))/(16/
767 (void) WriteBlobMSBShort(image,(unsigned short) bytes_per_row);
768 if ((image_info->compression == RLECompression) ||
769 (image_info->compression == FaxCompression))
770 flags|=PALM_IS_COMPRESSED_FLAG;
771 (void) WriteBlobMSBShort(image, flags);
772 (void) WriteBlobByte(image,(unsigned char) bits_per_pixel);
773 if (bits_per_pixel > 1)
775 if ((image_info->compression == RLECompression) ||
776 (image_info->compression == FaxCompression))
778 (void) WriteBlobByte(image,version);
779 (void) WriteBlobMSBShort(image,0); /* nextDepthOffset */
780 (void) WriteBlobByte(image,(unsigned char) transparentIndex);
781 if (image_info->compression == RLECompression)
782 (void) WriteBlobByte(image,PALM_COMPRESSION_RLE);
784 if (image_info->compression == FaxCompression)
785 (void) WriteBlobByte(image,PALM_COMPRESSION_SCANLINE);
787 (void) WriteBlobByte(image,PALM_COMPRESSION_NONE);
788 (void) WriteBlobMSBShort(image,0); /* reserved */
790 if (bits_per_pixel == 16)
792 (void) WriteBlobByte(image,5); /* # of bits of red */
793 (void) WriteBlobByte(image,6); /* # of bits of green */
794 (void) WriteBlobByte(image,5); /* # of bits of blue */
795 (void) WriteBlobByte(image,0); /* reserved by Palm */
796 (void) WriteBlobMSBLong(image,0); /* no transparent color, YET */
799 if (bits_per_pixel == 8)
801 if (flags & PALM_HAS_COLORMAP_FLAG) /* Write out colormap */
803 quantize_info->dither=IsPaletteImage(image,&image->exception);
804 quantize_info->number_colors=image->colors;
805 (void) QuantizeImage(quantize_info,image);
806 (void) WriteBlobMSBShort(image,(unsigned short) image->colors);
807 for (count = 0; count < image->colors; count++)
809 (void) WriteBlobByte(image,(unsigned char) count);
810 (void) WriteBlobByte(image,ScaleQuantumToChar(
811 image->colormap[count].red));
812 (void) WriteBlobByte(image,
813 ScaleQuantumToChar(image->colormap[count].green));
814 (void) WriteBlobByte(image,
815 ScaleQuantumToChar(image->colormap[count].blue));
819 else /* Map colors to Palm standard colormap */
824 affinity_image=ConstituteImage(256,1,"RGB",CharPixel,&PalmPalette,
826 (void) TransformImageColorspace(affinity_image,affinity_image->colorspace);
827 (void) RemapImage(quantize_info,image,affinity_image);
828 for (y=0; y < (ssize_t) image->rows; y++)
830 p=GetAuthenticPixels(image,0,y,image->columns,1,&exception);
831 indexes=GetAuthenticIndexQueue(image);
832 for (x=0; x < (ssize_t) image->columns; x++)
833 indexes[x]=(IndexPacket) FindColor(&image->colormap[(ssize_t)
836 affinity_image=DestroyImage(affinity_image);
839 if (flags & PALM_IS_COMPRESSED_FLAG)
840 (void) WriteBlobMSBShort(image,0); /* fill in size later */
841 lastrow=(unsigned char *) NULL;
842 if (image_info->compression == FaxCompression)
843 lastrow=(unsigned char *) AcquireQuantumMemory(bytes_per_row,
845 /* TODO check whether memory really was acquired? */
846 one_row=(unsigned char *) AcquireQuantumMemory(bytes_per_row,
848 if (one_row == (unsigned char *) NULL)
849 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
850 for (y=0; y < (int) image->rows; y++)
853 (void) ResetMagickMemory(ptr,0,bytes_per_row);
854 p=GetAuthenticPixels(image,0,y,image->columns,1,&exception);
855 if (p == (PixelPacket *) NULL)
857 indexes=GetAuthenticIndexQueue(image);
858 if (bits_per_pixel == 16)
860 for (x=0; x < (int) image->columns; x++)
862 color16=(unsigned short) ((((31*(size_t) GetRedPixelComponent(p))/
863 (size_t) QuantumRange) << 11) |
864 (((63*(size_t) GetGreenPixelComponent(p))/(size_t) QuantumRange) << 5) |
865 ((31*(size_t) GetBluePixelComponent(p))/(size_t) QuantumRange));
866 if (p->opacity == (Quantum) TransparentOpacity)
868 transpix.red=GetRedPixelComponent(p);
869 transpix.green=GetGreenPixelComponent(p);
870 transpix.blue=GetBluePixelComponent(p);
871 transpix.opacity=GetOpacityPixelComponent(p);
872 flags|=PALM_HAS_TRANSPARENCY_FLAG;
874 *ptr++=(unsigned char) ((color16 >> 8) & 0xff);
875 *ptr++=(unsigned char) (color16 & 0xff);
882 bit=(unsigned char) (8-bits_per_pixel);
883 for (x=0; x < (int) image->columns; x++)
885 if (bits_per_pixel >= 8)
886 color=(unsigned char) indexes[x];
888 color=(unsigned char) (indexes[x]*((one << bits_per_pixel)-1)/
889 MagickMax(1*image->colors-1,1));
892 bit-=(unsigned char) bits_per_pixel;
897 bit=(unsigned char) (8-bits_per_pixel);
900 if ((image->columns % (8/bits_per_pixel)) != 0)
903 if (image_info->compression == RLECompression)
906 while (x < (ssize_t) bytes_per_row)
910 while ((one_row[++x] == byte) && (count < 255) &&
911 (x < (ssize_t) bytes_per_row))
913 (void) WriteBlobByte(image,(unsigned char) count);
914 (void) WriteBlobByte(image,(unsigned char) byte);
918 if (image_info->compression == FaxCompression)
924 for (x = 0; x < (ssize_t) bytes_per_row; x += 8)
927 for (bit=0, byte=0; bit < (unsigned char) MagickMin(8,(ssize_t) bytes_per_row-x); bit++)
929 if ((y == 0) || (lastrow[x + bit] != one_row[x + bit]))
931 byte |= (1 << (7 - bit));
932 *tptr++ = (char) one_row[x + bit];
935 (void) WriteBlobByte(image, byte);
936 (void) WriteBlob(image,tptr-tmpbuf,(unsigned char *) tmpbuf);
938 (void) CopyMagickMemory(lastrow,one_row,bytes_per_row);
941 (void) WriteBlob(image,bytes_per_row,one_row);
943 if (flags & PALM_HAS_TRANSPARENCY_FLAG)
945 offset=SeekBlob(image,currentOffset+6,SEEK_SET);
946 (void) WriteBlobMSBShort(image,flags);
947 offset=SeekBlob(image,currentOffset+12,SEEK_SET);
948 (void) WriteBlobByte(image,(unsigned char) transparentIndex); /* trans index */
950 if (bits_per_pixel == 16)
952 offset=SeekBlob(image,currentOffset+20,SEEK_SET);
953 (void) WriteBlobByte(image,0); /* reserved by Palm */
954 (void) WriteBlobByte(image,(unsigned char) ((31*transpix.red)/QuantumRange));
955 (void) WriteBlobByte(image,(unsigned char) ((63*transpix.green)/QuantumRange));
956 (void) WriteBlobByte(image,(unsigned char) ((31*transpix.blue)/QuantumRange));
958 if (flags & PALM_IS_COMPRESSED_FLAG) /* fill in size now */
960 offset=SeekBlob(image,currentOffset+offset,SEEK_SET);
961 (void) WriteBlobMSBShort(image,(unsigned short) (GetBlobSize(image)-
962 currentOffset-offset));
964 if (one_row != (unsigned char *) NULL)
965 one_row=(unsigned char *) RelinquishMagickMemory(one_row);
966 if (lastrow != (unsigned char *) NULL)
967 lastrow=(unsigned char *) RelinquishMagickMemory(lastrow);
968 if (GetNextImageInList(image) == (Image *) NULL)
970 /* padding to 4 byte word */
971 for (cc = (GetBlobSize(image))%4; cc > 0; cc--)
973 (void) WriteBlobByte(image,0);
975 /* write nextDepthOffset and return to end of image */
976 offset=SeekBlob(image,currentOffset+10,SEEK_SET);
977 nextDepthOffset=(size_t) ((GetBlobSize(image)-currentOffset)/4);
978 (void) WriteBlobMSBShort(image,(unsigned short) nextDepthOffset);
979 currentOffset=(MagickOffsetType) GetBlobSize(image);
980 offset=SeekBlob(image,currentOffset,SEEK_SET);
981 image=SyncNextImageInList(image);
982 status=SetImageProgress(image,SaveImagesTag,scene++,
983 GetImageListLength(image));
984 if (status == MagickFalse)
986 } while (image_info->adjoin != MagickFalse);
987 quantize_info=DestroyQuantizeInfo(quantize_info);
988 (void) CloseBlob(image);
989 (void) DestroyExceptionInfo(&exception);