LCOV - code coverage report
Current view: directory - frmts/gtiff/libtiff - tif_aux.c (source / functions) Found Hit Coverage
Test: gdal_filtered.info Lines: 160 41 25.6 %
Date: 2011-12-18 Functions: 10 6 60.0 %

       1                 : /* $Id: tif_aux.c,v 1.26 2010-07-01 15:33:28 dron Exp $ */
       2                 : 
       3                 : /*
       4                 :  * Copyright (c) 1991-1997 Sam Leffler
       5                 :  * Copyright (c) 1991-1997 Silicon Graphics, Inc.
       6                 :  *
       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.
      14                 :  * 
      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.  
      18                 :  * 
      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 
      24                 :  * OF THIS SOFTWARE.
      25                 :  */
      26                 : 
      27                 : /*
      28                 :  * TIFF Library.
      29                 :  *
      30                 :  * Auxiliary Support Routines.
      31                 :  */
      32                 : #include "tiffiop.h"
      33                 : #include "tif_predict.h"
      34                 : #include <math.h>
      35                 : 
      36                 : uint32
      37            5065 : _TIFFMultiply32(TIFF* tif, uint32 first, uint32 second, const char* where)
      38                 : {
      39            5065 :   uint32 bytes = first * second;
      40                 : 
      41            5065 :   if (second && bytes / second != first) {
      42               0 :     TIFFErrorExt(tif->tif_clientdata, where, "Integer overflow in %s", where);
      43               0 :     bytes = 0;
      44                 :   }
      45                 : 
      46            5065 :   return bytes;
      47                 : }
      48                 : 
      49                 : uint64
      50          671144 : _TIFFMultiply64(TIFF* tif, uint64 first, uint64 second, const char* where)
      51                 : {
      52          671144 :   uint64 bytes = first * second;
      53                 : 
      54          671144 :   if (second && bytes / second != first) {
      55               0 :     TIFFErrorExt(tif->tif_clientdata, where, "Integer overflow in %s", where);
      56               0 :     bytes = 0;
      57                 :   }
      58                 : 
      59          671144 :   return bytes;
      60                 : }
      61                 : 
      62                 : void*
      63          143016 : _TIFFCheckRealloc(TIFF* tif, void* buffer,
      64                 :       tmsize_t nmemb, tmsize_t elem_size, const char* what)
      65                 : {
      66          143016 :   void* cp = NULL;
      67          143016 :   tmsize_t bytes = nmemb * elem_size;
      68                 : 
      69                 :   /*
      70                 :    * XXX: Check for integer overflow.
      71                 :    */
      72          143016 :   if (nmemb && elem_size && bytes / elem_size == nmemb)
      73          143016 :     cp = _TIFFrealloc(buffer, bytes);
      74                 : 
      75          143016 :   if (cp == NULL) {
      76               0 :     TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
      77                 :            "Failed to allocate memory for %s "
      78                 :            "(%ld elements of %ld bytes each)",
      79                 :            what,(long) nmemb, (long) elem_size);
      80                 :   }
      81                 : 
      82          143016 :   return cp;
      83                 : }
      84                 : 
      85                 : void*
      86          101826 : _TIFFCheckMalloc(TIFF* tif, tmsize_t nmemb, tmsize_t elem_size, const char* what)
      87                 : {
      88          101826 :   return _TIFFCheckRealloc(tif, NULL, nmemb, elem_size, what);  
      89                 : }
      90                 : 
      91                 : static int
      92               0 : TIFFDefaultTransferFunction(TIFFDirectory* td)
      93                 : {
      94               0 :   uint16 **tf = td->td_transferfunction;
      95                 :   tmsize_t i, n, nbytes;
      96                 : 
      97               0 :   tf[0] = tf[1] = tf[2] = 0;
      98               0 :   if (td->td_bitspersample >= sizeof(tmsize_t) * 8 - 2)
      99               0 :     return 0;
     100                 : 
     101               0 :   n = ((tmsize_t)1)<<td->td_bitspersample;
     102               0 :   nbytes = n * sizeof (uint16);
     103               0 :   if (!(tf[0] = (uint16 *)_TIFFmalloc(nbytes)))
     104               0 :     return 0;
     105               0 :   tf[0][0] = 0;
     106               0 :   for (i = 1; i < n; i++) {
     107               0 :     double t = (double)i/((double) n-1.);
     108               0 :     tf[0][i] = (uint16)floor(65535.*pow(t, 2.2) + .5);
     109                 :   }
     110                 : 
     111               0 :   if (td->td_samplesperpixel - td->td_extrasamples > 1) {
     112               0 :     if (!(tf[1] = (uint16 *)_TIFFmalloc(nbytes)))
     113               0 :       goto bad;
     114               0 :     _TIFFmemcpy(tf[1], tf[0], nbytes);
     115               0 :     if (!(tf[2] = (uint16 *)_TIFFmalloc(nbytes)))
     116               0 :       goto bad;
     117               0 :     _TIFFmemcpy(tf[2], tf[0], nbytes);
     118                 :   }
     119               0 :   return 1;
     120                 : 
     121                 : bad:
     122               0 :   if (tf[0])
     123               0 :     _TIFFfree(tf[0]);
     124               0 :   if (tf[1])
     125               0 :     _TIFFfree(tf[1]);
     126               0 :   if (tf[2])
     127               0 :     _TIFFfree(tf[2]);
     128               0 :   tf[0] = tf[1] = tf[2] = 0;
     129               0 :   return 0;
     130                 : }
     131                 : 
     132                 : static int
     133               0 : TIFFDefaultRefBlackWhite(TIFFDirectory* td)
     134                 : {
     135                 :   int i;
     136                 : 
     137               0 :   if (!(td->td_refblackwhite = (float *)_TIFFmalloc(6*sizeof (float))))
     138               0 :     return 0;
     139               0 :         if (td->td_photometric == PHOTOMETRIC_YCBCR) {
     140                 :     /*
     141                 :      * YCbCr (Class Y) images must have the ReferenceBlackWhite
     142                 :      * tag set. Fix the broken images, which lacks that tag.
     143                 :      */
     144               0 :     td->td_refblackwhite[0] = 0.0F;
     145               0 :     td->td_refblackwhite[1] = td->td_refblackwhite[3] =
     146               0 :       td->td_refblackwhite[5] = 255.0F;
     147               0 :     td->td_refblackwhite[2] = td->td_refblackwhite[4] = 128.0F;
     148                 :   } else {
     149                 :     /*
     150                 :      * Assume RGB (Class R)
     151                 :      */
     152               0 :     for (i = 0; i < 3; i++) {
     153               0 :         td->td_refblackwhite[2*i+0] = 0;
     154               0 :         td->td_refblackwhite[2*i+1] =
     155               0 :           (float)((1L<<td->td_bitspersample)-1L);
     156                 :     }
     157                 :   }
     158               0 :   return 1;
     159                 : }
     160                 : 
     161                 : /*
     162                 :  * Like TIFFGetField, but return any default
     163                 :  * value if the tag is not present in the directory.
     164                 :  *
     165                 :  * NB:  We use the value in the directory, rather than
     166                 :  *  explcit values so that defaults exist only one
     167                 :  *  place in the library -- in TIFFDefaultDirectory.
     168                 :  */
     169                 : int
     170            1581 : TIFFVGetFieldDefaulted(TIFF* tif, uint32 tag, va_list ap)
     171                 : {
     172            1581 :   TIFFDirectory *td = &tif->tif_dir;
     173                 : 
     174            1581 :   if (TIFFVGetField(tif, tag, ap))
     175             251 :     return (1);
     176            1330 :   switch (tag) {
     177                 :   case TIFFTAG_SUBFILETYPE:
     178               0 :     *va_arg(ap, uint32 *) = td->td_subfiletype;
     179               0 :     return (1);
     180                 :   case TIFFTAG_BITSPERSAMPLE:
     181               0 :     *va_arg(ap, uint16 *) = td->td_bitspersample;
     182               0 :     return (1);
     183                 :   case TIFFTAG_THRESHHOLDING:
     184               0 :     *va_arg(ap, uint16 *) = td->td_threshholding;
     185               0 :     return (1);
     186                 :   case TIFFTAG_FILLORDER:
     187               0 :     *va_arg(ap, uint16 *) = td->td_fillorder;
     188               0 :     return (1);
     189                 :   case TIFFTAG_ORIENTATION:
     190               4 :     *va_arg(ap, uint16 *) = td->td_orientation;
     191               4 :     return (1);
     192                 :   case TIFFTAG_SAMPLESPERPIXEL:
     193               0 :     *va_arg(ap, uint16 *) = td->td_samplesperpixel;
     194               0 :     return (1);
     195                 :   case TIFFTAG_ROWSPERSTRIP:
     196               0 :     *va_arg(ap, uint32 *) = td->td_rowsperstrip;
     197               0 :     return (1);
     198                 :   case TIFFTAG_MINSAMPLEVALUE:
     199               0 :     *va_arg(ap, uint16 *) = td->td_minsamplevalue;
     200               0 :     return (1);
     201                 :   case TIFFTAG_MAXSAMPLEVALUE:
     202               0 :     *va_arg(ap, uint16 *) = td->td_maxsamplevalue;
     203               0 :     return (1);
     204                 :   case TIFFTAG_PLANARCONFIG:
     205               0 :     *va_arg(ap, uint16 *) = td->td_planarconfig;
     206               0 :     return (1);
     207                 :   case TIFFTAG_RESOLUTIONUNIT:
     208               0 :     *va_arg(ap, uint16 *) = td->td_resolutionunit;
     209               0 :     return (1);
     210                 :   case TIFFTAG_PREDICTOR:
     211                 :                 {
     212               0 :       TIFFPredictorState* sp = (TIFFPredictorState*) tif->tif_data;
     213               0 :       *va_arg(ap, uint16*) = (uint16) sp->predictor;
     214               0 :       return 1;
     215                 :                 }
     216                 :   case TIFFTAG_DOTRANGE:
     217               0 :     *va_arg(ap, uint16 *) = 0;
     218               0 :     *va_arg(ap, uint16 *) = (1<<td->td_bitspersample)-1;
     219               0 :     return (1);
     220                 :   case TIFFTAG_INKSET:
     221               7 :     *va_arg(ap, uint16 *) = INKSET_CMYK;
     222               7 :     return 1;
     223                 :   case TIFFTAG_NUMBEROFINKS:
     224               0 :     *va_arg(ap, uint16 *) = 4;
     225               0 :     return (1);
     226                 :   case TIFFTAG_EXTRASAMPLES:
     227               4 :     *va_arg(ap, uint16 *) = td->td_extrasamples;
     228               4 :     *va_arg(ap, uint16 **) = td->td_sampleinfo;
     229               4 :     return (1);
     230                 :   case TIFFTAG_MATTEING:
     231               0 :     *va_arg(ap, uint16 *) =
     232               0 :         (td->td_extrasamples == 1 &&
     233               0 :          td->td_sampleinfo[0] == EXTRASAMPLE_ASSOCALPHA);
     234               0 :     return (1);
     235                 :   case TIFFTAG_TILEDEPTH:
     236               0 :     *va_arg(ap, uint32 *) = td->td_tiledepth;
     237               0 :     return (1);
     238                 :   case TIFFTAG_DATATYPE:
     239               0 :     *va_arg(ap, uint16 *) = td->td_sampleformat-1;
     240               0 :     return (1);
     241                 :   case TIFFTAG_SAMPLEFORMAT:
     242               0 :     *va_arg(ap, uint16 *) = td->td_sampleformat;
     243               0 :                 return(1);
     244                 :   case TIFFTAG_IMAGEDEPTH:
     245               0 :     *va_arg(ap, uint32 *) = td->td_imagedepth;
     246               0 :     return (1);
     247                 :   case TIFFTAG_YCBCRCOEFFICIENTS:
     248                 :     {
     249                 :       /* defaults are from CCIR Recommendation 601-1 */
     250                 :       static float ycbcrcoeffs[] = { 0.299f, 0.587f, 0.114f };
     251               0 :       *va_arg(ap, float **) = ycbcrcoeffs;
     252               0 :       return 1;
     253                 :     }
     254                 :   case TIFFTAG_YCBCRSUBSAMPLING:
     255            1314 :     *va_arg(ap, uint16 *) = td->td_ycbcrsubsampling[0];
     256            1314 :     *va_arg(ap, uint16 *) = td->td_ycbcrsubsampling[1];
     257            1314 :     return (1);
     258                 :   case TIFFTAG_YCBCRPOSITIONING:
     259               0 :     *va_arg(ap, uint16 *) = td->td_ycbcrpositioning;
     260               0 :     return (1);
     261                 :   case TIFFTAG_WHITEPOINT:
     262                 :     {
     263                 :       static float whitepoint[2];
     264                 : 
     265                 :       /* TIFF 6.0 specification tells that it is no default
     266                 :          value for the WhitePoint, but AdobePhotoshop TIFF
     267                 :          Technical Note tells that it should be CIE D50. */
     268               1 :       whitepoint[0] = D50_X0 / (D50_X0 + D50_Y0 + D50_Z0);
     269               1 :       whitepoint[1] = D50_Y0 / (D50_X0 + D50_Y0 + D50_Z0);
     270               1 :       *va_arg(ap, float **) = whitepoint;
     271               1 :       return 1;
     272                 :     }
     273                 :   case TIFFTAG_TRANSFERFUNCTION:
     274               0 :     if (!td->td_transferfunction[0] &&
     275               0 :         !TIFFDefaultTransferFunction(td)) {
     276               0 :       TIFFErrorExt(tif->tif_clientdata, tif->tif_name, "No space for \"TransferFunction\" tag");
     277               0 :       return (0);
     278                 :     }
     279               0 :     *va_arg(ap, uint16 **) = td->td_transferfunction[0];
     280               0 :     if (td->td_samplesperpixel - td->td_extrasamples > 1) {
     281               0 :       *va_arg(ap, uint16 **) = td->td_transferfunction[1];
     282               0 :       *va_arg(ap, uint16 **) = td->td_transferfunction[2];
     283                 :     }
     284               0 :     return (1);
     285                 :   case TIFFTAG_REFERENCEBLACKWHITE:
     286               0 :     if (!td->td_refblackwhite && !TIFFDefaultRefBlackWhite(td))
     287               0 :       return (0);
     288               0 :     *va_arg(ap, float **) = td->td_refblackwhite;
     289               0 :     return (1);
     290                 :   }
     291               0 :   return 0;
     292                 : }
     293                 : 
     294                 : /*
     295                 :  * Like TIFFGetField, but return any default
     296                 :  * value if the tag is not present in the directory.
     297                 :  */
     298                 : int
     299            1581 : TIFFGetFieldDefaulted(TIFF* tif, uint32 tag, ...)
     300                 : {
     301                 :   int ok;
     302                 :   va_list ap;
     303                 : 
     304            1581 :   va_start(ap, tag);
     305            1581 :   ok =  TIFFVGetFieldDefaulted(tif, tag, ap);
     306            1581 :   va_end(ap);
     307            1581 :   return (ok);
     308                 : }
     309                 : 
     310                 : struct _Int64Parts {
     311                 :   int32 low, high;
     312                 : };
     313                 : 
     314                 : typedef union {
     315                 :   struct _Int64Parts part;
     316                 :   int64 value;
     317                 : } _Int64;
     318                 : 
     319                 : float
     320               0 : _TIFFUInt64ToFloat(uint64 ui64)
     321                 : {
     322                 :   _Int64 i;
     323                 : 
     324               0 :   i.value = ui64;
     325               0 :   if (i.part.high >= 0) {
     326               0 :     return (float)i.value;
     327                 :   } else {
     328                 :     long double df;
     329               0 :     df = (long double)i.value;
     330               0 :     df += 18446744073709551616.0; /* adding 2**64 */
     331               0 :     return (float)df;
     332                 :   }
     333                 : }
     334                 : 
     335                 : double
     336               0 : _TIFFUInt64ToDouble(uint64 ui64)
     337                 : {
     338                 :   _Int64 i;
     339                 : 
     340               0 :   i.value = ui64;
     341               0 :   if (i.part.high >= 0) {
     342               0 :     return (double)i.value;
     343                 :   } else {
     344                 :     long double df;
     345               0 :     df = (long double)i.value;
     346               0 :     df += 18446744073709551616.0; /* adding 2**64 */
     347               0 :     return (double)df;
     348                 :   }
     349                 : }
     350                 : 
     351                 : /* vim: set ts=8 sts=8 sw=8 noet: */
     352                 : /*
     353                 :  * Local Variables:
     354                 :  * mode: c
     355                 :  * c-basic-offset: 8
     356                 :  * fill-column: 78
     357                 :  * End:
     358                 :  */

Generated by: LCOV version 1.7