1 /* 2 * Copyright (c) 1988-1997 Sam Leffler 3 * Copyright (c) 1991-1997 Silicon Graphics, Inc. 4 * 5 * Permission to use, copy, modify, distribute, and sell this software and 6 * its documentation for any purpose is hereby granted without fee, provided 7 * that (i) the above copyright notices and this permission notice appear in 8 * all copies of the software and related documentation, and (ii) the names of 9 * Sam Leffler and Silicon Graphics may not be used in any advertising or 10 * publicity relating to the software without the specific, prior written 11 * permission of Sam Leffler and Silicon Graphics. 12 * 13 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, 14 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 15 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. 16 * 17 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR 18 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, 19 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, 20 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF 21 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 22 * OF THIS SOFTWARE. 23 */ 24 25 /* 26 * TIFF Library. 27 * 28 * Predictor Tag Support (used by multiple codecs). 29 */ 30 #include <precomp.h> 31 #include "tif_predict.h" 32 33 #define PredictorState(tif) ((TIFFPredictorState*) (tif)->tif_data) 34 35 static int horAcc8(TIFF* tif, uint8* cp0, tmsize_t cc); 36 static int horAcc16(TIFF* tif, uint8* cp0, tmsize_t cc); 37 static int horAcc32(TIFF* tif, uint8* cp0, tmsize_t cc); 38 static int swabHorAcc16(TIFF* tif, uint8* cp0, tmsize_t cc); 39 static int swabHorAcc32(TIFF* tif, uint8* cp0, tmsize_t cc); 40 static int horDiff8(TIFF* tif, uint8* cp0, tmsize_t cc); 41 static int horDiff16(TIFF* tif, uint8* cp0, tmsize_t cc); 42 static int horDiff32(TIFF* tif, uint8* cp0, tmsize_t cc); 43 static int swabHorDiff16(TIFF* tif, uint8* cp0, tmsize_t cc); 44 static int swabHorDiff32(TIFF* tif, uint8* cp0, tmsize_t cc); 45 static int fpAcc(TIFF* tif, uint8* cp0, tmsize_t cc); 46 static int fpDiff(TIFF* tif, uint8* cp0, tmsize_t cc); 47 static int PredictorDecodeRow(TIFF* tif, uint8* op0, tmsize_t occ0, uint16 s); 48 static int PredictorDecodeTile(TIFF* tif, uint8* op0, tmsize_t occ0, uint16 s); 49 static int PredictorEncodeRow(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s); 50 static int PredictorEncodeTile(TIFF* tif, uint8* bp0, tmsize_t cc0, uint16 s); 51 52 static int 53 PredictorSetup(TIFF* tif) 54 { 55 static const char module[] = "PredictorSetup"; 56 57 TIFFPredictorState* sp = PredictorState(tif); 58 TIFFDirectory* td = &tif->tif_dir; 59 60 switch (sp->predictor) /* no differencing */ 61 { 62 case PREDICTOR_NONE: 63 return 1; 64 case PREDICTOR_HORIZONTAL: 65 if (td->td_bitspersample != 8 66 && td->td_bitspersample != 16 67 && td->td_bitspersample != 32) { 68 TIFFErrorExt(tif->tif_clientdata, module, 69 "Horizontal differencing \"Predictor\" not supported with %d-bit samples", 70 td->td_bitspersample); 71 return 0; 72 } 73 break; 74 case PREDICTOR_FLOATINGPOINT: 75 if (td->td_sampleformat != SAMPLEFORMAT_IEEEFP) { 76 TIFFErrorExt(tif->tif_clientdata, module, 77 "Floating point \"Predictor\" not supported with %d data format", 78 td->td_sampleformat); 79 return 0; 80 } 81 if (td->td_bitspersample != 16 82 && td->td_bitspersample != 24 83 && td->td_bitspersample != 32 84 && td->td_bitspersample != 64) { /* Should 64 be allowed? */ 85 TIFFErrorExt(tif->tif_clientdata, module, 86 "Floating point \"Predictor\" not supported with %d-bit samples", 87 td->td_bitspersample); 88 return 0; 89 } 90 break; 91 default: 92 TIFFErrorExt(tif->tif_clientdata, module, 93 "\"Predictor\" value %d not supported", 94 sp->predictor); 95 return 0; 96 } 97 sp->stride = (td->td_planarconfig == PLANARCONFIG_CONTIG ? 98 td->td_samplesperpixel : 1); 99 /* 100 * Calculate the scanline/tile-width size in bytes. 101 */ 102 if (isTiled(tif)) 103 sp->rowsize = TIFFTileRowSize(tif); 104 else 105 sp->rowsize = TIFFScanlineSize(tif); 106 if (sp->rowsize == 0) 107 return 0; 108 109 return 1; 110 } 111 112 static int 113 PredictorSetupDecode(TIFF* tif) 114 { 115 TIFFPredictorState* sp = PredictorState(tif); 116 TIFFDirectory* td = &tif->tif_dir; 117 118 /* Note: when PredictorSetup() fails, the effets of setupdecode() */ 119 /* will not be "cancelled" so setupdecode() might be robust to */ 120 /* be called several times. */ 121 if (!(*sp->setupdecode)(tif) || !PredictorSetup(tif)) 122 return 0; 123 124 if (sp->predictor == 2) { 125 switch (td->td_bitspersample) { 126 case 8: sp->decodepfunc = horAcc8; break; 127 case 16: sp->decodepfunc = horAcc16; break; 128 case 32: sp->decodepfunc = horAcc32; break; 129 } 130 /* 131 * Override default decoding method with one that does the 132 * predictor stuff. 133 */ 134 if( tif->tif_decoderow != PredictorDecodeRow ) 135 { 136 sp->decoderow = tif->tif_decoderow; 137 tif->tif_decoderow = PredictorDecodeRow; 138 sp->decodestrip = tif->tif_decodestrip; 139 tif->tif_decodestrip = PredictorDecodeTile; 140 sp->decodetile = tif->tif_decodetile; 141 tif->tif_decodetile = PredictorDecodeTile; 142 } 143 144 /* 145 * If the data is horizontally differenced 16-bit data that 146 * requires byte-swapping, then it must be byte swapped before 147 * the accumulation step. We do this with a special-purpose 148 * routine and override the normal post decoding logic that 149 * the library setup when the directory was read. 150 */ 151 if (tif->tif_flags & TIFF_SWAB) { 152 if (sp->decodepfunc == horAcc16) { 153 sp->decodepfunc = swabHorAcc16; 154 tif->tif_postdecode = _TIFFNoPostDecode; 155 } else if (sp->decodepfunc == horAcc32) { 156 sp->decodepfunc = swabHorAcc32; 157 tif->tif_postdecode = _TIFFNoPostDecode; 158 } 159 } 160 } 161 162 else if (sp->predictor == 3) { 163 sp->decodepfunc = fpAcc; 164 /* 165 * Override default decoding method with one that does the 166 * predictor stuff. 167 */ 168 if( tif->tif_decoderow != PredictorDecodeRow ) 169 { 170 sp->decoderow = tif->tif_decoderow; 171 tif->tif_decoderow = PredictorDecodeRow; 172 sp->decodestrip = tif->tif_decodestrip; 173 tif->tif_decodestrip = PredictorDecodeTile; 174 sp->decodetile = tif->tif_decodetile; 175 tif->tif_decodetile = PredictorDecodeTile; 176 } 177 /* 178 * The data should not be swapped outside of the floating 179 * point predictor, the accumulation routine should return 180 * byres in the native order. 181 */ 182 if (tif->tif_flags & TIFF_SWAB) { 183 tif->tif_postdecode = _TIFFNoPostDecode; 184 } 185 /* 186 * Allocate buffer to keep the decoded bytes before 187 * rearranging in the right order 188 */ 189 } 190 191 return 1; 192 } 193 194 static int 195 PredictorSetupEncode(TIFF* tif) 196 { 197 TIFFPredictorState* sp = PredictorState(tif); 198 TIFFDirectory* td = &tif->tif_dir; 199 200 if (!(*sp->setupencode)(tif) || !PredictorSetup(tif)) 201 return 0; 202 203 if (sp->predictor == 2) { 204 switch (td->td_bitspersample) { 205 case 8: sp->encodepfunc = horDiff8; break; 206 case 16: sp->encodepfunc = horDiff16; break; 207 case 32: sp->encodepfunc = horDiff32; break; 208 } 209 /* 210 * Override default encoding method with one that does the 211 * predictor stuff. 212 */ 213 if( tif->tif_encoderow != PredictorEncodeRow ) 214 { 215 sp->encoderow = tif->tif_encoderow; 216 tif->tif_encoderow = PredictorEncodeRow; 217 sp->encodestrip = tif->tif_encodestrip; 218 tif->tif_encodestrip = PredictorEncodeTile; 219 sp->encodetile = tif->tif_encodetile; 220 tif->tif_encodetile = PredictorEncodeTile; 221 } 222 223 /* 224 * If the data is horizontally differenced 16-bit data that 225 * requires byte-swapping, then it must be byte swapped after 226 * the differentiation step. We do this with a special-purpose 227 * routine and override the normal post decoding logic that 228 * the library setup when the directory was read. 229 */ 230 if (tif->tif_flags & TIFF_SWAB) { 231 if (sp->encodepfunc == horDiff16) { 232 sp->encodepfunc = swabHorDiff16; 233 tif->tif_postdecode = _TIFFNoPostDecode; 234 } else if (sp->encodepfunc == horDiff32) { 235 sp->encodepfunc = swabHorDiff32; 236 tif->tif_postdecode = _TIFFNoPostDecode; 237 } 238 } 239 } 240 241 else if (sp->predictor == 3) { 242 sp->encodepfunc = fpDiff; 243 /* 244 * Override default encoding method with one that does the 245 * predictor stuff. 246 */ 247 if( tif->tif_encoderow != PredictorEncodeRow ) 248 { 249 sp->encoderow = tif->tif_encoderow; 250 tif->tif_encoderow = PredictorEncodeRow; 251 sp->encodestrip = tif->tif_encodestrip; 252 tif->tif_encodestrip = PredictorEncodeTile; 253 sp->encodetile = tif->tif_encodetile; 254 tif->tif_encodetile = PredictorEncodeTile; 255 } 256 } 257 258 return 1; 259 } 260 261 #define REPEAT4(n, op) \ 262 switch (n) { \ 263 default: { \ 264 tmsize_t i; for (i = n-4; i > 0; i--) { op; } } /*-fallthrough*/ \ 265 case 4: op; /*-fallthrough*/ \ 266 case 3: op; /*-fallthrough*/ \ 267 case 2: op; /*-fallthrough*/ \ 268 case 1: op; /*-fallthrough*/ \ 269 case 0: ; \ 270 } 271 272 /* Remarks related to C standard compliance in all below functions : */ 273 /* - to avoid any undefined behaviour, we only operate on unsigned types */ 274 /* since the behaviour of "overflows" is defined (wrap over) */ 275 /* - when storing into the byte stream, we explicitly mask with 0xff so */ 276 /* as to make icc -check=conversions happy (not necessary by the standard) */ 277 278 TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW 279 static int 280 horAcc8(TIFF* tif, uint8* cp0, tmsize_t cc) 281 { 282 tmsize_t stride = PredictorState(tif)->stride; 283 284 unsigned char* cp = (unsigned char*) cp0; 285 if((cc%stride)!=0) 286 { 287 TIFFErrorExt(tif->tif_clientdata, "horAcc8", 288 "%s", "(cc%stride)!=0"); 289 return 0; 290 } 291 292 if (cc > stride) { 293 /* 294 * Pipeline the most common cases. 295 */ 296 if (stride == 3) { 297 unsigned int cr = cp[0]; 298 unsigned int cg = cp[1]; 299 unsigned int cb = cp[2]; 300 cc -= 3; 301 cp += 3; 302 while (cc>0) { 303 cp[0] = (unsigned char) ((cr += cp[0]) & 0xff); 304 cp[1] = (unsigned char) ((cg += cp[1]) & 0xff); 305 cp[2] = (unsigned char) ((cb += cp[2]) & 0xff); 306 cc -= 3; 307 cp += 3; 308 } 309 } else if (stride == 4) { 310 unsigned int cr = cp[0]; 311 unsigned int cg = cp[1]; 312 unsigned int cb = cp[2]; 313 unsigned int ca = cp[3]; 314 cc -= 4; 315 cp += 4; 316 while (cc>0) { 317 cp[0] = (unsigned char) ((cr += cp[0]) & 0xff); 318 cp[1] = (unsigned char) ((cg += cp[1]) & 0xff); 319 cp[2] = (unsigned char) ((cb += cp[2]) & 0xff); 320 cp[3] = (unsigned char) ((ca += cp[3]) & 0xff); 321 cc -= 4; 322 cp += 4; 323 } 324 } else { 325 cc -= stride; 326 do { 327 REPEAT4(stride, cp[stride] = 328 (unsigned char) ((cp[stride] + *cp) & 0xff); cp++) 329 cc -= stride; 330 } while (cc>0); 331 } 332 } 333 return 1; 334 } 335 336 static int 337 swabHorAcc16(TIFF* tif, uint8* cp0, tmsize_t cc) 338 { 339 uint16* wp = (uint16*) cp0; 340 tmsize_t wc = cc / 2; 341 342 TIFFSwabArrayOfShort(wp, wc); 343 return horAcc16(tif, cp0, cc); 344 } 345 346 TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW 347 static int 348 horAcc16(TIFF* tif, uint8* cp0, tmsize_t cc) 349 { 350 tmsize_t stride = PredictorState(tif)->stride; 351 uint16* wp = (uint16*) cp0; 352 tmsize_t wc = cc / 2; 353 354 if((cc%(2*stride))!=0) 355 { 356 TIFFErrorExt(tif->tif_clientdata, "horAcc16", 357 "%s", "cc%(2*stride))!=0"); 358 return 0; 359 } 360 361 if (wc > stride) { 362 wc -= stride; 363 do { 364 REPEAT4(stride, wp[stride] = (uint16)(((unsigned int)wp[stride] + (unsigned int)wp[0]) & 0xffff); wp++) 365 wc -= stride; 366 } while (wc > 0); 367 } 368 return 1; 369 } 370 371 static int 372 swabHorAcc32(TIFF* tif, uint8* cp0, tmsize_t cc) 373 { 374 uint32* wp = (uint32*) cp0; 375 tmsize_t wc = cc / 4; 376 377 TIFFSwabArrayOfLong(wp, wc); 378 return horAcc32(tif, cp0, cc); 379 } 380 381 TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW 382 static int 383 horAcc32(TIFF* tif, uint8* cp0, tmsize_t cc) 384 { 385 tmsize_t stride = PredictorState(tif)->stride; 386 uint32* wp = (uint32*) cp0; 387 tmsize_t wc = cc / 4; 388 389 if((cc%(4*stride))!=0) 390 { 391 TIFFErrorExt(tif->tif_clientdata, "horAcc32", 392 "%s", "cc%(4*stride))!=0"); 393 return 0; 394 } 395 396 if (wc > stride) { 397 wc -= stride; 398 do { 399 REPEAT4(stride, wp[stride] += wp[0]; wp++) 400 wc -= stride; 401 } while (wc > 0); 402 } 403 return 1; 404 } 405 406 /* 407 * Floating point predictor accumulation routine. 408 */ 409 static int 410 fpAcc(TIFF* tif, uint8* cp0, tmsize_t cc) 411 { 412 tmsize_t stride = PredictorState(tif)->stride; 413 uint32 bps = tif->tif_dir.td_bitspersample / 8; 414 tmsize_t wc = cc / bps; 415 tmsize_t count = cc; 416 uint8 *cp = (uint8 *) cp0; 417 uint8 *tmp; 418 419 if(cc%(bps*stride)!=0) 420 { 421 TIFFErrorExt(tif->tif_clientdata, "fpAcc", 422 "%s", "cc%(bps*stride))!=0"); 423 return 0; 424 } 425 426 tmp = (uint8 *)_TIFFmalloc(cc); 427 if (!tmp) 428 return 0; 429 430 while (count > stride) { 431 REPEAT4(stride, cp[stride] = 432 (unsigned char) ((cp[stride] + cp[0]) & 0xff); cp++) 433 count -= stride; 434 } 435 436 _TIFFmemcpy(tmp, cp0, cc); 437 cp = (uint8 *) cp0; 438 for (count = 0; count < wc; count++) { 439 uint32 byte; 440 for (byte = 0; byte < bps; byte++) { 441 #if WORDS_BIGENDIAN 442 cp[bps * count + byte] = tmp[byte * wc + count]; 443 #else 444 cp[bps * count + byte] = 445 tmp[(bps - byte - 1) * wc + count]; 446 #endif 447 } 448 } 449 _TIFFfree(tmp); 450 return 1; 451 } 452 453 /* 454 * Decode a scanline and apply the predictor routine. 455 */ 456 static int 457 PredictorDecodeRow(TIFF* tif, uint8* op0, tmsize_t occ0, uint16 s) 458 { 459 TIFFPredictorState *sp = PredictorState(tif); 460 461 assert(sp != NULL); 462 assert(sp->decoderow != NULL); 463 assert(sp->decodepfunc != NULL); 464 465 if ((*sp->decoderow)(tif, op0, occ0, s)) { 466 return (*sp->decodepfunc)(tif, op0, occ0); 467 } else 468 return 0; 469 } 470 471 /* 472 * Decode a tile/strip and apply the predictor routine. 473 * Note that horizontal differencing must be done on a 474 * row-by-row basis. The width of a "row" has already 475 * been calculated at pre-decode time according to the 476 * strip/tile dimensions. 477 */ 478 static int 479 PredictorDecodeTile(TIFF* tif, uint8* op0, tmsize_t occ0, uint16 s) 480 { 481 TIFFPredictorState *sp = PredictorState(tif); 482 483 assert(sp != NULL); 484 assert(sp->decodetile != NULL); 485 486 if ((*sp->decodetile)(tif, op0, occ0, s)) { 487 tmsize_t rowsize = sp->rowsize; 488 assert(rowsize > 0); 489 if((occ0%rowsize) !=0) 490 { 491 TIFFErrorExt(tif->tif_clientdata, "PredictorDecodeTile", 492 "%s", "occ0%rowsize != 0"); 493 return 0; 494 } 495 assert(sp->decodepfunc != NULL); 496 while (occ0 > 0) { 497 if( !(*sp->decodepfunc)(tif, op0, rowsize) ) 498 return 0; 499 occ0 -= rowsize; 500 op0 += rowsize; 501 } 502 return 1; 503 } else 504 return 0; 505 } 506 507 TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW 508 static int 509 horDiff8(TIFF* tif, uint8* cp0, tmsize_t cc) 510 { 511 TIFFPredictorState* sp = PredictorState(tif); 512 tmsize_t stride = sp->stride; 513 unsigned char* cp = (unsigned char*) cp0; 514 515 if((cc%stride)!=0) 516 { 517 TIFFErrorExt(tif->tif_clientdata, "horDiff8", 518 "%s", "(cc%stride)!=0"); 519 return 0; 520 } 521 522 if (cc > stride) { 523 cc -= stride; 524 /* 525 * Pipeline the most common cases. 526 */ 527 if (stride == 3) { 528 unsigned int r1, g1, b1; 529 unsigned int r2 = cp[0]; 530 unsigned int g2 = cp[1]; 531 unsigned int b2 = cp[2]; 532 do { 533 r1 = cp[3]; cp[3] = (unsigned char)((r1-r2)&0xff); r2 = r1; 534 g1 = cp[4]; cp[4] = (unsigned char)((g1-g2)&0xff); g2 = g1; 535 b1 = cp[5]; cp[5] = (unsigned char)((b1-b2)&0xff); b2 = b1; 536 cp += 3; 537 } while ((cc -= 3) > 0); 538 } else if (stride == 4) { 539 unsigned int r1, g1, b1, a1; 540 unsigned int r2 = cp[0]; 541 unsigned int g2 = cp[1]; 542 unsigned int b2 = cp[2]; 543 unsigned int a2 = cp[3]; 544 do { 545 r1 = cp[4]; cp[4] = (unsigned char)((r1-r2)&0xff); r2 = r1; 546 g1 = cp[5]; cp[5] = (unsigned char)((g1-g2)&0xff); g2 = g1; 547 b1 = cp[6]; cp[6] = (unsigned char)((b1-b2)&0xff); b2 = b1; 548 a1 = cp[7]; cp[7] = (unsigned char)((a1-a2)&0xff); a2 = a1; 549 cp += 4; 550 } while ((cc -= 4) > 0); 551 } else { 552 cp += cc - 1; 553 do { 554 REPEAT4(stride, cp[stride] = (unsigned char)((cp[stride] - cp[0])&0xff); cp--) 555 } while ((cc -= stride) > 0); 556 } 557 } 558 return 1; 559 } 560 561 TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW 562 static int 563 horDiff16(TIFF* tif, uint8* cp0, tmsize_t cc) 564 { 565 TIFFPredictorState* sp = PredictorState(tif); 566 tmsize_t stride = sp->stride; 567 uint16 *wp = (uint16*) cp0; 568 tmsize_t wc = cc/2; 569 570 if((cc%(2*stride))!=0) 571 { 572 TIFFErrorExt(tif->tif_clientdata, "horDiff8", 573 "%s", "(cc%(2*stride))!=0"); 574 return 0; 575 } 576 577 if (wc > stride) { 578 wc -= stride; 579 wp += wc - 1; 580 do { 581 REPEAT4(stride, wp[stride] = (uint16)(((unsigned int)wp[stride] - (unsigned int)wp[0]) & 0xffff); wp--) 582 wc -= stride; 583 } while (wc > 0); 584 } 585 return 1; 586 } 587 588 static int 589 swabHorDiff16(TIFF* tif, uint8* cp0, tmsize_t cc) 590 { 591 uint16* wp = (uint16*) cp0; 592 tmsize_t wc = cc / 2; 593 594 if( !horDiff16(tif, cp0, cc) ) 595 return 0; 596 597 TIFFSwabArrayOfShort(wp, wc); 598 return 1; 599 } 600 601 TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW 602 static int 603 horDiff32(TIFF* tif, uint8* cp0, tmsize_t cc) 604 { 605 TIFFPredictorState* sp = PredictorState(tif); 606 tmsize_t stride = sp->stride; 607 uint32 *wp = (uint32*) cp0; 608 tmsize_t wc = cc/4; 609 610 if((cc%(4*stride))!=0) 611 { 612 TIFFErrorExt(tif->tif_clientdata, "horDiff32", 613 "%s", "(cc%(4*stride))!=0"); 614 return 0; 615 } 616 617 if (wc > stride) { 618 wc -= stride; 619 wp += wc - 1; 620 do { 621 REPEAT4(stride, wp[stride] -= wp[0]; wp--) 622 wc -= stride; 623 } while (wc > 0); 624 } 625 return 1; 626 } 627 628 static int 629 swabHorDiff32(TIFF* tif, uint8* cp0, tmsize_t cc) 630 { 631 uint32* wp = (uint32*) cp0; 632 tmsize_t wc = cc / 4; 633 634 if( !horDiff32(tif, cp0, cc) ) 635 return 0; 636 637 TIFFSwabArrayOfLong(wp, wc); 638 return 1; 639 } 640 641 /* 642 * Floating point predictor differencing routine. 643 */ 644 TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW 645 static int 646 fpDiff(TIFF* tif, uint8* cp0, tmsize_t cc) 647 { 648 tmsize_t stride = PredictorState(tif)->stride; 649 uint32 bps = tif->tif_dir.td_bitspersample / 8; 650 tmsize_t wc = cc / bps; 651 tmsize_t count; 652 uint8 *cp = (uint8 *) cp0; 653 uint8 *tmp; 654 655 if((cc%(bps*stride))!=0) 656 { 657 TIFFErrorExt(tif->tif_clientdata, "fpDiff", 658 "%s", "(cc%(bps*stride))!=0"); 659 return 0; 660 } 661 662 tmp = (uint8 *)_TIFFmalloc(cc); 663 if (!tmp) 664 return 0; 665 666 _TIFFmemcpy(tmp, cp0, cc); 667 for (count = 0; count < wc; count++) { 668 uint32 byte; 669 for (byte = 0; byte < bps; byte++) { 670 #if WORDS_BIGENDIAN 671 cp[byte * wc + count] = tmp[bps * count + byte]; 672 #else 673 cp[(bps - byte - 1) * wc + count] = 674 tmp[bps * count + byte]; 675 #endif 676 } 677 } 678 _TIFFfree(tmp); 679 680 cp = (uint8 *) cp0; 681 cp += cc - stride - 1; 682 for (count = cc; count > stride; count -= stride) 683 REPEAT4(stride, cp[stride] = (unsigned char)((cp[stride] - cp[0])&0xff); cp--) 684 return 1; 685 } 686 687 static int 688 PredictorEncodeRow(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s) 689 { 690 TIFFPredictorState *sp = PredictorState(tif); 691 692 assert(sp != NULL); 693 assert(sp->encodepfunc != NULL); 694 assert(sp->encoderow != NULL); 695 696 /* XXX horizontal differencing alters user's data XXX */ 697 if( !(*sp->encodepfunc)(tif, bp, cc) ) 698 return 0; 699 return (*sp->encoderow)(tif, bp, cc, s); 700 } 701 702 static int 703 PredictorEncodeTile(TIFF* tif, uint8* bp0, tmsize_t cc0, uint16 s) 704 { 705 static const char module[] = "PredictorEncodeTile"; 706 TIFFPredictorState *sp = PredictorState(tif); 707 uint8 *working_copy; 708 tmsize_t cc = cc0, rowsize; 709 unsigned char* bp; 710 int result_code; 711 712 assert(sp != NULL); 713 assert(sp->encodepfunc != NULL); 714 assert(sp->encodetile != NULL); 715 716 /* 717 * Do predictor manipulation in a working buffer to avoid altering 718 * the callers buffer. http://trac.osgeo.org/gdal/ticket/1965 719 */ 720 working_copy = (uint8*) _TIFFmalloc(cc0); 721 if( working_copy == NULL ) 722 { 723 TIFFErrorExt(tif->tif_clientdata, module, 724 "Out of memory allocating " TIFF_SSIZE_FORMAT " byte temp buffer.", 725 cc0 ); 726 return 0; 727 } 728 memcpy( working_copy, bp0, cc0 ); 729 bp = working_copy; 730 731 rowsize = sp->rowsize; 732 assert(rowsize > 0); 733 if((cc0%rowsize)!=0) 734 { 735 TIFFErrorExt(tif->tif_clientdata, "PredictorEncodeTile", 736 "%s", "(cc0%rowsize)!=0"); 737 _TIFFfree( working_copy ); 738 return 0; 739 } 740 while (cc > 0) { 741 (*sp->encodepfunc)(tif, bp, rowsize); 742 cc -= rowsize; 743 bp += rowsize; 744 } 745 result_code = (*sp->encodetile)(tif, working_copy, cc0, s); 746 747 _TIFFfree( working_copy ); 748 749 return result_code; 750 } 751 752 #define FIELD_PREDICTOR (FIELD_CODEC+0) /* XXX */ 753 754 static const TIFFField predictFields[] = { 755 { TIFFTAG_PREDICTOR, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, TIFF_SETGET_UINT16, FIELD_PREDICTOR, FALSE, FALSE, "Predictor", NULL }, 756 }; 757 758 static int 759 PredictorVSetField(TIFF* tif, uint32 tag, va_list ap) 760 { 761 TIFFPredictorState *sp = PredictorState(tif); 762 763 assert(sp != NULL); 764 assert(sp->vsetparent != NULL); 765 766 switch (tag) { 767 case TIFFTAG_PREDICTOR: 768 sp->predictor = (uint16) va_arg(ap, uint16_vap); 769 TIFFSetFieldBit(tif, FIELD_PREDICTOR); 770 break; 771 default: 772 return (*sp->vsetparent)(tif, tag, ap); 773 } 774 tif->tif_flags |= TIFF_DIRTYDIRECT; 775 return 1; 776 } 777 778 static int 779 PredictorVGetField(TIFF* tif, uint32 tag, va_list ap) 780 { 781 TIFFPredictorState *sp = PredictorState(tif); 782 783 assert(sp != NULL); 784 assert(sp->vgetparent != NULL); 785 786 switch (tag) { 787 case TIFFTAG_PREDICTOR: 788 *va_arg(ap, uint16*) = (uint16)sp->predictor; 789 break; 790 default: 791 return (*sp->vgetparent)(tif, tag, ap); 792 } 793 return 1; 794 } 795 796 static void 797 PredictorPrintDir(TIFF* tif, FILE* fd, long flags) 798 { 799 TIFFPredictorState* sp = PredictorState(tif); 800 801 (void) flags; 802 if (TIFFFieldSet(tif,FIELD_PREDICTOR)) { 803 fprintf(fd, " Predictor: "); 804 switch (sp->predictor) { 805 case 1: fprintf(fd, "none "); break; 806 case 2: fprintf(fd, "horizontal differencing "); break; 807 case 3: fprintf(fd, "floating point predictor "); break; 808 } 809 fprintf(fd, "%d (0x%x)\n", sp->predictor, sp->predictor); 810 } 811 if (sp->printdir) 812 (*sp->printdir)(tif, fd, flags); 813 } 814 815 int 816 TIFFPredictorInit(TIFF* tif) 817 { 818 TIFFPredictorState* sp = PredictorState(tif); 819 820 assert(sp != 0); 821 822 /* 823 * Merge codec-specific tag information. 824 */ 825 if (!_TIFFMergeFields(tif, predictFields, 826 TIFFArrayCount(predictFields))) { 827 TIFFErrorExt(tif->tif_clientdata, "TIFFPredictorInit", 828 "Merging Predictor codec-specific tags failed"); 829 return 0; 830 } 831 832 /* 833 * Override parent get/set field methods. 834 */ 835 sp->vgetparent = tif->tif_tagmethods.vgetfield; 836 tif->tif_tagmethods.vgetfield = 837 PredictorVGetField;/* hook for predictor tag */ 838 sp->vsetparent = tif->tif_tagmethods.vsetfield; 839 tif->tif_tagmethods.vsetfield = 840 PredictorVSetField;/* hook for predictor tag */ 841 sp->printdir = tif->tif_tagmethods.printdir; 842 tif->tif_tagmethods.printdir = 843 PredictorPrintDir; /* hook for predictor tag */ 844 845 sp->setupdecode = tif->tif_setupdecode; 846 tif->tif_setupdecode = PredictorSetupDecode; 847 sp->setupencode = tif->tif_setupencode; 848 tif->tif_setupencode = PredictorSetupEncode; 849 850 sp->predictor = 1; /* default value */ 851 sp->encodepfunc = NULL; /* no predictor routine */ 852 sp->decodepfunc = NULL; /* no predictor routine */ 853 return 1; 854 } 855 856 int 857 TIFFPredictorCleanup(TIFF* tif) 858 { 859 TIFFPredictorState* sp = PredictorState(tif); 860 861 assert(sp != 0); 862 863 tif->tif_tagmethods.vgetfield = sp->vgetparent; 864 tif->tif_tagmethods.vsetfield = sp->vsetparent; 865 tif->tif_tagmethods.printdir = sp->printdir; 866 tif->tif_setupdecode = sp->setupdecode; 867 tif->tif_setupencode = sp->setupencode; 868 869 return 1; 870 } 871 872 /* vim: set ts=8 sts=8 sw=8 noet: */ 873 /* 874 * Local Variables: 875 * mode: c 876 * c-basic-offset: 8 877 * fill-column: 78 878 * End: 879 */ 880