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);
340 nextDepthOffset=(size_t) ReadBlobMSBShort(image);
341 transparentIndex=(size_t) ReadBlobByte(image);
342 compressionType=(size_t) ReadBlobByte(image);
343 pad=ReadBlobMSBShort(image);
345 Initialize image colormap.
348 if ((bits_per_pixel < 16) &&
349 (AcquireImageColormap(image,one << bits_per_pixel) == MagickFalse))
350 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
351 GetMagickPixelPacket(image,&transpix);
352 if (bits_per_pixel == 16) /* Direct Color */
354 redbits=(size_t) ReadBlobByte(image); /* # of bits of red */
355 greenbits=(size_t) ReadBlobByte(image); /* # of bits of green */
356 bluebits=(size_t) ReadBlobByte(image); /* # of bits of blue */
357 ReadBlobByte(image); /* reserved by Palm */
358 ReadBlobByte(image); /* reserved by Palm */
359 transpix.red=(MagickRealType) (QuantumRange*ReadBlobByte(image)/31);
360 transpix.green=(MagickRealType) (QuantumRange*ReadBlobByte(image)/63);
361 transpix.blue=(MagickRealType) (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);
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);
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);
399 image->storage_class=DirectClass;
400 if (bits_per_pixel < 16)
402 image->storage_class=PseudoClass;
405 one_row=(unsigned char *) AcquireQuantumMemory(bytes_per_row,
407 if (one_row == (unsigned char *) NULL)
408 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
409 lastrow=(unsigned char *) NULL;
410 if (compressionType == PALM_COMPRESSION_SCANLINE) {
411 lastrow=(unsigned char *) AcquireQuantumMemory(bytes_per_row,
413 if (lastrow == (unsigned char *) NULL)
414 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
416 mask=(1l << bits_per_pixel)-1;
417 for (y = 0; y < (ssize_t) image->rows; y++)
419 if ((flags & PALM_IS_COMPRESSED_FLAG) == 0)
421 /* TODO move out of loop! */
422 image->compression=NoCompression;
423 count=ReadBlob(image,bytes_per_row,one_row);
427 if (compressionType == PALM_COMPRESSION_RLE)
429 /* TODO move out of loop! */
430 image->compression=RLECompression;
431 for (i=0; i < (ssize_t) bytes_per_row; )
433 count=(ssize_t) ReadBlobByte(image);
434 count=MagickMin(count,(ssize_t) bytes_per_row-i);
435 byte=(size_t) ReadBlobByte(image);
436 (void) ResetMagickMemory(one_row+i,(int) byte,(size_t) count);
441 if (compressionType == PALM_COMPRESSION_SCANLINE)
446 /* TODO move out of loop! */
448 image->compression=FaxCompression;
449 for (i=0; i < (ssize_t) bytes_per_row; i+=8)
451 count=(ssize_t) ReadBlobByte(image);
452 byte=1UL*MagickMin((ssize_t) bytes_per_row-i,8);
453 for (bit=0; bit < byte; bit++)
455 if ((y == 0) || (count & (one << (7 - bit))))
456 one_row[i+bit]=(unsigned char) ReadBlobByte(image);
458 one_row[i+bit]=lastrow[i+bit];
461 (void) CopyMagickMemory(lastrow, one_row, bytes_per_row);
465 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
466 if (q == (PixelPacket *) NULL)
468 indexes=GetAuthenticIndexQueue(image);
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 q->red=(Quantum) ((QuantumRange*((color16 >> 11) & 0x1f))/0x1f);
478 q->green=(Quantum) ((QuantumRange*((color16 >> 5) & 0x3f))/0x3f);
479 q->blue=(Quantum) ((QuantumRange*((color16 >> 0) & 0x1f))/0x1f);
480 SetOpacityPixelComponent(q,OpaqueOpacity);
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=(IndexPacket) (mask-(((*ptr) & (mask << bit)) >> bit));
493 *q++=image->colormap[(ssize_t) index];
499 bit=8-bits_per_pixel;
502 if (SyncAuthenticPixels(image,exception) == MagickFalse)
505 if (image->previous == (Image *) NULL)
507 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
509 if (status == MagickFalse)
513 if (flags & PALM_HAS_TRANSPARENCY_FLAG)
515 if (bits_per_pixel != 16)
516 SetMagickPixelPacket(image,image->colormap+(mask-transparentIndex),
517 (const IndexPacket *) NULL,&transpix);
518 (void) TransparentPaintImage(image,&transpix,(Quantum)
519 TransparentOpacity,MagickFalse);
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);
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,
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.
652 static MagickBooleanType WritePALMImage(const ImageInfo *image_info,
712 Open output image file.
714 assert(image_info != (const ImageInfo *) NULL);
715 assert(image_info->signature == MagickSignature);
716 assert(image != (Image *) NULL);
717 assert(image->signature == MagickSignature);
718 if (image->debug != MagickFalse)
719 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
720 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&exception);
721 if (status == MagickFalse)
723 GetExceptionInfo(&exception);
724 quantize_info=AcquireQuantizeInfo(image_info);
737 if (image->colorspace != RGBColorspace)
738 (void) TransformImageColorspace(image,RGBColorspace);
739 count=GetNumberColors(image,NULL,&exception);
740 for (bits_per_pixel=1; (one << bits_per_pixel) < count; bits_per_pixel*=2) ;
741 if (image_info->depth > 100)
742 bits_per_pixel=image_info->depth-100;
743 if (bits_per_pixel < 16)
744 (void) TransformImageColorspace(image,image->colorspace);
745 if (bits_per_pixel < 8)
747 (void) TransformImageColorspace(image,GRAYColorspace);
748 (void) SetImageType(image,PaletteType);
749 (void) SortColormapByIntensity(image);
751 if ((image->storage_class == PseudoClass) && (image->colors > 256))
752 (void) SetImageStorageClass(image,DirectClass);
753 if (image->storage_class == PseudoClass)
754 flags|=PALM_HAS_COLORMAP_FLAG;
756 flags|=PALM_IS_DIRECT_COLOR;
757 (void) WriteBlobMSBShort(image,(unsigned short) image->columns); /* width */
758 (void) WriteBlobMSBShort(image,(unsigned short) image->rows); /* height */
759 bytes_per_row=((image->columns+(16/bits_per_pixel-1))/(16/
761 (void) WriteBlobMSBShort(image,(unsigned short) bytes_per_row);
762 if ((image_info->compression == RLECompression) ||
763 (image_info->compression == FaxCompression))
764 flags|=PALM_IS_COMPRESSED_FLAG;
765 (void) WriteBlobMSBShort(image, flags);
766 (void) WriteBlobByte(image,(unsigned char) bits_per_pixel);
767 if (bits_per_pixel > 1)
769 if ((image_info->compression == RLECompression) ||
770 (image_info->compression == FaxCompression))
772 (void) WriteBlobByte(image,version);
773 (void) WriteBlobMSBShort(image,0); /* nextDepthOffset */
774 (void) WriteBlobByte(image,(unsigned char) transparentIndex);
775 if (image_info->compression == RLECompression)
776 (void) WriteBlobByte(image,PALM_COMPRESSION_RLE);
778 if (image_info->compression == FaxCompression)
779 (void) WriteBlobByte(image,PALM_COMPRESSION_SCANLINE);
781 (void) WriteBlobByte(image,PALM_COMPRESSION_NONE);
782 (void) WriteBlobMSBShort(image,0); /* reserved */
784 if (bits_per_pixel == 16)
786 (void) WriteBlobByte(image,5); /* # of bits of red */
787 (void) WriteBlobByte(image,6); /* # of bits of green */
788 (void) WriteBlobByte(image,5); /* # of bits of blue */
789 (void) WriteBlobByte(image,0); /* reserved by Palm */
790 (void) WriteBlobMSBLong(image,0); /* no transparent color, YET */
793 if (bits_per_pixel == 8)
795 if (flags & PALM_HAS_COLORMAP_FLAG) /* Write out colormap */
797 quantize_info->dither=IsPaletteImage(image,&image->exception);
798 quantize_info->number_colors=image->colors;
799 (void) QuantizeImage(quantize_info,image);
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,affinity_image->colorspace);
821 (void) RemapImage(quantize_info,image,affinity_image);
822 for (y=0; y < (ssize_t) image->rows; y++)
824 p=GetAuthenticPixels(image,0,y,image->columns,1,&exception);
825 indexes=GetAuthenticIndexQueue(image);
826 for (x=0; x < (ssize_t) image->columns; x++)
827 indexes[x]=(IndexPacket) FindColor(&image->colormap[(ssize_t)
830 affinity_image=DestroyImage(affinity_image);
833 if (flags & PALM_IS_COMPRESSED_FLAG)
834 (void) WriteBlobMSBShort(image,0); /* fill in size later */
835 lastrow=(unsigned char *) NULL;
836 if (image_info->compression == FaxCompression)
837 lastrow=(unsigned char *) AcquireQuantumMemory(bytes_per_row,
839 /* TODO check whether memory really was acquired? */
840 one_row=(unsigned char *) AcquireQuantumMemory(bytes_per_row,
842 if (one_row == (unsigned char *) NULL)
843 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
844 for (y=0; y < (int) image->rows; y++)
847 (void) ResetMagickMemory(ptr,0,bytes_per_row);
848 p=GetAuthenticPixels(image,0,y,image->columns,1,&exception);
849 if (p == (PixelPacket *) NULL)
851 indexes=GetAuthenticIndexQueue(image);
852 if (bits_per_pixel == 16)
854 for (x=0; x < (int) image->columns; x++)
856 color16=(unsigned short) ((((31*(size_t) GetRedPixelComponent(p))/
857 (size_t) QuantumRange) << 11) |
858 (((63*(size_t) GetGreenPixelComponent(p))/(size_t) QuantumRange) << 5) |
859 ((31*(size_t) GetBluePixelComponent(p))/(size_t) QuantumRange));
860 if (p->opacity == (Quantum) TransparentOpacity)
862 transpix.red=GetRedPixelComponent(p);
863 transpix.green=GetGreenPixelComponent(p);
864 transpix.blue=GetBluePixelComponent(p);
865 transpix.opacity=GetOpacityPixelComponent(p);
866 flags|=PALM_HAS_TRANSPARENCY_FLAG;
868 *ptr++=(unsigned char) ((color16 >> 8) & 0xff);
869 *ptr++=(unsigned char) (color16 & 0xff);
876 bit=(unsigned char) (8-bits_per_pixel);
877 for (x=0; x < (int) image->columns; x++)
879 if (bits_per_pixel >= 8)
880 color=(unsigned char) indexes[x];
882 color=(unsigned char) (indexes[x]*((one << bits_per_pixel)-1)/
883 MagickMax(1*image->colors-1,1));
886 bit-=(ssize_t) bits_per_pixel;
891 bit=(unsigned char) (8-bits_per_pixel);
894 if ((image->columns % (8/bits_per_pixel)) != 0)
897 if (image_info->compression == RLECompression)
900 while (x < (ssize_t) bytes_per_row)
904 while ((one_row[++x] == byte) && (count < 255) &&
905 (x < (ssize_t) bytes_per_row))
907 (void) WriteBlobByte(image,(unsigned char) count);
908 (void) WriteBlobByte(image,(unsigned char) byte);
912 if (image_info->compression == FaxCompression)
918 for (x = 0; x < (ssize_t) bytes_per_row; x += 8)
921 for (bit=0, byte=0; bit < (unsigned char) MagickMin(8,(ssize_t) bytes_per_row-x); bit++)
923 if ((y == 0) || (lastrow[x + bit] != one_row[x + bit]))
925 byte |= (1 << (7 - bit));
926 *tptr++ = (char) one_row[x + bit];
929 (void) WriteBlobByte(image, byte);
930 (void) WriteBlob(image,tptr-tmpbuf,(unsigned char *) tmpbuf);
932 (void) CopyMagickMemory(lastrow,one_row,bytes_per_row);
935 (void) WriteBlob(image,bytes_per_row,one_row);
937 if (flags & PALM_HAS_TRANSPARENCY_FLAG)
939 offset=SeekBlob(image,currentOffset+6,SEEK_SET);
940 (void) WriteBlobMSBShort(image,flags);
941 offset=SeekBlob(image,currentOffset+12,SEEK_SET);
942 (void) WriteBlobByte(image,(unsigned char) transparentIndex); /* trans index */
944 if (bits_per_pixel == 16)
946 offset=SeekBlob(image,currentOffset+20,SEEK_SET);
947 (void) WriteBlobByte(image,0); /* reserved by Palm */
948 (void) WriteBlobByte(image,(unsigned char) ((31*transpix.red)/QuantumRange));
949 (void) WriteBlobByte(image,(unsigned char) ((63*transpix.green)/QuantumRange));
950 (void) WriteBlobByte(image,(unsigned char) ((31*transpix.blue)/QuantumRange));
952 if (flags & PALM_IS_COMPRESSED_FLAG) /* fill in size now */
954 offset=SeekBlob(image,currentOffset+offset,SEEK_SET);
955 (void) WriteBlobMSBShort(image,(unsigned short) (GetBlobSize(image)-
956 currentOffset-offset));
958 if (one_row != (unsigned char *) NULL)
959 one_row=(unsigned char *) RelinquishMagickMemory(one_row);
960 if (lastrow != (unsigned char *) NULL)
961 lastrow=(unsigned char *) RelinquishMagickMemory(lastrow);
962 if (GetNextImageInList(image) == (Image *) NULL)
964 /* padding to 4 byte word */
965 for (cc = (GetBlobSize(image))%4; cc > 0; cc--)
967 (void) WriteBlobByte(image,0);
969 /* write nextDepthOffset and return to end of image */
970 offset=SeekBlob(image,currentOffset+10,SEEK_SET);
971 nextDepthOffset=(size_t) ((GetBlobSize(image)-currentOffset)/4);
972 (void) WriteBlobMSBShort(image,(unsigned short) nextDepthOffset);
973 currentOffset=(MagickOffsetType) GetBlobSize(image);
974 offset=SeekBlob(image,currentOffset,SEEK_SET);
975 image=SyncNextImageInList(image);
976 status=SetImageProgress(image,SaveImagesTag,scene++,
977 GetImageListLength(image));
978 if (status == MagickFalse)
980 } while (image_info->adjoin != MagickFalse);
981 quantize_info=DestroyQuantizeInfo(quantize_info);
982 (void) CloseBlob(image);
983 (void) DestroyExceptionInfo(&exception);