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