4  * Copyright (c) 1988-1997 Sam Leffler 
   5  * Copyright (c) 1991-1997 Silicon Graphics, Inc. 
   7  * Permission to use, copy, modify, distribute, and sell this software and  
   8  * its documentation for any purpose is hereby granted without fee, provided 
   9  * that (i) the above copyright notices and this permission notice appear in 
  10  * all copies of the software and related documentation, and (ii) the names of 
  11  * Sam Leffler and Silicon Graphics may not be used in any advertising or 
  12  * publicity relating to the software without the specific, prior written 
  13  * permission of Sam Leffler and Silicon Graphics. 
  15  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,  
  16  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY  
  17  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.   
  19  * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR 
  20  * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, 
  21  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, 
  22  * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF  
  23  * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE  
  30  * Predictor Tag Support (used by multiple codecs). 
  33 #include "tif_predict.h" 
  35 #define PredictorState(tif)     ((TIFFPredictorState*) (tif)->tif_data) 
  37 static  void horAcc8(TIFF
*, tidata_t
, tsize_t
); 
  38 static  void horAcc16(TIFF
*, tidata_t
, tsize_t
); 
  39 static  void swabHorAcc16(TIFF
*, tidata_t
, tsize_t
); 
  40 static  void horDiff8(TIFF
*, tidata_t
, tsize_t
); 
  41 static  void horDiff16(TIFF
*, tidata_t
, tsize_t
); 
  42 static  void fpAcc(TIFF
*, tidata_t
, tsize_t
); 
  43 static  void fpDiff(TIFF
*, tidata_t
, tsize_t
); 
  44 static  int PredictorDecodeRow(TIFF
*, tidata_t
, tsize_t
, tsample_t
); 
  45 static  int PredictorDecodeTile(TIFF
*, tidata_t
, tsize_t
, tsample_t
); 
  46 static  int PredictorEncodeRow(TIFF
*, tidata_t
, tsize_t
, tsample_t
); 
  47 static  int PredictorEncodeTile(TIFF
*, tidata_t
, tsize_t
, tsample_t
); 
  50 PredictorSetup(TIFF
* tif
) 
  52         static const char module[] = "PredictorSetup"; 
  54         TIFFPredictorState
* sp 
= PredictorState(tif
); 
  55         TIFFDirectory
* td 
= &tif
->tif_dir
; 
  57         switch (sp
->predictor
)          /* no differencing */ 
  61                 case PREDICTOR_HORIZONTAL
: 
  62                         if (td
->td_bitspersample 
!= 8 
  63                             && td
->td_bitspersample 
!= 16) { 
  64                                 TIFFErrorExt(tif
->tif_clientdata
, module, 
  65     "Horizontal differencing \"Predictor\" not supported with %d-bit samples", 
  66                                           td
->td_bitspersample
); 
  70                 case PREDICTOR_FLOATINGPOINT
: 
  71                         if (td
->td_sampleformat 
!= SAMPLEFORMAT_IEEEFP
) { 
  72                                 TIFFErrorExt(tif
->tif_clientdata
, module, 
  73         "Floating point \"Predictor\" not supported with %d data format", 
  79                         TIFFErrorExt(tif
->tif_clientdata
, module, 
  80                                   "\"Predictor\" value %d not supported", 
  84         sp
->stride 
= (td
->td_planarconfig 
== PLANARCONFIG_CONTIG 
? 
  85             td
->td_samplesperpixel 
: 1); 
  87          * Calculate the scanline/tile-width size in bytes. 
  90                 sp
->rowsize 
= TIFFTileRowSize(tif
); 
  92                 sp
->rowsize 
= TIFFScanlineSize(tif
); 
  98 PredictorSetupDecode(TIFF
* tif
) 
 100         TIFFPredictorState
* sp 
= PredictorState(tif
); 
 101         TIFFDirectory
* td 
= &tif
->tif_dir
; 
 103         if (!(*sp
->setupdecode
)(tif
) || !PredictorSetup(tif
)) 
 106         if (sp
->predictor 
== 2) { 
 107                 switch (td
->td_bitspersample
) { 
 108                         case 8:  sp
->pfunc 
= horAcc8
; break; 
 109                         case 16: sp
->pfunc 
= horAcc16
; break; 
 112                  * Override default decoding method with one that does the 
 115                 sp
->coderow 
= tif
->tif_decoderow
; 
 116                 tif
->tif_decoderow 
= PredictorDecodeRow
; 
 117                 sp
->codestrip 
= tif
->tif_decodestrip
; 
 118                 tif
->tif_decodestrip 
= PredictorDecodeTile
; 
 119                 sp
->codetile 
= tif
->tif_decodetile
; 
 120                 tif
->tif_decodetile 
= PredictorDecodeTile
; 
 122                  * If the data is horizontally differenced 16-bit data that 
 123                  * requires byte-swapping, then it must be byte swapped before 
 124                  * the accumulation step.  We do this with a special-purpose 
 125                  * routine and override the normal post decoding logic that 
 126                  * the library setup when the directory was read. 
 128                 if (tif
->tif_flags 
& TIFF_SWAB
) { 
 129                         if (sp
->pfunc 
== horAcc16
) { 
 130                                 sp
->pfunc 
= swabHorAcc16
; 
 131                                 tif
->tif_postdecode 
= _TIFFNoPostDecode
; 
 132                         } /* else handle 32-bit case... */ 
 136         else if (sp
->predictor 
== 3) { 
 139                  * Override default decoding method with one that does the 
 142                 sp
->coderow 
= tif
->tif_decoderow
; 
 143                 tif
->tif_decoderow 
= PredictorDecodeRow
; 
 144                 sp
->codestrip 
= tif
->tif_decodestrip
; 
 145                 tif
->tif_decodestrip 
= PredictorDecodeTile
; 
 146                 sp
->codetile 
= tif
->tif_decodetile
; 
 147                 tif
->tif_decodetile 
= PredictorDecodeTile
; 
 149                  * The data should not be swapped outside of the floating 
 150                  * point predictor, the accumulation routine should return 
 151                  * byres in the native order. 
 153                 if (tif
->tif_flags 
& TIFF_SWAB
) { 
 154                         tif
->tif_postdecode 
= _TIFFNoPostDecode
; 
 157                  * Allocate buffer to keep the decoded bytes before 
 158                  * rearranging in the ight order 
 166 PredictorSetupEncode(TIFF
* tif
) 
 168         TIFFPredictorState
* sp 
= PredictorState(tif
); 
 169         TIFFDirectory
* td 
= &tif
->tif_dir
; 
 171         if (!(*sp
->setupencode
)(tif
) || !PredictorSetup(tif
)) 
 174         if (sp
->predictor 
== 2) { 
 175                 switch (td
->td_bitspersample
) { 
 176                         case 8:  sp
->pfunc 
= horDiff8
; break; 
 177                         case 16: sp
->pfunc 
= horDiff16
; break; 
 180                  * Override default encoding method with one that does the 
 183                 sp
->coderow 
= tif
->tif_encoderow
; 
 184                 tif
->tif_encoderow 
= PredictorEncodeRow
; 
 185                 sp
->codestrip 
= tif
->tif_encodestrip
; 
 186                 tif
->tif_encodestrip 
= PredictorEncodeTile
; 
 187                 sp
->codetile 
= tif
->tif_encodetile
; 
 188                 tif
->tif_encodetile 
= PredictorEncodeTile
; 
 191         else if (sp
->predictor 
== 3) { 
 194                  * Override default encoding method with one that does the 
 197                 sp
->coderow 
= tif
->tif_encoderow
; 
 198                 tif
->tif_encoderow 
= PredictorEncodeRow
; 
 199                 sp
->codestrip 
= tif
->tif_encodestrip
; 
 200                 tif
->tif_encodestrip 
= PredictorEncodeTile
; 
 201                 sp
->codetile 
= tif
->tif_encodetile
; 
 202                 tif
->tif_encodetile 
= PredictorEncodeTile
; 
 208 #define REPEAT4(n, op)          \ 
 210     default: { int i; for (i = n-4; i > 0; i--) { op; } } \ 
 219 horAcc8(TIFF
* tif
, tidata_t cp0
, tsize_t cc
) 
 221         tsize_t stride 
= PredictorState(tif
)->stride
; 
 223         char* cp 
= (char*) cp0
; 
 227                  * Pipeline the most common cases. 
 230                         unsigned int cr 
= cp
[0]; 
 231                         unsigned int cg 
= cp
[1]; 
 232                         unsigned int cb 
= cp
[2]; 
 235                                 cp
[0] = (char) (cr 
+= cp
[0]); 
 236                                 cp
[1] = (char) (cg 
+= cp
[1]); 
 237                                 cp
[2] = (char) (cb 
+= cp
[2]); 
 238                         } while ((int32
) cc 
> 0); 
 239                 } else if (stride 
== 4)  { 
 240                         unsigned int cr 
= cp
[0]; 
 241                         unsigned int cg 
= cp
[1]; 
 242                         unsigned int cb 
= cp
[2]; 
 243                         unsigned int ca 
= cp
[3]; 
 246                                 cp
[0] = (char) (cr 
+= cp
[0]); 
 247                                 cp
[1] = (char) (cg 
+= cp
[1]); 
 248                                 cp
[2] = (char) (cb 
+= cp
[2]); 
 249                                 cp
[3] = (char) (ca 
+= cp
[3]); 
 250                         } while ((int32
) cc 
> 0); 
 253                                 REPEAT4(stride
, cp
[stride
] = 
 254                                         (char) (cp
[stride
] + *cp
); cp
++) 
 256                         } while ((int32
) cc 
> 0); 
 262 swabHorAcc16(TIFF
* tif
, tidata_t cp0
, tsize_t cc
) 
 264         tsize_t stride 
= PredictorState(tif
)->stride
; 
 265         uint16
* wp 
= (uint16
*) cp0
; 
 269                 TIFFSwabArrayOfShort(wp
, wc
); 
 272                         REPEAT4(stride
, wp
[stride
] += wp
[0]; wp
++) 
 274                 } while ((int32
) wc 
> 0); 
 279 horAcc16(TIFF
* tif
, tidata_t cp0
, tsize_t cc
) 
 281         tsize_t stride 
= PredictorState(tif
)->stride
; 
 282         uint16
* wp 
= (uint16
*) cp0
; 
 288                         REPEAT4(stride
, wp
[stride
] += wp
[0]; wp
++) 
 290                 } while ((int32
) wc 
> 0); 
 295  * Floating point predictor accumulation routine. 
 298 fpAcc(TIFF
* tif
, tidata_t cp0
, tsize_t cc
) 
 300         tsize_t stride 
= PredictorState(tif
)->stride
; 
 301         uint32 bps 
= tif
->tif_dir
.td_bitspersample 
/ 8; 
 302         tsize_t wc 
= cc 
/ bps
; 
 304         uint8 
*cp 
= (uint8 
*) cp0
; 
 305         uint8 
*tmp 
= (uint8 
*)_TIFFmalloc(cc
); 
 310         while (count 
> stride
) { 
 311                 REPEAT4(stride
, cp
[stride
] += cp
[0]; cp
++) 
 315         _TIFFmemcpy(tmp
, cp0
, cc
); 
 317         for (count 
= 0; count 
< wc
; count
++) { 
 319                 for (byte 
= 0; byte 
< bps
; byte
++) { 
 320 #ifdef WORDS_BIGENDIAN 
 321                         cp
[bps 
* count 
+ byte
] = tmp
[byte 
* wc 
+ count
]; 
 323                         cp
[bps 
* count 
+ byte
] = 
 324                                 tmp
[(bps 
- byte 
- 1) * wc 
+ count
]; 
 332  * Decode a scanline and apply the predictor routine. 
 335 PredictorDecodeRow(TIFF
* tif
, tidata_t op0
, tsize_t occ0
, tsample_t s
) 
 337         TIFFPredictorState 
*sp 
= PredictorState(tif
); 
 340         assert(sp
->coderow 
!= NULL
); 
 341         assert(sp
->pfunc 
!= NULL
); 
 343         if ((*sp
->coderow
)(tif
, op0
, occ0
, s
)) { 
 344                 (*sp
->pfunc
)(tif
, op0
, occ0
); 
 351  * Decode a tile/strip and apply the predictor routine. 
 352  * Note that horizontal differencing must be done on a 
 353  * row-by-row basis.  The width of a "row" has already 
 354  * been calculated at pre-decode time according to the 
 355  * strip/tile dimensions. 
 358 PredictorDecodeTile(TIFF
* tif
, tidata_t op0
, tsize_t occ0
, tsample_t s
) 
 360         TIFFPredictorState 
*sp 
= PredictorState(tif
); 
 363         assert(sp
->codetile 
!= NULL
); 
 365         if ((*sp
->codetile
)(tif
, op0
, occ0
, s
)) { 
 366                 tsize_t rowsize 
= sp
->rowsize
; 
 368                 assert(sp
->pfunc 
!= NULL
); 
 369                 while ((long)occ0 
> 0) { 
 370                         (*sp
->pfunc
)(tif
, op0
, (tsize_t
) rowsize
); 
 380 horDiff8(TIFF
* tif
, tidata_t cp0
, tsize_t cc
) 
 382         TIFFPredictorState
* sp 
= PredictorState(tif
); 
 383         tsize_t stride 
= sp
->stride
; 
 384         char* cp 
= (char*) cp0
; 
 389                  * Pipeline the most common cases. 
 397                                 r1 
= cp
[3]; cp
[3] = r1
-r2
; r2 
= r1
; 
 398                                 g1 
= cp
[4]; cp
[4] = g1
-g2
; g2 
= g1
; 
 399                                 b1 
= cp
[5]; cp
[5] = b1
-b2
; b2 
= b1
; 
 401                         } while ((int32
)(cc 
-= 3) > 0); 
 402                 } else if (stride 
== 4) { 
 409                                 r1 
= cp
[4]; cp
[4] = r1
-r2
; r2 
= r1
; 
 410                                 g1 
= cp
[5]; cp
[5] = g1
-g2
; g2 
= g1
; 
 411                                 b1 
= cp
[6]; cp
[6] = b1
-b2
; b2 
= b1
; 
 412                                 a1 
= cp
[7]; cp
[7] = a1
-a2
; a2 
= a1
; 
 414                         } while ((int32
)(cc 
-= 4) > 0); 
 418                                 REPEAT4(stride
, cp
[stride
] -= cp
[0]; cp
--) 
 419                         } while ((int32
)(cc 
-= stride
) > 0); 
 425 horDiff16(TIFF
* tif
, tidata_t cp0
, tsize_t cc
) 
 427         TIFFPredictorState
* sp 
= PredictorState(tif
); 
 428         tsize_t stride 
= sp
->stride
; 
 429         int16 
*wp 
= (int16
*) cp0
; 
 436                         REPEAT4(stride
, wp
[stride
] -= wp
[0]; wp
--) 
 438                 } while ((int32
) wc 
> 0); 
 443  * Floating point predictor differencing routine. 
 446 fpDiff(TIFF
* tif
, tidata_t cp0
, tsize_t cc
) 
 448         tsize_t stride 
= PredictorState(tif
)->stride
; 
 449         uint32 bps 
= tif
->tif_dir
.td_bitspersample 
/ 8; 
 450         tsize_t wc 
= cc 
/ bps
; 
 452         uint8 
*cp 
= (uint8 
*) cp0
; 
 453         uint8 
*tmp 
= (uint8 
*)_TIFFmalloc(cc
); 
 458         _TIFFmemcpy(tmp
, cp0
, cc
); 
 459         for (count 
= 0; count 
< wc
; count
++) { 
 461                 for (byte 
= 0; byte 
< bps
; byte
++) { 
 462 #ifdef WORDS_BIGENDIAN 
 463                         cp
[byte 
* wc 
+ count
] = tmp
[bps 
* count 
+ byte
]; 
 465                         cp
[(bps 
- byte 
- 1) * wc 
+ count
] = 
 466                                 tmp
[bps 
* count 
+ byte
]; 
 473         cp 
+= cc 
- stride 
- 1; 
 474         for (count 
= cc
; count 
> stride
; count 
-= stride
) 
 475                 REPEAT4(stride
, cp
[stride
] -= cp
[0]; cp
--) 
 479 PredictorEncodeRow(TIFF
* tif
, tidata_t bp
, tsize_t cc
, tsample_t s
) 
 481         TIFFPredictorState 
*sp 
= PredictorState(tif
); 
 484         assert(sp
->pfunc 
!= NULL
); 
 485         assert(sp
->coderow 
!= NULL
); 
 487         /* XXX horizontal differencing alters user's data XXX */ 
 488         (*sp
->pfunc
)(tif
, bp
, cc
); 
 489         return (*sp
->coderow
)(tif
, bp
, cc
, s
); 
 493 PredictorEncodeTile(TIFF
* tif
, tidata_t bp0
, tsize_t cc0
, tsample_t s
) 
 495         TIFFPredictorState 
*sp 
= PredictorState(tif
); 
 496         tsize_t cc 
= cc0
, rowsize
; 
 497         unsigned char* bp 
= bp0
; 
 500         assert(sp
->pfunc 
!= NULL
); 
 501         assert(sp
->codetile 
!= NULL
); 
 503         rowsize 
= sp
->rowsize
; 
 505         while ((long)cc 
> 0) { 
 506                 (*sp
->pfunc
)(tif
, bp
, (tsize_t
) rowsize
); 
 510         return (*sp
->codetile
)(tif
, bp0
, cc0
, s
); 
 513 #define FIELD_PREDICTOR (FIELD_CODEC+0)         /* XXX */ 
 515 static const TIFFFieldInfo predictFieldInfo
[] = { 
 516     { TIFFTAG_PREDICTOR
,         1, 1, TIFF_SHORT
,      FIELD_PREDICTOR
, 
 517       FALSE
,    FALSE
,  "Predictor" }, 
 519 #define N(a)    (sizeof (a) / sizeof (a[0])) 
 522 PredictorVSetField(TIFF
* tif
, ttag_t tag
, va_list ap
) 
 524         TIFFPredictorState 
*sp 
= PredictorState(tif
); 
 527         assert(sp
->vsetparent 
!= NULL
); 
 530         case TIFFTAG_PREDICTOR
: 
 531                 sp
->predictor 
= (uint16
) va_arg(ap
, int); 
 532                 TIFFSetFieldBit(tif
, FIELD_PREDICTOR
); 
 535                 return (*sp
->vsetparent
)(tif
, tag
, ap
); 
 537         tif
->tif_flags 
|= TIFF_DIRTYDIRECT
; 
 542 PredictorVGetField(TIFF
* tif
, ttag_t tag
, va_list ap
) 
 544         TIFFPredictorState 
*sp 
= PredictorState(tif
); 
 547         assert(sp
->vgetparent 
!= NULL
); 
 550         case TIFFTAG_PREDICTOR
: 
 551                 *va_arg(ap
, uint16
*) = sp
->predictor
; 
 554                 return (*sp
->vgetparent
)(tif
, tag
, ap
); 
 560 PredictorPrintDir(TIFF
* tif
, FILE* fd
, long flags
) 
 562         TIFFPredictorState
* sp 
= PredictorState(tif
); 
 565         if (TIFFFieldSet(tif
,FIELD_PREDICTOR
)) { 
 566                 fprintf(fd
, "  Predictor: "); 
 567                 switch (sp
->predictor
) { 
 568                 case 1: fprintf(fd
, "none "); break; 
 569                 case 2: fprintf(fd
, "horizontal differencing "); break; 
 570                 case 3: fprintf(fd
, "floating point predictor "); break; 
 572                 fprintf(fd
, "%u (0x%x)\n", sp
->predictor
, sp
->predictor
); 
 575                 (*sp
->printdir
)(tif
, fd
, flags
); 
 579 TIFFPredictorInit(TIFF
* tif
) 
 581         TIFFPredictorState
* sp 
= PredictorState(tif
); 
 586          * Merge codec-specific tag information and 
 587          * override parent get/set field methods. 
 589         _TIFFMergeFieldInfo(tif
, predictFieldInfo
, N(predictFieldInfo
)); 
 590         sp
->vgetparent 
= tif
->tif_tagmethods
.vgetfield
; 
 591         tif
->tif_tagmethods
.vgetfield 
= 
 592             PredictorVGetField
;/* hook for predictor tag */ 
 593         sp
->vsetparent 
= tif
->tif_tagmethods
.vsetfield
; 
 594         tif
->tif_tagmethods
.vsetfield 
= 
 595             PredictorVSetField
;/* hook for predictor tag */ 
 596         sp
->printdir 
= tif
->tif_tagmethods
.printdir
; 
 597         tif
->tif_tagmethods
.printdir 
= 
 598             PredictorPrintDir
;  /* hook for predictor tag */ 
 600         sp
->setupdecode 
= tif
->tif_setupdecode
; 
 601         tif
->tif_setupdecode 
= PredictorSetupDecode
; 
 602         sp
->setupencode 
= tif
->tif_setupencode
; 
 603         tif
->tif_setupencode 
= PredictorSetupEncode
; 
 605         sp
->predictor 
= 1;                      /* default value */ 
 606         sp
->pfunc 
= NULL
;                       /* no predictor routine */ 
 611 TIFFPredictorCleanup(TIFF
* tif
) 
 613         TIFFPredictorState
* sp 
= PredictorState(tif
); 
 617         tif
->tif_tagmethods
.vgetfield 
= sp
->vgetparent
; 
 618         tif
->tif_tagmethods
.vsetfield 
= sp
->vsetparent
; 
 619         tif
->tif_tagmethods
.printdir 
= sp
->printdir
; 
 620         tif
->tif_setupdecode 
= sp
->setupdecode
; 
 621         tif
->tif_setupencode 
= sp
->setupencode
; 
 626 /* vim: set ts=8 sts=8 sw=8 noet: */