]> granicus.if.org Git - imagemagick/blob - coders/palm.c
(no commit message)
[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 %    http://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 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
233 static inline size_t MagickMax(const size_t x,const size_t y)
234 {
235   if (x > y)
236     return(x);
237   return(y);
238 }
239
240 static inline ssize_t MagickMin(const ssize_t x,const ssize_t y)
241 {
242   if (x < y)
243     return(x);
244   return(y);
245 }
246
247 static Image *ReadPALMImage(const ImageInfo *image_info,
248   ExceptionInfo *exception)
249 {
250   Image
251     *image;
252
253   MagickBooleanType
254     status;
255
256   MagickOffsetType
257     totalOffset,
258     seekNextDepth;
259
260   PixelInfo
261     transpix;
262
263   Quantum
264     index;
265
266   register ssize_t
267     i,
268     x;
269
270   register Quantum
271     *q;
272
273   ssize_t
274     count,
275     y;
276
277   size_t
278     bytes_per_row,
279     flags,
280     bits_per_pixel,
281     version,
282     nextDepthOffset,
283     transparentIndex,
284     compressionType,
285     byte,
286     mask,
287     redbits,
288     greenbits,
289     bluebits,
290     one,
291     pad,
292     size,
293     bit;
294
295   unsigned char
296     *lastrow,
297     *one_row,
298     *ptr;
299
300   unsigned short
301     color16;
302
303   /*
304     Open image file.
305   */
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,exception);
314   status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
315   if (status == MagickFalse)
316     {
317       (void) DestroyImageList(image);
318       return((Image *) NULL);
319     }
320   totalOffset=0;
321   do
322   {
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     status=SetImageExtent(image,image->columns,image->rows,exception);
330     if (status == MagickFalse)
331       return(DestroyImageList(image));
332     bytes_per_row=ReadBlobMSBShort(image);
333     flags=ReadBlobMSBShort(image);
334     bits_per_pixel=(size_t) ReadBlobByte(image);
335     if ((bits_per_pixel == 0) || (bits_per_pixel > 16))
336       ThrowReaderException(CorruptImageError,"ImproperImageHeader");
337     version=(size_t) ReadBlobByte(image);
338     (void) version;
339     nextDepthOffset=(size_t) ReadBlobMSBShort(image);
340     transparentIndex=(size_t) ReadBlobByte(image);
341     compressionType=(size_t) ReadBlobByte(image);
342     pad=ReadBlobMSBShort(image);
343     (void) pad;
344     /*
345       Initialize image colormap.
346     */
347     one=1;
348     if ((bits_per_pixel < 16) &&
349         (AcquireImageColormap(image,one << bits_per_pixel,exception) == MagickFalse))
350       ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
351     GetPixelInfo(image,&transpix);
352     if (bits_per_pixel == 16)  /* Direct Color */
353       {
354         redbits=(size_t) ReadBlobByte(image);  /* # of bits of red */
355         (void) redbits;
356         greenbits=(size_t) ReadBlobByte(image);  /* # of bits of green */
357         (void) greenbits;
358         bluebits=(size_t) ReadBlobByte(image);  /* # of bits of blue */
359         (void) bluebits;
360         ReadBlobByte(image);  /* reserved by Palm */
361         ReadBlobByte(image);  /* reserved by Palm */
362         transpix.red=(double) (QuantumRange*ReadBlobByte(image)/31);
363         transpix.green=(double) (QuantumRange*ReadBlobByte(image)/63);
364         transpix.blue=(double) (QuantumRange*ReadBlobByte(image)/31);
365       }
366     if (bits_per_pixel == 8)
367       {
368         ssize_t
369           index;
370
371         if (flags & PALM_HAS_COLORMAP_FLAG)
372           {
373             count=(ssize_t) ReadBlobMSBShort(image);
374             for (i=0; i < (ssize_t) count; i++)
375             {
376               ReadBlobByte(image);
377               index=ConstrainColormapIndex(image,255-i,exception);
378               image->colormap[index].red=(MagickRealType)
379                 ScaleCharToQuantum((unsigned char) ReadBlobByte(image));
380               image->colormap[index].green=(MagickRealType)
381                 ScaleCharToQuantum((unsigned char) ReadBlobByte(image));
382               image->colormap[index].blue=(MagickRealType)
383                 ScaleCharToQuantum((unsigned char) ReadBlobByte(image));
384           }
385         }
386       else
387         {
388           for (i=0; i < (ssize_t) (1L << bits_per_pixel); i++)
389           {
390             index=ConstrainColormapIndex(image,255-i,exception);
391             image->colormap[index].red=(MagickRealType)
392               ScaleCharToQuantum(PalmPalette[i][0]);
393             image->colormap[index].green=(MagickRealType)
394               ScaleCharToQuantum(PalmPalette[i][1]);
395             image->colormap[index].blue=(MagickRealType)
396               ScaleCharToQuantum(PalmPalette[i][2]);
397           }
398         }
399       }
400     if (flags & PALM_IS_COMPRESSED_FLAG)
401       size=ReadBlobMSBShort(image);
402     (void) size;
403     image->storage_class=DirectClass;
404     if (bits_per_pixel < 16)
405       {
406         image->storage_class=PseudoClass;
407         image->depth=8;
408       }
409     one_row=(unsigned char *) AcquireQuantumMemory(MagickMax(bytes_per_row,
410       2*image->columns),sizeof(*one_row));
411     if (one_row == (unsigned char *) NULL)
412       ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
413     lastrow=(unsigned char *) NULL;
414     if (compressionType == PALM_COMPRESSION_SCANLINE) {
415       lastrow=(unsigned char *) AcquireQuantumMemory(MagickMax(bytes_per_row,
416         2*image->columns),sizeof(*lastrow));
417     if (lastrow == (unsigned char *) NULL)
418       ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
419     }
420     mask=(size_t) (1U << bits_per_pixel)-1;
421     for (y = 0; y < (ssize_t) image->rows; y++)
422     {
423       if ((flags & PALM_IS_COMPRESSED_FLAG) == 0)
424         {
425           /* TODO move out of loop! */
426           image->compression=NoCompression;
427           count=ReadBlob(image,bytes_per_row,one_row);
428         }
429       else
430         {
431           if (compressionType == PALM_COMPRESSION_RLE)
432             { 
433               /* TODO move out of loop! */
434               image->compression=RLECompression;
435               for (i=0; i < (ssize_t) bytes_per_row; )
436               {
437                 count=(ssize_t) ReadBlobByte(image);
438                 count=MagickMin(count,(ssize_t) bytes_per_row-i);
439                 byte=(size_t) ReadBlobByte(image);
440                 (void) ResetMagickMemory(one_row+i,(int) byte,(size_t) count);
441                 i+=count;
442               }
443           }
444         else
445           if (compressionType == PALM_COMPRESSION_SCANLINE)
446             {
447               size_t
448                 one;
449
450               /* TODO move out of loop! */
451               one=1;
452               image->compression=FaxCompression;
453               for (i=0; i < (ssize_t) bytes_per_row; i+=8)
454               {
455                 count=(ssize_t) ReadBlobByte(image);
456                 byte=(size_t) MagickMin((ssize_t) bytes_per_row-i,8);
457                 for (bit=0; bit < byte; bit++)
458                 {
459                   if ((y == 0) || (count & (one << (7 - bit))))
460                     one_row[i+bit]=(unsigned char) ReadBlobByte(image);
461                   else
462                     one_row[i+bit]=lastrow[i+bit];
463                 }
464               }
465               (void) CopyMagickMemory(lastrow, one_row, bytes_per_row);
466             }
467         }
468       ptr=one_row;
469       q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
470       if (q == (Quantum *) NULL)
471         break;
472       if (bits_per_pixel == 16)
473         {
474           if (image->columns > (2*bytes_per_row))
475             ThrowReaderException(CorruptImageError,"CorruptImage");
476           for (x=0; x < (ssize_t) image->columns; x++)
477           {
478             color16=(*ptr++ << 8);
479             color16|=(*ptr++);
480             SetPixelRed(image,(Quantum) ((QuantumRange*((color16 >> 11) &
481               0x1f))/0x1f),q);
482             SetPixelGreen(image,(Quantum) ((QuantumRange*((color16 >> 5) &
483               0x3f))/0x3f),q);
484             SetPixelBlue(image,(Quantum) ((QuantumRange*((color16 >> 0) &
485               0x1f))/0x1f),q);
486             SetPixelAlpha(image,OpaqueAlpha,q);
487             q+=GetPixelChannels(image);
488           }
489         }
490       else
491         {
492           bit=8-bits_per_pixel;
493           for (x=0; x < (ssize_t) image->columns; x++)
494           {
495             if ((size_t) (ptr-one_row) >= bytes_per_row)
496               ThrowReaderException(CorruptImageError,"CorruptImage");
497             index=(Quantum) (mask-(((*ptr) & (mask << bit)) >> bit));
498             SetPixelIndex(image,index,q);
499             SetPixelInfoPixel(image,image->colormap+(ssize_t) index,q);
500             if (bit)
501               bit-=bits_per_pixel;
502             else
503               {
504                 ptr++;
505                 bit=8-bits_per_pixel;
506               }
507             q+=GetPixelChannels(image);
508           }
509           if (SyncAuthenticPixels(image,exception) == MagickFalse)
510             break;
511         }
512         if (image->previous == (Image *) NULL)
513           {
514             status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
515               image->rows);
516             if (status == MagickFalse)
517               break;
518           }
519       }
520     if (flags & PALM_HAS_TRANSPARENCY_FLAG)
521       {
522         ssize_t index=ConstrainColormapIndex(image,(mask-transparentIndex),
523           exception);
524         if (bits_per_pixel != 16)
525           transpix=image->colormap[index];
526         (void) TransparentPaintImage(image,&transpix,(Quantum) TransparentAlpha,
527           MagickFalse,exception);
528       }
529     one_row=(unsigned char *) RelinquishMagickMemory(one_row);
530     if (compressionType == PALM_COMPRESSION_SCANLINE)
531       lastrow=(unsigned char *) RelinquishMagickMemory(lastrow);
532     /*
533       Proceed to next image. Copied from coders/pnm.c
534     */
535     if (image_info->number_scenes != 0)
536       if (image->scene >= (image_info->scene+image_info->number_scenes-1))
537         break;
538     if (nextDepthOffset != 0)
539       { 
540         /*
541           Skip to next image. 
542         */
543         totalOffset+=(MagickOffsetType) (nextDepthOffset*4);
544         if (totalOffset >= (MagickOffsetType) GetBlobSize(image))
545           {
546             ThrowReaderException(CorruptImageError,"ImproperImageHeader");
547           }
548         else
549           {
550             seekNextDepth=SeekBlob(image,totalOffset,SEEK_SET);
551           }
552         if (seekNextDepth != totalOffset)
553           ThrowReaderException(CorruptImageError,"ImproperImageHeader");
554         /*
555           Allocate next image structure. Copied from coders/pnm.c
556         */
557         AcquireNextImage(image_info,image,exception);
558         if (GetNextImageInList(image) == (Image *) NULL)
559           {
560             (void) DestroyImageList(image);
561             return((Image *) NULL);
562           }
563         image=SyncNextImageInList(image);
564         status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
565           GetBlobSize(image));
566         if (status == MagickFalse)
567           break;
568       }
569   } while (nextDepthOffset != 0);
570   (void) CloseBlob(image);
571   return(GetFirstImageInList(image));
572 }
573 \f
574 /*
575 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
576 %                                                                             %
577 %                                                                             %
578 %                                                                             %
579 %   R e g i s t e r P A L M I m a g e                                         %
580 %                                                                             %
581 %                                                                             %
582 %                                                                             %
583 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
584 %
585 %  RegisterPALMImage() adds properties for the PALM image format to the list of
586 %  supported formats.  The properties include the image format tag, a method to
587 %  read and/or write the format, whether the format supports the saving of more
588 %  than one frame to the same file or blob, whether the format supports native
589 %  in-memory I/O, and a brief description of the format.
590 %
591 %  The format of the RegisterPALMImage method is:
592 %
593 %      size_t RegisterPALMImage(void)
594 %
595 */
596 ModuleExport size_t RegisterPALMImage(void)
597 {
598   MagickInfo
599     *entry;
600
601   entry=SetMagickInfo("PALM");
602   entry->decoder=(DecodeImageHandler *) ReadPALMImage;
603   entry->encoder=(EncodeImageHandler *) WritePALMImage;
604   entry->seekable_stream=MagickTrue;
605   entry->description=ConstantString("Palm pixmap");
606   entry->module=ConstantString("PALM");
607   (void) RegisterMagickInfo(entry);
608   return(MagickImageCoderSignature);
609 }
610 \f
611 /*
612 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
613 %                                                                             %
614 %                                                                             %
615 %                                                                             %
616 %   U n r e g i s t e r P A L M I m a g e                                     %
617 %                                                                             %
618 %                                                                             %
619 %                                                                             %
620 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
621 %
622 %  UnregisterPALMImage() removes format registrations made by the PALM
623 %  module from the list of supported formats.
624 %
625 %  The format of the UnregisterPALMImage method is:
626 %
627 %      UnregisterPALMImage(void)
628 %
629 */
630 ModuleExport void UnregisterPALMImage(void)
631 {
632   (void) UnregisterMagickInfo("PALM");
633 }
634 \f
635 /*
636 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
637 %                                                                             %
638 %                                                                             %
639 %                                                                             %
640 %   W r i t e P A L M I m a g e                                               %
641 %                                                                             %
642 %                                                                             %
643 %                                                                             %
644 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
645 %
646 %  WritePALMImage() writes an image of raw bits in LSB order to a file.
647 %
648 %  The format of the WritePALMImage method is:
649 %
650 %      MagickBooleanType WritePALMImage(const ImageInfo *image_info,
651 %        Image *image,ExceptionInfo *exception)
652 %
653 %  A description of each parameter follows.
654 %
655 %    o image_info: Specifies a pointer to an ImageInfo structure.
656 %
657 %    o image:  A pointer to a Image structure.
658 %
659 %    o exception: return any errors or warnings in this structure.
660 %
661 */
662 static MagickBooleanType WritePALMImage(const ImageInfo *image_info,
663   Image *image,ExceptionInfo *exception)
664 {
665   MagickBooleanType
666     status;
667
668   MagickOffsetType
669     currentOffset,
670     offset,
671     scene;
672
673   MagickSizeType
674     cc;
675
676   PixelInfo
677     transpix;
678
679   QuantizeInfo
680     *quantize_info;
681
682   register ssize_t
683     x;
684
685   register const Quantum
686     *p;
687
688   register Quantum
689     *q;
690
691   ssize_t
692     y;
693
694   size_t
695     count,
696     bits_per_pixel,
697     bytes_per_row,
698     nextDepthOffset,
699     one;
700
701   unsigned char
702     bit,
703     byte,
704     color,
705     *lastrow,
706     *one_row,
707     *ptr,
708     version;
709
710   unsigned int
711     transparentIndex;
712
713   unsigned short
714     color16,
715     flags;
716
717   /*
718     Open output image file.
719   */
720   assert(image_info != (const ImageInfo *) NULL);
721   assert(image_info->signature == MagickSignature);
722   assert(image != (Image *) NULL);
723   assert(image->signature == MagickSignature);
724   if (image->debug != MagickFalse)
725     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
726   assert(exception != (ExceptionInfo *) NULL);
727   assert(exception->signature == MagickSignature);
728   status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
729   if (status == MagickFalse)
730     return(status);
731   quantize_info=AcquireQuantizeInfo(image_info);
732   flags=0;
733   currentOffset=0;
734   transparentIndex=0;
735   transpix.red=0.0;
736   transpix.green=0.0;
737   transpix.blue=0.0;
738   transpix.alpha=0.0;
739   one=1;
740   version=0;
741   scene=0;
742   do
743   {
744     (void) TransformImageColorspace(image,sRGBColorspace,exception);
745     count=GetNumberColors(image,NULL,exception);
746     for (bits_per_pixel=1;  (one << bits_per_pixel) < count; bits_per_pixel*=2) ;
747     if (image_info->depth > 100)
748       bits_per_pixel=image_info->depth-100;
749     if (bits_per_pixel < 16)
750       (void) TransformImageColorspace(image,image->colorspace,exception);
751     if (bits_per_pixel < 8)
752       {
753         (void) TransformImageColorspace(image,GRAYColorspace,exception);
754         (void) SetImageType(image,PaletteType,exception);
755         (void) SortColormapByIntensity(image,exception);
756       }
757     if ((image->storage_class == PseudoClass) && (image->colors > 256))
758       (void) SetImageStorageClass(image,DirectClass,exception);
759     if (image->storage_class == PseudoClass)
760       flags|=PALM_HAS_COLORMAP_FLAG;
761     else
762       flags|=PALM_IS_DIRECT_COLOR;
763     (void) WriteBlobMSBShort(image,(unsigned short) image->columns); /* width */
764     (void) WriteBlobMSBShort(image,(unsigned short) image->rows);  /* height */
765     bytes_per_row=((image->columns+(16/bits_per_pixel-1))/(16/
766       bits_per_pixel))*2;
767     (void) WriteBlobMSBShort(image,(unsigned short) bytes_per_row);
768     if ((image_info->compression == RLECompression) ||
769         (image_info->compression == FaxCompression))
770       flags|=PALM_IS_COMPRESSED_FLAG;
771     (void) WriteBlobMSBShort(image, flags);
772     (void) WriteBlobByte(image,(unsigned char) bits_per_pixel);
773     if (bits_per_pixel > 1)
774       version=1;
775     if ((image_info->compression == RLECompression) ||
776         (image_info->compression == FaxCompression))
777       version=2;
778     (void) WriteBlobByte(image,version);
779     (void) WriteBlobMSBShort(image,0);  /* nextDepthOffset */
780     (void) WriteBlobByte(image,(unsigned char) transparentIndex);
781     if (image_info->compression == RLECompression)
782       (void) WriteBlobByte(image,PALM_COMPRESSION_RLE);
783     else
784       if (image_info->compression == FaxCompression)
785         (void) WriteBlobByte(image,PALM_COMPRESSION_SCANLINE);
786       else
787         (void) WriteBlobByte(image,PALM_COMPRESSION_NONE);
788     (void) WriteBlobMSBShort(image,0);  /* reserved */
789     offset=16;
790     if (bits_per_pixel == 16)
791       {
792         (void) WriteBlobByte(image,5);  /* # of bits of red */
793         (void) WriteBlobByte(image,6);  /* # of bits of green */
794         (void) WriteBlobByte(image,5);  /* # of bits of blue */
795         (void) WriteBlobByte(image,0);  /* reserved by Palm */
796         (void) WriteBlobMSBLong(image,0);  /* no transparent color, YET */
797         offset+=8;
798       }
799     if (bits_per_pixel == 8)
800       {
801         if (flags & PALM_HAS_COLORMAP_FLAG)  /* Write out colormap */
802           {
803             quantize_info->dither_method=IsPaletteImage(image,exception)
804               == MagickFalse ? RiemersmaDitherMethod : NoDitherMethod;
805             quantize_info->number_colors=image->colors;
806             (void) QuantizeImage(quantize_info,image,exception);
807             (void) WriteBlobMSBShort(image,(unsigned short) image->colors);
808             for (count = 0; count < image->colors; count++)
809             {
810               (void) WriteBlobByte(image,(unsigned char) count);
811               (void) WriteBlobByte(image,ScaleQuantumToChar(ClampToQuantum(
812                 image->colormap[count].red)));
813               (void) WriteBlobByte(image,ScaleQuantumToChar(ClampToQuantum(
814                 image->colormap[count].green)));
815               (void) WriteBlobByte(image,ScaleQuantumToChar(ClampToQuantum(
816                 image->colormap[count].blue)));
817             }
818             offset+=2+count*4;
819           }
820       else  /* Map colors to Palm standard colormap */
821         {
822           Image
823             *affinity_image;
824
825           affinity_image=ConstituteImage(256,1,"RGB",CharPixel,&PalmPalette,
826             exception);
827           (void) TransformImageColorspace(affinity_image,
828             affinity_image->colorspace,exception);
829           (void) RemapImage(quantize_info,image,affinity_image,exception);
830           for (y=0; y < (ssize_t) image->rows; y++)
831           {
832             q=GetAuthenticPixels(image,0,y,image->columns,1,exception);
833             for (x=0; x < (ssize_t) image->columns; x++)
834             {
835               SetPixelIndex(image,(Quantum) FindColor(&image->colormap[(ssize_t)
836                 GetPixelIndex(image,q)]),q);
837               q+=GetPixelChannels(image);
838             }
839           }
840           affinity_image=DestroyImage(affinity_image);
841         }
842       }
843     if (flags & PALM_IS_COMPRESSED_FLAG)
844       (void) WriteBlobMSBShort(image,0);  /* fill in size later */
845     lastrow=(unsigned char *) NULL;
846     if (image_info->compression == FaxCompression)
847       lastrow=(unsigned char *) AcquireQuantumMemory(bytes_per_row,
848         sizeof(*lastrow));
849       /* TODO check whether memory really was acquired? */
850     one_row=(unsigned char *) AcquireQuantumMemory(bytes_per_row,
851       sizeof(*one_row));
852     if (one_row == (unsigned char *) NULL)
853       ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
854     for (y=0; y < (ssize_t) image->rows; y++)
855     {
856       ptr=one_row;
857       (void) ResetMagickMemory(ptr,0,bytes_per_row);
858       p=GetVirtualPixels(image,0,y,image->columns,1,exception);
859       if (p == (const Quantum *) NULL)
860         break;
861       if (bits_per_pixel == 16)
862         {
863           for (x=0; x < (ssize_t) image->columns; x++)
864           {
865             color16=(unsigned short) ((((31*(size_t) GetPixelRed(image,p))/
866               (size_t) QuantumRange) << 11) | (((63*(size_t)
867               GetPixelGreen(image,p))/(size_t) QuantumRange) << 5) |
868               ((31*(size_t) GetPixelBlue(image,p))/(size_t) QuantumRange));
869             if (GetPixelAlpha(image,p) == (Quantum) TransparentAlpha)
870               {
871                 transpix.red=(MagickRealType) GetPixelRed(image,p);
872                 transpix.green=(MagickRealType) GetPixelGreen(image,p);
873                 transpix.blue=(MagickRealType) GetPixelBlue(image,p);
874                 transpix.alpha=(MagickRealType) GetPixelAlpha(image,p);
875                 flags|=PALM_HAS_TRANSPARENCY_FLAG;
876               }
877             *ptr++=(unsigned char) ((color16 >> 8) & 0xff);
878             *ptr++=(unsigned char) (color16 & 0xff);
879             p+=GetPixelChannels(image);
880           }
881         }
882       else
883         {
884           byte=0x00;
885           bit=(unsigned char) (8-bits_per_pixel);
886           for (x=0; x < (ssize_t) image->columns; x++)
887           {
888             if (bits_per_pixel >= 8)
889               color=(unsigned char) GetPixelIndex(image,p);
890             else
891               color=(unsigned char) (GetPixelIndex(image,p)*
892                 ((one << bits_per_pixel)-1)/MagickMax(1*image->colors-1,1));
893             byte|=color << bit;
894             if (bit != 0)
895               bit-=(unsigned char) bits_per_pixel;
896             else
897               {
898                 *ptr++=byte;
899                 byte=0x00;
900                 bit=(unsigned char) (8-bits_per_pixel);
901               }
902             p+=GetPixelChannels(image);
903           }
904           if ((image->columns % (8/bits_per_pixel)) != 0)
905             *ptr++=byte;
906         }
907       if (image_info->compression == RLECompression)
908         {
909           x=0;
910           while (x < (ssize_t) bytes_per_row)
911           {
912             byte=one_row[x];
913             count=1;
914             while ((one_row[++x] == byte) && (count < 255) &&
915                    (x < (ssize_t) bytes_per_row))
916               count++;
917             (void) WriteBlobByte(image,(unsigned char) count);
918             (void) WriteBlobByte(image,(unsigned char) byte);
919           }
920         }
921       else
922         if (image_info->compression == FaxCompression)
923           {
924             char
925               tmpbuf[8],
926               *tptr;
927   
928             for (x = 0;  x < (ssize_t) bytes_per_row;  x += 8)
929             {
930               tptr = tmpbuf;
931               for (bit=0, byte=0; bit < (unsigned char) MagickMin(8,(ssize_t) bytes_per_row-x); bit++)
932               {
933                 if ((y == 0) || (lastrow[x + bit] != one_row[x + bit]))
934                   {
935                     byte |= (1 << (7 - bit));
936                     *tptr++ = (char) one_row[x + bit];
937                   }
938               }
939               (void) WriteBlobByte(image, byte);
940               (void) WriteBlob(image,tptr-tmpbuf,(unsigned char *) tmpbuf);
941             }
942             (void) CopyMagickMemory(lastrow,one_row,bytes_per_row);
943           }
944         else
945           (void) WriteBlob(image,bytes_per_row,one_row);
946       }
947     if (flags & PALM_HAS_TRANSPARENCY_FLAG)
948       {
949         offset=SeekBlob(image,currentOffset+6,SEEK_SET);
950         (void) WriteBlobMSBShort(image,flags);
951         offset=SeekBlob(image,currentOffset+12,SEEK_SET);
952         (void) WriteBlobByte(image,(unsigned char) transparentIndex);  /* trans index */
953       }
954     if (bits_per_pixel == 16)
955       {
956         offset=SeekBlob(image,currentOffset+20,SEEK_SET);
957         (void) WriteBlobByte(image,0);  /* reserved by Palm */
958         (void) WriteBlobByte(image,(unsigned char) ((31*transpix.red)/QuantumRange));
959         (void) WriteBlobByte(image,(unsigned char) ((63*transpix.green)/QuantumRange));
960         (void) WriteBlobByte(image,(unsigned char) ((31*transpix.blue)/QuantumRange));
961       }
962     if (flags & PALM_IS_COMPRESSED_FLAG)  /* fill in size now */
963       {
964         offset=SeekBlob(image,currentOffset+offset,SEEK_SET);
965         (void) WriteBlobMSBShort(image,(unsigned short) (GetBlobSize(image)-
966           currentOffset-offset));
967       }
968     if (one_row != (unsigned char *) NULL) 
969       one_row=(unsigned char *) RelinquishMagickMemory(one_row);
970     if (lastrow != (unsigned char *) NULL) 
971       lastrow=(unsigned char *) RelinquishMagickMemory(lastrow);
972     if (GetNextImageInList(image) == (Image *) NULL)
973       break;
974     /* padding to 4 byte word */
975     for (cc = (GetBlobSize(image))%4; cc > 0; cc--)
976     {
977       (void) WriteBlobByte(image,0);
978     } 
979     /* write nextDepthOffset and return to end of image */
980     offset=SeekBlob(image,currentOffset+10,SEEK_SET);
981     nextDepthOffset=(size_t) ((GetBlobSize(image)-currentOffset)/4);
982     (void) WriteBlobMSBShort(image,(unsigned short) nextDepthOffset);
983     currentOffset=(MagickOffsetType) GetBlobSize(image);
984     offset=SeekBlob(image,currentOffset,SEEK_SET);
985     image=SyncNextImageInList(image);
986     status=SetImageProgress(image,SaveImagesTag,scene++,
987       GetImageListLength(image));
988     if (status == MagickFalse)
989       break;
990   } while (image_info->adjoin != MagickFalse);
991   quantize_info=DestroyQuantizeInfo(quantize_info);
992   (void) CloseBlob(image);
993   return(MagickTrue);
994 }