]> 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 "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"
71 \f
72 /*
73   Define declarations.
74 */
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
85 \f
86 /*
87  The 256 color system palette for Palm Computing Devices.
88 */
89 static unsigned char
90   PalmPalette[256][3] =
91   {
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}
156   };
157 \f
158 /*
159   Forward declarations.
160 */
161 static MagickBooleanType
162   WritePALMImage(const ImageInfo *,Image *);
163 \f
164 /*
165 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
166 %                                                                             %
167 %                                                                             %
168 %                                                                             %
169 %   F i n d C o l o r                                                         %
170 %                                                                             %
171 %                                                                             %
172 %                                                                             %
173 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
174 %
175 %  FindColor() returns the index of the matching entry from PalmPalette for a
176 %  given PixelPacket.
177 %
178 %  The format of the FindColor method is:
179 %
180 %      int FindColor(PixelPacket *pixel)
181 %
182 %  A description of each parameter follows:
183 %
184 %    o int: the index of the matching color or -1 if not found/
185 %
186 %    o pixel: a pointer to the PixelPacket to be matched.
187 %
188 */
189 static int FindColor(PixelPacket *pixel)
190 {
191   register ssize_t
192     i;
193
194   for (i=0; i < 256; i++)
195     if (ScaleQuantumToChar(pixel->red) == PalmPalette[i][0] &&
196         ScaleQuantumToChar(pixel->green) == PalmPalette[i][1] &&
197         ScaleQuantumToChar(pixel->blue) == PalmPalette[i][2])
198       return(i);
199   return(-1);
200 }
201 \f
202 /*
203 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
204 %                                                                             %
205 %                                                                             %
206 %                                                                             %
207 %   R e a d P A L M I m a g e                                                 %
208 %                                                                             %
209 %                                                                             %
210 %                                                                             %
211 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
212 %
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.
216 %
217 %  The format of the ReadPALMImage method is:
218 %
219 %      Image *ReadPALMImage(const ImageInfo *image_info,
220 %        ExceptionInfo *exception)
221 %
222 %  A description of each parameter follows:
223 %
224 %    o image_info: Specifies a pointer to an ImageInfo structure.
225 %
226 %    o exception: return any errors or warnings in this structure.
227 %
228 */
229
230 static inline size_t MagickMax(const size_t x,const size_t y)
231 {
232   if (x > y)
233     return(x);
234   return(y);
235 }
236
237 static inline ssize_t MagickMin(const ssize_t x,const ssize_t y)
238 {
239   if (x < y)
240     return(x);
241   return(y);
242 }
243
244 static Image *ReadPALMImage(const ImageInfo *image_info,
245   ExceptionInfo *exception)
246 {
247   Image
248     *image;
249
250   IndexPacket
251     index;
252
253   ssize_t
254     y;
255
256   MagickBooleanType
257     status;
258
259   MagickOffsetType
260     totalOffset,
261     seekNextDepth;
262
263   MagickPixelPacket
264     transpix;
265
266   register IndexPacket
267     *indexes;
268
269   register ssize_t
270     i,
271     x;
272
273   register PixelPacket
274     *q;
275
276   ssize_t
277     count;
278
279   unsigned char
280     *lastrow,
281     *one_row,
282     *ptr;
283
284   size_t
285     bytes_per_row,
286     flags,
287     bits_per_pixel,
288     version,
289     nextDepthOffset,
290     transparentIndex,
291     compressionType,
292     byte,
293     mask,
294     redbits,
295     greenbits,
296     bluebits,
297     one,
298     pad,
299     size,
300     bit;
301
302   unsigned short
303     color16;
304
305   /*
306     Open image file.
307   */
308   assert(image_info != (const ImageInfo *) NULL);
309   assert(image_info->signature == MagickSignature);
310   if (image_info->debug != MagickFalse)
311     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
312       image_info->filename);
313   assert(exception != (ExceptionInfo *) NULL);
314   assert(exception->signature == MagickSignature);
315   image=AcquireImage(image_info);
316   status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
317   if (status == MagickFalse)
318     {
319       (void) DestroyImageList(image);
320       return((Image *) NULL);
321     }
322   totalOffset=0;
323   do
324   {
325     image->columns=ReadBlobMSBShort(image);
326     image->rows=ReadBlobMSBShort(image);
327     if (EOFBlob(image) != MagickFalse)
328       ThrowReaderException(CorruptImageError,"ImproperImageHeader");
329     /*
330       Copied from coders/pnm.c. TODO other checks ...
331     */
332     if ((image->columns == 0) || (image->rows == 0))
333       ThrowReaderException(CorruptImageError,"NegativeOrZeroImageSize");
334     bytes_per_row=ReadBlobMSBShort(image);
335     flags=ReadBlobMSBShort(image);
336     bits_per_pixel=(size_t) ReadBlobByte(image);
337     if (bits_per_pixel > 16)
338       ThrowReaderException(CorruptImageError,"ImproperImageHeader");
339     version=(size_t) ReadBlobByte(image);
340     (void) version;
341     nextDepthOffset=(size_t) ReadBlobMSBShort(image);
342     transparentIndex=(size_t) ReadBlobByte(image);
343     compressionType=(size_t) ReadBlobByte(image);
344     pad=ReadBlobMSBShort(image);
345     (void) pad;
346     /*
347       Initialize image colormap.
348     */
349     one=1;
350     if ((bits_per_pixel < 16) &&
351         (AcquireImageColormap(image,one << bits_per_pixel) == MagickFalse))
352       ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
353     GetMagickPixelPacket(image,&transpix);
354     if (bits_per_pixel == 16)  /* Direct Color */
355       {
356         redbits=(size_t) ReadBlobByte(image);  /* # of bits of red */
357         (void) redbits;
358         greenbits=(size_t) ReadBlobByte(image);  /* # of bits of green */
359         (void) greenbits;
360         bluebits=(size_t) ReadBlobByte(image);  /* # of bits of blue */
361         (void) bluebits;
362         ReadBlobByte(image);  /* reserved by Palm */
363         ReadBlobByte(image);  /* reserved by Palm */
364         transpix.red=(MagickRealType) (QuantumRange*ReadBlobByte(image)/31);
365         transpix.green=(MagickRealType) (QuantumRange*ReadBlobByte(image)/63);
366         transpix.blue=(MagickRealType) (QuantumRange*ReadBlobByte(image)/31);
367       }
368     if (bits_per_pixel == 8)
369       {
370         IndexPacket
371           index;
372
373         if (flags & PALM_HAS_COLORMAP_FLAG)
374           {
375             count=(ssize_t) ReadBlobMSBShort(image);
376             for (i=0; i < (ssize_t) count; i++)
377             {
378               ReadBlobByte(image);
379               index=ConstrainColormapIndex(image,255-i);
380               image->colormap[(int) index].red=
381                 ScaleCharToQuantum((unsigned char) ReadBlobByte(image));
382               image->colormap[(int) index].green=
383                 ScaleCharToQuantum((unsigned char) ReadBlobByte(image));
384               image->colormap[(int) index].blue=
385                 ScaleCharToQuantum((unsigned char) ReadBlobByte(image));
386           }
387         }
388       else
389         {
390           for (i=0; i < (ssize_t) (1L << bits_per_pixel); i++)
391           {
392             index=ConstrainColormapIndex(image,255-i);
393             image->colormap[(int) index].red=
394               ScaleCharToQuantum(PalmPalette[i][0]);
395             image->colormap[(int) index].green=
396               ScaleCharToQuantum(PalmPalette[i][1]);
397             image->colormap[(int) index].blue=
398               ScaleCharToQuantum(PalmPalette[i][2]);
399           }
400         }
401       }
402     if (flags & PALM_IS_COMPRESSED_FLAG)
403       size=ReadBlobMSBShort(image);
404     (void) size;
405     image->storage_class=DirectClass;
406     if (bits_per_pixel < 16)
407       {
408         image->storage_class=PseudoClass;
409         image->depth=8;
410       }
411     one_row=(unsigned char *) AcquireQuantumMemory(bytes_per_row,
412       sizeof(*one_row));
413     if (one_row == (unsigned char *) NULL)
414       ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
415     lastrow=(unsigned char *) NULL;
416     if (compressionType == PALM_COMPRESSION_SCANLINE) {
417       lastrow=(unsigned char *) AcquireQuantumMemory(bytes_per_row,
418         sizeof(*lastrow));
419     if (lastrow == (unsigned char *) NULL)
420       ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
421     }
422     mask=(1l << 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         }
431       else
432         {
433           if (compressionType == PALM_COMPRESSION_RLE)
434             { 
435               /* TODO move out of loop! */
436               image->compression=RLECompression;
437               for (i=0; i < (ssize_t) bytes_per_row; )
438               {
439                 count=(ssize_t) ReadBlobByte(image);
440                 count=MagickMin(count,(ssize_t) bytes_per_row-i);
441                 byte=(size_t) ReadBlobByte(image);
442                 (void) ResetMagickMemory(one_row+i,(int) byte,(size_t) count);
443                 i+=count;
444               }
445           }
446         else
447           if (compressionType == PALM_COMPRESSION_SCANLINE)
448             {
449               size_t
450                 one;
451
452               /* TODO move out of loop! */
453               one=1;
454               image->compression=FaxCompression;
455               for (i=0; i < (ssize_t) bytes_per_row; i+=8)
456               {
457                 count=(ssize_t) ReadBlobByte(image);
458                 byte=1UL*MagickMin((ssize_t) bytes_per_row-i,8);
459                 for (bit=0; bit < byte; bit++)
460                 {
461                   if ((y == 0) || (count & (one << (7 - bit))))
462                     one_row[i+bit]=(unsigned char) ReadBlobByte(image);
463                   else
464                     one_row[i+bit]=lastrow[i+bit];
465                 }
466               }
467               (void) CopyMagickMemory(lastrow, one_row, bytes_per_row);
468             }
469         }
470       ptr=one_row;
471       q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
472       if (q == (PixelPacket *) NULL)
473         break;
474       indexes=GetAuthenticIndexQueue(image);
475       if (bits_per_pixel == 16)
476         {
477           if (image->columns > (2*bytes_per_row))
478             ThrowReaderException(CorruptImageError,"CorruptImage");
479           for (x=0; x < (ssize_t) image->columns; x++)
480           {
481             color16=(*ptr++ << 8);
482             color16|=(*ptr++);
483             q->red=(Quantum) ((QuantumRange*((color16 >> 11) & 0x1f))/0x1f);
484             q->green=(Quantum) ((QuantumRange*((color16 >> 5) & 0x3f))/0x3f);
485             q->blue=(Quantum) ((QuantumRange*((color16 >> 0) & 0x1f))/0x1f);
486             SetOpacityPixelComponent(q,OpaqueOpacity);
487             q++;
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=(IndexPacket) (mask-(((*ptr) & (mask << bit)) >> bit));
498             indexes[x]=index;
499             *q++=image->colormap[(ssize_t) index];
500             if (bit)
501               bit-=bits_per_pixel;
502             else
503               {
504                 ptr++;
505                 bit=8-bits_per_pixel;
506               }
507           }
508           if (SyncAuthenticPixels(image,exception) == MagickFalse)
509             break;
510         }
511         if (image->previous == (Image *) NULL)
512           {
513             status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
514                 image->rows);
515             if (status == MagickFalse)
516               break;
517           }
518       }
519     if (flags & PALM_HAS_TRANSPARENCY_FLAG)
520       {
521         if (bits_per_pixel != 16)
522           SetMagickPixelPacket(image,image->colormap+(mask-transparentIndex),
523             (const IndexPacket *) NULL,&transpix);
524         (void) TransparentPaintImage(image,&transpix,(Quantum)
525           TransparentOpacity,MagickFalse);
526       }
527     one_row=(unsigned char *) RelinquishMagickMemory(one_row);
528     if (compressionType == PALM_COMPRESSION_SCANLINE)
529     lastrow=(unsigned char *) RelinquishMagickMemory(lastrow);
530     /*
531       Proceed to next image. Copied from coders/pnm.c
532     */
533     if (image_info->number_scenes != 0)
534       if (image->scene >= (image_info->scene+image_info->number_scenes-1))
535         break;
536     if (nextDepthOffset != 0)
537       { 
538         /*
539           Skip to next image. 
540         */
541         totalOffset+=(MagickOffsetType) (nextDepthOffset*4);
542         if (totalOffset >= (MagickOffsetType) GetBlobSize(image))
543           {
544             ThrowReaderException(CorruptImageError,"ImproperImageHeader");
545           }
546         else
547           {
548             seekNextDepth=SeekBlob(image,totalOffset,SEEK_SET);
549           }
550         if (seekNextDepth != totalOffset)
551           ThrowReaderException(CorruptImageError,"ImproperImageHeader");
552         /*
553           Allocate next image structure. Copied from coders/pnm.c
554         */
555         AcquireNextImage(image_info,image);
556         if (GetNextImageInList(image) == (Image *) NULL)
557           {
558             (void) DestroyImageList(image);
559             return((Image *) NULL);
560           }
561         image=SyncNextImageInList(image);
562         status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
563           GetBlobSize(image));
564         if (status == MagickFalse)
565           break;
566       }
567   } while (nextDepthOffset != 0);
568   (void) CloseBlob(image);
569   return(GetFirstImageInList(image));
570 }
571 \f
572 /*
573 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
574 %                                                                             %
575 %                                                                             %
576 %                                                                             %
577 %   R e g i s t e r P A L M I m a g e                                         %
578 %                                                                             %
579 %                                                                             %
580 %                                                                             %
581 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
582 %
583 %  RegisterPALMImage() adds properties for the PALM image format to the list of
584 %  supported formats.  The properties include the image format tag, a method to
585 %  read and/or write the format, whether the format supports the saving of more
586 %  than one frame to the same file or blob, whether the format supports native
587 %  in-memory I/O, and a brief description of the format.
588 %
589 %  The format of the RegisterPALMImage method is:
590 %
591 %      size_t RegisterPALMImage(void)
592 %
593 */
594 ModuleExport size_t RegisterPALMImage(void)
595 {
596   MagickInfo
597     *entry;
598
599   entry=SetMagickInfo("PALM");
600   entry->decoder=(DecodeImageHandler *) ReadPALMImage;
601   entry->encoder=(EncodeImageHandler *) WritePALMImage;
602   entry->seekable_stream=MagickTrue;
603   entry->description=ConstantString("Palm pixmap");
604   entry->module=ConstantString("PALM");
605   (void) RegisterMagickInfo(entry);
606   return(MagickImageCoderSignature);
607 }
608 \f
609 /*
610 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
611 %                                                                             %
612 %                                                                             %
613 %                                                                             %
614 %   U n r e g i s t e r P A L M I m a g e                                     %
615 %                                                                             %
616 %                                                                             %
617 %                                                                             %
618 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
619 %
620 %  UnregisterPALMImage() removes format registrations made by the PALM
621 %  module from the list of supported formats.
622 %
623 %  The format of the UnregisterPALMImage method is:
624 %
625 %      UnregisterPALMImage(void)
626 %
627 */
628 ModuleExport void UnregisterPALMImage(void)
629 {
630   (void) UnregisterMagickInfo("PALM");
631 }
632 \f
633 /*
634 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
635 %                                                                             %
636 %                                                                             %
637 %                                                                             %
638 %   W r i t e P A L M I m a g e                                               %
639 %                                                                             %
640 %                                                                             %
641 %                                                                             %
642 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
643 %
644 %  WritePALMImage() writes an image of raw bits in LSB order to a file.
645 %
646 %  The format of the WritePALMImage method is:
647 %
648 %      MagickBooleanType WritePALMImage(const ImageInfo *image_info,
649 %        Image *image)
650 %
651 %  A description of each parameter follows.
652 %
653 %    o image_info: Specifies a pointer to an ImageInfo structure.
654 %
655 %    o image:  A pointer to a Image structure.
656 %
657 */
658 static MagickBooleanType WritePALMImage(const ImageInfo *image_info,
659   Image *image)
660 {
661   int
662     y;
663
664   ExceptionInfo
665     exception;
666
667   MagickBooleanType
668     status;
669
670   MagickOffsetType
671     currentOffset,
672     offset,
673     scene;
674
675   MagickSizeType
676     cc;
677
678   PixelPacket
679     transpix;
680
681   QuantizeInfo
682     *quantize_info;
683
684   register IndexPacket
685     *indexes;
686
687   register ssize_t
688     x;
689
690   register PixelPacket
691     *p;
692
693   unsigned char
694     bit,
695     byte,
696     color,
697     *lastrow,
698     *one_row,
699     *ptr,
700     version;
701
702   unsigned int
703     transparentIndex;
704
705   size_t
706     count,
707     bits_per_pixel,
708     bytes_per_row,
709     nextDepthOffset,
710     one;
711
712   unsigned short
713     color16,
714     flags;
715
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   status=OpenBlob(image_info,image,WriteBinaryBlobMode,&exception);
727   if (status == MagickFalse)
728     return(status);
729   GetExceptionInfo(&exception);
730   quantize_info=AcquireQuantizeInfo(image_info);
731   flags=0;
732   currentOffset=0;
733   transparentIndex=0;
734   transpix.red=0;
735   transpix.green=0;
736   transpix.blue=0;
737   transpix.opacity=0;
738   one=1;
739   version=0;
740   scene=0;
741   do
742   {
743     if (image->colorspace != RGBColorspace)
744       (void) TransformImageColorspace(image,RGBColorspace);
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);
751     if (bits_per_pixel < 8)
752       {
753         (void) TransformImageColorspace(image,GRAYColorspace);
754         (void) SetImageType(image,PaletteType);
755         (void) SortColormapByIntensity(image);
756       }
757     if ((image->storage_class == PseudoClass) && (image->colors > 256))
758       (void) SetImageStorageClass(image,DirectClass);
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=IsPaletteImage(image,&image->exception);
804             quantize_info->number_colors=image->colors;
805             (void) QuantizeImage(quantize_info,image);
806             (void) WriteBlobMSBShort(image,(unsigned short) image->colors);
807             for (count = 0; count < image->colors; count++)
808             {
809               (void) WriteBlobByte(image,(unsigned char) count);
810               (void) WriteBlobByte(image,ScaleQuantumToChar(
811                 image->colormap[count].red));
812               (void) WriteBlobByte(image,
813                 ScaleQuantumToChar(image->colormap[count].green));
814               (void) WriteBlobByte(image,
815                 ScaleQuantumToChar(image->colormap[count].blue));
816             }
817             offset+=2+count*4;
818           }
819       else  /* Map colors to Palm standard colormap */
820         {
821           Image
822             *affinity_image;
823
824           affinity_image=ConstituteImage(256,1,"RGB",CharPixel,&PalmPalette,
825             &exception);
826           (void) TransformImageColorspace(affinity_image,affinity_image->colorspace);
827           (void) RemapImage(quantize_info,image,affinity_image);
828           for (y=0; y < (ssize_t) image->rows; y++)
829           {
830             p=GetAuthenticPixels(image,0,y,image->columns,1,&exception);
831             indexes=GetAuthenticIndexQueue(image);
832             for (x=0; x < (ssize_t) image->columns; x++)
833               indexes[x]=(IndexPacket) FindColor(&image->colormap[(ssize_t)
834                 indexes[x]]);
835           }
836           affinity_image=DestroyImage(affinity_image);
837         }
838       }
839     if (flags & PALM_IS_COMPRESSED_FLAG)
840       (void) WriteBlobMSBShort(image,0);  /* fill in size later */
841     lastrow=(unsigned char *) NULL;
842     if (image_info->compression == FaxCompression)
843       lastrow=(unsigned char *) AcquireQuantumMemory(bytes_per_row,
844         sizeof(*lastrow));
845       /* TODO check whether memory really was acquired? */
846     one_row=(unsigned char *) AcquireQuantumMemory(bytes_per_row,
847       sizeof(*one_row));
848     if (one_row == (unsigned char *) NULL)
849       ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
850     for (y=0; y < (int) image->rows; y++)
851     {
852       ptr=one_row;
853       (void) ResetMagickMemory(ptr,0,bytes_per_row);
854       p=GetAuthenticPixels(image,0,y,image->columns,1,&exception);
855       if (p == (PixelPacket *) NULL)
856         break;
857       indexes=GetAuthenticIndexQueue(image);
858       if (bits_per_pixel == 16)
859         {
860           for (x=0; x < (int) image->columns; x++)
861           {
862             color16=(unsigned short) ((((31*(size_t) GetRedPixelComponent(p))/
863               (size_t) QuantumRange) << 11) |
864               (((63*(size_t) GetGreenPixelComponent(p))/(size_t) QuantumRange) << 5) |
865               ((31*(size_t) GetBluePixelComponent(p))/(size_t) QuantumRange));
866             if (p->opacity == (Quantum) TransparentOpacity)
867               {
868                 transpix.red=GetRedPixelComponent(p);
869                 transpix.green=GetGreenPixelComponent(p);
870                 transpix.blue=GetBluePixelComponent(p);
871                 transpix.opacity=GetOpacityPixelComponent(p);
872                 flags|=PALM_HAS_TRANSPARENCY_FLAG;
873               }
874             *ptr++=(unsigned char) ((color16 >> 8) & 0xff);
875             *ptr++=(unsigned char) (color16 & 0xff);
876             p++;
877           }
878         }
879       else
880         {
881           byte=0x00;
882           bit=(unsigned char) (8-bits_per_pixel);
883           for (x=0; x < (int) image->columns; x++)
884           {
885             if (bits_per_pixel >= 8)
886               color=(unsigned char) indexes[x];
887             else
888               color=(unsigned char) (indexes[x]*((one << bits_per_pixel)-1)/
889                 MagickMax(1*image->colors-1,1));
890             byte|=color << bit;
891             if (bit != 0)
892               bit-=(unsigned char) bits_per_pixel;
893             else
894               {
895                 *ptr++=byte;
896                 byte=0x00;
897                 bit=(unsigned char) (8-bits_per_pixel);
898               }
899           }
900           if ((image->columns % (8/bits_per_pixel)) != 0)
901             *ptr++=byte;
902         }
903       if (image_info->compression == RLECompression)
904         {
905           x=0;
906           while (x < (ssize_t) bytes_per_row)
907           {
908             byte=one_row[x];
909             count=1;
910             while ((one_row[++x] == byte) && (count < 255) &&
911                    (x < (ssize_t) bytes_per_row))
912               count++;
913             (void) WriteBlobByte(image,(unsigned char) count);
914             (void) WriteBlobByte(image,(unsigned char) byte);
915           }
916         }
917       else
918         if (image_info->compression == FaxCompression)
919           {
920             char
921               tmpbuf[8],
922               *tptr;
923   
924             for (x = 0;  x < (ssize_t) bytes_per_row;  x += 8)
925             {
926               tptr = tmpbuf;
927               for (bit=0, byte=0; bit < (unsigned char) MagickMin(8,(ssize_t) bytes_per_row-x); bit++)
928               {
929                 if ((y == 0) || (lastrow[x + bit] != one_row[x + bit]))
930                   {
931                   byte |= (1 << (7 - bit));
932                   *tptr++ = (char) one_row[x + bit];
933                 }
934               }
935               (void) WriteBlobByte(image, byte);
936               (void) WriteBlob(image,tptr-tmpbuf,(unsigned char *) tmpbuf);
937             }
938             (void) CopyMagickMemory(lastrow,one_row,bytes_per_row);
939           }
940         else
941           (void) WriteBlob(image,bytes_per_row,one_row);
942       }
943     if (flags & PALM_HAS_TRANSPARENCY_FLAG)
944       {
945         offset=SeekBlob(image,currentOffset+6,SEEK_SET);
946         (void) WriteBlobMSBShort(image,flags);
947         offset=SeekBlob(image,currentOffset+12,SEEK_SET);
948         (void) WriteBlobByte(image,(unsigned char) transparentIndex);  /* trans index */
949       }
950     if (bits_per_pixel == 16)
951       {
952         offset=SeekBlob(image,currentOffset+20,SEEK_SET);
953         (void) WriteBlobByte(image,0);  /* reserved by Palm */
954         (void) WriteBlobByte(image,(unsigned char) ((31*transpix.red)/QuantumRange));
955         (void) WriteBlobByte(image,(unsigned char) ((63*transpix.green)/QuantumRange));
956         (void) WriteBlobByte(image,(unsigned char) ((31*transpix.blue)/QuantumRange));
957       }
958     if (flags & PALM_IS_COMPRESSED_FLAG)  /* fill in size now */
959       {
960         offset=SeekBlob(image,currentOffset+offset,SEEK_SET);
961         (void) WriteBlobMSBShort(image,(unsigned short) (GetBlobSize(image)-
962           currentOffset-offset));
963       }
964     if (one_row != (unsigned char *) NULL) 
965       one_row=(unsigned char *) RelinquishMagickMemory(one_row);
966     if (lastrow != (unsigned char *) NULL) 
967       lastrow=(unsigned char *) RelinquishMagickMemory(lastrow);
968     if (GetNextImageInList(image) == (Image *) NULL)
969       break;
970     /* padding to 4 byte word */
971     for (cc = (GetBlobSize(image))%4; cc > 0; cc--)
972     {
973       (void) WriteBlobByte(image,0);
974     } 
975     /* write nextDepthOffset and return to end of image */
976     offset=SeekBlob(image,currentOffset+10,SEEK_SET);
977     nextDepthOffset=(size_t) ((GetBlobSize(image)-currentOffset)/4);
978     (void) WriteBlobMSBShort(image,(unsigned short) nextDepthOffset);
979     currentOffset=(MagickOffsetType) GetBlobSize(image);
980     offset=SeekBlob(image,currentOffset,SEEK_SET);
981     image=SyncNextImageInList(image);
982     status=SetImageProgress(image,SaveImagesTag,scene++,
983       GetImageListLength(image));
984     if (status == MagickFalse)
985       break;
986   } while (image_info->adjoin != MagickFalse);
987   quantize_info=DestroyQuantizeInfo(quantize_info);
988   (void) CloseBlob(image);
989   (void) DestroyExceptionInfo(&exception);
990   return(MagickTrue);
991 }