]> granicus.if.org Git - imagemagick/blob - MagickCore/compress.c
b79ef237ed09615361517cbe8b595a3f9872bad7
[imagemagick] / MagickCore / compress.c
1 /*
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 %                                                                             %
20 %  Copyright 1999-2014 ImageMagick Studio LLC, a non-profit organization      %
21 %  dedicated to making software imaging solutions freely available.           %
22 %                                                                             %
23 %  You may not use this file except in compliance with the License.  You may  %
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 \f
40 /*
41   Include declarations.
42 */
43 #include "MagickCore/studio.h"
44 #include "MagickCore/attribute.h"
45 #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"
62 #if defined(MAGICKCORE_ZLIB_DELEGATE)
63 #include "zlib.h"
64 #endif
65 \f
66 /*
67   Typedef declarations.
68 */
69 struct _Ascii85Info
70 {
71   ssize_t
72     offset,
73     line_break;
74
75   unsigned char
76     buffer[10];
77 };
78
79 typedef struct HuffmanTable
80 {
81   size_t
82     id,
83     code,
84     length,
85     count;
86 } HuffmanTable;
87 \f
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
97 static 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
116 static 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
128 static 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
147 static 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
174 static 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 \f
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 %
218 %      void Ascii85Encode(Image *image,const size_t code)
219 %
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
230 static char *Ascii85Tuple(unsigned char *data)
231 {
232   static char
233     tuple[6];
234
235   register ssize_t
236     i,
237     x;
238
239   size_t
240     code,
241     quantum;
242
243   code=((((size_t) data[0] << 8) | (size_t) data[1]) << 16) |
244     ((size_t) data[2] << 8) | (size_t) data[3];
245   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   {
254     x=(ssize_t) (code/quantum);
255     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
264 MagickExport void Ascii85Initialize(Image *image)
265 {
266   /*
267     Allocate image structure.
268   */
269   if (image->ascii85 == (Ascii85Info *) NULL)
270     image->ascii85=(Ascii85Info *) AcquireMagickMemory(sizeof(*image->ascii85));
271   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
278 MagickExport 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
302 MagickExport void Ascii85Encode(Image *image,const unsigned char code)
303 {
304   register char
305     *q;
306
307   register unsigned char
308     *p;
309
310   ssize_t
311     n;
312
313   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 \f
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 %
356 %      MagickBooleanType HuffmanDecodeImage(Image *image,
357 %        ExceptionInfo *exception)
358 %
359 %  A description of each parameter follows:
360 %
361 %    o image: the image.
362 %
363 %    o exception: return any errors or warnings in this structure.
364 %
365 */
366
367 static 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
374 static 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
381 MagickExport MagickBooleanType HuffmanDecodeImage(Image *image,
382   ExceptionInfo *exception)
383 {
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++;  \
410   bit=(size_t) ((byte & mask) != 0 ? 0x01 : 0x00); \
411   mask>>=1;  \
412   if (bit != 0)  \
413     runlength=0;  \
414 }
415
416   CacheView
417     *image_view;
418
419   const HuffmanTable
420     *entry;
421
422   HuffmanTable
423     **mb_hash,
424     **mw_hash;
425
426   int
427     byte;
428
429   MagickBooleanType
430     proceed;
431
432   Quantum
433     index;
434
435   register ssize_t
436     i;
437
438   register unsigned char
439     *p;
440
441   size_t
442     bit,
443     code,
444     mask,
445     length,
446     null_lines,
447     runlength;
448
449   ssize_t
450     count,
451     y;
452
453   unsigned char
454     *scanline;
455
456   unsigned int
457     bail,
458     color;
459
460   /*
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);
500   image->resolution.x=204.0;
501   image->resolution.y=196.0;
502   image->units=PixelsPerInchResolution;
503   image_view=AcquireAuthenticCacheView(image,exception);
504   for (y=0; ((y < (ssize_t) image->rows) && (null_lines < 3)); )
505   {
506     register Quantum
507       *restrict q;
508
509     register ssize_t
510       x;
511
512     /*
513       Initialize scanline to white.
514     */
515     p=scanline;
516     for (x=0; x < (ssize_t) image->columns; x++)
517       *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;
531       if (x >= (ssize_t) image->columns)
532         {
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           }
555         code=(code << 1)+(size_t) bit;
556         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         {
588           count+=(ssize_t) entry->count;
589           if ((x+count) > (ssize_t) image->columns)
590             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         {
610           count+=(ssize_t) entry->count;
611           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;
623     q=QueueCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
624     if (q == (Quantum *) NULL)
625       break;
626     for (x=0; x < (ssize_t) image->columns; x++)
627     {
628       index=(Quantum) (*p++);
629       SetPixelIndex(image,index,q);
630       SetPixelInfoPixel(image,image->colormap+(ssize_t) index,q);
631       q+=GetPixelChannels(image);
632     }
633     if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
634       break;
635     proceed=SetImageProgress(image,LoadImageTag,y,image->rows);
636     if (proceed == MagickFalse)
637       break;
638     y++;
639   }
640   image_view=DestroyCacheView(image_view);
641   image->rows=(size_t) MagickMax((size_t) y-3,1);
642   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 \f
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,
668 %        Image *image,Image *inject_image,ExceptionInfo *exception)
669 %
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 %
678 %    o exception: return any errors or warnings in this structure.
679 %
680 */
681 MagickExport MagickBooleanType HuffmanEncodeImage(const ImageInfo *image_info,
682   Image *image,Image *inject_image,ExceptionInfo *exception)
683 {
684 #define HuffmanOutputCode(entry)  \
685 {  \
686   mask=one << (entry->length-1);  \
687   while (mask != 0)  \
688   {  \
689     OutputBit(((entry->code & mask) != 0 ? 1 : 0));  \
690     mask>>=1;  \
691   }  \
692 }
693
694 #define OutputBit(count)  \
695 {  \
696 DisableMSCWarning(4127) \
697   if (count > 0)  \
698     byte=byte | bit;  \
699 RestoreMSCWarning \
700   bit>>=1;  \
701   if ((int) (bit & 0xff) == 0)   \
702     {  \
703       if (LocaleCompare(image_info->magick,"FAX") == 0) \
704         (void) WriteBlobByte(image,(unsigned char) byte);  \
705       else \
706         Ascii85Encode(image,byte); \
707       byte='\0';  \
708       bit=(unsigned char) 0x80;  \
709     }  \
710 }
711
712   const HuffmanTable
713     *entry;
714
715   int
716     k,
717     runlength;
718
719   Image
720     *huffman_image;
721
722   MagickBooleanType
723     proceed;
724
725   register ssize_t
726     i,
727     x;
728
729   register const Quantum
730     *p;
731
732   register unsigned char
733     *q;
734
735   size_t
736     mask,
737     one,
738     width;
739
740   ssize_t
741     n,
742     y;
743
744   unsigned char
745     byte,
746     bit,
747     *scanline;
748
749   /*
750     Allocate scanline buffer.
751   */
752   assert(image_info != (ImageInfo *) NULL);
753   assert(image_info->signature == MagickSignature);
754   assert(image != (Image *) NULL);
755   assert(image->signature == MagickSignature);
756   if (image->debug != MagickFalse)
757     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
758   assert(inject_image != (Image *) NULL);
759   assert(inject_image->signature == MagickSignature);
760   one=1;
761   width=inject_image->columns;
762   if (LocaleCompare(image_info->magick,"FAX") == 0)
763     width=(size_t) MagickMax(inject_image->columns,1728);
764   scanline=(unsigned char *) AcquireQuantumMemory((size_t) width+1UL,
765     sizeof(*scanline));
766   if (scanline == (unsigned char *) NULL)
767     ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
768       inject_image->filename);
769   (void) ResetMagickMemory(scanline,0,width*sizeof(*scanline));
770   huffman_image=CloneImage(inject_image,0,0,MagickTrue,exception);
771   if (huffman_image == (Image *) NULL)
772     {
773       scanline=(unsigned char *) RelinquishMagickMemory(scanline);
774       return(MagickFalse);
775     }
776   (void) SetImageType(huffman_image,BilevelType,exception);
777   byte='\0';
778   bit=(unsigned char) 0x80;
779   if (LocaleCompare(image_info->magick,"FAX") != 0)
780     Ascii85Initialize(image);
781   else
782     {
783       /*
784         End of line.
785       */
786       for (k=0; k < 11; k++)
787         OutputBit(0);
788       OutputBit(1);
789     }
790   /*
791     Compress to 1D Huffman pixels.
792   */
793   q=scanline;
794   for (y=0; y < (ssize_t) huffman_image->rows; y++)
795   {
796     p=GetVirtualPixels(huffman_image,0,y,huffman_image->columns,1,exception);
797     if (p == (const Quantum *) NULL)
798       break;
799     for (x=0; x < (ssize_t) huffman_image->columns; x++)
800     {
801       *q++=(unsigned char) (GetPixelIntensity(huffman_image,p) >=
802         ((double) QuantumRange/2.0) ? 0 : 1);
803       p+=GetPixelChannels(huffman_image);
804     }
805     /*
806       Huffman encode scanline.
807     */
808     q=scanline;
809     for (n=(ssize_t) width; n > 0; )
810     {
811       /*
812         Output white run.
813       */
814       for (runlength=0; ((n > 0) && (*q == 0)); n--)
815       {
816         q++;
817         runlength++;
818       }
819       if (runlength >= 64)
820         {
821           if (runlength < 1792)
822             entry=MWTable+((runlength/64)-1);
823           else
824             entry=EXTable+(MagickMin((size_t) runlength,2560)-1792)/64;
825           runlength-=(long) entry->count;
826           HuffmanOutputCode(entry);
827         }
828       entry=TWTable+MagickMin((size_t) runlength,63);
829       HuffmanOutputCode(entry);
830       if (n != 0)
831         {
832           /*
833             Output black run.
834           */
835           for (runlength=0; ((*q != 0) && (n > 0)); n--)
836           {
837             q++;
838             runlength++;
839           }
840           if (runlength >= 64)
841             {
842               entry=MBTable+((runlength/64)-1);
843               if (runlength >= 1792)
844                 entry=EXTable+(MagickMin((size_t) runlength,2560)-1792)/64;
845               runlength-=(long) entry->count;
846               HuffmanOutputCode(entry);
847             }
848           entry=TBTable+MagickMin((size_t) runlength,63);
849           HuffmanOutputCode(entry);
850         }
851     }
852     /*
853       End of line.
854     */
855     for (k=0; k < 11; k++)
856       OutputBit(0);
857     OutputBit(1);
858     q=scanline;
859     if (GetPreviousImageInList(huffman_image) == (Image *) NULL)
860       {
861         proceed=SetImageProgress(huffman_image,LoadImageTag,y,
862           huffman_image->rows);
863         if (proceed == MagickFalse)
864           break;
865       }
866   }
867   /*
868     End of page.
869   */
870   for (i=0; i < 6; i++)
871   {
872     for (k=0; k < 11; k++)
873       OutputBit(0);
874     OutputBit(1);
875   }
876   /*
877     Flush bits.
878   */
879   if (((int) bit != 0x80) != 0)
880     {
881       if (LocaleCompare(image_info->magick,"FAX") == 0)
882         (void) WriteBlobByte(image,byte);
883       else
884         Ascii85Encode(image,byte);
885     }
886   if (LocaleCompare(image_info->magick,"FAX") != 0)
887     Ascii85Flush(image);
888   huffman_image=DestroyImage(huffman_image);
889   scanline=(unsigned char *) RelinquishMagickMemory(scanline);
890   return(MagickTrue);
891 }
892 \f
893 /*
894 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
895 %                                                                             %
896 %                                                                             %
897 %                                                                             %
898 %   L Z W E n c o d e I m a g e                                               %
899 %                                                                             %
900 %                                                                             %
901 %                                                                             %
902 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
903 %
904 %  LZWEncodeImage() compresses an image via LZW-coding specific to Postscript
905 %  Level II or Portable Document Format.
906 %
907 %  The format of the LZWEncodeImage method is:
908 %
909 %      MagickBooleanType LZWEncodeImage(Image *image,const size_t length,
910 %        unsigned char *restrict pixels,ExceptionInfo *exception)
911 %
912 %  A description of each parameter follows:
913 %
914 %    o image: the image.
915 %
916 %    o length:  A value that specifies the number of pixels to compress.
917 %
918 %    o pixels: the address of an unsigned array of characters containing the
919 %      pixels to compress.
920 %
921 %    o exception: return any errors or warnings in this structure.
922 %
923 */
924 MagickExport MagickBooleanType LZWEncodeImage(Image *image,const size_t length,
925   unsigned char *restrict pixels,ExceptionInfo *exception)
926 {
927 #define LZWClr  256UL  /* Clear Table Marker */
928 #define LZWEod  257UL  /* End of Data marker */
929 #define OutputCode(code) \
930 { \
931     accumulator+=code << (32-code_width-number_bits); \
932     number_bits+=code_width; \
933     while (number_bits >= 8) \
934     { \
935         (void) WriteBlobByte(image,(unsigned char) (accumulator >> 24)); \
936         accumulator=accumulator << 8; \
937         number_bits-=8; \
938     } \
939 }
940
941   typedef struct _TableType
942   {
943     ssize_t
944       prefix,
945       suffix,
946       next;
947   } TableType;
948
949   register ssize_t
950     i;
951
952   size_t
953     accumulator,
954     number_bits,
955     code_width,
956     last_code,
957     next_index;
958
959   ssize_t
960     index;
961
962   TableType
963     *table;
964
965   /*
966     Allocate string table.
967   */
968   assert(image != (Image *) NULL);
969   assert(image->signature == MagickSignature);
970   if (image->debug != MagickFalse)
971     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
972   assert(pixels != (unsigned char *) NULL);
973   assert(exception != (ExceptionInfo *) NULL);
974   assert(exception->signature == MagickSignature);
975   table=(TableType *) AcquireQuantumMemory(1UL << 12,sizeof(*table));
976   if (table == (TableType *) NULL)
977     return(MagickFalse);
978   /*
979     Initialize variables.
980   */
981   accumulator=0;
982   code_width=9;
983   number_bits=0;
984   last_code=0;
985   OutputCode(LZWClr);
986   for (index=0; index < 256; index++)
987   {
988     table[index].prefix=(-1);
989     table[index].suffix=(short) index;
990     table[index].next=(-1);
991   }
992   next_index=LZWEod+1;
993   code_width=9;
994   last_code=(size_t) pixels[0];
995   for (i=1; i < (ssize_t) length; i++)
996   {
997     /*
998       Find string.
999     */
1000     index=(ssize_t) last_code;
1001     while (index != -1)
1002       if ((table[index].prefix != (ssize_t) last_code) ||
1003           (table[index].suffix != (ssize_t) pixels[i]))
1004         index=table[index].next;
1005       else
1006         {
1007           last_code=(size_t) index;
1008           break;
1009         }
1010     if (last_code != (size_t) index)
1011       {
1012         /*
1013           Add string.
1014         */
1015         OutputCode(last_code);
1016         table[next_index].prefix=(ssize_t) last_code;
1017         table[next_index].suffix=(short) pixels[i];
1018         table[next_index].next=table[last_code].next;
1019         table[last_code].next=(ssize_t) next_index;
1020         next_index++;
1021         /*
1022           Did we just move up to next bit width?
1023         */
1024         if ((next_index >> code_width) != 0)
1025           {
1026             code_width++;
1027             if (code_width > 12)
1028               {
1029                 /*
1030                   Did we overflow the max bit width?
1031                 */
1032                 code_width--;
1033                 OutputCode(LZWClr);
1034                 for (index=0; index < 256; index++)
1035                 {
1036                   table[index].prefix=(-1);
1037                   table[index].suffix=index;
1038                   table[index].next=(-1);
1039                 }
1040                 next_index=LZWEod+1;
1041                 code_width=9;
1042               }
1043             }
1044           last_code=(size_t) pixels[i];
1045       }
1046   }
1047   /*
1048     Flush tables.
1049   */
1050   OutputCode(last_code);
1051   OutputCode(LZWEod);
1052   if (number_bits != 0)
1053     (void) WriteBlobByte(image,(unsigned char) (accumulator >> 24));
1054   table=(TableType *) RelinquishMagickMemory(table);
1055   return(MagickTrue);
1056 }
1057 \f
1058 /*
1059 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1060 %                                                                             %
1061 %                                                                             %
1062 %                                                                             %
1063 %   P a c k b i t s E n c o d e I m a g e                                     %
1064 %                                                                             %
1065 %                                                                             %
1066 %                                                                             %
1067 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1068 %
1069 %  PackbitsEncodeImage() compresses an image via Macintosh Packbits encoding
1070 %  specific to Postscript Level II or Portable Document Format.  To ensure
1071 %  portability, the binary Packbits bytes are encoded as ASCII Base-85.
1072 %
1073 %  The format of the PackbitsEncodeImage method is:
1074 %
1075 %      MagickBooleanType PackbitsEncodeImage(Image *image,const size_t length,
1076 %        unsigned char *restrict pixels)
1077 %
1078 %  A description of each parameter follows:
1079 %
1080 %    o image: the image.
1081 %
1082 %    o length:  A value that specifies the number of pixels to compress.
1083 %
1084 %    o pixels: the address of an unsigned array of characters containing the
1085 %      pixels to compress.
1086 %
1087 */
1088 MagickExport MagickBooleanType PackbitsEncodeImage(Image *image,
1089   const size_t length,unsigned char *restrict pixels,ExceptionInfo *exception)
1090 {
1091   int
1092     count;
1093
1094   register ssize_t
1095     i,
1096     j;
1097
1098   unsigned char
1099     *packbits;
1100
1101   /*
1102     Compress pixels with Packbits encoding.
1103   */
1104   assert(image != (Image *) NULL);
1105   assert(image->signature == MagickSignature);
1106   if (image->debug != MagickFalse)
1107     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1108   assert(pixels != (unsigned char *) NULL);
1109   packbits=(unsigned char *) AcquireQuantumMemory(128UL,sizeof(*packbits));
1110   if (packbits == (unsigned char *) NULL)
1111     ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
1112       image->filename);
1113   for (i=(ssize_t) length; i != 0; )
1114   {
1115     switch (i)
1116     {
1117       case 1:
1118       {
1119         i--;
1120         (void) WriteBlobByte(image,(unsigned char) 0);
1121         (void) WriteBlobByte(image,*pixels);
1122         break;
1123       }
1124       case 2:
1125       {
1126         i-=2;
1127         (void) WriteBlobByte(image,(unsigned char) 1);
1128         (void) WriteBlobByte(image,*pixels);
1129         (void) WriteBlobByte(image,pixels[1]);
1130         break;
1131       }
1132       case 3:
1133       {
1134         i-=3;
1135         if ((*pixels == *(pixels+1)) && (*(pixels+1) == *(pixels+2)))
1136           {
1137             (void) WriteBlobByte(image,(unsigned char) ((256-3)+1));
1138             (void) WriteBlobByte(image,*pixels);
1139             break;
1140           }
1141         (void) WriteBlobByte(image,(unsigned char) 2);
1142         (void) WriteBlobByte(image,*pixels);
1143         (void) WriteBlobByte(image,pixels[1]);
1144         (void) WriteBlobByte(image,pixels[2]);
1145         break;
1146       }
1147       default:
1148       {
1149         if ((*pixels == *(pixels+1)) && (*(pixels+1) == *(pixels+2)))
1150           {
1151             /*
1152               Packed run.
1153             */
1154             count=3;
1155             while (((ssize_t) count < i) && (*pixels == *(pixels+count)))
1156             {
1157               count++;
1158               if (count >= 127)
1159                 break;
1160             }
1161             i-=count;
1162             (void) WriteBlobByte(image,(unsigned char) ((256-count)+1));
1163             (void) WriteBlobByte(image,*pixels);
1164             pixels+=count;
1165             break;
1166           }
1167         /*
1168           Literal run.
1169         */
1170         count=0;
1171         while ((*(pixels+count) != *(pixels+count+1)) ||
1172                (*(pixels+count+1) != *(pixels+count+2)))
1173         {
1174           packbits[count+1]=pixels[count];
1175           count++;
1176           if (((ssize_t) count >= (i-3)) || (count >= 127))
1177             break;
1178         }
1179         i-=count;
1180         *packbits=(unsigned char) (count-1);
1181         for (j=0; j <= (ssize_t) count; j++)
1182           (void) WriteBlobByte(image,packbits[j]);
1183         pixels+=count;
1184         break;
1185       }
1186     }
1187   }
1188   (void) WriteBlobByte(image,(unsigned char) 128);  /* EOD marker */
1189   packbits=(unsigned char *) RelinquishMagickMemory(packbits);
1190   return(MagickTrue);
1191 }
1192 \f
1193 #if defined(MAGICKCORE_ZLIB_DELEGATE)
1194 /*
1195 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1196 %                                                                             %
1197 %                                                                             %
1198 %                                                                             %
1199 %   Z L I B E n c o d e I m a g e                                             %
1200 %                                                                             %
1201 %                                                                             %
1202 %                                                                             %
1203 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1204 %
1205 %  ZLIBEncodeImage compresses an image via ZLIB-coding specific to
1206 %  Postscript Level II or Portable Document Format.
1207 %
1208 %  The format of the ZLIBEncodeImage method is:
1209 %
1210 %      MagickBooleanType ZLIBEncodeImage(Image *image,const size_t length,
1211 %        unsigned char *restrict pixels,ExceptionInfo *exception)
1212 %
1213 %  A description of each parameter follows:
1214 %
1215 %    o file: the address of a structure of type FILE.  ZLIB encoded pixels
1216 %      are written to this file.
1217 %
1218 %    o length:  A value that specifies the number of pixels to compress.
1219 %
1220 %    o pixels: the address of an unsigned array of characters containing the
1221 %      pixels to compress.
1222 %
1223 %    o exception: return any errors or warnings in this structure.
1224 %
1225 */
1226
1227 static voidpf AcquireZIPMemory(voidpf context,unsigned int items,
1228   unsigned int size)
1229 {
1230   (void) context;
1231   return((voidpf) AcquireQuantumMemory(items,size));
1232 }
1233
1234 static void RelinquishZIPMemory(voidpf context,voidpf memory)
1235 {
1236   (void) context;
1237   memory=RelinquishMagickMemory(memory);
1238 }
1239
1240 MagickExport MagickBooleanType ZLIBEncodeImage(Image *image,const size_t length,
1241   unsigned char *restrict pixels,ExceptionInfo *exception)
1242 {
1243   int
1244     status;
1245
1246   register ssize_t
1247     i;
1248
1249   size_t
1250     compress_packets;
1251
1252   unsigned char
1253     *compress_pixels;
1254
1255   z_stream
1256     stream;
1257
1258   assert(image != (Image *) NULL);
1259   assert(image->signature == MagickSignature);
1260   if (image->debug != MagickFalse)
1261     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1262   compress_packets=(size_t) (1.001*length+12);
1263   compress_pixels=(unsigned char *) AcquireQuantumMemory(compress_packets,
1264     sizeof(*compress_pixels));
1265   if (compress_pixels == (unsigned char *) NULL)
1266     ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
1267       image->filename);
1268   stream.next_in=pixels;
1269   stream.avail_in=(unsigned int) length;
1270   stream.next_out=compress_pixels;
1271   stream.avail_out=(unsigned int) compress_packets;
1272   stream.zalloc=AcquireZIPMemory;
1273   stream.zfree=RelinquishZIPMemory;
1274   stream.opaque=(voidpf) NULL;
1275   status=deflateInit(&stream,(int) (image->quality ==
1276     UndefinedCompressionQuality ? 7 : MagickMin(image->quality/10,9)));
1277   if (status == Z_OK)
1278     {
1279       status=deflate(&stream,Z_FINISH);
1280       if (status == Z_STREAM_END)
1281         status=deflateEnd(&stream);
1282       else
1283         (void) deflateEnd(&stream);
1284       compress_packets=(size_t) stream.total_out;
1285     }
1286   if (status != Z_OK)
1287     ThrowBinaryException(CoderError,"UnableToZipCompressImage",image->filename)
1288   else
1289     for (i=0; i < (ssize_t) compress_packets; i++)
1290       (void) WriteBlobByte(image,compress_pixels[i]);
1291   compress_pixels=(unsigned char *) RelinquishMagickMemory(compress_pixels);
1292   return(status == Z_OK ? MagickTrue : MagickFalse);
1293 }
1294 #else
1295 MagickExport MagickBooleanType ZLIBEncodeImage(Image *image,
1296   const size_t magick_unused(length),unsigned char *magick_unused(pixels),
1297   ExceptionInfo *exception)
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(exception,GetMagickModule(),MissingDelegateError,
1304     "DelegateLibrarySupportNotBuiltIn","'%s' (ZIP)",image->filename);
1305   return(MagickFalse);
1306 }
1307 #endif