LTP GCOV extension - code coverage report
Current view: directory - frmts/gtiff/libtiff - tif_aux.c
Test: gdal_filtered.info
Date: 2010-07-12 Instrumented lines: 144
Code covered: 22.9 % Executed lines: 33

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

Generated by: LTP GCOV extension version 1.5