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 #if defined(MAGICKCORE_TIFF_DELEGATE)
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 Huffman2DEncodeImage(const ImageInfo *image_info,
00928 Image *image,Image *inject_image)
00929 {
00930 char
00931 filename[MaxTextExtent];
00932
00933 FILE
00934 *file;
00935
00936 Image
00937 *huffman_image;
00938
00939 ImageInfo
00940 *write_info;
00941
00942 int
00943 unique_file;
00944
00945 MagickBooleanType
00946 status;
00947
00948 register long
00949 i;
00950
00951 ssize_t
00952 count;
00953
00954 TIFF
00955 *tiff;
00956
00957 uint16
00958 fillorder;
00959
00960 uint32
00961 *byte_count,
00962 strip_size;
00963
00964 unsigned char
00965 *buffer;
00966
00967
00968
00969
00970 assert(image_info != (const ImageInfo *) NULL);
00971 assert(image_info->signature == MagickSignature);
00972 assert(image != (Image *) NULL);
00973 assert(image->signature == MagickSignature);
00974 if (image->debug != MagickFalse)
00975 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
00976 assert(inject_image != (Image *) NULL);
00977 assert(inject_image->signature == MagickSignature);
00978 huffman_image=CloneImage(inject_image,0,0,MagickTrue,&image->exception);
00979 if (huffman_image == (Image *) NULL)
00980 return(MagickFalse);
00981 file=(FILE *) NULL;
00982 unique_file=AcquireUniqueFileResource(filename);
00983 if (unique_file != -1)
00984 file=fdopen(unique_file,"wb");
00985 if ((unique_file == -1) || (file == (FILE *) NULL))
00986 {
00987 ThrowFileException(&image->exception,FileOpenError,
00988 "UnableToCreateTemporaryFile",filename);
00989 return(MagickFalse);
00990 }
00991 (void) FormatMagickString(huffman_image->filename,MaxTextExtent,"tiff:%s",
00992 filename);
00993 write_info=CloneImageInfo(image_info);
00994 SetImageInfoFile(write_info,file);
00995 write_info->compression=Group4Compression;
00996 (void) SetImageOption(write_info,"quantum:polarity","min-is-white");
00997 status=WriteImage(write_info,huffman_image);
00998 (void) fflush(file);
00999 write_info=DestroyImageInfo(write_info);
01000 if (status == MagickFalse)
01001 return(MagickFalse);
01002 tiff=TIFFOpen(filename,"rb");
01003 if (tiff == (TIFF *) NULL)
01004 {
01005 huffman_image=DestroyImage(huffman_image);
01006 (void) fclose(file);
01007 (void) RelinquishUniqueFileResource(filename);
01008 ThrowFileException(&image->exception,FileOpenError,"UnableToOpenFile",
01009 image_info->filename);
01010 return(MagickFalse);
01011 }
01012
01013
01014
01015 byte_count=0;
01016 (void) TIFFGetField(tiff,TIFFTAG_STRIPBYTECOUNTS,&byte_count);
01017 strip_size=byte_count[0];
01018 for (i=1; i < (long) TIFFNumberOfStrips(tiff); i++)
01019 if (byte_count[i] > strip_size)
01020 strip_size=byte_count[i];
01021 buffer=(unsigned char *) AcquireQuantumMemory((size_t) strip_size,
01022 sizeof(*buffer));
01023 if (buffer == (unsigned char *) NULL)
01024 {
01025 TIFFClose(tiff);
01026 huffman_image=DestroyImage(huffman_image);
01027 (void) fclose(file);
01028 (void) RelinquishUniqueFileResource(filename);
01029 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
01030 image_info->filename);
01031 }
01032
01033
01034
01035 fillorder=FILLORDER_LSB2MSB;
01036 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_FILLORDER,&fillorder);
01037 for (i=0; i < (long) TIFFNumberOfStrips(tiff); i++)
01038 {
01039 count=(ssize_t) TIFFReadRawStrip(tiff,(uint32) i,buffer,(long)
01040 byte_count[i]);
01041 if (fillorder == FILLORDER_LSB2MSB)
01042 TIFFReverseBits(buffer,(unsigned long) count);
01043 (void) WriteBlob(image,(size_t) count,buffer);
01044 }
01045 buffer=(unsigned char *) RelinquishMagickMemory(buffer);
01046 TIFFClose(tiff);
01047 huffman_image=DestroyImage(huffman_image);
01048 (void) fclose(file);
01049 (void) RelinquishUniqueFileResource(filename);
01050 return(MagickTrue);
01051 }
01052 #else
01053 MagickExport MagickBooleanType Huffman2DEncodeImage(const ImageInfo *image_info,
01054 Image *image,Image *inject_image)
01055 {
01056 assert(image_info != (const ImageInfo *) NULL);
01057 assert(image_info->signature == MagickSignature);
01058 assert(image != (Image *) NULL);
01059 assert(image->signature == MagickSignature);
01060 (void) inject_image;
01061 if (image->debug != MagickFalse)
01062 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
01063 (void) ThrowMagickException(&image->exception,GetMagickModule(),
01064 MissingDelegateError,"DelegateLibrarySupportNotBuiltIn","`%s' (TIFF)",
01065 image->filename);
01066 return(MagickFalse);
01067 }
01068 #endif
01069
01070
01071
01072
01073
01074
01075
01076
01077
01078
01079
01080
01081
01082
01083
01084
01085
01086
01087
01088
01089
01090
01091
01092
01093
01094
01095
01096
01097
01098
01099 MagickExport MagickBooleanType LZWEncodeImage(Image *image,const size_t length,
01100 unsigned char *pixels)
01101 {
01102 #define LZWClr 256UL
01103 #define LZWEod 257UL
01104 #define OutputCode(code) \
01105 { \
01106 accumulator+=code << (32-code_width-number_bits); \
01107 number_bits+=code_width; \
01108 while (number_bits >= 8) \
01109 { \
01110 (void) WriteBlobByte(image,(unsigned char) (accumulator >> 24)); \
01111 accumulator=accumulator << 8; \
01112 number_bits-=8; \
01113 } \
01114 }
01115
01116 typedef struct _TableType
01117 {
01118 long
01119 prefix,
01120 suffix,
01121 next;
01122 } TableType;
01123
01124 long
01125 index;
01126
01127 register long
01128 i;
01129
01130 TableType
01131 *table;
01132
01133 unsigned long
01134 accumulator,
01135 number_bits,
01136 code_width,
01137 last_code,
01138 next_index;
01139
01140
01141
01142
01143 assert(image != (Image *) NULL);
01144 assert(image->signature == MagickSignature);
01145 if (image->debug != MagickFalse)
01146 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
01147 assert(pixels != (unsigned char *) NULL);
01148 table=(TableType *) AcquireQuantumMemory(1UL << 12,sizeof(*table));
01149 if (table == (TableType *) NULL)
01150 return(MagickFalse);
01151
01152
01153
01154 accumulator=0;
01155 code_width=9;
01156 number_bits=0;
01157 last_code=0;
01158 OutputCode(LZWClr);
01159 for (index=0; index < 256; index++)
01160 {
01161 table[index].prefix=(-1);
01162 table[index].suffix=(short) index;
01163 table[index].next=(-1);
01164 }
01165 next_index=LZWEod+1;
01166 code_width=9;
01167 last_code=(unsigned long) pixels[0];
01168 for (i=1; i < (long) length; i++)
01169 {
01170
01171
01172
01173 index=(long) last_code;
01174 while (index != -1)
01175 if ((table[index].prefix != (long) last_code) ||
01176 (table[index].suffix != (long) pixels[i]))
01177 index=table[index].next;
01178 else
01179 {
01180 last_code=(unsigned long) index;
01181 break;
01182 }
01183 if (last_code != (unsigned long) index)
01184 {
01185
01186
01187
01188 OutputCode(last_code);
01189 table[next_index].prefix=(long) last_code;
01190 table[next_index].suffix=(short) pixels[i];
01191 table[next_index].next=table[last_code].next;
01192 table[last_code].next=(long) next_index;
01193 next_index++;
01194
01195
01196
01197 if ((next_index >> code_width) != 0)
01198 {
01199 code_width++;
01200 if (code_width > 12)
01201 {
01202
01203
01204
01205 code_width--;
01206 OutputCode(LZWClr);
01207 for (index=0; index < 256; index++)
01208 {
01209 table[index].prefix=(-1);
01210 table[index].suffix=index;
01211 table[index].next=(-1);
01212 }
01213 next_index=LZWEod+1;
01214 code_width=9;
01215 }
01216 }
01217 last_code=(unsigned long) pixels[i];
01218 }
01219 }
01220
01221
01222
01223 OutputCode(last_code);
01224 OutputCode(LZWEod);
01225 if (number_bits != 0)
01226 (void) WriteBlobByte(image,(unsigned char) (accumulator >> 24));
01227 table=(TableType *) RelinquishMagickMemory(table);
01228 return(MagickTrue);
01229 }
01230
01231
01232
01233
01234
01235
01236
01237
01238
01239
01240
01241
01242
01243
01244
01245
01246
01247
01248
01249
01250
01251
01252
01253
01254
01255
01256
01257
01258
01259
01260
01261 MagickExport MagickBooleanType PackbitsEncodeImage(Image *image,
01262 const size_t length,unsigned char *pixels)
01263 {
01264 int
01265 count;
01266
01267 register long
01268 i,
01269 j;
01270
01271 unsigned char
01272 *packbits;
01273
01274
01275
01276
01277 assert(image != (Image *) NULL);
01278 assert(image->signature == MagickSignature);
01279 if (image->debug != MagickFalse)
01280 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
01281 assert(pixels != (unsigned char *) NULL);
01282 packbits=(unsigned char *) AcquireQuantumMemory(128UL,sizeof(*packbits));
01283 if (packbits == (unsigned char *) NULL)
01284 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
01285 image->filename);
01286 for (i=(long) length; i != 0; )
01287 {
01288 switch (i)
01289 {
01290 case 1:
01291 {
01292 i--;
01293 (void) WriteBlobByte(image,(unsigned char) 0);
01294 (void) WriteBlobByte(image,*pixels);
01295 break;
01296 }
01297 case 2:
01298 {
01299 i-=2;
01300 (void) WriteBlobByte(image,(unsigned char) 1);
01301 (void) WriteBlobByte(image,*pixels);
01302 (void) WriteBlobByte(image,pixels[1]);
01303 break;
01304 }
01305 case 3:
01306 {
01307 i-=3;
01308 if ((*pixels == *(pixels+1)) && (*(pixels+1) == *(pixels+2)))
01309 {
01310 (void) WriteBlobByte(image,(unsigned char) ((256-3)+1));
01311 (void) WriteBlobByte(image,*pixels);
01312 break;
01313 }
01314 (void) WriteBlobByte(image,(unsigned char) 2);
01315 (void) WriteBlobByte(image,*pixels);
01316 (void) WriteBlobByte(image,pixels[1]);
01317 (void) WriteBlobByte(image,pixels[2]);
01318 break;
01319 }
01320 default:
01321 {
01322 if ((*pixels == *(pixels+1)) && (*(pixels+1) == *(pixels+2)))
01323 {
01324
01325
01326
01327 count=3;
01328 while (((long) count < i) && (*pixels == *(pixels+count)))
01329 {
01330 count++;
01331 if (count >= 127)
01332 break;
01333 }
01334 i-=count;
01335 (void) WriteBlobByte(image,(unsigned char) ((256-count)+1));
01336 (void) WriteBlobByte(image,*pixels);
01337 pixels+=count;
01338 break;
01339 }
01340
01341
01342
01343 count=0;
01344 while ((*(pixels+count) != *(pixels+count+1)) ||
01345 (*(pixels+count+1) != *(pixels+count+2)))
01346 {
01347 packbits[count+1]=pixels[count];
01348 count++;
01349 if (((long) count >= (i-3)) || (count >= 127))
01350 break;
01351 }
01352 i-=count;
01353 *packbits=(unsigned char) (count-1);
01354 for (j=0; j <= (long) count; j++)
01355 (void) WriteBlobByte(image,packbits[j]);
01356 pixels+=count;
01357 break;
01358 }
01359 }
01360 }
01361 (void) WriteBlobByte(image,(unsigned char) 128);
01362 packbits=(unsigned char *) RelinquishMagickMemory(packbits);
01363 return(MagickTrue);
01364 }
01365
01366 #if defined(MAGICKCORE_ZLIB_DELEGATE)
01367
01368
01369
01370
01371
01372
01373
01374
01375
01376
01377
01378
01379
01380
01381
01382
01383
01384
01385
01386
01387
01388
01389
01390
01391
01392
01393
01394
01395
01396
01397
01398 static voidpf AcquireZIPMemory(voidpf context,unsigned int items,
01399 unsigned int size)
01400 {
01401 (void) context;
01402 return((voidpf) AcquireQuantumMemory(items,size));
01403 }
01404
01405 static void RelinquishZIPMemory(voidpf context,voidpf memory)
01406 {
01407 (void) context;
01408 memory=RelinquishMagickMemory(memory);
01409 }
01410
01411 MagickExport MagickBooleanType ZLIBEncodeImage(Image *image,const size_t length,
01412 unsigned char *pixels)
01413 {
01414 int
01415 status;
01416
01417 register long
01418 i;
01419
01420 size_t
01421 compress_packets;
01422
01423 unsigned char
01424 *compress_pixels;
01425
01426 z_stream
01427 stream;
01428
01429 assert(image != (Image *) NULL);
01430 assert(image->signature == MagickSignature);
01431 if (image->debug != MagickFalse)
01432 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
01433 compress_packets=(size_t) (1.001*length+12);
01434 compress_pixels=(unsigned char *) AcquireQuantumMemory(compress_packets,
01435 sizeof(*compress_pixels));
01436 if (compress_pixels == (unsigned char *) NULL)
01437 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
01438 image->filename);
01439 stream.next_in=pixels;
01440 stream.avail_in=(unsigned int) length;
01441 stream.next_out=compress_pixels;
01442 stream.avail_out=(unsigned int) compress_packets;
01443 stream.zalloc=AcquireZIPMemory;
01444 stream.zfree=RelinquishZIPMemory;
01445 stream.opaque=(voidpf) NULL;
01446 status=deflateInit(&stream,(int) (image->quality ==
01447 UndefinedCompressionQuality ? 7 : MagickMin(image->quality/10,9)));
01448 if (status == Z_OK)
01449 {
01450 status=deflate(&stream,Z_FINISH);
01451 if (status == Z_STREAM_END)
01452 status=deflateEnd(&stream);
01453 else
01454 (void) deflateEnd(&stream);
01455 compress_packets=(size_t) stream.total_out;
01456 }
01457 if (status != Z_OK)
01458 ThrowBinaryException(CoderError,"UnableToZipCompressImage",image->filename)
01459 else
01460 for (i=0; i < (long) compress_packets; i++)
01461 (void) WriteBlobByte(image,compress_pixels[i]);
01462 compress_pixels=(unsigned char *) RelinquishMagickMemory(compress_pixels);
01463 return(status == Z_OK ? MagickTrue : MagickFalse);
01464 }
01465 #else
01466 MagickExport MagickBooleanType ZLIBEncodeImage(Image *image,
01467 const size_t magick_unused(length),unsigned char *magick_unused(pixels))
01468 {
01469 assert(image != (Image *) NULL);
01470 assert(image->signature == MagickSignature);
01471 if (image->debug != MagickFalse)
01472 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
01473 (void) ThrowMagickException(&image->exception,GetMagickModule(),
01474 MissingDelegateError,"DelegateLibrarySupportNotBuiltIn","`%s' (ZIP)",
01475 image->filename);
01476 return(MagickFalse);
01477 }
01478 #endif