blob: 61b8edd763cf2bb4e2a92cd12ba2b420e89b200e [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% %
cristy1454be72011-12-19 01:52:48 +000020% Copyright 1999-2012 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"
cristy8941c702012-06-21 01:30:15 +000044#include "MagickCore/attribute.h"
cristy4c08aed2011-07-01 19:47:50 +000045#include "MagickCore/blob.h"
46#include "MagickCore/blob-private.h"
47#include "MagickCore/color-private.h"
48#include "MagickCore/cache.h"
49#include "MagickCore/compress.h"
50#include "MagickCore/constitute.h"
51#include "MagickCore/exception.h"
52#include "MagickCore/exception-private.h"
53#include "MagickCore/image-private.h"
54#include "MagickCore/list.h"
55#include "MagickCore/memory_.h"
56#include "MagickCore/monitor.h"
57#include "MagickCore/monitor-private.h"
58#include "MagickCore/option.h"
59#include "MagickCore/pixel-accessor.h"
60#include "MagickCore/resource_.h"
61#include "MagickCore/string_.h"
cristy3ed852e2009-09-05 21:47:34 +000062#if defined(MAGICKCORE_ZLIB_DELEGATE)
63#include "zlib.h"
64#endif
65
66/*
67 Typedef declarations.
68*/
69struct _Ascii85Info
70{
cristybb503372010-05-27 20:51:26 +000071 ssize_t
cristy3ed852e2009-09-05 21:47:34 +000072 offset,
73 line_break;
74
75 unsigned char
76 buffer[10];
77};
78
79typedef struct HuffmanTable
80{
cristybb503372010-05-27 20:51:26 +000081 size_t
cristy3ed852e2009-09-05 21:47:34 +000082 id,
83 code,
84 length,
85 count;
86} HuffmanTable;
87
88/*
89 Huffman coding declarations.
90*/
91#define TWId 23
92#define MWId 24
93#define TBId 25
94#define MBId 26
95#define EXId 27
96
97static const HuffmanTable
98 MBTable[]=
99 {
100 { MBId, 0x0f, 10, 64 }, { MBId, 0xc8, 12, 128 },
101 { MBId, 0xc9, 12, 192 }, { MBId, 0x5b, 12, 256 },
102 { MBId, 0x33, 12, 320 }, { MBId, 0x34, 12, 384 },
103 { MBId, 0x35, 12, 448 }, { MBId, 0x6c, 13, 512 },
104 { MBId, 0x6d, 13, 576 }, { MBId, 0x4a, 13, 640 },
105 { MBId, 0x4b, 13, 704 }, { MBId, 0x4c, 13, 768 },
106 { MBId, 0x4d, 13, 832 }, { MBId, 0x72, 13, 896 },
107 { MBId, 0x73, 13, 960 }, { MBId, 0x74, 13, 1024 },
108 { MBId, 0x75, 13, 1088 }, { MBId, 0x76, 13, 1152 },
109 { MBId, 0x77, 13, 1216 }, { MBId, 0x52, 13, 1280 },
110 { MBId, 0x53, 13, 1344 }, { MBId, 0x54, 13, 1408 },
111 { MBId, 0x55, 13, 1472 }, { MBId, 0x5a, 13, 1536 },
112 { MBId, 0x5b, 13, 1600 }, { MBId, 0x64, 13, 1664 },
113 { MBId, 0x65, 13, 1728 }, { MBId, 0x00, 0, 0 }
114 };
115
116static const HuffmanTable
117 EXTable[]=
118 {
119 { EXId, 0x08, 11, 1792 }, { EXId, 0x0c, 11, 1856 },
120 { EXId, 0x0d, 11, 1920 }, { EXId, 0x12, 12, 1984 },
121 { EXId, 0x13, 12, 2048 }, { EXId, 0x14, 12, 2112 },
122 { EXId, 0x15, 12, 2176 }, { EXId, 0x16, 12, 2240 },
123 { EXId, 0x17, 12, 2304 }, { EXId, 0x1c, 12, 2368 },
124 { EXId, 0x1d, 12, 2432 }, { EXId, 0x1e, 12, 2496 },
125 { EXId, 0x1f, 12, 2560 }, { EXId, 0x00, 0, 0 }
126 };
127
128static const HuffmanTable
129 MWTable[]=
130 {
131 { MWId, 0x1b, 5, 64 }, { MWId, 0x12, 5, 128 },
132 { MWId, 0x17, 6, 192 }, { MWId, 0x37, 7, 256 },
133 { MWId, 0x36, 8, 320 }, { MWId, 0x37, 8, 384 },
134 { MWId, 0x64, 8, 448 }, { MWId, 0x65, 8, 512 },
135 { MWId, 0x68, 8, 576 }, { MWId, 0x67, 8, 640 },
136 { MWId, 0xcc, 9, 704 }, { MWId, 0xcd, 9, 768 },
137 { MWId, 0xd2, 9, 832 }, { MWId, 0xd3, 9, 896 },
138 { MWId, 0xd4, 9, 960 }, { MWId, 0xd5, 9, 1024 },
139 { MWId, 0xd6, 9, 1088 }, { MWId, 0xd7, 9, 1152 },
140 { MWId, 0xd8, 9, 1216 }, { MWId, 0xd9, 9, 1280 },
141 { MWId, 0xda, 9, 1344 }, { MWId, 0xdb, 9, 1408 },
142 { MWId, 0x98, 9, 1472 }, { MWId, 0x99, 9, 1536 },
143 { MWId, 0x9a, 9, 1600 }, { MWId, 0x18, 6, 1664 },
144 { MWId, 0x9b, 9, 1728 }, { MWId, 0x00, 0, 0 }
145 };
146
147static const HuffmanTable
148 TBTable[]=
149 {
150 { TBId, 0x37, 10, 0 }, { TBId, 0x02, 3, 1 }, { TBId, 0x03, 2, 2 },
151 { TBId, 0x02, 2, 3 }, { TBId, 0x03, 3, 4 }, { TBId, 0x03, 4, 5 },
152 { TBId, 0x02, 4, 6 }, { TBId, 0x03, 5, 7 }, { TBId, 0x05, 6, 8 },
153 { TBId, 0x04, 6, 9 }, { TBId, 0x04, 7, 10 }, { TBId, 0x05, 7, 11 },
154 { TBId, 0x07, 7, 12 }, { TBId, 0x04, 8, 13 }, { TBId, 0x07, 8, 14 },
155 { TBId, 0x18, 9, 15 }, { TBId, 0x17, 10, 16 }, { TBId, 0x18, 10, 17 },
156 { TBId, 0x08, 10, 18 }, { TBId, 0x67, 11, 19 }, { TBId, 0x68, 11, 20 },
157 { TBId, 0x6c, 11, 21 }, { TBId, 0x37, 11, 22 }, { TBId, 0x28, 11, 23 },
158 { TBId, 0x17, 11, 24 }, { TBId, 0x18, 11, 25 }, { TBId, 0xca, 12, 26 },
159 { TBId, 0xcb, 12, 27 }, { TBId, 0xcc, 12, 28 }, { TBId, 0xcd, 12, 29 },
160 { TBId, 0x68, 12, 30 }, { TBId, 0x69, 12, 31 }, { TBId, 0x6a, 12, 32 },
161 { TBId, 0x6b, 12, 33 }, { TBId, 0xd2, 12, 34 }, { TBId, 0xd3, 12, 35 },
162 { TBId, 0xd4, 12, 36 }, { TBId, 0xd5, 12, 37 }, { TBId, 0xd6, 12, 38 },
163 { TBId, 0xd7, 12, 39 }, { TBId, 0x6c, 12, 40 }, { TBId, 0x6d, 12, 41 },
164 { TBId, 0xda, 12, 42 }, { TBId, 0xdb, 12, 43 }, { TBId, 0x54, 12, 44 },
165 { TBId, 0x55, 12, 45 }, { TBId, 0x56, 12, 46 }, { TBId, 0x57, 12, 47 },
166 { TBId, 0x64, 12, 48 }, { TBId, 0x65, 12, 49 }, { TBId, 0x52, 12, 50 },
167 { TBId, 0x53, 12, 51 }, { TBId, 0x24, 12, 52 }, { TBId, 0x37, 12, 53 },
168 { TBId, 0x38, 12, 54 }, { TBId, 0x27, 12, 55 }, { TBId, 0x28, 12, 56 },
169 { TBId, 0x58, 12, 57 }, { TBId, 0x59, 12, 58 }, { TBId, 0x2b, 12, 59 },
170 { TBId, 0x2c, 12, 60 }, { TBId, 0x5a, 12, 61 }, { TBId, 0x66, 12, 62 },
171 { TBId, 0x67, 12, 63 }, { TBId, 0x00, 0, 0 }
172 };
173
174static const HuffmanTable
175 TWTable[]=
176 {
177 { TWId, 0x35, 8, 0 }, { TWId, 0x07, 6, 1 }, { TWId, 0x07, 4, 2 },
178 { TWId, 0x08, 4, 3 }, { TWId, 0x0b, 4, 4 }, { TWId, 0x0c, 4, 5 },
179 { TWId, 0x0e, 4, 6 }, { TWId, 0x0f, 4, 7 }, { TWId, 0x13, 5, 8 },
180 { TWId, 0x14, 5, 9 }, { TWId, 0x07, 5, 10 }, { TWId, 0x08, 5, 11 },
181 { TWId, 0x08, 6, 12 }, { TWId, 0x03, 6, 13 }, { TWId, 0x34, 6, 14 },
182 { TWId, 0x35, 6, 15 }, { TWId, 0x2a, 6, 16 }, { TWId, 0x2b, 6, 17 },
183 { TWId, 0x27, 7, 18 }, { TWId, 0x0c, 7, 19 }, { TWId, 0x08, 7, 20 },
184 { TWId, 0x17, 7, 21 }, { TWId, 0x03, 7, 22 }, { TWId, 0x04, 7, 23 },
185 { TWId, 0x28, 7, 24 }, { TWId, 0x2b, 7, 25 }, { TWId, 0x13, 7, 26 },
186 { TWId, 0x24, 7, 27 }, { TWId, 0x18, 7, 28 }, { TWId, 0x02, 8, 29 },
187 { TWId, 0x03, 8, 30 }, { TWId, 0x1a, 8, 31 }, { TWId, 0x1b, 8, 32 },
188 { TWId, 0x12, 8, 33 }, { TWId, 0x13, 8, 34 }, { TWId, 0x14, 8, 35 },
189 { TWId, 0x15, 8, 36 }, { TWId, 0x16, 8, 37 }, { TWId, 0x17, 8, 38 },
190 { TWId, 0x28, 8, 39 }, { TWId, 0x29, 8, 40 }, { TWId, 0x2a, 8, 41 },
191 { TWId, 0x2b, 8, 42 }, { TWId, 0x2c, 8, 43 }, { TWId, 0x2d, 8, 44 },
192 { TWId, 0x04, 8, 45 }, { TWId, 0x05, 8, 46 }, { TWId, 0x0a, 8, 47 },
193 { TWId, 0x0b, 8, 48 }, { TWId, 0x52, 8, 49 }, { TWId, 0x53, 8, 50 },
194 { TWId, 0x54, 8, 51 }, { TWId, 0x55, 8, 52 }, { TWId, 0x24, 8, 53 },
195 { TWId, 0x25, 8, 54 }, { TWId, 0x58, 8, 55 }, { TWId, 0x59, 8, 56 },
196 { TWId, 0x5a, 8, 57 }, { TWId, 0x5b, 8, 58 }, { TWId, 0x4a, 8, 59 },
197 { TWId, 0x4b, 8, 60 }, { TWId, 0x32, 8, 61 }, { TWId, 0x33, 8, 62 },
198 { TWId, 0x34, 8, 63 }, { TWId, 0x00, 0, 0 }
199 };
200
201/*
202%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
203% %
204% %
205% %
206% A S C I I 8 5 E n c o d e %
207% %
208% %
209% %
210%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
211%
212% ASCII85Encode() encodes data in ASCII base-85 format. ASCII base-85
213% encoding produces five ASCII printing characters from every four bytes of
214% binary data.
215%
216% The format of the ASCII85Encode method is:
217%
cristybb503372010-05-27 20:51:26 +0000218% void Ascii85Encode(Image *image,const size_t code)
cristy3ed852e2009-09-05 21:47:34 +0000219%
220% A description of each parameter follows:
221%
222% o code: a binary unsigned char to encode to ASCII 85.
223%
224% o file: write the encoded ASCII character to this file.
225%
226%
227*/
228#define MaxLineExtent 36
229
230static char *Ascii85Tuple(unsigned char *data)
231{
232 static char
233 tuple[6];
234
cristybb503372010-05-27 20:51:26 +0000235 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000236 i,
237 x;
238
cristybb503372010-05-27 20:51:26 +0000239 size_t
cristy3ed852e2009-09-05 21:47:34 +0000240 code,
241 quantum;
242
cristybb503372010-05-27 20:51:26 +0000243 code=((((size_t) data[0] << 8) | (size_t) data[1]) << 16) |
244 ((size_t) data[2] << 8) | (size_t) data[3];
cristy3ed852e2009-09-05 21:47:34 +0000245 if (code == 0L)
246 {
247 tuple[0]='z';
248 tuple[1]='\0';
249 return(tuple);
250 }
251 quantum=85UL*85UL*85UL*85UL;
252 for (i=0; i < 4; i++)
253 {
cristybb503372010-05-27 20:51:26 +0000254 x=(ssize_t) (code/quantum);
cristy3ed852e2009-09-05 21:47:34 +0000255 code-=quantum*x;
256 tuple[i]=(char) (x+(int) '!');
257 quantum/=85L;
258 }
259 tuple[4]=(char) ((code % 85L)+(int) '!');
260 tuple[5]='\0';
261 return(tuple);
262}
263
264MagickExport void Ascii85Initialize(Image *image)
265{
266 /*
267 Allocate image structure.
268 */
269 if (image->ascii85 == (Ascii85Info *) NULL)
cristy73bd4a52010-10-05 11:24:23 +0000270 image->ascii85=(Ascii85Info *) AcquireMagickMemory(sizeof(*image->ascii85));
cristy3ed852e2009-09-05 21:47:34 +0000271 if (image->ascii85 == (Ascii85Info *) NULL)
272 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
273 (void) ResetMagickMemory(image->ascii85,0,sizeof(*image->ascii85));
274 image->ascii85->line_break=MaxLineExtent << 1;
275 image->ascii85->offset=0;
276}
277
278MagickExport void Ascii85Flush(Image *image)
279{
280 register char
281 *tuple;
282
283 assert(image != (Image *) NULL);
284 assert(image->signature == MagickSignature);
285 if (image->debug != MagickFalse)
286 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
287 assert(image->ascii85 != (Ascii85Info *) NULL);
288 if (image->ascii85->offset > 0)
289 {
290 image->ascii85->buffer[image->ascii85->offset]='\0';
291 image->ascii85->buffer[image->ascii85->offset+1]='\0';
292 image->ascii85->buffer[image->ascii85->offset+2]='\0';
293 tuple=Ascii85Tuple(image->ascii85->buffer);
294 (void) WriteBlob(image,(size_t) image->ascii85->offset+1,
295 (const unsigned char *) (*tuple == 'z' ? "!!!!" : tuple));
296 }
297 (void) WriteBlobByte(image,'~');
298 (void) WriteBlobByte(image,'>');
299 (void) WriteBlobByte(image,'\n');
300}
301
302MagickExport void Ascii85Encode(Image *image,const unsigned char code)
303{
cristy3ed852e2009-09-05 21:47:34 +0000304 register char
305 *q;
306
307 register unsigned char
308 *p;
309
cristy9d314ff2011-03-09 01:30:28 +0000310 ssize_t
311 n;
312
cristy3ed852e2009-09-05 21:47:34 +0000313 assert(image != (Image *) NULL);
314 assert(image->signature == MagickSignature);
315 assert(image->ascii85 != (Ascii85Info *) NULL);
316 image->ascii85->buffer[image->ascii85->offset]=code;
317 image->ascii85->offset++;
318 if (image->ascii85->offset < 4)
319 return;
320 p=image->ascii85->buffer;
321 for (n=image->ascii85->offset; n >= 4; n-=4)
322 {
323 for (q=Ascii85Tuple(p); *q != '\0'; q++)
324 {
325 image->ascii85->line_break--;
326 if ((image->ascii85->line_break < 0) && (*q != '%'))
327 {
328 (void) WriteBlobByte(image,'\n');
329 image->ascii85->line_break=2*MaxLineExtent;
330 }
331 (void) WriteBlobByte(image,(unsigned char) *q);
332 }
333 p+=8;
334 }
335 image->ascii85->offset=n;
336 p-=4;
337 for (n=0; n < 4; n++)
338 image->ascii85->buffer[n]=(*p++);
339}
340
341/*
342%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
343% %
344% %
345% %
346% H u f f m a n D e c o d e I m a g e %
347% %
348% %
349% %
350%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
351%
352% HuffmanDecodeImage() uncompresses an image via Huffman-coding.
353%
354% The format of the HuffmanDecodeImage method is:
355%
cristy018f07f2011-09-04 21:15:19 +0000356% MagickBooleanType HuffmanDecodeImage(Image *image,
357% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000358%
359% A description of each parameter follows:
360%
361% o image: the image.
362%
cristy018f07f2011-09-04 21:15:19 +0000363% o exception: return any errors or warnings in this structure.
364%
cristy3ed852e2009-09-05 21:47:34 +0000365*/
366
367static inline size_t MagickMax(const size_t x,const size_t y)
368{
369 if (x > y)
370 return(x);
371 return(y);
372}
373
374static inline size_t MagickMin(const size_t x,const size_t y)
375{
376 if (x < y)
377 return(x);
378 return(y);
379}
380
cristy018f07f2011-09-04 21:15:19 +0000381MagickExport MagickBooleanType HuffmanDecodeImage(Image *image,
382 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000383{
384#define HashSize 1021
385#define MBHashA 293
386#define MBHashB 2695
387#define MWHashA 3510
388#define MWHashB 1178
389
390#define InitializeHashTable(hash,table,a,b) \
391{ \
392 entry=table; \
393 while (entry->code != 0) \
394 { \
395 hash[((entry->length+a)*(entry->code+b)) % HashSize]=(HuffmanTable *) entry; \
396 entry++; \
397 } \
398}
399
400#define InputBit(bit) \
401{ \
402 if ((mask & 0xff) == 0) \
403 { \
404 byte=ReadBlobByte(image); \
405 if (byte == EOF) \
406 break; \
407 mask=0x80; \
408 } \
409 runlength++; \
cristybb503372010-05-27 20:51:26 +0000410 bit=(size_t) ((byte & mask) != 0 ? 0x01 : 0x00); \
cristy3ed852e2009-09-05 21:47:34 +0000411 mask>>=1; \
412 if (bit != 0) \
413 runlength=0; \
414}
415
cristyc5c6f662010-09-22 14:23:02 +0000416 CacheView
417 *image_view;
418
cristy3ed852e2009-09-05 21:47:34 +0000419 const HuffmanTable
420 *entry;
421
cristy3ed852e2009-09-05 21:47:34 +0000422 HuffmanTable
423 **mb_hash,
424 **mw_hash;
425
cristy3ed852e2009-09-05 21:47:34 +0000426 int
427 byte;
428
cristy3ed852e2009-09-05 21:47:34 +0000429 MagickBooleanType
430 proceed;
431
cristy4c08aed2011-07-01 19:47:50 +0000432 Quantum
433 index;
cristy3ed852e2009-09-05 21:47:34 +0000434
cristybb503372010-05-27 20:51:26 +0000435 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000436 i;
437
438 register unsigned char
439 *p;
440
cristy9d314ff2011-03-09 01:30:28 +0000441 size_t
442 bit,
443 code,
444 mask,
445 length,
446 null_lines,
447 runlength;
448
cristy3ed852e2009-09-05 21:47:34 +0000449 ssize_t
cristyc5c6f662010-09-22 14:23:02 +0000450 count,
451 y;
cristy3ed852e2009-09-05 21:47:34 +0000452
453 unsigned char
454 *scanline;
455
456 unsigned int
457 bail,
458 color;
459
cristy3ed852e2009-09-05 21:47:34 +0000460 /*
461 Allocate buffers.
462 */
463 assert(image != (Image *) NULL);
464 assert(image->signature == MagickSignature);
465 if (image->debug != MagickFalse)
466 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
467 mb_hash=(HuffmanTable **) AcquireQuantumMemory(HashSize,sizeof(*mb_hash));
468 mw_hash=(HuffmanTable **) AcquireQuantumMemory(HashSize,sizeof(*mw_hash));
469 scanline=(unsigned char *) AcquireQuantumMemory((size_t) image->columns,
470 sizeof(*scanline));
471 if ((mb_hash == (HuffmanTable **) NULL) ||
472 (mw_hash == (HuffmanTable **) NULL) ||
473 (scanline == (unsigned char *) NULL))
474 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
475 image->filename);
476 /*
477 Initialize Huffman tables.
478 */
479 for (i=0; i < HashSize; i++)
480 {
481 mb_hash[i]=(HuffmanTable *) NULL;
482 mw_hash[i]=(HuffmanTable *) NULL;
483 }
484 InitializeHashTable(mw_hash,TWTable,MWHashA,MWHashB);
485 InitializeHashTable(mw_hash,MWTable,MWHashA,MWHashB);
486 InitializeHashTable(mw_hash,EXTable,MWHashA,MWHashB);
487 InitializeHashTable(mb_hash,TBTable,MBHashA,MBHashB);
488 InitializeHashTable(mb_hash,MBTable,MBHashA,MBHashB);
489 InitializeHashTable(mb_hash,EXTable,MBHashA,MBHashB);
490 /*
491 Uncompress 1D Huffman to runlength encoded pixels.
492 */
493 byte=0;
494 mask=0;
495 null_lines=0;
496 runlength=0;
497 while (runlength < 11)
498 InputBit(bit);
499 do { InputBit(bit); } while ((int) bit == 0);
cristy2a11bef2011-10-28 18:33:11 +0000500 image->resolution.x=204.0;
501 image->resolution.y=196.0;
cristy3ed852e2009-09-05 21:47:34 +0000502 image->units=PixelsPerInchResolution;
cristydb070952012-04-20 14:33:00 +0000503 image_view=AcquireAuthenticCacheView(image,exception);
cristybb503372010-05-27 20:51:26 +0000504 for (y=0; ((y < (ssize_t) image->rows) && (null_lines < 3)); )
cristy3ed852e2009-09-05 21:47:34 +0000505 {
cristy4c08aed2011-07-01 19:47:50 +0000506 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000507 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000508
cristyc5c6f662010-09-22 14:23:02 +0000509 register ssize_t
510 x;
511
cristy3ed852e2009-09-05 21:47:34 +0000512 /*
513 Initialize scanline to white.
514 */
515 p=scanline;
cristybb503372010-05-27 20:51:26 +0000516 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000517 *p++=(unsigned char) 0;
518 /*
519 Decode Huffman encoded scanline.
520 */
521 color=MagickTrue;
522 code=0;
523 count=0;
524 length=0;
525 runlength=0;
526 x=0;
527 for ( ; ; )
528 {
529 if (byte == EOF)
530 break;
cristybb503372010-05-27 20:51:26 +0000531 if (x >= (ssize_t) image->columns)
cristy3ed852e2009-09-05 21:47:34 +0000532 {
533 while (runlength < 11)
534 InputBit(bit);
535 do { InputBit(bit); } while ((int) bit == 0);
536 break;
537 }
538 bail=MagickFalse;
539 do
540 {
541 if (runlength < 11)
542 InputBit(bit)
543 else
544 {
545 InputBit(bit);
546 if ((int) bit != 0)
547 {
548 null_lines++;
549 if (x != 0)
550 null_lines=0;
551 bail=MagickTrue;
552 break;
553 }
554 }
cristybb503372010-05-27 20:51:26 +0000555 code=(code << 1)+(size_t) bit;
cristy3ed852e2009-09-05 21:47:34 +0000556 length++;
557 } while (code == 0);
558 if (bail != MagickFalse)
559 break;
560 if (length > 13)
561 {
562 while (runlength < 11)
563 InputBit(bit);
564 do { InputBit(bit); } while ((int) bit == 0);
565 break;
566 }
567 if (color != MagickFalse)
568 {
569 if (length < 4)
570 continue;
571 entry=mw_hash[((length+MWHashA)*(code+MWHashB)) % HashSize];
572 }
573 else
574 {
575 if (length < 2)
576 continue;
577 entry=mb_hash[((length+MBHashA)*(code+MBHashB)) % HashSize];
578 }
579 if (entry == (const HuffmanTable *) NULL)
580 continue;
581 if ((entry->length != length) || (entry->code != code))
582 continue;
583 switch (entry->id)
584 {
585 case TWId:
586 case TBId:
587 {
cristyeaedf062010-05-29 22:36:02 +0000588 count+=(ssize_t) entry->count;
cristybb503372010-05-27 20:51:26 +0000589 if ((x+count) > (ssize_t) image->columns)
cristy3ed852e2009-09-05 21:47:34 +0000590 count=(ssize_t) image->columns-x;
591 if (count > 0)
592 {
593 if (color != MagickFalse)
594 {
595 x+=count;
596 count=0;
597 }
598 else
599 for ( ; count > 0; count--)
600 scanline[x++]=(unsigned char) 1;
601 }
602 color=(unsigned int)
603 ((color == MagickFalse) ? MagickTrue : MagickFalse);
604 break;
605 }
606 case MWId:
607 case MBId:
608 case EXId:
609 {
cristyeaedf062010-05-29 22:36:02 +0000610 count+=(ssize_t) entry->count;
cristy3ed852e2009-09-05 21:47:34 +0000611 break;
612 }
613 default:
614 break;
615 }
616 code=0;
617 length=0;
618 }
619 /*
620 Transfer scanline to image pixels.
621 */
622 p=scanline;
cristyc5c6f662010-09-22 14:23:02 +0000623 q=QueueCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +0000624 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000625 break;
cristybb503372010-05-27 20:51:26 +0000626 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000627 {
cristy4c08aed2011-07-01 19:47:50 +0000628 index=(Quantum) (*p++);
629 SetPixelIndex(image,index,q);
cristy803640d2011-11-17 02:11:32 +0000630 SetPixelInfoPixel(image,image->colormap+(ssize_t) index,q);
cristyed231572011-07-14 02:18:59 +0000631 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000632 }
cristyc5c6f662010-09-22 14:23:02 +0000633 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000634 break;
635 proceed=SetImageProgress(image,LoadImageTag,y,image->rows);
636 if (proceed == MagickFalse)
637 break;
638 y++;
639 }
cristyc5c6f662010-09-22 14:23:02 +0000640 image_view=DestroyCacheView(image_view);
cristybb503372010-05-27 20:51:26 +0000641 image->rows=(size_t) MagickMax((size_t) y-3,1);
cristy3ed852e2009-09-05 21:47:34 +0000642 image->compression=FaxCompression;
643 /*
644 Free decoder memory.
645 */
646 mw_hash=(HuffmanTable **) RelinquishMagickMemory(mw_hash);
647 mb_hash=(HuffmanTable **) RelinquishMagickMemory(mb_hash);
648 scanline=(unsigned char *) RelinquishMagickMemory(scanline);
649 return(MagickTrue);
650}
651
652/*
653%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
654% %
655% %
656% %
657% H u f f m a n E n c o d e I m a g e %
658% %
659% %
660% %
661%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
662%
663% HuffmanEncodeImage() compresses an image via Huffman-coding.
664%
665% The format of the HuffmanEncodeImage method is:
666%
667% MagickBooleanType HuffmanEncodeImage(const ImageInfo *image_info,
cristy018f07f2011-09-04 21:15:19 +0000668% Image *image,Image *inject_image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000669%
670% A description of each parameter follows:
671%
672% o image_info: the image info..
673%
674% o image: the image.
675%
676% o inject_image: inject into the image stream.
677%
cristy018f07f2011-09-04 21:15:19 +0000678% o exception: return any errors or warnings in this structure.
679%
cristy3ed852e2009-09-05 21:47:34 +0000680*/
681MagickExport MagickBooleanType HuffmanEncodeImage(const ImageInfo *image_info,
cristy018f07f2011-09-04 21:15:19 +0000682 Image *image,Image *inject_image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000683{
684#define HuffmanOutputCode(entry) \
685{ \
cristyeaedf062010-05-29 22:36:02 +0000686 mask=one << (entry->length-1); \
cristy3ed852e2009-09-05 21:47:34 +0000687 while (mask != 0) \
688 { \
689 OutputBit(((entry->code & mask) != 0 ? 1 : 0)); \
690 mask>>=1; \
691 } \
692}
693
694#define OutputBit(count) \
695{ \
696 if (count > 0) \
697 byte=byte | bit; \
698 bit>>=1; \
699 if ((int) (bit & 0xff) == 0) \
700 { \
701 if (LocaleCompare(image_info->magick,"FAX") == 0) \
702 (void) WriteBlobByte(image,(unsigned char) byte); \
703 else \
704 Ascii85Encode(image,byte); \
705 byte='\0'; \
706 bit=(unsigned char) 0x80; \
707 } \
708}
709
710 const HuffmanTable
711 *entry;
712
cristy3ed852e2009-09-05 21:47:34 +0000713 int
714 k,
715 runlength;
716
cristy3ed852e2009-09-05 21:47:34 +0000717 Image
718 *huffman_image;
719
720 MagickBooleanType
721 proceed;
722
cristybb503372010-05-27 20:51:26 +0000723 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000724 i,
725 x;
726
cristy4c08aed2011-07-01 19:47:50 +0000727 register const Quantum
cristy3ed852e2009-09-05 21:47:34 +0000728 *p;
729
730 register unsigned char
731 *q;
732
cristybb503372010-05-27 20:51:26 +0000733 size_t
cristy3ed852e2009-09-05 21:47:34 +0000734 mask,
cristyeaedf062010-05-29 22:36:02 +0000735 one,
cristy3ed852e2009-09-05 21:47:34 +0000736 width;
737
cristy9d314ff2011-03-09 01:30:28 +0000738 ssize_t
739 n,
740 y;
741
742 unsigned char
743 byte,
744 bit,
745 *scanline;
746
cristy3ed852e2009-09-05 21:47:34 +0000747 /*
748 Allocate scanline buffer.
749 */
750 assert(image_info != (ImageInfo *) NULL);
751 assert(image_info->signature == MagickSignature);
752 assert(image != (Image *) NULL);
753 assert(image->signature == MagickSignature);
754 if (image->debug != MagickFalse)
755 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
756 assert(inject_image != (Image *) NULL);
757 assert(inject_image->signature == MagickSignature);
cristyeaedf062010-05-29 22:36:02 +0000758 one=1;
cristy3ed852e2009-09-05 21:47:34 +0000759 width=inject_image->columns;
760 if (LocaleCompare(image_info->magick,"FAX") == 0)
cristybb503372010-05-27 20:51:26 +0000761 width=(size_t) MagickMax(inject_image->columns,1728);
cristy3ed852e2009-09-05 21:47:34 +0000762 scanline=(unsigned char *) AcquireQuantumMemory((size_t) width+1UL,
763 sizeof(*scanline));
764 if (scanline == (unsigned char *) NULL)
765 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
766 inject_image->filename);
767 (void) ResetMagickMemory(scanline,0,width*sizeof(*scanline));
cristyc82a27b2011-10-21 01:07:16 +0000768 huffman_image=CloneImage(inject_image,0,0,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +0000769 if (huffman_image == (Image *) NULL)
770 {
771 scanline=(unsigned char *) RelinquishMagickMemory(scanline);
772 return(MagickFalse);
773 }
cristy018f07f2011-09-04 21:15:19 +0000774 (void) SetImageType(huffman_image,BilevelType,exception);
cristy3ed852e2009-09-05 21:47:34 +0000775 byte='\0';
776 bit=(unsigned char) 0x80;
777 if (LocaleCompare(image_info->magick,"FAX") != 0)
778 Ascii85Initialize(image);
779 else
780 {
781 /*
782 End of line.
783 */
784 for (k=0; k < 11; k++)
785 OutputBit(0);
786 OutputBit(1);
787 }
788 /*
789 Compress to 1D Huffman pixels.
790 */
cristy3ed852e2009-09-05 21:47:34 +0000791 q=scanline;
cristybb503372010-05-27 20:51:26 +0000792 for (y=0; y < (ssize_t) huffman_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000793 {
794 p=GetVirtualPixels(huffman_image,0,y,huffman_image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +0000795 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000796 break;
cristybb503372010-05-27 20:51:26 +0000797 for (x=0; x < (ssize_t) huffman_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000798 {
cristy4c08aed2011-07-01 19:47:50 +0000799 *q++=(unsigned char) (GetPixelIntensity(huffman_image,p) >=
cristya19f1d72012-08-07 18:24:38 +0000800 ((double) QuantumRange/2.0) ? 0 : 1);
cristyed231572011-07-14 02:18:59 +0000801 p+=GetPixelChannels(huffman_image);
cristy3ed852e2009-09-05 21:47:34 +0000802 }
803 /*
804 Huffman encode scanline.
805 */
806 q=scanline;
cristybb503372010-05-27 20:51:26 +0000807 for (n=(ssize_t) width; n > 0; )
cristy3ed852e2009-09-05 21:47:34 +0000808 {
809 /*
810 Output white run.
811 */
812 for (runlength=0; ((n > 0) && (*q == 0)); n--)
813 {
814 q++;
815 runlength++;
816 }
817 if (runlength >= 64)
818 {
819 if (runlength < 1792)
820 entry=MWTable+((runlength/64)-1);
821 else
822 entry=EXTable+(MagickMin((size_t) runlength,2560)-1792)/64;
cristy8891f9c2010-06-04 23:32:17 +0000823 runlength-=(long) entry->count;
cristy3ed852e2009-09-05 21:47:34 +0000824 HuffmanOutputCode(entry);
825 }
826 entry=TWTable+MagickMin((size_t) runlength,63);
827 HuffmanOutputCode(entry);
828 if (n != 0)
829 {
830 /*
831 Output black run.
832 */
833 for (runlength=0; ((*q != 0) && (n > 0)); n--)
834 {
835 q++;
836 runlength++;
837 }
838 if (runlength >= 64)
839 {
840 entry=MBTable+((runlength/64)-1);
841 if (runlength >= 1792)
842 entry=EXTable+(MagickMin((size_t) runlength,2560)-1792)/64;
cristy8891f9c2010-06-04 23:32:17 +0000843 runlength-=(long) entry->count;
cristy3ed852e2009-09-05 21:47:34 +0000844 HuffmanOutputCode(entry);
845 }
846 entry=TBTable+MagickMin((size_t) runlength,63);
847 HuffmanOutputCode(entry);
848 }
849 }
850 /*
851 End of line.
852 */
853 for (k=0; k < 11; k++)
854 OutputBit(0);
855 OutputBit(1);
856 q=scanline;
857 if (GetPreviousImageInList(huffman_image) == (Image *) NULL)
858 {
859 proceed=SetImageProgress(huffman_image,LoadImageTag,y,
860 huffman_image->rows);
861 if (proceed == MagickFalse)
862 break;
863 }
864 }
865 /*
866 End of page.
867 */
868 for (i=0; i < 6; i++)
869 {
870 for (k=0; k < 11; k++)
871 OutputBit(0);
872 OutputBit(1);
873 }
874 /*
875 Flush bits.
876 */
877 if (((int) bit != 0x80) != 0)
878 {
879 if (LocaleCompare(image_info->magick,"FAX") == 0)
880 (void) WriteBlobByte(image,byte);
881 else
882 Ascii85Encode(image,byte);
883 }
884 if (LocaleCompare(image_info->magick,"FAX") != 0)
885 Ascii85Flush(image);
886 huffman_image=DestroyImage(huffman_image);
887 scanline=(unsigned char *) RelinquishMagickMemory(scanline);
888 return(MagickTrue);
889}
890
cristy3ed852e2009-09-05 21:47:34 +0000891/*
892%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
893% %
894% %
895% %
896% L Z W E n c o d e I m a g e %
897% %
898% %
899% %
900%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
901%
902% LZWEncodeImage() compresses an image via LZW-coding specific to Postscript
903% Level II or Portable Document Format.
904%
905% The format of the LZWEncodeImage method is:
906%
907% MagickBooleanType LZWEncodeImage(Image *image,const size_t length,
cristy018f07f2011-09-04 21:15:19 +0000908% unsigned char *pixels,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000909%
910% A description of each parameter follows:
911%
912% o image: the image.
913%
914% o length: A value that specifies the number of pixels to compress.
915%
916% o pixels: the address of an unsigned array of characters containing the
917% pixels to compress.
918%
cristy018f07f2011-09-04 21:15:19 +0000919% o exception: return any errors or warnings in this structure.
920%
cristy3ed852e2009-09-05 21:47:34 +0000921*/
922MagickExport MagickBooleanType LZWEncodeImage(Image *image,const size_t length,
cristy018f07f2011-09-04 21:15:19 +0000923 unsigned char *pixels,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000924{
925#define LZWClr 256UL /* Clear Table Marker */
926#define LZWEod 257UL /* End of Data marker */
927#define OutputCode(code) \
928{ \
929 accumulator+=code << (32-code_width-number_bits); \
930 number_bits+=code_width; \
931 while (number_bits >= 8) \
932 { \
933 (void) WriteBlobByte(image,(unsigned char) (accumulator >> 24)); \
934 accumulator=accumulator << 8; \
935 number_bits-=8; \
936 } \
937}
938
939 typedef struct _TableType
940 {
cristybb503372010-05-27 20:51:26 +0000941 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000942 prefix,
943 suffix,
944 next;
945 } TableType;
946
cristybb503372010-05-27 20:51:26 +0000947 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000948 i;
949
cristybb503372010-05-27 20:51:26 +0000950 size_t
cristy3ed852e2009-09-05 21:47:34 +0000951 accumulator,
952 number_bits,
953 code_width,
954 last_code,
955 next_index;
956
cristy9d314ff2011-03-09 01:30:28 +0000957 ssize_t
958 index;
959
960 TableType
961 *table;
962
cristy3ed852e2009-09-05 21:47:34 +0000963 /*
964 Allocate string table.
965 */
966 assert(image != (Image *) NULL);
967 assert(image->signature == MagickSignature);
968 if (image->debug != MagickFalse)
969 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
970 assert(pixels != (unsigned char *) NULL);
971 table=(TableType *) AcquireQuantumMemory(1UL << 12,sizeof(*table));
972 if (table == (TableType *) NULL)
973 return(MagickFalse);
974 /*
975 Initialize variables.
976 */
977 accumulator=0;
978 code_width=9;
979 number_bits=0;
980 last_code=0;
981 OutputCode(LZWClr);
982 for (index=0; index < 256; index++)
983 {
984 table[index].prefix=(-1);
985 table[index].suffix=(short) index;
986 table[index].next=(-1);
987 }
988 next_index=LZWEod+1;
989 code_width=9;
cristybb503372010-05-27 20:51:26 +0000990 last_code=(size_t) pixels[0];
991 for (i=1; i < (ssize_t) length; i++)
cristy3ed852e2009-09-05 21:47:34 +0000992 {
993 /*
994 Find string.
995 */
cristybb503372010-05-27 20:51:26 +0000996 index=(ssize_t) last_code;
cristy3ed852e2009-09-05 21:47:34 +0000997 while (index != -1)
cristybb503372010-05-27 20:51:26 +0000998 if ((table[index].prefix != (ssize_t) last_code) ||
999 (table[index].suffix != (ssize_t) pixels[i]))
cristy3ed852e2009-09-05 21:47:34 +00001000 index=table[index].next;
1001 else
1002 {
cristybb503372010-05-27 20:51:26 +00001003 last_code=(size_t) index;
cristy3ed852e2009-09-05 21:47:34 +00001004 break;
1005 }
cristybb503372010-05-27 20:51:26 +00001006 if (last_code != (size_t) index)
cristy3ed852e2009-09-05 21:47:34 +00001007 {
1008 /*
1009 Add string.
1010 */
1011 OutputCode(last_code);
cristybb503372010-05-27 20:51:26 +00001012 table[next_index].prefix=(ssize_t) last_code;
cristy3ed852e2009-09-05 21:47:34 +00001013 table[next_index].suffix=(short) pixels[i];
1014 table[next_index].next=table[last_code].next;
cristybb503372010-05-27 20:51:26 +00001015 table[last_code].next=(ssize_t) next_index;
cristy3ed852e2009-09-05 21:47:34 +00001016 next_index++;
1017 /*
1018 Did we just move up to next bit width?
1019 */
1020 if ((next_index >> code_width) != 0)
1021 {
1022 code_width++;
1023 if (code_width > 12)
1024 {
1025 /*
1026 Did we overflow the max bit width?
1027 */
1028 code_width--;
1029 OutputCode(LZWClr);
1030 for (index=0; index < 256; index++)
1031 {
1032 table[index].prefix=(-1);
1033 table[index].suffix=index;
1034 table[index].next=(-1);
1035 }
1036 next_index=LZWEod+1;
1037 code_width=9;
1038 }
1039 }
cristybb503372010-05-27 20:51:26 +00001040 last_code=(size_t) pixels[i];
cristy3ed852e2009-09-05 21:47:34 +00001041 }
1042 }
1043 /*
1044 Flush tables.
1045 */
1046 OutputCode(last_code);
1047 OutputCode(LZWEod);
1048 if (number_bits != 0)
1049 (void) WriteBlobByte(image,(unsigned char) (accumulator >> 24));
1050 table=(TableType *) RelinquishMagickMemory(table);
1051 return(MagickTrue);
1052}
1053
1054/*
1055%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1056% %
1057% %
1058% %
1059% P a c k b i t s E n c o d e I m a g e %
1060% %
1061% %
1062% %
1063%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1064%
1065% PackbitsEncodeImage() compresses an image via Macintosh Packbits encoding
1066% specific to Postscript Level II or Portable Document Format. To ensure
1067% portability, the binary Packbits bytes are encoded as ASCII Base-85.
1068%
1069% The format of the PackbitsEncodeImage method is:
1070%
1071% MagickBooleanType PackbitsEncodeImage(Image *image,const size_t length,
1072% unsigned char *pixels)
1073%
1074% A description of each parameter follows:
1075%
1076% o image: the image.
1077%
1078% o length: A value that specifies the number of pixels to compress.
1079%
1080% o pixels: the address of an unsigned array of characters containing the
1081% pixels to compress.
1082%
1083*/
1084MagickExport MagickBooleanType PackbitsEncodeImage(Image *image,
cristy018f07f2011-09-04 21:15:19 +00001085 const size_t length,unsigned char *pixels,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001086{
1087 int
1088 count;
1089
cristybb503372010-05-27 20:51:26 +00001090 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001091 i,
1092 j;
1093
1094 unsigned char
1095 *packbits;
1096
1097 /*
1098 Compress pixels with Packbits encoding.
1099 */
1100 assert(image != (Image *) NULL);
1101 assert(image->signature == MagickSignature);
1102 if (image->debug != MagickFalse)
1103 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1104 assert(pixels != (unsigned char *) NULL);
1105 packbits=(unsigned char *) AcquireQuantumMemory(128UL,sizeof(*packbits));
1106 if (packbits == (unsigned char *) NULL)
1107 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
1108 image->filename);
cristybb503372010-05-27 20:51:26 +00001109 for (i=(ssize_t) length; i != 0; )
cristy3ed852e2009-09-05 21:47:34 +00001110 {
1111 switch (i)
1112 {
1113 case 1:
1114 {
1115 i--;
1116 (void) WriteBlobByte(image,(unsigned char) 0);
1117 (void) WriteBlobByte(image,*pixels);
1118 break;
1119 }
1120 case 2:
1121 {
1122 i-=2;
1123 (void) WriteBlobByte(image,(unsigned char) 1);
1124 (void) WriteBlobByte(image,*pixels);
1125 (void) WriteBlobByte(image,pixels[1]);
1126 break;
1127 }
1128 case 3:
1129 {
1130 i-=3;
1131 if ((*pixels == *(pixels+1)) && (*(pixels+1) == *(pixels+2)))
1132 {
1133 (void) WriteBlobByte(image,(unsigned char) ((256-3)+1));
1134 (void) WriteBlobByte(image,*pixels);
1135 break;
1136 }
1137 (void) WriteBlobByte(image,(unsigned char) 2);
1138 (void) WriteBlobByte(image,*pixels);
1139 (void) WriteBlobByte(image,pixels[1]);
1140 (void) WriteBlobByte(image,pixels[2]);
1141 break;
1142 }
1143 default:
1144 {
1145 if ((*pixels == *(pixels+1)) && (*(pixels+1) == *(pixels+2)))
1146 {
1147 /*
1148 Packed run.
1149 */
1150 count=3;
cristybb503372010-05-27 20:51:26 +00001151 while (((ssize_t) count < i) && (*pixels == *(pixels+count)))
cristy3ed852e2009-09-05 21:47:34 +00001152 {
1153 count++;
1154 if (count >= 127)
1155 break;
1156 }
1157 i-=count;
1158 (void) WriteBlobByte(image,(unsigned char) ((256-count)+1));
1159 (void) WriteBlobByte(image,*pixels);
1160 pixels+=count;
1161 break;
1162 }
1163 /*
1164 Literal run.
1165 */
1166 count=0;
1167 while ((*(pixels+count) != *(pixels+count+1)) ||
1168 (*(pixels+count+1) != *(pixels+count+2)))
1169 {
1170 packbits[count+1]=pixels[count];
1171 count++;
cristybb503372010-05-27 20:51:26 +00001172 if (((ssize_t) count >= (i-3)) || (count >= 127))
cristy3ed852e2009-09-05 21:47:34 +00001173 break;
1174 }
1175 i-=count;
1176 *packbits=(unsigned char) (count-1);
cristybb503372010-05-27 20:51:26 +00001177 for (j=0; j <= (ssize_t) count; j++)
cristy3ed852e2009-09-05 21:47:34 +00001178 (void) WriteBlobByte(image,packbits[j]);
1179 pixels+=count;
1180 break;
1181 }
1182 }
1183 }
1184 (void) WriteBlobByte(image,(unsigned char) 128); /* EOD marker */
1185 packbits=(unsigned char *) RelinquishMagickMemory(packbits);
1186 return(MagickTrue);
1187}
1188
1189#if defined(MAGICKCORE_ZLIB_DELEGATE)
1190/*
1191%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1192% %
1193% %
1194% %
1195% Z L I B E n c o d e I m a g e %
1196% %
1197% %
1198% %
1199%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1200%
1201% ZLIBEncodeImage compresses an image via ZLIB-coding specific to
1202% Postscript Level II or Portable Document Format.
1203%
1204% The format of the ZLIBEncodeImage method is:
1205%
1206% MagickBooleanType ZLIBEncodeImage(Image *image,const size_t length,
cristy018f07f2011-09-04 21:15:19 +00001207% unsigned char *pixels,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001208%
1209% A description of each parameter follows:
1210%
1211% o file: the address of a structure of type FILE. ZLIB encoded pixels
1212% are written to this file.
1213%
1214% o length: A value that specifies the number of pixels to compress.
1215%
1216% o pixels: the address of an unsigned array of characters containing the
1217% pixels to compress.
1218%
cristy018f07f2011-09-04 21:15:19 +00001219% o exception: return any errors or warnings in this structure.
1220%
cristy3ed852e2009-09-05 21:47:34 +00001221*/
1222
1223static voidpf AcquireZIPMemory(voidpf context,unsigned int items,
1224 unsigned int size)
1225{
1226 (void) context;
1227 return((voidpf) AcquireQuantumMemory(items,size));
1228}
1229
1230static void RelinquishZIPMemory(voidpf context,voidpf memory)
1231{
1232 (void) context;
1233 memory=RelinquishMagickMemory(memory);
1234}
1235
1236MagickExport MagickBooleanType ZLIBEncodeImage(Image *image,const size_t length,
cristy018f07f2011-09-04 21:15:19 +00001237 unsigned char *pixels,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001238{
1239 int
1240 status;
1241
cristybb503372010-05-27 20:51:26 +00001242 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001243 i;
1244
1245 size_t
1246 compress_packets;
1247
1248 unsigned char
1249 *compress_pixels;
1250
1251 z_stream
1252 stream;
1253
1254 assert(image != (Image *) NULL);
1255 assert(image->signature == MagickSignature);
1256 if (image->debug != MagickFalse)
1257 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1258 compress_packets=(size_t) (1.001*length+12);
1259 compress_pixels=(unsigned char *) AcquireQuantumMemory(compress_packets,
1260 sizeof(*compress_pixels));
1261 if (compress_pixels == (unsigned char *) NULL)
1262 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
1263 image->filename);
1264 stream.next_in=pixels;
1265 stream.avail_in=(unsigned int) length;
1266 stream.next_out=compress_pixels;
1267 stream.avail_out=(unsigned int) compress_packets;
1268 stream.zalloc=AcquireZIPMemory;
1269 stream.zfree=RelinquishZIPMemory;
1270 stream.opaque=(voidpf) NULL;
1271 status=deflateInit(&stream,(int) (image->quality ==
1272 UndefinedCompressionQuality ? 7 : MagickMin(image->quality/10,9)));
1273 if (status == Z_OK)
1274 {
1275 status=deflate(&stream,Z_FINISH);
1276 if (status == Z_STREAM_END)
1277 status=deflateEnd(&stream);
1278 else
1279 (void) deflateEnd(&stream);
1280 compress_packets=(size_t) stream.total_out;
1281 }
1282 if (status != Z_OK)
1283 ThrowBinaryException(CoderError,"UnableToZipCompressImage",image->filename)
1284 else
cristybb503372010-05-27 20:51:26 +00001285 for (i=0; i < (ssize_t) compress_packets; i++)
cristy3ed852e2009-09-05 21:47:34 +00001286 (void) WriteBlobByte(image,compress_pixels[i]);
1287 compress_pixels=(unsigned char *) RelinquishMagickMemory(compress_pixels);
1288 return(status == Z_OK ? MagickTrue : MagickFalse);
1289}
1290#else
1291MagickExport MagickBooleanType ZLIBEncodeImage(Image *image,
cristy54519322012-02-21 13:32:22 +00001292 const size_t magick_unused(length),unsigned char *magick_unused(pixels),
1293 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001294{
1295 assert(image != (Image *) NULL);
1296 assert(image->signature == MagickSignature);
1297 if (image->debug != MagickFalse)
1298 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy54519322012-02-21 13:32:22 +00001299 (void) ThrowMagickException(exception,GetMagickModule(),MissingDelegateError,
anthonye5b39652012-04-21 05:37:29 +00001300 "DelegateLibrarySupportNotBuiltIn","'%s' (ZIP)",image->filename);
cristy3ed852e2009-09-05 21:47:34 +00001301 return(MagickFalse);
1302}
1303#endif