blob: e5a56f2f3532bd0a4dedc4160239de1d482dc520 [file] [log] [blame]
cristy3ed852e2009-09-05 21:47:34 +00001/*
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 %
cristyaff6d802011-04-26 01:46:31 +000024% obtain a copy of the License at %
cristy3ed852e2009-09-05 21:47:34 +000025% %
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
41/*
42 Include declarations.
43*/
cristy4c08aed2011-07-01 19:47:50 +000044#include "MagickCore/studio.h"
cristy8941c702012-06-21 01:30:15 +000045#include "MagickCore/attribute.h"
cristy4c08aed2011-07-01 19:47:50 +000046#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"
cristy510d06a2011-07-06 23:43:54 +000054#include "MagickCore/colorspace-private.h"
cristy4c08aed2011-07-01 19:47:50 +000055#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"
cristy3ed852e2009-09-05 21:47:34 +000074
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*/
Cristy629f8052015-09-14 07:48:08 -040092static const unsigned char
cristy3ed852e2009-09-05 21:47:34 +000093 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*/
164static MagickBooleanType
cristy1e178e72011-08-28 19:44:34 +0000165 WritePALMImage(const ImageInfo *,Image *,ExceptionInfo *);
cristy3ed852e2009-09-05 21:47:34 +0000166
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
cristy101ab702011-10-13 13:06:32 +0000179% given PixelInfo.
cristy3ed852e2009-09-05 21:47:34 +0000180%
181% The format of the FindColor method is:
182%
cristyb0de93f2013-05-03 13:39:25 +0000183% int FindColor(PixelInfo *pixel)
cristy3ed852e2009-09-05 21:47:34 +0000184%
185% A description of each parameter follows:
186%
187% o int: the index of the matching color or -1 if not found/
188%
cristy101ab702011-10-13 13:06:32 +0000189% o pixel: a pointer to the PixelInfo to be matched.
cristy3ed852e2009-09-05 21:47:34 +0000190%
191*/
cristydd12db52014-11-30 21:54:11 +0000192static ssize_t FindColor(PixelInfo *packet)
cristy3ed852e2009-09-05 21:47:34 +0000193{
cristybb503372010-05-27 20:51:26 +0000194 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000195 i;
196
197 for (i=0; i < 256; i++)
cristydd12db52014-11-30 21:54:11 +0000198 if (ScaleQuantumToChar(ClampToQuantum(packet->red)) == PalmPalette[i][0] &&
199 ScaleQuantumToChar(ClampToQuantum(packet->green)) == PalmPalette[i][1] &&
200 ScaleQuantumToChar(ClampToQuantum(packet->blue)) == PalmPalette[i][2])
cristy3ed852e2009-09-05 21:47:34 +0000201 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*/
cristy3ed852e2009-09-05 21:47:34 +0000232static Image *ReadPALMImage(const ImageInfo *image_info,
233 ExceptionInfo *exception)
234{
235 Image
236 *image;
237
cristy3ed852e2009-09-05 21:47:34 +0000238 MagickBooleanType
239 status;
240
cristyeaedf062010-05-29 22:36:02 +0000241 MagickOffsetType
242 totalOffset,
243 seekNextDepth;
244
cristy4c08aed2011-07-01 19:47:50 +0000245 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000246 transpix;
247
cristy4c08aed2011-07-01 19:47:50 +0000248 Quantum
249 index;
cristy3ed852e2009-09-05 21:47:34 +0000250
cristybb503372010-05-27 20:51:26 +0000251 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000252 i,
253 x;
254
cristy4c08aed2011-07-01 19:47:50 +0000255 register Quantum
cristy3ed852e2009-09-05 21:47:34 +0000256 *q;
257
cristybb503372010-05-27 20:51:26 +0000258 size_t
cristy3ed852e2009-09-05 21:47:34 +0000259 bytes_per_row,
260 flags,
261 bits_per_pixel,
262 version,
263 nextDepthOffset,
264 transparentIndex,
265 compressionType,
266 byte,
267 mask,
268 redbits,
269 greenbits,
270 bluebits,
cristyeaedf062010-05-29 22:36:02 +0000271 one,
cristy3ed852e2009-09-05 21:47:34 +0000272 pad,
273 size,
274 bit;
275
cristy64b17a62015-02-15 13:22:11 +0000276 ssize_t
277 count,
278 y;
279
cristyaff6d802011-04-26 01:46:31 +0000280 unsigned char
281 *lastrow,
282 *one_row,
283 *ptr;
284
cristy3ed852e2009-09-05 21:47:34 +0000285 unsigned short
286 color16;
287
cristy3ed852e2009-09-05 21:47:34 +0000288 /*
289 Open image file.
290 */
291 assert(image_info != (const ImageInfo *) NULL);
cristye1c94d92015-06-28 12:16:33 +0000292 assert(image_info->signature == MagickCoreSignature);
cristy3ed852e2009-09-05 21:47:34 +0000293 if (image_info->debug != MagickFalse)
294 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
295 image_info->filename);
296 assert(exception != (ExceptionInfo *) NULL);
cristye1c94d92015-06-28 12:16:33 +0000297 assert(exception->signature == MagickCoreSignature);
cristy9950d572011-10-01 18:22:35 +0000298 image=AcquireImage(image_info,exception);
cristy3ed852e2009-09-05 21:47:34 +0000299 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
300 if (status == MagickFalse)
301 {
302 (void) DestroyImageList(image);
303 return((Image *) NULL);
304 }
305 totalOffset=0;
306 do
307 {
308 image->columns=ReadBlobMSBShort(image);
309 image->rows=ReadBlobMSBShort(image);
310 if (EOFBlob(image) != MagickFalse)
311 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
cristy3ed852e2009-09-05 21:47:34 +0000312 if ((image->columns == 0) || (image->rows == 0))
313 ThrowReaderException(CorruptImageError,"NegativeOrZeroImageSize");
cristyacabb842014-12-14 23:36:33 +0000314 status=SetImageExtent(image,image->columns,image->rows,exception);
315 if (status == MagickFalse)
316 return(DestroyImageList(image));
cristy3ed852e2009-09-05 21:47:34 +0000317 bytes_per_row=ReadBlobMSBShort(image);
318 flags=ReadBlobMSBShort(image);
cristybb503372010-05-27 20:51:26 +0000319 bits_per_pixel=(size_t) ReadBlobByte(image);
cristy64b17a62015-02-15 13:22:11 +0000320 if ((bits_per_pixel != 1) && (bits_per_pixel != 2) &&
321 (bits_per_pixel != 4) && (bits_per_pixel != 8) &&
322 (bits_per_pixel != 16))
323 ThrowReaderException(CorruptImageError,"UnrecognizedBitsPerPixel");
cristybb503372010-05-27 20:51:26 +0000324 version=(size_t) ReadBlobByte(image);
cristy04c969d2015-02-17 02:00:32 +0000325 if ((version != 0) && (version != 1) && (version != 2))
326 ThrowReaderException(CorruptImageError,"FileFormatVersionMismatch");
cristybb503372010-05-27 20:51:26 +0000327 nextDepthOffset=(size_t) ReadBlobMSBShort(image);
328 transparentIndex=(size_t) ReadBlobByte(image);
329 compressionType=(size_t) ReadBlobByte(image);
cristy64b17a62015-02-15 13:22:11 +0000330 if ((compressionType != PALM_COMPRESSION_NONE) &&
331 (compressionType != PALM_COMPRESSION_SCANLINE ) &&
332 (compressionType != PALM_COMPRESSION_RLE))
333 ThrowReaderException(CorruptImageError,"UnrecognizedImageCompression");
cristy3ed852e2009-09-05 21:47:34 +0000334 pad=ReadBlobMSBShort(image);
cristyda16f162011-02-19 23:52:17 +0000335 (void) pad;
cristy3ed852e2009-09-05 21:47:34 +0000336 /*
337 Initialize image colormap.
338 */
cristyeaedf062010-05-29 22:36:02 +0000339 one=1;
cristy3ed852e2009-09-05 21:47:34 +0000340 if ((bits_per_pixel < 16) &&
cristy018f07f2011-09-04 21:15:19 +0000341 (AcquireImageColormap(image,one << bits_per_pixel,exception) == MagickFalse))
cristy3ed852e2009-09-05 21:47:34 +0000342 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
cristy4c08aed2011-07-01 19:47:50 +0000343 GetPixelInfo(image,&transpix);
cristy3ed852e2009-09-05 21:47:34 +0000344 if (bits_per_pixel == 16) /* Direct Color */
345 {
cristybb503372010-05-27 20:51:26 +0000346 redbits=(size_t) ReadBlobByte(image); /* # of bits of red */
cristyda16f162011-02-19 23:52:17 +0000347 (void) redbits;
cristybb503372010-05-27 20:51:26 +0000348 greenbits=(size_t) ReadBlobByte(image); /* # of bits of green */
cristyda16f162011-02-19 23:52:17 +0000349 (void) greenbits;
cristybb503372010-05-27 20:51:26 +0000350 bluebits=(size_t) ReadBlobByte(image); /* # of bits of blue */
cristyda16f162011-02-19 23:52:17 +0000351 (void) bluebits;
cristy3ed852e2009-09-05 21:47:34 +0000352 ReadBlobByte(image); /* reserved by Palm */
353 ReadBlobByte(image); /* reserved by Palm */
cristya19f1d72012-08-07 18:24:38 +0000354 transpix.red=(double) (QuantumRange*ReadBlobByte(image)/31);
355 transpix.green=(double) (QuantumRange*ReadBlobByte(image)/63);
356 transpix.blue=(double) (QuantumRange*ReadBlobByte(image)/31);
cristy3ed852e2009-09-05 21:47:34 +0000357 }
358 if (bits_per_pixel == 8)
359 {
cristydd12db52014-11-30 21:54:11 +0000360 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000361 index;
362
363 if (flags & PALM_HAS_COLORMAP_FLAG)
364 {
365 count=(ssize_t) ReadBlobMSBShort(image);
cristybb503372010-05-27 20:51:26 +0000366 for (i=0; i < (ssize_t) count; i++)
cristy3ed852e2009-09-05 21:47:34 +0000367 {
368 ReadBlobByte(image);
cristyc82a27b2011-10-21 01:07:16 +0000369 index=ConstrainColormapIndex(image,255-i,exception);
cristydd12db52014-11-30 21:54:11 +0000370 image->colormap[index].red=(MagickRealType)
cristy3ed852e2009-09-05 21:47:34 +0000371 ScaleCharToQuantum((unsigned char) ReadBlobByte(image));
cristydd12db52014-11-30 21:54:11 +0000372 image->colormap[index].green=(MagickRealType)
cristy3ed852e2009-09-05 21:47:34 +0000373 ScaleCharToQuantum((unsigned char) ReadBlobByte(image));
cristydd12db52014-11-30 21:54:11 +0000374 image->colormap[index].blue=(MagickRealType)
cristy3ed852e2009-09-05 21:47:34 +0000375 ScaleCharToQuantum((unsigned char) ReadBlobByte(image));
376 }
377 }
378 else
cristy04c969d2015-02-17 02:00:32 +0000379 for (i=0; i < (ssize_t) (1L << bits_per_pixel); i++)
cristy3ed852e2009-09-05 21:47:34 +0000380 {
cristy04c969d2015-02-17 02:00:32 +0000381 index=ConstrainColormapIndex(image,255-i,exception);
382 image->colormap[index].red=(MagickRealType)
383 ScaleCharToQuantum(PalmPalette[i][0]);
384 image->colormap[index].green=(MagickRealType)
385 ScaleCharToQuantum(PalmPalette[i][1]);
386 image->colormap[index].blue=(MagickRealType)
387 ScaleCharToQuantum(PalmPalette[i][2]);
cristy3ed852e2009-09-05 21:47:34 +0000388 }
389 }
390 if (flags & PALM_IS_COMPRESSED_FLAG)
391 size=ReadBlobMSBShort(image);
cristyda16f162011-02-19 23:52:17 +0000392 (void) size;
cristy3ed852e2009-09-05 21:47:34 +0000393 image->storage_class=DirectClass;
394 if (bits_per_pixel < 16)
395 {
396 image->storage_class=PseudoClass;
397 image->depth=8;
398 }
cristy64b17a62015-02-15 13:22:11 +0000399 if (image_info->ping != MagickFalse)
400 {
401 (void) CloseBlob(image);
402 return(image);
403 }
dirkd909dbb2015-02-15 17:18:13 +0000404 status=SetImageExtent(image,image->columns,image->rows,exception);
cristy64b17a62015-02-15 13:22:11 +0000405 if (status == MagickFalse)
406 return(DestroyImageList(image));
cristydd12db52014-11-30 21:54:11 +0000407 one_row=(unsigned char *) AcquireQuantumMemory(MagickMax(bytes_per_row,
408 2*image->columns),sizeof(*one_row));
cristy3ed852e2009-09-05 21:47:34 +0000409 if (one_row == (unsigned char *) NULL)
410 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
411 lastrow=(unsigned char *) NULL;
412 if (compressionType == PALM_COMPRESSION_SCANLINE) {
cristydd12db52014-11-30 21:54:11 +0000413 lastrow=(unsigned char *) AcquireQuantumMemory(MagickMax(bytes_per_row,
414 2*image->columns),sizeof(*lastrow));
cristy3ed852e2009-09-05 21:47:34 +0000415 if (lastrow == (unsigned char *) NULL)
416 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
417 }
cristydd12db52014-11-30 21:54:11 +0000418 mask=(size_t) (1U << bits_per_pixel)-1;
cristy04c969d2015-02-17 02:00:32 +0000419 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000420 {
421 if ((flags & PALM_IS_COMPRESSED_FLAG) == 0)
422 {
423 /* TODO move out of loop! */
424 image->compression=NoCompression;
425 count=ReadBlob(image,bytes_per_row,one_row);
cristy04c969d2015-02-17 02:00:32 +0000426 if (count != (ssize_t) bytes_per_row)
427 break;
cristy3ed852e2009-09-05 21:47:34 +0000428 }
429 else
430 {
431 if (compressionType == PALM_COMPRESSION_RLE)
432 {
433 /* TODO move out of loop! */
434 image->compression=RLECompression;
cristybb503372010-05-27 20:51:26 +0000435 for (i=0; i < (ssize_t) bytes_per_row; )
cristy3ed852e2009-09-05 21:47:34 +0000436 {
437 count=(ssize_t) ReadBlobByte(image);
cristy30fc0812015-02-14 15:56:20 +0000438 if (count < 0)
439 break;
cristy3ed852e2009-09-05 21:47:34 +0000440 count=MagickMin(count,(ssize_t) bytes_per_row-i);
cristybb503372010-05-27 20:51:26 +0000441 byte=(size_t) ReadBlobByte(image);
cristy3ed852e2009-09-05 21:47:34 +0000442 (void) ResetMagickMemory(one_row+i,(int) byte,(size_t) count);
443 i+=count;
444 }
445 }
446 else
447 if (compressionType == PALM_COMPRESSION_SCANLINE)
cristyf9cca6a2010-06-04 23:49:28 +0000448 {
449 size_t
450 one;
451
cristy3ed852e2009-09-05 21:47:34 +0000452 /* TODO move out of loop! */
cristyf9cca6a2010-06-04 23:49:28 +0000453 one=1;
cristy3ed852e2009-09-05 21:47:34 +0000454 image->compression=FaxCompression;
cristybb503372010-05-27 20:51:26 +0000455 for (i=0; i < (ssize_t) bytes_per_row; i+=8)
cristy3ed852e2009-09-05 21:47:34 +0000456 {
457 count=(ssize_t) ReadBlobByte(image);
cristy04c969d2015-02-17 02:00:32 +0000458 if (count < 0)
459 break;
cristydd12db52014-11-30 21:54:11 +0000460 byte=(size_t) MagickMin((ssize_t) bytes_per_row-i,8);
cristy3ed852e2009-09-05 21:47:34 +0000461 for (bit=0; bit < byte; bit++)
462 {
cristyf9cca6a2010-06-04 23:49:28 +0000463 if ((y == 0) || (count & (one << (7 - bit))))
cristy3ed852e2009-09-05 21:47:34 +0000464 one_row[i+bit]=(unsigned char) ReadBlobByte(image);
465 else
466 one_row[i+bit]=lastrow[i+bit];
467 }
468 }
469 (void) CopyMagickMemory(lastrow, one_row, bytes_per_row);
470 }
471 }
472 ptr=one_row;
473 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +0000474 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000475 break;
cristy3ed852e2009-09-05 21:47:34 +0000476 if (bits_per_pixel == 16)
477 {
478 if (image->columns > (2*bytes_per_row))
479 ThrowReaderException(CorruptImageError,"CorruptImage");
cristybb503372010-05-27 20:51:26 +0000480 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000481 {
482 color16=(*ptr++ << 8);
483 color16|=(*ptr++);
cristydd12db52014-11-30 21:54:11 +0000484 SetPixelRed(image,(Quantum) ((QuantumRange*((color16 >> 11) &
485 0x1f))/0x1f),q);
486 SetPixelGreen(image,(Quantum) ((QuantumRange*((color16 >> 5) &
487 0x3f))/0x3f),q);
488 SetPixelBlue(image,(Quantum) ((QuantumRange*((color16 >> 0) &
489 0x1f))/0x1f),q);
cristy4c08aed2011-07-01 19:47:50 +0000490 SetPixelAlpha(image,OpaqueAlpha,q);
cristyed231572011-07-14 02:18:59 +0000491 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000492 }
493 }
494 else
495 {
496 bit=8-bits_per_pixel;
cristybb503372010-05-27 20:51:26 +0000497 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000498 {
499 if ((size_t) (ptr-one_row) >= bytes_per_row)
500 ThrowReaderException(CorruptImageError,"CorruptImage");
cristy4c08aed2011-07-01 19:47:50 +0000501 index=(Quantum) (mask-(((*ptr) & (mask << bit)) >> bit));
502 SetPixelIndex(image,index,q);
cristy11a06d32015-01-04 12:03:27 +0000503 SetPixelViaPixelInfo(image,image->colormap+(ssize_t) index,q);
cristy3ed852e2009-09-05 21:47:34 +0000504 if (bit)
505 bit-=bits_per_pixel;
506 else
507 {
508 ptr++;
509 bit=8-bits_per_pixel;
510 }
cristyed231572011-07-14 02:18:59 +0000511 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000512 }
513 if (SyncAuthenticPixels(image,exception) == MagickFalse)
514 break;
515 }
516 if (image->previous == (Image *) NULL)
517 {
cristycee97112010-05-28 00:44:52 +0000518 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
cristyaff6d802011-04-26 01:46:31 +0000519 image->rows);
cristy3ed852e2009-09-05 21:47:34 +0000520 if (status == MagickFalse)
521 break;
522 }
523 }
524 if (flags & PALM_HAS_TRANSPARENCY_FLAG)
525 {
cristy69d1a3c2014-12-16 19:01:55 +0000526 ssize_t index=ConstrainColormapIndex(image,(ssize_t) (mask-
527 transparentIndex),exception);
cristy3ed852e2009-09-05 21:47:34 +0000528 if (bits_per_pixel != 16)
cristy53d01502014-12-16 18:59:44 +0000529 transpix=image->colormap[index];
cristy189e84c2011-08-27 18:08:53 +0000530 (void) TransparentPaintImage(image,&transpix,(Quantum) TransparentAlpha,
531 MagickFalse,exception);
cristy3ed852e2009-09-05 21:47:34 +0000532 }
533 one_row=(unsigned char *) RelinquishMagickMemory(one_row);
534 if (compressionType == PALM_COMPRESSION_SCANLINE)
cristy189e84c2011-08-27 18:08:53 +0000535 lastrow=(unsigned char *) RelinquishMagickMemory(lastrow);
cristy30fc0812015-02-14 15:56:20 +0000536 if (EOFBlob(image) != MagickFalse)
537 {
538 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
539 image->filename);
540 break;
541 }
cristy3ed852e2009-09-05 21:47:34 +0000542 /*
543 Proceed to next image. Copied from coders/pnm.c
544 */
545 if (image_info->number_scenes != 0)
546 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
547 break;
548 if (nextDepthOffset != 0)
549 {
550 /*
551 Skip to next image.
552 */
553 totalOffset+=(MagickOffsetType) (nextDepthOffset*4);
554 if (totalOffset >= (MagickOffsetType) GetBlobSize(image))
cristy04c969d2015-02-17 02:00:32 +0000555 ThrowReaderException(CorruptImageError,"ImproperImageHeader")
cristy3ed852e2009-09-05 21:47:34 +0000556 else
cristy04c969d2015-02-17 02:00:32 +0000557 seekNextDepth=SeekBlob(image,totalOffset,SEEK_SET);
cristy3ed852e2009-09-05 21:47:34 +0000558 if (seekNextDepth != totalOffset)
559 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
560 /*
561 Allocate next image structure. Copied from coders/pnm.c
562 */
cristy9950d572011-10-01 18:22:35 +0000563 AcquireNextImage(image_info,image,exception);
cristy3ed852e2009-09-05 21:47:34 +0000564 if (GetNextImageInList(image) == (Image *) NULL)
565 {
566 (void) DestroyImageList(image);
567 return((Image *) NULL);
568 }
569 image=SyncNextImageInList(image);
570 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
571 GetBlobSize(image));
572 if (status == MagickFalse)
573 break;
574 }
575 } while (nextDepthOffset != 0);
576 (void) CloseBlob(image);
577 return(GetFirstImageInList(image));
578}
579
580/*
581%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
582% %
583% %
584% %
585% R e g i s t e r P A L M I m a g e %
586% %
587% %
588% %
589%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
590%
591% RegisterPALMImage() adds properties for the PALM image format to the list of
592% supported formats. The properties include the image format tag, a method to
593% read and/or write the format, whether the format supports the saving of more
594% than one frame to the same file or blob, whether the format supports native
595% in-memory I/O, and a brief description of the format.
596%
597% The format of the RegisterPALMImage method is:
598%
cristybb503372010-05-27 20:51:26 +0000599% size_t RegisterPALMImage(void)
cristy3ed852e2009-09-05 21:47:34 +0000600%
601*/
cristybb503372010-05-27 20:51:26 +0000602ModuleExport size_t RegisterPALMImage(void)
cristy3ed852e2009-09-05 21:47:34 +0000603{
604 MagickInfo
605 *entry;
606
dirk06b627a2015-04-06 18:59:17 +0000607 entry=AcquireMagickInfo("PALM","PALM","Palm pixmap");
cristy3ed852e2009-09-05 21:47:34 +0000608 entry->decoder=(DecodeImageHandler *) ReadPALMImage;
609 entry->encoder=(EncodeImageHandler *) WritePALMImage;
dirk08e9a112015-02-22 01:51:41 +0000610 entry->flags|=CoderSeekableStreamFlag;
cristy3ed852e2009-09-05 21:47:34 +0000611 (void) RegisterMagickInfo(entry);
612 return(MagickImageCoderSignature);
613}
614
615/*
616%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
617% %
618% %
619% %
620% U n r e g i s t e r P A L M I m a g e %
621% %
622% %
623% %
624%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
625%
626% UnregisterPALMImage() removes format registrations made by the PALM
627% module from the list of supported formats.
628%
629% The format of the UnregisterPALMImage method is:
630%
631% UnregisterPALMImage(void)
632%
633*/
634ModuleExport void UnregisterPALMImage(void)
635{
636 (void) UnregisterMagickInfo("PALM");
637}
638
639/*
640%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
641% %
642% %
643% %
644% W r i t e P A L M I m a g e %
645% %
646% %
647% %
648%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
649%
650% WritePALMImage() writes an image of raw bits in LSB order to a file.
651%
652% The format of the WritePALMImage method is:
653%
654% MagickBooleanType WritePALMImage(const ImageInfo *image_info,
cristy1e178e72011-08-28 19:44:34 +0000655% Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000656%
657% A description of each parameter follows.
658%
659% o image_info: Specifies a pointer to an ImageInfo structure.
660%
661% o image: A pointer to a Image structure.
662%
cristy1e178e72011-08-28 19:44:34 +0000663% o exception: return any errors or warnings in this structure.
664%
cristy3ed852e2009-09-05 21:47:34 +0000665*/
666static MagickBooleanType WritePALMImage(const ImageInfo *image_info,
cristy1e178e72011-08-28 19:44:34 +0000667 Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000668{
cristy3ed852e2009-09-05 21:47:34 +0000669 MagickBooleanType
670 status;
671
672 MagickOffsetType
673 currentOffset,
674 offset,
675 scene;
676
677 MagickSizeType
678 cc;
679
cristy101ab702011-10-13 13:06:32 +0000680 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000681 transpix;
682
683 QuantizeInfo
684 *quantize_info;
685
cristybb503372010-05-27 20:51:26 +0000686 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000687 x;
688
cristy4c08aed2011-07-01 19:47:50 +0000689 register const Quantum
cristy3ed852e2009-09-05 21:47:34 +0000690 *p;
691
cristy4c08aed2011-07-01 19:47:50 +0000692 register Quantum
693 *q;
694
cristydd12db52014-11-30 21:54:11 +0000695 ssize_t
696 y;
697
cristyaff6d802011-04-26 01:46:31 +0000698 size_t
699 count,
700 bits_per_pixel,
701 bytes_per_row,
702 nextDepthOffset,
703 one;
704
cristy3ed852e2009-09-05 21:47:34 +0000705 unsigned char
706 bit,
707 byte,
708 color,
709 *lastrow,
710 *one_row,
711 *ptr,
712 version;
713
714 unsigned int
715 transparentIndex;
716
cristy3ed852e2009-09-05 21:47:34 +0000717 unsigned short
718 color16,
719 flags;
720
cristy3ed852e2009-09-05 21:47:34 +0000721 /*
722 Open output image file.
723 */
724 assert(image_info != (const ImageInfo *) NULL);
cristye1c94d92015-06-28 12:16:33 +0000725 assert(image_info->signature == MagickCoreSignature);
cristy3ed852e2009-09-05 21:47:34 +0000726 assert(image != (Image *) NULL);
cristye1c94d92015-06-28 12:16:33 +0000727 assert(image->signature == MagickCoreSignature);
cristy3ed852e2009-09-05 21:47:34 +0000728 if (image->debug != MagickFalse)
729 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy3a37efd2011-08-28 20:31:03 +0000730 assert(exception != (ExceptionInfo *) NULL);
cristye1c94d92015-06-28 12:16:33 +0000731 assert(exception->signature == MagickCoreSignature);
cristy1e178e72011-08-28 19:44:34 +0000732 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
cristy3ed852e2009-09-05 21:47:34 +0000733 if (status == MagickFalse)
734 return(status);
cristy3ed852e2009-09-05 21:47:34 +0000735 quantize_info=AcquireQuantizeInfo(image_info);
736 flags=0;
737 currentOffset=0;
738 transparentIndex=0;
cristydd12db52014-11-30 21:54:11 +0000739 transpix.red=0.0;
740 transpix.green=0.0;
741 transpix.blue=0.0;
742 transpix.alpha=0.0;
cristyeaedf062010-05-29 22:36:02 +0000743 one=1;
cristy3ed852e2009-09-05 21:47:34 +0000744 version=0;
745 scene=0;
746 do
747 {
cristyaf8d3912014-02-21 14:50:33 +0000748 (void) TransformImageColorspace(image,sRGBColorspace,exception);
cristy1e178e72011-08-28 19:44:34 +0000749 count=GetNumberColors(image,NULL,exception);
cristya7a9dbc2010-05-30 00:23:21 +0000750 for (bits_per_pixel=1; (one << bits_per_pixel) < count; bits_per_pixel*=2) ;
cristy3ed852e2009-09-05 21:47:34 +0000751 if (image_info->depth > 100)
752 bits_per_pixel=image_info->depth-100;
753 if (bits_per_pixel < 16)
cristye941a752011-10-15 01:52:48 +0000754 (void) TransformImageColorspace(image,image->colorspace,exception);
cristy3ed852e2009-09-05 21:47:34 +0000755 if (bits_per_pixel < 8)
756 {
cristye941a752011-10-15 01:52:48 +0000757 (void) TransformImageColorspace(image,GRAYColorspace,exception);
cristy018f07f2011-09-04 21:15:19 +0000758 (void) SetImageType(image,PaletteType,exception);
759 (void) SortColormapByIntensity(image,exception);
cristy3ed852e2009-09-05 21:47:34 +0000760 }
761 if ((image->storage_class == PseudoClass) && (image->colors > 256))
cristy1e178e72011-08-28 19:44:34 +0000762 (void) SetImageStorageClass(image,DirectClass,exception);
cristy3ed852e2009-09-05 21:47:34 +0000763 if (image->storage_class == PseudoClass)
764 flags|=PALM_HAS_COLORMAP_FLAG;
765 else
766 flags|=PALM_IS_DIRECT_COLOR;
767 (void) WriteBlobMSBShort(image,(unsigned short) image->columns); /* width */
768 (void) WriteBlobMSBShort(image,(unsigned short) image->rows); /* height */
769 bytes_per_row=((image->columns+(16/bits_per_pixel-1))/(16/
770 bits_per_pixel))*2;
771 (void) WriteBlobMSBShort(image,(unsigned short) bytes_per_row);
772 if ((image_info->compression == RLECompression) ||
773 (image_info->compression == FaxCompression))
774 flags|=PALM_IS_COMPRESSED_FLAG;
775 (void) WriteBlobMSBShort(image, flags);
776 (void) WriteBlobByte(image,(unsigned char) bits_per_pixel);
777 if (bits_per_pixel > 1)
778 version=1;
779 if ((image_info->compression == RLECompression) ||
780 (image_info->compression == FaxCompression))
781 version=2;
782 (void) WriteBlobByte(image,version);
783 (void) WriteBlobMSBShort(image,0); /* nextDepthOffset */
784 (void) WriteBlobByte(image,(unsigned char) transparentIndex);
785 if (image_info->compression == RLECompression)
786 (void) WriteBlobByte(image,PALM_COMPRESSION_RLE);
787 else
788 if (image_info->compression == FaxCompression)
789 (void) WriteBlobByte(image,PALM_COMPRESSION_SCANLINE);
790 else
791 (void) WriteBlobByte(image,PALM_COMPRESSION_NONE);
792 (void) WriteBlobMSBShort(image,0); /* reserved */
793 offset=16;
794 if (bits_per_pixel == 16)
795 {
796 (void) WriteBlobByte(image,5); /* # of bits of red */
797 (void) WriteBlobByte(image,6); /* # of bits of green */
798 (void) WriteBlobByte(image,5); /* # of bits of blue */
799 (void) WriteBlobByte(image,0); /* reserved by Palm */
800 (void) WriteBlobMSBLong(image,0); /* no transparent color, YET */
801 offset+=8;
802 }
803 if (bits_per_pixel == 8)
804 {
805 if (flags & PALM_HAS_COLORMAP_FLAG) /* Write out colormap */
806 {
dirkab4f0bb2015-07-25 11:46:32 +0000807 quantize_info->dither_method=IdentifyPaletteImage(image,exception)
cristycbda6112012-05-27 20:57:16 +0000808 == MagickFalse ? RiemersmaDitherMethod : NoDitherMethod;
cristy3ed852e2009-09-05 21:47:34 +0000809 quantize_info->number_colors=image->colors;
cristy018f07f2011-09-04 21:15:19 +0000810 (void) QuantizeImage(quantize_info,image,exception);
cristy3ed852e2009-09-05 21:47:34 +0000811 (void) WriteBlobMSBShort(image,(unsigned short) image->colors);
812 for (count = 0; count < image->colors; count++)
813 {
814 (void) WriteBlobByte(image,(unsigned char) count);
cristydd12db52014-11-30 21:54:11 +0000815 (void) WriteBlobByte(image,ScaleQuantumToChar(ClampToQuantum(
816 image->colormap[count].red)));
817 (void) WriteBlobByte(image,ScaleQuantumToChar(ClampToQuantum(
818 image->colormap[count].green)));
819 (void) WriteBlobByte(image,ScaleQuantumToChar(ClampToQuantum(
820 image->colormap[count].blue)));
cristy3ed852e2009-09-05 21:47:34 +0000821 }
822 offset+=2+count*4;
823 }
824 else /* Map colors to Palm standard colormap */
825 {
826 Image
827 *affinity_image;
828
829 affinity_image=ConstituteImage(256,1,"RGB",CharPixel,&PalmPalette,
cristy1e178e72011-08-28 19:44:34 +0000830 exception);
cristyaff6d802011-04-26 01:46:31 +0000831 (void) TransformImageColorspace(affinity_image,
cristye941a752011-10-15 01:52:48 +0000832 affinity_image->colorspace,exception);
cristy018f07f2011-09-04 21:15:19 +0000833 (void) RemapImage(quantize_info,image,affinity_image,exception);
cristybb503372010-05-27 20:51:26 +0000834 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000835 {
cristy1e178e72011-08-28 19:44:34 +0000836 q=GetAuthenticPixels(image,0,y,image->columns,1,exception);
dirkccad00b2015-06-21 16:58:22 +0000837 if (q == (Quantum *) NULL)
cristyc0505a02015-06-21 20:55:32 +0000838 break;
cristybb503372010-05-27 20:51:26 +0000839 for (x=0; x < (ssize_t) image->columns; x++)
cristy4c08aed2011-07-01 19:47:50 +0000840 {
cristydd12db52014-11-30 21:54:11 +0000841 SetPixelIndex(image,(Quantum) FindColor(&image->colormap[(ssize_t)
cristy4c08aed2011-07-01 19:47:50 +0000842 GetPixelIndex(image,q)]),q);
cristyed231572011-07-14 02:18:59 +0000843 q+=GetPixelChannels(image);
cristy4c08aed2011-07-01 19:47:50 +0000844 }
cristy3ed852e2009-09-05 21:47:34 +0000845 }
846 affinity_image=DestroyImage(affinity_image);
847 }
848 }
849 if (flags & PALM_IS_COMPRESSED_FLAG)
850 (void) WriteBlobMSBShort(image,0); /* fill in size later */
851 lastrow=(unsigned char *) NULL;
852 if (image_info->compression == FaxCompression)
853 lastrow=(unsigned char *) AcquireQuantumMemory(bytes_per_row,
854 sizeof(*lastrow));
855 /* TODO check whether memory really was acquired? */
856 one_row=(unsigned char *) AcquireQuantumMemory(bytes_per_row,
857 sizeof(*one_row));
858 if (one_row == (unsigned char *) NULL)
859 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
cristydd12db52014-11-30 21:54:11 +0000860 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000861 {
862 ptr=one_row;
863 (void) ResetMagickMemory(ptr,0,bytes_per_row);
cristy1e178e72011-08-28 19:44:34 +0000864 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +0000865 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000866 break;
cristy3ed852e2009-09-05 21:47:34 +0000867 if (bits_per_pixel == 16)
868 {
cristydd12db52014-11-30 21:54:11 +0000869 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000870 {
cristy4c08aed2011-07-01 19:47:50 +0000871 color16=(unsigned short) ((((31*(size_t) GetPixelRed(image,p))/
872 (size_t) QuantumRange) << 11) | (((63*(size_t)
873 GetPixelGreen(image,p))/(size_t) QuantumRange) << 5) |
874 ((31*(size_t) GetPixelBlue(image,p))/(size_t) QuantumRange));
875 if (GetPixelAlpha(image,p) == (Quantum) TransparentAlpha)
cristy3ed852e2009-09-05 21:47:34 +0000876 {
cristydd12db52014-11-30 21:54:11 +0000877 transpix.red=(MagickRealType) GetPixelRed(image,p);
878 transpix.green=(MagickRealType) GetPixelGreen(image,p);
879 transpix.blue=(MagickRealType) GetPixelBlue(image,p);
880 transpix.alpha=(MagickRealType) GetPixelAlpha(image,p);
cristy3ed852e2009-09-05 21:47:34 +0000881 flags|=PALM_HAS_TRANSPARENCY_FLAG;
882 }
883 *ptr++=(unsigned char) ((color16 >> 8) & 0xff);
884 *ptr++=(unsigned char) (color16 & 0xff);
cristyed231572011-07-14 02:18:59 +0000885 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000886 }
887 }
888 else
889 {
890 byte=0x00;
891 bit=(unsigned char) (8-bits_per_pixel);
cristydd12db52014-11-30 21:54:11 +0000892 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000893 {
894 if (bits_per_pixel >= 8)
cristy4c08aed2011-07-01 19:47:50 +0000895 color=(unsigned char) GetPixelIndex(image,p);
cristy3ed852e2009-09-05 21:47:34 +0000896 else
cristy4c08aed2011-07-01 19:47:50 +0000897 color=(unsigned char) (GetPixelIndex(image,p)*
cristyaff6d802011-04-26 01:46:31 +0000898 ((one << bits_per_pixel)-1)/MagickMax(1*image->colors-1,1));
cristy3ed852e2009-09-05 21:47:34 +0000899 byte|=color << bit;
900 if (bit != 0)
cristybefe4d22010-06-07 01:18:58 +0000901 bit-=(unsigned char) bits_per_pixel;
cristy3ed852e2009-09-05 21:47:34 +0000902 else
903 {
904 *ptr++=byte;
905 byte=0x00;
906 bit=(unsigned char) (8-bits_per_pixel);
907 }
cristyed231572011-07-14 02:18:59 +0000908 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000909 }
910 if ((image->columns % (8/bits_per_pixel)) != 0)
911 *ptr++=byte;
912 }
913 if (image_info->compression == RLECompression)
914 {
915 x=0;
cristybb503372010-05-27 20:51:26 +0000916 while (x < (ssize_t) bytes_per_row)
cristy3ed852e2009-09-05 21:47:34 +0000917 {
918 byte=one_row[x];
919 count=1;
920 while ((one_row[++x] == byte) && (count < 255) &&
cristybb503372010-05-27 20:51:26 +0000921 (x < (ssize_t) bytes_per_row))
cristy3ed852e2009-09-05 21:47:34 +0000922 count++;
923 (void) WriteBlobByte(image,(unsigned char) count);
924 (void) WriteBlobByte(image,(unsigned char) byte);
925 }
926 }
927 else
928 if (image_info->compression == FaxCompression)
929 {
930 char
931 tmpbuf[8],
932 *tptr;
933
cristybb503372010-05-27 20:51:26 +0000934 for (x = 0; x < (ssize_t) bytes_per_row; x += 8)
cristy3ed852e2009-09-05 21:47:34 +0000935 {
936 tptr = tmpbuf;
937 for (bit=0, byte=0; bit < (unsigned char) MagickMin(8,(ssize_t) bytes_per_row-x); bit++)
938 {
939 if ((y == 0) || (lastrow[x + bit] != one_row[x + bit]))
940 {
cristyaff6d802011-04-26 01:46:31 +0000941 byte |= (1 << (7 - bit));
942 *tptr++ = (char) one_row[x + bit];
943 }
cristy3ed852e2009-09-05 21:47:34 +0000944 }
945 (void) WriteBlobByte(image, byte);
946 (void) WriteBlob(image,tptr-tmpbuf,(unsigned char *) tmpbuf);
947 }
948 (void) CopyMagickMemory(lastrow,one_row,bytes_per_row);
949 }
950 else
951 (void) WriteBlob(image,bytes_per_row,one_row);
952 }
953 if (flags & PALM_HAS_TRANSPARENCY_FLAG)
954 {
955 offset=SeekBlob(image,currentOffset+6,SEEK_SET);
956 (void) WriteBlobMSBShort(image,flags);
957 offset=SeekBlob(image,currentOffset+12,SEEK_SET);
958 (void) WriteBlobByte(image,(unsigned char) transparentIndex); /* trans index */
959 }
960 if (bits_per_pixel == 16)
961 {
962 offset=SeekBlob(image,currentOffset+20,SEEK_SET);
963 (void) WriteBlobByte(image,0); /* reserved by Palm */
964 (void) WriteBlobByte(image,(unsigned char) ((31*transpix.red)/QuantumRange));
965 (void) WriteBlobByte(image,(unsigned char) ((63*transpix.green)/QuantumRange));
966 (void) WriteBlobByte(image,(unsigned char) ((31*transpix.blue)/QuantumRange));
967 }
968 if (flags & PALM_IS_COMPRESSED_FLAG) /* fill in size now */
969 {
970 offset=SeekBlob(image,currentOffset+offset,SEEK_SET);
971 (void) WriteBlobMSBShort(image,(unsigned short) (GetBlobSize(image)-
972 currentOffset-offset));
973 }
974 if (one_row != (unsigned char *) NULL)
975 one_row=(unsigned char *) RelinquishMagickMemory(one_row);
976 if (lastrow != (unsigned char *) NULL)
977 lastrow=(unsigned char *) RelinquishMagickMemory(lastrow);
978 if (GetNextImageInList(image) == (Image *) NULL)
979 break;
980 /* padding to 4 byte word */
cristy64b17a62015-02-15 13:22:11 +0000981 for (cc=(GetBlobSize(image)) % 4; cc > 0; cc--)
cristy3ed852e2009-09-05 21:47:34 +0000982 (void) WriteBlobByte(image,0);
cristy3ed852e2009-09-05 21:47:34 +0000983 /* write nextDepthOffset and return to end of image */
984 offset=SeekBlob(image,currentOffset+10,SEEK_SET);
cristybb503372010-05-27 20:51:26 +0000985 nextDepthOffset=(size_t) ((GetBlobSize(image)-currentOffset)/4);
cristy3ed852e2009-09-05 21:47:34 +0000986 (void) WriteBlobMSBShort(image,(unsigned short) nextDepthOffset);
987 currentOffset=(MagickOffsetType) GetBlobSize(image);
988 offset=SeekBlob(image,currentOffset,SEEK_SET);
989 image=SyncNextImageInList(image);
990 status=SetImageProgress(image,SaveImagesTag,scene++,
991 GetImageListLength(image));
992 if (status == MagickFalse)
993 break;
994 } while (image_info->adjoin != MagickFalse);
995 quantize_info=DestroyQuantizeInfo(quantize_info);
996 (void) CloseBlob(image);
cristy3ed852e2009-09-05 21:47:34 +0000997 return(MagickTrue);
998}