| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | |
|---|
| 21 | |
|---|
| 22 | |
|---|
| 23 | |
|---|
| 24 | |
|---|
| 25 | |
|---|
| 26 | |
|---|
| 27 | |
|---|
| 28 | |
|---|
| 29 | |
|---|
| 30 | |
|---|
| 31 | |
|---|
| 32 | |
|---|
| 33 | |
|---|
| 34 | |
|---|
| 35 | |
|---|
| 36 | |
|---|
| 37 | |
|---|
| 38 | |
|---|
| 39 | |
|---|
| 40 | |
|---|
| 41 | |
|---|
| 42 | |
|---|
| 43 | #include "magick/studio.h" |
|---|
| 44 | #include "magick/blob.h" |
|---|
| 45 | #include "magick/blob-private.h" |
|---|
| 46 | #include "magick/color-private.h" |
|---|
| 47 | #include "magick/compress.h" |
|---|
| 48 | #include "magick/constitute.h" |
|---|
| 49 | #include "magick/exception.h" |
|---|
| 50 | #include "magick/exception-private.h" |
|---|
| 51 | #include "magick/image-private.h" |
|---|
| 52 | #include "magick/list.h" |
|---|
| 53 | #include "magick/memory_.h" |
|---|
| 54 | #include "magick/monitor.h" |
|---|
| 55 | #include "magick/option.h" |
|---|
| 56 | #include "magick/resource_.h" |
|---|
| 57 | #include "magick/string_.h" |
|---|
| 58 | #if defined(HasTIFF) |
|---|
| 59 | #if defined(HAVE_TIFFCONF_H) |
|---|
| 60 | #include "tiffconf.h" |
|---|
| 61 | #endif |
|---|
| 62 | #include "tiffio.h" |
|---|
| 63 | #define CCITTParam "-1" |
|---|
| 64 | #else |
|---|
| 65 | #define CCITTParam "0" |
|---|
| 66 | #endif |
|---|
| 67 | #if defined(HasZLIB) |
|---|
| 68 | #include "zlib.h" |
|---|
| 69 | #endif |
|---|
| 70 | |
|---|
| 71 | |
|---|
| 72 | |
|---|
| 73 | |
|---|
| 74 | struct _Ascii85Info |
|---|
| 75 | { |
|---|
| 76 | long |
|---|
| 77 | offset, |
|---|
| 78 | line_break; |
|---|
| 79 | |
|---|
| 80 | unsigned char |
|---|
| 81 | buffer[10]; |
|---|
| 82 | }; |
|---|
| 83 | |
|---|
| 84 | typedef struct HuffmanTable |
|---|
| 85 | { |
|---|
| 86 | unsigned long |
|---|
| 87 | id, |
|---|
| 88 | code, |
|---|
| 89 | length, |
|---|
| 90 | count; |
|---|
| 91 | } HuffmanTable; |
|---|
| 92 | |
|---|
| 93 | |
|---|
| 94 | |
|---|
| 95 | |
|---|
| 96 | #define TWId 23 |
|---|
| 97 | #define MWId 24 |
|---|
| 98 | #define TBId 25 |
|---|
| 99 | #define MBId 26 |
|---|
| 100 | #define EXId 27 |
|---|
| 101 | |
|---|
| 102 | static const HuffmanTable |
|---|
| 103 | MBTable[]= |
|---|
| 104 | { |
|---|
| 105 | { MBId, 0x0f, 10, 64 }, { MBId, 0xc8, 12, 128 }, |
|---|
| 106 | { MBId, 0xc9, 12, 192 }, { MBId, 0x5b, 12, 256 }, |
|---|
| 107 | { MBId, 0x33, 12, 320 }, { MBId, 0x34, 12, 384 }, |
|---|
| 108 | { MBId, 0x35, 12, 448 }, { MBId, 0x6c, 13, 512 }, |
|---|
| 109 | { MBId, 0x6d, 13, 576 }, { MBId, 0x4a, 13, 640 }, |
|---|
| 110 | { MBId, 0x4b, 13, 704 }, { MBId, 0x4c, 13, 768 }, |
|---|
| 111 | { MBId, 0x4d, 13, 832 }, { MBId, 0x72, 13, 896 }, |
|---|
| 112 | { MBId, 0x73, 13, 960 }, { MBId, 0x74, 13, 1024 }, |
|---|
| 113 | { MBId, 0x75, 13, 1088 }, { MBId, 0x76, 13, 1152 }, |
|---|
| 114 | { MBId, 0x77, 13, 1216 }, { MBId, 0x52, 13, 1280 }, |
|---|
| 115 | { MBId, 0x53, 13, 1344 }, { MBId, 0x54, 13, 1408 }, |
|---|
| 116 | { MBId, 0x55, 13, 1472 }, { MBId, 0x5a, 13, 1536 }, |
|---|
| 117 | { MBId, 0x5b, 13, 1600 }, { MBId, 0x64, 13, 1664 }, |
|---|
| 118 | { MBId, 0x65, 13, 1728 }, { MBId, 0x00, 0, 0 } |
|---|
| 119 | }; |
|---|
| 120 | |
|---|
| 121 | static const HuffmanTable |
|---|
| 122 | EXTable[]= |
|---|
| 123 | { |
|---|
| 124 | { EXId, 0x08, 11, 1792 }, { EXId, 0x0c, 11, 1856 }, |
|---|
| 125 | { EXId, 0x0d, 11, 1920 }, { EXId, 0x12, 12, 1984 }, |
|---|
| 126 | { EXId, 0x13, 12, 2048 }, { EXId, 0x14, 12, 2112 }, |
|---|
| 127 | { EXId, 0x15, 12, 2176 }, { EXId, 0x16, 12, 2240 }, |
|---|
| 128 | { EXId, 0x17, 12, 2304 }, { EXId, 0x1c, 12, 2368 }, |
|---|
| 129 | { EXId, 0x1d, 12, 2432 }, { EXId, 0x1e, 12, 2496 }, |
|---|
| 130 | { EXId, 0x1f, 12, 2560 }, { EXId, 0x00, 0, 0 } |
|---|
| 131 | }; |
|---|
| 132 | |
|---|
| 133 | static const HuffmanTable |
|---|
| 134 | MWTable[]= |
|---|
| 135 | { |
|---|
| 136 | { MWId, 0x1b, 5, 64 }, { MWId, 0x12, 5, 128 }, |
|---|
| 137 | { MWId, 0x17, 6, 192 }, { MWId, 0x37, 7, 256 }, |
|---|
| 138 | { MWId, 0x36, 8, 320 }, { MWId, 0x37, 8, 384 }, |
|---|
| 139 | { MWId, 0x64, 8, 448 }, { MWId, 0x65, 8, 512 }, |
|---|
| 140 | { MWId, 0x68, 8, 576 }, { MWId, 0x67, 8, 640 }, |
|---|
| 141 | { MWId, 0xcc, 9, 704 }, { MWId, 0xcd, 9, 768 }, |
|---|
| 142 | { MWId, 0xd2, 9, 832 }, { MWId, 0xd3, 9, 896 }, |
|---|
| 143 | { MWId, 0xd4, 9, 960 }, { MWId, 0xd5, 9, 1024 }, |
|---|
| 144 | { MWId, 0xd6, 9, 1088 }, { MWId, 0xd7, 9, 1152 }, |
|---|
| 145 | { MWId, 0xd8, 9, 1216 }, { MWId, 0xd9, 9, 1280 }, |
|---|
| 146 | { MWId, 0xda, 9, 1344 }, { MWId, 0xdb, 9, 1408 }, |
|---|
| 147 | { MWId, 0x98, 9, 1472 }, { MWId, 0x99, 9, 1536 }, |
|---|
| 148 | { MWId, 0x9a, 9, 1600 }, { MWId, 0x18, 6, 1664 }, |
|---|
| 149 | { MWId, 0x9b, 9, 1728 }, { MWId, 0x00, 0, 0 } |
|---|
| 150 | }; |
|---|
| 151 | |
|---|
| 152 | static const HuffmanTable |
|---|
| 153 | TBTable[]= |
|---|
| 154 | { |
|---|
| 155 | { TBId, 0x37, 10, 0 }, { TBId, 0x02, 3, 1 }, { TBId, 0x03, 2, 2 }, |
|---|
| 156 | { TBId, 0x02, 2, 3 }, { TBId, 0x03, 3, 4 }, { TBId, 0x03, 4, 5 }, |
|---|
| 157 | { TBId, 0x02, 4, 6 }, { TBId, 0x03, 5, 7 }, { TBId, 0x05, 6, 8 }, |
|---|
| 158 | { TBId, 0x04, 6, 9 }, { TBId, 0x04, 7, 10 }, { TBId, 0x05, 7, 11 }, |
|---|
| 159 | { TBId, 0x07, 7, 12 }, { TBId, 0x04, 8, 13 }, { TBId, 0x07, 8, 14 }, |
|---|
| 160 | { TBId, 0x18, 9, 15 }, { TBId, 0x17, 10, 16 }, { TBId, 0x18, 10, 17 }, |
|---|
| 161 | { TBId, 0x08, 10, 18 }, { TBId, 0x67, 11, 19 }, { TBId, 0x68, 11, 20 }, |
|---|
| 162 | { TBId, 0x6c, 11, 21 }, { TBId, 0x37, 11, 22 }, { TBId, 0x28, 11, 23 }, |
|---|
| 163 | { TBId, 0x17, 11, 24 }, { TBId, 0x18, 11, 25 }, { TBId, 0xca, 12, 26 }, |
|---|
| 164 | { TBId, 0xcb, 12, 27 }, { TBId, 0xcc, 12, 28 }, { TBId, 0xcd, 12, 29 }, |
|---|
| 165 | { TBId, 0x68, 12, 30 }, { TBId, 0x69, 12, 31 }, { TBId, 0x6a, 12, 32 }, |
|---|
| 166 | { TBId, 0x6b, 12, 33 }, { TBId, 0xd2, 12, 34 }, { TBId, 0xd3, 12, 35 }, |
|---|
| 167 | { TBId, 0xd4, 12, 36 }, { TBId, 0xd5, 12, 37 }, { TBId, 0xd6, 12, 38 }, |
|---|
| 168 | { TBId, 0xd7, 12, 39 }, { TBId, 0x6c, 12, 40 }, { TBId, 0x6d, 12, 41 }, |
|---|
| 169 | { TBId, 0xda, 12, 42 }, { TBId, 0xdb, 12, 43 }, { TBId, 0x54, 12, 44 }, |
|---|
| 170 | { TBId, 0x55, 12, 45 }, { TBId, 0x56, 12, 46 }, { TBId, 0x57, 12, 47 }, |
|---|
| 171 | { TBId, 0x64, 12, 48 }, { TBId, 0x65, 12, 49 }, { TBId, 0x52, 12, 50 }, |
|---|
| 172 | { TBId, 0x53, 12, 51 }, { TBId, 0x24, 12, 52 }, { TBId, 0x37, 12, 53 }, |
|---|
| 173 | { TBId, 0x38, 12, 54 }, { TBId, 0x27, 12, 55 }, { TBId, 0x28, 12, 56 }, |
|---|
| 174 | { TBId, 0x58, 12, 57 }, { TBId, 0x59, 12, 58 }, { TBId, 0x2b, 12, 59 }, |
|---|
| 175 | { TBId, 0x2c, 12, 60 }, { TBId, 0x5a, 12, 61 }, { TBId, 0x66, 12, 62 }, |
|---|
| 176 | { TBId, 0x67, 12, 63 }, { TBId, 0x00, 0, 0 } |
|---|
| 177 | }; |
|---|
| 178 | |
|---|
| 179 | static const HuffmanTable |
|---|
| 180 | TWTable[]= |
|---|
| 181 | { |
|---|
| 182 | { TWId, 0x35, 8, 0 }, { TWId, 0x07, 6, 1 }, { TWId, 0x07, 4, 2 }, |
|---|
| 183 | { TWId, 0x08, 4, 3 }, { TWId, 0x0b, 4, 4 }, { TWId, 0x0c, 4, 5 }, |
|---|
| 184 | { TWId, 0x0e, 4, 6 }, { TWId, 0x0f, 4, 7 }, { TWId, 0x13, 5, 8 }, |
|---|
| 185 | { TWId, 0x14, 5, 9 }, { TWId, 0x07, 5, 10 }, { TWId, 0x08, 5, 11 }, |
|---|
| 186 | { TWId, 0x08, 6, 12 }, { TWId, 0x03, 6, 13 }, { TWId, 0x34, 6, 14 }, |
|---|
| 187 | { TWId, 0x35, 6, 15 }, { TWId, 0x2a, 6, 16 }, { TWId, 0x2b, 6, 17 }, |
|---|
| 188 | { TWId, 0x27, 7, 18 }, { TWId, 0x0c, 7, 19 }, { TWId, 0x08, 7, 20 }, |
|---|
| 189 | { TWId, 0x17, 7, 21 }, { TWId, 0x03, 7, 22 }, { TWId, 0x04, 7, 23 }, |
|---|
| 190 | { TWId, 0x28, 7, 24 }, { TWId, 0x2b, 7, 25 }, { TWId, 0x13, 7, 26 }, |
|---|
| 191 | { TWId, 0x24, 7, 27 }, { TWId, 0x18, 7, 28 }, { TWId, 0x02, 8, 29 }, |
|---|
| 192 | { TWId, 0x03, 8, 30 }, { TWId, 0x1a, 8, 31 }, { TWId, 0x1b, 8, 32 }, |
|---|
| 193 | { TWId, 0x12, 8, 33 }, { TWId, 0x13, 8, 34 }, { TWId, 0x14, 8, 35 }, |
|---|
| 194 | { TWId, 0x15, 8, 36 }, { TWId, 0x16, 8, 37 }, { TWId, 0x17, 8, 38 }, |
|---|
| 195 | { TWId, 0x28, 8, 39 }, { TWId, 0x29, 8, 40 }, { TWId, 0x2a, 8, 41 }, |
|---|
| 196 | { TWId, 0x2b, 8, 42 }, { TWId, 0x2c, 8, 43 }, { TWId, 0x2d, 8, 44 }, |
|---|
| 197 | { TWId, 0x04, 8, 45 }, { TWId, 0x05, 8, 46 }, { TWId, 0x0a, 8, 47 }, |
|---|
| 198 | { TWId, 0x0b, 8, 48 }, { TWId, 0x52, 8, 49 }, { TWId, 0x53, 8, 50 }, |
|---|
| 199 | { TWId, 0x54, 8, 51 }, { TWId, 0x55, 8, 52 }, { TWId, 0x24, 8, 53 }, |
|---|
| 200 | { TWId, 0x25, 8, 54 }, { TWId, 0x58, 8, 55 }, { TWId, 0x59, 8, 56 }, |
|---|
| 201 | { TWId, 0x5a, 8, 57 }, { TWId, 0x5b, 8, 58 }, { TWId, 0x4a, 8, 59 }, |
|---|
| 202 | { TWId, 0x4b, 8, 60 }, { TWId, 0x32, 8, 61 }, { TWId, 0x33, 8, 62 }, |
|---|
| 203 | { TWId, 0x34, 8, 63 }, { TWId, 0x00, 0, 0 } |
|---|
| 204 | }; |
|---|
| 205 | |
|---|
| 206 | |
|---|
| 207 | |
|---|
| 208 | |
|---|
| 209 | |
|---|
| 210 | |
|---|
| 211 | |
|---|
| 212 | |
|---|
| 213 | |
|---|
| 214 | |
|---|
| 215 | |
|---|
| 216 | |
|---|
| 217 | |
|---|
| 218 | |
|---|
| 219 | |
|---|
| 220 | |
|---|
| 221 | |
|---|
| 222 | |
|---|
| 223 | |
|---|
| 224 | |
|---|
| 225 | |
|---|
| 226 | |
|---|
| 227 | |
|---|
| 228 | |
|---|
| 229 | |
|---|
| 230 | |
|---|
| 231 | |
|---|
| 232 | |
|---|
| 233 | #define MaxLineExtent 36 |
|---|
| 234 | |
|---|
| 235 | static char *Ascii85Tuple(unsigned char *data) |
|---|
| 236 | { |
|---|
| 237 | static char |
|---|
| 238 | tuple[6]; |
|---|
| 239 | |
|---|
| 240 | register long |
|---|
| 241 | i, |
|---|
| 242 | x; |
|---|
| 243 | |
|---|
| 244 | unsigned long |
|---|
| 245 | code, |
|---|
| 246 | quantum; |
|---|
| 247 | |
|---|
| 248 | code=((((unsigned long) data[0] << 8) | (unsigned long) data[1]) << 16) | |
|---|
| 249 | ((unsigned long) data[2] << 8) | (unsigned long) data[3]; |
|---|
| 250 | if (code == 0L) |
|---|
| 251 | { |
|---|
| 252 | tuple[0]='z'; |
|---|
| 253 | tuple[1]='\0'; |
|---|
| 254 | return(tuple); |
|---|
| 255 | } |
|---|
| 256 | quantum=85UL*85UL*85UL*85UL; |
|---|
| 257 | for (i=0; i < 4; i++) |
|---|
| 258 | { |
|---|
| 259 | x=(long) (code/quantum); |
|---|
| 260 | code-=quantum*x; |
|---|
| 261 | tuple[i]=(char) (x+(int) '!'); |
|---|
| 262 | quantum/=85L; |
|---|
| 263 | } |
|---|
| 264 | tuple[4]=(char) ((code % 85L)+(int) '!'); |
|---|
| 265 | tuple[5]='\0'; |
|---|
| 266 | return(tuple); |
|---|
| 267 | } |
|---|
| 268 | |
|---|
| 269 | MagickExport void Ascii85Initialize(Image *image) |
|---|
| 270 | { |
|---|
| 271 | |
|---|
| 272 | |
|---|
| 273 | |
|---|
| 274 | if (image->ascii85 == (Ascii85Info *) NULL) |
|---|
| 275 | image->ascii85=(Ascii85Info *) AcquireMagickMemory(sizeof(*image->ascii85)); |
|---|
| 276 | if (image->ascii85 == (Ascii85Info *) NULL) |
|---|
| 277 | ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed"); |
|---|
| 278 | (void) ResetMagickMemory(image->ascii85,0,sizeof(*image->ascii85)); |
|---|
| 279 | image->ascii85->line_break=MaxLineExtent << 1; |
|---|
| 280 | image->ascii85->offset=0; |
|---|
| 281 | } |
|---|
| 282 | |
|---|
| 283 | MagickExport void Ascii85Flush(Image *image) |
|---|
| 284 | { |
|---|
| 285 | register char |
|---|
| 286 | *tuple; |
|---|
| 287 | |
|---|
| 288 | assert(image != (Image *) NULL); |
|---|
| 289 | assert(image->signature == MagickSignature); |
|---|
| 290 | if (image->debug != MagickFalse) |
|---|
| 291 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
|---|
| 292 | assert(image->ascii85 != (Ascii85Info *) NULL); |
|---|
| 293 | if (image->ascii85->offset > 0) |
|---|
| 294 | { |
|---|
| 295 | image->ascii85->buffer[image->ascii85->offset]='\0'; |
|---|
| 296 | image->ascii85->buffer[image->ascii85->offset+1]='\0'; |
|---|
| 297 | image->ascii85->buffer[image->ascii85->offset+2]='\0'; |
|---|
| 298 | tuple=Ascii85Tuple(image->ascii85->buffer); |
|---|
| 299 | (void) WriteBlob(image,(size_t) image->ascii85->offset+1, |
|---|
| 300 | (const unsigned char *) (*tuple == 'z' ? "!!!!" : tuple)); |
|---|
| 301 | } |
|---|
| 302 | (void) WriteBlobByte(image,'~'); |
|---|
| 303 | (void) WriteBlobByte(image,'>'); |
|---|
| 304 | (void) WriteBlobByte(image,'\n'); |
|---|
| 305 | } |
|---|
| 306 | |
|---|
| 307 | MagickExport void Ascii85Encode(Image *image,const unsigned char code) |
|---|
| 308 | { |
|---|
| 309 | long |
|---|
| 310 | n; |
|---|
| 311 | |
|---|
| 312 | register char |
|---|
| 313 | *q; |
|---|
| 314 | |
|---|
| 315 | register unsigned char |
|---|
| 316 | *p; |
|---|
| 317 | |
|---|
| 318 | assert(image != (Image *) NULL); |
|---|
| 319 | assert(image->signature == MagickSignature); |
|---|
| 320 | assert(image->ascii85 != (Ascii85Info *) NULL); |
|---|
| 321 | image->ascii85->buffer[image->ascii85->offset]=code; |
|---|
| 322 | image->ascii85->offset++; |
|---|
| 323 | if (image->ascii85->offset < 4) |
|---|
| 324 | return; |
|---|
| 325 | p=image->ascii85->buffer; |
|---|
| 326 | for (n=image->ascii85->offset; n >= 4; n-=4) |
|---|
| 327 | { |
|---|
| 328 | for (q=Ascii85Tuple(p); *q != '\0'; q++) |
|---|
| 329 | { |
|---|
| 330 | image->ascii85->line_break--; |
|---|
| 331 | if ((image->ascii85->line_break < 0) && (*q != '%')) |
|---|
| 332 | { |
|---|
| 333 | (void) WriteBlobByte(image,'\n'); |
|---|
| 334 | image->ascii85->line_break=2*MaxLineExtent; |
|---|
| 335 | } |
|---|
| 336 | (void) WriteBlobByte(image,(unsigned char) *q); |
|---|
| 337 | } |
|---|
| 338 | p+=8; |
|---|
| 339 | } |
|---|
| 340 | image->ascii85->offset=n; |
|---|
| 341 | p-=4; |
|---|
| 342 | for (n=0; n < 4; n++) |
|---|
| 343 | image->ascii85->buffer[n]=(*p++); |
|---|
| 344 | } |
|---|
| 345 | |
|---|
| 346 | |
|---|
| 347 | |
|---|
| 348 | |
|---|
| 349 | |
|---|
| 350 | |
|---|
| 351 | |
|---|
| 352 | |
|---|
| 353 | |
|---|
| 354 | |
|---|
| 355 | |
|---|
| 356 | |
|---|
| 357 | |
|---|
| 358 | |
|---|
| 359 | |
|---|
| 360 | |
|---|
| 361 | |
|---|
| 362 | |
|---|
| 363 | |
|---|
| 364 | |
|---|
| 365 | |
|---|
| 366 | |
|---|
| 367 | |
|---|
| 368 | |
|---|
| 369 | static inline size_t MagickMax(const size_t x,const size_t y) |
|---|
| 370 | { |
|---|
| 371 | if (x > y) |
|---|
| 372 | return(x); |
|---|
| 373 | return(y); |
|---|
| 374 | } |
|---|
| 375 | |
|---|
| 376 | static inline size_t MagickMin(const size_t x,const size_t y) |
|---|
| 377 | { |
|---|
| 378 | if (x < y) |
|---|
| 379 | return(x); |
|---|
| 380 | return(y); |
|---|
| 381 | } |
|---|
| 382 | |
|---|
| 383 | MagickExport MagickBooleanType HuffmanDecodeImage(Image *image) |
|---|
| 384 | { |
|---|
| 385 | #define HashSize 1021 |
|---|
| 386 | #define MBHashA 293 |
|---|
| 387 | #define MBHashB 2695 |
|---|
| 388 | #define MWHashA 3510 |
|---|
| 389 | #define MWHashB 1178 |
|---|
| 390 | |
|---|
| 391 | #define InitializeHashTable(hash,table,a,b) \ |
|---|
| 392 | { \ |
|---|
| 393 | entry=table; \ |
|---|
| 394 | while (entry->code != 0) \ |
|---|
| 395 | { \ |
|---|
| 396 | hash[((entry->length+a)*(entry->code+b)) % HashSize]=(HuffmanTable *) entry; \ |
|---|
| 397 | entry++; \ |
|---|
| 398 | } \ |
|---|
| 399 | } |
|---|
| 400 | |
|---|
| 401 | #define InputBit(bit) \ |
|---|
| 402 | { \ |
|---|
| 403 | if ((mask & 0xff) == 0) \ |
|---|
| 404 | { \ |
|---|
| 405 | byte=ReadBlobByte(image); \ |
|---|
| 406 | if (byte == EOF) \ |
|---|
| 407 | break; \ |
|---|
| 408 | mask=0x80; \ |
|---|
| 409 | } \ |
|---|
| 410 | runlength++; \ |
|---|
| 411 | bit=(unsigned long) ((byte & mask) != 0 ? 0x01 : 0x00); \ |
|---|
| 412 | mask>>=1; \ |
|---|
| 413 | if (bit != 0) \ |
|---|
| 414 | runlength=0; \ |
|---|
| 415 | } |
|---|
| 416 | |
|---|
| 417 | const HuffmanTable |
|---|
| 418 | *entry; |
|---|
| 419 | |
|---|
| 420 | HuffmanTable |
|---|
| 421 | **mb_hash, |
|---|
| 422 | **mw_hash; |
|---|
| 423 | |
|---|
| 424 | IndexPacket |
|---|
| 425 | index; |
|---|
| 426 | |
|---|
| 427 | int |
|---|
| 428 | byte; |
|---|
| 429 | |
|---|
| 430 | long |
|---|
| 431 | y; |
|---|
| 432 | |
|---|
| 433 | MagickBooleanType |
|---|
| 434 | status; |
|---|
| 435 | |
|---|
| 436 | register IndexPacket |
|---|
| 437 | *indexes; |
|---|
| 438 | |
|---|
| 439 | register long |
|---|
| 440 | i, |
|---|
| 441 | x; |
|---|
| 442 | |
|---|
| 443 | register PixelPacket |
|---|
| 444 | *q; |
|---|
| 445 | |
|---|
| 446 | register unsigned char |
|---|
| 447 | *p; |
|---|
| 448 | |
|---|
| 449 | ssize_t |
|---|
| 450 | count; |
|---|
| 451 | |
|---|
| 452 | unsigned char |
|---|
| 453 | *scanline; |
|---|
| 454 | |
|---|
| 455 | unsigned int |
|---|
| 456 | bail, |
|---|
| 457 | color; |
|---|
| 458 | |
|---|
| 459 | unsigned long |
|---|
| 460 | bit, |
|---|
| 461 | code, |
|---|
| 462 | mask, |
|---|
| 463 | length, |
|---|
| 464 | null_lines, |
|---|
| 465 | runlength; |
|---|
| 466 | |
|---|
| 467 | |
|---|
| 468 | |
|---|
| 469 | |
|---|
| 470 | assert(image != (Image *) NULL); |
|---|
| 471 | assert(image->signature == MagickSignature); |
|---|
| 472 | if (image->debug != MagickFalse) |
|---|
| 473 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
|---|
| 474 | mb_hash=(HuffmanTable **) AcquireQuantumMemory(HashSize,sizeof(*mb_hash)); |
|---|
| 475 | mw_hash=(HuffmanTable **) AcquireQuantumMemory(HashSize,sizeof(*mw_hash)); |
|---|
| 476 | scanline=(unsigned char *) AcquireQuantumMemory((size_t) image->columns, |
|---|
| 477 | sizeof(*scanline)); |
|---|
| 478 | if ((mb_hash == (HuffmanTable **) NULL) || |
|---|
| 479 | (mw_hash == (HuffmanTable **) NULL) || |
|---|
| 480 | (scanline == (unsigned char *) NULL)) |
|---|
| 481 | ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed", |
|---|
| 482 | image->filename); |
|---|
| 483 | |
|---|
| 484 | |
|---|
| 485 | |
|---|
| 486 | for (i=0; i < HashSize; i++) |
|---|
| 487 | { |
|---|
| 488 | mb_hash[i]=(HuffmanTable *) NULL; |
|---|
| 489 | mw_hash[i]=(HuffmanTable *) NULL; |
|---|
| 490 | } |
|---|
| 491 | InitializeHashTable(mw_hash,TWTable,MWHashA,MWHashB); |
|---|
| 492 | InitializeHashTable(mw_hash,MWTable,MWHashA,MWHashB); |
|---|
| 493 | InitializeHashTable(mw_hash,EXTable,MWHashA,MWHashB); |
|---|
| 494 | InitializeHashTable(mb_hash,TBTable,MBHashA,MBHashB); |
|---|
| 495 | InitializeHashTable(mb_hash,MBTable,MBHashA,MBHashB); |
|---|
| 496 | InitializeHashTable(mb_hash,EXTable,MBHashA,MBHashB); |
|---|
| 497 | |
|---|
| 498 | |
|---|
| 499 | |
|---|
| 500 | byte=0; |
|---|
| 501 | mask=0; |
|---|
| 502 | null_lines=0; |
|---|
| 503 | runlength=0; |
|---|
| 504 | while (runlength < 11) |
|---|
| 505 | InputBit(bit); |
|---|
| 506 | do { InputBit(bit); } while ((int) bit == 0); |
|---|
| 507 | image->x_resolution=204.0; |
|---|
| 508 | image->y_resolution=196.0; |
|---|
| 509 | image->units=PixelsPerInchResolution; |
|---|
| 510 | for (y=0; ((y < (long) image->rows) && (null_lines < 3)); ) |
|---|
| 511 | { |
|---|
| 512 | |
|---|
| 513 | |
|---|
| 514 | |
|---|
| 515 | p=scanline; |
|---|
| 516 | for (x=0; x < (long) image->columns; x++) |
|---|
| 517 | *p++=(unsigned char) 0; |
|---|
| 518 | |
|---|
| 519 | |
|---|
| 520 | |
|---|
| 521 | color=MagickTrue; |
|---|
| 522 | code=0; |
|---|
| 523 | count=0; |
|---|
| 524 | length=0; |
|---|
| 525 | runlength=0; |
|---|
| 526 | x=0; |
|---|
| 527 | for ( ; ; ) |
|---|
| 528 | { |
|---|
| 529 | if (byte == EOF) |
|---|
| 530 | break; |
|---|
| 531 | if (x >= (long) image->columns) |
|---|
| 532 | { |
|---|
| 533 | while (runlength < 11) |
|---|
| 534 | InputBit(bit); |
|---|
| 535 | do { InputBit(bit); } while ((int) bit == 0); |
|---|
| 536 | break; |
|---|
| 537 | } |
|---|
| 538 | bail=MagickFalse; |
|---|
| 539 | do |
|---|
| 540 | { |
|---|
| 541 | if (runlength < 11) |
|---|
| 542 | InputBit(bit) |
|---|
| 543 | else |
|---|
| 544 | { |
|---|
| 545 | InputBit(bit); |
|---|
| 546 | if ((int) bit != 0) |
|---|
| 547 | { |
|---|
| 548 | null_lines++; |
|---|
| 549 | if (x != 0) |
|---|
| 550 | null_lines=0; |
|---|
| 551 | bail=MagickTrue; |
|---|
| 552 | break; |
|---|
| 553 | } |
|---|
| 554 | } |
|---|
| 555 | code=(code << 1)+(unsigned long) bit; |
|---|
| 556 | length++; |
|---|
| 557 | } while (code == 0); |
|---|
| 558 | if (bail != MagickFalse) |
|---|
| 559 | break; |
|---|
| 560 | if (length > 13) |
|---|
| 561 | { |
|---|
| 562 | while (runlength < 11) |
|---|
| 563 | InputBit(bit); |
|---|
| 564 | do { InputBit(bit); } while ((int) bit == 0); |
|---|
| 565 | break; |
|---|
| 566 | } |
|---|
| 567 | if (color != MagickFalse) |
|---|
| 568 | { |
|---|
| 569 | if (length < 4) |
|---|
| 570 | continue; |
|---|
| 571 | entry=mw_hash[((length+MWHashA)*(code+MWHashB)) % HashSize]; |
|---|
| 572 | } |
|---|
| 573 | else |
|---|
| 574 | { |
|---|
| 575 | if (length < 2) |
|---|
| 576 | continue; |
|---|
| 577 | entry=mb_hash[((length+MBHashA)*(code+MBHashB)) % HashSize]; |
|---|
| 578 | } |
|---|
| 579 | if (entry == (const HuffmanTable *) NULL) |
|---|
| 580 | continue; |
|---|
| 581 | if ((entry->length != length) || (entry->code != code)) |
|---|
| 582 | continue; |
|---|
| 583 | switch (entry->id) |
|---|
| 584 | { |
|---|
| 585 | case TWId: |
|---|
| 586 | case TBId: |
|---|
| 587 | { |
|---|
| 588 | count+=entry->count; |
|---|
| 589 | if ((x+count) > (long) image->columns) |
|---|
| 590 | count=(ssize_t) image->columns-x; |
|---|
| 591 | if (count > 0) |
|---|
| 592 | { |
|---|
| 593 | if (color != MagickFalse) |
|---|
| 594 | { |
|---|
| 595 | x+=count; |
|---|
| 596 | count=0; |
|---|
| 597 | } |
|---|
| 598 | else |
|---|
| 599 | for ( ; count > 0; count--) |
|---|
| 600 | scanline[x++]=(unsigned char) 1; |
|---|
| 601 | } |
|---|
| 602 | color=(unsigned int) |
|---|
| 603 | ((color == MagickFalse) ? MagickTrue : MagickFalse); |
|---|
| 604 | break; |
|---|
| 605 | } |
|---|
| 606 | case MWId: |
|---|
| 607 | case MBId: |
|---|
| 608 | case EXId: |
|---|
| 609 | { |
|---|
| 610 | count+=entry->count; |
|---|
| 611 | break; |
|---|
| 612 | } |
|---|
| 613 | default: |
|---|
| 614 | break; |
|---|
| 615 | } |
|---|
| 616 | code=0; |
|---|
| 617 | length=0; |
|---|
| 618 | } |
|---|
| 619 | |
|---|
| 620 | |
|---|
| 621 | |
|---|
| 622 | p=scanline; |
|---|
| 623 | q=SetImagePixels(image,0,y,image->columns,1); |
|---|
| 624 | if (q == (PixelPacket *) NULL) |
|---|
| 625 | break; |
|---|
| 626 | indexes=GetIndexes(image); |
|---|
| 627 | for (x=0; x < (long) image->columns; x++) |
|---|
| 628 | { |
|---|
| 629 | index=(IndexPacket) (*p++); |
|---|
| 630 | indexes[x]=index; |
|---|
| 631 | *q++=image->colormap[(long) index]; |
|---|
| 632 | } |
|---|
| 633 | if (SyncImagePixels(image) == MagickFalse) |
|---|
| 634 | break; |
|---|
| 635 | if ((image->progress_monitor != (MagickProgressMonitor) NULL) && |
|---|
| 636 | (QuantumTick(y,image->rows) != MagickFalse)) |
|---|
| 637 | { |
|---|
| 638 | status=image->progress_monitor(LoadImageTag,y,image->rows, |
|---|
| 639 | image->client_data); |
|---|
| 640 | if (status == MagickFalse) |
|---|
| 641 | break; |
|---|
| 642 | } |
|---|
| 643 | y++; |
|---|
| 644 | } |
|---|
| 645 | image->rows=(unsigned long) MagickMax((size_t) y-3,1); |
|---|
| 646 | image->compression=FaxCompression; |
|---|
| 647 | |
|---|
| 648 | |
|---|
| 649 | |
|---|
| 650 | mw_hash=(HuffmanTable **) RelinquishMagickMemory(mw_hash); |
|---|
| 651 | mb_hash=(HuffmanTable **) RelinquishMagickMemory(mb_hash); |
|---|
| 652 | scanline=(unsigned char *) RelinquishMagickMemory(scanline); |
|---|
| 653 | return(MagickTrue); |
|---|
| 654 | } |
|---|
| 655 | |
|---|
| 656 | |
|---|
| 657 | |
|---|
| 658 | |
|---|
| 659 | |
|---|
| 660 | |
|---|
| 661 | |
|---|
| 662 | |
|---|
| 663 | |
|---|
| 664 | |
|---|
| 665 | |
|---|
| 666 | |
|---|
| 667 | |
|---|
| 668 | |
|---|
| 669 | |
|---|
| 670 | |
|---|
| 671 | |
|---|
| 672 | |
|---|
| 673 | |
|---|
| 674 | |
|---|
| 675 | |
|---|
| 676 | |
|---|
| 677 | |
|---|
| 678 | |
|---|
| 679 | |
|---|
| 680 | |
|---|
| 681 | MagickExport MagickBooleanType HuffmanEncodeImage(const ImageInfo *image_info, |
|---|
| 682 | Image *image) |
|---|
| 683 | { |
|---|
| 684 | #define HuffmanOutputCode(entry) \ |
|---|
| 685 | { \ |
|---|
| 686 | mask=1 << (entry->length-1); \ |
|---|
| 687 | while (mask != 0) \ |
|---|
| 688 | { \ |
|---|
| 689 | OutputBit(((entry->code & mask) != 0 ? 1 : 0)); \ |
|---|
| 690 | mask>>=1; \ |
|---|
| 691 | } \ |
|---|
| 692 | } |
|---|
| 693 | |
|---|
| 694 | #define OutputBit(count) \ |
|---|
| 695 | { \ |
|---|
| 696 | if (count > 0) \ |
|---|
| 697 | byte=byte | bit; \ |
|---|
| 698 | bit>>=1; \ |
|---|
| 699 | if ((int) (bit & 0xff) == 0) \ |
|---|
| 700 | { \ |
|---|
| 701 | if (LocaleCompare(image_info->magick,"FAX") == 0) \ |
|---|
| 702 | (void) WriteBlobByte(image,(unsigned char) byte); \ |
|---|
| 703 | else \ |
|---|
| 704 | Ascii85Encode(image,byte); \ |
|---|
| 705 | byte='\0'; \ |
|---|
| 706 | bit=(unsigned char) 0x80; \ |
|---|
| 707 | } \ |
|---|
| 708 | } |
|---|
| 709 | |
|---|
| 710 | const HuffmanTable |
|---|
| 711 | *entry; |
|---|
| 712 | |
|---|
| 713 | int |
|---|
| 714 | k, |
|---|
| 715 | runlength; |
|---|
| 716 | |
|---|
| 717 | long |
|---|
| 718 | n, |
|---|
| 719 | y; |
|---|
| 720 | |
|---|
| 721 | Image |
|---|
| 722 | *huffman_image; |
|---|
| 723 | |
|---|
| 724 | MagickBooleanType |
|---|
| 725 | status; |
|---|
| 726 | |
|---|
| 727 | register IndexPacket |
|---|
| 728 | *indexes; |
|---|
| 729 | |
|---|
| 730 | register long |
|---|
| 731 | i, |
|---|
| 732 | x; |
|---|
| 733 | |
|---|
| 734 | register const PixelPacket |
|---|
| 735 | *p; |
|---|
| 736 | |
|---|
| 737 | register unsigned char |
|---|
| 738 | *q; |
|---|
| 739 | |
|---|
| 740 | unsigned char |
|---|
| 741 | byte, |
|---|
| 742 | bit, |
|---|
| 743 | *scanline; |
|---|
| 744 | |
|---|
| 745 | unsigned long |
|---|
| 746 | mask, |
|---|
| 747 | width; |
|---|
| 748 | |
|---|
| 749 | |
|---|
| 750 | |
|---|
| 751 | |
|---|
| 752 | assert(image != (Image *) NULL); |
|---|
| 753 | assert(image->signature == MagickSignature); |
|---|
| 754 | if (image->debug != MagickFalse) |
|---|
| 755 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
|---|
| 756 | width=image->columns; |
|---|
| 757 | if (LocaleCompare(image_info->magick,"FAX") == 0) |
|---|
| 758 | width=(unsigned long) MagickMax(image->columns,1728); |
|---|
| 759 | scanline=(unsigned char *) AcquireQuantumMemory((size_t) width+1UL, |
|---|
| 760 | sizeof(*scanline)); |
|---|
| 761 | if (scanline == (unsigned char *) NULL) |
|---|
| 762 | ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed", |
|---|
| 763 | image->filename); |
|---|
| 764 | (void) ResetMagickMemory(scanline,0,width*sizeof(*scanline)); |
|---|
| 765 | huffman_image=CloneImage(image,0,0,MagickTrue,&image->exception); |
|---|
| 766 | if (huffman_image == (Image *) NULL) |
|---|
| 767 | { |
|---|
| 768 | scanline=(unsigned char *) RelinquishMagickMemory(scanline); |
|---|
| 769 | return(MagickFalse); |
|---|
| 770 | } |
|---|
| 771 | (void) SetImageType(huffman_image,BilevelType); |
|---|
| 772 | byte='\0'; |
|---|
| 773 | bit=(unsigned char) 0x80; |
|---|
| 774 | if (LocaleCompare(image_info->magick,"FAX") != 0) |
|---|
| 775 | Ascii85Initialize(image); |
|---|
| 776 | else |
|---|
| 777 | { |
|---|
| 778 | |
|---|
| 779 | |
|---|
| 780 | |
|---|
| 781 | for (k=0; k < 11; k++) |
|---|
| 782 | OutputBit(0); |
|---|
| 783 | OutputBit(1); |
|---|
| 784 | } |
|---|
| 785 | |
|---|
| 786 | |
|---|
| 787 | |
|---|
| 788 | q=scanline; |
|---|
| 789 | for (y=0; y < (long) huffman_image->rows; y++) |
|---|
| 790 | { |
|---|
| 791 | p=AcquireImagePixels(huffman_image,0,y,huffman_image->columns,1, |
|---|
| 792 | &huffman_image->exception); |
|---|
| 793 | if (p == (const PixelPacket *) NULL) |
|---|
| 794 | break; |
|---|
| 795 | indexes=GetIndexes(huffman_image); |
|---|
| 796 | for (x=0; x < (long) huffman_image->columns; x++) |
|---|
| 797 | { |
|---|
| 798 | *q++=(unsigned char) (PixelIntensity(p) >= ((MagickRealType) |
|---|
| 799 | QuantumRange/2.0) ? 0 : 1); |
|---|
| 800 | p++; |
|---|
| 801 | } |
|---|
| 802 | |
|---|
| 803 | |
|---|
| 804 | |
|---|
| 805 | q=scanline; |
|---|
| 806 | for (n=(long) width; n > 0; ) |
|---|
| 807 | { |
|---|
| 808 | |
|---|
| 809 | |
|---|
| 810 | |
|---|
| 811 | for (runlength=0; ((n > 0) && (*q == 0)); n--) |
|---|
| 812 | { |
|---|
| 813 | q++; |
|---|
| 814 | runlength++; |
|---|
| 815 | } |
|---|
| 816 | if (runlength >= 64) |
|---|
| 817 | { |
|---|
| 818 | if (runlength < 1792) |
|---|
| 819 | entry=MWTable+((runlength/64)-1); |
|---|
| 820 | else |
|---|
| 821 | entry=EXTable+(MagickMin((size_t) runlength,2560)-1792)/64; |
|---|
| 822 | runlength-=entry->count; |
|---|
| 823 | HuffmanOutputCode(entry); |
|---|
| 824 | } |
|---|
| 825 | entry=TWTable+MagickMin((size_t) runlength,63); |
|---|
| 826 | HuffmanOutputCode(entry); |
|---|
| 827 | if (n != 0) |
|---|
| 828 | { |
|---|
| 829 | |
|---|
| 830 | |
|---|
| 831 | |
|---|
| 832 | for (runlength=0; ((*q != 0) && (n > 0)); n--) |
|---|
| 833 | { |
|---|
| 834 | q++; |
|---|
| 835 | runlength++; |
|---|
| 836 | } |
|---|
| 837 | if (runlength >= 64) |
|---|
| 838 | { |
|---|
| 839 | entry=MBTable+((runlength/64)-1); |
|---|
| 840 | if (runlength >= 1792) |
|---|
| 841 | entry=EXTable+(MagickMin((size_t) runlength,2560)-1792)/64; |
|---|
| 842 | runlength-=entry->count; |
|---|
| 843 | HuffmanOutputCode(entry); |
|---|
| 844 | } |
|---|
| 845 | entry=TBTable+MagickMin((size_t) runlength,63); |
|---|
| 846 | HuffmanOutputCode(entry); |
|---|
| 847 | } |
|---|
| 848 | } |
|---|
| 849 | |
|---|
| 850 | |
|---|
| 851 | |
|---|
| 852 | for (k=0; k < 11; k++) |
|---|
| 853 | OutputBit(0); |
|---|
| 854 | OutputBit(1); |
|---|
| 855 | q=scanline; |
|---|
| 856 | if (GetPreviousImageInList(huffman_image) == (Image *) NULL) |
|---|
| 857 | if ((image->progress_monitor != (MagickProgressMonitor) NULL) && |
|---|
| 858 | (QuantumTick(y,huffman_image->rows) != MagickFalse)) |
|---|
| 859 | { |
|---|
| 860 | status=image->progress_monitor(LoadImageTag,y,huffman_image->rows, |
|---|
| 861 | image->client_data); |
|---|
| 862 | if (status == MagickFalse) |
|---|
| 863 | break; |
|---|
| 864 | } |
|---|
| 865 | } |
|---|
| 866 | |
|---|
| 867 | |
|---|
| 868 | |
|---|
| 869 | for (i=0; i < 6; i++) |
|---|
| 870 | { |
|---|
| 871 | for (k=0; k < 11; k++) |
|---|
| 872 | OutputBit(0); |
|---|
| 873 | OutputBit(1); |
|---|
| 874 | } |
|---|
| 875 | |
|---|
| 876 | |
|---|
| 877 | |
|---|
| 878 | if (((int) bit != 0x80) != 0) |
|---|
| 879 | { |
|---|
| 880 | if (LocaleCompare(image_info->magick,"FAX") == 0) |
|---|
| 881 | (void) WriteBlobByte(image,byte); |
|---|
| 882 | else |
|---|
| 883 | Ascii85Encode(image, byte); |
|---|
| 884 | } |
|---|
| 885 | if (LocaleCompare(image_info->magick,"FAX") != 0) |
|---|
| 886 | Ascii85Flush(image); |
|---|
| 887 | huffman_image=DestroyImage(huffman_image); |
|---|
| 888 | scanline=(unsigned char *) RelinquishMagickMemory(scanline); |
|---|
| 889 | return(MagickTrue); |
|---|
| 890 | } |
|---|
| 891 | |
|---|
| 892 | #if defined(HasTIFF) |
|---|
| 893 | |
|---|
| 894 | |
|---|
| 895 | |
|---|
| 896 | |
|---|
| 897 | |
|---|
| 898 | |
|---|
| 899 | |
|---|
| 900 | |
|---|
| 901 | |
|---|
| 902 | |
|---|
| 903 | |
|---|
| 904 | |
|---|
| 905 | |
|---|
| 906 | |
|---|
| 907 | |
|---|
| 908 | |
|---|
| 909 | |
|---|
| 910 | |
|---|
| 911 | |
|---|
| 912 | |
|---|
| 913 | |
|---|
| 914 | |
|---|
| 915 | |
|---|
| 916 | |
|---|
| 917 | |
|---|
| 918 | |
|---|
| 919 | MagickExport MagickBooleanType Huffman2DEncodeImage(const ImageInfo *image_info, |
|---|
| 920 | Image *image) |
|---|
| 921 | { |
|---|
| 922 | char |
|---|
| 923 | filename[MaxTextExtent]; |
|---|
| 924 | |
|---|
| 925 | FILE |
|---|
| 926 | *file; |
|---|
| 927 | |
|---|
| 928 | Image |
|---|
| 929 | *huffman_image; |
|---|
| 930 | |
|---|
| 931 | ImageInfo |
|---|
| 932 | *write_info; |
|---|
| 933 | |
|---|
| 934 | int |
|---|
| 935 | unique_file; |
|---|
| 936 | |
|---|
| 937 | MagickBooleanType |
|---|
| 938 | status; |
|---|
| 939 | |
|---|
| 940 | register long |
|---|
| 941 | i; |
|---|
| 942 | |
|---|
| 943 | ssize_t |
|---|
| 944 | count; |
|---|
| 945 | |
|---|
| 946 | TIFF |
|---|
| 947 | *tiff; |
|---|
| 948 | |
|---|
| 949 | uint16 |
|---|
| 950 | fillorder; |
|---|
| 951 | |
|---|
| 952 | unsigned char |
|---|
| 953 | *buffer; |
|---|
| 954 | |
|---|
| 955 | unsigned long |
|---|
| 956 | *byte_count, |
|---|
| 957 | strip_size; |
|---|
| 958 | |
|---|
| 959 | |
|---|
| 960 | |
|---|
| 961 | |
|---|
| 962 | assert(image_info != (ImageInfo *) NULL); |
|---|
| 963 | assert(image_info->signature == MagickSignature); |
|---|
| 964 | if (image->debug != MagickFalse) |
|---|
| 965 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
|---|
| 966 | assert(image != (Image *) NULL); |
|---|
| 967 | assert(image->signature == MagickSignature); |
|---|
| 968 | huffman_image=CloneImage(image,0,0,MagickTrue,&image->exception); |
|---|
| 969 | if (huffman_image == (Image *) NULL) |
|---|
| 970 | return(MagickFalse); |
|---|
| 971 | DestroyBlob(huffman_image); |
|---|
| 972 | huffman_image->blob=CloneBlobInfo((BlobInfo *) NULL); |
|---|
| 973 | (void) SetImageType(huffman_image,BilevelType); |
|---|
| 974 | file=(FILE *) NULL; |
|---|
| 975 | unique_file=AcquireUniqueFileResource(filename); |
|---|
| 976 | if (unique_file != -1) |
|---|
| 977 | file=fdopen(unique_file,"wb"); |
|---|
| 978 | if ((unique_file == -1) || (file == (FILE *) NULL)) |
|---|
| 979 | { |
|---|
| 980 | (void) CopyMagickString(image->filename,filename,MaxTextExtent); |
|---|
| 981 | ThrowFileException(&image->exception,FileOpenError, |
|---|
| 982 | "UnableToCreateTemporaryFile",image->filename); |
|---|
| 983 | return(MagickFalse); |
|---|
| 984 | } |
|---|
| 985 | (void) FormatMagickString(huffman_image->filename,MaxTextExtent,"tiff:%s", |
|---|
| 986 | filename); |
|---|
| 987 | write_info=CloneImageInfo(image_info); |
|---|
| 988 | SetImageInfoFile(write_info,file); |
|---|
| 989 | write_info->compression=Group4Compression; |
|---|
| 990 | (void) SetImageOption(write_info,"quantum:polarity","min-is-white"); |
|---|
| 991 | status=WriteImage(write_info,huffman_image); |
|---|
| 992 | write_info=DestroyImageInfo(write_info); |
|---|
| 993 | if (status == MagickFalse) |
|---|
| 994 | return(MagickFalse); |
|---|
| 995 | tiff=TIFFOpen(filename,"rb"); |
|---|
| 996 | if (tiff == (TIFF *) NULL) |
|---|
| 997 | { |
|---|
| 998 | huffman_image=DestroyImage(huffman_image); |
|---|
| 999 | (void) fclose(file); |
|---|
| 1000 | (void) RelinquishUniqueFileResource(filename); |
|---|
| 1001 | ThrowFileException(&image->exception,FileOpenError,"UnableToOpenFile", |
|---|
| 1002 | image_info->filename); |
|---|
| 1003 | return(MagickFalse); |
|---|
| 1004 | } |
|---|
| 1005 | |
|---|
| 1006 | |
|---|
| 1007 | |
|---|
| 1008 | byte_count=0; |
|---|
| 1009 | (void) TIFFGetField(tiff,TIFFTAG_STRIPBYTECOUNTS,&byte_count); |
|---|
| 1010 | strip_size=byte_count[0]; |
|---|
| 1011 | for (i=1; i < (long) TIFFNumberOfStrips(tiff); i++) |
|---|
| 1012 | if (byte_count[i] > strip_size) |
|---|
| 1013 | strip_size=byte_count[i]; |
|---|
| 1014 | buffer=(unsigned char *) AcquireQuantumMemory((size_t) strip_size, |
|---|
| 1015 | sizeof(*buffer)); |
|---|
| 1016 | if (buffer == (unsigned char *) NULL) |
|---|
| 1017 | { |
|---|
| 1018 | TIFFClose(tiff); |
|---|
| 1019 | huffman_image=DestroyImage(huffman_image); |
|---|
| 1020 | (void) fclose(file); |
|---|
| 1021 | (void) RelinquishUniqueFileResource(filename); |
|---|
| 1022 | ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed", |
|---|
| 1023 | image_info->filename); |
|---|
| 1024 | } |
|---|
| 1025 | |
|---|
| 1026 | |
|---|
| 1027 | |
|---|
| 1028 | fillorder=FILLORDER_LSB2MSB; |
|---|
| 1029 | (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_FILLORDER,&fillorder); |
|---|
| 1030 | for (i=0; i < (long) TIFFNumberOfStrips(tiff); i++) |
|---|
| 1031 | { |
|---|
| 1032 | count=(ssize_t) TIFFReadRawStrip(tiff,(uint32) i,buffer,(long) |
|---|
| 1033 | byte_count[i]); |
|---|
| 1034 | if (fillorder == FILLORDER_LSB2MSB) |
|---|
| 1035 | TIFFReverseBits(buffer,(unsigned long) count); |
|---|
| 1036 | (void) WriteBlob(image,count,buffer); |
|---|
| 1037 | } |
|---|
| 1038 | buffer=(unsigned char *) RelinquishMagickMemory(buffer); |
|---|
| 1039 | TIFFClose(tiff); |
|---|
| 1040 | huffman_image=DestroyImage(huffman_image); |
|---|
| 1041 | (void) fclose(file); |
|---|
| 1042 | (void) RelinquishUniqueFileResource(filename); |
|---|
| 1043 | return(MagickTrue); |
|---|
| 1044 | } |
|---|
| 1045 | #else |
|---|
| 1046 | MagickExport MagickBooleanType Huffman2DEncodeImage(const ImageInfo *image_info, |
|---|
| 1047 | Image *image) |
|---|
| 1048 | { |
|---|
| 1049 | assert(image != (Image *) NULL); |
|---|
| 1050 |
|---|