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)
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 }
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 }
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 }
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 }
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 }
231static inline void Ascii85Tuple(Ascii85Info *ascii85_info,
232 const unsigned char *magick_restrict data)
234#define MaxLineExtent 36L
242 code=((((size_t) data[0] << 8) | (size_t) data[1]) << 16) |
243 ((
size_t) data[2] << 8) | (size_t) data[3];
246 ascii85_info->tuple[0]=
'z';
247 ascii85_info->tuple[1]=
'\0';
250 quantum=85UL*85UL*85UL*85UL;
251 for (i=0; i < 4; i++)
255 ascii85_info->tuple[i]=(char) (x+(
int)
'!');
258 ascii85_info->tuple[4]=(char) ((code % 85L)+(
int)
'!');
259 ascii85_info->tuple[5]=
'\0';
262MagickExport
void Ascii85Initialize(Image *image)
267 if (image->ascii85 == (Ascii85Info *) NULL)
268 image->ascii85=(Ascii85Info *) AcquireCriticalMemory(
269 sizeof(*image->ascii85));
270 (void) memset(image->ascii85,0,
sizeof(*image->ascii85));
271 image->ascii85->line_break=(ssize_t) (MaxLineExtent << 1);
272 image->ascii85->offset=0;
275MagickExport
void Ascii85Flush(Image *image)
277 assert(image != (Image *) NULL);
278 assert(image->signature == MagickCoreSignature);
279 assert(image->ascii85 != (Ascii85Info *) NULL);
280 if (IsEventLogging() != MagickFalse)
281 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
282 if (image->ascii85->offset > 0)
284 image->ascii85->buffer[image->ascii85->offset]=
'\0';
285 image->ascii85->buffer[image->ascii85->offset+1]=
'\0';
286 image->ascii85->buffer[image->ascii85->offset+2]=
'\0';
287 Ascii85Tuple(image->ascii85,image->ascii85->buffer);
288 (void) WriteBlob(image,(
size_t) image->ascii85->offset+1,
289 (
const unsigned char *) (*image->ascii85->tuple ==
'z' ?
"!!!!" :
290 image->ascii85->tuple));
292 (void) WriteBlobByte(image,
'~');
293 (void) WriteBlobByte(image,
'>');
294 (void) WriteBlobByte(image,
'\n');
297MagickExport
void Ascii85Encode(Image *image,
const unsigned char code)
308 assert(image != (Image *) NULL);
309 assert(image->signature == MagickCoreSignature);
310 assert(image->ascii85 != (Ascii85Info *) NULL);
311 image->ascii85->buffer[image->ascii85->offset]=code;
312 image->ascii85->offset++;
313 if (image->ascii85->offset < 4)
315 p=image->ascii85->buffer;
316 for (n=image->ascii85->offset; n >= 4; n-=4)
318 Ascii85Tuple(image->ascii85,p);
319 for (q=image->ascii85->tuple; *q !=
'\0'; q++)
321 image->ascii85->line_break--;
322 if ((image->ascii85->line_break < 0) && (*q !=
'%'))
324 (void) WriteBlobByte(image,
'\n');
325 image->ascii85->line_break=2*MaxLineExtent;
327 (void) WriteBlobByte(image,(
unsigned char) *q);
331 image->ascii85->offset=n;
333 for (n=0; n < 4; n++)
334 image->ascii85->buffer[n]=(*p++);
362MagickExport MagickBooleanType HuffmanDecodeImage(Image *image,
363 ExceptionInfo *exception)
365#define HashSize 1021L
371#define InitializeHashTable(hash,table,a,b) \
374 while (entry->code != 0) \
376 hash[((entry->length+a)*(entry->code+b)) % HashSize]=(HuffmanTable *) entry; \
381#define InputBit(bit) \
383 if ((mask & 0xff) == 0) \
385 byte=ReadBlobByte(image); \
391 bit=(size_t) ((byte & mask) != 0 ? 0x01 : 0x00); \
440 assert(image != (Image *) NULL);
441 assert(image->signature == MagickCoreSignature);
442 if (IsEventLogging() != MagickFalse)
443 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
444 if (image->blob == (BlobInfo *) NULL)
445 ThrowBinaryException(BlobError,
"UnableToOpenBlob",image->filename);
446 mb_hash=(
HuffmanTable **) AcquireQuantumMemory(HashSize,
sizeof(*mb_hash));
447 mw_hash=(
HuffmanTable **) AcquireQuantumMemory(HashSize,
sizeof(*mw_hash));
448 scanline=(
unsigned char *) AcquireQuantumMemory((
size_t) image->columns,
452 (scanline == (
unsigned char *) NULL))
455 mb_hash=(
HuffmanTable **) RelinquishMagickMemory(mb_hash);
457 mw_hash=(
HuffmanTable **) RelinquishMagickMemory(mw_hash);
458 if (scanline != (
unsigned char *) NULL)
459 scanline=(
unsigned char *) RelinquishMagickMemory(scanline);
460 ThrowBinaryException(ResourceLimitError,
"MemoryAllocationFailed",
466 for (i=0; i < HashSize; i++)
471 InitializeHashTable(mw_hash,TWTable,MWHashA,MWHashB);
472 InitializeHashTable(mw_hash,MWTable,MWHashA,MWHashB);
473 InitializeHashTable(mw_hash,EXTable,MWHashA,MWHashB);
474 InitializeHashTable(mb_hash,TBTable,MBHashA,MBHashB);
475 InitializeHashTable(mb_hash,MBTable,MBHashA,MBHashB);
476 InitializeHashTable(mb_hash,EXTable,MBHashA,MBHashB);
484 while (runlength < 11)
486 do { InputBit(bit); }
while ((
int) bit == 0);
487 image->resolution.x=204.0;
488 image->resolution.y=196.0;
489 image->units=PixelsPerInchResolution;
490 image_view=AcquireAuthenticCacheView(image,exception);
491 for (y=0; ((y < (ssize_t) image->rows) && (null_lines < 3)); )
502 memset(scanline,0,
sizeof(*scanline)*image->columns);
516 if (x >= (ssize_t) image->columns)
518 while (runlength < 11)
520 do { InputBit(bit); }
while ((
int) bit == 0);
540 code=(code << 1)+(
size_t) bit;
543 if (bail != MagickFalse)
547 while (runlength < 11)
549 do { InputBit(bit); }
while ((
int) bit == 0);
552 if (color != MagickFalse)
556 entry=mw_hash[((length+MWHashA)*(code+MWHashB)) % HashSize];
562 entry=mb_hash[((length+MBHashA)*(code+MBHashB)) % HashSize];
566 if ((entry->length != length) || (entry->code != code))
573 count+=(ssize_t) entry->count;
574 if ((x+count) > (ssize_t) image->columns)
575 count=(ssize_t) image->columns-x;
578 if (color != MagickFalse)
584 for ( ; count > 0; count--)
585 if ((x >= 0) && (x < (ssize_t) image->columns))
586 scanline[x++]=(
unsigned char) 1;
589 ((color == MagickFalse) ? MagickTrue : MagickFalse);
596 count+=(ssize_t) entry->count;
609 q=QueueCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
610 if (q == (Quantum *) NULL)
612 for (x=0; x < (ssize_t) image->columns; x++)
614 index=(Quantum) (*p++);
615 SetPixelIndex(image,index,q);
616 SetPixelViaPixelInfo(image,image->colormap+(ssize_t) index,q);
617 q+=(ptrdiff_t) GetPixelChannels(image);
619 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
621 proceed=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
623 if (proceed == MagickFalse)
627 image_view=DestroyCacheView(image_view);
628 image->rows=(size_t) MagickMax((
size_t) y-3,1);
629 image->compression=FaxCompression;
633 mw_hash=(
HuffmanTable **) RelinquishMagickMemory(mw_hash);
634 mb_hash=(
HuffmanTable **) RelinquishMagickMemory(mb_hash);
635 scanline=(
unsigned char *) RelinquishMagickMemory(scanline);
668MagickExport MagickBooleanType HuffmanEncodeImage(
const ImageInfo *image_info,
669 Image *image,Image *inject_image,ExceptionInfo *exception)
671#define HuffmanOutputCode(entry) \
673 mask=one << (entry->length-1); \
676 OutputBit(((entry->code & mask) != 0 ? 1 : 0)); \
681#define OutputBit(count) \
683DisableMSCWarning(4127) \
688 if ((int) (bit & 0xff) == 0) \
690 if (LocaleCompare(image_info->magick,"FAX") == 0) \
691 (void) WriteBlobByte(image,(unsigned char) byte); \
693 Ascii85Encode(image,byte); \
695 bit=(unsigned char) 0x80; \
739 assert(image_info != (ImageInfo *) NULL);
740 assert(image_info->signature == MagickCoreSignature);
741 assert(image != (Image *) NULL);
742 assert(image->signature == MagickCoreSignature);
743 if (IsEventLogging() != MagickFalse)
744 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
745 assert(inject_image != (Image *) NULL);
746 assert(inject_image->signature == MagickCoreSignature);
748 width=inject_image->columns;
749 if (LocaleCompare(image_info->magick,
"FAX") == 0)
750 width=(size_t) MagickMax(inject_image->columns,1728);
751 scanline=(
unsigned char *) AcquireQuantumMemory((
size_t) width+1UL,
753 if (scanline == (
unsigned char *) NULL)
754 ThrowBinaryException(ResourceLimitError,
"MemoryAllocationFailed",
755 inject_image->filename);
756 (void) memset(scanline,0,width*
sizeof(*scanline));
757 huffman_image=CloneImage(inject_image,0,0,MagickTrue,exception);
758 if (huffman_image == (Image *) NULL)
760 scanline=(
unsigned char *) RelinquishMagickMemory(scanline);
763 (void) SetImageType(huffman_image,BilevelType,exception);
765 bit=(
unsigned char) 0x80;
766 if (LocaleCompare(image_info->magick,
"FAX") != 0)
767 Ascii85Initialize(image);
773 for (k=0; k < 11; k++)
781 for (y=0; y < (ssize_t) huffman_image->rows; y++)
783 p=GetVirtualPixels(huffman_image,0,y,huffman_image->columns,1,exception);
784 if (p == (
const Quantum *) NULL)
786 for (x=0; x < (ssize_t) huffman_image->columns; x++)
788 *q++=(
unsigned char) (GetPixelIntensity(huffman_image,p) >=
789 ((
double) QuantumRange/2.0) ? 0 : 1);
790 p+=(ptrdiff_t) GetPixelChannels(huffman_image);
796 for (n=(ssize_t) width; n > 0; )
801 for (runlength=0; ((n > 0) && (*q == 0)); n--)
808 if (runlength < 1792)
809 entry=MWTable+((runlength/64)-1);
811 entry=EXTable+(MagickMin((
size_t) runlength,2560)-1792)/64;
812 runlength-=(long) entry->count;
813 HuffmanOutputCode(entry);
815 entry=TWTable+MagickMin((
size_t) runlength,63);
816 HuffmanOutputCode(entry);
822 for (runlength=0; ((*q != 0) && (n > 0)); n--)
829 entry=MBTable+((runlength/64)-1);
830 if (runlength >= 1792)
831 entry=EXTable+(MagickMin((
size_t) runlength,2560)-1792)/64;
832 runlength-=(long) entry->count;
833 HuffmanOutputCode(entry);
835 entry=TBTable+MagickMin((
size_t) runlength,63);
836 HuffmanOutputCode(entry);
842 for (k=0; k < 11; k++)
846 if (GetPreviousImageInList(huffman_image) == (Image *) NULL)
848 proceed=SetImageProgress(huffman_image,LoadImageTag,
849 (MagickOffsetType) y,huffman_image->rows);
850 if (proceed == MagickFalse)
857 for (i=0; i < 6; i++)
859 for (k=0; k < 11; k++)
866 if (((
int) bit != 0x80) != 0)
868 if (LocaleCompare(image_info->magick,
"FAX") == 0)
869 (void) WriteBlobByte(image,
byte);
871 Ascii85Encode(image,
byte);
873 if (LocaleCompare(image_info->magick,
"FAX") != 0)
875 huffman_image=DestroyImage(huffman_image);
876 scanline=(
unsigned char *) RelinquishMagickMemory(scanline);
911MagickExport MagickBooleanType LZWEncodeImage(Image *image,
const size_t length,
912 unsigned char *magick_restrict pixels,ExceptionInfo *exception)
916#define OutputCode(code) \
918 accumulator+=code << (32-code_width-number_bits); \
919 number_bits+=code_width; \
920 while (number_bits >= 8) \
922 (void) WriteBlobByte(image,(unsigned char) (accumulator >> 24)); \
923 accumulator=accumulator << 8; \
928 typedef struct _TableType
955 assert(image != (Image *) NULL);
956 assert(image->signature == MagickCoreSignature);
957 assert(pixels != (
unsigned char *) NULL);
958 assert(exception != (ExceptionInfo *) NULL);
959 assert(exception->signature == MagickCoreSignature);
960 if (IsEventLogging() != MagickFalse)
961 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
962 table=(TableType *) AcquireQuantumMemory(1UL << 12,
sizeof(*table));
963 if (table == (TableType *) NULL)
964 ThrowBinaryException(ResourceLimitWarning,
"MemoryAllocationFailed",
974 for (index=0; index < 256; index++)
976 table[index].prefix=(-1);
977 table[index].suffix=(ssize_t) index;
978 table[index].next=(-1);
982 last_code=(size_t) pixels[0];
983 for (i=1; i < (ssize_t) length; i++)
988 index=(ssize_t) last_code;
990 if ((table[index].prefix != (ssize_t) last_code) ||
991 (table[index].suffix != (ssize_t) pixels[i]))
992 index=table[index].next;
995 last_code=(size_t) index;
998 if (last_code != (
size_t) index)
1003 OutputCode(last_code);
1004 table[next_index].prefix=(ssize_t) last_code;
1005 table[next_index].suffix=(ssize_t) pixels[i];
1006 table[next_index].next=table[last_code].next;
1007 table[last_code].next=(ssize_t) next_index;
1012 if ((next_index >> code_width) != 0)
1015 if (code_width > 12)
1022 for (index=0; index < 256; index++)
1024 table[index].prefix=(-1);
1025 table[index].suffix=index;
1026 table[index].next=(-1);
1028 next_index=LZWEod+1;
1032 last_code=(size_t) pixels[i];
1038 OutputCode(last_code);
1040 if (number_bits != 0)
1041 (void) WriteBlobByte(image,(
unsigned char) (accumulator >> 24));
1042 table=(TableType *) RelinquishMagickMemory(table);
1076MagickExport MagickBooleanType PackbitsEncodeImage(Image *image,
1077 const size_t length,
unsigned char *magick_restrict pixels,
1078 ExceptionInfo *exception)
1093 assert(image != (Image *) NULL);
1094 assert(image->signature == MagickCoreSignature);
1095 assert(pixels != (
unsigned char *) NULL);
1096 if (IsEventLogging() != MagickFalse)
1097 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1098 packbits=(
unsigned char *) AcquireQuantumMemory(128UL,
sizeof(*packbits));
1099 if (packbits == (
unsigned char *) NULL)
1100 ThrowBinaryException(ResourceLimitError,
"MemoryAllocationFailed",
1102 for (i=(ssize_t) length; i != 0; )
1109 (void) WriteBlobByte(image,(
unsigned char) 0);
1110 (void) WriteBlobByte(image,*pixels);
1116 (void) WriteBlobByte(image,(
unsigned char) 1);
1117 (void) WriteBlobByte(image,*pixels);
1118 (void) WriteBlobByte(image,pixels[1]);
1124 if ((*pixels == *(pixels+1)) && (*(pixels+1) == *(pixels+2)))
1126 (void) WriteBlobByte(image,(
unsigned char) ((256-3)+1));
1127 (void) WriteBlobByte(image,*pixels);
1130 (void) WriteBlobByte(image,(
unsigned char) 2);
1131 (void) WriteBlobByte(image,*pixels);
1132 (void) WriteBlobByte(image,pixels[1]);
1133 (void) WriteBlobByte(image,pixels[2]);
1138 if ((*pixels == *(pixels+1)) && (*(pixels+1) == *(pixels+2)))
1144 while (((ssize_t) count < i) && (*pixels == *(pixels+count)))
1151 (void) WriteBlobByte(image,(
unsigned char) ((256-count)+1));
1152 (void) WriteBlobByte(image,*pixels);
1160 while ((*(pixels+count) != *(pixels+count+1)) ||
1161 (*(pixels+count+1) != *(pixels+count+2)))
1163 packbits[count+1]=pixels[count];
1165 if (((ssize_t) count >= (i-3)) || (count >= 127))
1169 *packbits=(
unsigned char) (count-1);
1170 for (j=0; j <= (ssize_t) count; j++)
1171 (
void) WriteBlobByte(image,packbits[j]);
1177 (void) WriteBlobByte(image,(
unsigned char) 128);
1178 packbits=(
unsigned char *) RelinquishMagickMemory(packbits);
1182#if defined(MAGICKCORE_ZLIB_DELEGATE)
1216static voidpf AcquireZIPMemory(voidpf context,
unsigned int items,
1220 return((voidpf) AcquireQuantumMemory(items,size));
1223static void RelinquishZIPMemory(voidpf context,voidpf memory)
1226 memory=RelinquishMagickMemory(memory);
1229MagickExport MagickBooleanType ZLIBEncodeImage(Image *image,
const size_t length,
1230 unsigned char *magick_restrict pixels,ExceptionInfo *exception)
1247 assert(image != (Image *) NULL);
1248 assert(image->signature == MagickCoreSignature);
1249 if (IsEventLogging() != MagickFalse)
1250 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1251 compress_packets=(size_t) (1.001*length+12);
1252 compress_pixels=(
unsigned char *) AcquireQuantumMemory(compress_packets,
1253 sizeof(*compress_pixels));
1254 if (compress_pixels == (
unsigned char *) NULL)
1255 ThrowBinaryException(ResourceLimitError,
"MemoryAllocationFailed",
1257 (void) memset(&stream,0,
sizeof(stream));
1258 stream.next_in=pixels;
1259 stream.avail_in=(
unsigned int) length;
1260 stream.next_out=compress_pixels;
1261 stream.avail_out=(
unsigned int) compress_packets;
1262 stream.zalloc=AcquireZIPMemory;
1263 stream.zfree=RelinquishZIPMemory;
1264 stream.opaque=(voidpf) NULL;
1265 status=deflateInit(&stream,(
int) (image->quality ==
1266 UndefinedCompressionQuality ? 7 : MagickMin(image->quality/10,9)));
1269 status=deflate(&stream,Z_FINISH);
1270 if (status == Z_STREAM_END)
1271 status=deflateEnd(&stream);
1273 (
void) deflateEnd(&stream);
1274 compress_packets=(size_t) stream.total_out;
1277 ThrowBinaryException(CoderError,
"UnableToZipCompressImage",image->filename)
1278 for (i=0; i < (ssize_t) compress_packets; i++)
1279 (
void) WriteBlobByte(image,compress_pixels[i]);
1280 compress_pixels=(
unsigned char *) RelinquishMagickMemory(compress_pixels);
1284MagickExport MagickBooleanType ZLIBEncodeImage(Image *image,
1285 const size_t magick_unused(length),
unsigned char *magick_unused(pixels),
1286 ExceptionInfo *exception)
1288 magick_unreferenced(length);
1289 magick_unreferenced(pixels);
1290 assert(image != (Image *) NULL);
1291 assert(image->signature == MagickCoreSignature);
1292 if (IsEventLogging() != MagickFalse)
1293 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1294 (void) ThrowMagickException(exception,GetMagickModule(),MissingDelegateError,
1295 "DelegateLibrarySupportNotBuiltIn",
"'%s' (ZIP)",image->filename);
1296 return(MagickFalse);