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(GetRedPixelComponent(pixel)) == PalmPalette[i][0] &&
196 ScaleQuantumToChar(GetGreenPixelComponent(pixel)) == PalmPalette[i][1] &&
197 ScaleQuantumToChar(GetBluePixelComponent(pixel)) == 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)
306 assert(image_info != (const ImageInfo *) NULL);
307 assert(image_info->signature == MagickSignature);
308 if (image_info->debug != MagickFalse)
309 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
310 image_info->filename);
311 assert(exception != (ExceptionInfo *) NULL);
312 assert(exception->signature == MagickSignature);
313 image=AcquireImage(image_info);
314 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
315 if (status == MagickFalse)
317 (void) DestroyImageList(image);
318 return((Image *) NULL);
323 image->columns=ReadBlobMSBShort(image);
324 image->rows=ReadBlobMSBShort(image);
325 if (EOFBlob(image) != MagickFalse)
326 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
327 if ((image->columns == 0) || (image->rows == 0))
328 ThrowReaderException(CorruptImageError,"NegativeOrZeroImageSize");
329 bytes_per_row=ReadBlobMSBShort(image);
330 flags=ReadBlobMSBShort(image);
331 bits_per_pixel=(size_t) ReadBlobByte(image);
332 if (bits_per_pixel > 16)
333 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
334 version=(size_t) ReadBlobByte(image);
336 nextDepthOffset=(size_t) ReadBlobMSBShort(image);
337 transparentIndex=(size_t) ReadBlobByte(image);
338 compressionType=(size_t) ReadBlobByte(image);
339 pad=ReadBlobMSBShort(image);
342 Initialize image colormap.
345 if ((bits_per_pixel < 16) &&
346 (AcquireImageColormap(image,one << bits_per_pixel) == MagickFalse))
347 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
348 GetMagickPixelPacket(image,&transpix);
349 if (bits_per_pixel == 16) /* Direct Color */
351 redbits=(size_t) ReadBlobByte(image); /* # of bits of red */
353 greenbits=(size_t) ReadBlobByte(image); /* # of bits of green */
355 bluebits=(size_t) ReadBlobByte(image); /* # of bits of blue */
357 ReadBlobByte(image); /* reserved by Palm */
358 ReadBlobByte(image); /* reserved by Palm */
359 transpix.red=(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);
400 image->storage_class=DirectClass;
401 if (bits_per_pixel < 16)
403 image->storage_class=PseudoClass;
406 one_row=(unsigned char *) AcquireQuantumMemory(bytes_per_row,
408 if (one_row == (unsigned char *) NULL)
409 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
410 lastrow=(unsigned char *) NULL;
411 if (compressionType == PALM_COMPRESSION_SCANLINE) {
412 lastrow=(unsigned char *) AcquireQuantumMemory(bytes_per_row,
414 if (lastrow == (unsigned char *) NULL)
415 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
417 mask=(1l << bits_per_pixel)-1;
418 for (y = 0; y < (ssize_t) image->rows; y++)
420 if ((flags & PALM_IS_COMPRESSED_FLAG) == 0)
422 /* TODO move out of loop! */
423 image->compression=NoCompression;
424 count=ReadBlob(image,bytes_per_row,one_row);
428 if (compressionType == PALM_COMPRESSION_RLE)
430 /* TODO move out of loop! */
431 image->compression=RLECompression;
432 for (i=0; i < (ssize_t) bytes_per_row; )
434 count=(ssize_t) ReadBlobByte(image);
435 count=MagickMin(count,(ssize_t) bytes_per_row-i);
436 byte=(size_t) ReadBlobByte(image);
437 (void) ResetMagickMemory(one_row+i,(int) byte,(size_t) count);
442 if (compressionType == PALM_COMPRESSION_SCANLINE)
447 /* TODO move out of loop! */
449 image->compression=FaxCompression;
450 for (i=0; i < (ssize_t) bytes_per_row; i+=8)
452 count=(ssize_t) ReadBlobByte(image);
453 byte=1UL*MagickMin((ssize_t) bytes_per_row-i,8);
454 for (bit=0; bit < byte; bit++)
456 if ((y == 0) || (count & (one << (7 - bit))))
457 one_row[i+bit]=(unsigned char) ReadBlobByte(image);
459 one_row[i+bit]=lastrow[i+bit];
462 (void) CopyMagickMemory(lastrow, one_row, bytes_per_row);
466 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
467 if (q == (PixelPacket *) NULL)
469 indexes=GetAuthenticIndexQueue(image);
470 if (bits_per_pixel == 16)
472 if (image->columns > (2*bytes_per_row))
473 ThrowReaderException(CorruptImageError,"CorruptImage");
474 for (x=0; x < (ssize_t) image->columns; x++)
476 color16=(*ptr++ << 8);
478 SetRedPixelComponent(q,(QuantumRange*((color16 >> 11) & 0x1f))/
480 SetGreenPixelComponent(q,(QuantumRange*((color16 >> 5) & 0x3f))/
482 SetBluePixelComponent(q,(QuantumRange*((color16 >> 0) & 0x1f))/
484 SetOpacityPixelComponent(q,OpaqueOpacity);
490 bit=8-bits_per_pixel;
491 for (x=0; x < (ssize_t) image->columns; x++)
493 if ((size_t) (ptr-one_row) >= bytes_per_row)
494 ThrowReaderException(CorruptImageError,"CorruptImage");
495 index=(IndexPacket) (mask-(((*ptr) & (mask << bit)) >> bit));
496 SetIndexPixelComponent(indexes+x,index);
497 SetRGBOPixelComponents(q,image->colormap+(ssize_t) index);
503 bit=8-bits_per_pixel;
507 if (SyncAuthenticPixels(image,exception) == MagickFalse)
510 if (image->previous == (Image *) NULL)
512 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
514 if (status == MagickFalse)
518 if (flags & PALM_HAS_TRANSPARENCY_FLAG)
520 if (bits_per_pixel != 16)
521 SetMagickPixelPacket(image,image->colormap+(mask-transparentIndex),
522 (const IndexPacket *) NULL,&transpix);
523 (void) TransparentPaintImage(image,&transpix,(Quantum)
524 TransparentOpacity,MagickFalse);
526 one_row=(unsigned char *) RelinquishMagickMemory(one_row);
527 if (compressionType == PALM_COMPRESSION_SCANLINE)
528 lastrow=(unsigned char *) RelinquishMagickMemory(lastrow);
530 Proceed to next image. Copied from coders/pnm.c
532 if (image_info->number_scenes != 0)
533 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
535 if (nextDepthOffset != 0)
540 totalOffset+=(MagickOffsetType) (nextDepthOffset*4);
541 if (totalOffset >= (MagickOffsetType) GetBlobSize(image))
543 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
547 seekNextDepth=SeekBlob(image,totalOffset,SEEK_SET);
549 if (seekNextDepth != totalOffset)
550 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
552 Allocate next image structure. Copied from coders/pnm.c
554 AcquireNextImage(image_info,image);
555 if (GetNextImageInList(image) == (Image *) NULL)
557 (void) DestroyImageList(image);
558 return((Image *) NULL);
560 image=SyncNextImageInList(image);
561 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
563 if (status == MagickFalse)
566 } while (nextDepthOffset != 0);
567 (void) CloseBlob(image);
568 return(GetFirstImageInList(image));
572 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
576 % R e g i s t e r P A L M I m a g e %
580 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
582 % RegisterPALMImage() adds properties for the PALM image format to the list of
583 % supported formats. The properties include the image format tag, a method to
584 % read and/or write the format, whether the format supports the saving of more
585 % than one frame to the same file or blob, whether the format supports native
586 % in-memory I/O, and a brief description of the format.
588 % The format of the RegisterPALMImage method is:
590 % size_t RegisterPALMImage(void)
593 ModuleExport size_t RegisterPALMImage(void)
598 entry=SetMagickInfo("PALM");
599 entry->decoder=(DecodeImageHandler *) ReadPALMImage;
600 entry->encoder=(EncodeImageHandler *) WritePALMImage;
601 entry->seekable_stream=MagickTrue;
602 entry->description=ConstantString("Palm pixmap");
603 entry->module=ConstantString("PALM");
604 (void) RegisterMagickInfo(entry);
605 return(MagickImageCoderSignature);
609 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
613 % U n r e g i s t e r P A L M I m a g e %
617 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
619 % UnregisterPALMImage() removes format registrations made by the PALM
620 % module from the list of supported formats.
622 % The format of the UnregisterPALMImage method is:
624 % UnregisterPALMImage(void)
627 ModuleExport void UnregisterPALMImage(void)
629 (void) UnregisterMagickInfo("PALM");
633 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
637 % W r i t e P A L M I m a g e %
641 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
643 % WritePALMImage() writes an image of raw bits in LSB order to a file.
645 % The format of the WritePALMImage method is:
647 % MagickBooleanType WritePALMImage(const ImageInfo *image_info,
650 % A description of each parameter follows.
652 % o image_info: Specifies a pointer to an ImageInfo structure.
654 % o image: A pointer to a Image structure.
657 static MagickBooleanType WritePALMImage(const ImageInfo *image_info,
716 Open output image file.
718 assert(image_info != (const ImageInfo *) NULL);
719 assert(image_info->signature == MagickSignature);
720 assert(image != (Image *) NULL);
721 assert(image->signature == MagickSignature);
722 if (image->debug != MagickFalse)
723 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
724 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&exception);
725 if (status == MagickFalse)
727 GetExceptionInfo(&exception);
728 quantize_info=AcquireQuantizeInfo(image_info);
741 if (image->colorspace != RGBColorspace)
742 (void) TransformImageColorspace(image,RGBColorspace);
743 count=GetNumberColors(image,NULL,&exception);
744 for (bits_per_pixel=1; (one << bits_per_pixel) < count; bits_per_pixel*=2) ;
745 if (image_info->depth > 100)
746 bits_per_pixel=image_info->depth-100;
747 if (bits_per_pixel < 16)
748 (void) TransformImageColorspace(image,image->colorspace);
749 if (bits_per_pixel < 8)
751 (void) TransformImageColorspace(image,GRAYColorspace);
752 (void) SetImageType(image,PaletteType);
753 (void) SortColormapByIntensity(image);
755 if ((image->storage_class == PseudoClass) && (image->colors > 256))
756 (void) SetImageStorageClass(image,DirectClass);
757 if (image->storage_class == PseudoClass)
758 flags|=PALM_HAS_COLORMAP_FLAG;
760 flags|=PALM_IS_DIRECT_COLOR;
761 (void) WriteBlobMSBShort(image,(unsigned short) image->columns); /* width */
762 (void) WriteBlobMSBShort(image,(unsigned short) image->rows); /* height */
763 bytes_per_row=((image->columns+(16/bits_per_pixel-1))/(16/
765 (void) WriteBlobMSBShort(image,(unsigned short) bytes_per_row);
766 if ((image_info->compression == RLECompression) ||
767 (image_info->compression == FaxCompression))
768 flags|=PALM_IS_COMPRESSED_FLAG;
769 (void) WriteBlobMSBShort(image, flags);
770 (void) WriteBlobByte(image,(unsigned char) bits_per_pixel);
771 if (bits_per_pixel > 1)
773 if ((image_info->compression == RLECompression) ||
774 (image_info->compression == FaxCompression))
776 (void) WriteBlobByte(image,version);
777 (void) WriteBlobMSBShort(image,0); /* nextDepthOffset */
778 (void) WriteBlobByte(image,(unsigned char) transparentIndex);
779 if (image_info->compression == RLECompression)
780 (void) WriteBlobByte(image,PALM_COMPRESSION_RLE);
782 if (image_info->compression == FaxCompression)
783 (void) WriteBlobByte(image,PALM_COMPRESSION_SCANLINE);
785 (void) WriteBlobByte(image,PALM_COMPRESSION_NONE);
786 (void) WriteBlobMSBShort(image,0); /* reserved */
788 if (bits_per_pixel == 16)
790 (void) WriteBlobByte(image,5); /* # of bits of red */
791 (void) WriteBlobByte(image,6); /* # of bits of green */
792 (void) WriteBlobByte(image,5); /* # of bits of blue */
793 (void) WriteBlobByte(image,0); /* reserved by Palm */
794 (void) WriteBlobMSBLong(image,0); /* no transparent color, YET */
797 if (bits_per_pixel == 8)
799 if (flags & PALM_HAS_COLORMAP_FLAG) /* Write out colormap */
801 quantize_info->dither=IsPaletteImage(image,&image->exception);
802 quantize_info->number_colors=image->colors;
803 (void) QuantizeImage(quantize_info,image);
804 (void) WriteBlobMSBShort(image,(unsigned short) image->colors);
805 for (count = 0; count < image->colors; count++)
807 (void) WriteBlobByte(image,(unsigned char) count);
808 (void) WriteBlobByte(image,ScaleQuantumToChar(
809 image->colormap[count].red));
810 (void) WriteBlobByte(image,
811 ScaleQuantumToChar(image->colormap[count].green));
812 (void) WriteBlobByte(image,
813 ScaleQuantumToChar(image->colormap[count].blue));
817 else /* Map colors to Palm standard colormap */
822 affinity_image=ConstituteImage(256,1,"RGB",CharPixel,&PalmPalette,
824 (void) TransformImageColorspace(affinity_image,
825 affinity_image->colorspace);
826 (void) RemapImage(quantize_info,image,affinity_image);
827 for (y=0; y < (ssize_t) image->rows; y++)
829 p=GetAuthenticPixels(image,0,y,image->columns,1,&exception);
830 indexes=GetAuthenticIndexQueue(image);
831 for (x=0; x < (ssize_t) image->columns; x++)
832 SetIndexPixelComponent(indexes+x,FindColor(&image->colormap[
833 (ssize_t) GetIndexPixelComponent(indexes+x)]));
835 affinity_image=DestroyImage(affinity_image);
838 if (flags & PALM_IS_COMPRESSED_FLAG)
839 (void) WriteBlobMSBShort(image,0); /* fill in size later */
840 lastrow=(unsigned char *) NULL;
841 if (image_info->compression == FaxCompression)
842 lastrow=(unsigned char *) AcquireQuantumMemory(bytes_per_row,
844 /* TODO check whether memory really was acquired? */
845 one_row=(unsigned char *) AcquireQuantumMemory(bytes_per_row,
847 if (one_row == (unsigned char *) NULL)
848 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
849 for (y=0; y < (int) image->rows; y++)
852 (void) ResetMagickMemory(ptr,0,bytes_per_row);
853 p=GetAuthenticPixels(image,0,y,image->columns,1,&exception);
854 if (p == (PixelPacket *) NULL)
856 indexes=GetAuthenticIndexQueue(image);
857 if (bits_per_pixel == 16)
859 for (x=0; x < (int) image->columns; x++)
861 color16=(unsigned short) ((((31*(size_t) GetRedPixelComponent(p))/
862 (size_t) QuantumRange) << 11) |
863 (((63*(size_t) GetGreenPixelComponent(p))/(size_t) QuantumRange) << 5) |
864 ((31*(size_t) GetBluePixelComponent(p))/(size_t) QuantumRange));
865 if (GetOpacityPixelComponent(p) == (Quantum) TransparentOpacity)
867 transpix.red=GetRedPixelComponent(p);
868 transpix.green=GetGreenPixelComponent(p);
869 transpix.blue=GetBluePixelComponent(p);
870 transpix.opacity=GetOpacityPixelComponent(p);
871 flags|=PALM_HAS_TRANSPARENCY_FLAG;
873 *ptr++=(unsigned char) ((color16 >> 8) & 0xff);
874 *ptr++=(unsigned char) (color16 & 0xff);
881 bit=(unsigned char) (8-bits_per_pixel);
882 for (x=0; x < (int) image->columns; x++)
884 if (bits_per_pixel >= 8)
885 color=(unsigned char) GetIndexPixelComponent(indexes+x);
887 color=(unsigned char) (GetIndexPixelComponent(indexes+x)*
888 ((one << bits_per_pixel)-1)/MagickMax(1*image->colors-1,1));
891 bit-=(unsigned char) bits_per_pixel;
896 bit=(unsigned char) (8-bits_per_pixel);
899 if ((image->columns % (8/bits_per_pixel)) != 0)
902 if (image_info->compression == RLECompression)
905 while (x < (ssize_t) bytes_per_row)
909 while ((one_row[++x] == byte) && (count < 255) &&
910 (x < (ssize_t) bytes_per_row))
912 (void) WriteBlobByte(image,(unsigned char) count);
913 (void) WriteBlobByte(image,(unsigned char) byte);
917 if (image_info->compression == FaxCompression)
923 for (x = 0; x < (ssize_t) bytes_per_row; x += 8)
926 for (bit=0, byte=0; bit < (unsigned char) MagickMin(8,(ssize_t) bytes_per_row-x); bit++)
928 if ((y == 0) || (lastrow[x + bit] != one_row[x + bit]))
930 byte |= (1 << (7 - bit));
931 *tptr++ = (char) one_row[x + bit];
934 (void) WriteBlobByte(image, byte);
935 (void) WriteBlob(image,tptr-tmpbuf,(unsigned char *) tmpbuf);
937 (void) CopyMagickMemory(lastrow,one_row,bytes_per_row);
940 (void) WriteBlob(image,bytes_per_row,one_row);
942 if (flags & PALM_HAS_TRANSPARENCY_FLAG)
944 offset=SeekBlob(image,currentOffset+6,SEEK_SET);
945 (void) WriteBlobMSBShort(image,flags);
946 offset=SeekBlob(image,currentOffset+12,SEEK_SET);
947 (void) WriteBlobByte(image,(unsigned char) transparentIndex); /* trans index */
949 if (bits_per_pixel == 16)
951 offset=SeekBlob(image,currentOffset+20,SEEK_SET);
952 (void) WriteBlobByte(image,0); /* reserved by Palm */
953 (void) WriteBlobByte(image,(unsigned char) ((31*transpix.red)/QuantumRange));
954 (void) WriteBlobByte(image,(unsigned char) ((63*transpix.green)/QuantumRange));
955 (void) WriteBlobByte(image,(unsigned char) ((31*transpix.blue)/QuantumRange));
957 if (flags & PALM_IS_COMPRESSED_FLAG) /* fill in size now */
959 offset=SeekBlob(image,currentOffset+offset,SEEK_SET);
960 (void) WriteBlobMSBShort(image,(unsigned short) (GetBlobSize(image)-
961 currentOffset-offset));
963 if (one_row != (unsigned char *) NULL)
964 one_row=(unsigned char *) RelinquishMagickMemory(one_row);
965 if (lastrow != (unsigned char *) NULL)
966 lastrow=(unsigned char *) RelinquishMagickMemory(lastrow);
967 if (GetNextImageInList(image) == (Image *) NULL)
969 /* padding to 4 byte word */
970 for (cc = (GetBlobSize(image))%4; cc > 0; cc--)
972 (void) WriteBlobByte(image,0);
974 /* write nextDepthOffset and return to end of image */
975 offset=SeekBlob(image,currentOffset+10,SEEK_SET);
976 nextDepthOffset=(size_t) ((GetBlobSize(image)-currentOffset)/4);
977 (void) WriteBlobMSBShort(image,(unsigned short) nextDepthOffset);
978 currentOffset=(MagickOffsetType) GetBlobSize(image);
979 offset=SeekBlob(image,currentOffset,SEEK_SET);
980 image=SyncNextImageInList(image);
981 status=SetImageProgress(image,SaveImagesTag,scene++,
982 GetImageListLength(image));
983 if (status == MagickFalse)
985 } while (image_info->adjoin != MagickFalse);
986 quantize_info=DestroyQuantizeInfo(quantize_info);
987 (void) CloseBlob(image);
988 (void) DestroyExceptionInfo(&exception);