home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / NETWORK / netpbm_src.lzh / NETPBM / LIBTIFF / tif_aux.c < prev    next >
Text File  |  1996-11-18  |  6KB  |  205 lines

  1. #ifndef lint
  2. static char rcsid[] = "$Header: /usr/people/sam/tiff/libtiff/RCS/tif_aux.c,v 1.21 93/08/26 14:19:50 sam Exp $";
  3. #endif
  4.  
  5. /*
  6.  * Copyright (c) 1991, 1992 Sam Leffler
  7.  * Copyright (c) 1991, 1992 Silicon Graphics, Inc.
  8.  *
  9.  * Permission to use, copy, modify, distribute, and sell this software and 
  10.  * its documentation for any purpose is hereby granted without fee, provided
  11.  * that (i) the above copyright notices and this permission notice appear in
  12.  * all copies of the software and related documentation, and (ii) the names of
  13.  * Sam Leffler and Silicon Graphics may not be used in any advertising or
  14.  * publicity relating to the software without the specific, prior written
  15.  * permission of Sam Leffler and Silicon Graphics.
  16.  * 
  17.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, 
  18.  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 
  19.  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  
  20.  * 
  21.  * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
  22.  * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  23.  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  24.  * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF 
  25.  * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 
  26.  * OF THIS SOFTWARE.
  27.  */
  28.  
  29. /*
  30.  * TIFF Library.
  31.  *
  32.  * Auxiliary Support Routines.
  33.  */
  34. #include "tiffiop.h"
  35.  
  36. #ifdef COLORIMETRY_SUPPORT
  37. #include <math.h>
  38.  
  39. static void
  40. TIFFDefaultTransferFunction(TIFFDirectory* td)
  41. {
  42.     uint16 **tf = td->td_transferfunction;
  43.     long i, n = 1<<td->td_bitspersample;
  44.  
  45.     tf[0] = (uint16 *)_TIFFmalloc(n * sizeof (uint16));
  46.     tf[0][0] = 0;
  47.     for (i = 1; i < n; i++)
  48.         tf[0][i] = (uint16)floor(65535.*pow(i/(n-1.), 2.2) + .5);
  49.     if (td->td_samplesperpixel - td->td_extrasamples > 1) {
  50.         tf[1] = (uint16 *)_TIFFmalloc(n * sizeof (uint16));
  51.         memcpy(tf[1], tf[0], n * sizeof (uint16));
  52.         tf[2] = (uint16 *)_TIFFmalloc(n * sizeof (uint16));
  53.         memcpy(tf[2], tf[0], n * sizeof (uint16));
  54.     }
  55. }
  56.  
  57. static void
  58. TIFFDefaultRefBlackWhite(TIFFDirectory* td)
  59. {
  60.     int i;
  61.  
  62.     td->td_refblackwhite = (float *)_TIFFmalloc(6*sizeof (float));
  63.     for (i = 0; i < 3; i++) {
  64.         td->td_refblackwhite[2*i+0] = 0;
  65.         td->td_refblackwhite[2*i+1] = 1L<<td->td_bitspersample;
  66.     }
  67. }
  68. #endif
  69.  
  70. /*
  71.  * Like TIFFGetField, but return any default
  72.  * value if the tag is not present in the directory.
  73.  *
  74.  * NB:    We use the value in the directory, rather than
  75.  *    explcit values so that defaults exist only one
  76.  *    place in the library -- in TIFFDefaultDirectory.
  77.  */
  78. TIFFVGetFieldDefaulted(TIFF* tif, ttag_t tag, va_list ap)
  79. {
  80.     TIFFDirectory *td = &tif->tif_dir;
  81.  
  82.     if (TIFFVGetField(tif, tag, ap))
  83.         return (1);
  84.     switch (tag) {
  85.     case TIFFTAG_SUBFILETYPE:
  86.         *va_arg(ap, uint16 *) = td->td_subfiletype;
  87.         return (1);
  88.     case TIFFTAG_BITSPERSAMPLE:
  89.         *va_arg(ap, uint16 *) = td->td_bitspersample;
  90.         return (1);
  91.     case TIFFTAG_THRESHHOLDING:
  92.         *va_arg(ap, uint16 *) = td->td_threshholding;
  93.         return (1);
  94.     case TIFFTAG_FILLORDER:
  95.         *va_arg(ap, uint16 *) = td->td_fillorder;
  96.         return (1);
  97.     case TIFFTAG_ORIENTATION:
  98.         *va_arg(ap, uint16 *) = td->td_orientation;
  99.         return (1);
  100.     case TIFFTAG_SAMPLESPERPIXEL:
  101.         *va_arg(ap, uint16 *) = td->td_samplesperpixel;
  102.         return (1);
  103.     case TIFFTAG_ROWSPERSTRIP:
  104.         *va_arg(ap, uint32 *) = td->td_rowsperstrip;
  105.         return (1);
  106.     case TIFFTAG_MINSAMPLEVALUE:
  107.         *va_arg(ap, uint16 *) = td->td_minsamplevalue;
  108.         return (1);
  109.     case TIFFTAG_MAXSAMPLEVALUE:
  110.         *va_arg(ap, uint16 *) = td->td_maxsamplevalue;
  111.         return (1);
  112.     case TIFFTAG_PLANARCONFIG:
  113.         *va_arg(ap, uint16 *) = td->td_planarconfig;
  114.         return (1);
  115.     case TIFFTAG_GROUP4OPTIONS:
  116.         *va_arg(ap, uint32 *) = td->td_group4options;
  117.         return (1);
  118.     case TIFFTAG_RESOLUTIONUNIT:
  119.         *va_arg(ap, uint16 *) = td->td_resolutionunit;
  120.         return (1);
  121.     case TIFFTAG_PREDICTOR:
  122.         *va_arg(ap, uint16 *) = td->td_predictor;
  123.         return (1);
  124. #ifdef CMYK_SUPPORT
  125.     case TIFFTAG_DOTRANGE:
  126.         *va_arg(ap, uint16 *) = 0;
  127.         *va_arg(ap, uint16 *) = (1<<td->td_bitspersample)-1;
  128.         return (1);
  129.     case TIFFTAG_INKSET:
  130.         *va_arg(ap, uint16 *) = td->td_inkset;
  131.         return (1);
  132. #endif
  133.     case TIFFTAG_EXTRASAMPLES:
  134.         *va_arg(ap, uint16 *) = td->td_extrasamples;
  135.         *va_arg(ap, uint16 **) = td->td_sampleinfo;
  136.         return (1);
  137.     case TIFFTAG_MATTEING:
  138.         *va_arg(ap, uint16 *) =
  139.             (td->td_extrasamples == 1 &&
  140.              td->td_sampleinfo[0] == EXTRASAMPLE_ASSOCALPHA);
  141.         return (1);
  142.     case TIFFTAG_TILEDEPTH:
  143.         *va_arg(ap, uint32 *) = td->td_tiledepth;
  144.         return (1);
  145.     case TIFFTAG_DATATYPE:
  146.         *va_arg(ap, uint16 *) = td->td_sampleformat-1;
  147.         return (1);
  148.     case TIFFTAG_IMAGEDEPTH:
  149.         *va_arg(ap, uint16 *) = td->td_imagedepth;
  150.         return (1);
  151. #ifdef YCBCR_SUPPORT
  152.     case TIFFTAG_YCBCRCOEFFICIENTS:
  153.         if (!td->td_ycbcrcoeffs) {
  154.             td->td_ycbcrcoeffs = (float *)
  155.                 _TIFFmalloc(3*sizeof (float));
  156.             /* defaults are from CCIR Recommendation 601-1 */
  157.             td->td_ycbcrcoeffs[0] = .299;
  158.             td->td_ycbcrcoeffs[1] = .587;
  159.             td->td_ycbcrcoeffs[2] = .114;
  160.         }
  161.         *va_arg(ap, float **) = td->td_ycbcrcoeffs;
  162.         return (1);
  163.     case TIFFTAG_YCBCRSUBSAMPLING:
  164.         *va_arg(ap, uint16 *) = td->td_ycbcrsubsampling[0];
  165.         *va_arg(ap, uint16 *) = td->td_ycbcrsubsampling[1];
  166.         return (1);
  167.     case TIFFTAG_YCBCRPOSITIONING:
  168.         *va_arg(ap, uint16 *) = td->td_ycbcrpositioning;
  169.         return (1);
  170. #endif
  171. #ifdef COLORIMETRY_SUPPORT
  172.     case TIFFTAG_TRANSFERFUNCTION:
  173.         if (!td->td_transferfunction[0])
  174.             TIFFDefaultTransferFunction(td);
  175.         *va_arg(ap, uint16 **) = td->td_transferfunction[0];
  176.         if (td->td_samplesperpixel - td->td_extrasamples > 1) {
  177.             *va_arg(ap, uint16 **) = td->td_transferfunction[1];
  178.             *va_arg(ap, uint16 **) = td->td_transferfunction[2];
  179.         }
  180.         return (1);
  181.     case TIFFTAG_REFERENCEBLACKWHITE:
  182.         if (!td->td_refblackwhite)
  183.             TIFFDefaultRefBlackWhite(td);
  184.         *va_arg(ap, float **) = td->td_refblackwhite;
  185.         return (1);
  186. #endif
  187.     }
  188.     return (0);
  189. }
  190.  
  191. /*
  192.  * Like TIFFGetField, but return any default
  193.  * value if the tag is not present in the directory.
  194.  */
  195. TIFFGetFieldDefaulted(TIFF* tif, ttag_t tag, ...)
  196. {
  197.     int ok;
  198.     va_list ap;
  199.  
  200.     va_start(ap, tag);
  201.     ok =  TIFFVGetFieldDefaulted(tif, tag, ap);
  202.     va_end(ap);
  203.     return (ok);
  204. }
  205.