00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043 #include "magick/studio.h"
00044 #include "magick/blob.h"
00045 #include "magick/blob-private.h"
00046 #include "magick/color-private.h"
00047 #include "magick/cache.h"
00048 #include "magick/compress.h"
00049 #include "magick/constitute.h"
00050 #include "magick/exception.h"
00051 #include "magick/exception-private.h"
00052 #include "magick/image-private.h"
00053 #include "magick/list.h"
00054 #include "magick/memory_.h"
00055 #include "magick/monitor.h"
00056 #include "magick/monitor-private.h"
00057 #include "magick/option.h"
00058 #include "magick/resource_.h"
00059 #include "magick/string_.h"
00060 #if defined(MAGICKCORE_TIFF_DELEGATE)
00061 #if defined(MAGICKCORE_HAVE_TIFFCONF_H)
00062 #include "tiffconf.h"
00063 #endif
00064 #include "tiffio.h"
00065 #define CCITTParam "-1"
00066 #else
00067 #define CCITTParam "0"
00068 #endif
00069 #if defined(MAGICKCORE_ZLIB_DELEGATE)
00070 #include "zlib.h"
00071 #endif
00072
00073
00074
00075
00076 struct _Ascii85Info
00077 {
00078 long
00079 offset,
00080 line_break;
00081
00082 unsigned char
00083 buffer[10];
00084 };
00085
00086 typedef struct HuffmanTable
00087 {
00088 unsigned long
00089 id,
00090 code,
00091 length,
00092 count;
00093 } HuffmanTable;
00094
00095
00096
00097
00098 #define TWId 23
00099 #define MWId 24
00100 #define TBId 25
00101 #define MBId 26
00102 #define EXId 27
00103
00104 static const HuffmanTable
00105 MBTable[]=
00106 {
00107 { MBId, 0x0f, 10, 64 }, { MBId, 0xc8, 12, 128 },
00108 { MBId, 0xc9, 12, 192 }, { MBId, 0x5b, 12, 256 },
00109 { MBId, 0x33, 12, 320 }, { MBId, 0x34, 12, 384 },
00110 { MBId, 0x35, 12, 448 }, { MBId, 0x6c, 13, 512 },
00111 { MBId, 0x6d, 13, 576 }, { MBId, 0x4a, 13, 640 },
00112 { MBId, 0x4b, 13, 704 }, { MBId, 0x4c, 13, 768 },
00113 { MBId, 0x4d, 13, 832 }, { MBId, 0x72, 13, 896 },
00114 { MBId, 0x73, 13, 960 }, { MBId, 0x74, 13, 1024 },
00115 { MBId, 0x75, 13, 1088 }, { MBId, 0x76, 13, 1152 },
00116 { MBId, 0x77, 13, 1216 }, { MBId, 0x52, 13, 1280 },
00117 { MBId, 0x53, 13, 1344 }, { MBId, 0x54, 13, 1408 },
00118 { MBId, 0x55, 13, 1472 }, { MBId, 0x5a, 13, 1536 },
00119 { MBId, 0x5b, 13, 1600 }, { MBId, 0x64, 13, 1664 },
00120 { MBId, 0x65, 13, 1728 }, { MBId, 0x00, 0, 0 }
00121 };
00122
00123 static const HuffmanTable
00124 EXTable[]=
00125 {
00126 { EXId, 0x08, 11, 1792 }, { EXId, 0x0c, 11, 1856 },
00127 { EXId, 0x0d, 11, 1920 }, { EXId, 0x12, 12, 1984 },
00128 { EXId, 0x13, 12, 2048 }, { EXId, 0x14, 12, 2112 },
00129 { EXId, 0x15, 12, 2176 }, { EXId, 0x16, 12, 2240 },
00130 { EXId, 0x17, 12, 2304 }, { EXId, 0x1c, 12, 2368 },
00131 { EXId, 0x1d, 12, 2432 }, { EXId, 0x1e, 12, 2496 },
00132 { EXId, 0x1f, 12, 2560 }, { EXId, 0x00, 0, 0 }
00133 };
00134
00135 static const HuffmanTable
00136 MWTable[]=
00137 {
00138 { MWId, 0x1b, 5, 64 }, { MWId, 0x12, 5, 128 },
00139 { MWId, 0x17, 6, 192 }, { MWId, 0x37, 7, 256 },
00140 { MWId, 0x36, 8, 320 }, { MWId, 0x37, 8, 384 },
00141 { MWId, 0x64, 8, 448 }, { MWId, 0x65, 8, 512 },
00142 { MWId, 0x68, 8, 576 }, { MWId, 0x67, 8, 640 },
00143 { MWId, 0xcc, 9, 704 }, { MWId, 0xcd, 9, 768 },
00144 { MWId, 0xd2, 9, 832 }, { MWId, 0xd3, 9, 896 },
00145 { MWId, 0xd4, 9, 960 }, { MWId, 0xd5, 9, 1024 },
00146 { MWId, 0xd6, 9, 1088 }, { MWId, 0xd7, 9, 1152 },
00147 { MWId, 0xd8, 9, 1216 }, { MWId, 0xd9, 9, 1280 },
00148 { MWId, 0xda, 9, 1344 }, { MWId, 0xdb, 9, 1408 },
00149 { MWId, 0x98, 9, 1472 }, { MWId, 0x99, 9, 1536 },
00150 { MWId, 0x9a, 9, 1600 }, { MWId, 0x18, 6, 1664 },
00151 { MWId, 0x9b, 9, 1728 }, { MWId, 0x00, 0, 0 }
00152 };
00153
00154 static const HuffmanTable
00155 TBTable[]=
00156 {
00157 { TBId, 0x37, 10, 0 }, { TBId, 0x02, 3, 1 }, { TBId, 0x03, 2, 2 },
00158 { TBId, 0x02, 2, 3 }, { TBId, 0x03, 3, 4 }, { TBId, 0x03, 4, 5 },
00159 { TBId, 0x02, 4, 6 }, { TBId, 0x03, 5, 7 }, { TBId, 0x05, 6, 8 },
00160 { TBId, 0x04, 6, 9 }, { TBId, 0x04, 7, 10 }, { TBId, 0x05, 7, 11 },
00161 { TBId, 0x07, 7, 12 }, { TBId, 0x04, 8, 13 }, { TBId, 0x07, 8, 14 },
00162 { TBId, 0x18, 9, 15 }, { TBId, 0x17, 10, 16 }, { TBId, 0x18, 10, 17 },
00163 { TBId, 0x08, 10, 18 }, { TBId, 0x67, 11, 19 }, { TBId, 0x68, 11, 20 },
00164 { TBId, 0x6c, 11, 21 }, { TBId, 0x37, 11, 22 }, { TBId, 0x28, 11, 23 },
00165 { TBId, 0x17, 11, 24 }, { TBId, 0x18, 11, 25 }, { TBId, 0xca, 12, 26 },
00166 { TBId, 0xcb, 12, 27 }, { TBId, 0xcc, 12, 28 }, { TBId, 0xcd, 12, 29 },
00167 { TBId, 0x68, 12, 30 }, { TBId, 0x69, 12, 31 }, { TBId, 0x6a, 12, 32 },
00168 { TBId, 0x6b, 12, 33 }, { TBId, 0xd2, 12, 34 }, { TBId, 0xd3, 12, 35 },
00169 { TBId, 0xd4, 12, 36 }, { TBId, 0xd5, 12, 37 }, { TBId, 0xd6, 12, 38 },
00170 { TBId, 0xd7, 12, 39 }, { TBId, 0x6c, 12, 40 }, { TBId, 0x6d, 12, 41 },
00171 { TBId, 0xda, 12, 42 }, { TBId, 0xdb, 12, 43 }, { TBId, 0x54, 12, 44 },
00172 { TBId, 0x55, 12, 45 }, { TBId, 0x56, 12, 46 }, { TBId, 0x57, 12, 47 },
00173 { TBId, 0x64, 12, 48 }, { TBId, 0x65, 12, 49 }, { TBId, 0x52, 12, 50 },
00174 { TBId, 0x53, 12, 51 }, { TBId, 0x24, 12, 52 }, { TBId, 0x37, 12, 53 },
00175 { TBId, 0x38, 12, 54 }, { TBId, 0x27, 12, 55 }, { TBId, 0x28, 12, 56 },
00176 { TBId, 0x58, 12, 57 }, { TBId, 0x59, 12, 58 }, { TBId, 0x2b, 12, 59 },
00177 { TBId, 0x2c, 12, 60 }, { TBId, 0x5a, 12, 61 }, { TBId, 0x66, 12, 62 },
00178 { TBId, 0x67, 12, 63 }, { TBId, 0x00, 0, 0 }
00179 };
00180
00181 static const HuffmanTable
00182 TWTable[]=
00183 {
00184 { TWId, 0x35, 8, 0 }, { TWId, 0x07, 6, 1 }, { TWId, 0x07, 4, 2 },
00185 { TWId, 0x08, 4, 3 }, { TWId, 0x0b, 4, 4 }, { TWId, 0x0c, 4, 5 },
00186 { TWId, 0x0e, 4, 6 }, { TWId, 0x0f, 4, 7 }, { TWId, 0x13, 5, 8 },
00187 { TWId, 0x14, 5, 9 }, { TWId, 0x07, 5, 10 }, { TWId, 0x08, 5, 11 },
00188 { TWId, 0x08, 6, 12 }, { TWId, 0x03, 6, 13 }, { TWId, 0x34, 6, 14 },
00189 { TWId, 0x35, 6, 15 }, { TWId, 0x2a, 6, 16 }, { TWId, 0x2b, 6, 17 },
00190 { TWId, 0x27, 7, 18 }, { TWId, 0x0c, 7, 19 }, { TWId, 0x08, 7, 20 },
00191 { TWId, 0x17, 7, 21 }, { TWId, 0x03, 7, 22 }, { TWId, 0x04, 7, 23 },
00192 { TWId, 0x28, 7, 24 }, { TWId, 0x2b, 7, 25 }, { TWId, 0x13, 7, 26 },
00193 { TWId, 0x24, 7, 27 }, { TWId, 0x18, 7, 28 }, { TWId, 0x02, 8, 29 },
00194 { TWId, 0x03, 8, 30 }, { TWId, 0x1a, 8, 31 }, { TWId, 0x1b, 8, 32 },
00195 { TWId, 0x12, 8, 33 }, { TWId, 0x13, 8, 34 }, { TWId, 0x14, 8, 35 },
00196 { TWId, 0x15, 8, 36 }, { TWId, 0x16, 8, 37 }, { TWId, 0x17, 8, 38 },
00197 { TWId, 0x28, 8, 39 }, { TWId, 0x29, 8, 40 }, { TWId, 0x2a, 8, 41 },
00198 { TWId, 0x2b, 8, 42 }, { TWId, 0x2c, 8, 43 }, { TWId, 0x2d, 8, 44 },
00199 { TWId, 0x04, 8, 45 }, { TWId, 0x05, 8, 46 }, { TWId, 0x0a, 8, 47 },
00200 { TWId, 0x0b, 8, 48 }, { TWId, 0x52, 8, 49 }, { TWId, 0x53, 8, 50 },
00201 { TWId, 0x54, 8, 51 }, { TWId, 0x55, 8, 52 }, { TWId, 0x24, 8, 53 },
00202 { TWId, 0x25, 8, 54 }, { TWId, 0x58, 8, 55 }, { TWId, 0x59, 8, 56 },
00203 { TWId, 0x5a, 8, 57 }, { TWId, 0x5b, 8, 58 }, { TWId, 0x4a, 8, 59 },
00204 { TWId, 0x4b, 8, 60 }, { TWId, 0x32, 8, 61 }, { TWId, 0x33, 8, 62 },
00205 { TWId, 0x34, 8, 63 }, { TWId, 0x00, 0, 0 }
00206 };
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235 #define MaxLineExtent 36
00236
00237 static char *Ascii85Tuple(unsigned char *data)
00238 {
00239 static char
00240 tuple[6];
00241
00242 register long
00243 i,
00244 x;
00245
00246 unsigned long
00247 code,
00248 quantum;
00249
00250 code=((((unsigned long) data[0] << 8) | (unsigned long) data[1]) << 16) |
00251 ((unsigned long) data[2] << 8) | (unsigned long) data[3];
00252 if (code == 0L)
00253 {
00254 tuple[0]='z';
00255 tuple[1]='\0';
00256 return(tuple);
00257 }
00258 quantum=85UL*85UL*85UL*85UL;
00259 for (i=0; i < 4; i++)
00260 {
00261 x=(long) (code/quantum);
00262 code-=quantum*x;
00263 tuple[i]=(char) (x+(int) '!');
00264 quantum/=85L;
00265 }
00266 tuple[4]=(char) ((code % 85L)+(int) '!');
00267 tuple[5]='\0';
00268 return(tuple);
00269 }
00270
00271 MagickExport void Ascii85Initialize(Image *image)
00272 {
00273
00274
00275
00276 if (image->ascii85 == (Ascii85Info *) NULL)
00277 image->ascii85=(Ascii85Info *) AcquireMagickMemory(sizeof(*image->ascii85));
00278 if (image->ascii85 == (Ascii85Info *) NULL)
00279 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
00280 (void) ResetMagickMemory(image->ascii85,0,sizeof(*image->ascii85));
00281 image->ascii85->line_break=MaxLineExtent << 1;
00282 image->ascii85->offset=0;
00283 }
00284
00285 MagickExport void Ascii85Flush(Image *image)
00286 {
00287 register char
00288 *tuple;
00289
00290 assert(image != (Image *) NULL);
00291 assert(image->signature == MagickSignature);
00292 if (image->debug != MagickFalse)
00293 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
00294 assert(image->ascii85 != (Ascii85Info *) NULL);
00295 if (image->ascii85->offset > 0)
00296 {
00297 image->ascii85->buffer[image->ascii85->offset]='\0';
00298 image->ascii85->buffer[image->ascii85->offset+1]='\0';
00299 image->ascii85->buffer[image->ascii85->offset+2]='\0';
00300 tuple=Ascii85Tuple(image->ascii85->buffer);
00301 (void) WriteBlob(image,(size_t) image->ascii85->offset+1,
00302 (const unsigned char *) (*tuple == 'z' ? "!!!!" : tuple));
00303 }
00304 (void) WriteBlobByte(image,'~');
00305 (void) WriteBlobByte(image,'>');
00306 (void) WriteBlobByte(image,'\n');
00307 }
00308
00309 MagickExport void Ascii85Encode(Image *image,const unsigned char code)
00310 {
00311 long
00312 n;
00313
00314 register char
00315 *q;
00316
00317 register unsigned char
00318 *p;
00319
00320 assert(image != (Image *) NULL);
00321 assert(image->signature == MagickSignature);
00322 assert(image->ascii85 != (Ascii85Info *) NULL);
00323 image->ascii85->buffer[image->ascii85->offset]=code;
00324 image->ascii85->offset++;
00325 if (image->ascii85->offset < 4)
00326 return;
00327 p=image->ascii85->buffer;
00328 for (n=image->ascii85->offset; n >= 4; n-=4)
00329 {
00330 for (q=Ascii85Tuple(p); *q != '\0'; q++)
00331 {
00332 image->ascii85->line_break--;
00333 if ((image->ascii85->line_break < 0) && (*q != '%'))
00334 {
00335 (void) WriteBlobByte(image,'\n');
00336 image->ascii85->line_break=2*MaxLineExtent;
00337 }
00338 (void) WriteBlobByte(image,(unsigned char) *q);
00339 }
00340 p+=8;
00341 }
00342 image->ascii85->offset=n;
00343 p-=4;
00344 for (n=0; n < 4; n++)
00345 image->ascii85->buffer[n]=(*p++);
00346 }
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371 static inline size_t MagickMax(const size_t x,const size_t y)
00372 {
00373 if (x > y)
00374 return(x);
00375 return(y);
00376 }
00377
00378 static inline size_t MagickMin(const size_t x,const size_t y)
00379 {
00380 if (x < y)
00381 return(x);
00382 return(y);
00383 }
00384
00385 MagickExport MagickBooleanType HuffmanDecodeImage(Image *image)
00386 {
00387 #define HashSize 1021
00388 #define MBHashA 293
00389 #define MBHashB 2695
00390 #define MWHashA 3510
00391 #define MWHashB 1178
00392
00393 #define InitializeHashTable(hash,table,a,b) \
00394 { \
00395 entry=table; \
00396 while (entry->code != 0) \
00397 { \
00398 hash[((entry->length+a)*(entry->code+b)) % HashSize]=(HuffmanTable *) entry; \
00399 entry++; \
00400 } \
00401 }
00402
00403 #define InputBit(bit) \
00404 { \
00405 if ((mask & 0xff) == 0) \
00406 { \
00407 byte=ReadBlobByte(image); \
00408 if (byte == EOF) \
00409 break; \
00410 mask=0x80; \
00411 } \
00412 runlength++; \
00413 bit=(unsigned long) ((byte & mask) != 0 ? 0x01 : 0x00); \
00414 mask>>=1; \
00415 if (bit != 0) \
00416 runlength=0; \
00417 }
00418
00419 const HuffmanTable
00420 *entry;
00421
00422 ExceptionInfo
00423 *exception;
00424
00425 HuffmanTable
00426 **mb_hash,
00427 **mw_hash;
00428
00429 IndexPacket
00430 index;
00431
00432 int
00433 byte;
00434
00435 long
00436 y;
00437
00438 MagickBooleanType
00439 proceed;
00440
00441 register IndexPacket
00442 *indexes;
00443
00444 register long
00445 i;
00446
00447 register unsigned char
00448 *p;
00449
00450 ssize_t
00451 count;
00452
00453 unsigned char
00454 *scanline;
00455
00456 unsigned int
00457 bail,
00458 color;
00459
00460 unsigned long
00461 bit,
00462 code,
00463 mask,
00464 length,
00465 null_lines,
00466 runlength;
00467
00468
00469
00470
00471 assert(image != (Image *) NULL);
00472 assert(image->signature == MagickSignature);
00473 if (image->debug != MagickFalse)
00474 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
00475 mb_hash=(HuffmanTable **) AcquireQuantumMemory(HashSize,sizeof(*mb_hash));
00476 mw_hash=(HuffmanTable **) AcquireQuantumMemory(HashSize,sizeof(*mw_hash));
00477 scanline=(unsigned char *) AcquireQuantumMemory((size_t) image->columns,
00478 sizeof(*scanline));
00479 if ((mb_hash == (HuffmanTable **) NULL) ||
00480 (mw_hash == (HuffmanTable **) NULL) ||
00481 (scanline == (unsigned char *) NULL))
00482 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
00483 image->filename);
00484
00485
00486
00487 for (i=0; i < HashSize; i++)
00488 {
00489 mb_hash[i]=(HuffmanTable *) NULL;
00490 mw_hash[i]=(HuffmanTable *) NULL;
00491 }
00492 InitializeHashTable(mw_hash,TWTable,MWHashA,MWHashB);
00493 InitializeHashTable(mw_hash,MWTable,MWHashA,MWHashB);
00494 InitializeHashTable(mw_hash,EXTable,MWHashA,MWHashB);
00495 InitializeHashTable(mb_hash,TBTable,MBHashA,MBHashB);
00496 InitializeHashTable(mb_hash,MBTable,MBHashA,MBHashB);
00497 InitializeHashTable(mb_hash,EXTable,MBHashA,MBHashB);
00498
00499
00500
00501 byte=0;
00502 mask=0;
00503 null_lines=0;
00504 runlength=0;
00505 while (runlength < 11)
00506 InputBit(bit);
00507 do { InputBit(bit); } while ((int) bit == 0);
00508 image->x_resolution=204.0;
00509 image->y_resolution=196.0;
00510 image->units=PixelsPerInchResolution;
00511 exception=(&image->exception);
00512 for (y=0; ((y < (long) image->rows) && (null_lines < 3)); )
00513 {
00514 register long
00515 x;
00516
00517 register PixelPacket
00518 *__restrict q;
00519
00520
00521
00522
00523 p=scanline;
00524 for (x=0; x < (long) image->columns; x++)
00525 *p++=(unsigned char) 0;
00526
00527
00528
00529 color=MagickTrue;
00530 code=0;
00531 count=0;
00532 length=0;
00533 runlength=0;
00534 x=0;
00535 for ( ; ; )
00536 {
00537 if (byte == EOF)
00538 break;
00539 if (x >= (long) image->columns)
00540 {
00541 while (runlength < 11)
00542 InputBit(bit);
00543 do { InputBit(bit); } while ((int) bit == 0);
00544 break;
00545 }
00546 bail=MagickFalse;
00547 do
00548 {
00549 if (runlength < 11)
00550 InputBit(bit)
00551 else
00552 {
00553 InputBit(bit);
00554 if ((int) bit != 0)
00555 {
00556 null_lines++;
00557 if (x != 0)
00558 null_lines=0;
00559 bail=MagickTrue;
00560 break;
00561 }
00562 }
00563 code=(code << 1)+(unsigned long) bit;
00564 length++;
00565 } while (code == 0);
00566 if (bail != MagickFalse)
00567 break;
00568 if (length > 13)
00569 {
00570 while (runlength < 11)
00571 InputBit(bit);
00572 do { InputBit(bit); } while ((int) bit == 0);
00573 break;
00574 }
00575 if (color != MagickFalse)
00576 {
00577 if (length < 4)
00578 continue;
00579 entry=mw_hash[((length+MWHashA)*(code+MWHashB)) % HashSize];
00580 }
00581 else
00582 {
00583 if (length < 2)
00584 continue;
00585 entry=mb_hash[((length+MBHashA)*(code+MBHashB)) % HashSize];
00586 }
00587 if (entry == (const HuffmanTable *) NULL)
00588 continue;
00589 if ((entry->length != length) || (entry->code != code))
00590 continue;
00591 switch (entry->id)
00592 {
00593 case TWId:
00594 case TBId:
00595 {
00596 count+=entry->count;
00597 if ((x+count) > (long) image->columns)
00598 count=(ssize_t) image->columns-x;
00599 if (count > 0)
00600 {
00601 if (color != MagickFalse)
00602 {
00603 x+=count;
00604 count=0;
00605 }
00606 else
00607 for ( ; count > 0; count--)
00608 scanline[x++]=(unsigned char) 1;
00609 }
00610 color=(unsigned int)
00611 ((color == MagickFalse) ? MagickTrue : MagickFalse);
00612 break;
00613 }
00614 case MWId:
00615 case MBId:
00616 case EXId:
00617 {
00618 count+=entry->count;
00619 break;
00620 }
00621 default:
00622 break;
00623 }
00624 code=0;
00625 length=0;
00626 }
00627
00628
00629
00630 p=scanline;
00631 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
00632 if (q == (PixelPacket *) NULL)
00633 break;
00634 indexes=GetAuthenticIndexQueue(image);
00635 for (x=0; x < (long) image->columns; x++)
00636 {
00637 index=(IndexPacket) (*p++);
00638 indexes[x]=index;
00639 *q++=image->colormap[(long) index];
00640 }
00641 if (SyncAuthenticPixels(image,exception) == MagickFalse)
00642 break;
00643 proceed=SetImageProgress(image,LoadImageTag,y,image->rows);
00644 if (proceed == MagickFalse)
00645 break;
00646 y++;
00647 }
00648 image->rows=(unsigned long) MagickMax((size_t) y-3,1);
00649 image->compression=FaxCompression;
00650
00651
00652
00653 mw_hash=(HuffmanTable **) RelinquishMagickMemory(mw_hash);
00654 mb_hash=(HuffmanTable **) RelinquishMagickMemory(mb_hash);
00655 scanline=(unsigned char *) RelinquishMagickMemory(scanline);
00656 return(MagickTrue);
00657 }
00658
00659
00660
00661
00662
00663
00664
00665
00666
00667
00668
00669
00670
00671
00672
00673
00674
00675
00676
00677
00678
00679
00680
00681
00682
00683
00684
00685
00686 MagickExport MagickBooleanType HuffmanEncodeImage(const ImageInfo *image_info,
00687 Image *image,Image *inject_image)
00688 {
00689 #define HuffmanOutputCode(entry) \
00690 { \
00691 mask=1 << (entry->length-1); \
00692 while (mask != 0) \
00693 { \
00694 OutputBit(((entry->code & mask) != 0 ? 1 : 0)); \
00695 mask>>=1; \
00696 } \
00697 }
00698
00699 #define OutputBit(count) \
00700 { \
00701 if (count > 0) \
00702 byte=byte | bit; \
00703 bit>>=1; \
00704 if ((int) (bit & 0xff) == 0) \
00705 { \
00706 if (LocaleCompare(image_info->magick,"FAX") == 0) \
00707 (void) WriteBlobByte(image,(unsigned char) byte); \
00708 else \
00709 Ascii85Encode(image,byte); \
00710 byte='\0'; \
00711 bit=(unsigned char) 0x80; \
00712 } \
00713 }
00714
00715 const HuffmanTable
00716 *entry;
00717
00718 ExceptionInfo
00719 *exception;
00720
00721 int
00722 k,
00723 runlength;
00724
00725 long
00726 n,
00727 y;
00728
00729 Image
00730 *huffman_image;
00731
00732 MagickBooleanType
00733 proceed;
00734
00735 register long
00736 i,
00737 x;
00738
00739 register const PixelPacket
00740 *p;
00741
00742 register unsigned char
00743 *q;
00744
00745 unsigned char
00746 byte,
00747 bit,
00748 *scanline;
00749
00750 unsigned long
00751 mask,
00752 width;
00753
00754
00755
00756
00757 assert(image_info != (ImageInfo *) NULL);
00758 assert(image_info->signature == MagickSignature);
00759 assert(image != (Image *) NULL);
00760 assert(image->signature == MagickSignature);
00761 if (image->debug != MagickFalse)
00762 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
00763 assert(inject_image != (Image *) NULL);
00764 assert(inject_image->signature == MagickSignature);
00765 width=inject_image->columns;
00766 if (LocaleCompare(image_info->magick,"FAX") == 0)
00767 width=(unsigned long) MagickMax(inject_image->columns,1728);
00768 scanline=(unsigned char *) AcquireQuantumMemory((size_t) width+1UL,
00769 sizeof(*scanline));
00770 if (scanline == (unsigned char *) NULL)
00771 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
00772 inject_image->filename);
00773 (void) ResetMagickMemory(scanline,0,width*sizeof(*scanline));
00774 huffman_image=CloneImage(inject_image,0,0,MagickTrue,&image->exception);
00775 if (huffman_image == (Image *) NULL)
00776 {
00777 scanline=(unsigned char *) RelinquishMagickMemory(scanline);
00778 return(MagickFalse);
00779 }
00780 (void) SetImageType(huffman_image,BilevelType);
00781 byte='\0';
00782 bit=(unsigned char) 0x80;
00783 if (LocaleCompare(image_info->magick,"FAX") != 0)
00784 Ascii85Initialize(image);
00785 else
00786 {
00787
00788
00789
00790 for (k=0; k < 11; k++)
00791 OutputBit(0);
00792 OutputBit(1);
00793 }
00794
00795
00796
00797 exception=(&huffman_image->exception);
00798 q=scanline;
00799 for (y=0; y < (long) huffman_image->rows; y++)
00800 {
00801 p=GetVirtualPixels(huffman_image,0,y,huffman_image->columns,1,exception);
00802 if (p == (const PixelPacket *) NULL)
00803 break;
00804 for (x=0; x < (long) huffman_image->columns; x++)
00805 {
00806 *q++=(unsigned char) (PixelIntensity(p) >= ((MagickRealType)
00807 QuantumRange/2.0) ? 0 : 1);
00808 p++;
00809 }
00810
00811
00812
00813 q=scanline;
00814 for (n=(long) width; n > 0; )
00815 {
00816
00817
00818
00819 for (runlength=0; ((n > 0) && (*q == 0)); n--)
00820 {
00821 q++;
00822 runlength++;
00823 }
00824 if (runlength >= 64)
00825 {
00826 if (runlength < 1792)
00827 entry=MWTable+((runlength/64)-1);
00828 else
00829 entry=EXTable+(MagickMin((size_t) runlength,2560)-1792)/64;
00830 runlength-=entry->count;
00831 HuffmanOutputCode(entry);
00832 }
00833 entry=TWTable+MagickMin((size_t) runlength,63);
00834 HuffmanOutputCode(entry);
00835 if (n != 0)
00836 {
00837
00838
00839
00840 for (runlength=0; ((*q != 0) && (n > 0)); n--)
00841 {
00842 q++;
00843 runlength++;
00844 }
00845 if (runlength >= 64)
00846 {
00847 entry=MBTable+((runlength/64)-1);
00848 if (runlength >= 1792)
00849 entry=EXTable+(MagickMin((size_t) runlength,2560)-1792)/64;
00850 runlength-=entry->count;
00851 HuffmanOutputCode(entry);
00852 }
00853 entry=TBTable+MagickMin((size_t) runlength,63);
00854 HuffmanOutputCode(entry);
00855 }
00856 }
00857
00858
00859
00860 for (k=0; k < 11; k++)
00861 OutputBit(0);
00862 OutputBit(1);
00863 q=scanline;
00864 if (GetPreviousImageInList(huffman_image) == (Image *) NULL)
00865 {
00866 proceed=SetImageProgress(huffman_image,LoadImageTag,y,
00867 huffman_image->rows);
00868 if (proceed == MagickFalse)
00869 break;
00870 }
00871 }
00872
00873
00874
00875 for (i=0; i < 6; i++)
00876 {
00877 for (k=0; k < 11; k++)
00878 OutputBit(0);
00879 OutputBit(1);
00880 }
00881
00882
00883
00884 if (((int) bit != 0x80) != 0)
00885 {
00886 if (LocaleCompare(image_info->magick,"FAX") == 0)
00887 (void) WriteBlobByte(image,byte);
00888 else
00889 Ascii85Encode(image,byte);
00890 }
00891 if (LocaleCompare(image_info->magick,"FAX") != 0)
00892 Ascii85Flush(image);
00893 huffman_image=DestroyImage(huffman_image);
00894 scanline=(unsigned char *) RelinquishMagickMemory(scanline);
00895 return(MagickTrue);
00896 }
00897
00898
00899
00900
00901
00902
00903
00904
00905
00906
00907
00908
00909
00910
00911
00912
00913
00914
00915
00916
00917
00918
00919
00920
00921
00922
00923
00924
00925
00926
00927 MagickExport MagickBooleanType LZWEncodeImage(Image *image,const size_t length,
00928 unsigned char *pixels)
00929 {
00930 #define LZWClr 256UL
00931 #define LZWEod 257UL
00932 #define OutputCode(code) \
00933 { \
00934 accumulator+=code << (32-code_width-number_bits); \
00935 number_bits+=code_width; \
00936 while (number_bits >= 8) \
00937 { \
00938 (void) WriteBlobByte(image,(unsigned char) (accumulator >> 24)); \
00939 accumulator=accumulator << 8; \
00940 number_bits-=8; \
00941 } \
00942 }
00943
00944 typedef struct _TableType
00945 {
00946 long
00947 prefix,
00948 suffix,
00949 next;
00950 } TableType;
00951
00952 long
00953 index;
00954
00955 register long
00956 i;
00957
00958 TableType
00959 *table;
00960
00961 unsigned long
00962 accumulator,
00963 number_bits,
00964 code_width,
00965 last_code,
00966 next_index;
00967
00968
00969
00970
00971 assert(image != (Image *) NULL);
00972 assert(image->signature == MagickSignature);
00973 if (image->debug != MagickFalse)
00974 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
00975 assert(pixels != (unsigned char *) NULL);
00976 table=(TableType *) AcquireQuantumMemory(1UL << 12,sizeof(*table));
00977 if (table == (TableType *) NULL)
00978 return(MagickFalse);
00979
00980
00981
00982 accumulator=0;
00983 code_width=9;
00984 number_bits=0;
00985 last_code=0;
00986 OutputCode(LZWClr);
00987 for (index=0; index < 256; index++)
00988 {
00989 table[index].prefix=(-1);
00990 table[index].suffix=(short) index;
00991 table[index].next=(-1);
00992 }
00993 next_index=LZWEod+1;
00994 code_width=9;
00995 last_code=(unsigned long) pixels[0];
00996 for (i=1; i < (long) length; i++)
00997 {
00998
00999
01000
01001 index=(long) last_code;
01002 while (index != -1)
01003 if ((table[index].prefix != (long) last_code) ||
01004 (table[index].suffix != (long) pixels[i]))
01005 index=table[index].next;
01006 else
01007 {
01008 last_code=(unsigned long) index;
01009 break;
01010 }
01011 if (last_code != (unsigned long) index)
01012 {
01013
01014
01015
01016 OutputCode(last_code);
01017 table[next_index].prefix=(long) last_code;
01018 table[next_index].suffix=(short) pixels[i];
01019 table[next_index].next=table[last_code].next;
01020 table[last_code].next=(long) next_index;
01021 next_index++;
01022
01023
01024
01025 if ((next_index >> code_width) != 0)
01026 {
01027 code_width++;
01028 if (code_width > 12)
01029 {
01030
01031
01032
01033 code_width--;
01034 OutputCode(LZWClr);
01035 for (index=0; index < 256; index++)
01036 {
01037 table[index].prefix=(-1);
01038 table[index].suffix=index;
01039 table[index].next=(-1);
01040 }
01041 next_index=LZWEod+1;
01042 code_width=9;
01043 }
01044 }
01045 last_code=(unsigned long) pixels[i];
01046 }
01047 }
01048
01049
01050
01051 OutputCode(last_code);
01052 OutputCode(LZWEod);
01053 if (number_bits != 0)
01054 (void) WriteBlobByte(image,(unsigned char) (accumulator >> 24));
01055 table=(TableType *) RelinquishMagickMemory(table);
01056 return(MagickTrue);
01057 }
01058
01059
01060
01061
01062
01063
01064
01065
01066
01067
01068
01069
01070
01071
01072
01073
01074
01075
01076
01077
01078
01079
01080
01081
01082
01083
01084
01085
01086
01087
01088
01089 MagickExport MagickBooleanType PackbitsEncodeImage(Image *image,
01090 const size_t length,unsigned char *pixels)
01091 {
01092 int
01093 count;
01094
01095 register long
01096 i,
01097 j;
01098
01099 unsigned char
01100 *packbits;
01101
01102
01103
01104
01105 assert(image != (Image *) NULL);
01106 assert(image->signature == MagickSignature);
01107 if (image->debug != MagickFalse)
01108 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
01109 assert(pixels != (unsigned char *) NULL);
01110 packbits=(unsigned char *) AcquireQuantumMemory(128UL,sizeof(*packbits));
01111 if (packbits == (unsigned char *) NULL)
01112 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
01113 image->filename);
01114 for (i=(long) length; i != 0; )
01115 {
01116 switch (i)
01117 {
01118 case 1:
01119 {
01120 i--;
01121 (void) WriteBlobByte(image,(unsigned char) 0);
01122 (void) WriteBlobByte(image,*pixels);
01123 break;
01124 }
01125 case 2:
01126 {
01127 i-=2;
01128 (void) WriteBlobByte(image,(unsigned char) 1);
01129 (void) WriteBlobByte(image,*pixels);
01130 (void) WriteBlobByte(image,pixels[1]);
01131 break;
01132 }
01133 case 3:
01134 {
01135 i-=3;
01136 if ((*pixels == *(pixels+1)) && (*(pixels+1) == *(pixels+2)))
01137 {
01138 (void) WriteBlobByte(image,(unsigned char) ((256-3)+1));
01139 (void) WriteBlobByte(image,*pixels);
01140 break;
01141 }
01142 (void) WriteBlobByte(image,(unsigned char) 2);
01143 (void) WriteBlobByte(image,*pixels);
01144 (void) WriteBlobByte(image,pixels[1]);
01145 (void) WriteBlobByte(image,pixels[2]);
01146 break;
01147 }
01148 default:
01149 {
01150 if ((*pixels == *(pixels+1)) && (*(pixels+1) == *(pixels+2)))
01151 {
01152
01153
01154
01155 count=3;
01156 while (((long) count < i) && (*pixels == *(pixels+count)))
01157 {
01158 count++;
01159 if (count >= 127)
01160 break;
01161 }
01162 i-=count;
01163 (void) WriteBlobByte(image,(unsigned char) ((256-count)+1));
01164 (void) WriteBlobByte(image,*pixels);
01165 pixels+=count;
01166 break;
01167 }
01168
01169
01170
01171 count=0;
01172 while ((*(pixels+count) != *(pixels+count+1)) ||
01173 (*(pixels+count+1) != *(pixels+count+2)))
01174 {
01175 packbits[count+1]=pixels[count];
01176 count++;
01177 if (((long) count >= (i-3)) || (count >= 127))
01178 break;
01179 }
01180 i-=count;
01181 *packbits=(unsigned char) (count-1);
01182 for (j=0; j <= (long) count; j++)
01183 (void) WriteBlobByte(image,packbits[j]);
01184 pixels+=count;
01185 break;
01186 }
01187 }
01188 }
01189 (void) WriteBlobByte(image,(unsigned char) 128);
01190 packbits=(unsigned char *) RelinquishMagickMemory(packbits);
01191 return(MagickTrue);
01192 }
01193
01194 #if defined(MAGICKCORE_ZLIB_DELEGATE)
01195
01196
01197
01198
01199
01200
01201
01202
01203
01204
01205
01206
01207
01208
01209
01210
01211
01212
01213
01214
01215
01216
01217
01218
01219
01220
01221
01222
01223
01224
01225
01226 static voidpf AcquireZIPMemory(voidpf context,unsigned int items,
01227 unsigned int size)
01228 {
01229 (void) context;
01230 return((voidpf) AcquireQuantumMemory(items,size));
01231 }
01232
01233 static void RelinquishZIPMemory(voidpf context,voidpf memory)
01234 {
01235 (void) context;
01236 memory=RelinquishMagickMemory(memory);
01237 }
01238
01239 MagickExport MagickBooleanType ZLIBEncodeImage(Image *image,const size_t length,
01240 unsigned char *pixels)
01241 {
01242 int
01243 status;
01244
01245 register long
01246 i;
01247
01248 size_t
01249 compress_packets;
01250
01251 unsigned char
01252 *compress_pixels;
01253
01254 z_stream
01255 stream;
01256
01257 assert(image != (Image *) NULL);
01258 assert(image->signature == MagickSignature);
01259 if (image->debug != MagickFalse)
01260 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
01261 compress_packets=(size_t) (1.001*length+12);
01262 compress_pixels=(unsigned char *) AcquireQuantumMemory(compress_packets,
01263 sizeof(*compress_pixels));
01264 if (compress_pixels == (unsigned char *) NULL)
01265 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
01266 image->filename);
01267 stream.next_in=pixels;
01268 stream.avail_in=(unsigned int) length;
01269 stream.next_out=compress_pixels;
01270 stream.avail_out=(unsigned int) compress_packets;
01271 stream.zalloc=AcquireZIPMemory;
01272 stream.zfree=RelinquishZIPMemory;
01273 stream.opaque=(voidpf) NULL;
01274 status=deflateInit(&stream,(int) (image->quality ==
01275 UndefinedCompressionQuality ? 7 : MagickMin(image->quality/10,9)));
01276 if (status == Z_OK)
01277 {
01278 status=deflate(&stream,Z_FINISH);
01279 if (status == Z_STREAM_END)
01280 status=deflateEnd(&stream);
01281 else
01282 (void) deflateEnd(&stream);
01283 compress_packets=(size_t) stream.total_out;
01284 }
01285 if (status != Z_OK)
01286 ThrowBinaryException(CoderError,"UnableToZipCompressImage",image->filename)
01287 else
01288 for (i=0; i < (long) compress_packets; i++)
01289 (void) WriteBlobByte(image,compress_pixels[i]);
01290 compress_pixels=(unsigned char *) RelinquishMagickMemory(compress_pixels);
01291 return(status == Z_OK ? MagickTrue : MagickFalse);
01292 }
01293 #else
01294 MagickExport MagickBooleanType ZLIBEncodeImage(Image *image,
01295 const size_t magick_unused(length),unsigned char *magick_unused(pixels))
01296 {
01297 assert(image != (Image *) NULL);
01298 assert(image->signature == MagickSignature);
01299 if (image->debug != MagickFalse)
01300 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
01301 (void) ThrowMagickException(&image->exception,GetMagickModule(),
01302 MissingDelegateError,"DelegateLibrarySupportNotBuiltIn","`%s' (ZIP)",
01303 image->filename);
01304 return(MagickFalse);
01305 }
01306 #endif