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 x;
00447
00448 register PixelPacket
00449 *q;
00450
00451 register unsigned char
00452 *p;
00453
00454 ssize_t
00455 count;
00456
00457 unsigned char
00458 *scanline;
00459
00460 unsigned int
00461 bail,
00462 color;
00463
00464 unsigned long
00465 bit,
00466 code,
00467 mask,
00468 length,
00469 null_lines,
00470 runlength;
00471
00472
00473
00474
00475 assert(image != (Image *) NULL);
00476 assert(image->signature == MagickSignature);
00477 if (image->debug != MagickFalse)
00478 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
00479 mb_hash=(HuffmanTable **) AcquireQuantumMemory(HashSize,sizeof(*mb_hash));
00480 mw_hash=(HuffmanTable **) AcquireQuantumMemory(HashSize,sizeof(*mw_hash));
00481 scanline=(unsigned char *) AcquireQuantumMemory((size_t) image->columns,
00482 sizeof(*scanline));
00483 if ((mb_hash == (HuffmanTable **) NULL) ||
00484 (mw_hash == (HuffmanTable **) NULL) ||
00485 (scanline == (unsigned char *) NULL))
00486 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
00487 image->filename);
00488
00489
00490
00491 for (i=0; i < HashSize; i++)
00492 {
00493 mb_hash[i]=(HuffmanTable *) NULL;
00494 mw_hash[i]=(HuffmanTable *) NULL;
00495 }
00496 InitializeHashTable(mw_hash,TWTable,MWHashA,MWHashB);
00497 InitializeHashTable(mw_hash,MWTable,MWHashA,MWHashB);
00498 InitializeHashTable(mw_hash,EXTable,MWHashA,MWHashB);
00499 InitializeHashTable(mb_hash,TBTable,MBHashA,MBHashB);
00500 InitializeHashTable(mb_hash,MBTable,MBHashA,MBHashB);
00501 InitializeHashTable(mb_hash,EXTable,MBHashA,MBHashB);
00502
00503
00504
00505 byte=0;
00506 mask=0;
00507 null_lines=0;
00508 runlength=0;
00509 while (runlength < 11)
00510 InputBit(bit);
00511 do { InputBit(bit); } while ((int) bit == 0);
00512 image->x_resolution=204.0;
00513 image->y_resolution=196.0;
00514 image->units=PixelsPerInchResolution;
00515 exception=(&image->exception);
00516 for (y=0; ((y < (long) image->rows) && (null_lines < 3)); )
00517 {
00518
00519
00520
00521 p=scanline;
00522 for (x=0; x < (long) image->columns; x++)
00523 *p++=(unsigned char) 0;
00524
00525
00526
00527 color=MagickTrue;
00528 code=0;
00529 count=0;
00530 length=0;
00531 runlength=0;
00532 x=0;
00533 for ( ; ; )
00534 {
00535 if (byte == EOF)
00536 break;
00537 if (x >= (long) image->columns)
00538 {
00539 while (runlength < 11)
00540 InputBit(bit);
00541 do { InputBit(bit); } while ((int) bit == 0);
00542 break;
00543 }
00544 bail=MagickFalse;
00545 do
00546 {
00547 if (runlength < 11)
00548 InputBit(bit)
00549 else
00550 {
00551 InputBit(bit);
00552 if ((int) bit != 0)
00553 {
00554 null_lines++;
00555 if (x != 0)
00556 null_lines=0;
00557 bail=MagickTrue;
00558 break;
00559 }
00560 }
00561 code=(code << 1)+(unsigned long) bit;
00562 length++;
00563 } while (code == 0);
00564 if (bail != MagickFalse)
00565 break;
00566 if (length > 13)
00567 {
00568 while (runlength < 11)
00569 InputBit(bit);
00570 do { InputBit(bit); } while ((int) bit == 0);
00571 break;
00572 }
00573 if (color != MagickFalse)
00574 {
00575 if (length < 4)
00576 continue;
00577 entry=mw_hash[((length+MWHashA)*(code+MWHashB)) % HashSize];
00578 }
00579 else
00580 {
00581 if (length < 2)
00582 continue;
00583 entry=mb_hash[((length+MBHashA)*(code+MBHashB)) % HashSize];
00584 }
00585 if (entry == (const HuffmanTable *) NULL)
00586 continue;
00587 if ((entry->length != length) || (entry->code != code))
00588 continue;
00589 switch (entry->id)
00590 {
00591 case TWId:
00592 case TBId:
00593 {
00594 count+=entry->count;
00595 if ((x+count) > (long) image->columns)
00596 count=(ssize_t) image->columns-x;
00597 if (count > 0)
00598 {
00599 if (color != MagickFalse)
00600 {
00601 x+=count;
00602 count=0;
00603 }
00604 else
00605 for ( ; count > 0; count--)
00606 scanline[x++]=(unsigned char) 1;
00607 }
00608 color=(unsigned int)
00609 ((color == MagickFalse) ? MagickTrue : MagickFalse);
00610 break;
00611 }
00612 case MWId:
00613 case MBId:
00614 case EXId:
00615 {
00616 count+=entry->count;
00617 break;
00618 }
00619 default:
00620 break;
00621 }
00622 code=0;
00623 length=0;
00624 }
00625
00626
00627
00628 p=scanline;
00629 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
00630 if (q == (PixelPacket *) NULL)
00631 break;
00632 indexes=GetAuthenticIndexQueue(image);
00633 for (x=0; x < (long) image->columns; x++)
00634 {
00635 index=(IndexPacket) (*p++);
00636 indexes[x]=index;
00637 *q++=image->colormap[(long) index];
00638 }
00639 if (SyncAuthenticPixels(image,exception) == MagickFalse)
00640 break;
00641 proceed=SetImageProgress(image,LoadImageTag,y,image->rows);
00642 if (proceed == MagickFalse)
00643 break;
00644 y++;
00645 }
00646 image->rows=(unsigned long) MagickMax((size_t) y-3,1);
00647 image->compression=FaxCompression;
00648
00649
00650
00651 mw_hash=(HuffmanTable **) RelinquishMagickMemory(mw_hash);
00652 mb_hash=(HuffmanTable **) RelinquishMagickMemory(mb_hash);
00653 scanline=(unsigned char *) RelinquishMagickMemory(scanline);
00654 return(MagickTrue);
00655 }
00656
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 MagickExport MagickBooleanType HuffmanEncodeImage(const ImageInfo *image_info,
00683 Image *image)
00684 {
00685 #define HuffmanOutputCode(entry) \
00686 { \
00687 mask=1 << (entry->length-1); \
00688 while (mask != 0) \
00689 { \
00690 OutputBit(((entry->code & mask) != 0 ? 1 : 0)); \
00691 mask>>=1; \
00692 } \
00693 }
00694
00695 #define OutputBit(count) \
00696 { \
00697 if (count > 0) \
00698 byte=byte | bit; \
00699 bit>>=1; \
00700 if ((int) (bit & 0xff) == 0) \
00701 { \
00702 if (LocaleCompare(image_info->magick,"FAX") == 0) \
00703 (void) WriteBlobByte(image,(unsigned char) byte); \
00704 else \
00705 Ascii85Encode(image,byte); \
00706 byte='\0'; \
00707 bit=(unsigned char) 0x80; \
00708 } \
00709 }
00710
00711 const HuffmanTable
00712 *entry;
00713
00714 ExceptionInfo
00715 *exception;
00716
00717 int
00718 k,
00719 runlength;
00720
00721 long
00722 n,
00723 y;
00724
00725 Image
00726 *huffman_image;
00727
00728 MagickBooleanType
00729 proceed;
00730
00731 register long
00732 i,
00733 x;
00734
00735 register const PixelPacket
00736 *p;
00737
00738 register unsigned char
00739 *q;
00740
00741 unsigned char
00742 byte,
00743 bit,
00744 *scanline;
00745
00746 unsigned long
00747 mask,
00748 width;
00749
00750
00751
00752
00753 assert(image != (Image *) NULL);
00754 assert(image->signature == MagickSignature);
00755 if (image->debug != MagickFalse)
00756 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
00757 width=image->columns;
00758 if (LocaleCompare(image_info->magick,"FAX") == 0)
00759 width=(unsigned long) MagickMax(image->columns,1728);
00760 scanline=(unsigned char *) AcquireQuantumMemory((size_t) width+1UL,
00761 sizeof(*scanline));
00762 if (scanline == (unsigned char *) NULL)
00763 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
00764 image->filename);
00765 (void) ResetMagickMemory(scanline,0,width*sizeof(*scanline));
00766 huffman_image=CloneImage(image,0,0,MagickTrue,&image->exception);
00767 if (huffman_image == (Image *) NULL)
00768 {
00769 scanline=(unsigned char *) RelinquishMagickMemory(scanline);
00770 return(MagickFalse);
00771 }
00772 (void) SetImageType(huffman_image,BilevelType);
00773 byte='\0';
00774 bit=(unsigned char) 0x80;
00775 if (LocaleCompare(image_info->magick,"FAX") != 0)
00776 Ascii85Initialize(image);
00777 else
00778 {
00779
00780
00781
00782 for (k=0; k < 11; k++)
00783 OutputBit(0);
00784 OutputBit(1);
00785 }
00786
00787
00788
00789 exception=(&huffman_image->exception);
00790 q=scanline;
00791 for (y=0; y < (long) huffman_image->rows; y++)
00792 {
00793 p=GetVirtualPixels(huffman_image,0,y,huffman_image->columns,1,exception);
00794 if (p == (const PixelPacket *) NULL)
00795 break;
00796 for (x=0; x < (long) huffman_image->columns; x++)
00797 {
00798 *q++=(unsigned char) (PixelIntensity(p) >= ((MagickRealType)
00799 QuantumRange/2.0) ? 0 : 1);
00800 p++;
00801 }
00802
00803
00804
00805 q=scanline;
00806 for (n=(long) width; n > 0; )
00807 {
00808
00809
00810
00811 for (runlength=0; ((n > 0) && (*q == 0)); n--)
00812 {
00813 q++;
00814 runlength++;
00815 }
00816 if (runlength >= 64)
00817 {
00818 if (runlength < 1792)
00819 entry=MWTable+((runlength/64)-1);
00820 else
00821 entry=EXTable+(MagickMin((size_t) runlength,2560)-1792)/64;
00822 runlength-=entry->count;
00823 HuffmanOutputCode(entry);
00824 }
00825 entry=TWTable+MagickMin((size_t) runlength,63);
00826 HuffmanOutputCode(entry);
00827 if (n != 0)
00828 {
00829
00830
00831
00832 for (runlength=0; ((*q != 0) && (n > 0)); n--)
00833 {
00834 q++;
00835 runlength++;
00836 }
00837 if (runlength >= 64)
00838 {
00839 entry=MBTable+((runlength/64)-1);
00840 if (runlength >= 1792)
00841 entry=EXTable+(MagickMin((size_t) runlength,2560)-1792)/64;
00842 runlength-=entry->count;
00843 HuffmanOutputCode(entry);
00844 }
00845 entry=TBTable+MagickMin((size_t) runlength,63);
00846 HuffmanOutputCode(entry);
00847 }
00848 }
00849
00850
00851
00852 for (k=0; k < 11; k++)
00853 OutputBit(0);
00854 OutputBit(1);
00855 q=scanline;
00856 if (GetPreviousImageInList(huffman_image) == (Image *) NULL)
00857 {
00858 proceed=SetImageProgress(huffman_image,LoadImageTag,y,
00859 huffman_image->rows);
00860 if (proceed == MagickFalse)
00861 break;
00862 }
00863 }
00864
00865
00866
00867 for (i=0; i < 6; i++)
00868 {
00869 for (k=0; k < 11; k++)
00870 OutputBit(0);
00871 OutputBit(1);
00872 }
00873
00874
00875
00876 if (((int) bit != 0x80) != 0)
00877 {
00878 if (LocaleCompare(image_info->magick,"FAX") == 0)
00879 (void) WriteBlobByte(image,byte);
00880 else
00881 Ascii85Encode(image, byte);
00882 }
00883 if (LocaleCompare(image_info->magick,"FAX") != 0)
00884 Ascii85Flush(image);
00885 huffman_image=DestroyImage(huffman_image);
00886 scanline=(unsigned char *) RelinquishMagickMemory(scanline);
00887 return(MagickTrue);
00888 }
00889
00890 #if defined(MAGICKCORE_TIFF_DELEGATE)
00891
00892
00893
00894
00895
00896
00897
00898
00899
00900
00901
00902
00903
00904
00905
00906
00907
00908
00909
00910
00911
00912
00913
00914
00915
00916
00917 MagickExport MagickBooleanType Huffman2DEncodeImage(const ImageInfo *image_info,
00918 Image *image)
00919 {
00920 char
00921 filename[MaxTextExtent];
00922
00923 FILE
00924 *file;
00925
00926 Image
00927 *huffman_image;
00928
00929 ImageInfo
00930 *write_info;
00931
00932 int
00933 unique_file;
00934
00935 MagickBooleanType
00936 status;
00937
00938 register long
00939 i;
00940
00941 ssize_t
00942 count;
00943
00944 TIFF
00945 *tiff;
00946
00947 uint16
00948 fillorder;
00949
00950 uint32
00951 *byte_count,
00952 strip_size;
00953
00954 unsigned char
00955 *buffer;
00956
00957
00958
00959
00960 assert(image_info != (ImageInfo *) NULL);
00961 assert(image_info->signature == MagickSignature);
00962 if (image->debug != MagickFalse)
00963 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
00964 assert(image != (Image *) NULL);
00965 assert(image->signature == MagickSignature);
00966 huffman_image=CloneImage(image,0,0,MagickTrue,&image->exception);
00967 if (huffman_image == (Image *) NULL)
00968 return(MagickFalse);
00969 DestroyBlob(huffman_image);
00970 huffman_image->blob=CloneBlobInfo((BlobInfo *) NULL);
00971 (void) SetImageType(huffman_image,BilevelType);
00972 file=(FILE *) NULL;
00973 unique_file=AcquireUniqueFileResource(filename);
00974 if (unique_file != -1)
00975 file=fdopen(unique_file,"wb");
00976 if ((unique_file == -1) || (file == (FILE *) NULL))
00977 {
00978 (void) CopyMagickString(image->filename,filename,MaxTextExtent);
00979 ThrowFileException(&image->exception,FileOpenError,
00980 "UnableToCreateTemporaryFile",image->filename);
00981 return(MagickFalse);
00982 }
00983 (void) FormatMagickString(huffman_image->filename,MaxTextExtent,"tiff:%s",
00984 filename);
00985 write_info=CloneImageInfo(image_info);
00986 SetImageInfoFile(write_info,file);
00987 write_info->compression=Group4Compression;
00988 (void) SetImageOption(write_info,"quantum:polarity","min-is-white");
00989 status=WriteImage(write_info,huffman_image);
00990 write_info=DestroyImageInfo(write_info);
00991 if (status == MagickFalse)
00992 return(MagickFalse);
00993 tiff=TIFFOpen(filename,"rb");
00994 if (tiff == (TIFF *) NULL)
00995 {
00996 huffman_image=DestroyImage(huffman_image);
00997 (void) fclose(file);
00998 (void) RelinquishUniqueFileResource(filename);
00999 ThrowFileException(&image->exception,FileOpenError,"UnableToOpenFile",
01000 image_info->filename);
01001 return(MagickFalse);
01002 }
01003
01004
01005
01006 byte_count=0;
01007 (void) TIFFGetField(tiff,TIFFTAG_STRIPBYTECOUNTS,&byte_count);
01008 strip_size=byte_count[0];
01009 for (i=1; i < (long) TIFFNumberOfStrips(tiff); i++)
01010 if (byte_count[i] > strip_size)
01011 strip_size=byte_count[i];
01012 buffer=(unsigned char *) AcquireQuantumMemory((size_t) strip_size,
01013 sizeof(*buffer));
01014 if (buffer == (unsigned char *) NULL)
01015 {
01016 TIFFClose(tiff);
01017 huffman_image=DestroyImage(huffman_image);
01018 (void) fclose(file);
01019 (void) RelinquishUniqueFileResource(filename);
01020 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
01021 image_info->filename);
01022 }
01023
01024
01025
01026 fillorder=FILLORDER_LSB2MSB;
01027 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_FILLORDER,&fillorder);
01028 for (i=0; i < (long) TIFFNumberOfStrips(tiff); i++)
01029 {
01030 count=(ssize_t) TIFFReadRawStrip(tiff,(uint32) i,buffer,(long)
01031 byte_count[i]);
01032 if (fillorder == FILLORDER_LSB2MSB)
01033 TIFFReverseBits(buffer,(unsigned long) count);
01034 (void) WriteBlob(image,count,buffer);
01035 }
01036 buffer=(unsigned char *) RelinquishMagickMemory(buffer);
01037 TIFFClose(tiff);
01038 huffman_image=DestroyImage(huffman_image);
01039 (void) fclose(file);