]> granicus.if.org Git - imagemagick/blob - coders/palm.c
...
[imagemagick] / coders / palm.c
1 /*
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %                                                                             %
4 %                                                                             %
5 %                                                                             %
6 %                        PPPP    AAA   L      M   M                           %
7 %                        P   P  A   A  L      MM MM                           %
8 %                        PPPP   AAAAA  L      M M M                           %
9 %                        P      A   A  L      M   M                           %
10 %                        P      A   A  LLLLL  M   M                           %
11 %                                                                             %
12 %                                                                             %
13 %                          Read/Write Palm Pixmap.                            %
14 %                                                                             %
15 %                                                                             %
16 %                              Software Design                                %
17 %                            Christopher R. Hawks                             %
18 %                               December 2001                                 %
19 %                                                                             %
20 %  Copyright 1999-2004 ImageMagick Studio LLC, a non-profit organization      %
21 %  dedicated to making software imaging solutions freely available.           %
22 %                                                                             %
23 %  You may not use this file except in compliance with the License.  You may  %
24 %  obtain a copy of the License at                                            %
25 %                                                                             %
26 %    https://www.imagemagick.org/script/license.php                           %
27 %                                                                             %
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.                                             %
33 %                                                                             %
34 %                                                                             %
35 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
36 %
37 %  Based on pnmtopalm by Bill Janssen and ppmtobmp by Ian Goldberg.
38 %
39 */
40 \f
41 /*
42   Include declarations.
43 */
44 #include "MagickCore/studio.h"
45 #include "MagickCore/attribute.h"
46 #include "MagickCore/blob.h"
47 #include "MagickCore/blob-private.h"
48 #include "MagickCore/cache.h"
49 #include "MagickCore/color.h"
50 #include "MagickCore/colormap.h"
51 #include "MagickCore/colormap-private.h"
52 #include "MagickCore/color-private.h"
53 #include "MagickCore/colorspace.h"
54 #include "MagickCore/colorspace-private.h"
55 #include "MagickCore/constitute.h"
56 #include "MagickCore/exception.h"
57 #include "MagickCore/histogram.h"
58 #include "MagickCore/image.h"
59 #include "MagickCore/image-private.h"
60 #include "MagickCore/list.h"
61 #include "MagickCore/magick.h"
62 #include "MagickCore/memory_.h"
63 #include "MagickCore/monitor.h"
64 #include "MagickCore/monitor-private.h"
65 #include "MagickCore/paint.h"
66 #include "MagickCore/pixel-accessor.h"
67 #include "MagickCore/property.h"
68 #include "MagickCore/quantize.h"
69 #include "MagickCore/quantum-private.h"
70 #include "MagickCore/static.h"
71 #include "MagickCore/string_.h"
72 #include "MagickCore/module.h"
73 #include "MagickCore/utility.h"
74 \f
75 /*
76   Define declarations.
77 */
78 #define PALM_IS_COMPRESSED_FLAG  0x8000
79 #define PALM_HAS_COLORMAP_FLAG  0x4000
80 #define PALM_HAS_FOUR_BYTE_FIELD  0x0200
81 #define PALM_HAS_TRANSPARENCY_FLAG  0x2000
82 #define PALM_IS_INDIRECT  0x1000
83 #define PALM_IS_FOR_SCREEN  0x0800
84 #define PALM_IS_DIRECT_COLOR  0x0400
85 #define PALM_COMPRESSION_SCANLINE  0x00
86 #define PALM_COMPRESSION_RLE  0x01
87 #define PALM_COMPRESSION_NONE  0xFF
88 \f
89 /*
90  The 256 color system palette for Palm Computing Devices.
91 */
92 static const unsigned char
93   PalmPalette[256][3] =
94   {
95     {255, 255,255}, {255, 204,255}, {255, 153,255}, {255, 102,255},
96     {255,  51,255}, {255,   0,255}, {255, 255,204}, {255, 204,204},
97     {255, 153,204}, {255, 102,204}, {255,  51,204}, {255,   0,204},
98     {255, 255,153}, {255, 204,153}, {255, 153,153}, {255, 102,153},
99     {255,  51,153}, {255,   0,153}, {204, 255,255}, {204, 204,255},
100     {204, 153,255}, {204, 102,255}, {204,  51,255}, {204,   0,255},
101     {204, 255,204}, {204, 204,204}, {204, 153,204}, {204, 102,204},
102     {204,  51,204}, {204,   0,204}, {204, 255,153}, {204, 204,153},
103     {204, 153,153}, {204, 102,153}, {204,  51,153}, {204,   0,153},
104     {153, 255,255}, {153, 204,255}, {153, 153,255}, {153, 102,255},
105     {153,  51,255}, {153,   0,255}, {153, 255,204}, {153, 204,204},
106     {153, 153,204}, {153, 102,204}, {153,  51,204}, {153,   0,204},
107     {153, 255,153}, {153, 204,153}, {153, 153,153}, {153, 102,153},
108     {153,  51,153}, {153,   0,153}, {102, 255,255}, {102, 204,255},
109     {102, 153,255}, {102, 102,255}, {102,  51,255}, {102,   0,255},
110     {102, 255,204}, {102, 204,204}, {102, 153,204}, {102, 102,204},
111     {102,  51,204}, {102,   0,204}, {102, 255,153}, {102, 204,153},
112     {102, 153,153}, {102, 102,153}, {102,  51,153}, {102,   0,153},
113     { 51, 255,255}, { 51, 204,255}, { 51, 153,255}, { 51, 102,255},
114     { 51,  51,255}, { 51,   0,255}, { 51, 255,204}, { 51, 204,204},
115     { 51, 153,204}, { 51, 102,204}, { 51,  51,204}, { 51,   0,204},
116     { 51, 255,153}, { 51, 204,153}, { 51, 153,153}, { 51, 102,153},
117     { 51,  51,153}, { 51,   0,153}, {  0, 255,255}, {  0, 204,255},
118     {  0, 153,255}, {  0, 102,255}, {  0,  51,255}, {  0,   0,255},
119     {  0, 255,204}, {  0, 204,204}, {  0, 153,204}, {  0, 102,204},
120     {  0,  51,204}, {  0,   0,204}, {  0, 255,153}, {  0, 204,153},
121     {  0, 153,153}, {  0, 102,153}, {  0,  51,153}, {  0,   0,153},
122     {255, 255,102}, {255, 204,102}, {255, 153,102}, {255, 102,102},
123     {255,  51,102}, {255,   0,102}, {255, 255, 51}, {255, 204, 51},
124     {255, 153, 51}, {255, 102, 51}, {255,  51, 51}, {255,   0, 51},
125     {255, 255,  0}, {255, 204,  0}, {255, 153,  0}, {255, 102,  0},
126     {255,  51,  0}, {255,   0,  0}, {204, 255,102}, {204, 204,102},
127     {204, 153,102}, {204, 102,102}, {204,  51,102}, {204,   0,102},
128     {204, 255, 51}, {204, 204, 51}, {204, 153, 51}, {204, 102, 51},
129     {204,  51, 51}, {204,   0, 51}, {204, 255,  0}, {204, 204,  0},
130     {204, 153,  0}, {204, 102,  0}, {204,  51,  0}, {204,   0,  0},
131     {153, 255,102}, {153, 204,102}, {153, 153,102}, {153, 102,102},
132     {153,  51,102}, {153,   0,102}, {153, 255, 51}, {153, 204, 51},
133     {153, 153, 51}, {153, 102, 51}, {153,  51, 51}, {153,   0, 51},
134     {153, 255,  0}, {153, 204,  0}, {153, 153,  0}, {153, 102,  0},
135     {153,  51,  0}, {153,   0,  0}, {102, 255,102}, {102, 204,102},
136     {102, 153,102}, {102, 102,102}, {102,  51,102}, {102,   0,102},
137     {102, 255, 51}, {102, 204, 51}, {102, 153, 51}, {102, 102, 51},
138     {102,  51, 51}, {102,   0, 51}, {102, 255,  0}, {102, 204,  0},
139     {102, 153,  0}, {102, 102,  0}, {102,  51,  0}, {102,   0,  0},
140     { 51, 255,102}, { 51, 204,102}, { 51, 153,102}, { 51, 102,102},
141     { 51,  51,102}, { 51,   0,102}, { 51, 255, 51}, { 51, 204, 51},
142     { 51, 153, 51}, { 51, 102, 51}, { 51,  51, 51}, { 51,   0, 51},
143     { 51, 255,  0}, { 51, 204,  0}, { 51, 153,  0}, { 51, 102,  0},
144     { 51,  51,  0}, { 51,   0,  0}, {  0, 255,102}, {  0, 204,102},
145     {  0, 153,102}, {  0, 102,102}, {  0,  51,102}, {  0,   0,102},
146     {  0, 255, 51}, {  0, 204, 51}, {  0, 153, 51}, {  0, 102, 51},
147     {  0,  51, 51}, {  0,   0, 51}, {  0, 255,  0}, {  0, 204,  0},
148     {  0, 153,  0}, {  0, 102,  0}, {  0,  51,  0}, { 17,  17, 17},
149     { 34,  34, 34}, { 68,  68, 68}, { 85,  85, 85}, {119, 119,119},
150     {136, 136,136}, {170, 170,170}, {187, 187,187}, {221, 221,221},
151     {238, 238,238}, {192, 192,192}, {128,   0,  0}, {128,   0,128},
152     {  0, 128,  0}, {  0, 128,128}, {  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},
157     {  0,   0,  0}, {  0,   0,  0}, {  0,   0,  0}, {  0,   0,  0},
158     {  0,   0,  0}, {  0,   0,  0}, {  0,   0,  0}, {  0,   0,  0}
159   };
160 \f
161 /*
162   Forward declarations.
163 */
164 static MagickBooleanType
165   WritePALMImage(const ImageInfo *,Image *,ExceptionInfo *);
166 \f
167 /*
168 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
169 %                                                                             %
170 %                                                                             %
171 %                                                                             %
172 %   F i n d C o l o r                                                         %
173 %                                                                             %
174 %                                                                             %
175 %                                                                             %
176 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
177 %
178 %  FindColor() returns the index of the matching entry from PalmPalette for a
179 %  given PixelInfo.
180 %
181 %  The format of the FindColor method is:
182 %
183 %      int FindColor(PixelInfo *pixel)
184 %
185 %  A description of each parameter follows:
186 %
187 %    o int: the index of the matching color or -1 if not found/
188 %
189 %    o pixel: a pointer to the PixelInfo to be matched.
190 %
191 */
192 static ssize_t FindColor(PixelInfo *packet)
193 {
194   register ssize_t
195     i;
196
197   for (i=0; i < 256; i++)
198     if (ScaleQuantumToChar(ClampToQuantum(packet->red)) == PalmPalette[i][0] &&
199         ScaleQuantumToChar(ClampToQuantum(packet->green)) == PalmPalette[i][1] &&
200         ScaleQuantumToChar(ClampToQuantum(packet->blue)) == PalmPalette[i][2])
201       return(i);
202   return(-1);
203 }
204 \f
205 /*
206 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
207 %                                                                             %
208 %                                                                             %
209 %                                                                             %
210 %   R e a d P A L M I m a g e                                                 %
211 %                                                                             %
212 %                                                                             %
213 %                                                                             %
214 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
215 %
216 %  ReadPALMImage() reads an image of raw bites in LSB order and returns it.  It
217 %  allocates the memory necessary for the new Image structure and returns a
218 %  pointer to the new image.
219 %
220 %  The format of the ReadPALMImage method is:
221 %
222 %      Image *ReadPALMImage(const ImageInfo *image_info,
223 %        ExceptionInfo *exception)
224 %
225 %  A description of each parameter follows:
226 %
227 %    o image_info: Specifies a pointer to an ImageInfo structure.
228 %
229 %    o exception: return any errors or warnings in this structure.
230 %
231 */
232 static Image *ReadPALMImage(const ImageInfo *image_info,
233   ExceptionInfo *exception)
234 {
235   Image
236     *image;
237
238   MagickBooleanType
239     status;
240
241   MagickOffsetType
242     totalOffset,
243     seekNextDepth;
244
245   PixelInfo
246     transpix;
247
248   Quantum
249     index;
250
251   register ssize_t
252     i,
253     x;
254
255   register Quantum
256     *q;
257
258   size_t
259     bytes_per_row,
260     flags,
261     bits_per_pixel,
262     version,
263     nextDepthOffset,
264     transparentIndex,
265     compressionType,
266     byte,
267     mask,
268     redbits,
269     greenbits,
270     bluebits,
271     one,
272     pad,
273     size,
274     bit;
275
276   ssize_t
277     count,
278     y;
279
280   unsigned char
281     *last_row,
282     *one_row,
283     *ptr;
284
285   unsigned short
286     color16;
287
288   /*
289     Open image file.
290   */
291   assert(image_info != (const ImageInfo *) NULL);
292   assert(image_info->signature == MagickCoreSignature);
293   if (image_info->debug != MagickFalse)
294     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
295       image_info->filename);
296   assert(exception != (ExceptionInfo *) NULL);
297   assert(exception->signature == MagickCoreSignature);
298   image=AcquireImage(image_info,exception);
299   status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
300   if (status == MagickFalse)
301     {
302       (void) DestroyImageList(image);
303       return((Image *) NULL);
304     }
305   totalOffset=0;
306   do
307   {
308     image->columns=ReadBlobMSBShort(image);
309     image->rows=ReadBlobMSBShort(image);
310     if (EOFBlob(image) != MagickFalse)
311       ThrowReaderException(CorruptImageError,"ImproperImageHeader");
312     if ((image->columns == 0) || (image->rows == 0))
313       ThrowReaderException(CorruptImageError,"NegativeOrZeroImageSize");
314     status=SetImageExtent(image,image->columns,image->rows,exception);
315     if (status == MagickFalse)
316       return(DestroyImageList(image));
317     bytes_per_row=ReadBlobMSBShort(image);
318     flags=ReadBlobMSBShort(image);
319     bits_per_pixel=(size_t) ReadBlobByte(image);
320     if ((bits_per_pixel != 1) && (bits_per_pixel != 2) &&
321         (bits_per_pixel != 4) && (bits_per_pixel != 8) &&
322         (bits_per_pixel != 16))
323       ThrowReaderException(CorruptImageError,"UnrecognizedBitsPerPixel");
324     version=(size_t) ReadBlobByte(image);
325     if ((version != 0) && (version != 1) && (version != 2))
326       ThrowReaderException(CorruptImageError,"FileFormatVersionMismatch");
327     nextDepthOffset=(size_t) ReadBlobMSBShort(image);
328     transparentIndex=(size_t) ReadBlobByte(image);
329     compressionType=(size_t) ReadBlobByte(image);
330     if ((compressionType != PALM_COMPRESSION_NONE) &&
331         (compressionType != PALM_COMPRESSION_SCANLINE ) &&
332         (compressionType != PALM_COMPRESSION_RLE))
333       ThrowReaderException(CorruptImageError,"UnrecognizedImageCompression");
334     pad=ReadBlobMSBShort(image);
335     (void) pad;
336     /*
337       Initialize image colormap.
338     */
339     one=1;
340     if ((bits_per_pixel < 16) &&
341         (AcquireImageColormap(image,one << bits_per_pixel,exception) == MagickFalse))
342       ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
343     GetPixelInfo(image,&transpix);
344     if (bits_per_pixel == 16)  /* Direct Color */
345       {
346         redbits=(size_t) ReadBlobByte(image);  /* # of bits of red */
347         (void) redbits;
348         greenbits=(size_t) ReadBlobByte(image);  /* # of bits of green */
349         (void) greenbits;
350         bluebits=(size_t) ReadBlobByte(image);  /* # of bits of blue */
351         (void) bluebits;
352         ReadBlobByte(image);  /* reserved by Palm */
353         ReadBlobByte(image);  /* reserved by Palm */
354         transpix.red=(double) (QuantumRange*ReadBlobByte(image)/31);
355         transpix.green=(double) (QuantumRange*ReadBlobByte(image)/63);
356         transpix.blue=(double) (QuantumRange*ReadBlobByte(image)/31);
357       }
358     if (bits_per_pixel == 8)
359       {
360         ssize_t
361           index;
362
363         if (flags & PALM_HAS_COLORMAP_FLAG)
364           {
365             count=(ssize_t) ReadBlobMSBShort(image);
366             for (i=0; i < (ssize_t) count; i++)
367             {
368               ReadBlobByte(image);
369               index=ConstrainColormapIndex(image,255-i,exception);
370               image->colormap[index].red=(MagickRealType)
371                 ScaleCharToQuantum((unsigned char) ReadBlobByte(image));
372               image->colormap[index].green=(MagickRealType)
373                 ScaleCharToQuantum((unsigned char) ReadBlobByte(image));
374               image->colormap[index].blue=(MagickRealType)
375                 ScaleCharToQuantum((unsigned char) ReadBlobByte(image));
376           }
377         }
378       else
379         for (i=0; i < (ssize_t) (1L << bits_per_pixel); i++)
380         {
381           index=ConstrainColormapIndex(image,255-i,exception);
382           image->colormap[index].red=(MagickRealType)
383             ScaleCharToQuantum(PalmPalette[i][0]);
384           image->colormap[index].green=(MagickRealType)
385             ScaleCharToQuantum(PalmPalette[i][1]);
386           image->colormap[index].blue=(MagickRealType)
387             ScaleCharToQuantum(PalmPalette[i][2]);
388         }
389       }
390     if (flags & PALM_IS_COMPRESSED_FLAG)
391       size=ReadBlobMSBShort(image);
392     (void) size;
393     image->storage_class=DirectClass;
394     if (bits_per_pixel < 16)
395       {
396         image->storage_class=PseudoClass;
397         image->depth=8;
398       }
399     if (image_info->ping != MagickFalse)
400       {
401         (void) CloseBlob(image);
402         return(image);
403       }
404     status=SetImageExtent(image,image->columns,image->rows,exception);
405     if (status == MagickFalse)
406       return(DestroyImageList(image));
407     one_row=(unsigned char *) AcquireQuantumMemory(MagickMax(bytes_per_row,
408       2*image->columns),sizeof(*one_row));
409     if (one_row == (unsigned char *) NULL)
410       ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
411     last_row=(unsigned char *) NULL;
412     if (compressionType == PALM_COMPRESSION_SCANLINE)
413       {
414         last_row=(unsigned char *) AcquireQuantumMemory(MagickMax(bytes_per_row,
415           2*image->columns),sizeof(*last_row));
416         if (last_row == (unsigned char *) NULL)
417           {
418             one_row=(unsigned char *) RelinquishMagickMemory(one_row);
419             ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
420           }
421       }
422     mask=(size_t) (1U << bits_per_pixel)-1;
423     for (y=0; y < (ssize_t) image->rows; y++)
424     {
425       if ((flags & PALM_IS_COMPRESSED_FLAG) == 0)
426         {
427           /* TODO move out of loop! */
428           image->compression=NoCompression;
429           count=ReadBlob(image,bytes_per_row,one_row);
430           if (count != (ssize_t) bytes_per_row)
431             break;
432         }
433       else
434         {
435           if (compressionType == PALM_COMPRESSION_RLE)
436             { 
437               /* TODO move out of loop! */
438               image->compression=RLECompression;
439               for (i=0; i < (ssize_t) bytes_per_row; )
440               {
441                 count=(ssize_t) ReadBlobByte(image);
442                 if (count < 0)
443                   break;
444                 count=MagickMin(count,(ssize_t) bytes_per_row-i);
445                 byte=(size_t) ReadBlobByte(image);
446                 (void) ResetMagickMemory(one_row+i,(int) byte,(size_t) count);
447                 i+=count;
448               }
449           }
450         else
451           if (compressionType == PALM_COMPRESSION_SCANLINE)
452             {
453               size_t
454                 one;
455
456               /* TODO move out of loop! */
457               one=1;
458               image->compression=FaxCompression;
459               for (i=0; i < (ssize_t) bytes_per_row; i+=8)
460               {
461                 count=(ssize_t) ReadBlobByte(image);
462                 if (count < 0)
463                   break;
464                 byte=(size_t) MagickMin((ssize_t) bytes_per_row-i,8);
465                 for (bit=0; bit < byte; bit++)
466                 {
467                   if ((y == 0) || (count & (one << (7 - bit))))
468                     one_row[i+bit]=(unsigned char) ReadBlobByte(image);
469                   else
470                     one_row[i+bit]=last_row[i+bit];
471                 }
472               }
473               (void) CopyMagickMemory(last_row, one_row, bytes_per_row);
474             }
475         }
476       ptr=one_row;
477       q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
478       if (q == (Quantum *) NULL)
479         break;
480       if (bits_per_pixel == 16)
481         {
482           if (image->columns > (2*bytes_per_row))
483             {
484               one_row=(unsigned char *) RelinquishMagickMemory(one_row);
485               if (compressionType == PALM_COMPRESSION_SCANLINE)
486                 last_row=(unsigned char *) RelinquishMagickMemory(last_row);
487               ThrowReaderException(CorruptImageError,"CorruptImage");
488             }
489           for (x=0; x < (ssize_t) image->columns; x++)
490           {
491             color16=(*ptr++ << 8);
492             color16|=(*ptr++);
493             SetPixelRed(image,(Quantum) ((QuantumRange*((color16 >> 11) &
494               0x1f))/0x1f),q);
495             SetPixelGreen(image,(Quantum) ((QuantumRange*((color16 >> 5) &
496               0x3f))/0x3f),q);
497             SetPixelBlue(image,(Quantum) ((QuantumRange*((color16 >> 0) &
498               0x1f))/0x1f),q);
499             SetPixelAlpha(image,OpaqueAlpha,q);
500             q+=GetPixelChannels(image);
501           }
502         }
503       else
504         {
505           bit=8-bits_per_pixel;
506           for (x=0; x < (ssize_t) image->columns; x++)
507           {
508             if ((size_t) (ptr-one_row) >= bytes_per_row)
509               {
510                 one_row=(unsigned char *) RelinquishMagickMemory(one_row);
511                 if (compressionType == PALM_COMPRESSION_SCANLINE)
512                   last_row=(unsigned char *) RelinquishMagickMemory(last_row);
513                 ThrowReaderException(CorruptImageError,"CorruptImage");
514               }
515             index=(Quantum) (mask-(((*ptr) & (mask << bit)) >> bit));
516             SetPixelIndex(image,index,q);
517             SetPixelViaPixelInfo(image,image->colormap+(ssize_t) index,q);
518             if (bit)
519               bit-=bits_per_pixel;
520             else
521               {
522                 ptr++;
523                 bit=8-bits_per_pixel;
524               }
525             q+=GetPixelChannels(image);
526           }
527           if (SyncAuthenticPixels(image,exception) == MagickFalse)
528             break;
529         }
530         if (image->previous == (Image *) NULL)
531           {
532             status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
533               image->rows);
534             if (status == MagickFalse)
535               break;
536           }
537       }
538     if (flags & PALM_HAS_TRANSPARENCY_FLAG)
539       {
540         ssize_t index=ConstrainColormapIndex(image,(ssize_t) (mask-
541           transparentIndex),exception);
542         if (bits_per_pixel != 16)
543           transpix=image->colormap[index];
544         (void) TransparentPaintImage(image,&transpix,(Quantum) TransparentAlpha,
545           MagickFalse,exception);
546       }
547     one_row=(unsigned char *) RelinquishMagickMemory(one_row);
548     if (compressionType == PALM_COMPRESSION_SCANLINE)
549       last_row=(unsigned char *) RelinquishMagickMemory(last_row);
550     if (EOFBlob(image) != MagickFalse)
551       {
552         ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
553           image->filename);
554         break;
555       }
556     /*
557       Proceed to next image. Copied from coders/pnm.c
558     */
559     if (image_info->number_scenes != 0)
560       if (image->scene >= (image_info->scene+image_info->number_scenes-1))
561         break;
562     if (nextDepthOffset != 0)
563       { 
564         /*
565           Skip to next image. 
566         */
567         totalOffset+=(MagickOffsetType) (nextDepthOffset*4);
568         if (totalOffset >= (MagickOffsetType) GetBlobSize(image))
569           ThrowReaderException(CorruptImageError,"ImproperImageHeader")
570         else
571           seekNextDepth=SeekBlob(image,totalOffset,SEEK_SET);
572         if (seekNextDepth != totalOffset)
573           ThrowReaderException(CorruptImageError,"ImproperImageHeader");
574         /*
575           Allocate next image structure. Copied from coders/pnm.c
576         */
577         AcquireNextImage(image_info,image,exception);
578         if (GetNextImageInList(image) == (Image *) NULL)
579           {
580             (void) DestroyImageList(image);
581             return((Image *) NULL);
582           }
583         image=SyncNextImageInList(image);
584         status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
585           GetBlobSize(image));
586         if (status == MagickFalse)
587           break;
588       }
589   } while (nextDepthOffset != 0);
590   (void) CloseBlob(image);
591   return(GetFirstImageInList(image));
592 }
593 \f
594 /*
595 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
596 %                                                                             %
597 %                                                                             %
598 %                                                                             %
599 %   R e g i s t e r P A L M I m a g e                                         %
600 %                                                                             %
601 %                                                                             %
602 %                                                                             %
603 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
604 %
605 %  RegisterPALMImage() adds properties for the PALM image format to the list of
606 %  supported formats.  The properties include the image format tag, a method to
607 %  read and/or write the format, whether the format supports the saving of more
608 %  than one frame to the same file or blob, whether the format supports native
609 %  in-memory I/O, and a brief description of the format.
610 %
611 %  The format of the RegisterPALMImage method is:
612 %
613 %      size_t RegisterPALMImage(void)
614 %
615 */
616 ModuleExport size_t RegisterPALMImage(void)
617 {
618   MagickInfo
619     *entry;
620
621   entry=AcquireMagickInfo("PALM","PALM","Palm pixmap");
622   entry->decoder=(DecodeImageHandler *) ReadPALMImage;
623   entry->encoder=(EncodeImageHandler *) WritePALMImage;
624   entry->flags|=CoderDecoderSeekableStreamFlag;
625   entry->flags|=CoderEncoderSeekableStreamFlag;
626   (void) RegisterMagickInfo(entry);
627   return(MagickImageCoderSignature);
628 }
629 \f
630 /*
631 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
632 %                                                                             %
633 %                                                                             %
634 %                                                                             %
635 %   U n r e g i s t e r P A L M I m a g e                                     %
636 %                                                                             %
637 %                                                                             %
638 %                                                                             %
639 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
640 %
641 %  UnregisterPALMImage() removes format registrations made by the PALM
642 %  module from the list of supported formats.
643 %
644 %  The format of the UnregisterPALMImage method is:
645 %
646 %      UnregisterPALMImage(void)
647 %
648 */
649 ModuleExport void UnregisterPALMImage(void)
650 {
651   (void) UnregisterMagickInfo("PALM");
652 }
653 \f
654 /*
655 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
656 %                                                                             %
657 %                                                                             %
658 %                                                                             %
659 %   W r i t e P A L M I m a g e                                               %
660 %                                                                             %
661 %                                                                             %
662 %                                                                             %
663 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
664 %
665 %  WritePALMImage() writes an image of raw bits in LSB order to a file.
666 %
667 %  The format of the WritePALMImage method is:
668 %
669 %      MagickBooleanType WritePALMImage(const ImageInfo *image_info,
670 %        Image *image,ExceptionInfo *exception)
671 %
672 %  A description of each parameter follows.
673 %
674 %    o image_info: Specifies a pointer to an ImageInfo structure.
675 %
676 %    o image:  A pointer to a Image structure.
677 %
678 %    o exception: return any errors or warnings in this structure.
679 %
680 */
681 static MagickBooleanType WritePALMImage(const ImageInfo *image_info,
682   Image *image,ExceptionInfo *exception)
683 {
684   MagickBooleanType
685     status;
686
687   MagickOffsetType
688     currentOffset,
689     offset,
690     scene;
691
692   MagickSizeType
693     cc;
694
695   PixelInfo
696     transpix;
697
698   QuantizeInfo
699     *quantize_info;
700
701   register ssize_t
702     x;
703
704   register const Quantum
705     *p;
706
707   register Quantum
708     *q;
709
710   ssize_t
711     y;
712
713   size_t
714     count,
715     bits_per_pixel,
716     bytes_per_row,
717     nextDepthOffset,
718     one;
719
720   unsigned char
721     bit,
722     byte,
723     color,
724     *last_row,
725     *one_row,
726     *ptr,
727     version;
728
729   unsigned int
730     transparentIndex;
731
732   unsigned short
733     color16,
734     flags;
735
736   /*
737     Open output image file.
738   */
739   assert(image_info != (const ImageInfo *) NULL);
740   assert(image_info->signature == MagickCoreSignature);
741   assert(image != (Image *) NULL);
742   assert(image->signature == MagickCoreSignature);
743   if (image->debug != MagickFalse)
744     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
745   assert(exception != (ExceptionInfo *) NULL);
746   assert(exception->signature == MagickCoreSignature);
747   status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
748   if (status == MagickFalse)
749     return(status);
750   quantize_info=AcquireQuantizeInfo(image_info);
751   flags=0;
752   currentOffset=0;
753   transparentIndex=0;
754   transpix.red=0.0;
755   transpix.green=0.0;
756   transpix.blue=0.0;
757   transpix.alpha=0.0;
758   one=1;
759   version=0;
760   scene=0;
761   do
762   {
763     (void) TransformImageColorspace(image,sRGBColorspace,exception);
764     count=GetNumberColors(image,NULL,exception);
765     for (bits_per_pixel=1;  (one << bits_per_pixel) < count; bits_per_pixel*=2) ;
766     if (bits_per_pixel > 16)
767       bits_per_pixel=16;
768     else
769       if (bits_per_pixel < 16)
770         (void) TransformImageColorspace(image,image->colorspace,exception);
771     if (bits_per_pixel < 8)
772       {
773         (void) TransformImageColorspace(image,GRAYColorspace,exception);
774         (void) SetImageType(image,PaletteType,exception);
775         (void) SortColormapByIntensity(image,exception);
776       }
777     if ((image->storage_class == PseudoClass) && (image->colors > 256))
778       (void) SetImageStorageClass(image,DirectClass,exception);
779     if (image->storage_class == PseudoClass)
780       flags|=PALM_HAS_COLORMAP_FLAG;
781     else
782       flags|=PALM_IS_DIRECT_COLOR;
783     (void) WriteBlobMSBShort(image,(unsigned short) image->columns); /* width */
784     (void) WriteBlobMSBShort(image,(unsigned short) image->rows);  /* height */
785     bytes_per_row=((image->columns+(16/bits_per_pixel-1))/(16/
786       bits_per_pixel))*2;
787     (void) WriteBlobMSBShort(image,(unsigned short) bytes_per_row);
788     if ((image_info->compression == RLECompression) ||
789         (image_info->compression == FaxCompression))
790       flags|=PALM_IS_COMPRESSED_FLAG;
791     (void) WriteBlobMSBShort(image, flags);
792     (void) WriteBlobByte(image,(unsigned char) bits_per_pixel);
793     if (bits_per_pixel > 1)
794       version=1;
795     if ((image_info->compression == RLECompression) ||
796         (image_info->compression == FaxCompression))
797       version=2;
798     (void) WriteBlobByte(image,version);
799     (void) WriteBlobMSBShort(image,0);  /* nextDepthOffset */
800     (void) WriteBlobByte(image,(unsigned char) transparentIndex);
801     if (image_info->compression == RLECompression)
802       (void) WriteBlobByte(image,PALM_COMPRESSION_RLE);
803     else
804       if (image_info->compression == FaxCompression)
805         (void) WriteBlobByte(image,PALM_COMPRESSION_SCANLINE);
806       else
807         (void) WriteBlobByte(image,PALM_COMPRESSION_NONE);
808     (void) WriteBlobMSBShort(image,0);  /* reserved */
809     offset=16;
810     if (bits_per_pixel == 16)
811       {
812         (void) WriteBlobByte(image,5);  /* # of bits of red */
813         (void) WriteBlobByte(image,6);  /* # of bits of green */
814         (void) WriteBlobByte(image,5);  /* # of bits of blue */
815         (void) WriteBlobByte(image,0);  /* reserved by Palm */
816         (void) WriteBlobMSBLong(image,0);  /* no transparent color, YET */
817         offset+=8;
818       }
819     if (bits_per_pixel == 8)
820       {
821         if (flags & PALM_HAS_COLORMAP_FLAG)  /* Write out colormap */
822           {
823             quantize_info->dither_method=IdentifyPaletteImage(image,exception)
824               == MagickFalse ? RiemersmaDitherMethod : NoDitherMethod;
825             quantize_info->number_colors=image->colors;
826             (void) QuantizeImage(quantize_info,image,exception);
827             (void) WriteBlobMSBShort(image,(unsigned short) image->colors);
828             for (count = 0; count < image->colors; count++)
829             {
830               (void) WriteBlobByte(image,(unsigned char) count);
831               (void) WriteBlobByte(image,ScaleQuantumToChar(ClampToQuantum(
832                 image->colormap[count].red)));
833               (void) WriteBlobByte(image,ScaleQuantumToChar(ClampToQuantum(
834                 image->colormap[count].green)));
835               (void) WriteBlobByte(image,ScaleQuantumToChar(ClampToQuantum(
836                 image->colormap[count].blue)));
837             }
838             offset+=2+count*4;
839           }
840       else  /* Map colors to Palm standard colormap */
841         {
842           Image
843             *affinity_image;
844
845           affinity_image=ConstituteImage(256,1,"RGB",CharPixel,&PalmPalette,
846             exception);
847           (void) TransformImageColorspace(affinity_image,
848             affinity_image->colorspace,exception);
849           (void) RemapImage(quantize_info,image,affinity_image,exception);
850           for (y=0; y < (ssize_t) image->rows; y++)
851           {
852             q=GetAuthenticPixels(image,0,y,image->columns,1,exception);
853             if (q == (Quantum *) NULL)
854               break;
855             for (x=0; x < (ssize_t) image->columns; x++)
856             {
857               SetPixelIndex(image,(Quantum) FindColor(&image->colormap[(ssize_t)
858                 GetPixelIndex(image,q)]),q);
859               q+=GetPixelChannels(image);
860             }
861           }
862           affinity_image=DestroyImage(affinity_image);
863         }
864       }
865     if (flags & PALM_IS_COMPRESSED_FLAG)
866       (void) WriteBlobMSBShort(image,0);  /* fill in size later */
867     last_row=(unsigned char *) NULL;
868     if (image_info->compression == FaxCompression)
869       {
870         last_row=(unsigned char *) AcquireQuantumMemory(bytes_per_row,
871           sizeof(*last_row));
872         if (last_row == (unsigned char *) NULL)
873           {
874             quantize_info=DestroyQuantizeInfo(quantize_info);
875             ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
876           }
877       }
878     one_row=(unsigned char *) AcquireQuantumMemory(bytes_per_row,
879       sizeof(*one_row));
880     if (one_row == (unsigned char *) NULL)
881       {
882         if (last_row != (unsigned char *) NULL) 
883           last_row=(unsigned char *) RelinquishMagickMemory(last_row);
884         quantize_info=DestroyQuantizeInfo(quantize_info);
885         ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
886       }
887     for (y=0; y < (ssize_t) image->rows; y++)
888     {
889       ptr=one_row;
890       (void) ResetMagickMemory(ptr,0,bytes_per_row);
891       p=GetVirtualPixels(image,0,y,image->columns,1,exception);
892       if (p == (const Quantum *) NULL)
893         break;
894       if (bits_per_pixel == 16)
895         {
896           for (x=0; x < (ssize_t) image->columns; x++)
897           {
898             color16=(unsigned short) ((((31*(size_t) GetPixelRed(image,p))/
899               (size_t) QuantumRange) << 11) | (((63*(size_t)
900               GetPixelGreen(image,p))/(size_t) QuantumRange) << 5) |
901               ((31*(size_t) GetPixelBlue(image,p))/(size_t) QuantumRange));
902             if (GetPixelAlpha(image,p) == (Quantum) TransparentAlpha)
903               {
904                 transpix.red=(MagickRealType) GetPixelRed(image,p);
905                 transpix.green=(MagickRealType) GetPixelGreen(image,p);
906                 transpix.blue=(MagickRealType) GetPixelBlue(image,p);
907                 transpix.alpha=(MagickRealType) GetPixelAlpha(image,p);
908                 flags|=PALM_HAS_TRANSPARENCY_FLAG;
909               }
910             *ptr++=(unsigned char) ((color16 >> 8) & 0xff);
911             *ptr++=(unsigned char) (color16 & 0xff);
912             p+=GetPixelChannels(image);
913           }
914         }
915       else
916         {
917           byte=0x00;
918           bit=(unsigned char) (8-bits_per_pixel);
919           for (x=0; x < (ssize_t) image->columns; x++)
920           {
921             if (bits_per_pixel >= 8)
922               color=(unsigned char) GetPixelIndex(image,p);
923             else
924               color=(unsigned char) (GetPixelIndex(image,p)*
925                 ((one << bits_per_pixel)-1)/MagickMax(1*image->colors-1,1));
926             byte|=color << bit;
927             if (bit != 0)
928               bit-=(unsigned char) bits_per_pixel;
929             else
930               {
931                 *ptr++=byte;
932                 byte=0x00;
933                 bit=(unsigned char) (8-bits_per_pixel);
934               }
935             p+=GetPixelChannels(image);
936           }
937           if ((image->columns % (8/bits_per_pixel)) != 0)
938             *ptr++=byte;
939         }
940       if (image_info->compression == RLECompression)
941         {
942           x=0;
943           while (x < (ssize_t) bytes_per_row)
944           {
945             byte=one_row[x];
946             count=1;
947             while ((one_row[++x] == byte) && (count < 255) &&
948                    (x < (ssize_t) bytes_per_row))
949               count++;
950             (void) WriteBlobByte(image,(unsigned char) count);
951             (void) WriteBlobByte(image,(unsigned char) byte);
952           }
953         }
954       else
955         if (image_info->compression == FaxCompression)
956           {
957             char
958               tmpbuf[8],
959               *tptr;
960   
961             for (x = 0;  x < (ssize_t) bytes_per_row;  x += 8)
962             {
963               tptr = tmpbuf;
964               for (bit=0, byte=0; bit < (unsigned char) MagickMin(8,(ssize_t) bytes_per_row-x); bit++)
965               {
966                 if ((y == 0) || (last_row[x + bit] != one_row[x + bit]))
967                   {
968                     byte |= (1 << (7 - bit));
969                     *tptr++ = (char) one_row[x + bit];
970                   }
971               }
972               (void) WriteBlobByte(image, byte);
973               (void) WriteBlob(image,tptr-tmpbuf,(unsigned char *) tmpbuf);
974             }
975             (void) CopyMagickMemory(last_row,one_row,bytes_per_row);
976           }
977         else
978           (void) WriteBlob(image,bytes_per_row,one_row);
979       }
980     if (flags & PALM_HAS_TRANSPARENCY_FLAG)
981       {
982         offset=SeekBlob(image,currentOffset+6,SEEK_SET);
983         (void) WriteBlobMSBShort(image,flags);
984         offset=SeekBlob(image,currentOffset+12,SEEK_SET);
985         (void) WriteBlobByte(image,(unsigned char) transparentIndex);  /* trans index */
986       }
987     if (bits_per_pixel == 16)
988       {
989         offset=SeekBlob(image,currentOffset+20,SEEK_SET);
990         (void) WriteBlobByte(image,0);  /* reserved by Palm */
991         (void) WriteBlobByte(image,(unsigned char) ((31*transpix.red)/
992           QuantumRange));
993         (void) WriteBlobByte(image,(unsigned char) ((63*transpix.green)/
994           QuantumRange));
995         (void) WriteBlobByte(image,(unsigned char) ((31*transpix.blue)/
996           QuantumRange));
997       }
998     if (flags & PALM_IS_COMPRESSED_FLAG)  /* fill in size now */
999       {
1000         offset=SeekBlob(image,currentOffset+offset,SEEK_SET);
1001         (void) WriteBlobMSBShort(image,(unsigned short) (GetBlobSize(image)-
1002           currentOffset-offset));
1003       }
1004     if (one_row != (unsigned char *) NULL) 
1005       one_row=(unsigned char *) RelinquishMagickMemory(one_row);
1006     if (last_row != (unsigned char *) NULL) 
1007       last_row=(unsigned char *) RelinquishMagickMemory(last_row);
1008     if (GetNextImageInList(image) == (Image *) NULL)
1009       break;
1010     /* padding to 4 byte word */
1011     for (cc=(GetBlobSize(image)) % 4; cc > 0; cc--)
1012       (void) WriteBlobByte(image,0);
1013     /* write nextDepthOffset and return to end of image */
1014     offset=SeekBlob(image,currentOffset+10,SEEK_SET);
1015     nextDepthOffset=(size_t) ((GetBlobSize(image)-currentOffset)/4);
1016     (void) WriteBlobMSBShort(image,(unsigned short) nextDepthOffset);
1017     currentOffset=(MagickOffsetType) GetBlobSize(image);
1018     offset=SeekBlob(image,currentOffset,SEEK_SET);
1019     image=SyncNextImageInList(image);
1020     status=SetImageProgress(image,SaveImagesTag,scene++,
1021       GetImageListLength(image));
1022     if (status == MagickFalse)
1023       break;
1024   } while (image_info->adjoin != MagickFalse);
1025   quantize_info=DestroyQuantizeInfo(quantize_info);
1026   (void) CloseBlob(image);
1027   return(MagickTrue);
1028 }