blob: 812537d9120a3cf83fac93eeab45f84a4697074e [file] [log] [blame]
cristy3ed852e2009-09-05 21:47:34 +00001/*
2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3% %
4% %
5% %
6% CCCC OOO M M PPPP RRRR EEEEE SSSSS SSSSS %
7% C O O MM MM P P R R E SS SS %
8% C O O M M M PPPP RRRR EEE SSS SSS %
9% C O O M M P R R E SS SS %
10% CCCC OOO M M P R R EEEEE SSSSS SSSSS %
11% %
12% %
13% MagickCore Image Compression/Decompression Methods %
14% %
15% Software Design %
16% John Cristy %
17% May 1993 %
18% %
19% %
cristy7e41fe82010-12-04 23:12:08 +000020% Copyright 1999-2011 ImageMagick Studio LLC, a non-profit organization %
cristy3ed852e2009-09-05 21:47:34 +000021% dedicated to making software imaging solutions freely available. %
22% %
23% You may not use this file except in compliance with the License. You may %
24% obtain a copy of the License at %
25% %
26% http://www.imagemagick.org/script/license.php %
27% %
28% Unless required by applicable law or agreed to in writing, software %
29% distributed under the License is distributed on an "AS IS" BASIS, %
30% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31% See the License for the specific language governing permissions and %
32% limitations under the License. %
33% %
34%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35%
36%
37%
38*/
39
40/*
41 Include declarations.
42*/
cristy4c08aed2011-07-01 19:47:50 +000043#include "MagickCore/studio.h"
44#include "MagickCore/blob.h"
45#include "MagickCore/blob-private.h"
46#include "MagickCore/color-private.h"
47#include "MagickCore/cache.h"
48#include "MagickCore/compress.h"
49#include "MagickCore/constitute.h"
50#include "MagickCore/exception.h"
51#include "MagickCore/exception-private.h"
52#include "MagickCore/image-private.h"
53#include "MagickCore/list.h"
54#include "MagickCore/memory_.h"
55#include "MagickCore/monitor.h"
56#include "MagickCore/monitor-private.h"
57#include "MagickCore/option.h"
58#include "MagickCore/pixel-accessor.h"
59#include "MagickCore/resource_.h"
60#include "MagickCore/string_.h"
cristy3ed852e2009-09-05 21:47:34 +000061#if defined(MAGICKCORE_TIFF_DELEGATE)
62#if defined(MAGICKCORE_HAVE_TIFFCONF_H)
63#include "tiffconf.h"
64#endif
65#include "tiffio.h"
66#define CCITTParam "-1"
67#else
68#define CCITTParam "0"
69#endif
70#if defined(MAGICKCORE_ZLIB_DELEGATE)
71#include "zlib.h"
72#endif
73
74/*
75 Typedef declarations.
76*/
77struct _Ascii85Info
78{
cristybb503372010-05-27 20:51:26 +000079 ssize_t
cristy3ed852e2009-09-05 21:47:34 +000080 offset,
81 line_break;
82
83 unsigned char
84 buffer[10];
85};
86
87typedef struct HuffmanTable
88{
cristybb503372010-05-27 20:51:26 +000089 size_t
cristy3ed852e2009-09-05 21:47:34 +000090 id,
91 code,
92 length,
93 count;
94} HuffmanTable;
95
96/*
97 Huffman coding declarations.
98*/
99#define TWId 23
100#define MWId 24
101#define TBId 25
102#define MBId 26
103#define EXId 27
104
105static const HuffmanTable
106 MBTable[]=
107 {
108 { MBId, 0x0f, 10, 64 }, { MBId, 0xc8, 12, 128 },
109 { MBId, 0xc9, 12, 192 }, { MBId, 0x5b, 12, 256 },
110 { MBId, 0x33, 12, 320 }, { MBId, 0x34, 12, 384 },
111 { MBId, 0x35, 12, 448 }, { MBId, 0x6c, 13, 512 },
112 { MBId, 0x6d, 13, 576 }, { MBId, 0x4a, 13, 640 },
113 { MBId, 0x4b, 13, 704 }, { MBId, 0x4c, 13, 768 },
114 { MBId, 0x4d, 13, 832 }, { MBId, 0x72, 13, 896 },
115 { MBId, 0x73, 13, 960 }, { MBId, 0x74, 13, 1024 },
116 { MBId, 0x75, 13, 1088 }, { MBId, 0x76, 13, 1152 },
117 { MBId, 0x77, 13, 1216 }, { MBId, 0x52, 13, 1280 },
118 { MBId, 0x53, 13, 1344 }, { MBId, 0x54, 13, 1408 },
119 { MBId, 0x55, 13, 1472 }, { MBId, 0x5a, 13, 1536 },
120 { MBId, 0x5b, 13, 1600 }, { MBId, 0x64, 13, 1664 },
121 { MBId, 0x65, 13, 1728 }, { MBId, 0x00, 0, 0 }
122 };
123
124static const HuffmanTable
125 EXTable[]=
126 {
127 { EXId, 0x08, 11, 1792 }, { EXId, 0x0c, 11, 1856 },
128 { EXId, 0x0d, 11, 1920 }, { EXId, 0x12, 12, 1984 },
129 { EXId, 0x13, 12, 2048 }, { EXId, 0x14, 12, 2112 },
130 { EXId, 0x15, 12, 2176 }, { EXId, 0x16, 12, 2240 },
131 { EXId, 0x17, 12, 2304 }, { EXId, 0x1c, 12, 2368 },
132 { EXId, 0x1d, 12, 2432 }, { EXId, 0x1e, 12, 2496 },
133 { EXId, 0x1f, 12, 2560 }, { EXId, 0x00, 0, 0 }
134 };
135
136static const HuffmanTable
137 MWTable[]=
138 {
139 { MWId, 0x1b, 5, 64 }, { MWId, 0x12, 5, 128 },
140 { MWId, 0x17, 6, 192 }, { MWId, 0x37, 7, 256 },
141 { MWId, 0x36, 8, 320 }, { MWId, 0x37, 8, 384 },
142 { MWId, 0x64, 8, 448 }, { MWId, 0x65, 8, 512 },
143 { MWId, 0x68, 8, 576 }, { MWId, 0x67, 8, 640 },
144 { MWId, 0xcc, 9, 704 }, { MWId, 0xcd, 9, 768 },
145 { MWId, 0xd2, 9, 832 }, { MWId, 0xd3, 9, 896 },
146 { MWId, 0xd4, 9, 960 }, { MWId, 0xd5, 9, 1024 },
147 { MWId, 0xd6, 9, 1088 }, { MWId, 0xd7, 9, 1152 },
148 { MWId, 0xd8, 9, 1216 }, { MWId, 0xd9, 9, 1280 },
149 { MWId, 0xda, 9, 1344 }, { MWId, 0xdb, 9, 1408 },
150 { MWId, 0x98, 9, 1472 }, { MWId, 0x99, 9, 1536 },
151 { MWId, 0x9a, 9, 1600 }, { MWId, 0x18, 6, 1664 },
152 { MWId, 0x9b, 9, 1728 }, { MWId, 0x00, 0, 0 }
153 };
154
155static const HuffmanTable
156 TBTable[]=
157 {
158 { TBId, 0x37, 10, 0 }, { TBId, 0x02, 3, 1 }, { TBId, 0x03, 2, 2 },
159 { TBId, 0x02, 2, 3 }, { TBId, 0x03, 3, 4 }, { TBId, 0x03, 4, 5 },
160 { TBId, 0x02, 4, 6 }, { TBId, 0x03, 5, 7 }, { TBId, 0x05, 6, 8 },
161 { TBId, 0x04, 6, 9 }, { TBId, 0x04, 7, 10 }, { TBId, 0x05, 7, 11 },
162 { TBId, 0x07, 7, 12 }, { TBId, 0x04, 8, 13 }, { TBId, 0x07, 8, 14 },
163 { TBId, 0x18, 9, 15 }, { TBId, 0x17, 10, 16 }, { TBId, 0x18, 10, 17 },
164 { TBId, 0x08, 10, 18 }, { TBId, 0x67, 11, 19 }, { TBId, 0x68, 11, 20 },
165 { TBId, 0x6c, 11, 21 }, { TBId, 0x37, 11, 22 }, { TBId, 0x28, 11, 23 },
166 { TBId, 0x17, 11, 24 }, { TBId, 0x18, 11, 25 }, { TBId, 0xca, 12, 26 },
167 { TBId, 0xcb, 12, 27 }, { TBId, 0xcc, 12, 28 }, { TBId, 0xcd, 12, 29 },
168 { TBId, 0x68, 12, 30 }, { TBId, 0x69, 12, 31 }, { TBId, 0x6a, 12, 32 },
169 { TBId, 0x6b, 12, 33 }, { TBId, 0xd2, 12, 34 }, { TBId, 0xd3, 12, 35 },
170 { TBId, 0xd4, 12, 36 }, { TBId, 0xd5, 12, 37 }, { TBId, 0xd6, 12, 38 },
171 { TBId, 0xd7, 12, 39 }, { TBId, 0x6c, 12, 40 }, { TBId, 0x6d, 12, 41 },
172 { TBId, 0xda, 12, 42 }, { TBId, 0xdb, 12, 43 }, { TBId, 0x54, 12, 44 },
173 { TBId, 0x55, 12, 45 }, { TBId, 0x56, 12, 46 }, { TBId, 0x57, 12, 47 },
174 { TBId, 0x64, 12, 48 }, { TBId, 0x65, 12, 49 }, { TBId, 0x52, 12, 50 },
175 { TBId, 0x53, 12, 51 }, { TBId, 0x24, 12, 52 }, { TBId, 0x37, 12, 53 },
176 { TBId, 0x38, 12, 54 }, { TBId, 0x27, 12, 55 }, { TBId, 0x28, 12, 56 },
177 { TBId, 0x58, 12, 57 }, { TBId, 0x59, 12, 58 }, { TBId, 0x2b, 12, 59 },
178 { TBId, 0x2c, 12, 60 }, { TBId, 0x5a, 12, 61 }, { TBId, 0x66, 12, 62 },
179 { TBId, 0x67, 12, 63 }, { TBId, 0x00, 0, 0 }
180 };
181
182static const HuffmanTable
183 TWTable[]=
184 {
185 { TWId, 0x35, 8, 0 }, { TWId, 0x07, 6, 1 }, { TWId, 0x07, 4, 2 },
186 { TWId, 0x08, 4, 3 }, { TWId, 0x0b, 4, 4 }, { TWId, 0x0c, 4, 5 },
187 { TWId, 0x0e, 4, 6 }, { TWId, 0x0f, 4, 7 }, { TWId, 0x13, 5, 8 },
188 { TWId, 0x14, 5, 9 }, { TWId, 0x07, 5, 10 }, { TWId, 0x08, 5, 11 },
189 { TWId, 0x08, 6, 12 }, { TWId, 0x03, 6, 13 }, { TWId, 0x34, 6, 14 },
190 { TWId, 0x35, 6, 15 }, { TWId, 0x2a, 6, 16 }, { TWId, 0x2b, 6, 17 },
191 { TWId, 0x27, 7, 18 }, { TWId, 0x0c, 7, 19 }, { TWId, 0x08, 7, 20 },
192 { TWId, 0x17, 7, 21 }, { TWId, 0x03, 7, 22 }, { TWId, 0x04, 7, 23 },
193 { TWId, 0x28, 7, 24 }, { TWId, 0x2b, 7, 25 }, { TWId, 0x13, 7, 26 },
194 { TWId, 0x24, 7, 27 }, { TWId, 0x18, 7, 28 }, { TWId, 0x02, 8, 29 },
195 { TWId, 0x03, 8, 30 }, { TWId, 0x1a, 8, 31 }, { TWId, 0x1b, 8, 32 },
196 { TWId, 0x12, 8, 33 }, { TWId, 0x13, 8, 34 }, { TWId, 0x14, 8, 35 },
197 { TWId, 0x15, 8, 36 }, { TWId, 0x16, 8, 37 }, { TWId, 0x17, 8, 38 },
198 { TWId, 0x28, 8, 39 }, { TWId, 0x29, 8, 40 }, { TWId, 0x2a, 8, 41 },
199 { TWId, 0x2b, 8, 42 }, { TWId, 0x2c, 8, 43 }, { TWId, 0x2d, 8, 44 },
200 { TWId, 0x04, 8, 45 }, { TWId, 0x05, 8, 46 }, { TWId, 0x0a, 8, 47 },
201 { TWId, 0x0b, 8, 48 }, { TWId, 0x52, 8, 49 }, { TWId, 0x53, 8, 50 },
202 { TWId, 0x54, 8, 51 }, { TWId, 0x55, 8, 52 }, { TWId, 0x24, 8, 53 },
203 { TWId, 0x25, 8, 54 }, { TWId, 0x58, 8, 55 }, { TWId, 0x59, 8, 56 },
204 { TWId, 0x5a, 8, 57 }, { TWId, 0x5b, 8, 58 }, { TWId, 0x4a, 8, 59 },
205 { TWId, 0x4b, 8, 60 }, { TWId, 0x32, 8, 61 }, { TWId, 0x33, 8, 62 },
206 { TWId, 0x34, 8, 63 }, { TWId, 0x00, 0, 0 }
207 };
208
209/*
210%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
211% %
212% %
213% %
214% A S C I I 8 5 E n c o d e %
215% %
216% %
217% %
218%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
219%
220% ASCII85Encode() encodes data in ASCII base-85 format. ASCII base-85
221% encoding produces five ASCII printing characters from every four bytes of
222% binary data.
223%
224% The format of the ASCII85Encode method is:
225%
cristybb503372010-05-27 20:51:26 +0000226% void Ascii85Encode(Image *image,const size_t code)
cristy3ed852e2009-09-05 21:47:34 +0000227%
228% A description of each parameter follows:
229%
230% o code: a binary unsigned char to encode to ASCII 85.
231%
232% o file: write the encoded ASCII character to this file.
233%
234%
235*/
236#define MaxLineExtent 36
237
238static char *Ascii85Tuple(unsigned char *data)
239{
240 static char
241 tuple[6];
242
cristybb503372010-05-27 20:51:26 +0000243 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000244 i,
245 x;
246
cristybb503372010-05-27 20:51:26 +0000247 size_t
cristy3ed852e2009-09-05 21:47:34 +0000248 code,
249 quantum;
250
cristybb503372010-05-27 20:51:26 +0000251 code=((((size_t) data[0] << 8) | (size_t) data[1]) << 16) |
252 ((size_t) data[2] << 8) | (size_t) data[3];
cristy3ed852e2009-09-05 21:47:34 +0000253 if (code == 0L)
254 {
255 tuple[0]='z';
256 tuple[1]='\0';
257 return(tuple);
258 }
259 quantum=85UL*85UL*85UL*85UL;
260 for (i=0; i < 4; i++)
261 {
cristybb503372010-05-27 20:51:26 +0000262 x=(ssize_t) (code/quantum);
cristy3ed852e2009-09-05 21:47:34 +0000263 code-=quantum*x;
264 tuple[i]=(char) (x+(int) '!');
265 quantum/=85L;
266 }
267 tuple[4]=(char) ((code % 85L)+(int) '!');
268 tuple[5]='\0';
269 return(tuple);
270}
271
272MagickExport void Ascii85Initialize(Image *image)
273{
274 /*
275 Allocate image structure.
276 */
277 if (image->ascii85 == (Ascii85Info *) NULL)
cristy73bd4a52010-10-05 11:24:23 +0000278 image->ascii85=(Ascii85Info *) AcquireMagickMemory(sizeof(*image->ascii85));
cristy3ed852e2009-09-05 21:47:34 +0000279 if (image->ascii85 == (Ascii85Info *) NULL)
280 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
281 (void) ResetMagickMemory(image->ascii85,0,sizeof(*image->ascii85));
282 image->ascii85->line_break=MaxLineExtent << 1;
283 image->ascii85->offset=0;
284}
285
286MagickExport void Ascii85Flush(Image *image)
287{
288 register char
289 *tuple;
290
291 assert(image != (Image *) NULL);
292 assert(image->signature == MagickSignature);
293 if (image->debug != MagickFalse)
294 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
295 assert(image->ascii85 != (Ascii85Info *) NULL);
296 if (image->ascii85->offset > 0)
297 {
298 image->ascii85->buffer[image->ascii85->offset]='\0';
299 image->ascii85->buffer[image->ascii85->offset+1]='\0';
300 image->ascii85->buffer[image->ascii85->offset+2]='\0';
301 tuple=Ascii85Tuple(image->ascii85->buffer);
302 (void) WriteBlob(image,(size_t) image->ascii85->offset+1,
303 (const unsigned char *) (*tuple == 'z' ? "!!!!" : tuple));
304 }
305 (void) WriteBlobByte(image,'~');
306 (void) WriteBlobByte(image,'>');
307 (void) WriteBlobByte(image,'\n');
308}
309
310MagickExport void Ascii85Encode(Image *image,const unsigned char code)
311{
cristy3ed852e2009-09-05 21:47:34 +0000312 register char
313 *q;
314
315 register unsigned char
316 *p;
317
cristy9d314ff2011-03-09 01:30:28 +0000318 ssize_t
319 n;
320
cristy3ed852e2009-09-05 21:47:34 +0000321 assert(image != (Image *) NULL);
322 assert(image->signature == MagickSignature);
323 assert(image->ascii85 != (Ascii85Info *) NULL);
324 image->ascii85->buffer[image->ascii85->offset]=code;
325 image->ascii85->offset++;
326 if (image->ascii85->offset < 4)
327 return;
328 p=image->ascii85->buffer;
329 for (n=image->ascii85->offset; n >= 4; n-=4)
330 {
331 for (q=Ascii85Tuple(p); *q != '\0'; q++)
332 {
333 image->ascii85->line_break--;
334 if ((image->ascii85->line_break < 0) && (*q != '%'))
335 {
336 (void) WriteBlobByte(image,'\n');
337 image->ascii85->line_break=2*MaxLineExtent;
338 }
339 (void) WriteBlobByte(image,(unsigned char) *q);
340 }
341 p+=8;
342 }
343 image->ascii85->offset=n;
344 p-=4;
345 for (n=0; n < 4; n++)
346 image->ascii85->buffer[n]=(*p++);
347}
348
349/*
350%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
351% %
352% %
353% %
354% H u f f m a n D e c o d e I m a g e %
355% %
356% %
357% %
358%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
359%
360% HuffmanDecodeImage() uncompresses an image via Huffman-coding.
361%
362% The format of the HuffmanDecodeImage method is:
363%
cristy018f07f2011-09-04 21:15:19 +0000364% MagickBooleanType HuffmanDecodeImage(Image *image,
365% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000366%
367% A description of each parameter follows:
368%
369% o image: the image.
370%
cristy018f07f2011-09-04 21:15:19 +0000371% o exception: return any errors or warnings in this structure.
372%
cristy3ed852e2009-09-05 21:47:34 +0000373*/
374
375static inline size_t MagickMax(const size_t x,const size_t y)
376{
377 if (x > y)
378 return(x);
379 return(y);
380}
381
382static inline size_t MagickMin(const size_t x,const size_t y)
383{
384 if (x < y)
385 return(x);
386 return(y);
387}
388
cristy018f07f2011-09-04 21:15:19 +0000389MagickExport MagickBooleanType HuffmanDecodeImage(Image *image,
390 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000391{
392#define HashSize 1021
393#define MBHashA 293
394#define MBHashB 2695
395#define MWHashA 3510
396#define MWHashB 1178
397
398#define InitializeHashTable(hash,table,a,b) \
399{ \
400 entry=table; \
401 while (entry->code != 0) \
402 { \
403 hash[((entry->length+a)*(entry->code+b)) % HashSize]=(HuffmanTable *) entry; \
404 entry++; \
405 } \
406}
407
408#define InputBit(bit) \
409{ \
410 if ((mask & 0xff) == 0) \
411 { \
412 byte=ReadBlobByte(image); \
413 if (byte == EOF) \
414 break; \
415 mask=0x80; \
416 } \
417 runlength++; \
cristybb503372010-05-27 20:51:26 +0000418 bit=(size_t) ((byte & mask) != 0 ? 0x01 : 0x00); \
cristy3ed852e2009-09-05 21:47:34 +0000419 mask>>=1; \
420 if (bit != 0) \
421 runlength=0; \
422}
423
cristyc5c6f662010-09-22 14:23:02 +0000424 CacheView
425 *image_view;
426
cristy3ed852e2009-09-05 21:47:34 +0000427 const HuffmanTable
428 *entry;
429
cristy3ed852e2009-09-05 21:47:34 +0000430 HuffmanTable
431 **mb_hash,
432 **mw_hash;
433
cristy3ed852e2009-09-05 21:47:34 +0000434 int
435 byte;
436
cristy3ed852e2009-09-05 21:47:34 +0000437 MagickBooleanType
438 proceed;
439
cristy4c08aed2011-07-01 19:47:50 +0000440 Quantum
441 index;
cristy3ed852e2009-09-05 21:47:34 +0000442
cristybb503372010-05-27 20:51:26 +0000443 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000444 i;
445
446 register unsigned char
447 *p;
448
cristy9d314ff2011-03-09 01:30:28 +0000449 size_t
450 bit,
451 code,
452 mask,
453 length,
454 null_lines,
455 runlength;
456
cristy3ed852e2009-09-05 21:47:34 +0000457 ssize_t
cristyc5c6f662010-09-22 14:23:02 +0000458 count,
459 y;
cristy3ed852e2009-09-05 21:47:34 +0000460
461 unsigned char
462 *scanline;
463
464 unsigned int
465 bail,
466 color;
467
cristy3ed852e2009-09-05 21:47:34 +0000468 /*
469 Allocate buffers.
470 */
471 assert(image != (Image *) NULL);
472 assert(image->signature == MagickSignature);
473 if (image->debug != MagickFalse)
474 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
475 mb_hash=(HuffmanTable **) AcquireQuantumMemory(HashSize,sizeof(*mb_hash));
476 mw_hash=(HuffmanTable **) AcquireQuantumMemory(HashSize,sizeof(*mw_hash));
477 scanline=(unsigned char *) AcquireQuantumMemory((size_t) image->columns,
478 sizeof(*scanline));
479 if ((mb_hash == (HuffmanTable **) NULL) ||
480 (mw_hash == (HuffmanTable **) NULL) ||
481 (scanline == (unsigned char *) NULL))
482 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
483 image->filename);
484 /*
485 Initialize Huffman tables.
486 */
487 for (i=0; i < HashSize; i++)
488 {
489 mb_hash[i]=(HuffmanTable *) NULL;
490 mw_hash[i]=(HuffmanTable *) NULL;
491 }
492 InitializeHashTable(mw_hash,TWTable,MWHashA,MWHashB);
493 InitializeHashTable(mw_hash,MWTable,MWHashA,MWHashB);
494 InitializeHashTable(mw_hash,EXTable,MWHashA,MWHashB);
495 InitializeHashTable(mb_hash,TBTable,MBHashA,MBHashB);
496 InitializeHashTable(mb_hash,MBTable,MBHashA,MBHashB);
497 InitializeHashTable(mb_hash,EXTable,MBHashA,MBHashB);
498 /*
499 Uncompress 1D Huffman to runlength encoded pixels.
500 */
501 byte=0;
502 mask=0;
503 null_lines=0;
504 runlength=0;
505 while (runlength < 11)
506 InputBit(bit);
507 do { InputBit(bit); } while ((int) bit == 0);
508 image->x_resolution=204.0;
509 image->y_resolution=196.0;
510 image->units=PixelsPerInchResolution;
cristyc5c6f662010-09-22 14:23:02 +0000511 image_view=AcquireCacheView(image);
cristybb503372010-05-27 20:51:26 +0000512 for (y=0; ((y < (ssize_t) image->rows) && (null_lines < 3)); )
cristy3ed852e2009-09-05 21:47:34 +0000513 {
cristy4c08aed2011-07-01 19:47:50 +0000514 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000515 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000516
cristyc5c6f662010-09-22 14:23:02 +0000517 register ssize_t
518 x;
519
cristy3ed852e2009-09-05 21:47:34 +0000520 /*
521 Initialize scanline to white.
522 */
523 p=scanline;
cristybb503372010-05-27 20:51:26 +0000524 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000525 *p++=(unsigned char) 0;
526 /*
527 Decode Huffman encoded scanline.
528 */
529 color=MagickTrue;
530 code=0;
531 count=0;
532 length=0;
533 runlength=0;
534 x=0;
535 for ( ; ; )
536 {
537 if (byte == EOF)
538 break;
cristybb503372010-05-27 20:51:26 +0000539 if (x >= (ssize_t) image->columns)
cristy3ed852e2009-09-05 21:47:34 +0000540 {
541 while (runlength < 11)
542 InputBit(bit);
543 do { InputBit(bit); } while ((int) bit == 0);
544 break;
545 }
546 bail=MagickFalse;
547 do
548 {
549 if (runlength < 11)
550 InputBit(bit)
551 else
552 {
553 InputBit(bit);
554 if ((int) bit != 0)
555 {
556 null_lines++;
557 if (x != 0)
558 null_lines=0;
559 bail=MagickTrue;
560 break;
561 }
562 }
cristybb503372010-05-27 20:51:26 +0000563 code=(code << 1)+(size_t) bit;
cristy3ed852e2009-09-05 21:47:34 +0000564 length++;
565 } while (code == 0);
566 if (bail != MagickFalse)
567 break;
568 if (length > 13)
569 {
570 while (runlength < 11)
571 InputBit(bit);
572 do { InputBit(bit); } while ((int) bit == 0);
573 break;
574 }
575 if (color != MagickFalse)
576 {
577 if (length < 4)
578 continue;
579 entry=mw_hash[((length+MWHashA)*(code+MWHashB)) % HashSize];
580 }
581 else
582 {
583 if (length < 2)
584 continue;
585 entry=mb_hash[((length+MBHashA)*(code+MBHashB)) % HashSize];
586 }
587 if (entry == (const HuffmanTable *) NULL)
588 continue;
589 if ((entry->length != length) || (entry->code != code))
590 continue;
591 switch (entry->id)
592 {
593 case TWId:
594 case TBId:
595 {
cristyeaedf062010-05-29 22:36:02 +0000596 count+=(ssize_t) entry->count;
cristybb503372010-05-27 20:51:26 +0000597 if ((x+count) > (ssize_t) image->columns)
cristy3ed852e2009-09-05 21:47:34 +0000598 count=(ssize_t) image->columns-x;
599 if (count > 0)
600 {
601 if (color != MagickFalse)
602 {
603 x+=count;
604 count=0;
605 }
606 else
607 for ( ; count > 0; count--)
608 scanline[x++]=(unsigned char) 1;
609 }
610 color=(unsigned int)
611 ((color == MagickFalse) ? MagickTrue : MagickFalse);
612 break;
613 }
614 case MWId:
615 case MBId:
616 case EXId:
617 {
cristyeaedf062010-05-29 22:36:02 +0000618 count+=(ssize_t) entry->count;
cristy3ed852e2009-09-05 21:47:34 +0000619 break;
620 }
621 default:
622 break;
623 }
624 code=0;
625 length=0;
626 }
627 /*
628 Transfer scanline to image pixels.
629 */
630 p=scanline;
cristyc5c6f662010-09-22 14:23:02 +0000631 q=QueueCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +0000632 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000633 break;
cristybb503372010-05-27 20:51:26 +0000634 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000635 {
cristy4c08aed2011-07-01 19:47:50 +0000636 index=(Quantum) (*p++);
637 SetPixelIndex(image,index,q);
638 SetPixelPacket(image,image->colormap+(ssize_t) index,q);
cristyed231572011-07-14 02:18:59 +0000639 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000640 }
cristyc5c6f662010-09-22 14:23:02 +0000641 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000642 break;
643 proceed=SetImageProgress(image,LoadImageTag,y,image->rows);
644 if (proceed == MagickFalse)
645 break;
646 y++;
647 }
cristyc5c6f662010-09-22 14:23:02 +0000648 image_view=DestroyCacheView(image_view);
cristybb503372010-05-27 20:51:26 +0000649 image->rows=(size_t) MagickMax((size_t) y-3,1);
cristy3ed852e2009-09-05 21:47:34 +0000650 image->compression=FaxCompression;
651 /*
652 Free decoder memory.
653 */
654 mw_hash=(HuffmanTable **) RelinquishMagickMemory(mw_hash);
655 mb_hash=(HuffmanTable **) RelinquishMagickMemory(mb_hash);
656 scanline=(unsigned char *) RelinquishMagickMemory(scanline);
657 return(MagickTrue);
658}
659
660/*
661%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
662% %
663% %
664% %
665% H u f f m a n E n c o d e I m a g e %
666% %
667% %
668% %
669%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
670%
671% HuffmanEncodeImage() compresses an image via Huffman-coding.
672%
673% The format of the HuffmanEncodeImage method is:
674%
675% MagickBooleanType HuffmanEncodeImage(const ImageInfo *image_info,
cristy018f07f2011-09-04 21:15:19 +0000676% Image *image,Image *inject_image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000677%
678% A description of each parameter follows:
679%
680% o image_info: the image info..
681%
682% o image: the image.
683%
684% o inject_image: inject into the image stream.
685%
cristy018f07f2011-09-04 21:15:19 +0000686% o exception: return any errors or warnings in this structure.
687%
cristy3ed852e2009-09-05 21:47:34 +0000688*/
689MagickExport MagickBooleanType HuffmanEncodeImage(const ImageInfo *image_info,
cristy018f07f2011-09-04 21:15:19 +0000690 Image *image,Image *inject_image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000691{
692#define HuffmanOutputCode(entry) \
693{ \
cristyeaedf062010-05-29 22:36:02 +0000694 mask=one << (entry->length-1); \
cristy3ed852e2009-09-05 21:47:34 +0000695 while (mask != 0) \
696 { \
697 OutputBit(((entry->code & mask) != 0 ? 1 : 0)); \
698 mask>>=1; \
699 } \
700}
701
702#define OutputBit(count) \
703{ \
704 if (count > 0) \
705 byte=byte | bit; \
706 bit>>=1; \
707 if ((int) (bit & 0xff) == 0) \
708 { \
709 if (LocaleCompare(image_info->magick,"FAX") == 0) \
710 (void) WriteBlobByte(image,(unsigned char) byte); \
711 else \
712 Ascii85Encode(image,byte); \
713 byte='\0'; \
714 bit=(unsigned char) 0x80; \
715 } \
716}
717
718 const HuffmanTable
719 *entry;
720
cristy3ed852e2009-09-05 21:47:34 +0000721 int
722 k,
723 runlength;
724
cristy3ed852e2009-09-05 21:47:34 +0000725 Image
726 *huffman_image;
727
728 MagickBooleanType
729 proceed;
730
cristybb503372010-05-27 20:51:26 +0000731 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000732 i,
733 x;
734
cristy4c08aed2011-07-01 19:47:50 +0000735 register const Quantum
cristy3ed852e2009-09-05 21:47:34 +0000736 *p;
737
738 register unsigned char
739 *q;
740
cristybb503372010-05-27 20:51:26 +0000741 size_t
cristy3ed852e2009-09-05 21:47:34 +0000742 mask,
cristyeaedf062010-05-29 22:36:02 +0000743 one,
cristy3ed852e2009-09-05 21:47:34 +0000744 width;
745
cristy9d314ff2011-03-09 01:30:28 +0000746 ssize_t
747 n,
748 y;
749
750 unsigned char
751 byte,
752 bit,
753 *scanline;
754
cristy3ed852e2009-09-05 21:47:34 +0000755 /*
756 Allocate scanline buffer.
757 */
758 assert(image_info != (ImageInfo *) NULL);
759 assert(image_info->signature == MagickSignature);
760 assert(image != (Image *) NULL);
761 assert(image->signature == MagickSignature);
762 if (image->debug != MagickFalse)
763 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
764 assert(inject_image != (Image *) NULL);
765 assert(inject_image->signature == MagickSignature);
cristyeaedf062010-05-29 22:36:02 +0000766 one=1;
cristy3ed852e2009-09-05 21:47:34 +0000767 width=inject_image->columns;
768 if (LocaleCompare(image_info->magick,"FAX") == 0)
cristybb503372010-05-27 20:51:26 +0000769 width=(size_t) MagickMax(inject_image->columns,1728);
cristy3ed852e2009-09-05 21:47:34 +0000770 scanline=(unsigned char *) AcquireQuantumMemory((size_t) width+1UL,
771 sizeof(*scanline));
772 if (scanline == (unsigned char *) NULL)
773 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
774 inject_image->filename);
775 (void) ResetMagickMemory(scanline,0,width*sizeof(*scanline));
776 huffman_image=CloneImage(inject_image,0,0,MagickTrue,&image->exception);
777 if (huffman_image == (Image *) NULL)
778 {
779 scanline=(unsigned char *) RelinquishMagickMemory(scanline);
780 return(MagickFalse);
781 }
cristy018f07f2011-09-04 21:15:19 +0000782 (void) SetImageType(huffman_image,BilevelType,exception);
cristy3ed852e2009-09-05 21:47:34 +0000783 byte='\0';
784 bit=(unsigned char) 0x80;
785 if (LocaleCompare(image_info->magick,"FAX") != 0)
786 Ascii85Initialize(image);
787 else
788 {
789 /*
790 End of line.
791 */
792 for (k=0; k < 11; k++)
793 OutputBit(0);
794 OutputBit(1);
795 }
796 /*
797 Compress to 1D Huffman pixels.
798 */
cristy3ed852e2009-09-05 21:47:34 +0000799 q=scanline;
cristybb503372010-05-27 20:51:26 +0000800 for (y=0; y < (ssize_t) huffman_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000801 {
802 p=GetVirtualPixels(huffman_image,0,y,huffman_image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +0000803 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000804 break;
cristybb503372010-05-27 20:51:26 +0000805 for (x=0; x < (ssize_t) huffman_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000806 {
cristy4c08aed2011-07-01 19:47:50 +0000807 *q++=(unsigned char) (GetPixelIntensity(huffman_image,p) >=
808 ((MagickRealType) QuantumRange/2.0) ? 0 : 1);
cristyed231572011-07-14 02:18:59 +0000809 p+=GetPixelChannels(huffman_image);
cristy3ed852e2009-09-05 21:47:34 +0000810 }
811 /*
812 Huffman encode scanline.
813 */
814 q=scanline;
cristybb503372010-05-27 20:51:26 +0000815 for (n=(ssize_t) width; n > 0; )
cristy3ed852e2009-09-05 21:47:34 +0000816 {
817 /*
818 Output white run.
819 */
820 for (runlength=0; ((n > 0) && (*q == 0)); n--)
821 {
822 q++;
823 runlength++;
824 }
825 if (runlength >= 64)
826 {
827 if (runlength < 1792)
828 entry=MWTable+((runlength/64)-1);
829 else
830 entry=EXTable+(MagickMin((size_t) runlength,2560)-1792)/64;
cristy8891f9c2010-06-04 23:32:17 +0000831 runlength-=(long) entry->count;
cristy3ed852e2009-09-05 21:47:34 +0000832 HuffmanOutputCode(entry);
833 }
834 entry=TWTable+MagickMin((size_t) runlength,63);
835 HuffmanOutputCode(entry);
836 if (n != 0)
837 {
838 /*
839 Output black run.
840 */
841 for (runlength=0; ((*q != 0) && (n > 0)); n--)
842 {
843 q++;
844 runlength++;
845 }
846 if (runlength >= 64)
847 {
848 entry=MBTable+((runlength/64)-1);
849 if (runlength >= 1792)
850 entry=EXTable+(MagickMin((size_t) runlength,2560)-1792)/64;
cristy8891f9c2010-06-04 23:32:17 +0000851 runlength-=(long) entry->count;
cristy3ed852e2009-09-05 21:47:34 +0000852 HuffmanOutputCode(entry);
853 }
854 entry=TBTable+MagickMin((size_t) runlength,63);
855 HuffmanOutputCode(entry);
856 }
857 }
858 /*
859 End of line.
860 */
861 for (k=0; k < 11; k++)
862 OutputBit(0);
863 OutputBit(1);
864 q=scanline;
865 if (GetPreviousImageInList(huffman_image) == (Image *) NULL)
866 {
867 proceed=SetImageProgress(huffman_image,LoadImageTag,y,
868 huffman_image->rows);
869 if (proceed == MagickFalse)
870 break;
871 }
872 }
873 /*
874 End of page.
875 */
876 for (i=0; i < 6; i++)
877 {
878 for (k=0; k < 11; k++)
879 OutputBit(0);
880 OutputBit(1);
881 }
882 /*
883 Flush bits.
884 */
885 if (((int) bit != 0x80) != 0)
886 {
887 if (LocaleCompare(image_info->magick,"FAX") == 0)
888 (void) WriteBlobByte(image,byte);
889 else
890 Ascii85Encode(image,byte);
891 }
892 if (LocaleCompare(image_info->magick,"FAX") != 0)
893 Ascii85Flush(image);
894 huffman_image=DestroyImage(huffman_image);
895 scanline=(unsigned char *) RelinquishMagickMemory(scanline);
896 return(MagickTrue);
897}
898
cristy3ed852e2009-09-05 21:47:34 +0000899/*
900%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
901% %
902% %
903% %
904% L Z W E n c o d e I m a g e %
905% %
906% %
907% %
908%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
909%
910% LZWEncodeImage() compresses an image via LZW-coding specific to Postscript
911% Level II or Portable Document Format.
912%
913% The format of the LZWEncodeImage method is:
914%
915% MagickBooleanType LZWEncodeImage(Image *image,const size_t length,
cristy018f07f2011-09-04 21:15:19 +0000916% unsigned char *pixels,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000917%
918% A description of each parameter follows:
919%
920% o image: the image.
921%
922% o length: A value that specifies the number of pixels to compress.
923%
924% o pixels: the address of an unsigned array of characters containing the
925% pixels to compress.
926%
cristy018f07f2011-09-04 21:15:19 +0000927% o exception: return any errors or warnings in this structure.
928%
cristy3ed852e2009-09-05 21:47:34 +0000929*/
930MagickExport MagickBooleanType LZWEncodeImage(Image *image,const size_t length,
cristy018f07f2011-09-04 21:15:19 +0000931 unsigned char *pixels,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000932{
933#define LZWClr 256UL /* Clear Table Marker */
934#define LZWEod 257UL /* End of Data marker */
935#define OutputCode(code) \
936{ \
937 accumulator+=code << (32-code_width-number_bits); \
938 number_bits+=code_width; \
939 while (number_bits >= 8) \
940 { \
941 (void) WriteBlobByte(image,(unsigned char) (accumulator >> 24)); \
942 accumulator=accumulator << 8; \
943 number_bits-=8; \
944 } \
945}
946
947 typedef struct _TableType
948 {
cristybb503372010-05-27 20:51:26 +0000949 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000950 prefix,
951 suffix,
952 next;
953 } TableType;
954
cristybb503372010-05-27 20:51:26 +0000955 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000956 i;
957
cristybb503372010-05-27 20:51:26 +0000958 size_t
cristy3ed852e2009-09-05 21:47:34 +0000959 accumulator,
960 number_bits,
961 code_width,
962 last_code,
963 next_index;
964
cristy9d314ff2011-03-09 01:30:28 +0000965 ssize_t
966 index;
967
968 TableType
969 *table;
970
cristy3ed852e2009-09-05 21:47:34 +0000971 /*
972 Allocate string table.
973 */
974 assert(image != (Image *) NULL);
975 assert(image->signature == MagickSignature);
976 if (image->debug != MagickFalse)
977 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
978 assert(pixels != (unsigned char *) NULL);
979 table=(TableType *) AcquireQuantumMemory(1UL << 12,sizeof(*table));
980 if (table == (TableType *) NULL)
981 return(MagickFalse);
982 /*
983 Initialize variables.
984 */
985 accumulator=0;
986 code_width=9;
987 number_bits=0;
988 last_code=0;
989 OutputCode(LZWClr);
990 for (index=0; index < 256; index++)
991 {
992 table[index].prefix=(-1);
993 table[index].suffix=(short) index;
994 table[index].next=(-1);
995 }
996 next_index=LZWEod+1;
997 code_width=9;
cristybb503372010-05-27 20:51:26 +0000998 last_code=(size_t) pixels[0];
999 for (i=1; i < (ssize_t) length; i++)
cristy3ed852e2009-09-05 21:47:34 +00001000 {
1001 /*
1002 Find string.
1003 */
cristybb503372010-05-27 20:51:26 +00001004 index=(ssize_t) last_code;
cristy3ed852e2009-09-05 21:47:34 +00001005 while (index != -1)
cristybb503372010-05-27 20:51:26 +00001006 if ((table[index].prefix != (ssize_t) last_code) ||
1007 (table[index].suffix != (ssize_t) pixels[i]))
cristy3ed852e2009-09-05 21:47:34 +00001008 index=table[index].next;
1009 else
1010 {
cristybb503372010-05-27 20:51:26 +00001011 last_code=(size_t) index;
cristy3ed852e2009-09-05 21:47:34 +00001012 break;
1013 }
cristybb503372010-05-27 20:51:26 +00001014 if (last_code != (size_t) index)
cristy3ed852e2009-09-05 21:47:34 +00001015 {
1016 /*
1017 Add string.
1018 */
1019 OutputCode(last_code);
cristybb503372010-05-27 20:51:26 +00001020 table[next_index].prefix=(ssize_t) last_code;
cristy3ed852e2009-09-05 21:47:34 +00001021 table[next_index].suffix=(short) pixels[i];
1022 table[next_index].next=table[last_code].next;
cristybb503372010-05-27 20:51:26 +00001023 table[last_code].next=(ssize_t) next_index;
cristy3ed852e2009-09-05 21:47:34 +00001024 next_index++;
1025 /*
1026 Did we just move up to next bit width?
1027 */
1028 if ((next_index >> code_width) != 0)
1029 {
1030 code_width++;
1031 if (code_width > 12)
1032 {
1033 /*
1034 Did we overflow the max bit width?
1035 */
1036 code_width--;
1037 OutputCode(LZWClr);
1038 for (index=0; index < 256; index++)
1039 {
1040 table[index].prefix=(-1);
1041 table[index].suffix=index;
1042 table[index].next=(-1);
1043 }
1044 next_index=LZWEod+1;
1045 code_width=9;
1046 }
1047 }
cristybb503372010-05-27 20:51:26 +00001048 last_code=(size_t) pixels[i];
cristy3ed852e2009-09-05 21:47:34 +00001049 }
1050 }
1051 /*
1052 Flush tables.
1053 */
1054 OutputCode(last_code);
1055 OutputCode(LZWEod);
1056 if (number_bits != 0)
1057 (void) WriteBlobByte(image,(unsigned char) (accumulator >> 24));
1058 table=(TableType *) RelinquishMagickMemory(table);
1059 return(MagickTrue);
1060}
1061
1062/*
1063%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1064% %
1065% %
1066% %
1067% P a c k b i t s E n c o d e I m a g e %
1068% %
1069% %
1070% %
1071%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1072%
1073% PackbitsEncodeImage() compresses an image via Macintosh Packbits encoding
1074% specific to Postscript Level II or Portable Document Format. To ensure
1075% portability, the binary Packbits bytes are encoded as ASCII Base-85.
1076%
1077% The format of the PackbitsEncodeImage method is:
1078%
1079% MagickBooleanType PackbitsEncodeImage(Image *image,const size_t length,
1080% unsigned char *pixels)
1081%
1082% A description of each parameter follows:
1083%
1084% o image: the image.
1085%
1086% o length: A value that specifies the number of pixels to compress.
1087%
1088% o pixels: the address of an unsigned array of characters containing the
1089% pixels to compress.
1090%
1091*/
1092MagickExport MagickBooleanType PackbitsEncodeImage(Image *image,
cristy018f07f2011-09-04 21:15:19 +00001093 const size_t length,unsigned char *pixels,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001094{
1095 int
1096 count;
1097
cristybb503372010-05-27 20:51:26 +00001098 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001099 i,
1100 j;
1101
1102 unsigned char
1103 *packbits;
1104
1105 /*
1106 Compress pixels with Packbits encoding.
1107 */
1108 assert(image != (Image *) NULL);
1109 assert(image->signature == MagickSignature);
1110 if (image->debug != MagickFalse)
1111 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1112 assert(pixels != (unsigned char *) NULL);
1113 packbits=(unsigned char *) AcquireQuantumMemory(128UL,sizeof(*packbits));
1114 if (packbits == (unsigned char *) NULL)
1115 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
1116 image->filename);
cristybb503372010-05-27 20:51:26 +00001117 for (i=(ssize_t) length; i != 0; )
cristy3ed852e2009-09-05 21:47:34 +00001118 {
1119 switch (i)
1120 {
1121 case 1:
1122 {
1123 i--;
1124 (void) WriteBlobByte(image,(unsigned char) 0);
1125 (void) WriteBlobByte(image,*pixels);
1126 break;
1127 }
1128 case 2:
1129 {
1130 i-=2;
1131 (void) WriteBlobByte(image,(unsigned char) 1);
1132 (void) WriteBlobByte(image,*pixels);
1133 (void) WriteBlobByte(image,pixels[1]);
1134 break;
1135 }
1136 case 3:
1137 {
1138 i-=3;
1139 if ((*pixels == *(pixels+1)) && (*(pixels+1) == *(pixels+2)))
1140 {
1141 (void) WriteBlobByte(image,(unsigned char) ((256-3)+1));
1142 (void) WriteBlobByte(image,*pixels);
1143 break;
1144 }
1145 (void) WriteBlobByte(image,(unsigned char) 2);
1146 (void) WriteBlobByte(image,*pixels);
1147 (void) WriteBlobByte(image,pixels[1]);
1148 (void) WriteBlobByte(image,pixels[2]);
1149 break;
1150 }
1151 default:
1152 {
1153 if ((*pixels == *(pixels+1)) && (*(pixels+1) == *(pixels+2)))
1154 {
1155 /*
1156 Packed run.
1157 */
1158 count=3;
cristybb503372010-05-27 20:51:26 +00001159 while (((ssize_t) count < i) && (*pixels == *(pixels+count)))
cristy3ed852e2009-09-05 21:47:34 +00001160 {
1161 count++;
1162 if (count >= 127)
1163 break;
1164 }
1165 i-=count;
1166 (void) WriteBlobByte(image,(unsigned char) ((256-count)+1));
1167 (void) WriteBlobByte(image,*pixels);
1168 pixels+=count;
1169 break;
1170 }
1171 /*
1172 Literal run.
1173 */
1174 count=0;
1175 while ((*(pixels+count) != *(pixels+count+1)) ||
1176 (*(pixels+count+1) != *(pixels+count+2)))
1177 {
1178 packbits[count+1]=pixels[count];
1179 count++;
cristybb503372010-05-27 20:51:26 +00001180 if (((ssize_t) count >= (i-3)) || (count >= 127))
cristy3ed852e2009-09-05 21:47:34 +00001181 break;
1182 }
1183 i-=count;
1184 *packbits=(unsigned char) (count-1);
cristybb503372010-05-27 20:51:26 +00001185 for (j=0; j <= (ssize_t) count; j++)
cristy3ed852e2009-09-05 21:47:34 +00001186 (void) WriteBlobByte(image,packbits[j]);
1187 pixels+=count;
1188 break;
1189 }
1190 }
1191 }
1192 (void) WriteBlobByte(image,(unsigned char) 128); /* EOD marker */
1193 packbits=(unsigned char *) RelinquishMagickMemory(packbits);
1194 return(MagickTrue);
1195}
1196
1197#if defined(MAGICKCORE_ZLIB_DELEGATE)
1198/*
1199%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1200% %
1201% %
1202% %
1203% Z L I B E n c o d e I m a g e %
1204% %
1205% %
1206% %
1207%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1208%
1209% ZLIBEncodeImage compresses an image via ZLIB-coding specific to
1210% Postscript Level II or Portable Document Format.
1211%
1212% The format of the ZLIBEncodeImage method is:
1213%
1214% MagickBooleanType ZLIBEncodeImage(Image *image,const size_t length,
cristy018f07f2011-09-04 21:15:19 +00001215% unsigned char *pixels,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001216%
1217% A description of each parameter follows:
1218%
1219% o file: the address of a structure of type FILE. ZLIB encoded pixels
1220% are written to this file.
1221%
1222% o length: A value that specifies the number of pixels to compress.
1223%
1224% o pixels: the address of an unsigned array of characters containing the
1225% pixels to compress.
1226%
cristy018f07f2011-09-04 21:15:19 +00001227% o exception: return any errors or warnings in this structure.
1228%
cristy3ed852e2009-09-05 21:47:34 +00001229*/
1230
1231static voidpf AcquireZIPMemory(voidpf context,unsigned int items,
1232 unsigned int size)
1233{
1234 (void) context;
1235 return((voidpf) AcquireQuantumMemory(items,size));
1236}
1237
1238static void RelinquishZIPMemory(voidpf context,voidpf memory)
1239{
1240 (void) context;
1241 memory=RelinquishMagickMemory(memory);
1242}
1243
1244MagickExport MagickBooleanType ZLIBEncodeImage(Image *image,const size_t length,
cristy018f07f2011-09-04 21:15:19 +00001245 unsigned char *pixels,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001246{
1247 int
1248 status;
1249
cristybb503372010-05-27 20:51:26 +00001250 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001251 i;
1252
1253 size_t
1254 compress_packets;
1255
1256 unsigned char
1257 *compress_pixels;
1258
1259 z_stream
1260 stream;
1261
1262 assert(image != (Image *) NULL);
1263 assert(image->signature == MagickSignature);
1264 if (image->debug != MagickFalse)
1265 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1266 compress_packets=(size_t) (1.001*length+12);
1267 compress_pixels=(unsigned char *) AcquireQuantumMemory(compress_packets,
1268 sizeof(*compress_pixels));
1269 if (compress_pixels == (unsigned char *) NULL)
1270 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
1271 image->filename);
1272 stream.next_in=pixels;
1273 stream.avail_in=(unsigned int) length;
1274 stream.next_out=compress_pixels;
1275 stream.avail_out=(unsigned int) compress_packets;
1276 stream.zalloc=AcquireZIPMemory;
1277 stream.zfree=RelinquishZIPMemory;
1278 stream.opaque=(voidpf) NULL;
1279 status=deflateInit(&stream,(int) (image->quality ==
1280 UndefinedCompressionQuality ? 7 : MagickMin(image->quality/10,9)));
1281 if (status == Z_OK)
1282 {
1283 status=deflate(&stream,Z_FINISH);
1284 if (status == Z_STREAM_END)
1285 status=deflateEnd(&stream);
1286 else
1287 (void) deflateEnd(&stream);
1288 compress_packets=(size_t) stream.total_out;
1289 }
1290 if (status != Z_OK)
1291 ThrowBinaryException(CoderError,"UnableToZipCompressImage",image->filename)
1292 else
cristybb503372010-05-27 20:51:26 +00001293 for (i=0; i < (ssize_t) compress_packets; i++)
cristy3ed852e2009-09-05 21:47:34 +00001294 (void) WriteBlobByte(image,compress_pixels[i]);
1295 compress_pixels=(unsigned char *) RelinquishMagickMemory(compress_pixels);
1296 return(status == Z_OK ? MagickTrue : MagickFalse);
1297}
1298#else
1299MagickExport MagickBooleanType ZLIBEncodeImage(Image *image,
1300 const size_t magick_unused(length),unsigned char *magick_unused(pixels))
1301{
1302 assert(image != (Image *) NULL);
1303 assert(image->signature == MagickSignature);
1304 if (image->debug != MagickFalse)
1305 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1306 (void) ThrowMagickException(&image->exception,GetMagickModule(),
1307 MissingDelegateError,"DelegateLibrarySupportNotBuiltIn","`%s' (ZIP)",
1308 image->filename);
1309 return(MagickFalse);
1310}
1311#endif