LCOV - code coverage report
Current view: top level - frmts/gtiff/libtiff - tif_predict.c (source / functions) Hit Total Coverage
Test: gdal_filtered.info Lines: 404 502 80.5 %
Date: 2024-05-04 12:52:34 Functions: 23 28 82.1 %

          Line data    Source code
       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 "tif_predict.h"
      31             : #include "tiffiop.h"
      32             : 
      33             : #define PredictorState(tif) ((TIFFPredictorState *)(tif)->tif_data)
      34             : 
      35             : static int horAcc8(TIFF *tif, uint8_t *cp0, tmsize_t cc);
      36             : static int horAcc16(TIFF *tif, uint8_t *cp0, tmsize_t cc);
      37             : static int horAcc32(TIFF *tif, uint8_t *cp0, tmsize_t cc);
      38             : static int horAcc64(TIFF *tif, uint8_t *cp0, tmsize_t cc);
      39             : static int swabHorAcc16(TIFF *tif, uint8_t *cp0, tmsize_t cc);
      40             : static int swabHorAcc32(TIFF *tif, uint8_t *cp0, tmsize_t cc);
      41             : static int swabHorAcc64(TIFF *tif, uint8_t *cp0, tmsize_t cc);
      42             : static int horDiff8(TIFF *tif, uint8_t *cp0, tmsize_t cc);
      43             : static int horDiff16(TIFF *tif, uint8_t *cp0, tmsize_t cc);
      44             : static int horDiff32(TIFF *tif, uint8_t *cp0, tmsize_t cc);
      45             : static int horDiff64(TIFF *tif, uint8_t *cp0, tmsize_t cc);
      46             : static int swabHorDiff16(TIFF *tif, uint8_t *cp0, tmsize_t cc);
      47             : static int swabHorDiff32(TIFF *tif, uint8_t *cp0, tmsize_t cc);
      48             : static int swabHorDiff64(TIFF *tif, uint8_t *cp0, tmsize_t cc);
      49             : static int fpAcc(TIFF *tif, uint8_t *cp0, tmsize_t cc);
      50             : static int fpDiff(TIFF *tif, uint8_t *cp0, tmsize_t cc);
      51             : static int PredictorDecodeRow(TIFF *tif, uint8_t *op0, tmsize_t occ0,
      52             :                               uint16_t s);
      53             : static int PredictorDecodeTile(TIFF *tif, uint8_t *op0, tmsize_t occ0,
      54             :                                uint16_t s);
      55             : static int PredictorEncodeRow(TIFF *tif, uint8_t *bp, tmsize_t cc, uint16_t s);
      56             : static int PredictorEncodeTile(TIFF *tif, uint8_t *bp0, tmsize_t cc0,
      57             :                                uint16_t s);
      58             : 
      59       20011 : static int PredictorSetup(TIFF *tif)
      60             : {
      61             :     static const char module[] = "PredictorSetup";
      62             : 
      63       20011 :     TIFFPredictorState *sp = PredictorState(tif);
      64       20011 :     TIFFDirectory *td = &tif->tif_dir;
      65             : 
      66       20011 :     switch (sp->predictor) /* no differencing */
      67             :     {
      68       19626 :         case PREDICTOR_NONE:
      69       19626 :             return 1;
      70         378 :         case PREDICTOR_HORIZONTAL:
      71         378 :             if (td->td_bitspersample != 8 && td->td_bitspersample != 16 &&
      72           6 :                 td->td_bitspersample != 32 && td->td_bitspersample != 64)
      73             :             {
      74           0 :                 TIFFErrorExtR(tif, module,
      75             :                               "Horizontal differencing \"Predictor\" not "
      76             :                               "supported with %" PRIu16 "-bit samples",
      77           0 :                               td->td_bitspersample);
      78           0 :                 return 0;
      79             :             }
      80         378 :             break;
      81           6 :         case PREDICTOR_FLOATINGPOINT:
      82           6 :             if (td->td_sampleformat != SAMPLEFORMAT_IEEEFP)
      83             :             {
      84           0 :                 TIFFErrorExtR(
      85             :                     tif, module,
      86             :                     "Floating point \"Predictor\" not supported with %" PRIu16
      87             :                     " data format",
      88           0 :                     td->td_sampleformat);
      89           0 :                 return 0;
      90             :             }
      91           6 :             if (td->td_bitspersample != 16 && td->td_bitspersample != 24 &&
      92           6 :                 td->td_bitspersample != 32 && td->td_bitspersample != 64)
      93             :             { /* Should 64 be allowed? */
      94           0 :                 TIFFErrorExtR(
      95             :                     tif, module,
      96             :                     "Floating point \"Predictor\" not supported with %" PRIu16
      97             :                     "-bit samples",
      98           0 :                     td->td_bitspersample);
      99           0 :                 return 0;
     100             :             }
     101           6 :             break;
     102           1 :         default:
     103           1 :             TIFFErrorExtR(tif, module, "\"Predictor\" value %d not supported",
     104             :                           sp->predictor);
     105           1 :             return 0;
     106             :     }
     107         384 :     sp->stride =
     108         768 :         (td->td_planarconfig == PLANARCONFIG_CONTIG ? td->td_samplesperpixel
     109         384 :                                                     : 1);
     110             :     /*
     111             :      * Calculate the scanline/tile-width size in bytes.
     112             :      */
     113         384 :     if (isTiled(tif))
     114          20 :         sp->rowsize = TIFFTileRowSize(tif);
     115             :     else
     116         364 :         sp->rowsize = TIFFScanlineSize(tif);
     117         383 :     if (sp->rowsize == 0)
     118           0 :         return 0;
     119             : 
     120         383 :     return 1;
     121             : }
     122             : 
     123        3015 : static int PredictorSetupDecode(TIFF *tif)
     124             : {
     125        3015 :     TIFFPredictorState *sp = PredictorState(tif);
     126        3015 :     TIFFDirectory *td = &tif->tif_dir;
     127             : 
     128             :     /* Note: when PredictorSetup() fails, the effets of setupdecode() */
     129             :     /* will not be "canceled" so setupdecode() might be robust to */
     130             :     /* be called several times. */
     131        3015 :     if (!(*sp->setupdecode)(tif) || !PredictorSetup(tif))
     132           1 :         return 0;
     133             : 
     134        3011 :     if (sp->predictor == 2)
     135             :     {
     136          93 :         switch (td->td_bitspersample)
     137             :         {
     138          73 :             case 8:
     139          73 :                 sp->decodepfunc = horAcc8;
     140          73 :                 break;
     141          17 :             case 16:
     142          17 :                 sp->decodepfunc = horAcc16;
     143          17 :                 break;
     144           2 :             case 32:
     145           2 :                 sp->decodepfunc = horAcc32;
     146           2 :                 break;
     147           1 :             case 64:
     148           1 :                 sp->decodepfunc = horAcc64;
     149           1 :                 break;
     150             :         }
     151             :         /*
     152             :          * Override default decoding method with one that does the
     153             :          * predictor stuff.
     154             :          */
     155          93 :         if (tif->tif_decoderow != PredictorDecodeRow)
     156             :         {
     157          93 :             sp->decoderow = tif->tif_decoderow;
     158          93 :             tif->tif_decoderow = PredictorDecodeRow;
     159          93 :             sp->decodestrip = tif->tif_decodestrip;
     160          93 :             tif->tif_decodestrip = PredictorDecodeTile;
     161          93 :             sp->decodetile = tif->tif_decodetile;
     162          93 :             tif->tif_decodetile = PredictorDecodeTile;
     163             :         }
     164             : 
     165             :         /*
     166             :          * If the data is horizontally differenced 16-bit data that
     167             :          * requires byte-swapping, then it must be byte swapped before
     168             :          * the accumulation step.  We do this with a special-purpose
     169             :          * routine and override the normal post decoding logic that
     170             :          * the library setup when the directory was read.
     171             :          */
     172          93 :         if (tif->tif_flags & TIFF_SWAB)
     173             :         {
     174           5 :             if (sp->decodepfunc == horAcc16)
     175             :             {
     176           4 :                 sp->decodepfunc = swabHorAcc16;
     177           4 :                 tif->tif_postdecode = _TIFFNoPostDecode;
     178             :             }
     179           1 :             else if (sp->decodepfunc == horAcc32)
     180             :             {
     181           1 :                 sp->decodepfunc = swabHorAcc32;
     182           1 :                 tif->tif_postdecode = _TIFFNoPostDecode;
     183             :             }
     184           0 :             else if (sp->decodepfunc == horAcc64)
     185             :             {
     186           0 :                 sp->decodepfunc = swabHorAcc64;
     187           0 :                 tif->tif_postdecode = _TIFFNoPostDecode;
     188             :             }
     189             :         }
     190             :     }
     191             : 
     192        2918 :     else if (sp->predictor == 3)
     193             :     {
     194           5 :         sp->decodepfunc = fpAcc;
     195             :         /*
     196             :          * Override default decoding method with one that does the
     197             :          * predictor stuff.
     198             :          */
     199           5 :         if (tif->tif_decoderow != PredictorDecodeRow)
     200             :         {
     201           5 :             sp->decoderow = tif->tif_decoderow;
     202           5 :             tif->tif_decoderow = PredictorDecodeRow;
     203           5 :             sp->decodestrip = tif->tif_decodestrip;
     204           5 :             tif->tif_decodestrip = PredictorDecodeTile;
     205           5 :             sp->decodetile = tif->tif_decodetile;
     206           5 :             tif->tif_decodetile = PredictorDecodeTile;
     207             :         }
     208             :         /*
     209             :          * The data should not be swapped outside of the floating
     210             :          * point predictor, the accumulation routine should return
     211             :          * byres in the native order.
     212             :          */
     213           5 :         if (tif->tif_flags & TIFF_SWAB)
     214             :         {
     215           0 :             tif->tif_postdecode = _TIFFNoPostDecode;
     216             :         }
     217             :         /*
     218             :          * Allocate buffer to keep the decoded bytes before
     219             :          * rearranging in the right order
     220             :          */
     221             :     }
     222             : 
     223        3011 :     return 1;
     224             : }
     225             : 
     226       16999 : static int PredictorSetupEncode(TIFF *tif)
     227             : {
     228       16999 :     TIFFPredictorState *sp = PredictorState(tif);
     229       16999 :     TIFFDirectory *td = &tif->tif_dir;
     230             : 
     231       16999 :     if (!(*sp->setupencode)(tif) || !PredictorSetup(tif))
     232           0 :         return 0;
     233             : 
     234       16997 :     if (sp->predictor == 2)
     235             :     {
     236         284 :         switch (td->td_bitspersample)
     237             :         {
     238         270 :             case 8:
     239         270 :                 sp->encodepfunc = horDiff8;
     240         270 :                 break;
     241          12 :             case 16:
     242          12 :                 sp->encodepfunc = horDiff16;
     243          12 :                 break;
     244           2 :             case 32:
     245           2 :                 sp->encodepfunc = horDiff32;
     246           2 :                 break;
     247           1 :             case 64:
     248           1 :                 sp->encodepfunc = horDiff64;
     249           1 :                 break;
     250             :         }
     251             :         /*
     252             :          * Override default encoding method with one that does the
     253             :          * predictor stuff.
     254             :          */
     255         284 :         if (tif->tif_encoderow != PredictorEncodeRow)
     256             :         {
     257         283 :             sp->encoderow = tif->tif_encoderow;
     258         283 :             tif->tif_encoderow = PredictorEncodeRow;
     259         283 :             sp->encodestrip = tif->tif_encodestrip;
     260         283 :             tif->tif_encodestrip = PredictorEncodeTile;
     261         283 :             sp->encodetile = tif->tif_encodetile;
     262         283 :             tif->tif_encodetile = PredictorEncodeTile;
     263             :         }
     264             : 
     265             :         /*
     266             :          * If the data is horizontally differenced 16-bit data that
     267             :          * requires byte-swapping, then it must be byte swapped after
     268             :          * the differentiation step.  We do this with a special-purpose
     269             :          * routine and override the normal post decoding logic that
     270             :          * the library setup when the directory was read.
     271             :          */
     272         284 :         if (tif->tif_flags & TIFF_SWAB)
     273             :         {
     274           6 :             if (sp->encodepfunc == horDiff16)
     275             :             {
     276           5 :                 sp->encodepfunc = swabHorDiff16;
     277           5 :                 tif->tif_postdecode = _TIFFNoPostDecode;
     278             :             }
     279           1 :             else if (sp->encodepfunc == horDiff32)
     280             :             {
     281           1 :                 sp->encodepfunc = swabHorDiff32;
     282           1 :                 tif->tif_postdecode = _TIFFNoPostDecode;
     283             :             }
     284           0 :             else if (sp->encodepfunc == horDiff64)
     285             :             {
     286           0 :                 sp->encodepfunc = swabHorDiff64;
     287           0 :                 tif->tif_postdecode = _TIFFNoPostDecode;
     288             :             }
     289             :         }
     290             :     }
     291             : 
     292       16713 :     else if (sp->predictor == 3)
     293             :     {
     294           1 :         sp->encodepfunc = fpDiff;
     295             :         /*
     296             :          * Override default encoding method with one that does the
     297             :          * predictor stuff.
     298             :          */
     299           1 :         if (tif->tif_encoderow != PredictorEncodeRow)
     300             :         {
     301           1 :             sp->encoderow = tif->tif_encoderow;
     302           1 :             tif->tif_encoderow = PredictorEncodeRow;
     303           1 :             sp->encodestrip = tif->tif_encodestrip;
     304           1 :             tif->tif_encodestrip = PredictorEncodeTile;
     305           1 :             sp->encodetile = tif->tif_encodetile;
     306           1 :             tif->tif_encodetile = PredictorEncodeTile;
     307             :         }
     308             :     }
     309             : 
     310       16997 :     return 1;
     311             : }
     312             : 
     313             : #define REPEAT4(n, op)                                                         \
     314             :     switch (n)                                                                 \
     315             :     {                                                                          \
     316             :         default:                                                               \
     317             :         {                                                                      \
     318             :             tmsize_t i;                                                        \
     319             :             for (i = n - 4; i > 0; i--)                                        \
     320             :             {                                                                  \
     321             :                 op;                                                            \
     322             :             }                                                                  \
     323             :         } /*-fallthrough*/                                                     \
     324             :         case 4:                                                                \
     325             :             op; /*-fallthrough*/                                               \
     326             :         case 3:                                                                \
     327             :             op; /*-fallthrough*/                                               \
     328             :         case 2:                                                                \
     329             :             op; /*-fallthrough*/                                               \
     330             :         case 1:                                                                \
     331             :             op; /*-fallthrough*/                                               \
     332             :         case 0:;                                                               \
     333             :     }
     334             : 
     335             : /* Remarks related to C standard compliance in all below functions : */
     336             : /* - to avoid any undefined behavior, we only operate on unsigned types */
     337             : /*   since the behavior of "overflows" is defined (wrap over) */
     338             : /* - when storing into the byte stream, we explicitly mask with 0xff so */
     339             : /*   as to make icc -check=conversions happy (not necessary by the standard) */
     340             : 
     341             : TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW
     342       66845 : static int horAcc8(TIFF *tif, uint8_t *cp0, tmsize_t cc)
     343             : {
     344       66845 :     tmsize_t stride = PredictorState(tif)->stride;
     345             : 
     346       66845 :     unsigned char *cp = (unsigned char *)cp0;
     347       66845 :     if ((cc % stride) != 0)
     348             :     {
     349           0 :         TIFFErrorExtR(tif, "horAcc8", "%s", "(cc%stride)!=0");
     350           0 :         return 0;
     351             :     }
     352             : 
     353       66845 :     if (cc > stride)
     354             :     {
     355             :         /*
     356             :          * Pipeline the most common cases.
     357             :          */
     358       66845 :         if (stride == 3)
     359             :         {
     360        2305 :             unsigned int cr = cp[0];
     361        2305 :             unsigned int cg = cp[1];
     362        2305 :             unsigned int cb = cp[2];
     363        2305 :             tmsize_t i = stride;
     364       36868 :             for (; i < cc; i += stride)
     365             :             {
     366       34563 :                 cp[i + 0] = (unsigned char)((cr += cp[i + 0]) & 0xff);
     367       34563 :                 cp[i + 1] = (unsigned char)((cg += cp[i + 1]) & 0xff);
     368       34563 :                 cp[i + 2] = (unsigned char)((cb += cp[i + 2]) & 0xff);
     369             :             }
     370             :         }
     371       64540 :         else if (stride == 4)
     372             :         {
     373          65 :             unsigned int cr = cp[0];
     374          65 :             unsigned int cg = cp[1];
     375          65 :             unsigned int cb = cp[2];
     376          65 :             unsigned int ca = cp[3];
     377          65 :             tmsize_t i = stride;
     378        2052 :             for (; i < cc; i += stride)
     379             :             {
     380        1987 :                 cp[i + 0] = (unsigned char)((cr += cp[i + 0]) & 0xff);
     381        1987 :                 cp[i + 1] = (unsigned char)((cg += cp[i + 1]) & 0xff);
     382        1987 :                 cp[i + 2] = (unsigned char)((cb += cp[i + 2]) & 0xff);
     383        1987 :                 cp[i + 3] = (unsigned char)((ca += cp[i + 3]) & 0xff);
     384             :             }
     385             :         }
     386             :         else
     387             :         {
     388       64475 :             cc -= stride;
     389             :             do
     390             :             {
     391    16423800 :                 REPEAT4(stride,
     392             :                         cp[stride] = (unsigned char)((cp[stride] + *cp) & 0xff);
     393             :                         cp++)
     394    16423800 :                 cc -= stride;
     395    16423800 :             } while (cc > 0);
     396             :         }
     397             :     }
     398       66845 :     return 1;
     399             : }
     400             : 
     401         245 : static int swabHorAcc16(TIFF *tif, uint8_t *cp0, tmsize_t cc)
     402             : {
     403         245 :     uint16_t *wp = (uint16_t *)cp0;
     404         245 :     tmsize_t wc = cc / 2;
     405             : 
     406         245 :     TIFFSwabArrayOfShort(wp, wc);
     407         245 :     return horAcc16(tif, cp0, cc);
     408             : }
     409             : 
     410             : TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW
     411        1736 : static int horAcc16(TIFF *tif, uint8_t *cp0, tmsize_t cc)
     412             : {
     413        1736 :     tmsize_t stride = PredictorState(tif)->stride;
     414        1736 :     uint16_t *wp = (uint16_t *)cp0;
     415        1736 :     tmsize_t wc = cc / 2;
     416             : 
     417        1736 :     if ((cc % (2 * stride)) != 0)
     418             :     {
     419           0 :         TIFFErrorExtR(tif, "horAcc16", "%s", "cc%(2*stride))!=0");
     420           0 :         return 0;
     421             :     }
     422             : 
     423        1736 :     if (wc > stride)
     424             :     {
     425        1736 :         wc -= stride;
     426             :         do
     427             :         {
     428      514708 :             REPEAT4(stride, wp[stride] = (uint16_t)(((unsigned int)wp[stride] +
     429             :                                                      (unsigned int)wp[0]) &
     430             :                                                     0xffff);
     431             :                     wp++)
     432      514708 :             wc -= stride;
     433      514708 :         } while (wc > 0);
     434             :     }
     435        1736 :     return 1;
     436             : }
     437             : 
     438           1 : static int swabHorAcc32(TIFF *tif, uint8_t *cp0, tmsize_t cc)
     439             : {
     440           1 :     uint32_t *wp = (uint32_t *)cp0;
     441           1 :     tmsize_t wc = cc / 4;
     442             : 
     443           1 :     TIFFSwabArrayOfLong(wp, wc);
     444           1 :     return horAcc32(tif, cp0, cc);
     445             : }
     446             : 
     447             : TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW
     448           2 : static int horAcc32(TIFF *tif, uint8_t *cp0, tmsize_t cc)
     449             : {
     450           2 :     tmsize_t stride = PredictorState(tif)->stride;
     451           2 :     uint32_t *wp = (uint32_t *)cp0;
     452           2 :     tmsize_t wc = cc / 4;
     453             : 
     454           2 :     if ((cc % (4 * stride)) != 0)
     455             :     {
     456           0 :         TIFFErrorExtR(tif, "horAcc32", "%s", "cc%(4*stride))!=0");
     457           0 :         return 0;
     458             :     }
     459             : 
     460           2 :     if (wc > stride)
     461             :     {
     462           2 :         wc -= stride;
     463             :         do
     464             :         {
     465          10 :             REPEAT4(stride, wp[stride] += wp[0]; wp++)
     466          10 :             wc -= stride;
     467          10 :         } while (wc > 0);
     468             :     }
     469           2 :     return 1;
     470             : }
     471             : 
     472           0 : static int swabHorAcc64(TIFF *tif, uint8_t *cp0, tmsize_t cc)
     473             : {
     474           0 :     uint64_t *wp = (uint64_t *)cp0;
     475           0 :     tmsize_t wc = cc / 8;
     476             : 
     477           0 :     TIFFSwabArrayOfLong8(wp, wc);
     478           0 :     return horAcc64(tif, cp0, cc);
     479             : }
     480             : 
     481             : TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW
     482           1 : static int horAcc64(TIFF *tif, uint8_t *cp0, tmsize_t cc)
     483             : {
     484           1 :     tmsize_t stride = PredictorState(tif)->stride;
     485           1 :     uint64_t *wp = (uint64_t *)cp0;
     486           1 :     tmsize_t wc = cc / 8;
     487             : 
     488           1 :     if ((cc % (8 * stride)) != 0)
     489             :     {
     490           0 :         TIFFErrorExtR(tif, "horAcc64", "%s", "cc%(8*stride))!=0");
     491           0 :         return 0;
     492             :     }
     493             : 
     494           1 :     if (wc > stride)
     495             :     {
     496           1 :         wc -= stride;
     497             :         do
     498             :         {
     499           1 :             REPEAT4(stride, wp[stride] += wp[0]; wp++)
     500           1 :             wc -= stride;
     501           1 :         } while (wc > 0);
     502             :     }
     503           1 :     return 1;
     504             : }
     505             : 
     506             : /*
     507             :  * Floating point predictor accumulation routine.
     508             :  */
     509        1489 : static int fpAcc(TIFF *tif, uint8_t *cp0, tmsize_t cc)
     510             : {
     511        1489 :     tmsize_t stride = PredictorState(tif)->stride;
     512        1489 :     uint32_t bps = tif->tif_dir.td_bitspersample / 8;
     513        1489 :     tmsize_t wc = cc / bps;
     514        1489 :     tmsize_t count = cc;
     515        1489 :     uint8_t *cp = (uint8_t *)cp0;
     516             :     uint8_t *tmp;
     517             : 
     518        1489 :     if (cc % (bps * stride) != 0)
     519             :     {
     520           0 :         TIFFErrorExtR(tif, "fpAcc", "%s", "cc%(bps*stride))!=0");
     521           0 :         return 0;
     522             :     }
     523             : 
     524        1489 :     tmp = (uint8_t *)_TIFFmallocExt(tif, cc);
     525        1489 :     if (!tmp)
     526           0 :         return 0;
     527             : 
     528     1190430 :     while (count > stride)
     529             :     {
     530     1188940 :         REPEAT4(stride,
     531             :                 cp[stride] = (unsigned char)((cp[stride] + cp[0]) & 0xff);
     532             :                 cp++)
     533     1188940 :         count -= stride;
     534             :     }
     535             : 
     536        1489 :     _TIFFmemcpy(tmp, cp0, cc);
     537        1489 :     cp = (uint8_t *)cp0;
     538      299093 :     for (count = 0; count < wc; count++)
     539             :     {
     540             :         uint32_t byte;
     541     1488040 :         for (byte = 0; byte < bps; byte++)
     542             :         {
     543             : #if WORDS_BIGENDIAN
     544             :             cp[bps * count + byte] = tmp[byte * wc + count];
     545             : #else
     546     1190430 :             cp[bps * count + byte] = tmp[(bps - byte - 1) * wc + count];
     547             : #endif
     548             :         }
     549             :     }
     550        1489 :     _TIFFfreeExt(tif, tmp);
     551        1489 :     return 1;
     552             : }
     553             : 
     554             : /*
     555             :  * Decode a scanline and apply the predictor routine.
     556             :  */
     557           0 : static int PredictorDecodeRow(TIFF *tif, uint8_t *op0, tmsize_t occ0,
     558             :                               uint16_t s)
     559             : {
     560           0 :     TIFFPredictorState *sp = PredictorState(tif);
     561             : 
     562           0 :     assert(sp != NULL);
     563           0 :     assert(sp->decoderow != NULL);
     564           0 :     assert(sp->decodepfunc != NULL);
     565             : 
     566           0 :     if ((*sp->decoderow)(tif, op0, occ0, s))
     567             :     {
     568           0 :         return (*sp->decodepfunc)(tif, op0, occ0);
     569             :     }
     570             :     else
     571           0 :         return 0;
     572             : }
     573             : 
     574             : /*
     575             :  * Decode a tile/strip and apply the predictor routine.
     576             :  * Note that horizontal differencing must be done on a
     577             :  * row-by-row basis.  The width of a "row" has already
     578             :  * been calculated at pre-decode time according to the
     579             :  * strip/tile dimensions.
     580             :  */
     581         477 : static int PredictorDecodeTile(TIFF *tif, uint8_t *op0, tmsize_t occ0,
     582             :                                uint16_t s)
     583             : {
     584         477 :     TIFFPredictorState *sp = PredictorState(tif);
     585             : 
     586         477 :     assert(sp != NULL);
     587         477 :     assert(sp->decodetile != NULL);
     588             : 
     589         477 :     if ((*sp->decodetile)(tif, op0, occ0, s))
     590             :     {
     591         477 :         tmsize_t rowsize = sp->rowsize;
     592         477 :         assert(rowsize > 0);
     593         477 :         if ((occ0 % rowsize) != 0)
     594             :         {
     595           0 :             TIFFErrorExtR(tif, "PredictorDecodeTile", "%s",
     596             :                           "occ0%rowsize != 0");
     597           0 :             return 0;
     598             :         }
     599         477 :         assert(sp->decodepfunc != NULL);
     600       70550 :         while (occ0 > 0)
     601             :         {
     602       70073 :             if (!(*sp->decodepfunc)(tif, op0, rowsize))
     603           0 :                 return 0;
     604       70073 :             occ0 -= rowsize;
     605       70073 :             op0 += rowsize;
     606             :         }
     607         477 :         return 1;
     608             :     }
     609             :     else
     610           0 :         return 0;
     611             : }
     612             : 
     613             : TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW
     614       67238 : static int horDiff8(TIFF *tif, uint8_t *cp0, tmsize_t cc)
     615             : {
     616       67238 :     TIFFPredictorState *sp = PredictorState(tif);
     617       67238 :     tmsize_t stride = sp->stride;
     618       67238 :     unsigned char *cp = (unsigned char *)cp0;
     619             : 
     620       67238 :     if ((cc % stride) != 0)
     621             :     {
     622           0 :         TIFFErrorExtR(tif, "horDiff8", "%s", "(cc%stride)!=0");
     623           0 :         return 0;
     624             :     }
     625             : 
     626       67238 :     if (cc > stride)
     627             :     {
     628       67201 :         cc -= stride;
     629             :         /*
     630             :          * Pipeline the most common cases.
     631             :          */
     632       67201 :         if (stride == 3)
     633             :         {
     634             :             unsigned int r1, g1, b1;
     635         897 :             unsigned int r2 = cp[0];
     636         897 :             unsigned int g2 = cp[1];
     637         897 :             unsigned int b2 = cp[2];
     638             :             do
     639             :             {
     640      267395 :                 r1 = cp[3];
     641      267395 :                 cp[3] = (unsigned char)((r1 - r2) & 0xff);
     642      267395 :                 r2 = r1;
     643      267395 :                 g1 = cp[4];
     644      267395 :                 cp[4] = (unsigned char)((g1 - g2) & 0xff);
     645      267395 :                 g2 = g1;
     646      267395 :                 b1 = cp[5];
     647      267395 :                 cp[5] = (unsigned char)((b1 - b2) & 0xff);
     648      267395 :                 b2 = b1;
     649      267395 :                 cp += 3;
     650      267395 :             } while ((cc -= 3) > 0);
     651             :         }
     652       66304 :         else if (stride == 4)
     653             :         {
     654             :             unsigned int r1, g1, b1, a1;
     655          33 :             unsigned int r2 = cp[0];
     656          33 :             unsigned int g2 = cp[1];
     657          33 :             unsigned int b2 = cp[2];
     658          33 :             unsigned int a2 = cp[3];
     659             :             do
     660             :             {
     661         995 :                 r1 = cp[4];
     662         995 :                 cp[4] = (unsigned char)((r1 - r2) & 0xff);
     663         995 :                 r2 = r1;
     664         995 :                 g1 = cp[5];
     665         995 :                 cp[5] = (unsigned char)((g1 - g2) & 0xff);
     666         995 :                 g2 = g1;
     667         995 :                 b1 = cp[6];
     668         995 :                 cp[6] = (unsigned char)((b1 - b2) & 0xff);
     669         995 :                 b2 = b1;
     670         995 :                 a1 = cp[7];
     671         995 :                 cp[7] = (unsigned char)((a1 - a2) & 0xff);
     672         995 :                 a2 = a1;
     673         995 :                 cp += 4;
     674         995 :             } while ((cc -= 4) > 0);
     675             :         }
     676             :         else
     677             :         {
     678       66271 :             cp += cc - 1;
     679             :             do
     680             :             {
     681    10741700 :                 REPEAT4(stride,
     682             :                         cp[stride] =
     683             :                             (unsigned char)((cp[stride] - cp[0]) & 0xff);
     684             :                         cp--)
     685    10741700 :             } while ((cc -= stride) > 0);
     686             :         }
     687             :     }
     688       67238 :     return 1;
     689             : }
     690             : 
     691             : TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW
     692        1014 : static int horDiff16(TIFF *tif, uint8_t *cp0, tmsize_t cc)
     693             : {
     694        1014 :     TIFFPredictorState *sp = PredictorState(tif);
     695        1014 :     tmsize_t stride = sp->stride;
     696        1014 :     uint16_t *wp = (uint16_t *)cp0;
     697        1014 :     tmsize_t wc = cc / 2;
     698             : 
     699        1014 :     if ((cc % (2 * stride)) != 0)
     700             :     {
     701           0 :         TIFFErrorExtR(tif, "horDiff8", "%s", "(cc%(2*stride))!=0");
     702           0 :         return 0;
     703             :     }
     704             : 
     705        1014 :     if (wc > stride)
     706             :     {
     707        1014 :         wc -= stride;
     708        1014 :         wp += wc - 1;
     709             :         do
     710             :         {
     711      126596 :             REPEAT4(stride, wp[stride] = (uint16_t)(((unsigned int)wp[stride] -
     712             :                                                      (unsigned int)wp[0]) &
     713             :                                                     0xffff);
     714             :                     wp--)
     715      126596 :             wc -= stride;
     716      126596 :         } while (wc > 0);
     717             :     }
     718        1014 :     return 1;
     719             : }
     720             : 
     721         379 : static int swabHorDiff16(TIFF *tif, uint8_t *cp0, tmsize_t cc)
     722             : {
     723         379 :     uint16_t *wp = (uint16_t *)cp0;
     724         379 :     tmsize_t wc = cc / 2;
     725             : 
     726         379 :     if (!horDiff16(tif, cp0, cc))
     727           0 :         return 0;
     728             : 
     729         379 :     TIFFSwabArrayOfShort(wp, wc);
     730         379 :     return 1;
     731             : }
     732             : 
     733             : TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW
     734           2 : static int horDiff32(TIFF *tif, uint8_t *cp0, tmsize_t cc)
     735             : {
     736           2 :     TIFFPredictorState *sp = PredictorState(tif);
     737           2 :     tmsize_t stride = sp->stride;
     738           2 :     uint32_t *wp = (uint32_t *)cp0;
     739           2 :     tmsize_t wc = cc / 4;
     740             : 
     741           2 :     if ((cc % (4 * stride)) != 0)
     742             :     {
     743           0 :         TIFFErrorExtR(tif, "horDiff32", "%s", "(cc%(4*stride))!=0");
     744           0 :         return 0;
     745             :     }
     746             : 
     747           2 :     if (wc > stride)
     748             :     {
     749           2 :         wc -= stride;
     750           2 :         wp += wc - 1;
     751             :         do
     752             :         {
     753          10 :             REPEAT4(stride, wp[stride] -= wp[0]; wp--)
     754          10 :             wc -= stride;
     755          10 :         } while (wc > 0);
     756             :     }
     757           2 :     return 1;
     758             : }
     759             : 
     760           1 : static int swabHorDiff32(TIFF *tif, uint8_t *cp0, tmsize_t cc)
     761             : {
     762           1 :     uint32_t *wp = (uint32_t *)cp0;
     763           1 :     tmsize_t wc = cc / 4;
     764             : 
     765           1 :     if (!horDiff32(tif, cp0, cc))
     766           0 :         return 0;
     767             : 
     768           1 :     TIFFSwabArrayOfLong(wp, wc);
     769           1 :     return 1;
     770             : }
     771             : 
     772             : TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW
     773           1 : static int horDiff64(TIFF *tif, uint8_t *cp0, tmsize_t cc)
     774             : {
     775           1 :     TIFFPredictorState *sp = PredictorState(tif);
     776           1 :     tmsize_t stride = sp->stride;
     777           1 :     uint64_t *wp = (uint64_t *)cp0;
     778           1 :     tmsize_t wc = cc / 8;
     779             : 
     780           1 :     if ((cc % (8 * stride)) != 0)
     781             :     {
     782           0 :         TIFFErrorExtR(tif, "horDiff64", "%s", "(cc%(8*stride))!=0");
     783           0 :         return 0;
     784             :     }
     785             : 
     786           1 :     if (wc > stride)
     787             :     {
     788           1 :         wc -= stride;
     789           1 :         wp += wc - 1;
     790             :         do
     791             :         {
     792           1 :             REPEAT4(stride, wp[stride] -= wp[0]; wp--)
     793           1 :             wc -= stride;
     794           1 :         } while (wc > 0);
     795             :     }
     796           1 :     return 1;
     797             : }
     798             : 
     799           0 : static int swabHorDiff64(TIFF *tif, uint8_t *cp0, tmsize_t cc)
     800             : {
     801           0 :     uint64_t *wp = (uint64_t *)cp0;
     802           0 :     tmsize_t wc = cc / 8;
     803             : 
     804           0 :     if (!horDiff64(tif, cp0, cc))
     805           0 :         return 0;
     806             : 
     807           0 :     TIFFSwabArrayOfLong8(wp, wc);
     808           0 :     return 1;
     809             : }
     810             : 
     811             : /*
     812             :  * Floating point predictor differencing routine.
     813             :  */
     814             : TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW
     815           1 : static int fpDiff(TIFF *tif, uint8_t *cp0, tmsize_t cc)
     816             : {
     817           1 :     tmsize_t stride = PredictorState(tif)->stride;
     818           1 :     uint32_t bps = tif->tif_dir.td_bitspersample / 8;
     819           1 :     tmsize_t wc = cc / bps;
     820             :     tmsize_t count;
     821           1 :     uint8_t *cp = (uint8_t *)cp0;
     822             :     uint8_t *tmp;
     823             : 
     824           1 :     if ((cc % (bps * stride)) != 0)
     825             :     {
     826           0 :         TIFFErrorExtR(tif, "fpDiff", "%s", "(cc%(bps*stride))!=0");
     827           0 :         return 0;
     828             :     }
     829             : 
     830           1 :     tmp = (uint8_t *)_TIFFmallocExt(tif, cc);
     831           1 :     if (!tmp)
     832           0 :         return 0;
     833             : 
     834           1 :     _TIFFmemcpy(tmp, cp0, cc);
     835           5 :     for (count = 0; count < wc; count++)
     836             :     {
     837             :         uint32_t byte;
     838          36 :         for (byte = 0; byte < bps; byte++)
     839             :         {
     840             : #if WORDS_BIGENDIAN
     841             :             cp[byte * wc + count] = tmp[bps * count + byte];
     842             : #else
     843          32 :             cp[(bps - byte - 1) * wc + count] = tmp[bps * count + byte];
     844             : #endif
     845             :         }
     846             :     }
     847           1 :     _TIFFfreeExt(tif, tmp);
     848             : 
     849           1 :     cp = (uint8_t *)cp0;
     850           1 :     cp += cc - stride - 1;
     851          32 :     for (count = cc; count > stride; count -= stride)
     852          31 :         REPEAT4(stride,
     853             :                 cp[stride] = (unsigned char)((cp[stride] - cp[0]) & 0xff);
     854             :                 cp--)
     855           1 :     return 1;
     856             : }
     857             : 
     858           0 : static int PredictorEncodeRow(TIFF *tif, uint8_t *bp, tmsize_t cc, uint16_t s)
     859             : {
     860           0 :     TIFFPredictorState *sp = PredictorState(tif);
     861             : 
     862           0 :     assert(sp != NULL);
     863           0 :     assert(sp->encodepfunc != NULL);
     864           0 :     assert(sp->encoderow != NULL);
     865             : 
     866             :     /* XXX horizontal differencing alters user's data XXX */
     867           0 :     if (!(*sp->encodepfunc)(tif, bp, cc))
     868           0 :         return 0;
     869           0 :     return (*sp->encoderow)(tif, bp, cc, s);
     870             : }
     871             : 
     872         305 : static int PredictorEncodeTile(TIFF *tif, uint8_t *bp0, tmsize_t cc0,
     873             :                                uint16_t s)
     874             : {
     875             :     static const char module[] = "PredictorEncodeTile";
     876         305 :     TIFFPredictorState *sp = PredictorState(tif);
     877             :     uint8_t *working_copy;
     878         305 :     tmsize_t cc = cc0, rowsize;
     879             :     unsigned char *bp;
     880             :     int result_code;
     881             : 
     882         305 :     assert(sp != NULL);
     883         305 :     assert(sp->encodepfunc != NULL);
     884         305 :     assert(sp->encodetile != NULL);
     885             : 
     886             :     /*
     887             :      * Do predictor manipulation in a working buffer to avoid altering
     888             :      * the callers buffer. http://trac.osgeo.org/gdal/ticket/1965
     889             :      */
     890         305 :     working_copy = (uint8_t *)_TIFFmallocExt(tif, cc0);
     891         305 :     if (working_copy == NULL)
     892             :     {
     893           0 :         TIFFErrorExtR(tif, module,
     894             :                       "Out of memory allocating %" PRId64 " byte temp buffer.",
     895             :                       (int64_t)cc0);
     896           0 :         return 0;
     897             :     }
     898         305 :     memcpy(working_copy, bp0, cc0);
     899         305 :     bp = working_copy;
     900             : 
     901         305 :     rowsize = sp->rowsize;
     902         305 :     assert(rowsize > 0);
     903         305 :     if ((cc0 % rowsize) != 0)
     904             :     {
     905           0 :         TIFFErrorExtR(tif, "PredictorEncodeTile", "%s", "(cc0%rowsize)!=0");
     906           0 :         _TIFFfreeExt(tif, working_copy);
     907           0 :         return 0;
     908             :     }
     909       68561 :     while (cc > 0)
     910             :     {
     911       68260 :         (*sp->encodepfunc)(tif, bp, rowsize);
     912       68256 :         cc -= rowsize;
     913       68256 :         bp += rowsize;
     914             :     }
     915         301 :     result_code = (*sp->encodetile)(tif, working_copy, cc0, s);
     916             : 
     917         304 :     _TIFFfreeExt(tif, working_copy);
     918             : 
     919         303 :     return result_code;
     920             : }
     921             : 
     922             : #define FIELD_PREDICTOR (FIELD_CODEC + 0) /* XXX */
     923             : 
     924             : static const TIFFField predictFields[] = {
     925             :     {TIFFTAG_PREDICTOR, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16,
     926             :      TIFF_SETGET_UINT16, FIELD_PREDICTOR, FALSE, FALSE, "Predictor", NULL},
     927             : };
     928             : 
     929      166326 : static int PredictorVSetField(TIFF *tif, uint32_t tag, va_list ap)
     930             : {
     931      166326 :     TIFFPredictorState *sp = PredictorState(tif);
     932             : 
     933      166326 :     assert(sp != NULL);
     934      166326 :     assert(sp->vsetparent != NULL);
     935             : 
     936      166326 :     switch (tag)
     937             :     {
     938        5405 :         case TIFFTAG_PREDICTOR:
     939        5405 :             sp->predictor = (uint16_t)va_arg(ap, uint16_vap);
     940        5405 :             TIFFSetFieldBit(tif, FIELD_PREDICTOR);
     941        5405 :             break;
     942      160921 :         default:
     943      160921 :             return (*sp->vsetparent)(tif, tag, ap);
     944             :     }
     945        5405 :     tif->tif_flags |= TIFF_DIRTYDIRECT;
     946        5405 :     return 1;
     947             : }
     948             : 
     949      338380 : static int PredictorVGetField(TIFF *tif, uint32_t tag, va_list ap)
     950             : {
     951      338380 :     TIFFPredictorState *sp = PredictorState(tif);
     952             : 
     953      338380 :     assert(sp != NULL);
     954      338380 :     assert(sp->vgetparent != NULL);
     955             : 
     956      338380 :     switch (tag)
     957             :     {
     958       20658 :         case TIFFTAG_PREDICTOR:
     959       20658 :             *va_arg(ap, uint16_t *) = (uint16_t)sp->predictor;
     960       20658 :             break;
     961      317722 :         default:
     962      317722 :             return (*sp->vgetparent)(tif, tag, ap);
     963             :     }
     964       20658 :     return 1;
     965             : }
     966             : 
     967           0 : static void PredictorPrintDir(TIFF *tif, FILE *fd, long flags)
     968             : {
     969           0 :     TIFFPredictorState *sp = PredictorState(tif);
     970             : 
     971             :     (void)flags;
     972           0 :     if (TIFFFieldSet(tif, FIELD_PREDICTOR))
     973             :     {
     974           0 :         fprintf(fd, "  Predictor: ");
     975           0 :         switch (sp->predictor)
     976             :         {
     977           0 :             case 1:
     978           0 :                 fprintf(fd, "none ");
     979           0 :                 break;
     980           0 :             case 2:
     981           0 :                 fprintf(fd, "horizontal differencing ");
     982           0 :                 break;
     983           0 :             case 3:
     984           0 :                 fprintf(fd, "floating point predictor ");
     985           0 :                 break;
     986             :         }
     987           0 :         fprintf(fd, "%d (0x%x)\n", sp->predictor, sp->predictor);
     988             :     }
     989           0 :     if (sp->printdir)
     990           0 :         (*sp->printdir)(tif, fd, flags);
     991           0 : }
     992             : 
     993       26170 : int TIFFPredictorInit(TIFF *tif)
     994             : {
     995       26170 :     TIFFPredictorState *sp = PredictorState(tif);
     996             : 
     997       26170 :     assert(sp != 0);
     998             : 
     999             :     /*
    1000             :      * Merge codec-specific tag information.
    1001             :      */
    1002       26170 :     if (!_TIFFMergeFields(tif, predictFields, TIFFArrayCount(predictFields)))
    1003             :     {
    1004           1 :         TIFFErrorExtR(tif, "TIFFPredictorInit",
    1005             :                       "Merging Predictor codec-specific tags failed");
    1006           0 :         return 0;
    1007             :     }
    1008             : 
    1009             :     /*
    1010             :      * Override parent get/set field methods.
    1011             :      */
    1012       26170 :     sp->vgetparent = tif->tif_tagmethods.vgetfield;
    1013       26170 :     tif->tif_tagmethods.vgetfield =
    1014             :         PredictorVGetField; /* hook for predictor tag */
    1015       26170 :     sp->vsetparent = tif->tif_tagmethods.vsetfield;
    1016       26170 :     tif->tif_tagmethods.vsetfield =
    1017             :         PredictorVSetField; /* hook for predictor tag */
    1018       26170 :     sp->printdir = tif->tif_tagmethods.printdir;
    1019       26170 :     tif->tif_tagmethods.printdir =
    1020             :         PredictorPrintDir; /* hook for predictor tag */
    1021             : 
    1022       26170 :     sp->setupdecode = tif->tif_setupdecode;
    1023       26170 :     tif->tif_setupdecode = PredictorSetupDecode;
    1024       26170 :     sp->setupencode = tif->tif_setupencode;
    1025       26170 :     tif->tif_setupencode = PredictorSetupEncode;
    1026             : 
    1027       26170 :     sp->predictor = 1;      /* default value */
    1028       26170 :     sp->encodepfunc = NULL; /* no predictor routine */
    1029       26170 :     sp->decodepfunc = NULL; /* no predictor routine */
    1030       26170 :     return 1;
    1031             : }
    1032             : 
    1033       26161 : int TIFFPredictorCleanup(TIFF *tif)
    1034             : {
    1035       26161 :     TIFFPredictorState *sp = PredictorState(tif);
    1036             : 
    1037       26161 :     assert(sp != 0);
    1038             : 
    1039       26161 :     tif->tif_tagmethods.vgetfield = sp->vgetparent;
    1040       26161 :     tif->tif_tagmethods.vsetfield = sp->vsetparent;
    1041       26161 :     tif->tif_tagmethods.printdir = sp->printdir;
    1042       26161 :     tif->tif_setupdecode = sp->setupdecode;
    1043       26161 :     tif->tif_setupencode = sp->setupencode;
    1044             : 
    1045       26161 :     return 1;
    1046             : }

Generated by: LCOV version 1.14