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_dir.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-07-04  |  34.7 KB  |  1,264 lines  |  [TEXT/CWIE]

  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.  * Directory Tag Get & Set Routines.
  29.  * (and also some miscellaneous stuff)
  30.  */
  31.  
  32. /* $Id: tif_dir.c,v 1.9 2001/04/05 09:44:06 rjs Exp $ */
  33.  
  34. #include "tiffiop.h"
  35. #include "tif_predict.h"
  36.  
  37. #define    FIELD_PREDICTOR    (FIELD_CODEC+0)        /* XXX */
  38. #define    PredictorState(tif)    ((TIFFPredictorState*) (tif)->tif_data)
  39.  
  40. /*
  41.  * These are used in the backwards compatibility code...
  42.  */
  43. #define DATATYPE_VOID        0       /* !untyped data */
  44. #define DATATYPE_INT        1       /* !signed integer data */
  45. #define DATATYPE_UINT        2       /* !unsigned integer data */
  46. #define DATATYPE_IEEEFP        3       /* !IEEE floating point data */
  47.  
  48. void _TIFFsetNString(TIFF* tif, char** cpp, char* cp, long n);
  49. void _TIFFsetByteArray(TIFF* tif, void** vpp, void* vp, long n);
  50.  
  51. void
  52. _TIFFsetByteArray(TIFF* tif, void** vpp, void* vp, long n)
  53. {
  54.     if (*vpp)
  55.         _TIFFfree(tif, *vpp), *vpp = 0;
  56.     if (vp && (*vpp = (void*) _TIFFmalloc(tif, n)))
  57.         _TIFFmemcpy(*vpp, vp, n);
  58. }
  59. void _TIFFsetString(TIFF* tif, char** cpp, char* cp)
  60.     { _TIFFsetByteArray(tif, (void**) cpp, (void*) cp, (long) (strlen(cp)+1)); }
  61. void _TIFFsetNString(TIFF* tif, char** cpp, char* cp, long n)
  62.     { _TIFFsetByteArray(tif, (void**) cpp, (void*) cp, n); }
  63. void _TIFFsetShortArray(TIFF* tif, uint16** wpp, uint16* wp, long n)
  64.     { _TIFFsetByteArray(tif, (void**) wpp, (void*) wp, n*sizeof (uint16)); }
  65. void _TIFFsetLongArray(TIFF* tif, uint32** lpp, uint32* lp, long n)
  66.     { _TIFFsetByteArray(tif, (void**) lpp, (void*) lp, n*sizeof (uint32)); }
  67. void _TIFFsetFloatArray(TIFF* tif, float** fpp, float* fp, long n)
  68.     { _TIFFsetByteArray(tif, (void**) fpp, (void*) fp, n*sizeof (float)); }
  69. void _TIFFsetDoubleArray(TIFF* tif, double** dpp, double* dp, long n)
  70.     { _TIFFsetByteArray(tif, (void**) dpp, (void*) dp, n*sizeof (double)); }
  71.  
  72. /*
  73.  * Install extra samples information.
  74.  */
  75. static int
  76. setExtraSamples(TIFF* tif, TIFFDirectory* td, va_list ap, int* v)
  77. {
  78.     uint16* va;
  79.     int i;
  80.  
  81.     *v = va_arg(ap, int);
  82.     if ((uint16) *v > td->td_samplesperpixel)
  83.         return (0);
  84.     va = va_arg(ap, uint16*);
  85.     if (*v > 0 && va == NULL)        /* typically missing param */
  86.         return (0);
  87.     for (i = 0; i < *v; i++)
  88.         if (va[i] > EXTRASAMPLE_UNASSALPHA)
  89.             return (0);
  90.     td->td_extrasamples = (uint16) *v;
  91.     _TIFFsetShortArray(tif, &td->td_sampleinfo, va, td->td_extrasamples);
  92.     return (1);
  93. }
  94.  
  95. #ifdef CMYK_SUPPORT
  96. static int
  97. checkInkNamesString(TIFF* tif, int slen, const char* s)
  98. {
  99.     TIFFDirectory* td = &tif->tif_dir;
  100.     int i = td->td_samplesperpixel;
  101.  
  102.     if (slen > 0) {
  103.         const char* ep = s+slen;
  104.         const char* cp = s;
  105.         for (; i > 0; i--) {
  106.             for (; *cp != '\0'; cp++)
  107.                 if (cp >= ep)
  108.                     goto bad;
  109.             cp++;                /* skip \0 */
  110.         }
  111.         return (cp-s);
  112.     }
  113. bad:
  114.     TIFFError("TIFFSetField",
  115.         "%s: Invalid InkNames value; expecting %d names, found %d",
  116.         tif->tif_name,
  117.         td->td_samplesperpixel,
  118.         td->td_samplesperpixel-i);
  119.     return (0);
  120. }
  121. #endif
  122.  
  123. static int
  124. _TIFFVSetField(TIFF* tif, ttag_t tag, va_list ap)
  125. {
  126.     TIFFDirectory* td = &tif->tif_dir;
  127.     TIFFPredictorState *sp;
  128.     int status = 1;
  129.     uint32 v32;
  130.     int i, v;
  131.     double d;
  132.     char* s;
  133.  
  134.     switch (tag) {
  135.     case TIFFTAG_SUBFILETYPE:
  136.         td->td_subfiletype = va_arg(ap, uint32);
  137.         break;
  138.     case TIFFTAG_IMAGEWIDTH:
  139.         td->td_imagewidth = va_arg(ap, uint32);
  140.         break;
  141.     case TIFFTAG_IMAGELENGTH:
  142.         td->td_imagelength = va_arg(ap, uint32);
  143.         break;
  144.     case TIFFTAG_BITSPERSAMPLE:
  145.         td->td_bitspersample = (uint16) va_arg(ap, int);
  146.         /*
  147.          * If the data require post-decoding processing
  148.          * to byte-swap samples, set it up here.  Note
  149.          * that since tags are required to be ordered,
  150.          * compression code can override this behaviour
  151.          * in the setup method if it wants to roll the
  152.          * post decoding work in with its normal work.
  153.          */
  154.         if (tif->tif_flags & TIFF_SWAB) {
  155.             if (td->td_bitspersample == 16)
  156.                 tif->tif_postdecode = _TIFFSwab16BitData;
  157.             else if (td->td_bitspersample == 32)
  158.                 tif->tif_postdecode = _TIFFSwab32BitData;
  159.             else if (td->td_bitspersample == 64)
  160.                 tif->tif_postdecode = _TIFFSwab64BitData;
  161.         }
  162.         break;
  163.     case TIFFTAG_COMPRESSION:
  164.         v = va_arg(ap, int) & 0xffff;
  165.         /*
  166.          * If we're changing the compression scheme,
  167.          * the notify the previous module so that it
  168.          * can cleanup any state it's setup.
  169.          */
  170.         if (TIFFFieldSet(tif, FIELD_COMPRESSION)) {
  171.             if (td->td_compression == v)
  172.                 break;
  173.             (*tif->tif_cleanup)(tif);
  174.             tif->tif_flags &= ~TIFF_CODERSETUP;
  175.         }
  176.         /*
  177.          * Setup new compression routine state.
  178.          */
  179.         if ( ! tif->tif_mode == O_RDONLY ) { 
  180.           /* Handle removal of LZW compression */ 
  181.           if ( v == COMPRESSION_LZW ) { 
  182.             TIFFError(tif->tif_name, 
  183.       "LZW compression no longer supported due to Unisys patent enforcement"); 
  184.             v=COMPRESSION_NONE;
  185.           }
  186.         }
  187.         if( (status = TIFFSetCompressionScheme(tif, v)) != 0 )
  188.           td->td_compression = v;
  189.         break;
  190.     case TIFFTAG_PHOTOMETRIC:
  191.         td->td_photometric = (uint16) va_arg(ap, int);
  192.         break;
  193.     case TIFFTAG_THRESHHOLDING:
  194.         td->td_threshholding = (uint16) va_arg(ap, int);
  195.         break;
  196.     case TIFFTAG_FILLORDER:
  197.         v = va_arg(ap, int);
  198.         if (v != FILLORDER_LSB2MSB && v != FILLORDER_MSB2LSB)
  199.             goto badvalue;
  200.         td->td_fillorder = (uint16) v;
  201.         break;
  202.     case TIFFTAG_DOCUMENTNAME:
  203.         _TIFFsetString(tif, &td->td_documentname, va_arg(ap, char*));
  204.         break;
  205.     case TIFFTAG_ARTIST:
  206.         _TIFFsetString(tif, &td->td_artist, va_arg(ap, char*));
  207.         break;
  208.     case TIFFTAG_DATETIME:
  209.         _TIFFsetString(tif, &td->td_datetime, va_arg(ap, char*));
  210.         break;
  211.     case TIFFTAG_HOSTCOMPUTER:
  212.         _TIFFsetString(tif, &td->td_hostcomputer, va_arg(ap, char*));
  213.         break;
  214.     case TIFFTAG_IMAGEDESCRIPTION:
  215.         _TIFFsetString(tif,&td->td_imagedescription, va_arg(ap, char*));
  216.         break;
  217.     case TIFFTAG_MAKE:
  218.         _TIFFsetString(tif, &td->td_make, va_arg(ap, char*));
  219.         break;
  220.     case TIFFTAG_MODEL:
  221.         _TIFFsetString(tif, &td->td_model, va_arg(ap, char*));
  222.         break;
  223.     case TIFFTAG_SOFTWARE:
  224.         _TIFFsetString(tif, &td->td_software, va_arg(ap, char*));
  225.         break;
  226.     case TIFFTAG_ORIENTATION:
  227.         v = va_arg(ap, int);
  228.         if (v < ORIENTATION_TOPLEFT || ORIENTATION_LEFTBOT < v) {
  229.             TIFFWarning(tif->tif_name,
  230.                 "Bad value %ld for \"%s\" tag ignored",
  231.                 v, _TIFFFieldWithTag(tif, tag)->field_name);
  232.         } else
  233.             td->td_orientation = (uint16) v;
  234.         break;
  235.     case TIFFTAG_SAMPLESPERPIXEL:
  236.         /* XXX should cross check -- e.g. if pallette, then 1 */
  237.         v = va_arg(ap, int);
  238.         if (v == 0)
  239.             goto badvalue;
  240.         td->td_samplesperpixel = (uint16) v;
  241.         break;
  242.     case TIFFTAG_ROWSPERSTRIP:
  243.         v32 = va_arg(ap, uint32);
  244.         if (v32 == 0)
  245.             goto badvalue32;
  246.         td->td_rowsperstrip = v32;
  247.         if (!TIFFFieldSet(tif, FIELD_TILEDIMENSIONS)) {
  248.             td->td_tilelength = v32;
  249.             td->td_tilewidth = td->td_imagewidth;
  250.         }
  251.         break;
  252.     case TIFFTAG_MINSAMPLEVALUE:
  253.         td->td_minsamplevalue = (uint16) va_arg(ap, int);
  254.         break;
  255.     case TIFFTAG_MAXSAMPLEVALUE:
  256.         td->td_maxsamplevalue = (uint16) va_arg(ap, int);
  257.         break;
  258.     case TIFFTAG_SMINSAMPLEVALUE:
  259.         td->td_sminsamplevalue = (double) va_arg(ap, dblparam_t);
  260.         break;
  261.     case TIFFTAG_SMAXSAMPLEVALUE:
  262.         td->td_smaxsamplevalue = (double) va_arg(ap, dblparam_t);
  263.         break;
  264.     case TIFFTAG_XRESOLUTION:
  265.         td->td_xresolution = (float) va_arg(ap, dblparam_t);
  266.         break;
  267.     case TIFFTAG_YRESOLUTION:
  268.         td->td_yresolution = (float) va_arg(ap, dblparam_t);
  269.         break;
  270.     case TIFFTAG_PLANARCONFIG:
  271.         v = va_arg(ap, int);
  272.         if (v != PLANARCONFIG_CONTIG && v != PLANARCONFIG_SEPARATE)
  273.             goto badvalue;
  274.         td->td_planarconfig = (uint16) v;
  275.         break;
  276.     case TIFFTAG_PAGENAME:
  277.         _TIFFsetString(tif, &td->td_pagename, va_arg(ap, char*));
  278.         break;
  279.     case TIFFTAG_XPOSITION:
  280.         td->td_xposition = (float) va_arg(ap, dblparam_t);
  281.         break;
  282.     case TIFFTAG_YPOSITION:
  283.         td->td_yposition = (float) va_arg(ap, dblparam_t);
  284.         break;
  285.     case TIFFTAG_RESOLUTIONUNIT:
  286.         v = va_arg(ap, int);
  287.         if (v < RESUNIT_NONE || RESUNIT_CENTIMETER < v)
  288.             goto badvalue;
  289.         td->td_resolutionunit = (uint16) v;
  290.         break;
  291.     case TIFFTAG_PAGENUMBER:
  292.         td->td_pagenumber[0] = (uint16) va_arg(ap, int);
  293.         td->td_pagenumber[1] = (uint16) va_arg(ap, int);
  294.         break;
  295.     case TIFFTAG_HALFTONEHINTS:
  296.         td->td_halftonehints[0] = (uint16) va_arg(ap, int);
  297.         td->td_halftonehints[1] = (uint16) va_arg(ap, int);
  298.         break;
  299.     case TIFFTAG_COLORMAP:
  300.         v32 = (uint32)(1L<<td->td_bitspersample);
  301.         _TIFFsetShortArray(tif, &td->td_colormap[0],
  302.                 va_arg(ap,uint16*), v32);
  303.         _TIFFsetShortArray(tif, &td->td_colormap[1],
  304.                 va_arg(ap,uint16*), v32);
  305.         _TIFFsetShortArray(tif, &td->td_colormap[2],
  306.                 va_arg(ap,uint16*), v32);
  307.         break;
  308.     case TIFFTAG_EXTRASAMPLES:
  309.         if (!setExtraSamples(tif, td, ap, &v))
  310.             goto badvalue;
  311.         break;
  312.     case TIFFTAG_MATTEING:
  313.         td->td_extrasamples = (uint16) (va_arg(ap, int) != 0);
  314.         if (td->td_extrasamples) {
  315.             uint16 sv = EXTRASAMPLE_ASSOCALPHA;
  316.             _TIFFsetShortArray(tif, &td->td_sampleinfo, &sv, 1);
  317.         }
  318.         break;
  319.     case TIFFTAG_TILEWIDTH:
  320.         v32 = va_arg(ap, uint32);
  321.         if (v32 % 16) {
  322.             if (tif->tif_mode != O_RDONLY)
  323.                 goto badvalue32;
  324.             TIFFWarning(tif->tif_name,
  325.                 "Nonstandard tile width %d, convert file", v32);
  326.         }
  327.         td->td_tilewidth = v32;
  328.         tif->tif_flags |= TIFF_ISTILED;
  329.         break;
  330.     case TIFFTAG_TILELENGTH:
  331.         v32 = va_arg(ap, uint32);
  332.         if (v32 % 16) {
  333.             if (tif->tif_mode != O_RDONLY)
  334.                 goto badvalue32;
  335.             TIFFWarning(tif->tif_name,
  336.                 "Nonstandard tile length %d, convert file", v32);
  337.         }
  338.         td->td_tilelength = v32;
  339.         tif->tif_flags |= TIFF_ISTILED;
  340.         break;
  341.     case TIFFTAG_TILEDEPTH:
  342.         v32 = va_arg(ap, uint32);
  343.         if (v32 == 0)
  344.             goto badvalue32;
  345.         td->td_tiledepth = v32;
  346.         break;
  347.     case TIFFTAG_DATATYPE:
  348.         v = va_arg(ap, int);
  349.         switch (v) {
  350.         case DATATYPE_VOID:    v = SAMPLEFORMAT_VOID;    break;
  351.         case DATATYPE_INT:    v = SAMPLEFORMAT_INT;    break;
  352.         case DATATYPE_UINT:    v = SAMPLEFORMAT_UINT;    break;
  353.         case DATATYPE_IEEEFP:    v = SAMPLEFORMAT_IEEEFP;break;
  354.         default:        goto badvalue;
  355.         }
  356.         td->td_sampleformat = (uint16) v;
  357.         break;
  358.     case TIFFTAG_SAMPLEFORMAT:
  359.         v = va_arg(ap, int);
  360.         if (v < SAMPLEFORMAT_UINT || SAMPLEFORMAT_COMPLEXIEEEFP < v)
  361.             goto badvalue;
  362.         td->td_sampleformat = (uint16) v;
  363.         break;
  364.     case TIFFTAG_IMAGEDEPTH:
  365.         td->td_imagedepth = va_arg(ap, uint32);
  366.         break;
  367.     case TIFFTAG_STONITS:
  368.         d = va_arg(ap, dblparam_t);
  369.         if (d <= 0.)
  370.             goto badvaluedbl;
  371.         td->td_stonits = d;
  372.         break;
  373.  
  374.     /* Begin Pixar Tags */
  375.      case TIFFTAG_PIXAR_IMAGEFULLWIDTH:
  376.          td->td_imagefullwidth = va_arg(ap, uint32);
  377.          break;
  378.      case TIFFTAG_PIXAR_IMAGEFULLLENGTH:
  379.          td->td_imagefulllength = va_arg(ap, uint32);
  380.          break;
  381.      case TIFFTAG_PIXAR_TEXTUREFORMAT:
  382.          _TIFFsetString(tif, &td->td_textureformat, va_arg(ap, char*));
  383.          break;
  384.      case TIFFTAG_PIXAR_WRAPMODES:
  385.          _TIFFsetString(tif, &td->td_wrapmodes, va_arg(ap, char*));
  386.          break;
  387.      case TIFFTAG_PIXAR_FOVCOT:
  388.          td->td_fovcot = (float) va_arg(ap, dblparam_t);
  389.          break;
  390.      case TIFFTAG_PIXAR_MATRIX_WORLDTOSCREEN:
  391.          _TIFFsetFloatArray(tif, &td->td_matrixWorldToScreen,
  392.              va_arg(ap, float*), 16);
  393.          break;
  394.      case TIFFTAG_PIXAR_MATRIX_WORLDTOCAMERA:
  395.          _TIFFsetFloatArray(tif, &td->td_matrixWorldToCamera,
  396.              va_arg(ap, float*), 16);
  397.          break;
  398.      /* End Pixar Tags */           
  399.  
  400. #if SUBIFD_SUPPORT
  401.     case TIFFTAG_SUBIFD:
  402.         if ((tif->tif_flags & TIFF_INSUBIFD) == 0) {
  403.             td->td_nsubifd = (uint16) va_arg(ap, int);
  404.             _TIFFsetLongArray(tif, &td->td_subifd,
  405.                 va_arg(ap, uint32*), (long) td->td_nsubifd);
  406.         } else {
  407.             TIFFError(tif->tif_name, "Sorry, cannot nest SubIFDs");
  408.             status = 0;
  409.         }
  410.         break;
  411. #endif
  412. #ifdef YCBCR_SUPPORT
  413.     case TIFFTAG_YCBCRCOEFFICIENTS:
  414.         _TIFFsetFloatArray(tif, &td->td_ycbcrcoeffs,
  415.             va_arg(ap, float*), 3);
  416.         break;
  417.     case TIFFTAG_YCBCRPOSITIONING:
  418.         td->td_ycbcrpositioning = (uint16) va_arg(ap, int);
  419.         break;
  420.     case TIFFTAG_YCBCRSUBSAMPLING:
  421.         td->td_ycbcrsubsampling[0] = (uint16) va_arg(ap, int);
  422.         td->td_ycbcrsubsampling[1] = (uint16) va_arg(ap, int);
  423.         break;
  424. #endif
  425.     case TIFFTAG_PREDICTOR:
  426.         sp = PredictorState(tif);
  427.         sp->predictor = (uint16) va_arg(ap, int);
  428.         TIFFSetFieldBit(tif, FIELD_PREDICTOR);
  429.         break;
  430.     
  431. #ifdef COLORIMETRY_SUPPORT
  432.     case TIFFTAG_WHITEPOINT:
  433.         _TIFFsetFloatArray(tif, &td->td_whitepoint,
  434.                 va_arg(ap, float*), 2);
  435.         break;
  436.     case TIFFTAG_PRIMARYCHROMATICITIES:
  437.         _TIFFsetFloatArray(tif, &td->td_primarychromas,
  438.                 va_arg(ap,float*), 6);
  439.         break;
  440.     case TIFFTAG_TRANSFERFUNCTION:
  441.         v = (td->td_samplesperpixel - td->td_extrasamples) > 1 ? 3 : 1;
  442.         for (i = 0; i < v; i++)
  443.             _TIFFsetShortArray(tif, &td->td_transferfunction[i],
  444.                 va_arg(ap, uint16*), 1L<<td->td_bitspersample);
  445.         break;
  446.     case TIFFTAG_REFERENCEBLACKWHITE:
  447.         /* XXX should check for null range */
  448.         _TIFFsetFloatArray(tif, &td->td_refblackwhite,
  449.                 va_arg(ap,float*), 6);
  450.         break;
  451. #endif
  452. #ifdef CMYK_SUPPORT
  453.     case TIFFTAG_INKSET:
  454.         td->td_inkset = (uint16) va_arg(ap, int);
  455.         break;
  456.     case TIFFTAG_DOTRANGE:
  457.         /* XXX should check for null range */
  458.         td->td_dotrange[0] = (uint16) va_arg(ap, int);
  459.         td->td_dotrange[1] = (uint16) va_arg(ap, int);
  460.         break;
  461.     case TIFFTAG_INKNAMES:
  462.         i = va_arg(ap, int);
  463.         s = va_arg(ap, char*);
  464.         i = checkInkNamesString(tif, i, s);
  465.                 status = i > 0;
  466.         if( i > 0 ) {
  467.             _TIFFsetNString(tif, &td->td_inknames, s, i);
  468.             td->td_inknameslen = i;
  469.         }
  470.         break;
  471.     case TIFFTAG_NUMBEROFINKS:
  472.         td->td_ninks = (uint16) va_arg(ap, int);
  473.         break;
  474.     case TIFFTAG_TARGETPRINTER:
  475.         _TIFFsetString(tif, &td->td_targetprinter, va_arg(ap, char*));
  476.         break;
  477. #endif
  478. #ifdef ICC_SUPPORT
  479.     case TIFFTAG_ICCPROFILE:
  480.         td->td_profileLength = (uint32) va_arg(ap, uint32);
  481.         _TIFFsetByteArray(tif, &td->td_profileData, va_arg(ap, void*),
  482.             td->td_profileLength);
  483.         break;
  484. #endif
  485. #ifdef PHOTOSHOP_SUPPORT
  486.      case TIFFTAG_PHOTOSHOP:
  487.           td->td_photoshopLength = (uint32) va_arg(ap, uint32);
  488.           _TIFFsetByteArray (tif, &td->td_photoshopData,
  489.             va_arg(ap, void*), td->td_photoshopLength);
  490.          break;
  491. #endif
  492. #ifdef IPTC_SUPPORT
  493.     case TIFFTAG_RICHTIFFIPTC: 
  494.           td->td_richtiffiptcLength = (uint32) va_arg(ap, uint32);
  495. #ifdef PHOTOSHOP_SUPPORT
  496.           _TIFFsetLongArray (tif, (uint32**)&td->td_richtiffiptcData,
  497.             va_arg(ap, uint32*), td->td_richtiffiptcLength);
  498. #else
  499.           _TIFFsetByteArray (tif, &td->td_photoshopData,
  500.             va_arg(ap, void*), td->td_photoshopLength);
  501. #endif
  502.          break;
  503. #endif
  504.     default:
  505.         /*
  506.          * This can happen if multiple images are open with
  507.          * different codecs which have private tags.  The
  508.          * global tag information table may then have tags
  509.          * that are valid for one file but not the other. 
  510.          * If the client tries to set a tag that is not valid
  511.          * for the image's codec then we'll arrive here.  This
  512.          * happens, for example, when tiffcp is used to convert
  513.          * between compression schemes and codec-specific tags
  514.          * are blindly copied.
  515.          */
  516.         TIFFError("TIFFSetField",
  517.             "%s: Invalid %stag \"%s\" (not supported by codec)",
  518.             tif->tif_name, isPseudoTag(tag) ? "pseduo-" : "",
  519.             _TIFFFieldWithTag(tif, tag)->field_name);
  520.         status = 0;
  521.         break;
  522.     }
  523.     if (status) {
  524.         TIFFSetFieldBit(tif, _TIFFFieldWithTag(tif, tag)->field_bit);
  525.         tif->tif_flags |= TIFF_DIRTYDIRECT;
  526.     }
  527.     va_end(ap);
  528.     return (status);
  529. badvalue:
  530.     TIFFError(tif->tif_name, "%d: Bad value for \"%s\"", v,
  531.         _TIFFFieldWithTag(tif, tag)->field_name);
  532.     va_end(ap);
  533.     return (0);
  534. badvalue32:
  535.     TIFFError(tif->tif_name, "%ld: Bad value for \"%s\"", v32,
  536.         _TIFFFieldWithTag(tif, tag)->field_name);
  537.     va_end(ap);
  538.     return (0);
  539. badvaluedbl:
  540.     TIFFError(tif->tif_name, "%f: Bad value for \"%s\"", d,
  541.         _TIFFFieldWithTag(tif, tag)->field_name);
  542.     va_end(ap);
  543.     return (0);
  544. }
  545.  
  546. /*
  547.  * Return 1/0 according to whether or not
  548.  * it is permissible to set the tag's value.
  549.  * Note that we allow ImageLength to be changed
  550.  * so that we can append and extend to images.
  551.  * Any other tag may not be altered once writing
  552.  * has commenced, unless its value has no effect
  553.  * on the format of the data that is written.
  554.  */
  555. static int
  556. OkToChangeTag(TIFF* tif, ttag_t tag)
  557. {
  558.     const TIFFFieldInfo* fip = _TIFFFindFieldInfo(tif, tag, TIFF_ANY);
  559.     if (!fip) {            /* unknown tag */
  560.         TIFFError("TIFFSetField", "%s: Unknown %stag %u",
  561.             tif->tif_name, isPseudoTag(tag) ? "pseudo-" : "", tag);
  562.         return (0);
  563.     }
  564.     if (tag != TIFFTAG_IMAGELENGTH && (tif->tif_flags & TIFF_BEENWRITING) &&
  565.         !fip->field_oktochange) {
  566.         /*
  567.          * Consult info table to see if tag can be changed
  568.          * after we've started writing.  We only allow changes
  569.          * to those tags that don't/shouldn't affect the
  570.          * compression and/or format of the data.
  571.          */
  572.         TIFFError("TIFFSetField",
  573.             "%s: Cannot modify tag \"%s\" while writing",
  574.             tif->tif_name, fip->field_name);
  575.         return (0);
  576.     }
  577.     return (1);
  578. }
  579.  
  580. /*
  581.  * Record the value of a field in the
  582.  * internal directory structure.  The
  583.  * field will be written to the file
  584.  * when/if the directory structure is
  585.  * updated.
  586.  */
  587. int
  588. TIFFSetField(TIFF* tif, ttag_t tag, ...)
  589. {
  590.     va_list ap;
  591.     int status;
  592.  
  593.     va_start(ap, tag);
  594.     status = TIFFVSetField(tif, tag, ap);
  595.     va_end(ap);
  596.     return (status);
  597. }
  598.  
  599. /*
  600.  * Like TIFFSetField, but taking a varargs
  601.  * parameter list.  This routine is useful
  602.  * for building higher-level interfaces on
  603.  * top of the library.
  604.  */
  605. int
  606. TIFFVSetField(TIFF* tif, ttag_t tag, va_list ap)
  607. {
  608.     return OkToChangeTag(tif, tag) ?
  609.         (*tif->tif_vsetfield)(tif, tag, ap) : 0;
  610. }
  611.  
  612. static int
  613. _TIFFVGetField(TIFF* tif, ttag_t tag, va_list ap)
  614. {
  615.     TIFFDirectory* td = &tif->tif_dir;
  616.     TIFFPredictorState *sp;
  617.  
  618.     switch (tag) {
  619.     case TIFFTAG_SUBFILETYPE:
  620.         *va_arg(ap, uint32*) = td->td_subfiletype;
  621.         break;
  622.     case TIFFTAG_IMAGEWIDTH:
  623.         *va_arg(ap, uint32*) = td->td_imagewidth;
  624.         break;
  625.     case TIFFTAG_IMAGELENGTH:
  626.         *va_arg(ap, uint32*) = td->td_imagelength;
  627.         break;
  628.     case TIFFTAG_BITSPERSAMPLE:
  629.         *va_arg(ap, uint16*) = td->td_bitspersample;
  630.         break;
  631.     case TIFFTAG_COMPRESSION:
  632.         *va_arg(ap, uint16*) = td->td_compression;
  633.         break;
  634.     case TIFFTAG_PHOTOMETRIC:
  635.         *va_arg(ap, uint16*) = td->td_photometric;
  636.         break;
  637.     case TIFFTAG_THRESHHOLDING:
  638.         *va_arg(ap, uint16*) = td->td_threshholding;
  639.         break;
  640.     case TIFFTAG_FILLORDER:
  641.         *va_arg(ap, uint16*) = td->td_fillorder;
  642.         break;
  643.     case TIFFTAG_DOCUMENTNAME:
  644.         *va_arg(ap, char**) = td->td_documentname;
  645.         break;
  646.     case TIFFTAG_ARTIST:
  647.         *va_arg(ap, char**) = td->td_artist;
  648.         break;
  649.     case TIFFTAG_DATETIME:
  650.         *va_arg(ap, char**) = td->td_datetime;
  651.         break;
  652.     case TIFFTAG_HOSTCOMPUTER:
  653.         *va_arg(ap, char**) = td->td_hostcomputer;
  654.         break;
  655.     case TIFFTAG_IMAGEDESCRIPTION:
  656.         *va_arg(ap, char**) = td->td_imagedescription;
  657.         break;
  658.     case TIFFTAG_MAKE:
  659.         *va_arg(ap, char**) = td->td_make;
  660.         break;
  661.     case TIFFTAG_MODEL:
  662.         *va_arg(ap, char**) = td->td_model;
  663.         break;
  664.     case TIFFTAG_SOFTWARE:
  665.         *va_arg(ap, char**) = td->td_software;
  666.         break;
  667.     case TIFFTAG_ORIENTATION:
  668.         *va_arg(ap, uint16*) = td->td_orientation;
  669.         break;
  670.     case TIFFTAG_SAMPLESPERPIXEL:
  671.         *va_arg(ap, uint16*) = td->td_samplesperpixel;
  672.         break;
  673.     case TIFFTAG_ROWSPERSTRIP:
  674.         *va_arg(ap, uint32*) = td->td_rowsperstrip;
  675.         break;
  676.     case TIFFTAG_MINSAMPLEVALUE:
  677.         *va_arg(ap, uint16*) = td->td_minsamplevalue;
  678.         break;
  679.     case TIFFTAG_MAXSAMPLEVALUE:
  680.         *va_arg(ap, uint16*) = td->td_maxsamplevalue;
  681.         break;
  682.     case TIFFTAG_SMINSAMPLEVALUE:
  683.         *va_arg(ap, double*) = td->td_sminsamplevalue;
  684.         break;
  685.     case TIFFTAG_SMAXSAMPLEVALUE:
  686.         *va_arg(ap, double*) = td->td_smaxsamplevalue;
  687.         break;
  688.     case TIFFTAG_XRESOLUTION:
  689.         *va_arg(ap, float*) = td->td_xresolution;
  690.         break;
  691.     case TIFFTAG_YRESOLUTION:
  692.         *va_arg(ap, float*) = td->td_yresolution;
  693.         break;
  694.     case TIFFTAG_PLANARCONFIG:
  695.         *va_arg(ap, uint16*) = td->td_planarconfig;
  696.         break;
  697.     case TIFFTAG_XPOSITION:
  698.         *va_arg(ap, float*) = td->td_xposition;
  699.         break;
  700.     case TIFFTAG_YPOSITION:
  701.         *va_arg(ap, float*) = td->td_yposition;
  702.         break;
  703.     case TIFFTAG_PAGENAME:
  704.         *va_arg(ap, char**) = td->td_pagename;
  705.         break;
  706.     case TIFFTAG_RESOLUTIONUNIT:
  707.         *va_arg(ap, uint16*) = td->td_resolutionunit;
  708.         break;
  709.     case TIFFTAG_PAGENUMBER:
  710.         *va_arg(ap, uint16*) = td->td_pagenumber[0];
  711.         *va_arg(ap, uint16*) = td->td_pagenumber[1];
  712.         break;
  713.     case TIFFTAG_HALFTONEHINTS:
  714.         *va_arg(ap, uint16*) = td->td_halftonehints[0];
  715.         *va_arg(ap, uint16*) = td->td_halftonehints[1];
  716.         break;
  717.     case TIFFTAG_COLORMAP:
  718.         *va_arg(ap, uint16**) = td->td_colormap[0];
  719.         *va_arg(ap, uint16**) = td->td_colormap[1];
  720.         *va_arg(ap, uint16**) = td->td_colormap[2];
  721.         break;
  722.     case TIFFTAG_STRIPOFFSETS:
  723.     case TIFFTAG_TILEOFFSETS:
  724.         *va_arg(ap, uint32**) = td->td_stripoffset;
  725.         break;
  726.     case TIFFTAG_STRIPBYTECOUNTS:
  727.     case TIFFTAG_TILEBYTECOUNTS:
  728.         *va_arg(ap, uint32**) = td->td_stripbytecount;
  729.         break;
  730.     case TIFFTAG_MATTEING:
  731.         *va_arg(ap, uint16*) =
  732.             (td->td_extrasamples == 1 &&
  733.              td->td_sampleinfo[0] == EXTRASAMPLE_ASSOCALPHA);
  734.         break;
  735.     case TIFFTAG_EXTRASAMPLES:
  736.         *va_arg(ap, uint16*) = td->td_extrasamples;
  737.         *va_arg(ap, uint16**) = td->td_sampleinfo;
  738.         break;
  739.     case TIFFTAG_TILEWIDTH:
  740.         *va_arg(ap, uint32*) = td->td_tilewidth;
  741.         break;
  742.     case TIFFTAG_TILELENGTH:
  743.         *va_arg(ap, uint32*) = td->td_tilelength;
  744.         break;
  745.     case TIFFTAG_TILEDEPTH:
  746.         *va_arg(ap, uint32*) = td->td_tiledepth;
  747.         break;
  748.     case TIFFTAG_DATATYPE:
  749.         switch (td->td_sampleformat) {
  750.         case SAMPLEFORMAT_UINT:
  751.             *va_arg(ap, uint16*) = DATATYPE_UINT;
  752.             break;
  753.         case SAMPLEFORMAT_INT:
  754.             *va_arg(ap, uint16*) = DATATYPE_INT;
  755.             break;
  756.         case SAMPLEFORMAT_IEEEFP:
  757.             *va_arg(ap, uint16*) = DATATYPE_IEEEFP;
  758.             break;
  759.         case SAMPLEFORMAT_VOID:
  760.             *va_arg(ap, uint16*) = DATATYPE_VOID;
  761.             break;
  762.         }
  763.         break;
  764.     case TIFFTAG_SAMPLEFORMAT:
  765.         *va_arg(ap, uint16*) = td->td_sampleformat;
  766.         break;
  767.     case TIFFTAG_IMAGEDEPTH:
  768.         *va_arg(ap, uint32*) = td->td_imagedepth;
  769.         break;
  770.     case TIFFTAG_STONITS:
  771.         *va_arg(ap, double*) = td->td_stonits;
  772.         break;
  773. #if SUBIFD_SUPPORT
  774.     case TIFFTAG_SUBIFD:
  775.         *va_arg(ap, uint16*) = td->td_nsubifd;
  776.         *va_arg(ap, uint32**) = td->td_subifd;
  777.         break;
  778. #endif
  779. #ifdef YCBCR_SUPPORT
  780.     case TIFFTAG_YCBCRCOEFFICIENTS:
  781.         *va_arg(ap, float**) = td->td_ycbcrcoeffs;
  782.         break;
  783.     case TIFFTAG_YCBCRPOSITIONING:
  784.         *va_arg(ap, uint16*) = td->td_ycbcrpositioning;
  785.         break;
  786.     case TIFFTAG_YCBCRSUBSAMPLING:
  787.         *va_arg(ap, uint16*) = td->td_ycbcrsubsampling[0];
  788.         *va_arg(ap, uint16*) = td->td_ycbcrsubsampling[1];
  789.         break;
  790. #endif
  791.     case TIFFTAG_PREDICTOR:
  792.         sp = PredictorState(tif);
  793.         *va_arg(ap, uint16*) = sp->predictor;
  794.         break;
  795. #ifdef COLORIMETRY_SUPPORT
  796.     case TIFFTAG_WHITEPOINT:
  797.         *va_arg(ap, float**) = td->td_whitepoint;
  798.         break;
  799.     case TIFFTAG_PRIMARYCHROMATICITIES:
  800.         *va_arg(ap, float**) = td->td_primarychromas;
  801.         break;
  802.     case TIFFTAG_TRANSFERFUNCTION:
  803.         *va_arg(ap, uint16**) = td->td_transferfunction[0];
  804.         if (td->td_samplesperpixel - td->td_extrasamples > 1) {
  805.             *va_arg(ap, uint16**) = td->td_transferfunction[1];
  806.             *va_arg(ap, uint16**) = td->td_transferfunction[2];
  807.         }
  808.         break;
  809.     case TIFFTAG_REFERENCEBLACKWHITE:
  810.         *va_arg(ap, float**) = td->td_refblackwhite;
  811.         break;
  812. #endif
  813. #ifdef CMYK_SUPPORT
  814.     case TIFFTAG_INKSET:
  815.         *va_arg(ap, uint16*) = td->td_inkset;
  816.         break;
  817.     case TIFFTAG_DOTRANGE:
  818.         *va_arg(ap, uint16*) = td->td_dotrange[0];
  819.         *va_arg(ap, uint16*) = td->td_dotrange[1];
  820.         break;
  821.     case TIFFTAG_INKNAMES:
  822.         *va_arg(ap, char**) = td->td_inknames;
  823.         break;
  824.     case TIFFTAG_NUMBEROFINKS:
  825.         *va_arg(ap, uint16*) = td->td_ninks;
  826.         break;
  827.     case TIFFTAG_TARGETPRINTER:
  828.         *va_arg(ap, char**) = td->td_targetprinter;
  829.         break;
  830. #endif
  831. #ifdef ICC_SUPPORT
  832.     case TIFFTAG_ICCPROFILE:
  833.         *va_arg(ap, uint32*) = td->td_profileLength;
  834.         *va_arg(ap, void**) = td->td_profileData;
  835.         break;
  836. #endif
  837. #ifdef PHOTOSHOP_SUPPORT
  838.      case TIFFTAG_PHOTOSHOP:
  839.          *va_arg(ap, uint32*) = td->td_photoshopLength;
  840.          *va_arg(ap, void**) = td->td_photoshopData;
  841.          break;
  842. #endif
  843. #ifdef IPTC_SUPPORT
  844.      case TIFFTAG_RICHTIFFIPTC:
  845.          *va_arg(ap, uint32*) = td->td_richtiffiptcLength;
  846.          *va_arg(ap, void**) = td->td_richtiffiptcData;
  847.          break;
  848. #endif
  849.      /* Begin Pixar Tags */
  850.      case TIFFTAG_PIXAR_IMAGEFULLWIDTH:
  851.          *va_arg(ap, uint32*) = td->td_imagefullwidth;
  852.          break;
  853.      case TIFFTAG_PIXAR_IMAGEFULLLENGTH:
  854.          *va_arg(ap, uint32*) = td->td_imagefulllength;
  855.          break;
  856.      case TIFFTAG_PIXAR_TEXTUREFORMAT:
  857.          *va_arg(ap, char**) = td->td_textureformat;
  858.          break;
  859.      case TIFFTAG_PIXAR_WRAPMODES:
  860.          *va_arg(ap, char**) = td->td_wrapmodes;
  861.          break;
  862.      case TIFFTAG_PIXAR_FOVCOT:
  863.          *va_arg(ap, float*) = td->td_fovcot;
  864.          break;
  865.      case TIFFTAG_PIXAR_MATRIX_WORLDTOSCREEN:
  866.          *va_arg(ap, float**) = td->td_matrixWorldToScreen;
  867.          break;
  868.      case TIFFTAG_PIXAR_MATRIX_WORLDTOCAMERA:
  869.          *va_arg(ap, float**) = td->td_matrixWorldToCamera;
  870.          break;
  871.      /* End Pixar Tags */
  872.  
  873.     default:
  874.         /*
  875.          * This can happen if multiple images are open with
  876.          * different codecs which have private tags.  The
  877.          * global tag information table may then have tags
  878.          * that are valid for one file but not the other. 
  879.          * If the client tries to get a tag that is not valid
  880.          * for the image's codec then we'll arrive here.
  881.          */
  882.         TIFFError("TIFFGetField",
  883.             "%s: Invalid %stag \"%s\" (not supported by codec)",
  884.             tif->tif_name, isPseudoTag(tag) ? "pseudo-" : "",
  885.             _TIFFFieldWithTag(tif, tag)->field_name);
  886.         break;
  887.     }
  888.     return (1);
  889. }
  890.  
  891. /*
  892.  * Return the value of a field in the
  893.  * internal directory structure.
  894.  */
  895. int
  896. TIFFGetField(TIFF* tif, ttag_t tag, ...)
  897. {
  898.     int status;
  899.     va_list ap;
  900.  
  901.     va_start(ap, tag);
  902.     status = TIFFVGetField(tif, tag, ap);
  903.     va_end(ap);
  904.     return (status);
  905. }
  906.  
  907. /*
  908.  * Like TIFFGetField, but taking a varargs
  909.  * parameter list.  This routine is useful
  910.  * for building higher-level interfaces on
  911.  * top of the library.
  912.  */
  913. int
  914. TIFFVGetField(TIFF* tif, ttag_t tag, va_list ap)
  915. {
  916.     const TIFFFieldInfo* fip = _TIFFFindFieldInfo(tif, tag, TIFF_ANY);
  917.     return (fip && (isPseudoTag(tag) || TIFFFieldSet(tif, fip->field_bit)) ?
  918.         (*tif->tif_vgetfield)(tif, tag, ap) : 0);
  919. }
  920.  
  921. #define    CleanupField(member) {        \
  922.     if (td->member) {            \
  923.     _TIFFfree(tif, td->member);        \
  924.     td->member = 0;            \
  925.     }                    \
  926. }
  927.  
  928. /*
  929.  * Release storage associated with a directory.
  930.  */
  931. void
  932. TIFFFreeDirectory(TIFF* tif)
  933. {
  934.     register TIFFDirectory *td = &tif->tif_dir;
  935.  
  936.     CleanupField(td_colormap[0]);
  937.     CleanupField(td_colormap[1]);
  938.     CleanupField(td_colormap[2]);
  939.     CleanupField(td_documentname);
  940.     CleanupField(td_artist);
  941.     CleanupField(td_datetime);
  942.     CleanupField(td_hostcomputer);
  943.     CleanupField(td_imagedescription);
  944.     CleanupField(td_make);
  945.     CleanupField(td_model);
  946.     CleanupField(td_software);
  947.     CleanupField(td_pagename);
  948.     CleanupField(td_sampleinfo);
  949. #if SUBIFD_SUPPORT
  950.     CleanupField(td_subifd);
  951. #endif
  952. #ifdef YCBCR_SUPPORT
  953.     CleanupField(td_ycbcrcoeffs);
  954. #endif
  955. #ifdef CMYK_SUPPORT
  956.     CleanupField(td_inknames);
  957.     CleanupField(td_targetprinter);
  958. #endif
  959. #ifdef COLORIMETRY_SUPPORT
  960.     CleanupField(td_whitepoint);
  961.     CleanupField(td_primarychromas);
  962.     CleanupField(td_refblackwhite);
  963.     CleanupField(td_transferfunction[0]);
  964.     CleanupField(td_transferfunction[1]);
  965.     CleanupField(td_transferfunction[2]);
  966. #endif
  967. #ifdef ICC_SUPPORT
  968.     CleanupField(td_profileData);
  969. #endif
  970. #ifdef PHOTOSHOP_SUPPORT
  971.     CleanupField(td_photoshopData);
  972. #endif
  973. #ifdef IPTC_SUPPORT
  974.     CleanupField(td_richtiffiptcData);
  975. #endif
  976.     CleanupField(td_stripoffset);
  977.     CleanupField(td_stripbytecount);
  978.      /* Begin Pixar Tags */
  979.      CleanupField(td_textureformat);
  980.      CleanupField(td_wrapmodes);
  981.      CleanupField(td_matrixWorldToScreen);
  982.      CleanupField(td_matrixWorldToCamera);
  983.      /* End Pixar Tags */
  984. }
  985. #undef CleanupField
  986.  
  987. /*
  988.  * Client Tag extension support (from Niles Ritter).
  989.  */
  990. static TIFFExtendProc _TIFFextender = (TIFFExtendProc) NULL;
  991.  
  992. TIFFExtendProc
  993. TIFFSetTagExtender(TIFFExtendProc extender)
  994. {
  995.     TIFFExtendProc prev = _TIFFextender;
  996.     _TIFFextender = extender;
  997.     return (prev);
  998. }
  999.  
  1000. #ifdef PDF_UNUSED
  1001. /*
  1002.  * Setup for a new directory.  Should we automatically call
  1003.  * TIFFWriteDirectory() if the current one is dirty?
  1004.  *
  1005.  * The newly created directory will not exist on the file till
  1006.  * TIFFWriteDirectory(), TIFFFlush() or TIFFClose() is called.
  1007.  */
  1008. int
  1009. TIFFCreateDirectory(TIFF* tif)
  1010. {
  1011.     TIFFDefaultDirectory(tif);
  1012.     tif->tif_diroff = 0;
  1013.     tif->tif_nextdiroff = 0;
  1014.     tif->tif_curoff = 0;
  1015.     tif->tif_row = (uint32) -1;
  1016.     tif->tif_curstrip = (tstrip_t) -1;
  1017.  
  1018.     return 0;
  1019. }
  1020. #endif
  1021.  
  1022. /*
  1023.  * Setup a default directory structure.
  1024.  */
  1025. int
  1026. TIFFDefaultDirectory(TIFF* tif)
  1027. {
  1028.     register TIFFDirectory* td = &tif->tif_dir;
  1029.  
  1030.     _TIFFSetupFieldInfo(tif);
  1031.     _TIFFmemset(td, 0, sizeof (*td));
  1032.     td->td_fillorder = FILLORDER_MSB2LSB;
  1033.     td->td_bitspersample = 1;
  1034.     td->td_threshholding = THRESHHOLD_BILEVEL;
  1035.     td->td_orientation = ORIENTATION_TOPLEFT;
  1036.     td->td_samplesperpixel = 1;
  1037.     td->td_rowsperstrip = (uint32) -1;
  1038.     td->td_tilewidth = (uint32) -1;
  1039.     td->td_tilelength = (uint32) -1;
  1040.     td->td_tiledepth = 1;
  1041.     td->td_resolutionunit = RESUNIT_INCH;
  1042.     td->td_sampleformat = SAMPLEFORMAT_UINT;
  1043.     td->td_imagedepth = 1;
  1044. #ifdef YCBCR_SUPPORT
  1045.     td->td_ycbcrsubsampling[0] = 2;
  1046.     td->td_ycbcrsubsampling[1] = 2;
  1047.     td->td_ycbcrpositioning = YCBCRPOSITION_CENTERED;
  1048. #endif
  1049. #ifdef CMYK_SUPPORT
  1050.     td->td_inkset = INKSET_CMYK;
  1051.     td->td_ninks = 4;
  1052. #endif
  1053.     tif->tif_postdecode = _TIFFNoPostDecode;
  1054.     tif->tif_vsetfield = _TIFFVSetField;
  1055.     tif->tif_vgetfield = _TIFFVGetField;
  1056.     tif->tif_printdir = NULL;
  1057.     /*
  1058.      *  Give client code a chance to install their own
  1059.      *  tag extensions & methods, prior to compression overloads.
  1060.      */
  1061.     if (_TIFFextender)
  1062.         (*_TIFFextender)(tif);
  1063.     (void) TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_NONE);
  1064.     /*
  1065.      * NB: The directory is marked dirty as a result of setting
  1066.      * up the default compression scheme.  However, this really
  1067.      * isn't correct -- we want TIFF_DIRTYDIRECT to be set only
  1068.      * if the user does something.  We could just do the setup
  1069.      * by hand, but it seems better to use the normal mechanism
  1070.      * (i.e. TIFFSetField).
  1071.      */
  1072.     tif->tif_flags &= ~TIFF_DIRTYDIRECT;
  1073.  
  1074.         /*
  1075.          * As per http://bugzilla.remotesensing.org/show_bug.cgi?id=19
  1076.          * we clear the ISTILED flag when setting up a new directory.
  1077.          * Should we also be clearing stuff like INSUBIFD?
  1078.          */
  1079.         tif->tif_flags &= ~TIFF_ISTILED;
  1080.  
  1081.     return (1);
  1082. }
  1083.  
  1084. static int
  1085. TIFFAdvanceDirectory(TIFF* tif, uint32* nextdir, toff_t* off)
  1086. {
  1087.     static const char module[] = "TIFFAdvanceDirectory";
  1088.     uint16 dircount;
  1089.     if (isMapped(tif))
  1090.     {
  1091.         toff_t poff=*nextdir;
  1092.         if (poff+sizeof(uint16) > tif->tif_size)
  1093.         {
  1094.             TIFFError(module, "%s: Error fetching directory count",
  1095.                       tif->tif_name);
  1096.             return (0);
  1097.         }
  1098.         _TIFFmemcpy(&dircount, tif->tif_base+poff, sizeof (uint16));
  1099.         if (tif->tif_flags & TIFF_SWAB)
  1100.             TIFFSwabShort(&dircount);
  1101.         poff+=sizeof (uint16)+dircount*sizeof (TIFFDirEntry);
  1102.         if (off != NULL)
  1103.             *off = poff;
  1104.         if (((toff_t) (poff+sizeof (uint32))) > tif->tif_size)
  1105.         {
  1106.             TIFFError(module, "%s: Error fetching directory link",
  1107.                       tif->tif_name);
  1108.             return (0);
  1109.         }
  1110.         _TIFFmemcpy(nextdir, tif->tif_base+poff, sizeof (uint32));
  1111.         if (tif->tif_flags & TIFF_SWAB)
  1112.             TIFFSwabLong(nextdir);
  1113.         return (1);
  1114.     }
  1115.     else
  1116.     {
  1117.         if (!SeekOK(tif, *nextdir) ||
  1118.             !ReadOK(tif, &dircount, sizeof (uint16))) {
  1119.             TIFFError(module, "%s: Error fetching directory count",
  1120.                       tif->tif_name);
  1121.             return (0);
  1122.         }
  1123.         if (tif->tif_flags & TIFF_SWAB)
  1124.             TIFFSwabShort(&dircount);
  1125.         if (off != NULL)
  1126.             *off = TIFFSeekFile(tif,
  1127.                                 dircount*sizeof (TIFFDirEntry), SEEK_CUR);
  1128.         else
  1129.             (void) TIFFSeekFile(tif,
  1130.                                 dircount*sizeof (TIFFDirEntry), SEEK_CUR);
  1131.         if (!ReadOK(tif, nextdir, sizeof (uint32))) {
  1132.             TIFFError(module, "%s: Error fetching directory link",
  1133.                       tif->tif_name);
  1134.             return (0);
  1135.         }
  1136.         if (tif->tif_flags & TIFF_SWAB)
  1137.             TIFFSwabLong(nextdir);
  1138.         return (1);
  1139.     }
  1140. }
  1141.  
  1142. #ifdef PDF_UNUSED
  1143. /*
  1144.  * Count the number of directories in a file.
  1145.  */
  1146. tdir_t
  1147. TIFFNumberOfDirectories(TIFF* tif)
  1148. {
  1149.     toff_t nextdir = tif->tif_header.tiff_diroff;
  1150.     tdir_t n = 0;
  1151.     
  1152.     while (nextdir != 0 && TIFFAdvanceDirectory(tif, &nextdir, NULL))
  1153.         n++;
  1154.     return (n);
  1155. }
  1156. #endif
  1157.  
  1158. /*
  1159.  * Set the n-th directory as the current directory.
  1160.  * NB: Directories are numbered starting at 0.
  1161.  */
  1162. int
  1163. TIFFSetDirectory(TIFF* tif, tdir_t dirn)
  1164. {
  1165.     toff_t nextdir;
  1166.     tdir_t n;
  1167.  
  1168.     nextdir = tif->tif_header.tiff_diroff;
  1169.     for (n = dirn; n > 0 && nextdir != 0; n--)
  1170.         if (!TIFFAdvanceDirectory(tif, &nextdir, NULL))
  1171.             return (0);
  1172.     tif->tif_nextdiroff = nextdir;
  1173.     /*
  1174.      * Set curdir to the actual directory index.  The
  1175.      * -1 is because TIFFReadDirectory will increment
  1176.      * tif_curdir after successfully reading the directory.
  1177.      */
  1178.     tif->tif_curdir = (dirn - n) - 1;
  1179.     return (TIFFReadDirectory(tif));
  1180. }
  1181.  
  1182. #ifdef PDF_UNUSED
  1183. /*
  1184.  * Set the current directory to be the directory
  1185.  * located at the specified file offset.  This interface
  1186.  * is used mainly to access directories linked with
  1187.  * the SubIFD tag (e.g. thumbnail images).
  1188.  */
  1189. int
  1190. TIFFSetSubDirectory(TIFF* tif, uint32 diroff)
  1191. {
  1192.     tif->tif_nextdiroff = diroff;
  1193.     return (TIFFReadDirectory(tif));
  1194. }
  1195. /*
  1196.  * Return file offset of the current directory.
  1197.  */
  1198. uint32
  1199. TIFFCurrentDirOffset(TIFF* tif)
  1200. {
  1201.     return (tif->tif_diroff);
  1202. }
  1203.  
  1204. /*
  1205.  * Return an indication of whether or not we are
  1206.  * at the last directory in the file.
  1207.  */
  1208. int
  1209. TIFFLastDirectory(TIFF* tif)
  1210. {
  1211.     return (tif->tif_nextdiroff == 0);
  1212. }
  1213. #endif
  1214.  
  1215. /*            [BFC]
  1216.  *
  1217.  * Author: Bruce Cameron <cameron@petris.com>
  1218.  *
  1219.  * Set a table of tags that are to be replaced during directory process by the
  1220.  * 'IGNORE' state - or return TRUE/FALSE for the requested tag such that
  1221.  * 'ReadDirectory' can use the stored information.
  1222.  */
  1223. int
  1224. TIFFReassignTagToIgnore (enum TIFFIgnoreSense task, int TIFFtagID)
  1225. {
  1226.     static int TIFFignoretags [FIELD_LAST];
  1227.     static int tagcount = 0 ;
  1228.     int        i;                    /* Loop index */
  1229.     int        j;                    /* Loop index */
  1230.  
  1231.     switch (task)
  1232.     {
  1233.       case TIS_STORE:
  1234.         if ( tagcount < (FIELD_LAST - 1) )
  1235.         {
  1236.             for ( j = 0 ; j < tagcount ; ++j )
  1237.             {                    /* Do not add duplicate tag */
  1238.                 if ( TIFFignoretags [j] == TIFFtagID )
  1239.                     return (TRUE) ;
  1240.             }
  1241.             TIFFignoretags [tagcount++] = TIFFtagID ;
  1242.             return (TRUE) ;
  1243.         }
  1244.         break ;
  1245.         
  1246.       case TIS_EXTRACT:
  1247.         for ( i = 0 ; i < tagcount ; ++i )
  1248.         {
  1249.             if ( TIFFignoretags [i] == TIFFtagID )
  1250.                 return (TRUE) ;
  1251.         }
  1252.         break;
  1253.         
  1254.       case TIS_EMPTY:
  1255.         tagcount = 0 ;            /* Clear the list */
  1256.         return (TRUE) ;
  1257.         
  1258.       default:
  1259.         break;
  1260.     }
  1261.     
  1262.     return (FALSE);
  1263. }
  1264.