blob: 8baf5412e6b3cd2a34aba5b0f1680dc4c190522b [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"
45#include "MagickCore/blob.h"
46#include "MagickCore/blob-private.h"
47#include "MagickCore/cache.h"
48#include "MagickCore/color.h"
49#include "MagickCore/colormap.h"
50#include "MagickCore/colormap-private.h"
51#include "MagickCore/color-private.h"
52#include "MagickCore/colorspace.h"
cristy510d06a2011-07-06 23:43:54 +000053#include "MagickCore/colorspace-private.h"
cristy4c08aed2011-07-01 19:47:50 +000054#include "MagickCore/constitute.h"
55#include "MagickCore/exception.h"
56#include "MagickCore/histogram.h"
57#include "MagickCore/image.h"
58#include "MagickCore/image-private.h"
59#include "MagickCore/list.h"
60#include "MagickCore/magick.h"
61#include "MagickCore/memory_.h"
62#include "MagickCore/monitor.h"
63#include "MagickCore/monitor-private.h"
64#include "MagickCore/paint.h"
65#include "MagickCore/pixel-accessor.h"
66#include "MagickCore/property.h"
67#include "MagickCore/quantize.h"
68#include "MagickCore/quantum-private.h"
69#include "MagickCore/static.h"
70#include "MagickCore/string_.h"
71#include "MagickCore/module.h"
72#include "MagickCore/utility.h"
cristy3ed852e2009-09-05 21:47:34 +000073
74/*
75 Define declarations.
76*/
77#define PALM_IS_COMPRESSED_FLAG 0x8000
78#define PALM_HAS_COLORMAP_FLAG 0x4000
79#define PALM_HAS_FOUR_BYTE_FIELD 0x0200
80#define PALM_HAS_TRANSPARENCY_FLAG 0x2000
81#define PALM_IS_INDIRECT 0x1000
82#define PALM_IS_FOR_SCREEN 0x0800
83#define PALM_IS_DIRECT_COLOR 0x0400
84#define PALM_COMPRESSION_SCANLINE 0x00
85#define PALM_COMPRESSION_RLE 0x01
86#define PALM_COMPRESSION_NONE 0xFF
87
88/*
89 The 256 color system palette for Palm Computing Devices.
90*/
91static unsigned char
92 PalmPalette[256][3] =
93 {
94 {255, 255,255}, {255, 204,255}, {255, 153,255}, {255, 102,255},
95 {255, 51,255}, {255, 0,255}, {255, 255,204}, {255, 204,204},
96 {255, 153,204}, {255, 102,204}, {255, 51,204}, {255, 0,204},
97 {255, 255,153}, {255, 204,153}, {255, 153,153}, {255, 102,153},
98 {255, 51,153}, {255, 0,153}, {204, 255,255}, {204, 204,255},
99 {204, 153,255}, {204, 102,255}, {204, 51,255}, {204, 0,255},
100 {204, 255,204}, {204, 204,204}, {204, 153,204}, {204, 102,204},
101 {204, 51,204}, {204, 0,204}, {204, 255,153}, {204, 204,153},
102 {204, 153,153}, {204, 102,153}, {204, 51,153}, {204, 0,153},
103 {153, 255,255}, {153, 204,255}, {153, 153,255}, {153, 102,255},
104 {153, 51,255}, {153, 0,255}, {153, 255,204}, {153, 204,204},
105 {153, 153,204}, {153, 102,204}, {153, 51,204}, {153, 0,204},
106 {153, 255,153}, {153, 204,153}, {153, 153,153}, {153, 102,153},
107 {153, 51,153}, {153, 0,153}, {102, 255,255}, {102, 204,255},
108 {102, 153,255}, {102, 102,255}, {102, 51,255}, {102, 0,255},
109 {102, 255,204}, {102, 204,204}, {102, 153,204}, {102, 102,204},
110 {102, 51,204}, {102, 0,204}, {102, 255,153}, {102, 204,153},
111 {102, 153,153}, {102, 102,153}, {102, 51,153}, {102, 0,153},
112 { 51, 255,255}, { 51, 204,255}, { 51, 153,255}, { 51, 102,255},
113 { 51, 51,255}, { 51, 0,255}, { 51, 255,204}, { 51, 204,204},
114 { 51, 153,204}, { 51, 102,204}, { 51, 51,204}, { 51, 0,204},
115 { 51, 255,153}, { 51, 204,153}, { 51, 153,153}, { 51, 102,153},
116 { 51, 51,153}, { 51, 0,153}, { 0, 255,255}, { 0, 204,255},
117 { 0, 153,255}, { 0, 102,255}, { 0, 51,255}, { 0, 0,255},
118 { 0, 255,204}, { 0, 204,204}, { 0, 153,204}, { 0, 102,204},
119 { 0, 51,204}, { 0, 0,204}, { 0, 255,153}, { 0, 204,153},
120 { 0, 153,153}, { 0, 102,153}, { 0, 51,153}, { 0, 0,153},
121 {255, 255,102}, {255, 204,102}, {255, 153,102}, {255, 102,102},
122 {255, 51,102}, {255, 0,102}, {255, 255, 51}, {255, 204, 51},
123 {255, 153, 51}, {255, 102, 51}, {255, 51, 51}, {255, 0, 51},
124 {255, 255, 0}, {255, 204, 0}, {255, 153, 0}, {255, 102, 0},
125 {255, 51, 0}, {255, 0, 0}, {204, 255,102}, {204, 204,102},
126 {204, 153,102}, {204, 102,102}, {204, 51,102}, {204, 0,102},
127 {204, 255, 51}, {204, 204, 51}, {204, 153, 51}, {204, 102, 51},
128 {204, 51, 51}, {204, 0, 51}, {204, 255, 0}, {204, 204, 0},
129 {204, 153, 0}, {204, 102, 0}, {204, 51, 0}, {204, 0, 0},
130 {153, 255,102}, {153, 204,102}, {153, 153,102}, {153, 102,102},
131 {153, 51,102}, {153, 0,102}, {153, 255, 51}, {153, 204, 51},
132 {153, 153, 51}, {153, 102, 51}, {153, 51, 51}, {153, 0, 51},
133 {153, 255, 0}, {153, 204, 0}, {153, 153, 0}, {153, 102, 0},
134 {153, 51, 0}, {153, 0, 0}, {102, 255,102}, {102, 204,102},
135 {102, 153,102}, {102, 102,102}, {102, 51,102}, {102, 0,102},
136 {102, 255, 51}, {102, 204, 51}, {102, 153, 51}, {102, 102, 51},
137 {102, 51, 51}, {102, 0, 51}, {102, 255, 0}, {102, 204, 0},
138 {102, 153, 0}, {102, 102, 0}, {102, 51, 0}, {102, 0, 0},
139 { 51, 255,102}, { 51, 204,102}, { 51, 153,102}, { 51, 102,102},
140 { 51, 51,102}, { 51, 0,102}, { 51, 255, 51}, { 51, 204, 51},
141 { 51, 153, 51}, { 51, 102, 51}, { 51, 51, 51}, { 51, 0, 51},
142 { 51, 255, 0}, { 51, 204, 0}, { 51, 153, 0}, { 51, 102, 0},
143 { 51, 51, 0}, { 51, 0, 0}, { 0, 255,102}, { 0, 204,102},
144 { 0, 153,102}, { 0, 102,102}, { 0, 51,102}, { 0, 0,102},
145 { 0, 255, 51}, { 0, 204, 51}, { 0, 153, 51}, { 0, 102, 51},
146 { 0, 51, 51}, { 0, 0, 51}, { 0, 255, 0}, { 0, 204, 0},
147 { 0, 153, 0}, { 0, 102, 0}, { 0, 51, 0}, { 17, 17, 17},
148 { 34, 34, 34}, { 68, 68, 68}, { 85, 85, 85}, {119, 119,119},
149 {136, 136,136}, {170, 170,170}, {187, 187,187}, {221, 221,221},
150 {238, 238,238}, {192, 192,192}, {128, 0, 0}, {128, 0,128},
151 { 0, 128, 0}, { 0, 128,128}, { 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 { 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 };
159
160/*
161 Forward declarations.
162*/
163static MagickBooleanType
cristy1e178e72011-08-28 19:44:34 +0000164 WritePALMImage(const ImageInfo *,Image *,ExceptionInfo *);
cristy3ed852e2009-09-05 21:47:34 +0000165
166/*
167%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
168% %
169% %
170% %
171% F i n d C o l o r %
172% %
173% %
174% %
175%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
176%
177% FindColor() returns the index of the matching entry from PalmPalette for a
cristy101ab702011-10-13 13:06:32 +0000178% given PixelInfo.
cristy3ed852e2009-09-05 21:47:34 +0000179%
180% The format of the FindColor method is:
181%
cristy101ab702011-10-13 13:06:32 +0000182% int FindColor(const Image *image,PixelInfo *pixel)
cristy3ed852e2009-09-05 21:47:34 +0000183%
184% A description of each parameter follows:
185%
186% o int: the index of the matching color or -1 if not found/
187%
cristy101ab702011-10-13 13:06:32 +0000188% o pixel: a pointer to the PixelInfo to be matched.
cristy3ed852e2009-09-05 21:47:34 +0000189%
190*/
cristy101ab702011-10-13 13:06:32 +0000191static int FindColor(const Image *image,PixelInfo *packet)
cristy3ed852e2009-09-05 21:47:34 +0000192{
cristybb503372010-05-27 20:51:26 +0000193 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000194 i;
195
196 for (i=0; i < 256; i++)
cristy4c08aed2011-07-01 19:47:50 +0000197 if (ScaleQuantumToChar(packet->red) == PalmPalette[i][0] &&
198 ScaleQuantumToChar(packet->green) == PalmPalette[i][1] &&
199 ScaleQuantumToChar(packet->blue) == PalmPalette[i][2])
cristy3ed852e2009-09-05 21:47:34 +0000200 return(i);
201 return(-1);
202}
203
204/*
205%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
206% %
207% %
208% %
209% R e a d P A L M I m a g e %
210% %
211% %
212% %
213%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
214%
215% ReadPALMImage() reads an image of raw bites in LSB order and returns it. It
216% allocates the memory necessary for the new Image structure and returns a
217% pointer to the new image.
218%
219% The format of the ReadPALMImage method is:
220%
221% Image *ReadPALMImage(const ImageInfo *image_info,
222% ExceptionInfo *exception)
223%
224% A description of each parameter follows:
225%
226% o image_info: Specifies a pointer to an ImageInfo structure.
227%
228% o exception: return any errors or warnings in this structure.
229%
230*/
231
cristy35ef8242010-06-03 16:24:13 +0000232static inline size_t MagickMax(const size_t x,const size_t y)
cristy3ed852e2009-09-05 21:47:34 +0000233{
234 if (x > y)
235 return(x);
236 return(y);
237}
238
239static inline ssize_t MagickMin(const ssize_t x,const ssize_t y)
240{
241 if (x < y)
242 return(x);
243 return(y);
244}
245
246static Image *ReadPALMImage(const ImageInfo *image_info,
247 ExceptionInfo *exception)
248{
249 Image
250 *image;
251
cristy3ed852e2009-09-05 21:47:34 +0000252 MagickBooleanType
253 status;
254
cristyeaedf062010-05-29 22:36:02 +0000255 MagickOffsetType
256 totalOffset,
257 seekNextDepth;
258
cristy4c08aed2011-07-01 19:47:50 +0000259 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000260 transpix;
261
cristy4c08aed2011-07-01 19:47:50 +0000262 Quantum
263 index;
cristy3ed852e2009-09-05 21:47:34 +0000264
cristybb503372010-05-27 20:51:26 +0000265 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000266 i,
267 x;
268
cristy4c08aed2011-07-01 19:47:50 +0000269 register Quantum
cristy3ed852e2009-09-05 21:47:34 +0000270 *q;
271
272 ssize_t
cristyaff6d802011-04-26 01:46:31 +0000273 count,
274 y;
cristy3ed852e2009-09-05 21:47:34 +0000275
cristybb503372010-05-27 20:51:26 +0000276 size_t
cristy3ed852e2009-09-05 21:47:34 +0000277 bytes_per_row,
278 flags,
279 bits_per_pixel,
280 version,
281 nextDepthOffset,
282 transparentIndex,
283 compressionType,
284 byte,
285 mask,
286 redbits,
287 greenbits,
288 bluebits,
cristyeaedf062010-05-29 22:36:02 +0000289 one,
cristy3ed852e2009-09-05 21:47:34 +0000290 pad,
291 size,
292 bit;
293
cristyaff6d802011-04-26 01:46:31 +0000294 unsigned char
295 *lastrow,
296 *one_row,
297 *ptr;
298
cristy3ed852e2009-09-05 21:47:34 +0000299 unsigned short
300 color16;
301
cristy3ed852e2009-09-05 21:47:34 +0000302 /*
303 Open image file.
304 */
305 assert(image_info != (const ImageInfo *) NULL);
306 assert(image_info->signature == MagickSignature);
307 if (image_info->debug != MagickFalse)
308 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
309 image_info->filename);
310 assert(exception != (ExceptionInfo *) NULL);
311 assert(exception->signature == MagickSignature);
cristy9950d572011-10-01 18:22:35 +0000312 image=AcquireImage(image_info,exception);
cristy3ed852e2009-09-05 21:47:34 +0000313 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
314 if (status == MagickFalse)
315 {
316 (void) DestroyImageList(image);
317 return((Image *) NULL);
318 }
319 totalOffset=0;
320 do
321 {
322 image->columns=ReadBlobMSBShort(image);
323 image->rows=ReadBlobMSBShort(image);
324 if (EOFBlob(image) != MagickFalse)
325 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
cristy3ed852e2009-09-05 21:47:34 +0000326 if ((image->columns == 0) || (image->rows == 0))
327 ThrowReaderException(CorruptImageError,"NegativeOrZeroImageSize");
328 bytes_per_row=ReadBlobMSBShort(image);
329 flags=ReadBlobMSBShort(image);
cristybb503372010-05-27 20:51:26 +0000330 bits_per_pixel=(size_t) ReadBlobByte(image);
cristy3ed852e2009-09-05 21:47:34 +0000331 if (bits_per_pixel > 16)
332 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
cristybb503372010-05-27 20:51:26 +0000333 version=(size_t) ReadBlobByte(image);
cristyda16f162011-02-19 23:52:17 +0000334 (void) version;
cristybb503372010-05-27 20:51:26 +0000335 nextDepthOffset=(size_t) ReadBlobMSBShort(image);
336 transparentIndex=(size_t) ReadBlobByte(image);
337 compressionType=(size_t) ReadBlobByte(image);
cristy3ed852e2009-09-05 21:47:34 +0000338 pad=ReadBlobMSBShort(image);
cristyda16f162011-02-19 23:52:17 +0000339 (void) pad;
cristy3ed852e2009-09-05 21:47:34 +0000340 /*
341 Initialize image colormap.
342 */
cristyeaedf062010-05-29 22:36:02 +0000343 one=1;
cristy3ed852e2009-09-05 21:47:34 +0000344 if ((bits_per_pixel < 16) &&
cristy018f07f2011-09-04 21:15:19 +0000345 (AcquireImageColormap(image,one << bits_per_pixel,exception) == MagickFalse))
cristy3ed852e2009-09-05 21:47:34 +0000346 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
cristy4c08aed2011-07-01 19:47:50 +0000347 GetPixelInfo(image,&transpix);
cristy3ed852e2009-09-05 21:47:34 +0000348 if (bits_per_pixel == 16) /* Direct Color */
349 {
cristybb503372010-05-27 20:51:26 +0000350 redbits=(size_t) ReadBlobByte(image); /* # of bits of red */
cristyda16f162011-02-19 23:52:17 +0000351 (void) redbits;
cristybb503372010-05-27 20:51:26 +0000352 greenbits=(size_t) ReadBlobByte(image); /* # of bits of green */
cristyda16f162011-02-19 23:52:17 +0000353 (void) greenbits;
cristybb503372010-05-27 20:51:26 +0000354 bluebits=(size_t) ReadBlobByte(image); /* # of bits of blue */
cristyda16f162011-02-19 23:52:17 +0000355 (void) bluebits;
cristy3ed852e2009-09-05 21:47:34 +0000356 ReadBlobByte(image); /* reserved by Palm */
357 ReadBlobByte(image); /* reserved by Palm */
358 transpix.red=(MagickRealType) (QuantumRange*ReadBlobByte(image)/31);
359 transpix.green=(MagickRealType) (QuantumRange*ReadBlobByte(image)/63);
360 transpix.blue=(MagickRealType) (QuantumRange*ReadBlobByte(image)/31);
361 }
362 if (bits_per_pixel == 8)
363 {
cristy4c08aed2011-07-01 19:47:50 +0000364 Quantum
cristy3ed852e2009-09-05 21:47:34 +0000365 index;
366
367 if (flags & PALM_HAS_COLORMAP_FLAG)
368 {
369 count=(ssize_t) ReadBlobMSBShort(image);
cristybb503372010-05-27 20:51:26 +0000370 for (i=0; i < (ssize_t) count; i++)
cristy3ed852e2009-09-05 21:47:34 +0000371 {
372 ReadBlobByte(image);
cristyc82a27b2011-10-21 01:07:16 +0000373 index=ConstrainColormapIndex(image,255-i,exception);
cristy3ed852e2009-09-05 21:47:34 +0000374 image->colormap[(int) index].red=
375 ScaleCharToQuantum((unsigned char) ReadBlobByte(image));
376 image->colormap[(int) index].green=
377 ScaleCharToQuantum((unsigned char) ReadBlobByte(image));
378 image->colormap[(int) index].blue=
379 ScaleCharToQuantum((unsigned char) ReadBlobByte(image));
380 }
381 }
382 else
383 {
cristybb503372010-05-27 20:51:26 +0000384 for (i=0; i < (ssize_t) (1L << bits_per_pixel); i++)
cristy3ed852e2009-09-05 21:47:34 +0000385 {
cristyc82a27b2011-10-21 01:07:16 +0000386 index=ConstrainColormapIndex(image,255-i,exception);
cristy3ed852e2009-09-05 21:47:34 +0000387 image->colormap[(int) index].red=
388 ScaleCharToQuantum(PalmPalette[i][0]);
389 image->colormap[(int) index].green=
390 ScaleCharToQuantum(PalmPalette[i][1]);
391 image->colormap[(int) index].blue=
392 ScaleCharToQuantum(PalmPalette[i][2]);
393 }
394 }
395 }
396 if (flags & PALM_IS_COMPRESSED_FLAG)
397 size=ReadBlobMSBShort(image);
cristyda16f162011-02-19 23:52:17 +0000398 (void) size;
cristy3ed852e2009-09-05 21:47:34 +0000399 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;
cristybb503372010-05-27 20:51:26 +0000417 for (y = 0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000418 {
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;
cristybb503372010-05-27 20:51:26 +0000431 for (i=0; i < (ssize_t) bytes_per_row; )
cristy3ed852e2009-09-05 21:47:34 +0000432 {
433 count=(ssize_t) ReadBlobByte(image);
434 count=MagickMin(count,(ssize_t) bytes_per_row-i);
cristybb503372010-05-27 20:51:26 +0000435 byte=(size_t) ReadBlobByte(image);
cristy3ed852e2009-09-05 21:47:34 +0000436 (void) ResetMagickMemory(one_row+i,(int) byte,(size_t) count);
437 i+=count;
438 }
439 }
440 else
441 if (compressionType == PALM_COMPRESSION_SCANLINE)
cristyf9cca6a2010-06-04 23:49:28 +0000442 {
443 size_t
444 one;
445
cristy3ed852e2009-09-05 21:47:34 +0000446 /* TODO move out of loop! */
cristyf9cca6a2010-06-04 23:49:28 +0000447 one=1;
cristy3ed852e2009-09-05 21:47:34 +0000448 image->compression=FaxCompression;
cristybb503372010-05-27 20:51:26 +0000449 for (i=0; i < (ssize_t) bytes_per_row; i+=8)
cristy3ed852e2009-09-05 21:47:34 +0000450 {
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 {
cristyf9cca6a2010-06-04 23:49:28 +0000455 if ((y == 0) || (count & (one << (7 - bit))))
cristy3ed852e2009-09-05 21:47:34 +0000456 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);
cristyacd2ed22011-08-30 01:44:23 +0000466 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000467 break;
cristy3ed852e2009-09-05 21:47:34 +0000468 if (bits_per_pixel == 16)
469 {
470 if (image->columns > (2*bytes_per_row))
471 ThrowReaderException(CorruptImageError,"CorruptImage");
cristybb503372010-05-27 20:51:26 +0000472 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000473 {
474 color16=(*ptr++ << 8);
475 color16|=(*ptr++);
cristy4c08aed2011-07-01 19:47:50 +0000476 SetPixelRed(image,(QuantumRange*((color16 >> 11) & 0x1f))/0x1f,q);
477 SetPixelGreen(image,(QuantumRange*((color16 >> 5) & 0x3f))/0x3f,q);
478 SetPixelBlue(image,(QuantumRange*((color16 >> 0) & 0x1f))/0x1f,q);
479 SetPixelAlpha(image,OpaqueAlpha,q);
cristyed231572011-07-14 02:18:59 +0000480 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000481 }
482 }
483 else
484 {
485 bit=8-bits_per_pixel;
cristybb503372010-05-27 20:51:26 +0000486 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000487 {
488 if ((size_t) (ptr-one_row) >= bytes_per_row)
489 ThrowReaderException(CorruptImageError,"CorruptImage");
cristy4c08aed2011-07-01 19:47:50 +0000490 index=(Quantum) (mask-(((*ptr) & (mask << bit)) >> bit));
491 SetPixelIndex(image,index,q);
cristy101ab702011-10-13 13:06:32 +0000492 SetPixelPixelInfo(image,image->colormap+(ssize_t) index,q);
cristy3ed852e2009-09-05 21:47:34 +0000493 if (bit)
494 bit-=bits_per_pixel;
495 else
496 {
497 ptr++;
498 bit=8-bits_per_pixel;
499 }
cristyed231572011-07-14 02:18:59 +0000500 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000501 }
502 if (SyncAuthenticPixels(image,exception) == MagickFalse)
503 break;
504 }
505 if (image->previous == (Image *) NULL)
506 {
cristycee97112010-05-28 00:44:52 +0000507 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
cristyaff6d802011-04-26 01:46:31 +0000508 image->rows);
cristy3ed852e2009-09-05 21:47:34 +0000509 if (status == MagickFalse)
510 break;
511 }
512 }
513 if (flags & PALM_HAS_TRANSPARENCY_FLAG)
514 {
515 if (bits_per_pixel != 16)
cristyce1406f2011-10-25 11:37:00 +0000516 transpix=image->colormap[mask-transparentIndex];
cristy189e84c2011-08-27 18:08:53 +0000517 (void) TransparentPaintImage(image,&transpix,(Quantum) TransparentAlpha,
518 MagickFalse,exception);
cristy3ed852e2009-09-05 21:47:34 +0000519 }
520 one_row=(unsigned char *) RelinquishMagickMemory(one_row);
521 if (compressionType == PALM_COMPRESSION_SCANLINE)
cristy189e84c2011-08-27 18:08:53 +0000522 lastrow=(unsigned char *) RelinquishMagickMemory(lastrow);
cristy3ed852e2009-09-05 21:47:34 +0000523 /*
524 Proceed to next image. Copied from coders/pnm.c
525 */
526 if (image_info->number_scenes != 0)
527 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
528 break;
529 if (nextDepthOffset != 0)
530 {
531 /*
532 Skip to next image.
533 */
534 totalOffset+=(MagickOffsetType) (nextDepthOffset*4);
535 if (totalOffset >= (MagickOffsetType) GetBlobSize(image))
536 {
537 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
538 }
539 else
540 {
541 seekNextDepth=SeekBlob(image,totalOffset,SEEK_SET);
542 }
543 if (seekNextDepth != totalOffset)
544 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
545 /*
546 Allocate next image structure. Copied from coders/pnm.c
547 */
cristy9950d572011-10-01 18:22:35 +0000548 AcquireNextImage(image_info,image,exception);
cristy3ed852e2009-09-05 21:47:34 +0000549 if (GetNextImageInList(image) == (Image *) NULL)
550 {
551 (void) DestroyImageList(image);
552 return((Image *) NULL);
553 }
554 image=SyncNextImageInList(image);
555 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
556 GetBlobSize(image));
557 if (status == MagickFalse)
558 break;
559 }
560 } while (nextDepthOffset != 0);
561 (void) CloseBlob(image);
562 return(GetFirstImageInList(image));
563}
564
565/*
566%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
567% %
568% %
569% %
570% R e g i s t e r P A L M I m a g e %
571% %
572% %
573% %
574%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
575%
576% RegisterPALMImage() adds properties for the PALM image format to the list of
577% supported formats. The properties include the image format tag, a method to
578% read and/or write the format, whether the format supports the saving of more
579% than one frame to the same file or blob, whether the format supports native
580% in-memory I/O, and a brief description of the format.
581%
582% The format of the RegisterPALMImage method is:
583%
cristybb503372010-05-27 20:51:26 +0000584% size_t RegisterPALMImage(void)
cristy3ed852e2009-09-05 21:47:34 +0000585%
586*/
cristybb503372010-05-27 20:51:26 +0000587ModuleExport size_t RegisterPALMImage(void)
cristy3ed852e2009-09-05 21:47:34 +0000588{
589 MagickInfo
590 *entry;
591
592 entry=SetMagickInfo("PALM");
593 entry->decoder=(DecodeImageHandler *) ReadPALMImage;
594 entry->encoder=(EncodeImageHandler *) WritePALMImage;
595 entry->seekable_stream=MagickTrue;
596 entry->description=ConstantString("Palm pixmap");
597 entry->module=ConstantString("PALM");
598 (void) RegisterMagickInfo(entry);
599 return(MagickImageCoderSignature);
600}
601
602/*
603%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
604% %
605% %
606% %
607% U n r e g i s t e r P A L M I m a g e %
608% %
609% %
610% %
611%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
612%
613% UnregisterPALMImage() removes format registrations made by the PALM
614% module from the list of supported formats.
615%
616% The format of the UnregisterPALMImage method is:
617%
618% UnregisterPALMImage(void)
619%
620*/
621ModuleExport void UnregisterPALMImage(void)
622{
623 (void) UnregisterMagickInfo("PALM");
624}
625
626/*
627%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
628% %
629% %
630% %
631% W r i t e P A L M I m a g e %
632% %
633% %
634% %
635%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
636%
637% WritePALMImage() writes an image of raw bits in LSB order to a file.
638%
639% The format of the WritePALMImage method is:
640%
641% MagickBooleanType WritePALMImage(const ImageInfo *image_info,
cristy1e178e72011-08-28 19:44:34 +0000642% Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000643%
644% A description of each parameter follows.
645%
646% o image_info: Specifies a pointer to an ImageInfo structure.
647%
648% o image: A pointer to a Image structure.
649%
cristy1e178e72011-08-28 19:44:34 +0000650% o exception: return any errors or warnings in this structure.
651%
cristy3ed852e2009-09-05 21:47:34 +0000652*/
653static MagickBooleanType WritePALMImage(const ImageInfo *image_info,
cristy1e178e72011-08-28 19:44:34 +0000654 Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000655{
656 int
657 y;
658
cristy3ed852e2009-09-05 21:47:34 +0000659 MagickBooleanType
660 status;
661
662 MagickOffsetType
663 currentOffset,
664 offset,
665 scene;
666
667 MagickSizeType
668 cc;
669
cristy101ab702011-10-13 13:06:32 +0000670 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000671 transpix;
672
673 QuantizeInfo
674 *quantize_info;
675
cristybb503372010-05-27 20:51:26 +0000676 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000677 x;
678
cristy4c08aed2011-07-01 19:47:50 +0000679 register const Quantum
cristy3ed852e2009-09-05 21:47:34 +0000680 *p;
681
cristy4c08aed2011-07-01 19:47:50 +0000682 register Quantum
683 *q;
684
cristyaff6d802011-04-26 01:46:31 +0000685 size_t
686 count,
687 bits_per_pixel,
688 bytes_per_row,
689 nextDepthOffset,
690 one;
691
cristy3ed852e2009-09-05 21:47:34 +0000692 unsigned char
693 bit,
694 byte,
695 color,
696 *lastrow,
697 *one_row,
698 *ptr,
699 version;
700
701 unsigned int
702 transparentIndex;
703
cristy3ed852e2009-09-05 21:47:34 +0000704 unsigned short
705 color16,
706 flags;
707
cristy3ed852e2009-09-05 21:47:34 +0000708 /*
709 Open output image file.
710 */
711 assert(image_info != (const ImageInfo *) NULL);
712 assert(image_info->signature == MagickSignature);
713 assert(image != (Image *) NULL);
714 assert(image->signature == MagickSignature);
715 if (image->debug != MagickFalse)
716 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy3a37efd2011-08-28 20:31:03 +0000717 assert(exception != (ExceptionInfo *) NULL);
718 assert(exception->signature == MagickSignature);
cristy1e178e72011-08-28 19:44:34 +0000719 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
cristy3ed852e2009-09-05 21:47:34 +0000720 if (status == MagickFalse)
721 return(status);
cristy3ed852e2009-09-05 21:47:34 +0000722 quantize_info=AcquireQuantizeInfo(image_info);
723 flags=0;
724 currentOffset=0;
725 transparentIndex=0;
726 transpix.red=0;
727 transpix.green=0;
728 transpix.blue=0;
cristy4c08aed2011-07-01 19:47:50 +0000729 transpix.alpha=0;
cristyeaedf062010-05-29 22:36:02 +0000730 one=1;
cristy3ed852e2009-09-05 21:47:34 +0000731 version=0;
732 scene=0;
733 do
734 {
cristy510d06a2011-07-06 23:43:54 +0000735 if (IsRGBColorspace(image->colorspace) == MagickFalse)
cristye941a752011-10-15 01:52:48 +0000736 (void) TransformImageColorspace(image,RGBColorspace,exception);
cristy1e178e72011-08-28 19:44:34 +0000737 count=GetNumberColors(image,NULL,exception);
cristya7a9dbc2010-05-30 00:23:21 +0000738 for (bits_per_pixel=1; (one << bits_per_pixel) < count; bits_per_pixel*=2) ;
cristy3ed852e2009-09-05 21:47:34 +0000739 if (image_info->depth > 100)
740 bits_per_pixel=image_info->depth-100;
741 if (bits_per_pixel < 16)
cristye941a752011-10-15 01:52:48 +0000742 (void) TransformImageColorspace(image,image->colorspace,exception);
cristy3ed852e2009-09-05 21:47:34 +0000743 if (bits_per_pixel < 8)
744 {
cristye941a752011-10-15 01:52:48 +0000745 (void) TransformImageColorspace(image,GRAYColorspace,exception);
cristy018f07f2011-09-04 21:15:19 +0000746 (void) SetImageType(image,PaletteType,exception);
747 (void) SortColormapByIntensity(image,exception);
cristy3ed852e2009-09-05 21:47:34 +0000748 }
749 if ((image->storage_class == PseudoClass) && (image->colors > 256))
cristy1e178e72011-08-28 19:44:34 +0000750 (void) SetImageStorageClass(image,DirectClass,exception);
cristy3ed852e2009-09-05 21:47:34 +0000751 if (image->storage_class == PseudoClass)
752 flags|=PALM_HAS_COLORMAP_FLAG;
753 else
754 flags|=PALM_IS_DIRECT_COLOR;
755 (void) WriteBlobMSBShort(image,(unsigned short) image->columns); /* width */
756 (void) WriteBlobMSBShort(image,(unsigned short) image->rows); /* height */
757 bytes_per_row=((image->columns+(16/bits_per_pixel-1))/(16/
758 bits_per_pixel))*2;
759 (void) WriteBlobMSBShort(image,(unsigned short) bytes_per_row);
760 if ((image_info->compression == RLECompression) ||
761 (image_info->compression == FaxCompression))
762 flags|=PALM_IS_COMPRESSED_FLAG;
763 (void) WriteBlobMSBShort(image, flags);
764 (void) WriteBlobByte(image,(unsigned char) bits_per_pixel);
765 if (bits_per_pixel > 1)
766 version=1;
767 if ((image_info->compression == RLECompression) ||
768 (image_info->compression == FaxCompression))
769 version=2;
770 (void) WriteBlobByte(image,version);
771 (void) WriteBlobMSBShort(image,0); /* nextDepthOffset */
772 (void) WriteBlobByte(image,(unsigned char) transparentIndex);
773 if (image_info->compression == RLECompression)
774 (void) WriteBlobByte(image,PALM_COMPRESSION_RLE);
775 else
776 if (image_info->compression == FaxCompression)
777 (void) WriteBlobByte(image,PALM_COMPRESSION_SCANLINE);
778 else
779 (void) WriteBlobByte(image,PALM_COMPRESSION_NONE);
780 (void) WriteBlobMSBShort(image,0); /* reserved */
781 offset=16;
782 if (bits_per_pixel == 16)
783 {
784 (void) WriteBlobByte(image,5); /* # of bits of red */
785 (void) WriteBlobByte(image,6); /* # of bits of green */
786 (void) WriteBlobByte(image,5); /* # of bits of blue */
787 (void) WriteBlobByte(image,0); /* reserved by Palm */
788 (void) WriteBlobMSBLong(image,0); /* no transparent color, YET */
789 offset+=8;
790 }
791 if (bits_per_pixel == 8)
792 {
793 if (flags & PALM_HAS_COLORMAP_FLAG) /* Write out colormap */
794 {
cristy1e178e72011-08-28 19:44:34 +0000795 quantize_info->dither=IsPaletteImage(image,exception);
cristy3ed852e2009-09-05 21:47:34 +0000796 quantize_info->number_colors=image->colors;
cristy018f07f2011-09-04 21:15:19 +0000797 (void) QuantizeImage(quantize_info,image,exception);
cristy3ed852e2009-09-05 21:47:34 +0000798 (void) WriteBlobMSBShort(image,(unsigned short) image->colors);
799 for (count = 0; count < image->colors; count++)
800 {
801 (void) WriteBlobByte(image,(unsigned char) count);
802 (void) WriteBlobByte(image,ScaleQuantumToChar(
803 image->colormap[count].red));
804 (void) WriteBlobByte(image,
805 ScaleQuantumToChar(image->colormap[count].green));
806 (void) WriteBlobByte(image,
807 ScaleQuantumToChar(image->colormap[count].blue));
808 }
809 offset+=2+count*4;
810 }
811 else /* Map colors to Palm standard colormap */
812 {
813 Image
814 *affinity_image;
815
816 affinity_image=ConstituteImage(256,1,"RGB",CharPixel,&PalmPalette,
cristy1e178e72011-08-28 19:44:34 +0000817 exception);
cristyaff6d802011-04-26 01:46:31 +0000818 (void) TransformImageColorspace(affinity_image,
cristye941a752011-10-15 01:52:48 +0000819 affinity_image->colorspace,exception);
cristy018f07f2011-09-04 21:15:19 +0000820 (void) RemapImage(quantize_info,image,affinity_image,exception);
cristybb503372010-05-27 20:51:26 +0000821 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000822 {
cristy1e178e72011-08-28 19:44:34 +0000823 q=GetAuthenticPixels(image,0,y,image->columns,1,exception);
cristybb503372010-05-27 20:51:26 +0000824 for (x=0; x < (ssize_t) image->columns; x++)
cristy4c08aed2011-07-01 19:47:50 +0000825 {
826 SetPixelIndex(image,FindColor(image,&image->colormap[(ssize_t)
827 GetPixelIndex(image,q)]),q);
cristyed231572011-07-14 02:18:59 +0000828 q+=GetPixelChannels(image);
cristy4c08aed2011-07-01 19:47:50 +0000829 }
cristy3ed852e2009-09-05 21:47:34 +0000830 }
831 affinity_image=DestroyImage(affinity_image);
832 }
833 }
834 if (flags & PALM_IS_COMPRESSED_FLAG)
835 (void) WriteBlobMSBShort(image,0); /* fill in size later */
836 lastrow=(unsigned char *) NULL;
837 if (image_info->compression == FaxCompression)
838 lastrow=(unsigned char *) AcquireQuantumMemory(bytes_per_row,
839 sizeof(*lastrow));
840 /* TODO check whether memory really was acquired? */
841 one_row=(unsigned char *) AcquireQuantumMemory(bytes_per_row,
842 sizeof(*one_row));
843 if (one_row == (unsigned char *) NULL)
844 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
845 for (y=0; y < (int) image->rows; y++)
846 {
847 ptr=one_row;
848 (void) ResetMagickMemory(ptr,0,bytes_per_row);
cristy1e178e72011-08-28 19:44:34 +0000849 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +0000850 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000851 break;
cristy3ed852e2009-09-05 21:47:34 +0000852 if (bits_per_pixel == 16)
853 {
854 for (x=0; x < (int) image->columns; x++)
855 {
cristy4c08aed2011-07-01 19:47:50 +0000856 color16=(unsigned short) ((((31*(size_t) GetPixelRed(image,p))/
857 (size_t) QuantumRange) << 11) | (((63*(size_t)
858 GetPixelGreen(image,p))/(size_t) QuantumRange) << 5) |
859 ((31*(size_t) GetPixelBlue(image,p))/(size_t) QuantumRange));
860 if (GetPixelAlpha(image,p) == (Quantum) TransparentAlpha)
cristy3ed852e2009-09-05 21:47:34 +0000861 {
cristy4c08aed2011-07-01 19:47:50 +0000862 transpix.red=GetPixelRed(image,p);
863 transpix.green=GetPixelGreen(image,p);
864 transpix.blue=GetPixelBlue(image,p);
865 transpix.alpha=GetPixelAlpha(image,p);
cristy3ed852e2009-09-05 21:47:34 +0000866 flags|=PALM_HAS_TRANSPARENCY_FLAG;
867 }
868 *ptr++=(unsigned char) ((color16 >> 8) & 0xff);
869 *ptr++=(unsigned char) (color16 & 0xff);
cristyed231572011-07-14 02:18:59 +0000870 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000871 }
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)
cristy4c08aed2011-07-01 19:47:50 +0000880 color=(unsigned char) GetPixelIndex(image,p);
cristy3ed852e2009-09-05 21:47:34 +0000881 else
cristy4c08aed2011-07-01 19:47:50 +0000882 color=(unsigned char) (GetPixelIndex(image,p)*
cristyaff6d802011-04-26 01:46:31 +0000883 ((one << bits_per_pixel)-1)/MagickMax(1*image->colors-1,1));
cristy3ed852e2009-09-05 21:47:34 +0000884 byte|=color << bit;
885 if (bit != 0)
cristybefe4d22010-06-07 01:18:58 +0000886 bit-=(unsigned char) bits_per_pixel;
cristy3ed852e2009-09-05 21:47:34 +0000887 else
888 {
889 *ptr++=byte;
890 byte=0x00;
891 bit=(unsigned char) (8-bits_per_pixel);
892 }
cristyed231572011-07-14 02:18:59 +0000893 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000894 }
895 if ((image->columns % (8/bits_per_pixel)) != 0)
896 *ptr++=byte;
897 }
898 if (image_info->compression == RLECompression)
899 {
900 x=0;
cristybb503372010-05-27 20:51:26 +0000901 while (x < (ssize_t) bytes_per_row)
cristy3ed852e2009-09-05 21:47:34 +0000902 {
903 byte=one_row[x];
904 count=1;
905 while ((one_row[++x] == byte) && (count < 255) &&
cristybb503372010-05-27 20:51:26 +0000906 (x < (ssize_t) bytes_per_row))
cristy3ed852e2009-09-05 21:47:34 +0000907 count++;
908 (void) WriteBlobByte(image,(unsigned char) count);
909 (void) WriteBlobByte(image,(unsigned char) byte);
910 }
911 }
912 else
913 if (image_info->compression == FaxCompression)
914 {
915 char
916 tmpbuf[8],
917 *tptr;
918
cristybb503372010-05-27 20:51:26 +0000919 for (x = 0; x < (ssize_t) bytes_per_row; x += 8)
cristy3ed852e2009-09-05 21:47:34 +0000920 {
921 tptr = tmpbuf;
922 for (bit=0, byte=0; bit < (unsigned char) MagickMin(8,(ssize_t) bytes_per_row-x); bit++)
923 {
924 if ((y == 0) || (lastrow[x + bit] != one_row[x + bit]))
925 {
cristyaff6d802011-04-26 01:46:31 +0000926 byte |= (1 << (7 - bit));
927 *tptr++ = (char) one_row[x + bit];
928 }
cristy3ed852e2009-09-05 21:47:34 +0000929 }
930 (void) WriteBlobByte(image, byte);
931 (void) WriteBlob(image,tptr-tmpbuf,(unsigned char *) tmpbuf);
932 }
933 (void) CopyMagickMemory(lastrow,one_row,bytes_per_row);
934 }
935 else
936 (void) WriteBlob(image,bytes_per_row,one_row);
937 }
938 if (flags & PALM_HAS_TRANSPARENCY_FLAG)
939 {
940 offset=SeekBlob(image,currentOffset+6,SEEK_SET);
941 (void) WriteBlobMSBShort(image,flags);
942 offset=SeekBlob(image,currentOffset+12,SEEK_SET);
943 (void) WriteBlobByte(image,(unsigned char) transparentIndex); /* trans index */
944 }
945 if (bits_per_pixel == 16)
946 {
947 offset=SeekBlob(image,currentOffset+20,SEEK_SET);
948 (void) WriteBlobByte(image,0); /* reserved by Palm */
949 (void) WriteBlobByte(image,(unsigned char) ((31*transpix.red)/QuantumRange));
950 (void) WriteBlobByte(image,(unsigned char) ((63*transpix.green)/QuantumRange));
951 (void) WriteBlobByte(image,(unsigned char) ((31*transpix.blue)/QuantumRange));
952 }
953 if (flags & PALM_IS_COMPRESSED_FLAG) /* fill in size now */
954 {
955 offset=SeekBlob(image,currentOffset+offset,SEEK_SET);
956 (void) WriteBlobMSBShort(image,(unsigned short) (GetBlobSize(image)-
957 currentOffset-offset));
958 }
959 if (one_row != (unsigned char *) NULL)
960 one_row=(unsigned char *) RelinquishMagickMemory(one_row);
961 if (lastrow != (unsigned char *) NULL)
962 lastrow=(unsigned char *) RelinquishMagickMemory(lastrow);
963 if (GetNextImageInList(image) == (Image *) NULL)
964 break;
965 /* padding to 4 byte word */
966 for (cc = (GetBlobSize(image))%4; cc > 0; cc--)
967 {
968 (void) WriteBlobByte(image,0);
969 }
970 /* write nextDepthOffset and return to end of image */
971 offset=SeekBlob(image,currentOffset+10,SEEK_SET);
cristybb503372010-05-27 20:51:26 +0000972 nextDepthOffset=(size_t) ((GetBlobSize(image)-currentOffset)/4);
cristy3ed852e2009-09-05 21:47:34 +0000973 (void) WriteBlobMSBShort(image,(unsigned short) nextDepthOffset);
974 currentOffset=(MagickOffsetType) GetBlobSize(image);
975 offset=SeekBlob(image,currentOffset,SEEK_SET);
976 image=SyncNextImageInList(image);
977 status=SetImageProgress(image,SaveImagesTag,scene++,
978 GetImageListLength(image));
979 if (status == MagickFalse)
980 break;
981 } while (image_info->adjoin != MagickFalse);
982 quantize_info=DestroyQuantizeInfo(quantize_info);
983 (void) CloseBlob(image);
cristy3ed852e2009-09-05 21:47:34 +0000984 return(MagickTrue);
985}