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 %                                Cristy                                       %
17 %                              May  1993                                      %
18 %                                                                             %
19 %                                                                             %
20 %  Copyright 1999-2021 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 %    https://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 "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 
66 /*
67   Typedef declarations.
68 */
69 struct _Ascii85Info
70 {
71   ssize_t
72     offset,
73     line_break;
74 
75   char
76     tuple[6];
77 
78   unsigned char
79     buffer[10];
80 };
81 
82 typedef struct HuffmanTable
83 {
84   size_t
85     id,
86     code,
87     length,
88     count;
89 } HuffmanTable;
90 
91 /*
92   Huffman coding declarations.
93 */
94 #define TWId  23L
95 #define MWId  24L
96 #define TBId  25L
97 #define MBId  26L
98 #define EXId  27L
99 
100 static const HuffmanTable
101   MBTable[]=
102   {
103     { MBId, 0x0f, 10, 64 }, { MBId, 0xc8, 12, 128 },
104     { MBId, 0xc9, 12, 192 }, { MBId, 0x5b, 12, 256 },
105     { MBId, 0x33, 12, 320 }, { MBId, 0x34, 12, 384 },
106     { MBId, 0x35, 12, 448 }, { MBId, 0x6c, 13, 512 },
107     { MBId, 0x6d, 13, 576 }, { MBId, 0x4a, 13, 640 },
108     { MBId, 0x4b, 13, 704 }, { MBId, 0x4c, 13, 768 },
109     { MBId, 0x4d, 13, 832 }, { MBId, 0x72, 13, 896 },
110     { MBId, 0x73, 13, 960 }, { MBId, 0x74, 13, 1024 },
111     { MBId, 0x75, 13, 1088 }, { MBId, 0x76, 13, 1152 },
112     { MBId, 0x77, 13, 1216 }, { MBId, 0x52, 13, 1280 },
113     { MBId, 0x53, 13, 1344 }, { MBId, 0x54, 13, 1408 },
114     { MBId, 0x55, 13, 1472 }, { MBId, 0x5a, 13, 1536 },
115     { MBId, 0x5b, 13, 1600 }, { MBId, 0x64, 13, 1664 },
116     { MBId, 0x65, 13, 1728 }, { MBId, 0x00, 0, 0 }
117   };
118 
119 static const HuffmanTable
120   EXTable[]=
121   {
122     { EXId, 0x08, 11, 1792 }, { EXId, 0x0c, 11, 1856 },
123     { EXId, 0x0d, 11, 1920 }, { EXId, 0x12, 12, 1984 },
124     { EXId, 0x13, 12, 2048 }, { EXId, 0x14, 12, 2112 },
125     { EXId, 0x15, 12, 2176 }, { EXId, 0x16, 12, 2240 },
126     { EXId, 0x17, 12, 2304 }, { EXId, 0x1c, 12, 2368 },
127     { EXId, 0x1d, 12, 2432 }, { EXId, 0x1e, 12, 2496 },
128     { EXId, 0x1f, 12, 2560 }, { EXId, 0x00, 0, 0 }
129   };
130 
131 static const HuffmanTable
132   MWTable[]=
133   {
134     { MWId, 0x1b, 5, 64 }, { MWId, 0x12, 5, 128 },
135     { MWId, 0x17, 6, 192 }, { MWId, 0x37, 7, 256 },
136     { MWId, 0x36, 8, 320 }, { MWId, 0x37, 8, 384 },
137     { MWId, 0x64, 8, 448 }, { MWId, 0x65, 8, 512 },
138     { MWId, 0x68, 8, 576 }, { MWId, 0x67, 8, 640 },
139     { MWId, 0xcc, 9, 704 }, { MWId, 0xcd, 9, 768 },
140     { MWId, 0xd2, 9, 832 }, { MWId, 0xd3, 9, 896 },
141     { MWId, 0xd4, 9, 960 }, { MWId, 0xd5, 9, 1024 },
142     { MWId, 0xd6, 9, 1088 }, { MWId, 0xd7, 9, 1152 },
143     { MWId, 0xd8, 9, 1216 }, { MWId, 0xd9, 9, 1280 },
144     { MWId, 0xda, 9, 1344 }, { MWId, 0xdb, 9, 1408 },
145     { MWId, 0x98, 9, 1472 }, { MWId, 0x99, 9, 1536 },
146     { MWId, 0x9a, 9, 1600 }, { MWId, 0x18, 6, 1664 },
147     { MWId, 0x9b, 9, 1728 }, { MWId, 0x00, 0, 0 }
148   };
149 
150 static const HuffmanTable
151   TBTable[]=
152   {
153     { TBId, 0x37, 10, 0 }, { TBId, 0x02, 3, 1 }, { TBId, 0x03, 2, 2 },
154     { TBId, 0x02, 2, 3 }, { TBId, 0x03, 3, 4 }, { TBId, 0x03, 4, 5 },
155     { TBId, 0x02, 4, 6 }, { TBId, 0x03, 5, 7 }, { TBId, 0x05, 6, 8 },
156     { TBId, 0x04, 6, 9 }, { TBId, 0x04, 7, 10 }, { TBId, 0x05, 7, 11 },
157     { TBId, 0x07, 7, 12 }, { TBId, 0x04, 8, 13 }, { TBId, 0x07, 8, 14 },
158     { TBId, 0x18, 9, 15 }, { TBId, 0x17, 10, 16 }, { TBId, 0x18, 10, 17 },
159     { TBId, 0x08, 10, 18 }, { TBId, 0x67, 11, 19 }, { TBId, 0x68, 11, 20 },
160     { TBId, 0x6c, 11, 21 }, { TBId, 0x37, 11, 22 }, { TBId, 0x28, 11, 23 },
161     { TBId, 0x17, 11, 24 }, { TBId, 0x18, 11, 25 }, { TBId, 0xca, 12, 26 },
162     { TBId, 0xcb, 12, 27 }, { TBId, 0xcc, 12, 28 }, { TBId, 0xcd, 12, 29 },
163     { TBId, 0x68, 12, 30 }, { TBId, 0x69, 12, 31 }, { TBId, 0x6a, 12, 32 },
164     { TBId, 0x6b, 12, 33 }, { TBId, 0xd2, 12, 34 }, { TBId, 0xd3, 12, 35 },
165     { TBId, 0xd4, 12, 36 }, { TBId, 0xd5, 12, 37 }, { TBId, 0xd6, 12, 38 },
166     { TBId, 0xd7, 12, 39 }, { TBId, 0x6c, 12, 40 }, { TBId, 0x6d, 12, 41 },
167     { TBId, 0xda, 12, 42 }, { TBId, 0xdb, 12, 43 }, { TBId, 0x54, 12, 44 },
168     { TBId, 0x55, 12, 45 }, { TBId, 0x56, 12, 46 }, { TBId, 0x57, 12, 47 },
169     { TBId, 0x64, 12, 48 }, { TBId, 0x65, 12, 49 }, { TBId, 0x52, 12, 50 },
170     { TBId, 0x53, 12, 51 }, { TBId, 0x24, 12, 52 }, { TBId, 0x37, 12, 53 },
171     { TBId, 0x38, 12, 54 }, { TBId, 0x27, 12, 55 }, { TBId, 0x28, 12, 56 },
172     { TBId, 0x58, 12, 57 }, { TBId, 0x59, 12, 58 }, { TBId, 0x2b, 12, 59 },
173     { TBId, 0x2c, 12, 60 }, { TBId, 0x5a, 12, 61 }, { TBId, 0x66, 12, 62 },
174     { TBId, 0x67, 12, 63 }, { TBId, 0x00, 0, 0 }
175   };
176 
177 static const HuffmanTable
178   TWTable[]=
179   {
180     { TWId, 0x35, 8, 0 }, { TWId, 0x07, 6, 1 }, { TWId, 0x07, 4, 2 },
181     { TWId, 0x08, 4, 3 }, { TWId, 0x0b, 4, 4 }, { TWId, 0x0c, 4, 5 },
182     { TWId, 0x0e, 4, 6 }, { TWId, 0x0f, 4, 7 }, { TWId, 0x13, 5, 8 },
183     { TWId, 0x14, 5, 9 }, { TWId, 0x07, 5, 10 }, { TWId, 0x08, 5, 11 },
184     { TWId, 0x08, 6, 12 }, { TWId, 0x03, 6, 13 }, { TWId, 0x34, 6, 14 },
185     { TWId, 0x35, 6, 15 }, { TWId, 0x2a, 6, 16 }, { TWId, 0x2b, 6, 17 },
186     { TWId, 0x27, 7, 18 }, { TWId, 0x0c, 7, 19 }, { TWId, 0x08, 7, 20 },
187     { TWId, 0x17, 7, 21 }, { TWId, 0x03, 7, 22 }, { TWId, 0x04, 7, 23 },
188     { TWId, 0x28, 7, 24 }, { TWId, 0x2b, 7, 25 }, { TWId, 0x13, 7, 26 },
189     { TWId, 0x24, 7, 27 }, { TWId, 0x18, 7, 28 }, { TWId, 0x02, 8, 29 },
190     { TWId, 0x03, 8, 30 }, { TWId, 0x1a, 8, 31 }, { TWId, 0x1b, 8, 32 },
191     { TWId, 0x12, 8, 33 }, { TWId, 0x13, 8, 34 }, { TWId, 0x14, 8, 35 },
192     { TWId, 0x15, 8, 36 }, { TWId, 0x16, 8, 37 }, { TWId, 0x17, 8, 38 },
193     { TWId, 0x28, 8, 39 }, { TWId, 0x29, 8, 40 }, { TWId, 0x2a, 8, 41 },
194     { TWId, 0x2b, 8, 42 }, { TWId, 0x2c, 8, 43 }, { TWId, 0x2d, 8, 44 },
195     { TWId, 0x04, 8, 45 }, { TWId, 0x05, 8, 46 }, { TWId, 0x0a, 8, 47 },
196     { TWId, 0x0b, 8, 48 }, { TWId, 0x52, 8, 49 }, { TWId, 0x53, 8, 50 },
197     { TWId, 0x54, 8, 51 }, { TWId, 0x55, 8, 52 }, { TWId, 0x24, 8, 53 },
198     { TWId, 0x25, 8, 54 }, { TWId, 0x58, 8, 55 }, { TWId, 0x59, 8, 56 },
199     { TWId, 0x5a, 8, 57 }, { TWId, 0x5b, 8, 58 }, { TWId, 0x4a, 8, 59 },
200     { TWId, 0x4b, 8, 60 }, { TWId, 0x32, 8, 61 }, { TWId, 0x33, 8, 62 },
201     { TWId, 0x34, 8, 63 }, { TWId, 0x00, 0, 0 }
202   };
203 
204 /*
205 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
206 %                                                                             %
207 %                                                                             %
208 %                                                                             %
209 %   A S C I I 8 5 E n c o d e                                                 %
210 %                                                                             %
211 %                                                                             %
212 %                                                                             %
213 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
214 %
215 %  ASCII85Encode() encodes data in ASCII base-85 format.  ASCII base-85
216 %  encoding produces five ASCII printing characters from every four bytes of
217 %  binary data.
218 %
219 %  The format of the ASCII85Encode method is:
220 %
221 %      void Ascii85Encode(Image *image,const size_t code)
222 %
223 %  A description of each parameter follows:
224 %
225 %    o code: a binary unsigned char to encode to ASCII 85.
226 %
227 %    o file: write the encoded ASCII character to this file.
228 %
229 %
230 */
Ascii85Tuple(Ascii85Info * ascii85_info,const unsigned char * magick_restrict data)231 static inline void Ascii85Tuple(Ascii85Info *ascii85_info,
232   const unsigned char *magick_restrict data)
233 {
234 #define MaxLineExtent  36L
235 
236   ssize_t
237     i,
238     x;
239 
240   size_t
241     code,
242     quantum;
243 
244   code=((((size_t) data[0] << 8) | (size_t) data[1]) << 16) |
245     ((size_t) data[2] << 8) | (size_t) data[3];
246   if (code == 0L)
247     {
248       ascii85_info->tuple[0]='z';
249       ascii85_info->tuple[1]='\0';
250       return;
251     }
252   quantum=85UL*85UL*85UL*85UL;
253   for (i=0; i < 4; i++)
254   {
255     x=(ssize_t) (code/quantum);
256     code-=quantum*x;
257     ascii85_info->tuple[i]=(char) (x+(int) '!');
258     quantum/=85L;
259   }
260   ascii85_info->tuple[4]=(char) ((code % 85L)+(int) '!');
261   ascii85_info->tuple[5]='\0';
262 }
263 
Ascii85Initialize(Image * image)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) memset(image->ascii85,0,sizeof(*image->ascii85));
274   image->ascii85->line_break=(ssize_t) (MaxLineExtent << 1);
275   image->ascii85->offset=0;
276 }
277 
Ascii85Flush(Image * image)278 MagickExport void Ascii85Flush(Image *image)
279 {
280   assert(image != (Image *) NULL);
281   assert(image->signature == MagickCoreSignature);
282   if (image->debug != MagickFalse)
283     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
284   assert(image->ascii85 != (Ascii85Info *) NULL);
285   if (image->ascii85->offset > 0)
286     {
287       image->ascii85->buffer[image->ascii85->offset]='\0';
288       image->ascii85->buffer[image->ascii85->offset+1]='\0';
289       image->ascii85->buffer[image->ascii85->offset+2]='\0';
290       Ascii85Tuple(image->ascii85,image->ascii85->buffer);
291       (void) WriteBlob(image,(size_t) image->ascii85->offset+1,
292         (const unsigned char *) (*image->ascii85->tuple == 'z' ? "!!!!" :
293         image->ascii85->tuple));
294     }
295   (void) WriteBlobByte(image,'~');
296   (void) WriteBlobByte(image,'>');
297   (void) WriteBlobByte(image,'\n');
298 }
299 
Ascii85Encode(Image * image,const unsigned char code)300 MagickExport void Ascii85Encode(Image *image,const unsigned char code)
301 {
302   char
303     *q;
304 
305   unsigned char
306     *p;
307 
308   ssize_t
309     n;
310 
311   assert(image != (Image *) NULL);
312   assert(image->signature == MagickCoreSignature);
313   assert(image->ascii85 != (Ascii85Info *) NULL);
314   image->ascii85->buffer[image->ascii85->offset]=code;
315   image->ascii85->offset++;
316   if (image->ascii85->offset < 4)
317     return;
318   p=image->ascii85->buffer;
319   for (n=image->ascii85->offset; n >= 4; n-=4)
320   {
321     Ascii85Tuple(image->ascii85,p);
322     for (q=image->ascii85->tuple; *q != '\0'; q++)
323     {
324       image->ascii85->line_break--;
325       if ((image->ascii85->line_break < 0) && (*q != '%'))
326         {
327           (void) WriteBlobByte(image,'\n');
328           image->ascii85->line_break=2*MaxLineExtent;
329         }
330       (void) WriteBlobByte(image,(unsigned char) *q);
331     }
332     p+=8;
333   }
334   image->ascii85->offset=n;
335   p-=4;
336   for (n=0; n < 4; n++)
337     image->ascii85->buffer[n]=(*p++);
338 }
339 
340 /*
341 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
342 %                                                                             %
343 %                                                                             %
344 %                                                                             %
345 %   H u f f m a n D e c o d e I m a g e                                       %
346 %                                                                             %
347 %                                                                             %
348 %                                                                             %
349 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
350 %
351 %  HuffmanDecodeImage() uncompresses an image via Huffman-coding.
352 %
353 %  The format of the HuffmanDecodeImage method is:
354 %
355 %      MagickBooleanType HuffmanDecodeImage(Image *image,
356 %        ExceptionInfo *exception)
357 %
358 %  A description of each parameter follows:
359 %
360 %    o image: the image.
361 %
362 %    o exception: return any errors or warnings in this structure.
363 %
364 */
HuffmanDecodeImage(Image * image,ExceptionInfo * exception)365 MagickExport MagickBooleanType HuffmanDecodeImage(Image *image,
366   ExceptionInfo *exception)
367 {
368 #define HashSize  1021L
369 #define MBHashA  293L
370 #define MBHashB  2695L
371 #define MWHashA  3510L
372 #define MWHashB  1178L
373 
374 #define InitializeHashTable(hash,table,a,b) \
375 { \
376   entry=table; \
377   while (entry->code != 0) \
378   {  \
379     hash[((entry->length+a)*(entry->code+b)) % HashSize]=(HuffmanTable *) entry; \
380     entry++; \
381   } \
382 }
383 
384 #define InputBit(bit)  \
385 {  \
386   if ((mask & 0xff) == 0)  \
387     {  \
388       byte=ReadBlobByte(image);  \
389       if (byte == EOF)  \
390         break;  \
391       mask=0x80;  \
392     }  \
393   runlength++;  \
394   bit=(size_t) ((byte & mask) != 0 ? 0x01 : 0x00); \
395   mask>>=1;  \
396   if (bit != 0)  \
397     runlength=0;  \
398 }
399 
400   CacheView
401     *image_view;
402 
403   const HuffmanTable
404     *entry;
405 
406   HuffmanTable
407     **mb_hash,
408     **mw_hash;
409 
410   int
411     byte;
412 
413   MagickBooleanType
414     proceed;
415 
416   Quantum
417     index;
418 
419   ssize_t
420     i;
421 
422   unsigned char
423     *p;
424 
425   size_t
426     bit,
427     code,
428     mask,
429     length,
430     null_lines,
431     runlength;
432 
433   ssize_t
434     count,
435     y;
436 
437   unsigned char
438     *scanline;
439 
440   unsigned int
441     bail,
442     color;
443 
444   /*
445     Allocate buffers.
446   */
447   assert(image != (Image *) NULL);
448   assert(image->signature == MagickCoreSignature);
449   if (image->debug != MagickFalse)
450     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
451   if (image->blob == (BlobInfo *) NULL)
452     ThrowBinaryException(BlobError,"UnableToOpenBlob",image->filename);
453   mb_hash=(HuffmanTable **) AcquireQuantumMemory(HashSize,sizeof(*mb_hash));
454   mw_hash=(HuffmanTable **) AcquireQuantumMemory(HashSize,sizeof(*mw_hash));
455   scanline=(unsigned char *) AcquireQuantumMemory((size_t) image->columns,
456     sizeof(*scanline));
457   if ((mb_hash == (HuffmanTable **) NULL) ||
458       (mw_hash == (HuffmanTable **) NULL) ||
459       (scanline == (unsigned char *) NULL))
460     {
461       if (mb_hash != (HuffmanTable **) NULL)
462         mb_hash=(HuffmanTable **) RelinquishMagickMemory(mb_hash);
463       if (mw_hash != (HuffmanTable **) NULL)
464         mw_hash=(HuffmanTable **) RelinquishMagickMemory(mw_hash);
465       if (scanline != (unsigned char *) NULL)
466         scanline=(unsigned char *) RelinquishMagickMemory(scanline);
467       ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
468         image->filename);
469     }
470   /*
471     Initialize Huffman tables.
472   */
473   for (i=0; i < HashSize; i++)
474   {
475     mb_hash[i]=(HuffmanTable *) NULL;
476     mw_hash[i]=(HuffmanTable *) NULL;
477   }
478   InitializeHashTable(mw_hash,TWTable,MWHashA,MWHashB);
479   InitializeHashTable(mw_hash,MWTable,MWHashA,MWHashB);
480   InitializeHashTable(mw_hash,EXTable,MWHashA,MWHashB);
481   InitializeHashTable(mb_hash,TBTable,MBHashA,MBHashB);
482   InitializeHashTable(mb_hash,MBTable,MBHashA,MBHashB);
483   InitializeHashTable(mb_hash,EXTable,MBHashA,MBHashB);
484   /*
485     Uncompress 1D Huffman to runlength encoded pixels.
486   */
487   byte=0;
488   mask=0;
489   null_lines=0;
490   runlength=0;
491   while (runlength < 11)
492    InputBit(bit);
493   do { InputBit(bit); } while ((int) bit == 0);
494   image->resolution.x=204.0;
495   image->resolution.y=196.0;
496   image->units=PixelsPerInchResolution;
497   image_view=AcquireAuthenticCacheView(image,exception);
498   for (y=0; ((y < (ssize_t) image->rows) && (null_lines < 3)); )
499   {
500     Quantum
501       *magick_restrict q;
502 
503     ssize_t
504       x;
505 
506     /*
507       Initialize scanline to white.
508     */
509     memset(scanline,0,sizeof(*scanline)*image->columns);
510     /*
511       Decode Huffman encoded scanline.
512     */
513     color=MagickTrue;
514     code=0;
515     count=0;
516     length=0;
517     runlength=0;
518     x=0;
519     for ( ; ; )
520     {
521       if (byte == EOF)
522         break;
523       if (x >= (ssize_t) image->columns)
524         {
525           while (runlength < 11)
526             InputBit(bit);
527           do { InputBit(bit); } while ((int) bit == 0);
528           break;
529         }
530       bail=MagickFalse;
531       do
532       {
533         if (runlength < 11)
534           InputBit(bit)
535         else
536           {
537             InputBit(bit);
538             if ((int) bit != 0)
539               {
540                 null_lines++;
541                 if (x != 0)
542                   null_lines=0;
543                 bail=MagickTrue;
544                 break;
545               }
546           }
547         code=(code << 1)+(size_t) bit;
548         length++;
549       } while (code == 0);
550       if (bail != MagickFalse)
551         break;
552       if (length > 13)
553         {
554           while (runlength < 11)
555             InputBit(bit);
556           do { InputBit(bit); } while ((int) bit == 0);
557           break;
558         }
559       if (color != MagickFalse)
560         {
561           if (length < 4)
562             continue;
563           entry=mw_hash[((length+MWHashA)*(code+MWHashB)) % HashSize];
564         }
565       else
566         {
567           if (length < 2)
568             continue;
569           entry=mb_hash[((length+MBHashA)*(code+MBHashB)) % HashSize];
570         }
571       if (entry == (const HuffmanTable *) NULL)
572         continue;
573       if ((entry->length != length) || (entry->code != code))
574         continue;
575       switch (entry->id)
576       {
577         case TWId:
578         case TBId:
579         {
580           count+=(ssize_t) entry->count;
581           if ((x+count) > (ssize_t) image->columns)
582             count=(ssize_t) image->columns-x;
583           if (count > 0)
584             {
585               if (color != MagickFalse)
586                 {
587                   x+=count;
588                   count=0;
589                 }
590               else
591                 for ( ; count > 0; count--)
592                   if ((x >= 0) && (x < (ssize_t) image->columns))
593                     scanline[x++]=(unsigned char) 1;
594             }
595           color=(unsigned int)
596             ((color == MagickFalse) ? MagickTrue : MagickFalse);
597           break;
598         }
599         case MWId:
600         case MBId:
601         case EXId:
602         {
603           count+=(ssize_t) entry->count;
604           break;
605         }
606         default:
607           break;
608       }
609       code=0;
610       length=0;
611     }
612     /*
613       Transfer scanline to image pixels.
614     */
615     p=scanline;
616     q=QueueCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
617     if (q == (Quantum *) NULL)
618       break;
619     for (x=0; x < (ssize_t) image->columns; x++)
620     {
621       index=(Quantum) (*p++);
622       SetPixelIndex(image,index,q);
623       SetPixelViaPixelInfo(image,image->colormap+(ssize_t) index,q);
624       q+=GetPixelChannels(image);
625     }
626     if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
627       break;
628     proceed=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
629       image->rows);
630     if (proceed == MagickFalse)
631       break;
632     y++;
633   }
634   image_view=DestroyCacheView(image_view);
635   image->rows=(size_t) MagickMax((size_t) y-3,1);
636   image->compression=FaxCompression;
637   /*
638     Free decoder memory.
639   */
640   mw_hash=(HuffmanTable **) RelinquishMagickMemory(mw_hash);
641   mb_hash=(HuffmanTable **) RelinquishMagickMemory(mb_hash);
642   scanline=(unsigned char *) RelinquishMagickMemory(scanline);
643   return(MagickTrue);
644 }
645 
646 /*
647 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
648 %                                                                             %
649 %                                                                             %
650 %                                                                             %
651 %   H u f f m a n E n c o d e I m a g e                                       %
652 %                                                                             %
653 %                                                                             %
654 %                                                                             %
655 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
656 %
657 %  HuffmanEncodeImage() compresses an image via Huffman-coding.
658 %
659 %  The format of the HuffmanEncodeImage method is:
660 %
661 %      MagickBooleanType HuffmanEncodeImage(const ImageInfo *image_info,
662 %        Image *image,Image *inject_image,ExceptionInfo *exception)
663 %
664 %  A description of each parameter follows:
665 %
666 %    o image_info: the image info..
667 %
668 %    o image: the image.
669 %
670 %    o inject_image: inject into the image stream.
671 %
672 %    o exception: return any errors or warnings in this structure.
673 %
674 */
HuffmanEncodeImage(const ImageInfo * image_info,Image * image,Image * inject_image,ExceptionInfo * exception)675 MagickExport MagickBooleanType HuffmanEncodeImage(const ImageInfo *image_info,
676   Image *image,Image *inject_image,ExceptionInfo *exception)
677 {
678 #define HuffmanOutputCode(entry)  \
679 {  \
680   mask=one << (entry->length-1);  \
681   while (mask != 0)  \
682   {  \
683     OutputBit(((entry->code & mask) != 0 ? 1 : 0));  \
684     mask>>=1;  \
685   }  \
686 }
687 
688 #define OutputBit(count)  \
689 {  \
690 DisableMSCWarning(4127) \
691   if (count > 0)  \
692     byte=byte | bit;  \
693 RestoreMSCWarning \
694   bit>>=1;  \
695   if ((int) (bit & 0xff) == 0)   \
696     {  \
697       if (LocaleCompare(image_info->magick,"FAX") == 0) \
698         (void) WriteBlobByte(image,(unsigned char) byte);  \
699       else \
700         Ascii85Encode(image,byte); \
701       byte='\0';  \
702       bit=(unsigned char) 0x80;  \
703     }  \
704 }
705 
706   const HuffmanTable
707     *entry;
708 
709   int
710     k,
711     runlength;
712 
713   Image
714     *huffman_image;
715 
716   MagickBooleanType
717     proceed;
718 
719   ssize_t
720     i,
721     x;
722 
723   const Quantum
724     *p;
725 
726   unsigned char
727     *q;
728 
729   size_t
730     mask,
731     one,
732     width;
733 
734   ssize_t
735     n,
736     y;
737 
738   unsigned char
739     byte,
740     bit,
741     *scanline;
742 
743   /*
744     Allocate scanline buffer.
745   */
746   assert(image_info != (ImageInfo *) NULL);
747   assert(image_info->signature == MagickCoreSignature);
748   assert(image != (Image *) NULL);
749   assert(image->signature == MagickCoreSignature);
750   if (image->debug != MagickFalse)
751     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
752   assert(inject_image != (Image *) NULL);
753   assert(inject_image->signature == MagickCoreSignature);
754   one=1;
755   width=inject_image->columns;
756   if (LocaleCompare(image_info->magick,"FAX") == 0)
757     width=(size_t) MagickMax(inject_image->columns,1728);
758   scanline=(unsigned char *) AcquireQuantumMemory((size_t) width+1UL,
759     sizeof(*scanline));
760   if (scanline == (unsigned char *) NULL)
761     ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
762       inject_image->filename);
763   (void) memset(scanline,0,width*sizeof(*scanline));
764   huffman_image=CloneImage(inject_image,0,0,MagickTrue,exception);
765   if (huffman_image == (Image *) NULL)
766     {
767       scanline=(unsigned char *) RelinquishMagickMemory(scanline);
768       return(MagickFalse);
769     }
770   (void) SetImageType(huffman_image,BilevelType,exception);
771   byte='\0';
772   bit=(unsigned char) 0x80;
773   if (LocaleCompare(image_info->magick,"FAX") != 0)
774     Ascii85Initialize(image);
775   else
776     {
777       /*
778         End of line.
779       */
780       for (k=0; k < 11; k++)
781         OutputBit(0);
782       OutputBit(1);
783     }
784   /*
785     Compress to 1D Huffman pixels.
786   */
787   q=scanline;
788   for (y=0; y < (ssize_t) huffman_image->rows; y++)
789   {
790     p=GetVirtualPixels(huffman_image,0,y,huffman_image->columns,1,exception);
791     if (p == (const Quantum *) NULL)
792       break;
793     for (x=0; x < (ssize_t) huffman_image->columns; x++)
794     {
795       *q++=(unsigned char) (GetPixelIntensity(huffman_image,p) >=
796         ((double) QuantumRange/2.0) ? 0 : 1);
797       p+=GetPixelChannels(huffman_image);
798     }
799     /*
800       Huffman encode scanline.
801     */
802     q=scanline;
803     for (n=(ssize_t) width; n > 0; )
804     {
805       /*
806         Output white run.
807       */
808       for (runlength=0; ((n > 0) && (*q == 0)); n--)
809       {
810         q++;
811         runlength++;
812       }
813       if (runlength >= 64)
814         {
815           if (runlength < 1792)
816             entry=MWTable+((runlength/64)-1);
817           else
818             entry=EXTable+(MagickMin((size_t) runlength,2560)-1792)/64;
819           runlength-=(long) entry->count;
820           HuffmanOutputCode(entry);
821         }
822       entry=TWTable+MagickMin((size_t) runlength,63);
823       HuffmanOutputCode(entry);
824       if (n != 0)
825         {
826           /*
827             Output black run.
828           */
829           for (runlength=0; ((*q != 0) && (n > 0)); n--)
830           {
831             q++;
832             runlength++;
833           }
834           if (runlength >= 64)
835             {
836               entry=MBTable+((runlength/64)-1);
837               if (runlength >= 1792)
838                 entry=EXTable+(MagickMin((size_t) runlength,2560)-1792)/64;
839               runlength-=(long) entry->count;
840               HuffmanOutputCode(entry);
841             }
842           entry=TBTable+MagickMin((size_t) runlength,63);
843           HuffmanOutputCode(entry);
844         }
845     }
846     /*
847       End of line.
848     */
849     for (k=0; k < 11; k++)
850       OutputBit(0);
851     OutputBit(1);
852     q=scanline;
853     if (GetPreviousImageInList(huffman_image) == (Image *) NULL)
854       {
855         proceed=SetImageProgress(huffman_image,LoadImageTag,
856           (MagickOffsetType) y,huffman_image->rows);
857         if (proceed == MagickFalse)
858           break;
859       }
860   }
861   /*
862     End of page.
863   */
864   for (i=0; i < 6; i++)
865   {
866     for (k=0; k < 11; k++)
867       OutputBit(0);
868     OutputBit(1);
869   }
870   /*
871     Flush bits.
872   */
873   if (((int) bit != 0x80) != 0)
874     {
875       if (LocaleCompare(image_info->magick,"FAX") == 0)
876         (void) WriteBlobByte(image,byte);
877       else
878         Ascii85Encode(image,byte);
879     }
880   if (LocaleCompare(image_info->magick,"FAX") != 0)
881     Ascii85Flush(image);
882   huffman_image=DestroyImage(huffman_image);
883   scanline=(unsigned char *) RelinquishMagickMemory(scanline);
884   return(MagickTrue);
885 }
886 
887 /*
888 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
889 %                                                                             %
890 %                                                                             %
891 %                                                                             %
892 %   L Z W E n c o d e I m a g e                                               %
893 %                                                                             %
894 %                                                                             %
895 %                                                                             %
896 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
897 %
898 %  LZWEncodeImage() compresses an image via LZW-coding specific to Postscript
899 %  Level II or Portable Document Format.
900 %
901 %  The format of the LZWEncodeImage method is:
902 %
903 %      MagickBooleanType LZWEncodeImage(Image *image,const size_t length,
904 %        unsigned char *magick_restrict pixels,ExceptionInfo *exception)
905 %
906 %  A description of each parameter follows:
907 %
908 %    o image: the image.
909 %
910 %    o length:  A value that specifies the number of pixels to compress.
911 %
912 %    o pixels: the address of an unsigned array of characters containing the
913 %      pixels to compress.
914 %
915 %    o exception: return any errors or warnings in this structure.
916 %
917 */
LZWEncodeImage(Image * image,const size_t length,unsigned char * magick_restrict pixels,ExceptionInfo * exception)918 MagickExport MagickBooleanType LZWEncodeImage(Image *image,const size_t length,
919   unsigned char *magick_restrict pixels,ExceptionInfo *exception)
920 {
921 #define LZWClr  256UL  /* Clear Table Marker */
922 #define LZWEod  257UL  /* End of Data marker */
923 #define OutputCode(code) \
924 { \
925     accumulator+=code << (32-code_width-number_bits); \
926     number_bits+=code_width; \
927     while (number_bits >= 8) \
928     { \
929       (void) WriteBlobByte(image,(unsigned char) (accumulator >> 24)); \
930       accumulator=accumulator << 8; \
931       number_bits-=8; \
932     } \
933 }
934 
935   typedef struct _TableType
936   {
937     ssize_t
938       prefix,
939       suffix,
940       next;
941   } TableType;
942 
943   ssize_t
944     i;
945 
946   size_t
947     accumulator,
948     number_bits,
949     code_width,
950     last_code,
951     next_index;
952 
953   ssize_t
954     index;
955 
956   TableType
957     *table;
958 
959   /*
960     Allocate string table.
961   */
962   assert(image != (Image *) NULL);
963   assert(image->signature == MagickCoreSignature);
964   if (image->debug != MagickFalse)
965     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
966   assert(pixels != (unsigned char *) NULL);
967   assert(exception != (ExceptionInfo *) NULL);
968   assert(exception->signature == MagickCoreSignature);
969   table=(TableType *) AcquireQuantumMemory(1UL << 12,sizeof(*table));
970   if (table == (TableType *) NULL)
971     ThrowBinaryException(ResourceLimitWarning,"MemoryAllocationFailed",
972       image->filename);
973   /*
974     Initialize variables.
975   */
976   accumulator=0;
977   code_width=9;
978   number_bits=0;
979   last_code=0;
980   OutputCode(LZWClr);
981   for (index=0; index < 256; index++)
982   {
983     table[index].prefix=(-1);
984     table[index].suffix=(ssize_t) index;
985     table[index].next=(-1);
986   }
987   next_index=LZWEod+1;
988   code_width=9;
989   last_code=(size_t) pixels[0];
990   for (i=1; i < (ssize_t) length; i++)
991   {
992     /*
993       Find string.
994     */
995     index=(ssize_t) last_code;
996     while (index != -1)
997       if ((table[index].prefix != (ssize_t) last_code) ||
998           (table[index].suffix != (ssize_t) pixels[i]))
999         index=table[index].next;
1000       else
1001         {
1002           last_code=(size_t) index;
1003           break;
1004         }
1005     if (last_code != (size_t) index)
1006       {
1007         /*
1008           Add string.
1009         */
1010         OutputCode(last_code);
1011         table[next_index].prefix=(ssize_t) last_code;
1012         table[next_index].suffix=(ssize_t) pixels[i];
1013         table[next_index].next=table[last_code].next;
1014         table[last_code].next=(ssize_t) next_index;
1015         next_index++;
1016         /*
1017           Did we just move up to next bit width?
1018         */
1019         if ((next_index >> code_width) != 0)
1020           {
1021             code_width++;
1022             if (code_width > 12)
1023               {
1024                 /*
1025                   Did we overflow the max bit width?
1026                 */
1027                 code_width--;
1028                 OutputCode(LZWClr);
1029                 for (index=0; index < 256; index++)
1030                 {
1031                   table[index].prefix=(-1);
1032                   table[index].suffix=index;
1033                   table[index].next=(-1);
1034                 }
1035                 next_index=LZWEod+1;
1036                 code_width=9;
1037               }
1038             }
1039           last_code=(size_t) pixels[i];
1040       }
1041   }
1042   /*
1043     Flush tables.
1044   */
1045   OutputCode(last_code);
1046   OutputCode(LZWEod);
1047   if (number_bits != 0)
1048     (void) WriteBlobByte(image,(unsigned char) (accumulator >> 24));
1049   table=(TableType *) RelinquishMagickMemory(table);
1050   return(MagickTrue);
1051 }
1052 
1053 /*
1054 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1055 %                                                                             %
1056 %                                                                             %
1057 %                                                                             %
1058 %   P a c k b i t s E n c o d e I m a g e                                     %
1059 %                                                                             %
1060 %                                                                             %
1061 %                                                                             %
1062 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1063 %
1064 %  PackbitsEncodeImage() compresses an image via Macintosh Packbits encoding
1065 %  specific to Postscript Level II or Portable Document Format.  To ensure
1066 %  portability, the binary Packbits bytes are encoded as ASCII Base-85.
1067 %
1068 %  The format of the PackbitsEncodeImage method is:
1069 %
1070 %      MagickBooleanType PackbitsEncodeImage(Image *image,const size_t length,
1071 %        unsigned char *magick_restrict pixels)
1072 %
1073 %  A description of each parameter follows:
1074 %
1075 %    o image: the image.
1076 %
1077 %    o length:  A value that specifies the number of pixels to compress.
1078 %
1079 %    o pixels: the address of an unsigned array of characters containing the
1080 %      pixels to compress.
1081 %
1082 */
PackbitsEncodeImage(Image * image,const size_t length,unsigned char * magick_restrict pixels,ExceptionInfo * exception)1083 MagickExport MagickBooleanType PackbitsEncodeImage(Image *image,
1084   const size_t length,unsigned char *magick_restrict pixels,
1085   ExceptionInfo *exception)
1086 {
1087   int
1088     count;
1089 
1090   ssize_t
1091     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 == MagickCoreSignature);
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);
1109   for (i=(ssize_t) length; i != 0; )
1110   {
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;
1151             while (((ssize_t) count < i) && (*pixels == *(pixels+count)))
1152             {
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++;
1172           if (((ssize_t) count >= (i-3)) || (count >= 127))
1173             break;
1174         }
1175         i-=count;
1176         *packbits=(unsigned char) (count-1);
1177         for (j=0; j <= (ssize_t) count; j++)
1178           (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,
1207 %        unsigned char *magick_restrict pixels,ExceptionInfo *exception)
1208 %
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 %
1219 %    o exception: return any errors or warnings in this structure.
1220 %
1221 */
1222 
AcquireZIPMemory(voidpf context,unsigned int items,unsigned int size)1223 static voidpf AcquireZIPMemory(voidpf context,unsigned int items,
1224   unsigned int size)
1225 {
1226   (void) context;
1227   return((voidpf) AcquireQuantumMemory(items,size));
1228 }
1229 
RelinquishZIPMemory(voidpf context,voidpf memory)1230 static void RelinquishZIPMemory(voidpf context,voidpf memory)
1231 {
1232   (void) context;
1233   memory=RelinquishMagickMemory(memory);
1234 }
1235 
ZLIBEncodeImage(Image * image,const size_t length,unsigned char * magick_restrict pixels,ExceptionInfo * exception)1236 MagickExport MagickBooleanType ZLIBEncodeImage(Image *image,const size_t length,
1237   unsigned char *magick_restrict pixels,ExceptionInfo *exception)
1238 {
1239   int
1240     status;
1241 
1242   ssize_t
1243     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 == MagickCoreSignature);
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   for (i=0; i < (ssize_t) compress_packets; i++)
1285     (void) WriteBlobByte(image,compress_pixels[i]);
1286   compress_pixels=(unsigned char *) RelinquishMagickMemory(compress_pixels);
1287   return(MagickTrue);
1288 }
1289 #else
ZLIBEncodeImage(Image * image,const size_t magick_unused (length),unsigned char * magick_unused (pixels),ExceptionInfo * exception)1290 MagickExport MagickBooleanType ZLIBEncodeImage(Image *image,
1291   const size_t magick_unused(length),unsigned char *magick_unused(pixels),
1292   ExceptionInfo *exception)
1293 {
1294   magick_unreferenced(length);
1295   magick_unreferenced(pixels);
1296   assert(image != (Image *) NULL);
1297   assert(image->signature == MagickCoreSignature);
1298   if (image->debug != MagickFalse)
1299     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1300   (void) ThrowMagickException(exception,GetMagickModule(),MissingDelegateError,
1301     "DelegateLibrarySupportNotBuiltIn","'%s' (ZIP)",image->filename);
1302   return(MagickFalse);
1303 }
1304 #endif
1305