home *** CD-ROM | disk | FTP | other *** search
/ PC World 2002 February / PCWorld_2002-02_cd.bin / Software / Vyzkuste / pdflib / pdflib-4.0.1.sit / pdflib-4.0.1 / tiff / tif_aux.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-07-04  |  6.0 KB  |  208 lines  |  [TEXT/CWIE]

  1. /*
  2.  * Copyright (c) 1991-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.  * Auxiliary Support Routines.
  29.  */
  30.  
  31. /* $Id: tif_aux.c,v 1.4 2001/03/21 17:16:25 rjs Exp $ */
  32.  
  33. #include "tiffiop.h"
  34.  
  35. #ifdef COLORIMETRY_SUPPORT
  36. #include <math.h>
  37.  
  38. static void
  39. TIFFDefaultTransferFunction(TIFF* tif, TIFFDirectory* td)
  40. {
  41.     uint16 **tf = td->td_transferfunction;
  42.     long i, n = 1<<td->td_bitspersample;
  43.  
  44.     tf[0] = (uint16 *)_TIFFmalloc(tif, n * sizeof (uint16));
  45.     tf[0][0] = 0;
  46.     for (i = 1; i < n; i++) {
  47.         double t = (double)i/((double) n-1.);
  48.         tf[0][i] = (uint16)floor(65535.*pow(t, 2.2) + .5);
  49.     }
  50.     if (td->td_samplesperpixel - td->td_extrasamples > 1) {
  51.         tf[1] = (uint16 *)_TIFFmalloc(tif, n * sizeof (uint16));
  52.         _TIFFmemcpy(tf[1], tf[0], n * sizeof (uint16));
  53.         tf[2] = (uint16 *)_TIFFmalloc(tif, n * sizeof (uint16));
  54.         _TIFFmemcpy(tf[2], tf[0], n * sizeof (uint16));
  55.     }
  56. }
  57.  
  58. static void
  59. TIFFDefaultRefBlackWhite(TIFF* tif, TIFFDirectory* td)
  60. {
  61.     int i;
  62.  
  63.     td->td_refblackwhite = (float *)_TIFFmalloc(tif, 6*sizeof (float));
  64.     for (i = 0; i < 3; i++) {
  65.         td->td_refblackwhite[2*i+0] = 0;
  66.         td->td_refblackwhite[2*i+1] =(float)((1L<<td->td_bitspersample)-1L);
  67.     }
  68. }
  69. #endif
  70.  
  71. /*
  72.  * Like TIFFGetField, but return any default
  73.  * value if the tag is not present in the directory.
  74.  *
  75.  * NB:    We use the value in the directory, rather than
  76.  *    explcit values so that defaults exist only one
  77.  *    place in the library -- in TIFFDefaultDirectory.
  78.  */
  79. int
  80. TIFFVGetFieldDefaulted(TIFF* tif, ttag_t tag, va_list ap)
  81. {
  82.     TIFFDirectory *td = &tif->tif_dir;
  83.  
  84.     if (TIFFVGetField(tif, tag, ap))
  85.         return (1);
  86.     switch (tag) {
  87.     case TIFFTAG_SUBFILETYPE:
  88.         *va_arg(ap, uint32 *) = td->td_subfiletype;
  89.         return (1);
  90.     case TIFFTAG_BITSPERSAMPLE:
  91.         *va_arg(ap, uint16 *) = td->td_bitspersample;
  92.         return (1);
  93.     case TIFFTAG_THRESHHOLDING:
  94.         *va_arg(ap, uint16 *) = td->td_threshholding;
  95.         return (1);
  96.     case TIFFTAG_FILLORDER:
  97.         *va_arg(ap, uint16 *) = td->td_fillorder;
  98.         return (1);
  99.     case TIFFTAG_ORIENTATION:
  100.         *va_arg(ap, uint16 *) = td->td_orientation;
  101.         return (1);
  102.     case TIFFTAG_SAMPLESPERPIXEL:
  103.         *va_arg(ap, uint16 *) = td->td_samplesperpixel;
  104.         return (1);
  105.     case TIFFTAG_ROWSPERSTRIP:
  106.         *va_arg(ap, uint32 *) = td->td_rowsperstrip;
  107.         return (1);
  108.     case TIFFTAG_MINSAMPLEVALUE:
  109.         *va_arg(ap, uint16 *) = td->td_minsamplevalue;
  110.         return (1);
  111.     case TIFFTAG_MAXSAMPLEVALUE:
  112.         *va_arg(ap, uint16 *) = td->td_maxsamplevalue;
  113.         return (1);
  114.     case TIFFTAG_PLANARCONFIG:
  115.         *va_arg(ap, uint16 *) = td->td_planarconfig;
  116.         return (1);
  117.     case TIFFTAG_RESOLUTIONUNIT:
  118.         *va_arg(ap, uint16 *) = td->td_resolutionunit;
  119.         return (1);
  120. #ifdef CMYK_SUPPORT
  121.     case TIFFTAG_DOTRANGE:
  122.         *va_arg(ap, uint16 *) = 0;
  123.         *va_arg(ap, uint16 *) = (1<<td->td_bitspersample)-1;
  124.         return (1);
  125.     case TIFFTAG_INKSET:
  126.         *va_arg(ap, uint16 *) = td->td_inkset;
  127.         return (1);
  128.     case TIFFTAG_NUMBEROFINKS:
  129.         *va_arg(ap, uint16 *) = td->td_ninks;
  130.         return (1);
  131. #endif
  132.     case TIFFTAG_EXTRASAMPLES:
  133.         *va_arg(ap, uint16 *) = td->td_extrasamples;
  134.         *va_arg(ap, uint16 **) = td->td_sampleinfo;
  135.         return (1);
  136.     case TIFFTAG_MATTEING:
  137.         *va_arg(ap, uint16 *) =
  138.             (td->td_extrasamples == 1 &&
  139.              td->td_sampleinfo[0] == EXTRASAMPLE_ASSOCALPHA);
  140.         return (1);
  141.     case TIFFTAG_TILEDEPTH:
  142.         *va_arg(ap, uint32 *) = td->td_tiledepth;
  143.         return (1);
  144.     case TIFFTAG_DATATYPE:
  145.         *va_arg(ap, uint16 *) = td->td_sampleformat-1;
  146.         return (1);
  147.     case TIFFTAG_SAMPLEFORMAT:
  148.         *va_arg(ap, uint16 *) = td->td_sampleformat;
  149.                 return(1);
  150.     case TIFFTAG_IMAGEDEPTH:
  151.         *va_arg(ap, uint32 *) = td->td_imagedepth;
  152.         return (1);
  153. #ifdef YCBCR_SUPPORT
  154.     case TIFFTAG_YCBCRCOEFFICIENTS:
  155.         if (!td->td_ycbcrcoeffs) {
  156.             td->td_ycbcrcoeffs = (float *)
  157.                 _TIFFmalloc(tif, 3*sizeof (float));
  158.             /* defaults are from CCIR Recommendation 601-1 */
  159.             td->td_ycbcrcoeffs[0] = 0.299f;
  160.             td->td_ycbcrcoeffs[1] = 0.587f;
  161.             td->td_ycbcrcoeffs[2] = 0.114f;
  162.         }
  163.         *va_arg(ap, float **) = td->td_ycbcrcoeffs;
  164.         return (1);
  165.     case TIFFTAG_YCBCRSUBSAMPLING:
  166.         *va_arg(ap, uint16 *) = td->td_ycbcrsubsampling[0];
  167.         *va_arg(ap, uint16 *) = td->td_ycbcrsubsampling[1];
  168.         return (1);
  169.     case TIFFTAG_YCBCRPOSITIONING:
  170.         *va_arg(ap, uint16 *) = td->td_ycbcrpositioning;
  171.         return (1);
  172. #endif
  173. #ifdef COLORIMETRY_SUPPORT
  174.     case TIFFTAG_TRANSFERFUNCTION:
  175.         if (!td->td_transferfunction[0])
  176.             TIFFDefaultTransferFunction(tif, td);
  177.         *va_arg(ap, uint16 **) = td->td_transferfunction[0];
  178.         if (td->td_samplesperpixel - td->td_extrasamples > 1) {
  179.             *va_arg(ap, uint16 **) = td->td_transferfunction[1];
  180.             *va_arg(ap, uint16 **) = td->td_transferfunction[2];
  181.         }
  182.         return (1);
  183.     case TIFFTAG_REFERENCEBLACKWHITE:
  184.         if (!td->td_refblackwhite)
  185.             TIFFDefaultRefBlackWhite(tif, td);
  186.         *va_arg(ap, float **) = td->td_refblackwhite;
  187.         return (1);
  188. #endif
  189.     }
  190.     return (0);
  191. }
  192.  
  193. /*
  194.  * Like TIFFGetField, but return any default
  195.  * value if the tag is not present in the directory.
  196.  */
  197. int
  198. TIFFGetFieldDefaulted(TIFF* tif, ttag_t tag, ...)
  199. {
  200.     int ok;
  201.     va_list ap;
  202.  
  203.     va_start(ap, tag);
  204.     ok =  TIFFVGetFieldDefaulted(tif, tag, ap);
  205.     va_end(ap);
  206.     return (ok);
  207. }
  208.