blob: 3722a50a993fd3810b59375566db250ca0c6afd1 [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% %
cristy16af1cb2009-12-11 21:38:29 +000020% Copyright 1999-2010 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*/
43#include "magick/studio.h"
44#include "magick/blob.h"
45#include "magick/blob-private.h"
46#include "magick/color-private.h"
47#include "magick/cache.h"
48#include "magick/compress.h"
49#include "magick/constitute.h"
50#include "magick/exception.h"
51#include "magick/exception-private.h"
52#include "magick/image-private.h"
53#include "magick/list.h"
54#include "magick/memory_.h"
55#include "magick/monitor.h"
56#include "magick/monitor-private.h"
57#include "magick/option.h"
58#include "magick/resource_.h"
59#include "magick/string_.h"
60#if defined(MAGICKCORE_TIFF_DELEGATE)
61#if defined(MAGICKCORE_HAVE_TIFFCONF_H)
62#include "tiffconf.h"
63#endif
64#include "tiffio.h"
65#define CCITTParam "-1"
66#else
67#define CCITTParam "0"
68#endif
69#if defined(MAGICKCORE_ZLIB_DELEGATE)
70#include "zlib.h"
71#endif
72
73/*
74 Typedef declarations.
75*/
76struct _Ascii85Info
77{
cristybb503372010-05-27 20:51:26 +000078 ssize_t
cristy3ed852e2009-09-05 21:47:34 +000079 offset,
80 line_break;
81
82 unsigned char
83 buffer[10];
84};
85
86typedef struct HuffmanTable
87{
cristybb503372010-05-27 20:51:26 +000088 size_t
cristy3ed852e2009-09-05 21:47:34 +000089 id,
90 code,
91 length,
92 count;
93} HuffmanTable;
94
95/*
96 Huffman coding declarations.
97*/
98#define TWId 23
99#define MWId 24
100#define TBId 25
101#define MBId 26
102#define EXId 27
103
104static const HuffmanTable
105 MBTable[]=
106 {
107 { MBId, 0x0f, 10, 64 }, { MBId, 0xc8, 12, 128 },
108 { MBId, 0xc9, 12, 192 }, { MBId, 0x5b, 12, 256 },
109 { MBId, 0x33, 12, 320 }, { MBId, 0x34, 12, 384 },
110 { MBId, 0x35, 12, 448 }, { MBId, 0x6c, 13, 512 },
111 { MBId, 0x6d, 13, 576 }, { MBId, 0x4a, 13, 640 },
112 { MBId, 0x4b, 13, 704 }, { MBId, 0x4c, 13, 768 },
113 { MBId, 0x4d, 13, 832 }, { MBId, 0x72, 13, 896 },
114 { MBId, 0x73, 13, 960 }, { MBId, 0x74, 13, 1024 },
115 { MBId, 0x75, 13, 1088 }, { MBId, 0x76, 13, 1152 },
116 { MBId, 0x77, 13, 1216 }, { MBId, 0x52, 13, 1280 },
117 { MBId, 0x53, 13, 1344 }, { MBId, 0x54, 13, 1408 },
118 { MBId, 0x55, 13, 1472 }, { MBId, 0x5a, 13, 1536 },
119 { MBId, 0x5b, 13, 1600 }, { MBId, 0x64, 13, 1664 },
120 { MBId, 0x65, 13, 1728 }, { MBId, 0x00, 0, 0 }
121 };
122
123static const HuffmanTable
124 EXTable[]=
125 {
126 { EXId, 0x08, 11, 1792 }, { EXId, 0x0c, 11, 1856 },
127 { EXId, 0x0d, 11, 1920 }, { EXId, 0x12, 12, 1984 },
128 { EXId, 0x13, 12, 2048 }, { EXId, 0x14, 12, 2112 },
129 { EXId, 0x15, 12, 2176 }, { EXId, 0x16, 12, 2240 },
130 { EXId, 0x17, 12, 2304 }, { EXId, 0x1c, 12, 2368 },
131 { EXId, 0x1d, 12, 2432 }, { EXId, 0x1e, 12, 2496 },
132 { EXId, 0x1f, 12, 2560 }, { EXId, 0x00, 0, 0 }
133 };
134
135static const HuffmanTable
136 MWTable[]=
137 {
138 { MWId, 0x1b, 5, 64 }, { MWId, 0x12, 5, 128 },
139 { MWId, 0x17, 6, 192 }, { MWId, 0x37, 7, 256 },
140 { MWId, 0x36, 8, 320 }, { MWId, 0x37, 8, 384 },
141 { MWId, 0x64, 8, 448 }, { MWId, 0x65, 8, 512 },
142 { MWId, 0x68, 8, 576 }, { MWId, 0x67, 8, 640 },
143 { MWId, 0xcc, 9, 704 }, { MWId, 0xcd, 9, 768 },
144 { MWId, 0xd2, 9, 832 }, { MWId, 0xd3, 9, 896 },
145 { MWId, 0xd4, 9, 960 }, { MWId, 0xd5, 9, 1024 },
146 { MWId, 0xd6, 9, 1088 }, { MWId, 0xd7, 9, 1152 },
147 { MWId, 0xd8, 9, 1216 }, { MWId, 0xd9, 9, 1280 },
148 { MWId, 0xda, 9, 1344 }, { MWId, 0xdb, 9, 1408 },
149 { MWId, 0x98, 9, 1472 }, { MWId, 0x99, 9, 1536 },
150 { MWId, 0x9a, 9, 1600 }, { MWId, 0x18, 6, 1664 },
151 { MWId, 0x9b, 9, 1728 }, { MWId, 0x00, 0, 0 }
152 };
153
154static const HuffmanTable
155 TBTable[]=
156 {
157 { TBId, 0x37, 10, 0 }, { TBId, 0x02, 3, 1 }, { TBId, 0x03, 2, 2 },
158 { TBId, 0x02, 2, 3 }, { TBId, 0x03, 3, 4 }, { TBId, 0x03, 4, 5 },
159 { TBId, 0x02, 4, 6 }, { TBId, 0x03, 5, 7 }, { TBId, 0x05, 6, 8 },
160 { TBId, 0x04, 6, 9 }, { TBId, 0x04, 7, 10 }, { TBId, 0x05, 7, 11 },
161 { TBId, 0x07, 7, 12 }, { TBId, 0x04, 8, 13 }, { TBId, 0x07, 8, 14 },
162 { TBId, 0x18, 9, 15 }, { TBId, 0x17, 10, 16 }, { TBId, 0x18, 10, 17 },
163 { TBId, 0x08, 10, 18 }, { TBId, 0x67, 11, 19 }, { TBId, 0x68, 11, 20 },
164 { TBId, 0x6c, 11, 21 }, { TBId, 0x37, 11, 22 }, { TBId, 0x28, 11, 23 },
165 { TBId, 0x17, 11, 24 }, { TBId, 0x18, 11, 25 }, { TBId, 0xca, 12, 26 },
166 { TBId, 0xcb, 12, 27 }, { TBId, 0xcc, 12, 28 }, { TBId, 0xcd, 12, 29 },
167 { TBId, 0x68, 12, 30 }, { TBId, 0x69, 12, 31 }, { TBId, 0x6a, 12, 32 },
168 { TBId, 0x6b, 12, 33 }, { TBId, 0xd2, 12, 34 }, { TBId, 0xd3, 12, 35 },
169 { TBId, 0xd4, 12, 36 }, { TBId, 0xd5, 12, 37 }, { TBId, 0xd6, 12, 38 },
170 { TBId, 0xd7, 12, 39 }, { TBId, 0x6c, 12, 40 }, { TBId, 0x6d, 12, 41 },
171 { TBId, 0xda, 12, 42 }, { TBId, 0xdb, 12, 43 }, { TBId, 0x54, 12, 44 },
172 { TBId, 0x55, 12, 45 }, { TBId, 0x56, 12, 46 }, { TBId, 0x57, 12, 47 },
173 { TBId, 0x64, 12, 48 }, { TBId, 0x65, 12, 49 }, { TBId, 0x52, 12, 50 },
174 { TBId, 0x53, 12, 51 }, { TBId, 0x24, 12, 52 }, { TBId, 0x37, 12, 53 },
175 { TBId, 0x38, 12, 54 }, { TBId, 0x27, 12, 55 }, { TBId, 0x28, 12, 56 },
176 { TBId, 0x58, 12, 57 }, { TBId, 0x59, 12, 58 }, { TBId, 0x2b, 12, 59 },
177 { TBId, 0x2c, 12, 60 }, { TBId, 0x5a, 12, 61 }, { TBId, 0x66, 12, 62 },
178 { TBId, 0x67, 12, 63 }, { TBId, 0x00, 0, 0 }
179 };
180
181static const HuffmanTable
182 TWTable[]=
183 {
184 { TWId, 0x35, 8, 0 }, { TWId, 0x07, 6, 1 }, { TWId, 0x07, 4, 2 },
185 { TWId, 0x08, 4, 3 }, { TWId, 0x0b, 4, 4 }, { TWId, 0x0c, 4, 5 },
186 { TWId, 0x0e, 4, 6 }, { TWId, 0x0f, 4, 7 }, { TWId, 0x13, 5, 8 },
187 { TWId, 0x14, 5, 9 }, { TWId, 0x07, 5, 10 }, { TWId, 0x08, 5, 11 },
188 { TWId, 0x08, 6, 12 }, { TWId, 0x03, 6, 13 }, { TWId, 0x34, 6, 14 },
189 { TWId, 0x35, 6, 15 }, { TWId, 0x2a, 6, 16 }, { TWId, 0x2b, 6, 17 },
190 { TWId, 0x27, 7, 18 }, { TWId, 0x0c, 7, 19 }, { TWId, 0x08, 7, 20 },
191 { TWId, 0x17, 7, 21 }, { TWId, 0x03, 7, 22 }, { TWId, 0x04, 7, 23 },
192 { TWId, 0x28, 7, 24 }, { TWId, 0x2b, 7, 25 }, { TWId, 0x13, 7, 26 },
193 { TWId, 0x24, 7, 27 }, { TWId, 0x18, 7, 28 }, { TWId, 0x02, 8, 29 },
194 { TWId, 0x03, 8, 30 }, { TWId, 0x1a, 8, 31 }, { TWId, 0x1b, 8, 32 },
195 { TWId, 0x12, 8, 33 }, { TWId, 0x13, 8, 34 }, { TWId, 0x14, 8, 35 },
196 { TWId, 0x15, 8, 36 }, { TWId, 0x16, 8, 37 }, { TWId, 0x17, 8, 38 },
197 { TWId, 0x28, 8, 39 }, { TWId, 0x29, 8, 40 }, { TWId, 0x2a, 8, 41 },
198 { TWId, 0x2b, 8, 42 }, { TWId, 0x2c, 8, 43 }, { TWId, 0x2d, 8, 44 },
199 { TWId, 0x04, 8, 45 }, { TWId, 0x05, 8, 46 }, { TWId, 0x0a, 8, 47 },
200 { TWId, 0x0b, 8, 48 }, { TWId, 0x52, 8, 49 }, { TWId, 0x53, 8, 50 },
201 { TWId, 0x54, 8, 51 }, { TWId, 0x55, 8, 52 }, { TWId, 0x24, 8, 53 },
202 { TWId, 0x25, 8, 54 }, { TWId, 0x58, 8, 55 }, { TWId, 0x59, 8, 56 },
203 { TWId, 0x5a, 8, 57 }, { TWId, 0x5b, 8, 58 }, { TWId, 0x4a, 8, 59 },
204 { TWId, 0x4b, 8, 60 }, { TWId, 0x32, 8, 61 }, { TWId, 0x33, 8, 62 },
205 { TWId, 0x34, 8, 63 }, { TWId, 0x00, 0, 0 }
206 };
207
208/*
209%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
210% %
211% %
212% %
213% A S C I I 8 5 E n c o d e %
214% %
215% %
216% %
217%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
218%
219% ASCII85Encode() encodes data in ASCII base-85 format. ASCII base-85
220% encoding produces five ASCII printing characters from every four bytes of
221% binary data.
222%
223% The format of the ASCII85Encode method is:
224%
cristybb503372010-05-27 20:51:26 +0000225% void Ascii85Encode(Image *image,const size_t code)
cristy3ed852e2009-09-05 21:47:34 +0000226%
227% A description of each parameter follows:
228%
229% o code: a binary unsigned char to encode to ASCII 85.
230%
231% o file: write the encoded ASCII character to this file.
232%
233%
234*/
235#define MaxLineExtent 36
236
237static char *Ascii85Tuple(unsigned char *data)
238{
239 static char
240 tuple[6];
241
cristybb503372010-05-27 20:51:26 +0000242 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000243 i,
244 x;
245
cristybb503372010-05-27 20:51:26 +0000246 size_t
cristy3ed852e2009-09-05 21:47:34 +0000247 code,
248 quantum;
249
cristybb503372010-05-27 20:51:26 +0000250 code=((((size_t) data[0] << 8) | (size_t) data[1]) << 16) |
251 ((size_t) data[2] << 8) | (size_t) data[3];
cristy3ed852e2009-09-05 21:47:34 +0000252 if (code == 0L)
253 {
254 tuple[0]='z';
255 tuple[1]='\0';
256 return(tuple);
257 }
258 quantum=85UL*85UL*85UL*85UL;
259 for (i=0; i < 4; i++)
260 {
cristybb503372010-05-27 20:51:26 +0000261 x=(ssize_t) (code/quantum);
cristy3ed852e2009-09-05 21:47:34 +0000262 code-=quantum*x;
263 tuple[i]=(char) (x+(int) '!');
264 quantum/=85L;
265 }
266 tuple[4]=(char) ((code % 85L)+(int) '!');
267 tuple[5]='\0';
268 return(tuple);
269}
270
271MagickExport void Ascii85Initialize(Image *image)
272{
273 /*
274 Allocate image structure.
275 */
276 if (image->ascii85 == (Ascii85Info *) NULL)
cristy90823212009-12-12 20:48:33 +0000277 image->ascii85=(Ascii85Info *) AcquireAlignedMemory(1,sizeof(*image->ascii85));
cristy3ed852e2009-09-05 21:47:34 +0000278 if (image->ascii85 == (Ascii85Info *) NULL)
279 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
280 (void) ResetMagickMemory(image->ascii85,0,sizeof(*image->ascii85));
281 image->ascii85->line_break=MaxLineExtent << 1;
282 image->ascii85->offset=0;
283}
284
285MagickExport void Ascii85Flush(Image *image)
286{
287 register char
288 *tuple;
289
290 assert(image != (Image *) NULL);
291 assert(image->signature == MagickSignature);
292 if (image->debug != MagickFalse)
293 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
294 assert(image->ascii85 != (Ascii85Info *) NULL);
295 if (image->ascii85->offset > 0)
296 {
297 image->ascii85->buffer[image->ascii85->offset]='\0';
298 image->ascii85->buffer[image->ascii85->offset+1]='\0';
299 image->ascii85->buffer[image->ascii85->offset+2]='\0';
300 tuple=Ascii85Tuple(image->ascii85->buffer);
301 (void) WriteBlob(image,(size_t) image->ascii85->offset+1,
302 (const unsigned char *) (*tuple == 'z' ? "!!!!" : tuple));
303 }
304 (void) WriteBlobByte(image,'~');
305 (void) WriteBlobByte(image,'>');
306 (void) WriteBlobByte(image,'\n');
307}
308
309MagickExport void Ascii85Encode(Image *image,const unsigned char code)
310{
cristybb503372010-05-27 20:51:26 +0000311 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000312 n;
313
314 register char
315 *q;
316
317 register unsigned char
318 *p;
319
320 assert(image != (Image *) NULL);
321 assert(image->signature == MagickSignature);
322 assert(image->ascii85 != (Ascii85Info *) NULL);
323 image->ascii85->buffer[image->ascii85->offset]=code;
324 image->ascii85->offset++;
325 if (image->ascii85->offset < 4)
326 return;
327 p=image->ascii85->buffer;
328 for (n=image->ascii85->offset; n >= 4; n-=4)
329 {
330 for (q=Ascii85Tuple(p); *q != '\0'; q++)
331 {
332 image->ascii85->line_break--;
333 if ((image->ascii85->line_break < 0) && (*q != '%'))
334 {
335 (void) WriteBlobByte(image,'\n');
336 image->ascii85->line_break=2*MaxLineExtent;
337 }
338 (void) WriteBlobByte(image,(unsigned char) *q);
339 }
340 p+=8;
341 }
342 image->ascii85->offset=n;
343 p-=4;
344 for (n=0; n < 4; n++)
345 image->ascii85->buffer[n]=(*p++);
346}
347
348/*
349%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
350% %
351% %
352% %
353% H u f f m a n D e c o d e I m a g e %
354% %
355% %
356% %
357%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
358%
359% HuffmanDecodeImage() uncompresses an image via Huffman-coding.
360%
361% The format of the HuffmanDecodeImage method is:
362%
363% MagickBooleanType HuffmanDecodeImage(Image *image)
364%
365% A description of each parameter follows:
366%
367% o image: the image.
368%
369*/
370
371static inline size_t MagickMax(const size_t x,const size_t y)
372{
373 if (x > y)
374 return(x);
375 return(y);
376}
377
378static inline size_t MagickMin(const size_t x,const size_t y)
379{
380 if (x < y)
381 return(x);
382 return(y);
383}
384
385MagickExport MagickBooleanType HuffmanDecodeImage(Image *image)
386{
387#define HashSize 1021
388#define MBHashA 293
389#define MBHashB 2695
390#define MWHashA 3510
391#define MWHashB 1178
392
393#define InitializeHashTable(hash,table,a,b) \
394{ \
395 entry=table; \
396 while (entry->code != 0) \
397 { \
398 hash[((entry->length+a)*(entry->code+b)) % HashSize]=(HuffmanTable *) entry; \
399 entry++; \
400 } \
401}
402
403#define InputBit(bit) \
404{ \
405 if ((mask & 0xff) == 0) \
406 { \
407 byte=ReadBlobByte(image); \
408 if (byte == EOF) \
409 break; \
410 mask=0x80; \
411 } \
412 runlength++; \
cristybb503372010-05-27 20:51:26 +0000413 bit=(size_t) ((byte & mask) != 0 ? 0x01 : 0x00); \
cristy3ed852e2009-09-05 21:47:34 +0000414 mask>>=1; \
415 if (bit != 0) \
416 runlength=0; \
417}
418
419 const HuffmanTable
420 *entry;
421
422 ExceptionInfo
423 *exception;
424
425 HuffmanTable
426 **mb_hash,
427 **mw_hash;
428
429 IndexPacket
430 index;
431
432 int
433 byte;
434
cristybb503372010-05-27 20:51:26 +0000435 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000436 y;
437
438 MagickBooleanType
439 proceed;
440
441 register IndexPacket
442 *indexes;
443
cristybb503372010-05-27 20:51:26 +0000444 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000445 i;
446
447 register unsigned char
448 *p;
449
450 ssize_t
451 count;
452
453 unsigned char
454 *scanline;
455
456 unsigned int
457 bail,
458 color;
459
cristybb503372010-05-27 20:51:26 +0000460 size_t
cristy3ed852e2009-09-05 21:47:34 +0000461 bit,
462 code,
463 mask,
464 length,
465 null_lines,
466 runlength;
467
468 /*
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;
511 exception=(&image->exception);
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 {
cristybb503372010-05-27 20:51:26 +0000514 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000515 x;
516
517 register PixelPacket
cristyc47d1f82009-11-26 01:44:43 +0000518 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000519
520 /*
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;
631 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
632 if (q == (PixelPacket *) NULL)
633 break;
634 indexes=GetAuthenticIndexQueue(image);
cristybb503372010-05-27 20:51:26 +0000635 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000636 {
637 index=(IndexPacket) (*p++);
638 indexes[x]=index;
cristybb503372010-05-27 20:51:26 +0000639 *q++=image->colormap[(ssize_t) index];
cristy3ed852e2009-09-05 21:47:34 +0000640 }
641 if (SyncAuthenticPixels(image,exception) == MagickFalse)
642 break;
643 proceed=SetImageProgress(image,LoadImageTag,y,image->rows);
644 if (proceed == MagickFalse)
645 break;
646 y++;
647 }
cristybb503372010-05-27 20:51:26 +0000648 image->rows=(size_t) MagickMax((size_t) y-3,1);
cristy3ed852e2009-09-05 21:47:34 +0000649 image->compression=FaxCompression;
650 /*
651 Free decoder memory.
652 */
653 mw_hash=(HuffmanTable **) RelinquishMagickMemory(mw_hash);
654 mb_hash=(HuffmanTable **) RelinquishMagickMemory(mb_hash);
655 scanline=(unsigned char *) RelinquishMagickMemory(scanline);
656 return(MagickTrue);
657}
658
659/*
660%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
661% %
662% %
663% %
664% H u f f m a n E n c o d e I m a g e %
665% %
666% %
667% %
668%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
669%
670% HuffmanEncodeImage() compresses an image via Huffman-coding.
671%
672% The format of the HuffmanEncodeImage method is:
673%
674% MagickBooleanType HuffmanEncodeImage(const ImageInfo *image_info,
675% Image *image,Image *inject_image)
676%
677% A description of each parameter follows:
678%
679% o image_info: the image info..
680%
681% o image: the image.
682%
683% o inject_image: inject into the image stream.
684%
685*/
686MagickExport MagickBooleanType HuffmanEncodeImage(const ImageInfo *image_info,
687 Image *image,Image *inject_image)
688{
689#define HuffmanOutputCode(entry) \
690{ \
cristyeaedf062010-05-29 22:36:02 +0000691 mask=one << (entry->length-1); \
cristy3ed852e2009-09-05 21:47:34 +0000692 while (mask != 0) \
693 { \
694 OutputBit(((entry->code & mask) != 0 ? 1 : 0)); \
695 mask>>=1; \
696 } \
697}
698
699#define OutputBit(count) \
700{ \
701 if (count > 0) \
702 byte=byte | bit; \
703 bit>>=1; \
704 if ((int) (bit & 0xff) == 0) \
705 { \
706 if (LocaleCompare(image_info->magick,"FAX") == 0) \
707 (void) WriteBlobByte(image,(unsigned char) byte); \
708 else \
709 Ascii85Encode(image,byte); \
710 byte='\0'; \
711 bit=(unsigned char) 0x80; \
712 } \
713}
714
715 const HuffmanTable
716 *entry;
717
718 ExceptionInfo
719 *exception;
720
721 int
722 k,
723 runlength;
724
cristybb503372010-05-27 20:51:26 +0000725 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000726 n,
727 y;
728
729 Image
730 *huffman_image;
731
732 MagickBooleanType
733 proceed;
734
cristybb503372010-05-27 20:51:26 +0000735 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000736 i,
737 x;
738
739 register const PixelPacket
740 *p;
741
742 register unsigned char
743 *q;
744
745 unsigned char
746 byte,
747 bit,
748 *scanline;
749
cristybb503372010-05-27 20:51:26 +0000750 size_t
cristy3ed852e2009-09-05 21:47:34 +0000751 mask,
cristyeaedf062010-05-29 22:36:02 +0000752 one,
cristy3ed852e2009-09-05 21:47:34 +0000753 width;
754
755 /*
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 }
782 (void) SetImageType(huffman_image,BilevelType);
783 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 */
799 exception=(&huffman_image->exception);
800 q=scanline;
cristybb503372010-05-27 20:51:26 +0000801 for (y=0; y < (ssize_t) huffman_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000802 {
803 p=GetVirtualPixels(huffman_image,0,y,huffman_image->columns,1,exception);
804 if (p == (const PixelPacket *) NULL)
805 break;
cristybb503372010-05-27 20:51:26 +0000806 for (x=0; x < (ssize_t) huffman_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000807 {
808 *q++=(unsigned char) (PixelIntensity(p) >= ((MagickRealType)
809 QuantumRange/2.0) ? 0 : 1);
810 p++;
811 }
812 /*
813 Huffman encode scanline.
814 */
815 q=scanline;
cristybb503372010-05-27 20:51:26 +0000816 for (n=(ssize_t) width; n > 0; )
cristy3ed852e2009-09-05 21:47:34 +0000817 {
818 /*
819 Output white run.
820 */
821 for (runlength=0; ((n > 0) && (*q == 0)); n--)
822 {
823 q++;
824 runlength++;
825 }
826 if (runlength >= 64)
827 {
828 if (runlength < 1792)
829 entry=MWTable+((runlength/64)-1);
830 else
831 entry=EXTable+(MagickMin((size_t) runlength,2560)-1792)/64;
cristy8891f9c2010-06-04 23:32:17 +0000832 runlength-=(long) entry->count;
cristy3ed852e2009-09-05 21:47:34 +0000833 HuffmanOutputCode(entry);
834 }
835 entry=TWTable+MagickMin((size_t) runlength,63);
836 HuffmanOutputCode(entry);
837 if (n != 0)
838 {
839 /*
840 Output black run.
841 */
842 for (runlength=0; ((*q != 0) && (n > 0)); n--)
843 {
844 q++;
845 runlength++;
846 }
847 if (runlength >= 64)
848 {
849 entry=MBTable+((runlength/64)-1);
850 if (runlength >= 1792)
851 entry=EXTable+(MagickMin((size_t) runlength,2560)-1792)/64;
cristy8891f9c2010-06-04 23:32:17 +0000852 runlength-=(long) entry->count;
cristy3ed852e2009-09-05 21:47:34 +0000853 HuffmanOutputCode(entry);
854 }
855 entry=TBTable+MagickMin((size_t) runlength,63);
856 HuffmanOutputCode(entry);
857 }
858 }
859 /*
860 End of line.
861 */
862 for (k=0; k < 11; k++)
863 OutputBit(0);
864 OutputBit(1);
865 q=scanline;
866 if (GetPreviousImageInList(huffman_image) == (Image *) NULL)
867 {
868 proceed=SetImageProgress(huffman_image,LoadImageTag,y,
869 huffman_image->rows);
870 if (proceed == MagickFalse)
871 break;
872 }
873 }
874 /*
875 End of page.
876 */
877 for (i=0; i < 6; i++)
878 {
879 for (k=0; k < 11; k++)
880 OutputBit(0);
881 OutputBit(1);
882 }
883 /*
884 Flush bits.
885 */
886 if (((int) bit != 0x80) != 0)
887 {
888 if (LocaleCompare(image_info->magick,"FAX") == 0)
889 (void) WriteBlobByte(image,byte);
890 else
891 Ascii85Encode(image,byte);
892 }
893 if (LocaleCompare(image_info->magick,"FAX") != 0)
894 Ascii85Flush(image);
895 huffman_image=DestroyImage(huffman_image);
896 scanline=(unsigned char *) RelinquishMagickMemory(scanline);
897 return(MagickTrue);
898}
899
cristy3ed852e2009-09-05 21:47:34 +0000900/*
901%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
902% %
903% %
904% %
905% L Z W E n c o d e I m a g e %
906% %
907% %
908% %
909%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
910%
911% LZWEncodeImage() compresses an image via LZW-coding specific to Postscript
912% Level II or Portable Document Format.
913%
914% The format of the LZWEncodeImage method is:
915%
916% MagickBooleanType LZWEncodeImage(Image *image,const size_t length,
917% unsigned char *pixels)
918%
919% A description of each parameter follows:
920%
921% o image: the image.
922%
923% o length: A value that specifies the number of pixels to compress.
924%
925% o pixels: the address of an unsigned array of characters containing the
926% pixels to compress.
927%
928*/
929MagickExport MagickBooleanType LZWEncodeImage(Image *image,const size_t length,
930 unsigned char *pixels)
931{
932#define LZWClr 256UL /* Clear Table Marker */
933#define LZWEod 257UL /* End of Data marker */
934#define OutputCode(code) \
935{ \
936 accumulator+=code << (32-code_width-number_bits); \
937 number_bits+=code_width; \
938 while (number_bits >= 8) \
939 { \
940 (void) WriteBlobByte(image,(unsigned char) (accumulator >> 24)); \
941 accumulator=accumulator << 8; \
942 number_bits-=8; \
943 } \
944}
945
946 typedef struct _TableType
947 {
cristybb503372010-05-27 20:51:26 +0000948 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000949 prefix,
950 suffix,
951 next;
952 } TableType;
953
cristybb503372010-05-27 20:51:26 +0000954 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000955 index;
956
cristybb503372010-05-27 20:51:26 +0000957 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000958 i;
959
960 TableType
961 *table;
962
cristybb503372010-05-27 20:51:26 +0000963 size_t
cristy3ed852e2009-09-05 21:47:34 +0000964 accumulator,
965 number_bits,
966 code_width,
967 last_code,
968 next_index;
969
970 /*
971 Allocate string table.
972 */
973 assert(image != (Image *) NULL);
974 assert(image->signature == MagickSignature);
975 if (image->debug != MagickFalse)
976 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
977 assert(pixels != (unsigned char *) NULL);
978 table=(TableType *) AcquireQuantumMemory(1UL << 12,sizeof(*table));
979 if (table == (TableType *) NULL)
980 return(MagickFalse);
981 /*
982 Initialize variables.
983 */
984 accumulator=0;
985 code_width=9;
986 number_bits=0;
987 last_code=0;
988 OutputCode(LZWClr);
989 for (index=0; index < 256; index++)
990 {
991 table[index].prefix=(-1);
992 table[index].suffix=(short) index;
993 table[index].next=(-1);
994 }
995 next_index=LZWEod+1;
996 code_width=9;
cristybb503372010-05-27 20:51:26 +0000997 last_code=(size_t) pixels[0];
998 for (i=1; i < (ssize_t) length; i++)
cristy3ed852e2009-09-05 21:47:34 +0000999 {
1000 /*
1001 Find string.
1002 */
cristybb503372010-05-27 20:51:26 +00001003 index=(ssize_t) last_code;
cristy3ed852e2009-09-05 21:47:34 +00001004 while (index != -1)
cristybb503372010-05-27 20:51:26 +00001005 if ((table[index].prefix != (ssize_t) last_code) ||
1006 (table[index].suffix != (ssize_t) pixels[i]))
cristy3ed852e2009-09-05 21:47:34 +00001007 index=table[index].next;
1008 else
1009 {
cristybb503372010-05-27 20:51:26 +00001010 last_code=(size_t) index;
cristy3ed852e2009-09-05 21:47:34 +00001011 break;
1012 }
cristybb503372010-05-27 20:51:26 +00001013 if (last_code != (size_t) index)
cristy3ed852e2009-09-05 21:47:34 +00001014 {
1015 /*
1016 Add string.
1017 */
1018 OutputCode(last_code);
cristybb503372010-05-27 20:51:26 +00001019 table[next_index].prefix=(ssize_t) last_code;
cristy3ed852e2009-09-05 21:47:34 +00001020 table[next_index].suffix=(short) pixels[i];
1021 table[next_index].next=table[last_code].next;
cristybb503372010-05-27 20:51:26 +00001022 table[last_code].next=(ssize_t) next_index;
cristy3ed852e2009-09-05 21:47:34 +00001023 next_index++;
1024 /*
1025 Did we just move up to next bit width?
1026 */
1027 if ((next_index >> code_width) != 0)
1028 {
1029 code_width++;
1030 if (code_width > 12)
1031 {
1032 /*
1033 Did we overflow the max bit width?
1034 */
1035 code_width--;
1036 OutputCode(LZWClr);
1037 for (index=0; index < 256; index++)
1038 {
1039 table[index].prefix=(-1);
1040 table[index].suffix=index;
1041 table[index].next=(-1);
1042 }
1043 next_index=LZWEod+1;
1044 code_width=9;
1045 }
1046 }
cristybb503372010-05-27 20:51:26 +00001047 last_code=(size_t) pixels[i];
cristy3ed852e2009-09-05 21:47:34 +00001048 }
1049 }
1050 /*
1051 Flush tables.
1052 */
1053 OutputCode(last_code);
1054 OutputCode(LZWEod);
1055 if (number_bits != 0)
1056 (void) WriteBlobByte(image,(unsigned char) (accumulator >> 24));
1057 table=(TableType *) RelinquishMagickMemory(table);
1058 return(MagickTrue);
1059}
1060
1061/*
1062%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1063% %
1064% %
1065% %
1066% P a c k b i t s E n c o d e I m a g e %
1067% %
1068% %
1069% %
1070%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1071%
1072% PackbitsEncodeImage() compresses an image via Macintosh Packbits encoding
1073% specific to Postscript Level II or Portable Document Format. To ensure
1074% portability, the binary Packbits bytes are encoded as ASCII Base-85.
1075%
1076% The format of the PackbitsEncodeImage method is:
1077%
1078% MagickBooleanType PackbitsEncodeImage(Image *image,const size_t length,
1079% unsigned char *pixels)
1080%
1081% A description of each parameter follows:
1082%
1083% o image: the image.
1084%
1085% o length: A value that specifies the number of pixels to compress.
1086%
1087% o pixels: the address of an unsigned array of characters containing the
1088% pixels to compress.
1089%
1090*/
1091MagickExport MagickBooleanType PackbitsEncodeImage(Image *image,
1092 const size_t length,unsigned char *pixels)
1093{
1094 int
1095 count;
1096
cristybb503372010-05-27 20:51:26 +00001097 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001098 i,
1099 j;
1100
1101 unsigned char
1102 *packbits;
1103
1104 /*
1105 Compress pixels with Packbits encoding.
1106 */
1107 assert(image != (Image *) NULL);
1108 assert(image->signature == MagickSignature);
1109 if (image->debug != MagickFalse)
1110 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1111 assert(pixels != (unsigned char *) NULL);
1112 packbits=(unsigned char *) AcquireQuantumMemory(128UL,sizeof(*packbits));
1113 if (packbits == (unsigned char *) NULL)
1114 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
1115 image->filename);
cristybb503372010-05-27 20:51:26 +00001116 for (i=(ssize_t) length; i != 0; )
cristy3ed852e2009-09-05 21:47:34 +00001117 {
1118 switch (i)
1119 {
1120 case 1:
1121 {
1122 i--;
1123 (void) WriteBlobByte(image,(unsigned char) 0);
1124 (void) WriteBlobByte(image,*pixels);
1125 break;
1126 }
1127 case 2:
1128 {
1129 i-=2;
1130 (void) WriteBlobByte(image,(unsigned char) 1);
1131 (void) WriteBlobByte(image,*pixels);
1132 (void) WriteBlobByte(image,pixels[1]);
1133 break;
1134 }
1135 case 3:
1136 {
1137 i-=3;
1138 if ((*pixels == *(pixels+1)) && (*(pixels+1) == *(pixels+2)))
1139 {
1140 (void) WriteBlobByte(image,(unsigned char) ((256-3)+1));
1141 (void) WriteBlobByte(image,*pixels);
1142 break;
1143 }
1144 (void) WriteBlobByte(image,(unsigned char) 2);
1145 (void) WriteBlobByte(image,*pixels);
1146 (void) WriteBlobByte(image,pixels[1]);
1147 (void) WriteBlobByte(image,pixels[2]);
1148 break;
1149 }
1150 default:
1151 {
1152 if ((*pixels == *(pixels+1)) && (*(pixels+1) == *(pixels+2)))
1153 {
1154 /*
1155 Packed run.
1156 */
1157 count=3;
cristybb503372010-05-27 20:51:26 +00001158 while (((ssize_t) count < i) && (*pixels == *(pixels+count)))
cristy3ed852e2009-09-05 21:47:34 +00001159 {
1160 count++;
1161 if (count >= 127)
1162 break;
1163 }
1164 i-=count;
1165 (void) WriteBlobByte(image,(unsigned char) ((256-count)+1));
1166 (void) WriteBlobByte(image,*pixels);
1167 pixels+=count;
1168 break;
1169 }
1170 /*
1171 Literal run.
1172 */
1173 count=0;
1174 while ((*(pixels+count) != *(pixels+count+1)) ||
1175 (*(pixels+count+1) != *(pixels+count+2)))
1176 {
1177 packbits[count+1]=pixels[count];
1178 count++;
cristybb503372010-05-27 20:51:26 +00001179 if (((ssize_t) count >= (i-3)) || (count >= 127))
cristy3ed852e2009-09-05 21:47:34 +00001180 break;
1181 }
1182 i-=count;
1183 *packbits=(unsigned char) (count-1);
cristybb503372010-05-27 20:51:26 +00001184 for (j=0; j <= (ssize_t) count; j++)
cristy3ed852e2009-09-05 21:47:34 +00001185 (void) WriteBlobByte(image,packbits[j]);
1186 pixels+=count;
1187 break;
1188 }
1189 }
1190 }
1191 (void) WriteBlobByte(image,(unsigned char) 128); /* EOD marker */
1192 packbits=(unsigned char *) RelinquishMagickMemory(packbits);
1193 return(MagickTrue);
1194}
1195
1196#if defined(MAGICKCORE_ZLIB_DELEGATE)
1197/*
1198%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1199% %
1200% %
1201% %
1202% Z L I B E n c o d e I m a g e %
1203% %
1204% %
1205% %
1206%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1207%
1208% ZLIBEncodeImage compresses an image via ZLIB-coding specific to
1209% Postscript Level II or Portable Document Format.
1210%
1211% The format of the ZLIBEncodeImage method is:
1212%
1213% MagickBooleanType ZLIBEncodeImage(Image *image,const size_t length,
1214% unsigned char *pixels)
1215%
1216% A description of each parameter follows:
1217%
1218% o file: the address of a structure of type FILE. ZLIB encoded pixels
1219% are written to this file.
1220%
1221% o length: A value that specifies the number of pixels to compress.
1222%
1223% o pixels: the address of an unsigned array of characters containing the
1224% pixels to compress.
1225%
1226*/
1227
1228static voidpf AcquireZIPMemory(voidpf context,unsigned int items,
1229 unsigned int size)
1230{
1231 (void) context;
1232 return((voidpf) AcquireQuantumMemory(items,size));
1233}
1234
1235static void RelinquishZIPMemory(voidpf context,voidpf memory)
1236{
1237 (void) context;
1238 memory=RelinquishMagickMemory(memory);
1239}
1240
1241MagickExport MagickBooleanType ZLIBEncodeImage(Image *image,const size_t length,
1242 unsigned char *pixels)
1243{
1244 int
1245 status;
1246
cristybb503372010-05-27 20:51:26 +00001247 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001248 i;
1249
1250 size_t
1251 compress_packets;
1252
1253 unsigned char
1254 *compress_pixels;
1255
1256 z_stream
1257 stream;
1258
1259 assert(image != (Image *) NULL);
1260 assert(image->signature == MagickSignature);
1261 if (image->debug != MagickFalse)
1262 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1263 compress_packets=(size_t) (1.001*length+12);
1264 compress_pixels=(unsigned char *) AcquireQuantumMemory(compress_packets,
1265 sizeof(*compress_pixels));
1266 if (compress_pixels == (unsigned char *) NULL)
1267 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
1268 image->filename);
1269 stream.next_in=pixels;
1270 stream.avail_in=(unsigned int) length;
1271 stream.next_out=compress_pixels;
1272 stream.avail_out=(unsigned int) compress_packets;
1273 stream.zalloc=AcquireZIPMemory;
1274 stream.zfree=RelinquishZIPMemory;
1275 stream.opaque=(voidpf) NULL;
1276 status=deflateInit(&stream,(int) (image->quality ==
1277 UndefinedCompressionQuality ? 7 : MagickMin(image->quality/10,9)));
1278 if (status == Z_OK)
1279 {
1280 status=deflate(&stream,Z_FINISH);
1281 if (status == Z_STREAM_END)
1282 status=deflateEnd(&stream);
1283 else
1284 (void) deflateEnd(&stream);
1285 compress_packets=(size_t) stream.total_out;
1286 }
1287 if (status != Z_OK)
1288 ThrowBinaryException(CoderError,"UnableToZipCompressImage",image->filename)
1289 else
cristybb503372010-05-27 20:51:26 +00001290 for (i=0; i < (ssize_t) compress_packets; i++)
cristy3ed852e2009-09-05 21:47:34 +00001291 (void) WriteBlobByte(image,compress_pixels[i]);
1292 compress_pixels=(unsigned char *) RelinquishMagickMemory(compress_pixels);
1293 return(status == Z_OK ? MagickTrue : MagickFalse);
1294}
1295#else
1296MagickExport MagickBooleanType ZLIBEncodeImage(Image *image,
1297 const size_t magick_unused(length),unsigned char *magick_unused(pixels))
1298{
1299 assert(image != (Image *) NULL);
1300 assert(image->signature == MagickSignature);
1301 if (image->debug != MagickFalse)
1302 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1303 (void) ThrowMagickException(&image->exception,GetMagickModule(),
1304 MissingDelegateError,"DelegateLibrarySupportNotBuiltIn","`%s' (ZIP)",
1305 image->filename);
1306 return(MagickFalse);
1307}
1308#endif