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