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