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/blob.h"
46 #include "MagickCore/blob-private.h"
47 #include "MagickCore/cache.h"
48 #include "MagickCore/color.h"
49 #include "MagickCore/colormap.h"
50 #include "MagickCore/colormap-private.h"
51 #include "MagickCore/color-private.h"
52 #include "MagickCore/colorspace.h"
53 #include "MagickCore/constitute.h"
54 #include "MagickCore/exception.h"
55 #include "MagickCore/histogram.h"
56 #include "MagickCore/image.h"
57 #include "MagickCore/image-private.h"
58 #include "MagickCore/list.h"
59 #include "MagickCore/magick.h"
60 #include "MagickCore/memory_.h"
61 #include "MagickCore/monitor.h"
62 #include "MagickCore/monitor-private.h"
63 #include "MagickCore/paint.h"
64 #include "MagickCore/pixel-accessor.h"
65 #include "MagickCore/property.h"
66 #include "MagickCore/quantize.h"
67 #include "MagickCore/quantum-private.h"
68 #include "MagickCore/static.h"
69 #include "MagickCore/string_.h"
70 #include "MagickCore/module.h"
71 #include "MagickCore/utility.h"
76 #define PALM_IS_COMPRESSED_FLAG 0x8000
77 #define PALM_HAS_COLORMAP_FLAG 0x4000
78 #define PALM_HAS_FOUR_BYTE_FIELD 0x0200
79 #define PALM_HAS_TRANSPARENCY_FLAG 0x2000
80 #define PALM_IS_INDIRECT 0x1000
81 #define PALM_IS_FOR_SCREEN 0x0800
82 #define PALM_IS_DIRECT_COLOR 0x0400
83 #define PALM_COMPRESSION_SCANLINE 0x00
84 #define PALM_COMPRESSION_RLE 0x01
85 #define PALM_COMPRESSION_NONE 0xFF
88 The 256 color system palette for Palm Computing Devices.
93 {255, 255,255}, {255, 204,255}, {255, 153,255}, {255, 102,255},
94 {255, 51,255}, {255, 0,255}, {255, 255,204}, {255, 204,204},
95 {255, 153,204}, {255, 102,204}, {255, 51,204}, {255, 0,204},
96 {255, 255,153}, {255, 204,153}, {255, 153,153}, {255, 102,153},
97 {255, 51,153}, {255, 0,153}, {204, 255,255}, {204, 204,255},
98 {204, 153,255}, {204, 102,255}, {204, 51,255}, {204, 0,255},
99 {204, 255,204}, {204, 204,204}, {204, 153,204}, {204, 102,204},
100 {204, 51,204}, {204, 0,204}, {204, 255,153}, {204, 204,153},
101 {204, 153,153}, {204, 102,153}, {204, 51,153}, {204, 0,153},
102 {153, 255,255}, {153, 204,255}, {153, 153,255}, {153, 102,255},
103 {153, 51,255}, {153, 0,255}, {153, 255,204}, {153, 204,204},
104 {153, 153,204}, {153, 102,204}, {153, 51,204}, {153, 0,204},
105 {153, 255,153}, {153, 204,153}, {153, 153,153}, {153, 102,153},
106 {153, 51,153}, {153, 0,153}, {102, 255,255}, {102, 204,255},
107 {102, 153,255}, {102, 102,255}, {102, 51,255}, {102, 0,255},
108 {102, 255,204}, {102, 204,204}, {102, 153,204}, {102, 102,204},
109 {102, 51,204}, {102, 0,204}, {102, 255,153}, {102, 204,153},
110 {102, 153,153}, {102, 102,153}, {102, 51,153}, {102, 0,153},
111 { 51, 255,255}, { 51, 204,255}, { 51, 153,255}, { 51, 102,255},
112 { 51, 51,255}, { 51, 0,255}, { 51, 255,204}, { 51, 204,204},
113 { 51, 153,204}, { 51, 102,204}, { 51, 51,204}, { 51, 0,204},
114 { 51, 255,153}, { 51, 204,153}, { 51, 153,153}, { 51, 102,153},
115 { 51, 51,153}, { 51, 0,153}, { 0, 255,255}, { 0, 204,255},
116 { 0, 153,255}, { 0, 102,255}, { 0, 51,255}, { 0, 0,255},
117 { 0, 255,204}, { 0, 204,204}, { 0, 153,204}, { 0, 102,204},
118 { 0, 51,204}, { 0, 0,204}, { 0, 255,153}, { 0, 204,153},
119 { 0, 153,153}, { 0, 102,153}, { 0, 51,153}, { 0, 0,153},
120 {255, 255,102}, {255, 204,102}, {255, 153,102}, {255, 102,102},
121 {255, 51,102}, {255, 0,102}, {255, 255, 51}, {255, 204, 51},
122 {255, 153, 51}, {255, 102, 51}, {255, 51, 51}, {255, 0, 51},
123 {255, 255, 0}, {255, 204, 0}, {255, 153, 0}, {255, 102, 0},
124 {255, 51, 0}, {255, 0, 0}, {204, 255,102}, {204, 204,102},
125 {204, 153,102}, {204, 102,102}, {204, 51,102}, {204, 0,102},
126 {204, 255, 51}, {204, 204, 51}, {204, 153, 51}, {204, 102, 51},
127 {204, 51, 51}, {204, 0, 51}, {204, 255, 0}, {204, 204, 0},
128 {204, 153, 0}, {204, 102, 0}, {204, 51, 0}, {204, 0, 0},
129 {153, 255,102}, {153, 204,102}, {153, 153,102}, {153, 102,102},
130 {153, 51,102}, {153, 0,102}, {153, 255, 51}, {153, 204, 51},
131 {153, 153, 51}, {153, 102, 51}, {153, 51, 51}, {153, 0, 51},
132 {153, 255, 0}, {153, 204, 0}, {153, 153, 0}, {153, 102, 0},
133 {153, 51, 0}, {153, 0, 0}, {102, 255,102}, {102, 204,102},
134 {102, 153,102}, {102, 102,102}, {102, 51,102}, {102, 0,102},
135 {102, 255, 51}, {102, 204, 51}, {102, 153, 51}, {102, 102, 51},
136 {102, 51, 51}, {102, 0, 51}, {102, 255, 0}, {102, 204, 0},
137 {102, 153, 0}, {102, 102, 0}, {102, 51, 0}, {102, 0, 0},
138 { 51, 255,102}, { 51, 204,102}, { 51, 153,102}, { 51, 102,102},
139 { 51, 51,102}, { 51, 0,102}, { 51, 255, 51}, { 51, 204, 51},
140 { 51, 153, 51}, { 51, 102, 51}, { 51, 51, 51}, { 51, 0, 51},
141 { 51, 255, 0}, { 51, 204, 0}, { 51, 153, 0}, { 51, 102, 0},
142 { 51, 51, 0}, { 51, 0, 0}, { 0, 255,102}, { 0, 204,102},
143 { 0, 153,102}, { 0, 102,102}, { 0, 51,102}, { 0, 0,102},
144 { 0, 255, 51}, { 0, 204, 51}, { 0, 153, 51}, { 0, 102, 51},
145 { 0, 51, 51}, { 0, 0, 51}, { 0, 255, 0}, { 0, 204, 0},
146 { 0, 153, 0}, { 0, 102, 0}, { 0, 51, 0}, { 17, 17, 17},
147 { 34, 34, 34}, { 68, 68, 68}, { 85, 85, 85}, {119, 119,119},
148 {136, 136,136}, {170, 170,170}, {187, 187,187}, {221, 221,221},
149 {238, 238,238}, {192, 192,192}, {128, 0, 0}, {128, 0,128},
150 { 0, 128, 0}, { 0, 128,128}, { 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},
156 { 0, 0, 0}, { 0, 0, 0}, { 0, 0, 0}, { 0, 0, 0}
160 Forward declarations.
162 static MagickBooleanType
163 WritePALMImage(const ImageInfo *,Image *);
166 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
170 % F i n d C o l o r %
174 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
176 % FindColor() returns the index of the matching entry from PalmPalette for a
179 % The format of the FindColor method is:
181 % int FindColor(const Image *image,PixelPacket *pixel)
183 % A description of each parameter follows:
185 % o int: the index of the matching color or -1 if not found/
187 % o pixel: a pointer to the PixelPacket to be matched.
190 static int FindColor(const Image *image,PixelPacket *packet)
195 for (i=0; i < 256; i++)
196 if (ScaleQuantumToChar(packet->red) == PalmPalette[i][0] &&
197 ScaleQuantumToChar(packet->green) == PalmPalette[i][1] &&
198 ScaleQuantumToChar(packet->blue) == PalmPalette[i][2])
204 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
208 % R e a d P A L M I m a g e %
212 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
214 % ReadPALMImage() reads an image of raw bites in LSB order and returns it. It
215 % allocates the memory necessary for the new Image structure and returns a
216 % pointer to the new image.
218 % The format of the ReadPALMImage method is:
220 % Image *ReadPALMImage(const ImageInfo *image_info,
221 % ExceptionInfo *exception)
223 % A description of each parameter follows:
225 % o image_info: Specifies a pointer to an ImageInfo structure.
227 % o exception: return any errors or warnings in this structure.
231 static inline size_t MagickMax(const size_t x,const size_t y)
238 static inline ssize_t MagickMin(const ssize_t x,const ssize_t y)
245 static Image *ReadPALMImage(const ImageInfo *image_info,
246 ExceptionInfo *exception)
304 assert(image_info != (const ImageInfo *) NULL);
305 assert(image_info->signature == MagickSignature);
306 if (image_info->debug != MagickFalse)
307 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
308 image_info->filename);
309 assert(exception != (ExceptionInfo *) NULL);
310 assert(exception->signature == MagickSignature);
311 image=AcquireImage(image_info);
312 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
313 if (status == MagickFalse)
315 (void) DestroyImageList(image);
316 return((Image *) NULL);
321 image->columns=ReadBlobMSBShort(image);
322 image->rows=ReadBlobMSBShort(image);
323 if (EOFBlob(image) != MagickFalse)
324 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
325 if ((image->columns == 0) || (image->rows == 0))
326 ThrowReaderException(CorruptImageError,"NegativeOrZeroImageSize");
327 bytes_per_row=ReadBlobMSBShort(image);
328 flags=ReadBlobMSBShort(image);
329 bits_per_pixel=(size_t) ReadBlobByte(image);
330 if (bits_per_pixel > 16)
331 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
332 version=(size_t) ReadBlobByte(image);
334 nextDepthOffset=(size_t) ReadBlobMSBShort(image);
335 transparentIndex=(size_t) ReadBlobByte(image);
336 compressionType=(size_t) ReadBlobByte(image);
337 pad=ReadBlobMSBShort(image);
340 Initialize image colormap.
343 if ((bits_per_pixel < 16) &&
344 (AcquireImageColormap(image,one << bits_per_pixel) == MagickFalse))
345 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
346 GetPixelInfo(image,&transpix);
347 if (bits_per_pixel == 16) /* Direct Color */
349 redbits=(size_t) ReadBlobByte(image); /* # of bits of red */
351 greenbits=(size_t) ReadBlobByte(image); /* # of bits of green */
353 bluebits=(size_t) ReadBlobByte(image); /* # of bits of blue */
355 ReadBlobByte(image); /* reserved by Palm */
356 ReadBlobByte(image); /* reserved by Palm */
357 transpix.red=(MagickRealType) (QuantumRange*ReadBlobByte(image)/31);
358 transpix.green=(MagickRealType) (QuantumRange*ReadBlobByte(image)/63);
359 transpix.blue=(MagickRealType) (QuantumRange*ReadBlobByte(image)/31);
361 if (bits_per_pixel == 8)
366 if (flags & PALM_HAS_COLORMAP_FLAG)
368 count=(ssize_t) ReadBlobMSBShort(image);
369 for (i=0; i < (ssize_t) count; i++)
372 index=ConstrainColormapIndex(image,255-i);
373 image->colormap[(int) index].red=
374 ScaleCharToQuantum((unsigned char) ReadBlobByte(image));
375 image->colormap[(int) index].green=
376 ScaleCharToQuantum((unsigned char) ReadBlobByte(image));
377 image->colormap[(int) index].blue=
378 ScaleCharToQuantum((unsigned char) ReadBlobByte(image));
383 for (i=0; i < (ssize_t) (1L << bits_per_pixel); i++)
385 index=ConstrainColormapIndex(image,255-i);
386 image->colormap[(int) index].red=
387 ScaleCharToQuantum(PalmPalette[i][0]);
388 image->colormap[(int) index].green=
389 ScaleCharToQuantum(PalmPalette[i][1]);
390 image->colormap[(int) index].blue=
391 ScaleCharToQuantum(PalmPalette[i][2]);
395 if (flags & PALM_IS_COMPRESSED_FLAG)
396 size=ReadBlobMSBShort(image);
398 image->storage_class=DirectClass;
399 if (bits_per_pixel < 16)
401 image->storage_class=PseudoClass;
404 one_row=(unsigned char *) AcquireQuantumMemory(bytes_per_row,
406 if (one_row == (unsigned char *) NULL)
407 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
408 lastrow=(unsigned char *) NULL;
409 if (compressionType == PALM_COMPRESSION_SCANLINE) {
410 lastrow=(unsigned char *) AcquireQuantumMemory(bytes_per_row,
412 if (lastrow == (unsigned char *) NULL)
413 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
415 mask=(1l << bits_per_pixel)-1;
416 for (y = 0; y < (ssize_t) image->rows; y++)
418 if ((flags & PALM_IS_COMPRESSED_FLAG) == 0)
420 /* TODO move out of loop! */
421 image->compression=NoCompression;
422 count=ReadBlob(image,bytes_per_row,one_row);
426 if (compressionType == PALM_COMPRESSION_RLE)
428 /* TODO move out of loop! */
429 image->compression=RLECompression;
430 for (i=0; i < (ssize_t) bytes_per_row; )
432 count=(ssize_t) ReadBlobByte(image);
433 count=MagickMin(count,(ssize_t) bytes_per_row-i);
434 byte=(size_t) ReadBlobByte(image);
435 (void) ResetMagickMemory(one_row+i,(int) byte,(size_t) count);
440 if (compressionType == PALM_COMPRESSION_SCANLINE)
445 /* TODO move out of loop! */
447 image->compression=FaxCompression;
448 for (i=0; i < (ssize_t) bytes_per_row; i+=8)
450 count=(ssize_t) ReadBlobByte(image);
451 byte=1UL*MagickMin((ssize_t) bytes_per_row-i,8);
452 for (bit=0; bit < byte; bit++)
454 if ((y == 0) || (count & (one << (7 - bit))))
455 one_row[i+bit]=(unsigned char) ReadBlobByte(image);
457 one_row[i+bit]=lastrow[i+bit];
460 (void) CopyMagickMemory(lastrow, one_row, bytes_per_row);
464 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
465 if (q == (const Quantum *) NULL)
467 if (bits_per_pixel == 16)
469 if (image->columns > (2*bytes_per_row))
470 ThrowReaderException(CorruptImageError,"CorruptImage");
471 for (x=0; x < (ssize_t) image->columns; x++)
473 color16=(*ptr++ << 8);
475 SetPixelRed(image,(QuantumRange*((color16 >> 11) & 0x1f))/0x1f,q);
476 SetPixelGreen(image,(QuantumRange*((color16 >> 5) & 0x3f))/0x3f,q);
477 SetPixelBlue(image,(QuantumRange*((color16 >> 0) & 0x1f))/0x1f,q);
478 SetPixelAlpha(image,OpaqueAlpha,q);
479 q+=GetPixelChannels(image);
484 bit=8-bits_per_pixel;
485 for (x=0; x < (ssize_t) image->columns; x++)
487 if ((size_t) (ptr-one_row) >= bytes_per_row)
488 ThrowReaderException(CorruptImageError,"CorruptImage");
489 index=(Quantum) (mask-(((*ptr) & (mask << bit)) >> bit));
490 SetPixelIndex(image,index,q);
491 SetPixelPacket(image,image->colormap+(ssize_t) index,q);
497 bit=8-bits_per_pixel;
499 q+=GetPixelChannels(image);
501 if (SyncAuthenticPixels(image,exception) == MagickFalse)
504 if (image->previous == (Image *) NULL)
506 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
508 if (status == MagickFalse)
512 if (flags & PALM_HAS_TRANSPARENCY_FLAG)
514 if (bits_per_pixel != 16)
515 SetPixelInfoPacket(image,image->colormap+(mask-transparentIndex),
517 (void) TransparentPaintImage(image,&transpix,(Quantum)
518 TransparentAlpha,MagickFalse);
520 one_row=(unsigned char *) RelinquishMagickMemory(one_row);
521 if (compressionType == PALM_COMPRESSION_SCANLINE)
522 lastrow=(unsigned char *) RelinquishMagickMemory(lastrow);
524 Proceed to next image. Copied from coders/pnm.c
526 if (image_info->number_scenes != 0)
527 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
529 if (nextDepthOffset != 0)
534 totalOffset+=(MagickOffsetType) (nextDepthOffset*4);
535 if (totalOffset >= (MagickOffsetType) GetBlobSize(image))
537 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
541 seekNextDepth=SeekBlob(image,totalOffset,SEEK_SET);
543 if (seekNextDepth != totalOffset)
544 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
546 Allocate next image structure. Copied from coders/pnm.c
548 AcquireNextImage(image_info,image);
549 if (GetNextImageInList(image) == (Image *) NULL)
551 (void) DestroyImageList(image);
552 return((Image *) NULL);
554 image=SyncNextImageInList(image);
555 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
557 if (status == MagickFalse)
560 } while (nextDepthOffset != 0);
561 (void) CloseBlob(image);
562 return(GetFirstImageInList(image));
566 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
570 % R e g i s t e r P A L M I m a g e %
574 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
576 % RegisterPALMImage() adds properties for the PALM image format to the list of
577 % supported formats. The properties include the image format tag, a method to
578 % read and/or write the format, whether the format supports the saving of more
579 % than one frame to the same file or blob, whether the format supports native
580 % in-memory I/O, and a brief description of the format.
582 % The format of the RegisterPALMImage method is:
584 % size_t RegisterPALMImage(void)
587 ModuleExport size_t RegisterPALMImage(void)
592 entry=SetMagickInfo("PALM");
593 entry->decoder=(DecodeImageHandler *) ReadPALMImage;
594 entry->encoder=(EncodeImageHandler *) WritePALMImage;
595 entry->seekable_stream=MagickTrue;
596 entry->description=ConstantString("Palm pixmap");
597 entry->module=ConstantString("PALM");
598 (void) RegisterMagickInfo(entry);
599 return(MagickImageCoderSignature);
603 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
607 % U n r e g i s t e r P A L M I m a g e %
611 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
613 % UnregisterPALMImage() removes format registrations made by the PALM
614 % module from the list of supported formats.
616 % The format of the UnregisterPALMImage method is:
618 % UnregisterPALMImage(void)
621 ModuleExport void UnregisterPALMImage(void)
623 (void) UnregisterMagickInfo("PALM");
627 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
631 % W r i t e P A L M I m a g e %
635 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
637 % WritePALMImage() writes an image of raw bits in LSB order to a file.
639 % The format of the WritePALMImage method is:
641 % MagickBooleanType WritePALMImage(const ImageInfo *image_info,
644 % A description of each parameter follows.
646 % o image_info: Specifies a pointer to an ImageInfo structure.
648 % o image: A pointer to a Image structure.
651 static MagickBooleanType WritePALMImage(const ImageInfo *image_info,
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 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&exception);
719 if (status == MagickFalse)
721 GetExceptionInfo(&exception);
722 quantize_info=AcquireQuantizeInfo(image_info);
735 if (image->colorspace != RGBColorspace)
736 (void) TransformImageColorspace(image,RGBColorspace);
737 count=GetNumberColors(image,NULL,&exception);
738 for (bits_per_pixel=1; (one << bits_per_pixel) < count; bits_per_pixel*=2) ;
739 if (image_info->depth > 100)
740 bits_per_pixel=image_info->depth-100;
741 if (bits_per_pixel < 16)
742 (void) TransformImageColorspace(image,image->colorspace);
743 if (bits_per_pixel < 8)
745 (void) TransformImageColorspace(image,GRAYColorspace);
746 (void) SetImageType(image,PaletteType);
747 (void) SortColormapByIntensity(image);
749 if ((image->storage_class == PseudoClass) && (image->colors > 256))
750 (void) SetImageStorageClass(image,DirectClass);
751 if (image->storage_class == PseudoClass)
752 flags|=PALM_HAS_COLORMAP_FLAG;
754 flags|=PALM_IS_DIRECT_COLOR;
755 (void) WriteBlobMSBShort(image,(unsigned short) image->columns); /* width */
756 (void) WriteBlobMSBShort(image,(unsigned short) image->rows); /* height */
757 bytes_per_row=((image->columns+(16/bits_per_pixel-1))/(16/
759 (void) WriteBlobMSBShort(image,(unsigned short) bytes_per_row);
760 if ((image_info->compression == RLECompression) ||
761 (image_info->compression == FaxCompression))
762 flags|=PALM_IS_COMPRESSED_FLAG;
763 (void) WriteBlobMSBShort(image, flags);
764 (void) WriteBlobByte(image,(unsigned char) bits_per_pixel);
765 if (bits_per_pixel > 1)
767 if ((image_info->compression == RLECompression) ||
768 (image_info->compression == FaxCompression))
770 (void) WriteBlobByte(image,version);
771 (void) WriteBlobMSBShort(image,0); /* nextDepthOffset */
772 (void) WriteBlobByte(image,(unsigned char) transparentIndex);
773 if (image_info->compression == RLECompression)
774 (void) WriteBlobByte(image,PALM_COMPRESSION_RLE);
776 if (image_info->compression == FaxCompression)
777 (void) WriteBlobByte(image,PALM_COMPRESSION_SCANLINE);
779 (void) WriteBlobByte(image,PALM_COMPRESSION_NONE);
780 (void) WriteBlobMSBShort(image,0); /* reserved */
782 if (bits_per_pixel == 16)
784 (void) WriteBlobByte(image,5); /* # of bits of red */
785 (void) WriteBlobByte(image,6); /* # of bits of green */
786 (void) WriteBlobByte(image,5); /* # of bits of blue */
787 (void) WriteBlobByte(image,0); /* reserved by Palm */
788 (void) WriteBlobMSBLong(image,0); /* no transparent color, YET */
791 if (bits_per_pixel == 8)
793 if (flags & PALM_HAS_COLORMAP_FLAG) /* Write out colormap */
795 quantize_info->dither=IsPaletteImage(image,&image->exception);
796 quantize_info->number_colors=image->colors;
797 (void) QuantizeImage(quantize_info,image);
798 (void) WriteBlobMSBShort(image,(unsigned short) image->colors);
799 for (count = 0; count < image->colors; count++)
801 (void) WriteBlobByte(image,(unsigned char) count);
802 (void) WriteBlobByte(image,ScaleQuantumToChar(
803 image->colormap[count].red));
804 (void) WriteBlobByte(image,
805 ScaleQuantumToChar(image->colormap[count].green));
806 (void) WriteBlobByte(image,
807 ScaleQuantumToChar(image->colormap[count].blue));
811 else /* Map colors to Palm standard colormap */
816 affinity_image=ConstituteImage(256,1,"RGB",CharPixel,&PalmPalette,
818 (void) TransformImageColorspace(affinity_image,
819 affinity_image->colorspace);
820 (void) RemapImage(quantize_info,image,affinity_image);
821 for (y=0; y < (ssize_t) image->rows; y++)
823 q=GetAuthenticPixels(image,0,y,image->columns,1,&exception);
824 for (x=0; x < (ssize_t) image->columns; x++)
826 SetPixelIndex(image,FindColor(image,&image->colormap[(ssize_t)
827 GetPixelIndex(image,q)]),q);
828 q+=GetPixelChannels(image);
831 affinity_image=DestroyImage(affinity_image);
834 if (flags & PALM_IS_COMPRESSED_FLAG)
835 (void) WriteBlobMSBShort(image,0); /* fill in size later */
836 lastrow=(unsigned char *) NULL;
837 if (image_info->compression == FaxCompression)
838 lastrow=(unsigned char *) AcquireQuantumMemory(bytes_per_row,
840 /* TODO check whether memory really was acquired? */
841 one_row=(unsigned char *) AcquireQuantumMemory(bytes_per_row,
843 if (one_row == (unsigned char *) NULL)
844 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
845 for (y=0; y < (int) image->rows; y++)
848 (void) ResetMagickMemory(ptr,0,bytes_per_row);
849 p=GetVirtualPixels(image,0,y,image->columns,1,&exception);
850 if (p == (const Quantum *) NULL)
852 if (bits_per_pixel == 16)
854 for (x=0; x < (int) image->columns; x++)
856 color16=(unsigned short) ((((31*(size_t) GetPixelRed(image,p))/
857 (size_t) QuantumRange) << 11) | (((63*(size_t)
858 GetPixelGreen(image,p))/(size_t) QuantumRange) << 5) |
859 ((31*(size_t) GetPixelBlue(image,p))/(size_t) QuantumRange));
860 if (GetPixelAlpha(image,p) == (Quantum) TransparentAlpha)
862 transpix.red=GetPixelRed(image,p);
863 transpix.green=GetPixelGreen(image,p);
864 transpix.blue=GetPixelBlue(image,p);
865 transpix.alpha=GetPixelAlpha(image,p);
866 flags|=PALM_HAS_TRANSPARENCY_FLAG;
868 *ptr++=(unsigned char) ((color16 >> 8) & 0xff);
869 *ptr++=(unsigned char) (color16 & 0xff);
870 p+=GetPixelChannels(image);
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) GetPixelIndex(image,p);
882 color=(unsigned char) (GetPixelIndex(image,p)*
883 ((one << bits_per_pixel)-1)/MagickMax(1*image->colors-1,1));
886 bit-=(unsigned char) bits_per_pixel;
891 bit=(unsigned char) (8-bits_per_pixel);
893 p+=GetPixelChannels(image);
895 if ((image->columns % (8/bits_per_pixel)) != 0)
898 if (image_info->compression == RLECompression)
901 while (x < (ssize_t) bytes_per_row)
905 while ((one_row[++x] == byte) && (count < 255) &&
906 (x < (ssize_t) bytes_per_row))
908 (void) WriteBlobByte(image,(unsigned char) count);
909 (void) WriteBlobByte(image,(unsigned char) byte);
913 if (image_info->compression == FaxCompression)
919 for (x = 0; x < (ssize_t) bytes_per_row; x += 8)
922 for (bit=0, byte=0; bit < (unsigned char) MagickMin(8,(ssize_t) bytes_per_row-x); bit++)
924 if ((y == 0) || (lastrow[x + bit] != one_row[x + bit]))
926 byte |= (1 << (7 - bit));
927 *tptr++ = (char) one_row[x + bit];
930 (void) WriteBlobByte(image, byte);
931 (void) WriteBlob(image,tptr-tmpbuf,(unsigned char *) tmpbuf);
933 (void) CopyMagickMemory(lastrow,one_row,bytes_per_row);
936 (void) WriteBlob(image,bytes_per_row,one_row);
938 if (flags & PALM_HAS_TRANSPARENCY_FLAG)
940 offset=SeekBlob(image,currentOffset+6,SEEK_SET);
941 (void) WriteBlobMSBShort(image,flags);
942 offset=SeekBlob(image,currentOffset+12,SEEK_SET);
943 (void) WriteBlobByte(image,(unsigned char) transparentIndex); /* trans index */
945 if (bits_per_pixel == 16)
947 offset=SeekBlob(image,currentOffset+20,SEEK_SET);
948 (void) WriteBlobByte(image,0); /* reserved by Palm */
949 (void) WriteBlobByte(image,(unsigned char) ((31*transpix.red)/QuantumRange));
950 (void) WriteBlobByte(image,(unsigned char) ((63*transpix.green)/QuantumRange));
951 (void) WriteBlobByte(image,(unsigned char) ((31*transpix.blue)/QuantumRange));
953 if (flags & PALM_IS_COMPRESSED_FLAG) /* fill in size now */
955 offset=SeekBlob(image,currentOffset+offset,SEEK_SET);
956 (void) WriteBlobMSBShort(image,(unsigned short) (GetBlobSize(image)-
957 currentOffset-offset));
959 if (one_row != (unsigned char *) NULL)
960 one_row=(unsigned char *) RelinquishMagickMemory(one_row);
961 if (lastrow != (unsigned char *) NULL)
962 lastrow=(unsigned char *) RelinquishMagickMemory(lastrow);
963 if (GetNextImageInList(image) == (Image *) NULL)
965 /* padding to 4 byte word */
966 for (cc = (GetBlobSize(image))%4; cc > 0; cc--)
968 (void) WriteBlobByte(image,0);
970 /* write nextDepthOffset and return to end of image */
971 offset=SeekBlob(image,currentOffset+10,SEEK_SET);
972 nextDepthOffset=(size_t) ((GetBlobSize(image)-currentOffset)/4);
973 (void) WriteBlobMSBShort(image,(unsigned short) nextDepthOffset);
974 currentOffset=(MagickOffsetType) GetBlobSize(image);
975 offset=SeekBlob(image,currentOffset,SEEK_SET);
976 image=SyncNextImageInList(image);
977 status=SetImageProgress(image,SaveImagesTag,scene++,
978 GetImageListLength(image));
979 if (status == MagickFalse)
981 } while (image_info->adjoin != MagickFalse);
982 quantize_info=DestroyQuantizeInfo(quantize_info);
983 (void) CloseBlob(image);
984 (void) DestroyExceptionInfo(&exception);