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 long MagickMax(const long x,const long 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)
307 assert(image_info != (const ImageInfo *) NULL);
308 assert(image_info->signature == MagickSignature);
309 if (image_info->debug != MagickFalse)
310 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
311 image_info->filename);
312 assert(exception != (ExceptionInfo *) NULL);
313 assert(exception->signature == MagickSignature);
314 image=AcquireImage(image_info);
315 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
316 if (status == MagickFalse)
318 (void) DestroyImageList(image);
319 return((Image *) NULL);
324 image->columns=ReadBlobMSBShort(image);
325 image->rows=ReadBlobMSBShort(image);
326 if (EOFBlob(image) != MagickFalse)
327 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
329 Copied from coders/pnm.c. TODO other checks ...
331 if ((image->columns == 0) || (image->rows == 0))
332 ThrowReaderException(CorruptImageError,"NegativeOrZeroImageSize");
333 bytes_per_row=ReadBlobMSBShort(image);
334 flags=ReadBlobMSBShort(image);
335 bits_per_pixel=(unsigned long) ReadBlobByte(image);
336 if (bits_per_pixel > 16)
337 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
338 version=(unsigned long) ReadBlobByte(image);
339 nextDepthOffset=(unsigned long) ReadBlobMSBShort(image);
340 transparentIndex=(unsigned long) ReadBlobByte(image);
341 compressionType=(unsigned long) ReadBlobByte(image);
342 pad=ReadBlobMSBShort(image);
344 Initialize image colormap.
346 if ((bits_per_pixel < 16) &&
347 (AcquireImageColormap(image,1L << bits_per_pixel) == MagickFalse))
348 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
349 GetMagickPixelPacket(image,&transpix);
350 if (bits_per_pixel == 16) /* Direct Color */
352 redbits=(unsigned long) ReadBlobByte(image); /* # of bits of red */
353 greenbits=(unsigned long) ReadBlobByte(image); /* # of bits of green */
354 bluebits=(unsigned long) 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 < (long) 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 < (long) (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);
397 image->storage_class=DirectClass;
398 if (bits_per_pixel < 16)
400 image->storage_class=PseudoClass;
403 one_row=(unsigned char *) AcquireQuantumMemory(bytes_per_row,
405 if (one_row == (unsigned char *) NULL)
406 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
407 lastrow=(unsigned char *) NULL;
408 if (compressionType == PALM_COMPRESSION_SCANLINE) {
409 lastrow=(unsigned char *) AcquireQuantumMemory(bytes_per_row,
411 if (lastrow == (unsigned char *) NULL)
412 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
414 mask=(1l << bits_per_pixel)-1;
415 for (y = 0; y < (long) image->rows; y++)
417 if ((flags & PALM_IS_COMPRESSED_FLAG) == 0)
419 /* TODO move out of loop! */
420 image->compression=NoCompression;
421 count=ReadBlob(image,bytes_per_row,one_row);
425 if (compressionType == PALM_COMPRESSION_RLE)
427 /* TODO move out of loop! */
428 image->compression=RLECompression;
429 for (i=0; i < (long) bytes_per_row; )
431 count=(ssize_t) ReadBlobByte(image);
432 count=MagickMin(count,(ssize_t) bytes_per_row-i);
433 byte=(unsigned long) ReadBlobByte(image);
434 (void) ResetMagickMemory(one_row+i,(int) byte,(size_t) count);
439 if (compressionType == PALM_COMPRESSION_SCANLINE)
441 /* TODO move out of loop! */
442 image->compression=FaxCompression;
443 for (i=0; i < (long) bytes_per_row; i+=8)
445 count=(ssize_t) ReadBlobByte(image);
446 byte=1UL*MagickMin((ssize_t) bytes_per_row-i,8);
447 for (bit=0; bit < byte; bit++)
449 if ((y == 0) || (count & (1 << (7 - bit))))
450 one_row[i+bit]=(unsigned char) ReadBlobByte(image);
452 one_row[i+bit]=lastrow[i+bit];
455 (void) CopyMagickMemory(lastrow, one_row, bytes_per_row);
459 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
460 if (q == (PixelPacket *) NULL)
462 indexes=GetAuthenticIndexQueue(image);
463 if (bits_per_pixel == 16)
465 if (image->columns > (2*bytes_per_row))
466 ThrowReaderException(CorruptImageError,"CorruptImage");
467 for (x=0; x < (long) image->columns; x++)
469 color16=(*ptr++ << 8);
471 q->red=(Quantum) ((QuantumRange*((color16 >> 11) & 0x1f))/0x1f);
472 q->green=(Quantum) ((QuantumRange*((color16 >> 5) & 0x3f))/0x3f);
473 q->blue=(Quantum) ((QuantumRange*((color16 >> 0) & 0x1f))/0x1f);
474 SetOpacityPixelComponent(q,OpaqueOpacity);
480 bit=8-bits_per_pixel;
481 for (x=0; x < (long) image->columns; x++)
483 if ((size_t) (ptr-one_row) >= bytes_per_row)
484 ThrowReaderException(CorruptImageError,"CorruptImage");
485 index=(IndexPacket) (mask-(((*ptr) & (mask << bit)) >> bit));
487 *q++=image->colormap[(long) index];
493 bit=8-bits_per_pixel;
496 if (SyncAuthenticPixels(image,exception) == MagickFalse)
499 if (image->previous == (Image *) NULL)
501 status=SetImageProgress(image,LoadImageTag,y,image->rows);
502 if (status == MagickFalse)
506 if (flags & PALM_HAS_TRANSPARENCY_FLAG)
508 if (bits_per_pixel != 16)
509 SetMagickPixelPacket(image,image->colormap+(mask-transparentIndex),
510 (const IndexPacket *) NULL,&transpix);
511 (void) TransparentPaintImage(image,&transpix,(Quantum)
512 TransparentOpacity,MagickFalse);
514 one_row=(unsigned char *) RelinquishMagickMemory(one_row);
515 if (compressionType == PALM_COMPRESSION_SCANLINE)
516 lastrow=(unsigned char *) RelinquishMagickMemory(lastrow);
518 Proceed to next image. Copied from coders/pnm.c
520 if (image_info->number_scenes != 0)
521 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
523 if (nextDepthOffset != 0)
528 totalOffset+=(MagickOffsetType) (nextDepthOffset*4);
529 if (totalOffset >= (MagickOffsetType) GetBlobSize(image))
531 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
535 seekNextDepth=SeekBlob(image,totalOffset,SEEK_SET);
537 if (seekNextDepth != totalOffset)
538 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
540 Allocate next image structure. Copied from coders/pnm.c
542 AcquireNextImage(image_info,image);
543 if (GetNextImageInList(image) == (Image *) NULL)
545 (void) DestroyImageList(image);
546 return((Image *) NULL);
548 image=SyncNextImageInList(image);
549 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
551 if (status == MagickFalse)
554 } while (nextDepthOffset != 0);
555 (void) CloseBlob(image);
556 return(GetFirstImageInList(image));
560 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
564 % R e g i s t e r P A L M I m a g e %
568 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
570 % RegisterPALMImage() adds properties for the PALM image format to the list of
571 % supported formats. The properties include the image format tag, a method to
572 % read and/or write the format, whether the format supports the saving of more
573 % than one frame to the same file or blob, whether the format supports native
574 % in-memory I/O, and a brief description of the format.
576 % The format of the RegisterPALMImage method is:
578 % unsigned long RegisterPALMImage(void)
581 ModuleExport unsigned long RegisterPALMImage(void)
586 entry=SetMagickInfo("PALM");
587 entry->decoder=(DecodeImageHandler *) ReadPALMImage;
588 entry->encoder=(EncodeImageHandler *) WritePALMImage;
589 entry->seekable_stream=MagickTrue;
590 entry->description=ConstantString("Palm pixmap");
591 entry->module=ConstantString("PALM");
592 (void) RegisterMagickInfo(entry);
593 return(MagickImageCoderSignature);
597 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
601 % U n r e g i s t e r P A L M I m a g e %
605 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
607 % UnregisterPALMImage() removes format registrations made by the PALM
608 % module from the list of supported formats.
610 % The format of the UnregisterPALMImage method is:
612 % UnregisterPALMImage(void)
615 ModuleExport void UnregisterPALMImage(void)
617 (void) UnregisterMagickInfo("PALM");
621 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
625 % W r i t e P A L M I m a g e %
629 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
631 % WritePALMImage() writes an image of raw bits in LSB order to a file.
633 % The format of the WritePALMImage method is:
635 % MagickBooleanType WritePALMImage(const ImageInfo *image_info,
638 % A description of each parameter follows.
640 % o image_info: Specifies a pointer to an ImageInfo structure.
642 % o image: A pointer to a Image structure.
645 static MagickBooleanType WritePALMImage(const ImageInfo *image_info,
704 Open output image file.
706 assert(image_info != (const ImageInfo *) NULL);
707 assert(image_info->signature == MagickSignature);
708 assert(image != (Image *) NULL);
709 assert(image->signature == MagickSignature);
710 if (image->debug != MagickFalse)
711 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
712 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&exception);
713 if (status == MagickFalse)
715 GetExceptionInfo(&exception);
716 quantize_info=AcquireQuantizeInfo(image_info);
728 if (image->colorspace != RGBColorspace)
729 (void) TransformImageColorspace(image,RGBColorspace);
730 count=GetNumberColors(image,NULL,&exception);
731 for (bits_per_pixel=1; (1UL << bits_per_pixel) < count; bits_per_pixel*=2) ;
732 if (image_info->depth > 100)
733 bits_per_pixel=image_info->depth-100;
734 if (bits_per_pixel < 16)
735 (void) TransformImageColorspace(image,image->colorspace);
736 if (bits_per_pixel < 8)
738 (void) TransformImageColorspace(image,GRAYColorspace);
739 (void) SetImageType(image,PaletteType);
740 (void) SortColormapByIntensity(image);
742 if ((image->storage_class == PseudoClass) && (image->colors > 256))
743 (void) SetImageStorageClass(image,DirectClass);
744 if (image->storage_class == PseudoClass)
745 flags|=PALM_HAS_COLORMAP_FLAG;
747 flags|=PALM_IS_DIRECT_COLOR;
748 (void) WriteBlobMSBShort(image,(unsigned short) image->columns); /* width */
749 (void) WriteBlobMSBShort(image,(unsigned short) image->rows); /* height */
750 bytes_per_row=((image->columns+(16/bits_per_pixel-1))/(16/
752 (void) WriteBlobMSBShort(image,(unsigned short) bytes_per_row);
753 if ((image_info->compression == RLECompression) ||
754 (image_info->compression == FaxCompression))
755 flags|=PALM_IS_COMPRESSED_FLAG;
756 (void) WriteBlobMSBShort(image, flags);
757 (void) WriteBlobByte(image,(unsigned char) bits_per_pixel);
758 if (bits_per_pixel > 1)
760 if ((image_info->compression == RLECompression) ||
761 (image_info->compression == FaxCompression))
763 (void) WriteBlobByte(image,version);
764 (void) WriteBlobMSBShort(image,0); /* nextDepthOffset */
765 (void) WriteBlobByte(image,(unsigned char) transparentIndex);
766 if (image_info->compression == RLECompression)
767 (void) WriteBlobByte(image,PALM_COMPRESSION_RLE);
769 if (image_info->compression == FaxCompression)
770 (void) WriteBlobByte(image,PALM_COMPRESSION_SCANLINE);
772 (void) WriteBlobByte(image,PALM_COMPRESSION_NONE);
773 (void) WriteBlobMSBShort(image,0); /* reserved */
775 if (bits_per_pixel == 16)
777 (void) WriteBlobByte(image,5); /* # of bits of red */
778 (void) WriteBlobByte(image,6); /* # of bits of green */
779 (void) WriteBlobByte(image,5); /* # of bits of blue */
780 (void) WriteBlobByte(image,0); /* reserved by Palm */
781 (void) WriteBlobMSBLong(image,0); /* no transparent color, YET */
784 if (bits_per_pixel == 8)
786 if (flags & PALM_HAS_COLORMAP_FLAG) /* Write out colormap */
788 quantize_info->dither=IsPaletteImage(image,&image->exception);
789 quantize_info->number_colors=image->colors;
790 (void) QuantizeImage(quantize_info,image);
791 (void) WriteBlobMSBShort(image,(unsigned short) image->colors);
792 for (count = 0; count < image->colors; count++)
794 (void) WriteBlobByte(image,(unsigned char) count);
795 (void) WriteBlobByte(image,ScaleQuantumToChar(
796 image->colormap[count].red));
797 (void) WriteBlobByte(image,
798 ScaleQuantumToChar(image->colormap[count].green));
799 (void) WriteBlobByte(image,
800 ScaleQuantumToChar(image->colormap[count].blue));
804 else /* Map colors to Palm standard colormap */
809 affinity_image=ConstituteImage(256,1,"RGB",CharPixel,&PalmPalette,
811 (void) TransformImageColorspace(affinity_image,affinity_image->colorspace);
812 (void) RemapImage(quantize_info,image,affinity_image);
813 for (y=0; y < (long) image->rows; y++)
815 p=GetAuthenticPixels(image,0,y,image->columns,1,&exception);
816 indexes=GetAuthenticIndexQueue(image);
817 for (x=0; x < (long) image->columns; x++)
818 indexes[x]=(IndexPacket) FindColor(&image->colormap[(long)
821 affinity_image=DestroyImage(affinity_image);
824 if (flags & PALM_IS_COMPRESSED_FLAG)
825 (void) WriteBlobMSBShort(image,0); /* fill in size later */
826 lastrow=(unsigned char *) NULL;
827 if (image_info->compression == FaxCompression)
828 lastrow=(unsigned char *) AcquireQuantumMemory(bytes_per_row,
830 /* TODO check whether memory really was acquired? */
831 one_row=(unsigned char *) AcquireQuantumMemory(bytes_per_row,
833 if (one_row == (unsigned char *) NULL)
834 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
835 for (y=0; y < (int) image->rows; y++)
838 (void) ResetMagickMemory(ptr,0,bytes_per_row);
839 p=GetAuthenticPixels(image,0,y,image->columns,1,&exception);
840 if (p == (PixelPacket *) NULL)
842 indexes=GetAuthenticIndexQueue(image);
843 if (bits_per_pixel == 16)
845 for (x=0; x < (int) image->columns; x++)
847 color16=(unsigned short) ((((31*(unsigned long) GetRedPixelComponent(p))/
848 (unsigned long) QuantumRange) << 11) |
849 (((63*(unsigned long) GetGreenPixelComponent(p))/(unsigned long) QuantumRange) << 5) |
850 ((31*(unsigned long) GetBluePixelComponent(p))/(unsigned long) QuantumRange));
851 if (p->opacity == (Quantum) TransparentOpacity)
853 transpix.red=GetRedPixelComponent(p);
854 transpix.green=GetGreenPixelComponent(p);
855 transpix.blue=GetBluePixelComponent(p);
856 transpix.opacity=GetOpacityPixelComponent(p);
857 flags|=PALM_HAS_TRANSPARENCY_FLAG;
859 *ptr++=(unsigned char) ((color16 >> 8) & 0xff);
860 *ptr++=(unsigned char) (color16 & 0xff);
867 bit=(unsigned char) (8-bits_per_pixel);
868 for (x=0; x < (int) image->columns; x++)
870 if (bits_per_pixel >= 8)
871 color=(unsigned char) indexes[x];
873 color=(unsigned char) (indexes[x]*((1 << bits_per_pixel)-1)/
874 MagickMax(1L*image->colors-1L,1L));
882 bit=(unsigned char) (8-bits_per_pixel);
885 if ((image->columns % (8/bits_per_pixel)) != 0)
888 if (image_info->compression == RLECompression)
891 while (x < (long) bytes_per_row)
895 while ((one_row[++x] == byte) && (count < 255) &&
896 (x < (long) bytes_per_row))
898 (void) WriteBlobByte(image,(unsigned char) count);
899 (void) WriteBlobByte(image,(unsigned char) byte);
903 if (image_info->compression == FaxCompression)
909 for (x = 0; x < (long) bytes_per_row; x += 8)
912 for (bit=0, byte=0; bit < (unsigned char) MagickMin(8,(ssize_t) bytes_per_row-x); bit++)
914 if ((y == 0) || (lastrow[x + bit] != one_row[x + bit]))
916 byte |= (1 << (7 - bit));
917 *tptr++ = (char) one_row[x + bit];
920 (void) WriteBlobByte(image, byte);
921 (void) WriteBlob(image,tptr-tmpbuf,(unsigned char *) tmpbuf);
923 (void) CopyMagickMemory(lastrow,one_row,bytes_per_row);
926 (void) WriteBlob(image,bytes_per_row,one_row);
928 if (flags & PALM_HAS_TRANSPARENCY_FLAG)
930 offset=SeekBlob(image,currentOffset+6,SEEK_SET);
931 (void) WriteBlobMSBShort(image,flags);
932 offset=SeekBlob(image,currentOffset+12,SEEK_SET);
933 (void) WriteBlobByte(image,(unsigned char) transparentIndex); /* trans index */
935 if (bits_per_pixel == 16)
937 offset=SeekBlob(image,currentOffset+20,SEEK_SET);
938 (void) WriteBlobByte(image,0); /* reserved by Palm */
939 (void) WriteBlobByte(image,(unsigned char) ((31*transpix.red)/QuantumRange));
940 (void) WriteBlobByte(image,(unsigned char) ((63*transpix.green)/QuantumRange));
941 (void) WriteBlobByte(image,(unsigned char) ((31*transpix.blue)/QuantumRange));
943 if (flags & PALM_IS_COMPRESSED_FLAG) /* fill in size now */
945 offset=SeekBlob(image,currentOffset+offset,SEEK_SET);
946 (void) WriteBlobMSBShort(image,(unsigned short) (GetBlobSize(image)-
947 currentOffset-offset));
949 if (one_row != (unsigned char *) NULL)
950 one_row=(unsigned char *) RelinquishMagickMemory(one_row);
951 if (lastrow != (unsigned char *) NULL)
952 lastrow=(unsigned char *) RelinquishMagickMemory(lastrow);
953 if (GetNextImageInList(image) == (Image *) NULL)
955 /* padding to 4 byte word */
956 for (cc = (GetBlobSize(image))%4; cc > 0; cc--)
958 (void) WriteBlobByte(image,0);
960 /* write nextDepthOffset and return to end of image */
961 offset=SeekBlob(image,currentOffset+10,SEEK_SET);
962 nextDepthOffset=(unsigned long) ((GetBlobSize(image)-currentOffset)/4);
963 (void) WriteBlobMSBShort(image,(unsigned short) nextDepthOffset);
964 currentOffset=(MagickOffsetType) GetBlobSize(image);
965 offset=SeekBlob(image,currentOffset,SEEK_SET);
966 image=SyncNextImageInList(image);
967 status=SetImageProgress(image,SaveImagesTag,scene++,
968 GetImageListLength(image));
969 if (status == MagickFalse)
971 } while (image_info->adjoin != MagickFalse);
972 quantize_info=DestroyQuantizeInfo(quantize_info);
973 (void) CloseBlob(image);
974 (void) DestroyExceptionInfo(&exception);